blob: a692aad8fa5aa01ef2108ba19b4f28f68ca1c32e [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"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000059#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040060#include "sysfs.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040061
Mark Fasheh416161d2013-08-06 11:42:51 -070062static int btrfs_clone(struct inode *src, struct inode *inode,
63 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
64
Christoph Hellwig6cbff002009-04-17 10:37:41 +020065/* Mask out flags that are inappropriate for the given type of inode. */
66static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
67{
68 if (S_ISDIR(mode))
69 return flags;
70 else if (S_ISREG(mode))
71 return flags & ~FS_DIRSYNC_FL;
72 else
73 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
74}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040075
Christoph Hellwig6cbff002009-04-17 10:37:41 +020076/*
77 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
78 */
79static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
80{
81 unsigned int iflags = 0;
82
83 if (flags & BTRFS_INODE_SYNC)
84 iflags |= FS_SYNC_FL;
85 if (flags & BTRFS_INODE_IMMUTABLE)
86 iflags |= FS_IMMUTABLE_FL;
87 if (flags & BTRFS_INODE_APPEND)
88 iflags |= FS_APPEND_FL;
89 if (flags & BTRFS_INODE_NODUMP)
90 iflags |= FS_NODUMP_FL;
91 if (flags & BTRFS_INODE_NOATIME)
92 iflags |= FS_NOATIME_FL;
93 if (flags & BTRFS_INODE_DIRSYNC)
94 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000095 if (flags & BTRFS_INODE_NODATACOW)
96 iflags |= FS_NOCOW_FL;
97
98 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
99 iflags |= FS_COMPR_FL;
100 else if (flags & BTRFS_INODE_NOCOMPRESS)
101 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102
103 return iflags;
104}
105
106/*
107 * Update inode->i_flags based on the btrfs internal flags.
108 */
109void btrfs_update_iflags(struct inode *inode)
110{
111 struct btrfs_inode *ip = BTRFS_I(inode);
112
113 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
114
115 if (ip->flags & BTRFS_INODE_SYNC)
116 inode->i_flags |= S_SYNC;
117 if (ip->flags & BTRFS_INODE_IMMUTABLE)
118 inode->i_flags |= S_IMMUTABLE;
119 if (ip->flags & BTRFS_INODE_APPEND)
120 inode->i_flags |= S_APPEND;
121 if (ip->flags & BTRFS_INODE_NOATIME)
122 inode->i_flags |= S_NOATIME;
123 if (ip->flags & BTRFS_INODE_DIRSYNC)
124 inode->i_flags |= S_DIRSYNC;
125}
126
127/*
128 * Inherit flags from the parent inode.
129 *
Josef Bacike27425d2011-09-27 11:01:30 -0400130 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131 */
132void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
133{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400134 unsigned int flags;
135
136 if (!dir)
137 return;
138
139 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
Josef Bacike27425d2011-09-27 11:01:30 -0400141 if (flags & BTRFS_INODE_NOCOMPRESS) {
142 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
143 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
144 } else if (flags & BTRFS_INODE_COMPRESS) {
145 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
146 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
147 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200148
Liu Bo213490b2012-09-11 08:33:50 -0600149 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400150 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600151 if (S_ISREG(inode->i_mode))
152 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
153 }
Josef Bacike27425d2011-09-27 11:01:30 -0400154
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200155 btrfs_update_iflags(inode);
156}
157
158static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
159{
Al Viro496ad9a2013-01-23 17:07:38 -0500160 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200161 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
162
163 if (copy_to_user(arg, &flags, sizeof(flags)))
164 return -EFAULT;
165 return 0;
166}
167
Liu Bo75e7cb72011-03-22 10:12:20 +0000168static int check_flags(unsigned int flags)
169{
170 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
171 FS_NOATIME_FL | FS_NODUMP_FL | \
172 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000173 FS_NOCOMP_FL | FS_COMPR_FL |
174 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 return -EOPNOTSUPP;
176
177 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
178 return -EINVAL;
179
Liu Bo75e7cb72011-03-22 10:12:20 +0000180 return 0;
181}
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
184{
Al Viro496ad9a2013-01-23 17:07:38 -0500185 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200186 struct btrfs_inode *ip = BTRFS_I(inode);
187 struct btrfs_root *root = ip->root;
188 struct btrfs_trans_handle *trans;
189 unsigned int flags, oldflags;
190 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800191 u64 ip_oldflags;
192 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600193 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194
David Sterbabd60ea02014-01-16 15:50:22 +0100195 if (!inode_owner_or_capable(inode))
196 return -EPERM;
197
Li Zefanb83cc962010-12-20 16:04:08 +0800198 if (btrfs_root_readonly(root))
199 return -EROFS;
200
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200201 if (copy_from_user(&flags, arg, sizeof(flags)))
202 return -EFAULT;
203
Liu Bo75e7cb72011-03-22 10:12:20 +0000204 ret = check_flags(flags);
205 if (ret)
206 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207
Jan Karae7848682012-06-12 16:20:32 +0200208 ret = mnt_want_write_file(file);
209 if (ret)
210 return ret;
211
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200212 mutex_lock(&inode->i_mutex);
213
Li Zefanf062abf02011-12-29 13:36:45 +0800214 ip_oldflags = ip->flags;
215 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600216 mode = inode->i_mode;
Li Zefanf062abf02011-12-29 13:36:45 +0800217
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200218 flags = btrfs_mask_flags(inode->i_mode, flags);
219 oldflags = btrfs_flags_to_ioctl(ip->flags);
220 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
221 if (!capable(CAP_LINUX_IMMUTABLE)) {
222 ret = -EPERM;
223 goto out_unlock;
224 }
225 }
226
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227 if (flags & FS_SYNC_FL)
228 ip->flags |= BTRFS_INODE_SYNC;
229 else
230 ip->flags &= ~BTRFS_INODE_SYNC;
231 if (flags & FS_IMMUTABLE_FL)
232 ip->flags |= BTRFS_INODE_IMMUTABLE;
233 else
234 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
235 if (flags & FS_APPEND_FL)
236 ip->flags |= BTRFS_INODE_APPEND;
237 else
238 ip->flags &= ~BTRFS_INODE_APPEND;
239 if (flags & FS_NODUMP_FL)
240 ip->flags |= BTRFS_INODE_NODUMP;
241 else
242 ip->flags &= ~BTRFS_INODE_NODUMP;
243 if (flags & FS_NOATIME_FL)
244 ip->flags |= BTRFS_INODE_NOATIME;
245 else
246 ip->flags &= ~BTRFS_INODE_NOATIME;
247 if (flags & FS_DIRSYNC_FL)
248 ip->flags |= BTRFS_INODE_DIRSYNC;
249 else
250 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600251 if (flags & FS_NOCOW_FL) {
252 if (S_ISREG(mode)) {
253 /*
254 * It's safe to turn csums off here, no extents exist.
255 * Otherwise we want the flag to reflect the real COW
256 * status of the file and will not set it.
257 */
258 if (inode->i_size == 0)
259 ip->flags |= BTRFS_INODE_NODATACOW
260 | BTRFS_INODE_NODATASUM;
261 } else {
262 ip->flags |= BTRFS_INODE_NODATACOW;
263 }
264 } else {
265 /*
266 * Revert back under same assuptions as above
267 */
268 if (S_ISREG(mode)) {
269 if (inode->i_size == 0)
270 ip->flags &= ~(BTRFS_INODE_NODATACOW
271 | BTRFS_INODE_NODATASUM);
272 } else {
273 ip->flags &= ~BTRFS_INODE_NODATACOW;
274 }
275 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200276
Liu Bo75e7cb72011-03-22 10:12:20 +0000277 /*
278 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
279 * flag may be changed automatically if compression code won't make
280 * things smaller.
281 */
282 if (flags & FS_NOCOMP_FL) {
283 ip->flags &= ~BTRFS_INODE_COMPRESS;
284 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000285
286 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
287 if (ret && ret != -ENODATA)
288 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000289 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000290 const char *comp;
291
Liu Bo75e7cb72011-03-22 10:12:20 +0000292 ip->flags |= BTRFS_INODE_COMPRESS;
293 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000294
295 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
296 comp = "lzo";
297 else
298 comp = "zlib";
299 ret = btrfs_set_prop(inode, "btrfs.compression",
300 comp, strlen(comp), 0);
301 if (ret)
302 goto out_drop;
303
Li Zefanebcb9042011-04-15 03:03:17 +0000304 } else {
305 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000306 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200307
Li Zefan4da6f1a2011-12-29 13:39:50 +0800308 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800309 if (IS_ERR(trans)) {
310 ret = PTR_ERR(trans);
311 goto out_drop;
312 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200313
Li Zefan306424cc2011-12-14 20:12:02 -0500314 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400315 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500316 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200317 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200319 btrfs_end_transaction(trans, root);
Li Zefanf062abf02011-12-29 13:36:45 +0800320 out_drop:
321 if (ret) {
322 ip->flags = ip_oldflags;
323 inode->i_flags = i_oldflags;
324 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200325
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200326 out_unlock:
327 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200328 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000329 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200330}
331
332static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
333{
Al Viro496ad9a2013-01-23 17:07:38 -0500334 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200335
336 return put_user(inode->i_generation, arg);
337}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400338
Li Dongyangf7039b12011-03-24 10:24:28 +0000339static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
340{
Al Viro54563d42013-09-01 15:57:51 -0400341 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000342 struct btrfs_device *device;
343 struct request_queue *q;
344 struct fstrim_range range;
345 u64 minlen = ULLONG_MAX;
346 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500347 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000348 int ret;
349
350 if (!capable(CAP_SYS_ADMIN))
351 return -EPERM;
352
Xiao Guangrong1f781602011-04-20 10:09:16 +0000353 rcu_read_lock();
354 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
355 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000356 if (!device->bdev)
357 continue;
358 q = bdev_get_queue(device->bdev);
359 if (blk_queue_discard(q)) {
360 num_devices++;
361 minlen = min((u64)q->limits.discard_granularity,
362 minlen);
363 }
364 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000365 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200366
Li Dongyangf7039b12011-03-24 10:24:28 +0000367 if (!num_devices)
368 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000369 if (copy_from_user(&range, arg, sizeof(range)))
370 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000371 if (range.start > total_bytes ||
372 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200373 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000374
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200375 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000376 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500377 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000378 if (ret < 0)
379 return ret;
380
381 if (copy_to_user(arg, &range, sizeof(range)))
382 return -EFAULT;
383
384 return 0;
385}
386
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200387int btrfs_is_empty_uuid(u8 *uuid)
388{
Chris Mason46e0f662013-11-15 12:14:55 +0100389 int i;
390
391 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
392 if (uuid[i])
393 return 0;
394 }
395 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200396}
397
Miao Xied5c12072013-02-28 10:04:33 +0000398static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400399 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400400 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200401 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000402 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403{
404 struct btrfs_trans_handle *trans;
405 struct btrfs_key key;
406 struct btrfs_root_item root_item;
407 struct btrfs_inode_item *inode_item;
408 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000409 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400410 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000411 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200412 struct timespec cur_time = CURRENT_TIME;
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900413 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 int ret;
415 int err;
416 u64 objectid;
417 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500418 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000419 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200420 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
Li Zefan581bb052011-04-20 10:06:11 +0800422 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400423 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400424 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000425
Miao Xied5c12072013-02-28 10:04:33 +0000426 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400427 /*
Miao Xied5c12072013-02-28 10:04:33 +0000428 * The same as the snapshot creation, please see the comment
429 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400430 */
Miao Xied5c12072013-02-28 10:04:33 +0000431 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200432 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000433 if (ret)
434 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400435
Miao Xied5c12072013-02-28 10:04:33 +0000436 trans = btrfs_start_transaction(root, 0);
437 if (IS_ERR(trans)) {
438 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800439 btrfs_subvolume_release_metadata(root, &block_rsv,
440 qgroup_reserved);
441 return ret;
Miao Xied5c12072013-02-28 10:04:33 +0000442 }
443 trans->block_rsv = &block_rsv;
444 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445
Miao Xie8696c532013-02-07 06:02:44 +0000446 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200447 if (ret)
448 goto fail;
449
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200451 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400452 if (IS_ERR(leaf)) {
453 ret = PTR_ERR(leaf);
454 goto fail;
455 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400457 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400458 btrfs_set_header_bytenr(leaf, leaf->start);
459 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400460 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461 btrfs_set_header_owner(leaf, objectid);
462
Ross Kirk0a4e5582013-09-24 10:12:38 +0100463 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400465 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200466 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400467 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400468 btrfs_mark_buffer_dirty(leaf);
469
Alexander Block8ea05e32012-07-25 17:35:53 +0200470 memset(&root_item, 0, sizeof(root_item));
471
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400472 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800473 btrfs_set_stack_inode_generation(inode_item, 1);
474 btrfs_set_stack_inode_size(inode_item, 3);
475 btrfs_set_stack_inode_nlink(inode_item, 1);
476 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
477 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478
Qu Wenruo3cae2102013-07-16 11:19:18 +0800479 btrfs_set_root_flags(&root_item, 0);
480 btrfs_set_root_limit(&root_item, 0);
481 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000482
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400483 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400484 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400485 btrfs_set_root_level(&root_item, 0);
486 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000487 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400488 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489
Alexander Block8ea05e32012-07-25 17:35:53 +0200490 btrfs_set_root_generation_v2(&root_item,
491 btrfs_root_generation(&root_item));
492 uuid_le_gen(&new_uuid);
493 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800494 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
495 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200496 root_item.ctime = root_item.otime;
497 btrfs_set_root_ctransid(&root_item, trans->transid);
498 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400499
Chris Mason925baed2008-06-25 16:01:30 -0400500 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400501 free_extent_buffer(leaf);
502 leaf = NULL;
503
504 btrfs_set_root_dirid(&root_item, new_dirid);
505
506 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400507 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
509 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
510 &root_item);
511 if (ret)
512 goto fail;
513
Yan, Zheng76dda932009-09-21 16:00:26 -0400514 key.offset = (u64)-1;
515 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100516 if (IS_ERR(new_root)) {
517 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
518 ret = PTR_ERR(new_root);
519 goto fail;
520 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400521
522 btrfs_record_root_in_trans(trans, new_root);
523
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000524 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700525 if (ret) {
526 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700528 goto fail;
529 }
530
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531 /*
532 * insert the directory item
533 */
Chris Mason3de45862008-11-17 21:02:50 -0500534 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100535 if (ret) {
536 btrfs_abort_transaction(trans, root, ret);
537 goto fail;
538 }
Chris Mason3de45862008-11-17 21:02:50 -0500539
540 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800541 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500542 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100543 if (ret) {
544 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400545 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100546 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500547
Yan Zheng52c26172009-01-05 15:43:43 -0500548 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
549 ret = btrfs_update_inode(trans, root, dir);
550 BUG_ON(ret);
551
Chris Mason0660b5a2008-11-17 20:37:39 -0500552 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400553 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800554 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500555 BUG_ON(ret);
556
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200557 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
558 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
559 objectid);
560 if (ret)
561 btrfs_abort_transaction(trans, root, ret);
562
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563fail:
Miao Xied5c12072013-02-28 10:04:33 +0000564 trans->block_rsv = NULL;
565 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800566 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
567
Sage Weil72fd0322010-10-29 15:41:32 -0400568 if (async_transid) {
569 *async_transid = trans->transid;
570 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000571 if (err)
572 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400573 } else {
574 err = btrfs_commit_transaction(trans, root);
575 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400576 if (err && !ret)
577 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500578
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900579 if (!ret) {
580 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800581 if (IS_ERR(inode))
582 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900583 d_instantiate(dentry, inode);
584 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400585 return ret;
586}
587
Miao Xiee9662f72013-02-28 10:01:15 +0000588static int create_snapshot(struct btrfs_root *root, struct inode *dir,
589 struct dentry *dentry, char *name, int namelen,
590 u64 *async_transid, bool readonly,
591 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400592{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000593 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 struct btrfs_pending_snapshot *pending_snapshot;
595 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000596 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400597
598 if (!root->ref_cows)
599 return -EINVAL;
600
Miao Xie6a038432013-05-15 07:48:24 +0000601 ret = btrfs_start_delalloc_inodes(root, 0);
602 if (ret)
603 return ret;
604
Miao Xieb0244192013-11-04 23:13:25 +0800605 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000606
Yan, Zhenga22285a2010-05-16 10:48:46 -0400607 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
608 if (!pending_snapshot)
609 return -ENOMEM;
610
Miao Xie66d8f3d2012-09-06 04:02:28 -0600611 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
612 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000613 /*
614 * 1 - parent dir inode
615 * 2 - dir entries
616 * 1 - root item
617 * 2 - root ref/backref
618 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200619 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000620 */
621 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200622 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400623 &pending_snapshot->qgroup_reserved,
624 false);
Miao Xied5c12072013-02-28 10:04:33 +0000625 if (ret)
626 goto out;
627
Yan, Zhenga22285a2010-05-16 10:48:46 -0400628 pending_snapshot->dentry = dentry;
629 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800630 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000631 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000632 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400633
Miao Xied5c12072013-02-28 10:04:33 +0000634 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400635 if (IS_ERR(trans)) {
636 ret = PTR_ERR(trans);
637 goto fail;
638 }
639
Josef Bacik83515832011-06-14 15:16:14 -0400640 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400641 list_add(&pending_snapshot->list,
642 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400643 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400644 if (async_transid) {
645 *async_transid = trans->transid;
646 ret = btrfs_commit_transaction_async(trans,
647 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000648 if (ret)
649 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400650 } else {
651 ret = btrfs_commit_transaction(trans,
652 root->fs_info->extent_root);
653 }
Miao Xieaec80302013-03-04 09:44:29 +0000654 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400655 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400656
657 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000659 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500661 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
662 if (ret)
663 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664
Al Viro2fbe8c82011-07-16 21:38:06 -0400665 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000666 if (IS_ERR(inode)) {
667 ret = PTR_ERR(inode);
668 goto fail;
669 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900670
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000671 d_instantiate(dentry, inode);
672 ret = 0;
673fail:
Miao Xied5c12072013-02-28 10:04:33 +0000674 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
675 &pending_snapshot->block_rsv,
676 pending_snapshot->qgroup_reserved);
677out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400678 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400679 return ret;
680}
681
Sage Weil4260f7c2010-10-29 15:46:43 -0400682/* copy of check_sticky in fs/namei.c()
683* It's inline, so penalty for filesystems that don't use sticky bit is
684* minimal.
685*/
686static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
687{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800688 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400689
690 if (!(dir->i_mode & S_ISVTX))
691 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800692 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400693 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800694 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400695 return 0;
696 return !capable(CAP_FOWNER);
697}
698
699/* copy of may_delete in fs/namei.c()
700 * Check whether we can remove a link victim from directory dir, check
701 * whether the type of victim is right.
702 * 1. We can't do it if dir is read-only (done in permission())
703 * 2. We should have write and exec permissions on dir
704 * 3. We can't remove anything from append-only dir
705 * 4. We can't do anything with immutable dir (done in permission())
706 * 5. If the sticky bit on dir is set we should either
707 * a. be owner of dir, or
708 * b. be owner of victim, or
709 * c. have CAP_FOWNER capability
710 * 6. If the victim is append-only or immutable we can't do antyhing with
711 * links pointing to it.
712 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
713 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
714 * 9. We can't remove a root or mountpoint.
715 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
716 * nfs_async_unlink().
717 */
718
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530719static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400720{
721 int error;
722
723 if (!victim->d_inode)
724 return -ENOENT;
725
726 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400727 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400728
729 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
730 if (error)
731 return error;
732 if (IS_APPEND(dir))
733 return -EPERM;
734 if (btrfs_check_sticky(dir, victim->d_inode)||
735 IS_APPEND(victim->d_inode)||
736 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
737 return -EPERM;
738 if (isdir) {
739 if (!S_ISDIR(victim->d_inode->i_mode))
740 return -ENOTDIR;
741 if (IS_ROOT(victim))
742 return -EBUSY;
743 } else if (S_ISDIR(victim->d_inode->i_mode))
744 return -EISDIR;
745 if (IS_DEADDIR(dir))
746 return -ENOENT;
747 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
748 return -EBUSY;
749 return 0;
750}
751
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400752/* copy of may_create in fs/namei.c() */
753static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
754{
755 if (child->d_inode)
756 return -EEXIST;
757 if (IS_DEADDIR(dir))
758 return -ENOENT;
759 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
760}
761
762/*
763 * Create a new subvolume below @parent. This is largely modeled after
764 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
765 * inside this filesystem so it's quite a bit simpler.
766 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400767static noinline int btrfs_mksubvol(struct path *parent,
768 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400769 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200770 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000771 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400772{
Yan, Zheng76dda932009-09-21 16:00:26 -0400773 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400774 struct dentry *dentry;
775 int error;
776
David Sterba5c50c9b2013-03-22 18:12:51 +0000777 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
778 if (error == -EINTR)
779 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400780
781 dentry = lookup_one_len(name, parent->dentry, namelen);
782 error = PTR_ERR(dentry);
783 if (IS_ERR(dentry))
784 goto out_unlock;
785
786 error = -EEXIST;
787 if (dentry->d_inode)
788 goto out_dput;
789
Yan, Zheng76dda932009-09-21 16:00:26 -0400790 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400791 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600792 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400793
Chris Mason9c520572012-12-17 14:26:57 -0500794 /*
795 * even if this name doesn't exist, we may get hash collisions.
796 * check for them now when we can safely fail
797 */
798 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
799 dir->i_ino, name,
800 namelen);
801 if (error)
802 goto out_dput;
803
Yan, Zheng76dda932009-09-21 16:00:26 -0400804 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
805
806 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
807 goto out_up_read;
808
Chris Mason3de45862008-11-17 21:02:50 -0500809 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000810 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200811 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500812 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000813 error = create_subvol(dir, dentry, name, namelen,
814 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500815 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400816 if (!error)
817 fsnotify_mkdir(dir, dentry);
818out_up_read:
819 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400820out_dput:
821 dput(dentry);
822out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400823 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400824 return error;
825}
826
Chris Mason4cb53002011-05-24 15:35:30 -0400827/*
828 * When we're defragging a range, we don't want to kick it off again
829 * if it is really just waiting for delalloc to send it down.
830 * If we find a nice big extent or delalloc range for the bytes in the
831 * file you want to defrag, we return 0 to let you know to skip this
832 * part of the file
833 */
834static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
835{
836 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
837 struct extent_map *em = NULL;
838 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
839 u64 end;
840
841 read_lock(&em_tree->lock);
842 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
843 read_unlock(&em_tree->lock);
844
845 if (em) {
846 end = extent_map_end(em);
847 free_extent_map(em);
848 if (end - offset > thresh)
849 return 0;
850 }
851 /* if we already have a nice delalloc here, just stop */
852 thresh /= 2;
853 end = count_range_bits(io_tree, &offset, offset + thresh,
854 thresh, EXTENT_DELALLOC, 1);
855 if (end >= thresh)
856 return 0;
857 return 1;
858}
859
860/*
861 * helper function to walk through a file and find extents
862 * newer than a specific transid, and smaller than thresh.
863 *
864 * This is used by the defragging code to find new and small
865 * extents
866 */
867static int find_new_extents(struct btrfs_root *root,
868 struct inode *inode, u64 newer_than,
869 u64 *off, int thresh)
870{
871 struct btrfs_path *path;
872 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400873 struct extent_buffer *leaf;
874 struct btrfs_file_extent_item *extent;
875 int type;
876 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000877 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400878
879 path = btrfs_alloc_path();
880 if (!path)
881 return -ENOMEM;
882
David Sterbaa4689d22011-05-31 17:08:14 +0000883 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400884 min_key.type = BTRFS_EXTENT_DATA_KEY;
885 min_key.offset = *off;
886
Chris Mason4cb53002011-05-24 15:35:30 -0400887 path->keep_locks = 1;
888
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530889 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100890 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400891 if (ret != 0)
892 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000893 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400894 goto none;
895 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
896 goto none;
897
898 leaf = path->nodes[0];
899 extent = btrfs_item_ptr(leaf, path->slots[0],
900 struct btrfs_file_extent_item);
901
902 type = btrfs_file_extent_type(leaf, extent);
903 if (type == BTRFS_FILE_EXTENT_REG &&
904 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
905 check_defrag_in_cache(inode, min_key.offset, thresh)) {
906 *off = min_key.offset;
907 btrfs_free_path(path);
908 return 0;
909 }
910
911 if (min_key.offset == (u64)-1)
912 goto none;
913
914 min_key.offset++;
915 btrfs_release_path(path);
916 }
917none:
918 btrfs_free_path(path);
919 return -ENOENT;
920}
921
Li Zefan6c282eb2012-06-11 16:03:35 +0800922static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400923{
924 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500925 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800926 struct extent_map *em;
927 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500928
929 /*
930 * hopefully we have this extent in the tree already, try without
931 * the full extent lock
932 */
933 read_lock(&em_tree->lock);
934 em = lookup_extent_mapping(em_tree, start, len);
935 read_unlock(&em_tree->lock);
936
937 if (!em) {
938 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100939 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500940 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100941 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500942
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000943 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800944 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500945 }
946
Li Zefan6c282eb2012-06-11 16:03:35 +0800947 return em;
948}
949
950static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
951{
952 struct extent_map *next;
953 bool ret = true;
954
955 /* this is the last extent */
956 if (em->start + em->len >= i_size_read(inode))
957 return false;
958
959 next = defrag_lookup_extent(inode, em->start + em->len);
960 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
961 ret = false;
962
963 free_extent_map(next);
964 return ret;
965}
966
967static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400968 u64 *last_len, u64 *skip, u64 *defrag_end,
969 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800970{
971 struct extent_map *em;
972 int ret = 1;
973 bool next_mergeable = true;
974
975 /*
976 * make sure that once we start defragging an extent, we keep on
977 * defragging it
978 */
979 if (start < *defrag_end)
980 return 1;
981
982 *skip = 0;
983
984 em = defrag_lookup_extent(inode, start);
985 if (!em)
986 return 0;
987
Chris Mason940100a2010-03-10 10:52:59 -0500988 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400989 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500990 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400991 goto out;
992 }
993
Li Zefan6c282eb2012-06-11 16:03:35 +0800994 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500995
996 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800997 * we hit a real extent, if it is big or the next extent is not a
998 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500999 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001000 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +08001001 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -05001002 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001003out:
Chris Mason940100a2010-03-10 10:52:59 -05001004 /*
1005 * last_len ends up being a counter of how many bytes we've defragged.
1006 * every time we choose not to defrag an extent, we reset *last_len
1007 * so that the next tiny extent will force a defrag.
1008 *
1009 * The end result of this is that tiny extents before a single big
1010 * extent will force at least part of that big extent to be defragged.
1011 */
1012 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001013 *defrag_end = extent_map_end(em);
1014 } else {
1015 *last_len = 0;
1016 *skip = extent_map_end(em);
1017 *defrag_end = 0;
1018 }
1019
1020 free_extent_map(em);
1021 return ret;
1022}
1023
Chris Mason4cb53002011-05-24 15:35:30 -04001024/*
1025 * it doesn't do much good to defrag one or two pages
1026 * at a time. This pulls in a nice chunk of pages
1027 * to COW and defrag.
1028 *
1029 * It also makes sure the delalloc code has enough
1030 * dirty data to avoid making new small extents as part
1031 * of the defrag
1032 *
1033 * It's a good idea to start RA on this range
1034 * before calling this.
1035 */
1036static int cluster_pages_for_defrag(struct inode *inode,
1037 struct page **pages,
1038 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001039 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001040{
Chris Mason4cb53002011-05-24 15:35:30 -04001041 unsigned long file_end;
1042 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001043 u64 page_start;
1044 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001045 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001046 int ret;
1047 int i;
1048 int i_done;
1049 struct btrfs_ordered_extent *ordered;
1050 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001051 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001052 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001053
Chris Mason4cb53002011-05-24 15:35:30 -04001054 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001055 if (!isize || start_index > file_end)
1056 return 0;
1057
1058 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001059
1060 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001061 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001062 if (ret)
1063 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001064 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001065 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001066
1067 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001068 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001069 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001070again:
Josef Bacika94733d2011-07-11 10:47:06 -04001071 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001072 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001073 if (!page)
1074 break;
1075
Miao Xie600a45e2012-02-16 15:01:24 +08001076 page_start = page_offset(page);
1077 page_end = page_start + PAGE_CACHE_SIZE - 1;
1078 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001079 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001080 ordered = btrfs_lookup_ordered_extent(inode,
1081 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001082 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001083 if (!ordered)
1084 break;
1085
1086 unlock_page(page);
1087 btrfs_start_ordered_extent(inode, ordered, 1);
1088 btrfs_put_ordered_extent(ordered);
1089 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001090 /*
1091 * we unlocked the page above, so we need check if
1092 * it was released or not.
1093 */
1094 if (page->mapping != inode->i_mapping) {
1095 unlock_page(page);
1096 page_cache_release(page);
1097 goto again;
1098 }
Miao Xie600a45e2012-02-16 15:01:24 +08001099 }
1100
Chris Mason4cb53002011-05-24 15:35:30 -04001101 if (!PageUptodate(page)) {
1102 btrfs_readpage(NULL, page);
1103 lock_page(page);
1104 if (!PageUptodate(page)) {
1105 unlock_page(page);
1106 page_cache_release(page);
1107 ret = -EIO;
1108 break;
1109 }
1110 }
Miao Xie600a45e2012-02-16 15:01:24 +08001111
Miao Xie600a45e2012-02-16 15:01:24 +08001112 if (page->mapping != inode->i_mapping) {
1113 unlock_page(page);
1114 page_cache_release(page);
1115 goto again;
1116 }
1117
Chris Mason4cb53002011-05-24 15:35:30 -04001118 pages[i] = page;
1119 i_done++;
1120 }
1121 if (!i_done || ret)
1122 goto out;
1123
1124 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1125 goto out;
1126
1127 /*
1128 * so now we have a nice long stream of locked
1129 * and up to date pages, lets wait on them
1130 */
1131 for (i = 0; i < i_done; i++)
1132 wait_on_page_writeback(pages[i]);
1133
1134 page_start = page_offset(pages[0]);
1135 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1136
1137 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001138 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001139 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1140 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001141 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1142 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001143
Liu Bo1f12bd02012-03-29 09:57:44 -04001144 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001145 spin_lock(&BTRFS_I(inode)->lock);
1146 BTRFS_I(inode)->outstanding_extents++;
1147 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001148 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001149 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001150 }
1151
1152
Liu Bo9e8a4a82012-09-05 19:10:51 -06001153 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1154 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001155
1156 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1157 page_start, page_end - 1, &cached_state,
1158 GFP_NOFS);
1159
1160 for (i = 0; i < i_done; i++) {
1161 clear_page_dirty_for_io(pages[i]);
1162 ClearPageChecked(pages[i]);
1163 set_page_extent_mapped(pages[i]);
1164 set_page_dirty(pages[i]);
1165 unlock_page(pages[i]);
1166 page_cache_release(pages[i]);
1167 }
1168 return i_done;
1169out:
1170 for (i = 0; i < i_done; i++) {
1171 unlock_page(pages[i]);
1172 page_cache_release(pages[i]);
1173 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001174 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001175 return ret;
1176
1177}
1178
1179int btrfs_defrag_file(struct inode *inode, struct file *file,
1180 struct btrfs_ioctl_defrag_range_args *range,
1181 u64 newer_than, unsigned long max_to_defrag)
1182{
1183 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001184 struct file_ra_state *ra = NULL;
1185 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001186 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001187 u64 last_len = 0;
1188 u64 skip = 0;
1189 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001190 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001191 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001192 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001193 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001194 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001195 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001196 int extent_thresh = range->extent_thresh;
Justin Maggardc41570c2014-01-21 11:18:29 -08001197 unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1198 unsigned long cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001199 u64 new_align = ~((u64)128 * 1024 - 1);
1200 struct page **pages = NULL;
1201
Liu Bo0abd5b12013-04-16 09:20:28 +00001202 if (isize == 0)
1203 return 0;
1204
1205 if (range->start >= isize)
1206 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001207
1208 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1209 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1210 return -EINVAL;
1211 if (range->compress_type)
1212 compress_type = range->compress_type;
1213 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001214
Liu Bo0abd5b12013-04-16 09:20:28 +00001215 if (extent_thresh == 0)
1216 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001217
Chris Mason4cb53002011-05-24 15:35:30 -04001218 /*
1219 * if we were not given a file, allocate a readahead
1220 * context
1221 */
1222 if (!file) {
1223 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1224 if (!ra)
1225 return -ENOMEM;
1226 file_ra_state_init(ra, inode->i_mapping);
1227 } else {
1228 ra = &file->f_ra;
1229 }
1230
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301231 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001232 GFP_NOFS);
1233 if (!pages) {
1234 ret = -ENOMEM;
1235 goto out_ra;
1236 }
1237
1238 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001239 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001240 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001241 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1242 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001243 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001244 }
1245
Chris Mason4cb53002011-05-24 15:35:30 -04001246 if (newer_than) {
1247 ret = find_new_extents(root, inode, newer_than,
1248 &newer_off, 64 * 1024);
1249 if (!ret) {
1250 range->start = newer_off;
1251 /*
1252 * we always align our defrag to help keep
1253 * the extents in the file evenly spaced
1254 */
1255 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001256 } else
1257 goto out_ra;
1258 } else {
1259 i = range->start >> PAGE_CACHE_SHIFT;
1260 }
1261 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001262 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001263
Li Zefan2a0f7f52011-10-10 15:43:34 -04001264 /*
1265 * make writeback starts from i, so the defrag range can be
1266 * written sequentially.
1267 */
1268 if (i < inode->i_mapping->writeback_index)
1269 inode->i_mapping->writeback_index = i;
1270
Chris Masonf7f43cc2011-10-11 11:41:40 -04001271 while (i <= last_index && defrag_count < max_to_defrag &&
1272 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1273 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001274 /*
1275 * make sure we stop running if someone unmounts
1276 * the FS
1277 */
1278 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1279 break;
1280
David Sterba210549e2013-02-09 23:38:06 +00001281 if (btrfs_defrag_cancelled(root->fs_info)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001282 printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
David Sterba210549e2013-02-09 23:38:06 +00001283 ret = -EAGAIN;
1284 break;
1285 }
1286
Liu Bo66c26892012-03-29 09:57:45 -04001287 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001288 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001289 &defrag_end, range->flags &
1290 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001291 unsigned long next;
1292 /*
1293 * the should_defrag function tells us how much to skip
1294 * bump our counter by the suggested amount
1295 */
1296 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1297 i = max(i + 1, next);
1298 continue;
1299 }
Li Zefan008873e2011-09-02 15:57:07 +08001300
1301 if (!newer_than) {
1302 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1303 PAGE_CACHE_SHIFT) - i;
1304 cluster = min(cluster, max_cluster);
1305 } else {
1306 cluster = max_cluster;
1307 }
1308
Li Zefan008873e2011-09-02 15:57:07 +08001309 if (i + cluster > ra_index) {
1310 ra_index = max(i, ra_index);
1311 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1312 cluster);
1313 ra_index += max_cluster;
1314 }
Chris Mason940100a2010-03-10 10:52:59 -05001315
Liu Boecb8bea2012-03-29 09:57:44 -04001316 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001317 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1318 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001319 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001320 if (ret < 0) {
1321 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001322 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001323 }
Chris Mason940100a2010-03-10 10:52:59 -05001324
Chris Mason4cb53002011-05-24 15:35:30 -04001325 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001326 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001327 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001328
1329 if (newer_than) {
1330 if (newer_off == (u64)-1)
1331 break;
1332
Liu Boe1f041e2012-03-29 09:57:45 -04001333 if (ret > 0)
1334 i += ret;
1335
Chris Mason4cb53002011-05-24 15:35:30 -04001336 newer_off = max(newer_off + 1,
1337 (u64)i << PAGE_CACHE_SHIFT);
1338
1339 ret = find_new_extents(root, inode,
1340 newer_than, &newer_off,
1341 64 * 1024);
1342 if (!ret) {
1343 range->start = newer_off;
1344 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001345 } else {
1346 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001347 }
Chris Mason4cb53002011-05-24 15:35:30 -04001348 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001349 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001350 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001351 last_len += ret << PAGE_CACHE_SHIFT;
1352 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001353 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001354 last_len = 0;
1355 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001356 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001357 }
1358
Chris Mason1e701a32010-03-11 09:42:04 -05001359 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1360 filemap_flush(inode->i_mapping);
1361
1362 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1363 /* the filemap_flush will queue IO into the worker threads, but
1364 * we have to make sure the IO is actually started and that
1365 * ordered extents get created before we return
1366 */
1367 atomic_inc(&root->fs_info->async_submit_draining);
1368 while (atomic_read(&root->fs_info->nr_async_submits) ||
1369 atomic_read(&root->fs_info->async_delalloc_pages)) {
1370 wait_event(root->fs_info->async_submit_wait,
1371 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1372 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1373 }
1374 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001375 }
1376
Li Zefan1a419d82010-10-25 15:12:50 +08001377 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001378 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001379 }
1380
Diego Calleja60ccf822011-09-01 16:33:57 +02001381 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001382
Chris Mason4cb53002011-05-24 15:35:30 -04001383out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001384 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1385 mutex_lock(&inode->i_mutex);
1386 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1387 mutex_unlock(&inode->i_mutex);
1388 }
Chris Mason4cb53002011-05-24 15:35:30 -04001389 if (!file)
1390 kfree(ra);
1391 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001392 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001393}
1394
Miao Xie198605a2012-11-26 08:43:45 +00001395static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001396 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001397{
1398 u64 new_size;
1399 u64 old_size;
1400 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001401 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001402 struct btrfs_ioctl_vol_args *vol_args;
1403 struct btrfs_trans_handle *trans;
1404 struct btrfs_device *device = NULL;
1405 char *sizestr;
1406 char *devstr = NULL;
1407 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001408 int mod = 0;
1409
Chris Masone441d542009-01-05 16:57:23 -05001410 if (!capable(CAP_SYS_ADMIN))
1411 return -EPERM;
1412
Miao Xie198605a2012-11-26 08:43:45 +00001413 ret = mnt_want_write_file(file);
1414 if (ret)
1415 return ret;
1416
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001417 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1418 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001419 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001420 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001421 }
1422
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001423 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001424 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001425 if (IS_ERR(vol_args)) {
1426 ret = PTR_ERR(vol_args);
1427 goto out;
1428 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001429
1430 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001432 sizestr = vol_args->name;
1433 devstr = strchr(sizestr, ':');
1434 if (devstr) {
1435 char *end;
1436 sizestr = devstr + 1;
1437 *devstr = '\0';
1438 devstr = vol_args->name;
1439 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001440 if (!devid) {
1441 ret = -EINVAL;
1442 goto out_free;
1443 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001444 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001445 }
Miao Xiedba60f32012-12-21 09:19:51 +00001446
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001447 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001448 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001449 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001450 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001451 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001452 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001453 }
Miao Xiedba60f32012-12-21 09:19:51 +00001454
1455 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001456 btrfs_info(root->fs_info,
1457 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001458 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001459 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001460 goto out_free;
1461 }
1462
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463 if (!strcmp(sizestr, "max"))
1464 new_size = device->bdev->bd_inode->i_size;
1465 else {
1466 if (sizestr[0] == '-') {
1467 mod = -1;
1468 sizestr++;
1469 } else if (sizestr[0] == '+') {
1470 mod = 1;
1471 sizestr++;
1472 }
Akinobu Mita91748462010-02-28 10:59:11 +00001473 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001474 if (new_size == 0) {
1475 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001476 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001477 }
1478 }
1479
Stefan Behrens63a212a2012-11-05 18:29:28 +01001480 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001481 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001482 goto out_free;
1483 }
1484
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001485 old_size = device->total_bytes;
1486
1487 if (mod < 0) {
1488 if (new_size > old_size) {
1489 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001490 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001491 }
1492 new_size = old_size - new_size;
1493 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001494 if (new_size > ULLONG_MAX - old_size) {
1495 ret = -EINVAL;
1496 goto out_free;
1497 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001498 new_size = old_size + new_size;
1499 }
1500
1501 if (new_size < 256 * 1024 * 1024) {
1502 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001503 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001504 }
1505 if (new_size > device->bdev->bd_inode->i_size) {
1506 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001507 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001508 }
1509
1510 do_div(new_size, root->sectorsize);
1511 new_size *= root->sectorsize;
1512
Frank Holtonefe120a2013-12-20 11:37:06 -05001513 printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001514 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001515
1516 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001517 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001518 if (IS_ERR(trans)) {
1519 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001520 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001521 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001522 ret = btrfs_grow_device(trans, device, new_size);
1523 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001524 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001525 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001526 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001527
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001528out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001530out:
1531 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001532 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001533 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001534 return ret;
1535}
1536
Sage Weil72fd0322010-10-29 15:41:32 -04001537static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001538 char *name, unsigned long fd, int subvol,
1539 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001540 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001541{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001542 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001543 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001544
Liu Boa874a632012-06-29 03:58:46 -06001545 ret = mnt_want_write_file(file);
1546 if (ret)
1547 goto out;
1548
Sage Weil72fd0322010-10-29 15:41:32 -04001549 namelen = strlen(name);
1550 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001551 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001552 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553 }
1554
Chris Mason16780ca2012-02-20 22:14:55 -05001555 if (name[0] == '.' &&
1556 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1557 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001558 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001559 }
1560
Chris Mason3de45862008-11-17 21:02:50 -05001561 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001562 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001563 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001564 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001565 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001566 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001567 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001568 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001569 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001570 }
1571
Al Viro496ad9a2013-01-23 17:07:38 -05001572 src_inode = file_inode(src.file);
1573 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001574 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1575 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001576 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001577 } else if (!inode_owner_or_capable(src_inode)) {
1578 /*
1579 * Subvolume creation is not restricted, but snapshots
1580 * are limited to own subvolumes only
1581 */
1582 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001583 } else {
1584 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1585 BTRFS_I(src_inode)->root,
1586 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001587 }
Al Viro2903ff02012-08-28 12:52:22 -04001588 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001589 }
Liu Boa874a632012-06-29 03:58:46 -06001590out_drop_write:
1591 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001592out:
Sage Weil72fd0322010-10-29 15:41:32 -04001593 return ret;
1594}
1595
1596static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001597 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001598{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001599 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001600 int ret;
1601
Li Zefanfa0d2b92010-12-20 15:53:28 +08001602 vol_args = memdup_user(arg, sizeof(*vol_args));
1603 if (IS_ERR(vol_args))
1604 return PTR_ERR(vol_args);
1605 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001606
Li Zefanfa0d2b92010-12-20 15:53:28 +08001607 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001608 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001609 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001610
Li Zefanfa0d2b92010-12-20 15:53:28 +08001611 kfree(vol_args);
1612 return ret;
1613}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001614
Li Zefanfa0d2b92010-12-20 15:53:28 +08001615static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1616 void __user *arg, int subvol)
1617{
1618 struct btrfs_ioctl_vol_args_v2 *vol_args;
1619 int ret;
1620 u64 transid = 0;
1621 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001622 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001623 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001624
Li Zefanfa0d2b92010-12-20 15:53:28 +08001625 vol_args = memdup_user(arg, sizeof(*vol_args));
1626 if (IS_ERR(vol_args))
1627 return PTR_ERR(vol_args);
1628 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001629
Li Zefanb83cc962010-12-20 16:04:08 +08001630 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001631 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1632 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001633 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001634 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001635 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001636
1637 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1638 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001639 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1640 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001641 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1642 if (vol_args->size > PAGE_CACHE_SIZE) {
1643 ret = -EINVAL;
1644 goto out;
1645 }
1646 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1647 if (IS_ERR(inherit)) {
1648 ret = PTR_ERR(inherit);
1649 goto out;
1650 }
1651 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001652
1653 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001654 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001655 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001656
1657 if (ret == 0 && ptr &&
1658 copy_to_user(arg +
1659 offsetof(struct btrfs_ioctl_vol_args_v2,
1660 transid), ptr, sizeof(*ptr)))
1661 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001662out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001663 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001664 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001665 return ret;
1666}
1667
Li Zefan0caa1022010-12-20 16:30:25 +08001668static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1669 void __user *arg)
1670{
Al Viro496ad9a2013-01-23 17:07:38 -05001671 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001672 struct btrfs_root *root = BTRFS_I(inode)->root;
1673 int ret = 0;
1674 u64 flags = 0;
1675
Li Zefan33345d012011-04-20 10:31:50 +08001676 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001677 return -EINVAL;
1678
1679 down_read(&root->fs_info->subvol_sem);
1680 if (btrfs_root_readonly(root))
1681 flags |= BTRFS_SUBVOL_RDONLY;
1682 up_read(&root->fs_info->subvol_sem);
1683
1684 if (copy_to_user(arg, &flags, sizeof(flags)))
1685 ret = -EFAULT;
1686
1687 return ret;
1688}
1689
1690static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1691 void __user *arg)
1692{
Al Viro496ad9a2013-01-23 17:07:38 -05001693 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001694 struct btrfs_root *root = BTRFS_I(inode)->root;
1695 struct btrfs_trans_handle *trans;
1696 u64 root_flags;
1697 u64 flags;
1698 int ret = 0;
1699
David Sterbabd60ea02014-01-16 15:50:22 +01001700 if (!inode_owner_or_capable(inode))
1701 return -EPERM;
1702
Liu Bob9ca0662012-06-29 03:58:49 -06001703 ret = mnt_want_write_file(file);
1704 if (ret)
1705 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001706
Liu Bob9ca0662012-06-29 03:58:49 -06001707 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1708 ret = -EINVAL;
1709 goto out_drop_write;
1710 }
Li Zefan0caa1022010-12-20 16:30:25 +08001711
Liu Bob9ca0662012-06-29 03:58:49 -06001712 if (copy_from_user(&flags, arg, sizeof(flags))) {
1713 ret = -EFAULT;
1714 goto out_drop_write;
1715 }
Li Zefan0caa1022010-12-20 16:30:25 +08001716
Liu Bob9ca0662012-06-29 03:58:49 -06001717 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1718 ret = -EINVAL;
1719 goto out_drop_write;
1720 }
Li Zefan0caa1022010-12-20 16:30:25 +08001721
Liu Bob9ca0662012-06-29 03:58:49 -06001722 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1723 ret = -EOPNOTSUPP;
1724 goto out_drop_write;
1725 }
Li Zefan0caa1022010-12-20 16:30:25 +08001726
1727 down_write(&root->fs_info->subvol_sem);
1728
1729 /* nothing to do */
1730 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001731 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001732
1733 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001734 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001735 btrfs_set_root_flags(&root->root_item,
1736 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001737 } else {
1738 /*
1739 * Block RO -> RW transition if this subvolume is involved in
1740 * send
1741 */
1742 spin_lock(&root->root_item_lock);
1743 if (root->send_in_progress == 0) {
1744 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001745 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001746 spin_unlock(&root->root_item_lock);
1747 } else {
1748 spin_unlock(&root->root_item_lock);
1749 btrfs_warn(root->fs_info,
1750 "Attempt to set subvolume %llu read-write during send",
1751 root->root_key.objectid);
1752 ret = -EPERM;
1753 goto out_drop_sem;
1754 }
1755 }
Li Zefan0caa1022010-12-20 16:30:25 +08001756
1757 trans = btrfs_start_transaction(root, 1);
1758 if (IS_ERR(trans)) {
1759 ret = PTR_ERR(trans);
1760 goto out_reset;
1761 }
1762
Li Zefanb4dc2b82011-02-16 06:06:34 +00001763 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001764 &root->root_key, &root->root_item);
1765
1766 btrfs_commit_transaction(trans, root);
1767out_reset:
1768 if (ret)
1769 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001770out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001771 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001772out_drop_write:
1773 mnt_drop_write_file(file);
1774out:
Li Zefan0caa1022010-12-20 16:30:25 +08001775 return ret;
1776}
1777
Yan, Zheng76dda932009-09-21 16:00:26 -04001778/*
1779 * helper to check if the subvolume references other subvolumes
1780 */
1781static noinline int may_destroy_subvol(struct btrfs_root *root)
1782{
1783 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001784 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001785 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001786 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001787 int ret;
1788
1789 path = btrfs_alloc_path();
1790 if (!path)
1791 return -ENOMEM;
1792
Josef Bacik175a2b82013-08-12 15:36:44 -04001793 /* Make sure this root isn't set as the default subvol */
1794 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1795 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1796 dir_id, "default", 7, 0);
1797 if (di && !IS_ERR(di)) {
1798 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1799 if (key.objectid == root->root_key.objectid) {
1800 ret = -ENOTEMPTY;
1801 goto out;
1802 }
1803 btrfs_release_path(path);
1804 }
1805
Yan, Zheng76dda932009-09-21 16:00:26 -04001806 key.objectid = root->root_key.objectid;
1807 key.type = BTRFS_ROOT_REF_KEY;
1808 key.offset = (u64)-1;
1809
1810 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1811 &key, path, 0, 0);
1812 if (ret < 0)
1813 goto out;
1814 BUG_ON(ret == 0);
1815
1816 ret = 0;
1817 if (path->slots[0] > 0) {
1818 path->slots[0]--;
1819 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1820 if (key.objectid == root->root_key.objectid &&
1821 key.type == BTRFS_ROOT_REF_KEY)
1822 ret = -ENOTEMPTY;
1823 }
1824out:
1825 btrfs_free_path(path);
1826 return ret;
1827}
1828
Chris Masonac8e9812010-02-28 15:39:26 -05001829static noinline int key_in_sk(struct btrfs_key *key,
1830 struct btrfs_ioctl_search_key *sk)
1831{
Chris Masonabc6e132010-03-18 12:10:08 -04001832 struct btrfs_key test;
1833 int ret;
1834
1835 test.objectid = sk->min_objectid;
1836 test.type = sk->min_type;
1837 test.offset = sk->min_offset;
1838
1839 ret = btrfs_comp_cpu_keys(key, &test);
1840 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001841 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001842
1843 test.objectid = sk->max_objectid;
1844 test.type = sk->max_type;
1845 test.offset = sk->max_offset;
1846
1847 ret = btrfs_comp_cpu_keys(key, &test);
1848 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001849 return 0;
1850 return 1;
1851}
1852
1853static noinline int copy_to_sk(struct btrfs_root *root,
1854 struct btrfs_path *path,
1855 struct btrfs_key *key,
1856 struct btrfs_ioctl_search_key *sk,
1857 char *buf,
1858 unsigned long *sk_offset,
1859 int *num_found)
1860{
1861 u64 found_transid;
1862 struct extent_buffer *leaf;
1863 struct btrfs_ioctl_search_header sh;
1864 unsigned long item_off;
1865 unsigned long item_len;
1866 int nritems;
1867 int i;
1868 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001869 int ret = 0;
1870
1871 leaf = path->nodes[0];
1872 slot = path->slots[0];
1873 nritems = btrfs_header_nritems(leaf);
1874
1875 if (btrfs_header_generation(leaf) > sk->max_transid) {
1876 i = nritems;
1877 goto advance_key;
1878 }
1879 found_transid = btrfs_header_generation(leaf);
1880
1881 for (i = slot; i < nritems; i++) {
1882 item_off = btrfs_item_ptr_offset(leaf, i);
1883 item_len = btrfs_item_size_nr(leaf, i);
1884
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001885 btrfs_item_key_to_cpu(leaf, key, i);
1886 if (!key_in_sk(key, sk))
1887 continue;
1888
1889 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001890 item_len = 0;
1891
1892 if (sizeof(sh) + item_len + *sk_offset >
1893 BTRFS_SEARCH_ARGS_BUFSIZE) {
1894 ret = 1;
1895 goto overflow;
1896 }
1897
Chris Masonac8e9812010-02-28 15:39:26 -05001898 sh.objectid = key->objectid;
1899 sh.offset = key->offset;
1900 sh.type = key->type;
1901 sh.len = item_len;
1902 sh.transid = found_transid;
1903
1904 /* copy search result header */
1905 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1906 *sk_offset += sizeof(sh);
1907
1908 if (item_len) {
1909 char *p = buf + *sk_offset;
1910 /* copy the item */
1911 read_extent_buffer(leaf, p,
1912 item_off, item_len);
1913 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001914 }
Hugo Millse2156862011-05-14 17:43:41 +00001915 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001916
1917 if (*num_found >= sk->nr_items)
1918 break;
1919 }
1920advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001921 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001922 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1923 key->offset++;
1924 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1925 key->offset = 0;
1926 key->type++;
1927 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1928 key->offset = 0;
1929 key->type = 0;
1930 key->objectid++;
1931 } else
1932 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001933overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001934 return ret;
1935}
1936
1937static noinline int search_ioctl(struct inode *inode,
1938 struct btrfs_ioctl_search_args *args)
1939{
1940 struct btrfs_root *root;
1941 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001942 struct btrfs_path *path;
1943 struct btrfs_ioctl_search_key *sk = &args->key;
1944 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1945 int ret;
1946 int num_found = 0;
1947 unsigned long sk_offset = 0;
1948
1949 path = btrfs_alloc_path();
1950 if (!path)
1951 return -ENOMEM;
1952
1953 if (sk->tree_id == 0) {
1954 /* search the root of the inode that was passed */
1955 root = BTRFS_I(inode)->root;
1956 } else {
1957 key.objectid = sk->tree_id;
1958 key.type = BTRFS_ROOT_ITEM_KEY;
1959 key.offset = (u64)-1;
1960 root = btrfs_read_fs_root_no_name(info, &key);
1961 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001962 printk(KERN_ERR "BTRFS: could not find root %llu\n",
Chris Masonac8e9812010-02-28 15:39:26 -05001963 sk->tree_id);
1964 btrfs_free_path(path);
1965 return -ENOENT;
1966 }
1967 }
1968
1969 key.objectid = sk->min_objectid;
1970 key.type = sk->min_type;
1971 key.offset = sk->min_offset;
1972
Chris Masonac8e9812010-02-28 15:39:26 -05001973 path->keep_locks = 1;
1974
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301975 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001976 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05001977 if (ret != 0) {
1978 if (ret > 0)
1979 ret = 0;
1980 goto err;
1981 }
1982 ret = copy_to_sk(root, path, &key, sk, args->buf,
1983 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001984 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001985 if (ret || num_found >= sk->nr_items)
1986 break;
1987
1988 }
1989 ret = 0;
1990err:
1991 sk->nr_items = num_found;
1992 btrfs_free_path(path);
1993 return ret;
1994}
1995
1996static noinline int btrfs_ioctl_tree_search(struct file *file,
1997 void __user *argp)
1998{
1999 struct btrfs_ioctl_search_args *args;
2000 struct inode *inode;
2001 int ret;
2002
2003 if (!capable(CAP_SYS_ADMIN))
2004 return -EPERM;
2005
Julia Lawall2354d08f2010-10-29 15:14:18 -04002006 args = memdup_user(argp, sizeof(*args));
2007 if (IS_ERR(args))
2008 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05002009
Al Viro496ad9a2013-01-23 17:07:38 -05002010 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002011 ret = search_ioctl(inode, args);
2012 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2013 ret = -EFAULT;
2014 kfree(args);
2015 return ret;
2016}
2017
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002018/*
Chris Masonac8e9812010-02-28 15:39:26 -05002019 * Search INODE_REFs to identify path name of 'dirid' directory
2020 * in a 'tree_id' tree. and sets path name to 'name'.
2021 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002022static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2023 u64 tree_id, u64 dirid, char *name)
2024{
2025 struct btrfs_root *root;
2026 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002027 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002028 int ret = -1;
2029 int slot;
2030 int len;
2031 int total_len = 0;
2032 struct btrfs_inode_ref *iref;
2033 struct extent_buffer *l;
2034 struct btrfs_path *path;
2035
2036 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2037 name[0]='\0';
2038 return 0;
2039 }
2040
2041 path = btrfs_alloc_path();
2042 if (!path)
2043 return -ENOMEM;
2044
Chris Masonac8e9812010-02-28 15:39:26 -05002045 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002046
2047 key.objectid = tree_id;
2048 key.type = BTRFS_ROOT_ITEM_KEY;
2049 key.offset = (u64)-1;
2050 root = btrfs_read_fs_root_no_name(info, &key);
2051 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002052 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002053 ret = -ENOENT;
2054 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002055 }
2056
2057 key.objectid = dirid;
2058 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002059 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002060
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302061 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002062 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2063 if (ret < 0)
2064 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002065 else if (ret > 0) {
2066 ret = btrfs_previous_item(root, path, dirid,
2067 BTRFS_INODE_REF_KEY);
2068 if (ret < 0)
2069 goto out;
2070 else if (ret > 0) {
2071 ret = -ENOENT;
2072 goto out;
2073 }
2074 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002075
2076 l = path->nodes[0];
2077 slot = path->slots[0];
2078 btrfs_item_key_to_cpu(l, &key, slot);
2079
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002080 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2081 len = btrfs_inode_ref_name_len(l, iref);
2082 ptr -= len + 1;
2083 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002084 if (ptr < name) {
2085 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002086 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002087 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002088
2089 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302090 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002091
2092 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2093 break;
2094
David Sterbab3b4aa72011-04-21 01:20:15 +02002095 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002096 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002097 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002098 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002099 }
Li Zefan77906a502011-07-14 03:16:00 +00002100 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302101 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002102 ret = 0;
2103out:
2104 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002105 return ret;
2106}
2107
2108static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2109 void __user *argp)
2110{
2111 struct btrfs_ioctl_ino_lookup_args *args;
2112 struct inode *inode;
2113 int ret;
2114
2115 if (!capable(CAP_SYS_ADMIN))
2116 return -EPERM;
2117
Julia Lawall2354d08f2010-10-29 15:14:18 -04002118 args = memdup_user(argp, sizeof(*args));
2119 if (IS_ERR(args))
2120 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002121
Al Viro496ad9a2013-01-23 17:07:38 -05002122 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002123
Chris Mason1b53ac42010-03-18 12:17:05 -04002124 if (args->treeid == 0)
2125 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2126
Chris Masonac8e9812010-02-28 15:39:26 -05002127 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2128 args->treeid, args->objectid,
2129 args->name);
2130
2131 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2132 ret = -EFAULT;
2133
2134 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002135 return ret;
2136}
2137
Yan, Zheng76dda932009-09-21 16:00:26 -04002138static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2139 void __user *arg)
2140{
Al Viro54563d42013-09-01 15:57:51 -04002141 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002142 struct dentry *dentry;
2143 struct inode *dir = parent->d_inode;
2144 struct inode *inode;
2145 struct btrfs_root *root = BTRFS_I(dir)->root;
2146 struct btrfs_root *dest = NULL;
2147 struct btrfs_ioctl_vol_args *vol_args;
2148 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002149 struct btrfs_block_rsv block_rsv;
2150 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002151 int namelen;
2152 int ret;
2153 int err = 0;
2154
Yan, Zheng76dda932009-09-21 16:00:26 -04002155 vol_args = memdup_user(arg, sizeof(*vol_args));
2156 if (IS_ERR(vol_args))
2157 return PTR_ERR(vol_args);
2158
2159 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2160 namelen = strlen(vol_args->name);
2161 if (strchr(vol_args->name, '/') ||
2162 strncmp(vol_args->name, "..", namelen) == 0) {
2163 err = -EINVAL;
2164 goto out;
2165 }
2166
Al Viroa561be72011-11-23 11:57:51 -05002167 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002168 if (err)
2169 goto out;
2170
David Sterba5c50c9b2013-03-22 18:12:51 +00002171 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2172 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002173 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002174 dentry = lookup_one_len(vol_args->name, parent, namelen);
2175 if (IS_ERR(dentry)) {
2176 err = PTR_ERR(dentry);
2177 goto out_unlock_dir;
2178 }
2179
2180 if (!dentry->d_inode) {
2181 err = -ENOENT;
2182 goto out_dput;
2183 }
2184
2185 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002186 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302187 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002188 /*
2189 * Regular user. Only allow this with a special mount
2190 * option, when the user has write+exec access to the
2191 * subvol root, and when rmdir(2) would have been
2192 * allowed.
2193 *
2194 * Note that this is _not_ check that the subvol is
2195 * empty or doesn't contain data that we wouldn't
2196 * otherwise be able to delete.
2197 *
2198 * Users who want to delete empty subvols should try
2199 * rmdir(2).
2200 */
2201 err = -EPERM;
2202 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2203 goto out_dput;
2204
2205 /*
2206 * Do not allow deletion if the parent dir is the same
2207 * as the dir to be deleted. That means the ioctl
2208 * must be called on the dentry referencing the root
2209 * of the subvol, not a random directory contained
2210 * within it.
2211 */
2212 err = -EINVAL;
2213 if (root == dest)
2214 goto out_dput;
2215
2216 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2217 if (err)
2218 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002219 }
2220
Miao Xie5c39da52012-10-22 11:39:53 +00002221 /* check if subvolume may be deleted by a user */
2222 err = btrfs_may_delete(dir, dentry, 1);
2223 if (err)
2224 goto out_dput;
2225
Li Zefan33345d012011-04-20 10:31:50 +08002226 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002227 err = -EINVAL;
2228 goto out_dput;
2229 }
2230
Yan, Zheng76dda932009-09-21 16:00:26 -04002231 mutex_lock(&inode->i_mutex);
2232 err = d_invalidate(dentry);
2233 if (err)
2234 goto out_unlock;
2235
2236 down_write(&root->fs_info->subvol_sem);
2237
2238 err = may_destroy_subvol(dest);
2239 if (err)
2240 goto out_up_write;
2241
Miao Xiec58aaad2013-02-28 10:05:36 +00002242 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2243 /*
2244 * One for dir inode, two for dir entries, two for root
2245 * ref/backref.
2246 */
2247 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002248 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002249 if (err)
2250 goto out_up_write;
2251
Yan, Zhenga22285a2010-05-16 10:48:46 -04002252 trans = btrfs_start_transaction(root, 0);
2253 if (IS_ERR(trans)) {
2254 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002255 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002256 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002257 trans->block_rsv = &block_rsv;
2258 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002259
Yan, Zheng76dda932009-09-21 16:00:26 -04002260 ret = btrfs_unlink_subvol(trans, root, dir,
2261 dest->root_key.objectid,
2262 dentry->d_name.name,
2263 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264 if (ret) {
2265 err = ret;
2266 btrfs_abort_transaction(trans, root, ret);
2267 goto out_end_trans;
2268 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002269
2270 btrfs_record_root_in_trans(trans, dest);
2271
2272 memset(&dest->root_item.drop_progress, 0,
2273 sizeof(dest->root_item.drop_progress));
2274 dest->root_item.drop_level = 0;
2275 btrfs_set_root_refs(&dest->root_item, 0);
2276
Yan, Zhengd68fc572010-05-16 10:49:58 -04002277 if (!xchg(&dest->orphan_item_inserted, 1)) {
2278 ret = btrfs_insert_orphan_item(trans,
2279 root->fs_info->tree_root,
2280 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002281 if (ret) {
2282 btrfs_abort_transaction(trans, root, ret);
2283 err = ret;
2284 goto out_end_trans;
2285 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002286 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002287
2288 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2289 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2290 dest->root_key.objectid);
2291 if (ret && ret != -ENOENT) {
2292 btrfs_abort_transaction(trans, root, ret);
2293 err = ret;
2294 goto out_end_trans;
2295 }
2296 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2297 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2298 dest->root_item.received_uuid,
2299 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2300 dest->root_key.objectid);
2301 if (ret && ret != -ENOENT) {
2302 btrfs_abort_transaction(trans, root, ret);
2303 err = ret;
2304 goto out_end_trans;
2305 }
2306 }
2307
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002308out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002309 trans->block_rsv = NULL;
2310 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002311 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002312 if (ret && !err)
2313 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002314 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002315out_release:
2316 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002317out_up_write:
2318 up_write(&root->fs_info->subvol_sem);
2319out_unlock:
2320 mutex_unlock(&inode->i_mutex);
2321 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002322 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002323 btrfs_invalidate_inodes(dest);
2324 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002325
2326 /* the last ref */
2327 if (dest->cache_inode) {
2328 iput(dest->cache_inode);
2329 dest->cache_inode = NULL;
2330 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002331 }
2332out_dput:
2333 dput(dentry);
2334out_unlock_dir:
2335 mutex_unlock(&dir->i_mutex);
David Sterbae43f9982013-12-06 17:51:32 +01002336out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002337 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002338out:
2339 kfree(vol_args);
2340 return err;
2341}
2342
Chris Mason1e701a32010-03-11 09:42:04 -05002343static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002344{
Al Viro496ad9a2013-01-23 17:07:38 -05002345 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002346 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002347 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002348 int ret;
2349
Ilya Dryomov25122d12013-01-20 15:57:57 +02002350 ret = mnt_want_write_file(file);
2351 if (ret)
2352 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002353
Ilya Dryomov25122d12013-01-20 15:57:57 +02002354 if (btrfs_root_readonly(root)) {
2355 ret = -EROFS;
2356 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002357 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002358
2359 switch (inode->i_mode & S_IFMT) {
2360 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002361 if (!capable(CAP_SYS_ADMIN)) {
2362 ret = -EPERM;
2363 goto out;
2364 }
Eric Sandeende78b512013-01-31 18:21:12 +00002365 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002366 if (ret)
2367 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002368 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002369 break;
2370 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002371 if (!(file->f_mode & FMODE_WRITE)) {
2372 ret = -EINVAL;
2373 goto out;
2374 }
Chris Mason1e701a32010-03-11 09:42:04 -05002375
2376 range = kzalloc(sizeof(*range), GFP_KERNEL);
2377 if (!range) {
2378 ret = -ENOMEM;
2379 goto out;
2380 }
2381
2382 if (argp) {
2383 if (copy_from_user(range, argp,
2384 sizeof(*range))) {
2385 ret = -EFAULT;
2386 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002387 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002388 }
2389 /* compression requires us to start the IO */
2390 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2391 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2392 range->extent_thresh = (u32)-1;
2393 }
2394 } else {
2395 /* the rest are all set to zero by kzalloc */
2396 range->len = (u64)-1;
2397 }
Al Viro496ad9a2013-01-23 17:07:38 -05002398 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002399 range, 0, 0);
2400 if (ret > 0)
2401 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002402 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002403 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002404 default:
2405 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002406 }
Chris Masone441d542009-01-05 16:57:23 -05002407out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002408 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002409 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410}
2411
Christoph Hellwigb2950862008-12-02 09:54:17 -05002412static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413{
2414 struct btrfs_ioctl_vol_args *vol_args;
2415 int ret;
2416
Chris Masone441d542009-01-05 16:57:23 -05002417 if (!capable(CAP_SYS_ADMIN))
2418 return -EPERM;
2419
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002420 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2421 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002422 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002423 }
2424
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002425 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002426 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002427 if (IS_ERR(vol_args)) {
2428 ret = PTR_ERR(vol_args);
2429 goto out;
2430 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002431
Mark Fasheh5516e592008-07-24 12:20:14 -04002432 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002433 ret = btrfs_init_new_device(root, vol_args->name);
2434
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002435 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002436out:
2437 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002438 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002439 return ret;
2440}
2441
Miao Xieda249272012-11-26 08:44:50 +00002442static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002443{
Al Viro496ad9a2013-01-23 17:07:38 -05002444 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002445 struct btrfs_ioctl_vol_args *vol_args;
2446 int ret;
2447
Chris Masone441d542009-01-05 16:57:23 -05002448 if (!capable(CAP_SYS_ADMIN))
2449 return -EPERM;
2450
Miao Xieda249272012-11-26 08:44:50 +00002451 ret = mnt_want_write_file(file);
2452 if (ret)
2453 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002454
Li Zefandae7b662009-04-08 15:06:54 +08002455 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002456 if (IS_ERR(vol_args)) {
2457 ret = PTR_ERR(vol_args);
2458 goto out;
2459 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002460
Mark Fasheh5516e592008-07-24 12:20:14 -04002461 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002462
Anand Jain183860f2013-05-17 10:52:45 +00002463 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2464 1)) {
2465 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2466 goto out;
2467 }
2468
2469 mutex_lock(&root->fs_info->volume_mutex);
2470 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002471 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002472 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002473
2474out:
2475 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002476 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002477 return ret;
2478}
2479
Jan Schmidt475f6382011-03-11 15:41:01 +01002480static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2481{
Li Zefan027ed2f2011-06-08 08:27:56 +00002482 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002483 struct btrfs_device *device;
2484 struct btrfs_device *next;
2485 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002486 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002487
2488 if (!capable(CAP_SYS_ADMIN))
2489 return -EPERM;
2490
Li Zefan027ed2f2011-06-08 08:27:56 +00002491 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2492 if (!fi_args)
2493 return -ENOMEM;
2494
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002495 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002496 fi_args->num_devices = fs_devices->num_devices;
2497 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002498
Jan Schmidt475f6382011-03-11 15:41:01 +01002499 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002500 if (device->devid > fi_args->max_id)
2501 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002502 }
2503 mutex_unlock(&fs_devices->device_list_mutex);
2504
Li Zefan027ed2f2011-06-08 08:27:56 +00002505 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2506 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002507
Li Zefan027ed2f2011-06-08 08:27:56 +00002508 kfree(fi_args);
2509 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002510}
2511
2512static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2513{
2514 struct btrfs_ioctl_dev_info_args *di_args;
2515 struct btrfs_device *dev;
2516 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2517 int ret = 0;
2518 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002519
2520 if (!capable(CAP_SYS_ADMIN))
2521 return -EPERM;
2522
2523 di_args = memdup_user(arg, sizeof(*di_args));
2524 if (IS_ERR(di_args))
2525 return PTR_ERR(di_args);
2526
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002527 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002528 s_uuid = di_args->uuid;
2529
2530 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002531 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002532
2533 if (!dev) {
2534 ret = -ENODEV;
2535 goto out;
2536 }
2537
2538 di_args->devid = dev->devid;
2539 di_args->bytes_used = dev->bytes_used;
2540 di_args->total_bytes = dev->total_bytes;
2541 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002542 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002543 struct rcu_string *name;
2544
2545 rcu_read_lock();
2546 name = rcu_dereference(dev->name);
2547 strncpy(di_args->path, name->str, sizeof(di_args->path));
2548 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002549 di_args->path[sizeof(di_args->path) - 1] = 0;
2550 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002551 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002552 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002553
2554out:
David Sterba55793c02013-04-26 15:20:23 +00002555 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002556 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2557 ret = -EFAULT;
2558
2559 kfree(di_args);
2560 return ret;
2561}
2562
Mark Fasheh416161d2013-08-06 11:42:51 -07002563static struct page *extent_same_get_page(struct inode *inode, u64 off)
2564{
2565 struct page *page;
2566 pgoff_t index;
2567 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2568
2569 index = off >> PAGE_CACHE_SHIFT;
2570
2571 page = grab_cache_page(inode->i_mapping, index);
2572 if (!page)
2573 return NULL;
2574
2575 if (!PageUptodate(page)) {
2576 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2577 0))
2578 return NULL;
2579 lock_page(page);
2580 if (!PageUptodate(page)) {
2581 unlock_page(page);
2582 page_cache_release(page);
2583 return NULL;
2584 }
2585 }
2586 unlock_page(page);
2587
2588 return page;
2589}
2590
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002591static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2592{
2593 /* do any pending delalloc/csum calc on src, one way or
2594 another, and lock file content */
2595 while (1) {
2596 struct btrfs_ordered_extent *ordered;
2597 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2598 ordered = btrfs_lookup_first_ordered_extent(inode,
2599 off + len - 1);
2600 if (!ordered &&
2601 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2602 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2603 break;
2604 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2605 if (ordered)
2606 btrfs_put_ordered_extent(ordered);
2607 btrfs_wait_ordered_range(inode, off, len);
2608 }
2609}
2610
Mark Fasheh416161d2013-08-06 11:42:51 -07002611static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2612 struct inode *inode2, u64 loff2, u64 len)
2613{
2614 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2615 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2616
2617 mutex_unlock(&inode1->i_mutex);
2618 mutex_unlock(&inode2->i_mutex);
2619}
2620
2621static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2622 struct inode *inode2, u64 loff2, u64 len)
2623{
2624 if (inode1 < inode2) {
2625 swap(inode1, inode2);
2626 swap(loff1, loff2);
2627 }
2628
2629 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2630 lock_extent_range(inode1, loff1, len);
2631 if (inode1 != inode2) {
2632 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2633 lock_extent_range(inode2, loff2, len);
2634 }
2635}
2636
2637static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2638 u64 dst_loff, u64 len)
2639{
2640 int ret = 0;
2641 struct page *src_page, *dst_page;
2642 unsigned int cmp_len = PAGE_CACHE_SIZE;
2643 void *addr, *dst_addr;
2644
2645 while (len) {
2646 if (len < PAGE_CACHE_SIZE)
2647 cmp_len = len;
2648
2649 src_page = extent_same_get_page(src, loff);
2650 if (!src_page)
2651 return -EINVAL;
2652 dst_page = extent_same_get_page(dst, dst_loff);
2653 if (!dst_page) {
2654 page_cache_release(src_page);
2655 return -EINVAL;
2656 }
2657 addr = kmap_atomic(src_page);
2658 dst_addr = kmap_atomic(dst_page);
2659
2660 flush_dcache_page(src_page);
2661 flush_dcache_page(dst_page);
2662
2663 if (memcmp(addr, dst_addr, cmp_len))
2664 ret = BTRFS_SAME_DATA_DIFFERS;
2665
2666 kunmap_atomic(addr);
2667 kunmap_atomic(dst_addr);
2668 page_cache_release(src_page);
2669 page_cache_release(dst_page);
2670
2671 if (ret)
2672 break;
2673
2674 loff += cmp_len;
2675 dst_loff += cmp_len;
2676 len -= cmp_len;
2677 }
2678
2679 return ret;
2680}
2681
2682static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2683{
2684 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2685
2686 if (off + len > inode->i_size || off + len < off)
2687 return -EINVAL;
2688 /* Check that we are block aligned - btrfs_clone() requires this */
2689 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2690 return -EINVAL;
2691
2692 return 0;
2693}
2694
2695static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2696 struct inode *dst, u64 dst_loff)
2697{
2698 int ret;
2699
2700 /*
2701 * btrfs_clone() can't handle extents in the same file
2702 * yet. Once that works, we can drop this check and replace it
2703 * with a check for the same inode, but overlapping extents.
2704 */
2705 if (src == dst)
2706 return -EINVAL;
2707
2708 btrfs_double_lock(src, loff, dst, dst_loff, len);
2709
2710 ret = extent_same_check_offsets(src, loff, len);
2711 if (ret)
2712 goto out_unlock;
2713
2714 ret = extent_same_check_offsets(dst, dst_loff, len);
2715 if (ret)
2716 goto out_unlock;
2717
2718 /* don't make the dst file partly checksummed */
2719 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2720 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2721 ret = -EINVAL;
2722 goto out_unlock;
2723 }
2724
2725 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2726 if (ret == 0)
2727 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2728
2729out_unlock:
2730 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2731
2732 return ret;
2733}
2734
2735#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2736
2737static long btrfs_ioctl_file_extent_same(struct file *file,
2738 void __user *argp)
2739{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002740 struct btrfs_ioctl_same_args tmp;
2741 struct btrfs_ioctl_same_args *same;
2742 struct btrfs_ioctl_same_extent_info *info;
Mark Fasheh416161d2013-08-06 11:42:51 -07002743 struct inode *src = file->f_dentry->d_inode;
2744 struct file *dst_file = NULL;
2745 struct inode *dst;
2746 u64 off;
2747 u64 len;
2748 int i;
2749 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002750 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002751 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2752 bool is_admin = capable(CAP_SYS_ADMIN);
2753
2754 if (!(file->f_mode & FMODE_READ))
2755 return -EINVAL;
2756
2757 ret = mnt_want_write_file(file);
2758 if (ret)
2759 return ret;
2760
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002761 if (copy_from_user(&tmp,
Mark Fasheh416161d2013-08-06 11:42:51 -07002762 (struct btrfs_ioctl_same_args __user *)argp,
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002763 sizeof(tmp))) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002764 ret = -EFAULT;
2765 goto out;
2766 }
2767
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002768 size = sizeof(tmp) +
2769 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2770
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002771 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002772
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002773 if (IS_ERR(same)) {
2774 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002775 goto out;
2776 }
2777
2778 off = same->logical_offset;
2779 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002780
2781 /*
2782 * Limit the total length we will dedupe for each operation.
2783 * This is intended to bound the total time spent in this
2784 * ioctl to something sane.
2785 */
2786 if (len > BTRFS_MAX_DEDUPE_LEN)
2787 len = BTRFS_MAX_DEDUPE_LEN;
2788
2789 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2790 /*
2791 * Btrfs does not support blocksize < page_size. As a
2792 * result, btrfs_cmp_data() won't correctly handle
2793 * this situation without an update.
2794 */
2795 ret = -EINVAL;
2796 goto out;
2797 }
2798
2799 ret = -EISDIR;
2800 if (S_ISDIR(src->i_mode))
2801 goto out;
2802
2803 ret = -EACCES;
2804 if (!S_ISREG(src->i_mode))
2805 goto out;
2806
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002807 /* pre-format output fields to sane values */
2808 for (i = 0; i < same->dest_count; i++) {
2809 same->info[i].bytes_deduped = 0ULL;
2810 same->info[i].status = 0;
2811 }
2812
Mark Fasheh416161d2013-08-06 11:42:51 -07002813 ret = 0;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002814 for (i = 0; i < same->dest_count; i++) {
2815 info = &same->info[i];
Mark Fasheh416161d2013-08-06 11:42:51 -07002816
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002817 dst_file = fget(info->fd);
Mark Fasheh416161d2013-08-06 11:42:51 -07002818 if (!dst_file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002819 info->status = -EBADF;
Mark Fasheh416161d2013-08-06 11:42:51 -07002820 goto next;
2821 }
2822
2823 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002824 info->status = -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07002825 goto next;
2826 }
2827
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002828 info->status = -EXDEV;
Mark Fasheh416161d2013-08-06 11:42:51 -07002829 if (file->f_path.mnt != dst_file->f_path.mnt)
2830 goto next;
2831
2832 dst = dst_file->f_dentry->d_inode;
2833 if (src->i_sb != dst->i_sb)
2834 goto next;
2835
2836 if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002837 info->status = -EISDIR;
Mark Fasheh416161d2013-08-06 11:42:51 -07002838 goto next;
2839 }
2840
2841 if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002842 info->status = -EACCES;
Mark Fasheh416161d2013-08-06 11:42:51 -07002843 goto next;
2844 }
2845
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002846 info->status = btrfs_extent_same(src, off, len, dst,
2847 info->logical_offset);
2848 if (info->status == 0)
2849 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07002850
2851next:
2852 if (dst_file)
2853 fput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002854 }
2855
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002856 ret = copy_to_user(argp, same, size);
2857 if (ret)
2858 ret = -EFAULT;
2859
Mark Fasheh416161d2013-08-06 11:42:51 -07002860out:
2861 mnt_drop_write_file(file);
2862 return ret;
2863}
2864
Mark Fasheh32b7c682013-08-06 11:42:49 -07002865/**
2866 * btrfs_clone() - clone a range from inode file to another
2867 *
2868 * @src: Inode to clone from
2869 * @inode: Inode to clone to
2870 * @off: Offset within source to start clone from
2871 * @olen: Original length, passed by user, of range to clone
2872 * @olen_aligned: Block-aligned value of olen, extent_same uses
2873 * identical values here
2874 * @destoff: Offset within @inode to start clone
2875 */
2876static int btrfs_clone(struct inode *src, struct inode *inode,
2877 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002878{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002879 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002880 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002881 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002882 struct btrfs_trans_handle *trans;
2883 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002884 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002885 u32 nritems;
2886 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002887 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002888 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002889
2890 ret = -ENOMEM;
2891 buf = vmalloc(btrfs_level_size(root, 0));
2892 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002893 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002894
2895 path = btrfs_alloc_path();
2896 if (!path) {
2897 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002898 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002899 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002900
Yan Zhengae01a0a2008-08-04 23:23:47 -04002901 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002902 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002903 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002904 key.type = BTRFS_EXTENT_DATA_KEY;
2905 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002906
2907 while (1) {
2908 /*
2909 * note the key will change type as we walk through the
2910 * tree.
2911 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00002912 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02002913 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2914 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002915 if (ret < 0)
2916 goto out;
2917
Yan Zhengae01a0a2008-08-04 23:23:47 -04002918 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00002919process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002920 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002921 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002922 if (ret < 0)
2923 goto out;
2924 if (ret > 0)
2925 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002926 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002927 }
2928 leaf = path->nodes[0];
2929 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002930
Yan Zhengae01a0a2008-08-04 23:23:47 -04002931 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002932 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002933 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002934 break;
2935
Sage Weilc5c9cd42008-11-12 14:32:25 -05002936 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2937 struct btrfs_file_extent_item *extent;
2938 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002939 u32 size;
2940 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002941 u64 disko = 0, diskl = 0;
2942 u64 datao = 0, datal = 0;
2943 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002944 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002945
Sage Weilc5c9cd42008-11-12 14:32:25 -05002946 extent = btrfs_item_ptr(leaf, slot,
2947 struct btrfs_file_extent_item);
2948 comp = btrfs_file_extent_compression(leaf, extent);
2949 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002950 if (type == BTRFS_FILE_EXTENT_REG ||
2951 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002952 disko = btrfs_file_extent_disk_bytenr(leaf,
2953 extent);
2954 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2955 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002956 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002957 datal = btrfs_file_extent_num_bytes(leaf,
2958 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002959 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2960 /* take upper bound, may be compressed */
2961 datal = btrfs_file_extent_ram_bytes(leaf,
2962 extent);
2963 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002964
Sage Weil050006a2010-10-29 15:37:33 -04002965 if (key.offset + datal <= off ||
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00002966 key.offset >= off + len - 1) {
2967 path->slots[0]++;
2968 goto process_slot;
2969 }
2970
2971 size = btrfs_item_size_nr(leaf, slot);
2972 read_extent_buffer(leaf, buf,
2973 btrfs_item_ptr_offset(leaf, slot),
2974 size);
2975
2976 btrfs_release_path(path);
2977 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002978
Zheng Yan31840ae2008-09-23 13:14:14 -04002979 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002980 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002981 if (off <= key.offset)
2982 new_key.offset = key.offset + destoff - off;
2983 else
2984 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002985
Sage Weilb6f34092011-09-20 14:48:51 -04002986 /*
2987 * 1 - adjusting old extent (we may have to split it)
2988 * 1 - add new extent
2989 * 1 - inode update
2990 */
2991 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002992 if (IS_ERR(trans)) {
2993 ret = PTR_ERR(trans);
2994 goto out;
2995 }
2996
Chris Masonc8a894d2009-06-27 21:07:03 -04002997 if (type == BTRFS_FILE_EXTENT_REG ||
2998 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002999 /*
3000 * a | --- range to clone ---| b
3001 * | ------------- extent ------------- |
3002 */
3003
3004 /* substract range b */
3005 if (key.offset + datal > off + len)
3006 datal = off + len - key.offset;
3007
3008 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003009 if (off > key.offset) {
3010 datao += off - key.offset;
3011 datal -= off - key.offset;
3012 }
3013
Josef Bacik5dc562c2012-08-17 13:14:17 -04003014 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003015 new_key.offset,
3016 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003017 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret) {
3019 btrfs_abort_transaction(trans, root,
3020 ret);
3021 btrfs_end_transaction(trans, root);
3022 goto out;
3023 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003024
Sage Weilc5c9cd42008-11-12 14:32:25 -05003025 ret = btrfs_insert_empty_item(trans, root, path,
3026 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 if (ret) {
3028 btrfs_abort_transaction(trans, root,
3029 ret);
3030 btrfs_end_transaction(trans, root);
3031 goto out;
3032 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003033
3034 leaf = path->nodes[0];
3035 slot = path->slots[0];
3036 write_extent_buffer(leaf, buf,
3037 btrfs_item_ptr_offset(leaf, slot),
3038 size);
3039
3040 extent = btrfs_item_ptr(leaf, slot,
3041 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003042
Sage Weilc5c9cd42008-11-12 14:32:25 -05003043 /* disko == 0 means it's a hole */
3044 if (!disko)
3045 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003046
3047 btrfs_set_file_extent_offset(leaf, extent,
3048 datao);
3049 btrfs_set_file_extent_num_bytes(leaf, extent,
3050 datal);
3051 if (disko) {
3052 inode_add_bytes(inode, datal);
3053 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003054 disko, diskl, 0,
3055 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003056 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003057 new_key.offset - datao,
3058 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059 if (ret) {
3060 btrfs_abort_transaction(trans,
3061 root,
3062 ret);
3063 btrfs_end_transaction(trans,
3064 root);
3065 goto out;
3066
3067 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003068 }
3069 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3070 u64 skip = 0;
3071 u64 trim = 0;
3072 if (off > key.offset) {
3073 skip = off - key.offset;
3074 new_key.offset += skip;
3075 }
Chris Masond3977122009-01-05 21:25:51 -05003076
Liu Boaa42ffd2012-09-18 03:52:23 -06003077 if (key.offset + datal > off + len)
3078 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003079
Sage Weilc5c9cd42008-11-12 14:32:25 -05003080 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003081 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003082 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003083 goto out;
3084 }
3085 size -= skip + trim;
3086 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087
Josef Bacik5dc562c2012-08-17 13:14:17 -04003088 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003089 new_key.offset,
3090 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003091 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003092 if (ret) {
3093 btrfs_abort_transaction(trans, root,
3094 ret);
3095 btrfs_end_transaction(trans, root);
3096 goto out;
3097 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003098
Sage Weilc5c9cd42008-11-12 14:32:25 -05003099 ret = btrfs_insert_empty_item(trans, root, path,
3100 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003101 if (ret) {
3102 btrfs_abort_transaction(trans, root,
3103 ret);
3104 btrfs_end_transaction(trans, root);
3105 goto out;
3106 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003107
3108 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003109 u32 start =
3110 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003111 memmove(buf+start, buf+start+skip,
3112 datal);
3113 }
3114
3115 leaf = path->nodes[0];
3116 slot = path->slots[0];
3117 write_extent_buffer(leaf, buf,
3118 btrfs_item_ptr_offset(leaf, slot),
3119 size);
3120 inode_add_bytes(inode, datal);
3121 }
3122
3123 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003124 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003125
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003126 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003127 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003128
3129 /*
3130 * we round up to the block size at eof when
3131 * determining which extents to clone above,
3132 * but shouldn't round up the file size
3133 */
3134 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003135 if (endoff > destoff+olen)
3136 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003137 if (endoff > inode->i_size)
3138 btrfs_i_size_write(inode, endoff);
3139
Yan, Zhenga22285a2010-05-16 10:48:46 -04003140 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003141 if (ret) {
3142 btrfs_abort_transaction(trans, root, ret);
3143 btrfs_end_transaction(trans, root);
3144 goto out;
3145 }
3146 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003147 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003148 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003149 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003150 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003151 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003152
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003153out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003154 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003155 btrfs_free_path(path);
3156 vfree(buf);
3157 return ret;
3158}
3159
3160static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3161 u64 off, u64 olen, u64 destoff)
3162{
Al Viro54563d42013-09-01 15:57:51 -04003163 struct inode *inode = file_inode(file);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003164 struct btrfs_root *root = BTRFS_I(inode)->root;
3165 struct fd src_file;
3166 struct inode *src;
3167 int ret;
3168 u64 len = olen;
3169 u64 bs = root->fs_info->sb->s_blocksize;
3170 int same_inode = 0;
3171
3172 /*
3173 * TODO:
3174 * - split compressed inline extents. annoying: we need to
3175 * decompress into destination's address_space (the file offset
3176 * may change, so source mapping won't do), then recompress (or
3177 * otherwise reinsert) a subrange.
3178 * - allow ranges within the same file to be cloned (provided
3179 * they don't overlap)?
3180 */
3181
3182 /* the destination must be opened for writing */
3183 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3184 return -EINVAL;
3185
3186 if (btrfs_root_readonly(root))
3187 return -EROFS;
3188
3189 ret = mnt_want_write_file(file);
3190 if (ret)
3191 return ret;
3192
3193 src_file = fdget(srcfd);
3194 if (!src_file.file) {
3195 ret = -EBADF;
3196 goto out_drop_write;
3197 }
3198
3199 ret = -EXDEV;
3200 if (src_file.file->f_path.mnt != file->f_path.mnt)
3201 goto out_fput;
3202
3203 src = file_inode(src_file.file);
3204
3205 ret = -EINVAL;
3206 if (src == inode)
3207 same_inode = 1;
3208
3209 /* the src must be open for reading */
3210 if (!(src_file.file->f_mode & FMODE_READ))
3211 goto out_fput;
3212
3213 /* don't make the dst file partly checksummed */
3214 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3215 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3216 goto out_fput;
3217
3218 ret = -EISDIR;
3219 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3220 goto out_fput;
3221
3222 ret = -EXDEV;
3223 if (src->i_sb != inode->i_sb)
3224 goto out_fput;
3225
3226 if (!same_inode) {
3227 if (inode < src) {
3228 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3229 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3230 } else {
3231 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3232 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3233 }
3234 } else {
3235 mutex_lock(&src->i_mutex);
3236 }
3237
3238 /* determine range to clone */
3239 ret = -EINVAL;
3240 if (off + len > src->i_size || off + len < off)
3241 goto out_unlock;
3242 if (len == 0)
3243 olen = len = src->i_size - off;
3244 /* if we extend to eof, continue to block boundary */
3245 if (off + len == src->i_size)
3246 len = ALIGN(src->i_size, bs) - off;
3247
3248 /* verify the end result is block aligned */
3249 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3250 !IS_ALIGNED(destoff, bs))
3251 goto out_unlock;
3252
3253 /* verify if ranges are overlapped within the same file */
3254 if (same_inode) {
3255 if (destoff + len > off && destoff < off + len)
3256 goto out_unlock;
3257 }
3258
3259 if (destoff > inode->i_size) {
3260 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3261 if (ret)
3262 goto out_unlock;
3263 }
3264
3265 /* truncate page cache pages from target inode range */
3266 truncate_inode_pages_range(&inode->i_data, destoff,
3267 PAGE_CACHE_ALIGN(destoff + len) - 1);
3268
3269 lock_extent_range(src, off, len);
3270
3271 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3272
Liu Boaa42ffd2012-09-18 03:52:23 -06003273 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003274out_unlock:
Filipe David Borba Mananac57c2b32014-01-11 21:31:25 +00003275 if (!same_inode) {
3276 if (inode < src) {
3277 mutex_unlock(&src->i_mutex);
3278 mutex_unlock(&inode->i_mutex);
3279 } else {
3280 mutex_unlock(&inode->i_mutex);
3281 mutex_unlock(&src->i_mutex);
3282 }
3283 } else {
3284 mutex_unlock(&src->i_mutex);
3285 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003286out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003287 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003288out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003289 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003290 return ret;
3291}
3292
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003293static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003294{
3295 struct btrfs_ioctl_clone_range_args args;
3296
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003297 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003298 return -EFAULT;
3299 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3300 args.src_length, args.dest_offset);
3301}
3302
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003303/*
3304 * there are many ways the trans_start and trans_end ioctls can lead
3305 * to deadlocks. They should only be used by applications that
3306 * basically own the machine, and have a very in depth understanding
3307 * of all the possible deadlocks and enospc problems.
3308 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003309static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003310{
Al Viro496ad9a2013-01-23 17:07:38 -05003311 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003312 struct btrfs_root *root = BTRFS_I(inode)->root;
3313 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003314 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003315
Sage Weil1ab86ae2009-09-29 18:38:44 -04003316 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003317 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003318 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003319
3320 ret = -EINPROGRESS;
3321 if (file->private_data)
3322 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003323
Li Zefanb83cc962010-12-20 16:04:08 +08003324 ret = -EROFS;
3325 if (btrfs_root_readonly(root))
3326 goto out;
3327
Al Viroa561be72011-11-23 11:57:51 -05003328 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003329 if (ret)
3330 goto out;
3331
Josef Bacika4abeea2011-04-11 17:25:13 -04003332 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003333
Sage Weil1ab86ae2009-09-29 18:38:44 -04003334 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003335 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003336 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003337 goto out_drop;
3338
3339 file->private_data = trans;
3340 return 0;
3341
3342out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003343 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003344 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003345out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003346 return ret;
3347}
3348
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003349static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3350{
Al Viro496ad9a2013-01-23 17:07:38 -05003351 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003352 struct btrfs_root *root = BTRFS_I(inode)->root;
3353 struct btrfs_root *new_root;
3354 struct btrfs_dir_item *di;
3355 struct btrfs_trans_handle *trans;
3356 struct btrfs_path *path;
3357 struct btrfs_key location;
3358 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003359 u64 objectid = 0;
3360 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003361 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003362
3363 if (!capable(CAP_SYS_ADMIN))
3364 return -EPERM;
3365
Miao Xie3c04ce02012-11-26 08:43:07 +00003366 ret = mnt_want_write_file(file);
3367 if (ret)
3368 return ret;
3369
3370 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3371 ret = -EFAULT;
3372 goto out;
3373 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003374
3375 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303376 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003377
3378 location.objectid = objectid;
3379 location.type = BTRFS_ROOT_ITEM_KEY;
3380 location.offset = (u64)-1;
3381
3382 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003383 if (IS_ERR(new_root)) {
3384 ret = PTR_ERR(new_root);
3385 goto out;
3386 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003387
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003388 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003389 if (!path) {
3390 ret = -ENOMEM;
3391 goto out;
3392 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003393 path->leave_spinning = 1;
3394
3395 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003396 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003397 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003398 ret = PTR_ERR(trans);
3399 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003400 }
3401
David Sterba6c417612011-04-13 15:41:04 +02003402 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003403 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3404 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003405 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003406 btrfs_free_path(path);
3407 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05003408 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3409 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003410 ret = -ENOENT;
3411 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003412 }
3413
3414 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3415 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3416 btrfs_mark_buffer_dirty(path->nodes[0]);
3417 btrfs_free_path(path);
3418
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003419 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003420 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003421out:
3422 mnt_drop_write_file(file);
3423 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003424}
3425
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003426void btrfs_get_block_group_info(struct list_head *groups_list,
3427 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003428{
3429 struct btrfs_block_group_cache *block_group;
3430
3431 space->total_bytes = 0;
3432 space->used_bytes = 0;
3433 space->flags = 0;
3434 list_for_each_entry(block_group, groups_list, list) {
3435 space->flags = block_group->flags;
3436 space->total_bytes += block_group->key.offset;
3437 space->used_bytes +=
3438 btrfs_block_group_used(&block_group->item);
3439 }
3440}
3441
Eric Sandeen48a3b632013-04-25 20:41:01 +00003442static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003443{
3444 struct btrfs_ioctl_space_args space_args;
3445 struct btrfs_ioctl_space_info space;
3446 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003447 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003448 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003449 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003450 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3451 BTRFS_BLOCK_GROUP_SYSTEM,
3452 BTRFS_BLOCK_GROUP_METADATA,
3453 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3454 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003455 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003456 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003457 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003458 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003459
3460 if (copy_from_user(&space_args,
3461 (struct btrfs_ioctl_space_args __user *)arg,
3462 sizeof(space_args)))
3463 return -EFAULT;
3464
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003465 for (i = 0; i < num_types; i++) {
3466 struct btrfs_space_info *tmp;
3467
3468 info = NULL;
3469 rcu_read_lock();
3470 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3471 list) {
3472 if (tmp->flags == types[i]) {
3473 info = tmp;
3474 break;
3475 }
3476 }
3477 rcu_read_unlock();
3478
3479 if (!info)
3480 continue;
3481
3482 down_read(&info->groups_sem);
3483 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3484 if (!list_empty(&info->block_groups[c]))
3485 slot_count++;
3486 }
3487 up_read(&info->groups_sem);
3488 }
Josef Bacik1406e432010-01-13 18:19:06 +00003489
Chris Mason7fde62b2010-03-16 15:40:10 -04003490 /* space_slots == 0 means they are asking for a count */
3491 if (space_args.space_slots == 0) {
3492 space_args.total_spaces = slot_count;
3493 goto out;
3494 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003495
Dan Rosenberg51788b12011-02-14 16:04:23 -05003496 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003497
Chris Mason7fde62b2010-03-16 15:40:10 -04003498 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003499
Chris Mason7fde62b2010-03-16 15:40:10 -04003500 /* we generally have at most 6 or so space infos, one for each raid
3501 * level. So, a whole page should be more than enough for everyone
3502 */
3503 if (alloc_size > PAGE_CACHE_SIZE)
3504 return -ENOMEM;
3505
3506 space_args.total_spaces = 0;
3507 dest = kmalloc(alloc_size, GFP_NOFS);
3508 if (!dest)
3509 return -ENOMEM;
3510 dest_orig = dest;
3511
3512 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003513 for (i = 0; i < num_types; i++) {
3514 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003515
Dan Rosenberg51788b12011-02-14 16:04:23 -05003516 if (!slot_count)
3517 break;
3518
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003519 info = NULL;
3520 rcu_read_lock();
3521 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3522 list) {
3523 if (tmp->flags == types[i]) {
3524 info = tmp;
3525 break;
3526 }
3527 }
3528 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003529
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003530 if (!info)
3531 continue;
3532 down_read(&info->groups_sem);
3533 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3534 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003535 btrfs_get_block_group_info(
3536 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003537 memcpy(dest, &space, sizeof(space));
3538 dest++;
3539 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003540 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003541 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003542 if (!slot_count)
3543 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003544 }
3545 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003546 }
Josef Bacik1406e432010-01-13 18:19:06 +00003547
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003548 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003549 (arg + sizeof(struct btrfs_ioctl_space_args));
3550
3551 if (copy_to_user(user_dest, dest_orig, alloc_size))
3552 ret = -EFAULT;
3553
3554 kfree(dest_orig);
3555out:
3556 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003557 ret = -EFAULT;
3558
3559 return ret;
3560}
3561
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003562/*
3563 * there are many ways the trans_start and trans_end ioctls can lead
3564 * to deadlocks. They should only be used by applications that
3565 * basically own the machine, and have a very in depth understanding
3566 * of all the possible deadlocks and enospc problems.
3567 */
3568long btrfs_ioctl_trans_end(struct file *file)
3569{
Al Viro496ad9a2013-01-23 17:07:38 -05003570 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003571 struct btrfs_root *root = BTRFS_I(inode)->root;
3572 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003573
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003574 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003575 if (!trans)
3576 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003577 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003578
Sage Weil1ab86ae2009-09-29 18:38:44 -04003579 btrfs_end_transaction(trans, root);
3580
Josef Bacika4abeea2011-04-11 17:25:13 -04003581 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003582
Al Viro2a79f172011-12-09 08:06:57 -05003583 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003584 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003585}
3586
Miao Xie9a8c28b2012-11-26 08:40:43 +00003587static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3588 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003589{
Sage Weil46204592010-10-29 15:41:32 -04003590 struct btrfs_trans_handle *trans;
3591 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003592 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003593
Miao Xied4edf392013-02-20 09:17:06 +00003594 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003595 if (IS_ERR(trans)) {
3596 if (PTR_ERR(trans) != -ENOENT)
3597 return PTR_ERR(trans);
3598
3599 /* No running transaction, don't bother */
3600 transid = root->fs_info->last_trans_committed;
3601 goto out;
3602 }
Sage Weil46204592010-10-29 15:41:32 -04003603 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003604 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003605 if (ret) {
3606 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003607 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003608 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003609out:
Sage Weil46204592010-10-29 15:41:32 -04003610 if (argp)
3611 if (copy_to_user(argp, &transid, sizeof(transid)))
3612 return -EFAULT;
3613 return 0;
3614}
3615
Miao Xie9a8c28b2012-11-26 08:40:43 +00003616static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3617 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003618{
Sage Weil46204592010-10-29 15:41:32 -04003619 u64 transid;
3620
3621 if (argp) {
3622 if (copy_from_user(&transid, argp, sizeof(transid)))
3623 return -EFAULT;
3624 } else {
3625 transid = 0; /* current trans */
3626 }
3627 return btrfs_wait_for_commit(root, transid);
3628}
3629
Miao Xieb8e95482012-11-26 08:48:01 +00003630static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003631{
Al Viro496ad9a2013-01-23 17:07:38 -05003632 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003633 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003634 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003635
3636 if (!capable(CAP_SYS_ADMIN))
3637 return -EPERM;
3638
3639 sa = memdup_user(arg, sizeof(*sa));
3640 if (IS_ERR(sa))
3641 return PTR_ERR(sa);
3642
Miao Xieb8e95482012-11-26 08:48:01 +00003643 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3644 ret = mnt_want_write_file(file);
3645 if (ret)
3646 goto out;
3647 }
3648
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003649 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003650 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3651 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003652
3653 if (copy_to_user(arg, sa, sizeof(*sa)))
3654 ret = -EFAULT;
3655
Miao Xieb8e95482012-11-26 08:48:01 +00003656 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3657 mnt_drop_write_file(file);
3658out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003659 kfree(sa);
3660 return ret;
3661}
3662
3663static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3664{
3665 if (!capable(CAP_SYS_ADMIN))
3666 return -EPERM;
3667
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003668 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003669}
3670
3671static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3672 void __user *arg)
3673{
3674 struct btrfs_ioctl_scrub_args *sa;
3675 int ret;
3676
3677 if (!capable(CAP_SYS_ADMIN))
3678 return -EPERM;
3679
3680 sa = memdup_user(arg, sizeof(*sa));
3681 if (IS_ERR(sa))
3682 return PTR_ERR(sa);
3683
3684 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3685
3686 if (copy_to_user(arg, sa, sizeof(*sa)))
3687 ret = -EFAULT;
3688
3689 kfree(sa);
3690 return ret;
3691}
3692
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003693static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003694 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003695{
3696 struct btrfs_ioctl_get_dev_stats *sa;
3697 int ret;
3698
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003699 sa = memdup_user(arg, sizeof(*sa));
3700 if (IS_ERR(sa))
3701 return PTR_ERR(sa);
3702
David Sterbab27f7c02012-06-22 06:30:39 -06003703 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3704 kfree(sa);
3705 return -EPERM;
3706 }
3707
3708 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003709
3710 if (copy_to_user(arg, sa, sizeof(*sa)))
3711 ret = -EFAULT;
3712
3713 kfree(sa);
3714 return ret;
3715}
3716
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003717static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3718{
3719 struct btrfs_ioctl_dev_replace_args *p;
3720 int ret;
3721
3722 if (!capable(CAP_SYS_ADMIN))
3723 return -EPERM;
3724
3725 p = memdup_user(arg, sizeof(*p));
3726 if (IS_ERR(p))
3727 return PTR_ERR(p);
3728
3729 switch (p->cmd) {
3730 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003731 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3732 ret = -EROFS;
3733 goto out;
3734 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003735 if (atomic_xchg(
3736 &root->fs_info->mutually_exclusive_operation_running,
3737 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003738 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003739 } else {
3740 ret = btrfs_dev_replace_start(root, p);
3741 atomic_set(
3742 &root->fs_info->mutually_exclusive_operation_running,
3743 0);
3744 }
3745 break;
3746 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3747 btrfs_dev_replace_status(root->fs_info, p);
3748 ret = 0;
3749 break;
3750 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3751 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3752 break;
3753 default:
3754 ret = -EINVAL;
3755 break;
3756 }
3757
3758 if (copy_to_user(arg, p, sizeof(*p)))
3759 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003760out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003761 kfree(p);
3762 return ret;
3763}
3764
Jan Schmidtd7728c92011-07-07 16:48:38 +02003765static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3766{
3767 int ret = 0;
3768 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003769 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003770 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003771 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003772 struct inode_fs_paths *ipath = NULL;
3773 struct btrfs_path *path;
3774
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003775 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003776 return -EPERM;
3777
3778 path = btrfs_alloc_path();
3779 if (!path) {
3780 ret = -ENOMEM;
3781 goto out;
3782 }
3783
3784 ipa = memdup_user(arg, sizeof(*ipa));
3785 if (IS_ERR(ipa)) {
3786 ret = PTR_ERR(ipa);
3787 ipa = NULL;
3788 goto out;
3789 }
3790
3791 size = min_t(u32, ipa->size, 4096);
3792 ipath = init_ipath(size, root, path);
3793 if (IS_ERR(ipath)) {
3794 ret = PTR_ERR(ipath);
3795 ipath = NULL;
3796 goto out;
3797 }
3798
3799 ret = paths_from_inode(ipa->inum, ipath);
3800 if (ret < 0)
3801 goto out;
3802
3803 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003804 rel_ptr = ipath->fspath->val[i] -
3805 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003806 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003807 }
3808
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003809 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3810 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003811 if (ret) {
3812 ret = -EFAULT;
3813 goto out;
3814 }
3815
3816out:
3817 btrfs_free_path(path);
3818 free_ipath(ipath);
3819 kfree(ipa);
3820
3821 return ret;
3822}
3823
3824static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3825{
3826 struct btrfs_data_container *inodes = ctx;
3827 const size_t c = 3 * sizeof(u64);
3828
3829 if (inodes->bytes_left >= c) {
3830 inodes->bytes_left -= c;
3831 inodes->val[inodes->elem_cnt] = inum;
3832 inodes->val[inodes->elem_cnt + 1] = offset;
3833 inodes->val[inodes->elem_cnt + 2] = root;
3834 inodes->elem_cnt += 3;
3835 } else {
3836 inodes->bytes_missing += c - inodes->bytes_left;
3837 inodes->bytes_left = 0;
3838 inodes->elem_missed += 3;
3839 }
3840
3841 return 0;
3842}
3843
3844static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3845 void __user *arg)
3846{
3847 int ret = 0;
3848 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003849 struct btrfs_ioctl_logical_ino_args *loi;
3850 struct btrfs_data_container *inodes = NULL;
3851 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003852
3853 if (!capable(CAP_SYS_ADMIN))
3854 return -EPERM;
3855
3856 loi = memdup_user(arg, sizeof(*loi));
3857 if (IS_ERR(loi)) {
3858 ret = PTR_ERR(loi);
3859 loi = NULL;
3860 goto out;
3861 }
3862
3863 path = btrfs_alloc_path();
3864 if (!path) {
3865 ret = -ENOMEM;
3866 goto out;
3867 }
3868
Liu Bo425d17a2012-09-07 20:01:30 -06003869 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003870 inodes = init_data_container(size);
3871 if (IS_ERR(inodes)) {
3872 ret = PTR_ERR(inodes);
3873 inodes = NULL;
3874 goto out;
3875 }
3876
Liu Bodf031f02012-09-07 20:01:29 -06003877 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3878 build_ino_list, inodes);
3879 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003880 ret = -ENOENT;
3881 if (ret < 0)
3882 goto out;
3883
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003884 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3885 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003886 if (ret)
3887 ret = -EFAULT;
3888
3889out:
3890 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003891 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003892 kfree(loi);
3893
3894 return ret;
3895}
3896
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003897void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003898 struct btrfs_ioctl_balance_args *bargs)
3899{
3900 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3901
3902 bargs->flags = bctl->flags;
3903
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003904 if (atomic_read(&fs_info->balance_running))
3905 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3906 if (atomic_read(&fs_info->balance_pause_req))
3907 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003908 if (atomic_read(&fs_info->balance_cancel_req))
3909 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003910
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003911 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3912 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3913 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003914
3915 if (lock) {
3916 spin_lock(&fs_info->balance_lock);
3917 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3918 spin_unlock(&fs_info->balance_lock);
3919 } else {
3920 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3921 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003922}
3923
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003924static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003925{
Al Viro496ad9a2013-01-23 17:07:38 -05003926 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003927 struct btrfs_fs_info *fs_info = root->fs_info;
3928 struct btrfs_ioctl_balance_args *bargs;
3929 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003930 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003931 int ret;
3932
3933 if (!capable(CAP_SYS_ADMIN))
3934 return -EPERM;
3935
Liu Boe54bfa312012-06-29 03:58:48 -06003936 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003937 if (ret)
3938 return ret;
3939
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003940again:
3941 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3942 mutex_lock(&fs_info->volume_mutex);
3943 mutex_lock(&fs_info->balance_mutex);
3944 need_unlock = true;
3945 goto locked;
3946 }
3947
3948 /*
3949 * mut. excl. ops lock is locked. Three possibilites:
3950 * (1) some other op is running
3951 * (2) balance is running
3952 * (3) balance is paused -- special case (think resume)
3953 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003954 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003955 if (fs_info->balance_ctl) {
3956 /* this is either (2) or (3) */
3957 if (!atomic_read(&fs_info->balance_running)) {
3958 mutex_unlock(&fs_info->balance_mutex);
3959 if (!mutex_trylock(&fs_info->volume_mutex))
3960 goto again;
3961 mutex_lock(&fs_info->balance_mutex);
3962
3963 if (fs_info->balance_ctl &&
3964 !atomic_read(&fs_info->balance_running)) {
3965 /* this is (3) */
3966 need_unlock = false;
3967 goto locked;
3968 }
3969
3970 mutex_unlock(&fs_info->balance_mutex);
3971 mutex_unlock(&fs_info->volume_mutex);
3972 goto again;
3973 } else {
3974 /* this is (2) */
3975 mutex_unlock(&fs_info->balance_mutex);
3976 ret = -EINPROGRESS;
3977 goto out;
3978 }
3979 } else {
3980 /* this is (1) */
3981 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003982 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003983 goto out;
3984 }
3985
3986locked:
3987 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988
3989 if (arg) {
3990 bargs = memdup_user(arg, sizeof(*bargs));
3991 if (IS_ERR(bargs)) {
3992 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003993 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003994 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003995
3996 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3997 if (!fs_info->balance_ctl) {
3998 ret = -ENOTCONN;
3999 goto out_bargs;
4000 }
4001
4002 bctl = fs_info->balance_ctl;
4003 spin_lock(&fs_info->balance_lock);
4004 bctl->flags |= BTRFS_BALANCE_RESUME;
4005 spin_unlock(&fs_info->balance_lock);
4006
4007 goto do_balance;
4008 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004009 } else {
4010 bargs = NULL;
4011 }
4012
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004013 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004014 ret = -EINPROGRESS;
4015 goto out_bargs;
4016 }
4017
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004018 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4019 if (!bctl) {
4020 ret = -ENOMEM;
4021 goto out_bargs;
4022 }
4023
4024 bctl->fs_info = fs_info;
4025 if (arg) {
4026 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4027 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4028 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4029
4030 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004031 } else {
4032 /* balance everything - no filters */
4033 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004034 }
4035
Ilya Dryomovde322262012-01-16 22:04:49 +02004036do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004037 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004038 * Ownership of bctl and mutually_exclusive_operation_running
4039 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4040 * or, if restriper was paused all the way until unmount, in
4041 * free_fs_info. mutually_exclusive_operation_running is
4042 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004043 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004044 need_unlock = false;
4045
4046 ret = btrfs_balance(bctl, bargs);
4047
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004048 if (arg) {
4049 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4050 ret = -EFAULT;
4051 }
4052
4053out_bargs:
4054 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004055out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004056 mutex_unlock(&fs_info->balance_mutex);
4057 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004058 if (need_unlock)
4059 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4060out:
Liu Boe54bfa312012-06-29 03:58:48 -06004061 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004062 return ret;
4063}
4064
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004065static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4066{
4067 if (!capable(CAP_SYS_ADMIN))
4068 return -EPERM;
4069
4070 switch (cmd) {
4071 case BTRFS_BALANCE_CTL_PAUSE:
4072 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004073 case BTRFS_BALANCE_CTL_CANCEL:
4074 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004075 }
4076
4077 return -EINVAL;
4078}
4079
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004080static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4081 void __user *arg)
4082{
4083 struct btrfs_fs_info *fs_info = root->fs_info;
4084 struct btrfs_ioctl_balance_args *bargs;
4085 int ret = 0;
4086
4087 if (!capable(CAP_SYS_ADMIN))
4088 return -EPERM;
4089
4090 mutex_lock(&fs_info->balance_mutex);
4091 if (!fs_info->balance_ctl) {
4092 ret = -ENOTCONN;
4093 goto out;
4094 }
4095
4096 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4097 if (!bargs) {
4098 ret = -ENOMEM;
4099 goto out;
4100 }
4101
4102 update_ioctl_balance_args(fs_info, 1, bargs);
4103
4104 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4105 ret = -EFAULT;
4106
4107 kfree(bargs);
4108out:
4109 mutex_unlock(&fs_info->balance_mutex);
4110 return ret;
4111}
4112
Miao Xie905b0dd2012-11-26 08:50:11 +00004113static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004114{
Al Viro496ad9a2013-01-23 17:07:38 -05004115 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004116 struct btrfs_ioctl_quota_ctl_args *sa;
4117 struct btrfs_trans_handle *trans = NULL;
4118 int ret;
4119 int err;
4120
4121 if (!capable(CAP_SYS_ADMIN))
4122 return -EPERM;
4123
Miao Xie905b0dd2012-11-26 08:50:11 +00004124 ret = mnt_want_write_file(file);
4125 if (ret)
4126 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004127
4128 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004129 if (IS_ERR(sa)) {
4130 ret = PTR_ERR(sa);
4131 goto drop_write;
4132 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004133
Wang Shilong7708f022013-04-07 10:24:57 +00004134 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004135 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4136 if (IS_ERR(trans)) {
4137 ret = PTR_ERR(trans);
4138 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004139 }
4140
4141 switch (sa->cmd) {
4142 case BTRFS_QUOTA_CTL_ENABLE:
4143 ret = btrfs_quota_enable(trans, root->fs_info);
4144 break;
4145 case BTRFS_QUOTA_CTL_DISABLE:
4146 ret = btrfs_quota_disable(trans, root->fs_info);
4147 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004148 default:
4149 ret = -EINVAL;
4150 break;
4151 }
4152
Jan Schmidt2f232032013-04-25 16:04:51 +00004153 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4154 if (err && !ret)
4155 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004156out:
4157 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004158 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004159drop_write:
4160 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004161 return ret;
4162}
4163
Miao Xie905b0dd2012-11-26 08:50:11 +00004164static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004165{
Al Viro496ad9a2013-01-23 17:07:38 -05004166 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004167 struct btrfs_ioctl_qgroup_assign_args *sa;
4168 struct btrfs_trans_handle *trans;
4169 int ret;
4170 int err;
4171
4172 if (!capable(CAP_SYS_ADMIN))
4173 return -EPERM;
4174
Miao Xie905b0dd2012-11-26 08:50:11 +00004175 ret = mnt_want_write_file(file);
4176 if (ret)
4177 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004178
4179 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004180 if (IS_ERR(sa)) {
4181 ret = PTR_ERR(sa);
4182 goto drop_write;
4183 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004184
4185 trans = btrfs_join_transaction(root);
4186 if (IS_ERR(trans)) {
4187 ret = PTR_ERR(trans);
4188 goto out;
4189 }
4190
4191 /* FIXME: check if the IDs really exist */
4192 if (sa->assign) {
4193 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4194 sa->src, sa->dst);
4195 } else {
4196 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4197 sa->src, sa->dst);
4198 }
4199
4200 err = btrfs_end_transaction(trans, root);
4201 if (err && !ret)
4202 ret = err;
4203
4204out:
4205 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004206drop_write:
4207 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004208 return ret;
4209}
4210
Miao Xie905b0dd2012-11-26 08:50:11 +00004211static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004212{
Al Viro496ad9a2013-01-23 17:07:38 -05004213 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004214 struct btrfs_ioctl_qgroup_create_args *sa;
4215 struct btrfs_trans_handle *trans;
4216 int ret;
4217 int err;
4218
4219 if (!capable(CAP_SYS_ADMIN))
4220 return -EPERM;
4221
Miao Xie905b0dd2012-11-26 08:50:11 +00004222 ret = mnt_want_write_file(file);
4223 if (ret)
4224 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004225
4226 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004227 if (IS_ERR(sa)) {
4228 ret = PTR_ERR(sa);
4229 goto drop_write;
4230 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004231
Miao Xied86e56c2012-11-15 11:35:41 +00004232 if (!sa->qgroupid) {
4233 ret = -EINVAL;
4234 goto out;
4235 }
4236
Arne Jansen5d13a372011-09-14 15:53:51 +02004237 trans = btrfs_join_transaction(root);
4238 if (IS_ERR(trans)) {
4239 ret = PTR_ERR(trans);
4240 goto out;
4241 }
4242
4243 /* FIXME: check if the IDs really exist */
4244 if (sa->create) {
4245 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4246 NULL);
4247 } else {
4248 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4249 }
4250
4251 err = btrfs_end_transaction(trans, root);
4252 if (err && !ret)
4253 ret = err;
4254
4255out:
4256 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004257drop_write:
4258 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004259 return ret;
4260}
4261
Miao Xie905b0dd2012-11-26 08:50:11 +00004262static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004263{
Al Viro496ad9a2013-01-23 17:07:38 -05004264 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004265 struct btrfs_ioctl_qgroup_limit_args *sa;
4266 struct btrfs_trans_handle *trans;
4267 int ret;
4268 int err;
4269 u64 qgroupid;
4270
4271 if (!capable(CAP_SYS_ADMIN))
4272 return -EPERM;
4273
Miao Xie905b0dd2012-11-26 08:50:11 +00004274 ret = mnt_want_write_file(file);
4275 if (ret)
4276 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004277
4278 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004279 if (IS_ERR(sa)) {
4280 ret = PTR_ERR(sa);
4281 goto drop_write;
4282 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004283
4284 trans = btrfs_join_transaction(root);
4285 if (IS_ERR(trans)) {
4286 ret = PTR_ERR(trans);
4287 goto out;
4288 }
4289
4290 qgroupid = sa->qgroupid;
4291 if (!qgroupid) {
4292 /* take the current subvol as qgroup */
4293 qgroupid = root->root_key.objectid;
4294 }
4295
4296 /* FIXME: check if the IDs really exist */
4297 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4298
4299 err = btrfs_end_transaction(trans, root);
4300 if (err && !ret)
4301 ret = err;
4302
4303out:
4304 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004305drop_write:
4306 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004307 return ret;
4308}
4309
Jan Schmidt2f232032013-04-25 16:04:51 +00004310static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4311{
Al Viro6d0379e2013-06-16 19:32:35 +04004312 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004313 struct btrfs_ioctl_quota_rescan_args *qsa;
4314 int ret;
4315
4316 if (!capable(CAP_SYS_ADMIN))
4317 return -EPERM;
4318
4319 ret = mnt_want_write_file(file);
4320 if (ret)
4321 return ret;
4322
4323 qsa = memdup_user(arg, sizeof(*qsa));
4324 if (IS_ERR(qsa)) {
4325 ret = PTR_ERR(qsa);
4326 goto drop_write;
4327 }
4328
4329 if (qsa->flags) {
4330 ret = -EINVAL;
4331 goto out;
4332 }
4333
4334 ret = btrfs_qgroup_rescan(root->fs_info);
4335
4336out:
4337 kfree(qsa);
4338drop_write:
4339 mnt_drop_write_file(file);
4340 return ret;
4341}
4342
4343static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4344{
Al Viro6d0379e2013-06-16 19:32:35 +04004345 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004346 struct btrfs_ioctl_quota_rescan_args *qsa;
4347 int ret = 0;
4348
4349 if (!capable(CAP_SYS_ADMIN))
4350 return -EPERM;
4351
4352 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4353 if (!qsa)
4354 return -ENOMEM;
4355
4356 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4357 qsa->flags = 1;
4358 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4359 }
4360
4361 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4362 ret = -EFAULT;
4363
4364 kfree(qsa);
4365 return ret;
4366}
4367
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004368static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4369{
Al Viro54563d42013-09-01 15:57:51 -04004370 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004371
4372 if (!capable(CAP_SYS_ADMIN))
4373 return -EPERM;
4374
4375 return btrfs_qgroup_wait_for_completion(root->fs_info);
4376}
4377
Alexander Block8ea05e32012-07-25 17:35:53 +02004378static long btrfs_ioctl_set_received_subvol(struct file *file,
4379 void __user *arg)
4380{
4381 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004382 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004383 struct btrfs_root *root = BTRFS_I(inode)->root;
4384 struct btrfs_root_item *root_item = &root->root_item;
4385 struct btrfs_trans_handle *trans;
4386 struct timespec ct = CURRENT_TIME;
4387 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004388 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004389
David Sterbabd60ea02014-01-16 15:50:22 +01004390 if (!inode_owner_or_capable(inode))
4391 return -EPERM;
4392
Alexander Block8ea05e32012-07-25 17:35:53 +02004393 ret = mnt_want_write_file(file);
4394 if (ret < 0)
4395 return ret;
4396
4397 down_write(&root->fs_info->subvol_sem);
4398
4399 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4400 ret = -EINVAL;
4401 goto out;
4402 }
4403
4404 if (btrfs_root_readonly(root)) {
4405 ret = -EROFS;
4406 goto out;
4407 }
4408
Alexander Block8ea05e32012-07-25 17:35:53 +02004409 sa = memdup_user(arg, sizeof(*sa));
4410 if (IS_ERR(sa)) {
4411 ret = PTR_ERR(sa);
4412 sa = NULL;
4413 goto out;
4414 }
4415
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004416 /*
4417 * 1 - root item
4418 * 2 - uuid items (received uuid + subvol uuid)
4419 */
4420 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004421 if (IS_ERR(trans)) {
4422 ret = PTR_ERR(trans);
4423 trans = NULL;
4424 goto out;
4425 }
4426
4427 sa->rtransid = trans->transid;
4428 sa->rtime.sec = ct.tv_sec;
4429 sa->rtime.nsec = ct.tv_nsec;
4430
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004431 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4432 BTRFS_UUID_SIZE);
4433 if (received_uuid_changed &&
4434 !btrfs_is_empty_uuid(root_item->received_uuid))
4435 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4436 root_item->received_uuid,
4437 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4438 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004439 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4440 btrfs_set_root_stransid(root_item, sa->stransid);
4441 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004442 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4443 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4444 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4445 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004446
4447 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4448 &root->root_key, &root->root_item);
4449 if (ret < 0) {
4450 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004451 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004452 }
4453 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4454 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4455 sa->uuid,
4456 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4457 root->root_key.objectid);
4458 if (ret < 0 && ret != -EEXIST) {
4459 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004460 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004461 }
4462 }
4463 ret = btrfs_commit_transaction(trans, root);
4464 if (ret < 0) {
4465 btrfs_abort_transaction(trans, root, ret);
4466 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004467 }
4468
4469 ret = copy_to_user(arg, sa, sizeof(*sa));
4470 if (ret)
4471 ret = -EFAULT;
4472
4473out:
4474 kfree(sa);
4475 up_write(&root->fs_info->subvol_sem);
4476 mnt_drop_write_file(file);
4477 return ret;
4478}
4479
jeff.liu867ab662013-01-05 02:48:01 +00004480static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4481{
Al Viro6d0379e2013-06-16 19:32:35 +04004482 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004483 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004484 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004485 char label[BTRFS_LABEL_SIZE];
4486
4487 spin_lock(&root->fs_info->super_lock);
4488 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4489 spin_unlock(&root->fs_info->super_lock);
4490
4491 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004492
4493 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004494 btrfs_warn(root->fs_info,
4495 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00004496 }
4497
jeff.liu867ab662013-01-05 02:48:01 +00004498 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004499
4500 return ret ? -EFAULT : 0;
4501}
4502
jeff.liua8bfd4a2013-01-05 02:48:08 +00004503static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4504{
Al Viro6d0379e2013-06-16 19:32:35 +04004505 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004506 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4507 struct btrfs_trans_handle *trans;
4508 char label[BTRFS_LABEL_SIZE];
4509 int ret;
4510
4511 if (!capable(CAP_SYS_ADMIN))
4512 return -EPERM;
4513
4514 if (copy_from_user(label, arg, sizeof(label)))
4515 return -EFAULT;
4516
4517 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004518 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00004519 BTRFS_LABEL_SIZE - 1);
4520 return -EINVAL;
4521 }
4522
4523 ret = mnt_want_write_file(file);
4524 if (ret)
4525 return ret;
4526
jeff.liua8bfd4a2013-01-05 02:48:08 +00004527 trans = btrfs_start_transaction(root, 0);
4528 if (IS_ERR(trans)) {
4529 ret = PTR_ERR(trans);
4530 goto out_unlock;
4531 }
4532
Anand Jaina1b83ac2013-07-19 17:39:32 +08004533 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004534 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004535 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01004536 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004537
4538out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004539 mnt_drop_write_file(file);
4540 return ret;
4541}
4542
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004543#define INIT_FEATURE_FLAGS(suffix) \
4544 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4545 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4546 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4547
4548static int btrfs_ioctl_get_supported_features(struct file *file,
4549 void __user *arg)
4550{
4551 static struct btrfs_ioctl_feature_flags features[3] = {
4552 INIT_FEATURE_FLAGS(SUPP),
4553 INIT_FEATURE_FLAGS(SAFE_SET),
4554 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4555 };
4556
4557 if (copy_to_user(arg, &features, sizeof(features)))
4558 return -EFAULT;
4559
4560 return 0;
4561}
4562
4563static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
4564{
4565 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4566 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4567 struct btrfs_ioctl_feature_flags features;
4568
4569 features.compat_flags = btrfs_super_compat_flags(super_block);
4570 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4571 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4572
4573 if (copy_to_user(arg, &features, sizeof(features)))
4574 return -EFAULT;
4575
4576 return 0;
4577}
4578
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004579static int check_feature_bits(struct btrfs_root *root,
4580 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004581 u64 change_mask, u64 flags, u64 supported_flags,
4582 u64 safe_set, u64 safe_clear)
4583{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004584 const char *type = btrfs_feature_set_names[set];
4585 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004586 u64 disallowed, unsupported;
4587 u64 set_mask = flags & change_mask;
4588 u64 clear_mask = ~flags & change_mask;
4589
4590 unsupported = set_mask & ~supported_flags;
4591 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004592 names = btrfs_printable_features(set, unsupported);
4593 if (names) {
4594 btrfs_warn(root->fs_info,
4595 "this kernel does not support the %s feature bit%s",
4596 names, strchr(names, ',') ? "s" : "");
4597 kfree(names);
4598 } else
4599 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004600 "this kernel does not support %s bits 0x%llx",
4601 type, unsupported);
4602 return -EOPNOTSUPP;
4603 }
4604
4605 disallowed = set_mask & ~safe_set;
4606 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004607 names = btrfs_printable_features(set, disallowed);
4608 if (names) {
4609 btrfs_warn(root->fs_info,
4610 "can't set the %s feature bit%s while mounted",
4611 names, strchr(names, ',') ? "s" : "");
4612 kfree(names);
4613 } else
4614 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004615 "can't set %s bits 0x%llx while mounted",
4616 type, disallowed);
4617 return -EPERM;
4618 }
4619
4620 disallowed = clear_mask & ~safe_clear;
4621 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004622 names = btrfs_printable_features(set, disallowed);
4623 if (names) {
4624 btrfs_warn(root->fs_info,
4625 "can't clear the %s feature bit%s while mounted",
4626 names, strchr(names, ',') ? "s" : "");
4627 kfree(names);
4628 } else
4629 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004630 "can't clear %s bits 0x%llx while mounted",
4631 type, disallowed);
4632 return -EPERM;
4633 }
4634
4635 return 0;
4636}
4637
4638#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004639check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004640 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4641 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4642 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4643
4644static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4645{
4646 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4647 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4648 struct btrfs_ioctl_feature_flags flags[2];
4649 struct btrfs_trans_handle *trans;
4650 u64 newflags;
4651 int ret;
4652
4653 if (!capable(CAP_SYS_ADMIN))
4654 return -EPERM;
4655
4656 if (copy_from_user(flags, arg, sizeof(flags)))
4657 return -EFAULT;
4658
4659 /* Nothing to do */
4660 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4661 !flags[0].incompat_flags)
4662 return 0;
4663
4664 ret = check_feature(root, flags[0].compat_flags,
4665 flags[1].compat_flags, COMPAT);
4666 if (ret)
4667 return ret;
4668
4669 ret = check_feature(root, flags[0].compat_ro_flags,
4670 flags[1].compat_ro_flags, COMPAT_RO);
4671 if (ret)
4672 return ret;
4673
4674 ret = check_feature(root, flags[0].incompat_flags,
4675 flags[1].incompat_flags, INCOMPAT);
4676 if (ret)
4677 return ret;
4678
David Sterba8051aa12014-02-07 14:34:04 +01004679 trans = btrfs_start_transaction(root, 0);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004680 if (IS_ERR(trans))
4681 return PTR_ERR(trans);
4682
4683 spin_lock(&root->fs_info->super_lock);
4684 newflags = btrfs_super_compat_flags(super_block);
4685 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4686 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4687 btrfs_set_super_compat_flags(super_block, newflags);
4688
4689 newflags = btrfs_super_compat_ro_flags(super_block);
4690 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4691 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4692 btrfs_set_super_compat_ro_flags(super_block, newflags);
4693
4694 newflags = btrfs_super_incompat_flags(super_block);
4695 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4696 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4697 btrfs_set_super_incompat_flags(super_block, newflags);
4698 spin_unlock(&root->fs_info->super_lock);
4699
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01004700 return btrfs_commit_transaction(trans, root);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004701}
4702
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004703long btrfs_ioctl(struct file *file, unsigned int
4704 cmd, unsigned long arg)
4705{
Al Viro496ad9a2013-01-23 17:07:38 -05004706 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004707 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004708
4709 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004710 case FS_IOC_GETFLAGS:
4711 return btrfs_ioctl_getflags(file, argp);
4712 case FS_IOC_SETFLAGS:
4713 return btrfs_ioctl_setflags(file, argp);
4714 case FS_IOC_GETVERSION:
4715 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004716 case FITRIM:
4717 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004718 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004719 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004720 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004721 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004722 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004723 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004724 case BTRFS_IOC_SUBVOL_CREATE_V2:
4725 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004726 case BTRFS_IOC_SNAP_DESTROY:
4727 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004728 case BTRFS_IOC_SUBVOL_GETFLAGS:
4729 return btrfs_ioctl_subvol_getflags(file, argp);
4730 case BTRFS_IOC_SUBVOL_SETFLAGS:
4731 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004732 case BTRFS_IOC_DEFAULT_SUBVOL:
4733 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004734 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004735 return btrfs_ioctl_defrag(file, NULL);
4736 case BTRFS_IOC_DEFRAG_RANGE:
4737 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004738 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004739 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004740 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004741 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004742 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004743 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004744 case BTRFS_IOC_FS_INFO:
4745 return btrfs_ioctl_fs_info(root, argp);
4746 case BTRFS_IOC_DEV_INFO:
4747 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004748 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004749 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004750 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004751 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4752 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004753 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004754 case BTRFS_IOC_TRANS_START:
4755 return btrfs_ioctl_trans_start(file);
4756 case BTRFS_IOC_TRANS_END:
4757 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004758 case BTRFS_IOC_TREE_SEARCH:
4759 return btrfs_ioctl_tree_search(file, argp);
4760 case BTRFS_IOC_INO_LOOKUP:
4761 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004762 case BTRFS_IOC_INO_PATHS:
4763 return btrfs_ioctl_ino_to_path(root, argp);
4764 case BTRFS_IOC_LOGICAL_INO:
4765 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004766 case BTRFS_IOC_SPACE_INFO:
4767 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004768 case BTRFS_IOC_SYNC: {
4769 int ret;
4770
Miao Xie91aef862013-11-04 23:13:26 +08004771 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004772 if (ret)
4773 return ret;
4774 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4775 return ret;
4776 }
Sage Weil46204592010-10-29 15:41:32 -04004777 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004778 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004779 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004780 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004781 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004782 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004783 case BTRFS_IOC_SCRUB_CANCEL:
4784 return btrfs_ioctl_scrub_cancel(root, argp);
4785 case BTRFS_IOC_SCRUB_PROGRESS:
4786 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004787 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004788 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004789 case BTRFS_IOC_BALANCE_CTL:
4790 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004791 case BTRFS_IOC_BALANCE_PROGRESS:
4792 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004793 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4794 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004795 case BTRFS_IOC_SEND:
4796 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004797 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004798 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004799 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004800 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004801 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004802 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004803 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004804 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004805 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004806 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004807 case BTRFS_IOC_QUOTA_RESCAN:
4808 return btrfs_ioctl_quota_rescan(file, argp);
4809 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4810 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004811 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4812 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004813 case BTRFS_IOC_DEV_REPLACE:
4814 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004815 case BTRFS_IOC_GET_FSLABEL:
4816 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004817 case BTRFS_IOC_SET_FSLABEL:
4818 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004819 case BTRFS_IOC_FILE_EXTENT_SAME:
4820 return btrfs_ioctl_file_extent_same(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004821 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4822 return btrfs_ioctl_get_supported_features(file, argp);
4823 case BTRFS_IOC_GET_FEATURES:
4824 return btrfs_ioctl_get_features(file, argp);
4825 case BTRFS_IOC_SET_FEATURES:
4826 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004827 }
4828
4829 return -ENOTTY;
4830}