blob: 996951a8d28d6a4be64d5fba988ed20038df356e [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 Zefan306424c2011-12-14 20:12:02 -0500297 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400298 inode_inc_iversion(inode);
Li Zefan306424c2011-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 Bo0abd5b172013-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 Bo0abd5b172013-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 }
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001424 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001425 }
Miao Xiedba60f32012-12-21 09:19:51 +00001426
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001427 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001429 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001430 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001431 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001432 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433 }
Miao Xiedba60f32012-12-21 09:19:51 +00001434
1435 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001436 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001437 "readonly device %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001438 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001439 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001440 goto out_free;
1441 }
1442
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001443 if (!strcmp(sizestr, "max"))
1444 new_size = device->bdev->bd_inode->i_size;
1445 else {
1446 if (sizestr[0] == '-') {
1447 mod = -1;
1448 sizestr++;
1449 } else if (sizestr[0] == '+') {
1450 mod = 1;
1451 sizestr++;
1452 }
Akinobu Mita91748462010-02-28 10:59:11 +00001453 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001454 if (new_size == 0) {
1455 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001456 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001457 }
1458 }
1459
Stefan Behrens63a212a2012-11-05 18:29:28 +01001460 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001461 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001462 goto out_free;
1463 }
1464
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001465 old_size = device->total_bytes;
1466
1467 if (mod < 0) {
1468 if (new_size > old_size) {
1469 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001470 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001471 }
1472 new_size = old_size - new_size;
1473 } else if (mod > 0) {
1474 new_size = old_size + new_size;
1475 }
1476
1477 if (new_size < 256 * 1024 * 1024) {
1478 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001479 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001480 }
1481 if (new_size > device->bdev->bd_inode->i_size) {
1482 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001483 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001484 }
1485
1486 do_div(new_size, root->sectorsize);
1487 new_size *= root->sectorsize;
1488
Josef Bacik606686e2012-06-04 14:03:51 -04001489 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001490 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001491
1492 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001493 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001494 if (IS_ERR(trans)) {
1495 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001496 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001497 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001498 ret = btrfs_grow_device(trans, device, new_size);
1499 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001500 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001501 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001502 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001503
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001504out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001506out:
1507 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001508 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001509 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001510 return ret;
1511}
1512
Sage Weil72fd0322010-10-29 15:41:32 -04001513static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001514 char *name, unsigned long fd, int subvol,
1515 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001516 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001517{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001518 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001519 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520
Liu Boa874a632012-06-29 03:58:46 -06001521 ret = mnt_want_write_file(file);
1522 if (ret)
1523 goto out;
1524
Sage Weil72fd0322010-10-29 15:41:32 -04001525 namelen = strlen(name);
1526 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001527 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001528 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 }
1530
Chris Mason16780ca2012-02-20 22:14:55 -05001531 if (name[0] == '.' &&
1532 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1533 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001534 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001535 }
1536
Chris Mason3de45862008-11-17 21:02:50 -05001537 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001538 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001539 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001540 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001541 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001542 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001543 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001544 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001545 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001546 }
1547
Al Viro496ad9a2013-01-23 17:07:38 -05001548 src_inode = file_inode(src.file);
1549 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001550 printk(KERN_INFO "btrfs: Snapshot src from "
1551 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001552 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001553 } else {
1554 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1555 BTRFS_I(src_inode)->root,
1556 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001557 }
Al Viro2903ff02012-08-28 12:52:22 -04001558 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001559 }
Liu Boa874a632012-06-29 03:58:46 -06001560out_drop_write:
1561 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001562out:
Sage Weil72fd0322010-10-29 15:41:32 -04001563 return ret;
1564}
1565
1566static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001567 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001568{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001569 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001570 int ret;
1571
Li Zefanfa0d2b92010-12-20 15:53:28 +08001572 vol_args = memdup_user(arg, sizeof(*vol_args));
1573 if (IS_ERR(vol_args))
1574 return PTR_ERR(vol_args);
1575 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001576
Li Zefanfa0d2b92010-12-20 15:53:28 +08001577 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001578 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001579 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001580
Li Zefanfa0d2b92010-12-20 15:53:28 +08001581 kfree(vol_args);
1582 return ret;
1583}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001584
Li Zefanfa0d2b92010-12-20 15:53:28 +08001585static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1586 void __user *arg, int subvol)
1587{
1588 struct btrfs_ioctl_vol_args_v2 *vol_args;
1589 int ret;
1590 u64 transid = 0;
1591 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001592 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001593 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001594
Li Zefanfa0d2b92010-12-20 15:53:28 +08001595 vol_args = memdup_user(arg, sizeof(*vol_args));
1596 if (IS_ERR(vol_args))
1597 return PTR_ERR(vol_args);
1598 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001599
Li Zefanb83cc962010-12-20 16:04:08 +08001600 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001601 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1602 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001603 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001604 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001605 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001606
1607 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1608 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001609 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1610 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001611 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1612 if (vol_args->size > PAGE_CACHE_SIZE) {
1613 ret = -EINVAL;
1614 goto out;
1615 }
1616 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1617 if (IS_ERR(inherit)) {
1618 ret = PTR_ERR(inherit);
1619 goto out;
1620 }
1621 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001622
1623 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001624 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001625 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001626
1627 if (ret == 0 && ptr &&
1628 copy_to_user(arg +
1629 offsetof(struct btrfs_ioctl_vol_args_v2,
1630 transid), ptr, sizeof(*ptr)))
1631 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001632out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001634 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001635 return ret;
1636}
1637
Li Zefan0caa1022010-12-20 16:30:25 +08001638static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1639 void __user *arg)
1640{
Al Viro496ad9a2013-01-23 17:07:38 -05001641 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001642 struct btrfs_root *root = BTRFS_I(inode)->root;
1643 int ret = 0;
1644 u64 flags = 0;
1645
Li Zefan33345d012011-04-20 10:31:50 +08001646 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001647 return -EINVAL;
1648
1649 down_read(&root->fs_info->subvol_sem);
1650 if (btrfs_root_readonly(root))
1651 flags |= BTRFS_SUBVOL_RDONLY;
1652 up_read(&root->fs_info->subvol_sem);
1653
1654 if (copy_to_user(arg, &flags, sizeof(flags)))
1655 ret = -EFAULT;
1656
1657 return ret;
1658}
1659
1660static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1661 void __user *arg)
1662{
Al Viro496ad9a2013-01-23 17:07:38 -05001663 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001664 struct btrfs_root *root = BTRFS_I(inode)->root;
1665 struct btrfs_trans_handle *trans;
1666 u64 root_flags;
1667 u64 flags;
1668 int ret = 0;
1669
Liu Bob9ca0662012-06-29 03:58:49 -06001670 ret = mnt_want_write_file(file);
1671 if (ret)
1672 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001673
Liu Bob9ca0662012-06-29 03:58:49 -06001674 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1675 ret = -EINVAL;
1676 goto out_drop_write;
1677 }
Li Zefan0caa1022010-12-20 16:30:25 +08001678
Liu Bob9ca0662012-06-29 03:58:49 -06001679 if (copy_from_user(&flags, arg, sizeof(flags))) {
1680 ret = -EFAULT;
1681 goto out_drop_write;
1682 }
Li Zefan0caa1022010-12-20 16:30:25 +08001683
Liu Bob9ca0662012-06-29 03:58:49 -06001684 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1685 ret = -EINVAL;
1686 goto out_drop_write;
1687 }
Li Zefan0caa1022010-12-20 16:30:25 +08001688
Liu Bob9ca0662012-06-29 03:58:49 -06001689 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1690 ret = -EOPNOTSUPP;
1691 goto out_drop_write;
1692 }
Li Zefan0caa1022010-12-20 16:30:25 +08001693
Liu Bob9ca0662012-06-29 03:58:49 -06001694 if (!inode_owner_or_capable(inode)) {
1695 ret = -EACCES;
1696 goto out_drop_write;
1697 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001698
Li Zefan0caa1022010-12-20 16:30:25 +08001699 down_write(&root->fs_info->subvol_sem);
1700
1701 /* nothing to do */
1702 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001703 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001704
1705 root_flags = btrfs_root_flags(&root->root_item);
1706 if (flags & BTRFS_SUBVOL_RDONLY)
1707 btrfs_set_root_flags(&root->root_item,
1708 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1709 else
1710 btrfs_set_root_flags(&root->root_item,
1711 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1712
1713 trans = btrfs_start_transaction(root, 1);
1714 if (IS_ERR(trans)) {
1715 ret = PTR_ERR(trans);
1716 goto out_reset;
1717 }
1718
Li Zefanb4dc2b82011-02-16 06:06:34 +00001719 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001720 &root->root_key, &root->root_item);
1721
1722 btrfs_commit_transaction(trans, root);
1723out_reset:
1724 if (ret)
1725 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001726out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001727 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001728out_drop_write:
1729 mnt_drop_write_file(file);
1730out:
Li Zefan0caa1022010-12-20 16:30:25 +08001731 return ret;
1732}
1733
Yan, Zheng76dda932009-09-21 16:00:26 -04001734/*
1735 * helper to check if the subvolume references other subvolumes
1736 */
1737static noinline int may_destroy_subvol(struct btrfs_root *root)
1738{
1739 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001740 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001741 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001742 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001743 int ret;
1744
1745 path = btrfs_alloc_path();
1746 if (!path)
1747 return -ENOMEM;
1748
Josef Bacik175a2b82013-08-12 15:36:44 -04001749 /* Make sure this root isn't set as the default subvol */
1750 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1751 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1752 dir_id, "default", 7, 0);
1753 if (di && !IS_ERR(di)) {
1754 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1755 if (key.objectid == root->root_key.objectid) {
1756 ret = -ENOTEMPTY;
1757 goto out;
1758 }
1759 btrfs_release_path(path);
1760 }
1761
Yan, Zheng76dda932009-09-21 16:00:26 -04001762 key.objectid = root->root_key.objectid;
1763 key.type = BTRFS_ROOT_REF_KEY;
1764 key.offset = (u64)-1;
1765
1766 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1767 &key, path, 0, 0);
1768 if (ret < 0)
1769 goto out;
1770 BUG_ON(ret == 0);
1771
1772 ret = 0;
1773 if (path->slots[0] > 0) {
1774 path->slots[0]--;
1775 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1776 if (key.objectid == root->root_key.objectid &&
1777 key.type == BTRFS_ROOT_REF_KEY)
1778 ret = -ENOTEMPTY;
1779 }
1780out:
1781 btrfs_free_path(path);
1782 return ret;
1783}
1784
Chris Masonac8e9812010-02-28 15:39:26 -05001785static noinline int key_in_sk(struct btrfs_key *key,
1786 struct btrfs_ioctl_search_key *sk)
1787{
Chris Masonabc6e132010-03-18 12:10:08 -04001788 struct btrfs_key test;
1789 int ret;
1790
1791 test.objectid = sk->min_objectid;
1792 test.type = sk->min_type;
1793 test.offset = sk->min_offset;
1794
1795 ret = btrfs_comp_cpu_keys(key, &test);
1796 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001797 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001798
1799 test.objectid = sk->max_objectid;
1800 test.type = sk->max_type;
1801 test.offset = sk->max_offset;
1802
1803 ret = btrfs_comp_cpu_keys(key, &test);
1804 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001805 return 0;
1806 return 1;
1807}
1808
1809static noinline int copy_to_sk(struct btrfs_root *root,
1810 struct btrfs_path *path,
1811 struct btrfs_key *key,
1812 struct btrfs_ioctl_search_key *sk,
1813 char *buf,
1814 unsigned long *sk_offset,
1815 int *num_found)
1816{
1817 u64 found_transid;
1818 struct extent_buffer *leaf;
1819 struct btrfs_ioctl_search_header sh;
1820 unsigned long item_off;
1821 unsigned long item_len;
1822 int nritems;
1823 int i;
1824 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001825 int ret = 0;
1826
1827 leaf = path->nodes[0];
1828 slot = path->slots[0];
1829 nritems = btrfs_header_nritems(leaf);
1830
1831 if (btrfs_header_generation(leaf) > sk->max_transid) {
1832 i = nritems;
1833 goto advance_key;
1834 }
1835 found_transid = btrfs_header_generation(leaf);
1836
1837 for (i = slot; i < nritems; i++) {
1838 item_off = btrfs_item_ptr_offset(leaf, i);
1839 item_len = btrfs_item_size_nr(leaf, i);
1840
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001841 btrfs_item_key_to_cpu(leaf, key, i);
1842 if (!key_in_sk(key, sk))
1843 continue;
1844
1845 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001846 item_len = 0;
1847
1848 if (sizeof(sh) + item_len + *sk_offset >
1849 BTRFS_SEARCH_ARGS_BUFSIZE) {
1850 ret = 1;
1851 goto overflow;
1852 }
1853
Chris Masonac8e9812010-02-28 15:39:26 -05001854 sh.objectid = key->objectid;
1855 sh.offset = key->offset;
1856 sh.type = key->type;
1857 sh.len = item_len;
1858 sh.transid = found_transid;
1859
1860 /* copy search result header */
1861 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1862 *sk_offset += sizeof(sh);
1863
1864 if (item_len) {
1865 char *p = buf + *sk_offset;
1866 /* copy the item */
1867 read_extent_buffer(leaf, p,
1868 item_off, item_len);
1869 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001870 }
Hugo Millse2156862011-05-14 17:43:41 +00001871 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001872
1873 if (*num_found >= sk->nr_items)
1874 break;
1875 }
1876advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001877 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001878 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1879 key->offset++;
1880 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1881 key->offset = 0;
1882 key->type++;
1883 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1884 key->offset = 0;
1885 key->type = 0;
1886 key->objectid++;
1887 } else
1888 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001889overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001890 return ret;
1891}
1892
1893static noinline int search_ioctl(struct inode *inode,
1894 struct btrfs_ioctl_search_args *args)
1895{
1896 struct btrfs_root *root;
1897 struct btrfs_key key;
1898 struct btrfs_key max_key;
1899 struct btrfs_path *path;
1900 struct btrfs_ioctl_search_key *sk = &args->key;
1901 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1902 int ret;
1903 int num_found = 0;
1904 unsigned long sk_offset = 0;
1905
1906 path = btrfs_alloc_path();
1907 if (!path)
1908 return -ENOMEM;
1909
1910 if (sk->tree_id == 0) {
1911 /* search the root of the inode that was passed */
1912 root = BTRFS_I(inode)->root;
1913 } else {
1914 key.objectid = sk->tree_id;
1915 key.type = BTRFS_ROOT_ITEM_KEY;
1916 key.offset = (u64)-1;
1917 root = btrfs_read_fs_root_no_name(info, &key);
1918 if (IS_ERR(root)) {
1919 printk(KERN_ERR "could not find root %llu\n",
1920 sk->tree_id);
1921 btrfs_free_path(path);
1922 return -ENOENT;
1923 }
1924 }
1925
1926 key.objectid = sk->min_objectid;
1927 key.type = sk->min_type;
1928 key.offset = sk->min_offset;
1929
1930 max_key.objectid = sk->max_objectid;
1931 max_key.type = sk->max_type;
1932 max_key.offset = sk->max_offset;
1933
1934 path->keep_locks = 1;
1935
1936 while(1) {
Eric Sandeende78b512013-01-31 18:21:12 +00001937 ret = btrfs_search_forward(root, &key, &max_key, path,
Chris Masonac8e9812010-02-28 15:39:26 -05001938 sk->min_transid);
1939 if (ret != 0) {
1940 if (ret > 0)
1941 ret = 0;
1942 goto err;
1943 }
1944 ret = copy_to_sk(root, path, &key, sk, args->buf,
1945 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001946 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001947 if (ret || num_found >= sk->nr_items)
1948 break;
1949
1950 }
1951 ret = 0;
1952err:
1953 sk->nr_items = num_found;
1954 btrfs_free_path(path);
1955 return ret;
1956}
1957
1958static noinline int btrfs_ioctl_tree_search(struct file *file,
1959 void __user *argp)
1960{
1961 struct btrfs_ioctl_search_args *args;
1962 struct inode *inode;
1963 int ret;
1964
1965 if (!capable(CAP_SYS_ADMIN))
1966 return -EPERM;
1967
Julia Lawall2354d082010-10-29 15:14:18 -04001968 args = memdup_user(argp, sizeof(*args));
1969 if (IS_ERR(args))
1970 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001971
Al Viro496ad9a2013-01-23 17:07:38 -05001972 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001973 ret = search_ioctl(inode, args);
1974 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1975 ret = -EFAULT;
1976 kfree(args);
1977 return ret;
1978}
1979
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001980/*
Chris Masonac8e9812010-02-28 15:39:26 -05001981 * Search INODE_REFs to identify path name of 'dirid' directory
1982 * in a 'tree_id' tree. and sets path name to 'name'.
1983 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001984static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1985 u64 tree_id, u64 dirid, char *name)
1986{
1987 struct btrfs_root *root;
1988 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001989 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001990 int ret = -1;
1991 int slot;
1992 int len;
1993 int total_len = 0;
1994 struct btrfs_inode_ref *iref;
1995 struct extent_buffer *l;
1996 struct btrfs_path *path;
1997
1998 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1999 name[0]='\0';
2000 return 0;
2001 }
2002
2003 path = btrfs_alloc_path();
2004 if (!path)
2005 return -ENOMEM;
2006
Chris Masonac8e9812010-02-28 15:39:26 -05002007 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002008
2009 key.objectid = tree_id;
2010 key.type = BTRFS_ROOT_ITEM_KEY;
2011 key.offset = (u64)-1;
2012 root = btrfs_read_fs_root_no_name(info, &key);
2013 if (IS_ERR(root)) {
2014 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002015 ret = -ENOENT;
2016 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002017 }
2018
2019 key.objectid = dirid;
2020 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002021 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002022
2023 while(1) {
2024 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2025 if (ret < 0)
2026 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002027 else if (ret > 0) {
2028 ret = btrfs_previous_item(root, path, dirid,
2029 BTRFS_INODE_REF_KEY);
2030 if (ret < 0)
2031 goto out;
2032 else if (ret > 0) {
2033 ret = -ENOENT;
2034 goto out;
2035 }
2036 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002037
2038 l = path->nodes[0];
2039 slot = path->slots[0];
2040 btrfs_item_key_to_cpu(l, &key, slot);
2041
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002042 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2043 len = btrfs_inode_ref_name_len(l, iref);
2044 ptr -= len + 1;
2045 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002046 if (ptr < name) {
2047 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002048 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002049 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002050
2051 *(ptr + len) = '/';
2052 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2053
2054 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2055 break;
2056
David Sterbab3b4aa72011-04-21 01:20:15 +02002057 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002058 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002059 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002060 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002061 }
Li Zefan77906a502011-07-14 03:16:00 +00002062 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002063 name[total_len]='\0';
2064 ret = 0;
2065out:
2066 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002067 return ret;
2068}
2069
2070static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2071 void __user *argp)
2072{
2073 struct btrfs_ioctl_ino_lookup_args *args;
2074 struct inode *inode;
2075 int ret;
2076
2077 if (!capable(CAP_SYS_ADMIN))
2078 return -EPERM;
2079
Julia Lawall2354d082010-10-29 15:14:18 -04002080 args = memdup_user(argp, sizeof(*args));
2081 if (IS_ERR(args))
2082 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002083
Al Viro496ad9a2013-01-23 17:07:38 -05002084 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002085
Chris Mason1b53ac42010-03-18 12:17:05 -04002086 if (args->treeid == 0)
2087 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2088
Chris Masonac8e9812010-02-28 15:39:26 -05002089 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2090 args->treeid, args->objectid,
2091 args->name);
2092
2093 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2094 ret = -EFAULT;
2095
2096 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002097 return ret;
2098}
2099
Yan, Zheng76dda932009-09-21 16:00:26 -04002100static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2101 void __user *arg)
2102{
2103 struct dentry *parent = fdentry(file);
2104 struct dentry *dentry;
2105 struct inode *dir = parent->d_inode;
2106 struct inode *inode;
2107 struct btrfs_root *root = BTRFS_I(dir)->root;
2108 struct btrfs_root *dest = NULL;
2109 struct btrfs_ioctl_vol_args *vol_args;
2110 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002111 struct btrfs_block_rsv block_rsv;
2112 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002113 int namelen;
2114 int ret;
2115 int err = 0;
2116
Yan, Zheng76dda932009-09-21 16:00:26 -04002117 vol_args = memdup_user(arg, sizeof(*vol_args));
2118 if (IS_ERR(vol_args))
2119 return PTR_ERR(vol_args);
2120
2121 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2122 namelen = strlen(vol_args->name);
2123 if (strchr(vol_args->name, '/') ||
2124 strncmp(vol_args->name, "..", namelen) == 0) {
2125 err = -EINVAL;
2126 goto out;
2127 }
2128
Al Viroa561be72011-11-23 11:57:51 -05002129 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002130 if (err)
2131 goto out;
2132
David Sterba5c50c9b2013-03-22 18:12:51 +00002133 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2134 if (err == -EINTR)
2135 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002136 dentry = lookup_one_len(vol_args->name, parent, namelen);
2137 if (IS_ERR(dentry)) {
2138 err = PTR_ERR(dentry);
2139 goto out_unlock_dir;
2140 }
2141
2142 if (!dentry->d_inode) {
2143 err = -ENOENT;
2144 goto out_dput;
2145 }
2146
2147 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002148 dest = BTRFS_I(inode)->root;
2149 if (!capable(CAP_SYS_ADMIN)){
2150 /*
2151 * Regular user. Only allow this with a special mount
2152 * option, when the user has write+exec access to the
2153 * subvol root, and when rmdir(2) would have been
2154 * allowed.
2155 *
2156 * Note that this is _not_ check that the subvol is
2157 * empty or doesn't contain data that we wouldn't
2158 * otherwise be able to delete.
2159 *
2160 * Users who want to delete empty subvols should try
2161 * rmdir(2).
2162 */
2163 err = -EPERM;
2164 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2165 goto out_dput;
2166
2167 /*
2168 * Do not allow deletion if the parent dir is the same
2169 * as the dir to be deleted. That means the ioctl
2170 * must be called on the dentry referencing the root
2171 * of the subvol, not a random directory contained
2172 * within it.
2173 */
2174 err = -EINVAL;
2175 if (root == dest)
2176 goto out_dput;
2177
2178 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2179 if (err)
2180 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002181 }
2182
Miao Xie5c39da52012-10-22 11:39:53 +00002183 /* check if subvolume may be deleted by a user */
2184 err = btrfs_may_delete(dir, dentry, 1);
2185 if (err)
2186 goto out_dput;
2187
Li Zefan33345d012011-04-20 10:31:50 +08002188 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002189 err = -EINVAL;
2190 goto out_dput;
2191 }
2192
Yan, Zheng76dda932009-09-21 16:00:26 -04002193 mutex_lock(&inode->i_mutex);
2194 err = d_invalidate(dentry);
2195 if (err)
2196 goto out_unlock;
2197
2198 down_write(&root->fs_info->subvol_sem);
2199
2200 err = may_destroy_subvol(dest);
2201 if (err)
2202 goto out_up_write;
2203
Miao Xiec58aaad2013-02-28 10:05:36 +00002204 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2205 /*
2206 * One for dir inode, two for dir entries, two for root
2207 * ref/backref.
2208 */
2209 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002210 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002211 if (err)
2212 goto out_up_write;
2213
Yan, Zhenga22285a2010-05-16 10:48:46 -04002214 trans = btrfs_start_transaction(root, 0);
2215 if (IS_ERR(trans)) {
2216 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002217 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002218 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002219 trans->block_rsv = &block_rsv;
2220 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002221
Yan, Zheng76dda932009-09-21 16:00:26 -04002222 ret = btrfs_unlink_subvol(trans, root, dir,
2223 dest->root_key.objectid,
2224 dentry->d_name.name,
2225 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002226 if (ret) {
2227 err = ret;
2228 btrfs_abort_transaction(trans, root, ret);
2229 goto out_end_trans;
2230 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002231
2232 btrfs_record_root_in_trans(trans, dest);
2233
2234 memset(&dest->root_item.drop_progress, 0,
2235 sizeof(dest->root_item.drop_progress));
2236 dest->root_item.drop_level = 0;
2237 btrfs_set_root_refs(&dest->root_item, 0);
2238
Yan, Zhengd68fc572010-05-16 10:49:58 -04002239 if (!xchg(&dest->orphan_item_inserted, 1)) {
2240 ret = btrfs_insert_orphan_item(trans,
2241 root->fs_info->tree_root,
2242 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002243 if (ret) {
2244 btrfs_abort_transaction(trans, root, ret);
2245 err = ret;
2246 goto out_end_trans;
2247 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002248 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002249
2250 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2251 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2252 dest->root_key.objectid);
2253 if (ret && ret != -ENOENT) {
2254 btrfs_abort_transaction(trans, root, ret);
2255 err = ret;
2256 goto out_end_trans;
2257 }
2258 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2259 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2260 dest->root_item.received_uuid,
2261 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2262 dest->root_key.objectid);
2263 if (ret && ret != -ENOENT) {
2264 btrfs_abort_transaction(trans, root, ret);
2265 err = ret;
2266 goto out_end_trans;
2267 }
2268 }
2269
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002270out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002271 trans->block_rsv = NULL;
2272 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002273 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002274 if (ret && !err)
2275 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002276 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002277out_release:
2278 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002279out_up_write:
2280 up_write(&root->fs_info->subvol_sem);
2281out_unlock:
2282 mutex_unlock(&inode->i_mutex);
2283 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002284 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002285 btrfs_invalidate_inodes(dest);
2286 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002287
2288 /* the last ref */
2289 if (dest->cache_inode) {
2290 iput(dest->cache_inode);
2291 dest->cache_inode = NULL;
2292 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002293 }
2294out_dput:
2295 dput(dentry);
2296out_unlock_dir:
2297 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002298 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002299out:
2300 kfree(vol_args);
2301 return err;
2302}
2303
Chris Mason1e701a32010-03-11 09:42:04 -05002304static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002305{
Al Viro496ad9a2013-01-23 17:07:38 -05002306 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002307 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002308 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002309 int ret;
2310
Ilya Dryomov25122d12013-01-20 15:57:57 +02002311 ret = mnt_want_write_file(file);
2312 if (ret)
2313 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002314
Ilya Dryomov25122d12013-01-20 15:57:57 +02002315 if (btrfs_root_readonly(root)) {
2316 ret = -EROFS;
2317 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002318 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002319
2320 switch (inode->i_mode & S_IFMT) {
2321 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002322 if (!capable(CAP_SYS_ADMIN)) {
2323 ret = -EPERM;
2324 goto out;
2325 }
Eric Sandeende78b512013-01-31 18:21:12 +00002326 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002327 if (ret)
2328 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002329 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002330 break;
2331 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002332 if (!(file->f_mode & FMODE_WRITE)) {
2333 ret = -EINVAL;
2334 goto out;
2335 }
Chris Mason1e701a32010-03-11 09:42:04 -05002336
2337 range = kzalloc(sizeof(*range), GFP_KERNEL);
2338 if (!range) {
2339 ret = -ENOMEM;
2340 goto out;
2341 }
2342
2343 if (argp) {
2344 if (copy_from_user(range, argp,
2345 sizeof(*range))) {
2346 ret = -EFAULT;
2347 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002348 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002349 }
2350 /* compression requires us to start the IO */
2351 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2352 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2353 range->extent_thresh = (u32)-1;
2354 }
2355 } else {
2356 /* the rest are all set to zero by kzalloc */
2357 range->len = (u64)-1;
2358 }
Al Viro496ad9a2013-01-23 17:07:38 -05002359 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002360 range, 0, 0);
2361 if (ret > 0)
2362 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002363 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002364 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002365 default:
2366 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002367 }
Chris Masone441d542009-01-05 16:57:23 -05002368out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002369 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002370 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002371}
2372
Christoph Hellwigb2950862008-12-02 09:54:17 -05002373static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002374{
2375 struct btrfs_ioctl_vol_args *vol_args;
2376 int ret;
2377
Chris Masone441d542009-01-05 16:57:23 -05002378 if (!capable(CAP_SYS_ADMIN))
2379 return -EPERM;
2380
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002381 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2382 1)) {
2383 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002384 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002385 }
2386
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002387 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002388 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002389 if (IS_ERR(vol_args)) {
2390 ret = PTR_ERR(vol_args);
2391 goto out;
2392 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002393
Mark Fasheh5516e592008-07-24 12:20:14 -04002394 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002395 ret = btrfs_init_new_device(root, vol_args->name);
2396
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002397 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002398out:
2399 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002400 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002401 return ret;
2402}
2403
Miao Xieda249272012-11-26 08:44:50 +00002404static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002405{
Al Viro496ad9a2013-01-23 17:07:38 -05002406 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407 struct btrfs_ioctl_vol_args *vol_args;
2408 int ret;
2409
Chris Masone441d542009-01-05 16:57:23 -05002410 if (!capable(CAP_SYS_ADMIN))
2411 return -EPERM;
2412
Miao Xieda249272012-11-26 08:44:50 +00002413 ret = mnt_want_write_file(file);
2414 if (ret)
2415 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002416
Li Zefandae7b662009-04-08 15:06:54 +08002417 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002418 if (IS_ERR(vol_args)) {
2419 ret = PTR_ERR(vol_args);
2420 goto out;
2421 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002422
Mark Fasheh5516e592008-07-24 12:20:14 -04002423 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424
Anand Jain183860f2013-05-17 10:52:45 +00002425 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2426 1)) {
2427 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2428 goto out;
2429 }
2430
2431 mutex_lock(&root->fs_info->volume_mutex);
2432 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002433 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002434 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002435
2436out:
2437 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002438 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002439 return ret;
2440}
2441
Jan Schmidt475f6382011-03-11 15:41:01 +01002442static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2443{
Li Zefan027ed2f2011-06-08 08:27:56 +00002444 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002445 struct btrfs_device *device;
2446 struct btrfs_device *next;
2447 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002448 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002449
2450 if (!capable(CAP_SYS_ADMIN))
2451 return -EPERM;
2452
Li Zefan027ed2f2011-06-08 08:27:56 +00002453 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2454 if (!fi_args)
2455 return -ENOMEM;
2456
2457 fi_args->num_devices = fs_devices->num_devices;
2458 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002459
2460 mutex_lock(&fs_devices->device_list_mutex);
2461 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002462 if (device->devid > fi_args->max_id)
2463 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002464 }
2465 mutex_unlock(&fs_devices->device_list_mutex);
2466
Li Zefan027ed2f2011-06-08 08:27:56 +00002467 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2468 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002469
Li Zefan027ed2f2011-06-08 08:27:56 +00002470 kfree(fi_args);
2471 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002472}
2473
2474static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2475{
2476 struct btrfs_ioctl_dev_info_args *di_args;
2477 struct btrfs_device *dev;
2478 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2479 int ret = 0;
2480 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002481
2482 if (!capable(CAP_SYS_ADMIN))
2483 return -EPERM;
2484
2485 di_args = memdup_user(arg, sizeof(*di_args));
2486 if (IS_ERR(di_args))
2487 return PTR_ERR(di_args);
2488
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002489 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002490 s_uuid = di_args->uuid;
2491
2492 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002493 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002494
2495 if (!dev) {
2496 ret = -ENODEV;
2497 goto out;
2498 }
2499
2500 di_args->devid = dev->devid;
2501 di_args->bytes_used = dev->bytes_used;
2502 di_args->total_bytes = dev->total_bytes;
2503 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002504 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002505 struct rcu_string *name;
2506
2507 rcu_read_lock();
2508 name = rcu_dereference(dev->name);
2509 strncpy(di_args->path, name->str, sizeof(di_args->path));
2510 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002511 di_args->path[sizeof(di_args->path) - 1] = 0;
2512 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002513 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002514 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002515
2516out:
David Sterba55793c02013-04-26 15:20:23 +00002517 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002518 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2519 ret = -EFAULT;
2520
2521 kfree(di_args);
2522 return ret;
2523}
2524
Mark Fasheh416161d2013-08-06 11:42:51 -07002525static struct page *extent_same_get_page(struct inode *inode, u64 off)
2526{
2527 struct page *page;
2528 pgoff_t index;
2529 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2530
2531 index = off >> PAGE_CACHE_SHIFT;
2532
2533 page = grab_cache_page(inode->i_mapping, index);
2534 if (!page)
2535 return NULL;
2536
2537 if (!PageUptodate(page)) {
2538 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2539 0))
2540 return NULL;
2541 lock_page(page);
2542 if (!PageUptodate(page)) {
2543 unlock_page(page);
2544 page_cache_release(page);
2545 return NULL;
2546 }
2547 }
2548 unlock_page(page);
2549
2550 return page;
2551}
2552
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002553static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2554{
2555 /* do any pending delalloc/csum calc on src, one way or
2556 another, and lock file content */
2557 while (1) {
2558 struct btrfs_ordered_extent *ordered;
2559 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2560 ordered = btrfs_lookup_first_ordered_extent(inode,
2561 off + len - 1);
2562 if (!ordered &&
2563 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2564 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2565 break;
2566 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2567 if (ordered)
2568 btrfs_put_ordered_extent(ordered);
2569 btrfs_wait_ordered_range(inode, off, len);
2570 }
2571}
2572
Mark Fasheh416161d2013-08-06 11:42:51 -07002573static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2574 struct inode *inode2, u64 loff2, u64 len)
2575{
2576 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2577 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2578
2579 mutex_unlock(&inode1->i_mutex);
2580 mutex_unlock(&inode2->i_mutex);
2581}
2582
2583static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2584 struct inode *inode2, u64 loff2, u64 len)
2585{
2586 if (inode1 < inode2) {
2587 swap(inode1, inode2);
2588 swap(loff1, loff2);
2589 }
2590
2591 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2592 lock_extent_range(inode1, loff1, len);
2593 if (inode1 != inode2) {
2594 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2595 lock_extent_range(inode2, loff2, len);
2596 }
2597}
2598
2599static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2600 u64 dst_loff, u64 len)
2601{
2602 int ret = 0;
2603 struct page *src_page, *dst_page;
2604 unsigned int cmp_len = PAGE_CACHE_SIZE;
2605 void *addr, *dst_addr;
2606
2607 while (len) {
2608 if (len < PAGE_CACHE_SIZE)
2609 cmp_len = len;
2610
2611 src_page = extent_same_get_page(src, loff);
2612 if (!src_page)
2613 return -EINVAL;
2614 dst_page = extent_same_get_page(dst, dst_loff);
2615 if (!dst_page) {
2616 page_cache_release(src_page);
2617 return -EINVAL;
2618 }
2619 addr = kmap_atomic(src_page);
2620 dst_addr = kmap_atomic(dst_page);
2621
2622 flush_dcache_page(src_page);
2623 flush_dcache_page(dst_page);
2624
2625 if (memcmp(addr, dst_addr, cmp_len))
2626 ret = BTRFS_SAME_DATA_DIFFERS;
2627
2628 kunmap_atomic(addr);
2629 kunmap_atomic(dst_addr);
2630 page_cache_release(src_page);
2631 page_cache_release(dst_page);
2632
2633 if (ret)
2634 break;
2635
2636 loff += cmp_len;
2637 dst_loff += cmp_len;
2638 len -= cmp_len;
2639 }
2640
2641 return ret;
2642}
2643
2644static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2645{
2646 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2647
2648 if (off + len > inode->i_size || off + len < off)
2649 return -EINVAL;
2650 /* Check that we are block aligned - btrfs_clone() requires this */
2651 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2652 return -EINVAL;
2653
2654 return 0;
2655}
2656
2657static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2658 struct inode *dst, u64 dst_loff)
2659{
2660 int ret;
2661
2662 /*
2663 * btrfs_clone() can't handle extents in the same file
2664 * yet. Once that works, we can drop this check and replace it
2665 * with a check for the same inode, but overlapping extents.
2666 */
2667 if (src == dst)
2668 return -EINVAL;
2669
2670 btrfs_double_lock(src, loff, dst, dst_loff, len);
2671
2672 ret = extent_same_check_offsets(src, loff, len);
2673 if (ret)
2674 goto out_unlock;
2675
2676 ret = extent_same_check_offsets(dst, dst_loff, len);
2677 if (ret)
2678 goto out_unlock;
2679
2680 /* don't make the dst file partly checksummed */
2681 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2682 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2683 ret = -EINVAL;
2684 goto out_unlock;
2685 }
2686
2687 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2688 if (ret == 0)
2689 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2690
2691out_unlock:
2692 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2693
2694 return ret;
2695}
2696
2697#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2698
2699static long btrfs_ioctl_file_extent_same(struct file *file,
2700 void __user *argp)
2701{
2702 struct btrfs_ioctl_same_args *args = argp;
2703 struct btrfs_ioctl_same_args same;
2704 struct btrfs_ioctl_same_extent_info info;
2705 struct inode *src = file->f_dentry->d_inode;
2706 struct file *dst_file = NULL;
2707 struct inode *dst;
2708 u64 off;
2709 u64 len;
2710 int i;
2711 int ret;
2712 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2713 bool is_admin = capable(CAP_SYS_ADMIN);
2714
2715 if (!(file->f_mode & FMODE_READ))
2716 return -EINVAL;
2717
2718 ret = mnt_want_write_file(file);
2719 if (ret)
2720 return ret;
2721
2722 if (copy_from_user(&same,
2723 (struct btrfs_ioctl_same_args __user *)argp,
2724 sizeof(same))) {
2725 ret = -EFAULT;
2726 goto out;
2727 }
2728
2729 off = same.logical_offset;
2730 len = same.length;
2731
2732 /*
2733 * Limit the total length we will dedupe for each operation.
2734 * This is intended to bound the total time spent in this
2735 * ioctl to something sane.
2736 */
2737 if (len > BTRFS_MAX_DEDUPE_LEN)
2738 len = BTRFS_MAX_DEDUPE_LEN;
2739
2740 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2741 /*
2742 * Btrfs does not support blocksize < page_size. As a
2743 * result, btrfs_cmp_data() won't correctly handle
2744 * this situation without an update.
2745 */
2746 ret = -EINVAL;
2747 goto out;
2748 }
2749
2750 ret = -EISDIR;
2751 if (S_ISDIR(src->i_mode))
2752 goto out;
2753
2754 ret = -EACCES;
2755 if (!S_ISREG(src->i_mode))
2756 goto out;
2757
2758 ret = 0;
2759 for (i = 0; i < same.dest_count; i++) {
2760 if (copy_from_user(&info, &args->info[i], sizeof(info))) {
2761 ret = -EFAULT;
2762 goto out;
2763 }
2764
2765 info.bytes_deduped = 0;
2766
2767 dst_file = fget(info.fd);
2768 if (!dst_file) {
2769 info.status = -EBADF;
2770 goto next;
2771 }
2772
2773 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2774 info.status = -EINVAL;
2775 goto next;
2776 }
2777
2778 info.status = -EXDEV;
2779 if (file->f_path.mnt != dst_file->f_path.mnt)
2780 goto next;
2781
2782 dst = dst_file->f_dentry->d_inode;
2783 if (src->i_sb != dst->i_sb)
2784 goto next;
2785
2786 if (S_ISDIR(dst->i_mode)) {
2787 info.status = -EISDIR;
2788 goto next;
2789 }
2790
2791 if (!S_ISREG(dst->i_mode)) {
2792 info.status = -EACCES;
2793 goto next;
2794 }
2795
2796 info.status = btrfs_extent_same(src, off, len, dst,
2797 info.logical_offset);
2798 if (info.status == 0)
2799 info.bytes_deduped += len;
2800
2801next:
2802 if (dst_file)
2803 fput(dst_file);
2804
2805 if (__put_user_unaligned(info.status, &args->info[i].status) ||
2806 __put_user_unaligned(info.bytes_deduped,
2807 &args->info[i].bytes_deduped)) {
2808 ret = -EFAULT;
2809 goto out;
2810 }
2811 }
2812
2813out:
2814 mnt_drop_write_file(file);
2815 return ret;
2816}
2817
Mark Fasheh32b7c682013-08-06 11:42:49 -07002818/**
2819 * btrfs_clone() - clone a range from inode file to another
2820 *
2821 * @src: Inode to clone from
2822 * @inode: Inode to clone to
2823 * @off: Offset within source to start clone from
2824 * @olen: Original length, passed by user, of range to clone
2825 * @olen_aligned: Block-aligned value of olen, extent_same uses
2826 * identical values here
2827 * @destoff: Offset within @inode to start clone
2828 */
2829static int btrfs_clone(struct inode *src, struct inode *inode,
2830 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002831{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002832 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002833 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002834 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002835 struct btrfs_trans_handle *trans;
2836 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002837 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002838 u32 nritems;
2839 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002840 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002841 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002842
2843 ret = -ENOMEM;
2844 buf = vmalloc(btrfs_level_size(root, 0));
2845 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002846 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002847
2848 path = btrfs_alloc_path();
2849 if (!path) {
2850 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002851 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002852 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002853
Yan Zhengae01a0a2008-08-04 23:23:47 -04002854 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002855 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002856 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002857 key.type = BTRFS_EXTENT_DATA_KEY;
2858 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002859
2860 while (1) {
2861 /*
2862 * note the key will change type as we walk through the
2863 * tree.
2864 */
David Sterba362a20c2011-08-01 18:11:57 +02002865 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2866 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002867 if (ret < 0)
2868 goto out;
2869
Yan Zhengae01a0a2008-08-04 23:23:47 -04002870 nritems = btrfs_header_nritems(path->nodes[0]);
2871 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002872 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002873 if (ret < 0)
2874 goto out;
2875 if (ret > 0)
2876 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002877 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002878 }
2879 leaf = path->nodes[0];
2880 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002881
Yan Zhengae01a0a2008-08-04 23:23:47 -04002882 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002883 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002884 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002885 break;
2886
Sage Weilc5c9cd42008-11-12 14:32:25 -05002887 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2888 struct btrfs_file_extent_item *extent;
2889 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002890 u32 size;
2891 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002892 u64 disko = 0, diskl = 0;
2893 u64 datao = 0, datal = 0;
2894 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002895 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002896
2897 size = btrfs_item_size_nr(leaf, slot);
2898 read_extent_buffer(leaf, buf,
2899 btrfs_item_ptr_offset(leaf, slot),
2900 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002901
2902 extent = btrfs_item_ptr(leaf, slot,
2903 struct btrfs_file_extent_item);
2904 comp = btrfs_file_extent_compression(leaf, extent);
2905 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002906 if (type == BTRFS_FILE_EXTENT_REG ||
2907 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002908 disko = btrfs_file_extent_disk_bytenr(leaf,
2909 extent);
2910 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2911 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002912 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002913 datal = btrfs_file_extent_num_bytes(leaf,
2914 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002915 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2916 /* take upper bound, may be compressed */
2917 datal = btrfs_file_extent_ram_bytes(leaf,
2918 extent);
2919 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002920 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002921
Sage Weil050006a2010-10-29 15:37:33 -04002922 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002923 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002924 goto next;
2925
Zheng Yan31840ae2008-09-23 13:14:14 -04002926 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002927 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002928 if (off <= key.offset)
2929 new_key.offset = key.offset + destoff - off;
2930 else
2931 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002932
Sage Weilb6f34092011-09-20 14:48:51 -04002933 /*
2934 * 1 - adjusting old extent (we may have to split it)
2935 * 1 - add new extent
2936 * 1 - inode update
2937 */
2938 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002939 if (IS_ERR(trans)) {
2940 ret = PTR_ERR(trans);
2941 goto out;
2942 }
2943
Chris Masonc8a894d2009-06-27 21:07:03 -04002944 if (type == BTRFS_FILE_EXTENT_REG ||
2945 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002946 /*
2947 * a | --- range to clone ---| b
2948 * | ------------- extent ------------- |
2949 */
2950
2951 /* substract range b */
2952 if (key.offset + datal > off + len)
2953 datal = off + len - key.offset;
2954
2955 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002956 if (off > key.offset) {
2957 datao += off - key.offset;
2958 datal -= off - key.offset;
2959 }
2960
Josef Bacik5dc562c2012-08-17 13:14:17 -04002961 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002962 new_key.offset,
2963 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002964 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002965 if (ret) {
2966 btrfs_abort_transaction(trans, root,
2967 ret);
2968 btrfs_end_transaction(trans, root);
2969 goto out;
2970 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002971
Sage Weilc5c9cd42008-11-12 14:32:25 -05002972 ret = btrfs_insert_empty_item(trans, root, path,
2973 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002974 if (ret) {
2975 btrfs_abort_transaction(trans, root,
2976 ret);
2977 btrfs_end_transaction(trans, root);
2978 goto out;
2979 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002980
2981 leaf = path->nodes[0];
2982 slot = path->slots[0];
2983 write_extent_buffer(leaf, buf,
2984 btrfs_item_ptr_offset(leaf, slot),
2985 size);
2986
2987 extent = btrfs_item_ptr(leaf, slot,
2988 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002989
Sage Weilc5c9cd42008-11-12 14:32:25 -05002990 /* disko == 0 means it's a hole */
2991 if (!disko)
2992 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002993
2994 btrfs_set_file_extent_offset(leaf, extent,
2995 datao);
2996 btrfs_set_file_extent_num_bytes(leaf, extent,
2997 datal);
2998 if (disko) {
2999 inode_add_bytes(inode, datal);
3000 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003001 disko, diskl, 0,
3002 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003003 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003004 new_key.offset - datao,
3005 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003006 if (ret) {
3007 btrfs_abort_transaction(trans,
3008 root,
3009 ret);
3010 btrfs_end_transaction(trans,
3011 root);
3012 goto out;
3013
3014 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003015 }
3016 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3017 u64 skip = 0;
3018 u64 trim = 0;
3019 if (off > key.offset) {
3020 skip = off - key.offset;
3021 new_key.offset += skip;
3022 }
Chris Masond3977122009-01-05 21:25:51 -05003023
Liu Boaa42ffd2012-09-18 03:52:23 -06003024 if (key.offset + datal > off + len)
3025 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003026
Sage Weilc5c9cd42008-11-12 14:32:25 -05003027 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003028 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003029 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003030 goto out;
3031 }
3032 size -= skip + trim;
3033 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003034
Josef Bacik5dc562c2012-08-17 13:14:17 -04003035 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003036 new_key.offset,
3037 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003038 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003039 if (ret) {
3040 btrfs_abort_transaction(trans, root,
3041 ret);
3042 btrfs_end_transaction(trans, root);
3043 goto out;
3044 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003045
Sage Weilc5c9cd42008-11-12 14:32:25 -05003046 ret = btrfs_insert_empty_item(trans, root, path,
3047 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003048 if (ret) {
3049 btrfs_abort_transaction(trans, root,
3050 ret);
3051 btrfs_end_transaction(trans, root);
3052 goto out;
3053 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003054
3055 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003056 u32 start =
3057 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003058 memmove(buf+start, buf+start+skip,
3059 datal);
3060 }
3061
3062 leaf = path->nodes[0];
3063 slot = path->slots[0];
3064 write_extent_buffer(leaf, buf,
3065 btrfs_item_ptr_offset(leaf, slot),
3066 size);
3067 inode_add_bytes(inode, datal);
3068 }
3069
3070 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003071 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003072
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003073 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003074 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003075
3076 /*
3077 * we round up to the block size at eof when
3078 * determining which extents to clone above,
3079 * but shouldn't round up the file size
3080 */
3081 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003082 if (endoff > destoff+olen)
3083 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003084 if (endoff > inode->i_size)
3085 btrfs_i_size_write(inode, endoff);
3086
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003088 if (ret) {
3089 btrfs_abort_transaction(trans, root, ret);
3090 btrfs_end_transaction(trans, root);
3091 goto out;
3092 }
3093 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003094 }
Chris Masond3977122009-01-05 21:25:51 -05003095next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003096 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003097 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003098 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003099 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003100
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003101out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003102 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003103 btrfs_free_path(path);
3104 vfree(buf);
3105 return ret;
3106}
3107
3108static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3109 u64 off, u64 olen, u64 destoff)
3110{
3111 struct inode *inode = fdentry(file)->d_inode;
3112 struct btrfs_root *root = BTRFS_I(inode)->root;
3113 struct fd src_file;
3114 struct inode *src;
3115 int ret;
3116 u64 len = olen;
3117 u64 bs = root->fs_info->sb->s_blocksize;
3118 int same_inode = 0;
3119
3120 /*
3121 * TODO:
3122 * - split compressed inline extents. annoying: we need to
3123 * decompress into destination's address_space (the file offset
3124 * may change, so source mapping won't do), then recompress (or
3125 * otherwise reinsert) a subrange.
3126 * - allow ranges within the same file to be cloned (provided
3127 * they don't overlap)?
3128 */
3129
3130 /* the destination must be opened for writing */
3131 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3132 return -EINVAL;
3133
3134 if (btrfs_root_readonly(root))
3135 return -EROFS;
3136
3137 ret = mnt_want_write_file(file);
3138 if (ret)
3139 return ret;
3140
3141 src_file = fdget(srcfd);
3142 if (!src_file.file) {
3143 ret = -EBADF;
3144 goto out_drop_write;
3145 }
3146
3147 ret = -EXDEV;
3148 if (src_file.file->f_path.mnt != file->f_path.mnt)
3149 goto out_fput;
3150
3151 src = file_inode(src_file.file);
3152
3153 ret = -EINVAL;
3154 if (src == inode)
3155 same_inode = 1;
3156
3157 /* the src must be open for reading */
3158 if (!(src_file.file->f_mode & FMODE_READ))
3159 goto out_fput;
3160
3161 /* don't make the dst file partly checksummed */
3162 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3163 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3164 goto out_fput;
3165
3166 ret = -EISDIR;
3167 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3168 goto out_fput;
3169
3170 ret = -EXDEV;
3171 if (src->i_sb != inode->i_sb)
3172 goto out_fput;
3173
3174 if (!same_inode) {
3175 if (inode < src) {
3176 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3177 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3178 } else {
3179 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3180 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3181 }
3182 } else {
3183 mutex_lock(&src->i_mutex);
3184 }
3185
3186 /* determine range to clone */
3187 ret = -EINVAL;
3188 if (off + len > src->i_size || off + len < off)
3189 goto out_unlock;
3190 if (len == 0)
3191 olen = len = src->i_size - off;
3192 /* if we extend to eof, continue to block boundary */
3193 if (off + len == src->i_size)
3194 len = ALIGN(src->i_size, bs) - off;
3195
3196 /* verify the end result is block aligned */
3197 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3198 !IS_ALIGNED(destoff, bs))
3199 goto out_unlock;
3200
3201 /* verify if ranges are overlapped within the same file */
3202 if (same_inode) {
3203 if (destoff + len > off && destoff < off + len)
3204 goto out_unlock;
3205 }
3206
3207 if (destoff > inode->i_size) {
3208 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3209 if (ret)
3210 goto out_unlock;
3211 }
3212
3213 /* truncate page cache pages from target inode range */
3214 truncate_inode_pages_range(&inode->i_data, destoff,
3215 PAGE_CACHE_ALIGN(destoff + len) - 1);
3216
3217 lock_extent_range(src, off, len);
3218
3219 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3220
Liu Boaa42ffd2012-09-18 03:52:23 -06003221 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003222out_unlock:
3223 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003224 if (!same_inode)
3225 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003226out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003227 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003228out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003229 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003230 return ret;
3231}
3232
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003233static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003234{
3235 struct btrfs_ioctl_clone_range_args args;
3236
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003237 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003238 return -EFAULT;
3239 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3240 args.src_length, args.dest_offset);
3241}
3242
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003243/*
3244 * there are many ways the trans_start and trans_end ioctls can lead
3245 * to deadlocks. They should only be used by applications that
3246 * basically own the machine, and have a very in depth understanding
3247 * of all the possible deadlocks and enospc problems.
3248 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003249static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003250{
Al Viro496ad9a2013-01-23 17:07:38 -05003251 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003252 struct btrfs_root *root = BTRFS_I(inode)->root;
3253 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003254 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003255
Sage Weil1ab86ae2009-09-29 18:38:44 -04003256 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003257 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003258 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003259
3260 ret = -EINPROGRESS;
3261 if (file->private_data)
3262 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003263
Li Zefanb83cc962010-12-20 16:04:08 +08003264 ret = -EROFS;
3265 if (btrfs_root_readonly(root))
3266 goto out;
3267
Al Viroa561be72011-11-23 11:57:51 -05003268 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003269 if (ret)
3270 goto out;
3271
Josef Bacika4abeea2011-04-11 17:25:13 -04003272 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003273
Sage Weil1ab86ae2009-09-29 18:38:44 -04003274 ret = -ENOMEM;
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003275 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003276 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003277 goto out_drop;
3278
3279 file->private_data = trans;
3280 return 0;
3281
3282out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003283 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003284 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003285out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003286 return ret;
3287}
3288
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003289static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3290{
Al Viro496ad9a2013-01-23 17:07:38 -05003291 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003292 struct btrfs_root *root = BTRFS_I(inode)->root;
3293 struct btrfs_root *new_root;
3294 struct btrfs_dir_item *di;
3295 struct btrfs_trans_handle *trans;
3296 struct btrfs_path *path;
3297 struct btrfs_key location;
3298 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003299 u64 objectid = 0;
3300 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003301 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003302
3303 if (!capable(CAP_SYS_ADMIN))
3304 return -EPERM;
3305
Miao Xie3c04ce02012-11-26 08:43:07 +00003306 ret = mnt_want_write_file(file);
3307 if (ret)
3308 return ret;
3309
3310 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3311 ret = -EFAULT;
3312 goto out;
3313 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003314
3315 if (!objectid)
3316 objectid = root->root_key.objectid;
3317
3318 location.objectid = objectid;
3319 location.type = BTRFS_ROOT_ITEM_KEY;
3320 location.offset = (u64)-1;
3321
3322 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003323 if (IS_ERR(new_root)) {
3324 ret = PTR_ERR(new_root);
3325 goto out;
3326 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003327
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003328 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003329 if (!path) {
3330 ret = -ENOMEM;
3331 goto out;
3332 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003333 path->leave_spinning = 1;
3334
3335 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003336 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003337 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003338 ret = PTR_ERR(trans);
3339 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003340 }
3341
David Sterba6c417612011-04-13 15:41:04 +02003342 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003343 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3344 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003345 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003346 btrfs_free_path(path);
3347 btrfs_end_transaction(trans, root);
3348 printk(KERN_ERR "Umm, you don't have the default dir item, "
3349 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003350 ret = -ENOENT;
3351 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003352 }
3353
3354 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3355 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3356 btrfs_mark_buffer_dirty(path->nodes[0]);
3357 btrfs_free_path(path);
3358
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003359 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003360 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003361out:
3362 mnt_drop_write_file(file);
3363 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003364}
3365
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003366void btrfs_get_block_group_info(struct list_head *groups_list,
3367 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc092010-09-29 11:22:36 -04003368{
3369 struct btrfs_block_group_cache *block_group;
3370
3371 space->total_bytes = 0;
3372 space->used_bytes = 0;
3373 space->flags = 0;
3374 list_for_each_entry(block_group, groups_list, list) {
3375 space->flags = block_group->flags;
3376 space->total_bytes += block_group->key.offset;
3377 space->used_bytes +=
3378 btrfs_block_group_used(&block_group->item);
3379 }
3380}
3381
Eric Sandeen48a3b632013-04-25 20:41:01 +00003382static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003383{
3384 struct btrfs_ioctl_space_args space_args;
3385 struct btrfs_ioctl_space_info space;
3386 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003387 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003388 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003389 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003390 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3391 BTRFS_BLOCK_GROUP_SYSTEM,
3392 BTRFS_BLOCK_GROUP_METADATA,
3393 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3394 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003395 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003396 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003397 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003398 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003399
3400 if (copy_from_user(&space_args,
3401 (struct btrfs_ioctl_space_args __user *)arg,
3402 sizeof(space_args)))
3403 return -EFAULT;
3404
Josef Bacikbf5fc092010-09-29 11:22:36 -04003405 for (i = 0; i < num_types; i++) {
3406 struct btrfs_space_info *tmp;
3407
3408 info = NULL;
3409 rcu_read_lock();
3410 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3411 list) {
3412 if (tmp->flags == types[i]) {
3413 info = tmp;
3414 break;
3415 }
3416 }
3417 rcu_read_unlock();
3418
3419 if (!info)
3420 continue;
3421
3422 down_read(&info->groups_sem);
3423 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3424 if (!list_empty(&info->block_groups[c]))
3425 slot_count++;
3426 }
3427 up_read(&info->groups_sem);
3428 }
Josef Bacik1406e432010-01-13 18:19:06 +00003429
Chris Mason7fde62b2010-03-16 15:40:10 -04003430 /* space_slots == 0 means they are asking for a count */
3431 if (space_args.space_slots == 0) {
3432 space_args.total_spaces = slot_count;
3433 goto out;
3434 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04003435
Dan Rosenberg51788b12011-02-14 16:04:23 -05003436 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04003437
Chris Mason7fde62b2010-03-16 15:40:10 -04003438 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003439
Chris Mason7fde62b2010-03-16 15:40:10 -04003440 /* we generally have at most 6 or so space infos, one for each raid
3441 * level. So, a whole page should be more than enough for everyone
3442 */
3443 if (alloc_size > PAGE_CACHE_SIZE)
3444 return -ENOMEM;
3445
3446 space_args.total_spaces = 0;
3447 dest = kmalloc(alloc_size, GFP_NOFS);
3448 if (!dest)
3449 return -ENOMEM;
3450 dest_orig = dest;
3451
3452 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04003453 for (i = 0; i < num_types; i++) {
3454 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003455
Dan Rosenberg51788b12011-02-14 16:04:23 -05003456 if (!slot_count)
3457 break;
3458
Josef Bacikbf5fc092010-09-29 11:22:36 -04003459 info = NULL;
3460 rcu_read_lock();
3461 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3462 list) {
3463 if (tmp->flags == types[i]) {
3464 info = tmp;
3465 break;
3466 }
3467 }
3468 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003469
Josef Bacikbf5fc092010-09-29 11:22:36 -04003470 if (!info)
3471 continue;
3472 down_read(&info->groups_sem);
3473 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3474 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003475 btrfs_get_block_group_info(
3476 &info->block_groups[c], &space);
Josef Bacikbf5fc092010-09-29 11:22:36 -04003477 memcpy(dest, &space, sizeof(space));
3478 dest++;
3479 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003480 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003481 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003482 if (!slot_count)
3483 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04003484 }
3485 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003486 }
Josef Bacik1406e432010-01-13 18:19:06 +00003487
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003488 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003489 (arg + sizeof(struct btrfs_ioctl_space_args));
3490
3491 if (copy_to_user(user_dest, dest_orig, alloc_size))
3492 ret = -EFAULT;
3493
3494 kfree(dest_orig);
3495out:
3496 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003497 ret = -EFAULT;
3498
3499 return ret;
3500}
3501
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003502/*
3503 * there are many ways the trans_start and trans_end ioctls can lead
3504 * to deadlocks. They should only be used by applications that
3505 * basically own the machine, and have a very in depth understanding
3506 * of all the possible deadlocks and enospc problems.
3507 */
3508long btrfs_ioctl_trans_end(struct file *file)
3509{
Al Viro496ad9a2013-01-23 17:07:38 -05003510 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003511 struct btrfs_root *root = BTRFS_I(inode)->root;
3512 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003513
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003514 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003515 if (!trans)
3516 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003517 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003518
Sage Weil1ab86ae2009-09-29 18:38:44 -04003519 btrfs_end_transaction(trans, root);
3520
Josef Bacika4abeea2011-04-11 17:25:13 -04003521 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003522
Al Viro2a79f172011-12-09 08:06:57 -05003523 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003524 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003525}
3526
Miao Xie9a8c28b2012-11-26 08:40:43 +00003527static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3528 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003529{
Sage Weil46204592010-10-29 15:41:32 -04003530 struct btrfs_trans_handle *trans;
3531 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003532 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003533
Miao Xied4edf392013-02-20 09:17:06 +00003534 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003535 if (IS_ERR(trans)) {
3536 if (PTR_ERR(trans) != -ENOENT)
3537 return PTR_ERR(trans);
3538
3539 /* No running transaction, don't bother */
3540 transid = root->fs_info->last_trans_committed;
3541 goto out;
3542 }
Sage Weil46204592010-10-29 15:41:32 -04003543 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003544 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003545 if (ret) {
3546 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003547 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003548 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003549out:
Sage Weil46204592010-10-29 15:41:32 -04003550 if (argp)
3551 if (copy_to_user(argp, &transid, sizeof(transid)))
3552 return -EFAULT;
3553 return 0;
3554}
3555
Miao Xie9a8c28b2012-11-26 08:40:43 +00003556static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3557 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003558{
Sage Weil46204592010-10-29 15:41:32 -04003559 u64 transid;
3560
3561 if (argp) {
3562 if (copy_from_user(&transid, argp, sizeof(transid)))
3563 return -EFAULT;
3564 } else {
3565 transid = 0; /* current trans */
3566 }
3567 return btrfs_wait_for_commit(root, transid);
3568}
3569
Miao Xieb8e95482012-11-26 08:48:01 +00003570static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003571{
Al Viro496ad9a2013-01-23 17:07:38 -05003572 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003573 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003574 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003575
3576 if (!capable(CAP_SYS_ADMIN))
3577 return -EPERM;
3578
3579 sa = memdup_user(arg, sizeof(*sa));
3580 if (IS_ERR(sa))
3581 return PTR_ERR(sa);
3582
Miao Xieb8e95482012-11-26 08:48:01 +00003583 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3584 ret = mnt_want_write_file(file);
3585 if (ret)
3586 goto out;
3587 }
3588
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003589 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003590 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3591 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003592
3593 if (copy_to_user(arg, sa, sizeof(*sa)))
3594 ret = -EFAULT;
3595
Miao Xieb8e95482012-11-26 08:48:01 +00003596 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3597 mnt_drop_write_file(file);
3598out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003599 kfree(sa);
3600 return ret;
3601}
3602
3603static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3604{
3605 if (!capable(CAP_SYS_ADMIN))
3606 return -EPERM;
3607
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003608 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003609}
3610
3611static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3612 void __user *arg)
3613{
3614 struct btrfs_ioctl_scrub_args *sa;
3615 int ret;
3616
3617 if (!capable(CAP_SYS_ADMIN))
3618 return -EPERM;
3619
3620 sa = memdup_user(arg, sizeof(*sa));
3621 if (IS_ERR(sa))
3622 return PTR_ERR(sa);
3623
3624 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3625
3626 if (copy_to_user(arg, sa, sizeof(*sa)))
3627 ret = -EFAULT;
3628
3629 kfree(sa);
3630 return ret;
3631}
3632
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003633static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003634 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003635{
3636 struct btrfs_ioctl_get_dev_stats *sa;
3637 int ret;
3638
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003639 sa = memdup_user(arg, sizeof(*sa));
3640 if (IS_ERR(sa))
3641 return PTR_ERR(sa);
3642
David Sterbab27f7c02012-06-22 06:30:39 -06003643 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3644 kfree(sa);
3645 return -EPERM;
3646 }
3647
3648 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003649
3650 if (copy_to_user(arg, sa, sizeof(*sa)))
3651 ret = -EFAULT;
3652
3653 kfree(sa);
3654 return ret;
3655}
3656
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003657static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3658{
3659 struct btrfs_ioctl_dev_replace_args *p;
3660 int ret;
3661
3662 if (!capable(CAP_SYS_ADMIN))
3663 return -EPERM;
3664
3665 p = memdup_user(arg, sizeof(*p));
3666 if (IS_ERR(p))
3667 return PTR_ERR(p);
3668
3669 switch (p->cmd) {
3670 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Stefan Behrensbbb651e2013-08-19 18:51:13 +02003671 if (root->fs_info->sb->s_flags & MS_RDONLY)
3672 return -EROFS;
3673
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003674 if (atomic_xchg(
3675 &root->fs_info->mutually_exclusive_operation_running,
3676 1)) {
3677 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3678 ret = -EINPROGRESS;
3679 } else {
3680 ret = btrfs_dev_replace_start(root, p);
3681 atomic_set(
3682 &root->fs_info->mutually_exclusive_operation_running,
3683 0);
3684 }
3685 break;
3686 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3687 btrfs_dev_replace_status(root->fs_info, p);
3688 ret = 0;
3689 break;
3690 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3691 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3692 break;
3693 default:
3694 ret = -EINVAL;
3695 break;
3696 }
3697
3698 if (copy_to_user(arg, p, sizeof(*p)))
3699 ret = -EFAULT;
3700
3701 kfree(p);
3702 return ret;
3703}
3704
Jan Schmidtd7728c92011-07-07 16:48:38 +02003705static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3706{
3707 int ret = 0;
3708 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003709 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003710 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003711 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003712 struct inode_fs_paths *ipath = NULL;
3713 struct btrfs_path *path;
3714
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003715 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003716 return -EPERM;
3717
3718 path = btrfs_alloc_path();
3719 if (!path) {
3720 ret = -ENOMEM;
3721 goto out;
3722 }
3723
3724 ipa = memdup_user(arg, sizeof(*ipa));
3725 if (IS_ERR(ipa)) {
3726 ret = PTR_ERR(ipa);
3727 ipa = NULL;
3728 goto out;
3729 }
3730
3731 size = min_t(u32, ipa->size, 4096);
3732 ipath = init_ipath(size, root, path);
3733 if (IS_ERR(ipath)) {
3734 ret = PTR_ERR(ipath);
3735 ipath = NULL;
3736 goto out;
3737 }
3738
3739 ret = paths_from_inode(ipa->inum, ipath);
3740 if (ret < 0)
3741 goto out;
3742
3743 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003744 rel_ptr = ipath->fspath->val[i] -
3745 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003746 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003747 }
3748
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003749 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3750 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003751 if (ret) {
3752 ret = -EFAULT;
3753 goto out;
3754 }
3755
3756out:
3757 btrfs_free_path(path);
3758 free_ipath(ipath);
3759 kfree(ipa);
3760
3761 return ret;
3762}
3763
3764static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3765{
3766 struct btrfs_data_container *inodes = ctx;
3767 const size_t c = 3 * sizeof(u64);
3768
3769 if (inodes->bytes_left >= c) {
3770 inodes->bytes_left -= c;
3771 inodes->val[inodes->elem_cnt] = inum;
3772 inodes->val[inodes->elem_cnt + 1] = offset;
3773 inodes->val[inodes->elem_cnt + 2] = root;
3774 inodes->elem_cnt += 3;
3775 } else {
3776 inodes->bytes_missing += c - inodes->bytes_left;
3777 inodes->bytes_left = 0;
3778 inodes->elem_missed += 3;
3779 }
3780
3781 return 0;
3782}
3783
3784static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3785 void __user *arg)
3786{
3787 int ret = 0;
3788 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003789 struct btrfs_ioctl_logical_ino_args *loi;
3790 struct btrfs_data_container *inodes = NULL;
3791 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003792
3793 if (!capable(CAP_SYS_ADMIN))
3794 return -EPERM;
3795
3796 loi = memdup_user(arg, sizeof(*loi));
3797 if (IS_ERR(loi)) {
3798 ret = PTR_ERR(loi);
3799 loi = NULL;
3800 goto out;
3801 }
3802
3803 path = btrfs_alloc_path();
3804 if (!path) {
3805 ret = -ENOMEM;
3806 goto out;
3807 }
3808
Liu Bo425d17a2012-09-07 20:01:30 -06003809 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003810 inodes = init_data_container(size);
3811 if (IS_ERR(inodes)) {
3812 ret = PTR_ERR(inodes);
3813 inodes = NULL;
3814 goto out;
3815 }
3816
Liu Bodf031f02012-09-07 20:01:29 -06003817 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3818 build_ino_list, inodes);
3819 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003820 ret = -ENOENT;
3821 if (ret < 0)
3822 goto out;
3823
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003824 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3825 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003826 if (ret)
3827 ret = -EFAULT;
3828
3829out:
3830 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003831 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003832 kfree(loi);
3833
3834 return ret;
3835}
3836
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003837void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003838 struct btrfs_ioctl_balance_args *bargs)
3839{
3840 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3841
3842 bargs->flags = bctl->flags;
3843
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003844 if (atomic_read(&fs_info->balance_running))
3845 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3846 if (atomic_read(&fs_info->balance_pause_req))
3847 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003848 if (atomic_read(&fs_info->balance_cancel_req))
3849 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003850
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003851 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3852 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3853 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003854
3855 if (lock) {
3856 spin_lock(&fs_info->balance_lock);
3857 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3858 spin_unlock(&fs_info->balance_lock);
3859 } else {
3860 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3861 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003862}
3863
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003864static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003865{
Al Viro496ad9a2013-01-23 17:07:38 -05003866 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003867 struct btrfs_fs_info *fs_info = root->fs_info;
3868 struct btrfs_ioctl_balance_args *bargs;
3869 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003870 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003871 int ret;
3872
3873 if (!capable(CAP_SYS_ADMIN))
3874 return -EPERM;
3875
Liu Boe54bfa32012-06-29 03:58:48 -06003876 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003877 if (ret)
3878 return ret;
3879
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003880again:
3881 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3882 mutex_lock(&fs_info->volume_mutex);
3883 mutex_lock(&fs_info->balance_mutex);
3884 need_unlock = true;
3885 goto locked;
3886 }
3887
3888 /*
3889 * mut. excl. ops lock is locked. Three possibilites:
3890 * (1) some other op is running
3891 * (2) balance is running
3892 * (3) balance is paused -- special case (think resume)
3893 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003894 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003895 if (fs_info->balance_ctl) {
3896 /* this is either (2) or (3) */
3897 if (!atomic_read(&fs_info->balance_running)) {
3898 mutex_unlock(&fs_info->balance_mutex);
3899 if (!mutex_trylock(&fs_info->volume_mutex))
3900 goto again;
3901 mutex_lock(&fs_info->balance_mutex);
3902
3903 if (fs_info->balance_ctl &&
3904 !atomic_read(&fs_info->balance_running)) {
3905 /* this is (3) */
3906 need_unlock = false;
3907 goto locked;
3908 }
3909
3910 mutex_unlock(&fs_info->balance_mutex);
3911 mutex_unlock(&fs_info->volume_mutex);
3912 goto again;
3913 } else {
3914 /* this is (2) */
3915 mutex_unlock(&fs_info->balance_mutex);
3916 ret = -EINPROGRESS;
3917 goto out;
3918 }
3919 } else {
3920 /* this is (1) */
3921 mutex_unlock(&fs_info->balance_mutex);
3922 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3923 ret = -EINVAL;
3924 goto out;
3925 }
3926
3927locked:
3928 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003929
3930 if (arg) {
3931 bargs = memdup_user(arg, sizeof(*bargs));
3932 if (IS_ERR(bargs)) {
3933 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003934 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003935 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003936
3937 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3938 if (!fs_info->balance_ctl) {
3939 ret = -ENOTCONN;
3940 goto out_bargs;
3941 }
3942
3943 bctl = fs_info->balance_ctl;
3944 spin_lock(&fs_info->balance_lock);
3945 bctl->flags |= BTRFS_BALANCE_RESUME;
3946 spin_unlock(&fs_info->balance_lock);
3947
3948 goto do_balance;
3949 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003950 } else {
3951 bargs = NULL;
3952 }
3953
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003954 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003955 ret = -EINPROGRESS;
3956 goto out_bargs;
3957 }
3958
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003959 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3960 if (!bctl) {
3961 ret = -ENOMEM;
3962 goto out_bargs;
3963 }
3964
3965 bctl->fs_info = fs_info;
3966 if (arg) {
3967 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3968 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3969 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3970
3971 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003972 } else {
3973 /* balance everything - no filters */
3974 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003975 }
3976
Ilya Dryomovde322262012-01-16 22:04:49 +02003977do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003978 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003979 * Ownership of bctl and mutually_exclusive_operation_running
3980 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3981 * or, if restriper was paused all the way until unmount, in
3982 * free_fs_info. mutually_exclusive_operation_running is
3983 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003984 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003985 need_unlock = false;
3986
3987 ret = btrfs_balance(bctl, bargs);
3988
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003989 if (arg) {
3990 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3991 ret = -EFAULT;
3992 }
3993
3994out_bargs:
3995 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003996out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003997 mutex_unlock(&fs_info->balance_mutex);
3998 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003999 if (need_unlock)
4000 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4001out:
Liu Boe54bfa32012-06-29 03:58:48 -06004002 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004003 return ret;
4004}
4005
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004006static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4007{
4008 if (!capable(CAP_SYS_ADMIN))
4009 return -EPERM;
4010
4011 switch (cmd) {
4012 case BTRFS_BALANCE_CTL_PAUSE:
4013 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004014 case BTRFS_BALANCE_CTL_CANCEL:
4015 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004016 }
4017
4018 return -EINVAL;
4019}
4020
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004021static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4022 void __user *arg)
4023{
4024 struct btrfs_fs_info *fs_info = root->fs_info;
4025 struct btrfs_ioctl_balance_args *bargs;
4026 int ret = 0;
4027
4028 if (!capable(CAP_SYS_ADMIN))
4029 return -EPERM;
4030
4031 mutex_lock(&fs_info->balance_mutex);
4032 if (!fs_info->balance_ctl) {
4033 ret = -ENOTCONN;
4034 goto out;
4035 }
4036
4037 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4038 if (!bargs) {
4039 ret = -ENOMEM;
4040 goto out;
4041 }
4042
4043 update_ioctl_balance_args(fs_info, 1, bargs);
4044
4045 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4046 ret = -EFAULT;
4047
4048 kfree(bargs);
4049out:
4050 mutex_unlock(&fs_info->balance_mutex);
4051 return ret;
4052}
4053
Miao Xie905b0dd2012-11-26 08:50:11 +00004054static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004055{
Al Viro496ad9a2013-01-23 17:07:38 -05004056 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004057 struct btrfs_ioctl_quota_ctl_args *sa;
4058 struct btrfs_trans_handle *trans = NULL;
4059 int ret;
4060 int err;
4061
4062 if (!capable(CAP_SYS_ADMIN))
4063 return -EPERM;
4064
Miao Xie905b0dd2012-11-26 08:50:11 +00004065 ret = mnt_want_write_file(file);
4066 if (ret)
4067 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004068
4069 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004070 if (IS_ERR(sa)) {
4071 ret = PTR_ERR(sa);
4072 goto drop_write;
4073 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004074
Wang Shilong7708f022013-04-07 10:24:57 +00004075 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004076 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4077 if (IS_ERR(trans)) {
4078 ret = PTR_ERR(trans);
4079 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004080 }
4081
4082 switch (sa->cmd) {
4083 case BTRFS_QUOTA_CTL_ENABLE:
4084 ret = btrfs_quota_enable(trans, root->fs_info);
4085 break;
4086 case BTRFS_QUOTA_CTL_DISABLE:
4087 ret = btrfs_quota_disable(trans, root->fs_info);
4088 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004089 default:
4090 ret = -EINVAL;
4091 break;
4092 }
4093
Jan Schmidt2f232032013-04-25 16:04:51 +00004094 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4095 if (err && !ret)
4096 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004097out:
4098 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004099 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004100drop_write:
4101 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004102 return ret;
4103}
4104
Miao Xie905b0dd2012-11-26 08:50:11 +00004105static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004106{
Al Viro496ad9a2013-01-23 17:07:38 -05004107 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004108 struct btrfs_ioctl_qgroup_assign_args *sa;
4109 struct btrfs_trans_handle *trans;
4110 int ret;
4111 int err;
4112
4113 if (!capable(CAP_SYS_ADMIN))
4114 return -EPERM;
4115
Miao Xie905b0dd2012-11-26 08:50:11 +00004116 ret = mnt_want_write_file(file);
4117 if (ret)
4118 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004119
4120 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004121 if (IS_ERR(sa)) {
4122 ret = PTR_ERR(sa);
4123 goto drop_write;
4124 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004125
4126 trans = btrfs_join_transaction(root);
4127 if (IS_ERR(trans)) {
4128 ret = PTR_ERR(trans);
4129 goto out;
4130 }
4131
4132 /* FIXME: check if the IDs really exist */
4133 if (sa->assign) {
4134 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4135 sa->src, sa->dst);
4136 } else {
4137 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4138 sa->src, sa->dst);
4139 }
4140
4141 err = btrfs_end_transaction(trans, root);
4142 if (err && !ret)
4143 ret = err;
4144
4145out:
4146 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004147drop_write:
4148 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004149 return ret;
4150}
4151
Miao Xie905b0dd2012-11-26 08:50:11 +00004152static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004153{
Al Viro496ad9a2013-01-23 17:07:38 -05004154 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004155 struct btrfs_ioctl_qgroup_create_args *sa;
4156 struct btrfs_trans_handle *trans;
4157 int ret;
4158 int err;
4159
4160 if (!capable(CAP_SYS_ADMIN))
4161 return -EPERM;
4162
Miao Xie905b0dd2012-11-26 08:50:11 +00004163 ret = mnt_want_write_file(file);
4164 if (ret)
4165 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004166
4167 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004168 if (IS_ERR(sa)) {
4169 ret = PTR_ERR(sa);
4170 goto drop_write;
4171 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004172
Miao Xied86e56c2012-11-15 11:35:41 +00004173 if (!sa->qgroupid) {
4174 ret = -EINVAL;
4175 goto out;
4176 }
4177
Arne Jansen5d13a372011-09-14 15:53:51 +02004178 trans = btrfs_join_transaction(root);
4179 if (IS_ERR(trans)) {
4180 ret = PTR_ERR(trans);
4181 goto out;
4182 }
4183
4184 /* FIXME: check if the IDs really exist */
4185 if (sa->create) {
4186 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4187 NULL);
4188 } else {
4189 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4190 }
4191
4192 err = btrfs_end_transaction(trans, root);
4193 if (err && !ret)
4194 ret = err;
4195
4196out:
4197 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004198drop_write:
4199 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004200 return ret;
4201}
4202
Miao Xie905b0dd2012-11-26 08:50:11 +00004203static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004204{
Al Viro496ad9a2013-01-23 17:07:38 -05004205 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004206 struct btrfs_ioctl_qgroup_limit_args *sa;
4207 struct btrfs_trans_handle *trans;
4208 int ret;
4209 int err;
4210 u64 qgroupid;
4211
4212 if (!capable(CAP_SYS_ADMIN))
4213 return -EPERM;
4214
Miao Xie905b0dd2012-11-26 08:50:11 +00004215 ret = mnt_want_write_file(file);
4216 if (ret)
4217 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004218
4219 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004220 if (IS_ERR(sa)) {
4221 ret = PTR_ERR(sa);
4222 goto drop_write;
4223 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004224
4225 trans = btrfs_join_transaction(root);
4226 if (IS_ERR(trans)) {
4227 ret = PTR_ERR(trans);
4228 goto out;
4229 }
4230
4231 qgroupid = sa->qgroupid;
4232 if (!qgroupid) {
4233 /* take the current subvol as qgroup */
4234 qgroupid = root->root_key.objectid;
4235 }
4236
4237 /* FIXME: check if the IDs really exist */
4238 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4239
4240 err = btrfs_end_transaction(trans, root);
4241 if (err && !ret)
4242 ret = err;
4243
4244out:
4245 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004246drop_write:
4247 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004248 return ret;
4249}
4250
Jan Schmidt2f232032013-04-25 16:04:51 +00004251static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4252{
Al Viro6d0379e2013-06-16 19:32:35 +04004253 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004254 struct btrfs_ioctl_quota_rescan_args *qsa;
4255 int ret;
4256
4257 if (!capable(CAP_SYS_ADMIN))
4258 return -EPERM;
4259
4260 ret = mnt_want_write_file(file);
4261 if (ret)
4262 return ret;
4263
4264 qsa = memdup_user(arg, sizeof(*qsa));
4265 if (IS_ERR(qsa)) {
4266 ret = PTR_ERR(qsa);
4267 goto drop_write;
4268 }
4269
4270 if (qsa->flags) {
4271 ret = -EINVAL;
4272 goto out;
4273 }
4274
4275 ret = btrfs_qgroup_rescan(root->fs_info);
4276
4277out:
4278 kfree(qsa);
4279drop_write:
4280 mnt_drop_write_file(file);
4281 return ret;
4282}
4283
4284static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4285{
Al Viro6d0379e2013-06-16 19:32:35 +04004286 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004287 struct btrfs_ioctl_quota_rescan_args *qsa;
4288 int ret = 0;
4289
4290 if (!capable(CAP_SYS_ADMIN))
4291 return -EPERM;
4292
4293 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4294 if (!qsa)
4295 return -ENOMEM;
4296
4297 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4298 qsa->flags = 1;
4299 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4300 }
4301
4302 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4303 ret = -EFAULT;
4304
4305 kfree(qsa);
4306 return ret;
4307}
4308
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004309static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4310{
4311 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4312
4313 if (!capable(CAP_SYS_ADMIN))
4314 return -EPERM;
4315
4316 return btrfs_qgroup_wait_for_completion(root->fs_info);
4317}
4318
Alexander Block8ea05e32012-07-25 17:35:53 +02004319static long btrfs_ioctl_set_received_subvol(struct file *file,
4320 void __user *arg)
4321{
4322 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004323 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004324 struct btrfs_root *root = BTRFS_I(inode)->root;
4325 struct btrfs_root_item *root_item = &root->root_item;
4326 struct btrfs_trans_handle *trans;
4327 struct timespec ct = CURRENT_TIME;
4328 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004329 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004330
4331 ret = mnt_want_write_file(file);
4332 if (ret < 0)
4333 return ret;
4334
4335 down_write(&root->fs_info->subvol_sem);
4336
4337 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4338 ret = -EINVAL;
4339 goto out;
4340 }
4341
4342 if (btrfs_root_readonly(root)) {
4343 ret = -EROFS;
4344 goto out;
4345 }
4346
4347 if (!inode_owner_or_capable(inode)) {
4348 ret = -EACCES;
4349 goto out;
4350 }
4351
4352 sa = memdup_user(arg, sizeof(*sa));
4353 if (IS_ERR(sa)) {
4354 ret = PTR_ERR(sa);
4355 sa = NULL;
4356 goto out;
4357 }
4358
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004359 /*
4360 * 1 - root item
4361 * 2 - uuid items (received uuid + subvol uuid)
4362 */
4363 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004364 if (IS_ERR(trans)) {
4365 ret = PTR_ERR(trans);
4366 trans = NULL;
4367 goto out;
4368 }
4369
4370 sa->rtransid = trans->transid;
4371 sa->rtime.sec = ct.tv_sec;
4372 sa->rtime.nsec = ct.tv_nsec;
4373
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004374 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4375 BTRFS_UUID_SIZE);
4376 if (received_uuid_changed &&
4377 !btrfs_is_empty_uuid(root_item->received_uuid))
4378 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4379 root_item->received_uuid,
4380 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4381 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004382 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4383 btrfs_set_root_stransid(root_item, sa->stransid);
4384 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004385 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4386 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4387 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4388 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004389
4390 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4391 &root->root_key, &root->root_item);
4392 if (ret < 0) {
4393 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004394 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004395 }
4396 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4397 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4398 sa->uuid,
4399 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4400 root->root_key.objectid);
4401 if (ret < 0 && ret != -EEXIST) {
4402 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004403 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004404 }
4405 }
4406 ret = btrfs_commit_transaction(trans, root);
4407 if (ret < 0) {
4408 btrfs_abort_transaction(trans, root, ret);
4409 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004410 }
4411
4412 ret = copy_to_user(arg, sa, sizeof(*sa));
4413 if (ret)
4414 ret = -EFAULT;
4415
4416out:
4417 kfree(sa);
4418 up_write(&root->fs_info->subvol_sem);
4419 mnt_drop_write_file(file);
4420 return ret;
4421}
4422
jeff.liu867ab662013-01-05 02:48:01 +00004423static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4424{
Al Viro6d0379e2013-06-16 19:32:35 +04004425 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004426 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004427 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004428 char label[BTRFS_LABEL_SIZE];
4429
4430 spin_lock(&root->fs_info->super_lock);
4431 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4432 spin_unlock(&root->fs_info->super_lock);
4433
4434 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004435
4436 if (len == BTRFS_LABEL_SIZE) {
4437 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4438 --len);
4439 }
4440
jeff.liu867ab662013-01-05 02:48:01 +00004441 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004442
4443 return ret ? -EFAULT : 0;
4444}
4445
jeff.liua8bfd4a2013-01-05 02:48:08 +00004446static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4447{
Al Viro6d0379e2013-06-16 19:32:35 +04004448 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004449 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4450 struct btrfs_trans_handle *trans;
4451 char label[BTRFS_LABEL_SIZE];
4452 int ret;
4453
4454 if (!capable(CAP_SYS_ADMIN))
4455 return -EPERM;
4456
4457 if (copy_from_user(label, arg, sizeof(label)))
4458 return -EFAULT;
4459
4460 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4461 pr_err("btrfs: unable to set label with more than %d bytes\n",
4462 BTRFS_LABEL_SIZE - 1);
4463 return -EINVAL;
4464 }
4465
4466 ret = mnt_want_write_file(file);
4467 if (ret)
4468 return ret;
4469
jeff.liua8bfd4a2013-01-05 02:48:08 +00004470 trans = btrfs_start_transaction(root, 0);
4471 if (IS_ERR(trans)) {
4472 ret = PTR_ERR(trans);
4473 goto out_unlock;
4474 }
4475
Anand Jaina1b83ac2013-07-19 17:39:32 +08004476 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004477 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004478 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004479 ret = btrfs_end_transaction(trans, root);
4480
4481out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004482 mnt_drop_write_file(file);
4483 return ret;
4484}
4485
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004486long btrfs_ioctl(struct file *file, unsigned int
4487 cmd, unsigned long arg)
4488{
Al Viro496ad9a2013-01-23 17:07:38 -05004489 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004490 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004491
4492 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004493 case FS_IOC_GETFLAGS:
4494 return btrfs_ioctl_getflags(file, argp);
4495 case FS_IOC_SETFLAGS:
4496 return btrfs_ioctl_setflags(file, argp);
4497 case FS_IOC_GETVERSION:
4498 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004499 case FITRIM:
4500 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004501 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004502 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004503 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004504 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004505 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004506 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004507 case BTRFS_IOC_SUBVOL_CREATE_V2:
4508 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004509 case BTRFS_IOC_SNAP_DESTROY:
4510 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004511 case BTRFS_IOC_SUBVOL_GETFLAGS:
4512 return btrfs_ioctl_subvol_getflags(file, argp);
4513 case BTRFS_IOC_SUBVOL_SETFLAGS:
4514 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004515 case BTRFS_IOC_DEFAULT_SUBVOL:
4516 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004517 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004518 return btrfs_ioctl_defrag(file, NULL);
4519 case BTRFS_IOC_DEFRAG_RANGE:
4520 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004521 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004522 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004523 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004524 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004525 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004526 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004527 case BTRFS_IOC_FS_INFO:
4528 return btrfs_ioctl_fs_info(root, argp);
4529 case BTRFS_IOC_DEV_INFO:
4530 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004531 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004532 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004533 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004534 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4535 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004536 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004537 case BTRFS_IOC_TRANS_START:
4538 return btrfs_ioctl_trans_start(file);
4539 case BTRFS_IOC_TRANS_END:
4540 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004541 case BTRFS_IOC_TREE_SEARCH:
4542 return btrfs_ioctl_tree_search(file, argp);
4543 case BTRFS_IOC_INO_LOOKUP:
4544 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004545 case BTRFS_IOC_INO_PATHS:
4546 return btrfs_ioctl_ino_to_path(root, argp);
4547 case BTRFS_IOC_LOGICAL_INO:
4548 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004549 case BTRFS_IOC_SPACE_INFO:
4550 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004551 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004552 btrfs_sync_fs(file->f_dentry->d_sb, 1);
4553 return 0;
Sage Weil46204592010-10-29 15:41:32 -04004554 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004555 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004556 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004557 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004558 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004559 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004560 case BTRFS_IOC_SCRUB_CANCEL:
4561 return btrfs_ioctl_scrub_cancel(root, argp);
4562 case BTRFS_IOC_SCRUB_PROGRESS:
4563 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004564 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004565 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004566 case BTRFS_IOC_BALANCE_CTL:
4567 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004568 case BTRFS_IOC_BALANCE_PROGRESS:
4569 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004570 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4571 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004572 case BTRFS_IOC_SEND:
4573 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004574 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004575 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004576 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004577 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004578 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004579 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004580 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004581 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004582 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004583 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004584 case BTRFS_IOC_QUOTA_RESCAN:
4585 return btrfs_ioctl_quota_rescan(file, argp);
4586 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4587 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004588 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4589 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004590 case BTRFS_IOC_DEV_REPLACE:
4591 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004592 case BTRFS_IOC_GET_FSLABEL:
4593 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004594 case BTRFS_IOC_SET_FSLABEL:
4595 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004596 case BTRFS_IOC_FILE_EXTENT_SAME:
4597 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004598 }
4599
4600 return -ENOTTY;
4601}