blob: 841044b80658aff9f164692b1af011e4eb981d3f [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>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000045#include <linux/btrfs.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050046#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040059
Christoph Hellwig6cbff002009-04-17 10:37:41 +020060/* Mask out flags that are inappropriate for the given type of inode. */
61static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
62{
63 if (S_ISDIR(mode))
64 return flags;
65 else if (S_ISREG(mode))
66 return flags & ~FS_DIRSYNC_FL;
67 else
68 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
69}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040070
Christoph Hellwig6cbff002009-04-17 10:37:41 +020071/*
72 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
73 */
74static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
75{
76 unsigned int iflags = 0;
77
78 if (flags & BTRFS_INODE_SYNC)
79 iflags |= FS_SYNC_FL;
80 if (flags & BTRFS_INODE_IMMUTABLE)
81 iflags |= FS_IMMUTABLE_FL;
82 if (flags & BTRFS_INODE_APPEND)
83 iflags |= FS_APPEND_FL;
84 if (flags & BTRFS_INODE_NODUMP)
85 iflags |= FS_NODUMP_FL;
86 if (flags & BTRFS_INODE_NOATIME)
87 iflags |= FS_NOATIME_FL;
88 if (flags & BTRFS_INODE_DIRSYNC)
89 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000090 if (flags & BTRFS_INODE_NODATACOW)
91 iflags |= FS_NOCOW_FL;
92
93 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
94 iflags |= FS_COMPR_FL;
95 else if (flags & BTRFS_INODE_NOCOMPRESS)
96 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020097
98 return iflags;
99}
100
101/*
102 * Update inode->i_flags based on the btrfs internal flags.
103 */
104void btrfs_update_iflags(struct inode *inode)
105{
106 struct btrfs_inode *ip = BTRFS_I(inode);
107
108 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
109
110 if (ip->flags & BTRFS_INODE_SYNC)
111 inode->i_flags |= S_SYNC;
112 if (ip->flags & BTRFS_INODE_IMMUTABLE)
113 inode->i_flags |= S_IMMUTABLE;
114 if (ip->flags & BTRFS_INODE_APPEND)
115 inode->i_flags |= S_APPEND;
116 if (ip->flags & BTRFS_INODE_NOATIME)
117 inode->i_flags |= S_NOATIME;
118 if (ip->flags & BTRFS_INODE_DIRSYNC)
119 inode->i_flags |= S_DIRSYNC;
120}
121
122/*
123 * Inherit flags from the parent inode.
124 *
Josef Bacike27425d2011-09-27 11:01:30 -0400125 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200126 */
127void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
128{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400129 unsigned int flags;
130
131 if (!dir)
132 return;
133
134 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200135
Josef Bacike27425d2011-09-27 11:01:30 -0400136 if (flags & BTRFS_INODE_NOCOMPRESS) {
137 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
138 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
139 } else if (flags & BTRFS_INODE_COMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
142 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143
Liu Bo213490b2012-09-11 08:33:50 -0600144 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400145 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600146 if (S_ISREG(inode->i_mode))
147 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
148 }
Josef Bacike27425d2011-09-27 11:01:30 -0400149
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200150 btrfs_update_iflags(inode);
151}
152
153static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
154{
Al Viro496ad9a2013-01-23 17:07:38 -0500155 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200156 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
157
158 if (copy_to_user(arg, &flags, sizeof(flags)))
159 return -EFAULT;
160 return 0;
161}
162
Liu Bo75e7cb72011-03-22 10:12:20 +0000163static int check_flags(unsigned int flags)
164{
165 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
166 FS_NOATIME_FL | FS_NODUMP_FL | \
167 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000168 FS_NOCOMP_FL | FS_COMPR_FL |
169 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000170 return -EOPNOTSUPP;
171
172 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
173 return -EINVAL;
174
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return 0;
176}
177
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200178static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
179{
Al Viro496ad9a2013-01-23 17:07:38 -0500180 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181 struct btrfs_inode *ip = BTRFS_I(inode);
182 struct btrfs_root *root = ip->root;
183 struct btrfs_trans_handle *trans;
184 unsigned int flags, oldflags;
185 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800186 u64 ip_oldflags;
187 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600188 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189
Li Zefanb83cc962010-12-20 16:04:08 +0800190 if (btrfs_root_readonly(root))
191 return -EROFS;
192
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 if (copy_from_user(&flags, arg, sizeof(flags)))
194 return -EFAULT;
195
Liu Bo75e7cb72011-03-22 10:12:20 +0000196 ret = check_flags(flags);
197 if (ret)
198 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200199
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700200 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200201 return -EACCES;
202
Jan Karae7848682012-06-12 16:20:32 +0200203 ret = mnt_want_write_file(file);
204 if (ret)
205 return ret;
206
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207 mutex_lock(&inode->i_mutex);
208
Li Zefanf062abf2011-12-29 13:36:45 +0800209 ip_oldflags = ip->flags;
210 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600211 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800212
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200213 flags = btrfs_mask_flags(inode->i_mode, flags);
214 oldflags = btrfs_flags_to_ioctl(ip->flags);
215 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
216 if (!capable(CAP_LINUX_IMMUTABLE)) {
217 ret = -EPERM;
218 goto out_unlock;
219 }
220 }
221
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222 if (flags & FS_SYNC_FL)
223 ip->flags |= BTRFS_INODE_SYNC;
224 else
225 ip->flags &= ~BTRFS_INODE_SYNC;
226 if (flags & FS_IMMUTABLE_FL)
227 ip->flags |= BTRFS_INODE_IMMUTABLE;
228 else
229 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
230 if (flags & FS_APPEND_FL)
231 ip->flags |= BTRFS_INODE_APPEND;
232 else
233 ip->flags &= ~BTRFS_INODE_APPEND;
234 if (flags & FS_NODUMP_FL)
235 ip->flags |= BTRFS_INODE_NODUMP;
236 else
237 ip->flags &= ~BTRFS_INODE_NODUMP;
238 if (flags & FS_NOATIME_FL)
239 ip->flags |= BTRFS_INODE_NOATIME;
240 else
241 ip->flags &= ~BTRFS_INODE_NOATIME;
242 if (flags & FS_DIRSYNC_FL)
243 ip->flags |= BTRFS_INODE_DIRSYNC;
244 else
245 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600246 if (flags & FS_NOCOW_FL) {
247 if (S_ISREG(mode)) {
248 /*
249 * It's safe to turn csums off here, no extents exist.
250 * Otherwise we want the flag to reflect the real COW
251 * status of the file and will not set it.
252 */
253 if (inode->i_size == 0)
254 ip->flags |= BTRFS_INODE_NODATACOW
255 | BTRFS_INODE_NODATASUM;
256 } else {
257 ip->flags |= BTRFS_INODE_NODATACOW;
258 }
259 } else {
260 /*
261 * Revert back under same assuptions as above
262 */
263 if (S_ISREG(mode)) {
264 if (inode->i_size == 0)
265 ip->flags &= ~(BTRFS_INODE_NODATACOW
266 | BTRFS_INODE_NODATASUM);
267 } else {
268 ip->flags &= ~BTRFS_INODE_NODATACOW;
269 }
270 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200271
Liu Bo75e7cb72011-03-22 10:12:20 +0000272 /*
273 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
274 * flag may be changed automatically if compression code won't make
275 * things smaller.
276 */
277 if (flags & FS_NOCOMP_FL) {
278 ip->flags &= ~BTRFS_INODE_COMPRESS;
279 ip->flags |= BTRFS_INODE_NOCOMPRESS;
280 } else if (flags & FS_COMPR_FL) {
281 ip->flags |= BTRFS_INODE_COMPRESS;
282 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000283 } else {
284 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000285 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200286
Li Zefan4da6f1a2011-12-29 13:39:50 +0800287 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800288 if (IS_ERR(trans)) {
289 ret = PTR_ERR(trans);
290 goto out_drop;
291 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200292
Li Zefan306424cc2011-12-14 20:12:02 -0500293 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400294 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500295 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200296 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200297
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200298 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800299 out_drop:
300 if (ret) {
301 ip->flags = ip_oldflags;
302 inode->i_flags = i_oldflags;
303 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200304
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200305 out_unlock:
306 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200307 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000308 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309}
310
311static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
312{
Al Viro496ad9a2013-01-23 17:07:38 -0500313 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200314
315 return put_user(inode->i_generation, arg);
316}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400317
Li Dongyangf7039b12011-03-24 10:24:28 +0000318static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
319{
Al Viro815745c2011-11-17 15:40:49 -0500320 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000321 struct btrfs_device *device;
322 struct request_queue *q;
323 struct fstrim_range range;
324 u64 minlen = ULLONG_MAX;
325 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500326 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000327 int ret;
328
329 if (!capable(CAP_SYS_ADMIN))
330 return -EPERM;
331
Xiao Guangrong1f781602011-04-20 10:09:16 +0000332 rcu_read_lock();
333 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
334 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000335 if (!device->bdev)
336 continue;
337 q = bdev_get_queue(device->bdev);
338 if (blk_queue_discard(q)) {
339 num_devices++;
340 minlen = min((u64)q->limits.discard_granularity,
341 minlen);
342 }
343 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000344 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200345
Li Dongyangf7039b12011-03-24 10:24:28 +0000346 if (!num_devices)
347 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000348 if (copy_from_user(&range, arg, sizeof(range)))
349 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000350 if (range.start > total_bytes ||
351 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200352 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000353
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200354 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000355 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500356 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000357 if (ret < 0)
358 return ret;
359
360 if (copy_to_user(arg, &range, sizeof(range)))
361 return -EFAULT;
362
363 return 0;
364}
365
Miao Xied5c12072013-02-28 10:04:33 +0000366static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400367 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400368 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200369 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000370 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371{
372 struct btrfs_trans_handle *trans;
373 struct btrfs_key key;
374 struct btrfs_root_item root_item;
375 struct btrfs_inode_item *inode_item;
376 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000377 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400378 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000379 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200380 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400381 int ret;
382 int err;
383 u64 objectid;
384 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500385 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000386 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200387 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400388
Li Zefan581bb052011-04-20 10:06:11 +0800389 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400390 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400391 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000392
Miao Xied5c12072013-02-28 10:04:33 +0000393 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400394 /*
Miao Xied5c12072013-02-28 10:04:33 +0000395 * The same as the snapshot creation, please see the comment
396 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400397 */
Miao Xied5c12072013-02-28 10:04:33 +0000398 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400399 7, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000400 if (ret)
401 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402
Miao Xied5c12072013-02-28 10:04:33 +0000403 trans = btrfs_start_transaction(root, 0);
404 if (IS_ERR(trans)) {
405 ret = PTR_ERR(trans);
406 goto out;
407 }
408 trans->block_rsv = &block_rsv;
409 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400410
Miao Xie8696c532013-02-07 06:02:44 +0000411 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200412 if (ret)
413 goto fail;
414
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400415 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200416 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400417 if (IS_ERR(leaf)) {
418 ret = PTR_ERR(leaf);
419 goto fail;
420 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400422 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400423 btrfs_set_header_bytenr(leaf, leaf->start);
424 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400425 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400426 btrfs_set_header_owner(leaf, objectid);
427
428 write_extent_buffer(leaf, root->fs_info->fsid,
429 (unsigned long)btrfs_header_fsid(leaf),
430 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400431 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
432 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
433 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 btrfs_mark_buffer_dirty(leaf);
435
Alexander Block8ea05e32012-07-25 17:35:53 +0200436 memset(&root_item, 0, sizeof(root_item));
437
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400438 inode_item = &root_item.inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400439 inode_item->generation = cpu_to_le64(1);
440 inode_item->size = cpu_to_le64(3);
441 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400442 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
444
Li Zefan08fe4db2011-03-28 02:01:25 +0000445 root_item.flags = 0;
446 root_item.byte_limit = 0;
447 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
448
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400450 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451 btrfs_set_root_level(&root_item, 0);
452 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000453 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400454 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455
Alexander Block8ea05e32012-07-25 17:35:53 +0200456 btrfs_set_root_generation_v2(&root_item,
457 btrfs_root_generation(&root_item));
458 uuid_le_gen(&new_uuid);
459 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
460 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600461 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200462 root_item.ctime = root_item.otime;
463 btrfs_set_root_ctransid(&root_item, trans->transid);
464 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465
Chris Mason925baed2008-06-25 16:01:30 -0400466 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400467 free_extent_buffer(leaf);
468 leaf = NULL;
469
470 btrfs_set_root_dirid(&root_item, new_dirid);
471
472 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400473 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400474 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
475 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
476 &root_item);
477 if (ret)
478 goto fail;
479
Yan, Zheng76dda932009-09-21 16:00:26 -0400480 key.offset = (u64)-1;
481 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100482 if (IS_ERR(new_root)) {
483 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
484 ret = PTR_ERR(new_root);
485 goto fail;
486 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400487
488 btrfs_record_root_in_trans(trans, new_root);
489
Josef Bacikd82a6f12011-05-11 15:26:06 -0400490 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700491 if (ret) {
492 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100493 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700494 goto fail;
495 }
496
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400497 /*
498 * insert the directory item
499 */
Chris Mason3de45862008-11-17 21:02:50 -0500500 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100501 if (ret) {
502 btrfs_abort_transaction(trans, root, ret);
503 goto fail;
504 }
Chris Mason3de45862008-11-17 21:02:50 -0500505
506 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800507 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500508 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100509 if (ret) {
510 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400511 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100512 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500513
Yan Zheng52c26172009-01-05 15:43:43 -0500514 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
515 ret = btrfs_update_inode(trans, root, dir);
516 BUG_ON(ret);
517
Chris Mason0660b5a2008-11-17 20:37:39 -0500518 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400519 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800520 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400521
Chris Mason0660b5a2008-11-17 20:37:39 -0500522 BUG_ON(ret);
523
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524fail:
Miao Xied5c12072013-02-28 10:04:33 +0000525 trans->block_rsv = NULL;
526 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400527 if (async_transid) {
528 *async_transid = trans->transid;
529 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000530 if (err)
531 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400532 } else {
533 err = btrfs_commit_transaction(trans, root);
534 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400535 if (err && !ret)
536 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500537
538 if (!ret)
539 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000540out:
541 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400542 return ret;
543}
544
Miao Xiee9662f72013-02-28 10:01:15 +0000545static int create_snapshot(struct btrfs_root *root, struct inode *dir,
546 struct dentry *dentry, char *name, int namelen,
547 u64 *async_transid, bool readonly,
548 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400549{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000550 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400551 struct btrfs_pending_snapshot *pending_snapshot;
552 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000553 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400554
555 if (!root->ref_cows)
556 return -EINVAL;
557
Miao Xie6a038432013-05-15 07:48:24 +0000558 ret = btrfs_start_delalloc_inodes(root, 0);
559 if (ret)
560 return ret;
561
562 btrfs_wait_ordered_extents(root, 0);
563
Yan, Zhenga22285a2010-05-16 10:48:46 -0400564 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
565 if (!pending_snapshot)
566 return -ENOMEM;
567
Miao Xie66d8f3d2012-09-06 04:02:28 -0600568 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
569 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000570 /*
571 * 1 - parent dir inode
572 * 2 - dir entries
573 * 1 - root item
574 * 2 - root ref/backref
575 * 1 - root of snapshot
576 */
577 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
578 &pending_snapshot->block_rsv, 7,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400579 &pending_snapshot->qgroup_reserved,
580 false);
Miao Xied5c12072013-02-28 10:04:33 +0000581 if (ret)
582 goto out;
583
Yan, Zhenga22285a2010-05-16 10:48:46 -0400584 pending_snapshot->dentry = dentry;
585 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800586 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000587 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000588 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400589
Miao Xied5c12072013-02-28 10:04:33 +0000590 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400591 if (IS_ERR(trans)) {
592 ret = PTR_ERR(trans);
593 goto fail;
594 }
595
Josef Bacik83515832011-06-14 15:16:14 -0400596 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400597 list_add(&pending_snapshot->list,
598 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400599 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400600 if (async_transid) {
601 *async_transid = trans->transid;
602 ret = btrfs_commit_transaction_async(trans,
603 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000604 if (ret)
605 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400606 } else {
607 ret = btrfs_commit_transaction(trans,
608 root->fs_info->extent_root);
609 }
Miao Xieaec80302013-03-04 09:44:29 +0000610 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400611 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400612
613 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400614 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000615 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400616
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500617 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
618 if (ret)
619 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620
Al Viro2fbe8c82011-07-16 21:38:06 -0400621 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000622 if (IS_ERR(inode)) {
623 ret = PTR_ERR(inode);
624 goto fail;
625 }
626 BUG_ON(!inode);
627 d_instantiate(dentry, inode);
628 ret = 0;
629fail:
Miao Xied5c12072013-02-28 10:04:33 +0000630 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
631 &pending_snapshot->block_rsv,
632 pending_snapshot->qgroup_reserved);
633out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400634 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400635 return ret;
636}
637
Sage Weil4260f7c2010-10-29 15:46:43 -0400638/* copy of check_sticky in fs/namei.c()
639* It's inline, so penalty for filesystems that don't use sticky bit is
640* minimal.
641*/
642static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
643{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800644 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400645
646 if (!(dir->i_mode & S_ISVTX))
647 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800648 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400649 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800650 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400651 return 0;
652 return !capable(CAP_FOWNER);
653}
654
655/* copy of may_delete in fs/namei.c()
656 * Check whether we can remove a link victim from directory dir, check
657 * whether the type of victim is right.
658 * 1. We can't do it if dir is read-only (done in permission())
659 * 2. We should have write and exec permissions on dir
660 * 3. We can't remove anything from append-only dir
661 * 4. We can't do anything with immutable dir (done in permission())
662 * 5. If the sticky bit on dir is set we should either
663 * a. be owner of dir, or
664 * b. be owner of victim, or
665 * c. have CAP_FOWNER capability
666 * 6. If the victim is append-only or immutable we can't do antyhing with
667 * links pointing to it.
668 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
669 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
670 * 9. We can't remove a root or mountpoint.
671 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
672 * nfs_async_unlink().
673 */
674
675static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
676{
677 int error;
678
679 if (!victim->d_inode)
680 return -ENOENT;
681
682 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400683 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400684
685 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
686 if (error)
687 return error;
688 if (IS_APPEND(dir))
689 return -EPERM;
690 if (btrfs_check_sticky(dir, victim->d_inode)||
691 IS_APPEND(victim->d_inode)||
692 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
693 return -EPERM;
694 if (isdir) {
695 if (!S_ISDIR(victim->d_inode->i_mode))
696 return -ENOTDIR;
697 if (IS_ROOT(victim))
698 return -EBUSY;
699 } else if (S_ISDIR(victim->d_inode->i_mode))
700 return -EISDIR;
701 if (IS_DEADDIR(dir))
702 return -ENOENT;
703 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
704 return -EBUSY;
705 return 0;
706}
707
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400708/* copy of may_create in fs/namei.c() */
709static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
710{
711 if (child->d_inode)
712 return -EEXIST;
713 if (IS_DEADDIR(dir))
714 return -ENOENT;
715 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
716}
717
718/*
719 * Create a new subvolume below @parent. This is largely modeled after
720 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
721 * inside this filesystem so it's quite a bit simpler.
722 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400723static noinline int btrfs_mksubvol(struct path *parent,
724 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400725 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200726 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000727 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400728{
Yan, Zheng76dda932009-09-21 16:00:26 -0400729 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400730 struct dentry *dentry;
731 int error;
732
David Sterba5c50c9b2013-03-22 18:12:51 +0000733 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
734 if (error == -EINTR)
735 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400736
737 dentry = lookup_one_len(name, parent->dentry, namelen);
738 error = PTR_ERR(dentry);
739 if (IS_ERR(dentry))
740 goto out_unlock;
741
742 error = -EEXIST;
743 if (dentry->d_inode)
744 goto out_dput;
745
Yan, Zheng76dda932009-09-21 16:00:26 -0400746 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400747 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600748 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400749
Chris Mason9c520572012-12-17 14:26:57 -0500750 /*
751 * even if this name doesn't exist, we may get hash collisions.
752 * check for them now when we can safely fail
753 */
754 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
755 dir->i_ino, name,
756 namelen);
757 if (error)
758 goto out_dput;
759
Yan, Zheng76dda932009-09-21 16:00:26 -0400760 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
761
762 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
763 goto out_up_read;
764
Chris Mason3de45862008-11-17 21:02:50 -0500765 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000766 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200767 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500768 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000769 error = create_subvol(dir, dentry, name, namelen,
770 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500771 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400772 if (!error)
773 fsnotify_mkdir(dir, dentry);
774out_up_read:
775 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400776out_dput:
777 dput(dentry);
778out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400779 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400780 return error;
781}
782
Chris Mason4cb53002011-05-24 15:35:30 -0400783/*
784 * When we're defragging a range, we don't want to kick it off again
785 * if it is really just waiting for delalloc to send it down.
786 * If we find a nice big extent or delalloc range for the bytes in the
787 * file you want to defrag, we return 0 to let you know to skip this
788 * part of the file
789 */
790static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
791{
792 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
793 struct extent_map *em = NULL;
794 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
795 u64 end;
796
797 read_lock(&em_tree->lock);
798 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
799 read_unlock(&em_tree->lock);
800
801 if (em) {
802 end = extent_map_end(em);
803 free_extent_map(em);
804 if (end - offset > thresh)
805 return 0;
806 }
807 /* if we already have a nice delalloc here, just stop */
808 thresh /= 2;
809 end = count_range_bits(io_tree, &offset, offset + thresh,
810 thresh, EXTENT_DELALLOC, 1);
811 if (end >= thresh)
812 return 0;
813 return 1;
814}
815
816/*
817 * helper function to walk through a file and find extents
818 * newer than a specific transid, and smaller than thresh.
819 *
820 * This is used by the defragging code to find new and small
821 * extents
822 */
823static int find_new_extents(struct btrfs_root *root,
824 struct inode *inode, u64 newer_than,
825 u64 *off, int thresh)
826{
827 struct btrfs_path *path;
828 struct btrfs_key min_key;
829 struct btrfs_key max_key;
830 struct extent_buffer *leaf;
831 struct btrfs_file_extent_item *extent;
832 int type;
833 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000834 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400835
836 path = btrfs_alloc_path();
837 if (!path)
838 return -ENOMEM;
839
David Sterbaa4689d22011-05-31 17:08:14 +0000840 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400841 min_key.type = BTRFS_EXTENT_DATA_KEY;
842 min_key.offset = *off;
843
David Sterbaa4689d22011-05-31 17:08:14 +0000844 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400845 max_key.type = (u8)-1;
846 max_key.offset = (u64)-1;
847
848 path->keep_locks = 1;
849
850 while(1) {
851 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +0000852 path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400853 if (ret != 0)
854 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000855 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400856 goto none;
857 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
858 goto none;
859
860 leaf = path->nodes[0];
861 extent = btrfs_item_ptr(leaf, path->slots[0],
862 struct btrfs_file_extent_item);
863
864 type = btrfs_file_extent_type(leaf, extent);
865 if (type == BTRFS_FILE_EXTENT_REG &&
866 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
867 check_defrag_in_cache(inode, min_key.offset, thresh)) {
868 *off = min_key.offset;
869 btrfs_free_path(path);
870 return 0;
871 }
872
873 if (min_key.offset == (u64)-1)
874 goto none;
875
876 min_key.offset++;
877 btrfs_release_path(path);
878 }
879none:
880 btrfs_free_path(path);
881 return -ENOENT;
882}
883
Li Zefan6c282eb2012-06-11 16:03:35 +0800884static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400885{
886 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500887 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800888 struct extent_map *em;
889 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500890
891 /*
892 * hopefully we have this extent in the tree already, try without
893 * the full extent lock
894 */
895 read_lock(&em_tree->lock);
896 em = lookup_extent_mapping(em_tree, start, len);
897 read_unlock(&em_tree->lock);
898
899 if (!em) {
900 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100901 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500902 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100903 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500904
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000905 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800906 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500907 }
908
Li Zefan6c282eb2012-06-11 16:03:35 +0800909 return em;
910}
911
912static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
913{
914 struct extent_map *next;
915 bool ret = true;
916
917 /* this is the last extent */
918 if (em->start + em->len >= i_size_read(inode))
919 return false;
920
921 next = defrag_lookup_extent(inode, em->start + em->len);
922 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
923 ret = false;
924
925 free_extent_map(next);
926 return ret;
927}
928
929static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400930 u64 *last_len, u64 *skip, u64 *defrag_end,
931 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800932{
933 struct extent_map *em;
934 int ret = 1;
935 bool next_mergeable = true;
936
937 /*
938 * make sure that once we start defragging an extent, we keep on
939 * defragging it
940 */
941 if (start < *defrag_end)
942 return 1;
943
944 *skip = 0;
945
946 em = defrag_lookup_extent(inode, start);
947 if (!em)
948 return 0;
949
Chris Mason940100a2010-03-10 10:52:59 -0500950 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400951 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500952 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400953 goto out;
954 }
955
Li Zefan6c282eb2012-06-11 16:03:35 +0800956 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500957
958 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800959 * we hit a real extent, if it is big or the next extent is not a
960 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500961 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400962 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800963 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500964 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400965out:
Chris Mason940100a2010-03-10 10:52:59 -0500966 /*
967 * last_len ends up being a counter of how many bytes we've defragged.
968 * every time we choose not to defrag an extent, we reset *last_len
969 * so that the next tiny extent will force a defrag.
970 *
971 * The end result of this is that tiny extents before a single big
972 * extent will force at least part of that big extent to be defragged.
973 */
974 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500975 *defrag_end = extent_map_end(em);
976 } else {
977 *last_len = 0;
978 *skip = extent_map_end(em);
979 *defrag_end = 0;
980 }
981
982 free_extent_map(em);
983 return ret;
984}
985
Chris Mason4cb53002011-05-24 15:35:30 -0400986/*
987 * it doesn't do much good to defrag one or two pages
988 * at a time. This pulls in a nice chunk of pages
989 * to COW and defrag.
990 *
991 * It also makes sure the delalloc code has enough
992 * dirty data to avoid making new small extents as part
993 * of the defrag
994 *
995 * It's a good idea to start RA on this range
996 * before calling this.
997 */
998static int cluster_pages_for_defrag(struct inode *inode,
999 struct page **pages,
1000 unsigned long start_index,
1001 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001002{
Chris Mason4cb53002011-05-24 15:35:30 -04001003 unsigned long file_end;
1004 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001005 u64 page_start;
1006 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001007 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001008 int ret;
1009 int i;
1010 int i_done;
1011 struct btrfs_ordered_extent *ordered;
1012 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001013 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001014 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001015
Chris Mason4cb53002011-05-24 15:35:30 -04001016 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001017 if (!isize || start_index > file_end)
1018 return 0;
1019
1020 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001021
1022 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001023 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001024 if (ret)
1025 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001026 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001027 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001028
1029 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001030 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001031 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001032again:
Josef Bacika94733d2011-07-11 10:47:06 -04001033 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001034 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001035 if (!page)
1036 break;
1037
Miao Xie600a45e2012-02-16 15:01:24 +08001038 page_start = page_offset(page);
1039 page_end = page_start + PAGE_CACHE_SIZE - 1;
1040 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001041 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001042 ordered = btrfs_lookup_ordered_extent(inode,
1043 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001044 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001045 if (!ordered)
1046 break;
1047
1048 unlock_page(page);
1049 btrfs_start_ordered_extent(inode, ordered, 1);
1050 btrfs_put_ordered_extent(ordered);
1051 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001052 /*
1053 * we unlocked the page above, so we need check if
1054 * it was released or not.
1055 */
1056 if (page->mapping != inode->i_mapping) {
1057 unlock_page(page);
1058 page_cache_release(page);
1059 goto again;
1060 }
Miao Xie600a45e2012-02-16 15:01:24 +08001061 }
1062
Chris Mason4cb53002011-05-24 15:35:30 -04001063 if (!PageUptodate(page)) {
1064 btrfs_readpage(NULL, page);
1065 lock_page(page);
1066 if (!PageUptodate(page)) {
1067 unlock_page(page);
1068 page_cache_release(page);
1069 ret = -EIO;
1070 break;
1071 }
1072 }
Miao Xie600a45e2012-02-16 15:01:24 +08001073
Miao Xie600a45e2012-02-16 15:01:24 +08001074 if (page->mapping != inode->i_mapping) {
1075 unlock_page(page);
1076 page_cache_release(page);
1077 goto again;
1078 }
1079
Chris Mason4cb53002011-05-24 15:35:30 -04001080 pages[i] = page;
1081 i_done++;
1082 }
1083 if (!i_done || ret)
1084 goto out;
1085
1086 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1087 goto out;
1088
1089 /*
1090 * so now we have a nice long stream of locked
1091 * and up to date pages, lets wait on them
1092 */
1093 for (i = 0; i < i_done; i++)
1094 wait_on_page_writeback(pages[i]);
1095
1096 page_start = page_offset(pages[0]);
1097 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1098
1099 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001100 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001101 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1102 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001103 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1104 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001105
Liu Bo1f12bd02012-03-29 09:57:44 -04001106 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001107 spin_lock(&BTRFS_I(inode)->lock);
1108 BTRFS_I(inode)->outstanding_extents++;
1109 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001110 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001111 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001112 }
1113
1114
Liu Bo9e8a4a82012-09-05 19:10:51 -06001115 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1116 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001117
1118 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1119 page_start, page_end - 1, &cached_state,
1120 GFP_NOFS);
1121
1122 for (i = 0; i < i_done; i++) {
1123 clear_page_dirty_for_io(pages[i]);
1124 ClearPageChecked(pages[i]);
1125 set_page_extent_mapped(pages[i]);
1126 set_page_dirty(pages[i]);
1127 unlock_page(pages[i]);
1128 page_cache_release(pages[i]);
1129 }
1130 return i_done;
1131out:
1132 for (i = 0; i < i_done; i++) {
1133 unlock_page(pages[i]);
1134 page_cache_release(pages[i]);
1135 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001136 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001137 return ret;
1138
1139}
1140
1141int btrfs_defrag_file(struct inode *inode, struct file *file,
1142 struct btrfs_ioctl_defrag_range_args *range,
1143 u64 newer_than, unsigned long max_to_defrag)
1144{
1145 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001146 struct file_ra_state *ra = NULL;
1147 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001148 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001149 u64 last_len = 0;
1150 u64 skip = 0;
1151 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001152 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001153 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001154 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001155 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001156 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001157 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001158 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001159 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1160 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001161 u64 new_align = ~((u64)128 * 1024 - 1);
1162 struct page **pages = NULL;
1163
Liu Bo0abd5b12013-04-16 09:20:28 +00001164 if (isize == 0)
1165 return 0;
1166
1167 if (range->start >= isize)
1168 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001169
1170 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1171 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1172 return -EINVAL;
1173 if (range->compress_type)
1174 compress_type = range->compress_type;
1175 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001176
Liu Bo0abd5b12013-04-16 09:20:28 +00001177 if (extent_thresh == 0)
1178 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001179
Chris Mason4cb53002011-05-24 15:35:30 -04001180 /*
1181 * if we were not given a file, allocate a readahead
1182 * context
1183 */
1184 if (!file) {
1185 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1186 if (!ra)
1187 return -ENOMEM;
1188 file_ra_state_init(ra, inode->i_mapping);
1189 } else {
1190 ra = &file->f_ra;
1191 }
1192
Li Zefan008873e2011-09-02 15:57:07 +08001193 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001194 GFP_NOFS);
1195 if (!pages) {
1196 ret = -ENOMEM;
1197 goto out_ra;
1198 }
1199
1200 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001201 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001202 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001203 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1204 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001205 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001206 }
1207
Chris Mason4cb53002011-05-24 15:35:30 -04001208 if (newer_than) {
1209 ret = find_new_extents(root, inode, newer_than,
1210 &newer_off, 64 * 1024);
1211 if (!ret) {
1212 range->start = newer_off;
1213 /*
1214 * we always align our defrag to help keep
1215 * the extents in the file evenly spaced
1216 */
1217 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001218 } else
1219 goto out_ra;
1220 } else {
1221 i = range->start >> PAGE_CACHE_SHIFT;
1222 }
1223 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001224 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001225
Li Zefan2a0f7f52011-10-10 15:43:34 -04001226 /*
1227 * make writeback starts from i, so the defrag range can be
1228 * written sequentially.
1229 */
1230 if (i < inode->i_mapping->writeback_index)
1231 inode->i_mapping->writeback_index = i;
1232
Chris Masonf7f43cc2011-10-11 11:41:40 -04001233 while (i <= last_index && defrag_count < max_to_defrag &&
1234 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1235 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001236 /*
1237 * make sure we stop running if someone unmounts
1238 * the FS
1239 */
1240 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1241 break;
1242
David Sterba210549e2013-02-09 23:38:06 +00001243 if (btrfs_defrag_cancelled(root->fs_info)) {
1244 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1245 ret = -EAGAIN;
1246 break;
1247 }
1248
Liu Bo66c26892012-03-29 09:57:45 -04001249 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001250 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001251 &defrag_end, range->flags &
1252 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001253 unsigned long next;
1254 /*
1255 * the should_defrag function tells us how much to skip
1256 * bump our counter by the suggested amount
1257 */
1258 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1259 i = max(i + 1, next);
1260 continue;
1261 }
Li Zefan008873e2011-09-02 15:57:07 +08001262
1263 if (!newer_than) {
1264 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1265 PAGE_CACHE_SHIFT) - i;
1266 cluster = min(cluster, max_cluster);
1267 } else {
1268 cluster = max_cluster;
1269 }
1270
Chris Mason1e701a32010-03-11 09:42:04 -05001271 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001272 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001273
Li Zefan008873e2011-09-02 15:57:07 +08001274 if (i + cluster > ra_index) {
1275 ra_index = max(i, ra_index);
1276 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1277 cluster);
1278 ra_index += max_cluster;
1279 }
Chris Mason940100a2010-03-10 10:52:59 -05001280
Liu Boecb8bea2012-03-29 09:57:44 -04001281 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001282 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001283 if (ret < 0) {
1284 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001285 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001286 }
Chris Mason940100a2010-03-10 10:52:59 -05001287
Chris Mason4cb53002011-05-24 15:35:30 -04001288 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001289 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001290 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001291
1292 if (newer_than) {
1293 if (newer_off == (u64)-1)
1294 break;
1295
Liu Boe1f041e2012-03-29 09:57:45 -04001296 if (ret > 0)
1297 i += ret;
1298
Chris Mason4cb53002011-05-24 15:35:30 -04001299 newer_off = max(newer_off + 1,
1300 (u64)i << PAGE_CACHE_SHIFT);
1301
1302 ret = find_new_extents(root, inode,
1303 newer_than, &newer_off,
1304 64 * 1024);
1305 if (!ret) {
1306 range->start = newer_off;
1307 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001308 } else {
1309 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001310 }
Chris Mason4cb53002011-05-24 15:35:30 -04001311 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001312 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001313 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001314 last_len += ret << PAGE_CACHE_SHIFT;
1315 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001316 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001317 last_len = 0;
1318 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001319 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001320 }
1321
Chris Mason1e701a32010-03-11 09:42:04 -05001322 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1323 filemap_flush(inode->i_mapping);
1324
1325 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1326 /* the filemap_flush will queue IO into the worker threads, but
1327 * we have to make sure the IO is actually started and that
1328 * ordered extents get created before we return
1329 */
1330 atomic_inc(&root->fs_info->async_submit_draining);
1331 while (atomic_read(&root->fs_info->nr_async_submits) ||
1332 atomic_read(&root->fs_info->async_delalloc_pages)) {
1333 wait_event(root->fs_info->async_submit_wait,
1334 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1335 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1336 }
1337 atomic_dec(&root->fs_info->async_submit_draining);
1338
1339 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001340 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001341 mutex_unlock(&inode->i_mutex);
1342 }
1343
Li Zefan1a419d82010-10-25 15:12:50 +08001344 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001345 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001346 }
1347
Diego Calleja60ccf822011-09-01 16:33:57 +02001348 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001349
Chris Mason4cb53002011-05-24 15:35:30 -04001350out_ra:
1351 if (!file)
1352 kfree(ra);
1353 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001354 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001355}
1356
Miao Xie198605a2012-11-26 08:43:45 +00001357static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001358 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001359{
1360 u64 new_size;
1361 u64 old_size;
1362 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001363 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001364 struct btrfs_ioctl_vol_args *vol_args;
1365 struct btrfs_trans_handle *trans;
1366 struct btrfs_device *device = NULL;
1367 char *sizestr;
1368 char *devstr = NULL;
1369 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001370 int mod = 0;
1371
Chris Masone441d542009-01-05 16:57:23 -05001372 if (!capable(CAP_SYS_ADMIN))
1373 return -EPERM;
1374
Miao Xie198605a2012-11-26 08:43:45 +00001375 ret = mnt_want_write_file(file);
1376 if (ret)
1377 return ret;
1378
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001379 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1380 1)) {
1381 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Miao Xie97547672012-12-21 10:38:50 +00001382 mnt_drop_write_file(file);
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02001383 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001384 }
1385
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001386 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001387 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001388 if (IS_ERR(vol_args)) {
1389 ret = PTR_ERR(vol_args);
1390 goto out;
1391 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001392
1393 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001394
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001395 sizestr = vol_args->name;
1396 devstr = strchr(sizestr, ':');
1397 if (devstr) {
1398 char *end;
1399 sizestr = devstr + 1;
1400 *devstr = '\0';
1401 devstr = vol_args->name;
1402 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001403 if (!devid) {
1404 ret = -EINVAL;
1405 goto out_free;
1406 }
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001407 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001408 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409 }
Miao Xiedba60f32012-12-21 09:19:51 +00001410
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001411 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001412 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001413 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001414 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001415 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001416 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001417 }
Miao Xiedba60f32012-12-21 09:19:51 +00001418
1419 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001420 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001421 "readonly device %llu\n",
Chris Masona8c4a332012-06-15 20:07:17 -04001422 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001423 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001424 goto out_free;
1425 }
1426
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001427 if (!strcmp(sizestr, "max"))
1428 new_size = device->bdev->bd_inode->i_size;
1429 else {
1430 if (sizestr[0] == '-') {
1431 mod = -1;
1432 sizestr++;
1433 } else if (sizestr[0] == '+') {
1434 mod = 1;
1435 sizestr++;
1436 }
Akinobu Mita91748462010-02-28 10:59:11 +00001437 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001438 if (new_size == 0) {
1439 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001440 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001441 }
1442 }
1443
Stefan Behrens63a212a2012-11-05 18:29:28 +01001444 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001445 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001446 goto out_free;
1447 }
1448
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001449 old_size = device->total_bytes;
1450
1451 if (mod < 0) {
1452 if (new_size > old_size) {
1453 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001454 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455 }
1456 new_size = old_size - new_size;
1457 } else if (mod > 0) {
1458 new_size = old_size + new_size;
1459 }
1460
1461 if (new_size < 256 * 1024 * 1024) {
1462 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001463 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001464 }
1465 if (new_size > device->bdev->bd_inode->i_size) {
1466 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001467 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001468 }
1469
1470 do_div(new_size, root->sectorsize);
1471 new_size *= root->sectorsize;
1472
Josef Bacik606686e2012-06-04 14:03:51 -04001473 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1474 rcu_str_deref(device->name),
1475 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001476
1477 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001478 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001479 if (IS_ERR(trans)) {
1480 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001481 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001482 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001483 ret = btrfs_grow_device(trans, device, new_size);
1484 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001485 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001486 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001487 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001488
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001489out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001490 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001491out:
1492 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001493 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001494 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001495 return ret;
1496}
1497
Sage Weil72fd0322010-10-29 15:41:32 -04001498static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001499 char *name, unsigned long fd, int subvol,
1500 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001501 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001504 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505
Liu Boa874a632012-06-29 03:58:46 -06001506 ret = mnt_want_write_file(file);
1507 if (ret)
1508 goto out;
1509
Sage Weil72fd0322010-10-29 15:41:32 -04001510 namelen = strlen(name);
1511 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001513 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001514 }
1515
Chris Mason16780ca2012-02-20 22:14:55 -05001516 if (name[0] == '.' &&
1517 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1518 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001519 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001520 }
1521
Chris Mason3de45862008-11-17 21:02:50 -05001522 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001523 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001524 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001525 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001526 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001527 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001528 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001529 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001530 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001531 }
1532
Al Viro496ad9a2013-01-23 17:07:38 -05001533 src_inode = file_inode(src.file);
1534 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001535 printk(KERN_INFO "btrfs: Snapshot src from "
1536 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001537 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001538 } else {
1539 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1540 BTRFS_I(src_inode)->root,
1541 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001542 }
Al Viro2903ff02012-08-28 12:52:22 -04001543 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001544 }
Liu Boa874a632012-06-29 03:58:46 -06001545out_drop_write:
1546 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001547out:
Sage Weil72fd0322010-10-29 15:41:32 -04001548 return ret;
1549}
1550
1551static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001552 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001553{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001554 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001555 int ret;
1556
Li Zefanfa0d2b92010-12-20 15:53:28 +08001557 vol_args = memdup_user(arg, sizeof(*vol_args));
1558 if (IS_ERR(vol_args))
1559 return PTR_ERR(vol_args);
1560 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001561
Li Zefanfa0d2b92010-12-20 15:53:28 +08001562 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001563 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001564 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001565
Li Zefanfa0d2b92010-12-20 15:53:28 +08001566 kfree(vol_args);
1567 return ret;
1568}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001569
Li Zefanfa0d2b92010-12-20 15:53:28 +08001570static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1571 void __user *arg, int subvol)
1572{
1573 struct btrfs_ioctl_vol_args_v2 *vol_args;
1574 int ret;
1575 u64 transid = 0;
1576 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001577 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001578 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001579
Li Zefanfa0d2b92010-12-20 15:53:28 +08001580 vol_args = memdup_user(arg, sizeof(*vol_args));
1581 if (IS_ERR(vol_args))
1582 return PTR_ERR(vol_args);
1583 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001584
Li Zefanb83cc962010-12-20 16:04:08 +08001585 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001586 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1587 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001588 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001589 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001590 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001591
1592 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1593 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001594 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1595 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001596 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1597 if (vol_args->size > PAGE_CACHE_SIZE) {
1598 ret = -EINVAL;
1599 goto out;
1600 }
1601 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1602 if (IS_ERR(inherit)) {
1603 ret = PTR_ERR(inherit);
1604 goto out;
1605 }
1606 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001607
1608 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001609 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001610 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001611
1612 if (ret == 0 && ptr &&
1613 copy_to_user(arg +
1614 offsetof(struct btrfs_ioctl_vol_args_v2,
1615 transid), ptr, sizeof(*ptr)))
1616 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001617out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001618 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001619 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001620 return ret;
1621}
1622
Li Zefan0caa1022010-12-20 16:30:25 +08001623static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1624 void __user *arg)
1625{
Al Viro496ad9a2013-01-23 17:07:38 -05001626 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001627 struct btrfs_root *root = BTRFS_I(inode)->root;
1628 int ret = 0;
1629 u64 flags = 0;
1630
Li Zefan33345d012011-04-20 10:31:50 +08001631 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001632 return -EINVAL;
1633
1634 down_read(&root->fs_info->subvol_sem);
1635 if (btrfs_root_readonly(root))
1636 flags |= BTRFS_SUBVOL_RDONLY;
1637 up_read(&root->fs_info->subvol_sem);
1638
1639 if (copy_to_user(arg, &flags, sizeof(flags)))
1640 ret = -EFAULT;
1641
1642 return ret;
1643}
1644
1645static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1646 void __user *arg)
1647{
Al Viro496ad9a2013-01-23 17:07:38 -05001648 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001649 struct btrfs_root *root = BTRFS_I(inode)->root;
1650 struct btrfs_trans_handle *trans;
1651 u64 root_flags;
1652 u64 flags;
1653 int ret = 0;
1654
Liu Bob9ca0662012-06-29 03:58:49 -06001655 ret = mnt_want_write_file(file);
1656 if (ret)
1657 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001658
Liu Bob9ca0662012-06-29 03:58:49 -06001659 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1660 ret = -EINVAL;
1661 goto out_drop_write;
1662 }
Li Zefan0caa1022010-12-20 16:30:25 +08001663
Liu Bob9ca0662012-06-29 03:58:49 -06001664 if (copy_from_user(&flags, arg, sizeof(flags))) {
1665 ret = -EFAULT;
1666 goto out_drop_write;
1667 }
Li Zefan0caa1022010-12-20 16:30:25 +08001668
Liu Bob9ca0662012-06-29 03:58:49 -06001669 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1670 ret = -EINVAL;
1671 goto out_drop_write;
1672 }
Li Zefan0caa1022010-12-20 16:30:25 +08001673
Liu Bob9ca0662012-06-29 03:58:49 -06001674 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1675 ret = -EOPNOTSUPP;
1676 goto out_drop_write;
1677 }
Li Zefan0caa1022010-12-20 16:30:25 +08001678
Liu Bob9ca0662012-06-29 03:58:49 -06001679 if (!inode_owner_or_capable(inode)) {
1680 ret = -EACCES;
1681 goto out_drop_write;
1682 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001683
Li Zefan0caa1022010-12-20 16:30:25 +08001684 down_write(&root->fs_info->subvol_sem);
1685
1686 /* nothing to do */
1687 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001688 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001689
1690 root_flags = btrfs_root_flags(&root->root_item);
1691 if (flags & BTRFS_SUBVOL_RDONLY)
1692 btrfs_set_root_flags(&root->root_item,
1693 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1694 else
1695 btrfs_set_root_flags(&root->root_item,
1696 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1697
1698 trans = btrfs_start_transaction(root, 1);
1699 if (IS_ERR(trans)) {
1700 ret = PTR_ERR(trans);
1701 goto out_reset;
1702 }
1703
Li Zefanb4dc2b82011-02-16 06:06:34 +00001704 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001705 &root->root_key, &root->root_item);
1706
1707 btrfs_commit_transaction(trans, root);
1708out_reset:
1709 if (ret)
1710 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001711out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001712 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001713out_drop_write:
1714 mnt_drop_write_file(file);
1715out:
Li Zefan0caa1022010-12-20 16:30:25 +08001716 return ret;
1717}
1718
Yan, Zheng76dda932009-09-21 16:00:26 -04001719/*
1720 * helper to check if the subvolume references other subvolumes
1721 */
1722static noinline int may_destroy_subvol(struct btrfs_root *root)
1723{
1724 struct btrfs_path *path;
1725 struct btrfs_key key;
1726 int ret;
1727
1728 path = btrfs_alloc_path();
1729 if (!path)
1730 return -ENOMEM;
1731
1732 key.objectid = root->root_key.objectid;
1733 key.type = BTRFS_ROOT_REF_KEY;
1734 key.offset = (u64)-1;
1735
1736 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1737 &key, path, 0, 0);
1738 if (ret < 0)
1739 goto out;
1740 BUG_ON(ret == 0);
1741
1742 ret = 0;
1743 if (path->slots[0] > 0) {
1744 path->slots[0]--;
1745 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1746 if (key.objectid == root->root_key.objectid &&
1747 key.type == BTRFS_ROOT_REF_KEY)
1748 ret = -ENOTEMPTY;
1749 }
1750out:
1751 btrfs_free_path(path);
1752 return ret;
1753}
1754
Chris Masonac8e9812010-02-28 15:39:26 -05001755static noinline int key_in_sk(struct btrfs_key *key,
1756 struct btrfs_ioctl_search_key *sk)
1757{
Chris Masonabc6e132010-03-18 12:10:08 -04001758 struct btrfs_key test;
1759 int ret;
1760
1761 test.objectid = sk->min_objectid;
1762 test.type = sk->min_type;
1763 test.offset = sk->min_offset;
1764
1765 ret = btrfs_comp_cpu_keys(key, &test);
1766 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001767 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001768
1769 test.objectid = sk->max_objectid;
1770 test.type = sk->max_type;
1771 test.offset = sk->max_offset;
1772
1773 ret = btrfs_comp_cpu_keys(key, &test);
1774 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001775 return 0;
1776 return 1;
1777}
1778
1779static noinline int copy_to_sk(struct btrfs_root *root,
1780 struct btrfs_path *path,
1781 struct btrfs_key *key,
1782 struct btrfs_ioctl_search_key *sk,
1783 char *buf,
1784 unsigned long *sk_offset,
1785 int *num_found)
1786{
1787 u64 found_transid;
1788 struct extent_buffer *leaf;
1789 struct btrfs_ioctl_search_header sh;
1790 unsigned long item_off;
1791 unsigned long item_len;
1792 int nritems;
1793 int i;
1794 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001795 int ret = 0;
1796
1797 leaf = path->nodes[0];
1798 slot = path->slots[0];
1799 nritems = btrfs_header_nritems(leaf);
1800
1801 if (btrfs_header_generation(leaf) > sk->max_transid) {
1802 i = nritems;
1803 goto advance_key;
1804 }
1805 found_transid = btrfs_header_generation(leaf);
1806
1807 for (i = slot; i < nritems; i++) {
1808 item_off = btrfs_item_ptr_offset(leaf, i);
1809 item_len = btrfs_item_size_nr(leaf, i);
1810
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001811 btrfs_item_key_to_cpu(leaf, key, i);
1812 if (!key_in_sk(key, sk))
1813 continue;
1814
1815 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001816 item_len = 0;
1817
1818 if (sizeof(sh) + item_len + *sk_offset >
1819 BTRFS_SEARCH_ARGS_BUFSIZE) {
1820 ret = 1;
1821 goto overflow;
1822 }
1823
Chris Masonac8e9812010-02-28 15:39:26 -05001824 sh.objectid = key->objectid;
1825 sh.offset = key->offset;
1826 sh.type = key->type;
1827 sh.len = item_len;
1828 sh.transid = found_transid;
1829
1830 /* copy search result header */
1831 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1832 *sk_offset += sizeof(sh);
1833
1834 if (item_len) {
1835 char *p = buf + *sk_offset;
1836 /* copy the item */
1837 read_extent_buffer(leaf, p,
1838 item_off, item_len);
1839 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001840 }
Hugo Millse2156862011-05-14 17:43:41 +00001841 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001842
1843 if (*num_found >= sk->nr_items)
1844 break;
1845 }
1846advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001847 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001848 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1849 key->offset++;
1850 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1851 key->offset = 0;
1852 key->type++;
1853 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1854 key->offset = 0;
1855 key->type = 0;
1856 key->objectid++;
1857 } else
1858 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001859overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001860 return ret;
1861}
1862
1863static noinline int search_ioctl(struct inode *inode,
1864 struct btrfs_ioctl_search_args *args)
1865{
1866 struct btrfs_root *root;
1867 struct btrfs_key key;
1868 struct btrfs_key max_key;
1869 struct btrfs_path *path;
1870 struct btrfs_ioctl_search_key *sk = &args->key;
1871 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1872 int ret;
1873 int num_found = 0;
1874 unsigned long sk_offset = 0;
1875
1876 path = btrfs_alloc_path();
1877 if (!path)
1878 return -ENOMEM;
1879
1880 if (sk->tree_id == 0) {
1881 /* search the root of the inode that was passed */
1882 root = BTRFS_I(inode)->root;
1883 } else {
1884 key.objectid = sk->tree_id;
1885 key.type = BTRFS_ROOT_ITEM_KEY;
1886 key.offset = (u64)-1;
1887 root = btrfs_read_fs_root_no_name(info, &key);
1888 if (IS_ERR(root)) {
1889 printk(KERN_ERR "could not find root %llu\n",
1890 sk->tree_id);
1891 btrfs_free_path(path);
1892 return -ENOENT;
1893 }
1894 }
1895
1896 key.objectid = sk->min_objectid;
1897 key.type = sk->min_type;
1898 key.offset = sk->min_offset;
1899
1900 max_key.objectid = sk->max_objectid;
1901 max_key.type = sk->max_type;
1902 max_key.offset = sk->max_offset;
1903
1904 path->keep_locks = 1;
1905
1906 while(1) {
Eric Sandeende78b512013-01-31 18:21:12 +00001907 ret = btrfs_search_forward(root, &key, &max_key, path,
Chris Masonac8e9812010-02-28 15:39:26 -05001908 sk->min_transid);
1909 if (ret != 0) {
1910 if (ret > 0)
1911 ret = 0;
1912 goto err;
1913 }
1914 ret = copy_to_sk(root, path, &key, sk, args->buf,
1915 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001916 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001917 if (ret || num_found >= sk->nr_items)
1918 break;
1919
1920 }
1921 ret = 0;
1922err:
1923 sk->nr_items = num_found;
1924 btrfs_free_path(path);
1925 return ret;
1926}
1927
1928static noinline int btrfs_ioctl_tree_search(struct file *file,
1929 void __user *argp)
1930{
1931 struct btrfs_ioctl_search_args *args;
1932 struct inode *inode;
1933 int ret;
1934
1935 if (!capable(CAP_SYS_ADMIN))
1936 return -EPERM;
1937
Julia Lawall2354d082010-10-29 15:14:18 -04001938 args = memdup_user(argp, sizeof(*args));
1939 if (IS_ERR(args))
1940 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001941
Al Viro496ad9a2013-01-23 17:07:38 -05001942 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001943 ret = search_ioctl(inode, args);
1944 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1945 ret = -EFAULT;
1946 kfree(args);
1947 return ret;
1948}
1949
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001950/*
Chris Masonac8e9812010-02-28 15:39:26 -05001951 * Search INODE_REFs to identify path name of 'dirid' directory
1952 * in a 'tree_id' tree. and sets path name to 'name'.
1953 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001954static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1955 u64 tree_id, u64 dirid, char *name)
1956{
1957 struct btrfs_root *root;
1958 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001959 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001960 int ret = -1;
1961 int slot;
1962 int len;
1963 int total_len = 0;
1964 struct btrfs_inode_ref *iref;
1965 struct extent_buffer *l;
1966 struct btrfs_path *path;
1967
1968 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1969 name[0]='\0';
1970 return 0;
1971 }
1972
1973 path = btrfs_alloc_path();
1974 if (!path)
1975 return -ENOMEM;
1976
Chris Masonac8e9812010-02-28 15:39:26 -05001977 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001978
1979 key.objectid = tree_id;
1980 key.type = BTRFS_ROOT_ITEM_KEY;
1981 key.offset = (u64)-1;
1982 root = btrfs_read_fs_root_no_name(info, &key);
1983 if (IS_ERR(root)) {
1984 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001985 ret = -ENOENT;
1986 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001987 }
1988
1989 key.objectid = dirid;
1990 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001991 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001992
1993 while(1) {
1994 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1995 if (ret < 0)
1996 goto out;
1997
1998 l = path->nodes[0];
1999 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04002000 if (ret > 0 && slot > 0)
2001 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002002 btrfs_item_key_to_cpu(l, &key, slot);
2003
2004 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05002005 key.type != BTRFS_INODE_REF_KEY)) {
2006 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002007 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05002008 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002009
2010 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2011 len = btrfs_inode_ref_name_len(l, iref);
2012 ptr -= len + 1;
2013 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05002014 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002015 goto out;
2016
2017 *(ptr + len) = '/';
2018 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2019
2020 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2021 break;
2022
David Sterbab3b4aa72011-04-21 01:20:15 +02002023 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002024 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002025 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002026 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002027 }
Chris Masonac8e9812010-02-28 15:39:26 -05002028 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002029 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00002030 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002031 name[total_len]='\0';
2032 ret = 0;
2033out:
2034 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002035 return ret;
2036}
2037
2038static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2039 void __user *argp)
2040{
2041 struct btrfs_ioctl_ino_lookup_args *args;
2042 struct inode *inode;
2043 int ret;
2044
2045 if (!capable(CAP_SYS_ADMIN))
2046 return -EPERM;
2047
Julia Lawall2354d082010-10-29 15:14:18 -04002048 args = memdup_user(argp, sizeof(*args));
2049 if (IS_ERR(args))
2050 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002051
Al Viro496ad9a2013-01-23 17:07:38 -05002052 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002053
Chris Mason1b53ac42010-03-18 12:17:05 -04002054 if (args->treeid == 0)
2055 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2056
Chris Masonac8e9812010-02-28 15:39:26 -05002057 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2058 args->treeid, args->objectid,
2059 args->name);
2060
2061 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2062 ret = -EFAULT;
2063
2064 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002065 return ret;
2066}
2067
Yan, Zheng76dda932009-09-21 16:00:26 -04002068static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2069 void __user *arg)
2070{
2071 struct dentry *parent = fdentry(file);
2072 struct dentry *dentry;
2073 struct inode *dir = parent->d_inode;
2074 struct inode *inode;
2075 struct btrfs_root *root = BTRFS_I(dir)->root;
2076 struct btrfs_root *dest = NULL;
2077 struct btrfs_ioctl_vol_args *vol_args;
2078 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002079 struct btrfs_block_rsv block_rsv;
2080 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002081 int namelen;
2082 int ret;
2083 int err = 0;
2084
Yan, Zheng76dda932009-09-21 16:00:26 -04002085 vol_args = memdup_user(arg, sizeof(*vol_args));
2086 if (IS_ERR(vol_args))
2087 return PTR_ERR(vol_args);
2088
2089 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2090 namelen = strlen(vol_args->name);
2091 if (strchr(vol_args->name, '/') ||
2092 strncmp(vol_args->name, "..", namelen) == 0) {
2093 err = -EINVAL;
2094 goto out;
2095 }
2096
Al Viroa561be72011-11-23 11:57:51 -05002097 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002098 if (err)
2099 goto out;
2100
David Sterba5c50c9b2013-03-22 18:12:51 +00002101 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2102 if (err == -EINTR)
2103 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002104 dentry = lookup_one_len(vol_args->name, parent, namelen);
2105 if (IS_ERR(dentry)) {
2106 err = PTR_ERR(dentry);
2107 goto out_unlock_dir;
2108 }
2109
2110 if (!dentry->d_inode) {
2111 err = -ENOENT;
2112 goto out_dput;
2113 }
2114
2115 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002116 dest = BTRFS_I(inode)->root;
2117 if (!capable(CAP_SYS_ADMIN)){
2118 /*
2119 * Regular user. Only allow this with a special mount
2120 * option, when the user has write+exec access to the
2121 * subvol root, and when rmdir(2) would have been
2122 * allowed.
2123 *
2124 * Note that this is _not_ check that the subvol is
2125 * empty or doesn't contain data that we wouldn't
2126 * otherwise be able to delete.
2127 *
2128 * Users who want to delete empty subvols should try
2129 * rmdir(2).
2130 */
2131 err = -EPERM;
2132 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2133 goto out_dput;
2134
2135 /*
2136 * Do not allow deletion if the parent dir is the same
2137 * as the dir to be deleted. That means the ioctl
2138 * must be called on the dentry referencing the root
2139 * of the subvol, not a random directory contained
2140 * within it.
2141 */
2142 err = -EINVAL;
2143 if (root == dest)
2144 goto out_dput;
2145
2146 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2147 if (err)
2148 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002149 }
2150
Miao Xie5c39da52012-10-22 11:39:53 +00002151 /* check if subvolume may be deleted by a user */
2152 err = btrfs_may_delete(dir, dentry, 1);
2153 if (err)
2154 goto out_dput;
2155
Li Zefan33345d012011-04-20 10:31:50 +08002156 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002157 err = -EINVAL;
2158 goto out_dput;
2159 }
2160
Yan, Zheng76dda932009-09-21 16:00:26 -04002161 mutex_lock(&inode->i_mutex);
2162 err = d_invalidate(dentry);
2163 if (err)
2164 goto out_unlock;
2165
2166 down_write(&root->fs_info->subvol_sem);
2167
2168 err = may_destroy_subvol(dest);
2169 if (err)
2170 goto out_up_write;
2171
Miao Xiec58aaad2013-02-28 10:05:36 +00002172 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2173 /*
2174 * One for dir inode, two for dir entries, two for root
2175 * ref/backref.
2176 */
2177 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002178 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002179 if (err)
2180 goto out_up_write;
2181
Yan, Zhenga22285a2010-05-16 10:48:46 -04002182 trans = btrfs_start_transaction(root, 0);
2183 if (IS_ERR(trans)) {
2184 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002185 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002186 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002187 trans->block_rsv = &block_rsv;
2188 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002189
Yan, Zheng76dda932009-09-21 16:00:26 -04002190 ret = btrfs_unlink_subvol(trans, root, dir,
2191 dest->root_key.objectid,
2192 dentry->d_name.name,
2193 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002194 if (ret) {
2195 err = ret;
2196 btrfs_abort_transaction(trans, root, ret);
2197 goto out_end_trans;
2198 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002199
2200 btrfs_record_root_in_trans(trans, dest);
2201
2202 memset(&dest->root_item.drop_progress, 0,
2203 sizeof(dest->root_item.drop_progress));
2204 dest->root_item.drop_level = 0;
2205 btrfs_set_root_refs(&dest->root_item, 0);
2206
Yan, Zhengd68fc572010-05-16 10:49:58 -04002207 if (!xchg(&dest->orphan_item_inserted, 1)) {
2208 ret = btrfs_insert_orphan_item(trans,
2209 root->fs_info->tree_root,
2210 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002211 if (ret) {
2212 btrfs_abort_transaction(trans, root, ret);
2213 err = ret;
2214 goto out_end_trans;
2215 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002216 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002217out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002218 trans->block_rsv = NULL;
2219 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002220 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002221 if (ret && !err)
2222 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002223 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002224out_release:
2225 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002226out_up_write:
2227 up_write(&root->fs_info->subvol_sem);
2228out_unlock:
2229 mutex_unlock(&inode->i_mutex);
2230 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002231 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002232 btrfs_invalidate_inodes(dest);
2233 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002234
2235 /* the last ref */
2236 if (dest->cache_inode) {
2237 iput(dest->cache_inode);
2238 dest->cache_inode = NULL;
2239 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002240 }
2241out_dput:
2242 dput(dentry);
2243out_unlock_dir:
2244 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002245 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002246out:
2247 kfree(vol_args);
2248 return err;
2249}
2250
Chris Mason1e701a32010-03-11 09:42:04 -05002251static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002252{
Al Viro496ad9a2013-01-23 17:07:38 -05002253 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002254 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002255 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002256 int ret;
2257
Ilya Dryomov25122d12013-01-20 15:57:57 +02002258 ret = mnt_want_write_file(file);
2259 if (ret)
2260 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002261
Ilya Dryomov25122d12013-01-20 15:57:57 +02002262 if (btrfs_root_readonly(root)) {
2263 ret = -EROFS;
2264 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002265 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002266
2267 switch (inode->i_mode & S_IFMT) {
2268 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002269 if (!capable(CAP_SYS_ADMIN)) {
2270 ret = -EPERM;
2271 goto out;
2272 }
Eric Sandeende78b512013-01-31 18:21:12 +00002273 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002274 if (ret)
2275 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002276 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002277 break;
2278 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002279 if (!(file->f_mode & FMODE_WRITE)) {
2280 ret = -EINVAL;
2281 goto out;
2282 }
Chris Mason1e701a32010-03-11 09:42:04 -05002283
2284 range = kzalloc(sizeof(*range), GFP_KERNEL);
2285 if (!range) {
2286 ret = -ENOMEM;
2287 goto out;
2288 }
2289
2290 if (argp) {
2291 if (copy_from_user(range, argp,
2292 sizeof(*range))) {
2293 ret = -EFAULT;
2294 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002295 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002296 }
2297 /* compression requires us to start the IO */
2298 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2299 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2300 range->extent_thresh = (u32)-1;
2301 }
2302 } else {
2303 /* the rest are all set to zero by kzalloc */
2304 range->len = (u64)-1;
2305 }
Al Viro496ad9a2013-01-23 17:07:38 -05002306 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002307 range, 0, 0);
2308 if (ret > 0)
2309 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002310 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002311 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002312 default:
2313 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002314 }
Chris Masone441d542009-01-05 16:57:23 -05002315out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002316 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002317 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002318}
2319
Christoph Hellwigb2950862008-12-02 09:54:17 -05002320static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002321{
2322 struct btrfs_ioctl_vol_args *vol_args;
2323 int ret;
2324
Chris Masone441d542009-01-05 16:57:23 -05002325 if (!capable(CAP_SYS_ADMIN))
2326 return -EPERM;
2327
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002328 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2329 1)) {
2330 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002331 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002332 }
2333
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002334 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002335 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002336 if (IS_ERR(vol_args)) {
2337 ret = PTR_ERR(vol_args);
2338 goto out;
2339 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002340
Mark Fasheh5516e592008-07-24 12:20:14 -04002341 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002342 ret = btrfs_init_new_device(root, vol_args->name);
2343
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002344 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002345out:
2346 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002347 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002348 return ret;
2349}
2350
Miao Xieda249272012-11-26 08:44:50 +00002351static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002352{
Al Viro496ad9a2013-01-23 17:07:38 -05002353 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002354 struct btrfs_ioctl_vol_args *vol_args;
2355 int ret;
2356
Chris Masone441d542009-01-05 16:57:23 -05002357 if (!capable(CAP_SYS_ADMIN))
2358 return -EPERM;
2359
Miao Xieda249272012-11-26 08:44:50 +00002360 ret = mnt_want_write_file(file);
2361 if (ret)
2362 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002363
Li Zefandae7b662009-04-08 15:06:54 +08002364 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002365 if (IS_ERR(vol_args)) {
2366 ret = PTR_ERR(vol_args);
2367 goto out;
2368 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002369
Mark Fasheh5516e592008-07-24 12:20:14 -04002370 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002371
Anand Jain183860f2013-05-17 10:52:45 +00002372 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2373 1)) {
2374 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2375 goto out;
2376 }
2377
2378 mutex_lock(&root->fs_info->volume_mutex);
2379 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002380 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002381 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002382
2383out:
2384 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002385 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002386 return ret;
2387}
2388
Jan Schmidt475f6382011-03-11 15:41:01 +01002389static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2390{
Li Zefan027ed2f2011-06-08 08:27:56 +00002391 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002392 struct btrfs_device *device;
2393 struct btrfs_device *next;
2394 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002395 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002396
2397 if (!capable(CAP_SYS_ADMIN))
2398 return -EPERM;
2399
Li Zefan027ed2f2011-06-08 08:27:56 +00002400 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2401 if (!fi_args)
2402 return -ENOMEM;
2403
2404 fi_args->num_devices = fs_devices->num_devices;
2405 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002406
2407 mutex_lock(&fs_devices->device_list_mutex);
2408 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002409 if (device->devid > fi_args->max_id)
2410 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002411 }
2412 mutex_unlock(&fs_devices->device_list_mutex);
2413
Li Zefan027ed2f2011-06-08 08:27:56 +00002414 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2415 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002416
Li Zefan027ed2f2011-06-08 08:27:56 +00002417 kfree(fi_args);
2418 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002419}
2420
2421static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2422{
2423 struct btrfs_ioctl_dev_info_args *di_args;
2424 struct btrfs_device *dev;
2425 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2426 int ret = 0;
2427 char *s_uuid = NULL;
2428 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2429
2430 if (!capable(CAP_SYS_ADMIN))
2431 return -EPERM;
2432
2433 di_args = memdup_user(arg, sizeof(*di_args));
2434 if (IS_ERR(di_args))
2435 return PTR_ERR(di_args);
2436
2437 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2438 s_uuid = di_args->uuid;
2439
2440 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002441 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002442
2443 if (!dev) {
2444 ret = -ENODEV;
2445 goto out;
2446 }
2447
2448 di_args->devid = dev->devid;
2449 di_args->bytes_used = dev->bytes_used;
2450 di_args->total_bytes = dev->total_bytes;
2451 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002452 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002453 struct rcu_string *name;
2454
2455 rcu_read_lock();
2456 name = rcu_dereference(dev->name);
2457 strncpy(di_args->path, name->str, sizeof(di_args->path));
2458 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002459 di_args->path[sizeof(di_args->path) - 1] = 0;
2460 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002461 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002462 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002463
2464out:
David Sterba55793c02013-04-26 15:20:23 +00002465 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002466 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2467 ret = -EFAULT;
2468
2469 kfree(di_args);
2470 return ret;
2471}
2472
Yan, Zheng76dda932009-09-21 16:00:26 -04002473static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2474 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002475{
Al Viro496ad9a2013-01-23 17:07:38 -05002476 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002477 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro2903ff02012-08-28 12:52:22 -04002478 struct fd src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002479 struct inode *src;
2480 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002481 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002482 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002483 char *buf;
2484 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002485 u32 nritems;
2486 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002487 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002488 u64 len = olen;
2489 u64 bs = root->fs_info->sb->s_blocksize;
Liu Boa96fbc72013-05-26 13:50:31 +00002490 int same_inode = 0;
Chris Masond20f7042008-12-08 16:58:54 -05002491
Sage Weilc5c9cd42008-11-12 14:32:25 -05002492 /*
2493 * TODO:
2494 * - split compressed inline extents. annoying: we need to
2495 * decompress into destination's address_space (the file offset
2496 * may change, so source mapping won't do), then recompress (or
2497 * otherwise reinsert) a subrange.
2498 * - allow ranges within the same file to be cloned (provided
2499 * they don't overlap)?
2500 */
2501
Chris Masone441d542009-01-05 16:57:23 -05002502 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002503 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002504 return -EINVAL;
2505
Li Zefanb83cc962010-12-20 16:04:08 +08002506 if (btrfs_root_readonly(root))
2507 return -EROFS;
2508
Al Viroa561be72011-11-23 11:57:51 -05002509 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002510 if (ret)
2511 return ret;
2512
Al Viro2903ff02012-08-28 12:52:22 -04002513 src_file = fdget(srcfd);
2514 if (!src_file.file) {
Yan Zhengab67b7c2008-12-19 10:58:39 -05002515 ret = -EBADF;
2516 goto out_drop_write;
2517 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002518
David Sterba362a20c2011-08-01 18:11:57 +02002519 ret = -EXDEV;
Al Viro2903ff02012-08-28 12:52:22 -04002520 if (src_file.file->f_path.mnt != file->f_path.mnt)
David Sterba362a20c2011-08-01 18:11:57 +02002521 goto out_fput;
2522
Al Viro496ad9a2013-01-23 17:07:38 -05002523 src = file_inode(src_file.file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002524
Sage Weilc5c9cd42008-11-12 14:32:25 -05002525 ret = -EINVAL;
2526 if (src == inode)
Liu Boa96fbc72013-05-26 13:50:31 +00002527 same_inode = 1;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002528
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002529 /* the src must be open for reading */
Al Viro2903ff02012-08-28 12:52:22 -04002530 if (!(src_file.file->f_mode & FMODE_READ))
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002531 goto out_fput;
2532
Li Zefan0e7b8242011-09-18 10:20:46 -04002533 /* don't make the dst file partly checksummed */
2534 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2535 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2536 goto out_fput;
2537
Yan Zhengae01a0a2008-08-04 23:23:47 -04002538 ret = -EISDIR;
2539 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002540 goto out_fput;
2541
Yan Zhengae01a0a2008-08-04 23:23:47 -04002542 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002543 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002544 goto out_fput;
2545
2546 ret = -ENOMEM;
2547 buf = vmalloc(btrfs_level_size(root, 0));
2548 if (!buf)
2549 goto out_fput;
2550
2551 path = btrfs_alloc_path();
2552 if (!path) {
2553 vfree(buf);
2554 goto out_fput;
2555 }
2556 path->reada = 2;
2557
Liu Boa96fbc72013-05-26 13:50:31 +00002558 if (!same_inode) {
2559 if (inode < src) {
2560 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2561 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
2562 } else {
2563 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2564 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2565 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002566 } else {
Liu Boa96fbc72013-05-26 13:50:31 +00002567 mutex_lock(&src->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002568 }
2569
Sage Weilc5c9cd42008-11-12 14:32:25 -05002570 /* determine range to clone */
2571 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002572 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002573 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002574 if (len == 0)
2575 olen = len = src->i_size - off;
2576 /* if we extend to eof, continue to block boundary */
2577 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002578 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002579
2580 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002581 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2582 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002583 goto out_unlock;
2584
Liu Boa96fbc72013-05-26 13:50:31 +00002585 /* verify if ranges are overlapped within the same file */
2586 if (same_inode) {
2587 if (destoff + len > off && destoff < off + len)
2588 goto out_unlock;
2589 }
2590
Li Zefand525e8a2011-09-11 10:52:25 -04002591 if (destoff > inode->i_size) {
2592 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2593 if (ret)
2594 goto out_unlock;
2595 }
2596
Li Zefan71ef0782011-09-18 10:20:46 -04002597 /* truncate page cache pages from target inode range */
2598 truncate_inode_pages_range(&inode->i_data, destoff,
2599 PAGE_CACHE_ALIGN(destoff + len) - 1);
2600
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002601 /* do any pending delalloc/csum calc on src, one way or
2602 another, and lock file content */
2603 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002604 struct btrfs_ordered_extent *ordered;
Liu Boaa42ffd2012-09-18 03:52:23 -06002605 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2606 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
Sage Weil9a019192010-10-29 15:37:33 -04002607 if (!ordered &&
Liu Boaa42ffd2012-09-18 03:52:23 -06002608 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2609 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002610 break;
Liu Boaa42ffd2012-09-18 03:52:23 -06002611 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002612 if (ordered)
2613 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002614 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002615 }
2616
Sage Weilc5c9cd42008-11-12 14:32:25 -05002617 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002618 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002619 key.type = BTRFS_EXTENT_DATA_KEY;
2620 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002621
2622 while (1) {
2623 /*
2624 * note the key will change type as we walk through the
2625 * tree.
2626 */
David Sterba362a20c2011-08-01 18:11:57 +02002627 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2628 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002629 if (ret < 0)
2630 goto out;
2631
Yan Zhengae01a0a2008-08-04 23:23:47 -04002632 nritems = btrfs_header_nritems(path->nodes[0]);
2633 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002634 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002635 if (ret < 0)
2636 goto out;
2637 if (ret > 0)
2638 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002639 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002640 }
2641 leaf = path->nodes[0];
2642 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002643
Yan Zhengae01a0a2008-08-04 23:23:47 -04002644 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002645 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002646 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002647 break;
2648
Sage Weilc5c9cd42008-11-12 14:32:25 -05002649 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2650 struct btrfs_file_extent_item *extent;
2651 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002652 u32 size;
2653 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002654 u64 disko = 0, diskl = 0;
2655 u64 datao = 0, datal = 0;
2656 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002657 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002658
2659 size = btrfs_item_size_nr(leaf, slot);
2660 read_extent_buffer(leaf, buf,
2661 btrfs_item_ptr_offset(leaf, slot),
2662 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002663
2664 extent = btrfs_item_ptr(leaf, slot,
2665 struct btrfs_file_extent_item);
2666 comp = btrfs_file_extent_compression(leaf, extent);
2667 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002668 if (type == BTRFS_FILE_EXTENT_REG ||
2669 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002670 disko = btrfs_file_extent_disk_bytenr(leaf,
2671 extent);
2672 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2673 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002674 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002675 datal = btrfs_file_extent_num_bytes(leaf,
2676 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002677 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2678 /* take upper bound, may be compressed */
2679 datal = btrfs_file_extent_ram_bytes(leaf,
2680 extent);
2681 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002682 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002683
Sage Weil050006a2010-10-29 15:37:33 -04002684 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002685 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002686 goto next;
2687
Zheng Yan31840ae2008-09-23 13:14:14 -04002688 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002689 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002690 if (off <= key.offset)
2691 new_key.offset = key.offset + destoff - off;
2692 else
2693 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002694
Sage Weilb6f34092011-09-20 14:48:51 -04002695 /*
2696 * 1 - adjusting old extent (we may have to split it)
2697 * 1 - add new extent
2698 * 1 - inode update
2699 */
2700 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002701 if (IS_ERR(trans)) {
2702 ret = PTR_ERR(trans);
2703 goto out;
2704 }
2705
Chris Masonc8a894d2009-06-27 21:07:03 -04002706 if (type == BTRFS_FILE_EXTENT_REG ||
2707 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002708 /*
2709 * a | --- range to clone ---| b
2710 * | ------------- extent ------------- |
2711 */
2712
2713 /* substract range b */
2714 if (key.offset + datal > off + len)
2715 datal = off + len - key.offset;
2716
2717 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002718 if (off > key.offset) {
2719 datao += off - key.offset;
2720 datal -= off - key.offset;
2721 }
2722
Josef Bacik5dc562c2012-08-17 13:14:17 -04002723 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002724 new_key.offset,
2725 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002726 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002727 if (ret) {
2728 btrfs_abort_transaction(trans, root,
2729 ret);
2730 btrfs_end_transaction(trans, root);
2731 goto out;
2732 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002733
Sage Weilc5c9cd42008-11-12 14:32:25 -05002734 ret = btrfs_insert_empty_item(trans, root, path,
2735 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002736 if (ret) {
2737 btrfs_abort_transaction(trans, root,
2738 ret);
2739 btrfs_end_transaction(trans, root);
2740 goto out;
2741 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002742
2743 leaf = path->nodes[0];
2744 slot = path->slots[0];
2745 write_extent_buffer(leaf, buf,
2746 btrfs_item_ptr_offset(leaf, slot),
2747 size);
2748
2749 extent = btrfs_item_ptr(leaf, slot,
2750 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002751
Sage Weilc5c9cd42008-11-12 14:32:25 -05002752 /* disko == 0 means it's a hole */
2753 if (!disko)
2754 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002755
2756 btrfs_set_file_extent_offset(leaf, extent,
2757 datao);
2758 btrfs_set_file_extent_num_bytes(leaf, extent,
2759 datal);
2760 if (disko) {
2761 inode_add_bytes(inode, datal);
2762 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002763 disko, diskl, 0,
2764 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002765 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002766 new_key.offset - datao,
2767 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002768 if (ret) {
2769 btrfs_abort_transaction(trans,
2770 root,
2771 ret);
2772 btrfs_end_transaction(trans,
2773 root);
2774 goto out;
2775
2776 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002777 }
2778 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2779 u64 skip = 0;
2780 u64 trim = 0;
2781 if (off > key.offset) {
2782 skip = off - key.offset;
2783 new_key.offset += skip;
2784 }
Chris Masond3977122009-01-05 21:25:51 -05002785
Liu Boaa42ffd2012-09-18 03:52:23 -06002786 if (key.offset + datal > off + len)
2787 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002788
Sage Weilc5c9cd42008-11-12 14:32:25 -05002789 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002790 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002791 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002792 goto out;
2793 }
2794 size -= skip + trim;
2795 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002796
Josef Bacik5dc562c2012-08-17 13:14:17 -04002797 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002798 new_key.offset,
2799 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002800 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002801 if (ret) {
2802 btrfs_abort_transaction(trans, root,
2803 ret);
2804 btrfs_end_transaction(trans, root);
2805 goto out;
2806 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002807
Sage Weilc5c9cd42008-11-12 14:32:25 -05002808 ret = btrfs_insert_empty_item(trans, root, path,
2809 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810 if (ret) {
2811 btrfs_abort_transaction(trans, root,
2812 ret);
2813 btrfs_end_transaction(trans, root);
2814 goto out;
2815 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002816
2817 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002818 u32 start =
2819 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002820 memmove(buf+start, buf+start+skip,
2821 datal);
2822 }
2823
2824 leaf = path->nodes[0];
2825 slot = path->slots[0];
2826 write_extent_buffer(leaf, buf,
2827 btrfs_item_ptr_offset(leaf, slot),
2828 size);
2829 inode_add_bytes(inode, datal);
2830 }
2831
2832 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002833 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002834
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002835 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002836 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002837
2838 /*
2839 * we round up to the block size at eof when
2840 * determining which extents to clone above,
2841 * but shouldn't round up the file size
2842 */
2843 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002844 if (endoff > destoff+olen)
2845 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002846 if (endoff > inode->i_size)
2847 btrfs_i_size_write(inode, endoff);
2848
Yan, Zhenga22285a2010-05-16 10:48:46 -04002849 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002850 if (ret) {
2851 btrfs_abort_transaction(trans, root, ret);
2852 btrfs_end_transaction(trans, root);
2853 goto out;
2854 }
2855 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002856 }
Chris Masond3977122009-01-05 21:25:51 -05002857next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002858 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002859 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002860 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002861 ret = 0;
2862out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002863 btrfs_release_path(path);
Liu Boaa42ffd2012-09-18 03:52:23 -06002864 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002865out_unlock:
2866 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00002867 if (!same_inode)
2868 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002869 vfree(buf);
2870 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002871out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04002872 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002873out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002874 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002875 return ret;
2876}
2877
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002878static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002879{
2880 struct btrfs_ioctl_clone_range_args args;
2881
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002882 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002883 return -EFAULT;
2884 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2885 args.src_length, args.dest_offset);
2886}
2887
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002888/*
2889 * there are many ways the trans_start and trans_end ioctls can lead
2890 * to deadlocks. They should only be used by applications that
2891 * basically own the machine, and have a very in depth understanding
2892 * of all the possible deadlocks and enospc problems.
2893 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002894static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002895{
Al Viro496ad9a2013-01-23 17:07:38 -05002896 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002897 struct btrfs_root *root = BTRFS_I(inode)->root;
2898 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002899 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002900
Sage Weil1ab86ae2009-09-29 18:38:44 -04002901 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002902 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002903 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002904
2905 ret = -EINPROGRESS;
2906 if (file->private_data)
2907 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002908
Li Zefanb83cc962010-12-20 16:04:08 +08002909 ret = -EROFS;
2910 if (btrfs_root_readonly(root))
2911 goto out;
2912
Al Viroa561be72011-11-23 11:57:51 -05002913 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002914 if (ret)
2915 goto out;
2916
Josef Bacika4abeea2011-04-11 17:25:13 -04002917 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002918
Sage Weil1ab86ae2009-09-29 18:38:44 -04002919 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002920 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002921 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002922 goto out_drop;
2923
2924 file->private_data = trans;
2925 return 0;
2926
2927out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002928 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002929 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002930out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002931 return ret;
2932}
2933
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002934static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2935{
Al Viro496ad9a2013-01-23 17:07:38 -05002936 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002937 struct btrfs_root *root = BTRFS_I(inode)->root;
2938 struct btrfs_root *new_root;
2939 struct btrfs_dir_item *di;
2940 struct btrfs_trans_handle *trans;
2941 struct btrfs_path *path;
2942 struct btrfs_key location;
2943 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002944 u64 objectid = 0;
2945 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00002946 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002947
2948 if (!capable(CAP_SYS_ADMIN))
2949 return -EPERM;
2950
Miao Xie3c04ce02012-11-26 08:43:07 +00002951 ret = mnt_want_write_file(file);
2952 if (ret)
2953 return ret;
2954
2955 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2956 ret = -EFAULT;
2957 goto out;
2958 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002959
2960 if (!objectid)
2961 objectid = root->root_key.objectid;
2962
2963 location.objectid = objectid;
2964 location.type = BTRFS_ROOT_ITEM_KEY;
2965 location.offset = (u64)-1;
2966
2967 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00002968 if (IS_ERR(new_root)) {
2969 ret = PTR_ERR(new_root);
2970 goto out;
2971 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002972
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002973 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00002974 if (!path) {
2975 ret = -ENOMEM;
2976 goto out;
2977 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002978 path->leave_spinning = 1;
2979
2980 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002981 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002982 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00002983 ret = PTR_ERR(trans);
2984 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002985 }
2986
David Sterba6c417612011-04-13 15:41:04 +02002987 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002988 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2989 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002990 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002991 btrfs_free_path(path);
2992 btrfs_end_transaction(trans, root);
2993 printk(KERN_ERR "Umm, you don't have the default dir item, "
2994 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00002995 ret = -ENOENT;
2996 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002997 }
2998
2999 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3000 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3001 btrfs_mark_buffer_dirty(path->nodes[0]);
3002 btrfs_free_path(path);
3003
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003004 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003005 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003006out:
3007 mnt_drop_write_file(file);
3008 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003009}
3010
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003011void btrfs_get_block_group_info(struct list_head *groups_list,
3012 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003013{
3014 struct btrfs_block_group_cache *block_group;
3015
3016 space->total_bytes = 0;
3017 space->used_bytes = 0;
3018 space->flags = 0;
3019 list_for_each_entry(block_group, groups_list, list) {
3020 space->flags = block_group->flags;
3021 space->total_bytes += block_group->key.offset;
3022 space->used_bytes +=
3023 btrfs_block_group_used(&block_group->item);
3024 }
3025}
3026
Eric Sandeen48a3b632013-04-25 20:41:01 +00003027static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003028{
3029 struct btrfs_ioctl_space_args space_args;
3030 struct btrfs_ioctl_space_info space;
3031 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003032 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003033 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003034 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003035 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3036 BTRFS_BLOCK_GROUP_SYSTEM,
3037 BTRFS_BLOCK_GROUP_METADATA,
3038 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3039 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003040 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003041 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003042 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003043 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003044
3045 if (copy_from_user(&space_args,
3046 (struct btrfs_ioctl_space_args __user *)arg,
3047 sizeof(space_args)))
3048 return -EFAULT;
3049
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003050 for (i = 0; i < num_types; i++) {
3051 struct btrfs_space_info *tmp;
3052
3053 info = NULL;
3054 rcu_read_lock();
3055 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3056 list) {
3057 if (tmp->flags == types[i]) {
3058 info = tmp;
3059 break;
3060 }
3061 }
3062 rcu_read_unlock();
3063
3064 if (!info)
3065 continue;
3066
3067 down_read(&info->groups_sem);
3068 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3069 if (!list_empty(&info->block_groups[c]))
3070 slot_count++;
3071 }
3072 up_read(&info->groups_sem);
3073 }
Josef Bacik1406e432010-01-13 18:19:06 +00003074
Chris Mason7fde62b2010-03-16 15:40:10 -04003075 /* space_slots == 0 means they are asking for a count */
3076 if (space_args.space_slots == 0) {
3077 space_args.total_spaces = slot_count;
3078 goto out;
3079 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003080
Dan Rosenberg51788b12011-02-14 16:04:23 -05003081 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003082
Chris Mason7fde62b2010-03-16 15:40:10 -04003083 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003084
Chris Mason7fde62b2010-03-16 15:40:10 -04003085 /* we generally have at most 6 or so space infos, one for each raid
3086 * level. So, a whole page should be more than enough for everyone
3087 */
3088 if (alloc_size > PAGE_CACHE_SIZE)
3089 return -ENOMEM;
3090
3091 space_args.total_spaces = 0;
3092 dest = kmalloc(alloc_size, GFP_NOFS);
3093 if (!dest)
3094 return -ENOMEM;
3095 dest_orig = dest;
3096
3097 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003098 for (i = 0; i < num_types; i++) {
3099 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003100
Dan Rosenberg51788b12011-02-14 16:04:23 -05003101 if (!slot_count)
3102 break;
3103
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003104 info = NULL;
3105 rcu_read_lock();
3106 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3107 list) {
3108 if (tmp->flags == types[i]) {
3109 info = tmp;
3110 break;
3111 }
3112 }
3113 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003114
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003115 if (!info)
3116 continue;
3117 down_read(&info->groups_sem);
3118 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3119 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003120 btrfs_get_block_group_info(
3121 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003122 memcpy(dest, &space, sizeof(space));
3123 dest++;
3124 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003125 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003126 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003127 if (!slot_count)
3128 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003129 }
3130 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003131 }
Josef Bacik1406e432010-01-13 18:19:06 +00003132
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003133 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003134 (arg + sizeof(struct btrfs_ioctl_space_args));
3135
3136 if (copy_to_user(user_dest, dest_orig, alloc_size))
3137 ret = -EFAULT;
3138
3139 kfree(dest_orig);
3140out:
3141 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003142 ret = -EFAULT;
3143
3144 return ret;
3145}
3146
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003147/*
3148 * there are many ways the trans_start and trans_end ioctls can lead
3149 * to deadlocks. They should only be used by applications that
3150 * basically own the machine, and have a very in depth understanding
3151 * of all the possible deadlocks and enospc problems.
3152 */
3153long btrfs_ioctl_trans_end(struct file *file)
3154{
Al Viro496ad9a2013-01-23 17:07:38 -05003155 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003156 struct btrfs_root *root = BTRFS_I(inode)->root;
3157 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003158
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003159 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003160 if (!trans)
3161 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003162 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003163
Sage Weil1ab86ae2009-09-29 18:38:44 -04003164 btrfs_end_transaction(trans, root);
3165
Josef Bacika4abeea2011-04-11 17:25:13 -04003166 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003167
Al Viro2a79f172011-12-09 08:06:57 -05003168 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003169 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003170}
3171
Miao Xie9a8c28b2012-11-26 08:40:43 +00003172static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3173 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003174{
Sage Weil46204592010-10-29 15:41:32 -04003175 struct btrfs_trans_handle *trans;
3176 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003177 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003178
Miao Xied4edf392013-02-20 09:17:06 +00003179 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003180 if (IS_ERR(trans)) {
3181 if (PTR_ERR(trans) != -ENOENT)
3182 return PTR_ERR(trans);
3183
3184 /* No running transaction, don't bother */
3185 transid = root->fs_info->last_trans_committed;
3186 goto out;
3187 }
Sage Weil46204592010-10-29 15:41:32 -04003188 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003189 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003190 if (ret) {
3191 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003192 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003193 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003194out:
Sage Weil46204592010-10-29 15:41:32 -04003195 if (argp)
3196 if (copy_to_user(argp, &transid, sizeof(transid)))
3197 return -EFAULT;
3198 return 0;
3199}
3200
Miao Xie9a8c28b2012-11-26 08:40:43 +00003201static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3202 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003203{
Sage Weil46204592010-10-29 15:41:32 -04003204 u64 transid;
3205
3206 if (argp) {
3207 if (copy_from_user(&transid, argp, sizeof(transid)))
3208 return -EFAULT;
3209 } else {
3210 transid = 0; /* current trans */
3211 }
3212 return btrfs_wait_for_commit(root, transid);
3213}
3214
Miao Xieb8e95482012-11-26 08:48:01 +00003215static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003216{
Al Viro496ad9a2013-01-23 17:07:38 -05003217 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003218 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003219 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003220
3221 if (!capable(CAP_SYS_ADMIN))
3222 return -EPERM;
3223
3224 sa = memdup_user(arg, sizeof(*sa));
3225 if (IS_ERR(sa))
3226 return PTR_ERR(sa);
3227
Miao Xieb8e95482012-11-26 08:48:01 +00003228 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3229 ret = mnt_want_write_file(file);
3230 if (ret)
3231 goto out;
3232 }
3233
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003234 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003235 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3236 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003237
3238 if (copy_to_user(arg, sa, sizeof(*sa)))
3239 ret = -EFAULT;
3240
Miao Xieb8e95482012-11-26 08:48:01 +00003241 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3242 mnt_drop_write_file(file);
3243out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003244 kfree(sa);
3245 return ret;
3246}
3247
3248static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3249{
3250 if (!capable(CAP_SYS_ADMIN))
3251 return -EPERM;
3252
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003253 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003254}
3255
3256static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3257 void __user *arg)
3258{
3259 struct btrfs_ioctl_scrub_args *sa;
3260 int ret;
3261
3262 if (!capable(CAP_SYS_ADMIN))
3263 return -EPERM;
3264
3265 sa = memdup_user(arg, sizeof(*sa));
3266 if (IS_ERR(sa))
3267 return PTR_ERR(sa);
3268
3269 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3270
3271 if (copy_to_user(arg, sa, sizeof(*sa)))
3272 ret = -EFAULT;
3273
3274 kfree(sa);
3275 return ret;
3276}
3277
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003278static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003279 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003280{
3281 struct btrfs_ioctl_get_dev_stats *sa;
3282 int ret;
3283
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003284 sa = memdup_user(arg, sizeof(*sa));
3285 if (IS_ERR(sa))
3286 return PTR_ERR(sa);
3287
David Sterbab27f7c02012-06-22 06:30:39 -06003288 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3289 kfree(sa);
3290 return -EPERM;
3291 }
3292
3293 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003294
3295 if (copy_to_user(arg, sa, sizeof(*sa)))
3296 ret = -EFAULT;
3297
3298 kfree(sa);
3299 return ret;
3300}
3301
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003302static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3303{
3304 struct btrfs_ioctl_dev_replace_args *p;
3305 int ret;
3306
3307 if (!capable(CAP_SYS_ADMIN))
3308 return -EPERM;
3309
3310 p = memdup_user(arg, sizeof(*p));
3311 if (IS_ERR(p))
3312 return PTR_ERR(p);
3313
3314 switch (p->cmd) {
3315 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3316 if (atomic_xchg(
3317 &root->fs_info->mutually_exclusive_operation_running,
3318 1)) {
3319 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3320 ret = -EINPROGRESS;
3321 } else {
3322 ret = btrfs_dev_replace_start(root, p);
3323 atomic_set(
3324 &root->fs_info->mutually_exclusive_operation_running,
3325 0);
3326 }
3327 break;
3328 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3329 btrfs_dev_replace_status(root->fs_info, p);
3330 ret = 0;
3331 break;
3332 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3333 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3334 break;
3335 default:
3336 ret = -EINVAL;
3337 break;
3338 }
3339
3340 if (copy_to_user(arg, p, sizeof(*p)))
3341 ret = -EFAULT;
3342
3343 kfree(p);
3344 return ret;
3345}
3346
Jan Schmidtd7728c92011-07-07 16:48:38 +02003347static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3348{
3349 int ret = 0;
3350 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003351 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003352 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003353 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003354 struct inode_fs_paths *ipath = NULL;
3355 struct btrfs_path *path;
3356
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003357 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003358 return -EPERM;
3359
3360 path = btrfs_alloc_path();
3361 if (!path) {
3362 ret = -ENOMEM;
3363 goto out;
3364 }
3365
3366 ipa = memdup_user(arg, sizeof(*ipa));
3367 if (IS_ERR(ipa)) {
3368 ret = PTR_ERR(ipa);
3369 ipa = NULL;
3370 goto out;
3371 }
3372
3373 size = min_t(u32, ipa->size, 4096);
3374 ipath = init_ipath(size, root, path);
3375 if (IS_ERR(ipath)) {
3376 ret = PTR_ERR(ipath);
3377 ipath = NULL;
3378 goto out;
3379 }
3380
3381 ret = paths_from_inode(ipa->inum, ipath);
3382 if (ret < 0)
3383 goto out;
3384
3385 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003386 rel_ptr = ipath->fspath->val[i] -
3387 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003388 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003389 }
3390
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003391 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3392 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003393 if (ret) {
3394 ret = -EFAULT;
3395 goto out;
3396 }
3397
3398out:
3399 btrfs_free_path(path);
3400 free_ipath(ipath);
3401 kfree(ipa);
3402
3403 return ret;
3404}
3405
3406static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3407{
3408 struct btrfs_data_container *inodes = ctx;
3409 const size_t c = 3 * sizeof(u64);
3410
3411 if (inodes->bytes_left >= c) {
3412 inodes->bytes_left -= c;
3413 inodes->val[inodes->elem_cnt] = inum;
3414 inodes->val[inodes->elem_cnt + 1] = offset;
3415 inodes->val[inodes->elem_cnt + 2] = root;
3416 inodes->elem_cnt += 3;
3417 } else {
3418 inodes->bytes_missing += c - inodes->bytes_left;
3419 inodes->bytes_left = 0;
3420 inodes->elem_missed += 3;
3421 }
3422
3423 return 0;
3424}
3425
3426static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3427 void __user *arg)
3428{
3429 int ret = 0;
3430 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003431 struct btrfs_ioctl_logical_ino_args *loi;
3432 struct btrfs_data_container *inodes = NULL;
3433 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003434
3435 if (!capable(CAP_SYS_ADMIN))
3436 return -EPERM;
3437
3438 loi = memdup_user(arg, sizeof(*loi));
3439 if (IS_ERR(loi)) {
3440 ret = PTR_ERR(loi);
3441 loi = NULL;
3442 goto out;
3443 }
3444
3445 path = btrfs_alloc_path();
3446 if (!path) {
3447 ret = -ENOMEM;
3448 goto out;
3449 }
3450
Liu Bo425d17a2012-09-07 20:01:30 -06003451 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003452 inodes = init_data_container(size);
3453 if (IS_ERR(inodes)) {
3454 ret = PTR_ERR(inodes);
3455 inodes = NULL;
3456 goto out;
3457 }
3458
Liu Bodf031f02012-09-07 20:01:29 -06003459 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3460 build_ino_list, inodes);
3461 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003462 ret = -ENOENT;
3463 if (ret < 0)
3464 goto out;
3465
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003466 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3467 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003468 if (ret)
3469 ret = -EFAULT;
3470
3471out:
3472 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003473 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003474 kfree(loi);
3475
3476 return ret;
3477}
3478
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003479void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003480 struct btrfs_ioctl_balance_args *bargs)
3481{
3482 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3483
3484 bargs->flags = bctl->flags;
3485
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003486 if (atomic_read(&fs_info->balance_running))
3487 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3488 if (atomic_read(&fs_info->balance_pause_req))
3489 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003490 if (atomic_read(&fs_info->balance_cancel_req))
3491 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003492
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003493 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3494 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3495 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003496
3497 if (lock) {
3498 spin_lock(&fs_info->balance_lock);
3499 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3500 spin_unlock(&fs_info->balance_lock);
3501 } else {
3502 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3503 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003504}
3505
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003506static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003507{
Al Viro496ad9a2013-01-23 17:07:38 -05003508 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003509 struct btrfs_fs_info *fs_info = root->fs_info;
3510 struct btrfs_ioctl_balance_args *bargs;
3511 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003512 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003513 int ret;
3514
3515 if (!capable(CAP_SYS_ADMIN))
3516 return -EPERM;
3517
Liu Boe54bfa32012-06-29 03:58:48 -06003518 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003519 if (ret)
3520 return ret;
3521
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003522again:
3523 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3524 mutex_lock(&fs_info->volume_mutex);
3525 mutex_lock(&fs_info->balance_mutex);
3526 need_unlock = true;
3527 goto locked;
3528 }
3529
3530 /*
3531 * mut. excl. ops lock is locked. Three possibilites:
3532 * (1) some other op is running
3533 * (2) balance is running
3534 * (3) balance is paused -- special case (think resume)
3535 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003536 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003537 if (fs_info->balance_ctl) {
3538 /* this is either (2) or (3) */
3539 if (!atomic_read(&fs_info->balance_running)) {
3540 mutex_unlock(&fs_info->balance_mutex);
3541 if (!mutex_trylock(&fs_info->volume_mutex))
3542 goto again;
3543 mutex_lock(&fs_info->balance_mutex);
3544
3545 if (fs_info->balance_ctl &&
3546 !atomic_read(&fs_info->balance_running)) {
3547 /* this is (3) */
3548 need_unlock = false;
3549 goto locked;
3550 }
3551
3552 mutex_unlock(&fs_info->balance_mutex);
3553 mutex_unlock(&fs_info->volume_mutex);
3554 goto again;
3555 } else {
3556 /* this is (2) */
3557 mutex_unlock(&fs_info->balance_mutex);
3558 ret = -EINPROGRESS;
3559 goto out;
3560 }
3561 } else {
3562 /* this is (1) */
3563 mutex_unlock(&fs_info->balance_mutex);
3564 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3565 ret = -EINVAL;
3566 goto out;
3567 }
3568
3569locked:
3570 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003571
3572 if (arg) {
3573 bargs = memdup_user(arg, sizeof(*bargs));
3574 if (IS_ERR(bargs)) {
3575 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003576 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003577 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003578
3579 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3580 if (!fs_info->balance_ctl) {
3581 ret = -ENOTCONN;
3582 goto out_bargs;
3583 }
3584
3585 bctl = fs_info->balance_ctl;
3586 spin_lock(&fs_info->balance_lock);
3587 bctl->flags |= BTRFS_BALANCE_RESUME;
3588 spin_unlock(&fs_info->balance_lock);
3589
3590 goto do_balance;
3591 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003592 } else {
3593 bargs = NULL;
3594 }
3595
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003596 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003597 ret = -EINPROGRESS;
3598 goto out_bargs;
3599 }
3600
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003601 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3602 if (!bctl) {
3603 ret = -ENOMEM;
3604 goto out_bargs;
3605 }
3606
3607 bctl->fs_info = fs_info;
3608 if (arg) {
3609 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3610 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3611 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3612
3613 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003614 } else {
3615 /* balance everything - no filters */
3616 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003617 }
3618
Ilya Dryomovde322262012-01-16 22:04:49 +02003619do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003620 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003621 * Ownership of bctl and mutually_exclusive_operation_running
3622 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3623 * or, if restriper was paused all the way until unmount, in
3624 * free_fs_info. mutually_exclusive_operation_running is
3625 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003626 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003627 need_unlock = false;
3628
3629 ret = btrfs_balance(bctl, bargs);
3630
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003631 if (arg) {
3632 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3633 ret = -EFAULT;
3634 }
3635
3636out_bargs:
3637 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003638out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003639 mutex_unlock(&fs_info->balance_mutex);
3640 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003641 if (need_unlock)
3642 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3643out:
Liu Boe54bfa32012-06-29 03:58:48 -06003644 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003645 return ret;
3646}
3647
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003648static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3649{
3650 if (!capable(CAP_SYS_ADMIN))
3651 return -EPERM;
3652
3653 switch (cmd) {
3654 case BTRFS_BALANCE_CTL_PAUSE:
3655 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003656 case BTRFS_BALANCE_CTL_CANCEL:
3657 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003658 }
3659
3660 return -EINVAL;
3661}
3662
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003663static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3664 void __user *arg)
3665{
3666 struct btrfs_fs_info *fs_info = root->fs_info;
3667 struct btrfs_ioctl_balance_args *bargs;
3668 int ret = 0;
3669
3670 if (!capable(CAP_SYS_ADMIN))
3671 return -EPERM;
3672
3673 mutex_lock(&fs_info->balance_mutex);
3674 if (!fs_info->balance_ctl) {
3675 ret = -ENOTCONN;
3676 goto out;
3677 }
3678
3679 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3680 if (!bargs) {
3681 ret = -ENOMEM;
3682 goto out;
3683 }
3684
3685 update_ioctl_balance_args(fs_info, 1, bargs);
3686
3687 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3688 ret = -EFAULT;
3689
3690 kfree(bargs);
3691out:
3692 mutex_unlock(&fs_info->balance_mutex);
3693 return ret;
3694}
3695
Miao Xie905b0dd2012-11-26 08:50:11 +00003696static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003697{
Al Viro496ad9a2013-01-23 17:07:38 -05003698 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003699 struct btrfs_ioctl_quota_ctl_args *sa;
3700 struct btrfs_trans_handle *trans = NULL;
3701 int ret;
3702 int err;
3703
3704 if (!capable(CAP_SYS_ADMIN))
3705 return -EPERM;
3706
Miao Xie905b0dd2012-11-26 08:50:11 +00003707 ret = mnt_want_write_file(file);
3708 if (ret)
3709 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003710
3711 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003712 if (IS_ERR(sa)) {
3713 ret = PTR_ERR(sa);
3714 goto drop_write;
3715 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003716
Wang Shilong7708f022013-04-07 10:24:57 +00003717 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00003718 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
3719 if (IS_ERR(trans)) {
3720 ret = PTR_ERR(trans);
3721 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02003722 }
3723
3724 switch (sa->cmd) {
3725 case BTRFS_QUOTA_CTL_ENABLE:
3726 ret = btrfs_quota_enable(trans, root->fs_info);
3727 break;
3728 case BTRFS_QUOTA_CTL_DISABLE:
3729 ret = btrfs_quota_disable(trans, root->fs_info);
3730 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02003731 default:
3732 ret = -EINVAL;
3733 break;
3734 }
3735
Jan Schmidt2f232032013-04-25 16:04:51 +00003736 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
3737 if (err && !ret)
3738 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02003739out:
3740 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00003741 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00003742drop_write:
3743 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003744 return ret;
3745}
3746
Miao Xie905b0dd2012-11-26 08:50:11 +00003747static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003748{
Al Viro496ad9a2013-01-23 17:07:38 -05003749 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003750 struct btrfs_ioctl_qgroup_assign_args *sa;
3751 struct btrfs_trans_handle *trans;
3752 int ret;
3753 int err;
3754
3755 if (!capable(CAP_SYS_ADMIN))
3756 return -EPERM;
3757
Miao Xie905b0dd2012-11-26 08:50:11 +00003758 ret = mnt_want_write_file(file);
3759 if (ret)
3760 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003761
3762 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003763 if (IS_ERR(sa)) {
3764 ret = PTR_ERR(sa);
3765 goto drop_write;
3766 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003767
3768 trans = btrfs_join_transaction(root);
3769 if (IS_ERR(trans)) {
3770 ret = PTR_ERR(trans);
3771 goto out;
3772 }
3773
3774 /* FIXME: check if the IDs really exist */
3775 if (sa->assign) {
3776 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3777 sa->src, sa->dst);
3778 } else {
3779 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3780 sa->src, sa->dst);
3781 }
3782
3783 err = btrfs_end_transaction(trans, root);
3784 if (err && !ret)
3785 ret = err;
3786
3787out:
3788 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003789drop_write:
3790 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003791 return ret;
3792}
3793
Miao Xie905b0dd2012-11-26 08:50:11 +00003794static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003795{
Al Viro496ad9a2013-01-23 17:07:38 -05003796 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003797 struct btrfs_ioctl_qgroup_create_args *sa;
3798 struct btrfs_trans_handle *trans;
3799 int ret;
3800 int err;
3801
3802 if (!capable(CAP_SYS_ADMIN))
3803 return -EPERM;
3804
Miao Xie905b0dd2012-11-26 08:50:11 +00003805 ret = mnt_want_write_file(file);
3806 if (ret)
3807 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003808
3809 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003810 if (IS_ERR(sa)) {
3811 ret = PTR_ERR(sa);
3812 goto drop_write;
3813 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003814
Miao Xied86e56c2012-11-15 11:35:41 +00003815 if (!sa->qgroupid) {
3816 ret = -EINVAL;
3817 goto out;
3818 }
3819
Arne Jansen5d13a372011-09-14 15:53:51 +02003820 trans = btrfs_join_transaction(root);
3821 if (IS_ERR(trans)) {
3822 ret = PTR_ERR(trans);
3823 goto out;
3824 }
3825
3826 /* FIXME: check if the IDs really exist */
3827 if (sa->create) {
3828 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3829 NULL);
3830 } else {
3831 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3832 }
3833
3834 err = btrfs_end_transaction(trans, root);
3835 if (err && !ret)
3836 ret = err;
3837
3838out:
3839 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003840drop_write:
3841 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003842 return ret;
3843}
3844
Miao Xie905b0dd2012-11-26 08:50:11 +00003845static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003846{
Al Viro496ad9a2013-01-23 17:07:38 -05003847 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003848 struct btrfs_ioctl_qgroup_limit_args *sa;
3849 struct btrfs_trans_handle *trans;
3850 int ret;
3851 int err;
3852 u64 qgroupid;
3853
3854 if (!capable(CAP_SYS_ADMIN))
3855 return -EPERM;
3856
Miao Xie905b0dd2012-11-26 08:50:11 +00003857 ret = mnt_want_write_file(file);
3858 if (ret)
3859 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003860
3861 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003862 if (IS_ERR(sa)) {
3863 ret = PTR_ERR(sa);
3864 goto drop_write;
3865 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003866
3867 trans = btrfs_join_transaction(root);
3868 if (IS_ERR(trans)) {
3869 ret = PTR_ERR(trans);
3870 goto out;
3871 }
3872
3873 qgroupid = sa->qgroupid;
3874 if (!qgroupid) {
3875 /* take the current subvol as qgroup */
3876 qgroupid = root->root_key.objectid;
3877 }
3878
3879 /* FIXME: check if the IDs really exist */
3880 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3881
3882 err = btrfs_end_transaction(trans, root);
3883 if (err && !ret)
3884 ret = err;
3885
3886out:
3887 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003888drop_write:
3889 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003890 return ret;
3891}
3892
Jan Schmidt2f232032013-04-25 16:04:51 +00003893static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
3894{
Al Viro6d0379e2013-06-16 19:32:35 +04003895 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00003896 struct btrfs_ioctl_quota_rescan_args *qsa;
3897 int ret;
3898
3899 if (!capable(CAP_SYS_ADMIN))
3900 return -EPERM;
3901
3902 ret = mnt_want_write_file(file);
3903 if (ret)
3904 return ret;
3905
3906 qsa = memdup_user(arg, sizeof(*qsa));
3907 if (IS_ERR(qsa)) {
3908 ret = PTR_ERR(qsa);
3909 goto drop_write;
3910 }
3911
3912 if (qsa->flags) {
3913 ret = -EINVAL;
3914 goto out;
3915 }
3916
3917 ret = btrfs_qgroup_rescan(root->fs_info);
3918
3919out:
3920 kfree(qsa);
3921drop_write:
3922 mnt_drop_write_file(file);
3923 return ret;
3924}
3925
3926static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
3927{
Al Viro6d0379e2013-06-16 19:32:35 +04003928 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00003929 struct btrfs_ioctl_quota_rescan_args *qsa;
3930 int ret = 0;
3931
3932 if (!capable(CAP_SYS_ADMIN))
3933 return -EPERM;
3934
3935 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
3936 if (!qsa)
3937 return -ENOMEM;
3938
3939 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
3940 qsa->flags = 1;
3941 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
3942 }
3943
3944 if (copy_to_user(arg, qsa, sizeof(*qsa)))
3945 ret = -EFAULT;
3946
3947 kfree(qsa);
3948 return ret;
3949}
3950
Jan Schmidt57254b6e2013-05-06 19:14:17 +00003951static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
3952{
3953 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
3954
3955 if (!capable(CAP_SYS_ADMIN))
3956 return -EPERM;
3957
3958 return btrfs_qgroup_wait_for_completion(root->fs_info);
3959}
3960
Alexander Block8ea05e32012-07-25 17:35:53 +02003961static long btrfs_ioctl_set_received_subvol(struct file *file,
3962 void __user *arg)
3963{
3964 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05003965 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02003966 struct btrfs_root *root = BTRFS_I(inode)->root;
3967 struct btrfs_root_item *root_item = &root->root_item;
3968 struct btrfs_trans_handle *trans;
3969 struct timespec ct = CURRENT_TIME;
3970 int ret = 0;
3971
3972 ret = mnt_want_write_file(file);
3973 if (ret < 0)
3974 return ret;
3975
3976 down_write(&root->fs_info->subvol_sem);
3977
3978 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3979 ret = -EINVAL;
3980 goto out;
3981 }
3982
3983 if (btrfs_root_readonly(root)) {
3984 ret = -EROFS;
3985 goto out;
3986 }
3987
3988 if (!inode_owner_or_capable(inode)) {
3989 ret = -EACCES;
3990 goto out;
3991 }
3992
3993 sa = memdup_user(arg, sizeof(*sa));
3994 if (IS_ERR(sa)) {
3995 ret = PTR_ERR(sa);
3996 sa = NULL;
3997 goto out;
3998 }
3999
4000 trans = btrfs_start_transaction(root, 1);
4001 if (IS_ERR(trans)) {
4002 ret = PTR_ERR(trans);
4003 trans = NULL;
4004 goto out;
4005 }
4006
4007 sa->rtransid = trans->transid;
4008 sa->rtime.sec = ct.tv_sec;
4009 sa->rtime.nsec = ct.tv_nsec;
4010
4011 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4012 btrfs_set_root_stransid(root_item, sa->stransid);
4013 btrfs_set_root_rtransid(root_item, sa->rtransid);
4014 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
4015 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
4016 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
4017 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
4018
4019 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4020 &root->root_key, &root->root_item);
4021 if (ret < 0) {
4022 btrfs_end_transaction(trans, root);
4023 trans = NULL;
4024 goto out;
4025 } else {
4026 ret = btrfs_commit_transaction(trans, root);
4027 if (ret < 0)
4028 goto out;
4029 }
4030
4031 ret = copy_to_user(arg, sa, sizeof(*sa));
4032 if (ret)
4033 ret = -EFAULT;
4034
4035out:
4036 kfree(sa);
4037 up_write(&root->fs_info->subvol_sem);
4038 mnt_drop_write_file(file);
4039 return ret;
4040}
4041
jeff.liu867ab662013-01-05 02:48:01 +00004042static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4043{
Al Viro6d0379e2013-06-16 19:32:35 +04004044 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liu867ab662013-01-05 02:48:01 +00004045 const char *label = root->fs_info->super_copy->label;
4046 size_t len = strnlen(label, BTRFS_LABEL_SIZE);
4047 int ret;
4048
4049 if (len == BTRFS_LABEL_SIZE) {
4050 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4051 --len);
4052 }
4053
4054 mutex_lock(&root->fs_info->volume_mutex);
4055 ret = copy_to_user(arg, label, len);
4056 mutex_unlock(&root->fs_info->volume_mutex);
4057
4058 return ret ? -EFAULT : 0;
4059}
4060
jeff.liua8bfd4a2013-01-05 02:48:08 +00004061static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4062{
Al Viro6d0379e2013-06-16 19:32:35 +04004063 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004064 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4065 struct btrfs_trans_handle *trans;
4066 char label[BTRFS_LABEL_SIZE];
4067 int ret;
4068
4069 if (!capable(CAP_SYS_ADMIN))
4070 return -EPERM;
4071
4072 if (copy_from_user(label, arg, sizeof(label)))
4073 return -EFAULT;
4074
4075 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4076 pr_err("btrfs: unable to set label with more than %d bytes\n",
4077 BTRFS_LABEL_SIZE - 1);
4078 return -EINVAL;
4079 }
4080
4081 ret = mnt_want_write_file(file);
4082 if (ret)
4083 return ret;
4084
4085 mutex_lock(&root->fs_info->volume_mutex);
4086 trans = btrfs_start_transaction(root, 0);
4087 if (IS_ERR(trans)) {
4088 ret = PTR_ERR(trans);
4089 goto out_unlock;
4090 }
4091
4092 strcpy(super_block->label, label);
4093 ret = btrfs_end_transaction(trans, root);
4094
4095out_unlock:
4096 mutex_unlock(&root->fs_info->volume_mutex);
4097 mnt_drop_write_file(file);
4098 return ret;
4099}
4100
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004101long btrfs_ioctl(struct file *file, unsigned int
4102 cmd, unsigned long arg)
4103{
Al Viro496ad9a2013-01-23 17:07:38 -05004104 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004105 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004106
4107 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004108 case FS_IOC_GETFLAGS:
4109 return btrfs_ioctl_getflags(file, argp);
4110 case FS_IOC_SETFLAGS:
4111 return btrfs_ioctl_setflags(file, argp);
4112 case FS_IOC_GETVERSION:
4113 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004114 case FITRIM:
4115 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004116 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004117 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004118 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004119 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004120 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004121 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004122 case BTRFS_IOC_SUBVOL_CREATE_V2:
4123 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004124 case BTRFS_IOC_SNAP_DESTROY:
4125 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004126 case BTRFS_IOC_SUBVOL_GETFLAGS:
4127 return btrfs_ioctl_subvol_getflags(file, argp);
4128 case BTRFS_IOC_SUBVOL_SETFLAGS:
4129 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004130 case BTRFS_IOC_DEFAULT_SUBVOL:
4131 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004132 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004133 return btrfs_ioctl_defrag(file, NULL);
4134 case BTRFS_IOC_DEFRAG_RANGE:
4135 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004136 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004137 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004138 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004139 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004140 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004141 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004142 case BTRFS_IOC_FS_INFO:
4143 return btrfs_ioctl_fs_info(root, argp);
4144 case BTRFS_IOC_DEV_INFO:
4145 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004146 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004147 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004148 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004149 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4150 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004151 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004152 case BTRFS_IOC_TRANS_START:
4153 return btrfs_ioctl_trans_start(file);
4154 case BTRFS_IOC_TRANS_END:
4155 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004156 case BTRFS_IOC_TREE_SEARCH:
4157 return btrfs_ioctl_tree_search(file, argp);
4158 case BTRFS_IOC_INO_LOOKUP:
4159 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004160 case BTRFS_IOC_INO_PATHS:
4161 return btrfs_ioctl_ino_to_path(root, argp);
4162 case BTRFS_IOC_LOGICAL_INO:
4163 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004164 case BTRFS_IOC_SPACE_INFO:
4165 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004166 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004167 btrfs_sync_fs(file->f_dentry->d_sb, 1);
4168 return 0;
Sage Weil46204592010-10-29 15:41:32 -04004169 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004170 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004171 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004172 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004173 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004174 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004175 case BTRFS_IOC_SCRUB_CANCEL:
4176 return btrfs_ioctl_scrub_cancel(root, argp);
4177 case BTRFS_IOC_SCRUB_PROGRESS:
4178 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004179 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004180 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004181 case BTRFS_IOC_BALANCE_CTL:
4182 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004183 case BTRFS_IOC_BALANCE_PROGRESS:
4184 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004185 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4186 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004187 case BTRFS_IOC_SEND:
4188 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004189 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004190 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004191 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004192 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004193 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004194 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004195 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004196 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004197 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004198 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004199 case BTRFS_IOC_QUOTA_RESCAN:
4200 return btrfs_ioctl_quota_rescan(file, argp);
4201 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4202 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004203 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4204 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004205 case BTRFS_IOC_DEV_REPLACE:
4206 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004207 case BTRFS_IOC_GET_FSLABEL:
4208 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004209 case BTRFS_IOC_SET_FSLABEL:
4210 return btrfs_ioctl_set_fslabel(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004211 }
4212
4213 return -ENOTTY;
4214}