blob: 21da5762b0b1b33d193f3bd7f664995a43062bd6 [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>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040059
Mark Fasheh416161d2013-08-06 11:42:51 -070060static int btrfs_clone(struct inode *src, struct inode *inode,
61 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
62
Christoph Hellwig6cbff002009-04-17 10:37:41 +020063/* Mask out flags that are inappropriate for the given type of inode. */
64static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
65{
66 if (S_ISDIR(mode))
67 return flags;
68 else if (S_ISREG(mode))
69 return flags & ~FS_DIRSYNC_FL;
70 else
71 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
72}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040073
Christoph Hellwig6cbff002009-04-17 10:37:41 +020074/*
75 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
76 */
77static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
78{
79 unsigned int iflags = 0;
80
81 if (flags & BTRFS_INODE_SYNC)
82 iflags |= FS_SYNC_FL;
83 if (flags & BTRFS_INODE_IMMUTABLE)
84 iflags |= FS_IMMUTABLE_FL;
85 if (flags & BTRFS_INODE_APPEND)
86 iflags |= FS_APPEND_FL;
87 if (flags & BTRFS_INODE_NODUMP)
88 iflags |= FS_NODUMP_FL;
89 if (flags & BTRFS_INODE_NOATIME)
90 iflags |= FS_NOATIME_FL;
91 if (flags & BTRFS_INODE_DIRSYNC)
92 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000093 if (flags & BTRFS_INODE_NODATACOW)
94 iflags |= FS_NOCOW_FL;
95
96 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
97 iflags |= FS_COMPR_FL;
98 else if (flags & BTRFS_INODE_NOCOMPRESS)
99 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200100
101 return iflags;
102}
103
104/*
105 * Update inode->i_flags based on the btrfs internal flags.
106 */
107void btrfs_update_iflags(struct inode *inode)
108{
109 struct btrfs_inode *ip = BTRFS_I(inode);
110
111 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
112
113 if (ip->flags & BTRFS_INODE_SYNC)
114 inode->i_flags |= S_SYNC;
115 if (ip->flags & BTRFS_INODE_IMMUTABLE)
116 inode->i_flags |= S_IMMUTABLE;
117 if (ip->flags & BTRFS_INODE_APPEND)
118 inode->i_flags |= S_APPEND;
119 if (ip->flags & BTRFS_INODE_NOATIME)
120 inode->i_flags |= S_NOATIME;
121 if (ip->flags & BTRFS_INODE_DIRSYNC)
122 inode->i_flags |= S_DIRSYNC;
123}
124
125/*
126 * Inherit flags from the parent inode.
127 *
Josef Bacike27425d2011-09-27 11:01:30 -0400128 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129 */
130void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
131{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400132 unsigned int flags;
133
134 if (!dir)
135 return;
136
137 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200138
Josef Bacike27425d2011-09-27 11:01:30 -0400139 if (flags & BTRFS_INODE_NOCOMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
142 } else if (flags & BTRFS_INODE_COMPRESS) {
143 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
144 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
145 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200146
Liu Bo213490b2012-09-11 08:33:50 -0600147 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400148 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600149 if (S_ISREG(inode->i_mode))
150 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
151 }
Josef Bacike27425d2011-09-27 11:01:30 -0400152
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200153 btrfs_update_iflags(inode);
154}
155
156static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
157{
Al Viro496ad9a2013-01-23 17:07:38 -0500158 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200159 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
160
161 if (copy_to_user(arg, &flags, sizeof(flags)))
162 return -EFAULT;
163 return 0;
164}
165
Liu Bo75e7cb72011-03-22 10:12:20 +0000166static int check_flags(unsigned int flags)
167{
168 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
169 FS_NOATIME_FL | FS_NODUMP_FL | \
170 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000171 FS_NOCOMP_FL | FS_COMPR_FL |
172 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000173 return -EOPNOTSUPP;
174
175 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
176 return -EINVAL;
177
Liu Bo75e7cb72011-03-22 10:12:20 +0000178 return 0;
179}
180
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
182{
Al Viro496ad9a2013-01-23 17:07:38 -0500183 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200184 struct btrfs_inode *ip = BTRFS_I(inode);
185 struct btrfs_root *root = ip->root;
186 struct btrfs_trans_handle *trans;
187 unsigned int flags, oldflags;
188 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800189 u64 ip_oldflags;
190 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600191 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192
Li Zefanb83cc962010-12-20 16:04:08 +0800193 if (btrfs_root_readonly(root))
194 return -EROFS;
195
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200196 if (copy_from_user(&flags, arg, sizeof(flags)))
197 return -EFAULT;
198
Liu Bo75e7cb72011-03-22 10:12:20 +0000199 ret = check_flags(flags);
200 if (ret)
201 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200202
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700203 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200204 return -EACCES;
205
Jan Karae7848682012-06-12 16:20:32 +0200206 ret = mnt_want_write_file(file);
207 if (ret)
208 return ret;
209
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210 mutex_lock(&inode->i_mutex);
211
Li Zefanf062abf02011-12-29 13:36:45 +0800212 ip_oldflags = ip->flags;
213 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600214 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800215
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200216 flags = btrfs_mask_flags(inode->i_mode, flags);
217 oldflags = btrfs_flags_to_ioctl(ip->flags);
218 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
219 if (!capable(CAP_LINUX_IMMUTABLE)) {
220 ret = -EPERM;
221 goto out_unlock;
222 }
223 }
224
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200225 if (flags & FS_SYNC_FL)
226 ip->flags |= BTRFS_INODE_SYNC;
227 else
228 ip->flags &= ~BTRFS_INODE_SYNC;
229 if (flags & FS_IMMUTABLE_FL)
230 ip->flags |= BTRFS_INODE_IMMUTABLE;
231 else
232 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
233 if (flags & FS_APPEND_FL)
234 ip->flags |= BTRFS_INODE_APPEND;
235 else
236 ip->flags &= ~BTRFS_INODE_APPEND;
237 if (flags & FS_NODUMP_FL)
238 ip->flags |= BTRFS_INODE_NODUMP;
239 else
240 ip->flags &= ~BTRFS_INODE_NODUMP;
241 if (flags & FS_NOATIME_FL)
242 ip->flags |= BTRFS_INODE_NOATIME;
243 else
244 ip->flags &= ~BTRFS_INODE_NOATIME;
245 if (flags & FS_DIRSYNC_FL)
246 ip->flags |= BTRFS_INODE_DIRSYNC;
247 else
248 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600249 if (flags & FS_NOCOW_FL) {
250 if (S_ISREG(mode)) {
251 /*
252 * It's safe to turn csums off here, no extents exist.
253 * Otherwise we want the flag to reflect the real COW
254 * status of the file and will not set it.
255 */
256 if (inode->i_size == 0)
257 ip->flags |= BTRFS_INODE_NODATACOW
258 | BTRFS_INODE_NODATASUM;
259 } else {
260 ip->flags |= BTRFS_INODE_NODATACOW;
261 }
262 } else {
263 /*
264 * Revert back under same assuptions as above
265 */
266 if (S_ISREG(mode)) {
267 if (inode->i_size == 0)
268 ip->flags &= ~(BTRFS_INODE_NODATACOW
269 | BTRFS_INODE_NODATASUM);
270 } else {
271 ip->flags &= ~BTRFS_INODE_NODATACOW;
272 }
273 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200274
Liu Bo75e7cb72011-03-22 10:12:20 +0000275 /*
276 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
277 * flag may be changed automatically if compression code won't make
278 * things smaller.
279 */
280 if (flags & FS_NOCOMP_FL) {
281 ip->flags &= ~BTRFS_INODE_COMPRESS;
282 ip->flags |= BTRFS_INODE_NOCOMPRESS;
283 } else if (flags & FS_COMPR_FL) {
284 ip->flags |= BTRFS_INODE_COMPRESS;
285 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000286 } else {
287 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000288 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200289
Li Zefan4da6f1a2011-12-29 13:39:50 +0800290 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800291 if (IS_ERR(trans)) {
292 ret = PTR_ERR(trans);
293 goto out_drop;
294 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200295
Li Zefan306424cc2011-12-14 20:12:02 -0500296 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400297 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500298 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200299 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301 btrfs_end_transaction(trans, root);
Li Zefanf062abf02011-12-29 13:36:45 +0800302 out_drop:
303 if (ret) {
304 ip->flags = ip_oldflags;
305 inode->i_flags = i_oldflags;
306 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200307
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308 out_unlock:
309 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200310 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000311 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200312}
313
314static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
315{
Al Viro496ad9a2013-01-23 17:07:38 -0500316 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200317
318 return put_user(inode->i_generation, arg);
319}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400320
Li Dongyangf7039b12011-03-24 10:24:28 +0000321static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
322{
Al Viro54563d42013-09-01 15:57:51 -0400323 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000324 struct btrfs_device *device;
325 struct request_queue *q;
326 struct fstrim_range range;
327 u64 minlen = ULLONG_MAX;
328 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500329 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000330 int ret;
331
332 if (!capable(CAP_SYS_ADMIN))
333 return -EPERM;
334
Xiao Guangrong1f781602011-04-20 10:09:16 +0000335 rcu_read_lock();
336 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
337 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000338 if (!device->bdev)
339 continue;
340 q = bdev_get_queue(device->bdev);
341 if (blk_queue_discard(q)) {
342 num_devices++;
343 minlen = min((u64)q->limits.discard_granularity,
344 minlen);
345 }
346 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000347 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200348
Li Dongyangf7039b12011-03-24 10:24:28 +0000349 if (!num_devices)
350 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000351 if (copy_from_user(&range, arg, sizeof(range)))
352 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000353 if (range.start > total_bytes ||
354 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200355 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000356
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200357 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000358 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500359 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000360 if (ret < 0)
361 return ret;
362
363 if (copy_to_user(arg, &range, sizeof(range)))
364 return -EFAULT;
365
366 return 0;
367}
368
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200369int btrfs_is_empty_uuid(u8 *uuid)
370{
Chris Mason46e0f662013-11-15 12:14:55 +0100371 int i;
372
373 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
374 if (uuid[i])
375 return 0;
376 }
377 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200378}
379
Miao Xied5c12072013-02-28 10:04:33 +0000380static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400381 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400382 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200383 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000384 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400385{
386 struct btrfs_trans_handle *trans;
387 struct btrfs_key key;
388 struct btrfs_root_item root_item;
389 struct btrfs_inode_item *inode_item;
390 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000391 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400392 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000393 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200394 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400395 int ret;
396 int err;
397 u64 objectid;
398 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500399 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000400 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200401 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402
Li Zefan581bb052011-04-20 10:06:11 +0800403 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400404 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400405 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000406
Miao Xied5c12072013-02-28 10:04:33 +0000407 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400408 /*
Miao Xied5c12072013-02-28 10:04:33 +0000409 * The same as the snapshot creation, please see the comment
410 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400411 */
Miao Xied5c12072013-02-28 10:04:33 +0000412 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200413 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000414 if (ret)
415 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400416
Miao Xied5c12072013-02-28 10:04:33 +0000417 trans = btrfs_start_transaction(root, 0);
418 if (IS_ERR(trans)) {
419 ret = PTR_ERR(trans);
420 goto out;
421 }
422 trans->block_rsv = &block_rsv;
423 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400424
Miao Xie8696c532013-02-07 06:02:44 +0000425 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200426 if (ret)
427 goto fail;
428
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400429 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200430 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400431 if (IS_ERR(leaf)) {
432 ret = PTR_ERR(leaf);
433 goto fail;
434 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400435
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437 btrfs_set_header_bytenr(leaf, leaf->start);
438 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400439 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 btrfs_set_header_owner(leaf, objectid);
441
Ross Kirk0a4e5582013-09-24 10:12:38 +0100442 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400444 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200445 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400446 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400447 btrfs_mark_buffer_dirty(leaf);
448
Alexander Block8ea05e32012-07-25 17:35:53 +0200449 memset(&root_item, 0, sizeof(root_item));
450
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800452 btrfs_set_stack_inode_generation(inode_item, 1);
453 btrfs_set_stack_inode_size(inode_item, 3);
454 btrfs_set_stack_inode_nlink(inode_item, 1);
455 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
456 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457
Qu Wenruo3cae2102013-07-16 11:19:18 +0800458 btrfs_set_root_flags(&root_item, 0);
459 btrfs_set_root_limit(&root_item, 0);
460 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000461
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400462 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400463 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 btrfs_set_root_level(&root_item, 0);
465 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000466 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400467 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400468
Alexander Block8ea05e32012-07-25 17:35:53 +0200469 btrfs_set_root_generation_v2(&root_item,
470 btrfs_root_generation(&root_item));
471 uuid_le_gen(&new_uuid);
472 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800473 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
474 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200475 root_item.ctime = root_item.otime;
476 btrfs_set_root_ctransid(&root_item, trans->transid);
477 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478
Chris Mason925baed2008-06-25 16:01:30 -0400479 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400480 free_extent_buffer(leaf);
481 leaf = NULL;
482
483 btrfs_set_root_dirid(&root_item, new_dirid);
484
485 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400486 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400487 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
488 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
489 &root_item);
490 if (ret)
491 goto fail;
492
Yan, Zheng76dda932009-09-21 16:00:26 -0400493 key.offset = (u64)-1;
494 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100495 if (IS_ERR(new_root)) {
496 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
497 ret = PTR_ERR(new_root);
498 goto fail;
499 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400500
501 btrfs_record_root_in_trans(trans, new_root);
502
Josef Bacikd82a6f1d2011-05-11 15:26:06 -0400503 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700504 if (ret) {
505 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100506 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700507 goto fail;
508 }
509
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400510 /*
511 * insert the directory item
512 */
Chris Mason3de45862008-11-17 21:02:50 -0500513 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100514 if (ret) {
515 btrfs_abort_transaction(trans, root, ret);
516 goto fail;
517 }
Chris Mason3de45862008-11-17 21:02:50 -0500518
519 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800520 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500521 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100522 if (ret) {
523 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400524 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100525 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500526
Yan Zheng52c26172009-01-05 15:43:43 -0500527 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
528 ret = btrfs_update_inode(trans, root, dir);
529 BUG_ON(ret);
530
Chris Mason0660b5a2008-11-17 20:37:39 -0500531 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400532 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800533 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500534 BUG_ON(ret);
535
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200536 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
537 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
538 objectid);
539 if (ret)
540 btrfs_abort_transaction(trans, root, ret);
541
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400542fail:
Miao Xied5c12072013-02-28 10:04:33 +0000543 trans->block_rsv = NULL;
544 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400545 if (async_transid) {
546 *async_transid = trans->transid;
547 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000548 if (err)
549 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400550 } else {
551 err = btrfs_commit_transaction(trans, root);
552 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400553 if (err && !ret)
554 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500555
556 if (!ret)
557 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000558out:
559 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400560 return ret;
561}
562
Miao Xiee9662f72013-02-28 10:01:15 +0000563static int create_snapshot(struct btrfs_root *root, struct inode *dir,
564 struct dentry *dentry, char *name, int namelen,
565 u64 *async_transid, bool readonly,
566 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000568 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400569 struct btrfs_pending_snapshot *pending_snapshot;
570 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000571 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400572
573 if (!root->ref_cows)
574 return -EINVAL;
575
Miao Xie6a038432013-05-15 07:48:24 +0000576 ret = btrfs_start_delalloc_inodes(root, 0);
577 if (ret)
578 return ret;
579
Miao Xieb0244192013-11-04 23:13:25 +0800580 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000581
Yan, Zhenga22285a2010-05-16 10:48:46 -0400582 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
583 if (!pending_snapshot)
584 return -ENOMEM;
585
Miao Xie66d8f3d2012-09-06 04:02:28 -0600586 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
587 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000588 /*
589 * 1 - parent dir inode
590 * 2 - dir entries
591 * 1 - root item
592 * 2 - root ref/backref
593 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200594 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000595 */
596 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200597 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400598 &pending_snapshot->qgroup_reserved,
599 false);
Miao Xied5c12072013-02-28 10:04:33 +0000600 if (ret)
601 goto out;
602
Yan, Zhenga22285a2010-05-16 10:48:46 -0400603 pending_snapshot->dentry = dentry;
604 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800605 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000606 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000607 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400608
Miao Xied5c12072013-02-28 10:04:33 +0000609 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400610 if (IS_ERR(trans)) {
611 ret = PTR_ERR(trans);
612 goto fail;
613 }
614
Josef Bacik83515832011-06-14 15:16:14 -0400615 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400616 list_add(&pending_snapshot->list,
617 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400618 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400619 if (async_transid) {
620 *async_transid = trans->transid;
621 ret = btrfs_commit_transaction_async(trans,
622 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000623 if (ret)
624 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400625 } else {
626 ret = btrfs_commit_transaction(trans,
627 root->fs_info->extent_root);
628 }
Miao Xieaec80302013-03-04 09:44:29 +0000629 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400630 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400631
632 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400633 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000634 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400635
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500636 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
637 if (ret)
638 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639
Al Viro2fbe8c82011-07-16 21:38:06 -0400640 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000641 if (IS_ERR(inode)) {
642 ret = PTR_ERR(inode);
643 goto fail;
644 }
645 BUG_ON(!inode);
646 d_instantiate(dentry, inode);
647 ret = 0;
648fail:
Miao Xied5c12072013-02-28 10:04:33 +0000649 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
650 &pending_snapshot->block_rsv,
651 pending_snapshot->qgroup_reserved);
652out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400653 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654 return ret;
655}
656
Sage Weil4260f7c2010-10-29 15:46:43 -0400657/* copy of check_sticky in fs/namei.c()
658* It's inline, so penalty for filesystems that don't use sticky bit is
659* minimal.
660*/
661static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
662{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800663 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400664
665 if (!(dir->i_mode & S_ISVTX))
666 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800667 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400668 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800669 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400670 return 0;
671 return !capable(CAP_FOWNER);
672}
673
674/* copy of may_delete in fs/namei.c()
675 * Check whether we can remove a link victim from directory dir, check
676 * whether the type of victim is right.
677 * 1. We can't do it if dir is read-only (done in permission())
678 * 2. We should have write and exec permissions on dir
679 * 3. We can't remove anything from append-only dir
680 * 4. We can't do anything with immutable dir (done in permission())
681 * 5. If the sticky bit on dir is set we should either
682 * a. be owner of dir, or
683 * b. be owner of victim, or
684 * c. have CAP_FOWNER capability
685 * 6. If the victim is append-only or immutable we can't do antyhing with
686 * links pointing to it.
687 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
688 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
689 * 9. We can't remove a root or mountpoint.
690 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
691 * nfs_async_unlink().
692 */
693
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530694static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400695{
696 int error;
697
698 if (!victim->d_inode)
699 return -ENOENT;
700
701 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400702 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400703
704 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
705 if (error)
706 return error;
707 if (IS_APPEND(dir))
708 return -EPERM;
709 if (btrfs_check_sticky(dir, victim->d_inode)||
710 IS_APPEND(victim->d_inode)||
711 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
712 return -EPERM;
713 if (isdir) {
714 if (!S_ISDIR(victim->d_inode->i_mode))
715 return -ENOTDIR;
716 if (IS_ROOT(victim))
717 return -EBUSY;
718 } else if (S_ISDIR(victim->d_inode->i_mode))
719 return -EISDIR;
720 if (IS_DEADDIR(dir))
721 return -ENOENT;
722 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
723 return -EBUSY;
724 return 0;
725}
726
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400727/* copy of may_create in fs/namei.c() */
728static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
729{
730 if (child->d_inode)
731 return -EEXIST;
732 if (IS_DEADDIR(dir))
733 return -ENOENT;
734 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
735}
736
737/*
738 * Create a new subvolume below @parent. This is largely modeled after
739 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
740 * inside this filesystem so it's quite a bit simpler.
741 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400742static noinline int btrfs_mksubvol(struct path *parent,
743 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400744 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200745 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000746 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400747{
Yan, Zheng76dda932009-09-21 16:00:26 -0400748 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400749 struct dentry *dentry;
750 int error;
751
David Sterba5c50c9b2013-03-22 18:12:51 +0000752 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
753 if (error == -EINTR)
754 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400755
756 dentry = lookup_one_len(name, parent->dentry, namelen);
757 error = PTR_ERR(dentry);
758 if (IS_ERR(dentry))
759 goto out_unlock;
760
761 error = -EEXIST;
762 if (dentry->d_inode)
763 goto out_dput;
764
Yan, Zheng76dda932009-09-21 16:00:26 -0400765 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400766 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600767 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400768
Chris Mason9c520572012-12-17 14:26:57 -0500769 /*
770 * even if this name doesn't exist, we may get hash collisions.
771 * check for them now when we can safely fail
772 */
773 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
774 dir->i_ino, name,
775 namelen);
776 if (error)
777 goto out_dput;
778
Yan, Zheng76dda932009-09-21 16:00:26 -0400779 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
780
781 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
782 goto out_up_read;
783
Chris Mason3de45862008-11-17 21:02:50 -0500784 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000785 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200786 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500787 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000788 error = create_subvol(dir, dentry, name, namelen,
789 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500790 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400791 if (!error)
792 fsnotify_mkdir(dir, dentry);
793out_up_read:
794 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400795out_dput:
796 dput(dentry);
797out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400798 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400799 return error;
800}
801
Chris Mason4cb53002011-05-24 15:35:30 -0400802/*
803 * When we're defragging a range, we don't want to kick it off again
804 * if it is really just waiting for delalloc to send it down.
805 * If we find a nice big extent or delalloc range for the bytes in the
806 * file you want to defrag, we return 0 to let you know to skip this
807 * part of the file
808 */
809static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
810{
811 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
812 struct extent_map *em = NULL;
813 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
814 u64 end;
815
816 read_lock(&em_tree->lock);
817 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
818 read_unlock(&em_tree->lock);
819
820 if (em) {
821 end = extent_map_end(em);
822 free_extent_map(em);
823 if (end - offset > thresh)
824 return 0;
825 }
826 /* if we already have a nice delalloc here, just stop */
827 thresh /= 2;
828 end = count_range_bits(io_tree, &offset, offset + thresh,
829 thresh, EXTENT_DELALLOC, 1);
830 if (end >= thresh)
831 return 0;
832 return 1;
833}
834
835/*
836 * helper function to walk through a file and find extents
837 * newer than a specific transid, and smaller than thresh.
838 *
839 * This is used by the defragging code to find new and small
840 * extents
841 */
842static int find_new_extents(struct btrfs_root *root,
843 struct inode *inode, u64 newer_than,
844 u64 *off, int thresh)
845{
846 struct btrfs_path *path;
847 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400848 struct extent_buffer *leaf;
849 struct btrfs_file_extent_item *extent;
850 int type;
851 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000852 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400853
854 path = btrfs_alloc_path();
855 if (!path)
856 return -ENOMEM;
857
David Sterbaa4689d22011-05-31 17:08:14 +0000858 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400859 min_key.type = BTRFS_EXTENT_DATA_KEY;
860 min_key.offset = *off;
861
Chris Mason4cb53002011-05-24 15:35:30 -0400862 path->keep_locks = 1;
863
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530864 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100865 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400866 if (ret != 0)
867 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000868 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400869 goto none;
870 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
871 goto none;
872
873 leaf = path->nodes[0];
874 extent = btrfs_item_ptr(leaf, path->slots[0],
875 struct btrfs_file_extent_item);
876
877 type = btrfs_file_extent_type(leaf, extent);
878 if (type == BTRFS_FILE_EXTENT_REG &&
879 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
880 check_defrag_in_cache(inode, min_key.offset, thresh)) {
881 *off = min_key.offset;
882 btrfs_free_path(path);
883 return 0;
884 }
885
886 if (min_key.offset == (u64)-1)
887 goto none;
888
889 min_key.offset++;
890 btrfs_release_path(path);
891 }
892none:
893 btrfs_free_path(path);
894 return -ENOENT;
895}
896
Li Zefan6c282eb2012-06-11 16:03:35 +0800897static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400898{
899 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500900 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800901 struct extent_map *em;
902 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500903
904 /*
905 * hopefully we have this extent in the tree already, try without
906 * the full extent lock
907 */
908 read_lock(&em_tree->lock);
909 em = lookup_extent_mapping(em_tree, start, len);
910 read_unlock(&em_tree->lock);
911
912 if (!em) {
913 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100914 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500915 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100916 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500917
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000918 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800919 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500920 }
921
Li Zefan6c282eb2012-06-11 16:03:35 +0800922 return em;
923}
924
925static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
926{
927 struct extent_map *next;
928 bool ret = true;
929
930 /* this is the last extent */
931 if (em->start + em->len >= i_size_read(inode))
932 return false;
933
934 next = defrag_lookup_extent(inode, em->start + em->len);
935 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
936 ret = false;
937
938 free_extent_map(next);
939 return ret;
940}
941
942static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400943 u64 *last_len, u64 *skip, u64 *defrag_end,
944 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800945{
946 struct extent_map *em;
947 int ret = 1;
948 bool next_mergeable = true;
949
950 /*
951 * make sure that once we start defragging an extent, we keep on
952 * defragging it
953 */
954 if (start < *defrag_end)
955 return 1;
956
957 *skip = 0;
958
959 em = defrag_lookup_extent(inode, start);
960 if (!em)
961 return 0;
962
Chris Mason940100a2010-03-10 10:52:59 -0500963 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400964 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500965 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400966 goto out;
967 }
968
Li Zefan6c282eb2012-06-11 16:03:35 +0800969 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500970
971 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800972 * we hit a real extent, if it is big or the next extent is not a
973 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500974 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400975 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800976 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500977 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400978out:
Chris Mason940100a2010-03-10 10:52:59 -0500979 /*
980 * last_len ends up being a counter of how many bytes we've defragged.
981 * every time we choose not to defrag an extent, we reset *last_len
982 * so that the next tiny extent will force a defrag.
983 *
984 * The end result of this is that tiny extents before a single big
985 * extent will force at least part of that big extent to be defragged.
986 */
987 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500988 *defrag_end = extent_map_end(em);
989 } else {
990 *last_len = 0;
991 *skip = extent_map_end(em);
992 *defrag_end = 0;
993 }
994
995 free_extent_map(em);
996 return ret;
997}
998
Chris Mason4cb53002011-05-24 15:35:30 -0400999/*
1000 * it doesn't do much good to defrag one or two pages
1001 * at a time. This pulls in a nice chunk of pages
1002 * to COW and defrag.
1003 *
1004 * It also makes sure the delalloc code has enough
1005 * dirty data to avoid making new small extents as part
1006 * of the defrag
1007 *
1008 * It's a good idea to start RA on this range
1009 * before calling this.
1010 */
1011static int cluster_pages_for_defrag(struct inode *inode,
1012 struct page **pages,
1013 unsigned long start_index,
1014 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001015{
Chris Mason4cb53002011-05-24 15:35:30 -04001016 unsigned long file_end;
1017 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001018 u64 page_start;
1019 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001020 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001021 int ret;
1022 int i;
1023 int i_done;
1024 struct btrfs_ordered_extent *ordered;
1025 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001026 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001027 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001028
Chris Mason4cb53002011-05-24 15:35:30 -04001029 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001030 if (!isize || start_index > file_end)
1031 return 0;
1032
1033 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001034
1035 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001036 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001037 if (ret)
1038 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001039 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001040 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001041
1042 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001043 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001044 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001045again:
Josef Bacika94733d2011-07-11 10:47:06 -04001046 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001047 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001048 if (!page)
1049 break;
1050
Miao Xie600a45e2012-02-16 15:01:24 +08001051 page_start = page_offset(page);
1052 page_end = page_start + PAGE_CACHE_SIZE - 1;
1053 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001054 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001055 ordered = btrfs_lookup_ordered_extent(inode,
1056 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001057 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001058 if (!ordered)
1059 break;
1060
1061 unlock_page(page);
1062 btrfs_start_ordered_extent(inode, ordered, 1);
1063 btrfs_put_ordered_extent(ordered);
1064 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001065 /*
1066 * we unlocked the page above, so we need check if
1067 * it was released or not.
1068 */
1069 if (page->mapping != inode->i_mapping) {
1070 unlock_page(page);
1071 page_cache_release(page);
1072 goto again;
1073 }
Miao Xie600a45e2012-02-16 15:01:24 +08001074 }
1075
Chris Mason4cb53002011-05-24 15:35:30 -04001076 if (!PageUptodate(page)) {
1077 btrfs_readpage(NULL, page);
1078 lock_page(page);
1079 if (!PageUptodate(page)) {
1080 unlock_page(page);
1081 page_cache_release(page);
1082 ret = -EIO;
1083 break;
1084 }
1085 }
Miao Xie600a45e2012-02-16 15:01:24 +08001086
Miao Xie600a45e2012-02-16 15:01:24 +08001087 if (page->mapping != inode->i_mapping) {
1088 unlock_page(page);
1089 page_cache_release(page);
1090 goto again;
1091 }
1092
Chris Mason4cb53002011-05-24 15:35:30 -04001093 pages[i] = page;
1094 i_done++;
1095 }
1096 if (!i_done || ret)
1097 goto out;
1098
1099 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1100 goto out;
1101
1102 /*
1103 * so now we have a nice long stream of locked
1104 * and up to date pages, lets wait on them
1105 */
1106 for (i = 0; i < i_done; i++)
1107 wait_on_page_writeback(pages[i]);
1108
1109 page_start = page_offset(pages[0]);
1110 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1111
1112 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001113 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001114 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1115 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001116 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1117 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001118
Liu Bo1f12bd02012-03-29 09:57:44 -04001119 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001120 spin_lock(&BTRFS_I(inode)->lock);
1121 BTRFS_I(inode)->outstanding_extents++;
1122 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001123 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001124 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001125 }
1126
1127
Liu Bo9e8a4a82012-09-05 19:10:51 -06001128 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1129 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001130
1131 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1132 page_start, page_end - 1, &cached_state,
1133 GFP_NOFS);
1134
1135 for (i = 0; i < i_done; i++) {
1136 clear_page_dirty_for_io(pages[i]);
1137 ClearPageChecked(pages[i]);
1138 set_page_extent_mapped(pages[i]);
1139 set_page_dirty(pages[i]);
1140 unlock_page(pages[i]);
1141 page_cache_release(pages[i]);
1142 }
1143 return i_done;
1144out:
1145 for (i = 0; i < i_done; i++) {
1146 unlock_page(pages[i]);
1147 page_cache_release(pages[i]);
1148 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001149 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001150 return ret;
1151
1152}
1153
1154int btrfs_defrag_file(struct inode *inode, struct file *file,
1155 struct btrfs_ioctl_defrag_range_args *range,
1156 u64 newer_than, unsigned long max_to_defrag)
1157{
1158 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001159 struct file_ra_state *ra = NULL;
1160 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001161 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001162 u64 last_len = 0;
1163 u64 skip = 0;
1164 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001165 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001166 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001167 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001168 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001169 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001170 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001171 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001172 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1173 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001174 u64 new_align = ~((u64)128 * 1024 - 1);
1175 struct page **pages = NULL;
1176
Liu Bo0abd5b12013-04-16 09:20:28 +00001177 if (isize == 0)
1178 return 0;
1179
1180 if (range->start >= isize)
1181 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001182
1183 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1184 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1185 return -EINVAL;
1186 if (range->compress_type)
1187 compress_type = range->compress_type;
1188 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001189
Liu Bo0abd5b12013-04-16 09:20:28 +00001190 if (extent_thresh == 0)
1191 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001192
Chris Mason4cb53002011-05-24 15:35:30 -04001193 /*
1194 * if we were not given a file, allocate a readahead
1195 * context
1196 */
1197 if (!file) {
1198 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1199 if (!ra)
1200 return -ENOMEM;
1201 file_ra_state_init(ra, inode->i_mapping);
1202 } else {
1203 ra = &file->f_ra;
1204 }
1205
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301206 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001207 GFP_NOFS);
1208 if (!pages) {
1209 ret = -ENOMEM;
1210 goto out_ra;
1211 }
1212
1213 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001214 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001215 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001216 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1217 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001218 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001219 }
1220
Chris Mason4cb53002011-05-24 15:35:30 -04001221 if (newer_than) {
1222 ret = find_new_extents(root, inode, newer_than,
1223 &newer_off, 64 * 1024);
1224 if (!ret) {
1225 range->start = newer_off;
1226 /*
1227 * we always align our defrag to help keep
1228 * the extents in the file evenly spaced
1229 */
1230 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001231 } else
1232 goto out_ra;
1233 } else {
1234 i = range->start >> PAGE_CACHE_SHIFT;
1235 }
1236 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001237 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001238
Li Zefan2a0f7f52011-10-10 15:43:34 -04001239 /*
1240 * make writeback starts from i, so the defrag range can be
1241 * written sequentially.
1242 */
1243 if (i < inode->i_mapping->writeback_index)
1244 inode->i_mapping->writeback_index = i;
1245
Chris Masonf7f43cc2011-10-11 11:41:40 -04001246 while (i <= last_index && defrag_count < max_to_defrag &&
1247 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1248 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001249 /*
1250 * make sure we stop running if someone unmounts
1251 * the FS
1252 */
1253 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1254 break;
1255
David Sterba210549e2013-02-09 23:38:06 +00001256 if (btrfs_defrag_cancelled(root->fs_info)) {
1257 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1258 ret = -EAGAIN;
1259 break;
1260 }
1261
Liu Bo66c26892012-03-29 09:57:45 -04001262 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001263 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001264 &defrag_end, range->flags &
1265 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001266 unsigned long next;
1267 /*
1268 * the should_defrag function tells us how much to skip
1269 * bump our counter by the suggested amount
1270 */
1271 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1272 i = max(i + 1, next);
1273 continue;
1274 }
Li Zefan008873e2011-09-02 15:57:07 +08001275
1276 if (!newer_than) {
1277 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1278 PAGE_CACHE_SHIFT) - i;
1279 cluster = min(cluster, max_cluster);
1280 } else {
1281 cluster = max_cluster;
1282 }
1283
Li Zefan008873e2011-09-02 15:57:07 +08001284 if (i + cluster > ra_index) {
1285 ra_index = max(i, ra_index);
1286 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1287 cluster);
1288 ra_index += max_cluster;
1289 }
Chris Mason940100a2010-03-10 10:52:59 -05001290
Liu Boecb8bea2012-03-29 09:57:44 -04001291 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001292 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1293 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001294 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001295 if (ret < 0) {
1296 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001297 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001298 }
Chris Mason940100a2010-03-10 10:52:59 -05001299
Chris Mason4cb53002011-05-24 15:35:30 -04001300 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001301 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001302 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001303
1304 if (newer_than) {
1305 if (newer_off == (u64)-1)
1306 break;
1307
Liu Boe1f041e2012-03-29 09:57:45 -04001308 if (ret > 0)
1309 i += ret;
1310
Chris Mason4cb53002011-05-24 15:35:30 -04001311 newer_off = max(newer_off + 1,
1312 (u64)i << PAGE_CACHE_SHIFT);
1313
1314 ret = find_new_extents(root, inode,
1315 newer_than, &newer_off,
1316 64 * 1024);
1317 if (!ret) {
1318 range->start = newer_off;
1319 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001320 } else {
1321 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001322 }
Chris Mason4cb53002011-05-24 15:35:30 -04001323 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001324 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001325 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001326 last_len += ret << PAGE_CACHE_SHIFT;
1327 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001328 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001329 last_len = 0;
1330 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001331 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001332 }
1333
Chris Mason1e701a32010-03-11 09:42:04 -05001334 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1335 filemap_flush(inode->i_mapping);
1336
1337 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1338 /* the filemap_flush will queue IO into the worker threads, but
1339 * we have to make sure the IO is actually started and that
1340 * ordered extents get created before we return
1341 */
1342 atomic_inc(&root->fs_info->async_submit_draining);
1343 while (atomic_read(&root->fs_info->nr_async_submits) ||
1344 atomic_read(&root->fs_info->async_delalloc_pages)) {
1345 wait_event(root->fs_info->async_submit_wait,
1346 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1347 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1348 }
1349 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001350 }
1351
Li Zefan1a419d82010-10-25 15:12:50 +08001352 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001353 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001354 }
1355
Diego Calleja60ccf822011-09-01 16:33:57 +02001356 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001357
Chris Mason4cb53002011-05-24 15:35:30 -04001358out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001359 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1360 mutex_lock(&inode->i_mutex);
1361 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1362 mutex_unlock(&inode->i_mutex);
1363 }
Chris Mason4cb53002011-05-24 15:35:30 -04001364 if (!file)
1365 kfree(ra);
1366 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001367 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001368}
1369
Miao Xie198605a2012-11-26 08:43:45 +00001370static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001371 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372{
1373 u64 new_size;
1374 u64 old_size;
1375 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001376 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001377 struct btrfs_ioctl_vol_args *vol_args;
1378 struct btrfs_trans_handle *trans;
1379 struct btrfs_device *device = NULL;
1380 char *sizestr;
1381 char *devstr = NULL;
1382 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383 int mod = 0;
1384
Chris Masone441d542009-01-05 16:57:23 -05001385 if (!capable(CAP_SYS_ADMIN))
1386 return -EPERM;
1387
Miao Xie198605a2012-11-26 08:43:45 +00001388 ret = mnt_want_write_file(file);
1389 if (ret)
1390 return ret;
1391
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001392 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1393 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001394 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001395 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001396 }
1397
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001398 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001399 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001400 if (IS_ERR(vol_args)) {
1401 ret = PTR_ERR(vol_args);
1402 goto out;
1403 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001404
1405 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001407 sizestr = vol_args->name;
1408 devstr = strchr(sizestr, ':');
1409 if (devstr) {
1410 char *end;
1411 sizestr = devstr + 1;
1412 *devstr = '\0';
1413 devstr = vol_args->name;
1414 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001415 if (!devid) {
1416 ret = -EINVAL;
1417 goto out_free;
1418 }
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001419 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001420 }
Miao Xiedba60f32012-12-21 09:19:51 +00001421
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001422 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001424 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001425 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001426 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001427 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428 }
Miao Xiedba60f32012-12-21 09:19:51 +00001429
1430 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001431 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001432 "readonly device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001433 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001434 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001435 goto out_free;
1436 }
1437
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001438 if (!strcmp(sizestr, "max"))
1439 new_size = device->bdev->bd_inode->i_size;
1440 else {
1441 if (sizestr[0] == '-') {
1442 mod = -1;
1443 sizestr++;
1444 } else if (sizestr[0] == '+') {
1445 mod = 1;
1446 sizestr++;
1447 }
Akinobu Mita91748462010-02-28 10:59:11 +00001448 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001449 if (new_size == 0) {
1450 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001451 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001452 }
1453 }
1454
Stefan Behrens63a212a2012-11-05 18:29:28 +01001455 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001456 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001457 goto out_free;
1458 }
1459
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001460 old_size = device->total_bytes;
1461
1462 if (mod < 0) {
1463 if (new_size > old_size) {
1464 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001465 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001466 }
1467 new_size = old_size - new_size;
1468 } else if (mod > 0) {
1469 new_size = old_size + new_size;
1470 }
1471
1472 if (new_size < 256 * 1024 * 1024) {
1473 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001474 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001475 }
1476 if (new_size > device->bdev->bd_inode->i_size) {
1477 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001478 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001479 }
1480
1481 do_div(new_size, root->sectorsize);
1482 new_size *= root->sectorsize;
1483
Josef Bacik606686e2012-06-04 14:03:51 -04001484 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001485 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001486
1487 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001488 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001489 if (IS_ERR(trans)) {
1490 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001491 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001492 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001493 ret = btrfs_grow_device(trans, device, new_size);
1494 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001495 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001496 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001497 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001498
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001499out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001500 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001501out:
1502 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001503 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001504 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505 return ret;
1506}
1507
Sage Weil72fd0322010-10-29 15:41:32 -04001508static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001509 char *name, unsigned long fd, int subvol,
1510 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001511 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001513 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001514 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001515
Liu Boa874a632012-06-29 03:58:46 -06001516 ret = mnt_want_write_file(file);
1517 if (ret)
1518 goto out;
1519
Sage Weil72fd0322010-10-29 15:41:32 -04001520 namelen = strlen(name);
1521 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001522 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001523 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001524 }
1525
Chris Mason16780ca2012-02-20 22:14:55 -05001526 if (name[0] == '.' &&
1527 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1528 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001529 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001530 }
1531
Chris Mason3de45862008-11-17 21:02:50 -05001532 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001533 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001534 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001535 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001536 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001537 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001538 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001539 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001540 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001541 }
1542
Al Viro496ad9a2013-01-23 17:07:38 -05001543 src_inode = file_inode(src.file);
1544 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001545 printk(KERN_INFO "btrfs: Snapshot src from "
1546 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001547 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001548 } else {
1549 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1550 BTRFS_I(src_inode)->root,
1551 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001552 }
Al Viro2903ff02012-08-28 12:52:22 -04001553 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001554 }
Liu Boa874a632012-06-29 03:58:46 -06001555out_drop_write:
1556 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001557out:
Sage Weil72fd0322010-10-29 15:41:32 -04001558 return ret;
1559}
1560
1561static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001562 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001563{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001564 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001565 int ret;
1566
Li Zefanfa0d2b92010-12-20 15:53:28 +08001567 vol_args = memdup_user(arg, sizeof(*vol_args));
1568 if (IS_ERR(vol_args))
1569 return PTR_ERR(vol_args);
1570 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001571
Li Zefanfa0d2b92010-12-20 15:53:28 +08001572 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001573 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001574 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001575
Li Zefanfa0d2b92010-12-20 15:53:28 +08001576 kfree(vol_args);
1577 return ret;
1578}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001579
Li Zefanfa0d2b92010-12-20 15:53:28 +08001580static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1581 void __user *arg, int subvol)
1582{
1583 struct btrfs_ioctl_vol_args_v2 *vol_args;
1584 int ret;
1585 u64 transid = 0;
1586 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001587 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001588 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001589
Li Zefanfa0d2b92010-12-20 15:53:28 +08001590 vol_args = memdup_user(arg, sizeof(*vol_args));
1591 if (IS_ERR(vol_args))
1592 return PTR_ERR(vol_args);
1593 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001594
Li Zefanb83cc962010-12-20 16:04:08 +08001595 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001596 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1597 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001598 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001599 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001600 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001601
1602 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1603 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001604 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1605 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001606 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1607 if (vol_args->size > PAGE_CACHE_SIZE) {
1608 ret = -EINVAL;
1609 goto out;
1610 }
1611 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1612 if (IS_ERR(inherit)) {
1613 ret = PTR_ERR(inherit);
1614 goto out;
1615 }
1616 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001617
1618 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001619 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001620 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001621
1622 if (ret == 0 && ptr &&
1623 copy_to_user(arg +
1624 offsetof(struct btrfs_ioctl_vol_args_v2,
1625 transid), ptr, sizeof(*ptr)))
1626 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001627out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001628 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001629 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001630 return ret;
1631}
1632
Li Zefan0caa1022010-12-20 16:30:25 +08001633static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1634 void __user *arg)
1635{
Al Viro496ad9a2013-01-23 17:07:38 -05001636 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001637 struct btrfs_root *root = BTRFS_I(inode)->root;
1638 int ret = 0;
1639 u64 flags = 0;
1640
Li Zefan33345d012011-04-20 10:31:50 +08001641 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001642 return -EINVAL;
1643
1644 down_read(&root->fs_info->subvol_sem);
1645 if (btrfs_root_readonly(root))
1646 flags |= BTRFS_SUBVOL_RDONLY;
1647 up_read(&root->fs_info->subvol_sem);
1648
1649 if (copy_to_user(arg, &flags, sizeof(flags)))
1650 ret = -EFAULT;
1651
1652 return ret;
1653}
1654
1655static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1656 void __user *arg)
1657{
Al Viro496ad9a2013-01-23 17:07:38 -05001658 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001659 struct btrfs_root *root = BTRFS_I(inode)->root;
1660 struct btrfs_trans_handle *trans;
1661 u64 root_flags;
1662 u64 flags;
1663 int ret = 0;
1664
Liu Bob9ca0662012-06-29 03:58:49 -06001665 ret = mnt_want_write_file(file);
1666 if (ret)
1667 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001668
Liu Bob9ca0662012-06-29 03:58:49 -06001669 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1670 ret = -EINVAL;
1671 goto out_drop_write;
1672 }
Li Zefan0caa1022010-12-20 16:30:25 +08001673
Liu Bob9ca0662012-06-29 03:58:49 -06001674 if (copy_from_user(&flags, arg, sizeof(flags))) {
1675 ret = -EFAULT;
1676 goto out_drop_write;
1677 }
Li Zefan0caa1022010-12-20 16:30:25 +08001678
Liu Bob9ca0662012-06-29 03:58:49 -06001679 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1680 ret = -EINVAL;
1681 goto out_drop_write;
1682 }
Li Zefan0caa1022010-12-20 16:30:25 +08001683
Liu Bob9ca0662012-06-29 03:58:49 -06001684 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1685 ret = -EOPNOTSUPP;
1686 goto out_drop_write;
1687 }
Li Zefan0caa1022010-12-20 16:30:25 +08001688
Liu Bob9ca0662012-06-29 03:58:49 -06001689 if (!inode_owner_or_capable(inode)) {
1690 ret = -EACCES;
1691 goto out_drop_write;
1692 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001693
Li Zefan0caa1022010-12-20 16:30:25 +08001694 down_write(&root->fs_info->subvol_sem);
1695
1696 /* nothing to do */
1697 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001698 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001699
1700 root_flags = btrfs_root_flags(&root->root_item);
1701 if (flags & BTRFS_SUBVOL_RDONLY)
1702 btrfs_set_root_flags(&root->root_item,
1703 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1704 else
1705 btrfs_set_root_flags(&root->root_item,
1706 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1707
1708 trans = btrfs_start_transaction(root, 1);
1709 if (IS_ERR(trans)) {
1710 ret = PTR_ERR(trans);
1711 goto out_reset;
1712 }
1713
Li Zefanb4dc2b82011-02-16 06:06:34 +00001714 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001715 &root->root_key, &root->root_item);
1716
1717 btrfs_commit_transaction(trans, root);
1718out_reset:
1719 if (ret)
1720 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001721out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001722 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001723out_drop_write:
1724 mnt_drop_write_file(file);
1725out:
Li Zefan0caa1022010-12-20 16:30:25 +08001726 return ret;
1727}
1728
Yan, Zheng76dda932009-09-21 16:00:26 -04001729/*
1730 * helper to check if the subvolume references other subvolumes
1731 */
1732static noinline int may_destroy_subvol(struct btrfs_root *root)
1733{
1734 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001735 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001736 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001737 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001738 int ret;
1739
1740 path = btrfs_alloc_path();
1741 if (!path)
1742 return -ENOMEM;
1743
Josef Bacik175a2b82013-08-12 15:36:44 -04001744 /* Make sure this root isn't set as the default subvol */
1745 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1746 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1747 dir_id, "default", 7, 0);
1748 if (di && !IS_ERR(di)) {
1749 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1750 if (key.objectid == root->root_key.objectid) {
1751 ret = -ENOTEMPTY;
1752 goto out;
1753 }
1754 btrfs_release_path(path);
1755 }
1756
Yan, Zheng76dda932009-09-21 16:00:26 -04001757 key.objectid = root->root_key.objectid;
1758 key.type = BTRFS_ROOT_REF_KEY;
1759 key.offset = (u64)-1;
1760
1761 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1762 &key, path, 0, 0);
1763 if (ret < 0)
1764 goto out;
1765 BUG_ON(ret == 0);
1766
1767 ret = 0;
1768 if (path->slots[0] > 0) {
1769 path->slots[0]--;
1770 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1771 if (key.objectid == root->root_key.objectid &&
1772 key.type == BTRFS_ROOT_REF_KEY)
1773 ret = -ENOTEMPTY;
1774 }
1775out:
1776 btrfs_free_path(path);
1777 return ret;
1778}
1779
Chris Masonac8e9812010-02-28 15:39:26 -05001780static noinline int key_in_sk(struct btrfs_key *key,
1781 struct btrfs_ioctl_search_key *sk)
1782{
Chris Masonabc6e132010-03-18 12:10:08 -04001783 struct btrfs_key test;
1784 int ret;
1785
1786 test.objectid = sk->min_objectid;
1787 test.type = sk->min_type;
1788 test.offset = sk->min_offset;
1789
1790 ret = btrfs_comp_cpu_keys(key, &test);
1791 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001792 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001793
1794 test.objectid = sk->max_objectid;
1795 test.type = sk->max_type;
1796 test.offset = sk->max_offset;
1797
1798 ret = btrfs_comp_cpu_keys(key, &test);
1799 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001800 return 0;
1801 return 1;
1802}
1803
1804static noinline int copy_to_sk(struct btrfs_root *root,
1805 struct btrfs_path *path,
1806 struct btrfs_key *key,
1807 struct btrfs_ioctl_search_key *sk,
1808 char *buf,
1809 unsigned long *sk_offset,
1810 int *num_found)
1811{
1812 u64 found_transid;
1813 struct extent_buffer *leaf;
1814 struct btrfs_ioctl_search_header sh;
1815 unsigned long item_off;
1816 unsigned long item_len;
1817 int nritems;
1818 int i;
1819 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001820 int ret = 0;
1821
1822 leaf = path->nodes[0];
1823 slot = path->slots[0];
1824 nritems = btrfs_header_nritems(leaf);
1825
1826 if (btrfs_header_generation(leaf) > sk->max_transid) {
1827 i = nritems;
1828 goto advance_key;
1829 }
1830 found_transid = btrfs_header_generation(leaf);
1831
1832 for (i = slot; i < nritems; i++) {
1833 item_off = btrfs_item_ptr_offset(leaf, i);
1834 item_len = btrfs_item_size_nr(leaf, i);
1835
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001836 btrfs_item_key_to_cpu(leaf, key, i);
1837 if (!key_in_sk(key, sk))
1838 continue;
1839
1840 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001841 item_len = 0;
1842
1843 if (sizeof(sh) + item_len + *sk_offset >
1844 BTRFS_SEARCH_ARGS_BUFSIZE) {
1845 ret = 1;
1846 goto overflow;
1847 }
1848
Chris Masonac8e9812010-02-28 15:39:26 -05001849 sh.objectid = key->objectid;
1850 sh.offset = key->offset;
1851 sh.type = key->type;
1852 sh.len = item_len;
1853 sh.transid = found_transid;
1854
1855 /* copy search result header */
1856 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1857 *sk_offset += sizeof(sh);
1858
1859 if (item_len) {
1860 char *p = buf + *sk_offset;
1861 /* copy the item */
1862 read_extent_buffer(leaf, p,
1863 item_off, item_len);
1864 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001865 }
Hugo Millse2156862011-05-14 17:43:41 +00001866 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001867
1868 if (*num_found >= sk->nr_items)
1869 break;
1870 }
1871advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001872 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001873 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1874 key->offset++;
1875 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1876 key->offset = 0;
1877 key->type++;
1878 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1879 key->offset = 0;
1880 key->type = 0;
1881 key->objectid++;
1882 } else
1883 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001884overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001885 return ret;
1886}
1887
1888static noinline int search_ioctl(struct inode *inode,
1889 struct btrfs_ioctl_search_args *args)
1890{
1891 struct btrfs_root *root;
1892 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001893 struct btrfs_path *path;
1894 struct btrfs_ioctl_search_key *sk = &args->key;
1895 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1896 int ret;
1897 int num_found = 0;
1898 unsigned long sk_offset = 0;
1899
1900 path = btrfs_alloc_path();
1901 if (!path)
1902 return -ENOMEM;
1903
1904 if (sk->tree_id == 0) {
1905 /* search the root of the inode that was passed */
1906 root = BTRFS_I(inode)->root;
1907 } else {
1908 key.objectid = sk->tree_id;
1909 key.type = BTRFS_ROOT_ITEM_KEY;
1910 key.offset = (u64)-1;
1911 root = btrfs_read_fs_root_no_name(info, &key);
1912 if (IS_ERR(root)) {
1913 printk(KERN_ERR "could not find root %llu\n",
1914 sk->tree_id);
1915 btrfs_free_path(path);
1916 return -ENOENT;
1917 }
1918 }
1919
1920 key.objectid = sk->min_objectid;
1921 key.type = sk->min_type;
1922 key.offset = sk->min_offset;
1923
Chris Masonac8e9812010-02-28 15:39:26 -05001924 path->keep_locks = 1;
1925
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301926 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001927 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05001928 if (ret != 0) {
1929 if (ret > 0)
1930 ret = 0;
1931 goto err;
1932 }
1933 ret = copy_to_sk(root, path, &key, sk, args->buf,
1934 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001935 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001936 if (ret || num_found >= sk->nr_items)
1937 break;
1938
1939 }
1940 ret = 0;
1941err:
1942 sk->nr_items = num_found;
1943 btrfs_free_path(path);
1944 return ret;
1945}
1946
1947static noinline int btrfs_ioctl_tree_search(struct file *file,
1948 void __user *argp)
1949{
1950 struct btrfs_ioctl_search_args *args;
1951 struct inode *inode;
1952 int ret;
1953
1954 if (!capable(CAP_SYS_ADMIN))
1955 return -EPERM;
1956
Julia Lawall2354d082010-10-29 15:14:18 -04001957 args = memdup_user(argp, sizeof(*args));
1958 if (IS_ERR(args))
1959 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001960
Al Viro496ad9a2013-01-23 17:07:38 -05001961 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001962 ret = search_ioctl(inode, args);
1963 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1964 ret = -EFAULT;
1965 kfree(args);
1966 return ret;
1967}
1968
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001969/*
Chris Masonac8e9812010-02-28 15:39:26 -05001970 * Search INODE_REFs to identify path name of 'dirid' directory
1971 * in a 'tree_id' tree. and sets path name to 'name'.
1972 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001973static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1974 u64 tree_id, u64 dirid, char *name)
1975{
1976 struct btrfs_root *root;
1977 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001978 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001979 int ret = -1;
1980 int slot;
1981 int len;
1982 int total_len = 0;
1983 struct btrfs_inode_ref *iref;
1984 struct extent_buffer *l;
1985 struct btrfs_path *path;
1986
1987 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1988 name[0]='\0';
1989 return 0;
1990 }
1991
1992 path = btrfs_alloc_path();
1993 if (!path)
1994 return -ENOMEM;
1995
Chris Masonac8e9812010-02-28 15:39:26 -05001996 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001997
1998 key.objectid = tree_id;
1999 key.type = BTRFS_ROOT_ITEM_KEY;
2000 key.offset = (u64)-1;
2001 root = btrfs_read_fs_root_no_name(info, &key);
2002 if (IS_ERR(root)) {
2003 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002004 ret = -ENOENT;
2005 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002006 }
2007
2008 key.objectid = dirid;
2009 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002010 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002011
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302012 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002013 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2014 if (ret < 0)
2015 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002016 else if (ret > 0) {
2017 ret = btrfs_previous_item(root, path, dirid,
2018 BTRFS_INODE_REF_KEY);
2019 if (ret < 0)
2020 goto out;
2021 else if (ret > 0) {
2022 ret = -ENOENT;
2023 goto out;
2024 }
2025 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002026
2027 l = path->nodes[0];
2028 slot = path->slots[0];
2029 btrfs_item_key_to_cpu(l, &key, slot);
2030
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002031 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2032 len = btrfs_inode_ref_name_len(l, iref);
2033 ptr -= len + 1;
2034 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002035 if (ptr < name) {
2036 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002037 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002038 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002039
2040 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302041 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002042
2043 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2044 break;
2045
David Sterbab3b4aa72011-04-21 01:20:15 +02002046 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002047 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002048 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002049 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002050 }
Li Zefan77906a502011-07-14 03:16:00 +00002051 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302052 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002053 ret = 0;
2054out:
2055 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002056 return ret;
2057}
2058
2059static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2060 void __user *argp)
2061{
2062 struct btrfs_ioctl_ino_lookup_args *args;
2063 struct inode *inode;
2064 int ret;
2065
2066 if (!capable(CAP_SYS_ADMIN))
2067 return -EPERM;
2068
Julia Lawall2354d082010-10-29 15:14:18 -04002069 args = memdup_user(argp, sizeof(*args));
2070 if (IS_ERR(args))
2071 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002072
Al Viro496ad9a2013-01-23 17:07:38 -05002073 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002074
Chris Mason1b53ac42010-03-18 12:17:05 -04002075 if (args->treeid == 0)
2076 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2077
Chris Masonac8e9812010-02-28 15:39:26 -05002078 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2079 args->treeid, args->objectid,
2080 args->name);
2081
2082 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2083 ret = -EFAULT;
2084
2085 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002086 return ret;
2087}
2088
Yan, Zheng76dda932009-09-21 16:00:26 -04002089static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2090 void __user *arg)
2091{
Al Viro54563d42013-09-01 15:57:51 -04002092 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002093 struct dentry *dentry;
2094 struct inode *dir = parent->d_inode;
2095 struct inode *inode;
2096 struct btrfs_root *root = BTRFS_I(dir)->root;
2097 struct btrfs_root *dest = NULL;
2098 struct btrfs_ioctl_vol_args *vol_args;
2099 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002100 struct btrfs_block_rsv block_rsv;
2101 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002102 int namelen;
2103 int ret;
2104 int err = 0;
2105
Yan, Zheng76dda932009-09-21 16:00:26 -04002106 vol_args = memdup_user(arg, sizeof(*vol_args));
2107 if (IS_ERR(vol_args))
2108 return PTR_ERR(vol_args);
2109
2110 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2111 namelen = strlen(vol_args->name);
2112 if (strchr(vol_args->name, '/') ||
2113 strncmp(vol_args->name, "..", namelen) == 0) {
2114 err = -EINVAL;
2115 goto out;
2116 }
2117
Al Viroa561be72011-11-23 11:57:51 -05002118 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002119 if (err)
2120 goto out;
2121
David Sterba5c50c9b2013-03-22 18:12:51 +00002122 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2123 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002124 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002125 dentry = lookup_one_len(vol_args->name, parent, namelen);
2126 if (IS_ERR(dentry)) {
2127 err = PTR_ERR(dentry);
2128 goto out_unlock_dir;
2129 }
2130
2131 if (!dentry->d_inode) {
2132 err = -ENOENT;
2133 goto out_dput;
2134 }
2135
2136 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002137 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302138 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002139 /*
2140 * Regular user. Only allow this with a special mount
2141 * option, when the user has write+exec access to the
2142 * subvol root, and when rmdir(2) would have been
2143 * allowed.
2144 *
2145 * Note that this is _not_ check that the subvol is
2146 * empty or doesn't contain data that we wouldn't
2147 * otherwise be able to delete.
2148 *
2149 * Users who want to delete empty subvols should try
2150 * rmdir(2).
2151 */
2152 err = -EPERM;
2153 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2154 goto out_dput;
2155
2156 /*
2157 * Do not allow deletion if the parent dir is the same
2158 * as the dir to be deleted. That means the ioctl
2159 * must be called on the dentry referencing the root
2160 * of the subvol, not a random directory contained
2161 * within it.
2162 */
2163 err = -EINVAL;
2164 if (root == dest)
2165 goto out_dput;
2166
2167 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2168 if (err)
2169 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002170 }
2171
Miao Xie5c39da52012-10-22 11:39:53 +00002172 /* check if subvolume may be deleted by a user */
2173 err = btrfs_may_delete(dir, dentry, 1);
2174 if (err)
2175 goto out_dput;
2176
Li Zefan33345d012011-04-20 10:31:50 +08002177 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002178 err = -EINVAL;
2179 goto out_dput;
2180 }
2181
Yan, Zheng76dda932009-09-21 16:00:26 -04002182 mutex_lock(&inode->i_mutex);
2183 err = d_invalidate(dentry);
2184 if (err)
2185 goto out_unlock;
2186
2187 down_write(&root->fs_info->subvol_sem);
2188
2189 err = may_destroy_subvol(dest);
2190 if (err)
2191 goto out_up_write;
2192
Miao Xiec58aaad2013-02-28 10:05:36 +00002193 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2194 /*
2195 * One for dir inode, two for dir entries, two for root
2196 * ref/backref.
2197 */
2198 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002199 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002200 if (err)
2201 goto out_up_write;
2202
Yan, Zhenga22285a2010-05-16 10:48:46 -04002203 trans = btrfs_start_transaction(root, 0);
2204 if (IS_ERR(trans)) {
2205 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002206 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002207 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002208 trans->block_rsv = &block_rsv;
2209 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002210
Yan, Zheng76dda932009-09-21 16:00:26 -04002211 ret = btrfs_unlink_subvol(trans, root, dir,
2212 dest->root_key.objectid,
2213 dentry->d_name.name,
2214 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002215 if (ret) {
2216 err = ret;
2217 btrfs_abort_transaction(trans, root, ret);
2218 goto out_end_trans;
2219 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002220
2221 btrfs_record_root_in_trans(trans, dest);
2222
2223 memset(&dest->root_item.drop_progress, 0,
2224 sizeof(dest->root_item.drop_progress));
2225 dest->root_item.drop_level = 0;
2226 btrfs_set_root_refs(&dest->root_item, 0);
2227
Yan, Zhengd68fc572010-05-16 10:49:58 -04002228 if (!xchg(&dest->orphan_item_inserted, 1)) {
2229 ret = btrfs_insert_orphan_item(trans,
2230 root->fs_info->tree_root,
2231 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002232 if (ret) {
2233 btrfs_abort_transaction(trans, root, ret);
2234 err = ret;
2235 goto out_end_trans;
2236 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002238
2239 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2240 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2241 dest->root_key.objectid);
2242 if (ret && ret != -ENOENT) {
2243 btrfs_abort_transaction(trans, root, ret);
2244 err = ret;
2245 goto out_end_trans;
2246 }
2247 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2248 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2249 dest->root_item.received_uuid,
2250 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2251 dest->root_key.objectid);
2252 if (ret && ret != -ENOENT) {
2253 btrfs_abort_transaction(trans, root, ret);
2254 err = ret;
2255 goto out_end_trans;
2256 }
2257 }
2258
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002259out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002260 trans->block_rsv = NULL;
2261 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002262 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002263 if (ret && !err)
2264 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002265 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002266out_release:
2267 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002268out_up_write:
2269 up_write(&root->fs_info->subvol_sem);
2270out_unlock:
2271 mutex_unlock(&inode->i_mutex);
2272 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002273 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002274 btrfs_invalidate_inodes(dest);
2275 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002276
2277 /* the last ref */
2278 if (dest->cache_inode) {
2279 iput(dest->cache_inode);
2280 dest->cache_inode = NULL;
2281 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002282 }
2283out_dput:
2284 dput(dentry);
2285out_unlock_dir:
2286 mutex_unlock(&dir->i_mutex);
David Sterbae43f9982013-12-06 17:51:32 +01002287out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002288 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002289out:
2290 kfree(vol_args);
2291 return err;
2292}
2293
Chris Mason1e701a32010-03-11 09:42:04 -05002294static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002295{
Al Viro496ad9a2013-01-23 17:07:38 -05002296 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002297 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002298 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002299 int ret;
2300
Ilya Dryomov25122d12013-01-20 15:57:57 +02002301 ret = mnt_want_write_file(file);
2302 if (ret)
2303 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002304
Ilya Dryomov25122d12013-01-20 15:57:57 +02002305 if (btrfs_root_readonly(root)) {
2306 ret = -EROFS;
2307 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002308 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002309
2310 switch (inode->i_mode & S_IFMT) {
2311 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002312 if (!capable(CAP_SYS_ADMIN)) {
2313 ret = -EPERM;
2314 goto out;
2315 }
Eric Sandeende78b512013-01-31 18:21:12 +00002316 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002317 if (ret)
2318 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002319 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002320 break;
2321 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002322 if (!(file->f_mode & FMODE_WRITE)) {
2323 ret = -EINVAL;
2324 goto out;
2325 }
Chris Mason1e701a32010-03-11 09:42:04 -05002326
2327 range = kzalloc(sizeof(*range), GFP_KERNEL);
2328 if (!range) {
2329 ret = -ENOMEM;
2330 goto out;
2331 }
2332
2333 if (argp) {
2334 if (copy_from_user(range, argp,
2335 sizeof(*range))) {
2336 ret = -EFAULT;
2337 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002338 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002339 }
2340 /* compression requires us to start the IO */
2341 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2342 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2343 range->extent_thresh = (u32)-1;
2344 }
2345 } else {
2346 /* the rest are all set to zero by kzalloc */
2347 range->len = (u64)-1;
2348 }
Al Viro496ad9a2013-01-23 17:07:38 -05002349 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002350 range, 0, 0);
2351 if (ret > 0)
2352 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002353 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002354 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002355 default:
2356 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002357 }
Chris Masone441d542009-01-05 16:57:23 -05002358out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002359 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002360 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002361}
2362
Christoph Hellwigb2950862008-12-02 09:54:17 -05002363static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002364{
2365 struct btrfs_ioctl_vol_args *vol_args;
2366 int ret;
2367
Chris Masone441d542009-01-05 16:57:23 -05002368 if (!capable(CAP_SYS_ADMIN))
2369 return -EPERM;
2370
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002371 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2372 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002373 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002374 }
2375
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002376 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002377 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002378 if (IS_ERR(vol_args)) {
2379 ret = PTR_ERR(vol_args);
2380 goto out;
2381 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002382
Mark Fasheh5516e592008-07-24 12:20:14 -04002383 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002384 ret = btrfs_init_new_device(root, vol_args->name);
2385
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002386 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002387out:
2388 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002389 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390 return ret;
2391}
2392
Miao Xieda249272012-11-26 08:44:50 +00002393static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002394{
Al Viro496ad9a2013-01-23 17:07:38 -05002395 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002396 struct btrfs_ioctl_vol_args *vol_args;
2397 int ret;
2398
Chris Masone441d542009-01-05 16:57:23 -05002399 if (!capable(CAP_SYS_ADMIN))
2400 return -EPERM;
2401
Miao Xieda249272012-11-26 08:44:50 +00002402 ret = mnt_want_write_file(file);
2403 if (ret)
2404 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002405
Li Zefandae7b662009-04-08 15:06:54 +08002406 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002407 if (IS_ERR(vol_args)) {
2408 ret = PTR_ERR(vol_args);
2409 goto out;
2410 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002411
Mark Fasheh5516e592008-07-24 12:20:14 -04002412 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413
Anand Jain183860f2013-05-17 10:52:45 +00002414 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2415 1)) {
2416 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2417 goto out;
2418 }
2419
2420 mutex_lock(&root->fs_info->volume_mutex);
2421 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002422 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002423 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002424
2425out:
2426 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002427 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002428 return ret;
2429}
2430
Jan Schmidt475f6382011-03-11 15:41:01 +01002431static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2432{
Li Zefan027ed2f2011-06-08 08:27:56 +00002433 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002434 struct btrfs_device *device;
2435 struct btrfs_device *next;
2436 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002437 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002438
2439 if (!capable(CAP_SYS_ADMIN))
2440 return -EPERM;
2441
Li Zefan027ed2f2011-06-08 08:27:56 +00002442 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2443 if (!fi_args)
2444 return -ENOMEM;
2445
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002446 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002447 fi_args->num_devices = fs_devices->num_devices;
2448 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002449
Jan Schmidt475f6382011-03-11 15:41:01 +01002450 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002451 if (device->devid > fi_args->max_id)
2452 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002453 }
2454 mutex_unlock(&fs_devices->device_list_mutex);
2455
Li Zefan027ed2f2011-06-08 08:27:56 +00002456 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2457 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002458
Li Zefan027ed2f2011-06-08 08:27:56 +00002459 kfree(fi_args);
2460 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002461}
2462
2463static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2464{
2465 struct btrfs_ioctl_dev_info_args *di_args;
2466 struct btrfs_device *dev;
2467 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2468 int ret = 0;
2469 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002470
2471 if (!capable(CAP_SYS_ADMIN))
2472 return -EPERM;
2473
2474 di_args = memdup_user(arg, sizeof(*di_args));
2475 if (IS_ERR(di_args))
2476 return PTR_ERR(di_args);
2477
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002478 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002479 s_uuid = di_args->uuid;
2480
2481 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002482 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002483
2484 if (!dev) {
2485 ret = -ENODEV;
2486 goto out;
2487 }
2488
2489 di_args->devid = dev->devid;
2490 di_args->bytes_used = dev->bytes_used;
2491 di_args->total_bytes = dev->total_bytes;
2492 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002493 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002494 struct rcu_string *name;
2495
2496 rcu_read_lock();
2497 name = rcu_dereference(dev->name);
2498 strncpy(di_args->path, name->str, sizeof(di_args->path));
2499 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002500 di_args->path[sizeof(di_args->path) - 1] = 0;
2501 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002502 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002503 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002504
2505out:
David Sterba55793c02013-04-26 15:20:23 +00002506 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002507 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2508 ret = -EFAULT;
2509
2510 kfree(di_args);
2511 return ret;
2512}
2513
Mark Fasheh416161d2013-08-06 11:42:51 -07002514static struct page *extent_same_get_page(struct inode *inode, u64 off)
2515{
2516 struct page *page;
2517 pgoff_t index;
2518 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2519
2520 index = off >> PAGE_CACHE_SHIFT;
2521
2522 page = grab_cache_page(inode->i_mapping, index);
2523 if (!page)
2524 return NULL;
2525
2526 if (!PageUptodate(page)) {
2527 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2528 0))
2529 return NULL;
2530 lock_page(page);
2531 if (!PageUptodate(page)) {
2532 unlock_page(page);
2533 page_cache_release(page);
2534 return NULL;
2535 }
2536 }
2537 unlock_page(page);
2538
2539 return page;
2540}
2541
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002542static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2543{
2544 /* do any pending delalloc/csum calc on src, one way or
2545 another, and lock file content */
2546 while (1) {
2547 struct btrfs_ordered_extent *ordered;
2548 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2549 ordered = btrfs_lookup_first_ordered_extent(inode,
2550 off + len - 1);
2551 if (!ordered &&
2552 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2553 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2554 break;
2555 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2556 if (ordered)
2557 btrfs_put_ordered_extent(ordered);
2558 btrfs_wait_ordered_range(inode, off, len);
2559 }
2560}
2561
Mark Fasheh416161d2013-08-06 11:42:51 -07002562static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2563 struct inode *inode2, u64 loff2, u64 len)
2564{
2565 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2566 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2567
2568 mutex_unlock(&inode1->i_mutex);
2569 mutex_unlock(&inode2->i_mutex);
2570}
2571
2572static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2573 struct inode *inode2, u64 loff2, u64 len)
2574{
2575 if (inode1 < inode2) {
2576 swap(inode1, inode2);
2577 swap(loff1, loff2);
2578 }
2579
2580 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2581 lock_extent_range(inode1, loff1, len);
2582 if (inode1 != inode2) {
2583 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2584 lock_extent_range(inode2, loff2, len);
2585 }
2586}
2587
2588static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2589 u64 dst_loff, u64 len)
2590{
2591 int ret = 0;
2592 struct page *src_page, *dst_page;
2593 unsigned int cmp_len = PAGE_CACHE_SIZE;
2594 void *addr, *dst_addr;
2595
2596 while (len) {
2597 if (len < PAGE_CACHE_SIZE)
2598 cmp_len = len;
2599
2600 src_page = extent_same_get_page(src, loff);
2601 if (!src_page)
2602 return -EINVAL;
2603 dst_page = extent_same_get_page(dst, dst_loff);
2604 if (!dst_page) {
2605 page_cache_release(src_page);
2606 return -EINVAL;
2607 }
2608 addr = kmap_atomic(src_page);
2609 dst_addr = kmap_atomic(dst_page);
2610
2611 flush_dcache_page(src_page);
2612 flush_dcache_page(dst_page);
2613
2614 if (memcmp(addr, dst_addr, cmp_len))
2615 ret = BTRFS_SAME_DATA_DIFFERS;
2616
2617 kunmap_atomic(addr);
2618 kunmap_atomic(dst_addr);
2619 page_cache_release(src_page);
2620 page_cache_release(dst_page);
2621
2622 if (ret)
2623 break;
2624
2625 loff += cmp_len;
2626 dst_loff += cmp_len;
2627 len -= cmp_len;
2628 }
2629
2630 return ret;
2631}
2632
2633static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2634{
2635 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2636
2637 if (off + len > inode->i_size || off + len < off)
2638 return -EINVAL;
2639 /* Check that we are block aligned - btrfs_clone() requires this */
2640 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2641 return -EINVAL;
2642
2643 return 0;
2644}
2645
2646static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2647 struct inode *dst, u64 dst_loff)
2648{
2649 int ret;
2650
2651 /*
2652 * btrfs_clone() can't handle extents in the same file
2653 * yet. Once that works, we can drop this check and replace it
2654 * with a check for the same inode, but overlapping extents.
2655 */
2656 if (src == dst)
2657 return -EINVAL;
2658
2659 btrfs_double_lock(src, loff, dst, dst_loff, len);
2660
2661 ret = extent_same_check_offsets(src, loff, len);
2662 if (ret)
2663 goto out_unlock;
2664
2665 ret = extent_same_check_offsets(dst, dst_loff, len);
2666 if (ret)
2667 goto out_unlock;
2668
2669 /* don't make the dst file partly checksummed */
2670 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2671 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2672 ret = -EINVAL;
2673 goto out_unlock;
2674 }
2675
2676 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2677 if (ret == 0)
2678 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2679
2680out_unlock:
2681 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2682
2683 return ret;
2684}
2685
2686#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2687
2688static long btrfs_ioctl_file_extent_same(struct file *file,
2689 void __user *argp)
2690{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002691 struct btrfs_ioctl_same_args tmp;
2692 struct btrfs_ioctl_same_args *same;
2693 struct btrfs_ioctl_same_extent_info *info;
Mark Fasheh416161d2013-08-06 11:42:51 -07002694 struct inode *src = file->f_dentry->d_inode;
2695 struct file *dst_file = NULL;
2696 struct inode *dst;
2697 u64 off;
2698 u64 len;
2699 int i;
2700 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002701 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002702 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2703 bool is_admin = capable(CAP_SYS_ADMIN);
2704
2705 if (!(file->f_mode & FMODE_READ))
2706 return -EINVAL;
2707
2708 ret = mnt_want_write_file(file);
2709 if (ret)
2710 return ret;
2711
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002712 if (copy_from_user(&tmp,
Mark Fasheh416161d2013-08-06 11:42:51 -07002713 (struct btrfs_ioctl_same_args __user *)argp,
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002714 sizeof(tmp))) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002715 ret = -EFAULT;
2716 goto out;
2717 }
2718
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002719 size = sizeof(tmp) +
2720 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2721
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002722 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002723
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002724 if (IS_ERR(same)) {
2725 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002726 goto out;
2727 }
2728
2729 off = same->logical_offset;
2730 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002731
2732 /*
2733 * Limit the total length we will dedupe for each operation.
2734 * This is intended to bound the total time spent in this
2735 * ioctl to something sane.
2736 */
2737 if (len > BTRFS_MAX_DEDUPE_LEN)
2738 len = BTRFS_MAX_DEDUPE_LEN;
2739
2740 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2741 /*
2742 * Btrfs does not support blocksize < page_size. As a
2743 * result, btrfs_cmp_data() won't correctly handle
2744 * this situation without an update.
2745 */
2746 ret = -EINVAL;
2747 goto out;
2748 }
2749
2750 ret = -EISDIR;
2751 if (S_ISDIR(src->i_mode))
2752 goto out;
2753
2754 ret = -EACCES;
2755 if (!S_ISREG(src->i_mode))
2756 goto out;
2757
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002758 /* pre-format output fields to sane values */
2759 for (i = 0; i < same->dest_count; i++) {
2760 same->info[i].bytes_deduped = 0ULL;
2761 same->info[i].status = 0;
2762 }
2763
Mark Fasheh416161d2013-08-06 11:42:51 -07002764 ret = 0;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002765 for (i = 0; i < same->dest_count; i++) {
2766 info = &same->info[i];
Mark Fasheh416161d2013-08-06 11:42:51 -07002767
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002768 dst_file = fget(info->fd);
Mark Fasheh416161d2013-08-06 11:42:51 -07002769 if (!dst_file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002770 info->status = -EBADF;
Mark Fasheh416161d2013-08-06 11:42:51 -07002771 goto next;
2772 }
2773
2774 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002775 info->status = -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07002776 goto next;
2777 }
2778
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002779 info->status = -EXDEV;
Mark Fasheh416161d2013-08-06 11:42:51 -07002780 if (file->f_path.mnt != dst_file->f_path.mnt)
2781 goto next;
2782
2783 dst = dst_file->f_dentry->d_inode;
2784 if (src->i_sb != dst->i_sb)
2785 goto next;
2786
2787 if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002788 info->status = -EISDIR;
Mark Fasheh416161d2013-08-06 11:42:51 -07002789 goto next;
2790 }
2791
2792 if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002793 info->status = -EACCES;
Mark Fasheh416161d2013-08-06 11:42:51 -07002794 goto next;
2795 }
2796
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002797 info->status = btrfs_extent_same(src, off, len, dst,
2798 info->logical_offset);
2799 if (info->status == 0)
2800 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07002801
2802next:
2803 if (dst_file)
2804 fput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002805 }
2806
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002807 ret = copy_to_user(argp, same, size);
2808 if (ret)
2809 ret = -EFAULT;
2810
Mark Fasheh416161d2013-08-06 11:42:51 -07002811out:
2812 mnt_drop_write_file(file);
2813 return ret;
2814}
2815
Mark Fasheh32b7c682013-08-06 11:42:49 -07002816/**
2817 * btrfs_clone() - clone a range from inode file to another
2818 *
2819 * @src: Inode to clone from
2820 * @inode: Inode to clone to
2821 * @off: Offset within source to start clone from
2822 * @olen: Original length, passed by user, of range to clone
2823 * @olen_aligned: Block-aligned value of olen, extent_same uses
2824 * identical values here
2825 * @destoff: Offset within @inode to start clone
2826 */
2827static int btrfs_clone(struct inode *src, struct inode *inode,
2828 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002829{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002830 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002831 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002832 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002833 struct btrfs_trans_handle *trans;
2834 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002835 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002836 u32 nritems;
2837 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002838 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002839 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002840
2841 ret = -ENOMEM;
2842 buf = vmalloc(btrfs_level_size(root, 0));
2843 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002844 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002845
2846 path = btrfs_alloc_path();
2847 if (!path) {
2848 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002849 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002850 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002851
Yan Zhengae01a0a2008-08-04 23:23:47 -04002852 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002853 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002854 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002855 key.type = BTRFS_EXTENT_DATA_KEY;
2856 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002857
2858 while (1) {
2859 /*
2860 * note the key will change type as we walk through the
2861 * tree.
2862 */
David Sterba362a20c2011-08-01 18:11:57 +02002863 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2864 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002865 if (ret < 0)
2866 goto out;
2867
Yan Zhengae01a0a2008-08-04 23:23:47 -04002868 nritems = btrfs_header_nritems(path->nodes[0]);
2869 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002870 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002871 if (ret < 0)
2872 goto out;
2873 if (ret > 0)
2874 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002875 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002876 }
2877 leaf = path->nodes[0];
2878 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002879
Yan Zhengae01a0a2008-08-04 23:23:47 -04002880 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002881 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002882 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002883 break;
2884
Sage Weilc5c9cd42008-11-12 14:32:25 -05002885 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2886 struct btrfs_file_extent_item *extent;
2887 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002888 u32 size;
2889 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002890 u64 disko = 0, diskl = 0;
2891 u64 datao = 0, datal = 0;
2892 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002893 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002894
2895 size = btrfs_item_size_nr(leaf, slot);
2896 read_extent_buffer(leaf, buf,
2897 btrfs_item_ptr_offset(leaf, slot),
2898 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002899
2900 extent = btrfs_item_ptr(leaf, slot,
2901 struct btrfs_file_extent_item);
2902 comp = btrfs_file_extent_compression(leaf, extent);
2903 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002904 if (type == BTRFS_FILE_EXTENT_REG ||
2905 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002906 disko = btrfs_file_extent_disk_bytenr(leaf,
2907 extent);
2908 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2909 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002910 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002911 datal = btrfs_file_extent_num_bytes(leaf,
2912 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002913 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2914 /* take upper bound, may be compressed */
2915 datal = btrfs_file_extent_ram_bytes(leaf,
2916 extent);
2917 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002918 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002919
Sage Weil050006a2010-10-29 15:37:33 -04002920 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002921 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002922 goto next;
2923
Zheng Yan31840ae2008-09-23 13:14:14 -04002924 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002925 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002926 if (off <= key.offset)
2927 new_key.offset = key.offset + destoff - off;
2928 else
2929 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002930
Sage Weilb6f34092011-09-20 14:48:51 -04002931 /*
2932 * 1 - adjusting old extent (we may have to split it)
2933 * 1 - add new extent
2934 * 1 - inode update
2935 */
2936 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002937 if (IS_ERR(trans)) {
2938 ret = PTR_ERR(trans);
2939 goto out;
2940 }
2941
Chris Masonc8a894d2009-06-27 21:07:03 -04002942 if (type == BTRFS_FILE_EXTENT_REG ||
2943 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002944 /*
2945 * a | --- range to clone ---| b
2946 * | ------------- extent ------------- |
2947 */
2948
2949 /* substract range b */
2950 if (key.offset + datal > off + len)
2951 datal = off + len - key.offset;
2952
2953 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002954 if (off > key.offset) {
2955 datao += off - key.offset;
2956 datal -= off - key.offset;
2957 }
2958
Josef Bacik5dc562c2012-08-17 13:14:17 -04002959 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002960 new_key.offset,
2961 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002962 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002963 if (ret) {
2964 btrfs_abort_transaction(trans, root,
2965 ret);
2966 btrfs_end_transaction(trans, root);
2967 goto out;
2968 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002969
Sage Weilc5c9cd42008-11-12 14:32:25 -05002970 ret = btrfs_insert_empty_item(trans, root, path,
2971 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 if (ret) {
2973 btrfs_abort_transaction(trans, root,
2974 ret);
2975 btrfs_end_transaction(trans, root);
2976 goto out;
2977 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002978
2979 leaf = path->nodes[0];
2980 slot = path->slots[0];
2981 write_extent_buffer(leaf, buf,
2982 btrfs_item_ptr_offset(leaf, slot),
2983 size);
2984
2985 extent = btrfs_item_ptr(leaf, slot,
2986 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002987
Sage Weilc5c9cd42008-11-12 14:32:25 -05002988 /* disko == 0 means it's a hole */
2989 if (!disko)
2990 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002991
2992 btrfs_set_file_extent_offset(leaf, extent,
2993 datao);
2994 btrfs_set_file_extent_num_bytes(leaf, extent,
2995 datal);
2996 if (disko) {
2997 inode_add_bytes(inode, datal);
2998 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 disko, diskl, 0,
3000 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003001 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003002 new_key.offset - datao,
3003 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003004 if (ret) {
3005 btrfs_abort_transaction(trans,
3006 root,
3007 ret);
3008 btrfs_end_transaction(trans,
3009 root);
3010 goto out;
3011
3012 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003013 }
3014 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3015 u64 skip = 0;
3016 u64 trim = 0;
3017 if (off > key.offset) {
3018 skip = off - key.offset;
3019 new_key.offset += skip;
3020 }
Chris Masond3977122009-01-05 21:25:51 -05003021
Liu Boaa42ffd2012-09-18 03:52:23 -06003022 if (key.offset + datal > off + len)
3023 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003024
Sage Weilc5c9cd42008-11-12 14:32:25 -05003025 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003026 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003027 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003028 goto out;
3029 }
3030 size -= skip + trim;
3031 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003032
Josef Bacik5dc562c2012-08-17 13:14:17 -04003033 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003034 new_key.offset,
3035 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003036 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003037 if (ret) {
3038 btrfs_abort_transaction(trans, root,
3039 ret);
3040 btrfs_end_transaction(trans, root);
3041 goto out;
3042 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003043
Sage Weilc5c9cd42008-11-12 14:32:25 -05003044 ret = btrfs_insert_empty_item(trans, root, path,
3045 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003046 if (ret) {
3047 btrfs_abort_transaction(trans, root,
3048 ret);
3049 btrfs_end_transaction(trans, root);
3050 goto out;
3051 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003052
3053 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003054 u32 start =
3055 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003056 memmove(buf+start, buf+start+skip,
3057 datal);
3058 }
3059
3060 leaf = path->nodes[0];
3061 slot = path->slots[0];
3062 write_extent_buffer(leaf, buf,
3063 btrfs_item_ptr_offset(leaf, slot),
3064 size);
3065 inode_add_bytes(inode, datal);
3066 }
3067
3068 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003069 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003070
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003071 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003072 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003073
3074 /*
3075 * we round up to the block size at eof when
3076 * determining which extents to clone above,
3077 * but shouldn't round up the file size
3078 */
3079 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003080 if (endoff > destoff+olen)
3081 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003082 if (endoff > inode->i_size)
3083 btrfs_i_size_write(inode, endoff);
3084
Yan, Zhenga22285a2010-05-16 10:48:46 -04003085 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003086 if (ret) {
3087 btrfs_abort_transaction(trans, root, ret);
3088 btrfs_end_transaction(trans, root);
3089 goto out;
3090 }
3091 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003092 }
Chris Masond3977122009-01-05 21:25:51 -05003093next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003094 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003095 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003096 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003097 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003098
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003099out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003100 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003101 btrfs_free_path(path);
3102 vfree(buf);
3103 return ret;
3104}
3105
3106static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3107 u64 off, u64 olen, u64 destoff)
3108{
Al Viro54563d42013-09-01 15:57:51 -04003109 struct inode *inode = file_inode(file);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003110 struct btrfs_root *root = BTRFS_I(inode)->root;
3111 struct fd src_file;
3112 struct inode *src;
3113 int ret;
3114 u64 len = olen;
3115 u64 bs = root->fs_info->sb->s_blocksize;
3116 int same_inode = 0;
3117
3118 /*
3119 * TODO:
3120 * - split compressed inline extents. annoying: we need to
3121 * decompress into destination's address_space (the file offset
3122 * may change, so source mapping won't do), then recompress (or
3123 * otherwise reinsert) a subrange.
3124 * - allow ranges within the same file to be cloned (provided
3125 * they don't overlap)?
3126 */
3127
3128 /* the destination must be opened for writing */
3129 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3130 return -EINVAL;
3131
3132 if (btrfs_root_readonly(root))
3133 return -EROFS;
3134
3135 ret = mnt_want_write_file(file);
3136 if (ret)
3137 return ret;
3138
3139 src_file = fdget(srcfd);
3140 if (!src_file.file) {
3141 ret = -EBADF;
3142 goto out_drop_write;
3143 }
3144
3145 ret = -EXDEV;
3146 if (src_file.file->f_path.mnt != file->f_path.mnt)
3147 goto out_fput;
3148
3149 src = file_inode(src_file.file);
3150
3151 ret = -EINVAL;
3152 if (src == inode)
3153 same_inode = 1;
3154
3155 /* the src must be open for reading */
3156 if (!(src_file.file->f_mode & FMODE_READ))
3157 goto out_fput;
3158
3159 /* don't make the dst file partly checksummed */
3160 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3161 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3162 goto out_fput;
3163
3164 ret = -EISDIR;
3165 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3166 goto out_fput;
3167
3168 ret = -EXDEV;
3169 if (src->i_sb != inode->i_sb)
3170 goto out_fput;
3171
3172 if (!same_inode) {
3173 if (inode < src) {
3174 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3175 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3176 } else {
3177 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3178 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3179 }
3180 } else {
3181 mutex_lock(&src->i_mutex);
3182 }
3183
3184 /* determine range to clone */
3185 ret = -EINVAL;
3186 if (off + len > src->i_size || off + len < off)
3187 goto out_unlock;
3188 if (len == 0)
3189 olen = len = src->i_size - off;
3190 /* if we extend to eof, continue to block boundary */
3191 if (off + len == src->i_size)
3192 len = ALIGN(src->i_size, bs) - off;
3193
3194 /* verify the end result is block aligned */
3195 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3196 !IS_ALIGNED(destoff, bs))
3197 goto out_unlock;
3198
3199 /* verify if ranges are overlapped within the same file */
3200 if (same_inode) {
3201 if (destoff + len > off && destoff < off + len)
3202 goto out_unlock;
3203 }
3204
3205 if (destoff > inode->i_size) {
3206 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3207 if (ret)
3208 goto out_unlock;
3209 }
3210
3211 /* truncate page cache pages from target inode range */
3212 truncate_inode_pages_range(&inode->i_data, destoff,
3213 PAGE_CACHE_ALIGN(destoff + len) - 1);
3214
3215 lock_extent_range(src, off, len);
3216
3217 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3218
Liu Boaa42ffd2012-09-18 03:52:23 -06003219 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003220out_unlock:
3221 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003222 if (!same_inode)
3223 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003224out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003225 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003226out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003227 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003228 return ret;
3229}
3230
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003231static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003232{
3233 struct btrfs_ioctl_clone_range_args args;
3234
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003235 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003236 return -EFAULT;
3237 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3238 args.src_length, args.dest_offset);
3239}
3240
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003241/*
3242 * there are many ways the trans_start and trans_end ioctls can lead
3243 * to deadlocks. They should only be used by applications that
3244 * basically own the machine, and have a very in depth understanding
3245 * of all the possible deadlocks and enospc problems.
3246 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003247static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003248{
Al Viro496ad9a2013-01-23 17:07:38 -05003249 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003250 struct btrfs_root *root = BTRFS_I(inode)->root;
3251 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003252 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003253
Sage Weil1ab86ae2009-09-29 18:38:44 -04003254 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003255 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003256 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003257
3258 ret = -EINPROGRESS;
3259 if (file->private_data)
3260 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003261
Li Zefanb83cc962010-12-20 16:04:08 +08003262 ret = -EROFS;
3263 if (btrfs_root_readonly(root))
3264 goto out;
3265
Al Viroa561be72011-11-23 11:57:51 -05003266 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003267 if (ret)
3268 goto out;
3269
Josef Bacika4abeea2011-04-11 17:25:13 -04003270 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003271
Sage Weil1ab86ae2009-09-29 18:38:44 -04003272 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003273 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003274 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003275 goto out_drop;
3276
3277 file->private_data = trans;
3278 return 0;
3279
3280out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003281 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003282 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003283out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003284 return ret;
3285}
3286
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003287static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3288{
Al Viro496ad9a2013-01-23 17:07:38 -05003289 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003290 struct btrfs_root *root = BTRFS_I(inode)->root;
3291 struct btrfs_root *new_root;
3292 struct btrfs_dir_item *di;
3293 struct btrfs_trans_handle *trans;
3294 struct btrfs_path *path;
3295 struct btrfs_key location;
3296 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003297 u64 objectid = 0;
3298 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003299 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003300
3301 if (!capable(CAP_SYS_ADMIN))
3302 return -EPERM;
3303
Miao Xie3c04ce02012-11-26 08:43:07 +00003304 ret = mnt_want_write_file(file);
3305 if (ret)
3306 return ret;
3307
3308 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3309 ret = -EFAULT;
3310 goto out;
3311 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003312
3313 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303314 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003315
3316 location.objectid = objectid;
3317 location.type = BTRFS_ROOT_ITEM_KEY;
3318 location.offset = (u64)-1;
3319
3320 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003321 if (IS_ERR(new_root)) {
3322 ret = PTR_ERR(new_root);
3323 goto out;
3324 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003325
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003326 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003327 if (!path) {
3328 ret = -ENOMEM;
3329 goto out;
3330 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003331 path->leave_spinning = 1;
3332
3333 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003334 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003335 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003336 ret = PTR_ERR(trans);
3337 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003338 }
3339
David Sterba6c417612011-04-13 15:41:04 +02003340 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003341 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3342 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003343 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003344 btrfs_free_path(path);
3345 btrfs_end_transaction(trans, root);
3346 printk(KERN_ERR "Umm, you don't have the default dir item, "
3347 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003348 ret = -ENOENT;
3349 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003350 }
3351
3352 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3353 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3354 btrfs_mark_buffer_dirty(path->nodes[0]);
3355 btrfs_free_path(path);
3356
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003357 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003358 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003359out:
3360 mnt_drop_write_file(file);
3361 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003362}
3363
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003364void btrfs_get_block_group_info(struct list_head *groups_list,
3365 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003366{
3367 struct btrfs_block_group_cache *block_group;
3368
3369 space->total_bytes = 0;
3370 space->used_bytes = 0;
3371 space->flags = 0;
3372 list_for_each_entry(block_group, groups_list, list) {
3373 space->flags = block_group->flags;
3374 space->total_bytes += block_group->key.offset;
3375 space->used_bytes +=
3376 btrfs_block_group_used(&block_group->item);
3377 }
3378}
3379
Eric Sandeen48a3b632013-04-25 20:41:01 +00003380static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003381{
3382 struct btrfs_ioctl_space_args space_args;
3383 struct btrfs_ioctl_space_info space;
3384 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003385 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003386 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003387 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003388 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3389 BTRFS_BLOCK_GROUP_SYSTEM,
3390 BTRFS_BLOCK_GROUP_METADATA,
3391 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3392 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003393 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003394 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003395 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003396 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003397
3398 if (copy_from_user(&space_args,
3399 (struct btrfs_ioctl_space_args __user *)arg,
3400 sizeof(space_args)))
3401 return -EFAULT;
3402
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003403 for (i = 0; i < num_types; i++) {
3404 struct btrfs_space_info *tmp;
3405
3406 info = NULL;
3407 rcu_read_lock();
3408 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3409 list) {
3410 if (tmp->flags == types[i]) {
3411 info = tmp;
3412 break;
3413 }
3414 }
3415 rcu_read_unlock();
3416
3417 if (!info)
3418 continue;
3419
3420 down_read(&info->groups_sem);
3421 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3422 if (!list_empty(&info->block_groups[c]))
3423 slot_count++;
3424 }
3425 up_read(&info->groups_sem);
3426 }
Josef Bacik1406e432010-01-13 18:19:06 +00003427
Chris Mason7fde62b2010-03-16 15:40:10 -04003428 /* space_slots == 0 means they are asking for a count */
3429 if (space_args.space_slots == 0) {
3430 space_args.total_spaces = slot_count;
3431 goto out;
3432 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003433
Dan Rosenberg51788b12011-02-14 16:04:23 -05003434 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003435
Chris Mason7fde62b2010-03-16 15:40:10 -04003436 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003437
Chris Mason7fde62b2010-03-16 15:40:10 -04003438 /* we generally have at most 6 or so space infos, one for each raid
3439 * level. So, a whole page should be more than enough for everyone
3440 */
3441 if (alloc_size > PAGE_CACHE_SIZE)
3442 return -ENOMEM;
3443
3444 space_args.total_spaces = 0;
3445 dest = kmalloc(alloc_size, GFP_NOFS);
3446 if (!dest)
3447 return -ENOMEM;
3448 dest_orig = dest;
3449
3450 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003451 for (i = 0; i < num_types; i++) {
3452 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003453
Dan Rosenberg51788b12011-02-14 16:04:23 -05003454 if (!slot_count)
3455 break;
3456
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003457 info = NULL;
3458 rcu_read_lock();
3459 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3460 list) {
3461 if (tmp->flags == types[i]) {
3462 info = tmp;
3463 break;
3464 }
3465 }
3466 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003467
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003468 if (!info)
3469 continue;
3470 down_read(&info->groups_sem);
3471 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3472 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003473 btrfs_get_block_group_info(
3474 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003475 memcpy(dest, &space, sizeof(space));
3476 dest++;
3477 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003478 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003479 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003480 if (!slot_count)
3481 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003482 }
3483 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003484 }
Josef Bacik1406e432010-01-13 18:19:06 +00003485
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003486 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003487 (arg + sizeof(struct btrfs_ioctl_space_args));
3488
3489 if (copy_to_user(user_dest, dest_orig, alloc_size))
3490 ret = -EFAULT;
3491
3492 kfree(dest_orig);
3493out:
3494 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003495 ret = -EFAULT;
3496
3497 return ret;
3498}
3499
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003500/*
3501 * there are many ways the trans_start and trans_end ioctls can lead
3502 * to deadlocks. They should only be used by applications that
3503 * basically own the machine, and have a very in depth understanding
3504 * of all the possible deadlocks and enospc problems.
3505 */
3506long btrfs_ioctl_trans_end(struct file *file)
3507{
Al Viro496ad9a2013-01-23 17:07:38 -05003508 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003509 struct btrfs_root *root = BTRFS_I(inode)->root;
3510 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003511
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003512 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003513 if (!trans)
3514 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003515 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003516
Sage Weil1ab86ae2009-09-29 18:38:44 -04003517 btrfs_end_transaction(trans, root);
3518
Josef Bacika4abeea2011-04-11 17:25:13 -04003519 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003520
Al Viro2a79f172011-12-09 08:06:57 -05003521 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003522 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003523}
3524
Miao Xie9a8c28b2012-11-26 08:40:43 +00003525static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3526 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003527{
Sage Weil46204592010-10-29 15:41:32 -04003528 struct btrfs_trans_handle *trans;
3529 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003530 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003531
Miao Xied4edf392013-02-20 09:17:06 +00003532 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003533 if (IS_ERR(trans)) {
3534 if (PTR_ERR(trans) != -ENOENT)
3535 return PTR_ERR(trans);
3536
3537 /* No running transaction, don't bother */
3538 transid = root->fs_info->last_trans_committed;
3539 goto out;
3540 }
Sage Weil46204592010-10-29 15:41:32 -04003541 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003542 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003543 if (ret) {
3544 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003545 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003546 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003547out:
Sage Weil46204592010-10-29 15:41:32 -04003548 if (argp)
3549 if (copy_to_user(argp, &transid, sizeof(transid)))
3550 return -EFAULT;
3551 return 0;
3552}
3553
Miao Xie9a8c28b2012-11-26 08:40:43 +00003554static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3555 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003556{
Sage Weil46204592010-10-29 15:41:32 -04003557 u64 transid;
3558
3559 if (argp) {
3560 if (copy_from_user(&transid, argp, sizeof(transid)))
3561 return -EFAULT;
3562 } else {
3563 transid = 0; /* current trans */
3564 }
3565 return btrfs_wait_for_commit(root, transid);
3566}
3567
Miao Xieb8e95482012-11-26 08:48:01 +00003568static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003569{
Al Viro496ad9a2013-01-23 17:07:38 -05003570 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003571 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003572 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003573
3574 if (!capable(CAP_SYS_ADMIN))
3575 return -EPERM;
3576
3577 sa = memdup_user(arg, sizeof(*sa));
3578 if (IS_ERR(sa))
3579 return PTR_ERR(sa);
3580
Miao Xieb8e95482012-11-26 08:48:01 +00003581 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3582 ret = mnt_want_write_file(file);
3583 if (ret)
3584 goto out;
3585 }
3586
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003587 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003588 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3589 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003590
3591 if (copy_to_user(arg, sa, sizeof(*sa)))
3592 ret = -EFAULT;
3593
Miao Xieb8e95482012-11-26 08:48:01 +00003594 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3595 mnt_drop_write_file(file);
3596out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003597 kfree(sa);
3598 return ret;
3599}
3600
3601static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3602{
3603 if (!capable(CAP_SYS_ADMIN))
3604 return -EPERM;
3605
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003606 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003607}
3608
3609static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3610 void __user *arg)
3611{
3612 struct btrfs_ioctl_scrub_args *sa;
3613 int ret;
3614
3615 if (!capable(CAP_SYS_ADMIN))
3616 return -EPERM;
3617
3618 sa = memdup_user(arg, sizeof(*sa));
3619 if (IS_ERR(sa))
3620 return PTR_ERR(sa);
3621
3622 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3623
3624 if (copy_to_user(arg, sa, sizeof(*sa)))
3625 ret = -EFAULT;
3626
3627 kfree(sa);
3628 return ret;
3629}
3630
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003631static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003632 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003633{
3634 struct btrfs_ioctl_get_dev_stats *sa;
3635 int ret;
3636
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003637 sa = memdup_user(arg, sizeof(*sa));
3638 if (IS_ERR(sa))
3639 return PTR_ERR(sa);
3640
David Sterbab27f7c02012-06-22 06:30:39 -06003641 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3642 kfree(sa);
3643 return -EPERM;
3644 }
3645
3646 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003647
3648 if (copy_to_user(arg, sa, sizeof(*sa)))
3649 ret = -EFAULT;
3650
3651 kfree(sa);
3652 return ret;
3653}
3654
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003655static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3656{
3657 struct btrfs_ioctl_dev_replace_args *p;
3658 int ret;
3659
3660 if (!capable(CAP_SYS_ADMIN))
3661 return -EPERM;
3662
3663 p = memdup_user(arg, sizeof(*p));
3664 if (IS_ERR(p))
3665 return PTR_ERR(p);
3666
3667 switch (p->cmd) {
3668 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003669 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3670 ret = -EROFS;
3671 goto out;
3672 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003673 if (atomic_xchg(
3674 &root->fs_info->mutually_exclusive_operation_running,
3675 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003676 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003677 } else {
3678 ret = btrfs_dev_replace_start(root, p);
3679 atomic_set(
3680 &root->fs_info->mutually_exclusive_operation_running,
3681 0);
3682 }
3683 break;
3684 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3685 btrfs_dev_replace_status(root->fs_info, p);
3686 ret = 0;
3687 break;
3688 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3689 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3690 break;
3691 default:
3692 ret = -EINVAL;
3693 break;
3694 }
3695
3696 if (copy_to_user(arg, p, sizeof(*p)))
3697 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003698out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003699 kfree(p);
3700 return ret;
3701}
3702
Jan Schmidtd7728c92011-07-07 16:48:38 +02003703static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3704{
3705 int ret = 0;
3706 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003707 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003708 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003709 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003710 struct inode_fs_paths *ipath = NULL;
3711 struct btrfs_path *path;
3712
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003713 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003714 return -EPERM;
3715
3716 path = btrfs_alloc_path();
3717 if (!path) {
3718 ret = -ENOMEM;
3719 goto out;
3720 }
3721
3722 ipa = memdup_user(arg, sizeof(*ipa));
3723 if (IS_ERR(ipa)) {
3724 ret = PTR_ERR(ipa);
3725 ipa = NULL;
3726 goto out;
3727 }
3728
3729 size = min_t(u32, ipa->size, 4096);
3730 ipath = init_ipath(size, root, path);
3731 if (IS_ERR(ipath)) {
3732 ret = PTR_ERR(ipath);
3733 ipath = NULL;
3734 goto out;
3735 }
3736
3737 ret = paths_from_inode(ipa->inum, ipath);
3738 if (ret < 0)
3739 goto out;
3740
3741 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003742 rel_ptr = ipath->fspath->val[i] -
3743 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003744 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003745 }
3746
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003747 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3748 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003749 if (ret) {
3750 ret = -EFAULT;
3751 goto out;
3752 }
3753
3754out:
3755 btrfs_free_path(path);
3756 free_ipath(ipath);
3757 kfree(ipa);
3758
3759 return ret;
3760}
3761
3762static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3763{
3764 struct btrfs_data_container *inodes = ctx;
3765 const size_t c = 3 * sizeof(u64);
3766
3767 if (inodes->bytes_left >= c) {
3768 inodes->bytes_left -= c;
3769 inodes->val[inodes->elem_cnt] = inum;
3770 inodes->val[inodes->elem_cnt + 1] = offset;
3771 inodes->val[inodes->elem_cnt + 2] = root;
3772 inodes->elem_cnt += 3;
3773 } else {
3774 inodes->bytes_missing += c - inodes->bytes_left;
3775 inodes->bytes_left = 0;
3776 inodes->elem_missed += 3;
3777 }
3778
3779 return 0;
3780}
3781
3782static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3783 void __user *arg)
3784{
3785 int ret = 0;
3786 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003787 struct btrfs_ioctl_logical_ino_args *loi;
3788 struct btrfs_data_container *inodes = NULL;
3789 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003790
3791 if (!capable(CAP_SYS_ADMIN))
3792 return -EPERM;
3793
3794 loi = memdup_user(arg, sizeof(*loi));
3795 if (IS_ERR(loi)) {
3796 ret = PTR_ERR(loi);
3797 loi = NULL;
3798 goto out;
3799 }
3800
3801 path = btrfs_alloc_path();
3802 if (!path) {
3803 ret = -ENOMEM;
3804 goto out;
3805 }
3806
Liu Bo425d17a2012-09-07 20:01:30 -06003807 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003808 inodes = init_data_container(size);
3809 if (IS_ERR(inodes)) {
3810 ret = PTR_ERR(inodes);
3811 inodes = NULL;
3812 goto out;
3813 }
3814
Liu Bodf031f02012-09-07 20:01:29 -06003815 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3816 build_ino_list, inodes);
3817 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003818 ret = -ENOENT;
3819 if (ret < 0)
3820 goto out;
3821
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003822 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3823 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003824 if (ret)
3825 ret = -EFAULT;
3826
3827out:
3828 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003829 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003830 kfree(loi);
3831
3832 return ret;
3833}
3834
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003835void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003836 struct btrfs_ioctl_balance_args *bargs)
3837{
3838 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3839
3840 bargs->flags = bctl->flags;
3841
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003842 if (atomic_read(&fs_info->balance_running))
3843 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3844 if (atomic_read(&fs_info->balance_pause_req))
3845 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003846 if (atomic_read(&fs_info->balance_cancel_req))
3847 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003848
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003849 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3850 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3851 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003852
3853 if (lock) {
3854 spin_lock(&fs_info->balance_lock);
3855 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3856 spin_unlock(&fs_info->balance_lock);
3857 } else {
3858 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3859 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003860}
3861
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003862static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003863{
Al Viro496ad9a2013-01-23 17:07:38 -05003864 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003865 struct btrfs_fs_info *fs_info = root->fs_info;
3866 struct btrfs_ioctl_balance_args *bargs;
3867 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003868 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003869 int ret;
3870
3871 if (!capable(CAP_SYS_ADMIN))
3872 return -EPERM;
3873
Liu Boe54bfa312012-06-29 03:58:48 -06003874 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003875 if (ret)
3876 return ret;
3877
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003878again:
3879 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3880 mutex_lock(&fs_info->volume_mutex);
3881 mutex_lock(&fs_info->balance_mutex);
3882 need_unlock = true;
3883 goto locked;
3884 }
3885
3886 /*
3887 * mut. excl. ops lock is locked. Three possibilites:
3888 * (1) some other op is running
3889 * (2) balance is running
3890 * (3) balance is paused -- special case (think resume)
3891 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003892 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003893 if (fs_info->balance_ctl) {
3894 /* this is either (2) or (3) */
3895 if (!atomic_read(&fs_info->balance_running)) {
3896 mutex_unlock(&fs_info->balance_mutex);
3897 if (!mutex_trylock(&fs_info->volume_mutex))
3898 goto again;
3899 mutex_lock(&fs_info->balance_mutex);
3900
3901 if (fs_info->balance_ctl &&
3902 !atomic_read(&fs_info->balance_running)) {
3903 /* this is (3) */
3904 need_unlock = false;
3905 goto locked;
3906 }
3907
3908 mutex_unlock(&fs_info->balance_mutex);
3909 mutex_unlock(&fs_info->volume_mutex);
3910 goto again;
3911 } else {
3912 /* this is (2) */
3913 mutex_unlock(&fs_info->balance_mutex);
3914 ret = -EINPROGRESS;
3915 goto out;
3916 }
3917 } else {
3918 /* this is (1) */
3919 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003920 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003921 goto out;
3922 }
3923
3924locked:
3925 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003926
3927 if (arg) {
3928 bargs = memdup_user(arg, sizeof(*bargs));
3929 if (IS_ERR(bargs)) {
3930 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003931 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003932 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003933
3934 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3935 if (!fs_info->balance_ctl) {
3936 ret = -ENOTCONN;
3937 goto out_bargs;
3938 }
3939
3940 bctl = fs_info->balance_ctl;
3941 spin_lock(&fs_info->balance_lock);
3942 bctl->flags |= BTRFS_BALANCE_RESUME;
3943 spin_unlock(&fs_info->balance_lock);
3944
3945 goto do_balance;
3946 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003947 } else {
3948 bargs = NULL;
3949 }
3950
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003951 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003952 ret = -EINPROGRESS;
3953 goto out_bargs;
3954 }
3955
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003956 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3957 if (!bctl) {
3958 ret = -ENOMEM;
3959 goto out_bargs;
3960 }
3961
3962 bctl->fs_info = fs_info;
3963 if (arg) {
3964 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3965 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3966 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3967
3968 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003969 } else {
3970 /* balance everything - no filters */
3971 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003972 }
3973
Ilya Dryomovde322262012-01-16 22:04:49 +02003974do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003975 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003976 * Ownership of bctl and mutually_exclusive_operation_running
3977 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3978 * or, if restriper was paused all the way until unmount, in
3979 * free_fs_info. mutually_exclusive_operation_running is
3980 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003981 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003982 need_unlock = false;
3983
3984 ret = btrfs_balance(bctl, bargs);
3985
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003986 if (arg) {
3987 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3988 ret = -EFAULT;
3989 }
3990
3991out_bargs:
3992 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003993out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003994 mutex_unlock(&fs_info->balance_mutex);
3995 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003996 if (need_unlock)
3997 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3998out:
Liu Boe54bfa312012-06-29 03:58:48 -06003999 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004000 return ret;
4001}
4002
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004003static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4004{
4005 if (!capable(CAP_SYS_ADMIN))
4006 return -EPERM;
4007
4008 switch (cmd) {
4009 case BTRFS_BALANCE_CTL_PAUSE:
4010 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004011 case BTRFS_BALANCE_CTL_CANCEL:
4012 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004013 }
4014
4015 return -EINVAL;
4016}
4017
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004018static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4019 void __user *arg)
4020{
4021 struct btrfs_fs_info *fs_info = root->fs_info;
4022 struct btrfs_ioctl_balance_args *bargs;
4023 int ret = 0;
4024
4025 if (!capable(CAP_SYS_ADMIN))
4026 return -EPERM;
4027
4028 mutex_lock(&fs_info->balance_mutex);
4029 if (!fs_info->balance_ctl) {
4030 ret = -ENOTCONN;
4031 goto out;
4032 }
4033
4034 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4035 if (!bargs) {
4036 ret = -ENOMEM;
4037 goto out;
4038 }
4039
4040 update_ioctl_balance_args(fs_info, 1, bargs);
4041
4042 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4043 ret = -EFAULT;
4044
4045 kfree(bargs);
4046out:
4047 mutex_unlock(&fs_info->balance_mutex);
4048 return ret;
4049}
4050
Miao Xie905b0dd2012-11-26 08:50:11 +00004051static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004052{
Al Viro496ad9a2013-01-23 17:07:38 -05004053 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004054 struct btrfs_ioctl_quota_ctl_args *sa;
4055 struct btrfs_trans_handle *trans = NULL;
4056 int ret;
4057 int err;
4058
4059 if (!capable(CAP_SYS_ADMIN))
4060 return -EPERM;
4061
Miao Xie905b0dd2012-11-26 08:50:11 +00004062 ret = mnt_want_write_file(file);
4063 if (ret)
4064 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004065
4066 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004067 if (IS_ERR(sa)) {
4068 ret = PTR_ERR(sa);
4069 goto drop_write;
4070 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004071
Wang Shilong7708f022013-04-07 10:24:57 +00004072 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004073 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4074 if (IS_ERR(trans)) {
4075 ret = PTR_ERR(trans);
4076 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004077 }
4078
4079 switch (sa->cmd) {
4080 case BTRFS_QUOTA_CTL_ENABLE:
4081 ret = btrfs_quota_enable(trans, root->fs_info);
4082 break;
4083 case BTRFS_QUOTA_CTL_DISABLE:
4084 ret = btrfs_quota_disable(trans, root->fs_info);
4085 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004086 default:
4087 ret = -EINVAL;
4088 break;
4089 }
4090
Jan Schmidt2f232032013-04-25 16:04:51 +00004091 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4092 if (err && !ret)
4093 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004094out:
4095 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004096 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004097drop_write:
4098 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004099 return ret;
4100}
4101
Miao Xie905b0dd2012-11-26 08:50:11 +00004102static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004103{
Al Viro496ad9a2013-01-23 17:07:38 -05004104 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004105 struct btrfs_ioctl_qgroup_assign_args *sa;
4106 struct btrfs_trans_handle *trans;
4107 int ret;
4108 int err;
4109
4110 if (!capable(CAP_SYS_ADMIN))
4111 return -EPERM;
4112
Miao Xie905b0dd2012-11-26 08:50:11 +00004113 ret = mnt_want_write_file(file);
4114 if (ret)
4115 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004116
4117 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004118 if (IS_ERR(sa)) {
4119 ret = PTR_ERR(sa);
4120 goto drop_write;
4121 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004122
4123 trans = btrfs_join_transaction(root);
4124 if (IS_ERR(trans)) {
4125 ret = PTR_ERR(trans);
4126 goto out;
4127 }
4128
4129 /* FIXME: check if the IDs really exist */
4130 if (sa->assign) {
4131 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4132 sa->src, sa->dst);
4133 } else {
4134 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4135 sa->src, sa->dst);
4136 }
4137
4138 err = btrfs_end_transaction(trans, root);
4139 if (err && !ret)
4140 ret = err;
4141
4142out:
4143 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004144drop_write:
4145 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004146 return ret;
4147}
4148
Miao Xie905b0dd2012-11-26 08:50:11 +00004149static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004150{
Al Viro496ad9a2013-01-23 17:07:38 -05004151 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004152 struct btrfs_ioctl_qgroup_create_args *sa;
4153 struct btrfs_trans_handle *trans;
4154 int ret;
4155 int err;
4156
4157 if (!capable(CAP_SYS_ADMIN))
4158 return -EPERM;
4159
Miao Xie905b0dd2012-11-26 08:50:11 +00004160 ret = mnt_want_write_file(file);
4161 if (ret)
4162 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004163
4164 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004165 if (IS_ERR(sa)) {
4166 ret = PTR_ERR(sa);
4167 goto drop_write;
4168 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004169
Miao Xied86e56c2012-11-15 11:35:41 +00004170 if (!sa->qgroupid) {
4171 ret = -EINVAL;
4172 goto out;
4173 }
4174
Arne Jansen5d13a372011-09-14 15:53:51 +02004175 trans = btrfs_join_transaction(root);
4176 if (IS_ERR(trans)) {
4177 ret = PTR_ERR(trans);
4178 goto out;
4179 }
4180
4181 /* FIXME: check if the IDs really exist */
4182 if (sa->create) {
4183 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4184 NULL);
4185 } else {
4186 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4187 }
4188
4189 err = btrfs_end_transaction(trans, root);
4190 if (err && !ret)
4191 ret = err;
4192
4193out:
4194 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004195drop_write:
4196 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004197 return ret;
4198}
4199
Miao Xie905b0dd2012-11-26 08:50:11 +00004200static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004201{
Al Viro496ad9a2013-01-23 17:07:38 -05004202 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004203 struct btrfs_ioctl_qgroup_limit_args *sa;
4204 struct btrfs_trans_handle *trans;
4205 int ret;
4206 int err;
4207 u64 qgroupid;
4208
4209 if (!capable(CAP_SYS_ADMIN))
4210 return -EPERM;
4211
Miao Xie905b0dd2012-11-26 08:50:11 +00004212 ret = mnt_want_write_file(file);
4213 if (ret)
4214 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004215
4216 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004217 if (IS_ERR(sa)) {
4218 ret = PTR_ERR(sa);
4219 goto drop_write;
4220 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004221
4222 trans = btrfs_join_transaction(root);
4223 if (IS_ERR(trans)) {
4224 ret = PTR_ERR(trans);
4225 goto out;
4226 }
4227
4228 qgroupid = sa->qgroupid;
4229 if (!qgroupid) {
4230 /* take the current subvol as qgroup */
4231 qgroupid = root->root_key.objectid;
4232 }
4233
4234 /* FIXME: check if the IDs really exist */
4235 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4236
4237 err = btrfs_end_transaction(trans, root);
4238 if (err && !ret)
4239 ret = err;
4240
4241out:
4242 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004243drop_write:
4244 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004245 return ret;
4246}
4247
Jan Schmidt2f232032013-04-25 16:04:51 +00004248static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4249{
Al Viro6d0379e2013-06-16 19:32:35 +04004250 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004251 struct btrfs_ioctl_quota_rescan_args *qsa;
4252 int ret;
4253
4254 if (!capable(CAP_SYS_ADMIN))
4255 return -EPERM;
4256
4257 ret = mnt_want_write_file(file);
4258 if (ret)
4259 return ret;
4260
4261 qsa = memdup_user(arg, sizeof(*qsa));
4262 if (IS_ERR(qsa)) {
4263 ret = PTR_ERR(qsa);
4264 goto drop_write;
4265 }
4266
4267 if (qsa->flags) {
4268 ret = -EINVAL;
4269 goto out;
4270 }
4271
4272 ret = btrfs_qgroup_rescan(root->fs_info);
4273
4274out:
4275 kfree(qsa);
4276drop_write:
4277 mnt_drop_write_file(file);
4278 return ret;
4279}
4280
4281static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4282{
Al Viro6d0379e2013-06-16 19:32:35 +04004283 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004284 struct btrfs_ioctl_quota_rescan_args *qsa;
4285 int ret = 0;
4286
4287 if (!capable(CAP_SYS_ADMIN))
4288 return -EPERM;
4289
4290 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4291 if (!qsa)
4292 return -ENOMEM;
4293
4294 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4295 qsa->flags = 1;
4296 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4297 }
4298
4299 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4300 ret = -EFAULT;
4301
4302 kfree(qsa);
4303 return ret;
4304}
4305
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004306static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4307{
Al Viro54563d42013-09-01 15:57:51 -04004308 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004309
4310 if (!capable(CAP_SYS_ADMIN))
4311 return -EPERM;
4312
4313 return btrfs_qgroup_wait_for_completion(root->fs_info);
4314}
4315
Alexander Block8ea05e32012-07-25 17:35:53 +02004316static long btrfs_ioctl_set_received_subvol(struct file *file,
4317 void __user *arg)
4318{
4319 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004320 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004321 struct btrfs_root *root = BTRFS_I(inode)->root;
4322 struct btrfs_root_item *root_item = &root->root_item;
4323 struct btrfs_trans_handle *trans;
4324 struct timespec ct = CURRENT_TIME;
4325 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004326 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004327
4328 ret = mnt_want_write_file(file);
4329 if (ret < 0)
4330 return ret;
4331
4332 down_write(&root->fs_info->subvol_sem);
4333
4334 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4335 ret = -EINVAL;
4336 goto out;
4337 }
4338
4339 if (btrfs_root_readonly(root)) {
4340 ret = -EROFS;
4341 goto out;
4342 }
4343
4344 if (!inode_owner_or_capable(inode)) {
4345 ret = -EACCES;
4346 goto out;
4347 }
4348
4349 sa = memdup_user(arg, sizeof(*sa));
4350 if (IS_ERR(sa)) {
4351 ret = PTR_ERR(sa);
4352 sa = NULL;
4353 goto out;
4354 }
4355
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004356 /*
4357 * 1 - root item
4358 * 2 - uuid items (received uuid + subvol uuid)
4359 */
4360 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004361 if (IS_ERR(trans)) {
4362 ret = PTR_ERR(trans);
4363 trans = NULL;
4364 goto out;
4365 }
4366
4367 sa->rtransid = trans->transid;
4368 sa->rtime.sec = ct.tv_sec;
4369 sa->rtime.nsec = ct.tv_nsec;
4370
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004371 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4372 BTRFS_UUID_SIZE);
4373 if (received_uuid_changed &&
4374 !btrfs_is_empty_uuid(root_item->received_uuid))
4375 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4376 root_item->received_uuid,
4377 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4378 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004379 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4380 btrfs_set_root_stransid(root_item, sa->stransid);
4381 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004382 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4383 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4384 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4385 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004386
4387 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4388 &root->root_key, &root->root_item);
4389 if (ret < 0) {
4390 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004391 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004392 }
4393 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4394 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4395 sa->uuid,
4396 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4397 root->root_key.objectid);
4398 if (ret < 0 && ret != -EEXIST) {
4399 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004400 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004401 }
4402 }
4403 ret = btrfs_commit_transaction(trans, root);
4404 if (ret < 0) {
4405 btrfs_abort_transaction(trans, root, ret);
4406 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004407 }
4408
4409 ret = copy_to_user(arg, sa, sizeof(*sa));
4410 if (ret)
4411 ret = -EFAULT;
4412
4413out:
4414 kfree(sa);
4415 up_write(&root->fs_info->subvol_sem);
4416 mnt_drop_write_file(file);
4417 return ret;
4418}
4419
jeff.liu867ab662013-01-05 02:48:01 +00004420static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4421{
Al Viro6d0379e2013-06-16 19:32:35 +04004422 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004423 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004424 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004425 char label[BTRFS_LABEL_SIZE];
4426
4427 spin_lock(&root->fs_info->super_lock);
4428 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4429 spin_unlock(&root->fs_info->super_lock);
4430
4431 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004432
4433 if (len == BTRFS_LABEL_SIZE) {
4434 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4435 --len);
4436 }
4437
jeff.liu867ab662013-01-05 02:48:01 +00004438 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004439
4440 return ret ? -EFAULT : 0;
4441}
4442
jeff.liua8bfd4a2013-01-05 02:48:08 +00004443static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4444{
Al Viro6d0379e2013-06-16 19:32:35 +04004445 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004446 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4447 struct btrfs_trans_handle *trans;
4448 char label[BTRFS_LABEL_SIZE];
4449 int ret;
4450
4451 if (!capable(CAP_SYS_ADMIN))
4452 return -EPERM;
4453
4454 if (copy_from_user(label, arg, sizeof(label)))
4455 return -EFAULT;
4456
4457 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4458 pr_err("btrfs: unable to set label with more than %d bytes\n",
4459 BTRFS_LABEL_SIZE - 1);
4460 return -EINVAL;
4461 }
4462
4463 ret = mnt_want_write_file(file);
4464 if (ret)
4465 return ret;
4466
jeff.liua8bfd4a2013-01-05 02:48:08 +00004467 trans = btrfs_start_transaction(root, 0);
4468 if (IS_ERR(trans)) {
4469 ret = PTR_ERR(trans);
4470 goto out_unlock;
4471 }
4472
Anand Jaina1b83ac2013-07-19 17:39:32 +08004473 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004474 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004475 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004476 ret = btrfs_end_transaction(trans, root);
4477
4478out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004479 mnt_drop_write_file(file);
4480 return ret;
4481}
4482
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004483long btrfs_ioctl(struct file *file, unsigned int
4484 cmd, unsigned long arg)
4485{
Al Viro496ad9a2013-01-23 17:07:38 -05004486 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004487 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004488
4489 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004490 case FS_IOC_GETFLAGS:
4491 return btrfs_ioctl_getflags(file, argp);
4492 case FS_IOC_SETFLAGS:
4493 return btrfs_ioctl_setflags(file, argp);
4494 case FS_IOC_GETVERSION:
4495 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004496 case FITRIM:
4497 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004498 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004499 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004500 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004501 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004502 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004503 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004504 case BTRFS_IOC_SUBVOL_CREATE_V2:
4505 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004506 case BTRFS_IOC_SNAP_DESTROY:
4507 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004508 case BTRFS_IOC_SUBVOL_GETFLAGS:
4509 return btrfs_ioctl_subvol_getflags(file, argp);
4510 case BTRFS_IOC_SUBVOL_SETFLAGS:
4511 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004512 case BTRFS_IOC_DEFAULT_SUBVOL:
4513 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004514 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004515 return btrfs_ioctl_defrag(file, NULL);
4516 case BTRFS_IOC_DEFRAG_RANGE:
4517 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004518 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004519 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004520 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004521 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004522 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004523 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004524 case BTRFS_IOC_FS_INFO:
4525 return btrfs_ioctl_fs_info(root, argp);
4526 case BTRFS_IOC_DEV_INFO:
4527 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004528 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004529 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004530 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004531 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4532 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004533 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004534 case BTRFS_IOC_TRANS_START:
4535 return btrfs_ioctl_trans_start(file);
4536 case BTRFS_IOC_TRANS_END:
4537 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004538 case BTRFS_IOC_TREE_SEARCH:
4539 return btrfs_ioctl_tree_search(file, argp);
4540 case BTRFS_IOC_INO_LOOKUP:
4541 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004542 case BTRFS_IOC_INO_PATHS:
4543 return btrfs_ioctl_ino_to_path(root, argp);
4544 case BTRFS_IOC_LOGICAL_INO:
4545 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004546 case BTRFS_IOC_SPACE_INFO:
4547 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004548 case BTRFS_IOC_SYNC: {
4549 int ret;
4550
Miao Xie91aef862013-11-04 23:13:26 +08004551 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004552 if (ret)
4553 return ret;
4554 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4555 return ret;
4556 }
Sage Weil46204592010-10-29 15:41:32 -04004557 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004558 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004559 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004560 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004561 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004562 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004563 case BTRFS_IOC_SCRUB_CANCEL:
4564 return btrfs_ioctl_scrub_cancel(root, argp);
4565 case BTRFS_IOC_SCRUB_PROGRESS:
4566 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004567 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004568 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004569 case BTRFS_IOC_BALANCE_CTL:
4570 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004571 case BTRFS_IOC_BALANCE_PROGRESS:
4572 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004573 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4574 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004575 case BTRFS_IOC_SEND:
4576 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004577 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004578 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004579 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004580 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004581 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004582 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004583 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004584 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004585 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004586 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004587 case BTRFS_IOC_QUOTA_RESCAN:
4588 return btrfs_ioctl_quota_rescan(file, argp);
4589 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4590 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004591 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4592 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004593 case BTRFS_IOC_DEV_REPLACE:
4594 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004595 case BTRFS_IOC_GET_FSLABEL:
4596 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004597 case BTRFS_IOC_SET_FSLABEL:
4598 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004599 case BTRFS_IOC_FILE_EXTENT_SAME:
4600 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004601 }
4602
4603 return -ENOTTY;
4604}