blob: 076cc7fff813e5ffd23094913ce1fb8ac7a6eb6f [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Alexander Block8ea05e32012-07-25 17:35:53 +020044#include <linux/uuid.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000045#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070046#include <linux/uaccess.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050047#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040048#include "ctree.h"
49#include "disk-io.h"
50#include "transaction.h"
51#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052#include "print-tree.h"
53#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040054#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080055#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020056#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040057#include "rcu-string.h"
Alexander Block31db9f72012-07-25 23:19:24 +020058#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010059#include "dev-replace.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040060
Mark Fasheh416161d2013-08-06 11:42:51 -070061static int btrfs_clone(struct inode *src, struct inode *inode,
62 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
63
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/* Mask out flags that are inappropriate for the given type of inode. */
65static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
66{
67 if (S_ISDIR(mode))
68 return flags;
69 else if (S_ISREG(mode))
70 return flags & ~FS_DIRSYNC_FL;
71 else
72 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
73}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040074
Christoph Hellwig6cbff002009-04-17 10:37:41 +020075/*
76 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
77 */
78static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
79{
80 unsigned int iflags = 0;
81
82 if (flags & BTRFS_INODE_SYNC)
83 iflags |= FS_SYNC_FL;
84 if (flags & BTRFS_INODE_IMMUTABLE)
85 iflags |= FS_IMMUTABLE_FL;
86 if (flags & BTRFS_INODE_APPEND)
87 iflags |= FS_APPEND_FL;
88 if (flags & BTRFS_INODE_NODUMP)
89 iflags |= FS_NODUMP_FL;
90 if (flags & BTRFS_INODE_NOATIME)
91 iflags |= FS_NOATIME_FL;
92 if (flags & BTRFS_INODE_DIRSYNC)
93 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000094 if (flags & BTRFS_INODE_NODATACOW)
95 iflags |= FS_NOCOW_FL;
96
97 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
98 iflags |= FS_COMPR_FL;
99 else if (flags & BTRFS_INODE_NOCOMPRESS)
100 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200101
102 return iflags;
103}
104
105/*
106 * Update inode->i_flags based on the btrfs internal flags.
107 */
108void btrfs_update_iflags(struct inode *inode)
109{
110 struct btrfs_inode *ip = BTRFS_I(inode);
111
112 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
113
114 if (ip->flags & BTRFS_INODE_SYNC)
115 inode->i_flags |= S_SYNC;
116 if (ip->flags & BTRFS_INODE_IMMUTABLE)
117 inode->i_flags |= S_IMMUTABLE;
118 if (ip->flags & BTRFS_INODE_APPEND)
119 inode->i_flags |= S_APPEND;
120 if (ip->flags & BTRFS_INODE_NOATIME)
121 inode->i_flags |= S_NOATIME;
122 if (ip->flags & BTRFS_INODE_DIRSYNC)
123 inode->i_flags |= S_DIRSYNC;
124}
125
126/*
127 * Inherit flags from the parent inode.
128 *
Josef Bacike27425d2011-09-27 11:01:30 -0400129 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200130 */
131void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
132{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400133 unsigned int flags;
134
135 if (!dir)
136 return;
137
138 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NOCOMPRESS) {
141 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
143 } else if (flags & BTRFS_INODE_COMPRESS) {
144 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
145 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
146 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200147
Liu Bo213490b2012-09-11 08:33:50 -0600148 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400149 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600150 if (S_ISREG(inode->i_mode))
151 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
152 }
Josef Bacike27425d2011-09-27 11:01:30 -0400153
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200154 btrfs_update_iflags(inode);
155}
156
157static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
158{
Al Viro496ad9a2013-01-23 17:07:38 -0500159 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
161
162 if (copy_to_user(arg, &flags, sizeof(flags)))
163 return -EFAULT;
164 return 0;
165}
166
Liu Bo75e7cb72011-03-22 10:12:20 +0000167static int check_flags(unsigned int flags)
168{
169 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
170 FS_NOATIME_FL | FS_NODUMP_FL | \
171 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000172 FS_NOCOMP_FL | FS_COMPR_FL |
173 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000174 return -EOPNOTSUPP;
175
176 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
177 return -EINVAL;
178
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 return 0;
180}
181
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200182static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
183{
Al Viro496ad9a2013-01-23 17:07:38 -0500184 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 struct btrfs_inode *ip = BTRFS_I(inode);
186 struct btrfs_root *root = ip->root;
187 struct btrfs_trans_handle *trans;
188 unsigned int flags, oldflags;
189 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800190 u64 ip_oldflags;
191 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600192 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193
Li Zefanb83cc962010-12-20 16:04:08 +0800194 if (btrfs_root_readonly(root))
195 return -EROFS;
196
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200197 if (copy_from_user(&flags, arg, sizeof(flags)))
198 return -EFAULT;
199
Liu Bo75e7cb72011-03-22 10:12:20 +0000200 ret = check_flags(flags);
201 if (ret)
202 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200203
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700204 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 return -EACCES;
206
Jan Karae7848682012-06-12 16:20:32 +0200207 ret = mnt_want_write_file(file);
208 if (ret)
209 return ret;
210
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200211 mutex_lock(&inode->i_mutex);
212
Li Zefanf062abf2011-12-29 13:36:45 +0800213 ip_oldflags = ip->flags;
214 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600215 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800216
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200217 flags = btrfs_mask_flags(inode->i_mode, flags);
218 oldflags = btrfs_flags_to_ioctl(ip->flags);
219 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
220 if (!capable(CAP_LINUX_IMMUTABLE)) {
221 ret = -EPERM;
222 goto out_unlock;
223 }
224 }
225
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200226 if (flags & FS_SYNC_FL)
227 ip->flags |= BTRFS_INODE_SYNC;
228 else
229 ip->flags &= ~BTRFS_INODE_SYNC;
230 if (flags & FS_IMMUTABLE_FL)
231 ip->flags |= BTRFS_INODE_IMMUTABLE;
232 else
233 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
234 if (flags & FS_APPEND_FL)
235 ip->flags |= BTRFS_INODE_APPEND;
236 else
237 ip->flags &= ~BTRFS_INODE_APPEND;
238 if (flags & FS_NODUMP_FL)
239 ip->flags |= BTRFS_INODE_NODUMP;
240 else
241 ip->flags &= ~BTRFS_INODE_NODUMP;
242 if (flags & FS_NOATIME_FL)
243 ip->flags |= BTRFS_INODE_NOATIME;
244 else
245 ip->flags &= ~BTRFS_INODE_NOATIME;
246 if (flags & FS_DIRSYNC_FL)
247 ip->flags |= BTRFS_INODE_DIRSYNC;
248 else
249 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600250 if (flags & FS_NOCOW_FL) {
251 if (S_ISREG(mode)) {
252 /*
253 * It's safe to turn csums off here, no extents exist.
254 * Otherwise we want the flag to reflect the real COW
255 * status of the file and will not set it.
256 */
257 if (inode->i_size == 0)
258 ip->flags |= BTRFS_INODE_NODATACOW
259 | BTRFS_INODE_NODATASUM;
260 } else {
261 ip->flags |= BTRFS_INODE_NODATACOW;
262 }
263 } else {
264 /*
265 * Revert back under same assuptions as above
266 */
267 if (S_ISREG(mode)) {
268 if (inode->i_size == 0)
269 ip->flags &= ~(BTRFS_INODE_NODATACOW
270 | BTRFS_INODE_NODATASUM);
271 } else {
272 ip->flags &= ~BTRFS_INODE_NODATACOW;
273 }
274 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275
Liu Bo75e7cb72011-03-22 10:12:20 +0000276 /*
277 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
278 * flag may be changed automatically if compression code won't make
279 * things smaller.
280 */
281 if (flags & FS_NOCOMP_FL) {
282 ip->flags &= ~BTRFS_INODE_COMPRESS;
283 ip->flags |= BTRFS_INODE_NOCOMPRESS;
284 } else if (flags & FS_COMPR_FL) {
285 ip->flags |= BTRFS_INODE_COMPRESS;
286 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000287 } else {
288 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000289 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200290
Li Zefan4da6f1a2011-12-29 13:39:50 +0800291 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
295 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200296
Li Zefan306424cc2011-12-14 20:12:02 -0500297 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400298 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500299 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800303 out_drop:
304 if (ret) {
305 ip->flags = ip_oldflags;
306 inode->i_flags = i_oldflags;
307 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200308
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200309 out_unlock:
310 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200311 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000312 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200313}
314
315static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
316{
Al Viro496ad9a2013-01-23 17:07:38 -0500317 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200318
319 return put_user(inode->i_generation, arg);
320}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400321
Li Dongyangf7039b12011-03-24 10:24:28 +0000322static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
323{
Al Viro815745c2011-11-17 15:40:49 -0500324 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 struct btrfs_device *device;
326 struct request_queue *q;
327 struct fstrim_range range;
328 u64 minlen = ULLONG_MAX;
329 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500330 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000331 int ret;
332
333 if (!capable(CAP_SYS_ADMIN))
334 return -EPERM;
335
Xiao Guangrong1f781602011-04-20 10:09:16 +0000336 rcu_read_lock();
337 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
338 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000339 if (!device->bdev)
340 continue;
341 q = bdev_get_queue(device->bdev);
342 if (blk_queue_discard(q)) {
343 num_devices++;
344 minlen = min((u64)q->limits.discard_granularity,
345 minlen);
346 }
347 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000348 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200349
Li Dongyangf7039b12011-03-24 10:24:28 +0000350 if (!num_devices)
351 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000352 if (copy_from_user(&range, arg, sizeof(range)))
353 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000354 if (range.start > total_bytes ||
355 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200356 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000357
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200358 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000359 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500360 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000361 if (ret < 0)
362 return ret;
363
364 if (copy_to_user(arg, &range, sizeof(range)))
365 return -EFAULT;
366
367 return 0;
368}
369
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200370int btrfs_is_empty_uuid(u8 *uuid)
371{
372 static char empty_uuid[BTRFS_UUID_SIZE] = {0};
373
374 return !memcmp(uuid, empty_uuid, BTRFS_UUID_SIZE);
375}
376
Miao Xied5c12072013-02-28 10:04:33 +0000377static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400378 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400379 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200380 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000381 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382{
383 struct btrfs_trans_handle *trans;
384 struct btrfs_key key;
385 struct btrfs_root_item root_item;
386 struct btrfs_inode_item *inode_item;
387 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000388 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400389 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000390 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200391 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392 int ret;
393 int err;
394 u64 objectid;
395 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500396 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000397 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200398 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400399
Li Zefan581bb052011-04-20 10:06:11 +0800400 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400401 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400402 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000403
Miao Xied5c12072013-02-28 10:04:33 +0000404 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400405 /*
Miao Xied5c12072013-02-28 10:04:33 +0000406 * The same as the snapshot creation, please see the comment
407 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400408 */
Miao Xied5c12072013-02-28 10:04:33 +0000409 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200410 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000411 if (ret)
412 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413
Miao Xied5c12072013-02-28 10:04:33 +0000414 trans = btrfs_start_transaction(root, 0);
415 if (IS_ERR(trans)) {
416 ret = PTR_ERR(trans);
417 goto out;
418 }
419 trans->block_rsv = &block_rsv;
420 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421
Miao Xie8696c532013-02-07 06:02:44 +0000422 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200423 if (ret)
424 goto fail;
425
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400426 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200427 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400428 if (IS_ERR(leaf)) {
429 ret = PTR_ERR(leaf);
430 goto fail;
431 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400432
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400433 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 btrfs_set_header_bytenr(leaf, leaf->start);
435 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437 btrfs_set_header_owner(leaf, objectid);
438
439 write_extent_buffer(leaf, root->fs_info->fsid,
440 (unsigned long)btrfs_header_fsid(leaf),
441 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400442 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
443 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
444 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445 btrfs_mark_buffer_dirty(leaf);
446
Alexander Block8ea05e32012-07-25 17:35:53 +0200447 memset(&root_item, 0, sizeof(root_item));
448
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800450 btrfs_set_stack_inode_generation(inode_item, 1);
451 btrfs_set_stack_inode_size(inode_item, 3);
452 btrfs_set_stack_inode_nlink(inode_item, 1);
453 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
454 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455
Qu Wenruo3cae2102013-07-16 11:19:18 +0800456 btrfs_set_root_flags(&root_item, 0);
457 btrfs_set_root_limit(&root_item, 0);
458 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000459
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400460 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400461 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400462 btrfs_set_root_level(&root_item, 0);
463 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000464 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400465 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400466
Alexander Block8ea05e32012-07-25 17:35:53 +0200467 btrfs_set_root_generation_v2(&root_item,
468 btrfs_root_generation(&root_item));
469 uuid_le_gen(&new_uuid);
470 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800471 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
472 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200473 root_item.ctime = root_item.otime;
474 btrfs_set_root_ctransid(&root_item, trans->transid);
475 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400476
Chris Mason925baed2008-06-25 16:01:30 -0400477 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478 free_extent_buffer(leaf);
479 leaf = NULL;
480
481 btrfs_set_root_dirid(&root_item, new_dirid);
482
483 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400484 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400485 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
486 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
487 &root_item);
488 if (ret)
489 goto fail;
490
Yan, Zheng76dda932009-09-21 16:00:26 -0400491 key.offset = (u64)-1;
492 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100493 if (IS_ERR(new_root)) {
494 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
495 ret = PTR_ERR(new_root);
496 goto fail;
497 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400498
499 btrfs_record_root_in_trans(trans, new_root);
500
Josef Bacikd82a6f12011-05-11 15:26:06 -0400501 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700502 if (ret) {
503 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100504 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700505 goto fail;
506 }
507
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508 /*
509 * insert the directory item
510 */
Chris Mason3de45862008-11-17 21:02:50 -0500511 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100512 if (ret) {
513 btrfs_abort_transaction(trans, root, ret);
514 goto fail;
515 }
Chris Mason3de45862008-11-17 21:02:50 -0500516
517 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800518 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500519 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100520 if (ret) {
521 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400522 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100523 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500524
Yan Zheng52c26172009-01-05 15:43:43 -0500525 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
526 ret = btrfs_update_inode(trans, root, dir);
527 BUG_ON(ret);
528
Chris Mason0660b5a2008-11-17 20:37:39 -0500529 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400530 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800531 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500532 BUG_ON(ret);
533
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200534 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
535 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
536 objectid);
537 if (ret)
538 btrfs_abort_transaction(trans, root, ret);
539
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400540fail:
Miao Xied5c12072013-02-28 10:04:33 +0000541 trans->block_rsv = NULL;
542 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400543 if (async_transid) {
544 *async_transid = trans->transid;
545 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000546 if (err)
547 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400548 } else {
549 err = btrfs_commit_transaction(trans, root);
550 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400551 if (err && !ret)
552 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500553
554 if (!ret)
555 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000556out:
557 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558 return ret;
559}
560
Miao Xiee9662f72013-02-28 10:01:15 +0000561static int create_snapshot(struct btrfs_root *root, struct inode *dir,
562 struct dentry *dentry, char *name, int namelen,
563 u64 *async_transid, bool readonly,
564 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400565{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000566 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400567 struct btrfs_pending_snapshot *pending_snapshot;
568 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000569 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570
571 if (!root->ref_cows)
572 return -EINVAL;
573
Miao Xie6a038432013-05-15 07:48:24 +0000574 ret = btrfs_start_delalloc_inodes(root, 0);
575 if (ret)
576 return ret;
577
578 btrfs_wait_ordered_extents(root, 0);
579
Yan, Zhenga22285a2010-05-16 10:48:46 -0400580 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
581 if (!pending_snapshot)
582 return -ENOMEM;
583
Miao Xie66d8f3d2012-09-06 04:02:28 -0600584 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
585 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000586 /*
587 * 1 - parent dir inode
588 * 2 - dir entries
589 * 1 - root item
590 * 2 - root ref/backref
591 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200592 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000593 */
594 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200595 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400596 &pending_snapshot->qgroup_reserved,
597 false);
Miao Xied5c12072013-02-28 10:04:33 +0000598 if (ret)
599 goto out;
600
Yan, Zhenga22285a2010-05-16 10:48:46 -0400601 pending_snapshot->dentry = dentry;
602 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800603 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000604 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000605 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400606
Miao Xied5c12072013-02-28 10:04:33 +0000607 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400608 if (IS_ERR(trans)) {
609 ret = PTR_ERR(trans);
610 goto fail;
611 }
612
Josef Bacik83515832011-06-14 15:16:14 -0400613 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400614 list_add(&pending_snapshot->list,
615 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400616 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400617 if (async_transid) {
618 *async_transid = trans->transid;
619 ret = btrfs_commit_transaction_async(trans,
620 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000621 if (ret)
622 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400623 } else {
624 ret = btrfs_commit_transaction(trans,
625 root->fs_info->extent_root);
626 }
Miao Xieaec80302013-03-04 09:44:29 +0000627 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400628 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400629
630 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400631 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000632 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400633
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500634 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
635 if (ret)
636 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400637
Al Viro2fbe8c82011-07-16 21:38:06 -0400638 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000639 if (IS_ERR(inode)) {
640 ret = PTR_ERR(inode);
641 goto fail;
642 }
643 BUG_ON(!inode);
644 d_instantiate(dentry, inode);
645 ret = 0;
646fail:
Miao Xied5c12072013-02-28 10:04:33 +0000647 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
648 &pending_snapshot->block_rsv,
649 pending_snapshot->qgroup_reserved);
650out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400651 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652 return ret;
653}
654
Sage Weil4260f7c2010-10-29 15:46:43 -0400655/* copy of check_sticky in fs/namei.c()
656* It's inline, so penalty for filesystems that don't use sticky bit is
657* minimal.
658*/
659static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
660{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800661 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400662
663 if (!(dir->i_mode & S_ISVTX))
664 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800665 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400666 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800667 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400668 return 0;
669 return !capable(CAP_FOWNER);
670}
671
672/* copy of may_delete in fs/namei.c()
673 * Check whether we can remove a link victim from directory dir, check
674 * whether the type of victim is right.
675 * 1. We can't do it if dir is read-only (done in permission())
676 * 2. We should have write and exec permissions on dir
677 * 3. We can't remove anything from append-only dir
678 * 4. We can't do anything with immutable dir (done in permission())
679 * 5. If the sticky bit on dir is set we should either
680 * a. be owner of dir, or
681 * b. be owner of victim, or
682 * c. have CAP_FOWNER capability
683 * 6. If the victim is append-only or immutable we can't do antyhing with
684 * links pointing to it.
685 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
686 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
687 * 9. We can't remove a root or mountpoint.
688 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
689 * nfs_async_unlink().
690 */
691
692static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
693{
694 int error;
695
696 if (!victim->d_inode)
697 return -ENOENT;
698
699 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400700 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400701
702 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
703 if (error)
704 return error;
705 if (IS_APPEND(dir))
706 return -EPERM;
707 if (btrfs_check_sticky(dir, victim->d_inode)||
708 IS_APPEND(victim->d_inode)||
709 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
710 return -EPERM;
711 if (isdir) {
712 if (!S_ISDIR(victim->d_inode->i_mode))
713 return -ENOTDIR;
714 if (IS_ROOT(victim))
715 return -EBUSY;
716 } else if (S_ISDIR(victim->d_inode->i_mode))
717 return -EISDIR;
718 if (IS_DEADDIR(dir))
719 return -ENOENT;
720 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
721 return -EBUSY;
722 return 0;
723}
724
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400725/* copy of may_create in fs/namei.c() */
726static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
727{
728 if (child->d_inode)
729 return -EEXIST;
730 if (IS_DEADDIR(dir))
731 return -ENOENT;
732 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
733}
734
735/*
736 * Create a new subvolume below @parent. This is largely modeled after
737 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
738 * inside this filesystem so it's quite a bit simpler.
739 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400740static noinline int btrfs_mksubvol(struct path *parent,
741 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400742 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200743 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000744 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400745{
Yan, Zheng76dda932009-09-21 16:00:26 -0400746 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400747 struct dentry *dentry;
748 int error;
749
David Sterba5c50c9b2013-03-22 18:12:51 +0000750 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
751 if (error == -EINTR)
752 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400753
754 dentry = lookup_one_len(name, parent->dentry, namelen);
755 error = PTR_ERR(dentry);
756 if (IS_ERR(dentry))
757 goto out_unlock;
758
759 error = -EEXIST;
760 if (dentry->d_inode)
761 goto out_dput;
762
Yan, Zheng76dda932009-09-21 16:00:26 -0400763 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400764 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600765 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400766
Chris Mason9c520572012-12-17 14:26:57 -0500767 /*
768 * even if this name doesn't exist, we may get hash collisions.
769 * check for them now when we can safely fail
770 */
771 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
772 dir->i_ino, name,
773 namelen);
774 if (error)
775 goto out_dput;
776
Yan, Zheng76dda932009-09-21 16:00:26 -0400777 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
778
779 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
780 goto out_up_read;
781
Chris Mason3de45862008-11-17 21:02:50 -0500782 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000783 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200784 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500785 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000786 error = create_subvol(dir, dentry, name, namelen,
787 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500788 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400789 if (!error)
790 fsnotify_mkdir(dir, dentry);
791out_up_read:
792 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400793out_dput:
794 dput(dentry);
795out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400796 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400797 return error;
798}
799
Chris Mason4cb53002011-05-24 15:35:30 -0400800/*
801 * When we're defragging a range, we don't want to kick it off again
802 * if it is really just waiting for delalloc to send it down.
803 * If we find a nice big extent or delalloc range for the bytes in the
804 * file you want to defrag, we return 0 to let you know to skip this
805 * part of the file
806 */
807static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
808{
809 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
810 struct extent_map *em = NULL;
811 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
812 u64 end;
813
814 read_lock(&em_tree->lock);
815 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
816 read_unlock(&em_tree->lock);
817
818 if (em) {
819 end = extent_map_end(em);
820 free_extent_map(em);
821 if (end - offset > thresh)
822 return 0;
823 }
824 /* if we already have a nice delalloc here, just stop */
825 thresh /= 2;
826 end = count_range_bits(io_tree, &offset, offset + thresh,
827 thresh, EXTENT_DELALLOC, 1);
828 if (end >= thresh)
829 return 0;
830 return 1;
831}
832
833/*
834 * helper function to walk through a file and find extents
835 * newer than a specific transid, and smaller than thresh.
836 *
837 * This is used by the defragging code to find new and small
838 * extents
839 */
840static int find_new_extents(struct btrfs_root *root,
841 struct inode *inode, u64 newer_than,
842 u64 *off, int thresh)
843{
844 struct btrfs_path *path;
845 struct btrfs_key min_key;
846 struct btrfs_key max_key;
847 struct extent_buffer *leaf;
848 struct btrfs_file_extent_item *extent;
849 int type;
850 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000851 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400852
853 path = btrfs_alloc_path();
854 if (!path)
855 return -ENOMEM;
856
David Sterbaa4689d22011-05-31 17:08:14 +0000857 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400858 min_key.type = BTRFS_EXTENT_DATA_KEY;
859 min_key.offset = *off;
860
David Sterbaa4689d22011-05-31 17:08:14 +0000861 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400862 max_key.type = (u8)-1;
863 max_key.offset = (u64)-1;
864
865 path->keep_locks = 1;
866
867 while(1) {
868 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +0000869 path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400870 if (ret != 0)
871 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000872 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400873 goto none;
874 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
875 goto none;
876
877 leaf = path->nodes[0];
878 extent = btrfs_item_ptr(leaf, path->slots[0],
879 struct btrfs_file_extent_item);
880
881 type = btrfs_file_extent_type(leaf, extent);
882 if (type == BTRFS_FILE_EXTENT_REG &&
883 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
884 check_defrag_in_cache(inode, min_key.offset, thresh)) {
885 *off = min_key.offset;
886 btrfs_free_path(path);
887 return 0;
888 }
889
890 if (min_key.offset == (u64)-1)
891 goto none;
892
893 min_key.offset++;
894 btrfs_release_path(path);
895 }
896none:
897 btrfs_free_path(path);
898 return -ENOENT;
899}
900
Li Zefan6c282eb2012-06-11 16:03:35 +0800901static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400902{
903 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500904 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800905 struct extent_map *em;
906 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500907
908 /*
909 * hopefully we have this extent in the tree already, try without
910 * the full extent lock
911 */
912 read_lock(&em_tree->lock);
913 em = lookup_extent_mapping(em_tree, start, len);
914 read_unlock(&em_tree->lock);
915
916 if (!em) {
917 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100918 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500919 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100920 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500921
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000922 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800923 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500924 }
925
Li Zefan6c282eb2012-06-11 16:03:35 +0800926 return em;
927}
928
929static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
930{
931 struct extent_map *next;
932 bool ret = true;
933
934 /* this is the last extent */
935 if (em->start + em->len >= i_size_read(inode))
936 return false;
937
938 next = defrag_lookup_extent(inode, em->start + em->len);
939 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
940 ret = false;
941
942 free_extent_map(next);
943 return ret;
944}
945
946static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400947 u64 *last_len, u64 *skip, u64 *defrag_end,
948 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800949{
950 struct extent_map *em;
951 int ret = 1;
952 bool next_mergeable = true;
953
954 /*
955 * make sure that once we start defragging an extent, we keep on
956 * defragging it
957 */
958 if (start < *defrag_end)
959 return 1;
960
961 *skip = 0;
962
963 em = defrag_lookup_extent(inode, start);
964 if (!em)
965 return 0;
966
Chris Mason940100a2010-03-10 10:52:59 -0500967 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400968 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500969 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400970 goto out;
971 }
972
Li Zefan6c282eb2012-06-11 16:03:35 +0800973 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500974
975 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800976 * we hit a real extent, if it is big or the next extent is not a
977 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500978 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400979 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800980 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500981 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400982out:
Chris Mason940100a2010-03-10 10:52:59 -0500983 /*
984 * last_len ends up being a counter of how many bytes we've defragged.
985 * every time we choose not to defrag an extent, we reset *last_len
986 * so that the next tiny extent will force a defrag.
987 *
988 * The end result of this is that tiny extents before a single big
989 * extent will force at least part of that big extent to be defragged.
990 */
991 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500992 *defrag_end = extent_map_end(em);
993 } else {
994 *last_len = 0;
995 *skip = extent_map_end(em);
996 *defrag_end = 0;
997 }
998
999 free_extent_map(em);
1000 return ret;
1001}
1002
Chris Mason4cb53002011-05-24 15:35:30 -04001003/*
1004 * it doesn't do much good to defrag one or two pages
1005 * at a time. This pulls in a nice chunk of pages
1006 * to COW and defrag.
1007 *
1008 * It also makes sure the delalloc code has enough
1009 * dirty data to avoid making new small extents as part
1010 * of the defrag
1011 *
1012 * It's a good idea to start RA on this range
1013 * before calling this.
1014 */
1015static int cluster_pages_for_defrag(struct inode *inode,
1016 struct page **pages,
1017 unsigned long start_index,
1018 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001019{
Chris Mason4cb53002011-05-24 15:35:30 -04001020 unsigned long file_end;
1021 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001022 u64 page_start;
1023 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001024 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001025 int ret;
1026 int i;
1027 int i_done;
1028 struct btrfs_ordered_extent *ordered;
1029 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001030 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001031 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001032
Chris Mason4cb53002011-05-24 15:35:30 -04001033 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001034 if (!isize || start_index > file_end)
1035 return 0;
1036
1037 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001038
1039 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001040 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001041 if (ret)
1042 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001043 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001044 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001045
1046 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001047 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001048 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001049again:
Josef Bacika94733d2011-07-11 10:47:06 -04001050 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001051 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001052 if (!page)
1053 break;
1054
Miao Xie600a45e2012-02-16 15:01:24 +08001055 page_start = page_offset(page);
1056 page_end = page_start + PAGE_CACHE_SIZE - 1;
1057 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001058 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001059 ordered = btrfs_lookup_ordered_extent(inode,
1060 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001061 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001062 if (!ordered)
1063 break;
1064
1065 unlock_page(page);
1066 btrfs_start_ordered_extent(inode, ordered, 1);
1067 btrfs_put_ordered_extent(ordered);
1068 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001069 /*
1070 * we unlocked the page above, so we need check if
1071 * it was released or not.
1072 */
1073 if (page->mapping != inode->i_mapping) {
1074 unlock_page(page);
1075 page_cache_release(page);
1076 goto again;
1077 }
Miao Xie600a45e2012-02-16 15:01:24 +08001078 }
1079
Chris Mason4cb53002011-05-24 15:35:30 -04001080 if (!PageUptodate(page)) {
1081 btrfs_readpage(NULL, page);
1082 lock_page(page);
1083 if (!PageUptodate(page)) {
1084 unlock_page(page);
1085 page_cache_release(page);
1086 ret = -EIO;
1087 break;
1088 }
1089 }
Miao Xie600a45e2012-02-16 15:01:24 +08001090
Miao Xie600a45e2012-02-16 15:01:24 +08001091 if (page->mapping != inode->i_mapping) {
1092 unlock_page(page);
1093 page_cache_release(page);
1094 goto again;
1095 }
1096
Chris Mason4cb53002011-05-24 15:35:30 -04001097 pages[i] = page;
1098 i_done++;
1099 }
1100 if (!i_done || ret)
1101 goto out;
1102
1103 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1104 goto out;
1105
1106 /*
1107 * so now we have a nice long stream of locked
1108 * and up to date pages, lets wait on them
1109 */
1110 for (i = 0; i < i_done; i++)
1111 wait_on_page_writeback(pages[i]);
1112
1113 page_start = page_offset(pages[0]);
1114 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1115
1116 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001117 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001118 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1119 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001120 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1121 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001122
Liu Bo1f12bd02012-03-29 09:57:44 -04001123 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001124 spin_lock(&BTRFS_I(inode)->lock);
1125 BTRFS_I(inode)->outstanding_extents++;
1126 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001127 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001128 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001129 }
1130
1131
Liu Bo9e8a4a82012-09-05 19:10:51 -06001132 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1133 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001134
1135 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1136 page_start, page_end - 1, &cached_state,
1137 GFP_NOFS);
1138
1139 for (i = 0; i < i_done; i++) {
1140 clear_page_dirty_for_io(pages[i]);
1141 ClearPageChecked(pages[i]);
1142 set_page_extent_mapped(pages[i]);
1143 set_page_dirty(pages[i]);
1144 unlock_page(pages[i]);
1145 page_cache_release(pages[i]);
1146 }
1147 return i_done;
1148out:
1149 for (i = 0; i < i_done; i++) {
1150 unlock_page(pages[i]);
1151 page_cache_release(pages[i]);
1152 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001153 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001154 return ret;
1155
1156}
1157
1158int btrfs_defrag_file(struct inode *inode, struct file *file,
1159 struct btrfs_ioctl_defrag_range_args *range,
1160 u64 newer_than, unsigned long max_to_defrag)
1161{
1162 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001163 struct file_ra_state *ra = NULL;
1164 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001165 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001166 u64 last_len = 0;
1167 u64 skip = 0;
1168 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001169 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001170 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001171 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001172 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001173 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001174 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001175 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001176 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1177 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001178 u64 new_align = ~((u64)128 * 1024 - 1);
1179 struct page **pages = NULL;
1180
Liu Bo0abd5b12013-04-16 09:20:28 +00001181 if (isize == 0)
1182 return 0;
1183
1184 if (range->start >= isize)
1185 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001186
1187 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1188 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1189 return -EINVAL;
1190 if (range->compress_type)
1191 compress_type = range->compress_type;
1192 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001193
Liu Bo0abd5b12013-04-16 09:20:28 +00001194 if (extent_thresh == 0)
1195 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001196
Chris Mason4cb53002011-05-24 15:35:30 -04001197 /*
1198 * if we were not given a file, allocate a readahead
1199 * context
1200 */
1201 if (!file) {
1202 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1203 if (!ra)
1204 return -ENOMEM;
1205 file_ra_state_init(ra, inode->i_mapping);
1206 } else {
1207 ra = &file->f_ra;
1208 }
1209
Li Zefan008873e2011-09-02 15:57:07 +08001210 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001211 GFP_NOFS);
1212 if (!pages) {
1213 ret = -ENOMEM;
1214 goto out_ra;
1215 }
1216
1217 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001218 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001219 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001220 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1221 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001222 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001223 }
1224
Chris Mason4cb53002011-05-24 15:35:30 -04001225 if (newer_than) {
1226 ret = find_new_extents(root, inode, newer_than,
1227 &newer_off, 64 * 1024);
1228 if (!ret) {
1229 range->start = newer_off;
1230 /*
1231 * we always align our defrag to help keep
1232 * the extents in the file evenly spaced
1233 */
1234 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001235 } else
1236 goto out_ra;
1237 } else {
1238 i = range->start >> PAGE_CACHE_SHIFT;
1239 }
1240 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001241 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001242
Li Zefan2a0f7f52011-10-10 15:43:34 -04001243 /*
1244 * make writeback starts from i, so the defrag range can be
1245 * written sequentially.
1246 */
1247 if (i < inode->i_mapping->writeback_index)
1248 inode->i_mapping->writeback_index = i;
1249
Chris Masonf7f43cc2011-10-11 11:41:40 -04001250 while (i <= last_index && defrag_count < max_to_defrag &&
1251 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1252 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001253 /*
1254 * make sure we stop running if someone unmounts
1255 * the FS
1256 */
1257 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1258 break;
1259
David Sterba210549e2013-02-09 23:38:06 +00001260 if (btrfs_defrag_cancelled(root->fs_info)) {
1261 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1262 ret = -EAGAIN;
1263 break;
1264 }
1265
Liu Bo66c26892012-03-29 09:57:45 -04001266 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001267 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001268 &defrag_end, range->flags &
1269 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001270 unsigned long next;
1271 /*
1272 * the should_defrag function tells us how much to skip
1273 * bump our counter by the suggested amount
1274 */
1275 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1276 i = max(i + 1, next);
1277 continue;
1278 }
Li Zefan008873e2011-09-02 15:57:07 +08001279
1280 if (!newer_than) {
1281 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1282 PAGE_CACHE_SHIFT) - i;
1283 cluster = min(cluster, max_cluster);
1284 } else {
1285 cluster = max_cluster;
1286 }
1287
Li Zefan008873e2011-09-02 15:57:07 +08001288 if (i + cluster > ra_index) {
1289 ra_index = max(i, ra_index);
1290 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1291 cluster);
1292 ra_index += max_cluster;
1293 }
Chris Mason940100a2010-03-10 10:52:59 -05001294
Liu Boecb8bea2012-03-29 09:57:44 -04001295 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001296 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1297 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001298 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001299 if (ret < 0) {
1300 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001301 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001302 }
Chris Mason940100a2010-03-10 10:52:59 -05001303
Chris Mason4cb53002011-05-24 15:35:30 -04001304 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001305 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001306 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001307
1308 if (newer_than) {
1309 if (newer_off == (u64)-1)
1310 break;
1311
Liu Boe1f041e2012-03-29 09:57:45 -04001312 if (ret > 0)
1313 i += ret;
1314
Chris Mason4cb53002011-05-24 15:35:30 -04001315 newer_off = max(newer_off + 1,
1316 (u64)i << PAGE_CACHE_SHIFT);
1317
1318 ret = find_new_extents(root, inode,
1319 newer_than, &newer_off,
1320 64 * 1024);
1321 if (!ret) {
1322 range->start = newer_off;
1323 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001324 } else {
1325 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001326 }
Chris Mason4cb53002011-05-24 15:35:30 -04001327 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001328 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001329 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001330 last_len += ret << PAGE_CACHE_SHIFT;
1331 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001332 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001333 last_len = 0;
1334 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001335 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001336 }
1337
Chris Mason1e701a32010-03-11 09:42:04 -05001338 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1339 filemap_flush(inode->i_mapping);
1340
1341 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1342 /* the filemap_flush will queue IO into the worker threads, but
1343 * we have to make sure the IO is actually started and that
1344 * ordered extents get created before we return
1345 */
1346 atomic_inc(&root->fs_info->async_submit_draining);
1347 while (atomic_read(&root->fs_info->nr_async_submits) ||
1348 atomic_read(&root->fs_info->async_delalloc_pages)) {
1349 wait_event(root->fs_info->async_submit_wait,
1350 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1351 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1352 }
1353 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001354 }
1355
Li Zefan1a419d82010-10-25 15:12:50 +08001356 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001357 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001358 }
1359
Diego Calleja60ccf822011-09-01 16:33:57 +02001360 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001361
Chris Mason4cb53002011-05-24 15:35:30 -04001362out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001363 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1364 mutex_lock(&inode->i_mutex);
1365 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1366 mutex_unlock(&inode->i_mutex);
1367 }
Chris Mason4cb53002011-05-24 15:35:30 -04001368 if (!file)
1369 kfree(ra);
1370 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001371 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372}
1373
Miao Xie198605a2012-11-26 08:43:45 +00001374static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001375 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001376{
1377 u64 new_size;
1378 u64 old_size;
1379 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001380 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001381 struct btrfs_ioctl_vol_args *vol_args;
1382 struct btrfs_trans_handle *trans;
1383 struct btrfs_device *device = NULL;
1384 char *sizestr;
1385 char *devstr = NULL;
1386 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001387 int mod = 0;
1388
Chris Masone441d542009-01-05 16:57:23 -05001389 if (!capable(CAP_SYS_ADMIN))
1390 return -EPERM;
1391
Miao Xie198605a2012-11-26 08:43:45 +00001392 ret = mnt_want_write_file(file);
1393 if (ret)
1394 return ret;
1395
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001396 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1397 1)) {
1398 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Miao Xie97547672012-12-21 10:38:50 +00001399 mnt_drop_write_file(file);
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02001400 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001401 }
1402
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001403 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001404 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001405 if (IS_ERR(vol_args)) {
1406 ret = PTR_ERR(vol_args);
1407 goto out;
1408 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001409
1410 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001411
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001412 sizestr = vol_args->name;
1413 devstr = strchr(sizestr, ':');
1414 if (devstr) {
1415 char *end;
1416 sizestr = devstr + 1;
1417 *devstr = '\0';
1418 devstr = vol_args->name;
1419 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001420 if (!devid) {
1421 ret = -EINVAL;
1422 goto out_free;
1423 }
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001424 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001425 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001426 }
Miao Xiedba60f32012-12-21 09:19:51 +00001427
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001428 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001429 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001430 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001431 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001432 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001433 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001434 }
Miao Xiedba60f32012-12-21 09:19:51 +00001435
1436 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001437 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001438 "readonly device %llu\n",
Chris Masona8c4a332012-06-15 20:07:17 -04001439 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001440 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001441 goto out_free;
1442 }
1443
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001444 if (!strcmp(sizestr, "max"))
1445 new_size = device->bdev->bd_inode->i_size;
1446 else {
1447 if (sizestr[0] == '-') {
1448 mod = -1;
1449 sizestr++;
1450 } else if (sizestr[0] == '+') {
1451 mod = 1;
1452 sizestr++;
1453 }
Akinobu Mita91748462010-02-28 10:59:11 +00001454 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001455 if (new_size == 0) {
1456 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001457 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001458 }
1459 }
1460
Stefan Behrens63a212a2012-11-05 18:29:28 +01001461 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001462 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001463 goto out_free;
1464 }
1465
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001466 old_size = device->total_bytes;
1467
1468 if (mod < 0) {
1469 if (new_size > old_size) {
1470 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001471 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001472 }
1473 new_size = old_size - new_size;
1474 } else if (mod > 0) {
1475 new_size = old_size + new_size;
1476 }
1477
1478 if (new_size < 256 * 1024 * 1024) {
1479 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001480 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481 }
1482 if (new_size > device->bdev->bd_inode->i_size) {
1483 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001484 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001485 }
1486
1487 do_div(new_size, root->sectorsize);
1488 new_size *= root->sectorsize;
1489
Josef Bacik606686e2012-06-04 14:03:51 -04001490 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1491 rcu_str_deref(device->name),
1492 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001493
1494 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001495 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001496 if (IS_ERR(trans)) {
1497 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001498 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001499 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001500 ret = btrfs_grow_device(trans, device, new_size);
1501 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001502 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001504 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001506out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001508out:
1509 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001510 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001511 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001512 return ret;
1513}
1514
Sage Weil72fd0322010-10-29 15:41:32 -04001515static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001516 char *name, unsigned long fd, int subvol,
1517 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001518 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001519{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001521 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001522
Liu Boa874a632012-06-29 03:58:46 -06001523 ret = mnt_want_write_file(file);
1524 if (ret)
1525 goto out;
1526
Sage Weil72fd0322010-10-29 15:41:32 -04001527 namelen = strlen(name);
1528 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001530 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001531 }
1532
Chris Mason16780ca2012-02-20 22:14:55 -05001533 if (name[0] == '.' &&
1534 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1535 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001536 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001537 }
1538
Chris Mason3de45862008-11-17 21:02:50 -05001539 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001540 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001541 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001542 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001543 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001544 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001545 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001546 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001547 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001548 }
1549
Al Viro496ad9a2013-01-23 17:07:38 -05001550 src_inode = file_inode(src.file);
1551 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001552 printk(KERN_INFO "btrfs: Snapshot src from "
1553 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001554 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001555 } else {
1556 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1557 BTRFS_I(src_inode)->root,
1558 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001559 }
Al Viro2903ff02012-08-28 12:52:22 -04001560 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001561 }
Liu Boa874a632012-06-29 03:58:46 -06001562out_drop_write:
1563 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001564out:
Sage Weil72fd0322010-10-29 15:41:32 -04001565 return ret;
1566}
1567
1568static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001569 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001570{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001571 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001572 int ret;
1573
Li Zefanfa0d2b92010-12-20 15:53:28 +08001574 vol_args = memdup_user(arg, sizeof(*vol_args));
1575 if (IS_ERR(vol_args))
1576 return PTR_ERR(vol_args);
1577 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001578
Li Zefanfa0d2b92010-12-20 15:53:28 +08001579 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001580 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001581 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001582
Li Zefanfa0d2b92010-12-20 15:53:28 +08001583 kfree(vol_args);
1584 return ret;
1585}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001586
Li Zefanfa0d2b92010-12-20 15:53:28 +08001587static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1588 void __user *arg, int subvol)
1589{
1590 struct btrfs_ioctl_vol_args_v2 *vol_args;
1591 int ret;
1592 u64 transid = 0;
1593 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001594 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001595 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001596
Li Zefanfa0d2b92010-12-20 15:53:28 +08001597 vol_args = memdup_user(arg, sizeof(*vol_args));
1598 if (IS_ERR(vol_args))
1599 return PTR_ERR(vol_args);
1600 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001601
Li Zefanb83cc962010-12-20 16:04:08 +08001602 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001603 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1604 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001605 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001606 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001607 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001608
1609 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1610 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001611 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1612 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001613 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1614 if (vol_args->size > PAGE_CACHE_SIZE) {
1615 ret = -EINVAL;
1616 goto out;
1617 }
1618 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1619 if (IS_ERR(inherit)) {
1620 ret = PTR_ERR(inherit);
1621 goto out;
1622 }
1623 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001624
1625 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001626 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001627 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001628
1629 if (ret == 0 && ptr &&
1630 copy_to_user(arg +
1631 offsetof(struct btrfs_ioctl_vol_args_v2,
1632 transid), ptr, sizeof(*ptr)))
1633 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001634out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001636 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001637 return ret;
1638}
1639
Li Zefan0caa1022010-12-20 16:30:25 +08001640static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1641 void __user *arg)
1642{
Al Viro496ad9a2013-01-23 17:07:38 -05001643 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001644 struct btrfs_root *root = BTRFS_I(inode)->root;
1645 int ret = 0;
1646 u64 flags = 0;
1647
Li Zefan33345d012011-04-20 10:31:50 +08001648 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001649 return -EINVAL;
1650
1651 down_read(&root->fs_info->subvol_sem);
1652 if (btrfs_root_readonly(root))
1653 flags |= BTRFS_SUBVOL_RDONLY;
1654 up_read(&root->fs_info->subvol_sem);
1655
1656 if (copy_to_user(arg, &flags, sizeof(flags)))
1657 ret = -EFAULT;
1658
1659 return ret;
1660}
1661
1662static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1663 void __user *arg)
1664{
Al Viro496ad9a2013-01-23 17:07:38 -05001665 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001666 struct btrfs_root *root = BTRFS_I(inode)->root;
1667 struct btrfs_trans_handle *trans;
1668 u64 root_flags;
1669 u64 flags;
1670 int ret = 0;
1671
Liu Bob9ca0662012-06-29 03:58:49 -06001672 ret = mnt_want_write_file(file);
1673 if (ret)
1674 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001675
Liu Bob9ca0662012-06-29 03:58:49 -06001676 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1677 ret = -EINVAL;
1678 goto out_drop_write;
1679 }
Li Zefan0caa1022010-12-20 16:30:25 +08001680
Liu Bob9ca0662012-06-29 03:58:49 -06001681 if (copy_from_user(&flags, arg, sizeof(flags))) {
1682 ret = -EFAULT;
1683 goto out_drop_write;
1684 }
Li Zefan0caa1022010-12-20 16:30:25 +08001685
Liu Bob9ca0662012-06-29 03:58:49 -06001686 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1687 ret = -EINVAL;
1688 goto out_drop_write;
1689 }
Li Zefan0caa1022010-12-20 16:30:25 +08001690
Liu Bob9ca0662012-06-29 03:58:49 -06001691 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1692 ret = -EOPNOTSUPP;
1693 goto out_drop_write;
1694 }
Li Zefan0caa1022010-12-20 16:30:25 +08001695
Liu Bob9ca0662012-06-29 03:58:49 -06001696 if (!inode_owner_or_capable(inode)) {
1697 ret = -EACCES;
1698 goto out_drop_write;
1699 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001700
Li Zefan0caa1022010-12-20 16:30:25 +08001701 down_write(&root->fs_info->subvol_sem);
1702
1703 /* nothing to do */
1704 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001705 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001706
1707 root_flags = btrfs_root_flags(&root->root_item);
1708 if (flags & BTRFS_SUBVOL_RDONLY)
1709 btrfs_set_root_flags(&root->root_item,
1710 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1711 else
1712 btrfs_set_root_flags(&root->root_item,
1713 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1714
1715 trans = btrfs_start_transaction(root, 1);
1716 if (IS_ERR(trans)) {
1717 ret = PTR_ERR(trans);
1718 goto out_reset;
1719 }
1720
Li Zefanb4dc2b82011-02-16 06:06:34 +00001721 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001722 &root->root_key, &root->root_item);
1723
1724 btrfs_commit_transaction(trans, root);
1725out_reset:
1726 if (ret)
1727 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001728out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001729 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001730out_drop_write:
1731 mnt_drop_write_file(file);
1732out:
Li Zefan0caa1022010-12-20 16:30:25 +08001733 return ret;
1734}
1735
Yan, Zheng76dda932009-09-21 16:00:26 -04001736/*
1737 * helper to check if the subvolume references other subvolumes
1738 */
1739static noinline int may_destroy_subvol(struct btrfs_root *root)
1740{
1741 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001742 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001743 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001744 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001745 int ret;
1746
1747 path = btrfs_alloc_path();
1748 if (!path)
1749 return -ENOMEM;
1750
Josef Bacik175a2b82013-08-12 15:36:44 -04001751 /* Make sure this root isn't set as the default subvol */
1752 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1753 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1754 dir_id, "default", 7, 0);
1755 if (di && !IS_ERR(di)) {
1756 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1757 if (key.objectid == root->root_key.objectid) {
1758 ret = -ENOTEMPTY;
1759 goto out;
1760 }
1761 btrfs_release_path(path);
1762 }
1763
Yan, Zheng76dda932009-09-21 16:00:26 -04001764 key.objectid = root->root_key.objectid;
1765 key.type = BTRFS_ROOT_REF_KEY;
1766 key.offset = (u64)-1;
1767
1768 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1769 &key, path, 0, 0);
1770 if (ret < 0)
1771 goto out;
1772 BUG_ON(ret == 0);
1773
1774 ret = 0;
1775 if (path->slots[0] > 0) {
1776 path->slots[0]--;
1777 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1778 if (key.objectid == root->root_key.objectid &&
1779 key.type == BTRFS_ROOT_REF_KEY)
1780 ret = -ENOTEMPTY;
1781 }
1782out:
1783 btrfs_free_path(path);
1784 return ret;
1785}
1786
Chris Masonac8e9812010-02-28 15:39:26 -05001787static noinline int key_in_sk(struct btrfs_key *key,
1788 struct btrfs_ioctl_search_key *sk)
1789{
Chris Masonabc6e132010-03-18 12:10:08 -04001790 struct btrfs_key test;
1791 int ret;
1792
1793 test.objectid = sk->min_objectid;
1794 test.type = sk->min_type;
1795 test.offset = sk->min_offset;
1796
1797 ret = btrfs_comp_cpu_keys(key, &test);
1798 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001799 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001800
1801 test.objectid = sk->max_objectid;
1802 test.type = sk->max_type;
1803 test.offset = sk->max_offset;
1804
1805 ret = btrfs_comp_cpu_keys(key, &test);
1806 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001807 return 0;
1808 return 1;
1809}
1810
1811static noinline int copy_to_sk(struct btrfs_root *root,
1812 struct btrfs_path *path,
1813 struct btrfs_key *key,
1814 struct btrfs_ioctl_search_key *sk,
1815 char *buf,
1816 unsigned long *sk_offset,
1817 int *num_found)
1818{
1819 u64 found_transid;
1820 struct extent_buffer *leaf;
1821 struct btrfs_ioctl_search_header sh;
1822 unsigned long item_off;
1823 unsigned long item_len;
1824 int nritems;
1825 int i;
1826 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001827 int ret = 0;
1828
1829 leaf = path->nodes[0];
1830 slot = path->slots[0];
1831 nritems = btrfs_header_nritems(leaf);
1832
1833 if (btrfs_header_generation(leaf) > sk->max_transid) {
1834 i = nritems;
1835 goto advance_key;
1836 }
1837 found_transid = btrfs_header_generation(leaf);
1838
1839 for (i = slot; i < nritems; i++) {
1840 item_off = btrfs_item_ptr_offset(leaf, i);
1841 item_len = btrfs_item_size_nr(leaf, i);
1842
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001843 btrfs_item_key_to_cpu(leaf, key, i);
1844 if (!key_in_sk(key, sk))
1845 continue;
1846
1847 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001848 item_len = 0;
1849
1850 if (sizeof(sh) + item_len + *sk_offset >
1851 BTRFS_SEARCH_ARGS_BUFSIZE) {
1852 ret = 1;
1853 goto overflow;
1854 }
1855
Chris Masonac8e9812010-02-28 15:39:26 -05001856 sh.objectid = key->objectid;
1857 sh.offset = key->offset;
1858 sh.type = key->type;
1859 sh.len = item_len;
1860 sh.transid = found_transid;
1861
1862 /* copy search result header */
1863 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1864 *sk_offset += sizeof(sh);
1865
1866 if (item_len) {
1867 char *p = buf + *sk_offset;
1868 /* copy the item */
1869 read_extent_buffer(leaf, p,
1870 item_off, item_len);
1871 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001872 }
Hugo Millse2156862011-05-14 17:43:41 +00001873 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001874
1875 if (*num_found >= sk->nr_items)
1876 break;
1877 }
1878advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001879 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001880 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1881 key->offset++;
1882 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1883 key->offset = 0;
1884 key->type++;
1885 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1886 key->offset = 0;
1887 key->type = 0;
1888 key->objectid++;
1889 } else
1890 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001891overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001892 return ret;
1893}
1894
1895static noinline int search_ioctl(struct inode *inode,
1896 struct btrfs_ioctl_search_args *args)
1897{
1898 struct btrfs_root *root;
1899 struct btrfs_key key;
1900 struct btrfs_key max_key;
1901 struct btrfs_path *path;
1902 struct btrfs_ioctl_search_key *sk = &args->key;
1903 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1904 int ret;
1905 int num_found = 0;
1906 unsigned long sk_offset = 0;
1907
1908 path = btrfs_alloc_path();
1909 if (!path)
1910 return -ENOMEM;
1911
1912 if (sk->tree_id == 0) {
1913 /* search the root of the inode that was passed */
1914 root = BTRFS_I(inode)->root;
1915 } else {
1916 key.objectid = sk->tree_id;
1917 key.type = BTRFS_ROOT_ITEM_KEY;
1918 key.offset = (u64)-1;
1919 root = btrfs_read_fs_root_no_name(info, &key);
1920 if (IS_ERR(root)) {
1921 printk(KERN_ERR "could not find root %llu\n",
1922 sk->tree_id);
1923 btrfs_free_path(path);
1924 return -ENOENT;
1925 }
1926 }
1927
1928 key.objectid = sk->min_objectid;
1929 key.type = sk->min_type;
1930 key.offset = sk->min_offset;
1931
1932 max_key.objectid = sk->max_objectid;
1933 max_key.type = sk->max_type;
1934 max_key.offset = sk->max_offset;
1935
1936 path->keep_locks = 1;
1937
1938 while(1) {
Eric Sandeende78b512013-01-31 18:21:12 +00001939 ret = btrfs_search_forward(root, &key, &max_key, path,
Chris Masonac8e9812010-02-28 15:39:26 -05001940 sk->min_transid);
1941 if (ret != 0) {
1942 if (ret > 0)
1943 ret = 0;
1944 goto err;
1945 }
1946 ret = copy_to_sk(root, path, &key, sk, args->buf,
1947 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001948 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001949 if (ret || num_found >= sk->nr_items)
1950 break;
1951
1952 }
1953 ret = 0;
1954err:
1955 sk->nr_items = num_found;
1956 btrfs_free_path(path);
1957 return ret;
1958}
1959
1960static noinline int btrfs_ioctl_tree_search(struct file *file,
1961 void __user *argp)
1962{
1963 struct btrfs_ioctl_search_args *args;
1964 struct inode *inode;
1965 int ret;
1966
1967 if (!capable(CAP_SYS_ADMIN))
1968 return -EPERM;
1969
Julia Lawall2354d082010-10-29 15:14:18 -04001970 args = memdup_user(argp, sizeof(*args));
1971 if (IS_ERR(args))
1972 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001973
Al Viro496ad9a2013-01-23 17:07:38 -05001974 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001975 ret = search_ioctl(inode, args);
1976 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1977 ret = -EFAULT;
1978 kfree(args);
1979 return ret;
1980}
1981
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001982/*
Chris Masonac8e9812010-02-28 15:39:26 -05001983 * Search INODE_REFs to identify path name of 'dirid' directory
1984 * in a 'tree_id' tree. and sets path name to 'name'.
1985 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001986static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1987 u64 tree_id, u64 dirid, char *name)
1988{
1989 struct btrfs_root *root;
1990 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001991 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001992 int ret = -1;
1993 int slot;
1994 int len;
1995 int total_len = 0;
1996 struct btrfs_inode_ref *iref;
1997 struct extent_buffer *l;
1998 struct btrfs_path *path;
1999
2000 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2001 name[0]='\0';
2002 return 0;
2003 }
2004
2005 path = btrfs_alloc_path();
2006 if (!path)
2007 return -ENOMEM;
2008
Chris Masonac8e9812010-02-28 15:39:26 -05002009 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002010
2011 key.objectid = tree_id;
2012 key.type = BTRFS_ROOT_ITEM_KEY;
2013 key.offset = (u64)-1;
2014 root = btrfs_read_fs_root_no_name(info, &key);
2015 if (IS_ERR(root)) {
2016 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002017 ret = -ENOENT;
2018 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002019 }
2020
2021 key.objectid = dirid;
2022 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002023 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002024
2025 while(1) {
2026 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2027 if (ret < 0)
2028 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002029 else if (ret > 0) {
2030 ret = btrfs_previous_item(root, path, dirid,
2031 BTRFS_INODE_REF_KEY);
2032 if (ret < 0)
2033 goto out;
2034 else if (ret > 0) {
2035 ret = -ENOENT;
2036 goto out;
2037 }
2038 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002039
2040 l = path->nodes[0];
2041 slot = path->slots[0];
2042 btrfs_item_key_to_cpu(l, &key, slot);
2043
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002044 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2045 len = btrfs_inode_ref_name_len(l, iref);
2046 ptr -= len + 1;
2047 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002048 if (ptr < name) {
2049 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002050 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002051 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002052
2053 *(ptr + len) = '/';
2054 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2055
2056 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2057 break;
2058
David Sterbab3b4aa72011-04-21 01:20:15 +02002059 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002060 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002061 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002062 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002063 }
Li Zefan77906a502011-07-14 03:16:00 +00002064 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002065 name[total_len]='\0';
2066 ret = 0;
2067out:
2068 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002069 return ret;
2070}
2071
2072static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2073 void __user *argp)
2074{
2075 struct btrfs_ioctl_ino_lookup_args *args;
2076 struct inode *inode;
2077 int ret;
2078
2079 if (!capable(CAP_SYS_ADMIN))
2080 return -EPERM;
2081
Julia Lawall2354d082010-10-29 15:14:18 -04002082 args = memdup_user(argp, sizeof(*args));
2083 if (IS_ERR(args))
2084 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002085
Al Viro496ad9a2013-01-23 17:07:38 -05002086 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002087
Chris Mason1b53ac42010-03-18 12:17:05 -04002088 if (args->treeid == 0)
2089 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2090
Chris Masonac8e9812010-02-28 15:39:26 -05002091 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2092 args->treeid, args->objectid,
2093 args->name);
2094
2095 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2096 ret = -EFAULT;
2097
2098 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002099 return ret;
2100}
2101
Yan, Zheng76dda932009-09-21 16:00:26 -04002102static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2103 void __user *arg)
2104{
2105 struct dentry *parent = fdentry(file);
2106 struct dentry *dentry;
2107 struct inode *dir = parent->d_inode;
2108 struct inode *inode;
2109 struct btrfs_root *root = BTRFS_I(dir)->root;
2110 struct btrfs_root *dest = NULL;
2111 struct btrfs_ioctl_vol_args *vol_args;
2112 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002113 struct btrfs_block_rsv block_rsv;
2114 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002115 int namelen;
2116 int ret;
2117 int err = 0;
2118
Yan, Zheng76dda932009-09-21 16:00:26 -04002119 vol_args = memdup_user(arg, sizeof(*vol_args));
2120 if (IS_ERR(vol_args))
2121 return PTR_ERR(vol_args);
2122
2123 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2124 namelen = strlen(vol_args->name);
2125 if (strchr(vol_args->name, '/') ||
2126 strncmp(vol_args->name, "..", namelen) == 0) {
2127 err = -EINVAL;
2128 goto out;
2129 }
2130
Al Viroa561be72011-11-23 11:57:51 -05002131 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002132 if (err)
2133 goto out;
2134
David Sterba5c50c9b2013-03-22 18:12:51 +00002135 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2136 if (err == -EINTR)
2137 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002138 dentry = lookup_one_len(vol_args->name, parent, namelen);
2139 if (IS_ERR(dentry)) {
2140 err = PTR_ERR(dentry);
2141 goto out_unlock_dir;
2142 }
2143
2144 if (!dentry->d_inode) {
2145 err = -ENOENT;
2146 goto out_dput;
2147 }
2148
2149 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002150 dest = BTRFS_I(inode)->root;
2151 if (!capable(CAP_SYS_ADMIN)){
2152 /*
2153 * Regular user. Only allow this with a special mount
2154 * option, when the user has write+exec access to the
2155 * subvol root, and when rmdir(2) would have been
2156 * allowed.
2157 *
2158 * Note that this is _not_ check that the subvol is
2159 * empty or doesn't contain data that we wouldn't
2160 * otherwise be able to delete.
2161 *
2162 * Users who want to delete empty subvols should try
2163 * rmdir(2).
2164 */
2165 err = -EPERM;
2166 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2167 goto out_dput;
2168
2169 /*
2170 * Do not allow deletion if the parent dir is the same
2171 * as the dir to be deleted. That means the ioctl
2172 * must be called on the dentry referencing the root
2173 * of the subvol, not a random directory contained
2174 * within it.
2175 */
2176 err = -EINVAL;
2177 if (root == dest)
2178 goto out_dput;
2179
2180 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2181 if (err)
2182 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002183 }
2184
Miao Xie5c39da52012-10-22 11:39:53 +00002185 /* check if subvolume may be deleted by a user */
2186 err = btrfs_may_delete(dir, dentry, 1);
2187 if (err)
2188 goto out_dput;
2189
Li Zefan33345d012011-04-20 10:31:50 +08002190 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002191 err = -EINVAL;
2192 goto out_dput;
2193 }
2194
Yan, Zheng76dda932009-09-21 16:00:26 -04002195 mutex_lock(&inode->i_mutex);
2196 err = d_invalidate(dentry);
2197 if (err)
2198 goto out_unlock;
2199
2200 down_write(&root->fs_info->subvol_sem);
2201
2202 err = may_destroy_subvol(dest);
2203 if (err)
2204 goto out_up_write;
2205
Miao Xiec58aaad2013-02-28 10:05:36 +00002206 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2207 /*
2208 * One for dir inode, two for dir entries, two for root
2209 * ref/backref.
2210 */
2211 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002212 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002213 if (err)
2214 goto out_up_write;
2215
Yan, Zhenga22285a2010-05-16 10:48:46 -04002216 trans = btrfs_start_transaction(root, 0);
2217 if (IS_ERR(trans)) {
2218 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002219 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002220 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002221 trans->block_rsv = &block_rsv;
2222 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002223
Yan, Zheng76dda932009-09-21 16:00:26 -04002224 ret = btrfs_unlink_subvol(trans, root, dir,
2225 dest->root_key.objectid,
2226 dentry->d_name.name,
2227 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228 if (ret) {
2229 err = ret;
2230 btrfs_abort_transaction(trans, root, ret);
2231 goto out_end_trans;
2232 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002233
2234 btrfs_record_root_in_trans(trans, dest);
2235
2236 memset(&dest->root_item.drop_progress, 0,
2237 sizeof(dest->root_item.drop_progress));
2238 dest->root_item.drop_level = 0;
2239 btrfs_set_root_refs(&dest->root_item, 0);
2240
Yan, Zhengd68fc572010-05-16 10:49:58 -04002241 if (!xchg(&dest->orphan_item_inserted, 1)) {
2242 ret = btrfs_insert_orphan_item(trans,
2243 root->fs_info->tree_root,
2244 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002245 if (ret) {
2246 btrfs_abort_transaction(trans, root, ret);
2247 err = ret;
2248 goto out_end_trans;
2249 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002250 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002251
2252 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2253 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2254 dest->root_key.objectid);
2255 if (ret && ret != -ENOENT) {
2256 btrfs_abort_transaction(trans, root, ret);
2257 err = ret;
2258 goto out_end_trans;
2259 }
2260 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2261 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2262 dest->root_item.received_uuid,
2263 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2264 dest->root_key.objectid);
2265 if (ret && ret != -ENOENT) {
2266 btrfs_abort_transaction(trans, root, ret);
2267 err = ret;
2268 goto out_end_trans;
2269 }
2270 }
2271
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002273 trans->block_rsv = NULL;
2274 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002275 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002276 if (ret && !err)
2277 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002278 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002279out_release:
2280 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002281out_up_write:
2282 up_write(&root->fs_info->subvol_sem);
2283out_unlock:
2284 mutex_unlock(&inode->i_mutex);
2285 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002286 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002287 btrfs_invalidate_inodes(dest);
2288 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002289
2290 /* the last ref */
2291 if (dest->cache_inode) {
2292 iput(dest->cache_inode);
2293 dest->cache_inode = NULL;
2294 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002295 }
2296out_dput:
2297 dput(dentry);
2298out_unlock_dir:
2299 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002300 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002301out:
2302 kfree(vol_args);
2303 return err;
2304}
2305
Chris Mason1e701a32010-03-11 09:42:04 -05002306static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002307{
Al Viro496ad9a2013-01-23 17:07:38 -05002308 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002309 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002310 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002311 int ret;
2312
Ilya Dryomov25122d12013-01-20 15:57:57 +02002313 ret = mnt_want_write_file(file);
2314 if (ret)
2315 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002316
Ilya Dryomov25122d12013-01-20 15:57:57 +02002317 if (btrfs_root_readonly(root)) {
2318 ret = -EROFS;
2319 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002320 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002321
2322 switch (inode->i_mode & S_IFMT) {
2323 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002324 if (!capable(CAP_SYS_ADMIN)) {
2325 ret = -EPERM;
2326 goto out;
2327 }
Eric Sandeende78b512013-01-31 18:21:12 +00002328 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002329 if (ret)
2330 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002331 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002332 break;
2333 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002334 if (!(file->f_mode & FMODE_WRITE)) {
2335 ret = -EINVAL;
2336 goto out;
2337 }
Chris Mason1e701a32010-03-11 09:42:04 -05002338
2339 range = kzalloc(sizeof(*range), GFP_KERNEL);
2340 if (!range) {
2341 ret = -ENOMEM;
2342 goto out;
2343 }
2344
2345 if (argp) {
2346 if (copy_from_user(range, argp,
2347 sizeof(*range))) {
2348 ret = -EFAULT;
2349 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002350 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002351 }
2352 /* compression requires us to start the IO */
2353 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2354 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2355 range->extent_thresh = (u32)-1;
2356 }
2357 } else {
2358 /* the rest are all set to zero by kzalloc */
2359 range->len = (u64)-1;
2360 }
Al Viro496ad9a2013-01-23 17:07:38 -05002361 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002362 range, 0, 0);
2363 if (ret > 0)
2364 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002365 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002366 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002367 default:
2368 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002369 }
Chris Masone441d542009-01-05 16:57:23 -05002370out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002371 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002372 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002373}
2374
Christoph Hellwigb2950862008-12-02 09:54:17 -05002375static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002376{
2377 struct btrfs_ioctl_vol_args *vol_args;
2378 int ret;
2379
Chris Masone441d542009-01-05 16:57:23 -05002380 if (!capable(CAP_SYS_ADMIN))
2381 return -EPERM;
2382
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002383 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2384 1)) {
2385 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002386 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002387 }
2388
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002389 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002390 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002391 if (IS_ERR(vol_args)) {
2392 ret = PTR_ERR(vol_args);
2393 goto out;
2394 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002395
Mark Fasheh5516e592008-07-24 12:20:14 -04002396 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002397 ret = btrfs_init_new_device(root, vol_args->name);
2398
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002399 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002400out:
2401 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002402 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002403 return ret;
2404}
2405
Miao Xieda249272012-11-26 08:44:50 +00002406static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407{
Al Viro496ad9a2013-01-23 17:07:38 -05002408 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002409 struct btrfs_ioctl_vol_args *vol_args;
2410 int ret;
2411
Chris Masone441d542009-01-05 16:57:23 -05002412 if (!capable(CAP_SYS_ADMIN))
2413 return -EPERM;
2414
Miao Xieda249272012-11-26 08:44:50 +00002415 ret = mnt_want_write_file(file);
2416 if (ret)
2417 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002418
Li Zefandae7b662009-04-08 15:06:54 +08002419 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002420 if (IS_ERR(vol_args)) {
2421 ret = PTR_ERR(vol_args);
2422 goto out;
2423 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424
Mark Fasheh5516e592008-07-24 12:20:14 -04002425 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002426
Anand Jain183860f2013-05-17 10:52:45 +00002427 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2428 1)) {
2429 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2430 goto out;
2431 }
2432
2433 mutex_lock(&root->fs_info->volume_mutex);
2434 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002435 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002436 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002437
2438out:
2439 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002440 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002441 return ret;
2442}
2443
Jan Schmidt475f6382011-03-11 15:41:01 +01002444static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2445{
Li Zefan027ed2f2011-06-08 08:27:56 +00002446 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002447 struct btrfs_device *device;
2448 struct btrfs_device *next;
2449 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002450 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002451
2452 if (!capable(CAP_SYS_ADMIN))
2453 return -EPERM;
2454
Li Zefan027ed2f2011-06-08 08:27:56 +00002455 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2456 if (!fi_args)
2457 return -ENOMEM;
2458
2459 fi_args->num_devices = fs_devices->num_devices;
2460 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002461
2462 mutex_lock(&fs_devices->device_list_mutex);
2463 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002464 if (device->devid > fi_args->max_id)
2465 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002466 }
2467 mutex_unlock(&fs_devices->device_list_mutex);
2468
Li Zefan027ed2f2011-06-08 08:27:56 +00002469 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2470 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002471
Li Zefan027ed2f2011-06-08 08:27:56 +00002472 kfree(fi_args);
2473 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002474}
2475
2476static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2477{
2478 struct btrfs_ioctl_dev_info_args *di_args;
2479 struct btrfs_device *dev;
2480 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2481 int ret = 0;
2482 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002483
2484 if (!capable(CAP_SYS_ADMIN))
2485 return -EPERM;
2486
2487 di_args = memdup_user(arg, sizeof(*di_args));
2488 if (IS_ERR(di_args))
2489 return PTR_ERR(di_args);
2490
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002491 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002492 s_uuid = di_args->uuid;
2493
2494 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002495 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002496
2497 if (!dev) {
2498 ret = -ENODEV;
2499 goto out;
2500 }
2501
2502 di_args->devid = dev->devid;
2503 di_args->bytes_used = dev->bytes_used;
2504 di_args->total_bytes = dev->total_bytes;
2505 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002506 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002507 struct rcu_string *name;
2508
2509 rcu_read_lock();
2510 name = rcu_dereference(dev->name);
2511 strncpy(di_args->path, name->str, sizeof(di_args->path));
2512 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002513 di_args->path[sizeof(di_args->path) - 1] = 0;
2514 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002515 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002516 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002517
2518out:
David Sterba55793c02013-04-26 15:20:23 +00002519 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002520 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2521 ret = -EFAULT;
2522
2523 kfree(di_args);
2524 return ret;
2525}
2526
Mark Fasheh416161d2013-08-06 11:42:51 -07002527static struct page *extent_same_get_page(struct inode *inode, u64 off)
2528{
2529 struct page *page;
2530 pgoff_t index;
2531 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2532
2533 index = off >> PAGE_CACHE_SHIFT;
2534
2535 page = grab_cache_page(inode->i_mapping, index);
2536 if (!page)
2537 return NULL;
2538
2539 if (!PageUptodate(page)) {
2540 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2541 0))
2542 return NULL;
2543 lock_page(page);
2544 if (!PageUptodate(page)) {
2545 unlock_page(page);
2546 page_cache_release(page);
2547 return NULL;
2548 }
2549 }
2550 unlock_page(page);
2551
2552 return page;
2553}
2554
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002555static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2556{
2557 /* do any pending delalloc/csum calc on src, one way or
2558 another, and lock file content */
2559 while (1) {
2560 struct btrfs_ordered_extent *ordered;
2561 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2562 ordered = btrfs_lookup_first_ordered_extent(inode,
2563 off + len - 1);
2564 if (!ordered &&
2565 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2566 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2567 break;
2568 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2569 if (ordered)
2570 btrfs_put_ordered_extent(ordered);
2571 btrfs_wait_ordered_range(inode, off, len);
2572 }
2573}
2574
Mark Fasheh416161d2013-08-06 11:42:51 -07002575static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2576 struct inode *inode2, u64 loff2, u64 len)
2577{
2578 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2579 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2580
2581 mutex_unlock(&inode1->i_mutex);
2582 mutex_unlock(&inode2->i_mutex);
2583}
2584
2585static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2586 struct inode *inode2, u64 loff2, u64 len)
2587{
2588 if (inode1 < inode2) {
2589 swap(inode1, inode2);
2590 swap(loff1, loff2);
2591 }
2592
2593 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2594 lock_extent_range(inode1, loff1, len);
2595 if (inode1 != inode2) {
2596 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2597 lock_extent_range(inode2, loff2, len);
2598 }
2599}
2600
2601static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2602 u64 dst_loff, u64 len)
2603{
2604 int ret = 0;
2605 struct page *src_page, *dst_page;
2606 unsigned int cmp_len = PAGE_CACHE_SIZE;
2607 void *addr, *dst_addr;
2608
2609 while (len) {
2610 if (len < PAGE_CACHE_SIZE)
2611 cmp_len = len;
2612
2613 src_page = extent_same_get_page(src, loff);
2614 if (!src_page)
2615 return -EINVAL;
2616 dst_page = extent_same_get_page(dst, dst_loff);
2617 if (!dst_page) {
2618 page_cache_release(src_page);
2619 return -EINVAL;
2620 }
2621 addr = kmap_atomic(src_page);
2622 dst_addr = kmap_atomic(dst_page);
2623
2624 flush_dcache_page(src_page);
2625 flush_dcache_page(dst_page);
2626
2627 if (memcmp(addr, dst_addr, cmp_len))
2628 ret = BTRFS_SAME_DATA_DIFFERS;
2629
2630 kunmap_atomic(addr);
2631 kunmap_atomic(dst_addr);
2632 page_cache_release(src_page);
2633 page_cache_release(dst_page);
2634
2635 if (ret)
2636 break;
2637
2638 loff += cmp_len;
2639 dst_loff += cmp_len;
2640 len -= cmp_len;
2641 }
2642
2643 return ret;
2644}
2645
2646static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2647{
2648 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2649
2650 if (off + len > inode->i_size || off + len < off)
2651 return -EINVAL;
2652 /* Check that we are block aligned - btrfs_clone() requires this */
2653 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2654 return -EINVAL;
2655
2656 return 0;
2657}
2658
2659static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2660 struct inode *dst, u64 dst_loff)
2661{
2662 int ret;
2663
2664 /*
2665 * btrfs_clone() can't handle extents in the same file
2666 * yet. Once that works, we can drop this check and replace it
2667 * with a check for the same inode, but overlapping extents.
2668 */
2669 if (src == dst)
2670 return -EINVAL;
2671
2672 btrfs_double_lock(src, loff, dst, dst_loff, len);
2673
2674 ret = extent_same_check_offsets(src, loff, len);
2675 if (ret)
2676 goto out_unlock;
2677
2678 ret = extent_same_check_offsets(dst, dst_loff, len);
2679 if (ret)
2680 goto out_unlock;
2681
2682 /* don't make the dst file partly checksummed */
2683 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2684 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2685 ret = -EINVAL;
2686 goto out_unlock;
2687 }
2688
2689 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2690 if (ret == 0)
2691 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2692
2693out_unlock:
2694 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2695
2696 return ret;
2697}
2698
2699#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2700
2701static long btrfs_ioctl_file_extent_same(struct file *file,
2702 void __user *argp)
2703{
2704 struct btrfs_ioctl_same_args *args = argp;
2705 struct btrfs_ioctl_same_args same;
2706 struct btrfs_ioctl_same_extent_info info;
2707 struct inode *src = file->f_dentry->d_inode;
2708 struct file *dst_file = NULL;
2709 struct inode *dst;
2710 u64 off;
2711 u64 len;
2712 int i;
2713 int ret;
2714 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2715 bool is_admin = capable(CAP_SYS_ADMIN);
2716
2717 if (!(file->f_mode & FMODE_READ))
2718 return -EINVAL;
2719
2720 ret = mnt_want_write_file(file);
2721 if (ret)
2722 return ret;
2723
2724 if (copy_from_user(&same,
2725 (struct btrfs_ioctl_same_args __user *)argp,
2726 sizeof(same))) {
2727 ret = -EFAULT;
2728 goto out;
2729 }
2730
2731 off = same.logical_offset;
2732 len = same.length;
2733
2734 /*
2735 * Limit the total length we will dedupe for each operation.
2736 * This is intended to bound the total time spent in this
2737 * ioctl to something sane.
2738 */
2739 if (len > BTRFS_MAX_DEDUPE_LEN)
2740 len = BTRFS_MAX_DEDUPE_LEN;
2741
2742 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2743 /*
2744 * Btrfs does not support blocksize < page_size. As a
2745 * result, btrfs_cmp_data() won't correctly handle
2746 * this situation without an update.
2747 */
2748 ret = -EINVAL;
2749 goto out;
2750 }
2751
2752 ret = -EISDIR;
2753 if (S_ISDIR(src->i_mode))
2754 goto out;
2755
2756 ret = -EACCES;
2757 if (!S_ISREG(src->i_mode))
2758 goto out;
2759
2760 ret = 0;
2761 for (i = 0; i < same.dest_count; i++) {
2762 if (copy_from_user(&info, &args->info[i], sizeof(info))) {
2763 ret = -EFAULT;
2764 goto out;
2765 }
2766
2767 info.bytes_deduped = 0;
2768
2769 dst_file = fget(info.fd);
2770 if (!dst_file) {
2771 info.status = -EBADF;
2772 goto next;
2773 }
2774
2775 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2776 info.status = -EINVAL;
2777 goto next;
2778 }
2779
2780 info.status = -EXDEV;
2781 if (file->f_path.mnt != dst_file->f_path.mnt)
2782 goto next;
2783
2784 dst = dst_file->f_dentry->d_inode;
2785 if (src->i_sb != dst->i_sb)
2786 goto next;
2787
2788 if (S_ISDIR(dst->i_mode)) {
2789 info.status = -EISDIR;
2790 goto next;
2791 }
2792
2793 if (!S_ISREG(dst->i_mode)) {
2794 info.status = -EACCES;
2795 goto next;
2796 }
2797
2798 info.status = btrfs_extent_same(src, off, len, dst,
2799 info.logical_offset);
2800 if (info.status == 0)
2801 info.bytes_deduped += len;
2802
2803next:
2804 if (dst_file)
2805 fput(dst_file);
2806
2807 if (__put_user_unaligned(info.status, &args->info[i].status) ||
2808 __put_user_unaligned(info.bytes_deduped,
2809 &args->info[i].bytes_deduped)) {
2810 ret = -EFAULT;
2811 goto out;
2812 }
2813 }
2814
2815out:
2816 mnt_drop_write_file(file);
2817 return ret;
2818}
2819
Mark Fasheh32b7c682013-08-06 11:42:49 -07002820/**
2821 * btrfs_clone() - clone a range from inode file to another
2822 *
2823 * @src: Inode to clone from
2824 * @inode: Inode to clone to
2825 * @off: Offset within source to start clone from
2826 * @olen: Original length, passed by user, of range to clone
2827 * @olen_aligned: Block-aligned value of olen, extent_same uses
2828 * identical values here
2829 * @destoff: Offset within @inode to start clone
2830 */
2831static int btrfs_clone(struct inode *src, struct inode *inode,
2832 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002833{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002834 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002835 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002836 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002837 struct btrfs_trans_handle *trans;
2838 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002839 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002840 u32 nritems;
2841 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002842 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002843 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002844
2845 ret = -ENOMEM;
2846 buf = vmalloc(btrfs_level_size(root, 0));
2847 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002848 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002849
2850 path = btrfs_alloc_path();
2851 if (!path) {
2852 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002853 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002854 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002855
Yan Zhengae01a0a2008-08-04 23:23:47 -04002856 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002857 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002858 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002859 key.type = BTRFS_EXTENT_DATA_KEY;
2860 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002861
2862 while (1) {
2863 /*
2864 * note the key will change type as we walk through the
2865 * tree.
2866 */
David Sterba362a20c2011-08-01 18:11:57 +02002867 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2868 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002869 if (ret < 0)
2870 goto out;
2871
Yan Zhengae01a0a2008-08-04 23:23:47 -04002872 nritems = btrfs_header_nritems(path->nodes[0]);
2873 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002874 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002875 if (ret < 0)
2876 goto out;
2877 if (ret > 0)
2878 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002879 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002880 }
2881 leaf = path->nodes[0];
2882 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002883
Yan Zhengae01a0a2008-08-04 23:23:47 -04002884 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002885 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002886 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002887 break;
2888
Sage Weilc5c9cd42008-11-12 14:32:25 -05002889 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2890 struct btrfs_file_extent_item *extent;
2891 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002892 u32 size;
2893 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002894 u64 disko = 0, diskl = 0;
2895 u64 datao = 0, datal = 0;
2896 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002897 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002898
2899 size = btrfs_item_size_nr(leaf, slot);
2900 read_extent_buffer(leaf, buf,
2901 btrfs_item_ptr_offset(leaf, slot),
2902 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002903
2904 extent = btrfs_item_ptr(leaf, slot,
2905 struct btrfs_file_extent_item);
2906 comp = btrfs_file_extent_compression(leaf, extent);
2907 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002908 if (type == BTRFS_FILE_EXTENT_REG ||
2909 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002910 disko = btrfs_file_extent_disk_bytenr(leaf,
2911 extent);
2912 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2913 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002914 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002915 datal = btrfs_file_extent_num_bytes(leaf,
2916 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002917 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2918 /* take upper bound, may be compressed */
2919 datal = btrfs_file_extent_ram_bytes(leaf,
2920 extent);
2921 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002922 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002923
Sage Weil050006a2010-10-29 15:37:33 -04002924 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002925 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002926 goto next;
2927
Zheng Yan31840ae2008-09-23 13:14:14 -04002928 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002929 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002930 if (off <= key.offset)
2931 new_key.offset = key.offset + destoff - off;
2932 else
2933 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002934
Sage Weilb6f34092011-09-20 14:48:51 -04002935 /*
2936 * 1 - adjusting old extent (we may have to split it)
2937 * 1 - add new extent
2938 * 1 - inode update
2939 */
2940 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002941 if (IS_ERR(trans)) {
2942 ret = PTR_ERR(trans);
2943 goto out;
2944 }
2945
Chris Masonc8a894d2009-06-27 21:07:03 -04002946 if (type == BTRFS_FILE_EXTENT_REG ||
2947 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002948 /*
2949 * a | --- range to clone ---| b
2950 * | ------------- extent ------------- |
2951 */
2952
2953 /* substract range b */
2954 if (key.offset + datal > off + len)
2955 datal = off + len - key.offset;
2956
2957 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002958 if (off > key.offset) {
2959 datao += off - key.offset;
2960 datal -= off - key.offset;
2961 }
2962
Josef Bacik5dc562c2012-08-17 13:14:17 -04002963 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002964 new_key.offset,
2965 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002966 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002967 if (ret) {
2968 btrfs_abort_transaction(trans, root,
2969 ret);
2970 btrfs_end_transaction(trans, root);
2971 goto out;
2972 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002973
Sage Weilc5c9cd42008-11-12 14:32:25 -05002974 ret = btrfs_insert_empty_item(trans, root, path,
2975 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002976 if (ret) {
2977 btrfs_abort_transaction(trans, root,
2978 ret);
2979 btrfs_end_transaction(trans, root);
2980 goto out;
2981 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002982
2983 leaf = path->nodes[0];
2984 slot = path->slots[0];
2985 write_extent_buffer(leaf, buf,
2986 btrfs_item_ptr_offset(leaf, slot),
2987 size);
2988
2989 extent = btrfs_item_ptr(leaf, slot,
2990 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002991
Sage Weilc5c9cd42008-11-12 14:32:25 -05002992 /* disko == 0 means it's a hole */
2993 if (!disko)
2994 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002995
2996 btrfs_set_file_extent_offset(leaf, extent,
2997 datao);
2998 btrfs_set_file_extent_num_bytes(leaf, extent,
2999 datal);
3000 if (disko) {
3001 inode_add_bytes(inode, datal);
3002 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003003 disko, diskl, 0,
3004 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003005 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003006 new_key.offset - datao,
3007 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003008 if (ret) {
3009 btrfs_abort_transaction(trans,
3010 root,
3011 ret);
3012 btrfs_end_transaction(trans,
3013 root);
3014 goto out;
3015
3016 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003017 }
3018 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3019 u64 skip = 0;
3020 u64 trim = 0;
3021 if (off > key.offset) {
3022 skip = off - key.offset;
3023 new_key.offset += skip;
3024 }
Chris Masond3977122009-01-05 21:25:51 -05003025
Liu Boaa42ffd2012-09-18 03:52:23 -06003026 if (key.offset + datal > off + len)
3027 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003028
Sage Weilc5c9cd42008-11-12 14:32:25 -05003029 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003030 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003031 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003032 goto out;
3033 }
3034 size -= skip + trim;
3035 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003036
Josef Bacik5dc562c2012-08-17 13:14:17 -04003037 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003038 new_key.offset,
3039 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003040 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003041 if (ret) {
3042 btrfs_abort_transaction(trans, root,
3043 ret);
3044 btrfs_end_transaction(trans, root);
3045 goto out;
3046 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003047
Sage Weilc5c9cd42008-11-12 14:32:25 -05003048 ret = btrfs_insert_empty_item(trans, root, path,
3049 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003050 if (ret) {
3051 btrfs_abort_transaction(trans, root,
3052 ret);
3053 btrfs_end_transaction(trans, root);
3054 goto out;
3055 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003056
3057 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003058 u32 start =
3059 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003060 memmove(buf+start, buf+start+skip,
3061 datal);
3062 }
3063
3064 leaf = path->nodes[0];
3065 slot = path->slots[0];
3066 write_extent_buffer(leaf, buf,
3067 btrfs_item_ptr_offset(leaf, slot),
3068 size);
3069 inode_add_bytes(inode, datal);
3070 }
3071
3072 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003073 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003074
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003075 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003076 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003077
3078 /*
3079 * we round up to the block size at eof when
3080 * determining which extents to clone above,
3081 * but shouldn't round up the file size
3082 */
3083 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003084 if (endoff > destoff+olen)
3085 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003086 if (endoff > inode->i_size)
3087 btrfs_i_size_write(inode, endoff);
3088
Yan, Zhenga22285a2010-05-16 10:48:46 -04003089 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003090 if (ret) {
3091 btrfs_abort_transaction(trans, root, ret);
3092 btrfs_end_transaction(trans, root);
3093 goto out;
3094 }
3095 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003096 }
Chris Masond3977122009-01-05 21:25:51 -05003097next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003098 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003099 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003100 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003101 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003102
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003103out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003104 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003105 btrfs_free_path(path);
3106 vfree(buf);
3107 return ret;
3108}
3109
3110static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3111 u64 off, u64 olen, u64 destoff)
3112{
3113 struct inode *inode = fdentry(file)->d_inode;
3114 struct btrfs_root *root = BTRFS_I(inode)->root;
3115 struct fd src_file;
3116 struct inode *src;
3117 int ret;
3118 u64 len = olen;
3119 u64 bs = root->fs_info->sb->s_blocksize;
3120 int same_inode = 0;
3121
3122 /*
3123 * TODO:
3124 * - split compressed inline extents. annoying: we need to
3125 * decompress into destination's address_space (the file offset
3126 * may change, so source mapping won't do), then recompress (or
3127 * otherwise reinsert) a subrange.
3128 * - allow ranges within the same file to be cloned (provided
3129 * they don't overlap)?
3130 */
3131
3132 /* the destination must be opened for writing */
3133 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3134 return -EINVAL;
3135
3136 if (btrfs_root_readonly(root))
3137 return -EROFS;
3138
3139 ret = mnt_want_write_file(file);
3140 if (ret)
3141 return ret;
3142
3143 src_file = fdget(srcfd);
3144 if (!src_file.file) {
3145 ret = -EBADF;
3146 goto out_drop_write;
3147 }
3148
3149 ret = -EXDEV;
3150 if (src_file.file->f_path.mnt != file->f_path.mnt)
3151 goto out_fput;
3152
3153 src = file_inode(src_file.file);
3154
3155 ret = -EINVAL;
3156 if (src == inode)
3157 same_inode = 1;
3158
3159 /* the src must be open for reading */
3160 if (!(src_file.file->f_mode & FMODE_READ))
3161 goto out_fput;
3162
3163 /* don't make the dst file partly checksummed */
3164 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3165 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3166 goto out_fput;
3167
3168 ret = -EISDIR;
3169 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3170 goto out_fput;
3171
3172 ret = -EXDEV;
3173 if (src->i_sb != inode->i_sb)
3174 goto out_fput;
3175
3176 if (!same_inode) {
3177 if (inode < src) {
3178 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3179 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3180 } else {
3181 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3182 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3183 }
3184 } else {
3185 mutex_lock(&src->i_mutex);
3186 }
3187
3188 /* determine range to clone */
3189 ret = -EINVAL;
3190 if (off + len > src->i_size || off + len < off)
3191 goto out_unlock;
3192 if (len == 0)
3193 olen = len = src->i_size - off;
3194 /* if we extend to eof, continue to block boundary */
3195 if (off + len == src->i_size)
3196 len = ALIGN(src->i_size, bs) - off;
3197
3198 /* verify the end result is block aligned */
3199 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3200 !IS_ALIGNED(destoff, bs))
3201 goto out_unlock;
3202
3203 /* verify if ranges are overlapped within the same file */
3204 if (same_inode) {
3205 if (destoff + len > off && destoff < off + len)
3206 goto out_unlock;
3207 }
3208
3209 if (destoff > inode->i_size) {
3210 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3211 if (ret)
3212 goto out_unlock;
3213 }
3214
3215 /* truncate page cache pages from target inode range */
3216 truncate_inode_pages_range(&inode->i_data, destoff,
3217 PAGE_CACHE_ALIGN(destoff + len) - 1);
3218
3219 lock_extent_range(src, off, len);
3220
3221 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3222
Liu Boaa42ffd2012-09-18 03:52:23 -06003223 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003224out_unlock:
3225 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003226 if (!same_inode)
3227 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003228out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003229 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003230out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003231 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003232 return ret;
3233}
3234
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003235static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003236{
3237 struct btrfs_ioctl_clone_range_args args;
3238
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003239 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003240 return -EFAULT;
3241 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3242 args.src_length, args.dest_offset);
3243}
3244
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003245/*
3246 * there are many ways the trans_start and trans_end ioctls can lead
3247 * to deadlocks. They should only be used by applications that
3248 * basically own the machine, and have a very in depth understanding
3249 * of all the possible deadlocks and enospc problems.
3250 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003251static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252{
Al Viro496ad9a2013-01-23 17:07:38 -05003253 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003254 struct btrfs_root *root = BTRFS_I(inode)->root;
3255 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003256 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003257
Sage Weil1ab86ae2009-09-29 18:38:44 -04003258 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003259 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003260 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003261
3262 ret = -EINPROGRESS;
3263 if (file->private_data)
3264 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003265
Li Zefanb83cc962010-12-20 16:04:08 +08003266 ret = -EROFS;
3267 if (btrfs_root_readonly(root))
3268 goto out;
3269
Al Viroa561be72011-11-23 11:57:51 -05003270 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003271 if (ret)
3272 goto out;
3273
Josef Bacika4abeea2011-04-11 17:25:13 -04003274 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003275
Sage Weil1ab86ae2009-09-29 18:38:44 -04003276 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003277 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003278 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003279 goto out_drop;
3280
3281 file->private_data = trans;
3282 return 0;
3283
3284out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003285 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003286 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003287out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003288 return ret;
3289}
3290
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003291static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3292{
Al Viro496ad9a2013-01-23 17:07:38 -05003293 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003294 struct btrfs_root *root = BTRFS_I(inode)->root;
3295 struct btrfs_root *new_root;
3296 struct btrfs_dir_item *di;
3297 struct btrfs_trans_handle *trans;
3298 struct btrfs_path *path;
3299 struct btrfs_key location;
3300 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003301 u64 objectid = 0;
3302 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003303 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003304
3305 if (!capable(CAP_SYS_ADMIN))
3306 return -EPERM;
3307
Miao Xie3c04ce02012-11-26 08:43:07 +00003308 ret = mnt_want_write_file(file);
3309 if (ret)
3310 return ret;
3311
3312 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3313 ret = -EFAULT;
3314 goto out;
3315 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003316
3317 if (!objectid)
3318 objectid = root->root_key.objectid;
3319
3320 location.objectid = objectid;
3321 location.type = BTRFS_ROOT_ITEM_KEY;
3322 location.offset = (u64)-1;
3323
3324 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003325 if (IS_ERR(new_root)) {
3326 ret = PTR_ERR(new_root);
3327 goto out;
3328 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003329
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003330 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003331 if (!path) {
3332 ret = -ENOMEM;
3333 goto out;
3334 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003335 path->leave_spinning = 1;
3336
3337 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003338 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003339 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003340 ret = PTR_ERR(trans);
3341 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003342 }
3343
David Sterba6c417612011-04-13 15:41:04 +02003344 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003345 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3346 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003347 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003348 btrfs_free_path(path);
3349 btrfs_end_transaction(trans, root);
3350 printk(KERN_ERR "Umm, you don't have the default dir item, "
3351 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003352 ret = -ENOENT;
3353 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003354 }
3355
3356 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3357 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3358 btrfs_mark_buffer_dirty(path->nodes[0]);
3359 btrfs_free_path(path);
3360
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003361 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003362 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003363out:
3364 mnt_drop_write_file(file);
3365 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003366}
3367
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003368void btrfs_get_block_group_info(struct list_head *groups_list,
3369 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003370{
3371 struct btrfs_block_group_cache *block_group;
3372
3373 space->total_bytes = 0;
3374 space->used_bytes = 0;
3375 space->flags = 0;
3376 list_for_each_entry(block_group, groups_list, list) {
3377 space->flags = block_group->flags;
3378 space->total_bytes += block_group->key.offset;
3379 space->used_bytes +=
3380 btrfs_block_group_used(&block_group->item);
3381 }
3382}
3383
Eric Sandeen48a3b632013-04-25 20:41:01 +00003384static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003385{
3386 struct btrfs_ioctl_space_args space_args;
3387 struct btrfs_ioctl_space_info space;
3388 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003389 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003390 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003391 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003392 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3393 BTRFS_BLOCK_GROUP_SYSTEM,
3394 BTRFS_BLOCK_GROUP_METADATA,
3395 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3396 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003397 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003398 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003399 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003400 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003401
3402 if (copy_from_user(&space_args,
3403 (struct btrfs_ioctl_space_args __user *)arg,
3404 sizeof(space_args)))
3405 return -EFAULT;
3406
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003407 for (i = 0; i < num_types; i++) {
3408 struct btrfs_space_info *tmp;
3409
3410 info = NULL;
3411 rcu_read_lock();
3412 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3413 list) {
3414 if (tmp->flags == types[i]) {
3415 info = tmp;
3416 break;
3417 }
3418 }
3419 rcu_read_unlock();
3420
3421 if (!info)
3422 continue;
3423
3424 down_read(&info->groups_sem);
3425 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3426 if (!list_empty(&info->block_groups[c]))
3427 slot_count++;
3428 }
3429 up_read(&info->groups_sem);
3430 }
Josef Bacik1406e432010-01-13 18:19:06 +00003431
Chris Mason7fde62b2010-03-16 15:40:10 -04003432 /* space_slots == 0 means they are asking for a count */
3433 if (space_args.space_slots == 0) {
3434 space_args.total_spaces = slot_count;
3435 goto out;
3436 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003437
Dan Rosenberg51788b12011-02-14 16:04:23 -05003438 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003439
Chris Mason7fde62b2010-03-16 15:40:10 -04003440 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003441
Chris Mason7fde62b2010-03-16 15:40:10 -04003442 /* we generally have at most 6 or so space infos, one for each raid
3443 * level. So, a whole page should be more than enough for everyone
3444 */
3445 if (alloc_size > PAGE_CACHE_SIZE)
3446 return -ENOMEM;
3447
3448 space_args.total_spaces = 0;
3449 dest = kmalloc(alloc_size, GFP_NOFS);
3450 if (!dest)
3451 return -ENOMEM;
3452 dest_orig = dest;
3453
3454 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003455 for (i = 0; i < num_types; i++) {
3456 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003457
Dan Rosenberg51788b12011-02-14 16:04:23 -05003458 if (!slot_count)
3459 break;
3460
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003461 info = NULL;
3462 rcu_read_lock();
3463 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3464 list) {
3465 if (tmp->flags == types[i]) {
3466 info = tmp;
3467 break;
3468 }
3469 }
3470 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003471
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003472 if (!info)
3473 continue;
3474 down_read(&info->groups_sem);
3475 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3476 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003477 btrfs_get_block_group_info(
3478 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003479 memcpy(dest, &space, sizeof(space));
3480 dest++;
3481 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003482 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003483 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003484 if (!slot_count)
3485 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003486 }
3487 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003488 }
Josef Bacik1406e432010-01-13 18:19:06 +00003489
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003490 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003491 (arg + sizeof(struct btrfs_ioctl_space_args));
3492
3493 if (copy_to_user(user_dest, dest_orig, alloc_size))
3494 ret = -EFAULT;
3495
3496 kfree(dest_orig);
3497out:
3498 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003499 ret = -EFAULT;
3500
3501 return ret;
3502}
3503
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003504/*
3505 * there are many ways the trans_start and trans_end ioctls can lead
3506 * to deadlocks. They should only be used by applications that
3507 * basically own the machine, and have a very in depth understanding
3508 * of all the possible deadlocks and enospc problems.
3509 */
3510long btrfs_ioctl_trans_end(struct file *file)
3511{
Al Viro496ad9a2013-01-23 17:07:38 -05003512 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003513 struct btrfs_root *root = BTRFS_I(inode)->root;
3514 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003515
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003516 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003517 if (!trans)
3518 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003519 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003520
Sage Weil1ab86ae2009-09-29 18:38:44 -04003521 btrfs_end_transaction(trans, root);
3522
Josef Bacika4abeea2011-04-11 17:25:13 -04003523 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003524
Al Viro2a79f172011-12-09 08:06:57 -05003525 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003526 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003527}
3528
Miao Xie9a8c28b2012-11-26 08:40:43 +00003529static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3530 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003531{
Sage Weil46204592010-10-29 15:41:32 -04003532 struct btrfs_trans_handle *trans;
3533 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003534 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003535
Miao Xied4edf392013-02-20 09:17:06 +00003536 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003537 if (IS_ERR(trans)) {
3538 if (PTR_ERR(trans) != -ENOENT)
3539 return PTR_ERR(trans);
3540
3541 /* No running transaction, don't bother */
3542 transid = root->fs_info->last_trans_committed;
3543 goto out;
3544 }
Sage Weil46204592010-10-29 15:41:32 -04003545 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003546 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003547 if (ret) {
3548 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003549 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003550 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003551out:
Sage Weil46204592010-10-29 15:41:32 -04003552 if (argp)
3553 if (copy_to_user(argp, &transid, sizeof(transid)))
3554 return -EFAULT;
3555 return 0;
3556}
3557
Miao Xie9a8c28b2012-11-26 08:40:43 +00003558static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3559 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003560{
Sage Weil46204592010-10-29 15:41:32 -04003561 u64 transid;
3562
3563 if (argp) {
3564 if (copy_from_user(&transid, argp, sizeof(transid)))
3565 return -EFAULT;
3566 } else {
3567 transid = 0; /* current trans */
3568 }
3569 return btrfs_wait_for_commit(root, transid);
3570}
3571
Miao Xieb8e95482012-11-26 08:48:01 +00003572static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003573{
Al Viro496ad9a2013-01-23 17:07:38 -05003574 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003575 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003576 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003577
3578 if (!capable(CAP_SYS_ADMIN))
3579 return -EPERM;
3580
3581 sa = memdup_user(arg, sizeof(*sa));
3582 if (IS_ERR(sa))
3583 return PTR_ERR(sa);
3584
Miao Xieb8e95482012-11-26 08:48:01 +00003585 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3586 ret = mnt_want_write_file(file);
3587 if (ret)
3588 goto out;
3589 }
3590
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003591 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003592 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3593 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003594
3595 if (copy_to_user(arg, sa, sizeof(*sa)))
3596 ret = -EFAULT;
3597
Miao Xieb8e95482012-11-26 08:48:01 +00003598 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3599 mnt_drop_write_file(file);
3600out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003601 kfree(sa);
3602 return ret;
3603}
3604
3605static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3606{
3607 if (!capable(CAP_SYS_ADMIN))
3608 return -EPERM;
3609
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003610 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003611}
3612
3613static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3614 void __user *arg)
3615{
3616 struct btrfs_ioctl_scrub_args *sa;
3617 int ret;
3618
3619 if (!capable(CAP_SYS_ADMIN))
3620 return -EPERM;
3621
3622 sa = memdup_user(arg, sizeof(*sa));
3623 if (IS_ERR(sa))
3624 return PTR_ERR(sa);
3625
3626 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3627
3628 if (copy_to_user(arg, sa, sizeof(*sa)))
3629 ret = -EFAULT;
3630
3631 kfree(sa);
3632 return ret;
3633}
3634
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003635static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003636 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003637{
3638 struct btrfs_ioctl_get_dev_stats *sa;
3639 int ret;
3640
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003641 sa = memdup_user(arg, sizeof(*sa));
3642 if (IS_ERR(sa))
3643 return PTR_ERR(sa);
3644
David Sterbab27f7c02012-06-22 06:30:39 -06003645 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3646 kfree(sa);
3647 return -EPERM;
3648 }
3649
3650 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003651
3652 if (copy_to_user(arg, sa, sizeof(*sa)))
3653 ret = -EFAULT;
3654
3655 kfree(sa);
3656 return ret;
3657}
3658
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003659static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3660{
3661 struct btrfs_ioctl_dev_replace_args *p;
3662 int ret;
3663
3664 if (!capable(CAP_SYS_ADMIN))
3665 return -EPERM;
3666
3667 p = memdup_user(arg, sizeof(*p));
3668 if (IS_ERR(p))
3669 return PTR_ERR(p);
3670
3671 switch (p->cmd) {
3672 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3673 if (atomic_xchg(
3674 &root->fs_info->mutually_exclusive_operation_running,
3675 1)) {
3676 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3677 ret = -EINPROGRESS;
3678 } else {
3679 ret = btrfs_dev_replace_start(root, p);
3680 atomic_set(
3681 &root->fs_info->mutually_exclusive_operation_running,
3682 0);
3683 }
3684 break;
3685 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3686 btrfs_dev_replace_status(root->fs_info, p);
3687 ret = 0;
3688 break;
3689 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3690 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3691 break;
3692 default:
3693 ret = -EINVAL;
3694 break;
3695 }
3696
3697 if (copy_to_user(arg, p, sizeof(*p)))
3698 ret = -EFAULT;
3699
3700 kfree(p);
3701 return ret;
3702}
3703
Jan Schmidtd7728c92011-07-07 16:48:38 +02003704static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3705{
3706 int ret = 0;
3707 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003708 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003709 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003710 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003711 struct inode_fs_paths *ipath = NULL;
3712 struct btrfs_path *path;
3713
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003714 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003715 return -EPERM;
3716
3717 path = btrfs_alloc_path();
3718 if (!path) {
3719 ret = -ENOMEM;
3720 goto out;
3721 }
3722
3723 ipa = memdup_user(arg, sizeof(*ipa));
3724 if (IS_ERR(ipa)) {
3725 ret = PTR_ERR(ipa);
3726 ipa = NULL;
3727 goto out;
3728 }
3729
3730 size = min_t(u32, ipa->size, 4096);
3731 ipath = init_ipath(size, root, path);
3732 if (IS_ERR(ipath)) {
3733 ret = PTR_ERR(ipath);
3734 ipath = NULL;
3735 goto out;
3736 }
3737
3738 ret = paths_from_inode(ipa->inum, ipath);
3739 if (ret < 0)
3740 goto out;
3741
3742 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003743 rel_ptr = ipath->fspath->val[i] -
3744 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003745 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003746 }
3747
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003748 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3749 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003750 if (ret) {
3751 ret = -EFAULT;
3752 goto out;
3753 }
3754
3755out:
3756 btrfs_free_path(path);
3757 free_ipath(ipath);
3758 kfree(ipa);
3759
3760 return ret;
3761}
3762
3763static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3764{
3765 struct btrfs_data_container *inodes = ctx;
3766 const size_t c = 3 * sizeof(u64);
3767
3768 if (inodes->bytes_left >= c) {
3769 inodes->bytes_left -= c;
3770 inodes->val[inodes->elem_cnt] = inum;
3771 inodes->val[inodes->elem_cnt + 1] = offset;
3772 inodes->val[inodes->elem_cnt + 2] = root;
3773 inodes->elem_cnt += 3;
3774 } else {
3775 inodes->bytes_missing += c - inodes->bytes_left;
3776 inodes->bytes_left = 0;
3777 inodes->elem_missed += 3;
3778 }
3779
3780 return 0;
3781}
3782
3783static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3784 void __user *arg)
3785{
3786 int ret = 0;
3787 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003788 struct btrfs_ioctl_logical_ino_args *loi;
3789 struct btrfs_data_container *inodes = NULL;
3790 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003791
3792 if (!capable(CAP_SYS_ADMIN))
3793 return -EPERM;
3794
3795 loi = memdup_user(arg, sizeof(*loi));
3796 if (IS_ERR(loi)) {
3797 ret = PTR_ERR(loi);
3798 loi = NULL;
3799 goto out;
3800 }
3801
3802 path = btrfs_alloc_path();
3803 if (!path) {
3804 ret = -ENOMEM;
3805 goto out;
3806 }
3807
Liu Bo425d17a2012-09-07 20:01:30 -06003808 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003809 inodes = init_data_container(size);
3810 if (IS_ERR(inodes)) {
3811 ret = PTR_ERR(inodes);
3812 inodes = NULL;
3813 goto out;
3814 }
3815
Liu Bodf031f02012-09-07 20:01:29 -06003816 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3817 build_ino_list, inodes);
3818 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003819 ret = -ENOENT;
3820 if (ret < 0)
3821 goto out;
3822
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003823 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3824 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003825 if (ret)
3826 ret = -EFAULT;
3827
3828out:
3829 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003830 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003831 kfree(loi);
3832
3833 return ret;
3834}
3835
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003836void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003837 struct btrfs_ioctl_balance_args *bargs)
3838{
3839 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3840
3841 bargs->flags = bctl->flags;
3842
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003843 if (atomic_read(&fs_info->balance_running))
3844 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3845 if (atomic_read(&fs_info->balance_pause_req))
3846 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003847 if (atomic_read(&fs_info->balance_cancel_req))
3848 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003849
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003850 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3851 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3852 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003853
3854 if (lock) {
3855 spin_lock(&fs_info->balance_lock);
3856 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3857 spin_unlock(&fs_info->balance_lock);
3858 } else {
3859 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3860 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003861}
3862
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003863static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003864{
Al Viro496ad9a2013-01-23 17:07:38 -05003865 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003866 struct btrfs_fs_info *fs_info = root->fs_info;
3867 struct btrfs_ioctl_balance_args *bargs;
3868 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003869 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003870 int ret;
3871
3872 if (!capable(CAP_SYS_ADMIN))
3873 return -EPERM;
3874
Liu Boe54bfa32012-06-29 03:58:48 -06003875 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003876 if (ret)
3877 return ret;
3878
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003879again:
3880 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3881 mutex_lock(&fs_info->volume_mutex);
3882 mutex_lock(&fs_info->balance_mutex);
3883 need_unlock = true;
3884 goto locked;
3885 }
3886
3887 /*
3888 * mut. excl. ops lock is locked. Three possibilites:
3889 * (1) some other op is running
3890 * (2) balance is running
3891 * (3) balance is paused -- special case (think resume)
3892 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003893 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003894 if (fs_info->balance_ctl) {
3895 /* this is either (2) or (3) */
3896 if (!atomic_read(&fs_info->balance_running)) {
3897 mutex_unlock(&fs_info->balance_mutex);
3898 if (!mutex_trylock(&fs_info->volume_mutex))
3899 goto again;
3900 mutex_lock(&fs_info->balance_mutex);
3901
3902 if (fs_info->balance_ctl &&
3903 !atomic_read(&fs_info->balance_running)) {
3904 /* this is (3) */
3905 need_unlock = false;
3906 goto locked;
3907 }
3908
3909 mutex_unlock(&fs_info->balance_mutex);
3910 mutex_unlock(&fs_info->volume_mutex);
3911 goto again;
3912 } else {
3913 /* this is (2) */
3914 mutex_unlock(&fs_info->balance_mutex);
3915 ret = -EINPROGRESS;
3916 goto out;
3917 }
3918 } else {
3919 /* this is (1) */
3920 mutex_unlock(&fs_info->balance_mutex);
3921 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3922 ret = -EINVAL;
3923 goto out;
3924 }
3925
3926locked:
3927 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003928
3929 if (arg) {
3930 bargs = memdup_user(arg, sizeof(*bargs));
3931 if (IS_ERR(bargs)) {
3932 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003933 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003934 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003935
3936 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3937 if (!fs_info->balance_ctl) {
3938 ret = -ENOTCONN;
3939 goto out_bargs;
3940 }
3941
3942 bctl = fs_info->balance_ctl;
3943 spin_lock(&fs_info->balance_lock);
3944 bctl->flags |= BTRFS_BALANCE_RESUME;
3945 spin_unlock(&fs_info->balance_lock);
3946
3947 goto do_balance;
3948 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003949 } else {
3950 bargs = NULL;
3951 }
3952
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003953 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003954 ret = -EINPROGRESS;
3955 goto out_bargs;
3956 }
3957
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003958 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3959 if (!bctl) {
3960 ret = -ENOMEM;
3961 goto out_bargs;
3962 }
3963
3964 bctl->fs_info = fs_info;
3965 if (arg) {
3966 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3967 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3968 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3969
3970 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003971 } else {
3972 /* balance everything - no filters */
3973 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003974 }
3975
Ilya Dryomovde322262012-01-16 22:04:49 +02003976do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003977 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003978 * Ownership of bctl and mutually_exclusive_operation_running
3979 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3980 * or, if restriper was paused all the way until unmount, in
3981 * free_fs_info. mutually_exclusive_operation_running is
3982 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003983 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003984 need_unlock = false;
3985
3986 ret = btrfs_balance(bctl, bargs);
3987
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988 if (arg) {
3989 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3990 ret = -EFAULT;
3991 }
3992
3993out_bargs:
3994 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003995out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996 mutex_unlock(&fs_info->balance_mutex);
3997 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003998 if (need_unlock)
3999 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4000out:
Liu Boe54bfa32012-06-29 03:58:48 -06004001 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004002 return ret;
4003}
4004
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004005static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4006{
4007 if (!capable(CAP_SYS_ADMIN))
4008 return -EPERM;
4009
4010 switch (cmd) {
4011 case BTRFS_BALANCE_CTL_PAUSE:
4012 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004013 case BTRFS_BALANCE_CTL_CANCEL:
4014 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004015 }
4016
4017 return -EINVAL;
4018}
4019
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004020static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4021 void __user *arg)
4022{
4023 struct btrfs_fs_info *fs_info = root->fs_info;
4024 struct btrfs_ioctl_balance_args *bargs;
4025 int ret = 0;
4026
4027 if (!capable(CAP_SYS_ADMIN))
4028 return -EPERM;
4029
4030 mutex_lock(&fs_info->balance_mutex);
4031 if (!fs_info->balance_ctl) {
4032 ret = -ENOTCONN;
4033 goto out;
4034 }
4035
4036 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4037 if (!bargs) {
4038 ret = -ENOMEM;
4039 goto out;
4040 }
4041
4042 update_ioctl_balance_args(fs_info, 1, bargs);
4043
4044 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4045 ret = -EFAULT;
4046
4047 kfree(bargs);
4048out:
4049 mutex_unlock(&fs_info->balance_mutex);
4050 return ret;
4051}
4052
Miao Xie905b0dd2012-11-26 08:50:11 +00004053static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004054{
Al Viro496ad9a2013-01-23 17:07:38 -05004055 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004056 struct btrfs_ioctl_quota_ctl_args *sa;
4057 struct btrfs_trans_handle *trans = NULL;
4058 int ret;
4059 int err;
4060
4061 if (!capable(CAP_SYS_ADMIN))
4062 return -EPERM;
4063
Miao Xie905b0dd2012-11-26 08:50:11 +00004064 ret = mnt_want_write_file(file);
4065 if (ret)
4066 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004067
4068 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004069 if (IS_ERR(sa)) {
4070 ret = PTR_ERR(sa);
4071 goto drop_write;
4072 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004073
Wang Shilong7708f022013-04-07 10:24:57 +00004074 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004075 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4076 if (IS_ERR(trans)) {
4077 ret = PTR_ERR(trans);
4078 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004079 }
4080
4081 switch (sa->cmd) {
4082 case BTRFS_QUOTA_CTL_ENABLE:
4083 ret = btrfs_quota_enable(trans, root->fs_info);
4084 break;
4085 case BTRFS_QUOTA_CTL_DISABLE:
4086 ret = btrfs_quota_disable(trans, root->fs_info);
4087 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004088 default:
4089 ret = -EINVAL;
4090 break;
4091 }
4092
Jan Schmidt2f232032013-04-25 16:04:51 +00004093 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4094 if (err && !ret)
4095 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004096out:
4097 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004098 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004099drop_write:
4100 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004101 return ret;
4102}
4103
Miao Xie905b0dd2012-11-26 08:50:11 +00004104static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004105{
Al Viro496ad9a2013-01-23 17:07:38 -05004106 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004107 struct btrfs_ioctl_qgroup_assign_args *sa;
4108 struct btrfs_trans_handle *trans;
4109 int ret;
4110 int err;
4111
4112 if (!capable(CAP_SYS_ADMIN))
4113 return -EPERM;
4114
Miao Xie905b0dd2012-11-26 08:50:11 +00004115 ret = mnt_want_write_file(file);
4116 if (ret)
4117 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004118
4119 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004120 if (IS_ERR(sa)) {
4121 ret = PTR_ERR(sa);
4122 goto drop_write;
4123 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004124
4125 trans = btrfs_join_transaction(root);
4126 if (IS_ERR(trans)) {
4127 ret = PTR_ERR(trans);
4128 goto out;
4129 }
4130
4131 /* FIXME: check if the IDs really exist */
4132 if (sa->assign) {
4133 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4134 sa->src, sa->dst);
4135 } else {
4136 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4137 sa->src, sa->dst);
4138 }
4139
4140 err = btrfs_end_transaction(trans, root);
4141 if (err && !ret)
4142 ret = err;
4143
4144out:
4145 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004146drop_write:
4147 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004148 return ret;
4149}
4150
Miao Xie905b0dd2012-11-26 08:50:11 +00004151static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004152{
Al Viro496ad9a2013-01-23 17:07:38 -05004153 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004154 struct btrfs_ioctl_qgroup_create_args *sa;
4155 struct btrfs_trans_handle *trans;
4156 int ret;
4157 int err;
4158
4159 if (!capable(CAP_SYS_ADMIN))
4160 return -EPERM;
4161
Miao Xie905b0dd2012-11-26 08:50:11 +00004162 ret = mnt_want_write_file(file);
4163 if (ret)
4164 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004165
4166 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004167 if (IS_ERR(sa)) {
4168 ret = PTR_ERR(sa);
4169 goto drop_write;
4170 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004171
Miao Xied86e56c2012-11-15 11:35:41 +00004172 if (!sa->qgroupid) {
4173 ret = -EINVAL;
4174 goto out;
4175 }
4176
Arne Jansen5d13a372011-09-14 15:53:51 +02004177 trans = btrfs_join_transaction(root);
4178 if (IS_ERR(trans)) {
4179 ret = PTR_ERR(trans);
4180 goto out;
4181 }
4182
4183 /* FIXME: check if the IDs really exist */
4184 if (sa->create) {
4185 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4186 NULL);
4187 } else {
4188 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4189 }
4190
4191 err = btrfs_end_transaction(trans, root);
4192 if (err && !ret)
4193 ret = err;
4194
4195out:
4196 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004197drop_write:
4198 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004199 return ret;
4200}
4201
Miao Xie905b0dd2012-11-26 08:50:11 +00004202static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004203{
Al Viro496ad9a2013-01-23 17:07:38 -05004204 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004205 struct btrfs_ioctl_qgroup_limit_args *sa;
4206 struct btrfs_trans_handle *trans;
4207 int ret;
4208 int err;
4209 u64 qgroupid;
4210
4211 if (!capable(CAP_SYS_ADMIN))
4212 return -EPERM;
4213
Miao Xie905b0dd2012-11-26 08:50:11 +00004214 ret = mnt_want_write_file(file);
4215 if (ret)
4216 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004217
4218 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004219 if (IS_ERR(sa)) {
4220 ret = PTR_ERR(sa);
4221 goto drop_write;
4222 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004223
4224 trans = btrfs_join_transaction(root);
4225 if (IS_ERR(trans)) {
4226 ret = PTR_ERR(trans);
4227 goto out;
4228 }
4229
4230 qgroupid = sa->qgroupid;
4231 if (!qgroupid) {
4232 /* take the current subvol as qgroup */
4233 qgroupid = root->root_key.objectid;
4234 }
4235
4236 /* FIXME: check if the IDs really exist */
4237 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4238
4239 err = btrfs_end_transaction(trans, root);
4240 if (err && !ret)
4241 ret = err;
4242
4243out:
4244 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004245drop_write:
4246 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004247 return ret;
4248}
4249
Jan Schmidt2f232032013-04-25 16:04:51 +00004250static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4251{
Al Viro6d0379e2013-06-16 19:32:35 +04004252 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004253 struct btrfs_ioctl_quota_rescan_args *qsa;
4254 int ret;
4255
4256 if (!capable(CAP_SYS_ADMIN))
4257 return -EPERM;
4258
4259 ret = mnt_want_write_file(file);
4260 if (ret)
4261 return ret;
4262
4263 qsa = memdup_user(arg, sizeof(*qsa));
4264 if (IS_ERR(qsa)) {
4265 ret = PTR_ERR(qsa);
4266 goto drop_write;
4267 }
4268
4269 if (qsa->flags) {
4270 ret = -EINVAL;
4271 goto out;
4272 }
4273
4274 ret = btrfs_qgroup_rescan(root->fs_info);
4275
4276out:
4277 kfree(qsa);
4278drop_write:
4279 mnt_drop_write_file(file);
4280 return ret;
4281}
4282
4283static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4284{
Al Viro6d0379e2013-06-16 19:32:35 +04004285 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004286 struct btrfs_ioctl_quota_rescan_args *qsa;
4287 int ret = 0;
4288
4289 if (!capable(CAP_SYS_ADMIN))
4290 return -EPERM;
4291
4292 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4293 if (!qsa)
4294 return -ENOMEM;
4295
4296 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4297 qsa->flags = 1;
4298 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4299 }
4300
4301 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4302 ret = -EFAULT;
4303
4304 kfree(qsa);
4305 return ret;
4306}
4307
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004308static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4309{
4310 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4311
4312 if (!capable(CAP_SYS_ADMIN))
4313 return -EPERM;
4314
4315 return btrfs_qgroup_wait_for_completion(root->fs_info);
4316}
4317
Alexander Block8ea05e32012-07-25 17:35:53 +02004318static long btrfs_ioctl_set_received_subvol(struct file *file,
4319 void __user *arg)
4320{
4321 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004322 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004323 struct btrfs_root *root = BTRFS_I(inode)->root;
4324 struct btrfs_root_item *root_item = &root->root_item;
4325 struct btrfs_trans_handle *trans;
4326 struct timespec ct = CURRENT_TIME;
4327 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004328 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004329
4330 ret = mnt_want_write_file(file);
4331 if (ret < 0)
4332 return ret;
4333
4334 down_write(&root->fs_info->subvol_sem);
4335
4336 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4337 ret = -EINVAL;
4338 goto out;
4339 }
4340
4341 if (btrfs_root_readonly(root)) {
4342 ret = -EROFS;
4343 goto out;
4344 }
4345
4346 if (!inode_owner_or_capable(inode)) {
4347 ret = -EACCES;
4348 goto out;
4349 }
4350
4351 sa = memdup_user(arg, sizeof(*sa));
4352 if (IS_ERR(sa)) {
4353 ret = PTR_ERR(sa);
4354 sa = NULL;
4355 goto out;
4356 }
4357
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004358 /*
4359 * 1 - root item
4360 * 2 - uuid items (received uuid + subvol uuid)
4361 */
4362 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004363 if (IS_ERR(trans)) {
4364 ret = PTR_ERR(trans);
4365 trans = NULL;
4366 goto out;
4367 }
4368
4369 sa->rtransid = trans->transid;
4370 sa->rtime.sec = ct.tv_sec;
4371 sa->rtime.nsec = ct.tv_nsec;
4372
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004373 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4374 BTRFS_UUID_SIZE);
4375 if (received_uuid_changed &&
4376 !btrfs_is_empty_uuid(root_item->received_uuid))
4377 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4378 root_item->received_uuid,
4379 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4380 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004381 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4382 btrfs_set_root_stransid(root_item, sa->stransid);
4383 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004384 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4385 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4386 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4387 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004388
4389 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4390 &root->root_key, &root->root_item);
4391 if (ret < 0) {
4392 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004393 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004394 }
4395 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4396 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4397 sa->uuid,
4398 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4399 root->root_key.objectid);
4400 if (ret < 0 && ret != -EEXIST) {
4401 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004402 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004403 }
4404 }
4405 ret = btrfs_commit_transaction(trans, root);
4406 if (ret < 0) {
4407 btrfs_abort_transaction(trans, root, ret);
4408 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004409 }
4410
4411 ret = copy_to_user(arg, sa, sizeof(*sa));
4412 if (ret)
4413 ret = -EFAULT;
4414
4415out:
4416 kfree(sa);
4417 up_write(&root->fs_info->subvol_sem);
4418 mnt_drop_write_file(file);
4419 return ret;
4420}
4421
jeff.liu867ab662013-01-05 02:48:01 +00004422static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4423{
Al Viro6d0379e2013-06-16 19:32:35 +04004424 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004425 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004426 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004427 char label[BTRFS_LABEL_SIZE];
4428
4429 spin_lock(&root->fs_info->super_lock);
4430 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4431 spin_unlock(&root->fs_info->super_lock);
4432
4433 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004434
4435 if (len == BTRFS_LABEL_SIZE) {
4436 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4437 --len);
4438 }
4439
jeff.liu867ab662013-01-05 02:48:01 +00004440 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004441
4442 return ret ? -EFAULT : 0;
4443}
4444
jeff.liua8bfd4a2013-01-05 02:48:08 +00004445static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4446{
Al Viro6d0379e2013-06-16 19:32:35 +04004447 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004448 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4449 struct btrfs_trans_handle *trans;
4450 char label[BTRFS_LABEL_SIZE];
4451 int ret;
4452
4453 if (!capable(CAP_SYS_ADMIN))
4454 return -EPERM;
4455
4456 if (copy_from_user(label, arg, sizeof(label)))
4457 return -EFAULT;
4458
4459 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4460 pr_err("btrfs: unable to set label with more than %d bytes\n",
4461 BTRFS_LABEL_SIZE - 1);
4462 return -EINVAL;
4463 }
4464
4465 ret = mnt_want_write_file(file);
4466 if (ret)
4467 return ret;
4468
jeff.liua8bfd4a2013-01-05 02:48:08 +00004469 trans = btrfs_start_transaction(root, 0);
4470 if (IS_ERR(trans)) {
4471 ret = PTR_ERR(trans);
4472 goto out_unlock;
4473 }
4474
Anand Jaina1b83ac2013-07-19 17:39:32 +08004475 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004476 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004477 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004478 ret = btrfs_end_transaction(trans, root);
4479
4480out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004481 mnt_drop_write_file(file);
4482 return ret;
4483}
4484
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004485long btrfs_ioctl(struct file *file, unsigned int
4486 cmd, unsigned long arg)
4487{
Al Viro496ad9a2013-01-23 17:07:38 -05004488 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004489 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004490
4491 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004492 case FS_IOC_GETFLAGS:
4493 return btrfs_ioctl_getflags(file, argp);
4494 case FS_IOC_SETFLAGS:
4495 return btrfs_ioctl_setflags(file, argp);
4496 case FS_IOC_GETVERSION:
4497 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004498 case FITRIM:
4499 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004500 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004501 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004502 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004503 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004504 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004505 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004506 case BTRFS_IOC_SUBVOL_CREATE_V2:
4507 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004508 case BTRFS_IOC_SNAP_DESTROY:
4509 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004510 case BTRFS_IOC_SUBVOL_GETFLAGS:
4511 return btrfs_ioctl_subvol_getflags(file, argp);
4512 case BTRFS_IOC_SUBVOL_SETFLAGS:
4513 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004514 case BTRFS_IOC_DEFAULT_SUBVOL:
4515 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004516 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004517 return btrfs_ioctl_defrag(file, NULL);
4518 case BTRFS_IOC_DEFRAG_RANGE:
4519 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004520 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004521 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004522 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004523 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004524 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004525 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004526 case BTRFS_IOC_FS_INFO:
4527 return btrfs_ioctl_fs_info(root, argp);
4528 case BTRFS_IOC_DEV_INFO:
4529 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004530 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004531 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004532 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004533 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4534 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004535 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004536 case BTRFS_IOC_TRANS_START:
4537 return btrfs_ioctl_trans_start(file);
4538 case BTRFS_IOC_TRANS_END:
4539 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004540 case BTRFS_IOC_TREE_SEARCH:
4541 return btrfs_ioctl_tree_search(file, argp);
4542 case BTRFS_IOC_INO_LOOKUP:
4543 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004544 case BTRFS_IOC_INO_PATHS:
4545 return btrfs_ioctl_ino_to_path(root, argp);
4546 case BTRFS_IOC_LOGICAL_INO:
4547 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004548 case BTRFS_IOC_SPACE_INFO:
4549 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004550 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004551 btrfs_sync_fs(file->f_dentry->d_sb, 1);
4552 return 0;
Sage Weil46204592010-10-29 15:41:32 -04004553 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004554 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004555 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004556 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004557 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004558 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004559 case BTRFS_IOC_SCRUB_CANCEL:
4560 return btrfs_ioctl_scrub_cancel(root, argp);
4561 case BTRFS_IOC_SCRUB_PROGRESS:
4562 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004563 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004564 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004565 case BTRFS_IOC_BALANCE_CTL:
4566 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004567 case BTRFS_IOC_BALANCE_PROGRESS:
4568 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004569 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4570 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004571 case BTRFS_IOC_SEND:
4572 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004573 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004574 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004575 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004576 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004577 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004578 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004579 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004580 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004581 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004582 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004583 case BTRFS_IOC_QUOTA_RESCAN:
4584 return btrfs_ioctl_quota_rescan(file, argp);
4585 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4586 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004587 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4588 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004589 case BTRFS_IOC_DEV_REPLACE:
4590 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004591 case BTRFS_IOC_GET_FSLABEL:
4592 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004593 case BTRFS_IOC_SET_FSLABEL:
4594 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004595 case BTRFS_IOC_FILE_EXTENT_SAME:
4596 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004597 }
4598
4599 return -ENOTTY;
4600}