blob: cd93eb530b743742a8461de356fc3f5eb28f74e5 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080053#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020054#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040055#include "rcu-string.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040056
Christoph Hellwig6cbff002009-04-17 10:37:41 +020057/* Mask out flags that are inappropriate for the given type of inode. */
58static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
59{
60 if (S_ISDIR(mode))
61 return flags;
62 else if (S_ISREG(mode))
63 return flags & ~FS_DIRSYNC_FL;
64 else
65 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
66}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040067
Christoph Hellwig6cbff002009-04-17 10:37:41 +020068/*
69 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
70 */
71static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
72{
73 unsigned int iflags = 0;
74
75 if (flags & BTRFS_INODE_SYNC)
76 iflags |= FS_SYNC_FL;
77 if (flags & BTRFS_INODE_IMMUTABLE)
78 iflags |= FS_IMMUTABLE_FL;
79 if (flags & BTRFS_INODE_APPEND)
80 iflags |= FS_APPEND_FL;
81 if (flags & BTRFS_INODE_NODUMP)
82 iflags |= FS_NODUMP_FL;
83 if (flags & BTRFS_INODE_NOATIME)
84 iflags |= FS_NOATIME_FL;
85 if (flags & BTRFS_INODE_DIRSYNC)
86 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000087 if (flags & BTRFS_INODE_NODATACOW)
88 iflags |= FS_NOCOW_FL;
89
90 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
91 iflags |= FS_COMPR_FL;
92 else if (flags & BTRFS_INODE_NOCOMPRESS)
93 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020094
95 return iflags;
96}
97
98/*
99 * Update inode->i_flags based on the btrfs internal flags.
100 */
101void btrfs_update_iflags(struct inode *inode)
102{
103 struct btrfs_inode *ip = BTRFS_I(inode);
104
105 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
106
107 if (ip->flags & BTRFS_INODE_SYNC)
108 inode->i_flags |= S_SYNC;
109 if (ip->flags & BTRFS_INODE_IMMUTABLE)
110 inode->i_flags |= S_IMMUTABLE;
111 if (ip->flags & BTRFS_INODE_APPEND)
112 inode->i_flags |= S_APPEND;
113 if (ip->flags & BTRFS_INODE_NOATIME)
114 inode->i_flags |= S_NOATIME;
115 if (ip->flags & BTRFS_INODE_DIRSYNC)
116 inode->i_flags |= S_DIRSYNC;
117}
118
119/*
120 * Inherit flags from the parent inode.
121 *
Josef Bacike27425d2011-09-27 11:01:30 -0400122 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200123 */
124void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
125{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400126 unsigned int flags;
127
128 if (!dir)
129 return;
130
131 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200132
Josef Bacike27425d2011-09-27 11:01:30 -0400133 if (flags & BTRFS_INODE_NOCOMPRESS) {
134 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
135 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
136 } else if (flags & BTRFS_INODE_COMPRESS) {
137 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
138 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
139 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200140
Josef Bacike27425d2011-09-27 11:01:30 -0400141 if (flags & BTRFS_INODE_NODATACOW)
142 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
143
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200144 btrfs_update_iflags(inode);
145}
146
147static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
148{
149 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
150 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
151
152 if (copy_to_user(arg, &flags, sizeof(flags)))
153 return -EFAULT;
154 return 0;
155}
156
Liu Bo75e7cb72011-03-22 10:12:20 +0000157static int check_flags(unsigned int flags)
158{
159 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
160 FS_NOATIME_FL | FS_NODUMP_FL | \
161 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000162 FS_NOCOMP_FL | FS_COMPR_FL |
163 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000164 return -EOPNOTSUPP;
165
166 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
167 return -EINVAL;
168
Liu Bo75e7cb72011-03-22 10:12:20 +0000169 return 0;
170}
171
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200172static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
173{
174 struct inode *inode = file->f_path.dentry->d_inode;
175 struct btrfs_inode *ip = BTRFS_I(inode);
176 struct btrfs_root *root = ip->root;
177 struct btrfs_trans_handle *trans;
178 unsigned int flags, oldflags;
179 int ret;
Li Zefanf062abf02011-12-29 13:36:45 +0800180 u64 ip_oldflags;
181 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200182
Li Zefanb83cc962010-12-20 16:04:08 +0800183 if (btrfs_root_readonly(root))
184 return -EROFS;
185
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200186 if (copy_from_user(&flags, arg, sizeof(flags)))
187 return -EFAULT;
188
Liu Bo75e7cb72011-03-22 10:12:20 +0000189 ret = check_flags(flags);
190 if (ret)
191 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200192
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700193 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200194 return -EACCES;
195
Jan Karae7848682012-06-12 16:20:32 +0200196 ret = mnt_want_write_file(file);
197 if (ret)
198 return ret;
199
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200 mutex_lock(&inode->i_mutex);
201
Li Zefanf062abf02011-12-29 13:36:45 +0800202 ip_oldflags = ip->flags;
203 i_oldflags = inode->i_flags;
204
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 flags = btrfs_mask_flags(inode->i_mode, flags);
206 oldflags = btrfs_flags_to_ioctl(ip->flags);
207 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
208 if (!capable(CAP_LINUX_IMMUTABLE)) {
209 ret = -EPERM;
210 goto out_unlock;
211 }
212 }
213
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200214 if (flags & FS_SYNC_FL)
215 ip->flags |= BTRFS_INODE_SYNC;
216 else
217 ip->flags &= ~BTRFS_INODE_SYNC;
218 if (flags & FS_IMMUTABLE_FL)
219 ip->flags |= BTRFS_INODE_IMMUTABLE;
220 else
221 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
222 if (flags & FS_APPEND_FL)
223 ip->flags |= BTRFS_INODE_APPEND;
224 else
225 ip->flags &= ~BTRFS_INODE_APPEND;
226 if (flags & FS_NODUMP_FL)
227 ip->flags |= BTRFS_INODE_NODUMP;
228 else
229 ip->flags &= ~BTRFS_INODE_NODUMP;
230 if (flags & FS_NOATIME_FL)
231 ip->flags |= BTRFS_INODE_NOATIME;
232 else
233 ip->flags &= ~BTRFS_INODE_NOATIME;
234 if (flags & FS_DIRSYNC_FL)
235 ip->flags |= BTRFS_INODE_DIRSYNC;
236 else
237 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000238 if (flags & FS_NOCOW_FL)
239 ip->flags |= BTRFS_INODE_NODATACOW;
240 else
241 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200242
Liu Bo75e7cb72011-03-22 10:12:20 +0000243 /*
244 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
245 * flag may be changed automatically if compression code won't make
246 * things smaller.
247 */
248 if (flags & FS_NOCOMP_FL) {
249 ip->flags &= ~BTRFS_INODE_COMPRESS;
250 ip->flags |= BTRFS_INODE_NOCOMPRESS;
251 } else if (flags & FS_COMPR_FL) {
252 ip->flags |= BTRFS_INODE_COMPRESS;
253 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000254 } else {
255 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000256 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200257
Li Zefan4da6f1a2011-12-29 13:39:50 +0800258 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf02011-12-29 13:36:45 +0800259 if (IS_ERR(trans)) {
260 ret = PTR_ERR(trans);
261 goto out_drop;
262 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200263
Li Zefan306424cc2011-12-14 20:12:02 -0500264 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400265 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500266 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200267 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200268
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200269 btrfs_end_transaction(trans, root);
Li Zefanf062abf02011-12-29 13:36:45 +0800270 out_drop:
271 if (ret) {
272 ip->flags = ip_oldflags;
273 inode->i_flags = i_oldflags;
274 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200275
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200276 out_unlock:
277 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200278 mnt_drop_write_file(file);
liubo2d4e6f6ad2011-02-24 09:38:16 +0000279 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200280}
281
282static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
283{
284 struct inode *inode = file->f_path.dentry->d_inode;
285
286 return put_user(inode->i_generation, arg);
287}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400288
Li Dongyangf7039b12011-03-24 10:24:28 +0000289static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
290{
Al Viro815745c2011-11-17 15:40:49 -0500291 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000292 struct btrfs_device *device;
293 struct request_queue *q;
294 struct fstrim_range range;
295 u64 minlen = ULLONG_MAX;
296 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500297 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000298 int ret;
299
300 if (!capable(CAP_SYS_ADMIN))
301 return -EPERM;
302
Xiao Guangrong1f781602011-04-20 10:09:16 +0000303 rcu_read_lock();
304 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
305 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000306 if (!device->bdev)
307 continue;
308 q = bdev_get_queue(device->bdev);
309 if (blk_queue_discard(q)) {
310 num_devices++;
311 minlen = min((u64)q->limits.discard_granularity,
312 minlen);
313 }
314 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000315 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200316
Li Dongyangf7039b12011-03-24 10:24:28 +0000317 if (!num_devices)
318 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000319 if (copy_from_user(&range, arg, sizeof(range)))
320 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200321 if (range.start > total_bytes)
322 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000323
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200324 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000325 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500326 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000327 if (ret < 0)
328 return ret;
329
330 if (copy_to_user(arg, &range, sizeof(range)))
331 return -EFAULT;
332
333 return 0;
334}
335
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400336static noinline int create_subvol(struct btrfs_root *root,
337 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400338 char *name, int namelen,
339 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400340{
341 struct btrfs_trans_handle *trans;
342 struct btrfs_key key;
343 struct btrfs_root_item root_item;
344 struct btrfs_inode_item *inode_item;
345 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400346 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400347 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000348 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400349 int ret;
350 int err;
351 u64 objectid;
352 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500353 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400354
Li Zefan581bb052011-04-20 10:06:11 +0800355 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400356 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400357 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000358
359 dir = parent->d_inode;
360
Josef Bacik9ed74f22009-09-11 16:12:44 -0400361 /*
362 * 1 - inode item
363 * 2 - refs
364 * 1 - root item
365 * 2 - dir items
366 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400367 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400368 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400369 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400370
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400371 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200372 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400373 if (IS_ERR(leaf)) {
374 ret = PTR_ERR(leaf);
375 goto fail;
376 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400377
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400378 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379 btrfs_set_header_bytenr(leaf, leaf->start);
380 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400381 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382 btrfs_set_header_owner(leaf, objectid);
383
384 write_extent_buffer(leaf, root->fs_info->fsid,
385 (unsigned long)btrfs_header_fsid(leaf),
386 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
388 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
389 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400390 btrfs_mark_buffer_dirty(leaf);
391
392 inode_item = &root_item.inode;
393 memset(inode_item, 0, sizeof(*inode_item));
394 inode_item->generation = cpu_to_le64(1);
395 inode_item->size = cpu_to_le64(3);
396 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400397 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
399
Li Zefan08fe4db2011-03-28 02:01:25 +0000400 root_item.flags = 0;
401 root_item.byte_limit = 0;
402 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
403
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400405 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400406 btrfs_set_root_level(&root_item, 0);
407 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000408 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400409 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400410
411 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
412 root_item.drop_level = 0;
413
Chris Mason925baed2008-06-25 16:01:30 -0400414 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400415 free_extent_buffer(leaf);
416 leaf = NULL;
417
418 btrfs_set_root_dirid(&root_item, new_dirid);
419
420 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400421 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400422 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
423 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
424 &root_item);
425 if (ret)
426 goto fail;
427
Yan, Zheng76dda932009-09-21 16:00:26 -0400428 key.offset = (u64)-1;
429 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100430 if (IS_ERR(new_root)) {
431 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
432 ret = PTR_ERR(new_root);
433 goto fail;
434 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400435
436 btrfs_record_root_in_trans(trans, new_root);
437
Josef Bacikd82a6f1d2011-05-11 15:26:06 -0400438 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700439 if (ret) {
440 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100441 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700442 goto fail;
443 }
444
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445 /*
446 * insert the directory item
447 */
Chris Mason3de45862008-11-17 21:02:50 -0500448 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100449 if (ret) {
450 btrfs_abort_transaction(trans, root, ret);
451 goto fail;
452 }
Chris Mason3de45862008-11-17 21:02:50 -0500453
454 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800455 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500456 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100457 if (ret) {
458 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400459 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100460 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500461
Yan Zheng52c26172009-01-05 15:43:43 -0500462 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
463 ret = btrfs_update_inode(trans, root, dir);
464 BUG_ON(ret);
465
Chris Mason0660b5a2008-11-17 20:37:39 -0500466 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400467 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800468 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400469
Chris Mason0660b5a2008-11-17 20:37:39 -0500470 BUG_ON(ret);
471
Yan, Zheng76dda932009-09-21 16:00:26 -0400472 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400473fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400474 if (async_transid) {
475 *async_transid = trans->transid;
476 err = btrfs_commit_transaction_async(trans, root, 1);
477 } else {
478 err = btrfs_commit_transaction(trans, root);
479 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400480 if (err && !ret)
481 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400482 return ret;
483}
484
Sage Weil72fd0322010-10-29 15:41:32 -0400485static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800486 char *name, int namelen, u64 *async_transid,
487 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000489 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400490 struct btrfs_pending_snapshot *pending_snapshot;
491 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000492 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400493
494 if (!root->ref_cows)
495 return -EINVAL;
496
Yan, Zhenga22285a2010-05-16 10:48:46 -0400497 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
498 if (!pending_snapshot)
499 return -ENOMEM;
500
501 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
502 pending_snapshot->dentry = dentry;
503 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800504 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400505
506 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
507 if (IS_ERR(trans)) {
508 ret = PTR_ERR(trans);
509 goto fail;
510 }
511
512 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
513 BUG_ON(ret);
514
Josef Bacik83515832011-06-14 15:16:14 -0400515 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400516 list_add(&pending_snapshot->list,
517 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400518 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400519 if (async_transid) {
520 *async_transid = trans->transid;
521 ret = btrfs_commit_transaction_async(trans,
522 root->fs_info->extent_root, 1);
523 } else {
524 ret = btrfs_commit_transaction(trans,
525 root->fs_info->extent_root);
526 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400527 BUG_ON(ret);
528
529 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400530 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000531 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400532
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500533 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
534 if (ret)
535 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400536
Al Viro2fbe8c82011-07-16 21:38:06 -0400537 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000538 if (IS_ERR(inode)) {
539 ret = PTR_ERR(inode);
540 goto fail;
541 }
542 BUG_ON(!inode);
543 d_instantiate(dentry, inode);
544 ret = 0;
545fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400546 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400547 return ret;
548}
549
Sage Weil4260f7c2010-10-29 15:46:43 -0400550/* copy of check_sticky in fs/namei.c()
551* It's inline, so penalty for filesystems that don't use sticky bit is
552* minimal.
553*/
554static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
555{
556 uid_t fsuid = current_fsuid();
557
558 if (!(dir->i_mode & S_ISVTX))
559 return 0;
560 if (inode->i_uid == fsuid)
561 return 0;
562 if (dir->i_uid == fsuid)
563 return 0;
564 return !capable(CAP_FOWNER);
565}
566
567/* copy of may_delete in fs/namei.c()
568 * Check whether we can remove a link victim from directory dir, check
569 * whether the type of victim is right.
570 * 1. We can't do it if dir is read-only (done in permission())
571 * 2. We should have write and exec permissions on dir
572 * 3. We can't remove anything from append-only dir
573 * 4. We can't do anything with immutable dir (done in permission())
574 * 5. If the sticky bit on dir is set we should either
575 * a. be owner of dir, or
576 * b. be owner of victim, or
577 * c. have CAP_FOWNER capability
578 * 6. If the victim is append-only or immutable we can't do antyhing with
579 * links pointing to it.
580 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
581 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
582 * 9. We can't remove a root or mountpoint.
583 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
584 * nfs_async_unlink().
585 */
586
587static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
588{
589 int error;
590
591 if (!victim->d_inode)
592 return -ENOENT;
593
594 BUG_ON(victim->d_parent->d_inode != dir);
595 audit_inode_child(victim, dir);
596
597 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
598 if (error)
599 return error;
600 if (IS_APPEND(dir))
601 return -EPERM;
602 if (btrfs_check_sticky(dir, victim->d_inode)||
603 IS_APPEND(victim->d_inode)||
604 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
605 return -EPERM;
606 if (isdir) {
607 if (!S_ISDIR(victim->d_inode->i_mode))
608 return -ENOTDIR;
609 if (IS_ROOT(victim))
610 return -EBUSY;
611 } else if (S_ISDIR(victim->d_inode->i_mode))
612 return -EISDIR;
613 if (IS_DEADDIR(dir))
614 return -ENOENT;
615 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
616 return -EBUSY;
617 return 0;
618}
619
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400620/* copy of may_create in fs/namei.c() */
621static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
622{
623 if (child->d_inode)
624 return -EEXIST;
625 if (IS_DEADDIR(dir))
626 return -ENOENT;
627 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
628}
629
630/*
631 * Create a new subvolume below @parent. This is largely modeled after
632 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
633 * inside this filesystem so it's quite a bit simpler.
634 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400635static noinline int btrfs_mksubvol(struct path *parent,
636 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400637 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800638 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400639{
Yan, Zheng76dda932009-09-21 16:00:26 -0400640 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400641 struct dentry *dentry;
642 int error;
643
Jan Karae7848682012-06-12 16:20:32 +0200644 error = mnt_want_write(parent->mnt);
645 if (error)
646 return error;
647
Yan, Zheng76dda932009-09-21 16:00:26 -0400648 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400649
650 dentry = lookup_one_len(name, parent->dentry, namelen);
651 error = PTR_ERR(dentry);
652 if (IS_ERR(dentry))
653 goto out_unlock;
654
655 error = -EEXIST;
656 if (dentry->d_inode)
657 goto out_dput;
658
Yan, Zheng76dda932009-09-21 16:00:26 -0400659 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400660 if (error)
Jan Karae7848682012-06-12 16:20:32 +0200661 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400662
Yan, Zheng76dda932009-09-21 16:00:26 -0400663 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
664
665 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
666 goto out_up_read;
667
Chris Mason3de45862008-11-17 21:02:50 -0500668 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400669 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800670 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500671 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400672 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400673 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500674 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400675 if (!error)
676 fsnotify_mkdir(dir, dentry);
677out_up_read:
678 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400679out_dput:
680 dput(dentry);
681out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400682 mutex_unlock(&dir->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200683 mnt_drop_write(parent->mnt);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400684 return error;
685}
686
Chris Mason4cb53002011-05-24 15:35:30 -0400687/*
688 * When we're defragging a range, we don't want to kick it off again
689 * if it is really just waiting for delalloc to send it down.
690 * If we find a nice big extent or delalloc range for the bytes in the
691 * file you want to defrag, we return 0 to let you know to skip this
692 * part of the file
693 */
694static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
695{
696 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
697 struct extent_map *em = NULL;
698 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
699 u64 end;
700
701 read_lock(&em_tree->lock);
702 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
703 read_unlock(&em_tree->lock);
704
705 if (em) {
706 end = extent_map_end(em);
707 free_extent_map(em);
708 if (end - offset > thresh)
709 return 0;
710 }
711 /* if we already have a nice delalloc here, just stop */
712 thresh /= 2;
713 end = count_range_bits(io_tree, &offset, offset + thresh,
714 thresh, EXTENT_DELALLOC, 1);
715 if (end >= thresh)
716 return 0;
717 return 1;
718}
719
720/*
721 * helper function to walk through a file and find extents
722 * newer than a specific transid, and smaller than thresh.
723 *
724 * This is used by the defragging code to find new and small
725 * extents
726 */
727static int find_new_extents(struct btrfs_root *root,
728 struct inode *inode, u64 newer_than,
729 u64 *off, int thresh)
730{
731 struct btrfs_path *path;
732 struct btrfs_key min_key;
733 struct btrfs_key max_key;
734 struct extent_buffer *leaf;
735 struct btrfs_file_extent_item *extent;
736 int type;
737 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000738 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400739
740 path = btrfs_alloc_path();
741 if (!path)
742 return -ENOMEM;
743
David Sterbaa4689d22011-05-31 17:08:14 +0000744 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400745 min_key.type = BTRFS_EXTENT_DATA_KEY;
746 min_key.offset = *off;
747
David Sterbaa4689d22011-05-31 17:08:14 +0000748 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400749 max_key.type = (u8)-1;
750 max_key.offset = (u64)-1;
751
752 path->keep_locks = 1;
753
754 while(1) {
755 ret = btrfs_search_forward(root, &min_key, &max_key,
756 path, 0, newer_than);
757 if (ret != 0)
758 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000759 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400760 goto none;
761 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
762 goto none;
763
764 leaf = path->nodes[0];
765 extent = btrfs_item_ptr(leaf, path->slots[0],
766 struct btrfs_file_extent_item);
767
768 type = btrfs_file_extent_type(leaf, extent);
769 if (type == BTRFS_FILE_EXTENT_REG &&
770 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
771 check_defrag_in_cache(inode, min_key.offset, thresh)) {
772 *off = min_key.offset;
773 btrfs_free_path(path);
774 return 0;
775 }
776
777 if (min_key.offset == (u64)-1)
778 goto none;
779
780 min_key.offset++;
781 btrfs_release_path(path);
782 }
783none:
784 btrfs_free_path(path);
785 return -ENOENT;
786}
787
Li Zefan6c282eb2012-06-11 16:03:35 +0800788static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400789{
790 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500791 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800792 struct extent_map *em;
793 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500794
795 /*
796 * hopefully we have this extent in the tree already, try without
797 * the full extent lock
798 */
799 read_lock(&em_tree->lock);
800 em = lookup_extent_mapping(em_tree, start, len);
801 read_unlock(&em_tree->lock);
802
803 if (!em) {
804 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100805 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500806 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100807 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500808
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000809 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800810 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500811 }
812
Li Zefan6c282eb2012-06-11 16:03:35 +0800813 return em;
814}
815
816static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
817{
818 struct extent_map *next;
819 bool ret = true;
820
821 /* this is the last extent */
822 if (em->start + em->len >= i_size_read(inode))
823 return false;
824
825 next = defrag_lookup_extent(inode, em->start + em->len);
826 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
827 ret = false;
828
829 free_extent_map(next);
830 return ret;
831}
832
833static int should_defrag_range(struct inode *inode, u64 start, int thresh,
834 u64 *last_len, u64 *skip, u64 *defrag_end)
835{
836 struct extent_map *em;
837 int ret = 1;
838 bool next_mergeable = true;
839
840 /*
841 * make sure that once we start defragging an extent, we keep on
842 * defragging it
843 */
844 if (start < *defrag_end)
845 return 1;
846
847 *skip = 0;
848
849 em = defrag_lookup_extent(inode, start);
850 if (!em)
851 return 0;
852
Chris Mason940100a2010-03-10 10:52:59 -0500853 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400854 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500855 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400856 goto out;
857 }
858
Li Zefan6c282eb2012-06-11 16:03:35 +0800859 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500860
861 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800862 * we hit a real extent, if it is big or the next extent is not a
863 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500864 */
Li Zefan6c282eb2012-06-11 16:03:35 +0800865 if ((*last_len == 0 || *last_len >= thresh) &&
866 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500867 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400868out:
Chris Mason940100a2010-03-10 10:52:59 -0500869 /*
870 * last_len ends up being a counter of how many bytes we've defragged.
871 * every time we choose not to defrag an extent, we reset *last_len
872 * so that the next tiny extent will force a defrag.
873 *
874 * The end result of this is that tiny extents before a single big
875 * extent will force at least part of that big extent to be defragged.
876 */
877 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500878 *defrag_end = extent_map_end(em);
879 } else {
880 *last_len = 0;
881 *skip = extent_map_end(em);
882 *defrag_end = 0;
883 }
884
885 free_extent_map(em);
886 return ret;
887}
888
Chris Mason4cb53002011-05-24 15:35:30 -0400889/*
890 * it doesn't do much good to defrag one or two pages
891 * at a time. This pulls in a nice chunk of pages
892 * to COW and defrag.
893 *
894 * It also makes sure the delalloc code has enough
895 * dirty data to avoid making new small extents as part
896 * of the defrag
897 *
898 * It's a good idea to start RA on this range
899 * before calling this.
900 */
901static int cluster_pages_for_defrag(struct inode *inode,
902 struct page **pages,
903 unsigned long start_index,
904 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400905{
Chris Mason4cb53002011-05-24 15:35:30 -0400906 unsigned long file_end;
907 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400908 u64 page_start;
909 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400910 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400911 int ret;
912 int i;
913 int i_done;
914 struct btrfs_ordered_extent *ordered;
915 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800916 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400917 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400918
Chris Mason4cb53002011-05-24 15:35:30 -0400919 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400920 if (!isize || start_index > file_end)
921 return 0;
922
923 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400924
925 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400926 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400927 if (ret)
928 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400929 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800930 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400931
932 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400933 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400934 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800935again:
Josef Bacika94733d2011-07-11 10:47:06 -0400936 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800937 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400938 if (!page)
939 break;
940
Miao Xie600a45e2012-02-16 15:01:24 +0800941 page_start = page_offset(page);
942 page_end = page_start + PAGE_CACHE_SIZE - 1;
943 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100944 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800945 ordered = btrfs_lookup_ordered_extent(inode,
946 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100947 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800948 if (!ordered)
949 break;
950
951 unlock_page(page);
952 btrfs_start_ordered_extent(inode, ordered, 1);
953 btrfs_put_ordered_extent(ordered);
954 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400955 /*
956 * we unlocked the page above, so we need check if
957 * it was released or not.
958 */
959 if (page->mapping != inode->i_mapping) {
960 unlock_page(page);
961 page_cache_release(page);
962 goto again;
963 }
Miao Xie600a45e2012-02-16 15:01:24 +0800964 }
965
Chris Mason4cb53002011-05-24 15:35:30 -0400966 if (!PageUptodate(page)) {
967 btrfs_readpage(NULL, page);
968 lock_page(page);
969 if (!PageUptodate(page)) {
970 unlock_page(page);
971 page_cache_release(page);
972 ret = -EIO;
973 break;
974 }
975 }
Miao Xie600a45e2012-02-16 15:01:24 +0800976
Miao Xie600a45e2012-02-16 15:01:24 +0800977 if (page->mapping != inode->i_mapping) {
978 unlock_page(page);
979 page_cache_release(page);
980 goto again;
981 }
982
Chris Mason4cb53002011-05-24 15:35:30 -0400983 pages[i] = page;
984 i_done++;
985 }
986 if (!i_done || ret)
987 goto out;
988
989 if (!(inode->i_sb->s_flags & MS_ACTIVE))
990 goto out;
991
992 /*
993 * so now we have a nice long stream of locked
994 * and up to date pages, lets wait on them
995 */
996 for (i = 0; i < i_done; i++)
997 wait_on_page_writeback(pages[i]);
998
999 page_start = page_offset(pages[0]);
1000 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1001
1002 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001003 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001004 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1005 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1006 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
1007 GFP_NOFS);
1008
Liu Bo1f12bd02012-03-29 09:57:44 -04001009 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001010 spin_lock(&BTRFS_I(inode)->lock);
1011 BTRFS_I(inode)->outstanding_extents++;
1012 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001013 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001014 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001015 }
1016
1017
1018 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
1019 &cached_state);
1020
1021 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1022 page_start, page_end - 1, &cached_state,
1023 GFP_NOFS);
1024
1025 for (i = 0; i < i_done; i++) {
1026 clear_page_dirty_for_io(pages[i]);
1027 ClearPageChecked(pages[i]);
1028 set_page_extent_mapped(pages[i]);
1029 set_page_dirty(pages[i]);
1030 unlock_page(pages[i]);
1031 page_cache_release(pages[i]);
1032 }
1033 return i_done;
1034out:
1035 for (i = 0; i < i_done; i++) {
1036 unlock_page(pages[i]);
1037 page_cache_release(pages[i]);
1038 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001039 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001040 return ret;
1041
1042}
1043
1044int btrfs_defrag_file(struct inode *inode, struct file *file,
1045 struct btrfs_ioctl_defrag_range_args *range,
1046 u64 newer_than, unsigned long max_to_defrag)
1047{
1048 struct btrfs_root *root = BTRFS_I(inode)->root;
1049 struct btrfs_super_block *disk_super;
1050 struct file_ra_state *ra = NULL;
1051 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001052 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001053 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001054 u64 last_len = 0;
1055 u64 skip = 0;
1056 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001057 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001058 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001059 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001060 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001061 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001062 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001063 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001064 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1065 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001066 u64 new_align = ~((u64)128 * 1024 - 1);
1067 struct page **pages = NULL;
1068
1069 if (extent_thresh == 0)
1070 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001071
1072 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1073 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1074 return -EINVAL;
1075 if (range->compress_type)
1076 compress_type = range->compress_type;
1077 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001078
Li Zefan151a31b2011-09-02 15:56:39 +08001079 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001080 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001081
Chris Mason4cb53002011-05-24 15:35:30 -04001082 /*
1083 * if we were not given a file, allocate a readahead
1084 * context
1085 */
1086 if (!file) {
1087 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1088 if (!ra)
1089 return -ENOMEM;
1090 file_ra_state_init(ra, inode->i_mapping);
1091 } else {
1092 ra = &file->f_ra;
1093 }
1094
Li Zefan008873e2011-09-02 15:57:07 +08001095 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001096 GFP_NOFS);
1097 if (!pages) {
1098 ret = -ENOMEM;
1099 goto out_ra;
1100 }
1101
1102 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001103 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001104 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001105 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1106 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001107 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001108 }
1109
Chris Mason4cb53002011-05-24 15:35:30 -04001110 if (newer_than) {
1111 ret = find_new_extents(root, inode, newer_than,
1112 &newer_off, 64 * 1024);
1113 if (!ret) {
1114 range->start = newer_off;
1115 /*
1116 * we always align our defrag to help keep
1117 * the extents in the file evenly spaced
1118 */
1119 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001120 } else
1121 goto out_ra;
1122 } else {
1123 i = range->start >> PAGE_CACHE_SHIFT;
1124 }
1125 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001126 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001127
Li Zefan2a0f7f52011-10-10 15:43:34 -04001128 /*
1129 * make writeback starts from i, so the defrag range can be
1130 * written sequentially.
1131 */
1132 if (i < inode->i_mapping->writeback_index)
1133 inode->i_mapping->writeback_index = i;
1134
Chris Masonf7f43cc2011-10-11 11:41:40 -04001135 while (i <= last_index && defrag_count < max_to_defrag &&
1136 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1137 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001138 /*
1139 * make sure we stop running if someone unmounts
1140 * the FS
1141 */
1142 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1143 break;
1144
Liu Bo66c26892012-03-29 09:57:45 -04001145 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001146 extent_thresh, &last_len, &skip,
1147 &defrag_end)) {
Chris Mason940100a2010-03-10 10:52:59 -05001148 unsigned long next;
1149 /*
1150 * the should_defrag function tells us how much to skip
1151 * bump our counter by the suggested amount
1152 */
1153 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1154 i = max(i + 1, next);
1155 continue;
1156 }
Li Zefan008873e2011-09-02 15:57:07 +08001157
1158 if (!newer_than) {
1159 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1160 PAGE_CACHE_SHIFT) - i;
1161 cluster = min(cluster, max_cluster);
1162 } else {
1163 cluster = max_cluster;
1164 }
1165
Chris Mason1e701a32010-03-11 09:42:04 -05001166 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001167 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001168
Li Zefan008873e2011-09-02 15:57:07 +08001169 if (i + cluster > ra_index) {
1170 ra_index = max(i, ra_index);
1171 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1172 cluster);
1173 ra_index += max_cluster;
1174 }
Chris Mason940100a2010-03-10 10:52:59 -05001175
Liu Boecb8bea2012-03-29 09:57:44 -04001176 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001177 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001178 if (ret < 0) {
1179 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001180 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001181 }
Chris Mason940100a2010-03-10 10:52:59 -05001182
Chris Mason4cb53002011-05-24 15:35:30 -04001183 defrag_count += ret;
1184 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001185 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001186
1187 if (newer_than) {
1188 if (newer_off == (u64)-1)
1189 break;
1190
Liu Boe1f041e2012-03-29 09:57:45 -04001191 if (ret > 0)
1192 i += ret;
1193
Chris Mason4cb53002011-05-24 15:35:30 -04001194 newer_off = max(newer_off + 1,
1195 (u64)i << PAGE_CACHE_SHIFT);
1196
1197 ret = find_new_extents(root, inode,
1198 newer_than, &newer_off,
1199 64 * 1024);
1200 if (!ret) {
1201 range->start = newer_off;
1202 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001203 } else {
1204 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001205 }
Chris Mason4cb53002011-05-24 15:35:30 -04001206 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001207 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001208 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001209 last_len += ret << PAGE_CACHE_SHIFT;
1210 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001211 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001212 last_len = 0;
1213 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001214 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001215 }
1216
Chris Mason1e701a32010-03-11 09:42:04 -05001217 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1218 filemap_flush(inode->i_mapping);
1219
1220 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1221 /* the filemap_flush will queue IO into the worker threads, but
1222 * we have to make sure the IO is actually started and that
1223 * ordered extents get created before we return
1224 */
1225 atomic_inc(&root->fs_info->async_submit_draining);
1226 while (atomic_read(&root->fs_info->nr_async_submits) ||
1227 atomic_read(&root->fs_info->async_delalloc_pages)) {
1228 wait_event(root->fs_info->async_submit_wait,
1229 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1230 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1231 }
1232 atomic_dec(&root->fs_info->async_submit_draining);
1233
1234 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001235 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001236 mutex_unlock(&inode->i_mutex);
1237 }
1238
David Sterba6c417612011-04-13 15:41:04 +02001239 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001240 features = btrfs_super_incompat_flags(disk_super);
1241 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1242 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1243 btrfs_set_super_incompat_flags(disk_super, features);
1244 }
1245
Diego Calleja60ccf822011-09-01 16:33:57 +02001246 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001247
Chris Mason4cb53002011-05-24 15:35:30 -04001248out_ra:
1249 if (!file)
1250 kfree(ra);
1251 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001252 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001253}
1254
Yan, Zheng76dda932009-09-21 16:00:26 -04001255static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1256 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001257{
1258 u64 new_size;
1259 u64 old_size;
1260 u64 devid = 1;
1261 struct btrfs_ioctl_vol_args *vol_args;
1262 struct btrfs_trans_handle *trans;
1263 struct btrfs_device *device = NULL;
1264 char *sizestr;
1265 char *devstr = NULL;
1266 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001267 int mod = 0;
1268
Yan Zhengc146afa2008-11-12 14:34:12 -05001269 if (root->fs_info->sb->s_flags & MS_RDONLY)
1270 return -EROFS;
1271
Chris Masone441d542009-01-05 16:57:23 -05001272 if (!capable(CAP_SYS_ADMIN))
1273 return -EPERM;
1274
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001275 mutex_lock(&root->fs_info->volume_mutex);
1276 if (root->fs_info->balance_ctl) {
1277 printk(KERN_INFO "btrfs: balance in progress\n");
1278 ret = -EINVAL;
1279 goto out;
1280 }
1281
Li Zefandae7b662009-04-08 15:06:54 +08001282 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001283 if (IS_ERR(vol_args)) {
1284 ret = PTR_ERR(vol_args);
1285 goto out;
1286 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001287
1288 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001289
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001290 sizestr = vol_args->name;
1291 devstr = strchr(sizestr, ':');
1292 if (devstr) {
1293 char *end;
1294 sizestr = devstr + 1;
1295 *devstr = '\0';
1296 devstr = vol_args->name;
1297 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001298 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001299 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001300 }
Yan Zheng2b820322008-11-17 21:11:30 -05001301 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001303 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001304 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001305 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001306 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001307 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001308 if (device->fs_devices && device->fs_devices->seeding) {
1309 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001310 "seeding device %llu\n",
1311 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001312 ret = -EINVAL;
1313 goto out_free;
1314 }
1315
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001316 if (!strcmp(sizestr, "max"))
1317 new_size = device->bdev->bd_inode->i_size;
1318 else {
1319 if (sizestr[0] == '-') {
1320 mod = -1;
1321 sizestr++;
1322 } else if (sizestr[0] == '+') {
1323 mod = 1;
1324 sizestr++;
1325 }
Akinobu Mita91748462010-02-28 10:59:11 +00001326 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001327 if (new_size == 0) {
1328 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001329 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330 }
1331 }
1332
1333 old_size = device->total_bytes;
1334
1335 if (mod < 0) {
1336 if (new_size > old_size) {
1337 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001338 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001339 }
1340 new_size = old_size - new_size;
1341 } else if (mod > 0) {
1342 new_size = old_size + new_size;
1343 }
1344
1345 if (new_size < 256 * 1024 * 1024) {
1346 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001347 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001348 }
1349 if (new_size > device->bdev->bd_inode->i_size) {
1350 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001351 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001352 }
1353
1354 do_div(new_size, root->sectorsize);
1355 new_size *= root->sectorsize;
1356
Josef Bacik606686e2012-06-04 14:03:51 -04001357 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1358 rcu_str_deref(device->name),
1359 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001360
1361 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001362 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001363 if (IS_ERR(trans)) {
1364 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001365 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001366 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001367 ret = btrfs_grow_device(trans, device, new_size);
1368 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001369 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001370 ret = btrfs_shrink_device(device, new_size);
1371 }
1372
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001373out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001374 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001375out:
1376 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001377 return ret;
1378}
1379
Sage Weil72fd0322010-10-29 15:41:32 -04001380static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1381 char *name,
1382 unsigned long fd,
1383 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001384 u64 *transid,
1385 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001386{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001387 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001388 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001389 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001390 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001391
Yan Zhengc146afa2008-11-12 14:34:12 -05001392 if (root->fs_info->sb->s_flags & MS_RDONLY)
1393 return -EROFS;
1394
Sage Weil72fd0322010-10-29 15:41:32 -04001395 namelen = strlen(name);
1396 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001397 ret = -EINVAL;
1398 goto out;
1399 }
1400
Chris Mason16780ca2012-02-20 22:14:55 -05001401 if (name[0] == '.' &&
1402 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1403 ret = -EEXIST;
1404 goto out;
1405 }
1406
Chris Mason3de45862008-11-17 21:02:50 -05001407 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001408 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001409 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001410 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001411 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001412 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001413 if (!src_file) {
1414 ret = -EINVAL;
1415 goto out;
1416 }
1417
1418 src_inode = src_file->f_path.dentry->d_inode;
1419 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001420 printk(KERN_INFO "btrfs: Snapshot src from "
1421 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001422 ret = -EINVAL;
1423 fput(src_file);
1424 goto out;
1425 }
Sage Weil72fd0322010-10-29 15:41:32 -04001426 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1427 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001428 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001429 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001430 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431out:
Sage Weil72fd0322010-10-29 15:41:32 -04001432 return ret;
1433}
1434
1435static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001436 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001437{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001438 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001439 int ret;
1440
Li Zefanfa0d2b92010-12-20 15:53:28 +08001441 vol_args = memdup_user(arg, sizeof(*vol_args));
1442 if (IS_ERR(vol_args))
1443 return PTR_ERR(vol_args);
1444 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001445
Li Zefanfa0d2b92010-12-20 15:53:28 +08001446 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001447 vol_args->fd, subvol,
1448 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001449
Li Zefanfa0d2b92010-12-20 15:53:28 +08001450 kfree(vol_args);
1451 return ret;
1452}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001453
Li Zefanfa0d2b92010-12-20 15:53:28 +08001454static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1455 void __user *arg, int subvol)
1456{
1457 struct btrfs_ioctl_vol_args_v2 *vol_args;
1458 int ret;
1459 u64 transid = 0;
1460 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001461 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001462
Li Zefanfa0d2b92010-12-20 15:53:28 +08001463 vol_args = memdup_user(arg, sizeof(*vol_args));
1464 if (IS_ERR(vol_args))
1465 return PTR_ERR(vol_args);
1466 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001467
Li Zefanb83cc962010-12-20 16:04:08 +08001468 if (vol_args->flags &
1469 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1470 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001471 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001472 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001473
1474 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1475 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001476 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1477 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001478
1479 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001480 vol_args->fd, subvol,
1481 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001482
1483 if (ret == 0 && ptr &&
1484 copy_to_user(arg +
1485 offsetof(struct btrfs_ioctl_vol_args_v2,
1486 transid), ptr, sizeof(*ptr)))
1487 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001488out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001489 kfree(vol_args);
1490 return ret;
1491}
1492
Li Zefan0caa1022010-12-20 16:30:25 +08001493static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1494 void __user *arg)
1495{
1496 struct inode *inode = fdentry(file)->d_inode;
1497 struct btrfs_root *root = BTRFS_I(inode)->root;
1498 int ret = 0;
1499 u64 flags = 0;
1500
Li Zefan33345d012011-04-20 10:31:50 +08001501 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001502 return -EINVAL;
1503
1504 down_read(&root->fs_info->subvol_sem);
1505 if (btrfs_root_readonly(root))
1506 flags |= BTRFS_SUBVOL_RDONLY;
1507 up_read(&root->fs_info->subvol_sem);
1508
1509 if (copy_to_user(arg, &flags, sizeof(flags)))
1510 ret = -EFAULT;
1511
1512 return ret;
1513}
1514
1515static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1516 void __user *arg)
1517{
1518 struct inode *inode = fdentry(file)->d_inode;
1519 struct btrfs_root *root = BTRFS_I(inode)->root;
1520 struct btrfs_trans_handle *trans;
1521 u64 root_flags;
1522 u64 flags;
1523 int ret = 0;
1524
1525 if (root->fs_info->sb->s_flags & MS_RDONLY)
1526 return -EROFS;
1527
Li Zefan33345d012011-04-20 10:31:50 +08001528 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001529 return -EINVAL;
1530
1531 if (copy_from_user(&flags, arg, sizeof(flags)))
1532 return -EFAULT;
1533
Li Zefanb4dc2b82011-02-16 06:06:34 +00001534 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001535 return -EINVAL;
1536
1537 if (flags & ~BTRFS_SUBVOL_RDONLY)
1538 return -EOPNOTSUPP;
1539
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001540 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001541 return -EACCES;
1542
Li Zefan0caa1022010-12-20 16:30:25 +08001543 down_write(&root->fs_info->subvol_sem);
1544
1545 /* nothing to do */
1546 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1547 goto out;
1548
1549 root_flags = btrfs_root_flags(&root->root_item);
1550 if (flags & BTRFS_SUBVOL_RDONLY)
1551 btrfs_set_root_flags(&root->root_item,
1552 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1553 else
1554 btrfs_set_root_flags(&root->root_item,
1555 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1556
1557 trans = btrfs_start_transaction(root, 1);
1558 if (IS_ERR(trans)) {
1559 ret = PTR_ERR(trans);
1560 goto out_reset;
1561 }
1562
Li Zefanb4dc2b82011-02-16 06:06:34 +00001563 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001564 &root->root_key, &root->root_item);
1565
1566 btrfs_commit_transaction(trans, root);
1567out_reset:
1568 if (ret)
1569 btrfs_set_root_flags(&root->root_item, root_flags);
1570out:
1571 up_write(&root->fs_info->subvol_sem);
1572 return ret;
1573}
1574
Yan, Zheng76dda932009-09-21 16:00:26 -04001575/*
1576 * helper to check if the subvolume references other subvolumes
1577 */
1578static noinline int may_destroy_subvol(struct btrfs_root *root)
1579{
1580 struct btrfs_path *path;
1581 struct btrfs_key key;
1582 int ret;
1583
1584 path = btrfs_alloc_path();
1585 if (!path)
1586 return -ENOMEM;
1587
1588 key.objectid = root->root_key.objectid;
1589 key.type = BTRFS_ROOT_REF_KEY;
1590 key.offset = (u64)-1;
1591
1592 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1593 &key, path, 0, 0);
1594 if (ret < 0)
1595 goto out;
1596 BUG_ON(ret == 0);
1597
1598 ret = 0;
1599 if (path->slots[0] > 0) {
1600 path->slots[0]--;
1601 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1602 if (key.objectid == root->root_key.objectid &&
1603 key.type == BTRFS_ROOT_REF_KEY)
1604 ret = -ENOTEMPTY;
1605 }
1606out:
1607 btrfs_free_path(path);
1608 return ret;
1609}
1610
Chris Masonac8e9812010-02-28 15:39:26 -05001611static noinline int key_in_sk(struct btrfs_key *key,
1612 struct btrfs_ioctl_search_key *sk)
1613{
Chris Masonabc6e132010-03-18 12:10:08 -04001614 struct btrfs_key test;
1615 int ret;
1616
1617 test.objectid = sk->min_objectid;
1618 test.type = sk->min_type;
1619 test.offset = sk->min_offset;
1620
1621 ret = btrfs_comp_cpu_keys(key, &test);
1622 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001623 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001624
1625 test.objectid = sk->max_objectid;
1626 test.type = sk->max_type;
1627 test.offset = sk->max_offset;
1628
1629 ret = btrfs_comp_cpu_keys(key, &test);
1630 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001631 return 0;
1632 return 1;
1633}
1634
1635static noinline int copy_to_sk(struct btrfs_root *root,
1636 struct btrfs_path *path,
1637 struct btrfs_key *key,
1638 struct btrfs_ioctl_search_key *sk,
1639 char *buf,
1640 unsigned long *sk_offset,
1641 int *num_found)
1642{
1643 u64 found_transid;
1644 struct extent_buffer *leaf;
1645 struct btrfs_ioctl_search_header sh;
1646 unsigned long item_off;
1647 unsigned long item_len;
1648 int nritems;
1649 int i;
1650 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001651 int ret = 0;
1652
1653 leaf = path->nodes[0];
1654 slot = path->slots[0];
1655 nritems = btrfs_header_nritems(leaf);
1656
1657 if (btrfs_header_generation(leaf) > sk->max_transid) {
1658 i = nritems;
1659 goto advance_key;
1660 }
1661 found_transid = btrfs_header_generation(leaf);
1662
1663 for (i = slot; i < nritems; i++) {
1664 item_off = btrfs_item_ptr_offset(leaf, i);
1665 item_len = btrfs_item_size_nr(leaf, i);
1666
1667 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1668 item_len = 0;
1669
1670 if (sizeof(sh) + item_len + *sk_offset >
1671 BTRFS_SEARCH_ARGS_BUFSIZE) {
1672 ret = 1;
1673 goto overflow;
1674 }
1675
1676 btrfs_item_key_to_cpu(leaf, key, i);
1677 if (!key_in_sk(key, sk))
1678 continue;
1679
1680 sh.objectid = key->objectid;
1681 sh.offset = key->offset;
1682 sh.type = key->type;
1683 sh.len = item_len;
1684 sh.transid = found_transid;
1685
1686 /* copy search result header */
1687 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1688 *sk_offset += sizeof(sh);
1689
1690 if (item_len) {
1691 char *p = buf + *sk_offset;
1692 /* copy the item */
1693 read_extent_buffer(leaf, p,
1694 item_off, item_len);
1695 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001696 }
Hugo Millse2156862011-05-14 17:43:41 +00001697 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001698
1699 if (*num_found >= sk->nr_items)
1700 break;
1701 }
1702advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001703 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001704 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1705 key->offset++;
1706 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1707 key->offset = 0;
1708 key->type++;
1709 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1710 key->offset = 0;
1711 key->type = 0;
1712 key->objectid++;
1713 } else
1714 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001715overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001716 return ret;
1717}
1718
1719static noinline int search_ioctl(struct inode *inode,
1720 struct btrfs_ioctl_search_args *args)
1721{
1722 struct btrfs_root *root;
1723 struct btrfs_key key;
1724 struct btrfs_key max_key;
1725 struct btrfs_path *path;
1726 struct btrfs_ioctl_search_key *sk = &args->key;
1727 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1728 int ret;
1729 int num_found = 0;
1730 unsigned long sk_offset = 0;
1731
1732 path = btrfs_alloc_path();
1733 if (!path)
1734 return -ENOMEM;
1735
1736 if (sk->tree_id == 0) {
1737 /* search the root of the inode that was passed */
1738 root = BTRFS_I(inode)->root;
1739 } else {
1740 key.objectid = sk->tree_id;
1741 key.type = BTRFS_ROOT_ITEM_KEY;
1742 key.offset = (u64)-1;
1743 root = btrfs_read_fs_root_no_name(info, &key);
1744 if (IS_ERR(root)) {
1745 printk(KERN_ERR "could not find root %llu\n",
1746 sk->tree_id);
1747 btrfs_free_path(path);
1748 return -ENOENT;
1749 }
1750 }
1751
1752 key.objectid = sk->min_objectid;
1753 key.type = sk->min_type;
1754 key.offset = sk->min_offset;
1755
1756 max_key.objectid = sk->max_objectid;
1757 max_key.type = sk->max_type;
1758 max_key.offset = sk->max_offset;
1759
1760 path->keep_locks = 1;
1761
1762 while(1) {
1763 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1764 sk->min_transid);
1765 if (ret != 0) {
1766 if (ret > 0)
1767 ret = 0;
1768 goto err;
1769 }
1770 ret = copy_to_sk(root, path, &key, sk, args->buf,
1771 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001772 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001773 if (ret || num_found >= sk->nr_items)
1774 break;
1775
1776 }
1777 ret = 0;
1778err:
1779 sk->nr_items = num_found;
1780 btrfs_free_path(path);
1781 return ret;
1782}
1783
1784static noinline int btrfs_ioctl_tree_search(struct file *file,
1785 void __user *argp)
1786{
1787 struct btrfs_ioctl_search_args *args;
1788 struct inode *inode;
1789 int ret;
1790
1791 if (!capable(CAP_SYS_ADMIN))
1792 return -EPERM;
1793
Julia Lawall2354d08f2010-10-29 15:14:18 -04001794 args = memdup_user(argp, sizeof(*args));
1795 if (IS_ERR(args))
1796 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001797
Chris Masonac8e9812010-02-28 15:39:26 -05001798 inode = fdentry(file)->d_inode;
1799 ret = search_ioctl(inode, args);
1800 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1801 ret = -EFAULT;
1802 kfree(args);
1803 return ret;
1804}
1805
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001806/*
Chris Masonac8e9812010-02-28 15:39:26 -05001807 * Search INODE_REFs to identify path name of 'dirid' directory
1808 * in a 'tree_id' tree. and sets path name to 'name'.
1809 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001810static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1811 u64 tree_id, u64 dirid, char *name)
1812{
1813 struct btrfs_root *root;
1814 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001815 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001816 int ret = -1;
1817 int slot;
1818 int len;
1819 int total_len = 0;
1820 struct btrfs_inode_ref *iref;
1821 struct extent_buffer *l;
1822 struct btrfs_path *path;
1823
1824 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1825 name[0]='\0';
1826 return 0;
1827 }
1828
1829 path = btrfs_alloc_path();
1830 if (!path)
1831 return -ENOMEM;
1832
Chris Masonac8e9812010-02-28 15:39:26 -05001833 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001834
1835 key.objectid = tree_id;
1836 key.type = BTRFS_ROOT_ITEM_KEY;
1837 key.offset = (u64)-1;
1838 root = btrfs_read_fs_root_no_name(info, &key);
1839 if (IS_ERR(root)) {
1840 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001841 ret = -ENOENT;
1842 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001843 }
1844
1845 key.objectid = dirid;
1846 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001847 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001848
1849 while(1) {
1850 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1851 if (ret < 0)
1852 goto out;
1853
1854 l = path->nodes[0];
1855 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001856 if (ret > 0 && slot > 0)
1857 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001858 btrfs_item_key_to_cpu(l, &key, slot);
1859
1860 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001861 key.type != BTRFS_INODE_REF_KEY)) {
1862 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001863 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001864 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001865
1866 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1867 len = btrfs_inode_ref_name_len(l, iref);
1868 ptr -= len + 1;
1869 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001870 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001871 goto out;
1872
1873 *(ptr + len) = '/';
1874 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1875
1876 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1877 break;
1878
David Sterbab3b4aa72011-04-21 01:20:15 +02001879 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001880 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001881 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001882 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001883 }
Chris Masonac8e9812010-02-28 15:39:26 -05001884 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001885 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001886 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001887 name[total_len]='\0';
1888 ret = 0;
1889out:
1890 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001891 return ret;
1892}
1893
1894static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1895 void __user *argp)
1896{
1897 struct btrfs_ioctl_ino_lookup_args *args;
1898 struct inode *inode;
1899 int ret;
1900
1901 if (!capable(CAP_SYS_ADMIN))
1902 return -EPERM;
1903
Julia Lawall2354d08f2010-10-29 15:14:18 -04001904 args = memdup_user(argp, sizeof(*args));
1905 if (IS_ERR(args))
1906 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001907
Chris Masonac8e9812010-02-28 15:39:26 -05001908 inode = fdentry(file)->d_inode;
1909
Chris Mason1b53ac42010-03-18 12:17:05 -04001910 if (args->treeid == 0)
1911 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1912
Chris Masonac8e9812010-02-28 15:39:26 -05001913 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1914 args->treeid, args->objectid,
1915 args->name);
1916
1917 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1918 ret = -EFAULT;
1919
1920 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001921 return ret;
1922}
1923
Yan, Zheng76dda932009-09-21 16:00:26 -04001924static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1925 void __user *arg)
1926{
1927 struct dentry *parent = fdentry(file);
1928 struct dentry *dentry;
1929 struct inode *dir = parent->d_inode;
1930 struct inode *inode;
1931 struct btrfs_root *root = BTRFS_I(dir)->root;
1932 struct btrfs_root *dest = NULL;
1933 struct btrfs_ioctl_vol_args *vol_args;
1934 struct btrfs_trans_handle *trans;
1935 int namelen;
1936 int ret;
1937 int err = 0;
1938
Yan, Zheng76dda932009-09-21 16:00:26 -04001939 vol_args = memdup_user(arg, sizeof(*vol_args));
1940 if (IS_ERR(vol_args))
1941 return PTR_ERR(vol_args);
1942
1943 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1944 namelen = strlen(vol_args->name);
1945 if (strchr(vol_args->name, '/') ||
1946 strncmp(vol_args->name, "..", namelen) == 0) {
1947 err = -EINVAL;
1948 goto out;
1949 }
1950
Al Viroa561be72011-11-23 11:57:51 -05001951 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001952 if (err)
1953 goto out;
1954
1955 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1956 dentry = lookup_one_len(vol_args->name, parent, namelen);
1957 if (IS_ERR(dentry)) {
1958 err = PTR_ERR(dentry);
1959 goto out_unlock_dir;
1960 }
1961
1962 if (!dentry->d_inode) {
1963 err = -ENOENT;
1964 goto out_dput;
1965 }
1966
1967 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001968 dest = BTRFS_I(inode)->root;
1969 if (!capable(CAP_SYS_ADMIN)){
1970 /*
1971 * Regular user. Only allow this with a special mount
1972 * option, when the user has write+exec access to the
1973 * subvol root, and when rmdir(2) would have been
1974 * allowed.
1975 *
1976 * Note that this is _not_ check that the subvol is
1977 * empty or doesn't contain data that we wouldn't
1978 * otherwise be able to delete.
1979 *
1980 * Users who want to delete empty subvols should try
1981 * rmdir(2).
1982 */
1983 err = -EPERM;
1984 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1985 goto out_dput;
1986
1987 /*
1988 * Do not allow deletion if the parent dir is the same
1989 * as the dir to be deleted. That means the ioctl
1990 * must be called on the dentry referencing the root
1991 * of the subvol, not a random directory contained
1992 * within it.
1993 */
1994 err = -EINVAL;
1995 if (root == dest)
1996 goto out_dput;
1997
1998 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1999 if (err)
2000 goto out_dput;
2001
2002 /* check if subvolume may be deleted by a non-root user */
2003 err = btrfs_may_delete(dir, dentry, 1);
2004 if (err)
2005 goto out_dput;
2006 }
2007
Li Zefan33345d012011-04-20 10:31:50 +08002008 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002009 err = -EINVAL;
2010 goto out_dput;
2011 }
2012
Yan, Zheng76dda932009-09-21 16:00:26 -04002013 mutex_lock(&inode->i_mutex);
2014 err = d_invalidate(dentry);
2015 if (err)
2016 goto out_unlock;
2017
2018 down_write(&root->fs_info->subvol_sem);
2019
2020 err = may_destroy_subvol(dest);
2021 if (err)
2022 goto out_up_write;
2023
Yan, Zhenga22285a2010-05-16 10:48:46 -04002024 trans = btrfs_start_transaction(root, 0);
2025 if (IS_ERR(trans)) {
2026 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002027 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002028 }
2029 trans->block_rsv = &root->fs_info->global_block_rsv;
2030
Yan, Zheng76dda932009-09-21 16:00:26 -04002031 ret = btrfs_unlink_subvol(trans, root, dir,
2032 dest->root_key.objectid,
2033 dentry->d_name.name,
2034 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002035 if (ret) {
2036 err = ret;
2037 btrfs_abort_transaction(trans, root, ret);
2038 goto out_end_trans;
2039 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002040
2041 btrfs_record_root_in_trans(trans, dest);
2042
2043 memset(&dest->root_item.drop_progress, 0,
2044 sizeof(dest->root_item.drop_progress));
2045 dest->root_item.drop_level = 0;
2046 btrfs_set_root_refs(&dest->root_item, 0);
2047
Yan, Zhengd68fc572010-05-16 10:49:58 -04002048 if (!xchg(&dest->orphan_item_inserted, 1)) {
2049 ret = btrfs_insert_orphan_item(trans,
2050 root->fs_info->tree_root,
2051 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002052 if (ret) {
2053 btrfs_abort_transaction(trans, root, ret);
2054 err = ret;
2055 goto out_end_trans;
2056 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002057 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002058out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002059 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002060 if (ret && !err)
2061 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002062 inode->i_flags |= S_DEAD;
2063out_up_write:
2064 up_write(&root->fs_info->subvol_sem);
2065out_unlock:
2066 mutex_unlock(&inode->i_mutex);
2067 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002068 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002069 btrfs_invalidate_inodes(dest);
2070 d_delete(dentry);
2071 }
2072out_dput:
2073 dput(dentry);
2074out_unlock_dir:
2075 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002076 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002077out:
2078 kfree(vol_args);
2079 return err;
2080}
2081
Chris Mason1e701a32010-03-11 09:42:04 -05002082static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002083{
2084 struct inode *inode = fdentry(file)->d_inode;
2085 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002086 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002087 int ret;
2088
Li Zefanb83cc962010-12-20 16:04:08 +08002089 if (btrfs_root_readonly(root))
2090 return -EROFS;
2091
Al Viroa561be72011-11-23 11:57:51 -05002092 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002093 if (ret)
2094 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002095
2096 switch (inode->i_mode & S_IFMT) {
2097 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002098 if (!capable(CAP_SYS_ADMIN)) {
2099 ret = -EPERM;
2100 goto out;
2101 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002102 ret = btrfs_defrag_root(root, 0);
2103 if (ret)
2104 goto out;
2105 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002106 break;
2107 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002108 if (!(file->f_mode & FMODE_WRITE)) {
2109 ret = -EINVAL;
2110 goto out;
2111 }
Chris Mason1e701a32010-03-11 09:42:04 -05002112
2113 range = kzalloc(sizeof(*range), GFP_KERNEL);
2114 if (!range) {
2115 ret = -ENOMEM;
2116 goto out;
2117 }
2118
2119 if (argp) {
2120 if (copy_from_user(range, argp,
2121 sizeof(*range))) {
2122 ret = -EFAULT;
2123 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002124 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002125 }
2126 /* compression requires us to start the IO */
2127 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2128 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2129 range->extent_thresh = (u32)-1;
2130 }
2131 } else {
2132 /* the rest are all set to zero by kzalloc */
2133 range->len = (u64)-1;
2134 }
Chris Mason4cb53002011-05-24 15:35:30 -04002135 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2136 range, 0, 0);
2137 if (ret > 0)
2138 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002139 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002140 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002141 default:
2142 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002143 }
Chris Masone441d542009-01-05 16:57:23 -05002144out:
Al Viro2a79f172011-12-09 08:06:57 -05002145 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002146 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002147}
2148
Christoph Hellwigb2950862008-12-02 09:54:17 -05002149static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002150{
2151 struct btrfs_ioctl_vol_args *vol_args;
2152 int ret;
2153
Chris Masone441d542009-01-05 16:57:23 -05002154 if (!capable(CAP_SYS_ADMIN))
2155 return -EPERM;
2156
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002157 mutex_lock(&root->fs_info->volume_mutex);
2158 if (root->fs_info->balance_ctl) {
2159 printk(KERN_INFO "btrfs: balance in progress\n");
2160 ret = -EINVAL;
2161 goto out;
2162 }
2163
Li Zefandae7b662009-04-08 15:06:54 +08002164 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002165 if (IS_ERR(vol_args)) {
2166 ret = PTR_ERR(vol_args);
2167 goto out;
2168 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002169
Mark Fasheh5516e592008-07-24 12:20:14 -04002170 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002171 ret = btrfs_init_new_device(root, vol_args->name);
2172
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002173 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002174out:
2175 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002176 return ret;
2177}
2178
Christoph Hellwigb2950862008-12-02 09:54:17 -05002179static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002180{
2181 struct btrfs_ioctl_vol_args *vol_args;
2182 int ret;
2183
Chris Masone441d542009-01-05 16:57:23 -05002184 if (!capable(CAP_SYS_ADMIN))
2185 return -EPERM;
2186
Yan Zhengc146afa2008-11-12 14:34:12 -05002187 if (root->fs_info->sb->s_flags & MS_RDONLY)
2188 return -EROFS;
2189
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002190 mutex_lock(&root->fs_info->volume_mutex);
2191 if (root->fs_info->balance_ctl) {
2192 printk(KERN_INFO "btrfs: balance in progress\n");
2193 ret = -EINVAL;
2194 goto out;
2195 }
2196
Li Zefandae7b662009-04-08 15:06:54 +08002197 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002198 if (IS_ERR(vol_args)) {
2199 ret = PTR_ERR(vol_args);
2200 goto out;
2201 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002202
Mark Fasheh5516e592008-07-24 12:20:14 -04002203 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002204 ret = btrfs_rm_device(root, vol_args->name);
2205
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002206 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002207out:
2208 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002209 return ret;
2210}
2211
Jan Schmidt475f6382011-03-11 15:41:01 +01002212static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2213{
Li Zefan027ed2f2011-06-08 08:27:56 +00002214 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002215 struct btrfs_device *device;
2216 struct btrfs_device *next;
2217 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002218 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002219
2220 if (!capable(CAP_SYS_ADMIN))
2221 return -EPERM;
2222
Li Zefan027ed2f2011-06-08 08:27:56 +00002223 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2224 if (!fi_args)
2225 return -ENOMEM;
2226
2227 fi_args->num_devices = fs_devices->num_devices;
2228 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002229
2230 mutex_lock(&fs_devices->device_list_mutex);
2231 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002232 if (device->devid > fi_args->max_id)
2233 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002234 }
2235 mutex_unlock(&fs_devices->device_list_mutex);
2236
Li Zefan027ed2f2011-06-08 08:27:56 +00002237 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2238 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002239
Li Zefan027ed2f2011-06-08 08:27:56 +00002240 kfree(fi_args);
2241 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002242}
2243
2244static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2245{
2246 struct btrfs_ioctl_dev_info_args *di_args;
2247 struct btrfs_device *dev;
2248 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2249 int ret = 0;
2250 char *s_uuid = NULL;
2251 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2252
2253 if (!capable(CAP_SYS_ADMIN))
2254 return -EPERM;
2255
2256 di_args = memdup_user(arg, sizeof(*di_args));
2257 if (IS_ERR(di_args))
2258 return PTR_ERR(di_args);
2259
2260 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2261 s_uuid = di_args->uuid;
2262
2263 mutex_lock(&fs_devices->device_list_mutex);
2264 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2265 mutex_unlock(&fs_devices->device_list_mutex);
2266
2267 if (!dev) {
2268 ret = -ENODEV;
2269 goto out;
2270 }
2271
2272 di_args->devid = dev->devid;
2273 di_args->bytes_used = dev->bytes_used;
2274 di_args->total_bytes = dev->total_bytes;
2275 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002276 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002277 struct rcu_string *name;
2278
2279 rcu_read_lock();
2280 name = rcu_dereference(dev->name);
2281 strncpy(di_args->path, name->str, sizeof(di_args->path));
2282 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002283 di_args->path[sizeof(di_args->path) - 1] = 0;
2284 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002285 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002286 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002287
2288out:
2289 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2290 ret = -EFAULT;
2291
2292 kfree(di_args);
2293 return ret;
2294}
2295
Yan, Zheng76dda932009-09-21 16:00:26 -04002296static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2297 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002298{
2299 struct inode *inode = fdentry(file)->d_inode;
2300 struct btrfs_root *root = BTRFS_I(inode)->root;
2301 struct file *src_file;
2302 struct inode *src;
2303 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002304 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002305 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002306 char *buf;
2307 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002308 u32 nritems;
2309 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002310 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002311 u64 len = olen;
2312 u64 bs = root->fs_info->sb->s_blocksize;
2313 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002314
Sage Weilc5c9cd42008-11-12 14:32:25 -05002315 /*
2316 * TODO:
2317 * - split compressed inline extents. annoying: we need to
2318 * decompress into destination's address_space (the file offset
2319 * may change, so source mapping won't do), then recompress (or
2320 * otherwise reinsert) a subrange.
2321 * - allow ranges within the same file to be cloned (provided
2322 * they don't overlap)?
2323 */
2324
Chris Masone441d542009-01-05 16:57:23 -05002325 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002326 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002327 return -EINVAL;
2328
Li Zefanb83cc962010-12-20 16:04:08 +08002329 if (btrfs_root_readonly(root))
2330 return -EROFS;
2331
Al Viroa561be72011-11-23 11:57:51 -05002332 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002333 if (ret)
2334 return ret;
2335
Sage Weilc5c9cd42008-11-12 14:32:25 -05002336 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002337 if (!src_file) {
2338 ret = -EBADF;
2339 goto out_drop_write;
2340 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002341
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002342 src = src_file->f_dentry->d_inode;
2343
Sage Weilc5c9cd42008-11-12 14:32:25 -05002344 ret = -EINVAL;
2345 if (src == inode)
2346 goto out_fput;
2347
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002348 /* the src must be open for reading */
2349 if (!(src_file->f_mode & FMODE_READ))
2350 goto out_fput;
2351
Li Zefan0e7b8242011-09-18 10:20:46 -04002352 /* don't make the dst file partly checksummed */
2353 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2354 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2355 goto out_fput;
2356
Yan Zhengae01a0a2008-08-04 23:23:47 -04002357 ret = -EISDIR;
2358 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002359 goto out_fput;
2360
Yan Zhengae01a0a2008-08-04 23:23:47 -04002361 ret = -EXDEV;
2362 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2363 goto out_fput;
2364
2365 ret = -ENOMEM;
2366 buf = vmalloc(btrfs_level_size(root, 0));
2367 if (!buf)
2368 goto out_fput;
2369
2370 path = btrfs_alloc_path();
2371 if (!path) {
2372 vfree(buf);
2373 goto out_fput;
2374 }
2375 path->reada = 2;
2376
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002377 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002378 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2379 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002380 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002381 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2382 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002383 }
2384
Sage Weilc5c9cd42008-11-12 14:32:25 -05002385 /* determine range to clone */
2386 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002387 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002388 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002389 if (len == 0)
2390 olen = len = src->i_size - off;
2391 /* if we extend to eof, continue to block boundary */
2392 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002393 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002394
2395 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002396 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2397 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002398 goto out_unlock;
2399
Li Zefand525e8a2011-09-11 10:52:25 -04002400 if (destoff > inode->i_size) {
2401 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2402 if (ret)
2403 goto out_unlock;
2404 }
2405
Li Zefan71ef0782011-09-18 10:20:46 -04002406 /* truncate page cache pages from target inode range */
2407 truncate_inode_pages_range(&inode->i_data, destoff,
2408 PAGE_CACHE_ALIGN(destoff + len) - 1);
2409
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410 /* do any pending delalloc/csum calc on src, one way or
2411 another, and lock file content */
2412 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002413 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002414 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002415 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2416 if (!ordered &&
2417 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2418 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002419 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002420 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002421 if (ordered)
2422 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002423 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002424 }
2425
Sage Weilc5c9cd42008-11-12 14:32:25 -05002426 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002427 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002428 key.type = BTRFS_EXTENT_DATA_KEY;
2429 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002430
2431 while (1) {
2432 /*
2433 * note the key will change type as we walk through the
2434 * tree.
2435 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002436 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002437 if (ret < 0)
2438 goto out;
2439
Yan Zhengae01a0a2008-08-04 23:23:47 -04002440 nritems = btrfs_header_nritems(path->nodes[0]);
2441 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002442 ret = btrfs_next_leaf(root, path);
2443 if (ret < 0)
2444 goto out;
2445 if (ret > 0)
2446 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002447 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002448 }
2449 leaf = path->nodes[0];
2450 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002451
Yan Zhengae01a0a2008-08-04 23:23:47 -04002452 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002453 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002454 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002455 break;
2456
Sage Weilc5c9cd42008-11-12 14:32:25 -05002457 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2458 struct btrfs_file_extent_item *extent;
2459 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 u32 size;
2461 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002462 u64 disko = 0, diskl = 0;
2463 u64 datao = 0, datal = 0;
2464 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002465 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002466
2467 size = btrfs_item_size_nr(leaf, slot);
2468 read_extent_buffer(leaf, buf,
2469 btrfs_item_ptr_offset(leaf, slot),
2470 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002471
2472 extent = btrfs_item_ptr(leaf, slot,
2473 struct btrfs_file_extent_item);
2474 comp = btrfs_file_extent_compression(leaf, extent);
2475 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002476 if (type == BTRFS_FILE_EXTENT_REG ||
2477 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002478 disko = btrfs_file_extent_disk_bytenr(leaf,
2479 extent);
2480 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2481 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002482 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002483 datal = btrfs_file_extent_num_bytes(leaf,
2484 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002485 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2486 /* take upper bound, may be compressed */
2487 datal = btrfs_file_extent_ram_bytes(leaf,
2488 extent);
2489 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002490 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002491
Sage Weil050006a2010-10-29 15:37:33 -04002492 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002493 key.offset >= off+len)
2494 goto next;
2495
Zheng Yan31840ae2008-09-23 13:14:14 -04002496 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002497 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002498 if (off <= key.offset)
2499 new_key.offset = key.offset + destoff - off;
2500 else
2501 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002502
Sage Weilb6f34092011-09-20 14:48:51 -04002503 /*
2504 * 1 - adjusting old extent (we may have to split it)
2505 * 1 - add new extent
2506 * 1 - inode update
2507 */
2508 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002509 if (IS_ERR(trans)) {
2510 ret = PTR_ERR(trans);
2511 goto out;
2512 }
2513
Chris Masonc8a894d2009-06-27 21:07:03 -04002514 if (type == BTRFS_FILE_EXTENT_REG ||
2515 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002516 /*
2517 * a | --- range to clone ---| b
2518 * | ------------- extent ------------- |
2519 */
2520
2521 /* substract range b */
2522 if (key.offset + datal > off + len)
2523 datal = off + len - key.offset;
2524
2525 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002526 if (off > key.offset) {
2527 datao += off - key.offset;
2528 datal -= off - key.offset;
2529 }
2530
Yan, Zhenga22285a2010-05-16 10:48:46 -04002531 ret = btrfs_drop_extents(trans, inode,
2532 new_key.offset,
2533 new_key.offset + datal,
2534 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002535 if (ret) {
2536 btrfs_abort_transaction(trans, root,
2537 ret);
2538 btrfs_end_transaction(trans, root);
2539 goto out;
2540 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002541
Sage Weilc5c9cd42008-11-12 14:32:25 -05002542 ret = btrfs_insert_empty_item(trans, root, path,
2543 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002544 if (ret) {
2545 btrfs_abort_transaction(trans, root,
2546 ret);
2547 btrfs_end_transaction(trans, root);
2548 goto out;
2549 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002550
2551 leaf = path->nodes[0];
2552 slot = path->slots[0];
2553 write_extent_buffer(leaf, buf,
2554 btrfs_item_ptr_offset(leaf, slot),
2555 size);
2556
2557 extent = btrfs_item_ptr(leaf, slot,
2558 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002559
Sage Weilc5c9cd42008-11-12 14:32:25 -05002560 /* disko == 0 means it's a hole */
2561 if (!disko)
2562 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002563
2564 btrfs_set_file_extent_offset(leaf, extent,
2565 datao);
2566 btrfs_set_file_extent_num_bytes(leaf, extent,
2567 datal);
2568 if (disko) {
2569 inode_add_bytes(inode, datal);
2570 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002571 disko, diskl, 0,
2572 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002573 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002574 new_key.offset - datao,
2575 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002576 if (ret) {
2577 btrfs_abort_transaction(trans,
2578 root,
2579 ret);
2580 btrfs_end_transaction(trans,
2581 root);
2582 goto out;
2583
2584 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002585 }
2586 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2587 u64 skip = 0;
2588 u64 trim = 0;
2589 if (off > key.offset) {
2590 skip = off - key.offset;
2591 new_key.offset += skip;
2592 }
Chris Masond3977122009-01-05 21:25:51 -05002593
Sage Weilc5c9cd42008-11-12 14:32:25 -05002594 if (key.offset + datal > off+len)
2595 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002596
Sage Weilc5c9cd42008-11-12 14:32:25 -05002597 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002598 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002599 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002600 goto out;
2601 }
2602 size -= skip + trim;
2603 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002604
2605 ret = btrfs_drop_extents(trans, inode,
2606 new_key.offset,
2607 new_key.offset + datal,
2608 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002609 if (ret) {
2610 btrfs_abort_transaction(trans, root,
2611 ret);
2612 btrfs_end_transaction(trans, root);
2613 goto out;
2614 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002615
Sage Weilc5c9cd42008-11-12 14:32:25 -05002616 ret = btrfs_insert_empty_item(trans, root, path,
2617 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002618 if (ret) {
2619 btrfs_abort_transaction(trans, root,
2620 ret);
2621 btrfs_end_transaction(trans, root);
2622 goto out;
2623 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002624
2625 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002626 u32 start =
2627 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002628 memmove(buf+start, buf+start+skip,
2629 datal);
2630 }
2631
2632 leaf = path->nodes[0];
2633 slot = path->slots[0];
2634 write_extent_buffer(leaf, buf,
2635 btrfs_item_ptr_offset(leaf, slot),
2636 size);
2637 inode_add_bytes(inode, datal);
2638 }
2639
2640 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002641 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002642
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002643 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002644 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002645
2646 /*
2647 * we round up to the block size at eof when
2648 * determining which extents to clone above,
2649 * but shouldn't round up the file size
2650 */
2651 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002652 if (endoff > destoff+olen)
2653 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002654 if (endoff > inode->i_size)
2655 btrfs_i_size_write(inode, endoff);
2656
Yan, Zhenga22285a2010-05-16 10:48:46 -04002657 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002658 if (ret) {
2659 btrfs_abort_transaction(trans, root, ret);
2660 btrfs_end_transaction(trans, root);
2661 goto out;
2662 }
2663 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002664 }
Chris Masond3977122009-01-05 21:25:51 -05002665next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002666 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002667 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002668 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002669 ret = 0;
2670out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002671 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002672 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002673out_unlock:
2674 mutex_unlock(&src->i_mutex);
2675 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002676 vfree(buf);
2677 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678out_fput:
2679 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002680out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002681 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002682 return ret;
2683}
2684
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002685static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002686{
2687 struct btrfs_ioctl_clone_range_args args;
2688
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002689 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002690 return -EFAULT;
2691 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2692 args.src_length, args.dest_offset);
2693}
2694
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002695/*
2696 * there are many ways the trans_start and trans_end ioctls can lead
2697 * to deadlocks. They should only be used by applications that
2698 * basically own the machine, and have a very in depth understanding
2699 * of all the possible deadlocks and enospc problems.
2700 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002701static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002702{
2703 struct inode *inode = fdentry(file)->d_inode;
2704 struct btrfs_root *root = BTRFS_I(inode)->root;
2705 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002706 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002707
Sage Weil1ab86ae2009-09-29 18:38:44 -04002708 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002709 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002710 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002711
2712 ret = -EINPROGRESS;
2713 if (file->private_data)
2714 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002715
Li Zefanb83cc962010-12-20 16:04:08 +08002716 ret = -EROFS;
2717 if (btrfs_root_readonly(root))
2718 goto out;
2719
Al Viroa561be72011-11-23 11:57:51 -05002720 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002721 if (ret)
2722 goto out;
2723
Josef Bacika4abeea2011-04-11 17:25:13 -04002724 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002725
Sage Weil1ab86ae2009-09-29 18:38:44 -04002726 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002727 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002728 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002729 goto out_drop;
2730
2731 file->private_data = trans;
2732 return 0;
2733
2734out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002735 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002736 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002737out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002738 return ret;
2739}
2740
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002741static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2742{
2743 struct inode *inode = fdentry(file)->d_inode;
2744 struct btrfs_root *root = BTRFS_I(inode)->root;
2745 struct btrfs_root *new_root;
2746 struct btrfs_dir_item *di;
2747 struct btrfs_trans_handle *trans;
2748 struct btrfs_path *path;
2749 struct btrfs_key location;
2750 struct btrfs_disk_key disk_key;
2751 struct btrfs_super_block *disk_super;
2752 u64 features;
2753 u64 objectid = 0;
2754 u64 dir_id;
2755
2756 if (!capable(CAP_SYS_ADMIN))
2757 return -EPERM;
2758
2759 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2760 return -EFAULT;
2761
2762 if (!objectid)
2763 objectid = root->root_key.objectid;
2764
2765 location.objectid = objectid;
2766 location.type = BTRFS_ROOT_ITEM_KEY;
2767 location.offset = (u64)-1;
2768
2769 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2770 if (IS_ERR(new_root))
2771 return PTR_ERR(new_root);
2772
2773 if (btrfs_root_refs(&new_root->root_item) == 0)
2774 return -ENOENT;
2775
2776 path = btrfs_alloc_path();
2777 if (!path)
2778 return -ENOMEM;
2779 path->leave_spinning = 1;
2780
2781 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002782 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002783 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002784 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002785 }
2786
David Sterba6c417612011-04-13 15:41:04 +02002787 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002788 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2789 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002790 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002791 btrfs_free_path(path);
2792 btrfs_end_transaction(trans, root);
2793 printk(KERN_ERR "Umm, you don't have the default dir item, "
2794 "this isn't going to work\n");
2795 return -ENOENT;
2796 }
2797
2798 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2799 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2800 btrfs_mark_buffer_dirty(path->nodes[0]);
2801 btrfs_free_path(path);
2802
David Sterba6c417612011-04-13 15:41:04 +02002803 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002804 features = btrfs_super_incompat_flags(disk_super);
2805 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2806 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2807 btrfs_set_super_incompat_flags(disk_super, features);
2808 }
2809 btrfs_end_transaction(trans, root);
2810
2811 return 0;
2812}
2813
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002814static void get_block_group_info(struct list_head *groups_list,
2815 struct btrfs_ioctl_space_info *space)
2816{
2817 struct btrfs_block_group_cache *block_group;
2818
2819 space->total_bytes = 0;
2820 space->used_bytes = 0;
2821 space->flags = 0;
2822 list_for_each_entry(block_group, groups_list, list) {
2823 space->flags = block_group->flags;
2824 space->total_bytes += block_group->key.offset;
2825 space->used_bytes +=
2826 btrfs_block_group_used(&block_group->item);
2827 }
2828}
2829
Josef Bacik1406e432010-01-13 18:19:06 +00002830long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2831{
2832 struct btrfs_ioctl_space_args space_args;
2833 struct btrfs_ioctl_space_info space;
2834 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002835 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002836 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002837 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002838 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2839 BTRFS_BLOCK_GROUP_SYSTEM,
2840 BTRFS_BLOCK_GROUP_METADATA,
2841 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2842 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002843 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002844 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002845 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002846 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002847
2848 if (copy_from_user(&space_args,
2849 (struct btrfs_ioctl_space_args __user *)arg,
2850 sizeof(space_args)))
2851 return -EFAULT;
2852
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002853 for (i = 0; i < num_types; i++) {
2854 struct btrfs_space_info *tmp;
2855
2856 info = NULL;
2857 rcu_read_lock();
2858 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2859 list) {
2860 if (tmp->flags == types[i]) {
2861 info = tmp;
2862 break;
2863 }
2864 }
2865 rcu_read_unlock();
2866
2867 if (!info)
2868 continue;
2869
2870 down_read(&info->groups_sem);
2871 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2872 if (!list_empty(&info->block_groups[c]))
2873 slot_count++;
2874 }
2875 up_read(&info->groups_sem);
2876 }
Josef Bacik1406e432010-01-13 18:19:06 +00002877
Chris Mason7fde62b2010-03-16 15:40:10 -04002878 /* space_slots == 0 means they are asking for a count */
2879 if (space_args.space_slots == 0) {
2880 space_args.total_spaces = slot_count;
2881 goto out;
2882 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002883
Dan Rosenberg51788b12011-02-14 16:04:23 -05002884 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002885
Chris Mason7fde62b2010-03-16 15:40:10 -04002886 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002887
Chris Mason7fde62b2010-03-16 15:40:10 -04002888 /* we generally have at most 6 or so space infos, one for each raid
2889 * level. So, a whole page should be more than enough for everyone
2890 */
2891 if (alloc_size > PAGE_CACHE_SIZE)
2892 return -ENOMEM;
2893
2894 space_args.total_spaces = 0;
2895 dest = kmalloc(alloc_size, GFP_NOFS);
2896 if (!dest)
2897 return -ENOMEM;
2898 dest_orig = dest;
2899
2900 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002901 for (i = 0; i < num_types; i++) {
2902 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002903
Dan Rosenberg51788b12011-02-14 16:04:23 -05002904 if (!slot_count)
2905 break;
2906
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002907 info = NULL;
2908 rcu_read_lock();
2909 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2910 list) {
2911 if (tmp->flags == types[i]) {
2912 info = tmp;
2913 break;
2914 }
2915 }
2916 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002917
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002918 if (!info)
2919 continue;
2920 down_read(&info->groups_sem);
2921 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2922 if (!list_empty(&info->block_groups[c])) {
2923 get_block_group_info(&info->block_groups[c],
2924 &space);
2925 memcpy(dest, &space, sizeof(space));
2926 dest++;
2927 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002928 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002929 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002930 if (!slot_count)
2931 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002932 }
2933 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002934 }
Josef Bacik1406e432010-01-13 18:19:06 +00002935
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08002936 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04002937 (arg + sizeof(struct btrfs_ioctl_space_args));
2938
2939 if (copy_to_user(user_dest, dest_orig, alloc_size))
2940 ret = -EFAULT;
2941
2942 kfree(dest_orig);
2943out:
2944 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002945 ret = -EFAULT;
2946
2947 return ret;
2948}
2949
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002950/*
2951 * there are many ways the trans_start and trans_end ioctls can lead
2952 * to deadlocks. They should only be used by applications that
2953 * basically own the machine, and have a very in depth understanding
2954 * of all the possible deadlocks and enospc problems.
2955 */
2956long btrfs_ioctl_trans_end(struct file *file)
2957{
2958 struct inode *inode = fdentry(file)->d_inode;
2959 struct btrfs_root *root = BTRFS_I(inode)->root;
2960 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002961
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002962 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002963 if (!trans)
2964 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002965 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002966
Sage Weil1ab86ae2009-09-29 18:38:44 -04002967 btrfs_end_transaction(trans, root);
2968
Josef Bacika4abeea2011-04-11 17:25:13 -04002969 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002970
Al Viro2a79f172011-12-09 08:06:57 -05002971 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002972 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002973}
2974
Sage Weil46204592010-10-29 15:41:32 -04002975static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2976{
2977 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2978 struct btrfs_trans_handle *trans;
2979 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002980 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002981
2982 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002983 if (IS_ERR(trans))
2984 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002985 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002986 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002987 if (ret) {
2988 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002989 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002990 }
Sage Weil46204592010-10-29 15:41:32 -04002991
2992 if (argp)
2993 if (copy_to_user(argp, &transid, sizeof(transid)))
2994 return -EFAULT;
2995 return 0;
2996}
2997
2998static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2999{
3000 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3001 u64 transid;
3002
3003 if (argp) {
3004 if (copy_from_user(&transid, argp, sizeof(transid)))
3005 return -EFAULT;
3006 } else {
3007 transid = 0; /* current trans */
3008 }
3009 return btrfs_wait_for_commit(root, transid);
3010}
3011
Jan Schmidt475f6382011-03-11 15:41:01 +01003012static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3013{
3014 int ret;
3015 struct btrfs_ioctl_scrub_args *sa;
3016
3017 if (!capable(CAP_SYS_ADMIN))
3018 return -EPERM;
3019
3020 sa = memdup_user(arg, sizeof(*sa));
3021 if (IS_ERR(sa))
3022 return PTR_ERR(sa);
3023
3024 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003025 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003026
3027 if (copy_to_user(arg, sa, sizeof(*sa)))
3028 ret = -EFAULT;
3029
3030 kfree(sa);
3031 return ret;
3032}
3033
3034static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3035{
3036 if (!capable(CAP_SYS_ADMIN))
3037 return -EPERM;
3038
3039 return btrfs_scrub_cancel(root);
3040}
3041
3042static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3043 void __user *arg)
3044{
3045 struct btrfs_ioctl_scrub_args *sa;
3046 int ret;
3047
3048 if (!capable(CAP_SYS_ADMIN))
3049 return -EPERM;
3050
3051 sa = memdup_user(arg, sizeof(*sa));
3052 if (IS_ERR(sa))
3053 return PTR_ERR(sa);
3054
3055 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3056
3057 if (copy_to_user(arg, sa, sizeof(*sa)))
3058 ret = -EFAULT;
3059
3060 kfree(sa);
3061 return ret;
3062}
3063
Stefan Behrensc11d2c232012-05-25 16:06:09 +02003064static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3065 void __user *arg, int reset_after_read)
3066{
3067 struct btrfs_ioctl_get_dev_stats *sa;
3068 int ret;
3069
3070 if (reset_after_read && !capable(CAP_SYS_ADMIN))
3071 return -EPERM;
3072
3073 sa = memdup_user(arg, sizeof(*sa));
3074 if (IS_ERR(sa))
3075 return PTR_ERR(sa);
3076
3077 ret = btrfs_get_dev_stats(root, sa, reset_after_read);
3078
3079 if (copy_to_user(arg, sa, sizeof(*sa)))
3080 ret = -EFAULT;
3081
3082 kfree(sa);
3083 return ret;
3084}
3085
Jan Schmidtd7728c92011-07-07 16:48:38 +02003086static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3087{
3088 int ret = 0;
3089 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003090 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003091 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003092 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003093 struct inode_fs_paths *ipath = NULL;
3094 struct btrfs_path *path;
3095
3096 if (!capable(CAP_SYS_ADMIN))
3097 return -EPERM;
3098
3099 path = btrfs_alloc_path();
3100 if (!path) {
3101 ret = -ENOMEM;
3102 goto out;
3103 }
3104
3105 ipa = memdup_user(arg, sizeof(*ipa));
3106 if (IS_ERR(ipa)) {
3107 ret = PTR_ERR(ipa);
3108 ipa = NULL;
3109 goto out;
3110 }
3111
3112 size = min_t(u32, ipa->size, 4096);
3113 ipath = init_ipath(size, root, path);
3114 if (IS_ERR(ipath)) {
3115 ret = PTR_ERR(ipath);
3116 ipath = NULL;
3117 goto out;
3118 }
3119
3120 ret = paths_from_inode(ipa->inum, ipath);
3121 if (ret < 0)
3122 goto out;
3123
3124 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003125 rel_ptr = ipath->fspath->val[i] -
3126 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003127 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003128 }
3129
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003130 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3131 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003132 if (ret) {
3133 ret = -EFAULT;
3134 goto out;
3135 }
3136
3137out:
3138 btrfs_free_path(path);
3139 free_ipath(ipath);
3140 kfree(ipa);
3141
3142 return ret;
3143}
3144
3145static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3146{
3147 struct btrfs_data_container *inodes = ctx;
3148 const size_t c = 3 * sizeof(u64);
3149
3150 if (inodes->bytes_left >= c) {
3151 inodes->bytes_left -= c;
3152 inodes->val[inodes->elem_cnt] = inum;
3153 inodes->val[inodes->elem_cnt + 1] = offset;
3154 inodes->val[inodes->elem_cnt + 2] = root;
3155 inodes->elem_cnt += 3;
3156 } else {
3157 inodes->bytes_missing += c - inodes->bytes_left;
3158 inodes->bytes_left = 0;
3159 inodes->elem_missed += 3;
3160 }
3161
3162 return 0;
3163}
3164
3165static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3166 void __user *arg)
3167{
3168 int ret = 0;
3169 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003170 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003171 struct btrfs_ioctl_logical_ino_args *loi;
3172 struct btrfs_data_container *inodes = NULL;
3173 struct btrfs_path *path = NULL;
3174 struct btrfs_key key;
3175
3176 if (!capable(CAP_SYS_ADMIN))
3177 return -EPERM;
3178
3179 loi = memdup_user(arg, sizeof(*loi));
3180 if (IS_ERR(loi)) {
3181 ret = PTR_ERR(loi);
3182 loi = NULL;
3183 goto out;
3184 }
3185
3186 path = btrfs_alloc_path();
3187 if (!path) {
3188 ret = -ENOMEM;
3189 goto out;
3190 }
3191
3192 size = min_t(u32, loi->size, 4096);
3193 inodes = init_data_container(size);
3194 if (IS_ERR(inodes)) {
3195 ret = PTR_ERR(inodes);
3196 inodes = NULL;
3197 goto out;
3198 }
3199
3200 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003201 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003202
3203 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3204 ret = -ENOENT;
3205 if (ret < 0)
3206 goto out;
3207
Jan Schmidt4692cf52011-12-02 14:56:41 +01003208 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003209 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3210 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003211 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003212
3213 if (ret < 0)
3214 goto out;
3215
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003216 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3217 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003218 if (ret)
3219 ret = -EFAULT;
3220
3221out:
3222 btrfs_free_path(path);
3223 kfree(inodes);
3224 kfree(loi);
3225
3226 return ret;
3227}
3228
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003229void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003230 struct btrfs_ioctl_balance_args *bargs)
3231{
3232 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3233
3234 bargs->flags = bctl->flags;
3235
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003236 if (atomic_read(&fs_info->balance_running))
3237 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3238 if (atomic_read(&fs_info->balance_pause_req))
3239 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003240 if (atomic_read(&fs_info->balance_cancel_req))
3241 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003242
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003243 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3244 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3245 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003246
3247 if (lock) {
3248 spin_lock(&fs_info->balance_lock);
3249 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3250 spin_unlock(&fs_info->balance_lock);
3251 } else {
3252 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3253 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003254}
3255
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003256static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003257{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003258 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003259 struct btrfs_fs_info *fs_info = root->fs_info;
3260 struct btrfs_ioctl_balance_args *bargs;
3261 struct btrfs_balance_control *bctl;
3262 int ret;
3263
3264 if (!capable(CAP_SYS_ADMIN))
3265 return -EPERM;
3266
3267 if (fs_info->sb->s_flags & MS_RDONLY)
3268 return -EROFS;
3269
Al Viro11e62a82012-07-19 11:17:49 +04003270 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003271 if (ret)
3272 return ret;
3273
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003274 mutex_lock(&fs_info->volume_mutex);
3275 mutex_lock(&fs_info->balance_mutex);
3276
3277 if (arg) {
3278 bargs = memdup_user(arg, sizeof(*bargs));
3279 if (IS_ERR(bargs)) {
3280 ret = PTR_ERR(bargs);
3281 goto out;
3282 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003283
3284 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3285 if (!fs_info->balance_ctl) {
3286 ret = -ENOTCONN;
3287 goto out_bargs;
3288 }
3289
3290 bctl = fs_info->balance_ctl;
3291 spin_lock(&fs_info->balance_lock);
3292 bctl->flags |= BTRFS_BALANCE_RESUME;
3293 spin_unlock(&fs_info->balance_lock);
3294
3295 goto do_balance;
3296 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003297 } else {
3298 bargs = NULL;
3299 }
3300
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003301 if (fs_info->balance_ctl) {
3302 ret = -EINPROGRESS;
3303 goto out_bargs;
3304 }
3305
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003306 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3307 if (!bctl) {
3308 ret = -ENOMEM;
3309 goto out_bargs;
3310 }
3311
3312 bctl->fs_info = fs_info;
3313 if (arg) {
3314 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3315 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3316 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3317
3318 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003319 } else {
3320 /* balance everything - no filters */
3321 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003322 }
3323
Ilya Dryomovde322262012-01-16 22:04:49 +02003324do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003325 ret = btrfs_balance(bctl, bargs);
3326 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003327 * bctl is freed in __cancel_balance or in free_fs_info if
3328 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003329 */
3330 if (arg) {
3331 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3332 ret = -EFAULT;
3333 }
3334
3335out_bargs:
3336 kfree(bargs);
3337out:
3338 mutex_unlock(&fs_info->balance_mutex);
3339 mutex_unlock(&fs_info->volume_mutex);
Al Viro11e62a82012-07-19 11:17:49 +04003340 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003341 return ret;
3342}
3343
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003344static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3345{
3346 if (!capable(CAP_SYS_ADMIN))
3347 return -EPERM;
3348
3349 switch (cmd) {
3350 case BTRFS_BALANCE_CTL_PAUSE:
3351 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003352 case BTRFS_BALANCE_CTL_CANCEL:
3353 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003354 }
3355
3356 return -EINVAL;
3357}
3358
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003359static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3360 void __user *arg)
3361{
3362 struct btrfs_fs_info *fs_info = root->fs_info;
3363 struct btrfs_ioctl_balance_args *bargs;
3364 int ret = 0;
3365
3366 if (!capable(CAP_SYS_ADMIN))
3367 return -EPERM;
3368
3369 mutex_lock(&fs_info->balance_mutex);
3370 if (!fs_info->balance_ctl) {
3371 ret = -ENOTCONN;
3372 goto out;
3373 }
3374
3375 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3376 if (!bargs) {
3377 ret = -ENOMEM;
3378 goto out;
3379 }
3380
3381 update_ioctl_balance_args(fs_info, 1, bargs);
3382
3383 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3384 ret = -EFAULT;
3385
3386 kfree(bargs);
3387out:
3388 mutex_unlock(&fs_info->balance_mutex);
3389 return ret;
3390}
3391
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003392long btrfs_ioctl(struct file *file, unsigned int
3393 cmd, unsigned long arg)
3394{
3395 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003396 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003397
3398 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003399 case FS_IOC_GETFLAGS:
3400 return btrfs_ioctl_getflags(file, argp);
3401 case FS_IOC_SETFLAGS:
3402 return btrfs_ioctl_setflags(file, argp);
3403 case FS_IOC_GETVERSION:
3404 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003405 case FITRIM:
3406 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003407 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003408 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003409 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003410 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003411 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003412 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003413 case BTRFS_IOC_SNAP_DESTROY:
3414 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003415 case BTRFS_IOC_SUBVOL_GETFLAGS:
3416 return btrfs_ioctl_subvol_getflags(file, argp);
3417 case BTRFS_IOC_SUBVOL_SETFLAGS:
3418 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003419 case BTRFS_IOC_DEFAULT_SUBVOL:
3420 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003421 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003422 return btrfs_ioctl_defrag(file, NULL);
3423 case BTRFS_IOC_DEFRAG_RANGE:
3424 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003425 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003426 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003427 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003428 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003429 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003430 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003431 case BTRFS_IOC_FS_INFO:
3432 return btrfs_ioctl_fs_info(root, argp);
3433 case BTRFS_IOC_DEV_INFO:
3434 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003435 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003436 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003437 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003438 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3439 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003440 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003441 case BTRFS_IOC_TRANS_START:
3442 return btrfs_ioctl_trans_start(file);
3443 case BTRFS_IOC_TRANS_END:
3444 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003445 case BTRFS_IOC_TREE_SEARCH:
3446 return btrfs_ioctl_tree_search(file, argp);
3447 case BTRFS_IOC_INO_LOOKUP:
3448 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003449 case BTRFS_IOC_INO_PATHS:
3450 return btrfs_ioctl_ino_to_path(root, argp);
3451 case BTRFS_IOC_LOGICAL_INO:
3452 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003453 case BTRFS_IOC_SPACE_INFO:
3454 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003455 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003456 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3457 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003458 case BTRFS_IOC_START_SYNC:
3459 return btrfs_ioctl_start_sync(file, argp);
3460 case BTRFS_IOC_WAIT_SYNC:
3461 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003462 case BTRFS_IOC_SCRUB:
3463 return btrfs_ioctl_scrub(root, argp);
3464 case BTRFS_IOC_SCRUB_CANCEL:
3465 return btrfs_ioctl_scrub_cancel(root, argp);
3466 case BTRFS_IOC_SCRUB_PROGRESS:
3467 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003468 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003469 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003470 case BTRFS_IOC_BALANCE_CTL:
3471 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003472 case BTRFS_IOC_BALANCE_PROGRESS:
3473 return btrfs_ioctl_balance_progress(root, argp);
Stefan Behrensc11d2c232012-05-25 16:06:09 +02003474 case BTRFS_IOC_GET_DEV_STATS:
3475 return btrfs_ioctl_get_dev_stats(root, argp, 0);
3476 case BTRFS_IOC_GET_AND_RESET_DEV_STATS:
3477 return btrfs_ioctl_get_dev_stats(root, argp, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003478 }
3479
3480 return -ENOTTY;
3481}