blob: 1a5b9462dd9ae2639513237358c2f55662bf366f [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>
Mark Fasheh416161d2013-08-06 11:42:51 -070046#include <linux/uaccess.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050047#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040048#include "ctree.h"
49#include "disk-io.h"
50#include "transaction.h"
51#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052#include "print-tree.h"
53#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040054#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080055#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020056#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040057#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020058#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010059#include "dev-replace.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040060
Mark Fasheh416161d2013-08-06 11:42:51 -070061static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/* Mask out flags that are inappropriate for the given type of inode. */
65static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
66{
67 if (S_ISDIR(mode))
68 return flags;
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
71 else
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
73}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040074
Christoph Hellwig6cbff002009-04-17 10:37:41 +020075/*
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
77 */
78static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
79{
80 unsigned int iflags = 0;
81
82 if (flags & BTRFS_INODE_SYNC)
83 iflags |= FS_SYNC_FL;
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000094 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
96
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200101
102 return iflags;
103}
104
105/*
106 * Update inode->i_flags based on the btrfs internal flags.
107 */
108void btrfs_update_iflags(struct inode *inode)
109{
110 struct btrfs_inode *ip = BTRFS_I(inode);
111
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
113
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
124}
125
126/*
127 * Inherit flags from the parent inode.
128 *
Josef Bacike27425d2011-09-27 11:01:30 -0400129 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200130 */
131void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
132{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400133 unsigned int flags;
134
135 if (!dir)
136 return;
137
138 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147
Liu Bo213490b2012-09-11 08:33:50 -0600148 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152 }
Josef Bacike27425d2011-09-27 11:01:30 -0400153
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200154 btrfs_update_iflags(inode);
155}
156
157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
Al Viro496ad9a2013-01-23 17:07:38 -0500159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
Liu Bo75e7cb72011-03-22 10:12:20 +0000167static int check_flags(unsigned int flags)
168{
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000172 FS_NOCOMP_FL | FS_COMPR_FL |
173 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000174 return -EOPNOTSUPP;
175
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
177 return -EINVAL;
178
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 return 0;
180}
181
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200182static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
183{
Al Viro496ad9a2013-01-23 17:07:38 -0500184 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
189 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800190 u64 ip_oldflags;
191 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600192 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193
Li Zefanb83cc962010-12-20 16:04:08 +0800194 if (btrfs_root_readonly(root))
195 return -EROFS;
196
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200197 if (copy_from_user(&flags, arg, sizeof(flags)))
198 return -EFAULT;
199
Liu Bo75e7cb72011-03-22 10:12:20 +0000200 ret = check_flags(flags);
201 if (ret)
202 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700204 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 return -EACCES;
206
Jan Karae7848682012-06-12 16:20:32 +0200207 ret = mnt_want_write_file(file);
208 if (ret)
209 return ret;
210
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200211 mutex_lock(&inode->i_mutex);
212
Li Zefanf062abf2011-12-29 13:36:45 +0800213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600215 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800216
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
221 ret = -EPERM;
222 goto out_unlock;
223 }
224 }
225
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
228 else
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
232 else
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
236 else
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
240 else
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
244 else
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
248 else
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600250 if (flags & FS_NOCOW_FL) {
251 if (S_ISREG(mode)) {
252 /*
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
256 */
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
260 } else {
261 ip->flags |= BTRFS_INODE_NODATACOW;
262 }
263 } else {
264 /*
265 * Revert back under same assuptions as above
266 */
267 if (S_ISREG(mode)) {
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
271 } else {
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
273 }
274 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275
Liu Bo75e7cb72011-03-22 10:12:20 +0000276 /*
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
279 * things smaller.
280 */
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000287 } else {
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000289 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200290
Li Zefan4da6f1a2011-12-29 13:39:50 +0800291 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
295 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200296
Li Zefan306424cc2011-12-14 20:12:02 -0500297 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400298 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500299 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800303 out_drop:
304 if (ret) {
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
307 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309 out_unlock:
310 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200311 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000312 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200313}
314
315static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
316{
Al Viro496ad9a2013-01-23 17:07:38 -0500317 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
319 return put_user(inode->i_generation, arg);
320}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400321
Li Dongyangf7039b12011-03-24 10:24:28 +0000322static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323{
Al Viro815745c2011-11-17 15:40:49 -0500324 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
329 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000331 int ret;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335
Xiao Guangrong1f781602011-04-20 10:09:16 +0000336 rcu_read_lock();
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
338 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000339 if (!device->bdev)
340 continue;
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
343 num_devices++;
344 minlen = min((u64)q->limits.discard_granularity,
345 minlen);
346 }
347 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000348 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200349
Li Dongyangf7039b12011-03-24 10:24:28 +0000350 if (!num_devices)
351 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000352 if (copy_from_user(&range, arg, sizeof(range)))
353 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200356 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000357
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200358 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000359 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000361 if (ret < 0)
362 return ret;
363
364 if (copy_to_user(arg, &range, sizeof(range)))
365 return -EFAULT;
366
367 return 0;
368}
369
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200370int btrfs_is_empty_uuid(u8 *uuid)
371{
372 static char empty_uuid[BTRFS_UUID_SIZE] = {0};
373
374 return !memcmp(uuid, empty_uuid, BTRFS_UUID_SIZE);
375}
376
Miao Xied5c12072013-02-28 10:04:33 +0000377static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400378 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400379 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200380 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000381 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382{
383 struct btrfs_trans_handle *trans;
384 struct btrfs_key key;
385 struct btrfs_root_item root_item;
386 struct btrfs_inode_item *inode_item;
387 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000388 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400389 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000390 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200391 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392 int ret;
393 int err;
394 u64 objectid;
395 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500396 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000397 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200398 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400399
Li Zefan581bb052011-04-20 10:06:11 +0800400 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400401 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400402 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000403
Miao Xied5c12072013-02-28 10:04:33 +0000404 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400405 /*
Miao Xied5c12072013-02-28 10:04:33 +0000406 * The same as the snapshot creation, please see the comment
407 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400408 */
Miao Xied5c12072013-02-28 10:04:33 +0000409 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200410 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000411 if (ret)
412 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413
Miao Xied5c12072013-02-28 10:04:33 +0000414 trans = btrfs_start_transaction(root, 0);
415 if (IS_ERR(trans)) {
416 ret = PTR_ERR(trans);
417 goto out;
418 }
419 trans->block_rsv = &block_rsv;
420 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
Miao Xie8696c532013-02-07 06:02:44 +0000422 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200423 if (ret)
424 goto fail;
425
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400426 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200427 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400428 if (IS_ERR(leaf)) {
429 ret = PTR_ERR(leaf);
430 goto fail;
431 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400432
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400433 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 btrfs_set_header_bytenr(leaf, leaf->start);
435 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437 btrfs_set_header_owner(leaf, objectid);
438
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +0200439 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(leaf),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400441 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200442 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400444 btrfs_mark_buffer_dirty(leaf);
445
Alexander Block8ea05e32012-07-25 17:35:53 +0200446 memset(&root_item, 0, sizeof(root_item));
447
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800449 btrfs_set_stack_inode_generation(inode_item, 1);
450 btrfs_set_stack_inode_size(inode_item, 3);
451 btrfs_set_stack_inode_nlink(inode_item, 1);
452 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
453 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400454
Qu Wenruo3cae2102013-07-16 11:19:18 +0800455 btrfs_set_root_flags(&root_item, 0);
456 btrfs_set_root_limit(&root_item, 0);
457 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000458
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400460 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461 btrfs_set_root_level(&root_item, 0);
462 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000463 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400464 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400465
Alexander Block8ea05e32012-07-25 17:35:53 +0200466 btrfs_set_root_generation_v2(&root_item,
467 btrfs_root_generation(&root_item));
468 uuid_le_gen(&new_uuid);
469 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800470 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
471 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200472 root_item.ctime = root_item.otime;
473 btrfs_set_root_ctransid(&root_item, trans->transid);
474 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400475
Chris Mason925baed2008-06-25 16:01:30 -0400476 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477 free_extent_buffer(leaf);
478 leaf = NULL;
479
480 btrfs_set_root_dirid(&root_item, new_dirid);
481
482 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400483 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
485 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
486 &root_item);
487 if (ret)
488 goto fail;
489
Yan, Zheng76dda932009-09-21 16:00:26 -0400490 key.offset = (u64)-1;
491 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100492 if (IS_ERR(new_root)) {
493 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
494 ret = PTR_ERR(new_root);
495 goto fail;
496 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400497
498 btrfs_record_root_in_trans(trans, new_root);
499
Josef Bacikd82a6f12011-05-11 15:26:06 -0400500 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700501 if (ret) {
502 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100503 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700504 goto fail;
505 }
506
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400507 /*
508 * insert the directory item
509 */
Chris Mason3de45862008-11-17 21:02:50 -0500510 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100511 if (ret) {
512 btrfs_abort_transaction(trans, root, ret);
513 goto fail;
514 }
Chris Mason3de45862008-11-17 21:02:50 -0500515
516 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800517 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500518 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100519 if (ret) {
520 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400521 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100522 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500523
Yan Zheng52c26172009-01-05 15:43:43 -0500524 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
525 ret = btrfs_update_inode(trans, root, dir);
526 BUG_ON(ret);
527
Chris Mason0660b5a2008-11-17 20:37:39 -0500528 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400529 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800530 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500531 BUG_ON(ret);
532
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200533 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
534 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
535 objectid);
536 if (ret)
537 btrfs_abort_transaction(trans, root, ret);
538
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539fail:
Miao Xied5c12072013-02-28 10:04:33 +0000540 trans->block_rsv = NULL;
541 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400542 if (async_transid) {
543 *async_transid = trans->transid;
544 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000545 if (err)
546 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400547 } else {
548 err = btrfs_commit_transaction(trans, root);
549 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400550 if (err && !ret)
551 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500552
553 if (!ret)
554 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000555out:
556 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 return ret;
558}
559
Miao Xiee9662f72013-02-28 10:01:15 +0000560static int create_snapshot(struct btrfs_root *root, struct inode *dir,
561 struct dentry *dentry, char *name, int namelen,
562 u64 *async_transid, bool readonly,
563 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400564{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000565 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400566 struct btrfs_pending_snapshot *pending_snapshot;
567 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000568 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400569
570 if (!root->ref_cows)
571 return -EINVAL;
572
Miao Xie6a038432013-05-15 07:48:24 +0000573 ret = btrfs_start_delalloc_inodes(root, 0);
574 if (ret)
575 return ret;
576
577 btrfs_wait_ordered_extents(root, 0);
578
Yan, Zhenga22285a2010-05-16 10:48:46 -0400579 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
580 if (!pending_snapshot)
581 return -ENOMEM;
582
Miao Xie66d8f3d2012-09-06 04:02:28 -0600583 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
584 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000585 /*
586 * 1 - parent dir inode
587 * 2 - dir entries
588 * 1 - root item
589 * 2 - root ref/backref
590 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200591 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000592 */
593 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200594 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400595 &pending_snapshot->qgroup_reserved,
596 false);
Miao Xied5c12072013-02-28 10:04:33 +0000597 if (ret)
598 goto out;
599
Yan, Zhenga22285a2010-05-16 10:48:46 -0400600 pending_snapshot->dentry = dentry;
601 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800602 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000603 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000604 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400605
Miao Xied5c12072013-02-28 10:04:33 +0000606 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400607 if (IS_ERR(trans)) {
608 ret = PTR_ERR(trans);
609 goto fail;
610 }
611
Josef Bacik83515832011-06-14 15:16:14 -0400612 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400613 list_add(&pending_snapshot->list,
614 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400615 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400616 if (async_transid) {
617 *async_transid = trans->transid;
618 ret = btrfs_commit_transaction_async(trans,
619 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000620 if (ret)
621 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400622 } else {
623 ret = btrfs_commit_transaction(trans,
624 root->fs_info->extent_root);
625 }
Miao Xieaec80302013-03-04 09:44:29 +0000626 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400627 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400628
629 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000631 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500633 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
634 if (ret)
635 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400636
Al Viro2fbe8c82011-07-16 21:38:06 -0400637 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000638 if (IS_ERR(inode)) {
639 ret = PTR_ERR(inode);
640 goto fail;
641 }
642 BUG_ON(!inode);
643 d_instantiate(dentry, inode);
644 ret = 0;
645fail:
Miao Xied5c12072013-02-28 10:04:33 +0000646 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
647 &pending_snapshot->block_rsv,
648 pending_snapshot->qgroup_reserved);
649out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400650 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651 return ret;
652}
653
Sage Weil4260f7c2010-10-29 15:46:43 -0400654/* copy of check_sticky in fs/namei.c()
655* It's inline, so penalty for filesystems that don't use sticky bit is
656* minimal.
657*/
658static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
659{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800660 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400661
662 if (!(dir->i_mode & S_ISVTX))
663 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800664 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400665 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800666 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400667 return 0;
668 return !capable(CAP_FOWNER);
669}
670
671/* copy of may_delete in fs/namei.c()
672 * Check whether we can remove a link victim from directory dir, check
673 * whether the type of victim is right.
674 * 1. We can't do it if dir is read-only (done in permission())
675 * 2. We should have write and exec permissions on dir
676 * 3. We can't remove anything from append-only dir
677 * 4. We can't do anything with immutable dir (done in permission())
678 * 5. If the sticky bit on dir is set we should either
679 * a. be owner of dir, or
680 * b. be owner of victim, or
681 * c. have CAP_FOWNER capability
682 * 6. If the victim is append-only or immutable we can't do antyhing with
683 * links pointing to it.
684 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
685 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
686 * 9. We can't remove a root or mountpoint.
687 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
688 * nfs_async_unlink().
689 */
690
691static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
692{
693 int error;
694
695 if (!victim->d_inode)
696 return -ENOENT;
697
698 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400699 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400700
701 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
702 if (error)
703 return error;
704 if (IS_APPEND(dir))
705 return -EPERM;
706 if (btrfs_check_sticky(dir, victim->d_inode)||
707 IS_APPEND(victim->d_inode)||
708 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
709 return -EPERM;
710 if (isdir) {
711 if (!S_ISDIR(victim->d_inode->i_mode))
712 return -ENOTDIR;
713 if (IS_ROOT(victim))
714 return -EBUSY;
715 } else if (S_ISDIR(victim->d_inode->i_mode))
716 return -EISDIR;
717 if (IS_DEADDIR(dir))
718 return -ENOENT;
719 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
720 return -EBUSY;
721 return 0;
722}
723
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400724/* copy of may_create in fs/namei.c() */
725static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
726{
727 if (child->d_inode)
728 return -EEXIST;
729 if (IS_DEADDIR(dir))
730 return -ENOENT;
731 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
732}
733
734/*
735 * Create a new subvolume below @parent. This is largely modeled after
736 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
737 * inside this filesystem so it's quite a bit simpler.
738 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400739static noinline int btrfs_mksubvol(struct path *parent,
740 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400741 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200742 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000743 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400744{
Yan, Zheng76dda932009-09-21 16:00:26 -0400745 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400746 struct dentry *dentry;
747 int error;
748
David Sterba5c50c9b2013-03-22 18:12:51 +0000749 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
750 if (error == -EINTR)
751 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400752
753 dentry = lookup_one_len(name, parent->dentry, namelen);
754 error = PTR_ERR(dentry);
755 if (IS_ERR(dentry))
756 goto out_unlock;
757
758 error = -EEXIST;
759 if (dentry->d_inode)
760 goto out_dput;
761
Yan, Zheng76dda932009-09-21 16:00:26 -0400762 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400763 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600764 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400765
Chris Mason9c520572012-12-17 14:26:57 -0500766 /*
767 * even if this name doesn't exist, we may get hash collisions.
768 * check for them now when we can safely fail
769 */
770 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
771 dir->i_ino, name,
772 namelen);
773 if (error)
774 goto out_dput;
775
Yan, Zheng76dda932009-09-21 16:00:26 -0400776 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
777
778 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
779 goto out_up_read;
780
Chris Mason3de45862008-11-17 21:02:50 -0500781 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000782 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200783 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500784 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000785 error = create_subvol(dir, dentry, name, namelen,
786 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500787 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400788 if (!error)
789 fsnotify_mkdir(dir, dentry);
790out_up_read:
791 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400792out_dput:
793 dput(dentry);
794out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400795 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400796 return error;
797}
798
Chris Mason4cb53002011-05-24 15:35:30 -0400799/*
800 * When we're defragging a range, we don't want to kick it off again
801 * if it is really just waiting for delalloc to send it down.
802 * If we find a nice big extent or delalloc range for the bytes in the
803 * file you want to defrag, we return 0 to let you know to skip this
804 * part of the file
805 */
806static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
807{
808 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
809 struct extent_map *em = NULL;
810 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
811 u64 end;
812
813 read_lock(&em_tree->lock);
814 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
815 read_unlock(&em_tree->lock);
816
817 if (em) {
818 end = extent_map_end(em);
819 free_extent_map(em);
820 if (end - offset > thresh)
821 return 0;
822 }
823 /* if we already have a nice delalloc here, just stop */
824 thresh /= 2;
825 end = count_range_bits(io_tree, &offset, offset + thresh,
826 thresh, EXTENT_DELALLOC, 1);
827 if (end >= thresh)
828 return 0;
829 return 1;
830}
831
832/*
833 * helper function to walk through a file and find extents
834 * newer than a specific transid, and smaller than thresh.
835 *
836 * This is used by the defragging code to find new and small
837 * extents
838 */
839static int find_new_extents(struct btrfs_root *root,
840 struct inode *inode, u64 newer_than,
841 u64 *off, int thresh)
842{
843 struct btrfs_path *path;
844 struct btrfs_key min_key;
845 struct btrfs_key max_key;
846 struct extent_buffer *leaf;
847 struct btrfs_file_extent_item *extent;
848 int type;
849 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000850 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400851
852 path = btrfs_alloc_path();
853 if (!path)
854 return -ENOMEM;
855
David Sterbaa4689d22011-05-31 17:08:14 +0000856 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400857 min_key.type = BTRFS_EXTENT_DATA_KEY;
858 min_key.offset = *off;
859
David Sterbaa4689d22011-05-31 17:08:14 +0000860 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400861 max_key.type = (u8)-1;
862 max_key.offset = (u64)-1;
863
864 path->keep_locks = 1;
865
866 while(1) {
867 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +0000868 path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400869 if (ret != 0)
870 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000871 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400872 goto none;
873 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
874 goto none;
875
876 leaf = path->nodes[0];
877 extent = btrfs_item_ptr(leaf, path->slots[0],
878 struct btrfs_file_extent_item);
879
880 type = btrfs_file_extent_type(leaf, extent);
881 if (type == BTRFS_FILE_EXTENT_REG &&
882 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
883 check_defrag_in_cache(inode, min_key.offset, thresh)) {
884 *off = min_key.offset;
885 btrfs_free_path(path);
886 return 0;
887 }
888
889 if (min_key.offset == (u64)-1)
890 goto none;
891
892 min_key.offset++;
893 btrfs_release_path(path);
894 }
895none:
896 btrfs_free_path(path);
897 return -ENOENT;
898}
899
Li Zefan6c282eb2012-06-11 16:03:35 +0800900static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400901{
902 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500903 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800904 struct extent_map *em;
905 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500906
907 /*
908 * hopefully we have this extent in the tree already, try without
909 * the full extent lock
910 */
911 read_lock(&em_tree->lock);
912 em = lookup_extent_mapping(em_tree, start, len);
913 read_unlock(&em_tree->lock);
914
915 if (!em) {
916 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100917 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500918 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100919 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500920
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000921 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800922 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500923 }
924
Li Zefan6c282eb2012-06-11 16:03:35 +0800925 return em;
926}
927
928static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
929{
930 struct extent_map *next;
931 bool ret = true;
932
933 /* this is the last extent */
934 if (em->start + em->len >= i_size_read(inode))
935 return false;
936
937 next = defrag_lookup_extent(inode, em->start + em->len);
938 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
939 ret = false;
940
941 free_extent_map(next);
942 return ret;
943}
944
945static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400946 u64 *last_len, u64 *skip, u64 *defrag_end,
947 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800948{
949 struct extent_map *em;
950 int ret = 1;
951 bool next_mergeable = true;
952
953 /*
954 * make sure that once we start defragging an extent, we keep on
955 * defragging it
956 */
957 if (start < *defrag_end)
958 return 1;
959
960 *skip = 0;
961
962 em = defrag_lookup_extent(inode, start);
963 if (!em)
964 return 0;
965
Chris Mason940100a2010-03-10 10:52:59 -0500966 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400967 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500968 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400969 goto out;
970 }
971
Li Zefan6c282eb2012-06-11 16:03:35 +0800972 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500973
974 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800975 * we hit a real extent, if it is big or the next extent is not a
976 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500977 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400978 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800979 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500980 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400981out:
Chris Mason940100a2010-03-10 10:52:59 -0500982 /*
983 * last_len ends up being a counter of how many bytes we've defragged.
984 * every time we choose not to defrag an extent, we reset *last_len
985 * so that the next tiny extent will force a defrag.
986 *
987 * The end result of this is that tiny extents before a single big
988 * extent will force at least part of that big extent to be defragged.
989 */
990 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500991 *defrag_end = extent_map_end(em);
992 } else {
993 *last_len = 0;
994 *skip = extent_map_end(em);
995 *defrag_end = 0;
996 }
997
998 free_extent_map(em);
999 return ret;
1000}
1001
Chris Mason4cb53002011-05-24 15:35:30 -04001002/*
1003 * it doesn't do much good to defrag one or two pages
1004 * at a time. This pulls in a nice chunk of pages
1005 * to COW and defrag.
1006 *
1007 * It also makes sure the delalloc code has enough
1008 * dirty data to avoid making new small extents as part
1009 * of the defrag
1010 *
1011 * It's a good idea to start RA on this range
1012 * before calling this.
1013 */
1014static int cluster_pages_for_defrag(struct inode *inode,
1015 struct page **pages,
1016 unsigned long start_index,
1017 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001018{
Chris Mason4cb53002011-05-24 15:35:30 -04001019 unsigned long file_end;
1020 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001021 u64 page_start;
1022 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001023 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001024 int ret;
1025 int i;
1026 int i_done;
1027 struct btrfs_ordered_extent *ordered;
1028 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001029 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001030 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001031
Chris Mason4cb53002011-05-24 15:35:30 -04001032 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001033 if (!isize || start_index > file_end)
1034 return 0;
1035
1036 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001037
1038 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001039 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001040 if (ret)
1041 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001042 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001043 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001044
1045 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001046 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001047 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001048again:
Josef Bacika94733d2011-07-11 10:47:06 -04001049 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001050 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001051 if (!page)
1052 break;
1053
Miao Xie600a45e2012-02-16 15:01:24 +08001054 page_start = page_offset(page);
1055 page_end = page_start + PAGE_CACHE_SIZE - 1;
1056 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001057 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001058 ordered = btrfs_lookup_ordered_extent(inode,
1059 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001060 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001061 if (!ordered)
1062 break;
1063
1064 unlock_page(page);
1065 btrfs_start_ordered_extent(inode, ordered, 1);
1066 btrfs_put_ordered_extent(ordered);
1067 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001068 /*
1069 * we unlocked the page above, so we need check if
1070 * it was released or not.
1071 */
1072 if (page->mapping != inode->i_mapping) {
1073 unlock_page(page);
1074 page_cache_release(page);
1075 goto again;
1076 }
Miao Xie600a45e2012-02-16 15:01:24 +08001077 }
1078
Chris Mason4cb53002011-05-24 15:35:30 -04001079 if (!PageUptodate(page)) {
1080 btrfs_readpage(NULL, page);
1081 lock_page(page);
1082 if (!PageUptodate(page)) {
1083 unlock_page(page);
1084 page_cache_release(page);
1085 ret = -EIO;
1086 break;
1087 }
1088 }
Miao Xie600a45e2012-02-16 15:01:24 +08001089
Miao Xie600a45e2012-02-16 15:01:24 +08001090 if (page->mapping != inode->i_mapping) {
1091 unlock_page(page);
1092 page_cache_release(page);
1093 goto again;
1094 }
1095
Chris Mason4cb53002011-05-24 15:35:30 -04001096 pages[i] = page;
1097 i_done++;
1098 }
1099 if (!i_done || ret)
1100 goto out;
1101
1102 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1103 goto out;
1104
1105 /*
1106 * so now we have a nice long stream of locked
1107 * and up to date pages, lets wait on them
1108 */
1109 for (i = 0; i < i_done; i++)
1110 wait_on_page_writeback(pages[i]);
1111
1112 page_start = page_offset(pages[0]);
1113 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1114
1115 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001116 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001117 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1118 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001119 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1120 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001121
Liu Bo1f12bd02012-03-29 09:57:44 -04001122 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001123 spin_lock(&BTRFS_I(inode)->lock);
1124 BTRFS_I(inode)->outstanding_extents++;
1125 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001126 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001127 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001128 }
1129
1130
Liu Bo9e8a4a82012-09-05 19:10:51 -06001131 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1132 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001133
1134 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1135 page_start, page_end - 1, &cached_state,
1136 GFP_NOFS);
1137
1138 for (i = 0; i < i_done; i++) {
1139 clear_page_dirty_for_io(pages[i]);
1140 ClearPageChecked(pages[i]);
1141 set_page_extent_mapped(pages[i]);
1142 set_page_dirty(pages[i]);
1143 unlock_page(pages[i]);
1144 page_cache_release(pages[i]);
1145 }
1146 return i_done;
1147out:
1148 for (i = 0; i < i_done; i++) {
1149 unlock_page(pages[i]);
1150 page_cache_release(pages[i]);
1151 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001152 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001153 return ret;
1154
1155}
1156
1157int btrfs_defrag_file(struct inode *inode, struct file *file,
1158 struct btrfs_ioctl_defrag_range_args *range,
1159 u64 newer_than, unsigned long max_to_defrag)
1160{
1161 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001162 struct file_ra_state *ra = NULL;
1163 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001164 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001165 u64 last_len = 0;
1166 u64 skip = 0;
1167 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001168 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001169 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001170 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001171 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001172 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001173 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001174 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001175 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1176 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001177 u64 new_align = ~((u64)128 * 1024 - 1);
1178 struct page **pages = NULL;
1179
Liu Bo0abd5b12013-04-16 09:20:28 +00001180 if (isize == 0)
1181 return 0;
1182
1183 if (range->start >= isize)
1184 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001185
1186 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1187 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1188 return -EINVAL;
1189 if (range->compress_type)
1190 compress_type = range->compress_type;
1191 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001192
Liu Bo0abd5b12013-04-16 09:20:28 +00001193 if (extent_thresh == 0)
1194 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001195
Chris Mason4cb53002011-05-24 15:35:30 -04001196 /*
1197 * if we were not given a file, allocate a readahead
1198 * context
1199 */
1200 if (!file) {
1201 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1202 if (!ra)
1203 return -ENOMEM;
1204 file_ra_state_init(ra, inode->i_mapping);
1205 } else {
1206 ra = &file->f_ra;
1207 }
1208
Li Zefan008873e2011-09-02 15:57:07 +08001209 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001210 GFP_NOFS);
1211 if (!pages) {
1212 ret = -ENOMEM;
1213 goto out_ra;
1214 }
1215
1216 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001217 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001218 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001219 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1220 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001221 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001222 }
1223
Chris Mason4cb53002011-05-24 15:35:30 -04001224 if (newer_than) {
1225 ret = find_new_extents(root, inode, newer_than,
1226 &newer_off, 64 * 1024);
1227 if (!ret) {
1228 range->start = newer_off;
1229 /*
1230 * we always align our defrag to help keep
1231 * the extents in the file evenly spaced
1232 */
1233 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001234 } else
1235 goto out_ra;
1236 } else {
1237 i = range->start >> PAGE_CACHE_SHIFT;
1238 }
1239 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001240 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001241
Li Zefan2a0f7f52011-10-10 15:43:34 -04001242 /*
1243 * make writeback starts from i, so the defrag range can be
1244 * written sequentially.
1245 */
1246 if (i < inode->i_mapping->writeback_index)
1247 inode->i_mapping->writeback_index = i;
1248
Chris Masonf7f43cc2011-10-11 11:41:40 -04001249 while (i <= last_index && defrag_count < max_to_defrag &&
1250 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1251 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001252 /*
1253 * make sure we stop running if someone unmounts
1254 * the FS
1255 */
1256 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1257 break;
1258
David Sterba210549e2013-02-09 23:38:06 +00001259 if (btrfs_defrag_cancelled(root->fs_info)) {
1260 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1261 ret = -EAGAIN;
1262 break;
1263 }
1264
Liu Bo66c26892012-03-29 09:57:45 -04001265 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001266 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001267 &defrag_end, range->flags &
1268 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001269 unsigned long next;
1270 /*
1271 * the should_defrag function tells us how much to skip
1272 * bump our counter by the suggested amount
1273 */
1274 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1275 i = max(i + 1, next);
1276 continue;
1277 }
Li Zefan008873e2011-09-02 15:57:07 +08001278
1279 if (!newer_than) {
1280 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1281 PAGE_CACHE_SHIFT) - i;
1282 cluster = min(cluster, max_cluster);
1283 } else {
1284 cluster = max_cluster;
1285 }
1286
Li Zefan008873e2011-09-02 15:57:07 +08001287 if (i + cluster > ra_index) {
1288 ra_index = max(i, ra_index);
1289 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1290 cluster);
1291 ra_index += max_cluster;
1292 }
Chris Mason940100a2010-03-10 10:52:59 -05001293
Liu Boecb8bea2012-03-29 09:57:44 -04001294 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001295 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1296 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001297 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001298 if (ret < 0) {
1299 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001300 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001301 }
Chris Mason940100a2010-03-10 10:52:59 -05001302
Chris Mason4cb53002011-05-24 15:35:30 -04001303 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001304 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001305 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001306
1307 if (newer_than) {
1308 if (newer_off == (u64)-1)
1309 break;
1310
Liu Boe1f041e2012-03-29 09:57:45 -04001311 if (ret > 0)
1312 i += ret;
1313
Chris Mason4cb53002011-05-24 15:35:30 -04001314 newer_off = max(newer_off + 1,
1315 (u64)i << PAGE_CACHE_SHIFT);
1316
1317 ret = find_new_extents(root, inode,
1318 newer_than, &newer_off,
1319 64 * 1024);
1320 if (!ret) {
1321 range->start = newer_off;
1322 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001323 } else {
1324 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001325 }
Chris Mason4cb53002011-05-24 15:35:30 -04001326 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001327 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001328 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001329 last_len += ret << PAGE_CACHE_SHIFT;
1330 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001331 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001332 last_len = 0;
1333 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001334 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001335 }
1336
Chris Mason1e701a32010-03-11 09:42:04 -05001337 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1338 filemap_flush(inode->i_mapping);
1339
1340 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1341 /* the filemap_flush will queue IO into the worker threads, but
1342 * we have to make sure the IO is actually started and that
1343 * ordered extents get created before we return
1344 */
1345 atomic_inc(&root->fs_info->async_submit_draining);
1346 while (atomic_read(&root->fs_info->nr_async_submits) ||
1347 atomic_read(&root->fs_info->async_delalloc_pages)) {
1348 wait_event(root->fs_info->async_submit_wait,
1349 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1350 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1351 }
1352 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001353 }
1354
Li Zefan1a419d82010-10-25 15:12:50 +08001355 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001356 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001357 }
1358
Diego Calleja60ccf822011-09-01 16:33:57 +02001359 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001360
Chris Mason4cb53002011-05-24 15:35:30 -04001361out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001362 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1363 mutex_lock(&inode->i_mutex);
1364 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1365 mutex_unlock(&inode->i_mutex);
1366 }
Chris Mason4cb53002011-05-24 15:35:30 -04001367 if (!file)
1368 kfree(ra);
1369 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001370 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001371}
1372
Miao Xie198605a2012-11-26 08:43:45 +00001373static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001374 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001375{
1376 u64 new_size;
1377 u64 old_size;
1378 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001379 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001380 struct btrfs_ioctl_vol_args *vol_args;
1381 struct btrfs_trans_handle *trans;
1382 struct btrfs_device *device = NULL;
1383 char *sizestr;
1384 char *devstr = NULL;
1385 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001386 int mod = 0;
1387
Chris Masone441d542009-01-05 16:57:23 -05001388 if (!capable(CAP_SYS_ADMIN))
1389 return -EPERM;
1390
Miao Xie198605a2012-11-26 08:43:45 +00001391 ret = mnt_want_write_file(file);
1392 if (ret)
1393 return ret;
1394
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001395 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1396 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001397 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001398 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001399 }
1400
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001401 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001402 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001403 if (IS_ERR(vol_args)) {
1404 ret = PTR_ERR(vol_args);
1405 goto out;
1406 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001407
1408 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001410 sizestr = vol_args->name;
1411 devstr = strchr(sizestr, ':');
1412 if (devstr) {
1413 char *end;
1414 sizestr = devstr + 1;
1415 *devstr = '\0';
1416 devstr = vol_args->name;
1417 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001418 if (!devid) {
1419 ret = -EINVAL;
1420 goto out_free;
1421 }
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001422 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423 }
Miao Xiedba60f32012-12-21 09:19:51 +00001424
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001425 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001426 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001427 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001428 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001429 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001430 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431 }
Miao Xiedba60f32012-12-21 09:19:51 +00001432
1433 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001434 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001435 "readonly device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001436 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001437 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001438 goto out_free;
1439 }
1440
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001441 if (!strcmp(sizestr, "max"))
1442 new_size = device->bdev->bd_inode->i_size;
1443 else {
1444 if (sizestr[0] == '-') {
1445 mod = -1;
1446 sizestr++;
1447 } else if (sizestr[0] == '+') {
1448 mod = 1;
1449 sizestr++;
1450 }
Akinobu Mita91748462010-02-28 10:59:11 +00001451 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001452 if (new_size == 0) {
1453 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001454 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455 }
1456 }
1457
Stefan Behrens63a212a2012-11-05 18:29:28 +01001458 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001459 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001460 goto out_free;
1461 }
1462
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463 old_size = device->total_bytes;
1464
1465 if (mod < 0) {
1466 if (new_size > old_size) {
1467 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001468 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001469 }
1470 new_size = old_size - new_size;
1471 } else if (mod > 0) {
1472 new_size = old_size + new_size;
1473 }
1474
1475 if (new_size < 256 * 1024 * 1024) {
1476 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001477 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001478 }
1479 if (new_size > device->bdev->bd_inode->i_size) {
1480 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001481 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001482 }
1483
1484 do_div(new_size, root->sectorsize);
1485 new_size *= root->sectorsize;
1486
Josef Bacik606686e2012-06-04 14:03:51 -04001487 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001488 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001489
1490 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001491 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001492 if (IS_ERR(trans)) {
1493 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001494 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001495 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001496 ret = btrfs_grow_device(trans, device, new_size);
1497 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001498 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001499 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001500 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001501
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001502out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001504out:
1505 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001506 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001507 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001508 return ret;
1509}
1510
Sage Weil72fd0322010-10-29 15:41:32 -04001511static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001512 char *name, unsigned long fd, int subvol,
1513 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001514 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001515{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001516 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001517 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001518
Liu Boa874a632012-06-29 03:58:46 -06001519 ret = mnt_want_write_file(file);
1520 if (ret)
1521 goto out;
1522
Sage Weil72fd0322010-10-29 15:41:32 -04001523 namelen = strlen(name);
1524 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001526 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001527 }
1528
Chris Mason16780ca2012-02-20 22:14:55 -05001529 if (name[0] == '.' &&
1530 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1531 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001532 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001533 }
1534
Chris Mason3de45862008-11-17 21:02:50 -05001535 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001536 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001537 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001538 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001539 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001540 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001541 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001542 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001543 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001544 }
1545
Al Viro496ad9a2013-01-23 17:07:38 -05001546 src_inode = file_inode(src.file);
1547 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001548 printk(KERN_INFO "btrfs: Snapshot src from "
1549 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001550 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001551 } else {
1552 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1553 BTRFS_I(src_inode)->root,
1554 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001555 }
Al Viro2903ff02012-08-28 12:52:22 -04001556 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001557 }
Liu Boa874a632012-06-29 03:58:46 -06001558out_drop_write:
1559 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001560out:
Sage Weil72fd0322010-10-29 15:41:32 -04001561 return ret;
1562}
1563
1564static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001565 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001566{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001567 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001568 int ret;
1569
Li Zefanfa0d2b92010-12-20 15:53:28 +08001570 vol_args = memdup_user(arg, sizeof(*vol_args));
1571 if (IS_ERR(vol_args))
1572 return PTR_ERR(vol_args);
1573 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001574
Li Zefanfa0d2b92010-12-20 15:53:28 +08001575 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001576 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001577 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001578
Li Zefanfa0d2b92010-12-20 15:53:28 +08001579 kfree(vol_args);
1580 return ret;
1581}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001582
Li Zefanfa0d2b92010-12-20 15:53:28 +08001583static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1584 void __user *arg, int subvol)
1585{
1586 struct btrfs_ioctl_vol_args_v2 *vol_args;
1587 int ret;
1588 u64 transid = 0;
1589 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001590 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001591 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001592
Li Zefanfa0d2b92010-12-20 15:53:28 +08001593 vol_args = memdup_user(arg, sizeof(*vol_args));
1594 if (IS_ERR(vol_args))
1595 return PTR_ERR(vol_args);
1596 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001597
Li Zefanb83cc962010-12-20 16:04:08 +08001598 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001599 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1600 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001601 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001602 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001603 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001604
1605 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1606 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001607 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1608 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001609 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1610 if (vol_args->size > PAGE_CACHE_SIZE) {
1611 ret = -EINVAL;
1612 goto out;
1613 }
1614 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1615 if (IS_ERR(inherit)) {
1616 ret = PTR_ERR(inherit);
1617 goto out;
1618 }
1619 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001620
1621 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001622 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001623 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001624
1625 if (ret == 0 && ptr &&
1626 copy_to_user(arg +
1627 offsetof(struct btrfs_ioctl_vol_args_v2,
1628 transid), ptr, sizeof(*ptr)))
1629 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001630out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001631 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001632 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 return ret;
1634}
1635
Li Zefan0caa1022010-12-20 16:30:25 +08001636static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1637 void __user *arg)
1638{
Al Viro496ad9a2013-01-23 17:07:38 -05001639 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001640 struct btrfs_root *root = BTRFS_I(inode)->root;
1641 int ret = 0;
1642 u64 flags = 0;
1643
Li Zefan33345d012011-04-20 10:31:50 +08001644 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001645 return -EINVAL;
1646
1647 down_read(&root->fs_info->subvol_sem);
1648 if (btrfs_root_readonly(root))
1649 flags |= BTRFS_SUBVOL_RDONLY;
1650 up_read(&root->fs_info->subvol_sem);
1651
1652 if (copy_to_user(arg, &flags, sizeof(flags)))
1653 ret = -EFAULT;
1654
1655 return ret;
1656}
1657
1658static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1659 void __user *arg)
1660{
Al Viro496ad9a2013-01-23 17:07:38 -05001661 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001662 struct btrfs_root *root = BTRFS_I(inode)->root;
1663 struct btrfs_trans_handle *trans;
1664 u64 root_flags;
1665 u64 flags;
1666 int ret = 0;
1667
Liu Bob9ca0662012-06-29 03:58:49 -06001668 ret = mnt_want_write_file(file);
1669 if (ret)
1670 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001671
Liu Bob9ca0662012-06-29 03:58:49 -06001672 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1673 ret = -EINVAL;
1674 goto out_drop_write;
1675 }
Li Zefan0caa1022010-12-20 16:30:25 +08001676
Liu Bob9ca0662012-06-29 03:58:49 -06001677 if (copy_from_user(&flags, arg, sizeof(flags))) {
1678 ret = -EFAULT;
1679 goto out_drop_write;
1680 }
Li Zefan0caa1022010-12-20 16:30:25 +08001681
Liu Bob9ca0662012-06-29 03:58:49 -06001682 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1683 ret = -EINVAL;
1684 goto out_drop_write;
1685 }
Li Zefan0caa1022010-12-20 16:30:25 +08001686
Liu Bob9ca0662012-06-29 03:58:49 -06001687 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1688 ret = -EOPNOTSUPP;
1689 goto out_drop_write;
1690 }
Li Zefan0caa1022010-12-20 16:30:25 +08001691
Liu Bob9ca0662012-06-29 03:58:49 -06001692 if (!inode_owner_or_capable(inode)) {
1693 ret = -EACCES;
1694 goto out_drop_write;
1695 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001696
Li Zefan0caa1022010-12-20 16:30:25 +08001697 down_write(&root->fs_info->subvol_sem);
1698
1699 /* nothing to do */
1700 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001701 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001702
1703 root_flags = btrfs_root_flags(&root->root_item);
1704 if (flags & BTRFS_SUBVOL_RDONLY)
1705 btrfs_set_root_flags(&root->root_item,
1706 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1707 else
1708 btrfs_set_root_flags(&root->root_item,
1709 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1710
1711 trans = btrfs_start_transaction(root, 1);
1712 if (IS_ERR(trans)) {
1713 ret = PTR_ERR(trans);
1714 goto out_reset;
1715 }
1716
Li Zefanb4dc2b82011-02-16 06:06:34 +00001717 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001718 &root->root_key, &root->root_item);
1719
1720 btrfs_commit_transaction(trans, root);
1721out_reset:
1722 if (ret)
1723 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001724out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001725 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001726out_drop_write:
1727 mnt_drop_write_file(file);
1728out:
Li Zefan0caa1022010-12-20 16:30:25 +08001729 return ret;
1730}
1731
Yan, Zheng76dda932009-09-21 16:00:26 -04001732/*
1733 * helper to check if the subvolume references other subvolumes
1734 */
1735static noinline int may_destroy_subvol(struct btrfs_root *root)
1736{
1737 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001738 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001739 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001740 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001741 int ret;
1742
1743 path = btrfs_alloc_path();
1744 if (!path)
1745 return -ENOMEM;
1746
Josef Bacik175a2b82013-08-12 15:36:44 -04001747 /* Make sure this root isn't set as the default subvol */
1748 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1749 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1750 dir_id, "default", 7, 0);
1751 if (di && !IS_ERR(di)) {
1752 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1753 if (key.objectid == root->root_key.objectid) {
1754 ret = -ENOTEMPTY;
1755 goto out;
1756 }
1757 btrfs_release_path(path);
1758 }
1759
Yan, Zheng76dda932009-09-21 16:00:26 -04001760 key.objectid = root->root_key.objectid;
1761 key.type = BTRFS_ROOT_REF_KEY;
1762 key.offset = (u64)-1;
1763
1764 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1765 &key, path, 0, 0);
1766 if (ret < 0)
1767 goto out;
1768 BUG_ON(ret == 0);
1769
1770 ret = 0;
1771 if (path->slots[0] > 0) {
1772 path->slots[0]--;
1773 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1774 if (key.objectid == root->root_key.objectid &&
1775 key.type == BTRFS_ROOT_REF_KEY)
1776 ret = -ENOTEMPTY;
1777 }
1778out:
1779 btrfs_free_path(path);
1780 return ret;
1781}
1782
Chris Masonac8e9812010-02-28 15:39:26 -05001783static noinline int key_in_sk(struct btrfs_key *key,
1784 struct btrfs_ioctl_search_key *sk)
1785{
Chris Masonabc6e132010-03-18 12:10:08 -04001786 struct btrfs_key test;
1787 int ret;
1788
1789 test.objectid = sk->min_objectid;
1790 test.type = sk->min_type;
1791 test.offset = sk->min_offset;
1792
1793 ret = btrfs_comp_cpu_keys(key, &test);
1794 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001795 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001796
1797 test.objectid = sk->max_objectid;
1798 test.type = sk->max_type;
1799 test.offset = sk->max_offset;
1800
1801 ret = btrfs_comp_cpu_keys(key, &test);
1802 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001803 return 0;
1804 return 1;
1805}
1806
1807static noinline int copy_to_sk(struct btrfs_root *root,
1808 struct btrfs_path *path,
1809 struct btrfs_key *key,
1810 struct btrfs_ioctl_search_key *sk,
1811 char *buf,
1812 unsigned long *sk_offset,
1813 int *num_found)
1814{
1815 u64 found_transid;
1816 struct extent_buffer *leaf;
1817 struct btrfs_ioctl_search_header sh;
1818 unsigned long item_off;
1819 unsigned long item_len;
1820 int nritems;
1821 int i;
1822 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001823 int ret = 0;
1824
1825 leaf = path->nodes[0];
1826 slot = path->slots[0];
1827 nritems = btrfs_header_nritems(leaf);
1828
1829 if (btrfs_header_generation(leaf) > sk->max_transid) {
1830 i = nritems;
1831 goto advance_key;
1832 }
1833 found_transid = btrfs_header_generation(leaf);
1834
1835 for (i = slot; i < nritems; i++) {
1836 item_off = btrfs_item_ptr_offset(leaf, i);
1837 item_len = btrfs_item_size_nr(leaf, i);
1838
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001839 btrfs_item_key_to_cpu(leaf, key, i);
1840 if (!key_in_sk(key, sk))
1841 continue;
1842
1843 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001844 item_len = 0;
1845
1846 if (sizeof(sh) + item_len + *sk_offset >
1847 BTRFS_SEARCH_ARGS_BUFSIZE) {
1848 ret = 1;
1849 goto overflow;
1850 }
1851
Chris Masonac8e9812010-02-28 15:39:26 -05001852 sh.objectid = key->objectid;
1853 sh.offset = key->offset;
1854 sh.type = key->type;
1855 sh.len = item_len;
1856 sh.transid = found_transid;
1857
1858 /* copy search result header */
1859 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1860 *sk_offset += sizeof(sh);
1861
1862 if (item_len) {
1863 char *p = buf + *sk_offset;
1864 /* copy the item */
1865 read_extent_buffer(leaf, p,
1866 item_off, item_len);
1867 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001868 }
Hugo Millse2156862011-05-14 17:43:41 +00001869 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001870
1871 if (*num_found >= sk->nr_items)
1872 break;
1873 }
1874advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001875 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001876 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1877 key->offset++;
1878 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1879 key->offset = 0;
1880 key->type++;
1881 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1882 key->offset = 0;
1883 key->type = 0;
1884 key->objectid++;
1885 } else
1886 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001887overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001888 return ret;
1889}
1890
1891static noinline int search_ioctl(struct inode *inode,
1892 struct btrfs_ioctl_search_args *args)
1893{
1894 struct btrfs_root *root;
1895 struct btrfs_key key;
1896 struct btrfs_key max_key;
1897 struct btrfs_path *path;
1898 struct btrfs_ioctl_search_key *sk = &args->key;
1899 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1900 int ret;
1901 int num_found = 0;
1902 unsigned long sk_offset = 0;
1903
1904 path = btrfs_alloc_path();
1905 if (!path)
1906 return -ENOMEM;
1907
1908 if (sk->tree_id == 0) {
1909 /* search the root of the inode that was passed */
1910 root = BTRFS_I(inode)->root;
1911 } else {
1912 key.objectid = sk->tree_id;
1913 key.type = BTRFS_ROOT_ITEM_KEY;
1914 key.offset = (u64)-1;
1915 root = btrfs_read_fs_root_no_name(info, &key);
1916 if (IS_ERR(root)) {
1917 printk(KERN_ERR "could not find root %llu\n",
1918 sk->tree_id);
1919 btrfs_free_path(path);
1920 return -ENOENT;
1921 }
1922 }
1923
1924 key.objectid = sk->min_objectid;
1925 key.type = sk->min_type;
1926 key.offset = sk->min_offset;
1927
1928 max_key.objectid = sk->max_objectid;
1929 max_key.type = sk->max_type;
1930 max_key.offset = sk->max_offset;
1931
1932 path->keep_locks = 1;
1933
1934 while(1) {
Eric Sandeende78b512013-01-31 18:21:12 +00001935 ret = btrfs_search_forward(root, &key, &max_key, path,
Chris Masonac8e9812010-02-28 15:39:26 -05001936 sk->min_transid);
1937 if (ret != 0) {
1938 if (ret > 0)
1939 ret = 0;
1940 goto err;
1941 }
1942 ret = copy_to_sk(root, path, &key, sk, args->buf,
1943 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001944 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001945 if (ret || num_found >= sk->nr_items)
1946 break;
1947
1948 }
1949 ret = 0;
1950err:
1951 sk->nr_items = num_found;
1952 btrfs_free_path(path);
1953 return ret;
1954}
1955
1956static noinline int btrfs_ioctl_tree_search(struct file *file,
1957 void __user *argp)
1958{
1959 struct btrfs_ioctl_search_args *args;
1960 struct inode *inode;
1961 int ret;
1962
1963 if (!capable(CAP_SYS_ADMIN))
1964 return -EPERM;
1965
Julia Lawall2354d082010-10-29 15:14:18 -04001966 args = memdup_user(argp, sizeof(*args));
1967 if (IS_ERR(args))
1968 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001969
Al Viro496ad9a2013-01-23 17:07:38 -05001970 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001971 ret = search_ioctl(inode, args);
1972 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1973 ret = -EFAULT;
1974 kfree(args);
1975 return ret;
1976}
1977
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001978/*
Chris Masonac8e9812010-02-28 15:39:26 -05001979 * Search INODE_REFs to identify path name of 'dirid' directory
1980 * in a 'tree_id' tree. and sets path name to 'name'.
1981 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001982static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1983 u64 tree_id, u64 dirid, char *name)
1984{
1985 struct btrfs_root *root;
1986 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001987 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001988 int ret = -1;
1989 int slot;
1990 int len;
1991 int total_len = 0;
1992 struct btrfs_inode_ref *iref;
1993 struct extent_buffer *l;
1994 struct btrfs_path *path;
1995
1996 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1997 name[0]='\0';
1998 return 0;
1999 }
2000
2001 path = btrfs_alloc_path();
2002 if (!path)
2003 return -ENOMEM;
2004
Chris Masonac8e9812010-02-28 15:39:26 -05002005 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002006
2007 key.objectid = tree_id;
2008 key.type = BTRFS_ROOT_ITEM_KEY;
2009 key.offset = (u64)-1;
2010 root = btrfs_read_fs_root_no_name(info, &key);
2011 if (IS_ERR(root)) {
2012 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002013 ret = -ENOENT;
2014 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002015 }
2016
2017 key.objectid = dirid;
2018 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002019 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002020
2021 while(1) {
2022 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2023 if (ret < 0)
2024 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002025 else if (ret > 0) {
2026 ret = btrfs_previous_item(root, path, dirid,
2027 BTRFS_INODE_REF_KEY);
2028 if (ret < 0)
2029 goto out;
2030 else if (ret > 0) {
2031 ret = -ENOENT;
2032 goto out;
2033 }
2034 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002035
2036 l = path->nodes[0];
2037 slot = path->slots[0];
2038 btrfs_item_key_to_cpu(l, &key, slot);
2039
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002040 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2041 len = btrfs_inode_ref_name_len(l, iref);
2042 ptr -= len + 1;
2043 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002044 if (ptr < name) {
2045 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002046 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002047 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002048
2049 *(ptr + len) = '/';
2050 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2051
2052 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2053 break;
2054
David Sterbab3b4aa72011-04-21 01:20:15 +02002055 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002056 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002057 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002058 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002059 }
Li Zefan77906a502011-07-14 03:16:00 +00002060 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002061 name[total_len]='\0';
2062 ret = 0;
2063out:
2064 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002065 return ret;
2066}
2067
2068static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2069 void __user *argp)
2070{
2071 struct btrfs_ioctl_ino_lookup_args *args;
2072 struct inode *inode;
2073 int ret;
2074
2075 if (!capable(CAP_SYS_ADMIN))
2076 return -EPERM;
2077
Julia Lawall2354d082010-10-29 15:14:18 -04002078 args = memdup_user(argp, sizeof(*args));
2079 if (IS_ERR(args))
2080 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002081
Al Viro496ad9a2013-01-23 17:07:38 -05002082 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002083
Chris Mason1b53ac42010-03-18 12:17:05 -04002084 if (args->treeid == 0)
2085 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2086
Chris Masonac8e9812010-02-28 15:39:26 -05002087 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2088 args->treeid, args->objectid,
2089 args->name);
2090
2091 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2092 ret = -EFAULT;
2093
2094 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002095 return ret;
2096}
2097
Yan, Zheng76dda932009-09-21 16:00:26 -04002098static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2099 void __user *arg)
2100{
2101 struct dentry *parent = fdentry(file);
2102 struct dentry *dentry;
2103 struct inode *dir = parent->d_inode;
2104 struct inode *inode;
2105 struct btrfs_root *root = BTRFS_I(dir)->root;
2106 struct btrfs_root *dest = NULL;
2107 struct btrfs_ioctl_vol_args *vol_args;
2108 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002109 struct btrfs_block_rsv block_rsv;
2110 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002111 int namelen;
2112 int ret;
2113 int err = 0;
2114
Yan, Zheng76dda932009-09-21 16:00:26 -04002115 vol_args = memdup_user(arg, sizeof(*vol_args));
2116 if (IS_ERR(vol_args))
2117 return PTR_ERR(vol_args);
2118
2119 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2120 namelen = strlen(vol_args->name);
2121 if (strchr(vol_args->name, '/') ||
2122 strncmp(vol_args->name, "..", namelen) == 0) {
2123 err = -EINVAL;
2124 goto out;
2125 }
2126
Al Viroa561be72011-11-23 11:57:51 -05002127 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002128 if (err)
2129 goto out;
2130
David Sterba5c50c9b2013-03-22 18:12:51 +00002131 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2132 if (err == -EINTR)
2133 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002134 dentry = lookup_one_len(vol_args->name, parent, namelen);
2135 if (IS_ERR(dentry)) {
2136 err = PTR_ERR(dentry);
2137 goto out_unlock_dir;
2138 }
2139
2140 if (!dentry->d_inode) {
2141 err = -ENOENT;
2142 goto out_dput;
2143 }
2144
2145 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002146 dest = BTRFS_I(inode)->root;
2147 if (!capable(CAP_SYS_ADMIN)){
2148 /*
2149 * Regular user. Only allow this with a special mount
2150 * option, when the user has write+exec access to the
2151 * subvol root, and when rmdir(2) would have been
2152 * allowed.
2153 *
2154 * Note that this is _not_ check that the subvol is
2155 * empty or doesn't contain data that we wouldn't
2156 * otherwise be able to delete.
2157 *
2158 * Users who want to delete empty subvols should try
2159 * rmdir(2).
2160 */
2161 err = -EPERM;
2162 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2163 goto out_dput;
2164
2165 /*
2166 * Do not allow deletion if the parent dir is the same
2167 * as the dir to be deleted. That means the ioctl
2168 * must be called on the dentry referencing the root
2169 * of the subvol, not a random directory contained
2170 * within it.
2171 */
2172 err = -EINVAL;
2173 if (root == dest)
2174 goto out_dput;
2175
2176 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2177 if (err)
2178 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002179 }
2180
Miao Xie5c39da52012-10-22 11:39:53 +00002181 /* check if subvolume may be deleted by a user */
2182 err = btrfs_may_delete(dir, dentry, 1);
2183 if (err)
2184 goto out_dput;
2185
Li Zefan33345d012011-04-20 10:31:50 +08002186 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002187 err = -EINVAL;
2188 goto out_dput;
2189 }
2190
Yan, Zheng76dda932009-09-21 16:00:26 -04002191 mutex_lock(&inode->i_mutex);
2192 err = d_invalidate(dentry);
2193 if (err)
2194 goto out_unlock;
2195
2196 down_write(&root->fs_info->subvol_sem);
2197
2198 err = may_destroy_subvol(dest);
2199 if (err)
2200 goto out_up_write;
2201
Miao Xiec58aaad2013-02-28 10:05:36 +00002202 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2203 /*
2204 * One for dir inode, two for dir entries, two for root
2205 * ref/backref.
2206 */
2207 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002208 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002209 if (err)
2210 goto out_up_write;
2211
Yan, Zhenga22285a2010-05-16 10:48:46 -04002212 trans = btrfs_start_transaction(root, 0);
2213 if (IS_ERR(trans)) {
2214 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002215 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002216 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002217 trans->block_rsv = &block_rsv;
2218 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002219
Yan, Zheng76dda932009-09-21 16:00:26 -04002220 ret = btrfs_unlink_subvol(trans, root, dir,
2221 dest->root_key.objectid,
2222 dentry->d_name.name,
2223 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002224 if (ret) {
2225 err = ret;
2226 btrfs_abort_transaction(trans, root, ret);
2227 goto out_end_trans;
2228 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002229
2230 btrfs_record_root_in_trans(trans, dest);
2231
2232 memset(&dest->root_item.drop_progress, 0,
2233 sizeof(dest->root_item.drop_progress));
2234 dest->root_item.drop_level = 0;
2235 btrfs_set_root_refs(&dest->root_item, 0);
2236
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 if (!xchg(&dest->orphan_item_inserted, 1)) {
2238 ret = btrfs_insert_orphan_item(trans,
2239 root->fs_info->tree_root,
2240 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002241 if (ret) {
2242 btrfs_abort_transaction(trans, root, ret);
2243 err = ret;
2244 goto out_end_trans;
2245 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002246 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002247
2248 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2249 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2250 dest->root_key.objectid);
2251 if (ret && ret != -ENOENT) {
2252 btrfs_abort_transaction(trans, root, ret);
2253 err = ret;
2254 goto out_end_trans;
2255 }
2256 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2257 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2258 dest->root_item.received_uuid,
2259 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2260 dest->root_key.objectid);
2261 if (ret && ret != -ENOENT) {
2262 btrfs_abort_transaction(trans, root, ret);
2263 err = ret;
2264 goto out_end_trans;
2265 }
2266 }
2267
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002268out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002269 trans->block_rsv = NULL;
2270 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002271 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272 if (ret && !err)
2273 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002274 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002275out_release:
2276 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002277out_up_write:
2278 up_write(&root->fs_info->subvol_sem);
2279out_unlock:
2280 mutex_unlock(&inode->i_mutex);
2281 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002282 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002283 btrfs_invalidate_inodes(dest);
2284 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002285
2286 /* the last ref */
2287 if (dest->cache_inode) {
2288 iput(dest->cache_inode);
2289 dest->cache_inode = NULL;
2290 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002291 }
2292out_dput:
2293 dput(dentry);
2294out_unlock_dir:
2295 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002296 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002297out:
2298 kfree(vol_args);
2299 return err;
2300}
2301
Chris Mason1e701a32010-03-11 09:42:04 -05002302static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002303{
Al Viro496ad9a2013-01-23 17:07:38 -05002304 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002305 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002306 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002307 int ret;
2308
Ilya Dryomov25122d12013-01-20 15:57:57 +02002309 ret = mnt_want_write_file(file);
2310 if (ret)
2311 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002312
Ilya Dryomov25122d12013-01-20 15:57:57 +02002313 if (btrfs_root_readonly(root)) {
2314 ret = -EROFS;
2315 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002316 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002317
2318 switch (inode->i_mode & S_IFMT) {
2319 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002320 if (!capable(CAP_SYS_ADMIN)) {
2321 ret = -EPERM;
2322 goto out;
2323 }
Eric Sandeende78b512013-01-31 18:21:12 +00002324 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002325 if (ret)
2326 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002327 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002328 break;
2329 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002330 if (!(file->f_mode & FMODE_WRITE)) {
2331 ret = -EINVAL;
2332 goto out;
2333 }
Chris Mason1e701a32010-03-11 09:42:04 -05002334
2335 range = kzalloc(sizeof(*range), GFP_KERNEL);
2336 if (!range) {
2337 ret = -ENOMEM;
2338 goto out;
2339 }
2340
2341 if (argp) {
2342 if (copy_from_user(range, argp,
2343 sizeof(*range))) {
2344 ret = -EFAULT;
2345 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002346 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002347 }
2348 /* compression requires us to start the IO */
2349 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2350 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2351 range->extent_thresh = (u32)-1;
2352 }
2353 } else {
2354 /* the rest are all set to zero by kzalloc */
2355 range->len = (u64)-1;
2356 }
Al Viro496ad9a2013-01-23 17:07:38 -05002357 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002358 range, 0, 0);
2359 if (ret > 0)
2360 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002361 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002362 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002363 default:
2364 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002365 }
Chris Masone441d542009-01-05 16:57:23 -05002366out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002367 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002368 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002369}
2370
Christoph Hellwigb2950862008-12-02 09:54:17 -05002371static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002372{
2373 struct btrfs_ioctl_vol_args *vol_args;
2374 int ret;
2375
Chris Masone441d542009-01-05 16:57:23 -05002376 if (!capable(CAP_SYS_ADMIN))
2377 return -EPERM;
2378
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002379 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2380 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002381 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002382 }
2383
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002384 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002385 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002386 if (IS_ERR(vol_args)) {
2387 ret = PTR_ERR(vol_args);
2388 goto out;
2389 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390
Mark Fasheh5516e592008-07-24 12:20:14 -04002391 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002392 ret = btrfs_init_new_device(root, vol_args->name);
2393
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002394 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002395out:
2396 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002397 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002398 return ret;
2399}
2400
Miao Xieda249272012-11-26 08:44:50 +00002401static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002402{
Al Viro496ad9a2013-01-23 17:07:38 -05002403 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002404 struct btrfs_ioctl_vol_args *vol_args;
2405 int ret;
2406
Chris Masone441d542009-01-05 16:57:23 -05002407 if (!capable(CAP_SYS_ADMIN))
2408 return -EPERM;
2409
Miao Xieda249272012-11-26 08:44:50 +00002410 ret = mnt_want_write_file(file);
2411 if (ret)
2412 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002413
Li Zefandae7b662009-04-08 15:06:54 +08002414 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002415 if (IS_ERR(vol_args)) {
2416 ret = PTR_ERR(vol_args);
2417 goto out;
2418 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002419
Mark Fasheh5516e592008-07-24 12:20:14 -04002420 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002421
Anand Jain183860f2013-05-17 10:52:45 +00002422 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2423 1)) {
2424 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2425 goto out;
2426 }
2427
2428 mutex_lock(&root->fs_info->volume_mutex);
2429 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002430 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002431 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002432
2433out:
2434 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002435 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002436 return ret;
2437}
2438
Jan Schmidt475f6382011-03-11 15:41:01 +01002439static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2440{
Li Zefan027ed2f2011-06-08 08:27:56 +00002441 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002442 struct btrfs_device *device;
2443 struct btrfs_device *next;
2444 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002445 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002446
2447 if (!capable(CAP_SYS_ADMIN))
2448 return -EPERM;
2449
Li Zefan027ed2f2011-06-08 08:27:56 +00002450 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2451 if (!fi_args)
2452 return -ENOMEM;
2453
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002454 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002455 fi_args->num_devices = fs_devices->num_devices;
2456 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002457
Jan Schmidt475f6382011-03-11 15:41:01 +01002458 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002459 if (device->devid > fi_args->max_id)
2460 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002461 }
2462 mutex_unlock(&fs_devices->device_list_mutex);
2463
Li Zefan027ed2f2011-06-08 08:27:56 +00002464 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2465 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002466
Li Zefan027ed2f2011-06-08 08:27:56 +00002467 kfree(fi_args);
2468 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002469}
2470
2471static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2472{
2473 struct btrfs_ioctl_dev_info_args *di_args;
2474 struct btrfs_device *dev;
2475 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2476 int ret = 0;
2477 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002478
2479 if (!capable(CAP_SYS_ADMIN))
2480 return -EPERM;
2481
2482 di_args = memdup_user(arg, sizeof(*di_args));
2483 if (IS_ERR(di_args))
2484 return PTR_ERR(di_args);
2485
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002486 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002487 s_uuid = di_args->uuid;
2488
2489 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002490 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002491
2492 if (!dev) {
2493 ret = -ENODEV;
2494 goto out;
2495 }
2496
2497 di_args->devid = dev->devid;
2498 di_args->bytes_used = dev->bytes_used;
2499 di_args->total_bytes = dev->total_bytes;
2500 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002501 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002502 struct rcu_string *name;
2503
2504 rcu_read_lock();
2505 name = rcu_dereference(dev->name);
2506 strncpy(di_args->path, name->str, sizeof(di_args->path));
2507 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002508 di_args->path[sizeof(di_args->path) - 1] = 0;
2509 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002510 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002511 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002512
2513out:
David Sterba55793c02013-04-26 15:20:23 +00002514 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002515 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2516 ret = -EFAULT;
2517
2518 kfree(di_args);
2519 return ret;
2520}
2521
Mark Fasheh416161d2013-08-06 11:42:51 -07002522static struct page *extent_same_get_page(struct inode *inode, u64 off)
2523{
2524 struct page *page;
2525 pgoff_t index;
2526 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2527
2528 index = off >> PAGE_CACHE_SHIFT;
2529
2530 page = grab_cache_page(inode->i_mapping, index);
2531 if (!page)
2532 return NULL;
2533
2534 if (!PageUptodate(page)) {
2535 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2536 0))
2537 return NULL;
2538 lock_page(page);
2539 if (!PageUptodate(page)) {
2540 unlock_page(page);
2541 page_cache_release(page);
2542 return NULL;
2543 }
2544 }
2545 unlock_page(page);
2546
2547 return page;
2548}
2549
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002550static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2551{
2552 /* do any pending delalloc/csum calc on src, one way or
2553 another, and lock file content */
2554 while (1) {
2555 struct btrfs_ordered_extent *ordered;
2556 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2557 ordered = btrfs_lookup_first_ordered_extent(inode,
2558 off + len - 1);
2559 if (!ordered &&
2560 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2561 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2562 break;
2563 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2564 if (ordered)
2565 btrfs_put_ordered_extent(ordered);
2566 btrfs_wait_ordered_range(inode, off, len);
2567 }
2568}
2569
Mark Fasheh416161d2013-08-06 11:42:51 -07002570static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2571 struct inode *inode2, u64 loff2, u64 len)
2572{
2573 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2574 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2575
2576 mutex_unlock(&inode1->i_mutex);
2577 mutex_unlock(&inode2->i_mutex);
2578}
2579
2580static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2581 struct inode *inode2, u64 loff2, u64 len)
2582{
2583 if (inode1 < inode2) {
2584 swap(inode1, inode2);
2585 swap(loff1, loff2);
2586 }
2587
2588 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2589 lock_extent_range(inode1, loff1, len);
2590 if (inode1 != inode2) {
2591 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2592 lock_extent_range(inode2, loff2, len);
2593 }
2594}
2595
2596static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2597 u64 dst_loff, u64 len)
2598{
2599 int ret = 0;
2600 struct page *src_page, *dst_page;
2601 unsigned int cmp_len = PAGE_CACHE_SIZE;
2602 void *addr, *dst_addr;
2603
2604 while (len) {
2605 if (len < PAGE_CACHE_SIZE)
2606 cmp_len = len;
2607
2608 src_page = extent_same_get_page(src, loff);
2609 if (!src_page)
2610 return -EINVAL;
2611 dst_page = extent_same_get_page(dst, dst_loff);
2612 if (!dst_page) {
2613 page_cache_release(src_page);
2614 return -EINVAL;
2615 }
2616 addr = kmap_atomic(src_page);
2617 dst_addr = kmap_atomic(dst_page);
2618
2619 flush_dcache_page(src_page);
2620 flush_dcache_page(dst_page);
2621
2622 if (memcmp(addr, dst_addr, cmp_len))
2623 ret = BTRFS_SAME_DATA_DIFFERS;
2624
2625 kunmap_atomic(addr);
2626 kunmap_atomic(dst_addr);
2627 page_cache_release(src_page);
2628 page_cache_release(dst_page);
2629
2630 if (ret)
2631 break;
2632
2633 loff += cmp_len;
2634 dst_loff += cmp_len;
2635 len -= cmp_len;
2636 }
2637
2638 return ret;
2639}
2640
2641static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2642{
2643 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2644
2645 if (off + len > inode->i_size || off + len < off)
2646 return -EINVAL;
2647 /* Check that we are block aligned - btrfs_clone() requires this */
2648 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2649 return -EINVAL;
2650
2651 return 0;
2652}
2653
2654static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2655 struct inode *dst, u64 dst_loff)
2656{
2657 int ret;
2658
2659 /*
2660 * btrfs_clone() can't handle extents in the same file
2661 * yet. Once that works, we can drop this check and replace it
2662 * with a check for the same inode, but overlapping extents.
2663 */
2664 if (src == dst)
2665 return -EINVAL;
2666
2667 btrfs_double_lock(src, loff, dst, dst_loff, len);
2668
2669 ret = extent_same_check_offsets(src, loff, len);
2670 if (ret)
2671 goto out_unlock;
2672
2673 ret = extent_same_check_offsets(dst, dst_loff, len);
2674 if (ret)
2675 goto out_unlock;
2676
2677 /* don't make the dst file partly checksummed */
2678 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2679 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2680 ret = -EINVAL;
2681 goto out_unlock;
2682 }
2683
2684 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2685 if (ret == 0)
2686 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2687
2688out_unlock:
2689 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2690
2691 return ret;
2692}
2693
2694#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2695
2696static long btrfs_ioctl_file_extent_same(struct file *file,
2697 void __user *argp)
2698{
2699 struct btrfs_ioctl_same_args *args = argp;
2700 struct btrfs_ioctl_same_args same;
2701 struct btrfs_ioctl_same_extent_info info;
2702 struct inode *src = file->f_dentry->d_inode;
2703 struct file *dst_file = NULL;
2704 struct inode *dst;
2705 u64 off;
2706 u64 len;
2707 int i;
2708 int ret;
2709 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2710 bool is_admin = capable(CAP_SYS_ADMIN);
2711
2712 if (!(file->f_mode & FMODE_READ))
2713 return -EINVAL;
2714
2715 ret = mnt_want_write_file(file);
2716 if (ret)
2717 return ret;
2718
2719 if (copy_from_user(&same,
2720 (struct btrfs_ioctl_same_args __user *)argp,
2721 sizeof(same))) {
2722 ret = -EFAULT;
2723 goto out;
2724 }
2725
2726 off = same.logical_offset;
2727 len = same.length;
2728
2729 /*
2730 * Limit the total length we will dedupe for each operation.
2731 * This is intended to bound the total time spent in this
2732 * ioctl to something sane.
2733 */
2734 if (len > BTRFS_MAX_DEDUPE_LEN)
2735 len = BTRFS_MAX_DEDUPE_LEN;
2736
2737 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2738 /*
2739 * Btrfs does not support blocksize < page_size. As a
2740 * result, btrfs_cmp_data() won't correctly handle
2741 * this situation without an update.
2742 */
2743 ret = -EINVAL;
2744 goto out;
2745 }
2746
2747 ret = -EISDIR;
2748 if (S_ISDIR(src->i_mode))
2749 goto out;
2750
2751 ret = -EACCES;
2752 if (!S_ISREG(src->i_mode))
2753 goto out;
2754
2755 ret = 0;
2756 for (i = 0; i < same.dest_count; i++) {
2757 if (copy_from_user(&info, &args->info[i], sizeof(info))) {
2758 ret = -EFAULT;
2759 goto out;
2760 }
2761
2762 info.bytes_deduped = 0;
2763
2764 dst_file = fget(info.fd);
2765 if (!dst_file) {
2766 info.status = -EBADF;
2767 goto next;
2768 }
2769
2770 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2771 info.status = -EINVAL;
2772 goto next;
2773 }
2774
2775 info.status = -EXDEV;
2776 if (file->f_path.mnt != dst_file->f_path.mnt)
2777 goto next;
2778
2779 dst = dst_file->f_dentry->d_inode;
2780 if (src->i_sb != dst->i_sb)
2781 goto next;
2782
2783 if (S_ISDIR(dst->i_mode)) {
2784 info.status = -EISDIR;
2785 goto next;
2786 }
2787
2788 if (!S_ISREG(dst->i_mode)) {
2789 info.status = -EACCES;
2790 goto next;
2791 }
2792
2793 info.status = btrfs_extent_same(src, off, len, dst,
2794 info.logical_offset);
2795 if (info.status == 0)
2796 info.bytes_deduped += len;
2797
2798next:
2799 if (dst_file)
2800 fput(dst_file);
2801
2802 if (__put_user_unaligned(info.status, &args->info[i].status) ||
2803 __put_user_unaligned(info.bytes_deduped,
2804 &args->info[i].bytes_deduped)) {
2805 ret = -EFAULT;
2806 goto out;
2807 }
2808 }
2809
2810out:
2811 mnt_drop_write_file(file);
2812 return ret;
2813}
2814
Mark Fasheh32b7c682013-08-06 11:42:49 -07002815/**
2816 * btrfs_clone() - clone a range from inode file to another
2817 *
2818 * @src: Inode to clone from
2819 * @inode: Inode to clone to
2820 * @off: Offset within source to start clone from
2821 * @olen: Original length, passed by user, of range to clone
2822 * @olen_aligned: Block-aligned value of olen, extent_same uses
2823 * identical values here
2824 * @destoff: Offset within @inode to start clone
2825 */
2826static int btrfs_clone(struct inode *src, struct inode *inode,
2827 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002828{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002829 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002830 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002831 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002832 struct btrfs_trans_handle *trans;
2833 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002834 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002835 u32 nritems;
2836 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002837 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002838 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002839
2840 ret = -ENOMEM;
2841 buf = vmalloc(btrfs_level_size(root, 0));
2842 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002843 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002844
2845 path = btrfs_alloc_path();
2846 if (!path) {
2847 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002848 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002849 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002850
Yan Zhengae01a0a2008-08-04 23:23:47 -04002851 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002852 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002853 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002854 key.type = BTRFS_EXTENT_DATA_KEY;
2855 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002856
2857 while (1) {
2858 /*
2859 * note the key will change type as we walk through the
2860 * tree.
2861 */
David Sterba362a20c2011-08-01 18:11:57 +02002862 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2863 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002864 if (ret < 0)
2865 goto out;
2866
Yan Zhengae01a0a2008-08-04 23:23:47 -04002867 nritems = btrfs_header_nritems(path->nodes[0]);
2868 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002869 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002870 if (ret < 0)
2871 goto out;
2872 if (ret > 0)
2873 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002874 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002875 }
2876 leaf = path->nodes[0];
2877 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002878
Yan Zhengae01a0a2008-08-04 23:23:47 -04002879 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002880 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002881 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002882 break;
2883
Sage Weilc5c9cd42008-11-12 14:32:25 -05002884 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2885 struct btrfs_file_extent_item *extent;
2886 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002887 u32 size;
2888 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002889 u64 disko = 0, diskl = 0;
2890 u64 datao = 0, datal = 0;
2891 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002892 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002893
2894 size = btrfs_item_size_nr(leaf, slot);
2895 read_extent_buffer(leaf, buf,
2896 btrfs_item_ptr_offset(leaf, slot),
2897 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002898
2899 extent = btrfs_item_ptr(leaf, slot,
2900 struct btrfs_file_extent_item);
2901 comp = btrfs_file_extent_compression(leaf, extent);
2902 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002903 if (type == BTRFS_FILE_EXTENT_REG ||
2904 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002905 disko = btrfs_file_extent_disk_bytenr(leaf,
2906 extent);
2907 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2908 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002909 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002910 datal = btrfs_file_extent_num_bytes(leaf,
2911 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002912 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2913 /* take upper bound, may be compressed */
2914 datal = btrfs_file_extent_ram_bytes(leaf,
2915 extent);
2916 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002917 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002918
Sage Weil050006a2010-10-29 15:37:33 -04002919 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002920 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002921 goto next;
2922
Zheng Yan31840ae2008-09-23 13:14:14 -04002923 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002924 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002925 if (off <= key.offset)
2926 new_key.offset = key.offset + destoff - off;
2927 else
2928 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002929
Sage Weilb6f34092011-09-20 14:48:51 -04002930 /*
2931 * 1 - adjusting old extent (we may have to split it)
2932 * 1 - add new extent
2933 * 1 - inode update
2934 */
2935 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002936 if (IS_ERR(trans)) {
2937 ret = PTR_ERR(trans);
2938 goto out;
2939 }
2940
Chris Masonc8a894d2009-06-27 21:07:03 -04002941 if (type == BTRFS_FILE_EXTENT_REG ||
2942 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002943 /*
2944 * a | --- range to clone ---| b
2945 * | ------------- extent ------------- |
2946 */
2947
2948 /* substract range b */
2949 if (key.offset + datal > off + len)
2950 datal = off + len - key.offset;
2951
2952 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002953 if (off > key.offset) {
2954 datao += off - key.offset;
2955 datal -= off - key.offset;
2956 }
2957
Josef Bacik5dc562c2012-08-17 13:14:17 -04002958 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002959 new_key.offset,
2960 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002961 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002962 if (ret) {
2963 btrfs_abort_transaction(trans, root,
2964 ret);
2965 btrfs_end_transaction(trans, root);
2966 goto out;
2967 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002968
Sage Weilc5c9cd42008-11-12 14:32:25 -05002969 ret = btrfs_insert_empty_item(trans, root, path,
2970 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 if (ret) {
2972 btrfs_abort_transaction(trans, root,
2973 ret);
2974 btrfs_end_transaction(trans, root);
2975 goto out;
2976 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002977
2978 leaf = path->nodes[0];
2979 slot = path->slots[0];
2980 write_extent_buffer(leaf, buf,
2981 btrfs_item_ptr_offset(leaf, slot),
2982 size);
2983
2984 extent = btrfs_item_ptr(leaf, slot,
2985 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002986
Sage Weilc5c9cd42008-11-12 14:32:25 -05002987 /* disko == 0 means it's a hole */
2988 if (!disko)
2989 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002990
2991 btrfs_set_file_extent_offset(leaf, extent,
2992 datao);
2993 btrfs_set_file_extent_num_bytes(leaf, extent,
2994 datal);
2995 if (disko) {
2996 inode_add_bytes(inode, datal);
2997 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002998 disko, diskl, 0,
2999 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003000 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003001 new_key.offset - datao,
3002 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003003 if (ret) {
3004 btrfs_abort_transaction(trans,
3005 root,
3006 ret);
3007 btrfs_end_transaction(trans,
3008 root);
3009 goto out;
3010
3011 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003012 }
3013 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3014 u64 skip = 0;
3015 u64 trim = 0;
3016 if (off > key.offset) {
3017 skip = off - key.offset;
3018 new_key.offset += skip;
3019 }
Chris Masond3977122009-01-05 21:25:51 -05003020
Liu Boaa42ffd2012-09-18 03:52:23 -06003021 if (key.offset + datal > off + len)
3022 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003023
Sage Weilc5c9cd42008-11-12 14:32:25 -05003024 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003025 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003026 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003027 goto out;
3028 }
3029 size -= skip + trim;
3030 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003031
Josef Bacik5dc562c2012-08-17 13:14:17 -04003032 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003033 new_key.offset,
3034 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003035 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003036 if (ret) {
3037 btrfs_abort_transaction(trans, root,
3038 ret);
3039 btrfs_end_transaction(trans, root);
3040 goto out;
3041 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003042
Sage Weilc5c9cd42008-11-12 14:32:25 -05003043 ret = btrfs_insert_empty_item(trans, root, path,
3044 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003045 if (ret) {
3046 btrfs_abort_transaction(trans, root,
3047 ret);
3048 btrfs_end_transaction(trans, root);
3049 goto out;
3050 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003051
3052 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003053 u32 start =
3054 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003055 memmove(buf+start, buf+start+skip,
3056 datal);
3057 }
3058
3059 leaf = path->nodes[0];
3060 slot = path->slots[0];
3061 write_extent_buffer(leaf, buf,
3062 btrfs_item_ptr_offset(leaf, slot),
3063 size);
3064 inode_add_bytes(inode, datal);
3065 }
3066
3067 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003068 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003069
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003070 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003071 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003072
3073 /*
3074 * we round up to the block size at eof when
3075 * determining which extents to clone above,
3076 * but shouldn't round up the file size
3077 */
3078 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003079 if (endoff > destoff+olen)
3080 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003081 if (endoff > inode->i_size)
3082 btrfs_i_size_write(inode, endoff);
3083
Yan, Zhenga22285a2010-05-16 10:48:46 -04003084 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 if (ret) {
3086 btrfs_abort_transaction(trans, root, ret);
3087 btrfs_end_transaction(trans, root);
3088 goto out;
3089 }
3090 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003091 }
Chris Masond3977122009-01-05 21:25:51 -05003092next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003093 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003094 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003095 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003096 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003097
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003098out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003099 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003100 btrfs_free_path(path);
3101 vfree(buf);
3102 return ret;
3103}
3104
3105static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3106 u64 off, u64 olen, u64 destoff)
3107{
3108 struct inode *inode = fdentry(file)->d_inode;
3109 struct btrfs_root *root = BTRFS_I(inode)->root;
3110 struct fd src_file;
3111 struct inode *src;
3112 int ret;
3113 u64 len = olen;
3114 u64 bs = root->fs_info->sb->s_blocksize;
3115 int same_inode = 0;
3116
3117 /*
3118 * TODO:
3119 * - split compressed inline extents. annoying: we need to
3120 * decompress into destination's address_space (the file offset
3121 * may change, so source mapping won't do), then recompress (or
3122 * otherwise reinsert) a subrange.
3123 * - allow ranges within the same file to be cloned (provided
3124 * they don't overlap)?
3125 */
3126
3127 /* the destination must be opened for writing */
3128 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3129 return -EINVAL;
3130
3131 if (btrfs_root_readonly(root))
3132 return -EROFS;
3133
3134 ret = mnt_want_write_file(file);
3135 if (ret)
3136 return ret;
3137
3138 src_file = fdget(srcfd);
3139 if (!src_file.file) {
3140 ret = -EBADF;
3141 goto out_drop_write;
3142 }
3143
3144 ret = -EXDEV;
3145 if (src_file.file->f_path.mnt != file->f_path.mnt)
3146 goto out_fput;
3147
3148 src = file_inode(src_file.file);
3149
3150 ret = -EINVAL;
3151 if (src == inode)
3152 same_inode = 1;
3153
3154 /* the src must be open for reading */
3155 if (!(src_file.file->f_mode & FMODE_READ))
3156 goto out_fput;
3157
3158 /* don't make the dst file partly checksummed */
3159 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3160 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3161 goto out_fput;
3162
3163 ret = -EISDIR;
3164 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3165 goto out_fput;
3166
3167 ret = -EXDEV;
3168 if (src->i_sb != inode->i_sb)
3169 goto out_fput;
3170
3171 if (!same_inode) {
3172 if (inode < src) {
3173 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3174 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3175 } else {
3176 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3177 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3178 }
3179 } else {
3180 mutex_lock(&src->i_mutex);
3181 }
3182
3183 /* determine range to clone */
3184 ret = -EINVAL;
3185 if (off + len > src->i_size || off + len < off)
3186 goto out_unlock;
3187 if (len == 0)
3188 olen = len = src->i_size - off;
3189 /* if we extend to eof, continue to block boundary */
3190 if (off + len == src->i_size)
3191 len = ALIGN(src->i_size, bs) - off;
3192
3193 /* verify the end result is block aligned */
3194 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3195 !IS_ALIGNED(destoff, bs))
3196 goto out_unlock;
3197
3198 /* verify if ranges are overlapped within the same file */
3199 if (same_inode) {
3200 if (destoff + len > off && destoff < off + len)
3201 goto out_unlock;
3202 }
3203
3204 if (destoff > inode->i_size) {
3205 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3206 if (ret)
3207 goto out_unlock;
3208 }
3209
3210 /* truncate page cache pages from target inode range */
3211 truncate_inode_pages_range(&inode->i_data, destoff,
3212 PAGE_CACHE_ALIGN(destoff + len) - 1);
3213
3214 lock_extent_range(src, off, len);
3215
3216 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3217
Liu Boaa42ffd2012-09-18 03:52:23 -06003218 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003219out_unlock:
3220 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003221 if (!same_inode)
3222 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003223out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003224 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003225out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003226 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003227 return ret;
3228}
3229
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003230static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003231{
3232 struct btrfs_ioctl_clone_range_args args;
3233
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003234 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003235 return -EFAULT;
3236 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3237 args.src_length, args.dest_offset);
3238}
3239
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003240/*
3241 * there are many ways the trans_start and trans_end ioctls can lead
3242 * to deadlocks. They should only be used by applications that
3243 * basically own the machine, and have a very in depth understanding
3244 * of all the possible deadlocks and enospc problems.
3245 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003246static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003247{
Al Viro496ad9a2013-01-23 17:07:38 -05003248 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003249 struct btrfs_root *root = BTRFS_I(inode)->root;
3250 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003251 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252
Sage Weil1ab86ae2009-09-29 18:38:44 -04003253 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003254 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003255 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003256
3257 ret = -EINPROGRESS;
3258 if (file->private_data)
3259 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003260
Li Zefanb83cc962010-12-20 16:04:08 +08003261 ret = -EROFS;
3262 if (btrfs_root_readonly(root))
3263 goto out;
3264
Al Viroa561be72011-11-23 11:57:51 -05003265 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003266 if (ret)
3267 goto out;
3268
Josef Bacika4abeea2011-04-11 17:25:13 -04003269 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003270
Sage Weil1ab86ae2009-09-29 18:38:44 -04003271 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003272 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003273 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003274 goto out_drop;
3275
3276 file->private_data = trans;
3277 return 0;
3278
3279out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003280 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003281 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003282out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003283 return ret;
3284}
3285
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003286static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3287{
Al Viro496ad9a2013-01-23 17:07:38 -05003288 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003289 struct btrfs_root *root = BTRFS_I(inode)->root;
3290 struct btrfs_root *new_root;
3291 struct btrfs_dir_item *di;
3292 struct btrfs_trans_handle *trans;
3293 struct btrfs_path *path;
3294 struct btrfs_key location;
3295 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003296 u64 objectid = 0;
3297 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003298 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003299
3300 if (!capable(CAP_SYS_ADMIN))
3301 return -EPERM;
3302
Miao Xie3c04ce02012-11-26 08:43:07 +00003303 ret = mnt_want_write_file(file);
3304 if (ret)
3305 return ret;
3306
3307 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3308 ret = -EFAULT;
3309 goto out;
3310 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003311
3312 if (!objectid)
3313 objectid = root->root_key.objectid;
3314
3315 location.objectid = objectid;
3316 location.type = BTRFS_ROOT_ITEM_KEY;
3317 location.offset = (u64)-1;
3318
3319 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003320 if (IS_ERR(new_root)) {
3321 ret = PTR_ERR(new_root);
3322 goto out;
3323 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003324
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003325 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003326 if (!path) {
3327 ret = -ENOMEM;
3328 goto out;
3329 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003330 path->leave_spinning = 1;
3331
3332 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003333 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003334 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003335 ret = PTR_ERR(trans);
3336 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003337 }
3338
David Sterba6c417612011-04-13 15:41:04 +02003339 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003340 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3341 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003342 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003343 btrfs_free_path(path);
3344 btrfs_end_transaction(trans, root);
3345 printk(KERN_ERR "Umm, you don't have the default dir item, "
3346 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003347 ret = -ENOENT;
3348 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003349 }
3350
3351 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3352 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3353 btrfs_mark_buffer_dirty(path->nodes[0]);
3354 btrfs_free_path(path);
3355
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003356 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003357 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003358out:
3359 mnt_drop_write_file(file);
3360 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003361}
3362
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003363void btrfs_get_block_group_info(struct list_head *groups_list,
3364 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003365{
3366 struct btrfs_block_group_cache *block_group;
3367
3368 space->total_bytes = 0;
3369 space->used_bytes = 0;
3370 space->flags = 0;
3371 list_for_each_entry(block_group, groups_list, list) {
3372 space->flags = block_group->flags;
3373 space->total_bytes += block_group->key.offset;
3374 space->used_bytes +=
3375 btrfs_block_group_used(&block_group->item);
3376 }
3377}
3378
Eric Sandeen48a3b632013-04-25 20:41:01 +00003379static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003380{
3381 struct btrfs_ioctl_space_args space_args;
3382 struct btrfs_ioctl_space_info space;
3383 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003384 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003385 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003386 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003387 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3388 BTRFS_BLOCK_GROUP_SYSTEM,
3389 BTRFS_BLOCK_GROUP_METADATA,
3390 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3391 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003392 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003393 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003394 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003395 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003396
3397 if (copy_from_user(&space_args,
3398 (struct btrfs_ioctl_space_args __user *)arg,
3399 sizeof(space_args)))
3400 return -EFAULT;
3401
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003402 for (i = 0; i < num_types; i++) {
3403 struct btrfs_space_info *tmp;
3404
3405 info = NULL;
3406 rcu_read_lock();
3407 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3408 list) {
3409 if (tmp->flags == types[i]) {
3410 info = tmp;
3411 break;
3412 }
3413 }
3414 rcu_read_unlock();
3415
3416 if (!info)
3417 continue;
3418
3419 down_read(&info->groups_sem);
3420 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3421 if (!list_empty(&info->block_groups[c]))
3422 slot_count++;
3423 }
3424 up_read(&info->groups_sem);
3425 }
Josef Bacik1406e432010-01-13 18:19:06 +00003426
Chris Mason7fde62b2010-03-16 15:40:10 -04003427 /* space_slots == 0 means they are asking for a count */
3428 if (space_args.space_slots == 0) {
3429 space_args.total_spaces = slot_count;
3430 goto out;
3431 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003432
Dan Rosenberg51788b12011-02-14 16:04:23 -05003433 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003434
Chris Mason7fde62b2010-03-16 15:40:10 -04003435 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003436
Chris Mason7fde62b2010-03-16 15:40:10 -04003437 /* we generally have at most 6 or so space infos, one for each raid
3438 * level. So, a whole page should be more than enough for everyone
3439 */
3440 if (alloc_size > PAGE_CACHE_SIZE)
3441 return -ENOMEM;
3442
3443 space_args.total_spaces = 0;
3444 dest = kmalloc(alloc_size, GFP_NOFS);
3445 if (!dest)
3446 return -ENOMEM;
3447 dest_orig = dest;
3448
3449 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003450 for (i = 0; i < num_types; i++) {
3451 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003452
Dan Rosenberg51788b12011-02-14 16:04:23 -05003453 if (!slot_count)
3454 break;
3455
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003456 info = NULL;
3457 rcu_read_lock();
3458 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3459 list) {
3460 if (tmp->flags == types[i]) {
3461 info = tmp;
3462 break;
3463 }
3464 }
3465 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003466
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003467 if (!info)
3468 continue;
3469 down_read(&info->groups_sem);
3470 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3471 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003472 btrfs_get_block_group_info(
3473 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003474 memcpy(dest, &space, sizeof(space));
3475 dest++;
3476 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003477 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003478 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003479 if (!slot_count)
3480 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003481 }
3482 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003483 }
Josef Bacik1406e432010-01-13 18:19:06 +00003484
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003485 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003486 (arg + sizeof(struct btrfs_ioctl_space_args));
3487
3488 if (copy_to_user(user_dest, dest_orig, alloc_size))
3489 ret = -EFAULT;
3490
3491 kfree(dest_orig);
3492out:
3493 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003494 ret = -EFAULT;
3495
3496 return ret;
3497}
3498
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003499/*
3500 * there are many ways the trans_start and trans_end ioctls can lead
3501 * to deadlocks. They should only be used by applications that
3502 * basically own the machine, and have a very in depth understanding
3503 * of all the possible deadlocks and enospc problems.
3504 */
3505long btrfs_ioctl_trans_end(struct file *file)
3506{
Al Viro496ad9a2013-01-23 17:07:38 -05003507 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003508 struct btrfs_root *root = BTRFS_I(inode)->root;
3509 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003510
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003511 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003512 if (!trans)
3513 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003514 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003515
Sage Weil1ab86ae2009-09-29 18:38:44 -04003516 btrfs_end_transaction(trans, root);
3517
Josef Bacika4abeea2011-04-11 17:25:13 -04003518 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003519
Al Viro2a79f172011-12-09 08:06:57 -05003520 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003521 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003522}
3523
Miao Xie9a8c28b2012-11-26 08:40:43 +00003524static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3525 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003526{
Sage Weil46204592010-10-29 15:41:32 -04003527 struct btrfs_trans_handle *trans;
3528 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003529 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003530
Miao Xied4edf392013-02-20 09:17:06 +00003531 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003532 if (IS_ERR(trans)) {
3533 if (PTR_ERR(trans) != -ENOENT)
3534 return PTR_ERR(trans);
3535
3536 /* No running transaction, don't bother */
3537 transid = root->fs_info->last_trans_committed;
3538 goto out;
3539 }
Sage Weil46204592010-10-29 15:41:32 -04003540 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003541 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003542 if (ret) {
3543 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003544 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003545 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003546out:
Sage Weil46204592010-10-29 15:41:32 -04003547 if (argp)
3548 if (copy_to_user(argp, &transid, sizeof(transid)))
3549 return -EFAULT;
3550 return 0;
3551}
3552
Miao Xie9a8c28b2012-11-26 08:40:43 +00003553static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3554 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003555{
Sage Weil46204592010-10-29 15:41:32 -04003556 u64 transid;
3557
3558 if (argp) {
3559 if (copy_from_user(&transid, argp, sizeof(transid)))
3560 return -EFAULT;
3561 } else {
3562 transid = 0; /* current trans */
3563 }
3564 return btrfs_wait_for_commit(root, transid);
3565}
3566
Miao Xieb8e95482012-11-26 08:48:01 +00003567static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003568{
Al Viro496ad9a2013-01-23 17:07:38 -05003569 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003570 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003571 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003572
3573 if (!capable(CAP_SYS_ADMIN))
3574 return -EPERM;
3575
3576 sa = memdup_user(arg, sizeof(*sa));
3577 if (IS_ERR(sa))
3578 return PTR_ERR(sa);
3579
Miao Xieb8e95482012-11-26 08:48:01 +00003580 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3581 ret = mnt_want_write_file(file);
3582 if (ret)
3583 goto out;
3584 }
3585
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003586 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003587 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3588 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003589
3590 if (copy_to_user(arg, sa, sizeof(*sa)))
3591 ret = -EFAULT;
3592
Miao Xieb8e95482012-11-26 08:48:01 +00003593 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3594 mnt_drop_write_file(file);
3595out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003596 kfree(sa);
3597 return ret;
3598}
3599
3600static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3601{
3602 if (!capable(CAP_SYS_ADMIN))
3603 return -EPERM;
3604
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003605 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003606}
3607
3608static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3609 void __user *arg)
3610{
3611 struct btrfs_ioctl_scrub_args *sa;
3612 int ret;
3613
3614 if (!capable(CAP_SYS_ADMIN))
3615 return -EPERM;
3616
3617 sa = memdup_user(arg, sizeof(*sa));
3618 if (IS_ERR(sa))
3619 return PTR_ERR(sa);
3620
3621 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3622
3623 if (copy_to_user(arg, sa, sizeof(*sa)))
3624 ret = -EFAULT;
3625
3626 kfree(sa);
3627 return ret;
3628}
3629
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003630static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003631 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003632{
3633 struct btrfs_ioctl_get_dev_stats *sa;
3634 int ret;
3635
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003636 sa = memdup_user(arg, sizeof(*sa));
3637 if (IS_ERR(sa))
3638 return PTR_ERR(sa);
3639
David Sterbab27f7c02012-06-22 06:30:39 -06003640 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3641 kfree(sa);
3642 return -EPERM;
3643 }
3644
3645 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003646
3647 if (copy_to_user(arg, sa, sizeof(*sa)))
3648 ret = -EFAULT;
3649
3650 kfree(sa);
3651 return ret;
3652}
3653
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003654static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3655{
3656 struct btrfs_ioctl_dev_replace_args *p;
3657 int ret;
3658
3659 if (!capable(CAP_SYS_ADMIN))
3660 return -EPERM;
3661
3662 p = memdup_user(arg, sizeof(*p));
3663 if (IS_ERR(p))
3664 return PTR_ERR(p);
3665
3666 switch (p->cmd) {
3667 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Stefan Behrensbbb651e2013-08-19 18:51:13 +02003668 if (root->fs_info->sb->s_flags & MS_RDONLY)
3669 return -EROFS;
3670
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003671 if (atomic_xchg(
3672 &root->fs_info->mutually_exclusive_operation_running,
3673 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003674 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003675 } else {
3676 ret = btrfs_dev_replace_start(root, p);
3677 atomic_set(
3678 &root->fs_info->mutually_exclusive_operation_running,
3679 0);
3680 }
3681 break;
3682 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3683 btrfs_dev_replace_status(root->fs_info, p);
3684 ret = 0;
3685 break;
3686 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3687 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3688 break;
3689 default:
3690 ret = -EINVAL;
3691 break;
3692 }
3693
3694 if (copy_to_user(arg, p, sizeof(*p)))
3695 ret = -EFAULT;
3696
3697 kfree(p);
3698 return ret;
3699}
3700
Jan Schmidtd7728c92011-07-07 16:48:38 +02003701static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3702{
3703 int ret = 0;
3704 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003705 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003706 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003707 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003708 struct inode_fs_paths *ipath = NULL;
3709 struct btrfs_path *path;
3710
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003711 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003712 return -EPERM;
3713
3714 path = btrfs_alloc_path();
3715 if (!path) {
3716 ret = -ENOMEM;
3717 goto out;
3718 }
3719
3720 ipa = memdup_user(arg, sizeof(*ipa));
3721 if (IS_ERR(ipa)) {
3722 ret = PTR_ERR(ipa);
3723 ipa = NULL;
3724 goto out;
3725 }
3726
3727 size = min_t(u32, ipa->size, 4096);
3728 ipath = init_ipath(size, root, path);
3729 if (IS_ERR(ipath)) {
3730 ret = PTR_ERR(ipath);
3731 ipath = NULL;
3732 goto out;
3733 }
3734
3735 ret = paths_from_inode(ipa->inum, ipath);
3736 if (ret < 0)
3737 goto out;
3738
3739 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003740 rel_ptr = ipath->fspath->val[i] -
3741 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003742 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003743 }
3744
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003745 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3746 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003747 if (ret) {
3748 ret = -EFAULT;
3749 goto out;
3750 }
3751
3752out:
3753 btrfs_free_path(path);
3754 free_ipath(ipath);
3755 kfree(ipa);
3756
3757 return ret;
3758}
3759
3760static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3761{
3762 struct btrfs_data_container *inodes = ctx;
3763 const size_t c = 3 * sizeof(u64);
3764
3765 if (inodes->bytes_left >= c) {
3766 inodes->bytes_left -= c;
3767 inodes->val[inodes->elem_cnt] = inum;
3768 inodes->val[inodes->elem_cnt + 1] = offset;
3769 inodes->val[inodes->elem_cnt + 2] = root;
3770 inodes->elem_cnt += 3;
3771 } else {
3772 inodes->bytes_missing += c - inodes->bytes_left;
3773 inodes->bytes_left = 0;
3774 inodes->elem_missed += 3;
3775 }
3776
3777 return 0;
3778}
3779
3780static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3781 void __user *arg)
3782{
3783 int ret = 0;
3784 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003785 struct btrfs_ioctl_logical_ino_args *loi;
3786 struct btrfs_data_container *inodes = NULL;
3787 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003788
3789 if (!capable(CAP_SYS_ADMIN))
3790 return -EPERM;
3791
3792 loi = memdup_user(arg, sizeof(*loi));
3793 if (IS_ERR(loi)) {
3794 ret = PTR_ERR(loi);
3795 loi = NULL;
3796 goto out;
3797 }
3798
3799 path = btrfs_alloc_path();
3800 if (!path) {
3801 ret = -ENOMEM;
3802 goto out;
3803 }
3804
Liu Bo425d17a2012-09-07 20:01:30 -06003805 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003806 inodes = init_data_container(size);
3807 if (IS_ERR(inodes)) {
3808 ret = PTR_ERR(inodes);
3809 inodes = NULL;
3810 goto out;
3811 }
3812
Liu Bodf031f02012-09-07 20:01:29 -06003813 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3814 build_ino_list, inodes);
3815 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003816 ret = -ENOENT;
3817 if (ret < 0)
3818 goto out;
3819
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003820 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3821 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003822 if (ret)
3823 ret = -EFAULT;
3824
3825out:
3826 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003827 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003828 kfree(loi);
3829
3830 return ret;
3831}
3832
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003833void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003834 struct btrfs_ioctl_balance_args *bargs)
3835{
3836 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3837
3838 bargs->flags = bctl->flags;
3839
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003840 if (atomic_read(&fs_info->balance_running))
3841 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3842 if (atomic_read(&fs_info->balance_pause_req))
3843 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003844 if (atomic_read(&fs_info->balance_cancel_req))
3845 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003846
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003847 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3848 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3849 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003850
3851 if (lock) {
3852 spin_lock(&fs_info->balance_lock);
3853 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3854 spin_unlock(&fs_info->balance_lock);
3855 } else {
3856 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3857 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003858}
3859
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003860static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003861{
Al Viro496ad9a2013-01-23 17:07:38 -05003862 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003863 struct btrfs_fs_info *fs_info = root->fs_info;
3864 struct btrfs_ioctl_balance_args *bargs;
3865 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003866 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003867 int ret;
3868
3869 if (!capable(CAP_SYS_ADMIN))
3870 return -EPERM;
3871
Liu Boe54bfa32012-06-29 03:58:48 -06003872 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003873 if (ret)
3874 return ret;
3875
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003876again:
3877 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3878 mutex_lock(&fs_info->volume_mutex);
3879 mutex_lock(&fs_info->balance_mutex);
3880 need_unlock = true;
3881 goto locked;
3882 }
3883
3884 /*
3885 * mut. excl. ops lock is locked. Three possibilites:
3886 * (1) some other op is running
3887 * (2) balance is running
3888 * (3) balance is paused -- special case (think resume)
3889 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003890 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003891 if (fs_info->balance_ctl) {
3892 /* this is either (2) or (3) */
3893 if (!atomic_read(&fs_info->balance_running)) {
3894 mutex_unlock(&fs_info->balance_mutex);
3895 if (!mutex_trylock(&fs_info->volume_mutex))
3896 goto again;
3897 mutex_lock(&fs_info->balance_mutex);
3898
3899 if (fs_info->balance_ctl &&
3900 !atomic_read(&fs_info->balance_running)) {
3901 /* this is (3) */
3902 need_unlock = false;
3903 goto locked;
3904 }
3905
3906 mutex_unlock(&fs_info->balance_mutex);
3907 mutex_unlock(&fs_info->volume_mutex);
3908 goto again;
3909 } else {
3910 /* this is (2) */
3911 mutex_unlock(&fs_info->balance_mutex);
3912 ret = -EINPROGRESS;
3913 goto out;
3914 }
3915 } else {
3916 /* this is (1) */
3917 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003918 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003919 goto out;
3920 }
3921
3922locked:
3923 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003924
3925 if (arg) {
3926 bargs = memdup_user(arg, sizeof(*bargs));
3927 if (IS_ERR(bargs)) {
3928 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003929 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003930 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003931
3932 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3933 if (!fs_info->balance_ctl) {
3934 ret = -ENOTCONN;
3935 goto out_bargs;
3936 }
3937
3938 bctl = fs_info->balance_ctl;
3939 spin_lock(&fs_info->balance_lock);
3940 bctl->flags |= BTRFS_BALANCE_RESUME;
3941 spin_unlock(&fs_info->balance_lock);
3942
3943 goto do_balance;
3944 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003945 } else {
3946 bargs = NULL;
3947 }
3948
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003949 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003950 ret = -EINPROGRESS;
3951 goto out_bargs;
3952 }
3953
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003954 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3955 if (!bctl) {
3956 ret = -ENOMEM;
3957 goto out_bargs;
3958 }
3959
3960 bctl->fs_info = fs_info;
3961 if (arg) {
3962 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3963 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3964 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3965
3966 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003967 } else {
3968 /* balance everything - no filters */
3969 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003970 }
3971
Ilya Dryomovde322262012-01-16 22:04:49 +02003972do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003973 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003974 * Ownership of bctl and mutually_exclusive_operation_running
3975 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3976 * or, if restriper was paused all the way until unmount, in
3977 * free_fs_info. mutually_exclusive_operation_running is
3978 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003979 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003980 need_unlock = false;
3981
3982 ret = btrfs_balance(bctl, bargs);
3983
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003984 if (arg) {
3985 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3986 ret = -EFAULT;
3987 }
3988
3989out_bargs:
3990 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003991out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003992 mutex_unlock(&fs_info->balance_mutex);
3993 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003994 if (need_unlock)
3995 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3996out:
Liu Boe54bfa32012-06-29 03:58:48 -06003997 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003998 return ret;
3999}
4000
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004001static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4002{
4003 if (!capable(CAP_SYS_ADMIN))
4004 return -EPERM;
4005
4006 switch (cmd) {
4007 case BTRFS_BALANCE_CTL_PAUSE:
4008 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004009 case BTRFS_BALANCE_CTL_CANCEL:
4010 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004011 }
4012
4013 return -EINVAL;
4014}
4015
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004016static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4017 void __user *arg)
4018{
4019 struct btrfs_fs_info *fs_info = root->fs_info;
4020 struct btrfs_ioctl_balance_args *bargs;
4021 int ret = 0;
4022
4023 if (!capable(CAP_SYS_ADMIN))
4024 return -EPERM;
4025
4026 mutex_lock(&fs_info->balance_mutex);
4027 if (!fs_info->balance_ctl) {
4028 ret = -ENOTCONN;
4029 goto out;
4030 }
4031
4032 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4033 if (!bargs) {
4034 ret = -ENOMEM;
4035 goto out;
4036 }
4037
4038 update_ioctl_balance_args(fs_info, 1, bargs);
4039
4040 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4041 ret = -EFAULT;
4042
4043 kfree(bargs);
4044out:
4045 mutex_unlock(&fs_info->balance_mutex);
4046 return ret;
4047}
4048
Miao Xie905b0dd2012-11-26 08:50:11 +00004049static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004050{
Al Viro496ad9a2013-01-23 17:07:38 -05004051 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004052 struct btrfs_ioctl_quota_ctl_args *sa;
4053 struct btrfs_trans_handle *trans = NULL;
4054 int ret;
4055 int err;
4056
4057 if (!capable(CAP_SYS_ADMIN))
4058 return -EPERM;
4059
Miao Xie905b0dd2012-11-26 08:50:11 +00004060 ret = mnt_want_write_file(file);
4061 if (ret)
4062 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004063
4064 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004065 if (IS_ERR(sa)) {
4066 ret = PTR_ERR(sa);
4067 goto drop_write;
4068 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004069
Wang Shilong7708f022013-04-07 10:24:57 +00004070 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004071 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4072 if (IS_ERR(trans)) {
4073 ret = PTR_ERR(trans);
4074 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004075 }
4076
4077 switch (sa->cmd) {
4078 case BTRFS_QUOTA_CTL_ENABLE:
4079 ret = btrfs_quota_enable(trans, root->fs_info);
4080 break;
4081 case BTRFS_QUOTA_CTL_DISABLE:
4082 ret = btrfs_quota_disable(trans, root->fs_info);
4083 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004084 default:
4085 ret = -EINVAL;
4086 break;
4087 }
4088
Jan Schmidt2f232032013-04-25 16:04:51 +00004089 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4090 if (err && !ret)
4091 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004092out:
4093 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004094 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004095drop_write:
4096 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004097 return ret;
4098}
4099
Miao Xie905b0dd2012-11-26 08:50:11 +00004100static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004101{
Al Viro496ad9a2013-01-23 17:07:38 -05004102 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004103 struct btrfs_ioctl_qgroup_assign_args *sa;
4104 struct btrfs_trans_handle *trans;
4105 int ret;
4106 int err;
4107
4108 if (!capable(CAP_SYS_ADMIN))
4109 return -EPERM;
4110
Miao Xie905b0dd2012-11-26 08:50:11 +00004111 ret = mnt_want_write_file(file);
4112 if (ret)
4113 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004114
4115 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004116 if (IS_ERR(sa)) {
4117 ret = PTR_ERR(sa);
4118 goto drop_write;
4119 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004120
4121 trans = btrfs_join_transaction(root);
4122 if (IS_ERR(trans)) {
4123 ret = PTR_ERR(trans);
4124 goto out;
4125 }
4126
4127 /* FIXME: check if the IDs really exist */
4128 if (sa->assign) {
4129 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4130 sa->src, sa->dst);
4131 } else {
4132 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4133 sa->src, sa->dst);
4134 }
4135
4136 err = btrfs_end_transaction(trans, root);
4137 if (err && !ret)
4138 ret = err;
4139
4140out:
4141 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004142drop_write:
4143 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004144 return ret;
4145}
4146
Miao Xie905b0dd2012-11-26 08:50:11 +00004147static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004148{
Al Viro496ad9a2013-01-23 17:07:38 -05004149 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004150 struct btrfs_ioctl_qgroup_create_args *sa;
4151 struct btrfs_trans_handle *trans;
4152 int ret;
4153 int err;
4154
4155 if (!capable(CAP_SYS_ADMIN))
4156 return -EPERM;
4157
Miao Xie905b0dd2012-11-26 08:50:11 +00004158 ret = mnt_want_write_file(file);
4159 if (ret)
4160 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004161
4162 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004163 if (IS_ERR(sa)) {
4164 ret = PTR_ERR(sa);
4165 goto drop_write;
4166 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004167
Miao Xied86e56c2012-11-15 11:35:41 +00004168 if (!sa->qgroupid) {
4169 ret = -EINVAL;
4170 goto out;
4171 }
4172
Arne Jansen5d13a372011-09-14 15:53:51 +02004173 trans = btrfs_join_transaction(root);
4174 if (IS_ERR(trans)) {
4175 ret = PTR_ERR(trans);
4176 goto out;
4177 }
4178
4179 /* FIXME: check if the IDs really exist */
4180 if (sa->create) {
4181 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4182 NULL);
4183 } else {
4184 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4185 }
4186
4187 err = btrfs_end_transaction(trans, root);
4188 if (err && !ret)
4189 ret = err;
4190
4191out:
4192 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004193drop_write:
4194 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004195 return ret;
4196}
4197
Miao Xie905b0dd2012-11-26 08:50:11 +00004198static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004199{
Al Viro496ad9a2013-01-23 17:07:38 -05004200 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004201 struct btrfs_ioctl_qgroup_limit_args *sa;
4202 struct btrfs_trans_handle *trans;
4203 int ret;
4204 int err;
4205 u64 qgroupid;
4206
4207 if (!capable(CAP_SYS_ADMIN))
4208 return -EPERM;
4209
Miao Xie905b0dd2012-11-26 08:50:11 +00004210 ret = mnt_want_write_file(file);
4211 if (ret)
4212 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004213
4214 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004215 if (IS_ERR(sa)) {
4216 ret = PTR_ERR(sa);
4217 goto drop_write;
4218 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004219
4220 trans = btrfs_join_transaction(root);
4221 if (IS_ERR(trans)) {
4222 ret = PTR_ERR(trans);
4223 goto out;
4224 }
4225
4226 qgroupid = sa->qgroupid;
4227 if (!qgroupid) {
4228 /* take the current subvol as qgroup */
4229 qgroupid = root->root_key.objectid;
4230 }
4231
4232 /* FIXME: check if the IDs really exist */
4233 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4234
4235 err = btrfs_end_transaction(trans, root);
4236 if (err && !ret)
4237 ret = err;
4238
4239out:
4240 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004241drop_write:
4242 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004243 return ret;
4244}
4245
Jan Schmidt2f232032013-04-25 16:04:51 +00004246static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4247{
Al Viro6d0379e2013-06-16 19:32:35 +04004248 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004249 struct btrfs_ioctl_quota_rescan_args *qsa;
4250 int ret;
4251
4252 if (!capable(CAP_SYS_ADMIN))
4253 return -EPERM;
4254
4255 ret = mnt_want_write_file(file);
4256 if (ret)
4257 return ret;
4258
4259 qsa = memdup_user(arg, sizeof(*qsa));
4260 if (IS_ERR(qsa)) {
4261 ret = PTR_ERR(qsa);
4262 goto drop_write;
4263 }
4264
4265 if (qsa->flags) {
4266 ret = -EINVAL;
4267 goto out;
4268 }
4269
4270 ret = btrfs_qgroup_rescan(root->fs_info);
4271
4272out:
4273 kfree(qsa);
4274drop_write:
4275 mnt_drop_write_file(file);
4276 return ret;
4277}
4278
4279static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4280{
Al Viro6d0379e2013-06-16 19:32:35 +04004281 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004282 struct btrfs_ioctl_quota_rescan_args *qsa;
4283 int ret = 0;
4284
4285 if (!capable(CAP_SYS_ADMIN))
4286 return -EPERM;
4287
4288 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4289 if (!qsa)
4290 return -ENOMEM;
4291
4292 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4293 qsa->flags = 1;
4294 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4295 }
4296
4297 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4298 ret = -EFAULT;
4299
4300 kfree(qsa);
4301 return ret;
4302}
4303
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004304static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4305{
4306 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4307
4308 if (!capable(CAP_SYS_ADMIN))
4309 return -EPERM;
4310
4311 return btrfs_qgroup_wait_for_completion(root->fs_info);
4312}
4313
Alexander Block8ea05e32012-07-25 17:35:53 +02004314static long btrfs_ioctl_set_received_subvol(struct file *file,
4315 void __user *arg)
4316{
4317 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004318 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004319 struct btrfs_root *root = BTRFS_I(inode)->root;
4320 struct btrfs_root_item *root_item = &root->root_item;
4321 struct btrfs_trans_handle *trans;
4322 struct timespec ct = CURRENT_TIME;
4323 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004324 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004325
4326 ret = mnt_want_write_file(file);
4327 if (ret < 0)
4328 return ret;
4329
4330 down_write(&root->fs_info->subvol_sem);
4331
4332 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4333 ret = -EINVAL;
4334 goto out;
4335 }
4336
4337 if (btrfs_root_readonly(root)) {
4338 ret = -EROFS;
4339 goto out;
4340 }
4341
4342 if (!inode_owner_or_capable(inode)) {
4343 ret = -EACCES;
4344 goto out;
4345 }
4346
4347 sa = memdup_user(arg, sizeof(*sa));
4348 if (IS_ERR(sa)) {
4349 ret = PTR_ERR(sa);
4350 sa = NULL;
4351 goto out;
4352 }
4353
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004354 /*
4355 * 1 - root item
4356 * 2 - uuid items (received uuid + subvol uuid)
4357 */
4358 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004359 if (IS_ERR(trans)) {
4360 ret = PTR_ERR(trans);
4361 trans = NULL;
4362 goto out;
4363 }
4364
4365 sa->rtransid = trans->transid;
4366 sa->rtime.sec = ct.tv_sec;
4367 sa->rtime.nsec = ct.tv_nsec;
4368
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004369 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4370 BTRFS_UUID_SIZE);
4371 if (received_uuid_changed &&
4372 !btrfs_is_empty_uuid(root_item->received_uuid))
4373 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4374 root_item->received_uuid,
4375 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4376 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004377 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4378 btrfs_set_root_stransid(root_item, sa->stransid);
4379 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004380 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4381 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4382 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4383 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004384
4385 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4386 &root->root_key, &root->root_item);
4387 if (ret < 0) {
4388 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004389 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004390 }
4391 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4392 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4393 sa->uuid,
4394 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4395 root->root_key.objectid);
4396 if (ret < 0 && ret != -EEXIST) {
4397 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004398 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004399 }
4400 }
4401 ret = btrfs_commit_transaction(trans, root);
4402 if (ret < 0) {
4403 btrfs_abort_transaction(trans, root, ret);
4404 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004405 }
4406
4407 ret = copy_to_user(arg, sa, sizeof(*sa));
4408 if (ret)
4409 ret = -EFAULT;
4410
4411out:
4412 kfree(sa);
4413 up_write(&root->fs_info->subvol_sem);
4414 mnt_drop_write_file(file);
4415 return ret;
4416}
4417
jeff.liu867ab662013-01-05 02:48:01 +00004418static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4419{
Al Viro6d0379e2013-06-16 19:32:35 +04004420 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004421 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004422 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004423 char label[BTRFS_LABEL_SIZE];
4424
4425 spin_lock(&root->fs_info->super_lock);
4426 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4427 spin_unlock(&root->fs_info->super_lock);
4428
4429 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004430
4431 if (len == BTRFS_LABEL_SIZE) {
4432 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4433 --len);
4434 }
4435
jeff.liu867ab662013-01-05 02:48:01 +00004436 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004437
4438 return ret ? -EFAULT : 0;
4439}
4440
jeff.liua8bfd4a2013-01-05 02:48:08 +00004441static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4442{
Al Viro6d0379e2013-06-16 19:32:35 +04004443 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004444 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4445 struct btrfs_trans_handle *trans;
4446 char label[BTRFS_LABEL_SIZE];
4447 int ret;
4448
4449 if (!capable(CAP_SYS_ADMIN))
4450 return -EPERM;
4451
4452 if (copy_from_user(label, arg, sizeof(label)))
4453 return -EFAULT;
4454
4455 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4456 pr_err("btrfs: unable to set label with more than %d bytes\n",
4457 BTRFS_LABEL_SIZE - 1);
4458 return -EINVAL;
4459 }
4460
4461 ret = mnt_want_write_file(file);
4462 if (ret)
4463 return ret;
4464
jeff.liua8bfd4a2013-01-05 02:48:08 +00004465 trans = btrfs_start_transaction(root, 0);
4466 if (IS_ERR(trans)) {
4467 ret = PTR_ERR(trans);
4468 goto out_unlock;
4469 }
4470
Anand Jaina1b83ac2013-07-19 17:39:32 +08004471 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004472 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004473 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004474 ret = btrfs_end_transaction(trans, root);
4475
4476out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004477 mnt_drop_write_file(file);
4478 return ret;
4479}
4480
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004481long btrfs_ioctl(struct file *file, unsigned int
4482 cmd, unsigned long arg)
4483{
Al Viro496ad9a2013-01-23 17:07:38 -05004484 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004485 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004486
4487 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004488 case FS_IOC_GETFLAGS:
4489 return btrfs_ioctl_getflags(file, argp);
4490 case FS_IOC_SETFLAGS:
4491 return btrfs_ioctl_setflags(file, argp);
4492 case FS_IOC_GETVERSION:
4493 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004494 case FITRIM:
4495 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004496 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004497 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004498 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004499 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004500 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004501 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004502 case BTRFS_IOC_SUBVOL_CREATE_V2:
4503 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004504 case BTRFS_IOC_SNAP_DESTROY:
4505 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004506 case BTRFS_IOC_SUBVOL_GETFLAGS:
4507 return btrfs_ioctl_subvol_getflags(file, argp);
4508 case BTRFS_IOC_SUBVOL_SETFLAGS:
4509 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004510 case BTRFS_IOC_DEFAULT_SUBVOL:
4511 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004512 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004513 return btrfs_ioctl_defrag(file, NULL);
4514 case BTRFS_IOC_DEFRAG_RANGE:
4515 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004516 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004517 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004518 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004519 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004520 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004521 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004522 case BTRFS_IOC_FS_INFO:
4523 return btrfs_ioctl_fs_info(root, argp);
4524 case BTRFS_IOC_DEV_INFO:
4525 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004526 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004527 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004528 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004529 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4530 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004531 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004532 case BTRFS_IOC_TRANS_START:
4533 return btrfs_ioctl_trans_start(file);
4534 case BTRFS_IOC_TRANS_END:
4535 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004536 case BTRFS_IOC_TREE_SEARCH:
4537 return btrfs_ioctl_tree_search(file, argp);
4538 case BTRFS_IOC_INO_LOOKUP:
4539 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004540 case BTRFS_IOC_INO_PATHS:
4541 return btrfs_ioctl_ino_to_path(root, argp);
4542 case BTRFS_IOC_LOGICAL_INO:
4543 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004544 case BTRFS_IOC_SPACE_INFO:
4545 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004546 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004547 btrfs_sync_fs(file->f_dentry->d_sb, 1);
4548 return 0;
Sage Weil46204592010-10-29 15:41:32 -04004549 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004550 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004551 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004552 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004553 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004554 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004555 case BTRFS_IOC_SCRUB_CANCEL:
4556 return btrfs_ioctl_scrub_cancel(root, argp);
4557 case BTRFS_IOC_SCRUB_PROGRESS:
4558 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004559 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004560 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004561 case BTRFS_IOC_BALANCE_CTL:
4562 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004563 case BTRFS_IOC_BALANCE_PROGRESS:
4564 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004565 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4566 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004567 case BTRFS_IOC_SEND:
4568 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004569 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004570 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004571 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004572 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004573 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004574 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004575 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004576 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004577 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004578 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004579 case BTRFS_IOC_QUOTA_RESCAN:
4580 return btrfs_ioctl_quota_rescan(file, argp);
4581 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4582 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004583 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4584 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004585 case BTRFS_IOC_DEV_REPLACE:
4586 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004587 case BTRFS_IOC_GET_FSLABEL:
4588 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004589 case BTRFS_IOC_SET_FSLABEL:
4590 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004591 case BTRFS_IOC_FILE_EXTENT_SAME:
4592 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004593 }
4594
4595 return -ENOTTY;
4596}