blob: 82fb11a361c4987e804900c4cda0b7291ee41209 [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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-12-29 13:36:45 +0800292 if (IS_ERR(trans)) {
293 ret = PTR_ERR(trans);
294 goto out_drop;
295 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200296
Li Zefan306424cc2011-12-14 20:12:02 -0500297 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400298 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500299 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200300 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200301
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302 btrfs_end_transaction(trans, root);
Li Zefanf062abf02011-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);
liubo2d4e6f6ad2011-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
Miao Xied5c12072013-02-28 10:04:33 +0000370static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400371 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400372 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200373 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000374 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375{
376 struct btrfs_trans_handle *trans;
377 struct btrfs_key key;
378 struct btrfs_root_item root_item;
379 struct btrfs_inode_item *inode_item;
380 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000381 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400382 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000383 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200384 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400385 int ret;
386 int err;
387 u64 objectid;
388 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500389 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000390 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200391 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392
Li Zefan581bb052011-04-20 10:06:11 +0800393 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400394 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400395 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000396
Miao Xied5c12072013-02-28 10:04:33 +0000397 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400398 /*
Miao Xied5c12072013-02-28 10:04:33 +0000399 * The same as the snapshot creation, please see the comment
400 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400401 */
Miao Xied5c12072013-02-28 10:04:33 +0000402 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400403 7, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000404 if (ret)
405 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400406
Miao Xied5c12072013-02-28 10:04:33 +0000407 trans = btrfs_start_transaction(root, 0);
408 if (IS_ERR(trans)) {
409 ret = PTR_ERR(trans);
410 goto out;
411 }
412 trans->block_rsv = &block_rsv;
413 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414
Miao Xie8696c532013-02-07 06:02:44 +0000415 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200416 if (ret)
417 goto fail;
418
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400419 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200420 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400421 if (IS_ERR(leaf)) {
422 ret = PTR_ERR(leaf);
423 goto fail;
424 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400425
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400426 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400427 btrfs_set_header_bytenr(leaf, leaf->start);
428 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400429 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400430 btrfs_set_header_owner(leaf, objectid);
431
432 write_extent_buffer(leaf, root->fs_info->fsid,
433 (unsigned long)btrfs_header_fsid(leaf),
434 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400435 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
436 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
437 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400438 btrfs_mark_buffer_dirty(leaf);
439
Alexander Block8ea05e32012-07-25 17:35:53 +0200440 memset(&root_item, 0, sizeof(root_item));
441
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800443 btrfs_set_stack_inode_generation(inode_item, 1);
444 btrfs_set_stack_inode_size(inode_item, 3);
445 btrfs_set_stack_inode_nlink(inode_item, 1);
446 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
447 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448
Qu Wenruo3cae2102013-07-16 11:19:18 +0800449 btrfs_set_root_flags(&root_item, 0);
450 btrfs_set_root_limit(&root_item, 0);
451 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000452
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400453 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400454 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400455 btrfs_set_root_level(&root_item, 0);
456 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000457 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400458 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459
Alexander Block8ea05e32012-07-25 17:35:53 +0200460 btrfs_set_root_generation_v2(&root_item,
461 btrfs_root_generation(&root_item));
462 uuid_le_gen(&new_uuid);
463 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800464 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
465 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200466 root_item.ctime = root_item.otime;
467 btrfs_set_root_ctransid(&root_item, trans->transid);
468 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400469
Chris Mason925baed2008-06-25 16:01:30 -0400470 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400471 free_extent_buffer(leaf);
472 leaf = NULL;
473
474 btrfs_set_root_dirid(&root_item, new_dirid);
475
476 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
479 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
480 &root_item);
481 if (ret)
482 goto fail;
483
Yan, Zheng76dda932009-09-21 16:00:26 -0400484 key.offset = (u64)-1;
485 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 if (IS_ERR(new_root)) {
487 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
488 ret = PTR_ERR(new_root);
489 goto fail;
490 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400491
492 btrfs_record_root_in_trans(trans, new_root);
493
Josef Bacikd82a6f1d2011-05-11 15:26:06 -0400494 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700495 if (ret) {
496 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100497 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700498 goto fail;
499 }
500
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400501 /*
502 * insert the directory item
503 */
Chris Mason3de45862008-11-17 21:02:50 -0500504 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100505 if (ret) {
506 btrfs_abort_transaction(trans, root, ret);
507 goto fail;
508 }
Chris Mason3de45862008-11-17 21:02:50 -0500509
510 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800511 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500512 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100513 if (ret) {
514 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100516 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500517
Yan Zheng52c26172009-01-05 15:43:43 -0500518 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
519 ret = btrfs_update_inode(trans, root, dir);
520 BUG_ON(ret);
521
Chris Mason0660b5a2008-11-17 20:37:39 -0500522 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400523 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800524 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400525
Chris Mason0660b5a2008-11-17 20:37:39 -0500526 BUG_ON(ret);
527
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400528fail:
Miao Xied5c12072013-02-28 10:04:33 +0000529 trans->block_rsv = NULL;
530 trans->bytes_reserved = 0;
Sage Weil72fd0322010-10-29 15:41:32 -0400531 if (async_transid) {
532 *async_transid = trans->transid;
533 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000534 if (err)
535 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400536 } else {
537 err = btrfs_commit_transaction(trans, root);
538 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400539 if (err && !ret)
540 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500541
542 if (!ret)
543 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Miao Xied5c12072013-02-28 10:04:33 +0000544out:
545 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546 return ret;
547}
548
Miao Xiee9662f72013-02-28 10:01:15 +0000549static int create_snapshot(struct btrfs_root *root, struct inode *dir,
550 struct dentry *dentry, char *name, int namelen,
551 u64 *async_transid, bool readonly,
552 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400553{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000554 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400555 struct btrfs_pending_snapshot *pending_snapshot;
556 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000557 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558
559 if (!root->ref_cows)
560 return -EINVAL;
561
Miao Xie6a038432013-05-15 07:48:24 +0000562 ret = btrfs_start_delalloc_inodes(root, 0);
563 if (ret)
564 return ret;
565
566 btrfs_wait_ordered_extents(root, 0);
567
Yan, Zhenga22285a2010-05-16 10:48:46 -0400568 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
569 if (!pending_snapshot)
570 return -ENOMEM;
571
Miao Xie66d8f3d2012-09-06 04:02:28 -0600572 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
573 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000574 /*
575 * 1 - parent dir inode
576 * 2 - dir entries
577 * 1 - root item
578 * 2 - root ref/backref
579 * 1 - root of snapshot
580 */
581 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
582 &pending_snapshot->block_rsv, 7,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400583 &pending_snapshot->qgroup_reserved,
584 false);
Miao Xied5c12072013-02-28 10:04:33 +0000585 if (ret)
586 goto out;
587
Yan, Zhenga22285a2010-05-16 10:48:46 -0400588 pending_snapshot->dentry = dentry;
589 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800590 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000591 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000592 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400593
Miao Xied5c12072013-02-28 10:04:33 +0000594 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400595 if (IS_ERR(trans)) {
596 ret = PTR_ERR(trans);
597 goto fail;
598 }
599
Josef Bacik83515832011-06-14 15:16:14 -0400600 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400601 list_add(&pending_snapshot->list,
602 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400603 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400604 if (async_transid) {
605 *async_transid = trans->transid;
606 ret = btrfs_commit_transaction_async(trans,
607 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000608 if (ret)
609 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400610 } else {
611 ret = btrfs_commit_transaction(trans,
612 root->fs_info->extent_root);
613 }
Miao Xieaec80302013-03-04 09:44:29 +0000614 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400615 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400616
617 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400618 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000619 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500621 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
622 if (ret)
623 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400624
Al Viro2fbe8c82011-07-16 21:38:06 -0400625 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000626 if (IS_ERR(inode)) {
627 ret = PTR_ERR(inode);
628 goto fail;
629 }
630 BUG_ON(!inode);
631 d_instantiate(dentry, inode);
632 ret = 0;
633fail:
Miao Xied5c12072013-02-28 10:04:33 +0000634 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
635 &pending_snapshot->block_rsv,
636 pending_snapshot->qgroup_reserved);
637out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400638 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400639 return ret;
640}
641
Sage Weil4260f7c2010-10-29 15:46:43 -0400642/* copy of check_sticky in fs/namei.c()
643* It's inline, so penalty for filesystems that don't use sticky bit is
644* minimal.
645*/
646static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
647{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800648 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400649
650 if (!(dir->i_mode & S_ISVTX))
651 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800652 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400653 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800654 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400655 return 0;
656 return !capable(CAP_FOWNER);
657}
658
659/* copy of may_delete in fs/namei.c()
660 * Check whether we can remove a link victim from directory dir, check
661 * whether the type of victim is right.
662 * 1. We can't do it if dir is read-only (done in permission())
663 * 2. We should have write and exec permissions on dir
664 * 3. We can't remove anything from append-only dir
665 * 4. We can't do anything with immutable dir (done in permission())
666 * 5. If the sticky bit on dir is set we should either
667 * a. be owner of dir, or
668 * b. be owner of victim, or
669 * c. have CAP_FOWNER capability
670 * 6. If the victim is append-only or immutable we can't do antyhing with
671 * links pointing to it.
672 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
673 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
674 * 9. We can't remove a root or mountpoint.
675 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
676 * nfs_async_unlink().
677 */
678
679static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
680{
681 int error;
682
683 if (!victim->d_inode)
684 return -ENOENT;
685
686 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400687 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400688
689 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
690 if (error)
691 return error;
692 if (IS_APPEND(dir))
693 return -EPERM;
694 if (btrfs_check_sticky(dir, victim->d_inode)||
695 IS_APPEND(victim->d_inode)||
696 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
697 return -EPERM;
698 if (isdir) {
699 if (!S_ISDIR(victim->d_inode->i_mode))
700 return -ENOTDIR;
701 if (IS_ROOT(victim))
702 return -EBUSY;
703 } else if (S_ISDIR(victim->d_inode->i_mode))
704 return -EISDIR;
705 if (IS_DEADDIR(dir))
706 return -ENOENT;
707 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
708 return -EBUSY;
709 return 0;
710}
711
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400712/* copy of may_create in fs/namei.c() */
713static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
714{
715 if (child->d_inode)
716 return -EEXIST;
717 if (IS_DEADDIR(dir))
718 return -ENOENT;
719 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
720}
721
722/*
723 * Create a new subvolume below @parent. This is largely modeled after
724 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
725 * inside this filesystem so it's quite a bit simpler.
726 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400727static noinline int btrfs_mksubvol(struct path *parent,
728 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400729 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200730 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000731 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400732{
Yan, Zheng76dda932009-09-21 16:00:26 -0400733 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400734 struct dentry *dentry;
735 int error;
736
David Sterba5c50c9b2013-03-22 18:12:51 +0000737 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
738 if (error == -EINTR)
739 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400740
741 dentry = lookup_one_len(name, parent->dentry, namelen);
742 error = PTR_ERR(dentry);
743 if (IS_ERR(dentry))
744 goto out_unlock;
745
746 error = -EEXIST;
747 if (dentry->d_inode)
748 goto out_dput;
749
Yan, Zheng76dda932009-09-21 16:00:26 -0400750 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400751 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600752 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400753
Chris Mason9c520572012-12-17 14:26:57 -0500754 /*
755 * even if this name doesn't exist, we may get hash collisions.
756 * check for them now when we can safely fail
757 */
758 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
759 dir->i_ino, name,
760 namelen);
761 if (error)
762 goto out_dput;
763
Yan, Zheng76dda932009-09-21 16:00:26 -0400764 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
765
766 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
767 goto out_up_read;
768
Chris Mason3de45862008-11-17 21:02:50 -0500769 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000770 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200771 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500772 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000773 error = create_subvol(dir, dentry, name, namelen,
774 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500775 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400776 if (!error)
777 fsnotify_mkdir(dir, dentry);
778out_up_read:
779 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400780out_dput:
781 dput(dentry);
782out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400783 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400784 return error;
785}
786
Chris Mason4cb53002011-05-24 15:35:30 -0400787/*
788 * When we're defragging a range, we don't want to kick it off again
789 * if it is really just waiting for delalloc to send it down.
790 * If we find a nice big extent or delalloc range for the bytes in the
791 * file you want to defrag, we return 0 to let you know to skip this
792 * part of the file
793 */
794static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
795{
796 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
797 struct extent_map *em = NULL;
798 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
799 u64 end;
800
801 read_lock(&em_tree->lock);
802 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
803 read_unlock(&em_tree->lock);
804
805 if (em) {
806 end = extent_map_end(em);
807 free_extent_map(em);
808 if (end - offset > thresh)
809 return 0;
810 }
811 /* if we already have a nice delalloc here, just stop */
812 thresh /= 2;
813 end = count_range_bits(io_tree, &offset, offset + thresh,
814 thresh, EXTENT_DELALLOC, 1);
815 if (end >= thresh)
816 return 0;
817 return 1;
818}
819
820/*
821 * helper function to walk through a file and find extents
822 * newer than a specific transid, and smaller than thresh.
823 *
824 * This is used by the defragging code to find new and small
825 * extents
826 */
827static int find_new_extents(struct btrfs_root *root,
828 struct inode *inode, u64 newer_than,
829 u64 *off, int thresh)
830{
831 struct btrfs_path *path;
832 struct btrfs_key min_key;
833 struct btrfs_key max_key;
834 struct extent_buffer *leaf;
835 struct btrfs_file_extent_item *extent;
836 int type;
837 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000838 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400839
840 path = btrfs_alloc_path();
841 if (!path)
842 return -ENOMEM;
843
David Sterbaa4689d22011-05-31 17:08:14 +0000844 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400845 min_key.type = BTRFS_EXTENT_DATA_KEY;
846 min_key.offset = *off;
847
David Sterbaa4689d22011-05-31 17:08:14 +0000848 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400849 max_key.type = (u8)-1;
850 max_key.offset = (u64)-1;
851
852 path->keep_locks = 1;
853
854 while(1) {
855 ret = btrfs_search_forward(root, &min_key, &max_key,
Eric Sandeende78b512013-01-31 18:21:12 +0000856 path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400857 if (ret != 0)
858 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000859 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400860 goto none;
861 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
862 goto none;
863
864 leaf = path->nodes[0];
865 extent = btrfs_item_ptr(leaf, path->slots[0],
866 struct btrfs_file_extent_item);
867
868 type = btrfs_file_extent_type(leaf, extent);
869 if (type == BTRFS_FILE_EXTENT_REG &&
870 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
871 check_defrag_in_cache(inode, min_key.offset, thresh)) {
872 *off = min_key.offset;
873 btrfs_free_path(path);
874 return 0;
875 }
876
877 if (min_key.offset == (u64)-1)
878 goto none;
879
880 min_key.offset++;
881 btrfs_release_path(path);
882 }
883none:
884 btrfs_free_path(path);
885 return -ENOENT;
886}
887
Li Zefan6c282eb2012-06-11 16:03:35 +0800888static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400889{
890 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500891 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800892 struct extent_map *em;
893 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500894
895 /*
896 * hopefully we have this extent in the tree already, try without
897 * the full extent lock
898 */
899 read_lock(&em_tree->lock);
900 em = lookup_extent_mapping(em_tree, start, len);
901 read_unlock(&em_tree->lock);
902
903 if (!em) {
904 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100905 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500906 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100907 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500908
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000909 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800910 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500911 }
912
Li Zefan6c282eb2012-06-11 16:03:35 +0800913 return em;
914}
915
916static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
917{
918 struct extent_map *next;
919 bool ret = true;
920
921 /* this is the last extent */
922 if (em->start + em->len >= i_size_read(inode))
923 return false;
924
925 next = defrag_lookup_extent(inode, em->start + em->len);
926 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
927 ret = false;
928
929 free_extent_map(next);
930 return ret;
931}
932
933static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400934 u64 *last_len, u64 *skip, u64 *defrag_end,
935 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800936{
937 struct extent_map *em;
938 int ret = 1;
939 bool next_mergeable = true;
940
941 /*
942 * make sure that once we start defragging an extent, we keep on
943 * defragging it
944 */
945 if (start < *defrag_end)
946 return 1;
947
948 *skip = 0;
949
950 em = defrag_lookup_extent(inode, start);
951 if (!em)
952 return 0;
953
Chris Mason940100a2010-03-10 10:52:59 -0500954 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400955 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500956 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400957 goto out;
958 }
959
Li Zefan6c282eb2012-06-11 16:03:35 +0800960 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500961
962 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800963 * we hit a real extent, if it is big or the next extent is not a
964 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500965 */
Andrew Mahonea43a2112012-06-19 21:08:32 -0400966 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +0800967 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500968 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400969out:
Chris Mason940100a2010-03-10 10:52:59 -0500970 /*
971 * last_len ends up being a counter of how many bytes we've defragged.
972 * every time we choose not to defrag an extent, we reset *last_len
973 * so that the next tiny extent will force a defrag.
974 *
975 * The end result of this is that tiny extents before a single big
976 * extent will force at least part of that big extent to be defragged.
977 */
978 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500979 *defrag_end = extent_map_end(em);
980 } else {
981 *last_len = 0;
982 *skip = extent_map_end(em);
983 *defrag_end = 0;
984 }
985
986 free_extent_map(em);
987 return ret;
988}
989
Chris Mason4cb53002011-05-24 15:35:30 -0400990/*
991 * it doesn't do much good to defrag one or two pages
992 * at a time. This pulls in a nice chunk of pages
993 * to COW and defrag.
994 *
995 * It also makes sure the delalloc code has enough
996 * dirty data to avoid making new small extents as part
997 * of the defrag
998 *
999 * It's a good idea to start RA on this range
1000 * before calling this.
1001 */
1002static int cluster_pages_for_defrag(struct inode *inode,
1003 struct page **pages,
1004 unsigned long start_index,
1005 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006{
Chris Mason4cb53002011-05-24 15:35:30 -04001007 unsigned long file_end;
1008 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001009 u64 page_start;
1010 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001011 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001012 int ret;
1013 int i;
1014 int i_done;
1015 struct btrfs_ordered_extent *ordered;
1016 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001017 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001018 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001019
Chris Mason4cb53002011-05-24 15:35:30 -04001020 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001021 if (!isize || start_index > file_end)
1022 return 0;
1023
1024 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001025
1026 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001027 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001028 if (ret)
1029 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001030 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001031 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001032
1033 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001034 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001035 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001036again:
Josef Bacika94733d2011-07-11 10:47:06 -04001037 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001038 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001039 if (!page)
1040 break;
1041
Miao Xie600a45e2012-02-16 15:01:24 +08001042 page_start = page_offset(page);
1043 page_end = page_start + PAGE_CACHE_SIZE - 1;
1044 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001045 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001046 ordered = btrfs_lookup_ordered_extent(inode,
1047 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001048 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001049 if (!ordered)
1050 break;
1051
1052 unlock_page(page);
1053 btrfs_start_ordered_extent(inode, ordered, 1);
1054 btrfs_put_ordered_extent(ordered);
1055 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001056 /*
1057 * we unlocked the page above, so we need check if
1058 * it was released or not.
1059 */
1060 if (page->mapping != inode->i_mapping) {
1061 unlock_page(page);
1062 page_cache_release(page);
1063 goto again;
1064 }
Miao Xie600a45e2012-02-16 15:01:24 +08001065 }
1066
Chris Mason4cb53002011-05-24 15:35:30 -04001067 if (!PageUptodate(page)) {
1068 btrfs_readpage(NULL, page);
1069 lock_page(page);
1070 if (!PageUptodate(page)) {
1071 unlock_page(page);
1072 page_cache_release(page);
1073 ret = -EIO;
1074 break;
1075 }
1076 }
Miao Xie600a45e2012-02-16 15:01:24 +08001077
Miao Xie600a45e2012-02-16 15:01:24 +08001078 if (page->mapping != inode->i_mapping) {
1079 unlock_page(page);
1080 page_cache_release(page);
1081 goto again;
1082 }
1083
Chris Mason4cb53002011-05-24 15:35:30 -04001084 pages[i] = page;
1085 i_done++;
1086 }
1087 if (!i_done || ret)
1088 goto out;
1089
1090 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1091 goto out;
1092
1093 /*
1094 * so now we have a nice long stream of locked
1095 * and up to date pages, lets wait on them
1096 */
1097 for (i = 0; i < i_done; i++)
1098 wait_on_page_writeback(pages[i]);
1099
1100 page_start = page_offset(pages[0]);
1101 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1102
1103 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001104 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001105 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1106 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001107 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1108 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001109
Liu Bo1f12bd02012-03-29 09:57:44 -04001110 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001111 spin_lock(&BTRFS_I(inode)->lock);
1112 BTRFS_I(inode)->outstanding_extents++;
1113 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001114 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001115 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001116 }
1117
1118
Liu Bo9e8a4a82012-09-05 19:10:51 -06001119 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1120 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001121
1122 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1123 page_start, page_end - 1, &cached_state,
1124 GFP_NOFS);
1125
1126 for (i = 0; i < i_done; i++) {
1127 clear_page_dirty_for_io(pages[i]);
1128 ClearPageChecked(pages[i]);
1129 set_page_extent_mapped(pages[i]);
1130 set_page_dirty(pages[i]);
1131 unlock_page(pages[i]);
1132 page_cache_release(pages[i]);
1133 }
1134 return i_done;
1135out:
1136 for (i = 0; i < i_done; i++) {
1137 unlock_page(pages[i]);
1138 page_cache_release(pages[i]);
1139 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001140 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001141 return ret;
1142
1143}
1144
1145int btrfs_defrag_file(struct inode *inode, struct file *file,
1146 struct btrfs_ioctl_defrag_range_args *range,
1147 u64 newer_than, unsigned long max_to_defrag)
1148{
1149 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001150 struct file_ra_state *ra = NULL;
1151 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001152 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001153 u64 last_len = 0;
1154 u64 skip = 0;
1155 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001156 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001157 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001158 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001159 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001160 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001161 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001162 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001163 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1164 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001165 u64 new_align = ~((u64)128 * 1024 - 1);
1166 struct page **pages = NULL;
1167
Liu Bo0abd5b12013-04-16 09:20:28 +00001168 if (isize == 0)
1169 return 0;
1170
1171 if (range->start >= isize)
1172 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001173
1174 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1175 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1176 return -EINVAL;
1177 if (range->compress_type)
1178 compress_type = range->compress_type;
1179 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001180
Liu Bo0abd5b12013-04-16 09:20:28 +00001181 if (extent_thresh == 0)
1182 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001183
Chris Mason4cb53002011-05-24 15:35:30 -04001184 /*
1185 * if we were not given a file, allocate a readahead
1186 * context
1187 */
1188 if (!file) {
1189 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1190 if (!ra)
1191 return -ENOMEM;
1192 file_ra_state_init(ra, inode->i_mapping);
1193 } else {
1194 ra = &file->f_ra;
1195 }
1196
Li Zefan008873e2011-09-02 15:57:07 +08001197 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001198 GFP_NOFS);
1199 if (!pages) {
1200 ret = -ENOMEM;
1201 goto out_ra;
1202 }
1203
1204 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001205 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001206 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001207 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1208 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001209 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001210 }
1211
Chris Mason4cb53002011-05-24 15:35:30 -04001212 if (newer_than) {
1213 ret = find_new_extents(root, inode, newer_than,
1214 &newer_off, 64 * 1024);
1215 if (!ret) {
1216 range->start = newer_off;
1217 /*
1218 * we always align our defrag to help keep
1219 * the extents in the file evenly spaced
1220 */
1221 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001222 } else
1223 goto out_ra;
1224 } else {
1225 i = range->start >> PAGE_CACHE_SHIFT;
1226 }
1227 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001228 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001229
Li Zefan2a0f7f52011-10-10 15:43:34 -04001230 /*
1231 * make writeback starts from i, so the defrag range can be
1232 * written sequentially.
1233 */
1234 if (i < inode->i_mapping->writeback_index)
1235 inode->i_mapping->writeback_index = i;
1236
Chris Masonf7f43cc2011-10-11 11:41:40 -04001237 while (i <= last_index && defrag_count < max_to_defrag &&
1238 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1239 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001240 /*
1241 * make sure we stop running if someone unmounts
1242 * the FS
1243 */
1244 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1245 break;
1246
David Sterba210549e2013-02-09 23:38:06 +00001247 if (btrfs_defrag_cancelled(root->fs_info)) {
1248 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1249 ret = -EAGAIN;
1250 break;
1251 }
1252
Liu Bo66c26892012-03-29 09:57:45 -04001253 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001254 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001255 &defrag_end, range->flags &
1256 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001257 unsigned long next;
1258 /*
1259 * the should_defrag function tells us how much to skip
1260 * bump our counter by the suggested amount
1261 */
1262 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1263 i = max(i + 1, next);
1264 continue;
1265 }
Li Zefan008873e2011-09-02 15:57:07 +08001266
1267 if (!newer_than) {
1268 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1269 PAGE_CACHE_SHIFT) - i;
1270 cluster = min(cluster, max_cluster);
1271 } else {
1272 cluster = max_cluster;
1273 }
1274
Chris Mason1e701a32010-03-11 09:42:04 -05001275 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001276 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001277
Li Zefan008873e2011-09-02 15:57:07 +08001278 if (i + cluster > ra_index) {
1279 ra_index = max(i, ra_index);
1280 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1281 cluster);
1282 ra_index += max_cluster;
1283 }
Chris Mason940100a2010-03-10 10:52:59 -05001284
Liu Boecb8bea2012-03-29 09:57:44 -04001285 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001286 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001287 if (ret < 0) {
1288 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001289 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001290 }
Chris Mason940100a2010-03-10 10:52:59 -05001291
Chris Mason4cb53002011-05-24 15:35:30 -04001292 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001293 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001294 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001295
1296 if (newer_than) {
1297 if (newer_off == (u64)-1)
1298 break;
1299
Liu Boe1f041e2012-03-29 09:57:45 -04001300 if (ret > 0)
1301 i += ret;
1302
Chris Mason4cb53002011-05-24 15:35:30 -04001303 newer_off = max(newer_off + 1,
1304 (u64)i << PAGE_CACHE_SHIFT);
1305
1306 ret = find_new_extents(root, inode,
1307 newer_than, &newer_off,
1308 64 * 1024);
1309 if (!ret) {
1310 range->start = newer_off;
1311 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001312 } else {
1313 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001314 }
Chris Mason4cb53002011-05-24 15:35:30 -04001315 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001316 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001317 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001318 last_len += ret << PAGE_CACHE_SHIFT;
1319 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001320 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001321 last_len = 0;
1322 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001323 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001324 }
1325
Chris Mason1e701a32010-03-11 09:42:04 -05001326 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1327 filemap_flush(inode->i_mapping);
1328
1329 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1330 /* the filemap_flush will queue IO into the worker threads, but
1331 * we have to make sure the IO is actually started and that
1332 * ordered extents get created before we return
1333 */
1334 atomic_inc(&root->fs_info->async_submit_draining);
1335 while (atomic_read(&root->fs_info->nr_async_submits) ||
1336 atomic_read(&root->fs_info->async_delalloc_pages)) {
1337 wait_event(root->fs_info->async_submit_wait,
1338 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1339 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1340 }
1341 atomic_dec(&root->fs_info->async_submit_draining);
1342
1343 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001344 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001345 mutex_unlock(&inode->i_mutex);
1346 }
1347
Li Zefan1a419d82010-10-25 15:12:50 +08001348 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001349 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001350 }
1351
Diego Calleja60ccf822011-09-01 16:33:57 +02001352 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001353
Chris Mason4cb53002011-05-24 15:35:30 -04001354out_ra:
1355 if (!file)
1356 kfree(ra);
1357 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001358 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001359}
1360
Miao Xie198605a2012-11-26 08:43:45 +00001361static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001362 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001363{
1364 u64 new_size;
1365 u64 old_size;
1366 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001367 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001368 struct btrfs_ioctl_vol_args *vol_args;
1369 struct btrfs_trans_handle *trans;
1370 struct btrfs_device *device = NULL;
1371 char *sizestr;
1372 char *devstr = NULL;
1373 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001374 int mod = 0;
1375
Chris Masone441d542009-01-05 16:57:23 -05001376 if (!capable(CAP_SYS_ADMIN))
1377 return -EPERM;
1378
Miao Xie198605a2012-11-26 08:43:45 +00001379 ret = mnt_want_write_file(file);
1380 if (ret)
1381 return ret;
1382
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001383 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1384 1)) {
1385 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Miao Xie97547672012-12-21 10:38:50 +00001386 mnt_drop_write_file(file);
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02001387 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001388 }
1389
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001390 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001391 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001392 if (IS_ERR(vol_args)) {
1393 ret = PTR_ERR(vol_args);
1394 goto out;
1395 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001396
1397 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001398
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001399 sizestr = vol_args->name;
1400 devstr = strchr(sizestr, ':');
1401 if (devstr) {
1402 char *end;
1403 sizestr = devstr + 1;
1404 *devstr = '\0';
1405 devstr = vol_args->name;
1406 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001407 if (!devid) {
1408 ret = -EINVAL;
1409 goto out_free;
1410 }
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001411 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001412 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001413 }
Miao Xiedba60f32012-12-21 09:19:51 +00001414
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001415 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001416 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001417 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001418 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001419 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001420 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421 }
Miao Xiedba60f32012-12-21 09:19:51 +00001422
1423 if (!device->writeable) {
Liu Bo4e42ae12012-06-14 02:23:19 -06001424 printk(KERN_INFO "btrfs: resizer unable to apply on "
Miao Xiedba60f32012-12-21 09:19:51 +00001425 "readonly device %llu\n",
Chris Masona8c4a332012-06-15 20:07:17 -04001426 (unsigned long long)devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001427 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001428 goto out_free;
1429 }
1430
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431 if (!strcmp(sizestr, "max"))
1432 new_size = device->bdev->bd_inode->i_size;
1433 else {
1434 if (sizestr[0] == '-') {
1435 mod = -1;
1436 sizestr++;
1437 } else if (sizestr[0] == '+') {
1438 mod = 1;
1439 sizestr++;
1440 }
Akinobu Mita91748462010-02-28 10:59:11 +00001441 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001442 if (new_size == 0) {
1443 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001444 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001445 }
1446 }
1447
Stefan Behrens63a212a2012-11-05 18:29:28 +01001448 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001449 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001450 goto out_free;
1451 }
1452
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001453 old_size = device->total_bytes;
1454
1455 if (mod < 0) {
1456 if (new_size > old_size) {
1457 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001458 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001459 }
1460 new_size = old_size - new_size;
1461 } else if (mod > 0) {
1462 new_size = old_size + new_size;
1463 }
1464
1465 if (new_size < 256 * 1024 * 1024) {
1466 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001467 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001468 }
1469 if (new_size > device->bdev->bd_inode->i_size) {
1470 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001471 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001472 }
1473
1474 do_div(new_size, root->sectorsize);
1475 new_size *= root->sectorsize;
1476
Josef Bacik606686e2012-06-04 14:03:51 -04001477 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1478 rcu_str_deref(device->name),
1479 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001480
1481 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001482 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001483 if (IS_ERR(trans)) {
1484 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001485 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001486 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001487 ret = btrfs_grow_device(trans, device, new_size);
1488 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001489 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001490 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001491 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001492
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001493out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001494 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001495out:
1496 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001497 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001498 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001499 return ret;
1500}
1501
Sage Weil72fd0322010-10-29 15:41:32 -04001502static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001503 char *name, unsigned long fd, int subvol,
1504 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001505 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001506{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001508 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001509
Liu Boa874a632012-06-29 03:58:46 -06001510 ret = mnt_want_write_file(file);
1511 if (ret)
1512 goto out;
1513
Sage Weil72fd0322010-10-29 15:41:32 -04001514 namelen = strlen(name);
1515 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001516 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001517 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001518 }
1519
Chris Mason16780ca2012-02-20 22:14:55 -05001520 if (name[0] == '.' &&
1521 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1522 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001523 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001524 }
1525
Chris Mason3de45862008-11-17 21:02:50 -05001526 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001527 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001528 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001529 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001530 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001531 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001532 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001533 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001534 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001535 }
1536
Al Viro496ad9a2013-01-23 17:07:38 -05001537 src_inode = file_inode(src.file);
1538 if (src_inode->i_sb != file_inode(file)->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001539 printk(KERN_INFO "btrfs: Snapshot src from "
1540 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001541 ret = -EINVAL;
Al Viroecd18812012-08-26 21:20:24 -04001542 } else {
1543 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1544 BTRFS_I(src_inode)->root,
1545 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001546 }
Al Viro2903ff02012-08-28 12:52:22 -04001547 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001548 }
Liu Boa874a632012-06-29 03:58:46 -06001549out_drop_write:
1550 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001551out:
Sage Weil72fd0322010-10-29 15:41:32 -04001552 return ret;
1553}
1554
1555static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001556 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001557{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001558 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001559 int ret;
1560
Li Zefanfa0d2b92010-12-20 15:53:28 +08001561 vol_args = memdup_user(arg, sizeof(*vol_args));
1562 if (IS_ERR(vol_args))
1563 return PTR_ERR(vol_args);
1564 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001565
Li Zefanfa0d2b92010-12-20 15:53:28 +08001566 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001567 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001568 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001569
Li Zefanfa0d2b92010-12-20 15:53:28 +08001570 kfree(vol_args);
1571 return ret;
1572}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001573
Li Zefanfa0d2b92010-12-20 15:53:28 +08001574static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1575 void __user *arg, int subvol)
1576{
1577 struct btrfs_ioctl_vol_args_v2 *vol_args;
1578 int ret;
1579 u64 transid = 0;
1580 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001581 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001582 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001583
Li Zefanfa0d2b92010-12-20 15:53:28 +08001584 vol_args = memdup_user(arg, sizeof(*vol_args));
1585 if (IS_ERR(vol_args))
1586 return PTR_ERR(vol_args);
1587 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001588
Li Zefanb83cc962010-12-20 16:04:08 +08001589 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001590 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1591 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001592 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001593 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001594 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001595
1596 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1597 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001598 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1599 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001600 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1601 if (vol_args->size > PAGE_CACHE_SIZE) {
1602 ret = -EINVAL;
1603 goto out;
1604 }
1605 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1606 if (IS_ERR(inherit)) {
1607 ret = PTR_ERR(inherit);
1608 goto out;
1609 }
1610 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001611
1612 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001613 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001614 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001615
1616 if (ret == 0 && ptr &&
1617 copy_to_user(arg +
1618 offsetof(struct btrfs_ioctl_vol_args_v2,
1619 transid), ptr, sizeof(*ptr)))
1620 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001621out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001622 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001623 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001624 return ret;
1625}
1626
Li Zefan0caa1022010-12-20 16:30:25 +08001627static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1628 void __user *arg)
1629{
Al Viro496ad9a2013-01-23 17:07:38 -05001630 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001631 struct btrfs_root *root = BTRFS_I(inode)->root;
1632 int ret = 0;
1633 u64 flags = 0;
1634
Li Zefan33345d012011-04-20 10:31:50 +08001635 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001636 return -EINVAL;
1637
1638 down_read(&root->fs_info->subvol_sem);
1639 if (btrfs_root_readonly(root))
1640 flags |= BTRFS_SUBVOL_RDONLY;
1641 up_read(&root->fs_info->subvol_sem);
1642
1643 if (copy_to_user(arg, &flags, sizeof(flags)))
1644 ret = -EFAULT;
1645
1646 return ret;
1647}
1648
1649static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1650 void __user *arg)
1651{
Al Viro496ad9a2013-01-23 17:07:38 -05001652 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001653 struct btrfs_root *root = BTRFS_I(inode)->root;
1654 struct btrfs_trans_handle *trans;
1655 u64 root_flags;
1656 u64 flags;
1657 int ret = 0;
1658
Liu Bob9ca0662012-06-29 03:58:49 -06001659 ret = mnt_want_write_file(file);
1660 if (ret)
1661 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001662
Liu Bob9ca0662012-06-29 03:58:49 -06001663 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1664 ret = -EINVAL;
1665 goto out_drop_write;
1666 }
Li Zefan0caa1022010-12-20 16:30:25 +08001667
Liu Bob9ca0662012-06-29 03:58:49 -06001668 if (copy_from_user(&flags, arg, sizeof(flags))) {
1669 ret = -EFAULT;
1670 goto out_drop_write;
1671 }
Li Zefan0caa1022010-12-20 16:30:25 +08001672
Liu Bob9ca0662012-06-29 03:58:49 -06001673 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1674 ret = -EINVAL;
1675 goto out_drop_write;
1676 }
Li Zefan0caa1022010-12-20 16:30:25 +08001677
Liu Bob9ca0662012-06-29 03:58:49 -06001678 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1679 ret = -EOPNOTSUPP;
1680 goto out_drop_write;
1681 }
Li Zefan0caa1022010-12-20 16:30:25 +08001682
Liu Bob9ca0662012-06-29 03:58:49 -06001683 if (!inode_owner_or_capable(inode)) {
1684 ret = -EACCES;
1685 goto out_drop_write;
1686 }
Li Zefanb4dc2b82011-02-16 06:06:34 +00001687
Li Zefan0caa1022010-12-20 16:30:25 +08001688 down_write(&root->fs_info->subvol_sem);
1689
1690 /* nothing to do */
1691 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001692 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001693
1694 root_flags = btrfs_root_flags(&root->root_item);
1695 if (flags & BTRFS_SUBVOL_RDONLY)
1696 btrfs_set_root_flags(&root->root_item,
1697 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1698 else
1699 btrfs_set_root_flags(&root->root_item,
1700 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1701
1702 trans = btrfs_start_transaction(root, 1);
1703 if (IS_ERR(trans)) {
1704 ret = PTR_ERR(trans);
1705 goto out_reset;
1706 }
1707
Li Zefanb4dc2b82011-02-16 06:06:34 +00001708 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001709 &root->root_key, &root->root_item);
1710
1711 btrfs_commit_transaction(trans, root);
1712out_reset:
1713 if (ret)
1714 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001715out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001716 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001717out_drop_write:
1718 mnt_drop_write_file(file);
1719out:
Li Zefan0caa1022010-12-20 16:30:25 +08001720 return ret;
1721}
1722
Yan, Zheng76dda932009-09-21 16:00:26 -04001723/*
1724 * helper to check if the subvolume references other subvolumes
1725 */
1726static noinline int may_destroy_subvol(struct btrfs_root *root)
1727{
1728 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001729 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001730 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001731 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001732 int ret;
1733
1734 path = btrfs_alloc_path();
1735 if (!path)
1736 return -ENOMEM;
1737
Josef Bacik175a2b82013-08-12 15:36:44 -04001738 /* Make sure this root isn't set as the default subvol */
1739 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1740 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1741 dir_id, "default", 7, 0);
1742 if (di && !IS_ERR(di)) {
1743 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1744 if (key.objectid == root->root_key.objectid) {
1745 ret = -ENOTEMPTY;
1746 goto out;
1747 }
1748 btrfs_release_path(path);
1749 }
1750
Yan, Zheng76dda932009-09-21 16:00:26 -04001751 key.objectid = root->root_key.objectid;
1752 key.type = BTRFS_ROOT_REF_KEY;
1753 key.offset = (u64)-1;
1754
1755 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1756 &key, path, 0, 0);
1757 if (ret < 0)
1758 goto out;
1759 BUG_ON(ret == 0);
1760
1761 ret = 0;
1762 if (path->slots[0] > 0) {
1763 path->slots[0]--;
1764 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1765 if (key.objectid == root->root_key.objectid &&
1766 key.type == BTRFS_ROOT_REF_KEY)
1767 ret = -ENOTEMPTY;
1768 }
1769out:
1770 btrfs_free_path(path);
1771 return ret;
1772}
1773
Chris Masonac8e9812010-02-28 15:39:26 -05001774static noinline int key_in_sk(struct btrfs_key *key,
1775 struct btrfs_ioctl_search_key *sk)
1776{
Chris Masonabc6e132010-03-18 12:10:08 -04001777 struct btrfs_key test;
1778 int ret;
1779
1780 test.objectid = sk->min_objectid;
1781 test.type = sk->min_type;
1782 test.offset = sk->min_offset;
1783
1784 ret = btrfs_comp_cpu_keys(key, &test);
1785 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001786 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001787
1788 test.objectid = sk->max_objectid;
1789 test.type = sk->max_type;
1790 test.offset = sk->max_offset;
1791
1792 ret = btrfs_comp_cpu_keys(key, &test);
1793 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001794 return 0;
1795 return 1;
1796}
1797
1798static noinline int copy_to_sk(struct btrfs_root *root,
1799 struct btrfs_path *path,
1800 struct btrfs_key *key,
1801 struct btrfs_ioctl_search_key *sk,
1802 char *buf,
1803 unsigned long *sk_offset,
1804 int *num_found)
1805{
1806 u64 found_transid;
1807 struct extent_buffer *leaf;
1808 struct btrfs_ioctl_search_header sh;
1809 unsigned long item_off;
1810 unsigned long item_len;
1811 int nritems;
1812 int i;
1813 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001814 int ret = 0;
1815
1816 leaf = path->nodes[0];
1817 slot = path->slots[0];
1818 nritems = btrfs_header_nritems(leaf);
1819
1820 if (btrfs_header_generation(leaf) > sk->max_transid) {
1821 i = nritems;
1822 goto advance_key;
1823 }
1824 found_transid = btrfs_header_generation(leaf);
1825
1826 for (i = slot; i < nritems; i++) {
1827 item_off = btrfs_item_ptr_offset(leaf, i);
1828 item_len = btrfs_item_size_nr(leaf, i);
1829
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001830 btrfs_item_key_to_cpu(leaf, key, i);
1831 if (!key_in_sk(key, sk))
1832 continue;
1833
1834 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001835 item_len = 0;
1836
1837 if (sizeof(sh) + item_len + *sk_offset >
1838 BTRFS_SEARCH_ARGS_BUFSIZE) {
1839 ret = 1;
1840 goto overflow;
1841 }
1842
Chris Masonac8e9812010-02-28 15:39:26 -05001843 sh.objectid = key->objectid;
1844 sh.offset = key->offset;
1845 sh.type = key->type;
1846 sh.len = item_len;
1847 sh.transid = found_transid;
1848
1849 /* copy search result header */
1850 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1851 *sk_offset += sizeof(sh);
1852
1853 if (item_len) {
1854 char *p = buf + *sk_offset;
1855 /* copy the item */
1856 read_extent_buffer(leaf, p,
1857 item_off, item_len);
1858 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001859 }
Hugo Millse2156862011-05-14 17:43:41 +00001860 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001861
1862 if (*num_found >= sk->nr_items)
1863 break;
1864 }
1865advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001866 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001867 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1868 key->offset++;
1869 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1870 key->offset = 0;
1871 key->type++;
1872 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1873 key->offset = 0;
1874 key->type = 0;
1875 key->objectid++;
1876 } else
1877 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001878overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001879 return ret;
1880}
1881
1882static noinline int search_ioctl(struct inode *inode,
1883 struct btrfs_ioctl_search_args *args)
1884{
1885 struct btrfs_root *root;
1886 struct btrfs_key key;
1887 struct btrfs_key max_key;
1888 struct btrfs_path *path;
1889 struct btrfs_ioctl_search_key *sk = &args->key;
1890 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1891 int ret;
1892 int num_found = 0;
1893 unsigned long sk_offset = 0;
1894
1895 path = btrfs_alloc_path();
1896 if (!path)
1897 return -ENOMEM;
1898
1899 if (sk->tree_id == 0) {
1900 /* search the root of the inode that was passed */
1901 root = BTRFS_I(inode)->root;
1902 } else {
1903 key.objectid = sk->tree_id;
1904 key.type = BTRFS_ROOT_ITEM_KEY;
1905 key.offset = (u64)-1;
1906 root = btrfs_read_fs_root_no_name(info, &key);
1907 if (IS_ERR(root)) {
1908 printk(KERN_ERR "could not find root %llu\n",
1909 sk->tree_id);
1910 btrfs_free_path(path);
1911 return -ENOENT;
1912 }
1913 }
1914
1915 key.objectid = sk->min_objectid;
1916 key.type = sk->min_type;
1917 key.offset = sk->min_offset;
1918
1919 max_key.objectid = sk->max_objectid;
1920 max_key.type = sk->max_type;
1921 max_key.offset = sk->max_offset;
1922
1923 path->keep_locks = 1;
1924
1925 while(1) {
Eric Sandeende78b512013-01-31 18:21:12 +00001926 ret = btrfs_search_forward(root, &key, &max_key, path,
Chris Masonac8e9812010-02-28 15:39:26 -05001927 sk->min_transid);
1928 if (ret != 0) {
1929 if (ret > 0)
1930 ret = 0;
1931 goto err;
1932 }
1933 ret = copy_to_sk(root, path, &key, sk, args->buf,
1934 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001935 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001936 if (ret || num_found >= sk->nr_items)
1937 break;
1938
1939 }
1940 ret = 0;
1941err:
1942 sk->nr_items = num_found;
1943 btrfs_free_path(path);
1944 return ret;
1945}
1946
1947static noinline int btrfs_ioctl_tree_search(struct file *file,
1948 void __user *argp)
1949{
1950 struct btrfs_ioctl_search_args *args;
1951 struct inode *inode;
1952 int ret;
1953
1954 if (!capable(CAP_SYS_ADMIN))
1955 return -EPERM;
1956
Julia Lawall2354d08f2010-10-29 15:14:18 -04001957 args = memdup_user(argp, sizeof(*args));
1958 if (IS_ERR(args))
1959 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001960
Al Viro496ad9a2013-01-23 17:07:38 -05001961 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05001962 ret = search_ioctl(inode, args);
1963 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1964 ret = -EFAULT;
1965 kfree(args);
1966 return ret;
1967}
1968
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001969/*
Chris Masonac8e9812010-02-28 15:39:26 -05001970 * Search INODE_REFs to identify path name of 'dirid' directory
1971 * in a 'tree_id' tree. and sets path name to 'name'.
1972 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001973static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1974 u64 tree_id, u64 dirid, char *name)
1975{
1976 struct btrfs_root *root;
1977 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001978 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001979 int ret = -1;
1980 int slot;
1981 int len;
1982 int total_len = 0;
1983 struct btrfs_inode_ref *iref;
1984 struct extent_buffer *l;
1985 struct btrfs_path *path;
1986
1987 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1988 name[0]='\0';
1989 return 0;
1990 }
1991
1992 path = btrfs_alloc_path();
1993 if (!path)
1994 return -ENOMEM;
1995
Chris Masonac8e9812010-02-28 15:39:26 -05001996 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001997
1998 key.objectid = tree_id;
1999 key.type = BTRFS_ROOT_ITEM_KEY;
2000 key.offset = (u64)-1;
2001 root = btrfs_read_fs_root_no_name(info, &key);
2002 if (IS_ERR(root)) {
2003 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002004 ret = -ENOENT;
2005 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002006 }
2007
2008 key.objectid = dirid;
2009 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002010 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002011
2012 while(1) {
2013 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2014 if (ret < 0)
2015 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002016 else if (ret > 0) {
2017 ret = btrfs_previous_item(root, path, dirid,
2018 BTRFS_INODE_REF_KEY);
2019 if (ret < 0)
2020 goto out;
2021 else if (ret > 0) {
2022 ret = -ENOENT;
2023 goto out;
2024 }
2025 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002026
2027 l = path->nodes[0];
2028 slot = path->slots[0];
2029 btrfs_item_key_to_cpu(l, &key, slot);
2030
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002031 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2032 len = btrfs_inode_ref_name_len(l, iref);
2033 ptr -= len + 1;
2034 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002035 if (ptr < name) {
2036 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002037 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002038 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002039
2040 *(ptr + len) = '/';
2041 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
2042
2043 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2044 break;
2045
David Sterbab3b4aa72011-04-21 01:20:15 +02002046 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002047 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002048 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002049 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002050 }
Li Zefan77906a502011-07-14 03:16:00 +00002051 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002052 name[total_len]='\0';
2053 ret = 0;
2054out:
2055 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002056 return ret;
2057}
2058
2059static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2060 void __user *argp)
2061{
2062 struct btrfs_ioctl_ino_lookup_args *args;
2063 struct inode *inode;
2064 int ret;
2065
2066 if (!capable(CAP_SYS_ADMIN))
2067 return -EPERM;
2068
Julia Lawall2354d08f2010-10-29 15:14:18 -04002069 args = memdup_user(argp, sizeof(*args));
2070 if (IS_ERR(args))
2071 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002072
Al Viro496ad9a2013-01-23 17:07:38 -05002073 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002074
Chris Mason1b53ac42010-03-18 12:17:05 -04002075 if (args->treeid == 0)
2076 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2077
Chris Masonac8e9812010-02-28 15:39:26 -05002078 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2079 args->treeid, args->objectid,
2080 args->name);
2081
2082 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2083 ret = -EFAULT;
2084
2085 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002086 return ret;
2087}
2088
Yan, Zheng76dda932009-09-21 16:00:26 -04002089static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2090 void __user *arg)
2091{
2092 struct dentry *parent = fdentry(file);
2093 struct dentry *dentry;
2094 struct inode *dir = parent->d_inode;
2095 struct inode *inode;
2096 struct btrfs_root *root = BTRFS_I(dir)->root;
2097 struct btrfs_root *dest = NULL;
2098 struct btrfs_ioctl_vol_args *vol_args;
2099 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002100 struct btrfs_block_rsv block_rsv;
2101 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002102 int namelen;
2103 int ret;
2104 int err = 0;
2105
Yan, Zheng76dda932009-09-21 16:00:26 -04002106 vol_args = memdup_user(arg, sizeof(*vol_args));
2107 if (IS_ERR(vol_args))
2108 return PTR_ERR(vol_args);
2109
2110 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2111 namelen = strlen(vol_args->name);
2112 if (strchr(vol_args->name, '/') ||
2113 strncmp(vol_args->name, "..", namelen) == 0) {
2114 err = -EINVAL;
2115 goto out;
2116 }
2117
Al Viroa561be72011-11-23 11:57:51 -05002118 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002119 if (err)
2120 goto out;
2121
David Sterba5c50c9b2013-03-22 18:12:51 +00002122 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2123 if (err == -EINTR)
2124 goto out;
Yan, Zheng76dda932009-09-21 16:00:26 -04002125 dentry = lookup_one_len(vol_args->name, parent, namelen);
2126 if (IS_ERR(dentry)) {
2127 err = PTR_ERR(dentry);
2128 goto out_unlock_dir;
2129 }
2130
2131 if (!dentry->d_inode) {
2132 err = -ENOENT;
2133 goto out_dput;
2134 }
2135
2136 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002137 dest = BTRFS_I(inode)->root;
2138 if (!capable(CAP_SYS_ADMIN)){
2139 /*
2140 * Regular user. Only allow this with a special mount
2141 * option, when the user has write+exec access to the
2142 * subvol root, and when rmdir(2) would have been
2143 * allowed.
2144 *
2145 * Note that this is _not_ check that the subvol is
2146 * empty or doesn't contain data that we wouldn't
2147 * otherwise be able to delete.
2148 *
2149 * Users who want to delete empty subvols should try
2150 * rmdir(2).
2151 */
2152 err = -EPERM;
2153 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2154 goto out_dput;
2155
2156 /*
2157 * Do not allow deletion if the parent dir is the same
2158 * as the dir to be deleted. That means the ioctl
2159 * must be called on the dentry referencing the root
2160 * of the subvol, not a random directory contained
2161 * within it.
2162 */
2163 err = -EINVAL;
2164 if (root == dest)
2165 goto out_dput;
2166
2167 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2168 if (err)
2169 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002170 }
2171
Miao Xie5c39da52012-10-22 11:39:53 +00002172 /* check if subvolume may be deleted by a user */
2173 err = btrfs_may_delete(dir, dentry, 1);
2174 if (err)
2175 goto out_dput;
2176
Li Zefan33345d012011-04-20 10:31:50 +08002177 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002178 err = -EINVAL;
2179 goto out_dput;
2180 }
2181
Yan, Zheng76dda932009-09-21 16:00:26 -04002182 mutex_lock(&inode->i_mutex);
2183 err = d_invalidate(dentry);
2184 if (err)
2185 goto out_unlock;
2186
2187 down_write(&root->fs_info->subvol_sem);
2188
2189 err = may_destroy_subvol(dest);
2190 if (err)
2191 goto out_up_write;
2192
Miao Xiec58aaad2013-02-28 10:05:36 +00002193 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2194 /*
2195 * One for dir inode, two for dir entries, two for root
2196 * ref/backref.
2197 */
2198 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002199 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002200 if (err)
2201 goto out_up_write;
2202
Yan, Zhenga22285a2010-05-16 10:48:46 -04002203 trans = btrfs_start_transaction(root, 0);
2204 if (IS_ERR(trans)) {
2205 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002206 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002207 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002208 trans->block_rsv = &block_rsv;
2209 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002210
Yan, Zheng76dda932009-09-21 16:00:26 -04002211 ret = btrfs_unlink_subvol(trans, root, dir,
2212 dest->root_key.objectid,
2213 dentry->d_name.name,
2214 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002215 if (ret) {
2216 err = ret;
2217 btrfs_abort_transaction(trans, root, ret);
2218 goto out_end_trans;
2219 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002220
2221 btrfs_record_root_in_trans(trans, dest);
2222
2223 memset(&dest->root_item.drop_progress, 0,
2224 sizeof(dest->root_item.drop_progress));
2225 dest->root_item.drop_level = 0;
2226 btrfs_set_root_refs(&dest->root_item, 0);
2227
Yan, Zhengd68fc572010-05-16 10:49:58 -04002228 if (!xchg(&dest->orphan_item_inserted, 1)) {
2229 ret = btrfs_insert_orphan_item(trans,
2230 root->fs_info->tree_root,
2231 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002232 if (ret) {
2233 btrfs_abort_transaction(trans, root, ret);
2234 err = ret;
2235 goto out_end_trans;
2236 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002237 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002238out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002239 trans->block_rsv = NULL;
2240 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002241 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002242 if (ret && !err)
2243 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002244 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002245out_release:
2246 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002247out_up_write:
2248 up_write(&root->fs_info->subvol_sem);
2249out_unlock:
2250 mutex_unlock(&inode->i_mutex);
2251 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002252 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002253 btrfs_invalidate_inodes(dest);
2254 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002255
2256 /* the last ref */
2257 if (dest->cache_inode) {
2258 iput(dest->cache_inode);
2259 dest->cache_inode = NULL;
2260 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002261 }
2262out_dput:
2263 dput(dentry);
2264out_unlock_dir:
2265 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002266 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002267out:
2268 kfree(vol_args);
2269 return err;
2270}
2271
Chris Mason1e701a32010-03-11 09:42:04 -05002272static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002273{
Al Viro496ad9a2013-01-23 17:07:38 -05002274 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002275 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002276 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002277 int ret;
2278
Ilya Dryomov25122d12013-01-20 15:57:57 +02002279 ret = mnt_want_write_file(file);
2280 if (ret)
2281 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002282
Ilya Dryomov25122d12013-01-20 15:57:57 +02002283 if (btrfs_root_readonly(root)) {
2284 ret = -EROFS;
2285 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002286 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002287
2288 switch (inode->i_mode & S_IFMT) {
2289 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002290 if (!capable(CAP_SYS_ADMIN)) {
2291 ret = -EPERM;
2292 goto out;
2293 }
Eric Sandeende78b512013-01-31 18:21:12 +00002294 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002295 if (ret)
2296 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002297 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002298 break;
2299 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002300 if (!(file->f_mode & FMODE_WRITE)) {
2301 ret = -EINVAL;
2302 goto out;
2303 }
Chris Mason1e701a32010-03-11 09:42:04 -05002304
2305 range = kzalloc(sizeof(*range), GFP_KERNEL);
2306 if (!range) {
2307 ret = -ENOMEM;
2308 goto out;
2309 }
2310
2311 if (argp) {
2312 if (copy_from_user(range, argp,
2313 sizeof(*range))) {
2314 ret = -EFAULT;
2315 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002316 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002317 }
2318 /* compression requires us to start the IO */
2319 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2320 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2321 range->extent_thresh = (u32)-1;
2322 }
2323 } else {
2324 /* the rest are all set to zero by kzalloc */
2325 range->len = (u64)-1;
2326 }
Al Viro496ad9a2013-01-23 17:07:38 -05002327 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002328 range, 0, 0);
2329 if (ret > 0)
2330 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002331 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002332 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002333 default:
2334 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002335 }
Chris Masone441d542009-01-05 16:57:23 -05002336out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002337 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002338 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002339}
2340
Christoph Hellwigb2950862008-12-02 09:54:17 -05002341static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002342{
2343 struct btrfs_ioctl_vol_args *vol_args;
2344 int ret;
2345
Chris Masone441d542009-01-05 16:57:23 -05002346 if (!capable(CAP_SYS_ADMIN))
2347 return -EPERM;
2348
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002349 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2350 1)) {
2351 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
Ilya Dryomov2c0c9da2013-01-20 15:57:57 +02002352 return -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002353 }
2354
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002355 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002356 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002357 if (IS_ERR(vol_args)) {
2358 ret = PTR_ERR(vol_args);
2359 goto out;
2360 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002361
Mark Fasheh5516e592008-07-24 12:20:14 -04002362 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002363 ret = btrfs_init_new_device(root, vol_args->name);
2364
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002365 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002366out:
2367 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002368 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002369 return ret;
2370}
2371
Miao Xieda249272012-11-26 08:44:50 +00002372static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002373{
Al Viro496ad9a2013-01-23 17:07:38 -05002374 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002375 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
Miao Xieda249272012-11-26 08:44:50 +00002381 ret = mnt_want_write_file(file);
2382 if (ret)
2383 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002384
Li Zefandae7b662009-04-08 15:06:54 +08002385 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002386 if (IS_ERR(vol_args)) {
2387 ret = PTR_ERR(vol_args);
2388 goto out;
2389 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002390
Mark Fasheh5516e592008-07-24 12:20:14 -04002391 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002392
Anand Jain183860f2013-05-17 10:52:45 +00002393 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2394 1)) {
2395 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2396 goto out;
2397 }
2398
2399 mutex_lock(&root->fs_info->volume_mutex);
2400 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002401 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002402 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002403
2404out:
2405 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002406 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407 return ret;
2408}
2409
Jan Schmidt475f6382011-03-11 15:41:01 +01002410static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2411{
Li Zefan027ed2f2011-06-08 08:27:56 +00002412 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002413 struct btrfs_device *device;
2414 struct btrfs_device *next;
2415 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002416 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002417
2418 if (!capable(CAP_SYS_ADMIN))
2419 return -EPERM;
2420
Li Zefan027ed2f2011-06-08 08:27:56 +00002421 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2422 if (!fi_args)
2423 return -ENOMEM;
2424
2425 fi_args->num_devices = fs_devices->num_devices;
2426 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002427
2428 mutex_lock(&fs_devices->device_list_mutex);
2429 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002430 if (device->devid > fi_args->max_id)
2431 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002432 }
2433 mutex_unlock(&fs_devices->device_list_mutex);
2434
Li Zefan027ed2f2011-06-08 08:27:56 +00002435 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2436 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002437
Li Zefan027ed2f2011-06-08 08:27:56 +00002438 kfree(fi_args);
2439 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002440}
2441
2442static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2443{
2444 struct btrfs_ioctl_dev_info_args *di_args;
2445 struct btrfs_device *dev;
2446 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2447 int ret = 0;
2448 char *s_uuid = NULL;
2449 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2450
2451 if (!capable(CAP_SYS_ADMIN))
2452 return -EPERM;
2453
2454 di_args = memdup_user(arg, sizeof(*di_args));
2455 if (IS_ERR(di_args))
2456 return PTR_ERR(di_args);
2457
2458 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2459 s_uuid = di_args->uuid;
2460
2461 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002462 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002463
2464 if (!dev) {
2465 ret = -ENODEV;
2466 goto out;
2467 }
2468
2469 di_args->devid = dev->devid;
2470 di_args->bytes_used = dev->bytes_used;
2471 di_args->total_bytes = dev->total_bytes;
2472 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002473 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002474 struct rcu_string *name;
2475
2476 rcu_read_lock();
2477 name = rcu_dereference(dev->name);
2478 strncpy(di_args->path, name->str, sizeof(di_args->path));
2479 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002480 di_args->path[sizeof(di_args->path) - 1] = 0;
2481 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002482 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002483 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002484
2485out:
David Sterba55793c02013-04-26 15:20:23 +00002486 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002487 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2488 ret = -EFAULT;
2489
2490 kfree(di_args);
2491 return ret;
2492}
2493
Mark Fasheh416161d2013-08-06 11:42:51 -07002494static struct page *extent_same_get_page(struct inode *inode, u64 off)
2495{
2496 struct page *page;
2497 pgoff_t index;
2498 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2499
2500 index = off >> PAGE_CACHE_SHIFT;
2501
2502 page = grab_cache_page(inode->i_mapping, index);
2503 if (!page)
2504 return NULL;
2505
2506 if (!PageUptodate(page)) {
2507 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2508 0))
2509 return NULL;
2510 lock_page(page);
2511 if (!PageUptodate(page)) {
2512 unlock_page(page);
2513 page_cache_release(page);
2514 return NULL;
2515 }
2516 }
2517 unlock_page(page);
2518
2519 return page;
2520}
2521
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002522static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2523{
2524 /* do any pending delalloc/csum calc on src, one way or
2525 another, and lock file content */
2526 while (1) {
2527 struct btrfs_ordered_extent *ordered;
2528 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2529 ordered = btrfs_lookup_first_ordered_extent(inode,
2530 off + len - 1);
2531 if (!ordered &&
2532 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2533 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2534 break;
2535 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2536 if (ordered)
2537 btrfs_put_ordered_extent(ordered);
2538 btrfs_wait_ordered_range(inode, off, len);
2539 }
2540}
2541
Mark Fasheh416161d2013-08-06 11:42:51 -07002542static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2543 struct inode *inode2, u64 loff2, u64 len)
2544{
2545 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2546 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2547
2548 mutex_unlock(&inode1->i_mutex);
2549 mutex_unlock(&inode2->i_mutex);
2550}
2551
2552static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2553 struct inode *inode2, u64 loff2, u64 len)
2554{
2555 if (inode1 < inode2) {
2556 swap(inode1, inode2);
2557 swap(loff1, loff2);
2558 }
2559
2560 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2561 lock_extent_range(inode1, loff1, len);
2562 if (inode1 != inode2) {
2563 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2564 lock_extent_range(inode2, loff2, len);
2565 }
2566}
2567
2568static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2569 u64 dst_loff, u64 len)
2570{
2571 int ret = 0;
2572 struct page *src_page, *dst_page;
2573 unsigned int cmp_len = PAGE_CACHE_SIZE;
2574 void *addr, *dst_addr;
2575
2576 while (len) {
2577 if (len < PAGE_CACHE_SIZE)
2578 cmp_len = len;
2579
2580 src_page = extent_same_get_page(src, loff);
2581 if (!src_page)
2582 return -EINVAL;
2583 dst_page = extent_same_get_page(dst, dst_loff);
2584 if (!dst_page) {
2585 page_cache_release(src_page);
2586 return -EINVAL;
2587 }
2588 addr = kmap_atomic(src_page);
2589 dst_addr = kmap_atomic(dst_page);
2590
2591 flush_dcache_page(src_page);
2592 flush_dcache_page(dst_page);
2593
2594 if (memcmp(addr, dst_addr, cmp_len))
2595 ret = BTRFS_SAME_DATA_DIFFERS;
2596
2597 kunmap_atomic(addr);
2598 kunmap_atomic(dst_addr);
2599 page_cache_release(src_page);
2600 page_cache_release(dst_page);
2601
2602 if (ret)
2603 break;
2604
2605 loff += cmp_len;
2606 dst_loff += cmp_len;
2607 len -= cmp_len;
2608 }
2609
2610 return ret;
2611}
2612
2613static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2614{
2615 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2616
2617 if (off + len > inode->i_size || off + len < off)
2618 return -EINVAL;
2619 /* Check that we are block aligned - btrfs_clone() requires this */
2620 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2621 return -EINVAL;
2622
2623 return 0;
2624}
2625
2626static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2627 struct inode *dst, u64 dst_loff)
2628{
2629 int ret;
2630
2631 /*
2632 * btrfs_clone() can't handle extents in the same file
2633 * yet. Once that works, we can drop this check and replace it
2634 * with a check for the same inode, but overlapping extents.
2635 */
2636 if (src == dst)
2637 return -EINVAL;
2638
2639 btrfs_double_lock(src, loff, dst, dst_loff, len);
2640
2641 ret = extent_same_check_offsets(src, loff, len);
2642 if (ret)
2643 goto out_unlock;
2644
2645 ret = extent_same_check_offsets(dst, dst_loff, len);
2646 if (ret)
2647 goto out_unlock;
2648
2649 /* don't make the dst file partly checksummed */
2650 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2651 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2652 ret = -EINVAL;
2653 goto out_unlock;
2654 }
2655
2656 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2657 if (ret == 0)
2658 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2659
2660out_unlock:
2661 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2662
2663 return ret;
2664}
2665
2666#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2667
2668static long btrfs_ioctl_file_extent_same(struct file *file,
2669 void __user *argp)
2670{
2671 struct btrfs_ioctl_same_args *args = argp;
2672 struct btrfs_ioctl_same_args same;
2673 struct btrfs_ioctl_same_extent_info info;
2674 struct inode *src = file->f_dentry->d_inode;
2675 struct file *dst_file = NULL;
2676 struct inode *dst;
2677 u64 off;
2678 u64 len;
2679 int i;
2680 int ret;
2681 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2682 bool is_admin = capable(CAP_SYS_ADMIN);
2683
2684 if (!(file->f_mode & FMODE_READ))
2685 return -EINVAL;
2686
2687 ret = mnt_want_write_file(file);
2688 if (ret)
2689 return ret;
2690
2691 if (copy_from_user(&same,
2692 (struct btrfs_ioctl_same_args __user *)argp,
2693 sizeof(same))) {
2694 ret = -EFAULT;
2695 goto out;
2696 }
2697
2698 off = same.logical_offset;
2699 len = same.length;
2700
2701 /*
2702 * Limit the total length we will dedupe for each operation.
2703 * This is intended to bound the total time spent in this
2704 * ioctl to something sane.
2705 */
2706 if (len > BTRFS_MAX_DEDUPE_LEN)
2707 len = BTRFS_MAX_DEDUPE_LEN;
2708
2709 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2710 /*
2711 * Btrfs does not support blocksize < page_size. As a
2712 * result, btrfs_cmp_data() won't correctly handle
2713 * this situation without an update.
2714 */
2715 ret = -EINVAL;
2716 goto out;
2717 }
2718
2719 ret = -EISDIR;
2720 if (S_ISDIR(src->i_mode))
2721 goto out;
2722
2723 ret = -EACCES;
2724 if (!S_ISREG(src->i_mode))
2725 goto out;
2726
2727 ret = 0;
2728 for (i = 0; i < same.dest_count; i++) {
2729 if (copy_from_user(&info, &args->info[i], sizeof(info))) {
2730 ret = -EFAULT;
2731 goto out;
2732 }
2733
2734 info.bytes_deduped = 0;
2735
2736 dst_file = fget(info.fd);
2737 if (!dst_file) {
2738 info.status = -EBADF;
2739 goto next;
2740 }
2741
2742 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
2743 info.status = -EINVAL;
2744 goto next;
2745 }
2746
2747 info.status = -EXDEV;
2748 if (file->f_path.mnt != dst_file->f_path.mnt)
2749 goto next;
2750
2751 dst = dst_file->f_dentry->d_inode;
2752 if (src->i_sb != dst->i_sb)
2753 goto next;
2754
2755 if (S_ISDIR(dst->i_mode)) {
2756 info.status = -EISDIR;
2757 goto next;
2758 }
2759
2760 if (!S_ISREG(dst->i_mode)) {
2761 info.status = -EACCES;
2762 goto next;
2763 }
2764
2765 info.status = btrfs_extent_same(src, off, len, dst,
2766 info.logical_offset);
2767 if (info.status == 0)
2768 info.bytes_deduped += len;
2769
2770next:
2771 if (dst_file)
2772 fput(dst_file);
2773
2774 if (__put_user_unaligned(info.status, &args->info[i].status) ||
2775 __put_user_unaligned(info.bytes_deduped,
2776 &args->info[i].bytes_deduped)) {
2777 ret = -EFAULT;
2778 goto out;
2779 }
2780 }
2781
2782out:
2783 mnt_drop_write_file(file);
2784 return ret;
2785}
2786
Mark Fasheh32b7c682013-08-06 11:42:49 -07002787/**
2788 * btrfs_clone() - clone a range from inode file to another
2789 *
2790 * @src: Inode to clone from
2791 * @inode: Inode to clone to
2792 * @off: Offset within source to start clone from
2793 * @olen: Original length, passed by user, of range to clone
2794 * @olen_aligned: Block-aligned value of olen, extent_same uses
2795 * identical values here
2796 * @destoff: Offset within @inode to start clone
2797 */
2798static int btrfs_clone(struct inode *src, struct inode *inode,
2799 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002800{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002801 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002802 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002803 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002804 struct btrfs_trans_handle *trans;
2805 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002806 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002807 u32 nritems;
2808 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002809 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002810 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002811
2812 ret = -ENOMEM;
2813 buf = vmalloc(btrfs_level_size(root, 0));
2814 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002815 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002816
2817 path = btrfs_alloc_path();
2818 if (!path) {
2819 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002820 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002821 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002822
Yan Zhengae01a0a2008-08-04 23:23:47 -04002823 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002824 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002825 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002826 key.type = BTRFS_EXTENT_DATA_KEY;
2827 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002828
2829 while (1) {
2830 /*
2831 * note the key will change type as we walk through the
2832 * tree.
2833 */
David Sterba362a20c2011-08-01 18:11:57 +02002834 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2835 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002836 if (ret < 0)
2837 goto out;
2838
Yan Zhengae01a0a2008-08-04 23:23:47 -04002839 nritems = btrfs_header_nritems(path->nodes[0]);
2840 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002841 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002842 if (ret < 0)
2843 goto out;
2844 if (ret > 0)
2845 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002846 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002847 }
2848 leaf = path->nodes[0];
2849 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002850
Yan Zhengae01a0a2008-08-04 23:23:47 -04002851 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002852 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002853 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002854 break;
2855
Sage Weilc5c9cd42008-11-12 14:32:25 -05002856 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2857 struct btrfs_file_extent_item *extent;
2858 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002859 u32 size;
2860 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002861 u64 disko = 0, diskl = 0;
2862 u64 datao = 0, datal = 0;
2863 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002864 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002865
2866 size = btrfs_item_size_nr(leaf, slot);
2867 read_extent_buffer(leaf, buf,
2868 btrfs_item_ptr_offset(leaf, slot),
2869 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002870
2871 extent = btrfs_item_ptr(leaf, slot,
2872 struct btrfs_file_extent_item);
2873 comp = btrfs_file_extent_compression(leaf, extent);
2874 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002875 if (type == BTRFS_FILE_EXTENT_REG ||
2876 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002877 disko = btrfs_file_extent_disk_bytenr(leaf,
2878 extent);
2879 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2880 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002881 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002882 datal = btrfs_file_extent_num_bytes(leaf,
2883 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002884 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2885 /* take upper bound, may be compressed */
2886 datal = btrfs_file_extent_ram_bytes(leaf,
2887 extent);
2888 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002889 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002890
Sage Weil050006a2010-10-29 15:37:33 -04002891 if (key.offset + datal <= off ||
Liu Boaa42ffd2012-09-18 03:52:23 -06002892 key.offset >= off + len - 1)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002893 goto next;
2894
Zheng Yan31840ae2008-09-23 13:14:14 -04002895 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002896 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002897 if (off <= key.offset)
2898 new_key.offset = key.offset + destoff - off;
2899 else
2900 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002901
Sage Weilb6f34092011-09-20 14:48:51 -04002902 /*
2903 * 1 - adjusting old extent (we may have to split it)
2904 * 1 - add new extent
2905 * 1 - inode update
2906 */
2907 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002908 if (IS_ERR(trans)) {
2909 ret = PTR_ERR(trans);
2910 goto out;
2911 }
2912
Chris Masonc8a894d2009-06-27 21:07:03 -04002913 if (type == BTRFS_FILE_EXTENT_REG ||
2914 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002915 /*
2916 * a | --- range to clone ---| b
2917 * | ------------- extent ------------- |
2918 */
2919
2920 /* substract range b */
2921 if (key.offset + datal > off + len)
2922 datal = off + len - key.offset;
2923
2924 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002925 if (off > key.offset) {
2926 datao += off - key.offset;
2927 datal -= off - key.offset;
2928 }
2929
Josef Bacik5dc562c2012-08-17 13:14:17 -04002930 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04002931 new_key.offset,
2932 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04002933 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002934 if (ret) {
2935 btrfs_abort_transaction(trans, root,
2936 ret);
2937 btrfs_end_transaction(trans, root);
2938 goto out;
2939 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002940
Sage Weilc5c9cd42008-11-12 14:32:25 -05002941 ret = btrfs_insert_empty_item(trans, root, path,
2942 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002943 if (ret) {
2944 btrfs_abort_transaction(trans, root,
2945 ret);
2946 btrfs_end_transaction(trans, root);
2947 goto out;
2948 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002949
2950 leaf = path->nodes[0];
2951 slot = path->slots[0];
2952 write_extent_buffer(leaf, buf,
2953 btrfs_item_ptr_offset(leaf, slot),
2954 size);
2955
2956 extent = btrfs_item_ptr(leaf, slot,
2957 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002958
Sage Weilc5c9cd42008-11-12 14:32:25 -05002959 /* disko == 0 means it's a hole */
2960 if (!disko)
2961 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002962
2963 btrfs_set_file_extent_offset(leaf, extent,
2964 datao);
2965 btrfs_set_file_extent_num_bytes(leaf, extent,
2966 datal);
2967 if (disko) {
2968 inode_add_bytes(inode, datal);
2969 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002970 disko, diskl, 0,
2971 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002972 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002973 new_key.offset - datao,
2974 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975 if (ret) {
2976 btrfs_abort_transaction(trans,
2977 root,
2978 ret);
2979 btrfs_end_transaction(trans,
2980 root);
2981 goto out;
2982
2983 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002984 }
2985 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2986 u64 skip = 0;
2987 u64 trim = 0;
2988 if (off > key.offset) {
2989 skip = off - key.offset;
2990 new_key.offset += skip;
2991 }
Chris Masond3977122009-01-05 21:25:51 -05002992
Liu Boaa42ffd2012-09-18 03:52:23 -06002993 if (key.offset + datal > off + len)
2994 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05002995
Sage Weilc5c9cd42008-11-12 14:32:25 -05002996 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002997 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002998 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002999 goto out;
3000 }
3001 size -= skip + trim;
3002 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003
Josef Bacik5dc562c2012-08-17 13:14:17 -04003004 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003005 new_key.offset,
3006 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003007 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003008 if (ret) {
3009 btrfs_abort_transaction(trans, root,
3010 ret);
3011 btrfs_end_transaction(trans, root);
3012 goto out;
3013 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003014
Sage Weilc5c9cd42008-11-12 14:32:25 -05003015 ret = btrfs_insert_empty_item(trans, root, path,
3016 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 if (ret) {
3018 btrfs_abort_transaction(trans, root,
3019 ret);
3020 btrfs_end_transaction(trans, root);
3021 goto out;
3022 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003023
3024 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003025 u32 start =
3026 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003027 memmove(buf+start, buf+start+skip,
3028 datal);
3029 }
3030
3031 leaf = path->nodes[0];
3032 slot = path->slots[0];
3033 write_extent_buffer(leaf, buf,
3034 btrfs_item_ptr_offset(leaf, slot),
3035 size);
3036 inode_add_bytes(inode, datal);
3037 }
3038
3039 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003040 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003041
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003042 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003043 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003044
3045 /*
3046 * we round up to the block size at eof when
3047 * determining which extents to clone above,
3048 * but shouldn't round up the file size
3049 */
3050 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003051 if (endoff > destoff+olen)
3052 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003053 if (endoff > inode->i_size)
3054 btrfs_i_size_write(inode, endoff);
3055
Yan, Zhenga22285a2010-05-16 10:48:46 -04003056 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003057 if (ret) {
3058 btrfs_abort_transaction(trans, root, ret);
3059 btrfs_end_transaction(trans, root);
3060 goto out;
3061 }
3062 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003063 }
Chris Masond3977122009-01-05 21:25:51 -05003064next:
David Sterbab3b4aa72011-04-21 01:20:15 +02003065 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003066 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003067 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003068 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003069
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003070out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003071 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003072 btrfs_free_path(path);
3073 vfree(buf);
3074 return ret;
3075}
3076
3077static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3078 u64 off, u64 olen, u64 destoff)
3079{
3080 struct inode *inode = fdentry(file)->d_inode;
3081 struct btrfs_root *root = BTRFS_I(inode)->root;
3082 struct fd src_file;
3083 struct inode *src;
3084 int ret;
3085 u64 len = olen;
3086 u64 bs = root->fs_info->sb->s_blocksize;
3087 int same_inode = 0;
3088
3089 /*
3090 * TODO:
3091 * - split compressed inline extents. annoying: we need to
3092 * decompress into destination's address_space (the file offset
3093 * may change, so source mapping won't do), then recompress (or
3094 * otherwise reinsert) a subrange.
3095 * - allow ranges within the same file to be cloned (provided
3096 * they don't overlap)?
3097 */
3098
3099 /* the destination must be opened for writing */
3100 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3101 return -EINVAL;
3102
3103 if (btrfs_root_readonly(root))
3104 return -EROFS;
3105
3106 ret = mnt_want_write_file(file);
3107 if (ret)
3108 return ret;
3109
3110 src_file = fdget(srcfd);
3111 if (!src_file.file) {
3112 ret = -EBADF;
3113 goto out_drop_write;
3114 }
3115
3116 ret = -EXDEV;
3117 if (src_file.file->f_path.mnt != file->f_path.mnt)
3118 goto out_fput;
3119
3120 src = file_inode(src_file.file);
3121
3122 ret = -EINVAL;
3123 if (src == inode)
3124 same_inode = 1;
3125
3126 /* the src must be open for reading */
3127 if (!(src_file.file->f_mode & FMODE_READ))
3128 goto out_fput;
3129
3130 /* don't make the dst file partly checksummed */
3131 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3132 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3133 goto out_fput;
3134
3135 ret = -EISDIR;
3136 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3137 goto out_fput;
3138
3139 ret = -EXDEV;
3140 if (src->i_sb != inode->i_sb)
3141 goto out_fput;
3142
3143 if (!same_inode) {
3144 if (inode < src) {
3145 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3146 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3147 } else {
3148 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3149 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3150 }
3151 } else {
3152 mutex_lock(&src->i_mutex);
3153 }
3154
3155 /* determine range to clone */
3156 ret = -EINVAL;
3157 if (off + len > src->i_size || off + len < off)
3158 goto out_unlock;
3159 if (len == 0)
3160 olen = len = src->i_size - off;
3161 /* if we extend to eof, continue to block boundary */
3162 if (off + len == src->i_size)
3163 len = ALIGN(src->i_size, bs) - off;
3164
3165 /* verify the end result is block aligned */
3166 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3167 !IS_ALIGNED(destoff, bs))
3168 goto out_unlock;
3169
3170 /* verify if ranges are overlapped within the same file */
3171 if (same_inode) {
3172 if (destoff + len > off && destoff < off + len)
3173 goto out_unlock;
3174 }
3175
3176 if (destoff > inode->i_size) {
3177 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3178 if (ret)
3179 goto out_unlock;
3180 }
3181
3182 /* truncate page cache pages from target inode range */
3183 truncate_inode_pages_range(&inode->i_data, destoff,
3184 PAGE_CACHE_ALIGN(destoff + len) - 1);
3185
3186 lock_extent_range(src, off, len);
3187
3188 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3189
Liu Boaa42ffd2012-09-18 03:52:23 -06003190 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003191out_unlock:
3192 mutex_unlock(&src->i_mutex);
Liu Boa96fbc72013-05-26 13:50:31 +00003193 if (!same_inode)
3194 mutex_unlock(&inode->i_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003195out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003196 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003197out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003198 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003199 return ret;
3200}
3201
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003202static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003203{
3204 struct btrfs_ioctl_clone_range_args args;
3205
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003206 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003207 return -EFAULT;
3208 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3209 args.src_length, args.dest_offset);
3210}
3211
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003212/*
3213 * there are many ways the trans_start and trans_end ioctls can lead
3214 * to deadlocks. They should only be used by applications that
3215 * basically own the machine, and have a very in depth understanding
3216 * of all the possible deadlocks and enospc problems.
3217 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003218static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003219{
Al Viro496ad9a2013-01-23 17:07:38 -05003220 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003221 struct btrfs_root *root = BTRFS_I(inode)->root;
3222 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003223 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003224
Sage Weil1ab86ae2009-09-29 18:38:44 -04003225 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003226 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003227 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003228
3229 ret = -EINPROGRESS;
3230 if (file->private_data)
3231 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003232
Li Zefanb83cc962010-12-20 16:04:08 +08003233 ret = -EROFS;
3234 if (btrfs_root_readonly(root))
3235 goto out;
3236
Al Viroa561be72011-11-23 11:57:51 -05003237 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003238 if (ret)
3239 goto out;
3240
Josef Bacika4abeea2011-04-11 17:25:13 -04003241 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003242
Sage Weil1ab86ae2009-09-29 18:38:44 -04003243 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003244 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003245 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003246 goto out_drop;
3247
3248 file->private_data = trans;
3249 return 0;
3250
3251out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003252 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003253 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003254out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003255 return ret;
3256}
3257
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003258static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3259{
Al Viro496ad9a2013-01-23 17:07:38 -05003260 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003261 struct btrfs_root *root = BTRFS_I(inode)->root;
3262 struct btrfs_root *new_root;
3263 struct btrfs_dir_item *di;
3264 struct btrfs_trans_handle *trans;
3265 struct btrfs_path *path;
3266 struct btrfs_key location;
3267 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003268 u64 objectid = 0;
3269 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003270 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003271
3272 if (!capable(CAP_SYS_ADMIN))
3273 return -EPERM;
3274
Miao Xie3c04ce02012-11-26 08:43:07 +00003275 ret = mnt_want_write_file(file);
3276 if (ret)
3277 return ret;
3278
3279 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3280 ret = -EFAULT;
3281 goto out;
3282 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003283
3284 if (!objectid)
3285 objectid = root->root_key.objectid;
3286
3287 location.objectid = objectid;
3288 location.type = BTRFS_ROOT_ITEM_KEY;
3289 location.offset = (u64)-1;
3290
3291 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003292 if (IS_ERR(new_root)) {
3293 ret = PTR_ERR(new_root);
3294 goto out;
3295 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003296
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003297 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003298 if (!path) {
3299 ret = -ENOMEM;
3300 goto out;
3301 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003302 path->leave_spinning = 1;
3303
3304 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003305 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003306 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003307 ret = PTR_ERR(trans);
3308 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003309 }
3310
David Sterba6c417612011-04-13 15:41:04 +02003311 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003312 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3313 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003314 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003315 btrfs_free_path(path);
3316 btrfs_end_transaction(trans, root);
3317 printk(KERN_ERR "Umm, you don't have the default dir item, "
3318 "this isn't going to work\n");
Miao Xie3c04ce02012-11-26 08:43:07 +00003319 ret = -ENOENT;
3320 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003321 }
3322
3323 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3324 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3325 btrfs_mark_buffer_dirty(path->nodes[0]);
3326 btrfs_free_path(path);
3327
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003328 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003329 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003330out:
3331 mnt_drop_write_file(file);
3332 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003333}
3334
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003335void btrfs_get_block_group_info(struct list_head *groups_list,
3336 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003337{
3338 struct btrfs_block_group_cache *block_group;
3339
3340 space->total_bytes = 0;
3341 space->used_bytes = 0;
3342 space->flags = 0;
3343 list_for_each_entry(block_group, groups_list, list) {
3344 space->flags = block_group->flags;
3345 space->total_bytes += block_group->key.offset;
3346 space->used_bytes +=
3347 btrfs_block_group_used(&block_group->item);
3348 }
3349}
3350
Eric Sandeen48a3b632013-04-25 20:41:01 +00003351static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003352{
3353 struct btrfs_ioctl_space_args space_args;
3354 struct btrfs_ioctl_space_info space;
3355 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003356 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003357 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003358 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003359 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3360 BTRFS_BLOCK_GROUP_SYSTEM,
3361 BTRFS_BLOCK_GROUP_METADATA,
3362 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3363 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003364 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003365 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003366 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003367 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003368
3369 if (copy_from_user(&space_args,
3370 (struct btrfs_ioctl_space_args __user *)arg,
3371 sizeof(space_args)))
3372 return -EFAULT;
3373
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003374 for (i = 0; i < num_types; i++) {
3375 struct btrfs_space_info *tmp;
3376
3377 info = NULL;
3378 rcu_read_lock();
3379 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3380 list) {
3381 if (tmp->flags == types[i]) {
3382 info = tmp;
3383 break;
3384 }
3385 }
3386 rcu_read_unlock();
3387
3388 if (!info)
3389 continue;
3390
3391 down_read(&info->groups_sem);
3392 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3393 if (!list_empty(&info->block_groups[c]))
3394 slot_count++;
3395 }
3396 up_read(&info->groups_sem);
3397 }
Josef Bacik1406e432010-01-13 18:19:06 +00003398
Chris Mason7fde62b2010-03-16 15:40:10 -04003399 /* space_slots == 0 means they are asking for a count */
3400 if (space_args.space_slots == 0) {
3401 space_args.total_spaces = slot_count;
3402 goto out;
3403 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003404
Dan Rosenberg51788b12011-02-14 16:04:23 -05003405 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003406
Chris Mason7fde62b2010-03-16 15:40:10 -04003407 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003408
Chris Mason7fde62b2010-03-16 15:40:10 -04003409 /* we generally have at most 6 or so space infos, one for each raid
3410 * level. So, a whole page should be more than enough for everyone
3411 */
3412 if (alloc_size > PAGE_CACHE_SIZE)
3413 return -ENOMEM;
3414
3415 space_args.total_spaces = 0;
3416 dest = kmalloc(alloc_size, GFP_NOFS);
3417 if (!dest)
3418 return -ENOMEM;
3419 dest_orig = dest;
3420
3421 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003422 for (i = 0; i < num_types; i++) {
3423 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003424
Dan Rosenberg51788b12011-02-14 16:04:23 -05003425 if (!slot_count)
3426 break;
3427
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003428 info = NULL;
3429 rcu_read_lock();
3430 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3431 list) {
3432 if (tmp->flags == types[i]) {
3433 info = tmp;
3434 break;
3435 }
3436 }
3437 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003438
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003439 if (!info)
3440 continue;
3441 down_read(&info->groups_sem);
3442 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3443 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003444 btrfs_get_block_group_info(
3445 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003446 memcpy(dest, &space, sizeof(space));
3447 dest++;
3448 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003449 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003450 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003451 if (!slot_count)
3452 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003453 }
3454 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003455 }
Josef Bacik1406e432010-01-13 18:19:06 +00003456
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003457 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003458 (arg + sizeof(struct btrfs_ioctl_space_args));
3459
3460 if (copy_to_user(user_dest, dest_orig, alloc_size))
3461 ret = -EFAULT;
3462
3463 kfree(dest_orig);
3464out:
3465 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003466 ret = -EFAULT;
3467
3468 return ret;
3469}
3470
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003471/*
3472 * there are many ways the trans_start and trans_end ioctls can lead
3473 * to deadlocks. They should only be used by applications that
3474 * basically own the machine, and have a very in depth understanding
3475 * of all the possible deadlocks and enospc problems.
3476 */
3477long btrfs_ioctl_trans_end(struct file *file)
3478{
Al Viro496ad9a2013-01-23 17:07:38 -05003479 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003480 struct btrfs_root *root = BTRFS_I(inode)->root;
3481 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003482
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003483 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003484 if (!trans)
3485 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003486 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003487
Sage Weil1ab86ae2009-09-29 18:38:44 -04003488 btrfs_end_transaction(trans, root);
3489
Josef Bacika4abeea2011-04-11 17:25:13 -04003490 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003491
Al Viro2a79f172011-12-09 08:06:57 -05003492 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003493 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003494}
3495
Miao Xie9a8c28b2012-11-26 08:40:43 +00003496static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3497 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003498{
Sage Weil46204592010-10-29 15:41:32 -04003499 struct btrfs_trans_handle *trans;
3500 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003501 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003502
Miao Xied4edf392013-02-20 09:17:06 +00003503 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003504 if (IS_ERR(trans)) {
3505 if (PTR_ERR(trans) != -ENOENT)
3506 return PTR_ERR(trans);
3507
3508 /* No running transaction, don't bother */
3509 transid = root->fs_info->last_trans_committed;
3510 goto out;
3511 }
Sage Weil46204592010-10-29 15:41:32 -04003512 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003513 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003514 if (ret) {
3515 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003516 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003517 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003518out:
Sage Weil46204592010-10-29 15:41:32 -04003519 if (argp)
3520 if (copy_to_user(argp, &transid, sizeof(transid)))
3521 return -EFAULT;
3522 return 0;
3523}
3524
Miao Xie9a8c28b2012-11-26 08:40:43 +00003525static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3526 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003527{
Sage Weil46204592010-10-29 15:41:32 -04003528 u64 transid;
3529
3530 if (argp) {
3531 if (copy_from_user(&transid, argp, sizeof(transid)))
3532 return -EFAULT;
3533 } else {
3534 transid = 0; /* current trans */
3535 }
3536 return btrfs_wait_for_commit(root, transid);
3537}
3538
Miao Xieb8e95482012-11-26 08:48:01 +00003539static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003540{
Al Viro496ad9a2013-01-23 17:07:38 -05003541 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003542 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003543 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003544
3545 if (!capable(CAP_SYS_ADMIN))
3546 return -EPERM;
3547
3548 sa = memdup_user(arg, sizeof(*sa));
3549 if (IS_ERR(sa))
3550 return PTR_ERR(sa);
3551
Miao Xieb8e95482012-11-26 08:48:01 +00003552 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3553 ret = mnt_want_write_file(file);
3554 if (ret)
3555 goto out;
3556 }
3557
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003558 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003559 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3560 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003561
3562 if (copy_to_user(arg, sa, sizeof(*sa)))
3563 ret = -EFAULT;
3564
Miao Xieb8e95482012-11-26 08:48:01 +00003565 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3566 mnt_drop_write_file(file);
3567out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003568 kfree(sa);
3569 return ret;
3570}
3571
3572static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3573{
3574 if (!capable(CAP_SYS_ADMIN))
3575 return -EPERM;
3576
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003577 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003578}
3579
3580static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3581 void __user *arg)
3582{
3583 struct btrfs_ioctl_scrub_args *sa;
3584 int ret;
3585
3586 if (!capable(CAP_SYS_ADMIN))
3587 return -EPERM;
3588
3589 sa = memdup_user(arg, sizeof(*sa));
3590 if (IS_ERR(sa))
3591 return PTR_ERR(sa);
3592
3593 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3594
3595 if (copy_to_user(arg, sa, sizeof(*sa)))
3596 ret = -EFAULT;
3597
3598 kfree(sa);
3599 return ret;
3600}
3601
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003602static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003603 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003604{
3605 struct btrfs_ioctl_get_dev_stats *sa;
3606 int ret;
3607
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003608 sa = memdup_user(arg, sizeof(*sa));
3609 if (IS_ERR(sa))
3610 return PTR_ERR(sa);
3611
David Sterbab27f7c02012-06-22 06:30:39 -06003612 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3613 kfree(sa);
3614 return -EPERM;
3615 }
3616
3617 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003618
3619 if (copy_to_user(arg, sa, sizeof(*sa)))
3620 ret = -EFAULT;
3621
3622 kfree(sa);
3623 return ret;
3624}
3625
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003626static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3627{
3628 struct btrfs_ioctl_dev_replace_args *p;
3629 int ret;
3630
3631 if (!capable(CAP_SYS_ADMIN))
3632 return -EPERM;
3633
3634 p = memdup_user(arg, sizeof(*p));
3635 if (IS_ERR(p))
3636 return PTR_ERR(p);
3637
3638 switch (p->cmd) {
3639 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
3640 if (atomic_xchg(
3641 &root->fs_info->mutually_exclusive_operation_running,
3642 1)) {
3643 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3644 ret = -EINPROGRESS;
3645 } else {
3646 ret = btrfs_dev_replace_start(root, p);
3647 atomic_set(
3648 &root->fs_info->mutually_exclusive_operation_running,
3649 0);
3650 }
3651 break;
3652 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3653 btrfs_dev_replace_status(root->fs_info, p);
3654 ret = 0;
3655 break;
3656 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3657 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3658 break;
3659 default:
3660 ret = -EINVAL;
3661 break;
3662 }
3663
3664 if (copy_to_user(arg, p, sizeof(*p)))
3665 ret = -EFAULT;
3666
3667 kfree(p);
3668 return ret;
3669}
3670
Jan Schmidtd7728c92011-07-07 16:48:38 +02003671static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3672{
3673 int ret = 0;
3674 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003675 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003676 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003677 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003678 struct inode_fs_paths *ipath = NULL;
3679 struct btrfs_path *path;
3680
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003681 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003682 return -EPERM;
3683
3684 path = btrfs_alloc_path();
3685 if (!path) {
3686 ret = -ENOMEM;
3687 goto out;
3688 }
3689
3690 ipa = memdup_user(arg, sizeof(*ipa));
3691 if (IS_ERR(ipa)) {
3692 ret = PTR_ERR(ipa);
3693 ipa = NULL;
3694 goto out;
3695 }
3696
3697 size = min_t(u32, ipa->size, 4096);
3698 ipath = init_ipath(size, root, path);
3699 if (IS_ERR(ipath)) {
3700 ret = PTR_ERR(ipath);
3701 ipath = NULL;
3702 goto out;
3703 }
3704
3705 ret = paths_from_inode(ipa->inum, ipath);
3706 if (ret < 0)
3707 goto out;
3708
3709 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003710 rel_ptr = ipath->fspath->val[i] -
3711 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003712 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003713 }
3714
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003715 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3716 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003717 if (ret) {
3718 ret = -EFAULT;
3719 goto out;
3720 }
3721
3722out:
3723 btrfs_free_path(path);
3724 free_ipath(ipath);
3725 kfree(ipa);
3726
3727 return ret;
3728}
3729
3730static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3731{
3732 struct btrfs_data_container *inodes = ctx;
3733 const size_t c = 3 * sizeof(u64);
3734
3735 if (inodes->bytes_left >= c) {
3736 inodes->bytes_left -= c;
3737 inodes->val[inodes->elem_cnt] = inum;
3738 inodes->val[inodes->elem_cnt + 1] = offset;
3739 inodes->val[inodes->elem_cnt + 2] = root;
3740 inodes->elem_cnt += 3;
3741 } else {
3742 inodes->bytes_missing += c - inodes->bytes_left;
3743 inodes->bytes_left = 0;
3744 inodes->elem_missed += 3;
3745 }
3746
3747 return 0;
3748}
3749
3750static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3751 void __user *arg)
3752{
3753 int ret = 0;
3754 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003755 struct btrfs_ioctl_logical_ino_args *loi;
3756 struct btrfs_data_container *inodes = NULL;
3757 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003758
3759 if (!capable(CAP_SYS_ADMIN))
3760 return -EPERM;
3761
3762 loi = memdup_user(arg, sizeof(*loi));
3763 if (IS_ERR(loi)) {
3764 ret = PTR_ERR(loi);
3765 loi = NULL;
3766 goto out;
3767 }
3768
3769 path = btrfs_alloc_path();
3770 if (!path) {
3771 ret = -ENOMEM;
3772 goto out;
3773 }
3774
Liu Bo425d17a2012-09-07 20:01:30 -06003775 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003776 inodes = init_data_container(size);
3777 if (IS_ERR(inodes)) {
3778 ret = PTR_ERR(inodes);
3779 inodes = NULL;
3780 goto out;
3781 }
3782
Liu Bodf031f02012-09-07 20:01:29 -06003783 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3784 build_ino_list, inodes);
3785 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003786 ret = -ENOENT;
3787 if (ret < 0)
3788 goto out;
3789
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003790 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3791 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003792 if (ret)
3793 ret = -EFAULT;
3794
3795out:
3796 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003797 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003798 kfree(loi);
3799
3800 return ret;
3801}
3802
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003803void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003804 struct btrfs_ioctl_balance_args *bargs)
3805{
3806 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3807
3808 bargs->flags = bctl->flags;
3809
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003810 if (atomic_read(&fs_info->balance_running))
3811 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3812 if (atomic_read(&fs_info->balance_pause_req))
3813 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003814 if (atomic_read(&fs_info->balance_cancel_req))
3815 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003816
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003817 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3818 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3819 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003820
3821 if (lock) {
3822 spin_lock(&fs_info->balance_lock);
3823 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3824 spin_unlock(&fs_info->balance_lock);
3825 } else {
3826 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3827 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003828}
3829
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003830static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003831{
Al Viro496ad9a2013-01-23 17:07:38 -05003832 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003833 struct btrfs_fs_info *fs_info = root->fs_info;
3834 struct btrfs_ioctl_balance_args *bargs;
3835 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003836 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003837 int ret;
3838
3839 if (!capable(CAP_SYS_ADMIN))
3840 return -EPERM;
3841
Liu Boe54bfa312012-06-29 03:58:48 -06003842 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003843 if (ret)
3844 return ret;
3845
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003846again:
3847 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3848 mutex_lock(&fs_info->volume_mutex);
3849 mutex_lock(&fs_info->balance_mutex);
3850 need_unlock = true;
3851 goto locked;
3852 }
3853
3854 /*
3855 * mut. excl. ops lock is locked. Three possibilites:
3856 * (1) some other op is running
3857 * (2) balance is running
3858 * (3) balance is paused -- special case (think resume)
3859 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003860 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003861 if (fs_info->balance_ctl) {
3862 /* this is either (2) or (3) */
3863 if (!atomic_read(&fs_info->balance_running)) {
3864 mutex_unlock(&fs_info->balance_mutex);
3865 if (!mutex_trylock(&fs_info->volume_mutex))
3866 goto again;
3867 mutex_lock(&fs_info->balance_mutex);
3868
3869 if (fs_info->balance_ctl &&
3870 !atomic_read(&fs_info->balance_running)) {
3871 /* this is (3) */
3872 need_unlock = false;
3873 goto locked;
3874 }
3875
3876 mutex_unlock(&fs_info->balance_mutex);
3877 mutex_unlock(&fs_info->volume_mutex);
3878 goto again;
3879 } else {
3880 /* this is (2) */
3881 mutex_unlock(&fs_info->balance_mutex);
3882 ret = -EINPROGRESS;
3883 goto out;
3884 }
3885 } else {
3886 /* this is (1) */
3887 mutex_unlock(&fs_info->balance_mutex);
3888 pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n");
3889 ret = -EINVAL;
3890 goto out;
3891 }
3892
3893locked:
3894 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003895
3896 if (arg) {
3897 bargs = memdup_user(arg, sizeof(*bargs));
3898 if (IS_ERR(bargs)) {
3899 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003900 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003901 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003902
3903 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3904 if (!fs_info->balance_ctl) {
3905 ret = -ENOTCONN;
3906 goto out_bargs;
3907 }
3908
3909 bctl = fs_info->balance_ctl;
3910 spin_lock(&fs_info->balance_lock);
3911 bctl->flags |= BTRFS_BALANCE_RESUME;
3912 spin_unlock(&fs_info->balance_lock);
3913
3914 goto do_balance;
3915 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003916 } else {
3917 bargs = NULL;
3918 }
3919
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003920 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003921 ret = -EINPROGRESS;
3922 goto out_bargs;
3923 }
3924
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003925 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3926 if (!bctl) {
3927 ret = -ENOMEM;
3928 goto out_bargs;
3929 }
3930
3931 bctl->fs_info = fs_info;
3932 if (arg) {
3933 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3934 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3935 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3936
3937 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003938 } else {
3939 /* balance everything - no filters */
3940 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003941 }
3942
Ilya Dryomovde322262012-01-16 22:04:49 +02003943do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003944 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003945 * Ownership of bctl and mutually_exclusive_operation_running
3946 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3947 * or, if restriper was paused all the way until unmount, in
3948 * free_fs_info. mutually_exclusive_operation_running is
3949 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003950 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003951 need_unlock = false;
3952
3953 ret = btrfs_balance(bctl, bargs);
3954
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003955 if (arg) {
3956 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3957 ret = -EFAULT;
3958 }
3959
3960out_bargs:
3961 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003962out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003963 mutex_unlock(&fs_info->balance_mutex);
3964 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003965 if (need_unlock)
3966 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3967out:
Liu Boe54bfa312012-06-29 03:58:48 -06003968 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003969 return ret;
3970}
3971
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003972static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3973{
3974 if (!capable(CAP_SYS_ADMIN))
3975 return -EPERM;
3976
3977 switch (cmd) {
3978 case BTRFS_BALANCE_CTL_PAUSE:
3979 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003980 case BTRFS_BALANCE_CTL_CANCEL:
3981 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003982 }
3983
3984 return -EINVAL;
3985}
3986
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003987static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3988 void __user *arg)
3989{
3990 struct btrfs_fs_info *fs_info = root->fs_info;
3991 struct btrfs_ioctl_balance_args *bargs;
3992 int ret = 0;
3993
3994 if (!capable(CAP_SYS_ADMIN))
3995 return -EPERM;
3996
3997 mutex_lock(&fs_info->balance_mutex);
3998 if (!fs_info->balance_ctl) {
3999 ret = -ENOTCONN;
4000 goto out;
4001 }
4002
4003 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4004 if (!bargs) {
4005 ret = -ENOMEM;
4006 goto out;
4007 }
4008
4009 update_ioctl_balance_args(fs_info, 1, bargs);
4010
4011 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4012 ret = -EFAULT;
4013
4014 kfree(bargs);
4015out:
4016 mutex_unlock(&fs_info->balance_mutex);
4017 return ret;
4018}
4019
Miao Xie905b0dd2012-11-26 08:50:11 +00004020static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004021{
Al Viro496ad9a2013-01-23 17:07:38 -05004022 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004023 struct btrfs_ioctl_quota_ctl_args *sa;
4024 struct btrfs_trans_handle *trans = NULL;
4025 int ret;
4026 int err;
4027
4028 if (!capable(CAP_SYS_ADMIN))
4029 return -EPERM;
4030
Miao Xie905b0dd2012-11-26 08:50:11 +00004031 ret = mnt_want_write_file(file);
4032 if (ret)
4033 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004034
4035 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004036 if (IS_ERR(sa)) {
4037 ret = PTR_ERR(sa);
4038 goto drop_write;
4039 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004040
Wang Shilong7708f022013-04-07 10:24:57 +00004041 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004042 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4043 if (IS_ERR(trans)) {
4044 ret = PTR_ERR(trans);
4045 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004046 }
4047
4048 switch (sa->cmd) {
4049 case BTRFS_QUOTA_CTL_ENABLE:
4050 ret = btrfs_quota_enable(trans, root->fs_info);
4051 break;
4052 case BTRFS_QUOTA_CTL_DISABLE:
4053 ret = btrfs_quota_disable(trans, root->fs_info);
4054 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004055 default:
4056 ret = -EINVAL;
4057 break;
4058 }
4059
Jan Schmidt2f232032013-04-25 16:04:51 +00004060 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4061 if (err && !ret)
4062 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004063out:
4064 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004065 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004066drop_write:
4067 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004068 return ret;
4069}
4070
Miao Xie905b0dd2012-11-26 08:50:11 +00004071static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004072{
Al Viro496ad9a2013-01-23 17:07:38 -05004073 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004074 struct btrfs_ioctl_qgroup_assign_args *sa;
4075 struct btrfs_trans_handle *trans;
4076 int ret;
4077 int err;
4078
4079 if (!capable(CAP_SYS_ADMIN))
4080 return -EPERM;
4081
Miao Xie905b0dd2012-11-26 08:50:11 +00004082 ret = mnt_want_write_file(file);
4083 if (ret)
4084 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004085
4086 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004087 if (IS_ERR(sa)) {
4088 ret = PTR_ERR(sa);
4089 goto drop_write;
4090 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004091
4092 trans = btrfs_join_transaction(root);
4093 if (IS_ERR(trans)) {
4094 ret = PTR_ERR(trans);
4095 goto out;
4096 }
4097
4098 /* FIXME: check if the IDs really exist */
4099 if (sa->assign) {
4100 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4101 sa->src, sa->dst);
4102 } else {
4103 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4104 sa->src, sa->dst);
4105 }
4106
4107 err = btrfs_end_transaction(trans, root);
4108 if (err && !ret)
4109 ret = err;
4110
4111out:
4112 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004113drop_write:
4114 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004115 return ret;
4116}
4117
Miao Xie905b0dd2012-11-26 08:50:11 +00004118static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004119{
Al Viro496ad9a2013-01-23 17:07:38 -05004120 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004121 struct btrfs_ioctl_qgroup_create_args *sa;
4122 struct btrfs_trans_handle *trans;
4123 int ret;
4124 int err;
4125
4126 if (!capable(CAP_SYS_ADMIN))
4127 return -EPERM;
4128
Miao Xie905b0dd2012-11-26 08:50:11 +00004129 ret = mnt_want_write_file(file);
4130 if (ret)
4131 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004132
4133 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004134 if (IS_ERR(sa)) {
4135 ret = PTR_ERR(sa);
4136 goto drop_write;
4137 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004138
Miao Xied86e56c2012-11-15 11:35:41 +00004139 if (!sa->qgroupid) {
4140 ret = -EINVAL;
4141 goto out;
4142 }
4143
Arne Jansen5d13a372011-09-14 15:53:51 +02004144 trans = btrfs_join_transaction(root);
4145 if (IS_ERR(trans)) {
4146 ret = PTR_ERR(trans);
4147 goto out;
4148 }
4149
4150 /* FIXME: check if the IDs really exist */
4151 if (sa->create) {
4152 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4153 NULL);
4154 } else {
4155 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4156 }
4157
4158 err = btrfs_end_transaction(trans, root);
4159 if (err && !ret)
4160 ret = err;
4161
4162out:
4163 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004164drop_write:
4165 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004166 return ret;
4167}
4168
Miao Xie905b0dd2012-11-26 08:50:11 +00004169static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004170{
Al Viro496ad9a2013-01-23 17:07:38 -05004171 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004172 struct btrfs_ioctl_qgroup_limit_args *sa;
4173 struct btrfs_trans_handle *trans;
4174 int ret;
4175 int err;
4176 u64 qgroupid;
4177
4178 if (!capable(CAP_SYS_ADMIN))
4179 return -EPERM;
4180
Miao Xie905b0dd2012-11-26 08:50:11 +00004181 ret = mnt_want_write_file(file);
4182 if (ret)
4183 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004184
4185 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004186 if (IS_ERR(sa)) {
4187 ret = PTR_ERR(sa);
4188 goto drop_write;
4189 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004190
4191 trans = btrfs_join_transaction(root);
4192 if (IS_ERR(trans)) {
4193 ret = PTR_ERR(trans);
4194 goto out;
4195 }
4196
4197 qgroupid = sa->qgroupid;
4198 if (!qgroupid) {
4199 /* take the current subvol as qgroup */
4200 qgroupid = root->root_key.objectid;
4201 }
4202
4203 /* FIXME: check if the IDs really exist */
4204 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4205
4206 err = btrfs_end_transaction(trans, root);
4207 if (err && !ret)
4208 ret = err;
4209
4210out:
4211 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004212drop_write:
4213 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004214 return ret;
4215}
4216
Jan Schmidt2f232032013-04-25 16:04:51 +00004217static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4218{
Al Viro6d0379e2013-06-16 19:32:35 +04004219 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004220 struct btrfs_ioctl_quota_rescan_args *qsa;
4221 int ret;
4222
4223 if (!capable(CAP_SYS_ADMIN))
4224 return -EPERM;
4225
4226 ret = mnt_want_write_file(file);
4227 if (ret)
4228 return ret;
4229
4230 qsa = memdup_user(arg, sizeof(*qsa));
4231 if (IS_ERR(qsa)) {
4232 ret = PTR_ERR(qsa);
4233 goto drop_write;
4234 }
4235
4236 if (qsa->flags) {
4237 ret = -EINVAL;
4238 goto out;
4239 }
4240
4241 ret = btrfs_qgroup_rescan(root->fs_info);
4242
4243out:
4244 kfree(qsa);
4245drop_write:
4246 mnt_drop_write_file(file);
4247 return ret;
4248}
4249
4250static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4251{
Al Viro6d0379e2013-06-16 19:32:35 +04004252 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004253 struct btrfs_ioctl_quota_rescan_args *qsa;
4254 int ret = 0;
4255
4256 if (!capable(CAP_SYS_ADMIN))
4257 return -EPERM;
4258
4259 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4260 if (!qsa)
4261 return -ENOMEM;
4262
4263 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4264 qsa->flags = 1;
4265 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4266 }
4267
4268 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4269 ret = -EFAULT;
4270
4271 kfree(qsa);
4272 return ret;
4273}
4274
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004275static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4276{
4277 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
4278
4279 if (!capable(CAP_SYS_ADMIN))
4280 return -EPERM;
4281
4282 return btrfs_qgroup_wait_for_completion(root->fs_info);
4283}
4284
Alexander Block8ea05e32012-07-25 17:35:53 +02004285static long btrfs_ioctl_set_received_subvol(struct file *file,
4286 void __user *arg)
4287{
4288 struct btrfs_ioctl_received_subvol_args *sa = NULL;
Al Viro496ad9a2013-01-23 17:07:38 -05004289 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004290 struct btrfs_root *root = BTRFS_I(inode)->root;
4291 struct btrfs_root_item *root_item = &root->root_item;
4292 struct btrfs_trans_handle *trans;
4293 struct timespec ct = CURRENT_TIME;
4294 int ret = 0;
4295
4296 ret = mnt_want_write_file(file);
4297 if (ret < 0)
4298 return ret;
4299
4300 down_write(&root->fs_info->subvol_sem);
4301
4302 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4303 ret = -EINVAL;
4304 goto out;
4305 }
4306
4307 if (btrfs_root_readonly(root)) {
4308 ret = -EROFS;
4309 goto out;
4310 }
4311
4312 if (!inode_owner_or_capable(inode)) {
4313 ret = -EACCES;
4314 goto out;
4315 }
4316
4317 sa = memdup_user(arg, sizeof(*sa));
4318 if (IS_ERR(sa)) {
4319 ret = PTR_ERR(sa);
4320 sa = NULL;
4321 goto out;
4322 }
4323
4324 trans = btrfs_start_transaction(root, 1);
4325 if (IS_ERR(trans)) {
4326 ret = PTR_ERR(trans);
4327 trans = NULL;
4328 goto out;
4329 }
4330
4331 sa->rtransid = trans->transid;
4332 sa->rtime.sec = ct.tv_sec;
4333 sa->rtime.nsec = ct.tv_nsec;
4334
4335 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4336 btrfs_set_root_stransid(root_item, sa->stransid);
4337 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004338 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4339 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4340 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4341 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004342
4343 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4344 &root->root_key, &root->root_item);
4345 if (ret < 0) {
4346 btrfs_end_transaction(trans, root);
4347 trans = NULL;
4348 goto out;
4349 } else {
4350 ret = btrfs_commit_transaction(trans, root);
4351 if (ret < 0)
4352 goto out;
4353 }
4354
4355 ret = copy_to_user(arg, sa, sizeof(*sa));
4356 if (ret)
4357 ret = -EFAULT;
4358
4359out:
4360 kfree(sa);
4361 up_write(&root->fs_info->subvol_sem);
4362 mnt_drop_write_file(file);
4363 return ret;
4364}
4365
jeff.liu867ab662013-01-05 02:48:01 +00004366static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4367{
Al Viro6d0379e2013-06-16 19:32:35 +04004368 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004369 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004370 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004371 char label[BTRFS_LABEL_SIZE];
4372
4373 spin_lock(&root->fs_info->super_lock);
4374 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4375 spin_unlock(&root->fs_info->super_lock);
4376
4377 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004378
4379 if (len == BTRFS_LABEL_SIZE) {
4380 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4381 --len);
4382 }
4383
jeff.liu867ab662013-01-05 02:48:01 +00004384 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004385
4386 return ret ? -EFAULT : 0;
4387}
4388
jeff.liua8bfd4a2013-01-05 02:48:08 +00004389static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4390{
Al Viro6d0379e2013-06-16 19:32:35 +04004391 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004392 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4393 struct btrfs_trans_handle *trans;
4394 char label[BTRFS_LABEL_SIZE];
4395 int ret;
4396
4397 if (!capable(CAP_SYS_ADMIN))
4398 return -EPERM;
4399
4400 if (copy_from_user(label, arg, sizeof(label)))
4401 return -EFAULT;
4402
4403 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4404 pr_err("btrfs: unable to set label with more than %d bytes\n",
4405 BTRFS_LABEL_SIZE - 1);
4406 return -EINVAL;
4407 }
4408
4409 ret = mnt_want_write_file(file);
4410 if (ret)
4411 return ret;
4412
jeff.liua8bfd4a2013-01-05 02:48:08 +00004413 trans = btrfs_start_transaction(root, 0);
4414 if (IS_ERR(trans)) {
4415 ret = PTR_ERR(trans);
4416 goto out_unlock;
4417 }
4418
Anand Jaina1b83ac2013-07-19 17:39:32 +08004419 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004420 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004421 spin_unlock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004422 ret = btrfs_end_transaction(trans, root);
4423
4424out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004425 mnt_drop_write_file(file);
4426 return ret;
4427}
4428
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004429long btrfs_ioctl(struct file *file, unsigned int
4430 cmd, unsigned long arg)
4431{
Al Viro496ad9a2013-01-23 17:07:38 -05004432 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004433 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004434
4435 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004436 case FS_IOC_GETFLAGS:
4437 return btrfs_ioctl_getflags(file, argp);
4438 case FS_IOC_SETFLAGS:
4439 return btrfs_ioctl_setflags(file, argp);
4440 case FS_IOC_GETVERSION:
4441 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004442 case FITRIM:
4443 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004444 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004445 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004446 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004447 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004448 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004449 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004450 case BTRFS_IOC_SUBVOL_CREATE_V2:
4451 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004452 case BTRFS_IOC_SNAP_DESTROY:
4453 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004454 case BTRFS_IOC_SUBVOL_GETFLAGS:
4455 return btrfs_ioctl_subvol_getflags(file, argp);
4456 case BTRFS_IOC_SUBVOL_SETFLAGS:
4457 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004458 case BTRFS_IOC_DEFAULT_SUBVOL:
4459 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004460 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004461 return btrfs_ioctl_defrag(file, NULL);
4462 case BTRFS_IOC_DEFRAG_RANGE:
4463 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004464 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004465 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004466 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004467 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004468 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004469 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004470 case BTRFS_IOC_FS_INFO:
4471 return btrfs_ioctl_fs_info(root, argp);
4472 case BTRFS_IOC_DEV_INFO:
4473 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004474 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004475 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004476 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004477 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4478 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004479 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004480 case BTRFS_IOC_TRANS_START:
4481 return btrfs_ioctl_trans_start(file);
4482 case BTRFS_IOC_TRANS_END:
4483 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004484 case BTRFS_IOC_TREE_SEARCH:
4485 return btrfs_ioctl_tree_search(file, argp);
4486 case BTRFS_IOC_INO_LOOKUP:
4487 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004488 case BTRFS_IOC_INO_PATHS:
4489 return btrfs_ioctl_ino_to_path(root, argp);
4490 case BTRFS_IOC_LOGICAL_INO:
4491 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004492 case BTRFS_IOC_SPACE_INFO:
4493 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004494 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004495 btrfs_sync_fs(file->f_dentry->d_sb, 1);
4496 return 0;
Sage Weil46204592010-10-29 15:41:32 -04004497 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004498 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004499 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004500 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004501 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004502 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004503 case BTRFS_IOC_SCRUB_CANCEL:
4504 return btrfs_ioctl_scrub_cancel(root, argp);
4505 case BTRFS_IOC_SCRUB_PROGRESS:
4506 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004507 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004508 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004509 case BTRFS_IOC_BALANCE_CTL:
4510 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004511 case BTRFS_IOC_BALANCE_PROGRESS:
4512 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004513 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4514 return btrfs_ioctl_set_received_subvol(file, argp);
Alexander Block31db9f72012-07-25 23:19:24 +02004515 case BTRFS_IOC_SEND:
4516 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004517 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004518 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004519 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004520 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004521 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004522 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004523 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004524 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004525 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004526 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004527 case BTRFS_IOC_QUOTA_RESCAN:
4528 return btrfs_ioctl_quota_rescan(file, argp);
4529 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4530 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004531 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4532 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004533 case BTRFS_IOC_DEV_REPLACE:
4534 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004535 case BTRFS_IOC_GET_FSLABEL:
4536 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004537 case BTRFS_IOC_SET_FSLABEL:
4538 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004539 case BTRFS_IOC_FILE_EXTENT_SAME:
4540 return btrfs_ioctl_file_extent_same(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004541 }
4542
4543 return -ENOTTY;
4544}