blob: edf5f0093f22292199cb828b40ffa321bafef1b5 [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"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040059#include "sysfs.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040060
Mark Fasheh416161d2013-08-06 11:42:51 -070061static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/* Mask out flags that are inappropriate for the given type of inode. */
65static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
66{
67 if (S_ISDIR(mode))
68 return flags;
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
71 else
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
73}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040074
Christoph Hellwig6cbff002009-04-17 10:37:41 +020075/*
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
77 */
78static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
79{
80 unsigned int iflags = 0;
81
82 if (flags & BTRFS_INODE_SYNC)
83 iflags |= FS_SYNC_FL;
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000094 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
96
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200101
102 return iflags;
103}
104
105/*
106 * Update inode->i_flags based on the btrfs internal flags.
107 */
108void btrfs_update_iflags(struct inode *inode)
109{
110 struct btrfs_inode *ip = BTRFS_I(inode);
111
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
113
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
124}
125
126/*
127 * Inherit flags from the parent inode.
128 *
Josef Bacike27425d2011-09-27 11:01:30 -0400129 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200130 */
131void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
132{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400133 unsigned int flags;
134
135 if (!dir)
136 return;
137
138 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147
Liu Bo213490b2012-09-11 08:33:50 -0600148 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152 }
Josef Bacike27425d2011-09-27 11:01:30 -0400153
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200154 btrfs_update_iflags(inode);
155}
156
157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
Al Viro496ad9a2013-01-23 17:07:38 -0500159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
Liu Bo75e7cb72011-03-22 10:12:20 +0000167static int check_flags(unsigned int flags)
168{
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000172 FS_NOCOMP_FL | FS_COMPR_FL |
173 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000174 return -EOPNOTSUPP;
175
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
177 return -EINVAL;
178
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 return 0;
180}
181
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200182static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
183{
Al Viro496ad9a2013-01-23 17:07:38 -0500184 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
189 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800190 u64 ip_oldflags;
191 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600192 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193
Li Zefanb83cc962010-12-20 16:04:08 +0800194 if (btrfs_root_readonly(root))
195 return -EROFS;
196
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200197 if (copy_from_user(&flags, arg, sizeof(flags)))
198 return -EFAULT;
199
Liu Bo75e7cb72011-03-22 10:12:20 +0000200 ret = check_flags(flags);
201 if (ret)
202 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700204 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 return -EACCES;
206
Jan Karae7848682012-06-12 16:20:32 +0200207 ret = mnt_want_write_file(file);
208 if (ret)
209 return ret;
210
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200211 mutex_lock(&inode->i_mutex);
212
Li Zefanf062abf2011-12-29 13:36:45 +0800213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600215 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800216
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
221 ret = -EPERM;
222 goto out_unlock;
223 }
224 }
225
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
228 else
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
232 else
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
236 else
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
240 else
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
244 else
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
248 else
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600250 if (flags & FS_NOCOW_FL) {
251 if (S_ISREG(mode)) {
252 /*
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
256 */
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
260 } else {
261 ip->flags |= BTRFS_INODE_NODATACOW;
262 }
263 } else {
264 /*
265 * Revert back under same assuptions as above
266 */
267 if (S_ISREG(mode)) {
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
271 } else {
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
273 }
274 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275
Liu Bo75e7cb72011-03-22 10:12:20 +0000276 /*
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
279 * things smaller.
280 */
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000287 } else {
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000289 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200290
Li Zefan4da6f1a2011-12-29 13:39:50 +0800291 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
295 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200296
Li Zefan306424cc2011-12-14 20:12:02 -0500297 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400298 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500299 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800303 out_drop:
304 if (ret) {
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
307 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309 out_unlock:
310 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200311 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000312 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200313}
314
315static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
316{
Al Viro496ad9a2013-01-23 17:07:38 -0500317 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
319 return put_user(inode->i_generation, arg);
320}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400321
Li Dongyangf7039b12011-03-24 10:24:28 +0000322static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323{
Al Viro54563d42013-09-01 15:57:51 -0400324 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
329 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000331 int ret;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335
Xiao Guangrong1f781602011-04-20 10:09:16 +0000336 rcu_read_lock();
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
338 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000339 if (!device->bdev)
340 continue;
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
343 num_devices++;
344 minlen = min((u64)q->limits.discard_granularity,
345 minlen);
346 }
347 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000348 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200349
Li Dongyangf7039b12011-03-24 10:24:28 +0000350 if (!num_devices)
351 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000352 if (copy_from_user(&range, arg, sizeof(range)))
353 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200356 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000357
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200358 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000359 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000361 if (ret < 0)
362 return ret;
363
364 if (copy_to_user(arg, &range, sizeof(range)))
365 return -EFAULT;
366
367 return 0;
368}
369
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200370int btrfs_is_empty_uuid(u8 *uuid)
371{
Chris Mason46e0f662013-11-15 12:14:55 +0100372 int i;
373
374 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
375 if (uuid[i])
376 return 0;
377 }
378 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200379}
380
Miao Xied5c12072013-02-28 10:04:33 +0000381static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400382 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400383 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200384 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000385 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400386{
387 struct btrfs_trans_handle *trans;
388 struct btrfs_key key;
389 struct btrfs_root_item root_item;
390 struct btrfs_inode_item *inode_item;
391 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000392 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400393 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000394 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200395 struct timespec cur_time = CURRENT_TIME;
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900396 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 int ret;
398 int err;
399 u64 objectid;
400 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500401 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000402 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200403 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404
Li Zefan581bb052011-04-20 10:06:11 +0800405 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400406 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400407 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000408
Miao Xied5c12072013-02-28 10:04:33 +0000409 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400410 /*
Miao Xied5c12072013-02-28 10:04:33 +0000411 * The same as the snapshot creation, please see the comment
412 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400413 */
Miao Xied5c12072013-02-28 10:04:33 +0000414 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200415 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000416 if (ret)
417 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400418
Miao Xied5c12072013-02-28 10:04:33 +0000419 trans = btrfs_start_transaction(root, 0);
420 if (IS_ERR(trans)) {
421 ret = PTR_ERR(trans);
422 goto out;
423 }
424 trans->block_rsv = &block_rsv;
425 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400426
Miao Xie8696c532013-02-07 06:02:44 +0000427 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200428 if (ret)
429 goto fail;
430
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400431 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200432 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400433 if (IS_ERR(leaf)) {
434 ret = PTR_ERR(leaf);
435 goto fail;
436 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400438 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400439 btrfs_set_header_bytenr(leaf, leaf->start);
440 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400441 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 btrfs_set_header_owner(leaf, objectid);
443
Ross Kirk0a4e5582013-09-24 10:12:38 +0100444 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400446 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200447 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400448 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449 btrfs_mark_buffer_dirty(leaf);
450
Alexander Block8ea05e32012-07-25 17:35:53 +0200451 memset(&root_item, 0, sizeof(root_item));
452
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400453 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800454 btrfs_set_stack_inode_generation(inode_item, 1);
455 btrfs_set_stack_inode_size(inode_item, 3);
456 btrfs_set_stack_inode_nlink(inode_item, 1);
457 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
458 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459
Qu Wenruo3cae2102013-07-16 11:19:18 +0800460 btrfs_set_root_flags(&root_item, 0);
461 btrfs_set_root_limit(&root_item, 0);
462 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000463
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400465 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400466 btrfs_set_root_level(&root_item, 0);
467 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000468 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400469 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400470
Alexander Block8ea05e32012-07-25 17:35:53 +0200471 btrfs_set_root_generation_v2(&root_item,
472 btrfs_root_generation(&root_item));
473 uuid_le_gen(&new_uuid);
474 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800475 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
476 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200477 root_item.ctime = root_item.otime;
478 btrfs_set_root_ctransid(&root_item, trans->transid);
479 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400480
Chris Mason925baed2008-06-25 16:01:30 -0400481 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400482 free_extent_buffer(leaf);
483 leaf = NULL;
484
485 btrfs_set_root_dirid(&root_item, new_dirid);
486
487 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400488 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
490 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
491 &root_item);
492 if (ret)
493 goto fail;
494
Yan, Zheng76dda932009-09-21 16:00:26 -0400495 key.offset = (u64)-1;
496 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100497 if (IS_ERR(new_root)) {
498 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
499 ret = PTR_ERR(new_root);
500 goto fail;
501 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400502
503 btrfs_record_root_in_trans(trans, new_root);
504
Josef Bacikd82a6f12011-05-11 15:26:06 -0400505 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700506 if (ret) {
507 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100508 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700509 goto fail;
510 }
511
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400512 /*
513 * insert the directory item
514 */
Chris Mason3de45862008-11-17 21:02:50 -0500515 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100516 if (ret) {
517 btrfs_abort_transaction(trans, root, ret);
518 goto fail;
519 }
Chris Mason3de45862008-11-17 21:02:50 -0500520
521 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800522 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500523 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100524 if (ret) {
525 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400526 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100527 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500528
Yan Zheng52c26172009-01-05 15:43:43 -0500529 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
530 ret = btrfs_update_inode(trans, root, dir);
531 BUG_ON(ret);
532
Chris Mason0660b5a2008-11-17 20:37:39 -0500533 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400534 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800535 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500536 BUG_ON(ret);
537
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200538 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
539 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
540 objectid);
541 if (ret)
542 btrfs_abort_transaction(trans, root, ret);
543
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400544fail:
Miao Xied5c12072013-02-28 10:04:33 +0000545 trans->block_rsv = NULL;
546 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400547 if (async_transid) {
548 *async_transid = trans->transid;
549 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000550 if (err)
551 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400552 } else {
553 err = btrfs_commit_transaction(trans, root);
554 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555 if (err && !ret)
556 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500557
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900558 if (!ret) {
559 inode = btrfs_lookup_dentry(dir, dentry);
560 if (IS_ERR(inode)) {
561 ret = PTR_ERR(inode);
562 goto out;
563 }
564 d_instantiate(dentry, inode);
565 }
Miao Xied5c12072013-02-28 10:04:33 +0000566out:
567 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400568 return ret;
569}
570
Miao Xiee9662f72013-02-28 10:01:15 +0000571static int create_snapshot(struct btrfs_root *root, struct inode *dir,
572 struct dentry *dentry, char *name, int namelen,
573 u64 *async_transid, bool readonly,
574 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000576 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400577 struct btrfs_pending_snapshot *pending_snapshot;
578 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000579 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580
581 if (!root->ref_cows)
582 return -EINVAL;
583
Miao Xie6a038432013-05-15 07:48:24 +0000584 ret = btrfs_start_delalloc_inodes(root, 0);
585 if (ret)
586 return ret;
587
Miao Xieb0244192013-11-04 23:13:25 +0800588 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000589
Yan, Zhenga22285a2010-05-16 10:48:46 -0400590 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
591 if (!pending_snapshot)
592 return -ENOMEM;
593
Miao Xie66d8f3d2012-09-06 04:02:28 -0600594 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
595 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000596 /*
597 * 1 - parent dir inode
598 * 2 - dir entries
599 * 1 - root item
600 * 2 - root ref/backref
601 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200602 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000603 */
604 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200605 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400606 &pending_snapshot->qgroup_reserved,
607 false);
Miao Xied5c12072013-02-28 10:04:33 +0000608 if (ret)
609 goto out;
610
Yan, Zhenga22285a2010-05-16 10:48:46 -0400611 pending_snapshot->dentry = dentry;
612 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800613 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000614 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000615 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400616
Miao Xied5c12072013-02-28 10:04:33 +0000617 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400618 if (IS_ERR(trans)) {
619 ret = PTR_ERR(trans);
620 goto fail;
621 }
622
Josef Bacik83515832011-06-14 15:16:14 -0400623 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400624 list_add(&pending_snapshot->list,
625 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400626 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400627 if (async_transid) {
628 *async_transid = trans->transid;
629 ret = btrfs_commit_transaction_async(trans,
630 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000631 if (ret)
632 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400633 } else {
634 ret = btrfs_commit_transaction(trans,
635 root->fs_info->extent_root);
636 }
Miao Xieaec80302013-03-04 09:44:29 +0000637 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400638 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400639
640 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400641 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000642 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400643
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500644 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
645 if (ret)
646 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400647
Al Viro2fbe8c82011-07-16 21:38:06 -0400648 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000649 if (IS_ERR(inode)) {
650 ret = PTR_ERR(inode);
651 goto fail;
652 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900653
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000654 d_instantiate(dentry, inode);
655 ret = 0;
656fail:
Miao Xied5c12072013-02-28 10:04:33 +0000657 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
658 &pending_snapshot->block_rsv,
659 pending_snapshot->qgroup_reserved);
660out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400661 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400662 return ret;
663}
664
Sage Weil4260f7c2010-10-29 15:46:43 -0400665/* copy of check_sticky in fs/namei.c()
666* It's inline, so penalty for filesystems that don't use sticky bit is
667* minimal.
668*/
669static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
670{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800671 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400672
673 if (!(dir->i_mode & S_ISVTX))
674 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800675 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400676 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800677 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400678 return 0;
679 return !capable(CAP_FOWNER);
680}
681
682/* copy of may_delete in fs/namei.c()
683 * Check whether we can remove a link victim from directory dir, check
684 * whether the type of victim is right.
685 * 1. We can't do it if dir is read-only (done in permission())
686 * 2. We should have write and exec permissions on dir
687 * 3. We can't remove anything from append-only dir
688 * 4. We can't do anything with immutable dir (done in permission())
689 * 5. If the sticky bit on dir is set we should either
690 * a. be owner of dir, or
691 * b. be owner of victim, or
692 * c. have CAP_FOWNER capability
693 * 6. If the victim is append-only or immutable we can't do antyhing with
694 * links pointing to it.
695 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
696 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
697 * 9. We can't remove a root or mountpoint.
698 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
699 * nfs_async_unlink().
700 */
701
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530702static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400703{
704 int error;
705
706 if (!victim->d_inode)
707 return -ENOENT;
708
709 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400710 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400711
712 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
713 if (error)
714 return error;
715 if (IS_APPEND(dir))
716 return -EPERM;
717 if (btrfs_check_sticky(dir, victim->d_inode)||
718 IS_APPEND(victim->d_inode)||
719 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
720 return -EPERM;
721 if (isdir) {
722 if (!S_ISDIR(victim->d_inode->i_mode))
723 return -ENOTDIR;
724 if (IS_ROOT(victim))
725 return -EBUSY;
726 } else if (S_ISDIR(victim->d_inode->i_mode))
727 return -EISDIR;
728 if (IS_DEADDIR(dir))
729 return -ENOENT;
730 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
731 return -EBUSY;
732 return 0;
733}
734
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400735/* copy of may_create in fs/namei.c() */
736static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
737{
738 if (child->d_inode)
739 return -EEXIST;
740 if (IS_DEADDIR(dir))
741 return -ENOENT;
742 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
743}
744
745/*
746 * Create a new subvolume below @parent. This is largely modeled after
747 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
748 * inside this filesystem so it's quite a bit simpler.
749 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400750static noinline int btrfs_mksubvol(struct path *parent,
751 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400752 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200753 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000754 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400755{
Yan, Zheng76dda932009-09-21 16:00:26 -0400756 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400757 struct dentry *dentry;
758 int error;
759
David Sterba5c50c9b2013-03-22 18:12:51 +0000760 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
761 if (error == -EINTR)
762 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400763
764 dentry = lookup_one_len(name, parent->dentry, namelen);
765 error = PTR_ERR(dentry);
766 if (IS_ERR(dentry))
767 goto out_unlock;
768
769 error = -EEXIST;
770 if (dentry->d_inode)
771 goto out_dput;
772
Yan, Zheng76dda932009-09-21 16:00:26 -0400773 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400774 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600775 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400776
Chris Mason9c520572012-12-17 14:26:57 -0500777 /*
778 * even if this name doesn't exist, we may get hash collisions.
779 * check for them now when we can safely fail
780 */
781 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
782 dir->i_ino, name,
783 namelen);
784 if (error)
785 goto out_dput;
786
Yan, Zheng76dda932009-09-21 16:00:26 -0400787 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
788
789 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
790 goto out_up_read;
791
Chris Mason3de45862008-11-17 21:02:50 -0500792 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000793 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200794 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500795 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000796 error = create_subvol(dir, dentry, name, namelen,
797 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500798 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400799 if (!error)
800 fsnotify_mkdir(dir, dentry);
801out_up_read:
802 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400803out_dput:
804 dput(dentry);
805out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400806 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400807 return error;
808}
809
Chris Mason4cb53002011-05-24 15:35:30 -0400810/*
811 * When we're defragging a range, we don't want to kick it off again
812 * if it is really just waiting for delalloc to send it down.
813 * If we find a nice big extent or delalloc range for the bytes in the
814 * file you want to defrag, we return 0 to let you know to skip this
815 * part of the file
816 */
817static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
818{
819 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
820 struct extent_map *em = NULL;
821 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
822 u64 end;
823
824 read_lock(&em_tree->lock);
825 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
826 read_unlock(&em_tree->lock);
827
828 if (em) {
829 end = extent_map_end(em);
830 free_extent_map(em);
831 if (end - offset > thresh)
832 return 0;
833 }
834 /* if we already have a nice delalloc here, just stop */
835 thresh /= 2;
836 end = count_range_bits(io_tree, &offset, offset + thresh,
837 thresh, EXTENT_DELALLOC, 1);
838 if (end >= thresh)
839 return 0;
840 return 1;
841}
842
843/*
844 * helper function to walk through a file and find extents
845 * newer than a specific transid, and smaller than thresh.
846 *
847 * This is used by the defragging code to find new and small
848 * extents
849 */
850static int find_new_extents(struct btrfs_root *root,
851 struct inode *inode, u64 newer_than,
852 u64 *off, int thresh)
853{
854 struct btrfs_path *path;
855 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400856 struct extent_buffer *leaf;
857 struct btrfs_file_extent_item *extent;
858 int type;
859 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000860 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400861
862 path = btrfs_alloc_path();
863 if (!path)
864 return -ENOMEM;
865
David Sterbaa4689d22011-05-31 17:08:14 +0000866 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400867 min_key.type = BTRFS_EXTENT_DATA_KEY;
868 min_key.offset = *off;
869
Chris Mason4cb53002011-05-24 15:35:30 -0400870 path->keep_locks = 1;
871
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530872 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100873 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400874 if (ret != 0)
875 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000876 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400877 goto none;
878 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
879 goto none;
880
881 leaf = path->nodes[0];
882 extent = btrfs_item_ptr(leaf, path->slots[0],
883 struct btrfs_file_extent_item);
884
885 type = btrfs_file_extent_type(leaf, extent);
886 if (type == BTRFS_FILE_EXTENT_REG &&
887 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
888 check_defrag_in_cache(inode, min_key.offset, thresh)) {
889 *off = min_key.offset;
890 btrfs_free_path(path);
891 return 0;
892 }
893
894 if (min_key.offset == (u64)-1)
895 goto none;
896
897 min_key.offset++;
898 btrfs_release_path(path);
899 }
900none:
901 btrfs_free_path(path);
902 return -ENOENT;
903}
904
Li Zefan6c282eb2012-06-11 16:03:35 +0800905static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400906{
907 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800909 struct extent_map *em;
910 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500911
912 /*
913 * hopefully we have this extent in the tree already, try without
914 * the full extent lock
915 */
916 read_lock(&em_tree->lock);
917 em = lookup_extent_mapping(em_tree, start, len);
918 read_unlock(&em_tree->lock);
919
920 if (!em) {
921 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100922 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500923 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100924 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500925
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000926 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800927 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500928 }
929
Li Zefan6c282eb2012-06-11 16:03:35 +0800930 return em;
931}
932
933static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
934{
935 struct extent_map *next;
936 bool ret = true;
937
938 /* this is the last extent */
939 if (em->start + em->len >= i_size_read(inode))
940 return false;
941
942 next = defrag_lookup_extent(inode, em->start + em->len);
943 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
944 ret = false;
945
946 free_extent_map(next);
947 return ret;
948}
949
950static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400951 u64 *last_len, u64 *skip, u64 *defrag_end,
952 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800953{
954 struct extent_map *em;
955 int ret = 1;
956 bool next_mergeable = true;
957
958 /*
959 * make sure that once we start defragging an extent, we keep on
960 * defragging it
961 */
962 if (start < *defrag_end)
963 return 1;
964
965 *skip = 0;
966
967 em = defrag_lookup_extent(inode, start);
968 if (!em)
969 return 0;
970
Chris Mason940100a2010-03-10 10:52:59 -0500971 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400972 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500973 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400974 goto out;
975 }
976
Li Zefan6c282eb2012-06-11 16:03:35 +0800977 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500978
979 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800980 * we hit a real extent, if it is big or the next extent is not a
981 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500982 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400983 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800984 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500985 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400986out:
Chris Mason940100a2010-03-10 10:52:59 -0500987 /*
988 * last_len ends up being a counter of how many bytes we've defragged.
989 * every time we choose not to defrag an extent, we reset *last_len
990 * so that the next tiny extent will force a defrag.
991 *
992 * The end result of this is that tiny extents before a single big
993 * extent will force at least part of that big extent to be defragged.
994 */
995 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500996 *defrag_end = extent_map_end(em);
997 } else {
998 *last_len = 0;
999 *skip = extent_map_end(em);
1000 *defrag_end = 0;
1001 }
1002
1003 free_extent_map(em);
1004 return ret;
1005}
1006
Chris Mason4cb53002011-05-24 15:35:30 -04001007/*
1008 * it doesn't do much good to defrag one or two pages
1009 * at a time. This pulls in a nice chunk of pages
1010 * to COW and defrag.
1011 *
1012 * It also makes sure the delalloc code has enough
1013 * dirty data to avoid making new small extents as part
1014 * of the defrag
1015 *
1016 * It's a good idea to start RA on this range
1017 * before calling this.
1018 */
1019static int cluster_pages_for_defrag(struct inode *inode,
1020 struct page **pages,
1021 unsigned long start_index,
1022 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001023{
Chris Mason4cb53002011-05-24 15:35:30 -04001024 unsigned long file_end;
1025 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001026 u64 page_start;
1027 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001028 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001029 int ret;
1030 int i;
1031 int i_done;
1032 struct btrfs_ordered_extent *ordered;
1033 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001034 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001035 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001036
Chris Mason4cb53002011-05-24 15:35:30 -04001037 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001038 if (!isize || start_index > file_end)
1039 return 0;
1040
1041 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001042
1043 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001044 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001045 if (ret)
1046 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001047 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001048 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001049
1050 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001051 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001052 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001053again:
Josef Bacika94733d2011-07-11 10:47:06 -04001054 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001055 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001056 if (!page)
1057 break;
1058
Miao Xie600a45e2012-02-16 15:01:24 +08001059 page_start = page_offset(page);
1060 page_end = page_start + PAGE_CACHE_SIZE - 1;
1061 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001062 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001063 ordered = btrfs_lookup_ordered_extent(inode,
1064 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001065 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001066 if (!ordered)
1067 break;
1068
1069 unlock_page(page);
1070 btrfs_start_ordered_extent(inode, ordered, 1);
1071 btrfs_put_ordered_extent(ordered);
1072 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001073 /*
1074 * we unlocked the page above, so we need check if
1075 * it was released or not.
1076 */
1077 if (page->mapping != inode->i_mapping) {
1078 unlock_page(page);
1079 page_cache_release(page);
1080 goto again;
1081 }
Miao Xie600a45e2012-02-16 15:01:24 +08001082 }
1083
Chris Mason4cb53002011-05-24 15:35:30 -04001084 if (!PageUptodate(page)) {
1085 btrfs_readpage(NULL, page);
1086 lock_page(page);
1087 if (!PageUptodate(page)) {
1088 unlock_page(page);
1089 page_cache_release(page);
1090 ret = -EIO;
1091 break;
1092 }
1093 }
Miao Xie600a45e2012-02-16 15:01:24 +08001094
Miao Xie600a45e2012-02-16 15:01:24 +08001095 if (page->mapping != inode->i_mapping) {
1096 unlock_page(page);
1097 page_cache_release(page);
1098 goto again;
1099 }
1100
Chris Mason4cb53002011-05-24 15:35:30 -04001101 pages[i] = page;
1102 i_done++;
1103 }
1104 if (!i_done || ret)
1105 goto out;
1106
1107 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1108 goto out;
1109
1110 /*
1111 * so now we have a nice long stream of locked
1112 * and up to date pages, lets wait on them
1113 */
1114 for (i = 0; i < i_done; i++)
1115 wait_on_page_writeback(pages[i]);
1116
1117 page_start = page_offset(pages[0]);
1118 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1119
1120 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001121 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001122 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1123 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001124 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1125 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001126
Liu Bo1f12bd02012-03-29 09:57:44 -04001127 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001128 spin_lock(&BTRFS_I(inode)->lock);
1129 BTRFS_I(inode)->outstanding_extents++;
1130 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001131 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001132 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001133 }
1134
1135
Liu Bo9e8a4a82012-09-05 19:10:51 -06001136 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1137 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001138
1139 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1140 page_start, page_end - 1, &cached_state,
1141 GFP_NOFS);
1142
1143 for (i = 0; i < i_done; i++) {
1144 clear_page_dirty_for_io(pages[i]);
1145 ClearPageChecked(pages[i]);
1146 set_page_extent_mapped(pages[i]);
1147 set_page_dirty(pages[i]);
1148 unlock_page(pages[i]);
1149 page_cache_release(pages[i]);
1150 }
1151 return i_done;
1152out:
1153 for (i = 0; i < i_done; i++) {
1154 unlock_page(pages[i]);
1155 page_cache_release(pages[i]);
1156 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001157 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001158 return ret;
1159
1160}
1161
1162int btrfs_defrag_file(struct inode *inode, struct file *file,
1163 struct btrfs_ioctl_defrag_range_args *range,
1164 u64 newer_than, unsigned long max_to_defrag)
1165{
1166 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001167 struct file_ra_state *ra = NULL;
1168 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001169 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001170 u64 last_len = 0;
1171 u64 skip = 0;
1172 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001173 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001174 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001175 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001176 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001177 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001178 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001179 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001180 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1181 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001182 u64 new_align = ~((u64)128 * 1024 - 1);
1183 struct page **pages = NULL;
1184
Liu Bo0abd5b12013-04-16 09:20:28 +00001185 if (isize == 0)
1186 return 0;
1187
1188 if (range->start >= isize)
1189 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001190
1191 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1192 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1193 return -EINVAL;
1194 if (range->compress_type)
1195 compress_type = range->compress_type;
1196 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001197
Liu Bo0abd5b12013-04-16 09:20:28 +00001198 if (extent_thresh == 0)
1199 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001200
Chris Mason4cb53002011-05-24 15:35:30 -04001201 /*
1202 * if we were not given a file, allocate a readahead
1203 * context
1204 */
1205 if (!file) {
1206 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1207 if (!ra)
1208 return -ENOMEM;
1209 file_ra_state_init(ra, inode->i_mapping);
1210 } else {
1211 ra = &file->f_ra;
1212 }
1213
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301214 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001215 GFP_NOFS);
1216 if (!pages) {
1217 ret = -ENOMEM;
1218 goto out_ra;
1219 }
1220
1221 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001222 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001223 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001224 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1225 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001226 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001227 }
1228
Chris Mason4cb53002011-05-24 15:35:30 -04001229 if (newer_than) {
1230 ret = find_new_extents(root, inode, newer_than,
1231 &newer_off, 64 * 1024);
1232 if (!ret) {
1233 range->start = newer_off;
1234 /*
1235 * we always align our defrag to help keep
1236 * the extents in the file evenly spaced
1237 */
1238 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001239 } else
1240 goto out_ra;
1241 } else {
1242 i = range->start >> PAGE_CACHE_SHIFT;
1243 }
1244 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001245 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001246
Li Zefan2a0f7f52011-10-10 15:43:34 -04001247 /*
1248 * make writeback starts from i, so the defrag range can be
1249 * written sequentially.
1250 */
1251 if (i < inode->i_mapping->writeback_index)
1252 inode->i_mapping->writeback_index = i;
1253
Chris Masonf7f43cc2011-10-11 11:41:40 -04001254 while (i <= last_index && defrag_count < max_to_defrag &&
1255 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1256 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001257 /*
1258 * make sure we stop running if someone unmounts
1259 * the FS
1260 */
1261 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1262 break;
1263
David Sterba210549e2013-02-09 23:38:06 +00001264 if (btrfs_defrag_cancelled(root->fs_info)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001265 printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
David Sterba210549e2013-02-09 23:38:06 +00001266 ret = -EAGAIN;
1267 break;
1268 }
1269
Liu Bo66c26892012-03-29 09:57:45 -04001270 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001271 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001272 &defrag_end, range->flags &
1273 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001274 unsigned long next;
1275 /*
1276 * the should_defrag function tells us how much to skip
1277 * bump our counter by the suggested amount
1278 */
1279 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1280 i = max(i + 1, next);
1281 continue;
1282 }
Li Zefan008873e2011-09-02 15:57:07 +08001283
1284 if (!newer_than) {
1285 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1286 PAGE_CACHE_SHIFT) - i;
1287 cluster = min(cluster, max_cluster);
1288 } else {
1289 cluster = max_cluster;
1290 }
1291
Li Zefan008873e2011-09-02 15:57:07 +08001292 if (i + cluster > ra_index) {
1293 ra_index = max(i, ra_index);
1294 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1295 cluster);
1296 ra_index += max_cluster;
1297 }
Chris Mason940100a2010-03-10 10:52:59 -05001298
Liu Boecb8bea2012-03-29 09:57:44 -04001299 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001300 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1301 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001302 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001303 if (ret < 0) {
1304 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001305 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001306 }
Chris Mason940100a2010-03-10 10:52:59 -05001307
Chris Mason4cb53002011-05-24 15:35:30 -04001308 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001309 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001310 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001311
1312 if (newer_than) {
1313 if (newer_off == (u64)-1)
1314 break;
1315
Liu Boe1f041e2012-03-29 09:57:45 -04001316 if (ret > 0)
1317 i += ret;
1318
Chris Mason4cb53002011-05-24 15:35:30 -04001319 newer_off = max(newer_off + 1,
1320 (u64)i << PAGE_CACHE_SHIFT);
1321
1322 ret = find_new_extents(root, inode,
1323 newer_than, &newer_off,
1324 64 * 1024);
1325 if (!ret) {
1326 range->start = newer_off;
1327 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001328 } else {
1329 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330 }
Chris Mason4cb53002011-05-24 15:35:30 -04001331 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001332 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001333 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001334 last_len += ret << PAGE_CACHE_SHIFT;
1335 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001336 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001337 last_len = 0;
1338 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001339 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001340 }
1341
Chris Mason1e701a32010-03-11 09:42:04 -05001342 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1343 filemap_flush(inode->i_mapping);
1344
1345 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1346 /* the filemap_flush will queue IO into the worker threads, but
1347 * we have to make sure the IO is actually started and that
1348 * ordered extents get created before we return
1349 */
1350 atomic_inc(&root->fs_info->async_submit_draining);
1351 while (atomic_read(&root->fs_info->nr_async_submits) ||
1352 atomic_read(&root->fs_info->async_delalloc_pages)) {
1353 wait_event(root->fs_info->async_submit_wait,
1354 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1355 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1356 }
1357 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001358 }
1359
Li Zefan1a419d82010-10-25 15:12:50 +08001360 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001361 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001362 }
1363
Diego Calleja60ccf822011-09-01 16:33:57 +02001364 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001365
Chris Mason4cb53002011-05-24 15:35:30 -04001366out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001367 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1368 mutex_lock(&inode->i_mutex);
1369 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1370 mutex_unlock(&inode->i_mutex);
1371 }
Chris Mason4cb53002011-05-24 15:35:30 -04001372 if (!file)
1373 kfree(ra);
1374 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001375 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001376}
1377
Miao Xie198605a2012-11-26 08:43:45 +00001378static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001379 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001380{
1381 u64 new_size;
1382 u64 old_size;
1383 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001384 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001385 struct btrfs_ioctl_vol_args *vol_args;
1386 struct btrfs_trans_handle *trans;
1387 struct btrfs_device *device = NULL;
1388 char *sizestr;
1389 char *devstr = NULL;
1390 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001391 int mod = 0;
1392
Chris Masone441d542009-01-05 16:57:23 -05001393 if (!capable(CAP_SYS_ADMIN))
1394 return -EPERM;
1395
Miao Xie198605a2012-11-26 08:43:45 +00001396 ret = mnt_want_write_file(file);
1397 if (ret)
1398 return ret;
1399
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001400 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1401 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001402 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001403 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001404 }
1405
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001406 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001407 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001408 if (IS_ERR(vol_args)) {
1409 ret = PTR_ERR(vol_args);
1410 goto out;
1411 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001412
1413 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001414
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001415 sizestr = vol_args->name;
1416 devstr = strchr(sizestr, ':');
1417 if (devstr) {
1418 char *end;
1419 sizestr = devstr + 1;
1420 *devstr = '\0';
1421 devstr = vol_args->name;
1422 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001423 if (!devid) {
1424 ret = -EINVAL;
1425 goto out_free;
1426 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001427 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428 }
Miao Xiedba60f32012-12-21 09:19:51 +00001429
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001430 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001432 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001433 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001434 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001435 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436 }
Miao Xiedba60f32012-12-21 09:19:51 +00001437
1438 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001439 btrfs_info(root->fs_info,
1440 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001441 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001442 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001443 goto out_free;
1444 }
1445
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001446 if (!strcmp(sizestr, "max"))
1447 new_size = device->bdev->bd_inode->i_size;
1448 else {
1449 if (sizestr[0] == '-') {
1450 mod = -1;
1451 sizestr++;
1452 } else if (sizestr[0] == '+') {
1453 mod = 1;
1454 sizestr++;
1455 }
Akinobu Mita91748462010-02-28 10:59:11 +00001456 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001457 if (new_size == 0) {
1458 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001459 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001460 }
1461 }
1462
Stefan Behrens63a212a2012-11-05 18:29:28 +01001463 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001464 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001465 goto out_free;
1466 }
1467
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001468 old_size = device->total_bytes;
1469
1470 if (mod < 0) {
1471 if (new_size > old_size) {
1472 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001473 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001474 }
1475 new_size = old_size - new_size;
1476 } else if (mod > 0) {
1477 new_size = old_size + new_size;
1478 }
1479
1480 if (new_size < 256 * 1024 * 1024) {
1481 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001482 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001483 }
1484 if (new_size > device->bdev->bd_inode->i_size) {
1485 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001486 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001487 }
1488
1489 do_div(new_size, root->sectorsize);
1490 new_size *= root->sectorsize;
1491
Frank Holtonefe120a2013-12-20 11:37:06 -05001492 printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001493 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001494
1495 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001496 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001497 if (IS_ERR(trans)) {
1498 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001499 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001500 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001501 ret = btrfs_grow_device(trans, device, new_size);
1502 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001503 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001504 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001505 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001506
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001507out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001508 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001509out:
1510 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001511 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001512 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001513 return ret;
1514}
1515
Sage Weil72fd0322010-10-29 15:41:32 -04001516static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001517 char *name, unsigned long fd, int subvol,
1518 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001519 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001521 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001522 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001523
Liu Boa874a632012-06-29 03:58:46 -06001524 ret = mnt_want_write_file(file);
1525 if (ret)
1526 goto out;
1527
Sage Weil72fd0322010-10-29 15:41:32 -04001528 namelen = strlen(name);
1529 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001530 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001531 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001532 }
1533
Chris Mason16780ca2012-02-20 22:14:55 -05001534 if (name[0] == '.' &&
1535 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1536 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001537 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001538 }
1539
Chris Mason3de45862008-11-17 21:02:50 -05001540 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001541 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001542 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001543 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001544 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001545 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001546 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001547 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001548 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001549 }
1550
Al Viro496ad9a2013-01-23 17:07:38 -05001551 src_inode = file_inode(src.file);
1552 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001553 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1554 "Snapshot src from another FS");
Chris Mason3de45862008-11-17 21:02:50 -05001555 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001556 } else {
1557 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1558 BTRFS_I(src_inode)->root,
1559 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001560 }
Al Viro2903ff02012-08-28 12:52:22 -04001561 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001562 }
Liu Boa874a632012-06-29 03:58:46 -06001563out_drop_write:
1564 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001565out:
Sage Weil72fd0322010-10-29 15:41:32 -04001566 return ret;
1567}
1568
1569static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001570 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001571{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001572 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001573 int ret;
1574
Li Zefanfa0d2b92010-12-20 15:53:28 +08001575 vol_args = memdup_user(arg, sizeof(*vol_args));
1576 if (IS_ERR(vol_args))
1577 return PTR_ERR(vol_args);
1578 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001579
Li Zefanfa0d2b92010-12-20 15:53:28 +08001580 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001581 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001582 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001583
Li Zefanfa0d2b92010-12-20 15:53:28 +08001584 kfree(vol_args);
1585 return ret;
1586}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001587
Li Zefanfa0d2b92010-12-20 15:53:28 +08001588static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1589 void __user *arg, int subvol)
1590{
1591 struct btrfs_ioctl_vol_args_v2 *vol_args;
1592 int ret;
1593 u64 transid = 0;
1594 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001595 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001596 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001597
Li Zefanfa0d2b92010-12-20 15:53:28 +08001598 vol_args = memdup_user(arg, sizeof(*vol_args));
1599 if (IS_ERR(vol_args))
1600 return PTR_ERR(vol_args);
1601 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001602
Li Zefanb83cc962010-12-20 16:04:08 +08001603 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001604 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1605 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001606 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001607 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001608 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001609
1610 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1611 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001612 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1613 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001614 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1615 if (vol_args->size > PAGE_CACHE_SIZE) {
1616 ret = -EINVAL;
1617 goto out;
1618 }
1619 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1620 if (IS_ERR(inherit)) {
1621 ret = PTR_ERR(inherit);
1622 goto out;
1623 }
1624 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001625
1626 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001627 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001628 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001629
1630 if (ret == 0 && ptr &&
1631 copy_to_user(arg +
1632 offsetof(struct btrfs_ioctl_vol_args_v2,
1633 transid), ptr, sizeof(*ptr)))
1634 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001635out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001637 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638 return ret;
1639}
1640
Li Zefan0caa1022010-12-20 16:30:25 +08001641static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1642 void __user *arg)
1643{
Al Viro496ad9a2013-01-23 17:07:38 -05001644 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001645 struct btrfs_root *root = BTRFS_I(inode)->root;
1646 int ret = 0;
1647 u64 flags = 0;
1648
Li Zefan33345d012011-04-20 10:31:50 +08001649 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001650 return -EINVAL;
1651
1652 down_read(&root->fs_info->subvol_sem);
1653 if (btrfs_root_readonly(root))
1654 flags |= BTRFS_SUBVOL_RDONLY;
1655 up_read(&root->fs_info->subvol_sem);
1656
1657 if (copy_to_user(arg, &flags, sizeof(flags)))
1658 ret = -EFAULT;
1659
1660 return ret;
1661}
1662
1663static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1664 void __user *arg)
1665{
Al Viro496ad9a2013-01-23 17:07:38 -05001666 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001667 struct btrfs_root *root = BTRFS_I(inode)->root;
1668 struct btrfs_trans_handle *trans;
1669 u64 root_flags;
1670 u64 flags;
1671 int ret = 0;
1672
Liu Bob9ca0662012-06-29 03:58:49 -06001673 ret = mnt_want_write_file(file);
1674 if (ret)
1675 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001676
Liu Bob9ca0662012-06-29 03:58:49 -06001677 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1678 ret = -EINVAL;
1679 goto out_drop_write;
1680 }
Li Zefan0caa1022010-12-20 16:30:25 +08001681
Liu Bob9ca0662012-06-29 03:58:49 -06001682 if (copy_from_user(&flags, arg, sizeof(flags))) {
1683 ret = -EFAULT;
1684 goto out_drop_write;
1685 }
Li Zefan0caa1022010-12-20 16:30:25 +08001686
Liu Bob9ca0662012-06-29 03:58:49 -06001687 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1688 ret = -EINVAL;
1689 goto out_drop_write;
1690 }
Li Zefan0caa1022010-12-20 16:30:25 +08001691
Liu Bob9ca0662012-06-29 03:58:49 -06001692 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1693 ret = -EOPNOTSUPP;
1694 goto out_drop_write;
1695 }
Li Zefan0caa1022010-12-20 16:30:25 +08001696
Liu Bob9ca0662012-06-29 03:58:49 -06001697 if (!inode_owner_or_capable(inode)) {
1698 ret = -EACCES;
1699 goto out_drop_write;
1700 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001701
Li Zefan0caa1022010-12-20 16:30:25 +08001702 down_write(&root->fs_info->subvol_sem);
1703
1704 /* nothing to do */
1705 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001706 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001707
1708 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001709 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001710 btrfs_set_root_flags(&root->root_item,
1711 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001712 } else {
1713 /*
1714 * Block RO -> RW transition if this subvolume is involved in
1715 * send
1716 */
1717 spin_lock(&root->root_item_lock);
1718 if (root->send_in_progress == 0) {
1719 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001720 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001721 spin_unlock(&root->root_item_lock);
1722 } else {
1723 spin_unlock(&root->root_item_lock);
1724 btrfs_warn(root->fs_info,
1725 "Attempt to set subvolume %llu read-write during send",
1726 root->root_key.objectid);
1727 ret = -EPERM;
1728 goto out_drop_sem;
1729 }
1730 }
Li Zefan0caa1022010-12-20 16:30:25 +08001731
1732 trans = btrfs_start_transaction(root, 1);
1733 if (IS_ERR(trans)) {
1734 ret = PTR_ERR(trans);
1735 goto out_reset;
1736 }
1737
Li Zefanb4dc2b82011-02-16 06:06:34 +00001738 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001739 &root->root_key, &root->root_item);
1740
1741 btrfs_commit_transaction(trans, root);
1742out_reset:
1743 if (ret)
1744 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001745out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001746 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001747out_drop_write:
1748 mnt_drop_write_file(file);
1749out:
Li Zefan0caa1022010-12-20 16:30:25 +08001750 return ret;
1751}
1752
Yan, Zheng76dda932009-09-21 16:00:26 -04001753/*
1754 * helper to check if the subvolume references other subvolumes
1755 */
1756static noinline int may_destroy_subvol(struct btrfs_root *root)
1757{
1758 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001759 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001760 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001761 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001762 int ret;
1763
1764 path = btrfs_alloc_path();
1765 if (!path)
1766 return -ENOMEM;
1767
Josef Bacik175a2b82013-08-12 15:36:44 -04001768 /* Make sure this root isn't set as the default subvol */
1769 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1770 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1771 dir_id, "default", 7, 0);
1772 if (di && !IS_ERR(di)) {
1773 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1774 if (key.objectid == root->root_key.objectid) {
1775 ret = -ENOTEMPTY;
1776 goto out;
1777 }
1778 btrfs_release_path(path);
1779 }
1780
Yan, Zheng76dda932009-09-21 16:00:26 -04001781 key.objectid = root->root_key.objectid;
1782 key.type = BTRFS_ROOT_REF_KEY;
1783 key.offset = (u64)-1;
1784
1785 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1786 &key, path, 0, 0);
1787 if (ret < 0)
1788 goto out;
1789 BUG_ON(ret == 0);
1790
1791 ret = 0;
1792 if (path->slots[0] > 0) {
1793 path->slots[0]--;
1794 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1795 if (key.objectid == root->root_key.objectid &&
1796 key.type == BTRFS_ROOT_REF_KEY)
1797 ret = -ENOTEMPTY;
1798 }
1799out:
1800 btrfs_free_path(path);
1801 return ret;
1802}
1803
Chris Masonac8e9812010-02-28 15:39:26 -05001804static noinline int key_in_sk(struct btrfs_key *key,
1805 struct btrfs_ioctl_search_key *sk)
1806{
Chris Masonabc6e132010-03-18 12:10:08 -04001807 struct btrfs_key test;
1808 int ret;
1809
1810 test.objectid = sk->min_objectid;
1811 test.type = sk->min_type;
1812 test.offset = sk->min_offset;
1813
1814 ret = btrfs_comp_cpu_keys(key, &test);
1815 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001816 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001817
1818 test.objectid = sk->max_objectid;
1819 test.type = sk->max_type;
1820 test.offset = sk->max_offset;
1821
1822 ret = btrfs_comp_cpu_keys(key, &test);
1823 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001824 return 0;
1825 return 1;
1826}
1827
1828static noinline int copy_to_sk(struct btrfs_root *root,
1829 struct btrfs_path *path,
1830 struct btrfs_key *key,
1831 struct btrfs_ioctl_search_key *sk,
1832 char *buf,
1833 unsigned long *sk_offset,
1834 int *num_found)
1835{
1836 u64 found_transid;
1837 struct extent_buffer *leaf;
1838 struct btrfs_ioctl_search_header sh;
1839 unsigned long item_off;
1840 unsigned long item_len;
1841 int nritems;
1842 int i;
1843 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001844 int ret = 0;
1845
1846 leaf = path->nodes[0];
1847 slot = path->slots[0];
1848 nritems = btrfs_header_nritems(leaf);
1849
1850 if (btrfs_header_generation(leaf) > sk->max_transid) {
1851 i = nritems;
1852 goto advance_key;
1853 }
1854 found_transid = btrfs_header_generation(leaf);
1855
1856 for (i = slot; i < nritems; i++) {
1857 item_off = btrfs_item_ptr_offset(leaf, i);
1858 item_len = btrfs_item_size_nr(leaf, i);
1859
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001860 btrfs_item_key_to_cpu(leaf, key, i);
1861 if (!key_in_sk(key, sk))
1862 continue;
1863
1864 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001865 item_len = 0;
1866
1867 if (sizeof(sh) + item_len + *sk_offset >
1868 BTRFS_SEARCH_ARGS_BUFSIZE) {
1869 ret = 1;
1870 goto overflow;
1871 }
1872
Chris Masonac8e9812010-02-28 15:39:26 -05001873 sh.objectid = key->objectid;
1874 sh.offset = key->offset;
1875 sh.type = key->type;
1876 sh.len = item_len;
1877 sh.transid = found_transid;
1878
1879 /* copy search result header */
1880 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1881 *sk_offset += sizeof(sh);
1882
1883 if (item_len) {
1884 char *p = buf + *sk_offset;
1885 /* copy the item */
1886 read_extent_buffer(leaf, p,
1887 item_off, item_len);
1888 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001889 }
Hugo Millse2156862011-05-14 17:43:41 +00001890 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001891
1892 if (*num_found >= sk->nr_items)
1893 break;
1894 }
1895advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001896 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001897 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1898 key->offset++;
1899 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1900 key->offset = 0;
1901 key->type++;
1902 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1903 key->offset = 0;
1904 key->type = 0;
1905 key->objectid++;
1906 } else
1907 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001908overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001909 return ret;
1910}
1911
1912static noinline int search_ioctl(struct inode *inode,
1913 struct btrfs_ioctl_search_args *args)
1914{
1915 struct btrfs_root *root;
1916 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001917 struct btrfs_path *path;
1918 struct btrfs_ioctl_search_key *sk = &args->key;
1919 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1920 int ret;
1921 int num_found = 0;
1922 unsigned long sk_offset = 0;
1923
1924 path = btrfs_alloc_path();
1925 if (!path)
1926 return -ENOMEM;
1927
1928 if (sk->tree_id == 0) {
1929 /* search the root of the inode that was passed */
1930 root = BTRFS_I(inode)->root;
1931 } else {
1932 key.objectid = sk->tree_id;
1933 key.type = BTRFS_ROOT_ITEM_KEY;
1934 key.offset = (u64)-1;
1935 root = btrfs_read_fs_root_no_name(info, &key);
1936 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001937 printk(KERN_ERR "BTRFS: could not find root %llu\n",
Chris Masonac8e9812010-02-28 15:39:26 -05001938 sk->tree_id);
1939 btrfs_free_path(path);
1940 return -ENOENT;
1941 }
1942 }
1943
1944 key.objectid = sk->min_objectid;
1945 key.type = sk->min_type;
1946 key.offset = sk->min_offset;
1947
Chris Masonac8e9812010-02-28 15:39:26 -05001948 path->keep_locks = 1;
1949
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05301950 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01001951 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05001952 if (ret != 0) {
1953 if (ret > 0)
1954 ret = 0;
1955 goto err;
1956 }
1957 ret = copy_to_sk(root, path, &key, sk, args->buf,
1958 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001959 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001960 if (ret || num_found >= sk->nr_items)
1961 break;
1962
1963 }
1964 ret = 0;
1965err:
1966 sk->nr_items = num_found;
1967 btrfs_free_path(path);
1968 return ret;
1969}
1970
1971static noinline int btrfs_ioctl_tree_search(struct file *file,
1972 void __user *argp)
1973{
1974 struct btrfs_ioctl_search_args *args;
1975 struct inode *inode;
1976 int ret;
1977
1978 if (!capable(CAP_SYS_ADMIN))
1979 return -EPERM;
1980
Julia Lawall2354d082010-10-29 15:14:18 -04001981 args = memdup_user(argp, sizeof(*args));
1982 if (IS_ERR(args))
1983 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001984
Al Viro496ad9a2013-01-23 17:07:38 -05001985 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001986 ret = search_ioctl(inode, args);
1987 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1988 ret = -EFAULT;
1989 kfree(args);
1990 return ret;
1991}
1992
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001993/*
Chris Masonac8e9812010-02-28 15:39:26 -05001994 * Search INODE_REFs to identify path name of 'dirid' directory
1995 * in a 'tree_id' tree. and sets path name to 'name'.
1996 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001997static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1998 u64 tree_id, u64 dirid, char *name)
1999{
2000 struct btrfs_root *root;
2001 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002002 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002003 int ret = -1;
2004 int slot;
2005 int len;
2006 int total_len = 0;
2007 struct btrfs_inode_ref *iref;
2008 struct extent_buffer *l;
2009 struct btrfs_path *path;
2010
2011 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2012 name[0]='\0';
2013 return 0;
2014 }
2015
2016 path = btrfs_alloc_path();
2017 if (!path)
2018 return -ENOMEM;
2019
Chris Masonac8e9812010-02-28 15:39:26 -05002020 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002021
2022 key.objectid = tree_id;
2023 key.type = BTRFS_ROOT_ITEM_KEY;
2024 key.offset = (u64)-1;
2025 root = btrfs_read_fs_root_no_name(info, &key);
2026 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002027 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002028 ret = -ENOENT;
2029 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002030 }
2031
2032 key.objectid = dirid;
2033 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002034 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002035
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302036 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2038 if (ret < 0)
2039 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002040 else if (ret > 0) {
2041 ret = btrfs_previous_item(root, path, dirid,
2042 BTRFS_INODE_REF_KEY);
2043 if (ret < 0)
2044 goto out;
2045 else if (ret > 0) {
2046 ret = -ENOENT;
2047 goto out;
2048 }
2049 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002050
2051 l = path->nodes[0];
2052 slot = path->slots[0];
2053 btrfs_item_key_to_cpu(l, &key, slot);
2054
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002055 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2056 len = btrfs_inode_ref_name_len(l, iref);
2057 ptr -= len + 1;
2058 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002059 if (ptr < name) {
2060 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002061 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002062 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002063
2064 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302065 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002066
2067 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2068 break;
2069
David Sterbab3b4aa72011-04-21 01:20:15 +02002070 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002071 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002072 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002073 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002074 }
Li Zefan77906a502011-07-14 03:16:00 +00002075 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302076 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002077 ret = 0;
2078out:
2079 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002080 return ret;
2081}
2082
2083static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2084 void __user *argp)
2085{
2086 struct btrfs_ioctl_ino_lookup_args *args;
2087 struct inode *inode;
2088 int ret;
2089
2090 if (!capable(CAP_SYS_ADMIN))
2091 return -EPERM;
2092
Julia Lawall2354d082010-10-29 15:14:18 -04002093 args = memdup_user(argp, sizeof(*args));
2094 if (IS_ERR(args))
2095 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002096
Al Viro496ad9a2013-01-23 17:07:38 -05002097 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002098
Chris Mason1b53ac42010-03-18 12:17:05 -04002099 if (args->treeid == 0)
2100 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2101
Chris Masonac8e9812010-02-28 15:39:26 -05002102 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2103 args->treeid, args->objectid,
2104 args->name);
2105
2106 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2107 ret = -EFAULT;
2108
2109 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002110 return ret;
2111}
2112
Yan, Zheng76dda932009-09-21 16:00:26 -04002113static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2114 void __user *arg)
2115{
Al Viro54563d42013-09-01 15:57:51 -04002116 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002117 struct dentry *dentry;
2118 struct inode *dir = parent->d_inode;
2119 struct inode *inode;
2120 struct btrfs_root *root = BTRFS_I(dir)->root;
2121 struct btrfs_root *dest = NULL;
2122 struct btrfs_ioctl_vol_args *vol_args;
2123 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002124 struct btrfs_block_rsv block_rsv;
2125 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002126 int namelen;
2127 int ret;
2128 int err = 0;
2129
Yan, Zheng76dda932009-09-21 16:00:26 -04002130 vol_args = memdup_user(arg, sizeof(*vol_args));
2131 if (IS_ERR(vol_args))
2132 return PTR_ERR(vol_args);
2133
2134 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2135 namelen = strlen(vol_args->name);
2136 if (strchr(vol_args->name, '/') ||
2137 strncmp(vol_args->name, "..", namelen) == 0) {
2138 err = -EINVAL;
2139 goto out;
2140 }
2141
Al Viroa561be72011-11-23 11:57:51 -05002142 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002143 if (err)
2144 goto out;
2145
David Sterba5c50c9b2013-03-22 18:12:51 +00002146 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2147 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002148 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002149 dentry = lookup_one_len(vol_args->name, parent, namelen);
2150 if (IS_ERR(dentry)) {
2151 err = PTR_ERR(dentry);
2152 goto out_unlock_dir;
2153 }
2154
2155 if (!dentry->d_inode) {
2156 err = -ENOENT;
2157 goto out_dput;
2158 }
2159
2160 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002161 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302162 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002163 /*
2164 * Regular user. Only allow this with a special mount
2165 * option, when the user has write+exec access to the
2166 * subvol root, and when rmdir(2) would have been
2167 * allowed.
2168 *
2169 * Note that this is _not_ check that the subvol is
2170 * empty or doesn't contain data that we wouldn't
2171 * otherwise be able to delete.
2172 *
2173 * Users who want to delete empty subvols should try
2174 * rmdir(2).
2175 */
2176 err = -EPERM;
2177 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2178 goto out_dput;
2179
2180 /*
2181 * Do not allow deletion if the parent dir is the same
2182 * as the dir to be deleted. That means the ioctl
2183 * must be called on the dentry referencing the root
2184 * of the subvol, not a random directory contained
2185 * within it.
2186 */
2187 err = -EINVAL;
2188 if (root == dest)
2189 goto out_dput;
2190
2191 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2192 if (err)
2193 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002194 }
2195
Miao Xie5c39da52012-10-22 11:39:53 +00002196 /* check if subvolume may be deleted by a user */
2197 err = btrfs_may_delete(dir, dentry, 1);
2198 if (err)
2199 goto out_dput;
2200
Li Zefan33345d012011-04-20 10:31:50 +08002201 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002202 err = -EINVAL;
2203 goto out_dput;
2204 }
2205
Yan, Zheng76dda932009-09-21 16:00:26 -04002206 mutex_lock(&inode->i_mutex);
2207 err = d_invalidate(dentry);
2208 if (err)
2209 goto out_unlock;
2210
2211 down_write(&root->fs_info->subvol_sem);
2212
2213 err = may_destroy_subvol(dest);
2214 if (err)
2215 goto out_up_write;
2216
Miao Xiec58aaad2013-02-28 10:05:36 +00002217 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2218 /*
2219 * One for dir inode, two for dir entries, two for root
2220 * ref/backref.
2221 */
2222 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002223 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002224 if (err)
2225 goto out_up_write;
2226
Yan, Zhenga22285a2010-05-16 10:48:46 -04002227 trans = btrfs_start_transaction(root, 0);
2228 if (IS_ERR(trans)) {
2229 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002230 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002231 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002232 trans->block_rsv = &block_rsv;
2233 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002234
Yan, Zheng76dda932009-09-21 16:00:26 -04002235 ret = btrfs_unlink_subvol(trans, root, dir,
2236 dest->root_key.objectid,
2237 dentry->d_name.name,
2238 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002239 if (ret) {
2240 err = ret;
2241 btrfs_abort_transaction(trans, root, ret);
2242 goto out_end_trans;
2243 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002244
2245 btrfs_record_root_in_trans(trans, dest);
2246
2247 memset(&dest->root_item.drop_progress, 0,
2248 sizeof(dest->root_item.drop_progress));
2249 dest->root_item.drop_level = 0;
2250 btrfs_set_root_refs(&dest->root_item, 0);
2251
Yan, Zhengd68fc572010-05-16 10:49:58 -04002252 if (!xchg(&dest->orphan_item_inserted, 1)) {
2253 ret = btrfs_insert_orphan_item(trans,
2254 root->fs_info->tree_root,
2255 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002256 if (ret) {
2257 btrfs_abort_transaction(trans, root, ret);
2258 err = ret;
2259 goto out_end_trans;
2260 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002261 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002262
2263 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2264 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2265 dest->root_key.objectid);
2266 if (ret && ret != -ENOENT) {
2267 btrfs_abort_transaction(trans, root, ret);
2268 err = ret;
2269 goto out_end_trans;
2270 }
2271 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2272 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2273 dest->root_item.received_uuid,
2274 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2275 dest->root_key.objectid);
2276 if (ret && ret != -ENOENT) {
2277 btrfs_abort_transaction(trans, root, ret);
2278 err = ret;
2279 goto out_end_trans;
2280 }
2281 }
2282
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002284 trans->block_rsv = NULL;
2285 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002286 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002287 if (ret && !err)
2288 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002289 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002290out_release:
2291 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002292out_up_write:
2293 up_write(&root->fs_info->subvol_sem);
2294out_unlock:
2295 mutex_unlock(&inode->i_mutex);
2296 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002297 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002298 btrfs_invalidate_inodes(dest);
2299 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002300
2301 /* the last ref */
2302 if (dest->cache_inode) {
2303 iput(dest->cache_inode);
2304 dest->cache_inode = NULL;
2305 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002306 }
2307out_dput:
2308 dput(dentry);
2309out_unlock_dir:
2310 mutex_unlock(&dir->i_mutex);
David Sterbae43f9982013-12-06 17:51:32 +01002311out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002312 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002313out:
2314 kfree(vol_args);
2315 return err;
2316}
2317
Chris Mason1e701a32010-03-11 09:42:04 -05002318static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002319{
Al Viro496ad9a2013-01-23 17:07:38 -05002320 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002321 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002322 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002323 int ret;
2324
Ilya Dryomov25122d12013-01-20 15:57:57 +02002325 ret = mnt_want_write_file(file);
2326 if (ret)
2327 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002328
Ilya Dryomov25122d12013-01-20 15:57:57 +02002329 if (btrfs_root_readonly(root)) {
2330 ret = -EROFS;
2331 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002332 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002333
2334 switch (inode->i_mode & S_IFMT) {
2335 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002336 if (!capable(CAP_SYS_ADMIN)) {
2337 ret = -EPERM;
2338 goto out;
2339 }
Eric Sandeende78b512013-01-31 18:21:12 +00002340 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002341 if (ret)
2342 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002343 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002344 break;
2345 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002346 if (!(file->f_mode & FMODE_WRITE)) {
2347 ret = -EINVAL;
2348 goto out;
2349 }
Chris Mason1e701a32010-03-11 09:42:04 -05002350
2351 range = kzalloc(sizeof(*range), GFP_KERNEL);
2352 if (!range) {
2353 ret = -ENOMEM;
2354 goto out;
2355 }
2356
2357 if (argp) {
2358 if (copy_from_user(range, argp,
2359 sizeof(*range))) {
2360 ret = -EFAULT;
2361 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002362 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002363 }
2364 /* compression requires us to start the IO */
2365 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2366 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2367 range->extent_thresh = (u32)-1;
2368 }
2369 } else {
2370 /* the rest are all set to zero by kzalloc */
2371 range->len = (u64)-1;
2372 }
Al Viro496ad9a2013-01-23 17:07:38 -05002373 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002374 range, 0, 0);
2375 if (ret > 0)
2376 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002377 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002378 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002379 default:
2380 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002381 }
Chris Masone441d542009-01-05 16:57:23 -05002382out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002383 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002384 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002385}
2386
Christoph Hellwigb2950862008-12-02 09:54:17 -05002387static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002388{
2389 struct btrfs_ioctl_vol_args *vol_args;
2390 int ret;
2391
Chris Masone441d542009-01-05 16:57:23 -05002392 if (!capable(CAP_SYS_ADMIN))
2393 return -EPERM;
2394
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002395 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2396 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002397 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002398 }
2399
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002400 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002401 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002402 if (IS_ERR(vol_args)) {
2403 ret = PTR_ERR(vol_args);
2404 goto out;
2405 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002406
Mark Fasheh5516e592008-07-24 12:20:14 -04002407 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002408 ret = btrfs_init_new_device(root, vol_args->name);
2409
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002411out:
2412 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002413 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002414 return ret;
2415}
2416
Miao Xieda249272012-11-26 08:44:50 +00002417static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002418{
Al Viro496ad9a2013-01-23 17:07:38 -05002419 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002420 struct btrfs_ioctl_vol_args *vol_args;
2421 int ret;
2422
Chris Masone441d542009-01-05 16:57:23 -05002423 if (!capable(CAP_SYS_ADMIN))
2424 return -EPERM;
2425
Miao Xieda249272012-11-26 08:44:50 +00002426 ret = mnt_want_write_file(file);
2427 if (ret)
2428 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002429
Li Zefandae7b662009-04-08 15:06:54 +08002430 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002431 if (IS_ERR(vol_args)) {
2432 ret = PTR_ERR(vol_args);
2433 goto out;
2434 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002435
Mark Fasheh5516e592008-07-24 12:20:14 -04002436 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002437
Anand Jain183860f2013-05-17 10:52:45 +00002438 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2439 1)) {
2440 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2441 goto out;
2442 }
2443
2444 mutex_lock(&root->fs_info->volume_mutex);
2445 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002446 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002447 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002448
2449out:
2450 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002451 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002452 return ret;
2453}
2454
Jan Schmidt475f6382011-03-11 15:41:01 +01002455static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2456{
Li Zefan027ed2f2011-06-08 08:27:56 +00002457 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002458 struct btrfs_device *device;
2459 struct btrfs_device *next;
2460 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002461 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002462
2463 if (!capable(CAP_SYS_ADMIN))
2464 return -EPERM;
2465
Li Zefan027ed2f2011-06-08 08:27:56 +00002466 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2467 if (!fi_args)
2468 return -ENOMEM;
2469
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002470 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002471 fi_args->num_devices = fs_devices->num_devices;
2472 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002473
Jan Schmidt475f6382011-03-11 15:41:01 +01002474 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002475 if (device->devid > fi_args->max_id)
2476 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002477 }
2478 mutex_unlock(&fs_devices->device_list_mutex);
2479
Li Zefan027ed2f2011-06-08 08:27:56 +00002480 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2481 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002482
Li Zefan027ed2f2011-06-08 08:27:56 +00002483 kfree(fi_args);
2484 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002485}
2486
2487static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2488{
2489 struct btrfs_ioctl_dev_info_args *di_args;
2490 struct btrfs_device *dev;
2491 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2492 int ret = 0;
2493 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002494
2495 if (!capable(CAP_SYS_ADMIN))
2496 return -EPERM;
2497
2498 di_args = memdup_user(arg, sizeof(*di_args));
2499 if (IS_ERR(di_args))
2500 return PTR_ERR(di_args);
2501
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002502 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002503 s_uuid = di_args->uuid;
2504
2505 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002506 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002507
2508 if (!dev) {
2509 ret = -ENODEV;
2510 goto out;
2511 }
2512
2513 di_args->devid = dev->devid;
2514 di_args->bytes_used = dev->bytes_used;
2515 di_args->total_bytes = dev->total_bytes;
2516 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002517 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002518 struct rcu_string *name;
2519
2520 rcu_read_lock();
2521 name = rcu_dereference(dev->name);
2522 strncpy(di_args->path, name->str, sizeof(di_args->path));
2523 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002524 di_args->path[sizeof(di_args->path) - 1] = 0;
2525 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002526 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002527 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002528
2529out:
David Sterba55793c02013-04-26 15:20:23 +00002530 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002531 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2532 ret = -EFAULT;
2533
2534 kfree(di_args);
2535 return ret;
2536}
2537
Mark Fasheh416161d2013-08-06 11:42:51 -07002538static struct page *extent_same_get_page(struct inode *inode, u64 off)
2539{
2540 struct page *page;
2541 pgoff_t index;
2542 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2543
2544 index = off >> PAGE_CACHE_SHIFT;
2545
2546 page = grab_cache_page(inode->i_mapping, index);
2547 if (!page)
2548 return NULL;
2549
2550 if (!PageUptodate(page)) {
2551 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2552 0))
2553 return NULL;
2554 lock_page(page);
2555 if (!PageUptodate(page)) {
2556 unlock_page(page);
2557 page_cache_release(page);
2558 return NULL;
2559 }
2560 }
2561 unlock_page(page);
2562
2563 return page;
2564}
2565
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002566static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2567{
2568 /* do any pending delalloc/csum calc on src, one way or
2569 another, and lock file content */
2570 while (1) {
2571 struct btrfs_ordered_extent *ordered;
2572 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2573 ordered = btrfs_lookup_first_ordered_extent(inode,
2574 off + len - 1);
2575 if (!ordered &&
2576 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2577 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2578 break;
2579 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2580 if (ordered)
2581 btrfs_put_ordered_extent(ordered);
2582 btrfs_wait_ordered_range(inode, off, len);
2583 }
2584}
2585
Mark Fasheh416161d2013-08-06 11:42:51 -07002586static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2587 struct inode *inode2, u64 loff2, u64 len)
2588{
2589 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2590 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2591
2592 mutex_unlock(&inode1->i_mutex);
2593 mutex_unlock(&inode2->i_mutex);
2594}
2595
2596static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2597 struct inode *inode2, u64 loff2, u64 len)
2598{
2599 if (inode1 < inode2) {
2600 swap(inode1, inode2);
2601 swap(loff1, loff2);
2602 }
2603
2604 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2605 lock_extent_range(inode1, loff1, len);
2606 if (inode1 != inode2) {
2607 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2608 lock_extent_range(inode2, loff2, len);
2609 }
2610}
2611
2612static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2613 u64 dst_loff, u64 len)
2614{
2615 int ret = 0;
2616 struct page *src_page, *dst_page;
2617 unsigned int cmp_len = PAGE_CACHE_SIZE;
2618 void *addr, *dst_addr;
2619
2620 while (len) {
2621 if (len < PAGE_CACHE_SIZE)
2622 cmp_len = len;
2623
2624 src_page = extent_same_get_page(src, loff);
2625 if (!src_page)
2626 return -EINVAL;
2627 dst_page = extent_same_get_page(dst, dst_loff);
2628 if (!dst_page) {
2629 page_cache_release(src_page);
2630 return -EINVAL;
2631 }
2632 addr = kmap_atomic(src_page);
2633 dst_addr = kmap_atomic(dst_page);
2634
2635 flush_dcache_page(src_page);
2636 flush_dcache_page(dst_page);
2637
2638 if (memcmp(addr, dst_addr, cmp_len))
2639 ret = BTRFS_SAME_DATA_DIFFERS;
2640
2641 kunmap_atomic(addr);
2642 kunmap_atomic(dst_addr);
2643 page_cache_release(src_page);
2644 page_cache_release(dst_page);
2645
2646 if (ret)
2647 break;
2648
2649 loff += cmp_len;
2650 dst_loff += cmp_len;
2651 len -= cmp_len;
2652 }
2653
2654 return ret;
2655}
2656
2657static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2658{
2659 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2660
2661 if (off + len > inode->i_size || off + len < off)
2662 return -EINVAL;
2663 /* Check that we are block aligned - btrfs_clone() requires this */
2664 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2665 return -EINVAL;
2666
2667 return 0;
2668}
2669
2670static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2671 struct inode *dst, u64 dst_loff)
2672{
2673 int ret;
2674
2675 /*
2676 * btrfs_clone() can't handle extents in the same file
2677 * yet. Once that works, we can drop this check and replace it
2678 * with a check for the same inode, but overlapping extents.
2679 */
2680 if (src == dst)
2681 return -EINVAL;
2682
2683 btrfs_double_lock(src, loff, dst, dst_loff, len);
2684
2685 ret = extent_same_check_offsets(src, loff, len);
2686 if (ret)
2687 goto out_unlock;
2688
2689 ret = extent_same_check_offsets(dst, dst_loff, len);
2690 if (ret)
2691 goto out_unlock;
2692
2693 /* don't make the dst file partly checksummed */
2694 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2695 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2696 ret = -EINVAL;
2697 goto out_unlock;
2698 }
2699
2700 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2701 if (ret == 0)
2702 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2703
2704out_unlock:
2705 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2706
2707 return ret;
2708}
2709
2710#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2711
2712static long btrfs_ioctl_file_extent_same(struct file *file,
2713 void __user *argp)
2714{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002715 struct btrfs_ioctl_same_args tmp;
2716 struct btrfs_ioctl_same_args *same;
2717 struct btrfs_ioctl_same_extent_info *info;
Mark Fasheh416161d2013-08-06 11:42:51 -07002718 struct inode *src = file->f_dentry->d_inode;
2719 struct file *dst_file = NULL;
2720 struct inode *dst;
2721 u64 off;
2722 u64 len;
2723 int i;
2724 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002725 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002726 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2727 bool is_admin = capable(CAP_SYS_ADMIN);
2728
2729 if (!(file->f_mode & FMODE_READ))
2730 return -EINVAL;
2731
2732 ret = mnt_want_write_file(file);
2733 if (ret)
2734 return ret;
2735
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002736 if (copy_from_user(&tmp,
Mark Fasheh416161d2013-08-06 11:42:51 -07002737 (struct btrfs_ioctl_same_args __user *)argp,
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002738 sizeof(tmp))) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002739 ret = -EFAULT;
2740 goto out;
2741 }
2742
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002743 size = sizeof(tmp) +
2744 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2745
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002746 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002747
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002748 if (IS_ERR(same)) {
2749 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002750 goto out;
2751 }
2752
2753 off = same->logical_offset;
2754 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002755
2756 /*
2757 * Limit the total length we will dedupe for each operation.
2758 * This is intended to bound the total time spent in this
2759 * ioctl to something sane.
2760 */
2761 if (len > BTRFS_MAX_DEDUPE_LEN)
2762 len = BTRFS_MAX_DEDUPE_LEN;
2763
2764 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2765 /*
2766 * Btrfs does not support blocksize < page_size. As a
2767 * result, btrfs_cmp_data() won't correctly handle
2768 * this situation without an update.
2769 */
2770 ret = -EINVAL;
2771 goto out;
2772 }
2773
2774 ret = -EISDIR;
2775 if (S_ISDIR(src->i_mode))
2776 goto out;
2777
2778 ret = -EACCES;
2779 if (!S_ISREG(src->i_mode))
2780 goto out;
2781
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002782 /* pre-format output fields to sane values */
2783 for (i = 0; i < same->dest_count; i++) {
2784 same->info[i].bytes_deduped = 0ULL;
2785 same->info[i].status = 0;
2786 }
2787
Mark Fasheh416161d2013-08-06 11:42:51 -07002788 ret = 0;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002789 for (i = 0; i < same->dest_count; i++) {
2790 info = &same->info[i];
Mark Fasheh416161d2013-08-06 11:42:51 -07002791
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002792 dst_file = fget(info->fd);
Mark Fasheh416161d2013-08-06 11:42:51 -07002793 if (!dst_file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002794 info->status = -EBADF;
Mark Fasheh416161d2013-08-06 11:42:51 -07002795 goto next;
2796 }
2797
2798 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002799 info->status = -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07002800 goto next;
2801 }
2802
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002803 info->status = -EXDEV;
Mark Fasheh416161d2013-08-06 11:42:51 -07002804 if (file->f_path.mnt != dst_file->f_path.mnt)
2805 goto next;
2806
2807 dst = dst_file->f_dentry->d_inode;
2808 if (src->i_sb != dst->i_sb)
2809 goto next;
2810
2811 if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002812 info->status = -EISDIR;
Mark Fasheh416161d2013-08-06 11:42:51 -07002813 goto next;
2814 }
2815
2816 if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002817 info->status = -EACCES;
Mark Fasheh416161d2013-08-06 11:42:51 -07002818 goto next;
2819 }
2820
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002821 info->status = btrfs_extent_same(src, off, len, dst,
2822 info->logical_offset);
2823 if (info->status == 0)
2824 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07002825
2826next:
2827 if (dst_file)
2828 fput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002829 }
2830
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002831 ret = copy_to_user(argp, same, size);
2832 if (ret)
2833 ret = -EFAULT;
2834
Mark Fasheh416161d2013-08-06 11:42:51 -07002835out:
2836 mnt_drop_write_file(file);
2837 return ret;
2838}
2839
Mark Fasheh32b7c682013-08-06 11:42:49 -07002840/**
2841 * btrfs_clone() - clone a range from inode file to another
2842 *
2843 * @src: Inode to clone from
2844 * @inode: Inode to clone to
2845 * @off: Offset within source to start clone from
2846 * @olen: Original length, passed by user, of range to clone
2847 * @olen_aligned: Block-aligned value of olen, extent_same uses
2848 * identical values here
2849 * @destoff: Offset within @inode to start clone
2850 */
2851static int btrfs_clone(struct inode *src, struct inode *inode,
2852 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002853{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002854 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002855 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002856 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002857 struct btrfs_trans_handle *trans;
2858 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002859 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002860 u32 nritems;
2861 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002862 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002863 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002864
2865 ret = -ENOMEM;
2866 buf = vmalloc(btrfs_level_size(root, 0));
2867 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002868 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002869
2870 path = btrfs_alloc_path();
2871 if (!path) {
2872 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002873 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002874 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002875
Yan Zhengae01a0a2008-08-04 23:23:47 -04002876 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002877 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002878 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002879 key.type = BTRFS_EXTENT_DATA_KEY;
2880 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002881
2882 while (1) {
2883 /*
2884 * note the key will change type as we walk through the
2885 * tree.
2886 */
David Sterba362a20c2011-08-01 18:11:57 +02002887 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2888 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002889 if (ret < 0)
2890 goto out;
2891
Yan Zhengae01a0a2008-08-04 23:23:47 -04002892 nritems = btrfs_header_nritems(path->nodes[0]);
2893 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002894 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002895 if (ret < 0)
2896 goto out;
2897 if (ret > 0)
2898 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002899 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002900 }
2901 leaf = path->nodes[0];
2902 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002903
Yan Zhengae01a0a2008-08-04 23:23:47 -04002904 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002905 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002906 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002907 break;
2908
Sage Weilc5c9cd42008-11-12 14:32:25 -05002909 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2910 struct btrfs_file_extent_item *extent;
2911 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002912 u32 size;
2913 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002914 u64 disko = 0, diskl = 0;
2915 u64 datao = 0, datal = 0;
2916 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002917 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002918
2919 size = btrfs_item_size_nr(leaf, slot);
2920 read_extent_buffer(leaf, buf,
2921 btrfs_item_ptr_offset(leaf, slot),
2922 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002923
2924 extent = btrfs_item_ptr(leaf, slot,
2925 struct btrfs_file_extent_item);
2926 comp = btrfs_file_extent_compression(leaf, extent);
2927 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002928 if (type == BTRFS_FILE_EXTENT_REG ||
2929 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002930 disko = btrfs_file_extent_disk_bytenr(leaf,
2931 extent);
2932 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2933 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002934 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002935 datal = btrfs_file_extent_num_bytes(leaf,
2936 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002937 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2938 /* take upper bound, may be compressed */
2939 datal = btrfs_file_extent_ram_bytes(leaf,
2940 extent);
2941 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002942 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002943
Sage Weil050006a2010-10-29 15:37:33 -04002944 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002945 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002946 goto next;
2947
Zheng Yan31840ae2008-09-23 13:14:14 -04002948 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002949 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002950 if (off <= key.offset)
2951 new_key.offset = key.offset + destoff - off;
2952 else
2953 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002954
Sage Weilb6f34092011-09-20 14:48:51 -04002955 /*
2956 * 1 - adjusting old extent (we may have to split it)
2957 * 1 - add new extent
2958 * 1 - inode update
2959 */
2960 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002961 if (IS_ERR(trans)) {
2962 ret = PTR_ERR(trans);
2963 goto out;
2964 }
2965
Chris Masonc8a894d2009-06-27 21:07:03 -04002966 if (type == BTRFS_FILE_EXTENT_REG ||
2967 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002968 /*
2969 * a | --- range to clone ---| b
2970 * | ------------- extent ------------- |
2971 */
2972
2973 /* substract range b */
2974 if (key.offset + datal > off + len)
2975 datal = off + len - key.offset;
2976
2977 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002978 if (off > key.offset) {
2979 datao += off - key.offset;
2980 datal -= off - key.offset;
2981 }
2982
Josef Bacik5dc562c2012-08-17 13:14:17 -04002983 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002984 new_key.offset,
2985 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002986 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002987 if (ret) {
2988 btrfs_abort_transaction(trans, root,
2989 ret);
2990 btrfs_end_transaction(trans, root);
2991 goto out;
2992 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002993
Sage Weilc5c9cd42008-11-12 14:32:25 -05002994 ret = btrfs_insert_empty_item(trans, root, path,
2995 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 if (ret) {
2997 btrfs_abort_transaction(trans, root,
2998 ret);
2999 btrfs_end_transaction(trans, root);
3000 goto out;
3001 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003002
3003 leaf = path->nodes[0];
3004 slot = path->slots[0];
3005 write_extent_buffer(leaf, buf,
3006 btrfs_item_ptr_offset(leaf, slot),
3007 size);
3008
3009 extent = btrfs_item_ptr(leaf, slot,
3010 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003011
Sage Weilc5c9cd42008-11-12 14:32:25 -05003012 /* disko == 0 means it's a hole */
3013 if (!disko)
3014 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003015
3016 btrfs_set_file_extent_offset(leaf, extent,
3017 datao);
3018 btrfs_set_file_extent_num_bytes(leaf, extent,
3019 datal);
3020 if (disko) {
3021 inode_add_bytes(inode, datal);
3022 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003023 disko, diskl, 0,
3024 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003025 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003026 new_key.offset - datao,
3027 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003028 if (ret) {
3029 btrfs_abort_transaction(trans,
3030 root,
3031 ret);
3032 btrfs_end_transaction(trans,
3033 root);
3034 goto out;
3035
3036 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003037 }
3038 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3039 u64 skip = 0;
3040 u64 trim = 0;
3041 if (off > key.offset) {
3042 skip = off - key.offset;
3043 new_key.offset += skip;
3044 }
Chris Masond3977122009-01-05 21:25:51 -05003045
Liu Boaa42ffd2012-09-18 03:52:23 -06003046 if (key.offset + datal > off + len)
3047 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003048
Sage Weilc5c9cd42008-11-12 14:32:25 -05003049 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003050 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003051 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003052 goto out;
3053 }
3054 size -= skip + trim;
3055 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003056
Josef Bacik5dc562c2012-08-17 13:14:17 -04003057 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003058 new_key.offset,
3059 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003060 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003061 if (ret) {
3062 btrfs_abort_transaction(trans, root,
3063 ret);
3064 btrfs_end_transaction(trans, root);
3065 goto out;
3066 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003067
Sage Weilc5c9cd42008-11-12 14:32:25 -05003068 ret = btrfs_insert_empty_item(trans, root, path,
3069 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003070 if (ret) {
3071 btrfs_abort_transaction(trans, root,
3072 ret);
3073 btrfs_end_transaction(trans, root);
3074 goto out;
3075 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003076
3077 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003078 u32 start =
3079 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003080 memmove(buf+start, buf+start+skip,
3081 datal);
3082 }
3083
3084 leaf = path->nodes[0];
3085 slot = path->slots[0];
3086 write_extent_buffer(leaf, buf,
3087 btrfs_item_ptr_offset(leaf, slot),
3088 size);
3089 inode_add_bytes(inode, datal);
3090 }
3091
3092 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003093 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003094
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003095 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003097
3098 /*
3099 * we round up to the block size at eof when
3100 * determining which extents to clone above,
3101 * but shouldn't round up the file size
3102 */
3103 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003104 if (endoff > destoff+olen)
3105 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003106 if (endoff > inode->i_size)
3107 btrfs_i_size_write(inode, endoff);
3108
Yan, Zhenga22285a2010-05-16 10:48:46 -04003109 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003110 if (ret) {
3111 btrfs_abort_transaction(trans, root, ret);
3112 btrfs_end_transaction(trans, root);
3113 goto out;
3114 }
3115 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003116 }
Chris Masond3977122009-01-05 21:25:51 -05003117next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003118 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003119 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003120 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003121 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003122
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003123out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003124 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003125 btrfs_free_path(path);
3126 vfree(buf);
3127 return ret;
3128}
3129
3130static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3131 u64 off, u64 olen, u64 destoff)
3132{
Al Viro54563d42013-09-01 15:57:51 -04003133 struct inode *inode = file_inode(file);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003134 struct btrfs_root *root = BTRFS_I(inode)->root;
3135 struct fd src_file;
3136 struct inode *src;
3137 int ret;
3138 u64 len = olen;
3139 u64 bs = root->fs_info->sb->s_blocksize;
3140 int same_inode = 0;
3141
3142 /*
3143 * TODO:
3144 * - split compressed inline extents. annoying: we need to
3145 * decompress into destination's address_space (the file offset
3146 * may change, so source mapping won't do), then recompress (or
3147 * otherwise reinsert) a subrange.
3148 * - allow ranges within the same file to be cloned (provided
3149 * they don't overlap)?
3150 */
3151
3152 /* the destination must be opened for writing */
3153 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3154 return -EINVAL;
3155
3156 if (btrfs_root_readonly(root))
3157 return -EROFS;
3158
3159 ret = mnt_want_write_file(file);
3160 if (ret)
3161 return ret;
3162
3163 src_file = fdget(srcfd);
3164 if (!src_file.file) {
3165 ret = -EBADF;
3166 goto out_drop_write;
3167 }
3168
3169 ret = -EXDEV;
3170 if (src_file.file->f_path.mnt != file->f_path.mnt)
3171 goto out_fput;
3172
3173 src = file_inode(src_file.file);
3174
3175 ret = -EINVAL;
3176 if (src == inode)
3177 same_inode = 1;
3178
3179 /* the src must be open for reading */
3180 if (!(src_file.file->f_mode & FMODE_READ))
3181 goto out_fput;
3182
3183 /* don't make the dst file partly checksummed */
3184 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3185 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3186 goto out_fput;
3187
3188 ret = -EISDIR;
3189 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3190 goto out_fput;
3191
3192 ret = -EXDEV;
3193 if (src->i_sb != inode->i_sb)
3194 goto out_fput;
3195
3196 if (!same_inode) {
3197 if (inode < src) {
3198 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3199 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3200 } else {
3201 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3202 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3203 }
3204 } else {
3205 mutex_lock(&src->i_mutex);
3206 }
3207
3208 /* determine range to clone */
3209 ret = -EINVAL;
3210 if (off + len > src->i_size || off + len < off)
3211 goto out_unlock;
3212 if (len == 0)
3213 olen = len = src->i_size - off;
3214 /* if we extend to eof, continue to block boundary */
3215 if (off + len == src->i_size)
3216 len = ALIGN(src->i_size, bs) - off;
3217
3218 /* verify the end result is block aligned */
3219 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3220 !IS_ALIGNED(destoff, bs))
3221 goto out_unlock;
3222
3223 /* verify if ranges are overlapped within the same file */
3224 if (same_inode) {
3225 if (destoff + len > off && destoff < off + len)
3226 goto out_unlock;
3227 }
3228
3229 if (destoff > inode->i_size) {
3230 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3231 if (ret)
3232 goto out_unlock;
3233 }
3234
3235 /* truncate page cache pages from target inode range */
3236 truncate_inode_pages_range(&inode->i_data, destoff,
3237 PAGE_CACHE_ALIGN(destoff + len) - 1);
3238
3239 lock_extent_range(src, off, len);
3240
3241 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3242
Liu Boaa42ffd2012-09-18 03:52:23 -06003243 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003244out_unlock:
3245 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003246 if (!same_inode)
3247 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003248out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003249 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003250out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003251 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252 return ret;
3253}
3254
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003255static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003256{
3257 struct btrfs_ioctl_clone_range_args args;
3258
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003259 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003260 return -EFAULT;
3261 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3262 args.src_length, args.dest_offset);
3263}
3264
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003265/*
3266 * there are many ways the trans_start and trans_end ioctls can lead
3267 * to deadlocks. They should only be used by applications that
3268 * basically own the machine, and have a very in depth understanding
3269 * of all the possible deadlocks and enospc problems.
3270 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003271static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003272{
Al Viro496ad9a2013-01-23 17:07:38 -05003273 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003274 struct btrfs_root *root = BTRFS_I(inode)->root;
3275 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003276 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003277
Sage Weil1ab86ae2009-09-29 18:38:44 -04003278 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003279 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003280 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003281
3282 ret = -EINPROGRESS;
3283 if (file->private_data)
3284 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003285
Li Zefanb83cc962010-12-20 16:04:08 +08003286 ret = -EROFS;
3287 if (btrfs_root_readonly(root))
3288 goto out;
3289
Al Viroa561be72011-11-23 11:57:51 -05003290 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003291 if (ret)
3292 goto out;
3293
Josef Bacika4abeea2011-04-11 17:25:13 -04003294 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003295
Sage Weil1ab86ae2009-09-29 18:38:44 -04003296 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003297 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003298 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003299 goto out_drop;
3300
3301 file->private_data = trans;
3302 return 0;
3303
3304out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003305 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003306 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003307out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003308 return ret;
3309}
3310
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003311static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3312{
Al Viro496ad9a2013-01-23 17:07:38 -05003313 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003314 struct btrfs_root *root = BTRFS_I(inode)->root;
3315 struct btrfs_root *new_root;
3316 struct btrfs_dir_item *di;
3317 struct btrfs_trans_handle *trans;
3318 struct btrfs_path *path;
3319 struct btrfs_key location;
3320 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003321 u64 objectid = 0;
3322 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003323 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003324
3325 if (!capable(CAP_SYS_ADMIN))
3326 return -EPERM;
3327
Miao Xie3c04ce02012-11-26 08:43:07 +00003328 ret = mnt_want_write_file(file);
3329 if (ret)
3330 return ret;
3331
3332 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3333 ret = -EFAULT;
3334 goto out;
3335 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003336
3337 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303338 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003339
3340 location.objectid = objectid;
3341 location.type = BTRFS_ROOT_ITEM_KEY;
3342 location.offset = (u64)-1;
3343
3344 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003345 if (IS_ERR(new_root)) {
3346 ret = PTR_ERR(new_root);
3347 goto out;
3348 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003349
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003350 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003351 if (!path) {
3352 ret = -ENOMEM;
3353 goto out;
3354 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003355 path->leave_spinning = 1;
3356
3357 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003358 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003359 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003360 ret = PTR_ERR(trans);
3361 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003362 }
3363
David Sterba6c417612011-04-13 15:41:04 +02003364 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003365 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3366 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003367 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003368 btrfs_free_path(path);
3369 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05003370 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3371 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003372 ret = -ENOENT;
3373 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003374 }
3375
3376 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3377 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3378 btrfs_mark_buffer_dirty(path->nodes[0]);
3379 btrfs_free_path(path);
3380
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003381 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003382 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003383out:
3384 mnt_drop_write_file(file);
3385 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003386}
3387
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003388void btrfs_get_block_group_info(struct list_head *groups_list,
3389 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003390{
3391 struct btrfs_block_group_cache *block_group;
3392
3393 space->total_bytes = 0;
3394 space->used_bytes = 0;
3395 space->flags = 0;
3396 list_for_each_entry(block_group, groups_list, list) {
3397 space->flags = block_group->flags;
3398 space->total_bytes += block_group->key.offset;
3399 space->used_bytes +=
3400 btrfs_block_group_used(&block_group->item);
3401 }
3402}
3403
Eric Sandeen48a3b632013-04-25 20:41:01 +00003404static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003405{
3406 struct btrfs_ioctl_space_args space_args;
3407 struct btrfs_ioctl_space_info space;
3408 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003409 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003410 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003411 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003412 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3413 BTRFS_BLOCK_GROUP_SYSTEM,
3414 BTRFS_BLOCK_GROUP_METADATA,
3415 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3416 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003417 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003418 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003419 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003420 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003421
3422 if (copy_from_user(&space_args,
3423 (struct btrfs_ioctl_space_args __user *)arg,
3424 sizeof(space_args)))
3425 return -EFAULT;
3426
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003427 for (i = 0; i < num_types; i++) {
3428 struct btrfs_space_info *tmp;
3429
3430 info = NULL;
3431 rcu_read_lock();
3432 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3433 list) {
3434 if (tmp->flags == types[i]) {
3435 info = tmp;
3436 break;
3437 }
3438 }
3439 rcu_read_unlock();
3440
3441 if (!info)
3442 continue;
3443
3444 down_read(&info->groups_sem);
3445 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3446 if (!list_empty(&info->block_groups[c]))
3447 slot_count++;
3448 }
3449 up_read(&info->groups_sem);
3450 }
Josef Bacik1406e432010-01-13 18:19:06 +00003451
Chris Mason7fde62b2010-03-16 15:40:10 -04003452 /* space_slots == 0 means they are asking for a count */
3453 if (space_args.space_slots == 0) {
3454 space_args.total_spaces = slot_count;
3455 goto out;
3456 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003457
Dan Rosenberg51788b12011-02-14 16:04:23 -05003458 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003459
Chris Mason7fde62b2010-03-16 15:40:10 -04003460 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003461
Chris Mason7fde62b2010-03-16 15:40:10 -04003462 /* we generally have at most 6 or so space infos, one for each raid
3463 * level. So, a whole page should be more than enough for everyone
3464 */
3465 if (alloc_size > PAGE_CACHE_SIZE)
3466 return -ENOMEM;
3467
3468 space_args.total_spaces = 0;
3469 dest = kmalloc(alloc_size, GFP_NOFS);
3470 if (!dest)
3471 return -ENOMEM;
3472 dest_orig = dest;
3473
3474 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003475 for (i = 0; i < num_types; i++) {
3476 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003477
Dan Rosenberg51788b12011-02-14 16:04:23 -05003478 if (!slot_count)
3479 break;
3480
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003481 info = NULL;
3482 rcu_read_lock();
3483 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3484 list) {
3485 if (tmp->flags == types[i]) {
3486 info = tmp;
3487 break;
3488 }
3489 }
3490 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003491
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003492 if (!info)
3493 continue;
3494 down_read(&info->groups_sem);
3495 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3496 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003497 btrfs_get_block_group_info(
3498 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003499 memcpy(dest, &space, sizeof(space));
3500 dest++;
3501 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003502 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003503 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003504 if (!slot_count)
3505 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003506 }
3507 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003508 }
Josef Bacik1406e432010-01-13 18:19:06 +00003509
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003510 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003511 (arg + sizeof(struct btrfs_ioctl_space_args));
3512
3513 if (copy_to_user(user_dest, dest_orig, alloc_size))
3514 ret = -EFAULT;
3515
3516 kfree(dest_orig);
3517out:
3518 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003519 ret = -EFAULT;
3520
3521 return ret;
3522}
3523
Jeff Mahoney01e219e2013-11-01 13:07:03 -04003524static long btrfs_ioctl_global_rsv(struct btrfs_root *root, void __user *arg)
3525{
3526 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv;
3527 u64 reserved;
3528
3529 spin_lock(&block_rsv->lock);
3530 reserved = block_rsv->reserved;
3531 spin_unlock(&block_rsv->lock);
3532
3533 if (arg && copy_to_user(arg, &reserved, sizeof(reserved)))
3534 return -EFAULT;
3535 return 0;
3536}
3537
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003538/*
3539 * there are many ways the trans_start and trans_end ioctls can lead
3540 * to deadlocks. They should only be used by applications that
3541 * basically own the machine, and have a very in depth understanding
3542 * of all the possible deadlocks and enospc problems.
3543 */
3544long btrfs_ioctl_trans_end(struct file *file)
3545{
Al Viro496ad9a2013-01-23 17:07:38 -05003546 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003547 struct btrfs_root *root = BTRFS_I(inode)->root;
3548 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003549
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003550 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003551 if (!trans)
3552 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003553 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003554
Sage Weil1ab86ae2009-09-29 18:38:44 -04003555 btrfs_end_transaction(trans, root);
3556
Josef Bacika4abeea2011-04-11 17:25:13 -04003557 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003558
Al Viro2a79f172011-12-09 08:06:57 -05003559 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003560 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003561}
3562
Miao Xie9a8c28b2012-11-26 08:40:43 +00003563static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3564 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003565{
Sage Weil46204592010-10-29 15:41:32 -04003566 struct btrfs_trans_handle *trans;
3567 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003568 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003569
Miao Xied4edf392013-02-20 09:17:06 +00003570 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003571 if (IS_ERR(trans)) {
3572 if (PTR_ERR(trans) != -ENOENT)
3573 return PTR_ERR(trans);
3574
3575 /* No running transaction, don't bother */
3576 transid = root->fs_info->last_trans_committed;
3577 goto out;
3578 }
Sage Weil46204592010-10-29 15:41:32 -04003579 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003580 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003581 if (ret) {
3582 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003583 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003584 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003585out:
Sage Weil46204592010-10-29 15:41:32 -04003586 if (argp)
3587 if (copy_to_user(argp, &transid, sizeof(transid)))
3588 return -EFAULT;
3589 return 0;
3590}
3591
Miao Xie9a8c28b2012-11-26 08:40:43 +00003592static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3593 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003594{
Sage Weil46204592010-10-29 15:41:32 -04003595 u64 transid;
3596
3597 if (argp) {
3598 if (copy_from_user(&transid, argp, sizeof(transid)))
3599 return -EFAULT;
3600 } else {
3601 transid = 0; /* current trans */
3602 }
3603 return btrfs_wait_for_commit(root, transid);
3604}
3605
Miao Xieb8e95482012-11-26 08:48:01 +00003606static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003607{
Al Viro496ad9a2013-01-23 17:07:38 -05003608 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003609 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003610 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003611
3612 if (!capable(CAP_SYS_ADMIN))
3613 return -EPERM;
3614
3615 sa = memdup_user(arg, sizeof(*sa));
3616 if (IS_ERR(sa))
3617 return PTR_ERR(sa);
3618
Miao Xieb8e95482012-11-26 08:48:01 +00003619 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3620 ret = mnt_want_write_file(file);
3621 if (ret)
3622 goto out;
3623 }
3624
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003625 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003626 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3627 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003628
3629 if (copy_to_user(arg, sa, sizeof(*sa)))
3630 ret = -EFAULT;
3631
Miao Xieb8e95482012-11-26 08:48:01 +00003632 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3633 mnt_drop_write_file(file);
3634out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003635 kfree(sa);
3636 return ret;
3637}
3638
3639static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3640{
3641 if (!capable(CAP_SYS_ADMIN))
3642 return -EPERM;
3643
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003644 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003645}
3646
3647static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3648 void __user *arg)
3649{
3650 struct btrfs_ioctl_scrub_args *sa;
3651 int ret;
3652
3653 if (!capable(CAP_SYS_ADMIN))
3654 return -EPERM;
3655
3656 sa = memdup_user(arg, sizeof(*sa));
3657 if (IS_ERR(sa))
3658 return PTR_ERR(sa);
3659
3660 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3661
3662 if (copy_to_user(arg, sa, sizeof(*sa)))
3663 ret = -EFAULT;
3664
3665 kfree(sa);
3666 return ret;
3667}
3668
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003669static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003670 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003671{
3672 struct btrfs_ioctl_get_dev_stats *sa;
3673 int ret;
3674
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003675 sa = memdup_user(arg, sizeof(*sa));
3676 if (IS_ERR(sa))
3677 return PTR_ERR(sa);
3678
David Sterbab27f7c02012-06-22 06:30:39 -06003679 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3680 kfree(sa);
3681 return -EPERM;
3682 }
3683
3684 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003685
3686 if (copy_to_user(arg, sa, sizeof(*sa)))
3687 ret = -EFAULT;
3688
3689 kfree(sa);
3690 return ret;
3691}
3692
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003693static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3694{
3695 struct btrfs_ioctl_dev_replace_args *p;
3696 int ret;
3697
3698 if (!capable(CAP_SYS_ADMIN))
3699 return -EPERM;
3700
3701 p = memdup_user(arg, sizeof(*p));
3702 if (IS_ERR(p))
3703 return PTR_ERR(p);
3704
3705 switch (p->cmd) {
3706 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003707 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3708 ret = -EROFS;
3709 goto out;
3710 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003711 if (atomic_xchg(
3712 &root->fs_info->mutually_exclusive_operation_running,
3713 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003714 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003715 } else {
3716 ret = btrfs_dev_replace_start(root, p);
3717 atomic_set(
3718 &root->fs_info->mutually_exclusive_operation_running,
3719 0);
3720 }
3721 break;
3722 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3723 btrfs_dev_replace_status(root->fs_info, p);
3724 ret = 0;
3725 break;
3726 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3727 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3728 break;
3729 default:
3730 ret = -EINVAL;
3731 break;
3732 }
3733
3734 if (copy_to_user(arg, p, sizeof(*p)))
3735 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003736out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003737 kfree(p);
3738 return ret;
3739}
3740
Jan Schmidtd7728c92011-07-07 16:48:38 +02003741static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3742{
3743 int ret = 0;
3744 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003745 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003746 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003747 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003748 struct inode_fs_paths *ipath = NULL;
3749 struct btrfs_path *path;
3750
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003751 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003752 return -EPERM;
3753
3754 path = btrfs_alloc_path();
3755 if (!path) {
3756 ret = -ENOMEM;
3757 goto out;
3758 }
3759
3760 ipa = memdup_user(arg, sizeof(*ipa));
3761 if (IS_ERR(ipa)) {
3762 ret = PTR_ERR(ipa);
3763 ipa = NULL;
3764 goto out;
3765 }
3766
3767 size = min_t(u32, ipa->size, 4096);
3768 ipath = init_ipath(size, root, path);
3769 if (IS_ERR(ipath)) {
3770 ret = PTR_ERR(ipath);
3771 ipath = NULL;
3772 goto out;
3773 }
3774
3775 ret = paths_from_inode(ipa->inum, ipath);
3776 if (ret < 0)
3777 goto out;
3778
3779 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003780 rel_ptr = ipath->fspath->val[i] -
3781 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003782 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003783 }
3784
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003785 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3786 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003787 if (ret) {
3788 ret = -EFAULT;
3789 goto out;
3790 }
3791
3792out:
3793 btrfs_free_path(path);
3794 free_ipath(ipath);
3795 kfree(ipa);
3796
3797 return ret;
3798}
3799
3800static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3801{
3802 struct btrfs_data_container *inodes = ctx;
3803 const size_t c = 3 * sizeof(u64);
3804
3805 if (inodes->bytes_left >= c) {
3806 inodes->bytes_left -= c;
3807 inodes->val[inodes->elem_cnt] = inum;
3808 inodes->val[inodes->elem_cnt + 1] = offset;
3809 inodes->val[inodes->elem_cnt + 2] = root;
3810 inodes->elem_cnt += 3;
3811 } else {
3812 inodes->bytes_missing += c - inodes->bytes_left;
3813 inodes->bytes_left = 0;
3814 inodes->elem_missed += 3;
3815 }
3816
3817 return 0;
3818}
3819
3820static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3821 void __user *arg)
3822{
3823 int ret = 0;
3824 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003825 struct btrfs_ioctl_logical_ino_args *loi;
3826 struct btrfs_data_container *inodes = NULL;
3827 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003828
3829 if (!capable(CAP_SYS_ADMIN))
3830 return -EPERM;
3831
3832 loi = memdup_user(arg, sizeof(*loi));
3833 if (IS_ERR(loi)) {
3834 ret = PTR_ERR(loi);
3835 loi = NULL;
3836 goto out;
3837 }
3838
3839 path = btrfs_alloc_path();
3840 if (!path) {
3841 ret = -ENOMEM;
3842 goto out;
3843 }
3844
Liu Bo425d17a2012-09-07 20:01:30 -06003845 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003846 inodes = init_data_container(size);
3847 if (IS_ERR(inodes)) {
3848 ret = PTR_ERR(inodes);
3849 inodes = NULL;
3850 goto out;
3851 }
3852
Liu Bodf031f02012-09-07 20:01:29 -06003853 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3854 build_ino_list, inodes);
3855 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003856 ret = -ENOENT;
3857 if (ret < 0)
3858 goto out;
3859
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003860 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3861 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003862 if (ret)
3863 ret = -EFAULT;
3864
3865out:
3866 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003867 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003868 kfree(loi);
3869
3870 return ret;
3871}
3872
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003873void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003874 struct btrfs_ioctl_balance_args *bargs)
3875{
3876 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3877
3878 bargs->flags = bctl->flags;
3879
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003880 if (atomic_read(&fs_info->balance_running))
3881 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3882 if (atomic_read(&fs_info->balance_pause_req))
3883 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003884 if (atomic_read(&fs_info->balance_cancel_req))
3885 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003886
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003887 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3888 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3889 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003890
3891 if (lock) {
3892 spin_lock(&fs_info->balance_lock);
3893 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3894 spin_unlock(&fs_info->balance_lock);
3895 } else {
3896 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3897 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003898}
3899
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003900static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003901{
Al Viro496ad9a2013-01-23 17:07:38 -05003902 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003903 struct btrfs_fs_info *fs_info = root->fs_info;
3904 struct btrfs_ioctl_balance_args *bargs;
3905 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003906 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003907 int ret;
3908
3909 if (!capable(CAP_SYS_ADMIN))
3910 return -EPERM;
3911
Liu Boe54bfa32012-06-29 03:58:48 -06003912 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003913 if (ret)
3914 return ret;
3915
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003916again:
3917 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3918 mutex_lock(&fs_info->volume_mutex);
3919 mutex_lock(&fs_info->balance_mutex);
3920 need_unlock = true;
3921 goto locked;
3922 }
3923
3924 /*
3925 * mut. excl. ops lock is locked. Three possibilites:
3926 * (1) some other op is running
3927 * (2) balance is running
3928 * (3) balance is paused -- special case (think resume)
3929 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003930 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003931 if (fs_info->balance_ctl) {
3932 /* this is either (2) or (3) */
3933 if (!atomic_read(&fs_info->balance_running)) {
3934 mutex_unlock(&fs_info->balance_mutex);
3935 if (!mutex_trylock(&fs_info->volume_mutex))
3936 goto again;
3937 mutex_lock(&fs_info->balance_mutex);
3938
3939 if (fs_info->balance_ctl &&
3940 !atomic_read(&fs_info->balance_running)) {
3941 /* this is (3) */
3942 need_unlock = false;
3943 goto locked;
3944 }
3945
3946 mutex_unlock(&fs_info->balance_mutex);
3947 mutex_unlock(&fs_info->volume_mutex);
3948 goto again;
3949 } else {
3950 /* this is (2) */
3951 mutex_unlock(&fs_info->balance_mutex);
3952 ret = -EINPROGRESS;
3953 goto out;
3954 }
3955 } else {
3956 /* this is (1) */
3957 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08003958 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003959 goto out;
3960 }
3961
3962locked:
3963 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003964
3965 if (arg) {
3966 bargs = memdup_user(arg, sizeof(*bargs));
3967 if (IS_ERR(bargs)) {
3968 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003969 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003970 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003971
3972 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3973 if (!fs_info->balance_ctl) {
3974 ret = -ENOTCONN;
3975 goto out_bargs;
3976 }
3977
3978 bctl = fs_info->balance_ctl;
3979 spin_lock(&fs_info->balance_lock);
3980 bctl->flags |= BTRFS_BALANCE_RESUME;
3981 spin_unlock(&fs_info->balance_lock);
3982
3983 goto do_balance;
3984 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003985 } else {
3986 bargs = NULL;
3987 }
3988
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003989 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003990 ret = -EINPROGRESS;
3991 goto out_bargs;
3992 }
3993
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003994 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3995 if (!bctl) {
3996 ret = -ENOMEM;
3997 goto out_bargs;
3998 }
3999
4000 bctl->fs_info = fs_info;
4001 if (arg) {
4002 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4003 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4004 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4005
4006 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004007 } else {
4008 /* balance everything - no filters */
4009 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004010 }
4011
Ilya Dryomovde322262012-01-16 22:04:49 +02004012do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004013 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004014 * Ownership of bctl and mutually_exclusive_operation_running
4015 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4016 * or, if restriper was paused all the way until unmount, in
4017 * free_fs_info. mutually_exclusive_operation_running is
4018 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004019 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004020 need_unlock = false;
4021
4022 ret = btrfs_balance(bctl, bargs);
4023
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004024 if (arg) {
4025 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4026 ret = -EFAULT;
4027 }
4028
4029out_bargs:
4030 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004031out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004032 mutex_unlock(&fs_info->balance_mutex);
4033 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004034 if (need_unlock)
4035 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4036out:
Liu Boe54bfa32012-06-29 03:58:48 -06004037 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004038 return ret;
4039}
4040
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004041static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4042{
4043 if (!capable(CAP_SYS_ADMIN))
4044 return -EPERM;
4045
4046 switch (cmd) {
4047 case BTRFS_BALANCE_CTL_PAUSE:
4048 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004049 case BTRFS_BALANCE_CTL_CANCEL:
4050 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004051 }
4052
4053 return -EINVAL;
4054}
4055
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004056static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4057 void __user *arg)
4058{
4059 struct btrfs_fs_info *fs_info = root->fs_info;
4060 struct btrfs_ioctl_balance_args *bargs;
4061 int ret = 0;
4062
4063 if (!capable(CAP_SYS_ADMIN))
4064 return -EPERM;
4065
4066 mutex_lock(&fs_info->balance_mutex);
4067 if (!fs_info->balance_ctl) {
4068 ret = -ENOTCONN;
4069 goto out;
4070 }
4071
4072 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4073 if (!bargs) {
4074 ret = -ENOMEM;
4075 goto out;
4076 }
4077
4078 update_ioctl_balance_args(fs_info, 1, bargs);
4079
4080 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4081 ret = -EFAULT;
4082
4083 kfree(bargs);
4084out:
4085 mutex_unlock(&fs_info->balance_mutex);
4086 return ret;
4087}
4088
Miao Xie905b0dd2012-11-26 08:50:11 +00004089static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004090{
Al Viro496ad9a2013-01-23 17:07:38 -05004091 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004092 struct btrfs_ioctl_quota_ctl_args *sa;
4093 struct btrfs_trans_handle *trans = NULL;
4094 int ret;
4095 int err;
4096
4097 if (!capable(CAP_SYS_ADMIN))
4098 return -EPERM;
4099
Miao Xie905b0dd2012-11-26 08:50:11 +00004100 ret = mnt_want_write_file(file);
4101 if (ret)
4102 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004103
4104 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004105 if (IS_ERR(sa)) {
4106 ret = PTR_ERR(sa);
4107 goto drop_write;
4108 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004109
Wang Shilong7708f022013-04-07 10:24:57 +00004110 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004111 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4112 if (IS_ERR(trans)) {
4113 ret = PTR_ERR(trans);
4114 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004115 }
4116
4117 switch (sa->cmd) {
4118 case BTRFS_QUOTA_CTL_ENABLE:
4119 ret = btrfs_quota_enable(trans, root->fs_info);
4120 break;
4121 case BTRFS_QUOTA_CTL_DISABLE:
4122 ret = btrfs_quota_disable(trans, root->fs_info);
4123 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004124 default:
4125 ret = -EINVAL;
4126 break;
4127 }
4128
Jan Schmidt2f232032013-04-25 16:04:51 +00004129 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4130 if (err && !ret)
4131 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004132out:
4133 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004134 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004135drop_write:
4136 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004137 return ret;
4138}
4139
Miao Xie905b0dd2012-11-26 08:50:11 +00004140static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004141{
Al Viro496ad9a2013-01-23 17:07:38 -05004142 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004143 struct btrfs_ioctl_qgroup_assign_args *sa;
4144 struct btrfs_trans_handle *trans;
4145 int ret;
4146 int err;
4147
4148 if (!capable(CAP_SYS_ADMIN))
4149 return -EPERM;
4150
Miao Xie905b0dd2012-11-26 08:50:11 +00004151 ret = mnt_want_write_file(file);
4152 if (ret)
4153 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004154
4155 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004156 if (IS_ERR(sa)) {
4157 ret = PTR_ERR(sa);
4158 goto drop_write;
4159 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004160
4161 trans = btrfs_join_transaction(root);
4162 if (IS_ERR(trans)) {
4163 ret = PTR_ERR(trans);
4164 goto out;
4165 }
4166
4167 /* FIXME: check if the IDs really exist */
4168 if (sa->assign) {
4169 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4170 sa->src, sa->dst);
4171 } else {
4172 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4173 sa->src, sa->dst);
4174 }
4175
4176 err = btrfs_end_transaction(trans, root);
4177 if (err && !ret)
4178 ret = err;
4179
4180out:
4181 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004182drop_write:
4183 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004184 return ret;
4185}
4186
Miao Xie905b0dd2012-11-26 08:50:11 +00004187static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004188{
Al Viro496ad9a2013-01-23 17:07:38 -05004189 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004190 struct btrfs_ioctl_qgroup_create_args *sa;
4191 struct btrfs_trans_handle *trans;
4192 int ret;
4193 int err;
4194
4195 if (!capable(CAP_SYS_ADMIN))
4196 return -EPERM;
4197
Miao Xie905b0dd2012-11-26 08:50:11 +00004198 ret = mnt_want_write_file(file);
4199 if (ret)
4200 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004201
4202 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004203 if (IS_ERR(sa)) {
4204 ret = PTR_ERR(sa);
4205 goto drop_write;
4206 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004207
Miao Xied86e56c2012-11-15 11:35:41 +00004208 if (!sa->qgroupid) {
4209 ret = -EINVAL;
4210 goto out;
4211 }
4212
Arne Jansen5d13a372011-09-14 15:53:51 +02004213 trans = btrfs_join_transaction(root);
4214 if (IS_ERR(trans)) {
4215 ret = PTR_ERR(trans);
4216 goto out;
4217 }
4218
4219 /* FIXME: check if the IDs really exist */
4220 if (sa->create) {
4221 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4222 NULL);
4223 } else {
4224 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4225 }
4226
4227 err = btrfs_end_transaction(trans, root);
4228 if (err && !ret)
4229 ret = err;
4230
4231out:
4232 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004233drop_write:
4234 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004235 return ret;
4236}
4237
Miao Xie905b0dd2012-11-26 08:50:11 +00004238static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004239{
Al Viro496ad9a2013-01-23 17:07:38 -05004240 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004241 struct btrfs_ioctl_qgroup_limit_args *sa;
4242 struct btrfs_trans_handle *trans;
4243 int ret;
4244 int err;
4245 u64 qgroupid;
4246
4247 if (!capable(CAP_SYS_ADMIN))
4248 return -EPERM;
4249
Miao Xie905b0dd2012-11-26 08:50:11 +00004250 ret = mnt_want_write_file(file);
4251 if (ret)
4252 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004253
4254 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004255 if (IS_ERR(sa)) {
4256 ret = PTR_ERR(sa);
4257 goto drop_write;
4258 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004259
4260 trans = btrfs_join_transaction(root);
4261 if (IS_ERR(trans)) {
4262 ret = PTR_ERR(trans);
4263 goto out;
4264 }
4265
4266 qgroupid = sa->qgroupid;
4267 if (!qgroupid) {
4268 /* take the current subvol as qgroup */
4269 qgroupid = root->root_key.objectid;
4270 }
4271
4272 /* FIXME: check if the IDs really exist */
4273 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4274
4275 err = btrfs_end_transaction(trans, root);
4276 if (err && !ret)
4277 ret = err;
4278
4279out:
4280 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004281drop_write:
4282 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004283 return ret;
4284}
4285
Jan Schmidt2f232032013-04-25 16:04:51 +00004286static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4287{
Al Viro6d0379e2013-06-16 19:32:35 +04004288 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004289 struct btrfs_ioctl_quota_rescan_args *qsa;
4290 int ret;
4291
4292 if (!capable(CAP_SYS_ADMIN))
4293 return -EPERM;
4294
4295 ret = mnt_want_write_file(file);
4296 if (ret)
4297 return ret;
4298
4299 qsa = memdup_user(arg, sizeof(*qsa));
4300 if (IS_ERR(qsa)) {
4301 ret = PTR_ERR(qsa);
4302 goto drop_write;
4303 }
4304
4305 if (qsa->flags) {
4306 ret = -EINVAL;
4307 goto out;
4308 }
4309
4310 ret = btrfs_qgroup_rescan(root->fs_info);
4311
4312out:
4313 kfree(qsa);
4314drop_write:
4315 mnt_drop_write_file(file);
4316 return ret;
4317}
4318
4319static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4320{
Al Viro6d0379e2013-06-16 19:32:35 +04004321 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004322 struct btrfs_ioctl_quota_rescan_args *qsa;
4323 int ret = 0;
4324
4325 if (!capable(CAP_SYS_ADMIN))
4326 return -EPERM;
4327
4328 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4329 if (!qsa)
4330 return -ENOMEM;
4331
4332 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4333 qsa->flags = 1;
4334 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4335 }
4336
4337 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4338 ret = -EFAULT;
4339
4340 kfree(qsa);
4341 return ret;
4342}
4343
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004344static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4345{
Al Viro54563d42013-09-01 15:57:51 -04004346 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004347
4348 if (!capable(CAP_SYS_ADMIN))
4349 return -EPERM;
4350
4351 return btrfs_qgroup_wait_for_completion(root->fs_info);
4352}
4353
Alexander Block8ea05e32012-07-25 17:35:53 +02004354static long btrfs_ioctl_set_received_subvol(struct file *file,
4355 void __user *arg)
4356{
4357 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004358 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004359 struct btrfs_root *root = BTRFS_I(inode)->root;
4360 struct btrfs_root_item *root_item = &root->root_item;
4361 struct btrfs_trans_handle *trans;
4362 struct timespec ct = CURRENT_TIME;
4363 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004364 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004365
4366 ret = mnt_want_write_file(file);
4367 if (ret < 0)
4368 return ret;
4369
4370 down_write(&root->fs_info->subvol_sem);
4371
4372 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4373 ret = -EINVAL;
4374 goto out;
4375 }
4376
4377 if (btrfs_root_readonly(root)) {
4378 ret = -EROFS;
4379 goto out;
4380 }
4381
4382 if (!inode_owner_or_capable(inode)) {
4383 ret = -EACCES;
4384 goto out;
4385 }
4386
4387 sa = memdup_user(arg, sizeof(*sa));
4388 if (IS_ERR(sa)) {
4389 ret = PTR_ERR(sa);
4390 sa = NULL;
4391 goto out;
4392 }
4393
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004394 /*
4395 * 1 - root item
4396 * 2 - uuid items (received uuid + subvol uuid)
4397 */
4398 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004399 if (IS_ERR(trans)) {
4400 ret = PTR_ERR(trans);
4401 trans = NULL;
4402 goto out;
4403 }
4404
4405 sa->rtransid = trans->transid;
4406 sa->rtime.sec = ct.tv_sec;
4407 sa->rtime.nsec = ct.tv_nsec;
4408
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004409 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4410 BTRFS_UUID_SIZE);
4411 if (received_uuid_changed &&
4412 !btrfs_is_empty_uuid(root_item->received_uuid))
4413 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4414 root_item->received_uuid,
4415 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4416 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004417 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4418 btrfs_set_root_stransid(root_item, sa->stransid);
4419 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004420 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4421 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4422 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4423 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004424
4425 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4426 &root->root_key, &root->root_item);
4427 if (ret < 0) {
4428 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004429 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004430 }
4431 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4432 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4433 sa->uuid,
4434 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4435 root->root_key.objectid);
4436 if (ret < 0 && ret != -EEXIST) {
4437 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004438 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004439 }
4440 }
4441 ret = btrfs_commit_transaction(trans, root);
4442 if (ret < 0) {
4443 btrfs_abort_transaction(trans, root, ret);
4444 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004445 }
4446
4447 ret = copy_to_user(arg, sa, sizeof(*sa));
4448 if (ret)
4449 ret = -EFAULT;
4450
4451out:
4452 kfree(sa);
4453 up_write(&root->fs_info->subvol_sem);
4454 mnt_drop_write_file(file);
4455 return ret;
4456}
4457
jeff.liu867ab662013-01-05 02:48:01 +00004458static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4459{
Al Viro6d0379e2013-06-16 19:32:35 +04004460 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004461 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004462 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004463 char label[BTRFS_LABEL_SIZE];
4464
4465 spin_lock(&root->fs_info->super_lock);
4466 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4467 spin_unlock(&root->fs_info->super_lock);
4468
4469 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004470
4471 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004472 btrfs_warn(root->fs_info,
4473 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00004474 }
4475
jeff.liu867ab662013-01-05 02:48:01 +00004476 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004477
4478 return ret ? -EFAULT : 0;
4479}
4480
jeff.liua8bfd4a2013-01-05 02:48:08 +00004481static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4482{
Al Viro6d0379e2013-06-16 19:32:35 +04004483 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004484 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4485 struct btrfs_trans_handle *trans;
4486 char label[BTRFS_LABEL_SIZE];
4487 int ret;
4488
4489 if (!capable(CAP_SYS_ADMIN))
4490 return -EPERM;
4491
4492 if (copy_from_user(label, arg, sizeof(label)))
4493 return -EFAULT;
4494
4495 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004496 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00004497 BTRFS_LABEL_SIZE - 1);
4498 return -EINVAL;
4499 }
4500
4501 ret = mnt_want_write_file(file);
4502 if (ret)
4503 return ret;
4504
jeff.liua8bfd4a2013-01-05 02:48:08 +00004505 trans = btrfs_start_transaction(root, 0);
4506 if (IS_ERR(trans)) {
4507 ret = PTR_ERR(trans);
4508 goto out_unlock;
4509 }
4510
Anand Jaina1b83ac2013-07-19 17:39:32 +08004511 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004512 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004513 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004514 ret = btrfs_end_transaction(trans, root);
4515
4516out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004517 mnt_drop_write_file(file);
4518 return ret;
4519}
4520
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004521#define INIT_FEATURE_FLAGS(suffix) \
4522 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4523 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4524 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4525
4526static int btrfs_ioctl_get_supported_features(struct file *file,
4527 void __user *arg)
4528{
4529 static struct btrfs_ioctl_feature_flags features[3] = {
4530 INIT_FEATURE_FLAGS(SUPP),
4531 INIT_FEATURE_FLAGS(SAFE_SET),
4532 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4533 };
4534
4535 if (copy_to_user(arg, &features, sizeof(features)))
4536 return -EFAULT;
4537
4538 return 0;
4539}
4540
4541static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
4542{
4543 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4544 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4545 struct btrfs_ioctl_feature_flags features;
4546
4547 features.compat_flags = btrfs_super_compat_flags(super_block);
4548 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4549 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4550
4551 if (copy_to_user(arg, &features, sizeof(features)))
4552 return -EFAULT;
4553
4554 return 0;
4555}
4556
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004557static int check_feature_bits(struct btrfs_root *root,
4558 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004559 u64 change_mask, u64 flags, u64 supported_flags,
4560 u64 safe_set, u64 safe_clear)
4561{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004562 const char *type = btrfs_feature_set_names[set];
4563 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004564 u64 disallowed, unsupported;
4565 u64 set_mask = flags & change_mask;
4566 u64 clear_mask = ~flags & change_mask;
4567
4568 unsupported = set_mask & ~supported_flags;
4569 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004570 names = btrfs_printable_features(set, unsupported);
4571 if (names) {
4572 btrfs_warn(root->fs_info,
4573 "this kernel does not support the %s feature bit%s",
4574 names, strchr(names, ',') ? "s" : "");
4575 kfree(names);
4576 } else
4577 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004578 "this kernel does not support %s bits 0x%llx",
4579 type, unsupported);
4580 return -EOPNOTSUPP;
4581 }
4582
4583 disallowed = set_mask & ~safe_set;
4584 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004585 names = btrfs_printable_features(set, disallowed);
4586 if (names) {
4587 btrfs_warn(root->fs_info,
4588 "can't set the %s feature bit%s while mounted",
4589 names, strchr(names, ',') ? "s" : "");
4590 kfree(names);
4591 } else
4592 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004593 "can't set %s bits 0x%llx while mounted",
4594 type, disallowed);
4595 return -EPERM;
4596 }
4597
4598 disallowed = clear_mask & ~safe_clear;
4599 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004600 names = btrfs_printable_features(set, disallowed);
4601 if (names) {
4602 btrfs_warn(root->fs_info,
4603 "can't clear the %s feature bit%s while mounted",
4604 names, strchr(names, ',') ? "s" : "");
4605 kfree(names);
4606 } else
4607 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004608 "can't clear %s bits 0x%llx while mounted",
4609 type, disallowed);
4610 return -EPERM;
4611 }
4612
4613 return 0;
4614}
4615
4616#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004617check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004618 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4619 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4620 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4621
4622static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4623{
4624 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4625 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4626 struct btrfs_ioctl_feature_flags flags[2];
4627 struct btrfs_trans_handle *trans;
4628 u64 newflags;
4629 int ret;
4630
4631 if (!capable(CAP_SYS_ADMIN))
4632 return -EPERM;
4633
4634 if (copy_from_user(flags, arg, sizeof(flags)))
4635 return -EFAULT;
4636
4637 /* Nothing to do */
4638 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4639 !flags[0].incompat_flags)
4640 return 0;
4641
4642 ret = check_feature(root, flags[0].compat_flags,
4643 flags[1].compat_flags, COMPAT);
4644 if (ret)
4645 return ret;
4646
4647 ret = check_feature(root, flags[0].compat_ro_flags,
4648 flags[1].compat_ro_flags, COMPAT_RO);
4649 if (ret)
4650 return ret;
4651
4652 ret = check_feature(root, flags[0].incompat_flags,
4653 flags[1].incompat_flags, INCOMPAT);
4654 if (ret)
4655 return ret;
4656
4657 trans = btrfs_start_transaction(root, 1);
4658 if (IS_ERR(trans))
4659 return PTR_ERR(trans);
4660
4661 spin_lock(&root->fs_info->super_lock);
4662 newflags = btrfs_super_compat_flags(super_block);
4663 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4664 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4665 btrfs_set_super_compat_flags(super_block, newflags);
4666
4667 newflags = btrfs_super_compat_ro_flags(super_block);
4668 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4669 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4670 btrfs_set_super_compat_ro_flags(super_block, newflags);
4671
4672 newflags = btrfs_super_incompat_flags(super_block);
4673 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4674 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4675 btrfs_set_super_incompat_flags(super_block, newflags);
4676 spin_unlock(&root->fs_info->super_lock);
4677
4678 return btrfs_end_transaction(trans, root);
4679}
4680
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004681long btrfs_ioctl(struct file *file, unsigned int
4682 cmd, unsigned long arg)
4683{
Al Viro496ad9a2013-01-23 17:07:38 -05004684 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004685 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004686
4687 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004688 case FS_IOC_GETFLAGS:
4689 return btrfs_ioctl_getflags(file, argp);
4690 case FS_IOC_SETFLAGS:
4691 return btrfs_ioctl_setflags(file, argp);
4692 case FS_IOC_GETVERSION:
4693 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004694 case FITRIM:
4695 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004696 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004697 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004698 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004699 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004700 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004701 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004702 case BTRFS_IOC_SUBVOL_CREATE_V2:
4703 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004704 case BTRFS_IOC_SNAP_DESTROY:
4705 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004706 case BTRFS_IOC_SUBVOL_GETFLAGS:
4707 return btrfs_ioctl_subvol_getflags(file, argp);
4708 case BTRFS_IOC_SUBVOL_SETFLAGS:
4709 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004710 case BTRFS_IOC_DEFAULT_SUBVOL:
4711 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004712 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004713 return btrfs_ioctl_defrag(file, NULL);
4714 case BTRFS_IOC_DEFRAG_RANGE:
4715 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004716 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004717 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004718 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004719 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004720 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004721 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004722 case BTRFS_IOC_FS_INFO:
4723 return btrfs_ioctl_fs_info(root, argp);
4724 case BTRFS_IOC_DEV_INFO:
4725 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004726 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004727 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004728 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004729 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4730 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004731 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004732 case BTRFS_IOC_TRANS_START:
4733 return btrfs_ioctl_trans_start(file);
4734 case BTRFS_IOC_TRANS_END:
4735 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004736 case BTRFS_IOC_TREE_SEARCH:
4737 return btrfs_ioctl_tree_search(file, argp);
4738 case BTRFS_IOC_INO_LOOKUP:
4739 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004740 case BTRFS_IOC_INO_PATHS:
4741 return btrfs_ioctl_ino_to_path(root, argp);
4742 case BTRFS_IOC_LOGICAL_INO:
4743 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004744 case BTRFS_IOC_SPACE_INFO:
4745 return btrfs_ioctl_space_info(root, argp);
Jeff Mahoney01e219e2013-11-01 13:07:03 -04004746 case BTRFS_IOC_GLOBAL_RSV:
4747 return btrfs_ioctl_global_rsv(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004748 case BTRFS_IOC_SYNC: {
4749 int ret;
4750
Miao Xie91aef862013-11-04 23:13:26 +08004751 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004752 if (ret)
4753 return ret;
4754 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4755 return ret;
4756 }
Sage Weil46204592010-10-29 15:41:32 -04004757 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004758 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004759 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004760 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004761 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004762 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004763 case BTRFS_IOC_SCRUB_CANCEL:
4764 return btrfs_ioctl_scrub_cancel(root, argp);
4765 case BTRFS_IOC_SCRUB_PROGRESS:
4766 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004767 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004768 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004769 case BTRFS_IOC_BALANCE_CTL:
4770 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004771 case BTRFS_IOC_BALANCE_PROGRESS:
4772 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004773 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4774 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004775 case BTRFS_IOC_SEND:
4776 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004777 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004778 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004779 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004780 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004781 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004782 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004783 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004784 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004785 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004786 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004787 case BTRFS_IOC_QUOTA_RESCAN:
4788 return btrfs_ioctl_quota_rescan(file, argp);
4789 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4790 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004791 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4792 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004793 case BTRFS_IOC_DEV_REPLACE:
4794 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004795 case BTRFS_IOC_GET_FSLABEL:
4796 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004797 case BTRFS_IOC_SET_FSLABEL:
4798 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004799 case BTRFS_IOC_FILE_EXTENT_SAME:
4800 return btrfs_ioctl_file_extent_same(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004801 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4802 return btrfs_ioctl_get_supported_features(file, argp);
4803 case BTRFS_IOC_GET_FEATURES:
4804 return btrfs_ioctl_get_features(file, argp);
4805 case BTRFS_IOC_SET_FEATURES:
4806 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004807 }
4808
4809 return -ENOTTY;
4810}