blob: daea831f3d362f07f2e024cc7c9a87b986c2bd0d [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{
155 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
156 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{
180 struct inode *inode = file->f_path.dentry->d_inode;
181 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{
313 struct inode *inode = file->f_path.dentry->d_inode;
314
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
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400366static noinline int create_subvol(struct btrfs_root *root,
367 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,
370 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;
Yan, Zheng76dda932009-09-21 16:00:26 -0400377 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400378 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000379 struct inode *dir;
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;
Alexander Block8ea05e32012-07-25 17:35:53 +0200386 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400387
Li Zefan581bb052011-04-20 10:06:11 +0800388 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400389 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400390 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000391
392 dir = parent->d_inode;
393
Josef Bacik9ed74f22009-09-11 16:12:44 -0400394 /*
395 * 1 - inode item
396 * 2 - refs
397 * 1 - root item
398 * 2 - dir items
399 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400400 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400401 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400402 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200404 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid,
405 inherit ? *inherit : NULL);
406 if (ret)
407 goto fail;
408
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400409 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200410 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400411 if (IS_ERR(leaf)) {
412 ret = PTR_ERR(leaf);
413 goto fail;
414 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400415
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400416 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400417 btrfs_set_header_bytenr(leaf, leaf->start);
418 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400419 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400420 btrfs_set_header_owner(leaf, objectid);
421
422 write_extent_buffer(leaf, root->fs_info->fsid,
423 (unsigned long)btrfs_header_fsid(leaf),
424 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400425 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
426 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
427 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400428 btrfs_mark_buffer_dirty(leaf);
429
Alexander Block8ea05e32012-07-25 17:35:53 +0200430 memset(&root_item, 0, sizeof(root_item));
431
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400432 inode_item = &root_item.inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433 inode_item->generation = cpu_to_le64(1);
434 inode_item->size = cpu_to_le64(3);
435 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400436 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
438
Li Zefan08fe4db2011-03-28 02:01:25 +0000439 root_item.flags = 0;
440 root_item.byte_limit = 0;
441 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
442
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400444 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445 btrfs_set_root_level(&root_item, 0);
446 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000447 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400448 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449
Alexander Block8ea05e32012-07-25 17:35:53 +0200450 btrfs_set_root_generation_v2(&root_item,
451 btrfs_root_generation(&root_item));
452 uuid_le_gen(&new_uuid);
453 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
454 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
Dan Carpenterdadd1102012-07-30 02:10:44 -0600455 root_item.otime.nsec = cpu_to_le32(cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200456 root_item.ctime = root_item.otime;
457 btrfs_set_root_ctransid(&root_item, trans->transid);
458 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459
Chris Mason925baed2008-06-25 16:01:30 -0400460 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461 free_extent_buffer(leaf);
462 leaf = NULL;
463
464 btrfs_set_root_dirid(&root_item, new_dirid);
465
466 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400467 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400468 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
469 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
470 &root_item);
471 if (ret)
472 goto fail;
473
Yan, Zheng76dda932009-09-21 16:00:26 -0400474 key.offset = (u64)-1;
475 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100476 if (IS_ERR(new_root)) {
477 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
478 ret = PTR_ERR(new_root);
479 goto fail;
480 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400481
482 btrfs_record_root_in_trans(trans, new_root);
483
Josef Bacikd82a6f12011-05-11 15:26:06 -0400484 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700485 if (ret) {
486 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100487 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700488 goto fail;
489 }
490
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491 /*
492 * insert the directory item
493 */
Chris Mason3de45862008-11-17 21:02:50 -0500494 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100495 if (ret) {
496 btrfs_abort_transaction(trans, root, ret);
497 goto fail;
498 }
Chris Mason3de45862008-11-17 21:02:50 -0500499
500 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800501 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500502 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100503 if (ret) {
504 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400505 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100506 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500507
Yan Zheng52c26172009-01-05 15:43:43 -0500508 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
509 ret = btrfs_update_inode(trans, root, dir);
510 BUG_ON(ret);
511
Chris Mason0660b5a2008-11-17 20:37:39 -0500512 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400513 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800514 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400515
Chris Mason0660b5a2008-11-17 20:37:39 -0500516 BUG_ON(ret);
517
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400518fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400519 if (async_transid) {
520 *async_transid = trans->transid;
521 err = btrfs_commit_transaction_async(trans, root, 1);
522 } else {
523 err = btrfs_commit_transaction(trans, root);
524 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525 if (err && !ret)
526 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500527
528 if (!ret)
529 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
530
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531 return ret;
532}
533
Sage Weil72fd0322010-10-29 15:41:32 -0400534static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800535 char *name, int namelen, u64 *async_transid,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200536 bool readonly, struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400537{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000538 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539 struct btrfs_pending_snapshot *pending_snapshot;
540 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000541 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400542
543 if (!root->ref_cows)
544 return -EINVAL;
545
Yan, Zhenga22285a2010-05-16 10:48:46 -0400546 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
547 if (!pending_snapshot)
548 return -ENOMEM;
549
Miao Xie66d8f3d2012-09-06 04:02:28 -0600550 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
551 BTRFS_BLOCK_RSV_TEMP);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400552 pending_snapshot->dentry = dentry;
553 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800554 pending_snapshot->readonly = readonly;
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200555 if (inherit) {
556 pending_snapshot->inherit = *inherit;
557 *inherit = NULL; /* take responsibility to free it */
558 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400559
Miao Xie48c03c42012-09-06 04:03:56 -0600560 trans = btrfs_start_transaction(root->fs_info->extent_root, 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400561 if (IS_ERR(trans)) {
562 ret = PTR_ERR(trans);
563 goto fail;
564 }
565
566 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
567 BUG_ON(ret);
568
Josef Bacik83515832011-06-14 15:16:14 -0400569 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400570 list_add(&pending_snapshot->list,
571 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400572 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400573 if (async_transid) {
574 *async_transid = trans->transid;
575 ret = btrfs_commit_transaction_async(trans,
576 root->fs_info->extent_root, 1);
577 } else {
578 ret = btrfs_commit_transaction(trans,
579 root->fs_info->extent_root);
580 }
Liu Bo109f2362012-11-05 12:42:09 +0000581 if (ret) {
582 /* cleanup_transaction has freed this for us */
583 if (trans->aborted)
584 pending_snapshot = NULL;
Josef Bacikc37b2b62012-10-22 15:51:44 -0400585 goto fail;
Liu Bo109f2362012-11-05 12:42:09 +0000586 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400587
588 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400589 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000590 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400591
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500592 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
593 if (ret)
594 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400595
Al Viro2fbe8c82011-07-16 21:38:06 -0400596 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000597 if (IS_ERR(inode)) {
598 ret = PTR_ERR(inode);
599 goto fail;
600 }
601 BUG_ON(!inode);
602 d_instantiate(dentry, inode);
603 ret = 0;
604fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400605 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400606 return ret;
607}
608
Sage Weil4260f7c2010-10-29 15:46:43 -0400609/* copy of check_sticky in fs/namei.c()
610* It's inline, so penalty for filesystems that don't use sticky bit is
611* minimal.
612*/
613static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
614{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800615 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400616
617 if (!(dir->i_mode & S_ISVTX))
618 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800619 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400620 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800621 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400622 return 0;
623 return !capable(CAP_FOWNER);
624}
625
626/* copy of may_delete in fs/namei.c()
627 * Check whether we can remove a link victim from directory dir, check
628 * whether the type of victim is right.
629 * 1. We can't do it if dir is read-only (done in permission())
630 * 2. We should have write and exec permissions on dir
631 * 3. We can't remove anything from append-only dir
632 * 4. We can't do anything with immutable dir (done in permission())
633 * 5. If the sticky bit on dir is set we should either
634 * a. be owner of dir, or
635 * b. be owner of victim, or
636 * c. have CAP_FOWNER capability
637 * 6. If the victim is append-only or immutable we can't do antyhing with
638 * links pointing to it.
639 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
640 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
641 * 9. We can't remove a root or mountpoint.
642 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
643 * nfs_async_unlink().
644 */
645
646static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
647{
648 int error;
649
650 if (!victim->d_inode)
651 return -ENOENT;
652
653 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400654 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400655
656 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
657 if (error)
658 return error;
659 if (IS_APPEND(dir))
660 return -EPERM;
661 if (btrfs_check_sticky(dir, victim->d_inode)||
662 IS_APPEND(victim->d_inode)||
663 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
664 return -EPERM;
665 if (isdir) {
666 if (!S_ISDIR(victim->d_inode->i_mode))
667 return -ENOTDIR;
668 if (IS_ROOT(victim))
669 return -EBUSY;
670 } else if (S_ISDIR(victim->d_inode->i_mode))
671 return -EISDIR;
672 if (IS_DEADDIR(dir))
673 return -ENOENT;
674 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
675 return -EBUSY;
676 return 0;
677}
678
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400679/* copy of may_create in fs/namei.c() */
680static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
681{
682 if (child->d_inode)
683 return -EEXIST;
684 if (IS_DEADDIR(dir))
685 return -ENOENT;
686 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
687}
688
689/*
690 * Create a new subvolume below @parent. This is largely modeled after
691 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
692 * inside this filesystem so it's quite a bit simpler.
693 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400694static noinline int btrfs_mksubvol(struct path *parent,
695 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400696 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200697 u64 *async_transid, bool readonly,
698 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400699{
Yan, Zheng76dda932009-09-21 16:00:26 -0400700 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400701 struct dentry *dentry;
702 int error;
703
Yan, Zheng76dda932009-09-21 16:00:26 -0400704 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400705
706 dentry = lookup_one_len(name, parent->dentry, namelen);
707 error = PTR_ERR(dentry);
708 if (IS_ERR(dentry))
709 goto out_unlock;
710
711 error = -EEXIST;
712 if (dentry->d_inode)
713 goto out_dput;
714
Yan, Zheng76dda932009-09-21 16:00:26 -0400715 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400716 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600717 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400718
Chris Mason9c520572012-12-17 14:26:57 -0500719 /*
720 * even if this name doesn't exist, we may get hash collisions.
721 * check for them now when we can safely fail
722 */
723 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
724 dir->i_ino, name,
725 namelen);
726 if (error)
727 goto out_dput;
728
Yan, Zheng76dda932009-09-21 16:00:26 -0400729 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
730
731 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
732 goto out_up_read;
733
Chris Mason3de45862008-11-17 21:02:50 -0500734 if (snap_src) {
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200735 error = create_snapshot(snap_src, dentry, name, namelen,
736 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500737 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400738 error = create_subvol(BTRFS_I(dir)->root, dentry,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200739 name, namelen, async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500740 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400741 if (!error)
742 fsnotify_mkdir(dir, dentry);
743out_up_read:
744 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400745out_dput:
746 dput(dentry);
747out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400748 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400749 return error;
750}
751
Chris Mason4cb53002011-05-24 15:35:30 -0400752/*
753 * When we're defragging a range, we don't want to kick it off again
754 * if it is really just waiting for delalloc to send it down.
755 * If we find a nice big extent or delalloc range for the bytes in the
756 * file you want to defrag, we return 0 to let you know to skip this
757 * part of the file
758 */
759static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
760{
761 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
762 struct extent_map *em = NULL;
763 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
764 u64 end;
765
766 read_lock(&em_tree->lock);
767 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
768 read_unlock(&em_tree->lock);
769
770 if (em) {
771 end = extent_map_end(em);
772 free_extent_map(em);
773 if (end - offset > thresh)
774 return 0;
775 }
776 /* if we already have a nice delalloc here, just stop */
777 thresh /= 2;
778 end = count_range_bits(io_tree, &offset, offset + thresh,
779 thresh, EXTENT_DELALLOC, 1);
780 if (end >= thresh)
781 return 0;
782 return 1;
783}
784
785/*
786 * helper function to walk through a file and find extents
787 * newer than a specific transid, and smaller than thresh.
788 *
789 * This is used by the defragging code to find new and small
790 * extents
791 */
792static int find_new_extents(struct btrfs_root *root,
793 struct inode *inode, u64 newer_than,
794 u64 *off, int thresh)
795{
796 struct btrfs_path *path;
797 struct btrfs_key min_key;
798 struct btrfs_key max_key;
799 struct extent_buffer *leaf;
800 struct btrfs_file_extent_item *extent;
801 int type;
802 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000803 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400804
805 path = btrfs_alloc_path();
806 if (!path)
807 return -ENOMEM;
808
David Sterbaa4689d22011-05-31 17:08:14 +0000809 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400810 min_key.type = BTRFS_EXTENT_DATA_KEY;
811 min_key.offset = *off;
812
David Sterbaa4689d22011-05-31 17:08:14 +0000813 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400814 max_key.type = (u8)-1;
815 max_key.offset = (u64)-1;
816
817 path->keep_locks = 1;
818
819 while(1) {
820 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +0000821 path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400822 if (ret != 0)
823 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000824 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400825 goto none;
826 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
827 goto none;
828
829 leaf = path->nodes[0];
830 extent = btrfs_item_ptr(leaf, path->slots[0],
831 struct btrfs_file_extent_item);
832
833 type = btrfs_file_extent_type(leaf, extent);
834 if (type == BTRFS_FILE_EXTENT_REG &&
835 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
836 check_defrag_in_cache(inode, min_key.offset, thresh)) {
837 *off = min_key.offset;
838 btrfs_free_path(path);
839 return 0;
840 }
841
842 if (min_key.offset == (u64)-1)
843 goto none;
844
845 min_key.offset++;
846 btrfs_release_path(path);
847 }
848none:
849 btrfs_free_path(path);
850 return -ENOENT;
851}
852
Li Zefan6c282eb2012-06-11 16:03:35 +0800853static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400854{
855 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800857 struct extent_map *em;
858 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500859
860 /*
861 * hopefully we have this extent in the tree already, try without
862 * the full extent lock
863 */
864 read_lock(&em_tree->lock);
865 em = lookup_extent_mapping(em_tree, start, len);
866 read_unlock(&em_tree->lock);
867
868 if (!em) {
869 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100870 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500871 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100872 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500873
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000874 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800875 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500876 }
877
Li Zefan6c282eb2012-06-11 16:03:35 +0800878 return em;
879}
880
881static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
882{
883 struct extent_map *next;
884 bool ret = true;
885
886 /* this is the last extent */
887 if (em->start + em->len >= i_size_read(inode))
888 return false;
889
890 next = defrag_lookup_extent(inode, em->start + em->len);
891 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
892 ret = false;
893
894 free_extent_map(next);
895 return ret;
896}
897
898static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400899 u64 *last_len, u64 *skip, u64 *defrag_end,
900 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800901{
902 struct extent_map *em;
903 int ret = 1;
904 bool next_mergeable = true;
905
906 /*
907 * make sure that once we start defragging an extent, we keep on
908 * defragging it
909 */
910 if (start < *defrag_end)
911 return 1;
912
913 *skip = 0;
914
915 em = defrag_lookup_extent(inode, start);
916 if (!em)
917 return 0;
918
Chris Mason940100a2010-03-10 10:52:59 -0500919 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400920 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500921 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400922 goto out;
923 }
924
Li Zefan6c282eb2012-06-11 16:03:35 +0800925 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500926
927 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800928 * we hit a real extent, if it is big or the next extent is not a
929 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500930 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400931 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800932 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500933 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400934out:
Chris Mason940100a2010-03-10 10:52:59 -0500935 /*
936 * last_len ends up being a counter of how many bytes we've defragged.
937 * every time we choose not to defrag an extent, we reset *last_len
938 * so that the next tiny extent will force a defrag.
939 *
940 * The end result of this is that tiny extents before a single big
941 * extent will force at least part of that big extent to be defragged.
942 */
943 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500944 *defrag_end = extent_map_end(em);
945 } else {
946 *last_len = 0;
947 *skip = extent_map_end(em);
948 *defrag_end = 0;
949 }
950
951 free_extent_map(em);
952 return ret;
953}
954
Chris Mason4cb53002011-05-24 15:35:30 -0400955/*
956 * it doesn't do much good to defrag one or two pages
957 * at a time. This pulls in a nice chunk of pages
958 * to COW and defrag.
959 *
960 * It also makes sure the delalloc code has enough
961 * dirty data to avoid making new small extents as part
962 * of the defrag
963 *
964 * It's a good idea to start RA on this range
965 * before calling this.
966 */
967static int cluster_pages_for_defrag(struct inode *inode,
968 struct page **pages,
969 unsigned long start_index,
970 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400971{
Chris Mason4cb53002011-05-24 15:35:30 -0400972 unsigned long file_end;
973 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400974 u64 page_start;
975 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400976 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400977 int ret;
978 int i;
979 int i_done;
980 struct btrfs_ordered_extent *ordered;
981 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800982 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400983 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
Chris Mason4cb53002011-05-24 15:35:30 -0400985 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400986 if (!isize || start_index > file_end)
987 return 0;
988
989 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400990
991 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400992 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400993 if (ret)
994 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400995 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800996 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400997
998 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400999 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001000 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001001again:
Josef Bacika94733d2011-07-11 10:47:06 -04001002 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001003 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001004 if (!page)
1005 break;
1006
Miao Xie600a45e2012-02-16 15:01:24 +08001007 page_start = page_offset(page);
1008 page_end = page_start + PAGE_CACHE_SIZE - 1;
1009 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001010 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001011 ordered = btrfs_lookup_ordered_extent(inode,
1012 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001013 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001014 if (!ordered)
1015 break;
1016
1017 unlock_page(page);
1018 btrfs_start_ordered_extent(inode, ordered, 1);
1019 btrfs_put_ordered_extent(ordered);
1020 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001021 /*
1022 * we unlocked the page above, so we need check if
1023 * it was released or not.
1024 */
1025 if (page->mapping != inode->i_mapping) {
1026 unlock_page(page);
1027 page_cache_release(page);
1028 goto again;
1029 }
Miao Xie600a45e2012-02-16 15:01:24 +08001030 }
1031
Chris Mason4cb53002011-05-24 15:35:30 -04001032 if (!PageUptodate(page)) {
1033 btrfs_readpage(NULL, page);
1034 lock_page(page);
1035 if (!PageUptodate(page)) {
1036 unlock_page(page);
1037 page_cache_release(page);
1038 ret = -EIO;
1039 break;
1040 }
1041 }
Miao Xie600a45e2012-02-16 15:01:24 +08001042
Miao Xie600a45e2012-02-16 15:01:24 +08001043 if (page->mapping != inode->i_mapping) {
1044 unlock_page(page);
1045 page_cache_release(page);
1046 goto again;
1047 }
1048
Chris Mason4cb53002011-05-24 15:35:30 -04001049 pages[i] = page;
1050 i_done++;
1051 }
1052 if (!i_done || ret)
1053 goto out;
1054
1055 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1056 goto out;
1057
1058 /*
1059 * so now we have a nice long stream of locked
1060 * and up to date pages, lets wait on them
1061 */
1062 for (i = 0; i < i_done; i++)
1063 wait_on_page_writeback(pages[i]);
1064
1065 page_start = page_offset(pages[0]);
1066 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1067
1068 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001069 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001070 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1071 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001072 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1073 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001074
Liu Bo1f12bd02012-03-29 09:57:44 -04001075 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001076 spin_lock(&BTRFS_I(inode)->lock);
1077 BTRFS_I(inode)->outstanding_extents++;
1078 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001079 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001080 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001081 }
1082
1083
Liu Bo9e8a4a82012-09-05 19:10:51 -06001084 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1085 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001086
1087 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1088 page_start, page_end - 1, &cached_state,
1089 GFP_NOFS);
1090
1091 for (i = 0; i < i_done; i++) {
1092 clear_page_dirty_for_io(pages[i]);
1093 ClearPageChecked(pages[i]);
1094 set_page_extent_mapped(pages[i]);
1095 set_page_dirty(pages[i]);
1096 unlock_page(pages[i]);
1097 page_cache_release(pages[i]);
1098 }
1099 return i_done;
1100out:
1101 for (i = 0; i < i_done; i++) {
1102 unlock_page(pages[i]);
1103 page_cache_release(pages[i]);
1104 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001105 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001106 return ret;
1107
1108}
1109
1110int btrfs_defrag_file(struct inode *inode, struct file *file,
1111 struct btrfs_ioctl_defrag_range_args *range,
1112 u64 newer_than, unsigned long max_to_defrag)
1113{
1114 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001115 struct file_ra_state *ra = NULL;
1116 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001117 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001118 u64 last_len = 0;
1119 u64 skip = 0;
1120 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001121 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001122 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001123 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001124 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001125 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001126 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001127 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001128 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1129 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001130 u64 new_align = ~((u64)128 * 1024 - 1);
1131 struct page **pages = NULL;
1132
1133 if (extent_thresh == 0)
1134 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001135
1136 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1137 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1138 return -EINVAL;
1139 if (range->compress_type)
1140 compress_type = range->compress_type;
1141 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001142
Li Zefan151a31b2011-09-02 15:56:39 +08001143 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001144 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001145
Chris Mason4cb53002011-05-24 15:35:30 -04001146 /*
1147 * if we were not given a file, allocate a readahead
1148 * context
1149 */
1150 if (!file) {
1151 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1152 if (!ra)
1153 return -ENOMEM;
1154 file_ra_state_init(ra, inode->i_mapping);
1155 } else {
1156 ra = &file->f_ra;
1157 }
1158
Li Zefan008873e2011-09-02 15:57:07 +08001159 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001160 GFP_NOFS);
1161 if (!pages) {
1162 ret = -ENOMEM;
1163 goto out_ra;
1164 }
1165
1166 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001167 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001168 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001169 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1170 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001171 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001172 }
1173
Chris Mason4cb53002011-05-24 15:35:30 -04001174 if (newer_than) {
1175 ret = find_new_extents(root, inode, newer_than,
1176 &newer_off, 64 * 1024);
1177 if (!ret) {
1178 range->start = newer_off;
1179 /*
1180 * we always align our defrag to help keep
1181 * the extents in the file evenly spaced
1182 */
1183 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001184 } else
1185 goto out_ra;
1186 } else {
1187 i = range->start >> PAGE_CACHE_SHIFT;
1188 }
1189 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001190 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001191
Li Zefan2a0f7f52011-10-10 15:43:34 -04001192 /*
1193 * make writeback starts from i, so the defrag range can be
1194 * written sequentially.
1195 */
1196 if (i < inode->i_mapping->writeback_index)
1197 inode->i_mapping->writeback_index = i;
1198
Chris Masonf7f43cc2011-10-11 11:41:40 -04001199 while (i <= last_index && defrag_count < max_to_defrag &&
1200 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1201 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001202 /*
1203 * make sure we stop running if someone unmounts
1204 * the FS
1205 */
1206 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1207 break;
1208
Liu Bo66c26892012-03-29 09:57:45 -04001209 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001210 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001211 &defrag_end, range->flags &
1212 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001213 unsigned long next;
1214 /*
1215 * the should_defrag function tells us how much to skip
1216 * bump our counter by the suggested amount
1217 */
1218 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1219 i = max(i + 1, next);
1220 continue;
1221 }
Li Zefan008873e2011-09-02 15:57:07 +08001222
1223 if (!newer_than) {
1224 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1225 PAGE_CACHE_SHIFT) - i;
1226 cluster = min(cluster, max_cluster);
1227 } else {
1228 cluster = max_cluster;
1229 }
1230
Chris Mason1e701a32010-03-11 09:42:04 -05001231 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001232 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001233
Li Zefan008873e2011-09-02 15:57:07 +08001234 if (i + cluster > ra_index) {
1235 ra_index = max(i, ra_index);
1236 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1237 cluster);
1238 ra_index += max_cluster;
1239 }
Chris Mason940100a2010-03-10 10:52:59 -05001240
Liu Boecb8bea2012-03-29 09:57:44 -04001241 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001242 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001243 if (ret < 0) {
1244 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001245 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001246 }
Chris Mason940100a2010-03-10 10:52:59 -05001247
Chris Mason4cb53002011-05-24 15:35:30 -04001248 defrag_count += ret;
1249 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001250 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001251
1252 if (newer_than) {
1253 if (newer_off == (u64)-1)
1254 break;
1255
Liu Boe1f041e2012-03-29 09:57:45 -04001256 if (ret > 0)
1257 i += ret;
1258
Chris Mason4cb53002011-05-24 15:35:30 -04001259 newer_off = max(newer_off + 1,
1260 (u64)i << PAGE_CACHE_SHIFT);
1261
1262 ret = find_new_extents(root, inode,
1263 newer_than, &newer_off,
1264 64 * 1024);
1265 if (!ret) {
1266 range->start = newer_off;
1267 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001268 } else {
1269 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001270 }
Chris Mason4cb53002011-05-24 15:35:30 -04001271 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001272 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001273 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001274 last_len += ret << PAGE_CACHE_SHIFT;
1275 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001276 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001277 last_len = 0;
1278 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001279 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001280 }
1281
Chris Mason1e701a32010-03-11 09:42:04 -05001282 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1283 filemap_flush(inode->i_mapping);
1284
1285 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1286 /* the filemap_flush will queue IO into the worker threads, but
1287 * we have to make sure the IO is actually started and that
1288 * ordered extents get created before we return
1289 */
1290 atomic_inc(&root->fs_info->async_submit_draining);
1291 while (atomic_read(&root->fs_info->nr_async_submits) ||
1292 atomic_read(&root->fs_info->async_delalloc_pages)) {
1293 wait_event(root->fs_info->async_submit_wait,
1294 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1295 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1296 }
1297 atomic_dec(&root->fs_info->async_submit_draining);
1298
1299 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001300 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001301 mutex_unlock(&inode->i_mutex);
1302 }
1303
Li Zefan1a419d82010-10-25 15:12:50 +08001304 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001305 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001306 }
1307
Diego Calleja60ccf822011-09-01 16:33:57 +02001308 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001309
Chris Mason4cb53002011-05-24 15:35:30 -04001310out_ra:
1311 if (!file)
1312 kfree(ra);
1313 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001314 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001315}
1316
Miao Xie198605a2012-11-26 08:43:45 +00001317static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001318 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001319{
1320 u64 new_size;
1321 u64 old_size;
1322 u64 devid = 1;
Miao Xie198605a2012-11-26 08:43:45 +00001323 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001324 struct btrfs_ioctl_vol_args *vol_args;
1325 struct btrfs_trans_handle *trans;
1326 struct btrfs_device *device = NULL;
1327 char *sizestr;
1328 char *devstr = NULL;
1329 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330 int mod = 0;
1331
Chris Masone441d542009-01-05 16:57:23 -05001332 if (!capable(CAP_SYS_ADMIN))
1333 return -EPERM;
1334
Miao Xie198605a2012-11-26 08:43:45 +00001335 ret = mnt_want_write_file(file);
1336 if (ret)
1337 return ret;
1338
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001339 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1340 1)) {
1341 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Miao Xie97547672012-12-21 10:38:50 +00001342 mnt_drop_write_file(file);
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02001343 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001344 }
1345
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001346 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001347 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001348 if (IS_ERR(vol_args)) {
1349 ret = PTR_ERR(vol_args);
1350 goto out;
1351 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001352
1353 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001354
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001355 sizestr = vol_args->name;
1356 devstr = strchr(sizestr, ':');
1357 if (devstr) {
1358 char *end;
1359 sizestr = devstr + 1;
1360 *devstr = '\0';
1361 devstr = vol_args->name;
1362 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001363 if (!devid) {
1364 ret = -EINVAL;
1365 goto out_free;
1366 }
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001367 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001368 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001369 }
Miao Xiedba60f32012-12-21 09:19:51 +00001370
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001371 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001373 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001374 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001375 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001376 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001377 }
Miao Xiedba60f32012-12-21 09:19:51 +00001378
1379 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001380 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001381 "readonly device %llu\n",
Chris Masona8c4a332012-06-15 20:07:17 -04001382 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001383 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001384 goto out_free;
1385 }
1386
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001387 if (!strcmp(sizestr, "max"))
1388 new_size = device->bdev->bd_inode->i_size;
1389 else {
1390 if (sizestr[0] == '-') {
1391 mod = -1;
1392 sizestr++;
1393 } else if (sizestr[0] == '+') {
1394 mod = 1;
1395 sizestr++;
1396 }
Akinobu Mita91748462010-02-28 10:59:11 +00001397 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001398 if (new_size == 0) {
1399 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001400 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401 }
1402 }
1403
Stefan Behrens63a212a2012-11-05 18:29:28 +01001404 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001405 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001406 goto out_free;
1407 }
1408
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409 old_size = device->total_bytes;
1410
1411 if (mod < 0) {
1412 if (new_size > old_size) {
1413 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001414 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001415 }
1416 new_size = old_size - new_size;
1417 } else if (mod > 0) {
1418 new_size = old_size + new_size;
1419 }
1420
1421 if (new_size < 256 * 1024 * 1024) {
1422 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001423 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001424 }
1425 if (new_size > device->bdev->bd_inode->i_size) {
1426 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001427 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428 }
1429
1430 do_div(new_size, root->sectorsize);
1431 new_size *= root->sectorsize;
1432
Josef Bacik606686e2012-06-04 14:03:51 -04001433 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1434 rcu_str_deref(device->name),
1435 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436
1437 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001438 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001439 if (IS_ERR(trans)) {
1440 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001441 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001442 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001443 ret = btrfs_grow_device(trans, device, new_size);
1444 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001445 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001447 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001448
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001449out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001450 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001451out:
1452 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001453 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001454 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455 return ret;
1456}
1457
Sage Weil72fd0322010-10-29 15:41:32 -04001458static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001459 char *name, unsigned long fd, int subvol,
1460 u64 *transid, bool readonly,
1461 struct btrfs_qgroup_inherit **inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001462{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001464 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001465
Liu Boa874a632012-06-29 03:58:46 -06001466 ret = mnt_want_write_file(file);
1467 if (ret)
1468 goto out;
1469
Sage Weil72fd0322010-10-29 15:41:32 -04001470 namelen = strlen(name);
1471 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001472 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001473 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001474 }
1475
Chris Mason16780ca2012-02-20 22:14:55 -05001476 if (name[0] == '.' &&
1477 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1478 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001479 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001480 }
1481
Chris Mason3de45862008-11-17 21:02:50 -05001482 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001483 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001484 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001485 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001486 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001487 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001488 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001489 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001490 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001491 }
1492
Al Viro2903ff02012-08-28 12:52:22 -04001493 src_inode = src.file->f_path.dentry->d_inode;
Chris Mason3de45862008-11-17 21:02:50 -05001494 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001495 printk(KERN_INFO "btrfs: Snapshot src from "
1496 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001497 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001498 } else {
1499 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1500 BTRFS_I(src_inode)->root,
1501 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001502 }
Al Viro2903ff02012-08-28 12:52:22 -04001503 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001504 }
Liu Boa874a632012-06-29 03:58:46 -06001505out_drop_write:
1506 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507out:
Sage Weil72fd0322010-10-29 15:41:32 -04001508 return ret;
1509}
1510
1511static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001512 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001513{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001514 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001515 int ret;
1516
Li Zefanfa0d2b92010-12-20 15:53:28 +08001517 vol_args = memdup_user(arg, sizeof(*vol_args));
1518 if (IS_ERR(vol_args))
1519 return PTR_ERR(vol_args);
1520 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001521
Li Zefanfa0d2b92010-12-20 15:53:28 +08001522 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001523 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001524 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001525
Li Zefanfa0d2b92010-12-20 15:53:28 +08001526 kfree(vol_args);
1527 return ret;
1528}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001529
Li Zefanfa0d2b92010-12-20 15:53:28 +08001530static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1531 void __user *arg, int subvol)
1532{
1533 struct btrfs_ioctl_vol_args_v2 *vol_args;
1534 int ret;
1535 u64 transid = 0;
1536 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001537 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001538 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001539
Li Zefanfa0d2b92010-12-20 15:53:28 +08001540 vol_args = memdup_user(arg, sizeof(*vol_args));
1541 if (IS_ERR(vol_args))
1542 return PTR_ERR(vol_args);
1543 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001544
Li Zefanb83cc962010-12-20 16:04:08 +08001545 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001546 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1547 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001548 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001549 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001550 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001551
1552 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1553 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001554 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1555 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001556 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1557 if (vol_args->size > PAGE_CACHE_SIZE) {
1558 ret = -EINVAL;
1559 goto out;
1560 }
1561 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1562 if (IS_ERR(inherit)) {
1563 ret = PTR_ERR(inherit);
1564 goto out;
1565 }
1566 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001567
1568 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001569 vol_args->fd, subvol, ptr,
1570 readonly, &inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001571
1572 if (ret == 0 && ptr &&
1573 copy_to_user(arg +
1574 offsetof(struct btrfs_ioctl_vol_args_v2,
1575 transid), ptr, sizeof(*ptr)))
1576 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001577out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001578 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001579 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001580 return ret;
1581}
1582
Li Zefan0caa1022010-12-20 16:30:25 +08001583static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1584 void __user *arg)
1585{
1586 struct inode *inode = fdentry(file)->d_inode;
1587 struct btrfs_root *root = BTRFS_I(inode)->root;
1588 int ret = 0;
1589 u64 flags = 0;
1590
Li Zefan33345d012011-04-20 10:31:50 +08001591 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001592 return -EINVAL;
1593
1594 down_read(&root->fs_info->subvol_sem);
1595 if (btrfs_root_readonly(root))
1596 flags |= BTRFS_SUBVOL_RDONLY;
1597 up_read(&root->fs_info->subvol_sem);
1598
1599 if (copy_to_user(arg, &flags, sizeof(flags)))
1600 ret = -EFAULT;
1601
1602 return ret;
1603}
1604
1605static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1606 void __user *arg)
1607{
1608 struct inode *inode = fdentry(file)->d_inode;
1609 struct btrfs_root *root = BTRFS_I(inode)->root;
1610 struct btrfs_trans_handle *trans;
1611 u64 root_flags;
1612 u64 flags;
1613 int ret = 0;
1614
Liu Bob9ca0662012-06-29 03:58:49 -06001615 ret = mnt_want_write_file(file);
1616 if (ret)
1617 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001618
Liu Bob9ca0662012-06-29 03:58:49 -06001619 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1620 ret = -EINVAL;
1621 goto out_drop_write;
1622 }
Li Zefan0caa1022010-12-20 16:30:25 +08001623
Liu Bob9ca0662012-06-29 03:58:49 -06001624 if (copy_from_user(&flags, arg, sizeof(flags))) {
1625 ret = -EFAULT;
1626 goto out_drop_write;
1627 }
Li Zefan0caa1022010-12-20 16:30:25 +08001628
Liu Bob9ca0662012-06-29 03:58:49 -06001629 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1630 ret = -EINVAL;
1631 goto out_drop_write;
1632 }
Li Zefan0caa1022010-12-20 16:30:25 +08001633
Liu Bob9ca0662012-06-29 03:58:49 -06001634 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1635 ret = -EOPNOTSUPP;
1636 goto out_drop_write;
1637 }
Li Zefan0caa1022010-12-20 16:30:25 +08001638
Liu Bob9ca0662012-06-29 03:58:49 -06001639 if (!inode_owner_or_capable(inode)) {
1640 ret = -EACCES;
1641 goto out_drop_write;
1642 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001643
Li Zefan0caa1022010-12-20 16:30:25 +08001644 down_write(&root->fs_info->subvol_sem);
1645
1646 /* nothing to do */
1647 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001648 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001649
1650 root_flags = btrfs_root_flags(&root->root_item);
1651 if (flags & BTRFS_SUBVOL_RDONLY)
1652 btrfs_set_root_flags(&root->root_item,
1653 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1654 else
1655 btrfs_set_root_flags(&root->root_item,
1656 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1657
1658 trans = btrfs_start_transaction(root, 1);
1659 if (IS_ERR(trans)) {
1660 ret = PTR_ERR(trans);
1661 goto out_reset;
1662 }
1663
Li Zefanb4dc2b82011-02-16 06:06:34 +00001664 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001665 &root->root_key, &root->root_item);
1666
1667 btrfs_commit_transaction(trans, root);
1668out_reset:
1669 if (ret)
1670 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001671out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001672 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001673out_drop_write:
1674 mnt_drop_write_file(file);
1675out:
Li Zefan0caa1022010-12-20 16:30:25 +08001676 return ret;
1677}
1678
Yan, Zheng76dda932009-09-21 16:00:26 -04001679/*
1680 * helper to check if the subvolume references other subvolumes
1681 */
1682static noinline int may_destroy_subvol(struct btrfs_root *root)
1683{
1684 struct btrfs_path *path;
1685 struct btrfs_key key;
1686 int ret;
1687
1688 path = btrfs_alloc_path();
1689 if (!path)
1690 return -ENOMEM;
1691
1692 key.objectid = root->root_key.objectid;
1693 key.type = BTRFS_ROOT_REF_KEY;
1694 key.offset = (u64)-1;
1695
1696 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1697 &key, path, 0, 0);
1698 if (ret < 0)
1699 goto out;
1700 BUG_ON(ret == 0);
1701
1702 ret = 0;
1703 if (path->slots[0] > 0) {
1704 path->slots[0]--;
1705 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1706 if (key.objectid == root->root_key.objectid &&
1707 key.type == BTRFS_ROOT_REF_KEY)
1708 ret = -ENOTEMPTY;
1709 }
1710out:
1711 btrfs_free_path(path);
1712 return ret;
1713}
1714
Chris Masonac8e9812010-02-28 15:39:26 -05001715static noinline int key_in_sk(struct btrfs_key *key,
1716 struct btrfs_ioctl_search_key *sk)
1717{
Chris Masonabc6e132010-03-18 12:10:08 -04001718 struct btrfs_key test;
1719 int ret;
1720
1721 test.objectid = sk->min_objectid;
1722 test.type = sk->min_type;
1723 test.offset = sk->min_offset;
1724
1725 ret = btrfs_comp_cpu_keys(key, &test);
1726 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001727 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001728
1729 test.objectid = sk->max_objectid;
1730 test.type = sk->max_type;
1731 test.offset = sk->max_offset;
1732
1733 ret = btrfs_comp_cpu_keys(key, &test);
1734 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001735 return 0;
1736 return 1;
1737}
1738
1739static noinline int copy_to_sk(struct btrfs_root *root,
1740 struct btrfs_path *path,
1741 struct btrfs_key *key,
1742 struct btrfs_ioctl_search_key *sk,
1743 char *buf,
1744 unsigned long *sk_offset,
1745 int *num_found)
1746{
1747 u64 found_transid;
1748 struct extent_buffer *leaf;
1749 struct btrfs_ioctl_search_header sh;
1750 unsigned long item_off;
1751 unsigned long item_len;
1752 int nritems;
1753 int i;
1754 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001755 int ret = 0;
1756
1757 leaf = path->nodes[0];
1758 slot = path->slots[0];
1759 nritems = btrfs_header_nritems(leaf);
1760
1761 if (btrfs_header_generation(leaf) > sk->max_transid) {
1762 i = nritems;
1763 goto advance_key;
1764 }
1765 found_transid = btrfs_header_generation(leaf);
1766
1767 for (i = slot; i < nritems; i++) {
1768 item_off = btrfs_item_ptr_offset(leaf, i);
1769 item_len = btrfs_item_size_nr(leaf, i);
1770
1771 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1772 item_len = 0;
1773
1774 if (sizeof(sh) + item_len + *sk_offset >
1775 BTRFS_SEARCH_ARGS_BUFSIZE) {
1776 ret = 1;
1777 goto overflow;
1778 }
1779
1780 btrfs_item_key_to_cpu(leaf, key, i);
1781 if (!key_in_sk(key, sk))
1782 continue;
1783
1784 sh.objectid = key->objectid;
1785 sh.offset = key->offset;
1786 sh.type = key->type;
1787 sh.len = item_len;
1788 sh.transid = found_transid;
1789
1790 /* copy search result header */
1791 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1792 *sk_offset += sizeof(sh);
1793
1794 if (item_len) {
1795 char *p = buf + *sk_offset;
1796 /* copy the item */
1797 read_extent_buffer(leaf, p,
1798 item_off, item_len);
1799 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001800 }
Hugo Millse2156862011-05-14 17:43:41 +00001801 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001802
1803 if (*num_found >= sk->nr_items)
1804 break;
1805 }
1806advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001807 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001808 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1809 key->offset++;
1810 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1811 key->offset = 0;
1812 key->type++;
1813 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1814 key->offset = 0;
1815 key->type = 0;
1816 key->objectid++;
1817 } else
1818 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001819overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001820 return ret;
1821}
1822
1823static noinline int search_ioctl(struct inode *inode,
1824 struct btrfs_ioctl_search_args *args)
1825{
1826 struct btrfs_root *root;
1827 struct btrfs_key key;
1828 struct btrfs_key max_key;
1829 struct btrfs_path *path;
1830 struct btrfs_ioctl_search_key *sk = &args->key;
1831 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1832 int ret;
1833 int num_found = 0;
1834 unsigned long sk_offset = 0;
1835
1836 path = btrfs_alloc_path();
1837 if (!path)
1838 return -ENOMEM;
1839
1840 if (sk->tree_id == 0) {
1841 /* search the root of the inode that was passed */
1842 root = BTRFS_I(inode)->root;
1843 } else {
1844 key.objectid = sk->tree_id;
1845 key.type = BTRFS_ROOT_ITEM_KEY;
1846 key.offset = (u64)-1;
1847 root = btrfs_read_fs_root_no_name(info, &key);
1848 if (IS_ERR(root)) {
1849 printk(KERN_ERR "could not find root %llu\n",
1850 sk->tree_id);
1851 btrfs_free_path(path);
1852 return -ENOENT;
1853 }
1854 }
1855
1856 key.objectid = sk->min_objectid;
1857 key.type = sk->min_type;
1858 key.offset = sk->min_offset;
1859
1860 max_key.objectid = sk->max_objectid;
1861 max_key.type = sk->max_type;
1862 max_key.offset = sk->max_offset;
1863
1864 path->keep_locks = 1;
1865
1866 while(1) {
Eric Sandeende78b512013-01-31 18:21:12 +00001867 ret = btrfs_search_forward(root, &key, &max_key, path,
Chris Masonac8e9812010-02-28 15:39:26 -05001868 sk->min_transid);
1869 if (ret != 0) {
1870 if (ret > 0)
1871 ret = 0;
1872 goto err;
1873 }
1874 ret = copy_to_sk(root, path, &key, sk, args->buf,
1875 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001876 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001877 if (ret || num_found >= sk->nr_items)
1878 break;
1879
1880 }
1881 ret = 0;
1882err:
1883 sk->nr_items = num_found;
1884 btrfs_free_path(path);
1885 return ret;
1886}
1887
1888static noinline int btrfs_ioctl_tree_search(struct file *file,
1889 void __user *argp)
1890{
1891 struct btrfs_ioctl_search_args *args;
1892 struct inode *inode;
1893 int ret;
1894
1895 if (!capable(CAP_SYS_ADMIN))
1896 return -EPERM;
1897
Julia Lawall2354d082010-10-29 15:14:18 -04001898 args = memdup_user(argp, sizeof(*args));
1899 if (IS_ERR(args))
1900 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001901
Chris Masonac8e9812010-02-28 15:39:26 -05001902 inode = fdentry(file)->d_inode;
1903 ret = search_ioctl(inode, args);
1904 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1905 ret = -EFAULT;
1906 kfree(args);
1907 return ret;
1908}
1909
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001910/*
Chris Masonac8e9812010-02-28 15:39:26 -05001911 * Search INODE_REFs to identify path name of 'dirid' directory
1912 * in a 'tree_id' tree. and sets path name to 'name'.
1913 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001914static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1915 u64 tree_id, u64 dirid, char *name)
1916{
1917 struct btrfs_root *root;
1918 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001919 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001920 int ret = -1;
1921 int slot;
1922 int len;
1923 int total_len = 0;
1924 struct btrfs_inode_ref *iref;
1925 struct extent_buffer *l;
1926 struct btrfs_path *path;
1927
1928 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1929 name[0]='\0';
1930 return 0;
1931 }
1932
1933 path = btrfs_alloc_path();
1934 if (!path)
1935 return -ENOMEM;
1936
Chris Masonac8e9812010-02-28 15:39:26 -05001937 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001938
1939 key.objectid = tree_id;
1940 key.type = BTRFS_ROOT_ITEM_KEY;
1941 key.offset = (u64)-1;
1942 root = btrfs_read_fs_root_no_name(info, &key);
1943 if (IS_ERR(root)) {
1944 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001945 ret = -ENOENT;
1946 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001947 }
1948
1949 key.objectid = dirid;
1950 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001951 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001952
1953 while(1) {
1954 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1955 if (ret < 0)
1956 goto out;
1957
1958 l = path->nodes[0];
1959 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001960 if (ret > 0 && slot > 0)
1961 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001962 btrfs_item_key_to_cpu(l, &key, slot);
1963
1964 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001965 key.type != BTRFS_INODE_REF_KEY)) {
1966 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001967 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001968 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001969
1970 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1971 len = btrfs_inode_ref_name_len(l, iref);
1972 ptr -= len + 1;
1973 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001974 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001975 goto out;
1976
1977 *(ptr + len) = '/';
1978 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1979
1980 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1981 break;
1982
David Sterbab3b4aa72011-04-21 01:20:15 +02001983 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001984 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001985 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001986 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001987 }
Chris Masonac8e9812010-02-28 15:39:26 -05001988 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001989 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001990 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001991 name[total_len]='\0';
1992 ret = 0;
1993out:
1994 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001995 return ret;
1996}
1997
1998static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1999 void __user *argp)
2000{
2001 struct btrfs_ioctl_ino_lookup_args *args;
2002 struct inode *inode;
2003 int ret;
2004
2005 if (!capable(CAP_SYS_ADMIN))
2006 return -EPERM;
2007
Julia Lawall2354d082010-10-29 15:14:18 -04002008 args = memdup_user(argp, sizeof(*args));
2009 if (IS_ERR(args))
2010 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002011
Chris Masonac8e9812010-02-28 15:39:26 -05002012 inode = fdentry(file)->d_inode;
2013
Chris Mason1b53ac42010-03-18 12:17:05 -04002014 if (args->treeid == 0)
2015 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2016
Chris Masonac8e9812010-02-28 15:39:26 -05002017 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2018 args->treeid, args->objectid,
2019 args->name);
2020
2021 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2022 ret = -EFAULT;
2023
2024 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002025 return ret;
2026}
2027
Yan, Zheng76dda932009-09-21 16:00:26 -04002028static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2029 void __user *arg)
2030{
2031 struct dentry *parent = fdentry(file);
2032 struct dentry *dentry;
2033 struct inode *dir = parent->d_inode;
2034 struct inode *inode;
2035 struct btrfs_root *root = BTRFS_I(dir)->root;
2036 struct btrfs_root *dest = NULL;
2037 struct btrfs_ioctl_vol_args *vol_args;
2038 struct btrfs_trans_handle *trans;
2039 int namelen;
2040 int ret;
2041 int err = 0;
2042
Yan, Zheng76dda932009-09-21 16:00:26 -04002043 vol_args = memdup_user(arg, sizeof(*vol_args));
2044 if (IS_ERR(vol_args))
2045 return PTR_ERR(vol_args);
2046
2047 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2048 namelen = strlen(vol_args->name);
2049 if (strchr(vol_args->name, '/') ||
2050 strncmp(vol_args->name, "..", namelen) == 0) {
2051 err = -EINVAL;
2052 goto out;
2053 }
2054
Al Viroa561be72011-11-23 11:57:51 -05002055 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002056 if (err)
2057 goto out;
2058
2059 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
2060 dentry = lookup_one_len(vol_args->name, parent, namelen);
2061 if (IS_ERR(dentry)) {
2062 err = PTR_ERR(dentry);
2063 goto out_unlock_dir;
2064 }
2065
2066 if (!dentry->d_inode) {
2067 err = -ENOENT;
2068 goto out_dput;
2069 }
2070
2071 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002072 dest = BTRFS_I(inode)->root;
2073 if (!capable(CAP_SYS_ADMIN)){
2074 /*
2075 * Regular user. Only allow this with a special mount
2076 * option, when the user has write+exec access to the
2077 * subvol root, and when rmdir(2) would have been
2078 * allowed.
2079 *
2080 * Note that this is _not_ check that the subvol is
2081 * empty or doesn't contain data that we wouldn't
2082 * otherwise be able to delete.
2083 *
2084 * Users who want to delete empty subvols should try
2085 * rmdir(2).
2086 */
2087 err = -EPERM;
2088 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2089 goto out_dput;
2090
2091 /*
2092 * Do not allow deletion if the parent dir is the same
2093 * as the dir to be deleted. That means the ioctl
2094 * must be called on the dentry referencing the root
2095 * of the subvol, not a random directory contained
2096 * within it.
2097 */
2098 err = -EINVAL;
2099 if (root == dest)
2100 goto out_dput;
2101
2102 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2103 if (err)
2104 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002105 }
2106
Miao Xie5c39da52012-10-22 11:39:53 +00002107 /* check if subvolume may be deleted by a user */
2108 err = btrfs_may_delete(dir, dentry, 1);
2109 if (err)
2110 goto out_dput;
2111
Li Zefan33345d012011-04-20 10:31:50 +08002112 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002113 err = -EINVAL;
2114 goto out_dput;
2115 }
2116
Yan, Zheng76dda932009-09-21 16:00:26 -04002117 mutex_lock(&inode->i_mutex);
2118 err = d_invalidate(dentry);
2119 if (err)
2120 goto out_unlock;
2121
2122 down_write(&root->fs_info->subvol_sem);
2123
2124 err = may_destroy_subvol(dest);
2125 if (err)
2126 goto out_up_write;
2127
Yan, Zhenga22285a2010-05-16 10:48:46 -04002128 trans = btrfs_start_transaction(root, 0);
2129 if (IS_ERR(trans)) {
2130 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002131 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002132 }
2133 trans->block_rsv = &root->fs_info->global_block_rsv;
2134
Yan, Zheng76dda932009-09-21 16:00:26 -04002135 ret = btrfs_unlink_subvol(trans, root, dir,
2136 dest->root_key.objectid,
2137 dentry->d_name.name,
2138 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002139 if (ret) {
2140 err = ret;
2141 btrfs_abort_transaction(trans, root, ret);
2142 goto out_end_trans;
2143 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002144
2145 btrfs_record_root_in_trans(trans, dest);
2146
2147 memset(&dest->root_item.drop_progress, 0,
2148 sizeof(dest->root_item.drop_progress));
2149 dest->root_item.drop_level = 0;
2150 btrfs_set_root_refs(&dest->root_item, 0);
2151
Yan, Zhengd68fc572010-05-16 10:49:58 -04002152 if (!xchg(&dest->orphan_item_inserted, 1)) {
2153 ret = btrfs_insert_orphan_item(trans,
2154 root->fs_info->tree_root,
2155 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002156 if (ret) {
2157 btrfs_abort_transaction(trans, root, ret);
2158 err = ret;
2159 goto out_end_trans;
2160 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002161 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002162out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002163 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002164 if (ret && !err)
2165 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002166 inode->i_flags |= S_DEAD;
2167out_up_write:
2168 up_write(&root->fs_info->subvol_sem);
2169out_unlock:
2170 mutex_unlock(&inode->i_mutex);
2171 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002172 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002173 btrfs_invalidate_inodes(dest);
2174 d_delete(dentry);
2175 }
2176out_dput:
2177 dput(dentry);
2178out_unlock_dir:
2179 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002180 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002181out:
2182 kfree(vol_args);
2183 return err;
2184}
2185
Chris Mason1e701a32010-03-11 09:42:04 -05002186static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002187{
2188 struct inode *inode = fdentry(file)->d_inode;
2189 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002190 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002191 int ret;
2192
Ilya Dryomov25122d12013-01-20 15:57:57 +02002193 ret = mnt_want_write_file(file);
2194 if (ret)
2195 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002196
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002197 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2198 1)) {
2199 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov25122d12013-01-20 15:57:57 +02002200 mnt_drop_write_file(file);
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002201 return -EINVAL;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002202 }
Ilya Dryomov25122d12013-01-20 15:57:57 +02002203
2204 if (btrfs_root_readonly(root)) {
2205 ret = -EROFS;
2206 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002207 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002208
2209 switch (inode->i_mode & S_IFMT) {
2210 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002211 if (!capable(CAP_SYS_ADMIN)) {
2212 ret = -EPERM;
2213 goto out;
2214 }
Eric Sandeende78b512013-01-31 18:21:12 +00002215 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002216 if (ret)
2217 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002218 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002219 break;
2220 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002221 if (!(file->f_mode & FMODE_WRITE)) {
2222 ret = -EINVAL;
2223 goto out;
2224 }
Chris Mason1e701a32010-03-11 09:42:04 -05002225
2226 range = kzalloc(sizeof(*range), GFP_KERNEL);
2227 if (!range) {
2228 ret = -ENOMEM;
2229 goto out;
2230 }
2231
2232 if (argp) {
2233 if (copy_from_user(range, argp,
2234 sizeof(*range))) {
2235 ret = -EFAULT;
2236 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002237 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002238 }
2239 /* compression requires us to start the IO */
2240 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2241 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2242 range->extent_thresh = (u32)-1;
2243 }
2244 } else {
2245 /* the rest are all set to zero by kzalloc */
2246 range->len = (u64)-1;
2247 }
Chris Mason4cb53002011-05-24 15:35:30 -04002248 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2249 range, 0, 0);
2250 if (ret > 0)
2251 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002252 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002253 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002254 default:
2255 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002256 }
Chris Masone441d542009-01-05 16:57:23 -05002257out:
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002258 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov25122d12013-01-20 15:57:57 +02002259 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002260 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002261}
2262
Christoph Hellwigb2950862008-12-02 09:54:17 -05002263static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002264{
2265 struct btrfs_ioctl_vol_args *vol_args;
2266 int ret;
2267
Chris Masone441d542009-01-05 16:57:23 -05002268 if (!capable(CAP_SYS_ADMIN))
2269 return -EPERM;
2270
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002271 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2272 1)) {
2273 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002274 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002275 }
2276
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002277 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002278 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002279 if (IS_ERR(vol_args)) {
2280 ret = PTR_ERR(vol_args);
2281 goto out;
2282 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002283
Mark Fasheh5516e592008-07-24 12:20:14 -04002284 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002285 ret = btrfs_init_new_device(root, vol_args->name);
2286
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002287 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002288out:
2289 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002290 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002291 return ret;
2292}
2293
Miao Xieda249272012-11-26 08:44:50 +00002294static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002295{
Miao Xieda249272012-11-26 08:44:50 +00002296 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002297 struct btrfs_ioctl_vol_args *vol_args;
2298 int ret;
2299
Chris Masone441d542009-01-05 16:57:23 -05002300 if (!capable(CAP_SYS_ADMIN))
2301 return -EPERM;
2302
Miao Xieda249272012-11-26 08:44:50 +00002303 ret = mnt_want_write_file(file);
2304 if (ret)
2305 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002306
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002307 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2308 1)) {
2309 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Miao Xieda249272012-11-26 08:44:50 +00002310 mnt_drop_write_file(file);
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002311 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002312 }
2313
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002314 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002315 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002316 if (IS_ERR(vol_args)) {
2317 ret = PTR_ERR(vol_args);
2318 goto out;
2319 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002320
Mark Fasheh5516e592008-07-24 12:20:14 -04002321 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002322 ret = btrfs_rm_device(root, vol_args->name);
2323
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002324 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002325out:
2326 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002327 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002328 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002329 return ret;
2330}
2331
Jan Schmidt475f6382011-03-11 15:41:01 +01002332static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2333{
Li Zefan027ed2f2011-06-08 08:27:56 +00002334 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002335 struct btrfs_device *device;
2336 struct btrfs_device *next;
2337 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002338 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002339
2340 if (!capable(CAP_SYS_ADMIN))
2341 return -EPERM;
2342
Li Zefan027ed2f2011-06-08 08:27:56 +00002343 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2344 if (!fi_args)
2345 return -ENOMEM;
2346
2347 fi_args->num_devices = fs_devices->num_devices;
2348 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002349
2350 mutex_lock(&fs_devices->device_list_mutex);
2351 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002352 if (device->devid > fi_args->max_id)
2353 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002354 }
2355 mutex_unlock(&fs_devices->device_list_mutex);
2356
Li Zefan027ed2f2011-06-08 08:27:56 +00002357 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2358 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002359
Li Zefan027ed2f2011-06-08 08:27:56 +00002360 kfree(fi_args);
2361 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002362}
2363
2364static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2365{
2366 struct btrfs_ioctl_dev_info_args *di_args;
2367 struct btrfs_device *dev;
2368 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2369 int ret = 0;
2370 char *s_uuid = NULL;
2371 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2372
2373 if (!capable(CAP_SYS_ADMIN))
2374 return -EPERM;
2375
2376 di_args = memdup_user(arg, sizeof(*di_args));
2377 if (IS_ERR(di_args))
2378 return PTR_ERR(di_args);
2379
2380 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2381 s_uuid = di_args->uuid;
2382
2383 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002384 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002385 mutex_unlock(&fs_devices->device_list_mutex);
2386
2387 if (!dev) {
2388 ret = -ENODEV;
2389 goto out;
2390 }
2391
2392 di_args->devid = dev->devid;
2393 di_args->bytes_used = dev->bytes_used;
2394 di_args->total_bytes = dev->total_bytes;
2395 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002396 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002397 struct rcu_string *name;
2398
2399 rcu_read_lock();
2400 name = rcu_dereference(dev->name);
2401 strncpy(di_args->path, name->str, sizeof(di_args->path));
2402 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002403 di_args->path[sizeof(di_args->path) - 1] = 0;
2404 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002405 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002406 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002407
2408out:
2409 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2410 ret = -EFAULT;
2411
2412 kfree(di_args);
2413 return ret;
2414}
2415
Yan, Zheng76dda932009-09-21 16:00:26 -04002416static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2417 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002418{
2419 struct inode *inode = fdentry(file)->d_inode;
2420 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro2903ff02012-08-28 12:52:22 -04002421 struct fd src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002422 struct inode *src;
2423 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002425 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002426 char *buf;
2427 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002428 u32 nritems;
2429 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002430 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002431 u64 len = olen;
2432 u64 bs = root->fs_info->sb->s_blocksize;
Chris Masond20f7042008-12-08 16:58:54 -05002433
Sage Weilc5c9cd42008-11-12 14:32:25 -05002434 /*
2435 * TODO:
2436 * - split compressed inline extents. annoying: we need to
2437 * decompress into destination's address_space (the file offset
2438 * may change, so source mapping won't do), then recompress (or
2439 * otherwise reinsert) a subrange.
2440 * - allow ranges within the same file to be cloned (provided
2441 * they don't overlap)?
2442 */
2443
Chris Masone441d542009-01-05 16:57:23 -05002444 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002445 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002446 return -EINVAL;
2447
Li Zefanb83cc962010-12-20 16:04:08 +08002448 if (btrfs_root_readonly(root))
2449 return -EROFS;
2450
Al Viroa561be72011-11-23 11:57:51 -05002451 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002452 if (ret)
2453 return ret;
2454
Al Viro2903ff02012-08-28 12:52:22 -04002455 src_file = fdget(srcfd);
2456 if (!src_file.file) {
Yan Zhengab67b7c2008-12-19 10:58:39 -05002457 ret = -EBADF;
2458 goto out_drop_write;
2459 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002460
David Sterba362a20c2011-08-01 18:11:57 +02002461 ret = -EXDEV;
Al Viro2903ff02012-08-28 12:52:22 -04002462 if (src_file.file->f_path.mnt != file->f_path.mnt)
David Sterba362a20c2011-08-01 18:11:57 +02002463 goto out_fput;
2464
Al Viro2903ff02012-08-28 12:52:22 -04002465 src = src_file.file->f_dentry->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002466
Sage Weilc5c9cd42008-11-12 14:32:25 -05002467 ret = -EINVAL;
2468 if (src == inode)
2469 goto out_fput;
2470
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002471 /* the src must be open for reading */
Al Viro2903ff02012-08-28 12:52:22 -04002472 if (!(src_file.file->f_mode & FMODE_READ))
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002473 goto out_fput;
2474
Li Zefan0e7b8242011-09-18 10:20:46 -04002475 /* don't make the dst file partly checksummed */
2476 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2477 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2478 goto out_fput;
2479
Yan Zhengae01a0a2008-08-04 23:23:47 -04002480 ret = -EISDIR;
2481 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002482 goto out_fput;
2483
Yan Zhengae01a0a2008-08-04 23:23:47 -04002484 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002485 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002486 goto out_fput;
2487
2488 ret = -ENOMEM;
2489 buf = vmalloc(btrfs_level_size(root, 0));
2490 if (!buf)
2491 goto out_fput;
2492
2493 path = btrfs_alloc_path();
2494 if (!path) {
2495 vfree(buf);
2496 goto out_fput;
2497 }
2498 path->reada = 2;
2499
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002500 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002501 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2502 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002503 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002504 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2505 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002506 }
2507
Sage Weilc5c9cd42008-11-12 14:32:25 -05002508 /* determine range to clone */
2509 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002510 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002511 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002512 if (len == 0)
2513 olen = len = src->i_size - off;
2514 /* if we extend to eof, continue to block boundary */
2515 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002516 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002517
2518 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002519 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2520 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002521 goto out_unlock;
2522
Li Zefand525e8a2011-09-11 10:52:25 -04002523 if (destoff > inode->i_size) {
2524 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2525 if (ret)
2526 goto out_unlock;
2527 }
2528
Li Zefan71ef0782011-09-18 10:20:46 -04002529 /* truncate page cache pages from target inode range */
2530 truncate_inode_pages_range(&inode->i_data, destoff,
2531 PAGE_CACHE_ALIGN(destoff + len) - 1);
2532
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002533 /* do any pending delalloc/csum calc on src, one way or
2534 another, and lock file content */
2535 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002536 struct btrfs_ordered_extent *ordered;
Liu Boaa42ffd2012-09-18 03:52:23 -06002537 lock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
2538 ordered = btrfs_lookup_first_ordered_extent(src, off + len - 1);
Sage Weil9a019192010-10-29 15:37:33 -04002539 if (!ordered &&
Liu Boaa42ffd2012-09-18 03:52:23 -06002540 !test_range_bit(&BTRFS_I(src)->io_tree, off, off + len - 1,
2541 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002542 break;
Liu Boaa42ffd2012-09-18 03:52:23 -06002543 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002544 if (ordered)
2545 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002546 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002547 }
2548
Sage Weilc5c9cd42008-11-12 14:32:25 -05002549 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002550 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002551 key.type = BTRFS_EXTENT_DATA_KEY;
2552 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002553
2554 while (1) {
2555 /*
2556 * note the key will change type as we walk through the
2557 * tree.
2558 */
David Sterba362a20c2011-08-01 18:11:57 +02002559 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2560 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002561 if (ret < 0)
2562 goto out;
2563
Yan Zhengae01a0a2008-08-04 23:23:47 -04002564 nritems = btrfs_header_nritems(path->nodes[0]);
2565 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002566 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002567 if (ret < 0)
2568 goto out;
2569 if (ret > 0)
2570 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002571 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002572 }
2573 leaf = path->nodes[0];
2574 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002575
Yan Zhengae01a0a2008-08-04 23:23:47 -04002576 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002577 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002578 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002579 break;
2580
Sage Weilc5c9cd42008-11-12 14:32:25 -05002581 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2582 struct btrfs_file_extent_item *extent;
2583 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002584 u32 size;
2585 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002586 u64 disko = 0, diskl = 0;
2587 u64 datao = 0, datal = 0;
2588 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002589 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002590
2591 size = btrfs_item_size_nr(leaf, slot);
2592 read_extent_buffer(leaf, buf,
2593 btrfs_item_ptr_offset(leaf, slot),
2594 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002595
2596 extent = btrfs_item_ptr(leaf, slot,
2597 struct btrfs_file_extent_item);
2598 comp = btrfs_file_extent_compression(leaf, extent);
2599 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002600 if (type == BTRFS_FILE_EXTENT_REG ||
2601 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002602 disko = btrfs_file_extent_disk_bytenr(leaf,
2603 extent);
2604 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2605 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002606 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002607 datal = btrfs_file_extent_num_bytes(leaf,
2608 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002609 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2610 /* take upper bound, may be compressed */
2611 datal = btrfs_file_extent_ram_bytes(leaf,
2612 extent);
2613 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002614 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002615
Sage Weil050006a2010-10-29 15:37:33 -04002616 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002617 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002618 goto next;
2619
Zheng Yan31840ae2008-09-23 13:14:14 -04002620 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002621 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002622 if (off <= key.offset)
2623 new_key.offset = key.offset + destoff - off;
2624 else
2625 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002626
Sage Weilb6f34092011-09-20 14:48:51 -04002627 /*
2628 * 1 - adjusting old extent (we may have to split it)
2629 * 1 - add new extent
2630 * 1 - inode update
2631 */
2632 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002633 if (IS_ERR(trans)) {
2634 ret = PTR_ERR(trans);
2635 goto out;
2636 }
2637
Chris Masonc8a894d2009-06-27 21:07:03 -04002638 if (type == BTRFS_FILE_EXTENT_REG ||
2639 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002640 /*
2641 * a | --- range to clone ---| b
2642 * | ------------- extent ------------- |
2643 */
2644
2645 /* substract range b */
2646 if (key.offset + datal > off + len)
2647 datal = off + len - key.offset;
2648
2649 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002650 if (off > key.offset) {
2651 datao += off - key.offset;
2652 datal -= off - key.offset;
2653 }
2654
Josef Bacik5dc562c2012-08-17 13:14:17 -04002655 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002656 new_key.offset,
2657 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002658 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002659 if (ret) {
2660 btrfs_abort_transaction(trans, root,
2661 ret);
2662 btrfs_end_transaction(trans, root);
2663 goto out;
2664 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002665
Sage Weilc5c9cd42008-11-12 14:32:25 -05002666 ret = btrfs_insert_empty_item(trans, root, path,
2667 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002668 if (ret) {
2669 btrfs_abort_transaction(trans, root,
2670 ret);
2671 btrfs_end_transaction(trans, root);
2672 goto out;
2673 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002674
2675 leaf = path->nodes[0];
2676 slot = path->slots[0];
2677 write_extent_buffer(leaf, buf,
2678 btrfs_item_ptr_offset(leaf, slot),
2679 size);
2680
2681 extent = btrfs_item_ptr(leaf, slot,
2682 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002683
Sage Weilc5c9cd42008-11-12 14:32:25 -05002684 /* disko == 0 means it's a hole */
2685 if (!disko)
2686 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002687
2688 btrfs_set_file_extent_offset(leaf, extent,
2689 datao);
2690 btrfs_set_file_extent_num_bytes(leaf, extent,
2691 datal);
2692 if (disko) {
2693 inode_add_bytes(inode, datal);
2694 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002695 disko, diskl, 0,
2696 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002697 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002698 new_key.offset - datao,
2699 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002700 if (ret) {
2701 btrfs_abort_transaction(trans,
2702 root,
2703 ret);
2704 btrfs_end_transaction(trans,
2705 root);
2706 goto out;
2707
2708 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002709 }
2710 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2711 u64 skip = 0;
2712 u64 trim = 0;
2713 if (off > key.offset) {
2714 skip = off - key.offset;
2715 new_key.offset += skip;
2716 }
Chris Masond3977122009-01-05 21:25:51 -05002717
Liu Boaa42ffd2012-09-18 03:52:23 -06002718 if (key.offset + datal > off + len)
2719 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002720
Sage Weilc5c9cd42008-11-12 14:32:25 -05002721 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002722 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002723 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002724 goto out;
2725 }
2726 size -= skip + trim;
2727 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002728
Josef Bacik5dc562c2012-08-17 13:14:17 -04002729 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002730 new_key.offset,
2731 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002732 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002733 if (ret) {
2734 btrfs_abort_transaction(trans, root,
2735 ret);
2736 btrfs_end_transaction(trans, root);
2737 goto out;
2738 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002739
Sage Weilc5c9cd42008-11-12 14:32:25 -05002740 ret = btrfs_insert_empty_item(trans, root, path,
2741 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002742 if (ret) {
2743 btrfs_abort_transaction(trans, root,
2744 ret);
2745 btrfs_end_transaction(trans, root);
2746 goto out;
2747 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002748
2749 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002750 u32 start =
2751 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002752 memmove(buf+start, buf+start+skip,
2753 datal);
2754 }
2755
2756 leaf = path->nodes[0];
2757 slot = path->slots[0];
2758 write_extent_buffer(leaf, buf,
2759 btrfs_item_ptr_offset(leaf, slot),
2760 size);
2761 inode_add_bytes(inode, datal);
2762 }
2763
2764 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002765 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002766
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002767 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002768 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002769
2770 /*
2771 * we round up to the block size at eof when
2772 * determining which extents to clone above,
2773 * but shouldn't round up the file size
2774 */
2775 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002776 if (endoff > destoff+olen)
2777 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002778 if (endoff > inode->i_size)
2779 btrfs_i_size_write(inode, endoff);
2780
Yan, Zhenga22285a2010-05-16 10:48:46 -04002781 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002782 if (ret) {
2783 btrfs_abort_transaction(trans, root, ret);
2784 btrfs_end_transaction(trans, root);
2785 goto out;
2786 }
2787 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002788 }
Chris Masond3977122009-01-05 21:25:51 -05002789next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002790 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002791 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002792 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002793 ret = 0;
2794out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002795 btrfs_release_path(path);
Liu Boaa42ffd2012-09-18 03:52:23 -06002796 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002797out_unlock:
2798 mutex_unlock(&src->i_mutex);
2799 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002800 vfree(buf);
2801 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002802out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04002803 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002804out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002805 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002806 return ret;
2807}
2808
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002809static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002810{
2811 struct btrfs_ioctl_clone_range_args args;
2812
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002813 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002814 return -EFAULT;
2815 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2816 args.src_length, args.dest_offset);
2817}
2818
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002819/*
2820 * there are many ways the trans_start and trans_end ioctls can lead
2821 * to deadlocks. They should only be used by applications that
2822 * basically own the machine, and have a very in depth understanding
2823 * of all the possible deadlocks and enospc problems.
2824 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002825static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002826{
2827 struct inode *inode = fdentry(file)->d_inode;
2828 struct btrfs_root *root = BTRFS_I(inode)->root;
2829 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002830 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002831
Sage Weil1ab86ae2009-09-29 18:38:44 -04002832 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002833 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002834 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002835
2836 ret = -EINPROGRESS;
2837 if (file->private_data)
2838 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002839
Li Zefanb83cc962010-12-20 16:04:08 +08002840 ret = -EROFS;
2841 if (btrfs_root_readonly(root))
2842 goto out;
2843
Al Viroa561be72011-11-23 11:57:51 -05002844 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002845 if (ret)
2846 goto out;
2847
Josef Bacika4abeea2011-04-11 17:25:13 -04002848 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002849
Sage Weil1ab86ae2009-09-29 18:38:44 -04002850 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002851 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002852 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002853 goto out_drop;
2854
2855 file->private_data = trans;
2856 return 0;
2857
2858out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002859 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002860 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002861out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002862 return ret;
2863}
2864
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002865static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2866{
2867 struct inode *inode = fdentry(file)->d_inode;
2868 struct btrfs_root *root = BTRFS_I(inode)->root;
2869 struct btrfs_root *new_root;
2870 struct btrfs_dir_item *di;
2871 struct btrfs_trans_handle *trans;
2872 struct btrfs_path *path;
2873 struct btrfs_key location;
2874 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002875 u64 objectid = 0;
2876 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00002877 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002878
2879 if (!capable(CAP_SYS_ADMIN))
2880 return -EPERM;
2881
Miao Xie3c04ce02012-11-26 08:43:07 +00002882 ret = mnt_want_write_file(file);
2883 if (ret)
2884 return ret;
2885
2886 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
2887 ret = -EFAULT;
2888 goto out;
2889 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002890
2891 if (!objectid)
2892 objectid = root->root_key.objectid;
2893
2894 location.objectid = objectid;
2895 location.type = BTRFS_ROOT_ITEM_KEY;
2896 location.offset = (u64)-1;
2897
2898 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00002899 if (IS_ERR(new_root)) {
2900 ret = PTR_ERR(new_root);
2901 goto out;
2902 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002903
Miao Xie3c04ce02012-11-26 08:43:07 +00002904 if (btrfs_root_refs(&new_root->root_item) == 0) {
2905 ret = -ENOENT;
2906 goto out;
2907 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002908
2909 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00002910 if (!path) {
2911 ret = -ENOMEM;
2912 goto out;
2913 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002914 path->leave_spinning = 1;
2915
2916 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002917 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002918 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00002919 ret = PTR_ERR(trans);
2920 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002921 }
2922
David Sterba6c417612011-04-13 15:41:04 +02002923 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002924 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2925 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002926 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002927 btrfs_free_path(path);
2928 btrfs_end_transaction(trans, root);
2929 printk(KERN_ERR "Umm, you don't have the default dir item, "
2930 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00002931 ret = -ENOENT;
2932 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002933 }
2934
2935 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2936 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2937 btrfs_mark_buffer_dirty(path->nodes[0]);
2938 btrfs_free_path(path);
2939
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06002940 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002941 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00002942out:
2943 mnt_drop_write_file(file);
2944 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002945}
2946
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002947void btrfs_get_block_group_info(struct list_head *groups_list,
2948 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002949{
2950 struct btrfs_block_group_cache *block_group;
2951
2952 space->total_bytes = 0;
2953 space->used_bytes = 0;
2954 space->flags = 0;
2955 list_for_each_entry(block_group, groups_list, list) {
2956 space->flags = block_group->flags;
2957 space->total_bytes += block_group->key.offset;
2958 space->used_bytes +=
2959 btrfs_block_group_used(&block_group->item);
2960 }
2961}
2962
Josef Bacik1406e432010-01-13 18:19:06 +00002963long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2964{
2965 struct btrfs_ioctl_space_args space_args;
2966 struct btrfs_ioctl_space_info space;
2967 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002968 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002969 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002970 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002971 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2972 BTRFS_BLOCK_GROUP_SYSTEM,
2973 BTRFS_BLOCK_GROUP_METADATA,
2974 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2975 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002976 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002977 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002978 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002979 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002980
2981 if (copy_from_user(&space_args,
2982 (struct btrfs_ioctl_space_args __user *)arg,
2983 sizeof(space_args)))
2984 return -EFAULT;
2985
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002986 for (i = 0; i < num_types; i++) {
2987 struct btrfs_space_info *tmp;
2988
2989 info = NULL;
2990 rcu_read_lock();
2991 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2992 list) {
2993 if (tmp->flags == types[i]) {
2994 info = tmp;
2995 break;
2996 }
2997 }
2998 rcu_read_unlock();
2999
3000 if (!info)
3001 continue;
3002
3003 down_read(&info->groups_sem);
3004 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3005 if (!list_empty(&info->block_groups[c]))
3006 slot_count++;
3007 }
3008 up_read(&info->groups_sem);
3009 }
Josef Bacik1406e432010-01-13 18:19:06 +00003010
Chris Mason7fde62b2010-03-16 15:40:10 -04003011 /* space_slots == 0 means they are asking for a count */
3012 if (space_args.space_slots == 0) {
3013 space_args.total_spaces = slot_count;
3014 goto out;
3015 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003016
Dan Rosenberg51788b12011-02-14 16:04:23 -05003017 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003018
Chris Mason7fde62b2010-03-16 15:40:10 -04003019 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003020
Chris Mason7fde62b2010-03-16 15:40:10 -04003021 /* we generally have at most 6 or so space infos, one for each raid
3022 * level. So, a whole page should be more than enough for everyone
3023 */
3024 if (alloc_size > PAGE_CACHE_SIZE)
3025 return -ENOMEM;
3026
3027 space_args.total_spaces = 0;
3028 dest = kmalloc(alloc_size, GFP_NOFS);
3029 if (!dest)
3030 return -ENOMEM;
3031 dest_orig = dest;
3032
3033 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003034 for (i = 0; i < num_types; i++) {
3035 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003036
Dan Rosenberg51788b12011-02-14 16:04:23 -05003037 if (!slot_count)
3038 break;
3039
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003040 info = NULL;
3041 rcu_read_lock();
3042 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3043 list) {
3044 if (tmp->flags == types[i]) {
3045 info = tmp;
3046 break;
3047 }
3048 }
3049 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003050
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003051 if (!info)
3052 continue;
3053 down_read(&info->groups_sem);
3054 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3055 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003056 btrfs_get_block_group_info(
3057 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003058 memcpy(dest, &space, sizeof(space));
3059 dest++;
3060 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003061 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003062 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003063 if (!slot_count)
3064 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003065 }
3066 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003067 }
Josef Bacik1406e432010-01-13 18:19:06 +00003068
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003069 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003070 (arg + sizeof(struct btrfs_ioctl_space_args));
3071
3072 if (copy_to_user(user_dest, dest_orig, alloc_size))
3073 ret = -EFAULT;
3074
3075 kfree(dest_orig);
3076out:
3077 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003078 ret = -EFAULT;
3079
3080 return ret;
3081}
3082
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003083/*
3084 * there are many ways the trans_start and trans_end ioctls can lead
3085 * to deadlocks. They should only be used by applications that
3086 * basically own the machine, and have a very in depth understanding
3087 * of all the possible deadlocks and enospc problems.
3088 */
3089long btrfs_ioctl_trans_end(struct file *file)
3090{
3091 struct inode *inode = fdentry(file)->d_inode;
3092 struct btrfs_root *root = BTRFS_I(inode)->root;
3093 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003094
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003095 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003096 if (!trans)
3097 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003098 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003099
Sage Weil1ab86ae2009-09-29 18:38:44 -04003100 btrfs_end_transaction(trans, root);
3101
Josef Bacika4abeea2011-04-11 17:25:13 -04003102 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003103
Al Viro2a79f172011-12-09 08:06:57 -05003104 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003105 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003106}
3107
Miao Xie9a8c28b2012-11-26 08:40:43 +00003108static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3109 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003110{
Sage Weil46204592010-10-29 15:41:32 -04003111 struct btrfs_trans_handle *trans;
3112 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003113 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003114
Miao Xieff7c1d32012-11-26 08:41:29 +00003115 trans = btrfs_attach_transaction(root);
3116 if (IS_ERR(trans)) {
3117 if (PTR_ERR(trans) != -ENOENT)
3118 return PTR_ERR(trans);
3119
3120 /* No running transaction, don't bother */
3121 transid = root->fs_info->last_trans_committed;
3122 goto out;
3123 }
Sage Weil46204592010-10-29 15:41:32 -04003124 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003125 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003126 if (ret) {
3127 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003128 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003129 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003130out:
Sage Weil46204592010-10-29 15:41:32 -04003131 if (argp)
3132 if (copy_to_user(argp, &transid, sizeof(transid)))
3133 return -EFAULT;
3134 return 0;
3135}
3136
Miao Xie9a8c28b2012-11-26 08:40:43 +00003137static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3138 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003139{
Sage Weil46204592010-10-29 15:41:32 -04003140 u64 transid;
3141
3142 if (argp) {
3143 if (copy_from_user(&transid, argp, sizeof(transid)))
3144 return -EFAULT;
3145 } else {
3146 transid = 0; /* current trans */
3147 }
3148 return btrfs_wait_for_commit(root, transid);
3149}
3150
Miao Xieb8e95482012-11-26 08:48:01 +00003151static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003152{
Miao Xieb8e95482012-11-26 08:48:01 +00003153 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003154 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003155 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003156
3157 if (!capable(CAP_SYS_ADMIN))
3158 return -EPERM;
3159
3160 sa = memdup_user(arg, sizeof(*sa));
3161 if (IS_ERR(sa))
3162 return PTR_ERR(sa);
3163
Miao Xieb8e95482012-11-26 08:48:01 +00003164 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3165 ret = mnt_want_write_file(file);
3166 if (ret)
3167 goto out;
3168 }
3169
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003170 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003171 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3172 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003173
3174 if (copy_to_user(arg, sa, sizeof(*sa)))
3175 ret = -EFAULT;
3176
Miao Xieb8e95482012-11-26 08:48:01 +00003177 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3178 mnt_drop_write_file(file);
3179out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003180 kfree(sa);
3181 return ret;
3182}
3183
3184static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3185{
3186 if (!capable(CAP_SYS_ADMIN))
3187 return -EPERM;
3188
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003189 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003190}
3191
3192static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3193 void __user *arg)
3194{
3195 struct btrfs_ioctl_scrub_args *sa;
3196 int ret;
3197
3198 if (!capable(CAP_SYS_ADMIN))
3199 return -EPERM;
3200
3201 sa = memdup_user(arg, sizeof(*sa));
3202 if (IS_ERR(sa))
3203 return PTR_ERR(sa);
3204
3205 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3206
3207 if (copy_to_user(arg, sa, sizeof(*sa)))
3208 ret = -EFAULT;
3209
3210 kfree(sa);
3211 return ret;
3212}
3213
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003214static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003215 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003216{
3217 struct btrfs_ioctl_get_dev_stats *sa;
3218 int ret;
3219
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003220 sa = memdup_user(arg, sizeof(*sa));
3221 if (IS_ERR(sa))
3222 return PTR_ERR(sa);
3223
David Sterbab27f7c02012-06-22 06:30:39 -06003224 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3225 kfree(sa);
3226 return -EPERM;
3227 }
3228
3229 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003230
3231 if (copy_to_user(arg, sa, sizeof(*sa)))
3232 ret = -EFAULT;
3233
3234 kfree(sa);
3235 return ret;
3236}
3237
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003238static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3239{
3240 struct btrfs_ioctl_dev_replace_args *p;
3241 int ret;
3242
3243 if (!capable(CAP_SYS_ADMIN))
3244 return -EPERM;
3245
3246 p = memdup_user(arg, sizeof(*p));
3247 if (IS_ERR(p))
3248 return PTR_ERR(p);
3249
3250 switch (p->cmd) {
3251 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3252 if (atomic_xchg(
3253 &root->fs_info->mutually_exclusive_operation_running,
3254 1)) {
3255 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3256 ret = -EINPROGRESS;
3257 } else {
3258 ret = btrfs_dev_replace_start(root, p);
3259 atomic_set(
3260 &root->fs_info->mutually_exclusive_operation_running,
3261 0);
3262 }
3263 break;
3264 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3265 btrfs_dev_replace_status(root->fs_info, p);
3266 ret = 0;
3267 break;
3268 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3269 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3270 break;
3271 default:
3272 ret = -EINVAL;
3273 break;
3274 }
3275
3276 if (copy_to_user(arg, p, sizeof(*p)))
3277 ret = -EFAULT;
3278
3279 kfree(p);
3280 return ret;
3281}
3282
Jan Schmidtd7728c92011-07-07 16:48:38 +02003283static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3284{
3285 int ret = 0;
3286 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003287 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003288 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003289 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003290 struct inode_fs_paths *ipath = NULL;
3291 struct btrfs_path *path;
3292
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003293 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003294 return -EPERM;
3295
3296 path = btrfs_alloc_path();
3297 if (!path) {
3298 ret = -ENOMEM;
3299 goto out;
3300 }
3301
3302 ipa = memdup_user(arg, sizeof(*ipa));
3303 if (IS_ERR(ipa)) {
3304 ret = PTR_ERR(ipa);
3305 ipa = NULL;
3306 goto out;
3307 }
3308
3309 size = min_t(u32, ipa->size, 4096);
3310 ipath = init_ipath(size, root, path);
3311 if (IS_ERR(ipath)) {
3312 ret = PTR_ERR(ipath);
3313 ipath = NULL;
3314 goto out;
3315 }
3316
3317 ret = paths_from_inode(ipa->inum, ipath);
3318 if (ret < 0)
3319 goto out;
3320
3321 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003322 rel_ptr = ipath->fspath->val[i] -
3323 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003324 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003325 }
3326
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003327 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3328 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003329 if (ret) {
3330 ret = -EFAULT;
3331 goto out;
3332 }
3333
3334out:
3335 btrfs_free_path(path);
3336 free_ipath(ipath);
3337 kfree(ipa);
3338
3339 return ret;
3340}
3341
3342static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3343{
3344 struct btrfs_data_container *inodes = ctx;
3345 const size_t c = 3 * sizeof(u64);
3346
3347 if (inodes->bytes_left >= c) {
3348 inodes->bytes_left -= c;
3349 inodes->val[inodes->elem_cnt] = inum;
3350 inodes->val[inodes->elem_cnt + 1] = offset;
3351 inodes->val[inodes->elem_cnt + 2] = root;
3352 inodes->elem_cnt += 3;
3353 } else {
3354 inodes->bytes_missing += c - inodes->bytes_left;
3355 inodes->bytes_left = 0;
3356 inodes->elem_missed += 3;
3357 }
3358
3359 return 0;
3360}
3361
3362static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3363 void __user *arg)
3364{
3365 int ret = 0;
3366 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003367 struct btrfs_ioctl_logical_ino_args *loi;
3368 struct btrfs_data_container *inodes = NULL;
3369 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003370
3371 if (!capable(CAP_SYS_ADMIN))
3372 return -EPERM;
3373
3374 loi = memdup_user(arg, sizeof(*loi));
3375 if (IS_ERR(loi)) {
3376 ret = PTR_ERR(loi);
3377 loi = NULL;
3378 goto out;
3379 }
3380
3381 path = btrfs_alloc_path();
3382 if (!path) {
3383 ret = -ENOMEM;
3384 goto out;
3385 }
3386
Liu Bo425d17a2012-09-07 20:01:30 -06003387 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003388 inodes = init_data_container(size);
3389 if (IS_ERR(inodes)) {
3390 ret = PTR_ERR(inodes);
3391 inodes = NULL;
3392 goto out;
3393 }
3394
Liu Bodf031f02012-09-07 20:01:29 -06003395 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3396 build_ino_list, inodes);
3397 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003398 ret = -ENOENT;
3399 if (ret < 0)
3400 goto out;
3401
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003402 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3403 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003404 if (ret)
3405 ret = -EFAULT;
3406
3407out:
3408 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003409 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003410 kfree(loi);
3411
3412 return ret;
3413}
3414
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003415void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416 struct btrfs_ioctl_balance_args *bargs)
3417{
3418 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3419
3420 bargs->flags = bctl->flags;
3421
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003422 if (atomic_read(&fs_info->balance_running))
3423 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3424 if (atomic_read(&fs_info->balance_pause_req))
3425 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003426 if (atomic_read(&fs_info->balance_cancel_req))
3427 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003428
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003429 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3430 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3431 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003432
3433 if (lock) {
3434 spin_lock(&fs_info->balance_lock);
3435 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3436 spin_unlock(&fs_info->balance_lock);
3437 } else {
3438 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3439 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003440}
3441
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003442static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003443{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003444 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003445 struct btrfs_fs_info *fs_info = root->fs_info;
3446 struct btrfs_ioctl_balance_args *bargs;
3447 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003448 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003449 int ret;
3450
3451 if (!capable(CAP_SYS_ADMIN))
3452 return -EPERM;
3453
Liu Boe54bfa32012-06-29 03:58:48 -06003454 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003455 if (ret)
3456 return ret;
3457
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003458again:
3459 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3460 mutex_lock(&fs_info->volume_mutex);
3461 mutex_lock(&fs_info->balance_mutex);
3462 need_unlock = true;
3463 goto locked;
3464 }
3465
3466 /*
3467 * mut. excl. ops lock is locked. Three possibilites:
3468 * (1) some other op is running
3469 * (2) balance is running
3470 * (3) balance is paused -- special case (think resume)
3471 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003472 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003473 if (fs_info->balance_ctl) {
3474 /* this is either (2) or (3) */
3475 if (!atomic_read(&fs_info->balance_running)) {
3476 mutex_unlock(&fs_info->balance_mutex);
3477 if (!mutex_trylock(&fs_info->volume_mutex))
3478 goto again;
3479 mutex_lock(&fs_info->balance_mutex);
3480
3481 if (fs_info->balance_ctl &&
3482 !atomic_read(&fs_info->balance_running)) {
3483 /* this is (3) */
3484 need_unlock = false;
3485 goto locked;
3486 }
3487
3488 mutex_unlock(&fs_info->balance_mutex);
3489 mutex_unlock(&fs_info->volume_mutex);
3490 goto again;
3491 } else {
3492 /* this is (2) */
3493 mutex_unlock(&fs_info->balance_mutex);
3494 ret = -EINPROGRESS;
3495 goto out;
3496 }
3497 } else {
3498 /* this is (1) */
3499 mutex_unlock(&fs_info->balance_mutex);
3500 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3501 ret = -EINVAL;
3502 goto out;
3503 }
3504
3505locked:
3506 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003507
3508 if (arg) {
3509 bargs = memdup_user(arg, sizeof(*bargs));
3510 if (IS_ERR(bargs)) {
3511 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003512 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003513 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003514
3515 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3516 if (!fs_info->balance_ctl) {
3517 ret = -ENOTCONN;
3518 goto out_bargs;
3519 }
3520
3521 bctl = fs_info->balance_ctl;
3522 spin_lock(&fs_info->balance_lock);
3523 bctl->flags |= BTRFS_BALANCE_RESUME;
3524 spin_unlock(&fs_info->balance_lock);
3525
3526 goto do_balance;
3527 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003528 } else {
3529 bargs = NULL;
3530 }
3531
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003532 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003533 ret = -EINPROGRESS;
3534 goto out_bargs;
3535 }
3536
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003537 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3538 if (!bctl) {
3539 ret = -ENOMEM;
3540 goto out_bargs;
3541 }
3542
3543 bctl->fs_info = fs_info;
3544 if (arg) {
3545 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3546 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3547 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3548
3549 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003550 } else {
3551 /* balance everything - no filters */
3552 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003553 }
3554
Ilya Dryomovde322262012-01-16 22:04:49 +02003555do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003556 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003557 * Ownership of bctl and mutually_exclusive_operation_running
3558 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3559 * or, if restriper was paused all the way until unmount, in
3560 * free_fs_info. mutually_exclusive_operation_running is
3561 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003562 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003563 need_unlock = false;
3564
3565 ret = btrfs_balance(bctl, bargs);
3566
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003567 if (arg) {
3568 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3569 ret = -EFAULT;
3570 }
3571
3572out_bargs:
3573 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003574out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003575 mutex_unlock(&fs_info->balance_mutex);
3576 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003577 if (need_unlock)
3578 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3579out:
Liu Boe54bfa32012-06-29 03:58:48 -06003580 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003581 return ret;
3582}
3583
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003584static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3585{
3586 if (!capable(CAP_SYS_ADMIN))
3587 return -EPERM;
3588
3589 switch (cmd) {
3590 case BTRFS_BALANCE_CTL_PAUSE:
3591 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003592 case BTRFS_BALANCE_CTL_CANCEL:
3593 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003594 }
3595
3596 return -EINVAL;
3597}
3598
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003599static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3600 void __user *arg)
3601{
3602 struct btrfs_fs_info *fs_info = root->fs_info;
3603 struct btrfs_ioctl_balance_args *bargs;
3604 int ret = 0;
3605
3606 if (!capable(CAP_SYS_ADMIN))
3607 return -EPERM;
3608
3609 mutex_lock(&fs_info->balance_mutex);
3610 if (!fs_info->balance_ctl) {
3611 ret = -ENOTCONN;
3612 goto out;
3613 }
3614
3615 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3616 if (!bargs) {
3617 ret = -ENOMEM;
3618 goto out;
3619 }
3620
3621 update_ioctl_balance_args(fs_info, 1, bargs);
3622
3623 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3624 ret = -EFAULT;
3625
3626 kfree(bargs);
3627out:
3628 mutex_unlock(&fs_info->balance_mutex);
3629 return ret;
3630}
3631
Miao Xie905b0dd2012-11-26 08:50:11 +00003632static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003633{
Miao Xie905b0dd2012-11-26 08:50:11 +00003634 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003635 struct btrfs_ioctl_quota_ctl_args *sa;
3636 struct btrfs_trans_handle *trans = NULL;
3637 int ret;
3638 int err;
3639
3640 if (!capable(CAP_SYS_ADMIN))
3641 return -EPERM;
3642
Miao Xie905b0dd2012-11-26 08:50:11 +00003643 ret = mnt_want_write_file(file);
3644 if (ret)
3645 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003646
3647 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003648 if (IS_ERR(sa)) {
3649 ret = PTR_ERR(sa);
3650 goto drop_write;
3651 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003652
3653 if (sa->cmd != BTRFS_QUOTA_CTL_RESCAN) {
3654 trans = btrfs_start_transaction(root, 2);
3655 if (IS_ERR(trans)) {
3656 ret = PTR_ERR(trans);
3657 goto out;
3658 }
3659 }
3660
3661 switch (sa->cmd) {
3662 case BTRFS_QUOTA_CTL_ENABLE:
3663 ret = btrfs_quota_enable(trans, root->fs_info);
3664 break;
3665 case BTRFS_QUOTA_CTL_DISABLE:
3666 ret = btrfs_quota_disable(trans, root->fs_info);
3667 break;
3668 case BTRFS_QUOTA_CTL_RESCAN:
3669 ret = btrfs_quota_rescan(root->fs_info);
3670 break;
3671 default:
3672 ret = -EINVAL;
3673 break;
3674 }
3675
3676 if (copy_to_user(arg, sa, sizeof(*sa)))
3677 ret = -EFAULT;
3678
3679 if (trans) {
3680 err = btrfs_commit_transaction(trans, root);
3681 if (err && !ret)
3682 ret = err;
3683 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003684out:
3685 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003686drop_write:
3687 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003688 return ret;
3689}
3690
Miao Xie905b0dd2012-11-26 08:50:11 +00003691static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003692{
Miao Xie905b0dd2012-11-26 08:50:11 +00003693 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003694 struct btrfs_ioctl_qgroup_assign_args *sa;
3695 struct btrfs_trans_handle *trans;
3696 int ret;
3697 int err;
3698
3699 if (!capable(CAP_SYS_ADMIN))
3700 return -EPERM;
3701
Miao Xie905b0dd2012-11-26 08:50:11 +00003702 ret = mnt_want_write_file(file);
3703 if (ret)
3704 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003705
3706 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003707 if (IS_ERR(sa)) {
3708 ret = PTR_ERR(sa);
3709 goto drop_write;
3710 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003711
3712 trans = btrfs_join_transaction(root);
3713 if (IS_ERR(trans)) {
3714 ret = PTR_ERR(trans);
3715 goto out;
3716 }
3717
3718 /* FIXME: check if the IDs really exist */
3719 if (sa->assign) {
3720 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
3721 sa->src, sa->dst);
3722 } else {
3723 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
3724 sa->src, sa->dst);
3725 }
3726
3727 err = btrfs_end_transaction(trans, root);
3728 if (err && !ret)
3729 ret = err;
3730
3731out:
3732 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003733drop_write:
3734 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003735 return ret;
3736}
3737
Miao Xie905b0dd2012-11-26 08:50:11 +00003738static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003739{
Miao Xie905b0dd2012-11-26 08:50:11 +00003740 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003741 struct btrfs_ioctl_qgroup_create_args *sa;
3742 struct btrfs_trans_handle *trans;
3743 int ret;
3744 int err;
3745
3746 if (!capable(CAP_SYS_ADMIN))
3747 return -EPERM;
3748
Miao Xie905b0dd2012-11-26 08:50:11 +00003749 ret = mnt_want_write_file(file);
3750 if (ret)
3751 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003752
3753 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003754 if (IS_ERR(sa)) {
3755 ret = PTR_ERR(sa);
3756 goto drop_write;
3757 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003758
Miao Xied86e56c2012-11-15 11:35:41 +00003759 if (!sa->qgroupid) {
3760 ret = -EINVAL;
3761 goto out;
3762 }
3763
Arne Jansen5d13a372011-09-14 15:53:51 +02003764 trans = btrfs_join_transaction(root);
3765 if (IS_ERR(trans)) {
3766 ret = PTR_ERR(trans);
3767 goto out;
3768 }
3769
3770 /* FIXME: check if the IDs really exist */
3771 if (sa->create) {
3772 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
3773 NULL);
3774 } else {
3775 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
3776 }
3777
3778 err = btrfs_end_transaction(trans, root);
3779 if (err && !ret)
3780 ret = err;
3781
3782out:
3783 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003784drop_write:
3785 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003786 return ret;
3787}
3788
Miao Xie905b0dd2012-11-26 08:50:11 +00003789static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02003790{
Miao Xie905b0dd2012-11-26 08:50:11 +00003791 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02003792 struct btrfs_ioctl_qgroup_limit_args *sa;
3793 struct btrfs_trans_handle *trans;
3794 int ret;
3795 int err;
3796 u64 qgroupid;
3797
3798 if (!capable(CAP_SYS_ADMIN))
3799 return -EPERM;
3800
Miao Xie905b0dd2012-11-26 08:50:11 +00003801 ret = mnt_want_write_file(file);
3802 if (ret)
3803 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02003804
3805 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00003806 if (IS_ERR(sa)) {
3807 ret = PTR_ERR(sa);
3808 goto drop_write;
3809 }
Arne Jansen5d13a372011-09-14 15:53:51 +02003810
3811 trans = btrfs_join_transaction(root);
3812 if (IS_ERR(trans)) {
3813 ret = PTR_ERR(trans);
3814 goto out;
3815 }
3816
3817 qgroupid = sa->qgroupid;
3818 if (!qgroupid) {
3819 /* take the current subvol as qgroup */
3820 qgroupid = root->root_key.objectid;
3821 }
3822
3823 /* FIXME: check if the IDs really exist */
3824 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
3825
3826 err = btrfs_end_transaction(trans, root);
3827 if (err && !ret)
3828 ret = err;
3829
3830out:
3831 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00003832drop_write:
3833 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02003834 return ret;
3835}
3836
Alexander Block8ea05e32012-07-25 17:35:53 +02003837static long btrfs_ioctl_set_received_subvol(struct file *file,
3838 void __user *arg)
3839{
3840 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3841 struct inode *inode = fdentry(file)->d_inode;
3842 struct btrfs_root *root = BTRFS_I(inode)->root;
3843 struct btrfs_root_item *root_item = &root->root_item;
3844 struct btrfs_trans_handle *trans;
3845 struct timespec ct = CURRENT_TIME;
3846 int ret = 0;
3847
3848 ret = mnt_want_write_file(file);
3849 if (ret < 0)
3850 return ret;
3851
3852 down_write(&root->fs_info->subvol_sem);
3853
3854 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3855 ret = -EINVAL;
3856 goto out;
3857 }
3858
3859 if (btrfs_root_readonly(root)) {
3860 ret = -EROFS;
3861 goto out;
3862 }
3863
3864 if (!inode_owner_or_capable(inode)) {
3865 ret = -EACCES;
3866 goto out;
3867 }
3868
3869 sa = memdup_user(arg, sizeof(*sa));
3870 if (IS_ERR(sa)) {
3871 ret = PTR_ERR(sa);
3872 sa = NULL;
3873 goto out;
3874 }
3875
3876 trans = btrfs_start_transaction(root, 1);
3877 if (IS_ERR(trans)) {
3878 ret = PTR_ERR(trans);
3879 trans = NULL;
3880 goto out;
3881 }
3882
3883 sa->rtransid = trans->transid;
3884 sa->rtime.sec = ct.tv_sec;
3885 sa->rtime.nsec = ct.tv_nsec;
3886
3887 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3888 btrfs_set_root_stransid(root_item, sa->stransid);
3889 btrfs_set_root_rtransid(root_item, sa->rtransid);
3890 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3891 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3892 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3893 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3894
3895 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3896 &root->root_key, &root->root_item);
3897 if (ret < 0) {
3898 btrfs_end_transaction(trans, root);
3899 trans = NULL;
3900 goto out;
3901 } else {
3902 ret = btrfs_commit_transaction(trans, root);
3903 if (ret < 0)
3904 goto out;
3905 }
3906
3907 ret = copy_to_user(arg, sa, sizeof(*sa));
3908 if (ret)
3909 ret = -EFAULT;
3910
3911out:
3912 kfree(sa);
3913 up_write(&root->fs_info->subvol_sem);
3914 mnt_drop_write_file(file);
3915 return ret;
3916}
3917
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003918long btrfs_ioctl(struct file *file, unsigned int
3919 cmd, unsigned long arg)
3920{
3921 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003922 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003923
3924 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003925 case FS_IOC_GETFLAGS:
3926 return btrfs_ioctl_getflags(file, argp);
3927 case FS_IOC_SETFLAGS:
3928 return btrfs_ioctl_setflags(file, argp);
3929 case FS_IOC_GETVERSION:
3930 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003931 case FITRIM:
3932 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003933 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003934 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003935 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003936 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003937 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003938 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02003939 case BTRFS_IOC_SUBVOL_CREATE_V2:
3940 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003941 case BTRFS_IOC_SNAP_DESTROY:
3942 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003943 case BTRFS_IOC_SUBVOL_GETFLAGS:
3944 return btrfs_ioctl_subvol_getflags(file, argp);
3945 case BTRFS_IOC_SUBVOL_SETFLAGS:
3946 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003947 case BTRFS_IOC_DEFAULT_SUBVOL:
3948 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003949 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003950 return btrfs_ioctl_defrag(file, NULL);
3951 case BTRFS_IOC_DEFRAG_RANGE:
3952 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003953 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00003954 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003955 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003956 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003957 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00003958 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003959 case BTRFS_IOC_FS_INFO:
3960 return btrfs_ioctl_fs_info(root, argp);
3961 case BTRFS_IOC_DEV_INFO:
3962 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003963 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003964 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003965 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003966 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3967 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003968 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003969 case BTRFS_IOC_TRANS_START:
3970 return btrfs_ioctl_trans_start(file);
3971 case BTRFS_IOC_TRANS_END:
3972 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003973 case BTRFS_IOC_TREE_SEARCH:
3974 return btrfs_ioctl_tree_search(file, argp);
3975 case BTRFS_IOC_INO_LOOKUP:
3976 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003977 case BTRFS_IOC_INO_PATHS:
3978 return btrfs_ioctl_ino_to_path(root, argp);
3979 case BTRFS_IOC_LOGICAL_INO:
3980 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003981 case BTRFS_IOC_SPACE_INFO:
3982 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003983 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003984 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3985 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003986 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00003987 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04003988 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00003989 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003990 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00003991 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003992 case BTRFS_IOC_SCRUB_CANCEL:
3993 return btrfs_ioctl_scrub_cancel(root, argp);
3994 case BTRFS_IOC_SCRUB_PROGRESS:
3995 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003997 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003998 case BTRFS_IOC_BALANCE_CTL:
3999 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004000 case BTRFS_IOC_BALANCE_PROGRESS:
4001 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004002 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4003 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004004 case BTRFS_IOC_SEND:
4005 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004006 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004007 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004008 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004009 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004010 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004011 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004012 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004013 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004014 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004015 return btrfs_ioctl_qgroup_limit(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004016 case BTRFS_IOC_DEV_REPLACE:
4017 return btrfs_ioctl_dev_replace(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004018 }
4019
4020 return -ENOTTY;
4021}