blob: e54b5e50c92715dc1c60376af9c3bbb9ec0ab7e2 [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"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040058
Christoph Hellwig6cbff002009-04-17 10:37:41 +020059/* Mask out flags that are inappropriate for the given type of inode. */
60static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
61{
62 if (S_ISDIR(mode))
63 return flags;
64 else if (S_ISREG(mode))
65 return flags & ~FS_DIRSYNC_FL;
66 else
67 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
68}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040069
Christoph Hellwig6cbff002009-04-17 10:37:41 +020070/*
71 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
72 */
73static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
74{
75 unsigned int iflags = 0;
76
77 if (flags & BTRFS_INODE_SYNC)
78 iflags |= FS_SYNC_FL;
79 if (flags & BTRFS_INODE_IMMUTABLE)
80 iflags |= FS_IMMUTABLE_FL;
81 if (flags & BTRFS_INODE_APPEND)
82 iflags |= FS_APPEND_FL;
83 if (flags & BTRFS_INODE_NODUMP)
84 iflags |= FS_NODUMP_FL;
85 if (flags & BTRFS_INODE_NOATIME)
86 iflags |= FS_NOATIME_FL;
87 if (flags & BTRFS_INODE_DIRSYNC)
88 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000089 if (flags & BTRFS_INODE_NODATACOW)
90 iflags |= FS_NOCOW_FL;
91
92 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
93 iflags |= FS_COMPR_FL;
94 else if (flags & BTRFS_INODE_NOCOMPRESS)
95 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020096
97 return iflags;
98}
99
100/*
101 * Update inode->i_flags based on the btrfs internal flags.
102 */
103void btrfs_update_iflags(struct inode *inode)
104{
105 struct btrfs_inode *ip = BTRFS_I(inode);
106
107 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
108
109 if (ip->flags & BTRFS_INODE_SYNC)
110 inode->i_flags |= S_SYNC;
111 if (ip->flags & BTRFS_INODE_IMMUTABLE)
112 inode->i_flags |= S_IMMUTABLE;
113 if (ip->flags & BTRFS_INODE_APPEND)
114 inode->i_flags |= S_APPEND;
115 if (ip->flags & BTRFS_INODE_NOATIME)
116 inode->i_flags |= S_NOATIME;
117 if (ip->flags & BTRFS_INODE_DIRSYNC)
118 inode->i_flags |= S_DIRSYNC;
119}
120
121/*
122 * Inherit flags from the parent inode.
123 *
Josef Bacike27425d2011-09-27 11:01:30 -0400124 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200125 */
126void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
127{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400128 unsigned int flags;
129
130 if (!dir)
131 return;
132
133 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200134
Josef Bacike27425d2011-09-27 11:01:30 -0400135 if (flags & BTRFS_INODE_NOCOMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
138 } else if (flags & BTRFS_INODE_COMPRESS) {
139 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
140 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
141 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200142
Josef Bacike27425d2011-09-27 11:01:30 -0400143 if (flags & BTRFS_INODE_NODATACOW)
144 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
145
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146 btrfs_update_iflags(inode);
147}
148
149static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
150{
151 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
152 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
153
154 if (copy_to_user(arg, &flags, sizeof(flags)))
155 return -EFAULT;
156 return 0;
157}
158
Liu Bo75e7cb72011-03-22 10:12:20 +0000159static int check_flags(unsigned int flags)
160{
161 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
162 FS_NOATIME_FL | FS_NODUMP_FL | \
163 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000164 FS_NOCOMP_FL | FS_COMPR_FL |
165 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000166 return -EOPNOTSUPP;
167
168 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
169 return -EINVAL;
170
Liu Bo75e7cb72011-03-22 10:12:20 +0000171 return 0;
172}
173
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200174static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
175{
176 struct inode *inode = file->f_path.dentry->d_inode;
177 struct btrfs_inode *ip = BTRFS_I(inode);
178 struct btrfs_root *root = ip->root;
179 struct btrfs_trans_handle *trans;
180 unsigned int flags, oldflags;
181 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800182 u64 ip_oldflags;
183 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600184 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185
Li Zefanb83cc962010-12-20 16:04:08 +0800186 if (btrfs_root_readonly(root))
187 return -EROFS;
188
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189 if (copy_from_user(&flags, arg, sizeof(flags)))
190 return -EFAULT;
191
Liu Bo75e7cb72011-03-22 10:12:20 +0000192 ret = check_flags(flags);
193 if (ret)
194 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200195
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700196 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200197 return -EACCES;
198
Jan Karae7848682012-06-12 16:20:32 +0200199 ret = mnt_want_write_file(file);
200 if (ret)
201 return ret;
202
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203 mutex_lock(&inode->i_mutex);
204
Li Zefanf062abf2011-12-29 13:36:45 +0800205 ip_oldflags = ip->flags;
206 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600207 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800208
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200209 flags = btrfs_mask_flags(inode->i_mode, flags);
210 oldflags = btrfs_flags_to_ioctl(ip->flags);
211 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
212 if (!capable(CAP_LINUX_IMMUTABLE)) {
213 ret = -EPERM;
214 goto out_unlock;
215 }
216 }
217
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200218 if (flags & FS_SYNC_FL)
219 ip->flags |= BTRFS_INODE_SYNC;
220 else
221 ip->flags &= ~BTRFS_INODE_SYNC;
222 if (flags & FS_IMMUTABLE_FL)
223 ip->flags |= BTRFS_INODE_IMMUTABLE;
224 else
225 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
226 if (flags & FS_APPEND_FL)
227 ip->flags |= BTRFS_INODE_APPEND;
228 else
229 ip->flags &= ~BTRFS_INODE_APPEND;
230 if (flags & FS_NODUMP_FL)
231 ip->flags |= BTRFS_INODE_NODUMP;
232 else
233 ip->flags &= ~BTRFS_INODE_NODUMP;
234 if (flags & FS_NOATIME_FL)
235 ip->flags |= BTRFS_INODE_NOATIME;
236 else
237 ip->flags &= ~BTRFS_INODE_NOATIME;
238 if (flags & FS_DIRSYNC_FL)
239 ip->flags |= BTRFS_INODE_DIRSYNC;
240 else
241 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600242 if (flags & FS_NOCOW_FL) {
243 if (S_ISREG(mode)) {
244 /*
245 * It's safe to turn csums off here, no extents exist.
246 * Otherwise we want the flag to reflect the real COW
247 * status of the file and will not set it.
248 */
249 if (inode->i_size == 0)
250 ip->flags |= BTRFS_INODE_NODATACOW
251 | BTRFS_INODE_NODATASUM;
252 } else {
253 ip->flags |= BTRFS_INODE_NODATACOW;
254 }
255 } else {
256 /*
257 * Revert back under same assuptions as above
258 */
259 if (S_ISREG(mode)) {
260 if (inode->i_size == 0)
261 ip->flags &= ~(BTRFS_INODE_NODATACOW
262 | BTRFS_INODE_NODATASUM);
263 } else {
264 ip->flags &= ~BTRFS_INODE_NODATACOW;
265 }
266 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200267
Liu Bo75e7cb72011-03-22 10:12:20 +0000268 /*
269 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
270 * flag may be changed automatically if compression code won't make
271 * things smaller.
272 */
273 if (flags & FS_NOCOMP_FL) {
274 ip->flags &= ~BTRFS_INODE_COMPRESS;
275 ip->flags |= BTRFS_INODE_NOCOMPRESS;
276 } else if (flags & FS_COMPR_FL) {
277 ip->flags |= BTRFS_INODE_COMPRESS;
278 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000279 } else {
280 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000281 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200282
Li Zefan4da6f1a2011-12-29 13:39:50 +0800283 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800284 if (IS_ERR(trans)) {
285 ret = PTR_ERR(trans);
286 goto out_drop;
287 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200288
Li Zefan306424c2011-12-14 20:12:02 -0500289 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400290 inode_inc_iversion(inode);
Li Zefan306424c2011-12-14 20:12:02 -0500291 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200292 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200293
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200294 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800295 out_drop:
296 if (ret) {
297 ip->flags = ip_oldflags;
298 inode->i_flags = i_oldflags;
299 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301 out_unlock:
302 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200303 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000304 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200305}
306
307static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
308{
309 struct inode *inode = file->f_path.dentry->d_inode;
310
311 return put_user(inode->i_generation, arg);
312}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400313
Li Dongyangf7039b12011-03-24 10:24:28 +0000314static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
315{
Al Viro815745c2011-11-17 15:40:49 -0500316 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000317 struct btrfs_device *device;
318 struct request_queue *q;
319 struct fstrim_range range;
320 u64 minlen = ULLONG_MAX;
321 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500322 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000323 int ret;
324
325 if (!capable(CAP_SYS_ADMIN))
326 return -EPERM;
327
Xiao Guangrong1f781602011-04-20 10:09:16 +0000328 rcu_read_lock();
329 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
330 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000331 if (!device->bdev)
332 continue;
333 q = bdev_get_queue(device->bdev);
334 if (blk_queue_discard(q)) {
335 num_devices++;
336 minlen = min((u64)q->limits.discard_granularity,
337 minlen);
338 }
339 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000340 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200341
Li Dongyangf7039b12011-03-24 10:24:28 +0000342 if (!num_devices)
343 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000344 if (copy_from_user(&range, arg, sizeof(range)))
345 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000346 if (range.start > total_bytes ||
347 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200348 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000349
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200350 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000351 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500352 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000353 if (ret < 0)
354 return ret;
355
356 if (copy_to_user(arg, &range, sizeof(range)))
357 return -EFAULT;
358
359 return 0;
360}
361
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400362static noinline int create_subvol(struct btrfs_root *root,
363 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400364 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200365 u64 *async_transid,
366 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400367{
368 struct btrfs_trans_handle *trans;
369 struct btrfs_key key;
370 struct btrfs_root_item root_item;
371 struct btrfs_inode_item *inode_item;
372 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400373 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400374 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000375 struct inode *dir;
Alexander Block8ea05e32012-07-25 17:35:53 +0200376 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400377 int ret;
378 int err;
379 u64 objectid;
380 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500381 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200382 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400383
Li Zefan581bb052011-04-20 10:06:11 +0800384 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400385 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400386 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000387
388 dir = parent->d_inode;
389
Josef Bacik9ed74f22009-09-11 16:12:44 -0400390 /*
391 * 1 - inode item
392 * 2 - refs
393 * 1 - root item
394 * 2 - dir items
395 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400396 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400397 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400398 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400399
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200400 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
401 inherit ? *inherit : NULL);
402 if (ret)
403 goto fail;
404
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400405 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200406 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400407 if (IS_ERR(leaf)) {
408 ret = PTR_ERR(leaf);
409 goto fail;
410 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400411
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413 btrfs_set_header_bytenr(leaf, leaf->start);
414 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400415 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400416 btrfs_set_header_owner(leaf, objectid);
417
418 write_extent_buffer(leaf, root->fs_info->fsid,
419 (unsigned long)btrfs_header_fsid(leaf),
420 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400421 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
422 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
423 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400424 btrfs_mark_buffer_dirty(leaf);
425
Alexander Block8ea05e32012-07-25 17:35:53 +0200426 memset(&root_item, 0, sizeof(root_item));
427
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400428 inode_item = &root_item.inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400429 inode_item->generation = cpu_to_le64(1);
430 inode_item->size = cpu_to_le64(3);
431 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400432 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
434
Li Zefan08fe4db2011-03-28 02:01:25 +0000435 root_item.flags = 0;
436 root_item.byte_limit = 0;
437 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
438
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400439 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400440 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400441 btrfs_set_root_level(&root_item, 0);
442 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000443 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400444 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445
Alexander Block8ea05e32012-07-25 17:35:53 +0200446 btrfs_set_root_generation_v2(&root_item,
447 btrfs_root_generation(&root_item));
448 uuid_le_gen(&new_uuid);
449 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
450 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600451 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200452 root_item.ctime = root_item.otime;
453 btrfs_set_root_ctransid(&root_item, trans->transid);
454 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455
Chris Mason925baed2008-06-25 16:01:30 -0400456 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457 free_extent_buffer(leaf);
458 leaf = NULL;
459
460 btrfs_set_root_dirid(&root_item, new_dirid);
461
462 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400463 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
465 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
466 &root_item);
467 if (ret)
468 goto fail;
469
Yan, Zheng76dda932009-09-21 16:00:26 -0400470 key.offset = (u64)-1;
471 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100472 if (IS_ERR(new_root)) {
473 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
474 ret = PTR_ERR(new_root);
475 goto fail;
476 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400477
478 btrfs_record_root_in_trans(trans, new_root);
479
Josef Bacikd82a6f12011-05-11 15:26:06 -0400480 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700481 if (ret) {
482 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100483 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700484 goto fail;
485 }
486
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400487 /*
488 * insert the directory item
489 */
Chris Mason3de45862008-11-17 21:02:50 -0500490 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100491 if (ret) {
492 btrfs_abort_transaction(trans, root, ret);
493 goto fail;
494 }
Chris Mason3de45862008-11-17 21:02:50 -0500495
496 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800497 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500498 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100499 if (ret) {
500 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400501 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100502 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500503
Yan Zheng52c26172009-01-05 15:43:43 -0500504 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
505 ret = btrfs_update_inode(trans, root, dir);
506 BUG_ON(ret);
507
Chris Mason0660b5a2008-11-17 20:37:39 -0500508 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400509 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800510 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400511
Chris Mason0660b5a2008-11-17 20:37:39 -0500512 BUG_ON(ret);
513
Yan, Zheng76dda932009-09-21 16:00:26 -0400514 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400516 if (async_transid) {
517 *async_transid = trans->transid;
518 err = btrfs_commit_transaction_async(trans, root, 1);
519 } else {
520 err = btrfs_commit_transaction(trans, root);
521 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400522 if (err && !ret)
523 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524 return ret;
525}
526
Sage Weil72fd0322010-10-29 15:41:32 -0400527static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800528 char *name, int namelen, u64 *async_transid,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200529 bool readonly, struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400530{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000531 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400532 struct btrfs_pending_snapshot *pending_snapshot;
533 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000534 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400535
536 if (!root->ref_cows)
537 return -EINVAL;
538
Yan, Zhenga22285a2010-05-16 10:48:46 -0400539 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
540 if (!pending_snapshot)
541 return -ENOMEM;
542
Miao Xie66d8f3d2012-09-06 04:02:28 -0600543 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
544 BTRFS_BLOCK_RSV_TEMP);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400545 pending_snapshot->dentry = dentry;
546 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800547 pending_snapshot->readonly = readonly;
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200548 if (inherit) {
549 pending_snapshot->inherit = *inherit;
550 *inherit = NULL; /* take responsibility to free it */
551 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400552
Miao Xie48c03c42012-09-06 04:03:56 -0600553 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400554 if (IS_ERR(trans)) {
555 ret = PTR_ERR(trans);
556 goto fail;
557 }
558
559 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
560 BUG_ON(ret);
561
Josef Bacik83515832011-06-14 15:16:14 -0400562 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400563 list_add(&pending_snapshot->list,
564 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400565 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400566 if (async_transid) {
567 *async_transid = trans->transid;
568 ret = btrfs_commit_transaction_async(trans,
569 root->fs_info->extent_root, 1);
570 } else {
571 ret = btrfs_commit_transaction(trans,
572 root->fs_info->extent_root);
573 }
Liu Bo109f2362012-11-05 12:42:09 +0000574 if (ret) {
575 /* cleanup_transaction has freed this for us */
576 if (trans->aborted)
577 pending_snapshot = NULL;
Josef Bacikc37b2b62012-10-22 15:51:44 -0400578 goto fail;
Liu Bo109f2362012-11-05 12:42:09 +0000579 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400580
581 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400582 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000583 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400584
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500585 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
586 if (ret)
587 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400588
Al Viro2fbe8c82011-07-16 21:38:06 -0400589 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000590 if (IS_ERR(inode)) {
591 ret = PTR_ERR(inode);
592 goto fail;
593 }
594 BUG_ON(!inode);
595 d_instantiate(dentry, inode);
596 ret = 0;
597fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400598 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400599 return ret;
600}
601
Sage Weil4260f7c2010-10-29 15:46:43 -0400602/* copy of check_sticky in fs/namei.c()
603* It's inline, so penalty for filesystems that don't use sticky bit is
604* minimal.
605*/
606static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
607{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800608 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400609
610 if (!(dir->i_mode & S_ISVTX))
611 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800612 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400613 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800614 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400615 return 0;
616 return !capable(CAP_FOWNER);
617}
618
619/* copy of may_delete in fs/namei.c()
620 * Check whether we can remove a link victim from directory dir, check
621 * whether the type of victim is right.
622 * 1. We can't do it if dir is read-only (done in permission())
623 * 2. We should have write and exec permissions on dir
624 * 3. We can't remove anything from append-only dir
625 * 4. We can't do anything with immutable dir (done in permission())
626 * 5. If the sticky bit on dir is set we should either
627 * a. be owner of dir, or
628 * b. be owner of victim, or
629 * c. have CAP_FOWNER capability
630 * 6. If the victim is append-only or immutable we can't do antyhing with
631 * links pointing to it.
632 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
633 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
634 * 9. We can't remove a root or mountpoint.
635 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
636 * nfs_async_unlink().
637 */
638
639static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
640{
641 int error;
642
643 if (!victim->d_inode)
644 return -ENOENT;
645
646 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400647 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400648
649 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
650 if (error)
651 return error;
652 if (IS_APPEND(dir))
653 return -EPERM;
654 if (btrfs_check_sticky(dir, victim->d_inode)||
655 IS_APPEND(victim->d_inode)||
656 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
657 return -EPERM;
658 if (isdir) {
659 if (!S_ISDIR(victim->d_inode->i_mode))
660 return -ENOTDIR;
661 if (IS_ROOT(victim))
662 return -EBUSY;
663 } else if (S_ISDIR(victim->d_inode->i_mode))
664 return -EISDIR;
665 if (IS_DEADDIR(dir))
666 return -ENOENT;
667 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
668 return -EBUSY;
669 return 0;
670}
671
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400672/* copy of may_create in fs/namei.c() */
673static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
674{
675 if (child->d_inode)
676 return -EEXIST;
677 if (IS_DEADDIR(dir))
678 return -ENOENT;
679 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
680}
681
682/*
683 * Create a new subvolume below @parent. This is largely modeled after
684 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
685 * inside this filesystem so it's quite a bit simpler.
686 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400687static noinline int btrfs_mksubvol(struct path *parent,
688 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400689 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200690 u64 *async_transid, bool readonly,
691 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400692{
Yan, Zheng76dda932009-09-21 16:00:26 -0400693 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400694 struct dentry *dentry;
695 int error;
696
Yan, Zheng76dda932009-09-21 16:00:26 -0400697 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400698
699 dentry = lookup_one_len(name, parent->dentry, namelen);
700 error = PTR_ERR(dentry);
701 if (IS_ERR(dentry))
702 goto out_unlock;
703
704 error = -EEXIST;
705 if (dentry->d_inode)
706 goto out_dput;
707
Yan, Zheng76dda932009-09-21 16:00:26 -0400708 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400709 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600710 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400711
Yan, Zheng76dda932009-09-21 16:00:26 -0400712 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
713
714 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
715 goto out_up_read;
716
Chris Mason3de45862008-11-17 21:02:50 -0500717 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200718 error = create_snapshot(snap_src, dentry, name, namelen,
719 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500720 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400721 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200722 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500723 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400724 if (!error)
725 fsnotify_mkdir(dir, dentry);
726out_up_read:
727 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400728out_dput:
729 dput(dentry);
730out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400731 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400732 return error;
733}
734
Chris Mason4cb53002011-05-24 15:35:30 -0400735/*
736 * When we're defragging a range, we don't want to kick it off again
737 * if it is really just waiting for delalloc to send it down.
738 * If we find a nice big extent or delalloc range for the bytes in the
739 * file you want to defrag, we return 0 to let you know to skip this
740 * part of the file
741 */
742static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
743{
744 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
745 struct extent_map *em = NULL;
746 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
747 u64 end;
748
749 read_lock(&em_tree->lock);
750 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
751 read_unlock(&em_tree->lock);
752
753 if (em) {
754 end = extent_map_end(em);
755 free_extent_map(em);
756 if (end - offset > thresh)
757 return 0;
758 }
759 /* if we already have a nice delalloc here, just stop */
760 thresh /= 2;
761 end = count_range_bits(io_tree, &offset, offset + thresh,
762 thresh, EXTENT_DELALLOC, 1);
763 if (end >= thresh)
764 return 0;
765 return 1;
766}
767
768/*
769 * helper function to walk through a file and find extents
770 * newer than a specific transid, and smaller than thresh.
771 *
772 * This is used by the defragging code to find new and small
773 * extents
774 */
775static int find_new_extents(struct btrfs_root *root,
776 struct inode *inode, u64 newer_than,
777 u64 *off, int thresh)
778{
779 struct btrfs_path *path;
780 struct btrfs_key min_key;
781 struct btrfs_key max_key;
782 struct extent_buffer *leaf;
783 struct btrfs_file_extent_item *extent;
784 int type;
785 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000786 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400787
788 path = btrfs_alloc_path();
789 if (!path)
790 return -ENOMEM;
791
David Sterbaa4689d22011-05-31 17:08:14 +0000792 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400793 min_key.type = BTRFS_EXTENT_DATA_KEY;
794 min_key.offset = *off;
795
David Sterbaa4689d22011-05-31 17:08:14 +0000796 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400797 max_key.type = (u8)-1;
798 max_key.offset = (u64)-1;
799
800 path->keep_locks = 1;
801
802 while(1) {
803 ret = btrfs_search_forward(root, &min_key, &max_key,
804 path, 0, newer_than);
805 if (ret != 0)
806 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000807 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400808 goto none;
809 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
810 goto none;
811
812 leaf = path->nodes[0];
813 extent = btrfs_item_ptr(leaf, path->slots[0],
814 struct btrfs_file_extent_item);
815
816 type = btrfs_file_extent_type(leaf, extent);
817 if (type == BTRFS_FILE_EXTENT_REG &&
818 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
819 check_defrag_in_cache(inode, min_key.offset, thresh)) {
820 *off = min_key.offset;
821 btrfs_free_path(path);
822 return 0;
823 }
824
825 if (min_key.offset == (u64)-1)
826 goto none;
827
828 min_key.offset++;
829 btrfs_release_path(path);
830 }
831none:
832 btrfs_free_path(path);
833 return -ENOENT;
834}
835
Li Zefan6c282eb2012-06-11 16:03:35 +0800836static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400837{
838 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800840 struct extent_map *em;
841 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500842
843 /*
844 * hopefully we have this extent in the tree already, try without
845 * the full extent lock
846 */
847 read_lock(&em_tree->lock);
848 em = lookup_extent_mapping(em_tree, start, len);
849 read_unlock(&em_tree->lock);
850
851 if (!em) {
852 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100853 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500854 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100855 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500856
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000857 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800858 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500859 }
860
Li Zefan6c282eb2012-06-11 16:03:35 +0800861 return em;
862}
863
864static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
865{
866 struct extent_map *next;
867 bool ret = true;
868
869 /* this is the last extent */
870 if (em->start + em->len >= i_size_read(inode))
871 return false;
872
873 next = defrag_lookup_extent(inode, em->start + em->len);
874 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
875 ret = false;
876
877 free_extent_map(next);
878 return ret;
879}
880
881static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400882 u64 *last_len, u64 *skip, u64 *defrag_end,
883 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800884{
885 struct extent_map *em;
886 int ret = 1;
887 bool next_mergeable = true;
888
889 /*
890 * make sure that once we start defragging an extent, we keep on
891 * defragging it
892 */
893 if (start < *defrag_end)
894 return 1;
895
896 *skip = 0;
897
898 em = defrag_lookup_extent(inode, start);
899 if (!em)
900 return 0;
901
Chris Mason940100a2010-03-10 10:52:59 -0500902 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400903 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500904 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400905 goto out;
906 }
907
Li Zefan6c282eb2012-06-11 16:03:35 +0800908 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500909
910 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800911 * we hit a real extent, if it is big or the next extent is not a
912 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500913 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400914 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800915 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500916 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400917out:
Chris Mason940100a2010-03-10 10:52:59 -0500918 /*
919 * last_len ends up being a counter of how many bytes we've defragged.
920 * every time we choose not to defrag an extent, we reset *last_len
921 * so that the next tiny extent will force a defrag.
922 *
923 * The end result of this is that tiny extents before a single big
924 * extent will force at least part of that big extent to be defragged.
925 */
926 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500927 *defrag_end = extent_map_end(em);
928 } else {
929 *last_len = 0;
930 *skip = extent_map_end(em);
931 *defrag_end = 0;
932 }
933
934 free_extent_map(em);
935 return ret;
936}
937
Chris Mason4cb53002011-05-24 15:35:30 -0400938/*
939 * it doesn't do much good to defrag one or two pages
940 * at a time. This pulls in a nice chunk of pages
941 * to COW and defrag.
942 *
943 * It also makes sure the delalloc code has enough
944 * dirty data to avoid making new small extents as part
945 * of the defrag
946 *
947 * It's a good idea to start RA on this range
948 * before calling this.
949 */
950static int cluster_pages_for_defrag(struct inode *inode,
951 struct page **pages,
952 unsigned long start_index,
953 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400954{
Chris Mason4cb53002011-05-24 15:35:30 -0400955 unsigned long file_end;
956 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400957 u64 page_start;
958 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400959 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400960 int ret;
961 int i;
962 int i_done;
963 struct btrfs_ordered_extent *ordered;
964 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800965 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400966 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400967
Chris Mason4cb53002011-05-24 15:35:30 -0400968 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400969 if (!isize || start_index > file_end)
970 return 0;
971
972 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400973
974 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400975 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400976 if (ret)
977 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400978 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800979 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400980
981 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400982 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400983 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800984again:
Josef Bacika94733d2011-07-11 10:47:06 -0400985 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800986 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400987 if (!page)
988 break;
989
Miao Xie600a45e2012-02-16 15:01:24 +0800990 page_start = page_offset(page);
991 page_end = page_start + PAGE_CACHE_SIZE - 1;
992 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100993 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800994 ordered = btrfs_lookup_ordered_extent(inode,
995 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100996 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800997 if (!ordered)
998 break;
999
1000 unlock_page(page);
1001 btrfs_start_ordered_extent(inode, ordered, 1);
1002 btrfs_put_ordered_extent(ordered);
1003 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001004 /*
1005 * we unlocked the page above, so we need check if
1006 * it was released or not.
1007 */
1008 if (page->mapping != inode->i_mapping) {
1009 unlock_page(page);
1010 page_cache_release(page);
1011 goto again;
1012 }
Miao Xie600a45e2012-02-16 15:01:24 +08001013 }
1014
Chris Mason4cb53002011-05-24 15:35:30 -04001015 if (!PageUptodate(page)) {
1016 btrfs_readpage(NULL, page);
1017 lock_page(page);
1018 if (!PageUptodate(page)) {
1019 unlock_page(page);
1020 page_cache_release(page);
1021 ret = -EIO;
1022 break;
1023 }
1024 }
Miao Xie600a45e2012-02-16 15:01:24 +08001025
Miao Xie600a45e2012-02-16 15:01:24 +08001026 if (page->mapping != inode->i_mapping) {
1027 unlock_page(page);
1028 page_cache_release(page);
1029 goto again;
1030 }
1031
Chris Mason4cb53002011-05-24 15:35:30 -04001032 pages[i] = page;
1033 i_done++;
1034 }
1035 if (!i_done || ret)
1036 goto out;
1037
1038 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1039 goto out;
1040
1041 /*
1042 * so now we have a nice long stream of locked
1043 * and up to date pages, lets wait on them
1044 */
1045 for (i = 0; i < i_done; i++)
1046 wait_on_page_writeback(pages[i]);
1047
1048 page_start = page_offset(pages[0]);
1049 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1050
1051 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001052 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001053 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1054 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001055 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1056 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001057
Liu Bo1f12bd02012-03-29 09:57:44 -04001058 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001059 spin_lock(&BTRFS_I(inode)->lock);
1060 BTRFS_I(inode)->outstanding_extents++;
1061 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001062 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001063 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001064 }
1065
1066
Liu Bo9e8a4a82012-09-05 19:10:51 -06001067 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1068 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001069
1070 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1071 page_start, page_end - 1, &cached_state,
1072 GFP_NOFS);
1073
1074 for (i = 0; i < i_done; i++) {
1075 clear_page_dirty_for_io(pages[i]);
1076 ClearPageChecked(pages[i]);
1077 set_page_extent_mapped(pages[i]);
1078 set_page_dirty(pages[i]);
1079 unlock_page(pages[i]);
1080 page_cache_release(pages[i]);
1081 }
1082 return i_done;
1083out:
1084 for (i = 0; i < i_done; i++) {
1085 unlock_page(pages[i]);
1086 page_cache_release(pages[i]);
1087 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001088 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001089 return ret;
1090
1091}
1092
1093int btrfs_defrag_file(struct inode *inode, struct file *file,
1094 struct btrfs_ioctl_defrag_range_args *range,
1095 u64 newer_than, unsigned long max_to_defrag)
1096{
1097 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001098 struct file_ra_state *ra = NULL;
1099 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001100 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001101 u64 last_len = 0;
1102 u64 skip = 0;
1103 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001104 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001105 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001106 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001107 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001108 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001109 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001110 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001111 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1112 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001113 u64 new_align = ~((u64)128 * 1024 - 1);
1114 struct page **pages = NULL;
1115
1116 if (extent_thresh == 0)
1117 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001118
1119 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1120 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1121 return -EINVAL;
1122 if (range->compress_type)
1123 compress_type = range->compress_type;
1124 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001125
Li Zefan151a31b2011-09-02 15:56:39 +08001126 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001127 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001128
Chris Mason4cb53002011-05-24 15:35:30 -04001129 /*
1130 * if we were not given a file, allocate a readahead
1131 * context
1132 */
1133 if (!file) {
1134 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1135 if (!ra)
1136 return -ENOMEM;
1137 file_ra_state_init(ra, inode->i_mapping);
1138 } else {
1139 ra = &file->f_ra;
1140 }
1141
Li Zefan008873e2011-09-02 15:57:07 +08001142 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001143 GFP_NOFS);
1144 if (!pages) {
1145 ret = -ENOMEM;
1146 goto out_ra;
1147 }
1148
1149 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001150 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001151 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001152 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1153 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001154 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001155 }
1156
Chris Mason4cb53002011-05-24 15:35:30 -04001157 if (newer_than) {
1158 ret = find_new_extents(root, inode, newer_than,
1159 &newer_off, 64 * 1024);
1160 if (!ret) {
1161 range->start = newer_off;
1162 /*
1163 * we always align our defrag to help keep
1164 * the extents in the file evenly spaced
1165 */
1166 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001167 } else
1168 goto out_ra;
1169 } else {
1170 i = range->start >> PAGE_CACHE_SHIFT;
1171 }
1172 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001173 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001174
Li Zefan2a0f7f52011-10-10 15:43:34 -04001175 /*
1176 * make writeback starts from i, so the defrag range can be
1177 * written sequentially.
1178 */
1179 if (i < inode->i_mapping->writeback_index)
1180 inode->i_mapping->writeback_index = i;
1181
Chris Masonf7f43cc2011-10-11 11:41:40 -04001182 while (i <= last_index && defrag_count < max_to_defrag &&
1183 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1184 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001185 /*
1186 * make sure we stop running if someone unmounts
1187 * the FS
1188 */
1189 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1190 break;
1191
Liu Bo66c26892012-03-29 09:57:45 -04001192 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001193 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001194 &defrag_end, range->flags &
1195 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001196 unsigned long next;
1197 /*
1198 * the should_defrag function tells us how much to skip
1199 * bump our counter by the suggested amount
1200 */
1201 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1202 i = max(i + 1, next);
1203 continue;
1204 }
Li Zefan008873e2011-09-02 15:57:07 +08001205
1206 if (!newer_than) {
1207 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1208 PAGE_CACHE_SHIFT) - i;
1209 cluster = min(cluster, max_cluster);
1210 } else {
1211 cluster = max_cluster;
1212 }
1213
Chris Mason1e701a32010-03-11 09:42:04 -05001214 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001215 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001216
Li Zefan008873e2011-09-02 15:57:07 +08001217 if (i + cluster > ra_index) {
1218 ra_index = max(i, ra_index);
1219 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1220 cluster);
1221 ra_index += max_cluster;
1222 }
Chris Mason940100a2010-03-10 10:52:59 -05001223
Liu Boecb8bea2012-03-29 09:57:44 -04001224 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001225 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001226 if (ret < 0) {
1227 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001228 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001229 }
Chris Mason940100a2010-03-10 10:52:59 -05001230
Chris Mason4cb53002011-05-24 15:35:30 -04001231 defrag_count += ret;
1232 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001233 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001234
1235 if (newer_than) {
1236 if (newer_off == (u64)-1)
1237 break;
1238
Liu Boe1f041e2012-03-29 09:57:45 -04001239 if (ret > 0)
1240 i += ret;
1241
Chris Mason4cb53002011-05-24 15:35:30 -04001242 newer_off = max(newer_off + 1,
1243 (u64)i << PAGE_CACHE_SHIFT);
1244
1245 ret = find_new_extents(root, inode,
1246 newer_than, &newer_off,
1247 64 * 1024);
1248 if (!ret) {
1249 range->start = newer_off;
1250 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001251 } else {
1252 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001253 }
Chris Mason4cb53002011-05-24 15:35:30 -04001254 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001255 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001256 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001257 last_len += ret << PAGE_CACHE_SHIFT;
1258 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001259 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001260 last_len = 0;
1261 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001262 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001263 }
1264
Chris Mason1e701a32010-03-11 09:42:04 -05001265 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1266 filemap_flush(inode->i_mapping);
1267
1268 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1269 /* the filemap_flush will queue IO into the worker threads, but
1270 * we have to make sure the IO is actually started and that
1271 * ordered extents get created before we return
1272 */
1273 atomic_inc(&root->fs_info->async_submit_draining);
1274 while (atomic_read(&root->fs_info->nr_async_submits) ||
1275 atomic_read(&root->fs_info->async_delalloc_pages)) {
1276 wait_event(root->fs_info->async_submit_wait,
1277 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1278 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1279 }
1280 atomic_dec(&root->fs_info->async_submit_draining);
1281
1282 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001283 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001284 mutex_unlock(&inode->i_mutex);
1285 }
1286
Li Zefan1a419d82010-10-25 15:12:50 +08001287 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001288 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001289 }
1290
Diego Calleja60ccf822011-09-01 16:33:57 +02001291 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001292
Chris Mason4cb53002011-05-24 15:35:30 -04001293out_ra:
1294 if (!file)
1295 kfree(ra);
1296 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001297 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001298}
1299
Yan, Zheng76dda932009-09-21 16:00:26 -04001300static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1301 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302{
1303 u64 new_size;
1304 u64 old_size;
1305 u64 devid = 1;
1306 struct btrfs_ioctl_vol_args *vol_args;
1307 struct btrfs_trans_handle *trans;
1308 struct btrfs_device *device = NULL;
1309 char *sizestr;
1310 char *devstr = NULL;
1311 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001312 int mod = 0;
1313
Yan Zhengc146afa2008-11-12 14:34:12 -05001314 if (root->fs_info->sb->s_flags & MS_RDONLY)
1315 return -EROFS;
1316
Chris Masone441d542009-01-05 16:57:23 -05001317 if (!capable(CAP_SYS_ADMIN))
1318 return -EPERM;
1319
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001320 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1321 1)) {
1322 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
1323 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001324 }
1325
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001326 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001327 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001328 if (IS_ERR(vol_args)) {
1329 ret = PTR_ERR(vol_args);
1330 goto out;
1331 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001332
1333 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001334
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001335 sizestr = vol_args->name;
1336 devstr = strchr(sizestr, ':');
1337 if (devstr) {
1338 char *end;
1339 sizestr = devstr + 1;
1340 *devstr = '\0';
1341 devstr = vol_args->name;
1342 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001343 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001344 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001345 }
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001346 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001347 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001348 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001349 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001350 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001351 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001352 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001353 if (device->fs_devices && device->fs_devices->seeding) {
1354 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001355 "seeding device %llu\n",
1356 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001357 ret = -EINVAL;
1358 goto out_free;
1359 }
1360
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001361 if (!strcmp(sizestr, "max"))
1362 new_size = device->bdev->bd_inode->i_size;
1363 else {
1364 if (sizestr[0] == '-') {
1365 mod = -1;
1366 sizestr++;
1367 } else if (sizestr[0] == '+') {
1368 mod = 1;
1369 sizestr++;
1370 }
Akinobu Mita91748462010-02-28 10:59:11 +00001371 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372 if (new_size == 0) {
1373 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001374 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001375 }
1376 }
1377
Stefan Behrens63a212a2012-11-05 18:29:28 +01001378 if (device->is_tgtdev_for_dev_replace) {
1379 ret = -EINVAL;
1380 goto out_free;
1381 }
1382
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383 old_size = device->total_bytes;
1384
1385 if (mod < 0) {
1386 if (new_size > old_size) {
1387 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001388 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001389 }
1390 new_size = old_size - new_size;
1391 } else if (mod > 0) {
1392 new_size = old_size + new_size;
1393 }
1394
1395 if (new_size < 256 * 1024 * 1024) {
1396 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001397 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001398 }
1399 if (new_size > device->bdev->bd_inode->i_size) {
1400 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001401 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001402 }
1403
1404 do_div(new_size, root->sectorsize);
1405 new_size *= root->sectorsize;
1406
Josef Bacik606686e2012-06-04 14:03:51 -04001407 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1408 rcu_str_deref(device->name),
1409 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001410
1411 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001412 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001413 if (IS_ERR(trans)) {
1414 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001415 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001416 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001417 ret = btrfs_grow_device(trans, device, new_size);
1418 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001419 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001420 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001421 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001422
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001423out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001424 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001425out:
1426 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001427 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428 return ret;
1429}
1430
Sage Weil72fd0322010-10-29 15:41:32 -04001431static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001432 char *name, unsigned long fd, int subvol,
1433 u64 *transid, bool readonly,
1434 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001437 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001438
Liu Boa874a632012-06-29 03:58:46 -06001439 ret = mnt_want_write_file(file);
1440 if (ret)
1441 goto out;
1442
Sage Weil72fd0322010-10-29 15:41:32 -04001443 namelen = strlen(name);
1444 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001445 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001446 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001447 }
1448
Chris Mason16780ca2012-02-20 22:14:55 -05001449 if (name[0] == '.' &&
1450 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1451 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001452 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001453 }
1454
Chris Mason3de45862008-11-17 21:02:50 -05001455 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001456 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001457 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001458 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001459 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001460 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001461 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001462 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001463 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001464 }
1465
Al Viro2903ff02012-08-28 12:52:22 -04001466 src_inode = src.file->f_path.dentry->d_inode;
Chris Mason3de45862008-11-17 21:02:50 -05001467 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001468 printk(KERN_INFO "btrfs: Snapshot src from "
1469 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001470 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001471 } else {
1472 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1473 BTRFS_I(src_inode)->root,
1474 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001475 }
Al Viro2903ff02012-08-28 12:52:22 -04001476 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001477 }
Liu Boa874a632012-06-29 03:58:46 -06001478out_drop_write:
1479 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001480out:
Sage Weil72fd0322010-10-29 15:41:32 -04001481 return ret;
1482}
1483
1484static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001485 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001486{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001487 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001488 int ret;
1489
Li Zefanfa0d2b92010-12-20 15:53:28 +08001490 vol_args = memdup_user(arg, sizeof(*vol_args));
1491 if (IS_ERR(vol_args))
1492 return PTR_ERR(vol_args);
1493 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001494
Li Zefanfa0d2b92010-12-20 15:53:28 +08001495 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001496 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001497 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001498
Li Zefanfa0d2b92010-12-20 15:53:28 +08001499 kfree(vol_args);
1500 return ret;
1501}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001502
Li Zefanfa0d2b92010-12-20 15:53:28 +08001503static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1504 void __user *arg, int subvol)
1505{
1506 struct btrfs_ioctl_vol_args_v2 *vol_args;
1507 int ret;
1508 u64 transid = 0;
1509 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001510 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001511 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001512
Li Zefanfa0d2b92010-12-20 15:53:28 +08001513 vol_args = memdup_user(arg, sizeof(*vol_args));
1514 if (IS_ERR(vol_args))
1515 return PTR_ERR(vol_args);
1516 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001517
Li Zefanb83cc962010-12-20 16:04:08 +08001518 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001519 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1520 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001521 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001522 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001523 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001524
1525 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1526 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001527 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1528 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001529 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1530 if (vol_args->size > PAGE_CACHE_SIZE) {
1531 ret = -EINVAL;
1532 goto out;
1533 }
1534 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1535 if (IS_ERR(inherit)) {
1536 ret = PTR_ERR(inherit);
1537 goto out;
1538 }
1539 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001540
1541 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001542 vol_args->fd, subvol, ptr,
1543 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001544
1545 if (ret == 0 && ptr &&
1546 copy_to_user(arg +
1547 offsetof(struct btrfs_ioctl_vol_args_v2,
1548 transid), ptr, sizeof(*ptr)))
1549 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001550out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001551 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001552 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553 return ret;
1554}
1555
Li Zefan0caa1022010-12-20 16:30:25 +08001556static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1557 void __user *arg)
1558{
1559 struct inode *inode = fdentry(file)->d_inode;
1560 struct btrfs_root *root = BTRFS_I(inode)->root;
1561 int ret = 0;
1562 u64 flags = 0;
1563
Li Zefan33345d012011-04-20 10:31:50 +08001564 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001565 return -EINVAL;
1566
1567 down_read(&root->fs_info->subvol_sem);
1568 if (btrfs_root_readonly(root))
1569 flags |= BTRFS_SUBVOL_RDONLY;
1570 up_read(&root->fs_info->subvol_sem);
1571
1572 if (copy_to_user(arg, &flags, sizeof(flags)))
1573 ret = -EFAULT;
1574
1575 return ret;
1576}
1577
1578static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1579 void __user *arg)
1580{
1581 struct inode *inode = fdentry(file)->d_inode;
1582 struct btrfs_root *root = BTRFS_I(inode)->root;
1583 struct btrfs_trans_handle *trans;
1584 u64 root_flags;
1585 u64 flags;
1586 int ret = 0;
1587
Liu Bob9ca0662012-06-29 03:58:49 -06001588 ret = mnt_want_write_file(file);
1589 if (ret)
1590 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001591
Liu Bob9ca0662012-06-29 03:58:49 -06001592 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1593 ret = -EINVAL;
1594 goto out_drop_write;
1595 }
Li Zefan0caa1022010-12-20 16:30:25 +08001596
Liu Bob9ca0662012-06-29 03:58:49 -06001597 if (copy_from_user(&flags, arg, sizeof(flags))) {
1598 ret = -EFAULT;
1599 goto out_drop_write;
1600 }
Li Zefan0caa1022010-12-20 16:30:25 +08001601
Liu Bob9ca0662012-06-29 03:58:49 -06001602 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1603 ret = -EINVAL;
1604 goto out_drop_write;
1605 }
Li Zefan0caa1022010-12-20 16:30:25 +08001606
Liu Bob9ca0662012-06-29 03:58:49 -06001607 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1608 ret = -EOPNOTSUPP;
1609 goto out_drop_write;
1610 }
Li Zefan0caa1022010-12-20 16:30:25 +08001611
Liu Bob9ca0662012-06-29 03:58:49 -06001612 if (!inode_owner_or_capable(inode)) {
1613 ret = -EACCES;
1614 goto out_drop_write;
1615 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001616
Li Zefan0caa1022010-12-20 16:30:25 +08001617 down_write(&root->fs_info->subvol_sem);
1618
1619 /* nothing to do */
1620 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001621 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001622
1623 root_flags = btrfs_root_flags(&root->root_item);
1624 if (flags & BTRFS_SUBVOL_RDONLY)
1625 btrfs_set_root_flags(&root->root_item,
1626 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1627 else
1628 btrfs_set_root_flags(&root->root_item,
1629 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1630
1631 trans = btrfs_start_transaction(root, 1);
1632 if (IS_ERR(trans)) {
1633 ret = PTR_ERR(trans);
1634 goto out_reset;
1635 }
1636
Li Zefanb4dc2b82011-02-16 06:06:34 +00001637 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001638 &root->root_key, &root->root_item);
1639
1640 btrfs_commit_transaction(trans, root);
1641out_reset:
1642 if (ret)
1643 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001644out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001645 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001646out_drop_write:
1647 mnt_drop_write_file(file);
1648out:
Li Zefan0caa1022010-12-20 16:30:25 +08001649 return ret;
1650}
1651
Yan, Zheng76dda932009-09-21 16:00:26 -04001652/*
1653 * helper to check if the subvolume references other subvolumes
1654 */
1655static noinline int may_destroy_subvol(struct btrfs_root *root)
1656{
1657 struct btrfs_path *path;
1658 struct btrfs_key key;
1659 int ret;
1660
1661 path = btrfs_alloc_path();
1662 if (!path)
1663 return -ENOMEM;
1664
1665 key.objectid = root->root_key.objectid;
1666 key.type = BTRFS_ROOT_REF_KEY;
1667 key.offset = (u64)-1;
1668
1669 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1670 &key, path, 0, 0);
1671 if (ret < 0)
1672 goto out;
1673 BUG_ON(ret == 0);
1674
1675 ret = 0;
1676 if (path->slots[0] > 0) {
1677 path->slots[0]--;
1678 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1679 if (key.objectid == root->root_key.objectid &&
1680 key.type == BTRFS_ROOT_REF_KEY)
1681 ret = -ENOTEMPTY;
1682 }
1683out:
1684 btrfs_free_path(path);
1685 return ret;
1686}
1687
Chris Masonac8e9812010-02-28 15:39:26 -05001688static noinline int key_in_sk(struct btrfs_key *key,
1689 struct btrfs_ioctl_search_key *sk)
1690{
Chris Masonabc6e132010-03-18 12:10:08 -04001691 struct btrfs_key test;
1692 int ret;
1693
1694 test.objectid = sk->min_objectid;
1695 test.type = sk->min_type;
1696 test.offset = sk->min_offset;
1697
1698 ret = btrfs_comp_cpu_keys(key, &test);
1699 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001700 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001701
1702 test.objectid = sk->max_objectid;
1703 test.type = sk->max_type;
1704 test.offset = sk->max_offset;
1705
1706 ret = btrfs_comp_cpu_keys(key, &test);
1707 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001708 return 0;
1709 return 1;
1710}
1711
1712static noinline int copy_to_sk(struct btrfs_root *root,
1713 struct btrfs_path *path,
1714 struct btrfs_key *key,
1715 struct btrfs_ioctl_search_key *sk,
1716 char *buf,
1717 unsigned long *sk_offset,
1718 int *num_found)
1719{
1720 u64 found_transid;
1721 struct extent_buffer *leaf;
1722 struct btrfs_ioctl_search_header sh;
1723 unsigned long item_off;
1724 unsigned long item_len;
1725 int nritems;
1726 int i;
1727 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001728 int ret = 0;
1729
1730 leaf = path->nodes[0];
1731 slot = path->slots[0];
1732 nritems = btrfs_header_nritems(leaf);
1733
1734 if (btrfs_header_generation(leaf) > sk->max_transid) {
1735 i = nritems;
1736 goto advance_key;
1737 }
1738 found_transid = btrfs_header_generation(leaf);
1739
1740 for (i = slot; i < nritems; i++) {
1741 item_off = btrfs_item_ptr_offset(leaf, i);
1742 item_len = btrfs_item_size_nr(leaf, i);
1743
1744 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1745 item_len = 0;
1746
1747 if (sizeof(sh) + item_len + *sk_offset >
1748 BTRFS_SEARCH_ARGS_BUFSIZE) {
1749 ret = 1;
1750 goto overflow;
1751 }
1752
1753 btrfs_item_key_to_cpu(leaf, key, i);
1754 if (!key_in_sk(key, sk))
1755 continue;
1756
1757 sh.objectid = key->objectid;
1758 sh.offset = key->offset;
1759 sh.type = key->type;
1760 sh.len = item_len;
1761 sh.transid = found_transid;
1762
1763 /* copy search result header */
1764 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1765 *sk_offset += sizeof(sh);
1766
1767 if (item_len) {
1768 char *p = buf + *sk_offset;
1769 /* copy the item */
1770 read_extent_buffer(leaf, p,
1771 item_off, item_len);
1772 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001773 }
Hugo Millse2156862011-05-14 17:43:41 +00001774 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001775
1776 if (*num_found >= sk->nr_items)
1777 break;
1778 }
1779advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001780 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001781 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1782 key->offset++;
1783 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1784 key->offset = 0;
1785 key->type++;
1786 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1787 key->offset = 0;
1788 key->type = 0;
1789 key->objectid++;
1790 } else
1791 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001792overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001793 return ret;
1794}
1795
1796static noinline int search_ioctl(struct inode *inode,
1797 struct btrfs_ioctl_search_args *args)
1798{
1799 struct btrfs_root *root;
1800 struct btrfs_key key;
1801 struct btrfs_key max_key;
1802 struct btrfs_path *path;
1803 struct btrfs_ioctl_search_key *sk = &args->key;
1804 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1805 int ret;
1806 int num_found = 0;
1807 unsigned long sk_offset = 0;
1808
1809 path = btrfs_alloc_path();
1810 if (!path)
1811 return -ENOMEM;
1812
1813 if (sk->tree_id == 0) {
1814 /* search the root of the inode that was passed */
1815 root = BTRFS_I(inode)->root;
1816 } else {
1817 key.objectid = sk->tree_id;
1818 key.type = BTRFS_ROOT_ITEM_KEY;
1819 key.offset = (u64)-1;
1820 root = btrfs_read_fs_root_no_name(info, &key);
1821 if (IS_ERR(root)) {
1822 printk(KERN_ERR "could not find root %llu\n",
1823 sk->tree_id);
1824 btrfs_free_path(path);
1825 return -ENOENT;
1826 }
1827 }
1828
1829 key.objectid = sk->min_objectid;
1830 key.type = sk->min_type;
1831 key.offset = sk->min_offset;
1832
1833 max_key.objectid = sk->max_objectid;
1834 max_key.type = sk->max_type;
1835 max_key.offset = sk->max_offset;
1836
1837 path->keep_locks = 1;
1838
1839 while(1) {
1840 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1841 sk->min_transid);
1842 if (ret != 0) {
1843 if (ret > 0)
1844 ret = 0;
1845 goto err;
1846 }
1847 ret = copy_to_sk(root, path, &key, sk, args->buf,
1848 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001849 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001850 if (ret || num_found >= sk->nr_items)
1851 break;
1852
1853 }
1854 ret = 0;
1855err:
1856 sk->nr_items = num_found;
1857 btrfs_free_path(path);
1858 return ret;
1859}
1860
1861static noinline int btrfs_ioctl_tree_search(struct file *file,
1862 void __user *argp)
1863{
1864 struct btrfs_ioctl_search_args *args;
1865 struct inode *inode;
1866 int ret;
1867
1868 if (!capable(CAP_SYS_ADMIN))
1869 return -EPERM;
1870
Julia Lawall2354d082010-10-29 15:14:18 -04001871 args = memdup_user(argp, sizeof(*args));
1872 if (IS_ERR(args))
1873 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001874
Chris Masonac8e9812010-02-28 15:39:26 -05001875 inode = fdentry(file)->d_inode;
1876 ret = search_ioctl(inode, args);
1877 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1878 ret = -EFAULT;
1879 kfree(args);
1880 return ret;
1881}
1882
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001883/*
Chris Masonac8e9812010-02-28 15:39:26 -05001884 * Search INODE_REFs to identify path name of 'dirid' directory
1885 * in a 'tree_id' tree. and sets path name to 'name'.
1886 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001887static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1888 u64 tree_id, u64 dirid, char *name)
1889{
1890 struct btrfs_root *root;
1891 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001892 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001893 int ret = -1;
1894 int slot;
1895 int len;
1896 int total_len = 0;
1897 struct btrfs_inode_ref *iref;
1898 struct extent_buffer *l;
1899 struct btrfs_path *path;
1900
1901 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1902 name[0]='\0';
1903 return 0;
1904 }
1905
1906 path = btrfs_alloc_path();
1907 if (!path)
1908 return -ENOMEM;
1909
Chris Masonac8e9812010-02-28 15:39:26 -05001910 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001911
1912 key.objectid = tree_id;
1913 key.type = BTRFS_ROOT_ITEM_KEY;
1914 key.offset = (u64)-1;
1915 root = btrfs_read_fs_root_no_name(info, &key);
1916 if (IS_ERR(root)) {
1917 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001918 ret = -ENOENT;
1919 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001920 }
1921
1922 key.objectid = dirid;
1923 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001924 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001925
1926 while(1) {
1927 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1928 if (ret < 0)
1929 goto out;
1930
1931 l = path->nodes[0];
1932 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001933 if (ret > 0 && slot > 0)
1934 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001935 btrfs_item_key_to_cpu(l, &key, slot);
1936
1937 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001938 key.type != BTRFS_INODE_REF_KEY)) {
1939 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001940 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001941 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001942
1943 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1944 len = btrfs_inode_ref_name_len(l, iref);
1945 ptr -= len + 1;
1946 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001947 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001948 goto out;
1949
1950 *(ptr + len) = '/';
1951 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1952
1953 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1954 break;
1955
David Sterbab3b4aa72011-04-21 01:20:15 +02001956 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001957 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001958 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001959 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001960 }
Chris Masonac8e9812010-02-28 15:39:26 -05001961 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001962 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001963 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001964 name[total_len]='\0';
1965 ret = 0;
1966out:
1967 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001968 return ret;
1969}
1970
1971static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1972 void __user *argp)
1973{
1974 struct btrfs_ioctl_ino_lookup_args *args;
1975 struct inode *inode;
1976 int ret;
1977
1978 if (!capable(CAP_SYS_ADMIN))
1979 return -EPERM;
1980
Julia Lawall2354d082010-10-29 15:14:18 -04001981 args = memdup_user(argp, sizeof(*args));
1982 if (IS_ERR(args))
1983 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001984
Chris Masonac8e9812010-02-28 15:39:26 -05001985 inode = fdentry(file)->d_inode;
1986
Chris Mason1b53ac42010-03-18 12:17:05 -04001987 if (args->treeid == 0)
1988 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1989
Chris Masonac8e9812010-02-28 15:39:26 -05001990 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1991 args->treeid, args->objectid,
1992 args->name);
1993
1994 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1995 ret = -EFAULT;
1996
1997 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001998 return ret;
1999}
2000
Yan, Zheng76dda932009-09-21 16:00:26 -04002001static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2002 void __user *arg)
2003{
2004 struct dentry *parent = fdentry(file);
2005 struct dentry *dentry;
2006 struct inode *dir = parent->d_inode;
2007 struct inode *inode;
2008 struct btrfs_root *root = BTRFS_I(dir)->root;
2009 struct btrfs_root *dest = NULL;
2010 struct btrfs_ioctl_vol_args *vol_args;
2011 struct btrfs_trans_handle *trans;
2012 int namelen;
2013 int ret;
2014 int err = 0;
2015
Yan, Zheng76dda932009-09-21 16:00:26 -04002016 vol_args = memdup_user(arg, sizeof(*vol_args));
2017 if (IS_ERR(vol_args))
2018 return PTR_ERR(vol_args);
2019
2020 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2021 namelen = strlen(vol_args->name);
2022 if (strchr(vol_args->name, '/') ||
2023 strncmp(vol_args->name, "..", namelen) == 0) {
2024 err = -EINVAL;
2025 goto out;
2026 }
2027
Al Viroa561be72011-11-23 11:57:51 -05002028 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002029 if (err)
2030 goto out;
2031
2032 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2033 dentry = lookup_one_len(vol_args->name, parent, namelen);
2034 if (IS_ERR(dentry)) {
2035 err = PTR_ERR(dentry);
2036 goto out_unlock_dir;
2037 }
2038
2039 if (!dentry->d_inode) {
2040 err = -ENOENT;
2041 goto out_dput;
2042 }
2043
2044 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002045 dest = BTRFS_I(inode)->root;
2046 if (!capable(CAP_SYS_ADMIN)){
2047 /*
2048 * Regular user. Only allow this with a special mount
2049 * option, when the user has write+exec access to the
2050 * subvol root, and when rmdir(2) would have been
2051 * allowed.
2052 *
2053 * Note that this is _not_ check that the subvol is
2054 * empty or doesn't contain data that we wouldn't
2055 * otherwise be able to delete.
2056 *
2057 * Users who want to delete empty subvols should try
2058 * rmdir(2).
2059 */
2060 err = -EPERM;
2061 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2062 goto out_dput;
2063
2064 /*
2065 * Do not allow deletion if the parent dir is the same
2066 * as the dir to be deleted. That means the ioctl
2067 * must be called on the dentry referencing the root
2068 * of the subvol, not a random directory contained
2069 * within it.
2070 */
2071 err = -EINVAL;
2072 if (root == dest)
2073 goto out_dput;
2074
2075 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2076 if (err)
2077 goto out_dput;
2078
2079 /* check if subvolume may be deleted by a non-root user */
2080 err = btrfs_may_delete(dir, dentry, 1);
2081 if (err)
2082 goto out_dput;
2083 }
2084
Li Zefan33345d012011-04-20 10:31:50 +08002085 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002086 err = -EINVAL;
2087 goto out_dput;
2088 }
2089
Yan, Zheng76dda932009-09-21 16:00:26 -04002090 mutex_lock(&inode->i_mutex);
2091 err = d_invalidate(dentry);
2092 if (err)
2093 goto out_unlock;
2094
2095 down_write(&root->fs_info->subvol_sem);
2096
2097 err = may_destroy_subvol(dest);
2098 if (err)
2099 goto out_up_write;
2100
Yan, Zhenga22285a2010-05-16 10:48:46 -04002101 trans = btrfs_start_transaction(root, 0);
2102 if (IS_ERR(trans)) {
2103 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002104 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002105 }
2106 trans->block_rsv = &root->fs_info->global_block_rsv;
2107
Yan, Zheng76dda932009-09-21 16:00:26 -04002108 ret = btrfs_unlink_subvol(trans, root, dir,
2109 dest->root_key.objectid,
2110 dentry->d_name.name,
2111 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002112 if (ret) {
2113 err = ret;
2114 btrfs_abort_transaction(trans, root, ret);
2115 goto out_end_trans;
2116 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002117
2118 btrfs_record_root_in_trans(trans, dest);
2119
2120 memset(&dest->root_item.drop_progress, 0,
2121 sizeof(dest->root_item.drop_progress));
2122 dest->root_item.drop_level = 0;
2123 btrfs_set_root_refs(&dest->root_item, 0);
2124
Yan, Zhengd68fc572010-05-16 10:49:58 -04002125 if (!xchg(&dest->orphan_item_inserted, 1)) {
2126 ret = btrfs_insert_orphan_item(trans,
2127 root->fs_info->tree_root,
2128 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002129 if (ret) {
2130 btrfs_abort_transaction(trans, root, ret);
2131 err = ret;
2132 goto out_end_trans;
2133 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002134 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002135out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002136 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002137 if (ret && !err)
2138 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002139 inode->i_flags |= S_DEAD;
2140out_up_write:
2141 up_write(&root->fs_info->subvol_sem);
2142out_unlock:
2143 mutex_unlock(&inode->i_mutex);
2144 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002145 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002146 btrfs_invalidate_inodes(dest);
2147 d_delete(dentry);
2148 }
2149out_dput:
2150 dput(dentry);
2151out_unlock_dir:
2152 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002153 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002154out:
2155 kfree(vol_args);
2156 return err;
2157}
2158
Chris Mason1e701a32010-03-11 09:42:04 -05002159static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002160{
2161 struct inode *inode = fdentry(file)->d_inode;
2162 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002163 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002164 int ret;
2165
Li Zefanb83cc962010-12-20 16:04:08 +08002166 if (btrfs_root_readonly(root))
2167 return -EROFS;
2168
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002169 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2170 1)) {
2171 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2172 return -EINPROGRESS;
2173 }
Al Viroa561be72011-11-23 11:57:51 -05002174 ret = mnt_want_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002175 if (ret) {
2176 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
2177 0);
Yan Zhengc146afa2008-11-12 14:34:12 -05002178 return ret;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002179 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002180
2181 switch (inode->i_mode & S_IFMT) {
2182 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002183 if (!capable(CAP_SYS_ADMIN)) {
2184 ret = -EPERM;
2185 goto out;
2186 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002187 ret = btrfs_defrag_root(root, 0);
2188 if (ret)
2189 goto out;
2190 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002191 break;
2192 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002193 if (!(file->f_mode & FMODE_WRITE)) {
2194 ret = -EINVAL;
2195 goto out;
2196 }
Chris Mason1e701a32010-03-11 09:42:04 -05002197
2198 range = kzalloc(sizeof(*range), GFP_KERNEL);
2199 if (!range) {
2200 ret = -ENOMEM;
2201 goto out;
2202 }
2203
2204 if (argp) {
2205 if (copy_from_user(range, argp,
2206 sizeof(*range))) {
2207 ret = -EFAULT;
2208 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002209 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002210 }
2211 /* compression requires us to start the IO */
2212 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2213 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2214 range->extent_thresh = (u32)-1;
2215 }
2216 } else {
2217 /* the rest are all set to zero by kzalloc */
2218 range->len = (u64)-1;
2219 }
Chris Mason4cb53002011-05-24 15:35:30 -04002220 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2221 range, 0, 0);
2222 if (ret > 0)
2223 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002224 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002225 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002226 default:
2227 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002228 }
Chris Masone441d542009-01-05 16:57:23 -05002229out:
Al Viro2a79f172011-12-09 08:06:57 -05002230 mnt_drop_write_file(file);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002231 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Chris Masone441d542009-01-05 16:57:23 -05002232 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002233}
2234
Christoph Hellwigb2950862008-12-02 09:54:17 -05002235static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002236{
2237 struct btrfs_ioctl_vol_args *vol_args;
2238 int ret;
2239
Chris Masone441d542009-01-05 16:57:23 -05002240 if (!capable(CAP_SYS_ADMIN))
2241 return -EPERM;
2242
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002243 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2244 1)) {
2245 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2246 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002247 }
2248
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002249 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002250 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002251 if (IS_ERR(vol_args)) {
2252 ret = PTR_ERR(vol_args);
2253 goto out;
2254 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002255
Mark Fasheh5516e592008-07-24 12:20:14 -04002256 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002257 ret = btrfs_init_new_device(root, vol_args->name);
2258
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002259 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002260out:
2261 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002262 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002263 return ret;
2264}
2265
Christoph Hellwigb2950862008-12-02 09:54:17 -05002266static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002267{
2268 struct btrfs_ioctl_vol_args *vol_args;
2269 int ret;
2270
Chris Masone441d542009-01-05 16:57:23 -05002271 if (!capable(CAP_SYS_ADMIN))
2272 return -EPERM;
2273
Yan Zhengc146afa2008-11-12 14:34:12 -05002274 if (root->fs_info->sb->s_flags & MS_RDONLY)
2275 return -EROFS;
2276
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002277 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2278 1)) {
2279 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
2280 return -EINPROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002281 }
2282
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002283 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002284 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002285 if (IS_ERR(vol_args)) {
2286 ret = PTR_ERR(vol_args);
2287 goto out;
2288 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002289
Mark Fasheh5516e592008-07-24 12:20:14 -04002290 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002291 ret = btrfs_rm_device(root, vol_args->name);
2292
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002293 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002294out:
2295 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002296 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002297 return ret;
2298}
2299
Jan Schmidt475f6382011-03-11 15:41:01 +01002300static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2301{
Li Zefan027ed2f2011-06-08 08:27:56 +00002302 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002303 struct btrfs_device *device;
2304 struct btrfs_device *next;
2305 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002306 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002307
2308 if (!capable(CAP_SYS_ADMIN))
2309 return -EPERM;
2310
Li Zefan027ed2f2011-06-08 08:27:56 +00002311 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2312 if (!fi_args)
2313 return -ENOMEM;
2314
2315 fi_args->num_devices = fs_devices->num_devices;
2316 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002317
2318 mutex_lock(&fs_devices->device_list_mutex);
2319 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002320 if (device->devid > fi_args->max_id)
2321 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002322 }
2323 mutex_unlock(&fs_devices->device_list_mutex);
2324
Li Zefan027ed2f2011-06-08 08:27:56 +00002325 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2326 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002327
Li Zefan027ed2f2011-06-08 08:27:56 +00002328 kfree(fi_args);
2329 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002330}
2331
2332static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2333{
2334 struct btrfs_ioctl_dev_info_args *di_args;
2335 struct btrfs_device *dev;
2336 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2337 int ret = 0;
2338 char *s_uuid = NULL;
2339 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2340
2341 if (!capable(CAP_SYS_ADMIN))
2342 return -EPERM;
2343
2344 di_args = memdup_user(arg, sizeof(*di_args));
2345 if (IS_ERR(di_args))
2346 return PTR_ERR(di_args);
2347
2348 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2349 s_uuid = di_args->uuid;
2350
2351 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002352 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002353 mutex_unlock(&fs_devices->device_list_mutex);
2354
2355 if (!dev) {
2356 ret = -ENODEV;
2357 goto out;
2358 }
2359
2360 di_args->devid = dev->devid;
2361 di_args->bytes_used = dev->bytes_used;
2362 di_args->total_bytes = dev->total_bytes;
2363 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002364 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002365 struct rcu_string *name;
2366
2367 rcu_read_lock();
2368 name = rcu_dereference(dev->name);
2369 strncpy(di_args->path, name->str, sizeof(di_args->path));
2370 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002371 di_args->path[sizeof(di_args->path) - 1] = 0;
2372 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002373 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002374 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002375
2376out:
2377 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2378 ret = -EFAULT;
2379
2380 kfree(di_args);
2381 return ret;
2382}
2383
Yan, Zheng76dda932009-09-21 16:00:26 -04002384static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2385 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002386{
2387 struct inode *inode = fdentry(file)->d_inode;
2388 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro2903ff02012-08-28 12:52:22 -04002389 struct fd src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390 struct inode *src;
2391 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002392 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002393 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002394 char *buf;
2395 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002396 u32 nritems;
2397 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002398 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002399 u64 len = olen;
2400 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002401
Sage Weilc5c9cd42008-11-12 14:32:25 -05002402 /*
2403 * TODO:
2404 * - split compressed inline extents. annoying: we need to
2405 * decompress into destination's address_space (the file offset
2406 * may change, so source mapping won't do), then recompress (or
2407 * otherwise reinsert) a subrange.
2408 * - allow ranges within the same file to be cloned (provided
2409 * they don't overlap)?
2410 */
2411
Chris Masone441d542009-01-05 16:57:23 -05002412 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002413 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002414 return -EINVAL;
2415
Li Zefanb83cc962010-12-20 16:04:08 +08002416 if (btrfs_root_readonly(root))
2417 return -EROFS;
2418
Al Viroa561be72011-11-23 11:57:51 -05002419 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002420 if (ret)
2421 return ret;
2422
Al Viro2903ff02012-08-28 12:52:22 -04002423 src_file = fdget(srcfd);
2424 if (!src_file.file) {
Yan Zhengab67b7c2008-12-19 10:58:39 -05002425 ret = -EBADF;
2426 goto out_drop_write;
2427 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002428
David Sterba362a20c2011-08-01 18:11:57 +02002429 ret = -EXDEV;
Al Viro2903ff02012-08-28 12:52:22 -04002430 if (src_file.file->f_path.mnt != file->f_path.mnt)
David Sterba362a20c2011-08-01 18:11:57 +02002431 goto out_fput;
2432
Al Viro2903ff02012-08-28 12:52:22 -04002433 src = src_file.file->f_dentry->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002434
Sage Weilc5c9cd42008-11-12 14:32:25 -05002435 ret = -EINVAL;
2436 if (src == inode)
2437 goto out_fput;
2438
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002439 /* the src must be open for reading */
Al Viro2903ff02012-08-28 12:52:22 -04002440 if (!(src_file.file->f_mode & FMODE_READ))
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002441 goto out_fput;
2442
Li Zefan0e7b8242011-09-18 10:20:46 -04002443 /* don't make the dst file partly checksummed */
2444 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2445 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2446 goto out_fput;
2447
Yan Zhengae01a0a2008-08-04 23:23:47 -04002448 ret = -EISDIR;
2449 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002450 goto out_fput;
2451
Yan Zhengae01a0a2008-08-04 23:23:47 -04002452 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002453 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002454 goto out_fput;
2455
2456 ret = -ENOMEM;
2457 buf = vmalloc(btrfs_level_size(root, 0));
2458 if (!buf)
2459 goto out_fput;
2460
2461 path = btrfs_alloc_path();
2462 if (!path) {
2463 vfree(buf);
2464 goto out_fput;
2465 }
2466 path->reada = 2;
2467
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002468 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002469 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2470 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002471 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002472 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2473 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002474 }
2475
Sage Weilc5c9cd42008-11-12 14:32:25 -05002476 /* determine range to clone */
2477 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002478 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002479 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002480 if (len == 0)
2481 olen = len = src->i_size - off;
2482 /* if we extend to eof, continue to block boundary */
2483 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002484 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002485
2486 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002487 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2488 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002489 goto out_unlock;
2490
Li Zefand525e8a2011-09-11 10:52:25 -04002491 if (destoff > inode->i_size) {
2492 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2493 if (ret)
2494 goto out_unlock;
2495 }
2496
Li Zefan71ef0782011-09-18 10:20:46 -04002497 /* truncate page cache pages from target inode range */
2498 truncate_inode_pages_range(&inode->i_data, destoff,
2499 PAGE_CACHE_ALIGN(destoff + len) - 1);
2500
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501 /* do any pending delalloc/csum calc on src, one way or
2502 another, and lock file content */
2503 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002504 struct btrfs_ordered_extent *ordered;
Liu Boaa42ffd2012-09-18 03:52:23 -06002505 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2506 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
Sage Weil9a019192010-10-29 15:37:33 -04002507 if (!ordered &&
Liu Boaa42ffd2012-09-18 03:52:23 -06002508 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2509 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002510 break;
Liu Boaa42ffd2012-09-18 03:52:23 -06002511 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002512 if (ordered)
2513 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002514 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002515 }
2516
Sage Weilc5c9cd42008-11-12 14:32:25 -05002517 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002518 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002519 key.type = BTRFS_EXTENT_DATA_KEY;
2520 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002521
2522 while (1) {
2523 /*
2524 * note the key will change type as we walk through the
2525 * tree.
2526 */
David Sterba362a20c2011-08-01 18:11:57 +02002527 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2528 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002529 if (ret < 0)
2530 goto out;
2531
Yan Zhengae01a0a2008-08-04 23:23:47 -04002532 nritems = btrfs_header_nritems(path->nodes[0]);
2533 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002534 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002535 if (ret < 0)
2536 goto out;
2537 if (ret > 0)
2538 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002539 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002540 }
2541 leaf = path->nodes[0];
2542 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002543
Yan Zhengae01a0a2008-08-04 23:23:47 -04002544 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002545 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002546 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002547 break;
2548
Sage Weilc5c9cd42008-11-12 14:32:25 -05002549 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2550 struct btrfs_file_extent_item *extent;
2551 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002552 u32 size;
2553 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002554 u64 disko = 0, diskl = 0;
2555 u64 datao = 0, datal = 0;
2556 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002557 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002558
2559 size = btrfs_item_size_nr(leaf, slot);
2560 read_extent_buffer(leaf, buf,
2561 btrfs_item_ptr_offset(leaf, slot),
2562 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002563
2564 extent = btrfs_item_ptr(leaf, slot,
2565 struct btrfs_file_extent_item);
2566 comp = btrfs_file_extent_compression(leaf, extent);
2567 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002568 if (type == BTRFS_FILE_EXTENT_REG ||
2569 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002570 disko = btrfs_file_extent_disk_bytenr(leaf,
2571 extent);
2572 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2573 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002574 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002575 datal = btrfs_file_extent_num_bytes(leaf,
2576 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002577 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2578 /* take upper bound, may be compressed */
2579 datal = btrfs_file_extent_ram_bytes(leaf,
2580 extent);
2581 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002582 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002583
Sage Weil050006a2010-10-29 15:37:33 -04002584 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002585 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002586 goto next;
2587
Zheng Yan31840ae2008-09-23 13:14:14 -04002588 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002589 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002590 if (off <= key.offset)
2591 new_key.offset = key.offset + destoff - off;
2592 else
2593 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002594
Sage Weilb6f34092011-09-20 14:48:51 -04002595 /*
2596 * 1 - adjusting old extent (we may have to split it)
2597 * 1 - add new extent
2598 * 1 - inode update
2599 */
2600 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002601 if (IS_ERR(trans)) {
2602 ret = PTR_ERR(trans);
2603 goto out;
2604 }
2605
Chris Masonc8a894d2009-06-27 21:07:03 -04002606 if (type == BTRFS_FILE_EXTENT_REG ||
2607 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002608 /*
2609 * a | --- range to clone ---| b
2610 * | ------------- extent ------------- |
2611 */
2612
2613 /* substract range b */
2614 if (key.offset + datal > off + len)
2615 datal = off + len - key.offset;
2616
2617 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002618 if (off > key.offset) {
2619 datao += off - key.offset;
2620 datal -= off - key.offset;
2621 }
2622
Josef Bacik5dc562c2012-08-17 13:14:17 -04002623 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002624 new_key.offset,
2625 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002626 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002627 if (ret) {
2628 btrfs_abort_transaction(trans, root,
2629 ret);
2630 btrfs_end_transaction(trans, root);
2631 goto out;
2632 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002633
Sage Weilc5c9cd42008-11-12 14:32:25 -05002634 ret = btrfs_insert_empty_item(trans, root, path,
2635 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002636 if (ret) {
2637 btrfs_abort_transaction(trans, root,
2638 ret);
2639 btrfs_end_transaction(trans, root);
2640 goto out;
2641 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002642
2643 leaf = path->nodes[0];
2644 slot = path->slots[0];
2645 write_extent_buffer(leaf, buf,
2646 btrfs_item_ptr_offset(leaf, slot),
2647 size);
2648
2649 extent = btrfs_item_ptr(leaf, slot,
2650 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002651
Sage Weilc5c9cd42008-11-12 14:32:25 -05002652 /* disko == 0 means it's a hole */
2653 if (!disko)
2654 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002655
2656 btrfs_set_file_extent_offset(leaf, extent,
2657 datao);
2658 btrfs_set_file_extent_num_bytes(leaf, extent,
2659 datal);
2660 if (disko) {
2661 inode_add_bytes(inode, datal);
2662 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002663 disko, diskl, 0,
2664 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002665 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002666 new_key.offset - datao,
2667 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002668 if (ret) {
2669 btrfs_abort_transaction(trans,
2670 root,
2671 ret);
2672 btrfs_end_transaction(trans,
2673 root);
2674 goto out;
2675
2676 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002677 }
2678 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2679 u64 skip = 0;
2680 u64 trim = 0;
2681 if (off > key.offset) {
2682 skip = off - key.offset;
2683 new_key.offset += skip;
2684 }
Chris Masond3977122009-01-05 21:25:51 -05002685
Liu Boaa42ffd2012-09-18 03:52:23 -06002686 if (key.offset + datal > off + len)
2687 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002688
Sage Weilc5c9cd42008-11-12 14:32:25 -05002689 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002690 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002691 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002692 goto out;
2693 }
2694 size -= skip + trim;
2695 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002696
Josef Bacik5dc562c2012-08-17 13:14:17 -04002697 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002698 new_key.offset,
2699 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002700 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002701 if (ret) {
2702 btrfs_abort_transaction(trans, root,
2703 ret);
2704 btrfs_end_transaction(trans, root);
2705 goto out;
2706 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002707
Sage Weilc5c9cd42008-11-12 14:32:25 -05002708 ret = btrfs_insert_empty_item(trans, root, path,
2709 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002710 if (ret) {
2711 btrfs_abort_transaction(trans, root,
2712 ret);
2713 btrfs_end_transaction(trans, root);
2714 goto out;
2715 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002716
2717 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002718 u32 start =
2719 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002720 memmove(buf+start, buf+start+skip,
2721 datal);
2722 }
2723
2724 leaf = path->nodes[0];
2725 slot = path->slots[0];
2726 write_extent_buffer(leaf, buf,
2727 btrfs_item_ptr_offset(leaf, slot),
2728 size);
2729 inode_add_bytes(inode, datal);
2730 }
2731
2732 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002733 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002734
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002735 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002736 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002737
2738 /*
2739 * we round up to the block size at eof when
2740 * determining which extents to clone above,
2741 * but shouldn't round up the file size
2742 */
2743 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002744 if (endoff > destoff+olen)
2745 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002746 if (endoff > inode->i_size)
2747 btrfs_i_size_write(inode, endoff);
2748
Yan, Zhenga22285a2010-05-16 10:48:46 -04002749 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002750 if (ret) {
2751 btrfs_abort_transaction(trans, root, ret);
2752 btrfs_end_transaction(trans, root);
2753 goto out;
2754 }
2755 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002756 }
Chris Masond3977122009-01-05 21:25:51 -05002757next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002758 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002759 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002760 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002761 ret = 0;
2762out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002763 btrfs_release_path(path);
Liu Boaa42ffd2012-09-18 03:52:23 -06002764 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002765out_unlock:
2766 mutex_unlock(&src->i_mutex);
2767 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002768 vfree(buf);
2769 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002770out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04002771 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002772out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002773 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002774 return ret;
2775}
2776
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002777static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002778{
2779 struct btrfs_ioctl_clone_range_args args;
2780
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002781 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002782 return -EFAULT;
2783 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2784 args.src_length, args.dest_offset);
2785}
2786
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002787/*
2788 * there are many ways the trans_start and trans_end ioctls can lead
2789 * to deadlocks. They should only be used by applications that
2790 * basically own the machine, and have a very in depth understanding
2791 * of all the possible deadlocks and enospc problems.
2792 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002793static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002794{
2795 struct inode *inode = fdentry(file)->d_inode;
2796 struct btrfs_root *root = BTRFS_I(inode)->root;
2797 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002798 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002799
Sage Weil1ab86ae2009-09-29 18:38:44 -04002800 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002801 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002802 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002803
2804 ret = -EINPROGRESS;
2805 if (file->private_data)
2806 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002807
Li Zefanb83cc962010-12-20 16:04:08 +08002808 ret = -EROFS;
2809 if (btrfs_root_readonly(root))
2810 goto out;
2811
Al Viroa561be72011-11-23 11:57:51 -05002812 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002813 if (ret)
2814 goto out;
2815
Josef Bacika4abeea2011-04-11 17:25:13 -04002816 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002817
Sage Weil1ab86ae2009-09-29 18:38:44 -04002818 ret = -ENOMEM;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04002819 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002820 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002821 goto out_drop;
2822
2823 file->private_data = trans;
2824 return 0;
2825
2826out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002827 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002828 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002829out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002830 return ret;
2831}
2832
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002833static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2834{
2835 struct inode *inode = fdentry(file)->d_inode;
2836 struct btrfs_root *root = BTRFS_I(inode)->root;
2837 struct btrfs_root *new_root;
2838 struct btrfs_dir_item *di;
2839 struct btrfs_trans_handle *trans;
2840 struct btrfs_path *path;
2841 struct btrfs_key location;
2842 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002843 u64 objectid = 0;
2844 u64 dir_id;
2845
2846 if (!capable(CAP_SYS_ADMIN))
2847 return -EPERM;
2848
2849 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2850 return -EFAULT;
2851
2852 if (!objectid)
2853 objectid = root->root_key.objectid;
2854
2855 location.objectid = objectid;
2856 location.type = BTRFS_ROOT_ITEM_KEY;
2857 location.offset = (u64)-1;
2858
2859 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2860 if (IS_ERR(new_root))
2861 return PTR_ERR(new_root);
2862
2863 if (btrfs_root_refs(&new_root->root_item) == 0)
2864 return -ENOENT;
2865
2866 path = btrfs_alloc_path();
2867 if (!path)
2868 return -ENOMEM;
2869 path->leave_spinning = 1;
2870
2871 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002872 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002873 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002874 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002875 }
2876
David Sterba6c417612011-04-13 15:41:04 +02002877 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002878 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2879 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002880 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002881 btrfs_free_path(path);
2882 btrfs_end_transaction(trans, root);
2883 printk(KERN_ERR "Umm, you don't have the default dir item, "
2884 "this isn't going to work\n");
2885 return -ENOENT;
2886 }
2887
2888 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2889 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2890 btrfs_mark_buffer_dirty(path->nodes[0]);
2891 btrfs_free_path(path);
2892
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002893 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002894 btrfs_end_transaction(trans, root);
2895
2896 return 0;
2897}
2898
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002899void btrfs_get_block_group_info(struct list_head *groups_list,
2900 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc092010-09-29 11:22:36 -04002901{
2902 struct btrfs_block_group_cache *block_group;
2903
2904 space->total_bytes = 0;
2905 space->used_bytes = 0;
2906 space->flags = 0;
2907 list_for_each_entry(block_group, groups_list, list) {
2908 space->flags = block_group->flags;
2909 space->total_bytes += block_group->key.offset;
2910 space->used_bytes +=
2911 btrfs_block_group_used(&block_group->item);
2912 }
2913}
2914
Josef Bacik1406e432010-01-13 18:19:06 +00002915long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2916{
2917 struct btrfs_ioctl_space_args space_args;
2918 struct btrfs_ioctl_space_info space;
2919 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002920 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002921 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002922 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002923 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2924 BTRFS_BLOCK_GROUP_SYSTEM,
2925 BTRFS_BLOCK_GROUP_METADATA,
2926 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2927 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002928 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002929 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002930 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002931 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002932
2933 if (copy_from_user(&space_args,
2934 (struct btrfs_ioctl_space_args __user *)arg,
2935 sizeof(space_args)))
2936 return -EFAULT;
2937
Josef Bacikbf5fc092010-09-29 11:22:36 -04002938 for (i = 0; i < num_types; i++) {
2939 struct btrfs_space_info *tmp;
2940
2941 info = NULL;
2942 rcu_read_lock();
2943 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2944 list) {
2945 if (tmp->flags == types[i]) {
2946 info = tmp;
2947 break;
2948 }
2949 }
2950 rcu_read_unlock();
2951
2952 if (!info)
2953 continue;
2954
2955 down_read(&info->groups_sem);
2956 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2957 if (!list_empty(&info->block_groups[c]))
2958 slot_count++;
2959 }
2960 up_read(&info->groups_sem);
2961 }
Josef Bacik1406e432010-01-13 18:19:06 +00002962
Chris Mason7fde62b2010-03-16 15:40:10 -04002963 /* space_slots == 0 means they are asking for a count */
2964 if (space_args.space_slots == 0) {
2965 space_args.total_spaces = slot_count;
2966 goto out;
2967 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002968
Dan Rosenberg51788b12011-02-14 16:04:23 -05002969 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002970
Chris Mason7fde62b2010-03-16 15:40:10 -04002971 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002972
Chris Mason7fde62b2010-03-16 15:40:10 -04002973 /* we generally have at most 6 or so space infos, one for each raid
2974 * level. So, a whole page should be more than enough for everyone
2975 */
2976 if (alloc_size > PAGE_CACHE_SIZE)
2977 return -ENOMEM;
2978
2979 space_args.total_spaces = 0;
2980 dest = kmalloc(alloc_size, GFP_NOFS);
2981 if (!dest)
2982 return -ENOMEM;
2983 dest_orig = dest;
2984
2985 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002986 for (i = 0; i < num_types; i++) {
2987 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002988
Dan Rosenberg51788b12011-02-14 16:04:23 -05002989 if (!slot_count)
2990 break;
2991
Josef Bacikbf5fc092010-09-29 11:22:36 -04002992 info = NULL;
2993 rcu_read_lock();
2994 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2995 list) {
2996 if (tmp->flags == types[i]) {
2997 info = tmp;
2998 break;
2999 }
3000 }
3001 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003002
Josef Bacikbf5fc092010-09-29 11:22:36 -04003003 if (!info)
3004 continue;
3005 down_read(&info->groups_sem);
3006 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3007 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003008 btrfs_get_block_group_info(
3009 &info->block_groups[c], &space);
Josef Bacikbf5fc092010-09-29 11:22:36 -04003010 memcpy(dest, &space, sizeof(space));
3011 dest++;
3012 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003013 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003014 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003015 if (!slot_count)
3016 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003017 }
3018 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003019 }
Josef Bacik1406e432010-01-13 18:19:06 +00003020
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003021 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003022 (arg + sizeof(struct btrfs_ioctl_space_args));
3023
3024 if (copy_to_user(user_dest, dest_orig, alloc_size))
3025 ret = -EFAULT;
3026
3027 kfree(dest_orig);
3028out:
3029 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003030 ret = -EFAULT;
3031
3032 return ret;
3033}
3034
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003035/*
3036 * there are many ways the trans_start and trans_end ioctls can lead
3037 * to deadlocks. They should only be used by applications that
3038 * basically own the machine, and have a very in depth understanding
3039 * of all the possible deadlocks and enospc problems.
3040 */
3041long btrfs_ioctl_trans_end(struct file *file)
3042{
3043 struct inode *inode = fdentry(file)->d_inode;
3044 struct btrfs_root *root = BTRFS_I(inode)->root;
3045 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003046
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003047 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003048 if (!trans)
3049 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003050 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003051
Sage Weil1ab86ae2009-09-29 18:38:44 -04003052 btrfs_end_transaction(trans, root);
3053
Josef Bacika4abeea2011-04-11 17:25:13 -04003054 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003055
Al Viro2a79f172011-12-09 08:06:57 -05003056 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003057 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003058}
3059
Sage Weil46204592010-10-29 15:41:32 -04003060static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
3061{
3062 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3063 struct btrfs_trans_handle *trans;
3064 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003065 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003066
3067 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003068 if (IS_ERR(trans))
3069 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04003070 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003071 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003072 if (ret) {
3073 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003074 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003075 }
Sage Weil46204592010-10-29 15:41:32 -04003076
3077 if (argp)
3078 if (copy_to_user(argp, &transid, sizeof(transid)))
3079 return -EFAULT;
3080 return 0;
3081}
3082
3083static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3084{
3085 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3086 u64 transid;
3087
3088 if (argp) {
3089 if (copy_from_user(&transid, argp, sizeof(transid)))
3090 return -EFAULT;
3091 } else {
3092 transid = 0; /* current trans */
3093 }
3094 return btrfs_wait_for_commit(root, transid);
3095}
3096
Jan Schmidt475f6382011-03-11 15:41:01 +01003097static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3098{
3099 int ret;
3100 struct btrfs_ioctl_scrub_args *sa;
3101
3102 if (!capable(CAP_SYS_ADMIN))
3103 return -EPERM;
3104
3105 sa = memdup_user(arg, sizeof(*sa));
3106 if (IS_ERR(sa))
3107 return PTR_ERR(sa);
3108
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003109 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003110 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3111 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003112
3113 if (copy_to_user(arg, sa, sizeof(*sa)))
3114 ret = -EFAULT;
3115
3116 kfree(sa);
3117 return ret;
3118}
3119
3120static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3121{
3122 if (!capable(CAP_SYS_ADMIN))
3123 return -EPERM;
3124
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003125 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003126}
3127
3128static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3129 void __user *arg)
3130{
3131 struct btrfs_ioctl_scrub_args *sa;
3132 int ret;
3133
3134 if (!capable(CAP_SYS_ADMIN))
3135 return -EPERM;
3136
3137 sa = memdup_user(arg, sizeof(*sa));
3138 if (IS_ERR(sa))
3139 return PTR_ERR(sa);
3140
3141 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3142
3143 if (copy_to_user(arg, sa, sizeof(*sa)))
3144 ret = -EFAULT;
3145
3146 kfree(sa);
3147 return ret;
3148}
3149
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003150static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003151 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003152{
3153 struct btrfs_ioctl_get_dev_stats *sa;
3154 int ret;
3155
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003156 sa = memdup_user(arg, sizeof(*sa));
3157 if (IS_ERR(sa))
3158 return PTR_ERR(sa);
3159
David Sterbab27f7c02012-06-22 06:30:39 -06003160 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3161 kfree(sa);
3162 return -EPERM;
3163 }
3164
3165 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003166
3167 if (copy_to_user(arg, sa, sizeof(*sa)))
3168 ret = -EFAULT;
3169
3170 kfree(sa);
3171 return ret;
3172}
3173
Jan Schmidtd7728c92011-07-07 16:48:38 +02003174static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3175{
3176 int ret = 0;
3177 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003178 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003179 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003180 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003181 struct inode_fs_paths *ipath = NULL;
3182 struct btrfs_path *path;
3183
3184 if (!capable(CAP_SYS_ADMIN))
3185 return -EPERM;
3186
3187 path = btrfs_alloc_path();
3188 if (!path) {
3189 ret = -ENOMEM;
3190 goto out;
3191 }
3192
3193 ipa = memdup_user(arg, sizeof(*ipa));
3194 if (IS_ERR(ipa)) {
3195 ret = PTR_ERR(ipa);
3196 ipa = NULL;
3197 goto out;
3198 }
3199
3200 size = min_t(u32, ipa->size, 4096);
3201 ipath = init_ipath(size, root, path);
3202 if (IS_ERR(ipath)) {
3203 ret = PTR_ERR(ipath);
3204 ipath = NULL;
3205 goto out;
3206 }
3207
3208 ret = paths_from_inode(ipa->inum, ipath);
3209 if (ret < 0)
3210 goto out;
3211
3212 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003213 rel_ptr = ipath->fspath->val[i] -
3214 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003215 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003216 }
3217
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003218 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3219 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003220 if (ret) {
3221 ret = -EFAULT;
3222 goto out;
3223 }
3224
3225out:
3226 btrfs_free_path(path);
3227 free_ipath(ipath);
3228 kfree(ipa);
3229
3230 return ret;
3231}
3232
3233static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3234{
3235 struct btrfs_data_container *inodes = ctx;
3236 const size_t c = 3 * sizeof(u64);
3237
3238 if (inodes->bytes_left >= c) {
3239 inodes->bytes_left -= c;
3240 inodes->val[inodes->elem_cnt] = inum;
3241 inodes->val[inodes->elem_cnt + 1] = offset;
3242 inodes->val[inodes->elem_cnt + 2] = root;
3243 inodes->elem_cnt += 3;
3244 } else {
3245 inodes->bytes_missing += c - inodes->bytes_left;
3246 inodes->bytes_left = 0;
3247 inodes->elem_missed += 3;
3248 }
3249
3250 return 0;
3251}
3252
3253static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3254 void __user *arg)
3255{
3256 int ret = 0;
3257 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003258 struct btrfs_ioctl_logical_ino_args *loi;
3259 struct btrfs_data_container *inodes = NULL;
3260 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003261
3262 if (!capable(CAP_SYS_ADMIN))
3263 return -EPERM;
3264
3265 loi = memdup_user(arg, sizeof(*loi));
3266 if (IS_ERR(loi)) {
3267 ret = PTR_ERR(loi);
3268 loi = NULL;
3269 goto out;
3270 }
3271
3272 path = btrfs_alloc_path();
3273 if (!path) {
3274 ret = -ENOMEM;
3275 goto out;
3276 }
3277
Liu Bo425d17a2012-09-07 20:01:30 -06003278 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003279 inodes = init_data_container(size);
3280 if (IS_ERR(inodes)) {
3281 ret = PTR_ERR(inodes);
3282 inodes = NULL;
3283 goto out;
3284 }
3285
Liu Bodf031f02012-09-07 20:01:29 -06003286 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3287 build_ino_list, inodes);
3288 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003289 ret = -ENOENT;
3290 if (ret < 0)
3291 goto out;
3292
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003293 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3294 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003295 if (ret)
3296 ret = -EFAULT;
3297
3298out:
3299 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003300 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003301 kfree(loi);
3302
3303 return ret;
3304}
3305
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003306void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003307 struct btrfs_ioctl_balance_args *bargs)
3308{
3309 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3310
3311 bargs->flags = bctl->flags;
3312
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003313 if (atomic_read(&fs_info->balance_running))
3314 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3315 if (atomic_read(&fs_info->balance_pause_req))
3316 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003317 if (atomic_read(&fs_info->balance_cancel_req))
3318 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003319
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003320 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3321 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3322 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003323
3324 if (lock) {
3325 spin_lock(&fs_info->balance_lock);
3326 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3327 spin_unlock(&fs_info->balance_lock);
3328 } else {
3329 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3330 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003331}
3332
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003333static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003334{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003335 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003336 struct btrfs_fs_info *fs_info = root->fs_info;
3337 struct btrfs_ioctl_balance_args *bargs;
3338 struct btrfs_balance_control *bctl;
3339 int ret;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003340 int need_to_clear_lock = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003341
3342 if (!capable(CAP_SYS_ADMIN))
3343 return -EPERM;
3344
Liu Boe54bfa32012-06-29 03:58:48 -06003345 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003346 if (ret)
3347 return ret;
3348
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003349 mutex_lock(&fs_info->volume_mutex);
3350 mutex_lock(&fs_info->balance_mutex);
3351
3352 if (arg) {
3353 bargs = memdup_user(arg, sizeof(*bargs));
3354 if (IS_ERR(bargs)) {
3355 ret = PTR_ERR(bargs);
3356 goto out;
3357 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003358
3359 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3360 if (!fs_info->balance_ctl) {
3361 ret = -ENOTCONN;
3362 goto out_bargs;
3363 }
3364
3365 bctl = fs_info->balance_ctl;
3366 spin_lock(&fs_info->balance_lock);
3367 bctl->flags |= BTRFS_BALANCE_RESUME;
3368 spin_unlock(&fs_info->balance_lock);
3369
3370 goto do_balance;
3371 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003372 } else {
3373 bargs = NULL;
3374 }
3375
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003376 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
3377 1)) {
3378 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003379 ret = -EINPROGRESS;
3380 goto out_bargs;
3381 }
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003382 need_to_clear_lock = 1;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003383
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003384 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3385 if (!bctl) {
3386 ret = -ENOMEM;
3387 goto out_bargs;
3388 }
3389
3390 bctl->fs_info = fs_info;
3391 if (arg) {
3392 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3393 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3394 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3395
3396 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003397 } else {
3398 /* balance everything - no filters */
3399 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003400 }
3401
Ilya Dryomovde322262012-01-16 22:04:49 +02003402do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003403 ret = btrfs_balance(bctl, bargs);
3404 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003405 * bctl is freed in __cancel_balance or in free_fs_info if
3406 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003407 */
3408 if (arg) {
3409 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3410 ret = -EFAULT;
3411 }
3412
3413out_bargs:
3414 kfree(bargs);
3415out:
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003416 if (need_to_clear_lock)
3417 atomic_set(&root->fs_info->mutually_exclusive_operation_running,
3418 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003419 mutex_unlock(&fs_info->balance_mutex);
3420 mutex_unlock(&fs_info->volume_mutex);
Liu Boe54bfa32012-06-29 03:58:48 -06003421 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003422 return ret;
3423}
3424
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003425static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3426{
3427 if (!capable(CAP_SYS_ADMIN))
3428 return -EPERM;
3429
3430 switch (cmd) {
3431 case BTRFS_BALANCE_CTL_PAUSE:
3432 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003433 case BTRFS_BALANCE_CTL_CANCEL:
3434 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003435 }
3436
3437 return -EINVAL;
3438}
3439
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003440static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3441 void __user *arg)
3442{
3443 struct btrfs_fs_info *fs_info = root->fs_info;
3444 struct btrfs_ioctl_balance_args *bargs;
3445 int ret = 0;
3446
3447 if (!capable(CAP_SYS_ADMIN))
3448 return -EPERM;
3449
3450 mutex_lock(&fs_info->balance_mutex);
3451 if (!fs_info->balance_ctl) {
3452 ret = -ENOTCONN;
3453 goto out;
3454 }
3455
3456 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3457 if (!bargs) {
3458 ret = -ENOMEM;
3459 goto out;
3460 }
3461
3462 update_ioctl_balance_args(fs_info, 1, bargs);
3463
3464 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3465 ret = -EFAULT;
3466
3467 kfree(bargs);
3468out:
3469 mutex_unlock(&fs_info->balance_mutex);
3470 return ret;
3471}
3472
Arne Jansen5d13a372011-09-14 15:53:51 +02003473static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3474{
3475 struct btrfs_ioctl_quota_ctl_args *sa;
3476 struct btrfs_trans_handle *trans = NULL;
3477 int ret;
3478 int err;
3479
3480 if (!capable(CAP_SYS_ADMIN))
3481 return -EPERM;
3482
3483 if (root->fs_info->sb->s_flags & MS_RDONLY)
3484 return -EROFS;
3485
3486 sa = memdup_user(arg, sizeof(*sa));
3487 if (IS_ERR(sa))
3488 return PTR_ERR(sa);
3489
3490 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3491 trans = btrfs_start_transaction(root, 2);
3492 if (IS_ERR(trans)) {
3493 ret = PTR_ERR(trans);
3494 goto out;
3495 }
3496 }
3497
3498 switch (sa->cmd) {
3499 case BTRFS_QUOTA_CTL_ENABLE:
3500 ret = btrfs_quota_enable(trans, root->fs_info);
3501 break;
3502 case BTRFS_QUOTA_CTL_DISABLE:
3503 ret = btrfs_quota_disable(trans, root->fs_info);
3504 break;
3505 case BTRFS_QUOTA_CTL_RESCAN:
3506 ret = btrfs_quota_rescan(root->fs_info);
3507 break;
3508 default:
3509 ret = -EINVAL;
3510 break;
3511 }
3512
3513 if (copy_to_user(arg, sa, sizeof(*sa)))
3514 ret = -EFAULT;
3515
3516 if (trans) {
3517 err = btrfs_commit_transaction(trans, root);
3518 if (err && !ret)
3519 ret = err;
3520 }
3521
3522out:
3523 kfree(sa);
3524 return ret;
3525}
3526
3527static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3528{
3529 struct btrfs_ioctl_qgroup_assign_args *sa;
3530 struct btrfs_trans_handle *trans;
3531 int ret;
3532 int err;
3533
3534 if (!capable(CAP_SYS_ADMIN))
3535 return -EPERM;
3536
3537 if (root->fs_info->sb->s_flags & MS_RDONLY)
3538 return -EROFS;
3539
3540 sa = memdup_user(arg, sizeof(*sa));
3541 if (IS_ERR(sa))
3542 return PTR_ERR(sa);
3543
3544 trans = btrfs_join_transaction(root);
3545 if (IS_ERR(trans)) {
3546 ret = PTR_ERR(trans);
3547 goto out;
3548 }
3549
3550 /* FIXME: check if the IDs really exist */
3551 if (sa->assign) {
3552 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3553 sa->src, sa->dst);
3554 } else {
3555 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3556 sa->src, sa->dst);
3557 }
3558
3559 err = btrfs_end_transaction(trans, root);
3560 if (err && !ret)
3561 ret = err;
3562
3563out:
3564 kfree(sa);
3565 return ret;
3566}
3567
3568static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3569{
3570 struct btrfs_ioctl_qgroup_create_args *sa;
3571 struct btrfs_trans_handle *trans;
3572 int ret;
3573 int err;
3574
3575 if (!capable(CAP_SYS_ADMIN))
3576 return -EPERM;
3577
3578 if (root->fs_info->sb->s_flags & MS_RDONLY)
3579 return -EROFS;
3580
3581 sa = memdup_user(arg, sizeof(*sa));
3582 if (IS_ERR(sa))
3583 return PTR_ERR(sa);
3584
3585 trans = btrfs_join_transaction(root);
3586 if (IS_ERR(trans)) {
3587 ret = PTR_ERR(trans);
3588 goto out;
3589 }
3590
3591 /* FIXME: check if the IDs really exist */
3592 if (sa->create) {
3593 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3594 NULL);
3595 } else {
3596 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3597 }
3598
3599 err = btrfs_end_transaction(trans, root);
3600 if (err && !ret)
3601 ret = err;
3602
3603out:
3604 kfree(sa);
3605 return ret;
3606}
3607
3608static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3609{
3610 struct btrfs_ioctl_qgroup_limit_args *sa;
3611 struct btrfs_trans_handle *trans;
3612 int ret;
3613 int err;
3614 u64 qgroupid;
3615
3616 if (!capable(CAP_SYS_ADMIN))
3617 return -EPERM;
3618
3619 if (root->fs_info->sb->s_flags & MS_RDONLY)
3620 return -EROFS;
3621
3622 sa = memdup_user(arg, sizeof(*sa));
3623 if (IS_ERR(sa))
3624 return PTR_ERR(sa);
3625
3626 trans = btrfs_join_transaction(root);
3627 if (IS_ERR(trans)) {
3628 ret = PTR_ERR(trans);
3629 goto out;
3630 }
3631
3632 qgroupid = sa->qgroupid;
3633 if (!qgroupid) {
3634 /* take the current subvol as qgroup */
3635 qgroupid = root->root_key.objectid;
3636 }
3637
3638 /* FIXME: check if the IDs really exist */
3639 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3640
3641 err = btrfs_end_transaction(trans, root);
3642 if (err && !ret)
3643 ret = err;
3644
3645out:
3646 kfree(sa);
3647 return ret;
3648}
3649
Alexander Block8ea05e32012-07-25 17:35:53 +02003650static long btrfs_ioctl_set_received_subvol(struct file *file,
3651 void __user *arg)
3652{
3653 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3654 struct inode *inode = fdentry(file)->d_inode;
3655 struct btrfs_root *root = BTRFS_I(inode)->root;
3656 struct btrfs_root_item *root_item = &root->root_item;
3657 struct btrfs_trans_handle *trans;
3658 struct timespec ct = CURRENT_TIME;
3659 int ret = 0;
3660
3661 ret = mnt_want_write_file(file);
3662 if (ret < 0)
3663 return ret;
3664
3665 down_write(&root->fs_info->subvol_sem);
3666
3667 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3668 ret = -EINVAL;
3669 goto out;
3670 }
3671
3672 if (btrfs_root_readonly(root)) {
3673 ret = -EROFS;
3674 goto out;
3675 }
3676
3677 if (!inode_owner_or_capable(inode)) {
3678 ret = -EACCES;
3679 goto out;
3680 }
3681
3682 sa = memdup_user(arg, sizeof(*sa));
3683 if (IS_ERR(sa)) {
3684 ret = PTR_ERR(sa);
3685 sa = NULL;
3686 goto out;
3687 }
3688
3689 trans = btrfs_start_transaction(root, 1);
3690 if (IS_ERR(trans)) {
3691 ret = PTR_ERR(trans);
3692 trans = NULL;
3693 goto out;
3694 }
3695
3696 sa->rtransid = trans->transid;
3697 sa->rtime.sec = ct.tv_sec;
3698 sa->rtime.nsec = ct.tv_nsec;
3699
3700 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3701 btrfs_set_root_stransid(root_item, sa->stransid);
3702 btrfs_set_root_rtransid(root_item, sa->rtransid);
3703 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3704 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3705 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3706 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3707
3708 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3709 &root->root_key, &root->root_item);
3710 if (ret < 0) {
3711 btrfs_end_transaction(trans, root);
3712 trans = NULL;
3713 goto out;
3714 } else {
3715 ret = btrfs_commit_transaction(trans, root);
3716 if (ret < 0)
3717 goto out;
3718 }
3719
3720 ret = copy_to_user(arg, sa, sizeof(*sa));
3721 if (ret)
3722 ret = -EFAULT;
3723
3724out:
3725 kfree(sa);
3726 up_write(&root->fs_info->subvol_sem);
3727 mnt_drop_write_file(file);
3728 return ret;
3729}
3730
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003731long btrfs_ioctl(struct file *file, unsigned int
3732 cmd, unsigned long arg)
3733{
3734 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003735 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003736
3737 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003738 case FS_IOC_GETFLAGS:
3739 return btrfs_ioctl_getflags(file, argp);
3740 case FS_IOC_SETFLAGS:
3741 return btrfs_ioctl_setflags(file, argp);
3742 case FS_IOC_GETVERSION:
3743 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003744 case FITRIM:
3745 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003746 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003747 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003748 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003749 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003750 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003751 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003752 case BTRFS_IOC_SUBVOL_CREATE_V2:
3753 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003754 case BTRFS_IOC_SNAP_DESTROY:
3755 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003756 case BTRFS_IOC_SUBVOL_GETFLAGS:
3757 return btrfs_ioctl_subvol_getflags(file, argp);
3758 case BTRFS_IOC_SUBVOL_SETFLAGS:
3759 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003760 case BTRFS_IOC_DEFAULT_SUBVOL:
3761 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003762 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003763 return btrfs_ioctl_defrag(file, NULL);
3764 case BTRFS_IOC_DEFRAG_RANGE:
3765 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003766 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003767 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003768 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003769 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003770 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003771 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003772 case BTRFS_IOC_FS_INFO:
3773 return btrfs_ioctl_fs_info(root, argp);
3774 case BTRFS_IOC_DEV_INFO:
3775 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003776 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003777 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003778 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003779 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3780 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003781 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003782 case BTRFS_IOC_TRANS_START:
3783 return btrfs_ioctl_trans_start(file);
3784 case BTRFS_IOC_TRANS_END:
3785 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003786 case BTRFS_IOC_TREE_SEARCH:
3787 return btrfs_ioctl_tree_search(file, argp);
3788 case BTRFS_IOC_INO_LOOKUP:
3789 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003790 case BTRFS_IOC_INO_PATHS:
3791 return btrfs_ioctl_ino_to_path(root, argp);
3792 case BTRFS_IOC_LOGICAL_INO:
3793 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003794 case BTRFS_IOC_SPACE_INFO:
3795 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003796 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003797 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3798 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003799 case BTRFS_IOC_START_SYNC:
3800 return btrfs_ioctl_start_sync(file, argp);
3801 case BTRFS_IOC_WAIT_SYNC:
3802 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003803 case BTRFS_IOC_SCRUB:
3804 return btrfs_ioctl_scrub(root, argp);
3805 case BTRFS_IOC_SCRUB_CANCEL:
3806 return btrfs_ioctl_scrub_cancel(root, argp);
3807 case BTRFS_IOC_SCRUB_PROGRESS:
3808 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003809 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003810 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003811 case BTRFS_IOC_BALANCE_CTL:
3812 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003813 case BTRFS_IOC_BALANCE_PROGRESS:
3814 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003815 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3816 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02003817 case BTRFS_IOC_SEND:
3818 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003819 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06003820 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003821 case BTRFS_IOC_QUOTA_CTL:
3822 return btrfs_ioctl_quota_ctl(root, argp);
3823 case BTRFS_IOC_QGROUP_ASSIGN:
3824 return btrfs_ioctl_qgroup_assign(root, argp);
3825 case BTRFS_IOC_QGROUP_CREATE:
3826 return btrfs_ioctl_qgroup_create(root, argp);
3827 case BTRFS_IOC_QGROUP_LIMIT:
3828 return btrfs_ioctl_qgroup_limit(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003829 }
3830
3831 return -ENOTTY;
3832}