blob: da518ded34bd620646087772d64cd256c346ebf7 [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 Zefan306424cc2011-12-14 20:12:02 -0500289 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400290 inode_inc_iversion(inode);
Li Zefan306424cc2011-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 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400574 BUG_ON(ret);
575
576 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400577 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000578 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400579
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500580 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
581 if (ret)
582 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400583
Al Viro2fbe8c82011-07-16 21:38:06 -0400584 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000585 if (IS_ERR(inode)) {
586 ret = PTR_ERR(inode);
587 goto fail;
588 }
589 BUG_ON(!inode);
590 d_instantiate(dentry, inode);
591 ret = 0;
592fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400593 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 return ret;
595}
596
Sage Weil4260f7c2010-10-29 15:46:43 -0400597/* copy of check_sticky in fs/namei.c()
598* It's inline, so penalty for filesystems that don't use sticky bit is
599* minimal.
600*/
601static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
602{
603 uid_t fsuid = current_fsuid();
604
605 if (!(dir->i_mode & S_ISVTX))
606 return 0;
607 if (inode->i_uid == fsuid)
608 return 0;
609 if (dir->i_uid == fsuid)
610 return 0;
611 return !capable(CAP_FOWNER);
612}
613
614/* copy of may_delete in fs/namei.c()
615 * Check whether we can remove a link victim from directory dir, check
616 * whether the type of victim is right.
617 * 1. We can't do it if dir is read-only (done in permission())
618 * 2. We should have write and exec permissions on dir
619 * 3. We can't remove anything from append-only dir
620 * 4. We can't do anything with immutable dir (done in permission())
621 * 5. If the sticky bit on dir is set we should either
622 * a. be owner of dir, or
623 * b. be owner of victim, or
624 * c. have CAP_FOWNER capability
625 * 6. If the victim is append-only or immutable we can't do antyhing with
626 * links pointing to it.
627 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
628 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
629 * 9. We can't remove a root or mountpoint.
630 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
631 * nfs_async_unlink().
632 */
633
634static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
635{
636 int error;
637
638 if (!victim->d_inode)
639 return -ENOENT;
640
641 BUG_ON(victim->d_parent->d_inode != dir);
642 audit_inode_child(victim, dir);
643
644 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
645 if (error)
646 return error;
647 if (IS_APPEND(dir))
648 return -EPERM;
649 if (btrfs_check_sticky(dir, victim->d_inode)||
650 IS_APPEND(victim->d_inode)||
651 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
652 return -EPERM;
653 if (isdir) {
654 if (!S_ISDIR(victim->d_inode->i_mode))
655 return -ENOTDIR;
656 if (IS_ROOT(victim))
657 return -EBUSY;
658 } else if (S_ISDIR(victim->d_inode->i_mode))
659 return -EISDIR;
660 if (IS_DEADDIR(dir))
661 return -ENOENT;
662 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
663 return -EBUSY;
664 return 0;
665}
666
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400667/* copy of may_create in fs/namei.c() */
668static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
669{
670 if (child->d_inode)
671 return -EEXIST;
672 if (IS_DEADDIR(dir))
673 return -ENOENT;
674 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
675}
676
677/*
678 * Create a new subvolume below @parent. This is largely modeled after
679 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
680 * inside this filesystem so it's quite a bit simpler.
681 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400682static noinline int btrfs_mksubvol(struct path *parent,
683 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400684 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200685 u64 *async_transid, bool readonly,
686 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400687{
Yan, Zheng76dda932009-09-21 16:00:26 -0400688 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400689 struct dentry *dentry;
690 int error;
691
Yan, Zheng76dda932009-09-21 16:00:26 -0400692 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400693
694 dentry = lookup_one_len(name, parent->dentry, namelen);
695 error = PTR_ERR(dentry);
696 if (IS_ERR(dentry))
697 goto out_unlock;
698
699 error = -EEXIST;
700 if (dentry->d_inode)
701 goto out_dput;
702
Yan, Zheng76dda932009-09-21 16:00:26 -0400703 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400704 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600705 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400706
Yan, Zheng76dda932009-09-21 16:00:26 -0400707 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
708
709 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
710 goto out_up_read;
711
Chris Mason3de45862008-11-17 21:02:50 -0500712 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200713 error = create_snapshot(snap_src, dentry, name, namelen,
714 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500715 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400716 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200717 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500718 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400719 if (!error)
720 fsnotify_mkdir(dir, dentry);
721out_up_read:
722 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400723out_dput:
724 dput(dentry);
725out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400726 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400727 return error;
728}
729
Chris Mason4cb53002011-05-24 15:35:30 -0400730/*
731 * When we're defragging a range, we don't want to kick it off again
732 * if it is really just waiting for delalloc to send it down.
733 * If we find a nice big extent or delalloc range for the bytes in the
734 * file you want to defrag, we return 0 to let you know to skip this
735 * part of the file
736 */
737static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
738{
739 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
740 struct extent_map *em = NULL;
741 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
742 u64 end;
743
744 read_lock(&em_tree->lock);
745 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
746 read_unlock(&em_tree->lock);
747
748 if (em) {
749 end = extent_map_end(em);
750 free_extent_map(em);
751 if (end - offset > thresh)
752 return 0;
753 }
754 /* if we already have a nice delalloc here, just stop */
755 thresh /= 2;
756 end = count_range_bits(io_tree, &offset, offset + thresh,
757 thresh, EXTENT_DELALLOC, 1);
758 if (end >= thresh)
759 return 0;
760 return 1;
761}
762
763/*
764 * helper function to walk through a file and find extents
765 * newer than a specific transid, and smaller than thresh.
766 *
767 * This is used by the defragging code to find new and small
768 * extents
769 */
770static int find_new_extents(struct btrfs_root *root,
771 struct inode *inode, u64 newer_than,
772 u64 *off, int thresh)
773{
774 struct btrfs_path *path;
775 struct btrfs_key min_key;
776 struct btrfs_key max_key;
777 struct extent_buffer *leaf;
778 struct btrfs_file_extent_item *extent;
779 int type;
780 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000781 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400782
783 path = btrfs_alloc_path();
784 if (!path)
785 return -ENOMEM;
786
David Sterbaa4689d22011-05-31 17:08:14 +0000787 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400788 min_key.type = BTRFS_EXTENT_DATA_KEY;
789 min_key.offset = *off;
790
David Sterbaa4689d22011-05-31 17:08:14 +0000791 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400792 max_key.type = (u8)-1;
793 max_key.offset = (u64)-1;
794
795 path->keep_locks = 1;
796
797 while(1) {
798 ret = btrfs_search_forward(root, &min_key, &max_key,
799 path, 0, newer_than);
800 if (ret != 0)
801 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000802 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400803 goto none;
804 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
805 goto none;
806
807 leaf = path->nodes[0];
808 extent = btrfs_item_ptr(leaf, path->slots[0],
809 struct btrfs_file_extent_item);
810
811 type = btrfs_file_extent_type(leaf, extent);
812 if (type == BTRFS_FILE_EXTENT_REG &&
813 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
814 check_defrag_in_cache(inode, min_key.offset, thresh)) {
815 *off = min_key.offset;
816 btrfs_free_path(path);
817 return 0;
818 }
819
820 if (min_key.offset == (u64)-1)
821 goto none;
822
823 min_key.offset++;
824 btrfs_release_path(path);
825 }
826none:
827 btrfs_free_path(path);
828 return -ENOENT;
829}
830
Li Zefan6c282eb2012-06-11 16:03:35 +0800831static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400832{
833 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500834 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800835 struct extent_map *em;
836 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500837
838 /*
839 * hopefully we have this extent in the tree already, try without
840 * the full extent lock
841 */
842 read_lock(&em_tree->lock);
843 em = lookup_extent_mapping(em_tree, start, len);
844 read_unlock(&em_tree->lock);
845
846 if (!em) {
847 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100848 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500849 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100850 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500851
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000852 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800853 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500854 }
855
Li Zefan6c282eb2012-06-11 16:03:35 +0800856 return em;
857}
858
859static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
860{
861 struct extent_map *next;
862 bool ret = true;
863
864 /* this is the last extent */
865 if (em->start + em->len >= i_size_read(inode))
866 return false;
867
868 next = defrag_lookup_extent(inode, em->start + em->len);
869 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
870 ret = false;
871
872 free_extent_map(next);
873 return ret;
874}
875
876static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400877 u64 *last_len, u64 *skip, u64 *defrag_end,
878 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800879{
880 struct extent_map *em;
881 int ret = 1;
882 bool next_mergeable = true;
883
884 /*
885 * make sure that once we start defragging an extent, we keep on
886 * defragging it
887 */
888 if (start < *defrag_end)
889 return 1;
890
891 *skip = 0;
892
893 em = defrag_lookup_extent(inode, start);
894 if (!em)
895 return 0;
896
Chris Mason940100a2010-03-10 10:52:59 -0500897 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400898 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500899 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400900 goto out;
901 }
902
Li Zefan6c282eb2012-06-11 16:03:35 +0800903 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500904
905 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800906 * we hit a real extent, if it is big or the next extent is not a
907 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500908 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400909 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800910 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500911 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400912out:
Chris Mason940100a2010-03-10 10:52:59 -0500913 /*
914 * last_len ends up being a counter of how many bytes we've defragged.
915 * every time we choose not to defrag an extent, we reset *last_len
916 * so that the next tiny extent will force a defrag.
917 *
918 * The end result of this is that tiny extents before a single big
919 * extent will force at least part of that big extent to be defragged.
920 */
921 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500922 *defrag_end = extent_map_end(em);
923 } else {
924 *last_len = 0;
925 *skip = extent_map_end(em);
926 *defrag_end = 0;
927 }
928
929 free_extent_map(em);
930 return ret;
931}
932
Chris Mason4cb53002011-05-24 15:35:30 -0400933/*
934 * it doesn't do much good to defrag one or two pages
935 * at a time. This pulls in a nice chunk of pages
936 * to COW and defrag.
937 *
938 * It also makes sure the delalloc code has enough
939 * dirty data to avoid making new small extents as part
940 * of the defrag
941 *
942 * It's a good idea to start RA on this range
943 * before calling this.
944 */
945static int cluster_pages_for_defrag(struct inode *inode,
946 struct page **pages,
947 unsigned long start_index,
948 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400949{
Chris Mason4cb53002011-05-24 15:35:30 -0400950 unsigned long file_end;
951 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400952 u64 page_start;
953 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400954 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400955 int ret;
956 int i;
957 int i_done;
958 struct btrfs_ordered_extent *ordered;
959 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800960 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400961 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400962
Chris Mason4cb53002011-05-24 15:35:30 -0400963 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400964 if (!isize || start_index > file_end)
965 return 0;
966
967 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400968
969 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400970 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400971 if (ret)
972 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400973 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800974 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400975
976 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400977 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400978 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800979again:
Josef Bacika94733d2011-07-11 10:47:06 -0400980 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800981 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400982 if (!page)
983 break;
984
Miao Xie600a45e2012-02-16 15:01:24 +0800985 page_start = page_offset(page);
986 page_end = page_start + PAGE_CACHE_SIZE - 1;
987 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100988 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800989 ordered = btrfs_lookup_ordered_extent(inode,
990 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100991 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800992 if (!ordered)
993 break;
994
995 unlock_page(page);
996 btrfs_start_ordered_extent(inode, ordered, 1);
997 btrfs_put_ordered_extent(ordered);
998 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400999 /*
1000 * we unlocked the page above, so we need check if
1001 * it was released or not.
1002 */
1003 if (page->mapping != inode->i_mapping) {
1004 unlock_page(page);
1005 page_cache_release(page);
1006 goto again;
1007 }
Miao Xie600a45e2012-02-16 15:01:24 +08001008 }
1009
Chris Mason4cb53002011-05-24 15:35:30 -04001010 if (!PageUptodate(page)) {
1011 btrfs_readpage(NULL, page);
1012 lock_page(page);
1013 if (!PageUptodate(page)) {
1014 unlock_page(page);
1015 page_cache_release(page);
1016 ret = -EIO;
1017 break;
1018 }
1019 }
Miao Xie600a45e2012-02-16 15:01:24 +08001020
Miao Xie600a45e2012-02-16 15:01:24 +08001021 if (page->mapping != inode->i_mapping) {
1022 unlock_page(page);
1023 page_cache_release(page);
1024 goto again;
1025 }
1026
Chris Mason4cb53002011-05-24 15:35:30 -04001027 pages[i] = page;
1028 i_done++;
1029 }
1030 if (!i_done || ret)
1031 goto out;
1032
1033 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1034 goto out;
1035
1036 /*
1037 * so now we have a nice long stream of locked
1038 * and up to date pages, lets wait on them
1039 */
1040 for (i = 0; i < i_done; i++)
1041 wait_on_page_writeback(pages[i]);
1042
1043 page_start = page_offset(pages[0]);
1044 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1045
1046 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001047 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001048 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1049 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001050 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1051 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001052
Liu Bo1f12bd02012-03-29 09:57:44 -04001053 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001054 spin_lock(&BTRFS_I(inode)->lock);
1055 BTRFS_I(inode)->outstanding_extents++;
1056 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001057 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001058 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001059 }
1060
1061
Liu Bo9e8a4a82012-09-05 19:10:51 -06001062 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1063 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001064
1065 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1066 page_start, page_end - 1, &cached_state,
1067 GFP_NOFS);
1068
1069 for (i = 0; i < i_done; i++) {
1070 clear_page_dirty_for_io(pages[i]);
1071 ClearPageChecked(pages[i]);
1072 set_page_extent_mapped(pages[i]);
1073 set_page_dirty(pages[i]);
1074 unlock_page(pages[i]);
1075 page_cache_release(pages[i]);
1076 }
1077 return i_done;
1078out:
1079 for (i = 0; i < i_done; i++) {
1080 unlock_page(pages[i]);
1081 page_cache_release(pages[i]);
1082 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001083 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001084 return ret;
1085
1086}
1087
1088int btrfs_defrag_file(struct inode *inode, struct file *file,
1089 struct btrfs_ioctl_defrag_range_args *range,
1090 u64 newer_than, unsigned long max_to_defrag)
1091{
1092 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001093 struct file_ra_state *ra = NULL;
1094 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001095 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001096 u64 last_len = 0;
1097 u64 skip = 0;
1098 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001099 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001100 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001101 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001102 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001103 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001104 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001105 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001106 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1107 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001108 u64 new_align = ~((u64)128 * 1024 - 1);
1109 struct page **pages = NULL;
1110
1111 if (extent_thresh == 0)
1112 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001113
1114 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1115 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1116 return -EINVAL;
1117 if (range->compress_type)
1118 compress_type = range->compress_type;
1119 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001120
Li Zefan151a31b2011-09-02 15:56:39 +08001121 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001122 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001123
Chris Mason4cb53002011-05-24 15:35:30 -04001124 /*
1125 * if we were not given a file, allocate a readahead
1126 * context
1127 */
1128 if (!file) {
1129 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1130 if (!ra)
1131 return -ENOMEM;
1132 file_ra_state_init(ra, inode->i_mapping);
1133 } else {
1134 ra = &file->f_ra;
1135 }
1136
Li Zefan008873e2011-09-02 15:57:07 +08001137 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001138 GFP_NOFS);
1139 if (!pages) {
1140 ret = -ENOMEM;
1141 goto out_ra;
1142 }
1143
1144 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001145 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001146 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001147 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1148 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001149 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001150 }
1151
Chris Mason4cb53002011-05-24 15:35:30 -04001152 if (newer_than) {
1153 ret = find_new_extents(root, inode, newer_than,
1154 &newer_off, 64 * 1024);
1155 if (!ret) {
1156 range->start = newer_off;
1157 /*
1158 * we always align our defrag to help keep
1159 * the extents in the file evenly spaced
1160 */
1161 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001162 } else
1163 goto out_ra;
1164 } else {
1165 i = range->start >> PAGE_CACHE_SHIFT;
1166 }
1167 if (!max_to_defrag)
Liu Bo7ec31b542012-01-26 15:01:12 -05001168 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001169
Li Zefan2a0f7f52011-10-10 15:43:34 -04001170 /*
1171 * make writeback starts from i, so the defrag range can be
1172 * written sequentially.
1173 */
1174 if (i < inode->i_mapping->writeback_index)
1175 inode->i_mapping->writeback_index = i;
1176
Chris Masonf7f43cc2011-10-11 11:41:40 -04001177 while (i <= last_index && defrag_count < max_to_defrag &&
1178 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1179 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001180 /*
1181 * make sure we stop running if someone unmounts
1182 * the FS
1183 */
1184 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1185 break;
1186
Liu Bo66c26892012-03-29 09:57:45 -04001187 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001188 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001189 &defrag_end, range->flags &
1190 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001191 unsigned long next;
1192 /*
1193 * the should_defrag function tells us how much to skip
1194 * bump our counter by the suggested amount
1195 */
1196 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1197 i = max(i + 1, next);
1198 continue;
1199 }
Li Zefan008873e2011-09-02 15:57:07 +08001200
1201 if (!newer_than) {
1202 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1203 PAGE_CACHE_SHIFT) - i;
1204 cluster = min(cluster, max_cluster);
1205 } else {
1206 cluster = max_cluster;
1207 }
1208
Chris Mason1e701a32010-03-11 09:42:04 -05001209 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001210 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001211
Li Zefan008873e2011-09-02 15:57:07 +08001212 if (i + cluster > ra_index) {
1213 ra_index = max(i, ra_index);
1214 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1215 cluster);
1216 ra_index += max_cluster;
1217 }
Chris Mason940100a2010-03-10 10:52:59 -05001218
Liu Boecb8bea2012-03-29 09:57:44 -04001219 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001220 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001221 if (ret < 0) {
1222 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001223 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001224 }
Chris Mason940100a2010-03-10 10:52:59 -05001225
Chris Mason4cb53002011-05-24 15:35:30 -04001226 defrag_count += ret;
1227 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001228 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001229
1230 if (newer_than) {
1231 if (newer_off == (u64)-1)
1232 break;
1233
Liu Boe1f041e2012-03-29 09:57:45 -04001234 if (ret > 0)
1235 i += ret;
1236
Chris Mason4cb53002011-05-24 15:35:30 -04001237 newer_off = max(newer_off + 1,
1238 (u64)i << PAGE_CACHE_SHIFT);
1239
1240 ret = find_new_extents(root, inode,
1241 newer_than, &newer_off,
1242 64 * 1024);
1243 if (!ret) {
1244 range->start = newer_off;
1245 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001246 } else {
1247 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001248 }
Chris Mason4cb53002011-05-24 15:35:30 -04001249 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001250 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001251 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001252 last_len += ret << PAGE_CACHE_SHIFT;
1253 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001254 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001255 last_len = 0;
1256 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001257 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001258 }
1259
Chris Mason1e701a32010-03-11 09:42:04 -05001260 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1261 filemap_flush(inode->i_mapping);
1262
1263 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1264 /* the filemap_flush will queue IO into the worker threads, but
1265 * we have to make sure the IO is actually started and that
1266 * ordered extents get created before we return
1267 */
1268 atomic_inc(&root->fs_info->async_submit_draining);
1269 while (atomic_read(&root->fs_info->nr_async_submits) ||
1270 atomic_read(&root->fs_info->async_delalloc_pages)) {
1271 wait_event(root->fs_info->async_submit_wait,
1272 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1273 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1274 }
1275 atomic_dec(&root->fs_info->async_submit_draining);
1276
1277 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001278 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001279 mutex_unlock(&inode->i_mutex);
1280 }
1281
Li Zefan1a419d82010-10-25 15:12:50 +08001282 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001283 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001284 }
1285
Diego Calleja60ccf822011-09-01 16:33:57 +02001286 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001287
Chris Mason4cb53002011-05-24 15:35:30 -04001288out_ra:
1289 if (!file)
1290 kfree(ra);
1291 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001292 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001293}
1294
Yan, Zheng76dda932009-09-21 16:00:26 -04001295static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1296 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001297{
1298 u64 new_size;
1299 u64 old_size;
1300 u64 devid = 1;
1301 struct btrfs_ioctl_vol_args *vol_args;
1302 struct btrfs_trans_handle *trans;
1303 struct btrfs_device *device = NULL;
1304 char *sizestr;
1305 char *devstr = NULL;
1306 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001307 int mod = 0;
1308
Yan Zhengc146afa2008-11-12 14:34:12 -05001309 if (root->fs_info->sb->s_flags & MS_RDONLY)
1310 return -EROFS;
1311
Chris Masone441d542009-01-05 16:57:23 -05001312 if (!capable(CAP_SYS_ADMIN))
1313 return -EPERM;
1314
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001315 mutex_lock(&root->fs_info->volume_mutex);
1316 if (root->fs_info->balance_ctl) {
1317 printk(KERN_INFO "btrfs: balance in progress\n");
1318 ret = -EINVAL;
1319 goto out;
1320 }
1321
Li Zefandae7b662009-04-08 15:06:54 +08001322 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001323 if (IS_ERR(vol_args)) {
1324 ret = PTR_ERR(vol_args);
1325 goto out;
1326 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001327
1328 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001329
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330 sizestr = vol_args->name;
1331 devstr = strchr(sizestr, ':');
1332 if (devstr) {
1333 char *end;
1334 sizestr = devstr + 1;
1335 *devstr = '\0';
1336 devstr = vol_args->name;
1337 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001338 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001339 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001340 }
Yan Zheng2b820322008-11-17 21:11:30 -05001341 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001342 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001343 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001344 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001345 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001346 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001347 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001348 if (device->fs_devices && device->fs_devices->seeding) {
1349 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001350 "seeding device %llu\n",
1351 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001352 ret = -EINVAL;
1353 goto out_free;
1354 }
1355
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001356 if (!strcmp(sizestr, "max"))
1357 new_size = device->bdev->bd_inode->i_size;
1358 else {
1359 if (sizestr[0] == '-') {
1360 mod = -1;
1361 sizestr++;
1362 } else if (sizestr[0] == '+') {
1363 mod = 1;
1364 sizestr++;
1365 }
Akinobu Mita91748462010-02-28 10:59:11 +00001366 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001367 if (new_size == 0) {
1368 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001369 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001370 }
1371 }
1372
1373 old_size = device->total_bytes;
1374
1375 if (mod < 0) {
1376 if (new_size > old_size) {
1377 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001378 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001379 }
1380 new_size = old_size - new_size;
1381 } else if (mod > 0) {
1382 new_size = old_size + new_size;
1383 }
1384
1385 if (new_size < 256 * 1024 * 1024) {
1386 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001387 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001388 }
1389 if (new_size > device->bdev->bd_inode->i_size) {
1390 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001391 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001392 }
1393
1394 do_div(new_size, root->sectorsize);
1395 new_size *= root->sectorsize;
1396
Josef Bacik606686e2012-06-04 14:03:51 -04001397 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1398 rcu_str_deref(device->name),
1399 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001400
1401 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001402 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001403 if (IS_ERR(trans)) {
1404 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001405 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001406 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001407 ret = btrfs_grow_device(trans, device, new_size);
1408 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001409 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001410 ret = btrfs_shrink_device(device, new_size);
1411 }
1412
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001413out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001414 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001415out:
1416 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001417 return ret;
1418}
1419
Sage Weil72fd0322010-10-29 15:41:32 -04001420static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001421 char *name, unsigned long fd, int subvol,
1422 u64 *transid, bool readonly,
1423 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001424{
Chris Mason3de45862008-11-17 21:02:50 -05001425 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001426 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001427 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428
Liu Boa874a632012-06-29 03:58:46 -06001429 ret = mnt_want_write_file(file);
1430 if (ret)
1431 goto out;
1432
Sage Weil72fd0322010-10-29 15:41:32 -04001433 namelen = strlen(name);
1434 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001436 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001437 }
1438
Chris Mason16780ca2012-02-20 22:14:55 -05001439 if (name[0] == '.' &&
1440 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1441 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001442 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001443 }
1444
Chris Mason3de45862008-11-17 21:02:50 -05001445 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001446 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001447 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001448 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001449 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001450 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001451 if (!src_file) {
1452 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001453 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001454 }
1455
1456 src_inode = src_file->f_path.dentry->d_inode;
1457 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001458 printk(KERN_INFO "btrfs: Snapshot src from "
1459 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001460 ret = -EINVAL;
1461 fput(src_file);
Liu Boa874a632012-06-29 03:58:46 -06001462 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001463 }
Sage Weil72fd0322010-10-29 15:41:32 -04001464 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1465 BTRFS_I(src_inode)->root,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001466 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001467 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001468 }
Liu Boa874a632012-06-29 03:58:46 -06001469out_drop_write:
1470 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001471out:
Sage Weil72fd0322010-10-29 15:41:32 -04001472 return ret;
1473}
1474
1475static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001476 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001477{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001478 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001479 int ret;
1480
Li Zefanfa0d2b92010-12-20 15:53:28 +08001481 vol_args = memdup_user(arg, sizeof(*vol_args));
1482 if (IS_ERR(vol_args))
1483 return PTR_ERR(vol_args);
1484 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001485
Li Zefanfa0d2b92010-12-20 15:53:28 +08001486 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001487 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001488 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001489
Li Zefanfa0d2b92010-12-20 15:53:28 +08001490 kfree(vol_args);
1491 return ret;
1492}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001493
Li Zefanfa0d2b92010-12-20 15:53:28 +08001494static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1495 void __user *arg, int subvol)
1496{
1497 struct btrfs_ioctl_vol_args_v2 *vol_args;
1498 int ret;
1499 u64 transid = 0;
1500 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001501 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001502 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001503
Li Zefanfa0d2b92010-12-20 15:53:28 +08001504 vol_args = memdup_user(arg, sizeof(*vol_args));
1505 if (IS_ERR(vol_args))
1506 return PTR_ERR(vol_args);
1507 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001508
Li Zefanb83cc962010-12-20 16:04:08 +08001509 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001510 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1511 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001512 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001513 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001514 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001515
1516 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1517 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001518 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1519 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001520 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1521 if (vol_args->size > PAGE_CACHE_SIZE) {
1522 ret = -EINVAL;
1523 goto out;
1524 }
1525 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1526 if (IS_ERR(inherit)) {
1527 ret = PTR_ERR(inherit);
1528 goto out;
1529 }
1530 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001531
1532 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001533 vol_args->fd, subvol, ptr,
1534 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001535
1536 if (ret == 0 && ptr &&
1537 copy_to_user(arg +
1538 offsetof(struct btrfs_ioctl_vol_args_v2,
1539 transid), ptr, sizeof(*ptr)))
1540 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001541out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001542 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001543 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001544 return ret;
1545}
1546
Li Zefan0caa1022010-12-20 16:30:25 +08001547static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1548 void __user *arg)
1549{
1550 struct inode *inode = fdentry(file)->d_inode;
1551 struct btrfs_root *root = BTRFS_I(inode)->root;
1552 int ret = 0;
1553 u64 flags = 0;
1554
Li Zefan33345d012011-04-20 10:31:50 +08001555 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001556 return -EINVAL;
1557
1558 down_read(&root->fs_info->subvol_sem);
1559 if (btrfs_root_readonly(root))
1560 flags |= BTRFS_SUBVOL_RDONLY;
1561 up_read(&root->fs_info->subvol_sem);
1562
1563 if (copy_to_user(arg, &flags, sizeof(flags)))
1564 ret = -EFAULT;
1565
1566 return ret;
1567}
1568
1569static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1570 void __user *arg)
1571{
1572 struct inode *inode = fdentry(file)->d_inode;
1573 struct btrfs_root *root = BTRFS_I(inode)->root;
1574 struct btrfs_trans_handle *trans;
1575 u64 root_flags;
1576 u64 flags;
1577 int ret = 0;
1578
Liu Bob9ca0662012-06-29 03:58:49 -06001579 ret = mnt_want_write_file(file);
1580 if (ret)
1581 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001582
Liu Bob9ca0662012-06-29 03:58:49 -06001583 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1584 ret = -EINVAL;
1585 goto out_drop_write;
1586 }
Li Zefan0caa1022010-12-20 16:30:25 +08001587
Liu Bob9ca0662012-06-29 03:58:49 -06001588 if (copy_from_user(&flags, arg, sizeof(flags))) {
1589 ret = -EFAULT;
1590 goto out_drop_write;
1591 }
Li Zefan0caa1022010-12-20 16:30:25 +08001592
Liu Bob9ca0662012-06-29 03:58:49 -06001593 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1594 ret = -EINVAL;
1595 goto out_drop_write;
1596 }
Li Zefan0caa1022010-12-20 16:30:25 +08001597
Liu Bob9ca0662012-06-29 03:58:49 -06001598 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1599 ret = -EOPNOTSUPP;
1600 goto out_drop_write;
1601 }
Li Zefan0caa1022010-12-20 16:30:25 +08001602
Liu Bob9ca0662012-06-29 03:58:49 -06001603 if (!inode_owner_or_capable(inode)) {
1604 ret = -EACCES;
1605 goto out_drop_write;
1606 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001607
Li Zefan0caa1022010-12-20 16:30:25 +08001608 down_write(&root->fs_info->subvol_sem);
1609
1610 /* nothing to do */
1611 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001612 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001613
1614 root_flags = btrfs_root_flags(&root->root_item);
1615 if (flags & BTRFS_SUBVOL_RDONLY)
1616 btrfs_set_root_flags(&root->root_item,
1617 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1618 else
1619 btrfs_set_root_flags(&root->root_item,
1620 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1621
1622 trans = btrfs_start_transaction(root, 1);
1623 if (IS_ERR(trans)) {
1624 ret = PTR_ERR(trans);
1625 goto out_reset;
1626 }
1627
Li Zefanb4dc2b82011-02-16 06:06:34 +00001628 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001629 &root->root_key, &root->root_item);
1630
1631 btrfs_commit_transaction(trans, root);
1632out_reset:
1633 if (ret)
1634 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001635out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001636 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001637out_drop_write:
1638 mnt_drop_write_file(file);
1639out:
Li Zefan0caa1022010-12-20 16:30:25 +08001640 return ret;
1641}
1642
Yan, Zheng76dda932009-09-21 16:00:26 -04001643/*
1644 * helper to check if the subvolume references other subvolumes
1645 */
1646static noinline int may_destroy_subvol(struct btrfs_root *root)
1647{
1648 struct btrfs_path *path;
1649 struct btrfs_key key;
1650 int ret;
1651
1652 path = btrfs_alloc_path();
1653 if (!path)
1654 return -ENOMEM;
1655
1656 key.objectid = root->root_key.objectid;
1657 key.type = BTRFS_ROOT_REF_KEY;
1658 key.offset = (u64)-1;
1659
1660 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1661 &key, path, 0, 0);
1662 if (ret < 0)
1663 goto out;
1664 BUG_ON(ret == 0);
1665
1666 ret = 0;
1667 if (path->slots[0] > 0) {
1668 path->slots[0]--;
1669 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1670 if (key.objectid == root->root_key.objectid &&
1671 key.type == BTRFS_ROOT_REF_KEY)
1672 ret = -ENOTEMPTY;
1673 }
1674out:
1675 btrfs_free_path(path);
1676 return ret;
1677}
1678
Chris Masonac8e9812010-02-28 15:39:26 -05001679static noinline int key_in_sk(struct btrfs_key *key,
1680 struct btrfs_ioctl_search_key *sk)
1681{
Chris Masonabc6e132010-03-18 12:10:08 -04001682 struct btrfs_key test;
1683 int ret;
1684
1685 test.objectid = sk->min_objectid;
1686 test.type = sk->min_type;
1687 test.offset = sk->min_offset;
1688
1689 ret = btrfs_comp_cpu_keys(key, &test);
1690 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001691 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001692
1693 test.objectid = sk->max_objectid;
1694 test.type = sk->max_type;
1695 test.offset = sk->max_offset;
1696
1697 ret = btrfs_comp_cpu_keys(key, &test);
1698 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001699 return 0;
1700 return 1;
1701}
1702
1703static noinline int copy_to_sk(struct btrfs_root *root,
1704 struct btrfs_path *path,
1705 struct btrfs_key *key,
1706 struct btrfs_ioctl_search_key *sk,
1707 char *buf,
1708 unsigned long *sk_offset,
1709 int *num_found)
1710{
1711 u64 found_transid;
1712 struct extent_buffer *leaf;
1713 struct btrfs_ioctl_search_header sh;
1714 unsigned long item_off;
1715 unsigned long item_len;
1716 int nritems;
1717 int i;
1718 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001719 int ret = 0;
1720
1721 leaf = path->nodes[0];
1722 slot = path->slots[0];
1723 nritems = btrfs_header_nritems(leaf);
1724
1725 if (btrfs_header_generation(leaf) > sk->max_transid) {
1726 i = nritems;
1727 goto advance_key;
1728 }
1729 found_transid = btrfs_header_generation(leaf);
1730
1731 for (i = slot; i < nritems; i++) {
1732 item_off = btrfs_item_ptr_offset(leaf, i);
1733 item_len = btrfs_item_size_nr(leaf, i);
1734
1735 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1736 item_len = 0;
1737
1738 if (sizeof(sh) + item_len + *sk_offset >
1739 BTRFS_SEARCH_ARGS_BUFSIZE) {
1740 ret = 1;
1741 goto overflow;
1742 }
1743
1744 btrfs_item_key_to_cpu(leaf, key, i);
1745 if (!key_in_sk(key, sk))
1746 continue;
1747
1748 sh.objectid = key->objectid;
1749 sh.offset = key->offset;
1750 sh.type = key->type;
1751 sh.len = item_len;
1752 sh.transid = found_transid;
1753
1754 /* copy search result header */
1755 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1756 *sk_offset += sizeof(sh);
1757
1758 if (item_len) {
1759 char *p = buf + *sk_offset;
1760 /* copy the item */
1761 read_extent_buffer(leaf, p,
1762 item_off, item_len);
1763 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001764 }
Hugo Millse2156862011-05-14 17:43:41 +00001765 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001766
1767 if (*num_found >= sk->nr_items)
1768 break;
1769 }
1770advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001771 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001772 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1773 key->offset++;
1774 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1775 key->offset = 0;
1776 key->type++;
1777 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1778 key->offset = 0;
1779 key->type = 0;
1780 key->objectid++;
1781 } else
1782 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001783overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001784 return ret;
1785}
1786
1787static noinline int search_ioctl(struct inode *inode,
1788 struct btrfs_ioctl_search_args *args)
1789{
1790 struct btrfs_root *root;
1791 struct btrfs_key key;
1792 struct btrfs_key max_key;
1793 struct btrfs_path *path;
1794 struct btrfs_ioctl_search_key *sk = &args->key;
1795 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1796 int ret;
1797 int num_found = 0;
1798 unsigned long sk_offset = 0;
1799
1800 path = btrfs_alloc_path();
1801 if (!path)
1802 return -ENOMEM;
1803
1804 if (sk->tree_id == 0) {
1805 /* search the root of the inode that was passed */
1806 root = BTRFS_I(inode)->root;
1807 } else {
1808 key.objectid = sk->tree_id;
1809 key.type = BTRFS_ROOT_ITEM_KEY;
1810 key.offset = (u64)-1;
1811 root = btrfs_read_fs_root_no_name(info, &key);
1812 if (IS_ERR(root)) {
1813 printk(KERN_ERR "could not find root %llu\n",
1814 sk->tree_id);
1815 btrfs_free_path(path);
1816 return -ENOENT;
1817 }
1818 }
1819
1820 key.objectid = sk->min_objectid;
1821 key.type = sk->min_type;
1822 key.offset = sk->min_offset;
1823
1824 max_key.objectid = sk->max_objectid;
1825 max_key.type = sk->max_type;
1826 max_key.offset = sk->max_offset;
1827
1828 path->keep_locks = 1;
1829
1830 while(1) {
1831 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1832 sk->min_transid);
1833 if (ret != 0) {
1834 if (ret > 0)
1835 ret = 0;
1836 goto err;
1837 }
1838 ret = copy_to_sk(root, path, &key, sk, args->buf,
1839 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001840 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001841 if (ret || num_found >= sk->nr_items)
1842 break;
1843
1844 }
1845 ret = 0;
1846err:
1847 sk->nr_items = num_found;
1848 btrfs_free_path(path);
1849 return ret;
1850}
1851
1852static noinline int btrfs_ioctl_tree_search(struct file *file,
1853 void __user *argp)
1854{
1855 struct btrfs_ioctl_search_args *args;
1856 struct inode *inode;
1857 int ret;
1858
1859 if (!capable(CAP_SYS_ADMIN))
1860 return -EPERM;
1861
Julia Lawall2354d082010-10-29 15:14:18 -04001862 args = memdup_user(argp, sizeof(*args));
1863 if (IS_ERR(args))
1864 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001865
Chris Masonac8e9812010-02-28 15:39:26 -05001866 inode = fdentry(file)->d_inode;
1867 ret = search_ioctl(inode, args);
1868 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1869 ret = -EFAULT;
1870 kfree(args);
1871 return ret;
1872}
1873
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001874/*
Chris Masonac8e9812010-02-28 15:39:26 -05001875 * Search INODE_REFs to identify path name of 'dirid' directory
1876 * in a 'tree_id' tree. and sets path name to 'name'.
1877 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001878static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1879 u64 tree_id, u64 dirid, char *name)
1880{
1881 struct btrfs_root *root;
1882 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001883 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001884 int ret = -1;
1885 int slot;
1886 int len;
1887 int total_len = 0;
1888 struct btrfs_inode_ref *iref;
1889 struct extent_buffer *l;
1890 struct btrfs_path *path;
1891
1892 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1893 name[0]='\0';
1894 return 0;
1895 }
1896
1897 path = btrfs_alloc_path();
1898 if (!path)
1899 return -ENOMEM;
1900
Chris Masonac8e9812010-02-28 15:39:26 -05001901 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001902
1903 key.objectid = tree_id;
1904 key.type = BTRFS_ROOT_ITEM_KEY;
1905 key.offset = (u64)-1;
1906 root = btrfs_read_fs_root_no_name(info, &key);
1907 if (IS_ERR(root)) {
1908 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001909 ret = -ENOENT;
1910 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001911 }
1912
1913 key.objectid = dirid;
1914 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001915 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001916
1917 while(1) {
1918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1919 if (ret < 0)
1920 goto out;
1921
1922 l = path->nodes[0];
1923 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001924 if (ret > 0 && slot > 0)
1925 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001926 btrfs_item_key_to_cpu(l, &key, slot);
1927
1928 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001929 key.type != BTRFS_INODE_REF_KEY)) {
1930 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001931 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001932 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001933
1934 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1935 len = btrfs_inode_ref_name_len(l, iref);
1936 ptr -= len + 1;
1937 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001938 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001939 goto out;
1940
1941 *(ptr + len) = '/';
1942 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1943
1944 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1945 break;
1946
David Sterbab3b4aa72011-04-21 01:20:15 +02001947 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001948 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001949 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001950 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001951 }
Chris Masonac8e9812010-02-28 15:39:26 -05001952 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001953 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001954 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001955 name[total_len]='\0';
1956 ret = 0;
1957out:
1958 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001959 return ret;
1960}
1961
1962static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1963 void __user *argp)
1964{
1965 struct btrfs_ioctl_ino_lookup_args *args;
1966 struct inode *inode;
1967 int ret;
1968
1969 if (!capable(CAP_SYS_ADMIN))
1970 return -EPERM;
1971
Julia Lawall2354d082010-10-29 15:14:18 -04001972 args = memdup_user(argp, sizeof(*args));
1973 if (IS_ERR(args))
1974 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001975
Chris Masonac8e9812010-02-28 15:39:26 -05001976 inode = fdentry(file)->d_inode;
1977
Chris Mason1b53ac42010-03-18 12:17:05 -04001978 if (args->treeid == 0)
1979 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1980
Chris Masonac8e9812010-02-28 15:39:26 -05001981 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1982 args->treeid, args->objectid,
1983 args->name);
1984
1985 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1986 ret = -EFAULT;
1987
1988 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001989 return ret;
1990}
1991
Yan, Zheng76dda932009-09-21 16:00:26 -04001992static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1993 void __user *arg)
1994{
1995 struct dentry *parent = fdentry(file);
1996 struct dentry *dentry;
1997 struct inode *dir = parent->d_inode;
1998 struct inode *inode;
1999 struct btrfs_root *root = BTRFS_I(dir)->root;
2000 struct btrfs_root *dest = NULL;
2001 struct btrfs_ioctl_vol_args *vol_args;
2002 struct btrfs_trans_handle *trans;
2003 int namelen;
2004 int ret;
2005 int err = 0;
2006
Yan, Zheng76dda932009-09-21 16:00:26 -04002007 vol_args = memdup_user(arg, sizeof(*vol_args));
2008 if (IS_ERR(vol_args))
2009 return PTR_ERR(vol_args);
2010
2011 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2012 namelen = strlen(vol_args->name);
2013 if (strchr(vol_args->name, '/') ||
2014 strncmp(vol_args->name, "..", namelen) == 0) {
2015 err = -EINVAL;
2016 goto out;
2017 }
2018
Al Viroa561be72011-11-23 11:57:51 -05002019 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002020 if (err)
2021 goto out;
2022
2023 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2024 dentry = lookup_one_len(vol_args->name, parent, namelen);
2025 if (IS_ERR(dentry)) {
2026 err = PTR_ERR(dentry);
2027 goto out_unlock_dir;
2028 }
2029
2030 if (!dentry->d_inode) {
2031 err = -ENOENT;
2032 goto out_dput;
2033 }
2034
2035 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002036 dest = BTRFS_I(inode)->root;
2037 if (!capable(CAP_SYS_ADMIN)){
2038 /*
2039 * Regular user. Only allow this with a special mount
2040 * option, when the user has write+exec access to the
2041 * subvol root, and when rmdir(2) would have been
2042 * allowed.
2043 *
2044 * Note that this is _not_ check that the subvol is
2045 * empty or doesn't contain data that we wouldn't
2046 * otherwise be able to delete.
2047 *
2048 * Users who want to delete empty subvols should try
2049 * rmdir(2).
2050 */
2051 err = -EPERM;
2052 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2053 goto out_dput;
2054
2055 /*
2056 * Do not allow deletion if the parent dir is the same
2057 * as the dir to be deleted. That means the ioctl
2058 * must be called on the dentry referencing the root
2059 * of the subvol, not a random directory contained
2060 * within it.
2061 */
2062 err = -EINVAL;
2063 if (root == dest)
2064 goto out_dput;
2065
2066 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2067 if (err)
2068 goto out_dput;
2069
2070 /* check if subvolume may be deleted by a non-root user */
2071 err = btrfs_may_delete(dir, dentry, 1);
2072 if (err)
2073 goto out_dput;
2074 }
2075
Li Zefan33345d012011-04-20 10:31:50 +08002076 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002077 err = -EINVAL;
2078 goto out_dput;
2079 }
2080
Yan, Zheng76dda932009-09-21 16:00:26 -04002081 mutex_lock(&inode->i_mutex);
2082 err = d_invalidate(dentry);
2083 if (err)
2084 goto out_unlock;
2085
2086 down_write(&root->fs_info->subvol_sem);
2087
2088 err = may_destroy_subvol(dest);
2089 if (err)
2090 goto out_up_write;
2091
Yan, Zhenga22285a2010-05-16 10:48:46 -04002092 trans = btrfs_start_transaction(root, 0);
2093 if (IS_ERR(trans)) {
2094 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002095 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002096 }
2097 trans->block_rsv = &root->fs_info->global_block_rsv;
2098
Yan, Zheng76dda932009-09-21 16:00:26 -04002099 ret = btrfs_unlink_subvol(trans, root, dir,
2100 dest->root_key.objectid,
2101 dentry->d_name.name,
2102 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002103 if (ret) {
2104 err = ret;
2105 btrfs_abort_transaction(trans, root, ret);
2106 goto out_end_trans;
2107 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002108
2109 btrfs_record_root_in_trans(trans, dest);
2110
2111 memset(&dest->root_item.drop_progress, 0,
2112 sizeof(dest->root_item.drop_progress));
2113 dest->root_item.drop_level = 0;
2114 btrfs_set_root_refs(&dest->root_item, 0);
2115
Yan, Zhengd68fc572010-05-16 10:49:58 -04002116 if (!xchg(&dest->orphan_item_inserted, 1)) {
2117 ret = btrfs_insert_orphan_item(trans,
2118 root->fs_info->tree_root,
2119 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002120 if (ret) {
2121 btrfs_abort_transaction(trans, root, ret);
2122 err = ret;
2123 goto out_end_trans;
2124 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002125 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002126out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002127 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002128 if (ret && !err)
2129 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002130 inode->i_flags |= S_DEAD;
2131out_up_write:
2132 up_write(&root->fs_info->subvol_sem);
2133out_unlock:
2134 mutex_unlock(&inode->i_mutex);
2135 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002136 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002137 btrfs_invalidate_inodes(dest);
2138 d_delete(dentry);
2139 }
2140out_dput:
2141 dput(dentry);
2142out_unlock_dir:
2143 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002144 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002145out:
2146 kfree(vol_args);
2147 return err;
2148}
2149
Chris Mason1e701a32010-03-11 09:42:04 -05002150static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002151{
2152 struct inode *inode = fdentry(file)->d_inode;
2153 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002154 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002155 int ret;
2156
Li Zefanb83cc962010-12-20 16:04:08 +08002157 if (btrfs_root_readonly(root))
2158 return -EROFS;
2159
Al Viroa561be72011-11-23 11:57:51 -05002160 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002161 if (ret)
2162 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002163
2164 switch (inode->i_mode & S_IFMT) {
2165 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002166 if (!capable(CAP_SYS_ADMIN)) {
2167 ret = -EPERM;
2168 goto out;
2169 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002170 ret = btrfs_defrag_root(root, 0);
2171 if (ret)
2172 goto out;
2173 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002174 break;
2175 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002176 if (!(file->f_mode & FMODE_WRITE)) {
2177 ret = -EINVAL;
2178 goto out;
2179 }
Chris Mason1e701a32010-03-11 09:42:04 -05002180
2181 range = kzalloc(sizeof(*range), GFP_KERNEL);
2182 if (!range) {
2183 ret = -ENOMEM;
2184 goto out;
2185 }
2186
2187 if (argp) {
2188 if (copy_from_user(range, argp,
2189 sizeof(*range))) {
2190 ret = -EFAULT;
2191 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002192 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002193 }
2194 /* compression requires us to start the IO */
2195 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2196 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2197 range->extent_thresh = (u32)-1;
2198 }
2199 } else {
2200 /* the rest are all set to zero by kzalloc */
2201 range->len = (u64)-1;
2202 }
Chris Mason4cb53002011-05-24 15:35:30 -04002203 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2204 range, 0, 0);
2205 if (ret > 0)
2206 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002207 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002208 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002209 default:
2210 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002211 }
Chris Masone441d542009-01-05 16:57:23 -05002212out:
Al Viro2a79f172011-12-09 08:06:57 -05002213 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002214 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002215}
2216
Christoph Hellwigb2950862008-12-02 09:54:17 -05002217static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002218{
2219 struct btrfs_ioctl_vol_args *vol_args;
2220 int ret;
2221
Chris Masone441d542009-01-05 16:57:23 -05002222 if (!capable(CAP_SYS_ADMIN))
2223 return -EPERM;
2224
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002225 mutex_lock(&root->fs_info->volume_mutex);
2226 if (root->fs_info->balance_ctl) {
2227 printk(KERN_INFO "btrfs: balance in progress\n");
2228 ret = -EINVAL;
2229 goto out;
2230 }
2231
Li Zefandae7b662009-04-08 15:06:54 +08002232 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002233 if (IS_ERR(vol_args)) {
2234 ret = PTR_ERR(vol_args);
2235 goto out;
2236 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002237
Mark Fasheh5516e592008-07-24 12:20:14 -04002238 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002239 ret = btrfs_init_new_device(root, vol_args->name);
2240
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002241 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002242out:
2243 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002244 return ret;
2245}
2246
Christoph Hellwigb2950862008-12-02 09:54:17 -05002247static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002248{
2249 struct btrfs_ioctl_vol_args *vol_args;
2250 int ret;
2251
Chris Masone441d542009-01-05 16:57:23 -05002252 if (!capable(CAP_SYS_ADMIN))
2253 return -EPERM;
2254
Yan Zhengc146afa2008-11-12 14:34:12 -05002255 if (root->fs_info->sb->s_flags & MS_RDONLY)
2256 return -EROFS;
2257
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002258 mutex_lock(&root->fs_info->volume_mutex);
2259 if (root->fs_info->balance_ctl) {
2260 printk(KERN_INFO "btrfs: balance in progress\n");
2261 ret = -EINVAL;
2262 goto out;
2263 }
2264
Li Zefandae7b662009-04-08 15:06:54 +08002265 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002266 if (IS_ERR(vol_args)) {
2267 ret = PTR_ERR(vol_args);
2268 goto out;
2269 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002270
Mark Fasheh5516e592008-07-24 12:20:14 -04002271 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002272 ret = btrfs_rm_device(root, vol_args->name);
2273
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002274 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002275out:
2276 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002277 return ret;
2278}
2279
Jan Schmidt475f6382011-03-11 15:41:01 +01002280static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2281{
Li Zefan027ed2f2011-06-08 08:27:56 +00002282 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002283 struct btrfs_device *device;
2284 struct btrfs_device *next;
2285 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002286 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002287
2288 if (!capable(CAP_SYS_ADMIN))
2289 return -EPERM;
2290
Li Zefan027ed2f2011-06-08 08:27:56 +00002291 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2292 if (!fi_args)
2293 return -ENOMEM;
2294
2295 fi_args->num_devices = fs_devices->num_devices;
2296 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002297
2298 mutex_lock(&fs_devices->device_list_mutex);
2299 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002300 if (device->devid > fi_args->max_id)
2301 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002302 }
2303 mutex_unlock(&fs_devices->device_list_mutex);
2304
Li Zefan027ed2f2011-06-08 08:27:56 +00002305 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2306 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002307
Li Zefan027ed2f2011-06-08 08:27:56 +00002308 kfree(fi_args);
2309 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002310}
2311
2312static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2313{
2314 struct btrfs_ioctl_dev_info_args *di_args;
2315 struct btrfs_device *dev;
2316 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2317 int ret = 0;
2318 char *s_uuid = NULL;
2319 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2320
2321 if (!capable(CAP_SYS_ADMIN))
2322 return -EPERM;
2323
2324 di_args = memdup_user(arg, sizeof(*di_args));
2325 if (IS_ERR(di_args))
2326 return PTR_ERR(di_args);
2327
2328 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2329 s_uuid = di_args->uuid;
2330
2331 mutex_lock(&fs_devices->device_list_mutex);
2332 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2333 mutex_unlock(&fs_devices->device_list_mutex);
2334
2335 if (!dev) {
2336 ret = -ENODEV;
2337 goto out;
2338 }
2339
2340 di_args->devid = dev->devid;
2341 di_args->bytes_used = dev->bytes_used;
2342 di_args->total_bytes = dev->total_bytes;
2343 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002344 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002345 struct rcu_string *name;
2346
2347 rcu_read_lock();
2348 name = rcu_dereference(dev->name);
2349 strncpy(di_args->path, name->str, sizeof(di_args->path));
2350 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002351 di_args->path[sizeof(di_args->path) - 1] = 0;
2352 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002353 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002354 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002355
2356out:
2357 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2358 ret = -EFAULT;
2359
2360 kfree(di_args);
2361 return ret;
2362}
2363
Yan, Zheng76dda932009-09-21 16:00:26 -04002364static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2365 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002366{
2367 struct inode *inode = fdentry(file)->d_inode;
2368 struct btrfs_root *root = BTRFS_I(inode)->root;
2369 struct file *src_file;
2370 struct inode *src;
2371 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002372 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002373 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002374 char *buf;
2375 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002376 u32 nritems;
2377 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002378 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002379 u64 len = olen;
2380 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002381
Sage Weilc5c9cd42008-11-12 14:32:25 -05002382 /*
2383 * TODO:
2384 * - split compressed inline extents. annoying: we need to
2385 * decompress into destination's address_space (the file offset
2386 * may change, so source mapping won't do), then recompress (or
2387 * otherwise reinsert) a subrange.
2388 * - allow ranges within the same file to be cloned (provided
2389 * they don't overlap)?
2390 */
2391
Chris Masone441d542009-01-05 16:57:23 -05002392 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002393 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002394 return -EINVAL;
2395
Li Zefanb83cc962010-12-20 16:04:08 +08002396 if (btrfs_root_readonly(root))
2397 return -EROFS;
2398
Al Viroa561be72011-11-23 11:57:51 -05002399 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002400 if (ret)
2401 return ret;
2402
Sage Weilc5c9cd42008-11-12 14:32:25 -05002403 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002404 if (!src_file) {
2405 ret = -EBADF;
2406 goto out_drop_write;
2407 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002408
David Sterba362a20c2011-08-01 18:11:57 +02002409 ret = -EXDEV;
2410 if (src_file->f_path.mnt != file->f_path.mnt)
2411 goto out_fput;
2412
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413 src = src_file->f_dentry->d_inode;
2414
Sage Weilc5c9cd42008-11-12 14:32:25 -05002415 ret = -EINVAL;
2416 if (src == inode)
2417 goto out_fput;
2418
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002419 /* the src must be open for reading */
2420 if (!(src_file->f_mode & FMODE_READ))
2421 goto out_fput;
2422
Li Zefan0e7b8242011-09-18 10:20:46 -04002423 /* don't make the dst file partly checksummed */
2424 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2425 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2426 goto out_fput;
2427
Yan Zhengae01a0a2008-08-04 23:23:47 -04002428 ret = -EISDIR;
2429 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002430 goto out_fput;
2431
Yan Zhengae01a0a2008-08-04 23:23:47 -04002432 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002433 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002434 goto out_fput;
2435
2436 ret = -ENOMEM;
2437 buf = vmalloc(btrfs_level_size(root, 0));
2438 if (!buf)
2439 goto out_fput;
2440
2441 path = btrfs_alloc_path();
2442 if (!path) {
2443 vfree(buf);
2444 goto out_fput;
2445 }
2446 path->reada = 2;
2447
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002448 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002449 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2450 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002451 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002452 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2453 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002454 }
2455
Sage Weilc5c9cd42008-11-12 14:32:25 -05002456 /* determine range to clone */
2457 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002458 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002459 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002460 if (len == 0)
2461 olen = len = src->i_size - off;
2462 /* if we extend to eof, continue to block boundary */
2463 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002464 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002465
2466 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002467 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2468 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002469 goto out_unlock;
2470
Li Zefand525e8a2011-09-11 10:52:25 -04002471 if (destoff > inode->i_size) {
2472 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2473 if (ret)
2474 goto out_unlock;
2475 }
2476
Li Zefan71ef0782011-09-18 10:20:46 -04002477 /* truncate page cache pages from target inode range */
2478 truncate_inode_pages_range(&inode->i_data, destoff,
2479 PAGE_CACHE_ALIGN(destoff + len) - 1);
2480
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002481 /* do any pending delalloc/csum calc on src, one way or
2482 another, and lock file content */
2483 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002484 struct btrfs_ordered_extent *ordered;
Liu Boaa42ffd2012-09-18 03:52:23 -06002485 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2486 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
Sage Weil9a019192010-10-29 15:37:33 -04002487 if (!ordered &&
Liu Boaa42ffd2012-09-18 03:52:23 -06002488 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2489 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002490 break;
Liu Boaa42ffd2012-09-18 03:52:23 -06002491 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002492 if (ordered)
2493 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002494 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002495 }
2496
Sage Weilc5c9cd42008-11-12 14:32:25 -05002497 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002498 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002499 key.type = BTRFS_EXTENT_DATA_KEY;
2500 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501
2502 while (1) {
2503 /*
2504 * note the key will change type as we walk through the
2505 * tree.
2506 */
David Sterba362a20c2011-08-01 18:11:57 +02002507 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2508 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002509 if (ret < 0)
2510 goto out;
2511
Yan Zhengae01a0a2008-08-04 23:23:47 -04002512 nritems = btrfs_header_nritems(path->nodes[0]);
2513 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002514 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002515 if (ret < 0)
2516 goto out;
2517 if (ret > 0)
2518 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002519 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002520 }
2521 leaf = path->nodes[0];
2522 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002523
Yan Zhengae01a0a2008-08-04 23:23:47 -04002524 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002525 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002526 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002527 break;
2528
Sage Weilc5c9cd42008-11-12 14:32:25 -05002529 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2530 struct btrfs_file_extent_item *extent;
2531 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002532 u32 size;
2533 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002534 u64 disko = 0, diskl = 0;
2535 u64 datao = 0, datal = 0;
2536 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002537 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002538
2539 size = btrfs_item_size_nr(leaf, slot);
2540 read_extent_buffer(leaf, buf,
2541 btrfs_item_ptr_offset(leaf, slot),
2542 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002543
2544 extent = btrfs_item_ptr(leaf, slot,
2545 struct btrfs_file_extent_item);
2546 comp = btrfs_file_extent_compression(leaf, extent);
2547 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002548 if (type == BTRFS_FILE_EXTENT_REG ||
2549 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002550 disko = btrfs_file_extent_disk_bytenr(leaf,
2551 extent);
2552 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2553 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002554 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002555 datal = btrfs_file_extent_num_bytes(leaf,
2556 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002557 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2558 /* take upper bound, may be compressed */
2559 datal = btrfs_file_extent_ram_bytes(leaf,
2560 extent);
2561 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002562 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002563
Sage Weil050006a2010-10-29 15:37:33 -04002564 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002565 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002566 goto next;
2567
Zheng Yan31840ae2008-09-23 13:14:14 -04002568 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002569 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002570 if (off <= key.offset)
2571 new_key.offset = key.offset + destoff - off;
2572 else
2573 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002574
Sage Weilb6f34092011-09-20 14:48:51 -04002575 /*
2576 * 1 - adjusting old extent (we may have to split it)
2577 * 1 - add new extent
2578 * 1 - inode update
2579 */
2580 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002581 if (IS_ERR(trans)) {
2582 ret = PTR_ERR(trans);
2583 goto out;
2584 }
2585
Chris Masonc8a894d2009-06-27 21:07:03 -04002586 if (type == BTRFS_FILE_EXTENT_REG ||
2587 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002588 /*
2589 * a | --- range to clone ---| b
2590 * | ------------- extent ------------- |
2591 */
2592
2593 /* substract range b */
2594 if (key.offset + datal > off + len)
2595 datal = off + len - key.offset;
2596
2597 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002598 if (off > key.offset) {
2599 datao += off - key.offset;
2600 datal -= off - key.offset;
2601 }
2602
Josef Bacik5dc562c2012-08-17 13:14:17 -04002603 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002604 new_key.offset,
2605 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002606 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 if (ret) {
2608 btrfs_abort_transaction(trans, root,
2609 ret);
2610 btrfs_end_transaction(trans, root);
2611 goto out;
2612 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002613
Sage Weilc5c9cd42008-11-12 14:32:25 -05002614 ret = btrfs_insert_empty_item(trans, root, path,
2615 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002616 if (ret) {
2617 btrfs_abort_transaction(trans, root,
2618 ret);
2619 btrfs_end_transaction(trans, root);
2620 goto out;
2621 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002622
2623 leaf = path->nodes[0];
2624 slot = path->slots[0];
2625 write_extent_buffer(leaf, buf,
2626 btrfs_item_ptr_offset(leaf, slot),
2627 size);
2628
2629 extent = btrfs_item_ptr(leaf, slot,
2630 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002631
Sage Weilc5c9cd42008-11-12 14:32:25 -05002632 /* disko == 0 means it's a hole */
2633 if (!disko)
2634 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002635
2636 btrfs_set_file_extent_offset(leaf, extent,
2637 datao);
2638 btrfs_set_file_extent_num_bytes(leaf, extent,
2639 datal);
2640 if (disko) {
2641 inode_add_bytes(inode, datal);
2642 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002643 disko, diskl, 0,
2644 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002645 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002646 new_key.offset - datao,
2647 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002648 if (ret) {
2649 btrfs_abort_transaction(trans,
2650 root,
2651 ret);
2652 btrfs_end_transaction(trans,
2653 root);
2654 goto out;
2655
2656 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002657 }
2658 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2659 u64 skip = 0;
2660 u64 trim = 0;
2661 if (off > key.offset) {
2662 skip = off - key.offset;
2663 new_key.offset += skip;
2664 }
Chris Masond3977122009-01-05 21:25:51 -05002665
Liu Boaa42ffd2012-09-18 03:52:23 -06002666 if (key.offset + datal > off + len)
2667 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002668
Sage Weilc5c9cd42008-11-12 14:32:25 -05002669 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002670 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002671 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002672 goto out;
2673 }
2674 size -= skip + trim;
2675 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002676
Josef Bacik5dc562c2012-08-17 13:14:17 -04002677 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002678 new_key.offset,
2679 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002680 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002681 if (ret) {
2682 btrfs_abort_transaction(trans, root,
2683 ret);
2684 btrfs_end_transaction(trans, root);
2685 goto out;
2686 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002687
Sage Weilc5c9cd42008-11-12 14:32:25 -05002688 ret = btrfs_insert_empty_item(trans, root, path,
2689 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002690 if (ret) {
2691 btrfs_abort_transaction(trans, root,
2692 ret);
2693 btrfs_end_transaction(trans, root);
2694 goto out;
2695 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002696
2697 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002698 u32 start =
2699 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002700 memmove(buf+start, buf+start+skip,
2701 datal);
2702 }
2703
2704 leaf = path->nodes[0];
2705 slot = path->slots[0];
2706 write_extent_buffer(leaf, buf,
2707 btrfs_item_ptr_offset(leaf, slot),
2708 size);
2709 inode_add_bytes(inode, datal);
2710 }
2711
2712 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002713 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002714
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002715 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002716 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002717
2718 /*
2719 * we round up to the block size at eof when
2720 * determining which extents to clone above,
2721 * but shouldn't round up the file size
2722 */
2723 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002724 if (endoff > destoff+olen)
2725 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002726 if (endoff > inode->i_size)
2727 btrfs_i_size_write(inode, endoff);
2728
Yan, Zhenga22285a2010-05-16 10:48:46 -04002729 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002730 if (ret) {
2731 btrfs_abort_transaction(trans, root, ret);
2732 btrfs_end_transaction(trans, root);
2733 goto out;
2734 }
2735 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002736 }
Chris Masond3977122009-01-05 21:25:51 -05002737next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002738 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002739 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002740 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002741 ret = 0;
2742out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002743 btrfs_release_path(path);
Liu Boaa42ffd2012-09-18 03:52:23 -06002744 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002745out_unlock:
2746 mutex_unlock(&src->i_mutex);
2747 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002748 vfree(buf);
2749 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002750out_fput:
2751 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002752out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002753 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002754 return ret;
2755}
2756
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002757static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002758{
2759 struct btrfs_ioctl_clone_range_args args;
2760
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002761 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002762 return -EFAULT;
2763 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2764 args.src_length, args.dest_offset);
2765}
2766
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002767/*
2768 * there are many ways the trans_start and trans_end ioctls can lead
2769 * to deadlocks. They should only be used by applications that
2770 * basically own the machine, and have a very in depth understanding
2771 * of all the possible deadlocks and enospc problems.
2772 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002773static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002774{
2775 struct inode *inode = fdentry(file)->d_inode;
2776 struct btrfs_root *root = BTRFS_I(inode)->root;
2777 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002778 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002779
Sage Weil1ab86ae2009-09-29 18:38:44 -04002780 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002781 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002782 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002783
2784 ret = -EINPROGRESS;
2785 if (file->private_data)
2786 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002787
Li Zefanb83cc962010-12-20 16:04:08 +08002788 ret = -EROFS;
2789 if (btrfs_root_readonly(root))
2790 goto out;
2791
Al Viroa561be72011-11-23 11:57:51 -05002792 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002793 if (ret)
2794 goto out;
2795
Josef Bacika4abeea2011-04-11 17:25:13 -04002796 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002797
Sage Weil1ab86ae2009-09-29 18:38:44 -04002798 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002799 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002800 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002801 goto out_drop;
2802
2803 file->private_data = trans;
2804 return 0;
2805
2806out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002807 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002808 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002809out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002810 return ret;
2811}
2812
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002813static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2814{
2815 struct inode *inode = fdentry(file)->d_inode;
2816 struct btrfs_root *root = BTRFS_I(inode)->root;
2817 struct btrfs_root *new_root;
2818 struct btrfs_dir_item *di;
2819 struct btrfs_trans_handle *trans;
2820 struct btrfs_path *path;
2821 struct btrfs_key location;
2822 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002823 u64 objectid = 0;
2824 u64 dir_id;
2825
2826 if (!capable(CAP_SYS_ADMIN))
2827 return -EPERM;
2828
2829 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2830 return -EFAULT;
2831
2832 if (!objectid)
2833 objectid = root->root_key.objectid;
2834
2835 location.objectid = objectid;
2836 location.type = BTRFS_ROOT_ITEM_KEY;
2837 location.offset = (u64)-1;
2838
2839 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2840 if (IS_ERR(new_root))
2841 return PTR_ERR(new_root);
2842
2843 if (btrfs_root_refs(&new_root->root_item) == 0)
2844 return -ENOENT;
2845
2846 path = btrfs_alloc_path();
2847 if (!path)
2848 return -ENOMEM;
2849 path->leave_spinning = 1;
2850
2851 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002852 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002853 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002854 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002855 }
2856
David Sterba6c417612011-04-13 15:41:04 +02002857 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002858 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2859 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002860 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002861 btrfs_free_path(path);
2862 btrfs_end_transaction(trans, root);
2863 printk(KERN_ERR "Umm, you don't have the default dir item, "
2864 "this isn't going to work\n");
2865 return -ENOENT;
2866 }
2867
2868 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2869 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2870 btrfs_mark_buffer_dirty(path->nodes[0]);
2871 btrfs_free_path(path);
2872
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002873 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002874 btrfs_end_transaction(trans, root);
2875
2876 return 0;
2877}
2878
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002879void btrfs_get_block_group_info(struct list_head *groups_list,
2880 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002881{
2882 struct btrfs_block_group_cache *block_group;
2883
2884 space->total_bytes = 0;
2885 space->used_bytes = 0;
2886 space->flags = 0;
2887 list_for_each_entry(block_group, groups_list, list) {
2888 space->flags = block_group->flags;
2889 space->total_bytes += block_group->key.offset;
2890 space->used_bytes +=
2891 btrfs_block_group_used(&block_group->item);
2892 }
2893}
2894
Josef Bacik1406e432010-01-13 18:19:06 +00002895long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2896{
2897 struct btrfs_ioctl_space_args space_args;
2898 struct btrfs_ioctl_space_info space;
2899 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002900 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002901 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002902 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002903 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2904 BTRFS_BLOCK_GROUP_SYSTEM,
2905 BTRFS_BLOCK_GROUP_METADATA,
2906 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2907 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002908 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002909 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002910 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002911 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002912
2913 if (copy_from_user(&space_args,
2914 (struct btrfs_ioctl_space_args __user *)arg,
2915 sizeof(space_args)))
2916 return -EFAULT;
2917
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002918 for (i = 0; i < num_types; i++) {
2919 struct btrfs_space_info *tmp;
2920
2921 info = NULL;
2922 rcu_read_lock();
2923 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2924 list) {
2925 if (tmp->flags == types[i]) {
2926 info = tmp;
2927 break;
2928 }
2929 }
2930 rcu_read_unlock();
2931
2932 if (!info)
2933 continue;
2934
2935 down_read(&info->groups_sem);
2936 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2937 if (!list_empty(&info->block_groups[c]))
2938 slot_count++;
2939 }
2940 up_read(&info->groups_sem);
2941 }
Josef Bacik1406e432010-01-13 18:19:06 +00002942
Chris Mason7fde62b2010-03-16 15:40:10 -04002943 /* space_slots == 0 means they are asking for a count */
2944 if (space_args.space_slots == 0) {
2945 space_args.total_spaces = slot_count;
2946 goto out;
2947 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002948
Dan Rosenberg51788b12011-02-14 16:04:23 -05002949 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002950
Chris Mason7fde62b2010-03-16 15:40:10 -04002951 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002952
Chris Mason7fde62b2010-03-16 15:40:10 -04002953 /* we generally have at most 6 or so space infos, one for each raid
2954 * level. So, a whole page should be more than enough for everyone
2955 */
2956 if (alloc_size > PAGE_CACHE_SIZE)
2957 return -ENOMEM;
2958
2959 space_args.total_spaces = 0;
2960 dest = kmalloc(alloc_size, GFP_NOFS);
2961 if (!dest)
2962 return -ENOMEM;
2963 dest_orig = dest;
2964
2965 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002966 for (i = 0; i < num_types; i++) {
2967 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002968
Dan Rosenberg51788b12011-02-14 16:04:23 -05002969 if (!slot_count)
2970 break;
2971
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002972 info = NULL;
2973 rcu_read_lock();
2974 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2975 list) {
2976 if (tmp->flags == types[i]) {
2977 info = tmp;
2978 break;
2979 }
2980 }
2981 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002982
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002983 if (!info)
2984 continue;
2985 down_read(&info->groups_sem);
2986 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2987 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002988 btrfs_get_block_group_info(
2989 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002990 memcpy(dest, &space, sizeof(space));
2991 dest++;
2992 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002993 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002994 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002995 if (!slot_count)
2996 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002997 }
2998 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002999 }
Josef Bacik1406e432010-01-13 18:19:06 +00003000
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003001 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003002 (arg + sizeof(struct btrfs_ioctl_space_args));
3003
3004 if (copy_to_user(user_dest, dest_orig, alloc_size))
3005 ret = -EFAULT;
3006
3007 kfree(dest_orig);
3008out:
3009 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003010 ret = -EFAULT;
3011
3012 return ret;
3013}
3014
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003015/*
3016 * there are many ways the trans_start and trans_end ioctls can lead
3017 * to deadlocks. They should only be used by applications that
3018 * basically own the machine, and have a very in depth understanding
3019 * of all the possible deadlocks and enospc problems.
3020 */
3021long btrfs_ioctl_trans_end(struct file *file)
3022{
3023 struct inode *inode = fdentry(file)->d_inode;
3024 struct btrfs_root *root = BTRFS_I(inode)->root;
3025 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003026
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003027 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003028 if (!trans)
3029 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003030 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003031
Sage Weil1ab86ae2009-09-29 18:38:44 -04003032 btrfs_end_transaction(trans, root);
3033
Josef Bacika4abeea2011-04-11 17:25:13 -04003034 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003035
Al Viro2a79f172011-12-09 08:06:57 -05003036 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003037 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003038}
3039
Sage Weil46204592010-10-29 15:41:32 -04003040static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
3041{
3042 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3043 struct btrfs_trans_handle *trans;
3044 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003045 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003046
3047 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003048 if (IS_ERR(trans))
3049 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04003050 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003051 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003052 if (ret) {
3053 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003054 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003055 }
Sage Weil46204592010-10-29 15:41:32 -04003056
3057 if (argp)
3058 if (copy_to_user(argp, &transid, sizeof(transid)))
3059 return -EFAULT;
3060 return 0;
3061}
3062
3063static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3064{
3065 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3066 u64 transid;
3067
3068 if (argp) {
3069 if (copy_from_user(&transid, argp, sizeof(transid)))
3070 return -EFAULT;
3071 } else {
3072 transid = 0; /* current trans */
3073 }
3074 return btrfs_wait_for_commit(root, transid);
3075}
3076
Jan Schmidt475f6382011-03-11 15:41:01 +01003077static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3078{
3079 int ret;
3080 struct btrfs_ioctl_scrub_args *sa;
3081
3082 if (!capable(CAP_SYS_ADMIN))
3083 return -EPERM;
3084
3085 sa = memdup_user(arg, sizeof(*sa));
3086 if (IS_ERR(sa))
3087 return PTR_ERR(sa);
3088
3089 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003090 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003091
3092 if (copy_to_user(arg, sa, sizeof(*sa)))
3093 ret = -EFAULT;
3094
3095 kfree(sa);
3096 return ret;
3097}
3098
3099static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3100{
3101 if (!capable(CAP_SYS_ADMIN))
3102 return -EPERM;
3103
3104 return btrfs_scrub_cancel(root);
3105}
3106
3107static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3108 void __user *arg)
3109{
3110 struct btrfs_ioctl_scrub_args *sa;
3111 int ret;
3112
3113 if (!capable(CAP_SYS_ADMIN))
3114 return -EPERM;
3115
3116 sa = memdup_user(arg, sizeof(*sa));
3117 if (IS_ERR(sa))
3118 return PTR_ERR(sa);
3119
3120 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3121
3122 if (copy_to_user(arg, sa, sizeof(*sa)))
3123 ret = -EFAULT;
3124
3125 kfree(sa);
3126 return ret;
3127}
3128
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003129static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003130 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003131{
3132 struct btrfs_ioctl_get_dev_stats *sa;
3133 int ret;
3134
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003135 sa = memdup_user(arg, sizeof(*sa));
3136 if (IS_ERR(sa))
3137 return PTR_ERR(sa);
3138
David Sterbab27f7c02012-06-22 06:30:39 -06003139 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3140 kfree(sa);
3141 return -EPERM;
3142 }
3143
3144 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003145
3146 if (copy_to_user(arg, sa, sizeof(*sa)))
3147 ret = -EFAULT;
3148
3149 kfree(sa);
3150 return ret;
3151}
3152
Jan Schmidtd7728c92011-07-07 16:48:38 +02003153static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3154{
3155 int ret = 0;
3156 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003157 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003158 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003159 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003160 struct inode_fs_paths *ipath = NULL;
3161 struct btrfs_path *path;
3162
3163 if (!capable(CAP_SYS_ADMIN))
3164 return -EPERM;
3165
3166 path = btrfs_alloc_path();
3167 if (!path) {
3168 ret = -ENOMEM;
3169 goto out;
3170 }
3171
3172 ipa = memdup_user(arg, sizeof(*ipa));
3173 if (IS_ERR(ipa)) {
3174 ret = PTR_ERR(ipa);
3175 ipa = NULL;
3176 goto out;
3177 }
3178
3179 size = min_t(u32, ipa->size, 4096);
3180 ipath = init_ipath(size, root, path);
3181 if (IS_ERR(ipath)) {
3182 ret = PTR_ERR(ipath);
3183 ipath = NULL;
3184 goto out;
3185 }
3186
3187 ret = paths_from_inode(ipa->inum, ipath);
3188 if (ret < 0)
3189 goto out;
3190
3191 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003192 rel_ptr = ipath->fspath->val[i] -
3193 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003194 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003195 }
3196
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003197 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3198 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003199 if (ret) {
3200 ret = -EFAULT;
3201 goto out;
3202 }
3203
3204out:
3205 btrfs_free_path(path);
3206 free_ipath(ipath);
3207 kfree(ipa);
3208
3209 return ret;
3210}
3211
3212static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3213{
3214 struct btrfs_data_container *inodes = ctx;
3215 const size_t c = 3 * sizeof(u64);
3216
3217 if (inodes->bytes_left >= c) {
3218 inodes->bytes_left -= c;
3219 inodes->val[inodes->elem_cnt] = inum;
3220 inodes->val[inodes->elem_cnt + 1] = offset;
3221 inodes->val[inodes->elem_cnt + 2] = root;
3222 inodes->elem_cnt += 3;
3223 } else {
3224 inodes->bytes_missing += c - inodes->bytes_left;
3225 inodes->bytes_left = 0;
3226 inodes->elem_missed += 3;
3227 }
3228
3229 return 0;
3230}
3231
3232static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3233 void __user *arg)
3234{
3235 int ret = 0;
3236 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003237 struct btrfs_ioctl_logical_ino_args *loi;
3238 struct btrfs_data_container *inodes = NULL;
3239 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003240
3241 if (!capable(CAP_SYS_ADMIN))
3242 return -EPERM;
3243
3244 loi = memdup_user(arg, sizeof(*loi));
3245 if (IS_ERR(loi)) {
3246 ret = PTR_ERR(loi);
3247 loi = NULL;
3248 goto out;
3249 }
3250
3251 path = btrfs_alloc_path();
3252 if (!path) {
3253 ret = -ENOMEM;
3254 goto out;
3255 }
3256
Liu Bo425d17a2012-09-07 20:01:30 -06003257 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003258 inodes = init_data_container(size);
3259 if (IS_ERR(inodes)) {
3260 ret = PTR_ERR(inodes);
3261 inodes = NULL;
3262 goto out;
3263 }
3264
Liu Bodf031f02012-09-07 20:01:29 -06003265 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3266 build_ino_list, inodes);
3267 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003268 ret = -ENOENT;
3269 if (ret < 0)
3270 goto out;
3271
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003272 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3273 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003274 if (ret)
3275 ret = -EFAULT;
3276
3277out:
3278 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003279 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003280 kfree(loi);
3281
3282 return ret;
3283}
3284
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003285void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003286 struct btrfs_ioctl_balance_args *bargs)
3287{
3288 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3289
3290 bargs->flags = bctl->flags;
3291
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003292 if (atomic_read(&fs_info->balance_running))
3293 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3294 if (atomic_read(&fs_info->balance_pause_req))
3295 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003296 if (atomic_read(&fs_info->balance_cancel_req))
3297 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003298
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003299 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3300 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3301 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003302
3303 if (lock) {
3304 spin_lock(&fs_info->balance_lock);
3305 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3306 spin_unlock(&fs_info->balance_lock);
3307 } else {
3308 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3309 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003310}
3311
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003312static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003313{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003314 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003315 struct btrfs_fs_info *fs_info = root->fs_info;
3316 struct btrfs_ioctl_balance_args *bargs;
3317 struct btrfs_balance_control *bctl;
3318 int ret;
3319
3320 if (!capable(CAP_SYS_ADMIN))
3321 return -EPERM;
3322
Liu Boe54bfa32012-06-29 03:58:48 -06003323 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003324 if (ret)
3325 return ret;
3326
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003327 mutex_lock(&fs_info->volume_mutex);
3328 mutex_lock(&fs_info->balance_mutex);
3329
3330 if (arg) {
3331 bargs = memdup_user(arg, sizeof(*bargs));
3332 if (IS_ERR(bargs)) {
3333 ret = PTR_ERR(bargs);
3334 goto out;
3335 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003336
3337 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3338 if (!fs_info->balance_ctl) {
3339 ret = -ENOTCONN;
3340 goto out_bargs;
3341 }
3342
3343 bctl = fs_info->balance_ctl;
3344 spin_lock(&fs_info->balance_lock);
3345 bctl->flags |= BTRFS_BALANCE_RESUME;
3346 spin_unlock(&fs_info->balance_lock);
3347
3348 goto do_balance;
3349 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003350 } else {
3351 bargs = NULL;
3352 }
3353
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003354 if (fs_info->balance_ctl) {
3355 ret = -EINPROGRESS;
3356 goto out_bargs;
3357 }
3358
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003359 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3360 if (!bctl) {
3361 ret = -ENOMEM;
3362 goto out_bargs;
3363 }
3364
3365 bctl->fs_info = fs_info;
3366 if (arg) {
3367 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3368 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3369 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3370
3371 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003372 } else {
3373 /* balance everything - no filters */
3374 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003375 }
3376
Ilya Dryomovde322262012-01-16 22:04:49 +02003377do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003378 ret = btrfs_balance(bctl, bargs);
3379 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003380 * bctl is freed in __cancel_balance or in free_fs_info if
3381 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003382 */
3383 if (arg) {
3384 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3385 ret = -EFAULT;
3386 }
3387
3388out_bargs:
3389 kfree(bargs);
3390out:
3391 mutex_unlock(&fs_info->balance_mutex);
3392 mutex_unlock(&fs_info->volume_mutex);
Liu Boe54bfa32012-06-29 03:58:48 -06003393 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003394 return ret;
3395}
3396
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003397static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3398{
3399 if (!capable(CAP_SYS_ADMIN))
3400 return -EPERM;
3401
3402 switch (cmd) {
3403 case BTRFS_BALANCE_CTL_PAUSE:
3404 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003405 case BTRFS_BALANCE_CTL_CANCEL:
3406 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003407 }
3408
3409 return -EINVAL;
3410}
3411
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003412static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3413 void __user *arg)
3414{
3415 struct btrfs_fs_info *fs_info = root->fs_info;
3416 struct btrfs_ioctl_balance_args *bargs;
3417 int ret = 0;
3418
3419 if (!capable(CAP_SYS_ADMIN))
3420 return -EPERM;
3421
3422 mutex_lock(&fs_info->balance_mutex);
3423 if (!fs_info->balance_ctl) {
3424 ret = -ENOTCONN;
3425 goto out;
3426 }
3427
3428 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3429 if (!bargs) {
3430 ret = -ENOMEM;
3431 goto out;
3432 }
3433
3434 update_ioctl_balance_args(fs_info, 1, bargs);
3435
3436 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3437 ret = -EFAULT;
3438
3439 kfree(bargs);
3440out:
3441 mutex_unlock(&fs_info->balance_mutex);
3442 return ret;
3443}
3444
Arne Jansen5d13a372011-09-14 15:53:51 +02003445static long btrfs_ioctl_quota_ctl(struct btrfs_root *root, void __user *arg)
3446{
3447 struct btrfs_ioctl_quota_ctl_args *sa;
3448 struct btrfs_trans_handle *trans = NULL;
3449 int ret;
3450 int err;
3451
3452 if (!capable(CAP_SYS_ADMIN))
3453 return -EPERM;
3454
3455 if (root->fs_info->sb->s_flags & MS_RDONLY)
3456 return -EROFS;
3457
3458 sa = memdup_user(arg, sizeof(*sa));
3459 if (IS_ERR(sa))
3460 return PTR_ERR(sa);
3461
3462 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3463 trans = btrfs_start_transaction(root, 2);
3464 if (IS_ERR(trans)) {
3465 ret = PTR_ERR(trans);
3466 goto out;
3467 }
3468 }
3469
3470 switch (sa->cmd) {
3471 case BTRFS_QUOTA_CTL_ENABLE:
3472 ret = btrfs_quota_enable(trans, root->fs_info);
3473 break;
3474 case BTRFS_QUOTA_CTL_DISABLE:
3475 ret = btrfs_quota_disable(trans, root->fs_info);
3476 break;
3477 case BTRFS_QUOTA_CTL_RESCAN:
3478 ret = btrfs_quota_rescan(root->fs_info);
3479 break;
3480 default:
3481 ret = -EINVAL;
3482 break;
3483 }
3484
3485 if (copy_to_user(arg, sa, sizeof(*sa)))
3486 ret = -EFAULT;
3487
3488 if (trans) {
3489 err = btrfs_commit_transaction(trans, root);
3490 if (err && !ret)
3491 ret = err;
3492 }
3493
3494out:
3495 kfree(sa);
3496 return ret;
3497}
3498
3499static long btrfs_ioctl_qgroup_assign(struct btrfs_root *root, void __user *arg)
3500{
3501 struct btrfs_ioctl_qgroup_assign_args *sa;
3502 struct btrfs_trans_handle *trans;
3503 int ret;
3504 int err;
3505
3506 if (!capable(CAP_SYS_ADMIN))
3507 return -EPERM;
3508
3509 if (root->fs_info->sb->s_flags & MS_RDONLY)
3510 return -EROFS;
3511
3512 sa = memdup_user(arg, sizeof(*sa));
3513 if (IS_ERR(sa))
3514 return PTR_ERR(sa);
3515
3516 trans = btrfs_join_transaction(root);
3517 if (IS_ERR(trans)) {
3518 ret = PTR_ERR(trans);
3519 goto out;
3520 }
3521
3522 /* FIXME: check if the IDs really exist */
3523 if (sa->assign) {
3524 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3525 sa->src, sa->dst);
3526 } else {
3527 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3528 sa->src, sa->dst);
3529 }
3530
3531 err = btrfs_end_transaction(trans, root);
3532 if (err && !ret)
3533 ret = err;
3534
3535out:
3536 kfree(sa);
3537 return ret;
3538}
3539
3540static long btrfs_ioctl_qgroup_create(struct btrfs_root *root, void __user *arg)
3541{
3542 struct btrfs_ioctl_qgroup_create_args *sa;
3543 struct btrfs_trans_handle *trans;
3544 int ret;
3545 int err;
3546
3547 if (!capable(CAP_SYS_ADMIN))
3548 return -EPERM;
3549
3550 if (root->fs_info->sb->s_flags & MS_RDONLY)
3551 return -EROFS;
3552
3553 sa = memdup_user(arg, sizeof(*sa));
3554 if (IS_ERR(sa))
3555 return PTR_ERR(sa);
3556
3557 trans = btrfs_join_transaction(root);
3558 if (IS_ERR(trans)) {
3559 ret = PTR_ERR(trans);
3560 goto out;
3561 }
3562
3563 /* FIXME: check if the IDs really exist */
3564 if (sa->create) {
3565 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3566 NULL);
3567 } else {
3568 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3569 }
3570
3571 err = btrfs_end_transaction(trans, root);
3572 if (err && !ret)
3573 ret = err;
3574
3575out:
3576 kfree(sa);
3577 return ret;
3578}
3579
3580static long btrfs_ioctl_qgroup_limit(struct btrfs_root *root, void __user *arg)
3581{
3582 struct btrfs_ioctl_qgroup_limit_args *sa;
3583 struct btrfs_trans_handle *trans;
3584 int ret;
3585 int err;
3586 u64 qgroupid;
3587
3588 if (!capable(CAP_SYS_ADMIN))
3589 return -EPERM;
3590
3591 if (root->fs_info->sb->s_flags & MS_RDONLY)
3592 return -EROFS;
3593
3594 sa = memdup_user(arg, sizeof(*sa));
3595 if (IS_ERR(sa))
3596 return PTR_ERR(sa);
3597
3598 trans = btrfs_join_transaction(root);
3599 if (IS_ERR(trans)) {
3600 ret = PTR_ERR(trans);
3601 goto out;
3602 }
3603
3604 qgroupid = sa->qgroupid;
3605 if (!qgroupid) {
3606 /* take the current subvol as qgroup */
3607 qgroupid = root->root_key.objectid;
3608 }
3609
3610 /* FIXME: check if the IDs really exist */
3611 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3612
3613 err = btrfs_end_transaction(trans, root);
3614 if (err && !ret)
3615 ret = err;
3616
3617out:
3618 kfree(sa);
3619 return ret;
3620}
3621
Alexander Block8ea05e32012-07-25 17:35:53 +02003622static long btrfs_ioctl_set_received_subvol(struct file *file,
3623 void __user *arg)
3624{
3625 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3626 struct inode *inode = fdentry(file)->d_inode;
3627 struct btrfs_root *root = BTRFS_I(inode)->root;
3628 struct btrfs_root_item *root_item = &root->root_item;
3629 struct btrfs_trans_handle *trans;
3630 struct timespec ct = CURRENT_TIME;
3631 int ret = 0;
3632
3633 ret = mnt_want_write_file(file);
3634 if (ret < 0)
3635 return ret;
3636
3637 down_write(&root->fs_info->subvol_sem);
3638
3639 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3640 ret = -EINVAL;
3641 goto out;
3642 }
3643
3644 if (btrfs_root_readonly(root)) {
3645 ret = -EROFS;
3646 goto out;
3647 }
3648
3649 if (!inode_owner_or_capable(inode)) {
3650 ret = -EACCES;
3651 goto out;
3652 }
3653
3654 sa = memdup_user(arg, sizeof(*sa));
3655 if (IS_ERR(sa)) {
3656 ret = PTR_ERR(sa);
3657 sa = NULL;
3658 goto out;
3659 }
3660
3661 trans = btrfs_start_transaction(root, 1);
3662 if (IS_ERR(trans)) {
3663 ret = PTR_ERR(trans);
3664 trans = NULL;
3665 goto out;
3666 }
3667
3668 sa->rtransid = trans->transid;
3669 sa->rtime.sec = ct.tv_sec;
3670 sa->rtime.nsec = ct.tv_nsec;
3671
3672 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3673 btrfs_set_root_stransid(root_item, sa->stransid);
3674 btrfs_set_root_rtransid(root_item, sa->rtransid);
3675 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3676 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3677 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3678 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3679
3680 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3681 &root->root_key, &root->root_item);
3682 if (ret < 0) {
3683 btrfs_end_transaction(trans, root);
3684 trans = NULL;
3685 goto out;
3686 } else {
3687 ret = btrfs_commit_transaction(trans, root);
3688 if (ret < 0)
3689 goto out;
3690 }
3691
3692 ret = copy_to_user(arg, sa, sizeof(*sa));
3693 if (ret)
3694 ret = -EFAULT;
3695
3696out:
3697 kfree(sa);
3698 up_write(&root->fs_info->subvol_sem);
3699 mnt_drop_write_file(file);
3700 return ret;
3701}
3702
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003703long btrfs_ioctl(struct file *file, unsigned int
3704 cmd, unsigned long arg)
3705{
3706 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003707 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003708
3709 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003710 case FS_IOC_GETFLAGS:
3711 return btrfs_ioctl_getflags(file, argp);
3712 case FS_IOC_SETFLAGS:
3713 return btrfs_ioctl_setflags(file, argp);
3714 case FS_IOC_GETVERSION:
3715 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003716 case FITRIM:
3717 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003718 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003719 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003720 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003721 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003722 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003723 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003724 case BTRFS_IOC_SUBVOL_CREATE_V2:
3725 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003726 case BTRFS_IOC_SNAP_DESTROY:
3727 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003728 case BTRFS_IOC_SUBVOL_GETFLAGS:
3729 return btrfs_ioctl_subvol_getflags(file, argp);
3730 case BTRFS_IOC_SUBVOL_SETFLAGS:
3731 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003732 case BTRFS_IOC_DEFAULT_SUBVOL:
3733 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003734 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003735 return btrfs_ioctl_defrag(file, NULL);
3736 case BTRFS_IOC_DEFRAG_RANGE:
3737 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003738 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003739 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003740 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003741 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003742 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003743 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003744 case BTRFS_IOC_FS_INFO:
3745 return btrfs_ioctl_fs_info(root, argp);
3746 case BTRFS_IOC_DEV_INFO:
3747 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003748 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003749 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003750 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003751 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3752 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003753 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003754 case BTRFS_IOC_TRANS_START:
3755 return btrfs_ioctl_trans_start(file);
3756 case BTRFS_IOC_TRANS_END:
3757 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003758 case BTRFS_IOC_TREE_SEARCH:
3759 return btrfs_ioctl_tree_search(file, argp);
3760 case BTRFS_IOC_INO_LOOKUP:
3761 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003762 case BTRFS_IOC_INO_PATHS:
3763 return btrfs_ioctl_ino_to_path(root, argp);
3764 case BTRFS_IOC_LOGICAL_INO:
3765 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003766 case BTRFS_IOC_SPACE_INFO:
3767 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003768 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003769 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3770 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003771 case BTRFS_IOC_START_SYNC:
3772 return btrfs_ioctl_start_sync(file, argp);
3773 case BTRFS_IOC_WAIT_SYNC:
3774 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003775 case BTRFS_IOC_SCRUB:
3776 return btrfs_ioctl_scrub(root, argp);
3777 case BTRFS_IOC_SCRUB_CANCEL:
3778 return btrfs_ioctl_scrub_cancel(root, argp);
3779 case BTRFS_IOC_SCRUB_PROGRESS:
3780 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003781 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003782 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003783 case BTRFS_IOC_BALANCE_CTL:
3784 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003785 case BTRFS_IOC_BALANCE_PROGRESS:
3786 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003787 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3788 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02003789 case BTRFS_IOC_SEND:
3790 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003791 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06003792 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02003793 case BTRFS_IOC_QUOTA_CTL:
3794 return btrfs_ioctl_quota_ctl(root, argp);
3795 case BTRFS_IOC_QGROUP_ASSIGN:
3796 return btrfs_ioctl_qgroup_assign(root, argp);
3797 case BTRFS_IOC_QGROUP_CREATE:
3798 return btrfs_ioctl_qgroup_create(root, argp);
3799 case BTRFS_IOC_QGROUP_LIMIT:
3800 return btrfs_ioctl_qgroup_limit(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003801 }
3802
3803 return -ENOTTY;
3804}