blob: fccde7402cfedfa741dca1865ce0d0f32af24e83 [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"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040055
Christoph Hellwig6cbff002009-04-17 10:37:41 +020056/* Mask out flags that are inappropriate for the given type of inode. */
57static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58{
59 if (S_ISDIR(mode))
60 return flags;
61 else if (S_ISREG(mode))
62 return flags & ~FS_DIRSYNC_FL;
63 else
64 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040066
Christoph Hellwig6cbff002009-04-17 10:37:41 +020067/*
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69 */
70static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
71{
72 unsigned int iflags = 0;
73
74 if (flags & BTRFS_INODE_SYNC)
75 iflags |= FS_SYNC_FL;
76 if (flags & BTRFS_INODE_IMMUTABLE)
77 iflags |= FS_IMMUTABLE_FL;
78 if (flags & BTRFS_INODE_APPEND)
79 iflags |= FS_APPEND_FL;
80 if (flags & BTRFS_INODE_NODUMP)
81 iflags |= FS_NODUMP_FL;
82 if (flags & BTRFS_INODE_NOATIME)
83 iflags |= FS_NOATIME_FL;
84 if (flags & BTRFS_INODE_DIRSYNC)
85 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000086 if (flags & BTRFS_INODE_NODATACOW)
87 iflags |= FS_NOCOW_FL;
88
89 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 iflags |= FS_COMPR_FL;
91 else if (flags & BTRFS_INODE_NOCOMPRESS)
92 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093
94 return iflags;
95}
96
97/*
98 * Update inode->i_flags based on the btrfs internal flags.
99 */
100void btrfs_update_iflags(struct inode *inode)
101{
102 struct btrfs_inode *ip = BTRFS_I(inode);
103
104 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
105
106 if (ip->flags & BTRFS_INODE_SYNC)
107 inode->i_flags |= S_SYNC;
108 if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 inode->i_flags |= S_IMMUTABLE;
110 if (ip->flags & BTRFS_INODE_APPEND)
111 inode->i_flags |= S_APPEND;
112 if (ip->flags & BTRFS_INODE_NOATIME)
113 inode->i_flags |= S_NOATIME;
114 if (ip->flags & BTRFS_INODE_DIRSYNC)
115 inode->i_flags |= S_DIRSYNC;
116}
117
118/*
119 * Inherit flags from the parent inode.
120 *
Josef Bacike27425d2011-09-27 11:01:30 -0400121 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122 */
123void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
124{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400125 unsigned int flags;
126
127 if (!dir)
128 return;
129
130 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
Josef Bacike27425d2011-09-27 11:01:30 -0400132 if (flags & BTRFS_INODE_NOCOMPRESS) {
133 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135 } else if (flags & BTRFS_INODE_COMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NODATACOW)
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
142
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143 btrfs_update_iflags(inode);
144}
145
146static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
147{
148 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
149 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
150
151 if (copy_to_user(arg, &flags, sizeof(flags)))
152 return -EFAULT;
153 return 0;
154}
155
Liu Bo75e7cb72011-03-22 10:12:20 +0000156static int check_flags(unsigned int flags)
157{
158 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
159 FS_NOATIME_FL | FS_NODUMP_FL | \
160 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000161 FS_NOCOMP_FL | FS_COMPR_FL |
162 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000163 return -EOPNOTSUPP;
164
165 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 return -EINVAL;
167
Liu Bo75e7cb72011-03-22 10:12:20 +0000168 return 0;
169}
170
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200171static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
172{
173 struct inode *inode = file->f_path.dentry->d_inode;
174 struct btrfs_inode *ip = BTRFS_I(inode);
175 struct btrfs_root *root = ip->root;
176 struct btrfs_trans_handle *trans;
177 unsigned int flags, oldflags;
178 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800179 u64 ip_oldflags;
180 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181
Li Zefanb83cc962010-12-20 16:04:08 +0800182 if (btrfs_root_readonly(root))
183 return -EROFS;
184
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 if (copy_from_user(&flags, arg, sizeof(flags)))
186 return -EFAULT;
187
Liu Bo75e7cb72011-03-22 10:12:20 +0000188 ret = check_flags(flags);
189 if (ret)
190 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700192 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 return -EACCES;
194
195 mutex_lock(&inode->i_mutex);
196
Li Zefanf062abf2011-12-29 13:36:45 +0800197 ip_oldflags = ip->flags;
198 i_oldflags = inode->i_flags;
199
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200 flags = btrfs_mask_flags(inode->i_mode, flags);
201 oldflags = btrfs_flags_to_ioctl(ip->flags);
202 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
203 if (!capable(CAP_LINUX_IMMUTABLE)) {
204 ret = -EPERM;
205 goto out_unlock;
206 }
207 }
208
Al Viroa561be72011-11-23 11:57:51 -0500209 ret = mnt_want_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200210 if (ret)
211 goto out_unlock;
212
213 if (flags & FS_SYNC_FL)
214 ip->flags |= BTRFS_INODE_SYNC;
215 else
216 ip->flags &= ~BTRFS_INODE_SYNC;
217 if (flags & FS_IMMUTABLE_FL)
218 ip->flags |= BTRFS_INODE_IMMUTABLE;
219 else
220 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
221 if (flags & FS_APPEND_FL)
222 ip->flags |= BTRFS_INODE_APPEND;
223 else
224 ip->flags &= ~BTRFS_INODE_APPEND;
225 if (flags & FS_NODUMP_FL)
226 ip->flags |= BTRFS_INODE_NODUMP;
227 else
228 ip->flags &= ~BTRFS_INODE_NODUMP;
229 if (flags & FS_NOATIME_FL)
230 ip->flags |= BTRFS_INODE_NOATIME;
231 else
232 ip->flags &= ~BTRFS_INODE_NOATIME;
233 if (flags & FS_DIRSYNC_FL)
234 ip->flags |= BTRFS_INODE_DIRSYNC;
235 else
236 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000237 if (flags & FS_NOCOW_FL)
238 ip->flags |= BTRFS_INODE_NODATACOW;
239 else
240 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241
Liu Bo75e7cb72011-03-22 10:12:20 +0000242 /*
243 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 * flag may be changed automatically if compression code won't make
245 * things smaller.
246 */
247 if (flags & FS_NOCOMP_FL) {
248 ip->flags &= ~BTRFS_INODE_COMPRESS;
249 ip->flags |= BTRFS_INODE_NOCOMPRESS;
250 } else if (flags & FS_COMPR_FL) {
251 ip->flags |= BTRFS_INODE_COMPRESS;
252 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000253 } else {
254 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000255 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200256
Li Zefan4da6f1a2011-12-29 13:39:50 +0800257 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800258 if (IS_ERR(trans)) {
259 ret = PTR_ERR(trans);
260 goto out_drop;
261 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200262
Li Zefan306424cc2011-12-14 20:12:02 -0500263 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400264 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500265 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200266 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200267
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200268 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800269 out_drop:
270 if (ret) {
271 ip->flags = ip_oldflags;
272 inode->i_flags = i_oldflags;
273 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200274
Al Viro2a79f172011-12-09 08:06:57 -0500275 mnt_drop_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200276 out_unlock:
277 mutex_unlock(&inode->i_mutex);
liubo2d4e6f62011-02-24 09:38:16 +0000278 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200279}
280
281static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
282{
283 struct inode *inode = file->f_path.dentry->d_inode;
284
285 return put_user(inode->i_generation, arg);
286}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400287
Li Dongyangf7039b12011-03-24 10:24:28 +0000288static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
289{
Al Viro815745c2011-11-17 15:40:49 -0500290 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000291 struct btrfs_device *device;
292 struct request_queue *q;
293 struct fstrim_range range;
294 u64 minlen = ULLONG_MAX;
295 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500296 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000297 int ret;
298
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
301
Xiao Guangrong1f781602011-04-20 10:09:16 +0000302 rcu_read_lock();
303 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
304 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000305 if (!device->bdev)
306 continue;
307 q = bdev_get_queue(device->bdev);
308 if (blk_queue_discard(q)) {
309 num_devices++;
310 minlen = min((u64)q->limits.discard_granularity,
311 minlen);
312 }
313 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000314 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200315
Li Dongyangf7039b12011-03-24 10:24:28 +0000316 if (!num_devices)
317 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000318 if (copy_from_user(&range, arg, sizeof(range)))
319 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200320 if (range.start > total_bytes)
321 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000322
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200323 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000324 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500325 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000326 if (ret < 0)
327 return ret;
328
329 if (copy_to_user(arg, &range, sizeof(range)))
330 return -EFAULT;
331
332 return 0;
333}
334
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400335static noinline int create_subvol(struct btrfs_root *root,
336 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400337 char *name, int namelen,
338 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400339{
340 struct btrfs_trans_handle *trans;
341 struct btrfs_key key;
342 struct btrfs_root_item root_item;
343 struct btrfs_inode_item *inode_item;
344 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400345 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400346 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000347 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348 int ret;
349 int err;
350 u64 objectid;
351 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500352 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353
Li Zefan581bb052011-04-20 10:06:11 +0800354 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400355 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400356 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000357
358 dir = parent->d_inode;
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360 /*
361 * 1 - inode item
362 * 2 - refs
363 * 1 - root item
364 * 2 - dir items
365 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400366 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400367 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400368 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400369
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200371 0, objectid, NULL, 0, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400372 if (IS_ERR(leaf)) {
373 ret = PTR_ERR(leaf);
374 goto fail;
375 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400377 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400378 btrfs_set_header_bytenr(leaf, leaf->start);
379 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400380 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400381 btrfs_set_header_owner(leaf, objectid);
382
383 write_extent_buffer(leaf, root->fs_info->fsid,
384 (unsigned long)btrfs_header_fsid(leaf),
385 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400386 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
387 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
388 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 btrfs_mark_buffer_dirty(leaf);
390
391 inode_item = &root_item.inode;
392 memset(inode_item, 0, sizeof(*inode_item));
393 inode_item->generation = cpu_to_le64(1);
394 inode_item->size = cpu_to_le64(3);
395 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400396 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
398
Li Zefan08fe4db2011-03-28 02:01:25 +0000399 root_item.flags = 0;
400 root_item.byte_limit = 0;
401 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
402
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400404 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400405 btrfs_set_root_level(&root_item, 0);
406 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000407 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400408 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409
410 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
411 root_item.drop_level = 0;
412
Chris Mason925baed2008-06-25 16:01:30 -0400413 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 free_extent_buffer(leaf);
415 leaf = NULL;
416
417 btrfs_set_root_dirid(&root_item, new_dirid);
418
419 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400420 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
422 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
423 &root_item);
424 if (ret)
425 goto fail;
426
Yan, Zheng76dda932009-09-21 16:00:26 -0400427 key.offset = (u64)-1;
428 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100429 if (IS_ERR(new_root)) {
430 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
431 ret = PTR_ERR(new_root);
432 goto fail;
433 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400434
435 btrfs_record_root_in_trans(trans, new_root);
436
Josef Bacikd82a6f12011-05-11 15:26:06 -0400437 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700438 if (ret) {
439 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100440 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700441 goto fail;
442 }
443
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400444 /*
445 * insert the directory item
446 */
Chris Mason3de45862008-11-17 21:02:50 -0500447 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100448 if (ret) {
449 btrfs_abort_transaction(trans, root, ret);
450 goto fail;
451 }
Chris Mason3de45862008-11-17 21:02:50 -0500452
453 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800454 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500455 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100456 if (ret) {
457 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400458 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100459 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500460
Yan Zheng52c26172009-01-05 15:43:43 -0500461 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
462 ret = btrfs_update_inode(trans, root, dir);
463 BUG_ON(ret);
464
Chris Mason0660b5a2008-11-17 20:37:39 -0500465 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400466 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800467 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400468
Chris Mason0660b5a2008-11-17 20:37:39 -0500469 BUG_ON(ret);
470
Yan, Zheng76dda932009-09-21 16:00:26 -0400471 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400472fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400473 if (async_transid) {
474 *async_transid = trans->transid;
475 err = btrfs_commit_transaction_async(trans, root, 1);
476 } else {
477 err = btrfs_commit_transaction(trans, root);
478 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400479 if (err && !ret)
480 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400481 return ret;
482}
483
Sage Weil72fd0322010-10-29 15:41:32 -0400484static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800485 char *name, int namelen, u64 *async_transid,
486 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400487{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000488 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489 struct btrfs_pending_snapshot *pending_snapshot;
490 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000491 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400492
493 if (!root->ref_cows)
494 return -EINVAL;
495
Yan, Zhenga22285a2010-05-16 10:48:46 -0400496 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
497 if (!pending_snapshot)
498 return -ENOMEM;
499
500 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
501 pending_snapshot->dentry = dentry;
502 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800503 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400504
505 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
506 if (IS_ERR(trans)) {
507 ret = PTR_ERR(trans);
508 goto fail;
509 }
510
511 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
512 BUG_ON(ret);
513
Josef Bacik83515832011-06-14 15:16:14 -0400514 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400515 list_add(&pending_snapshot->list,
516 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400517 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400518 if (async_transid) {
519 *async_transid = trans->transid;
520 ret = btrfs_commit_transaction_async(trans,
521 root->fs_info->extent_root, 1);
522 } else {
523 ret = btrfs_commit_transaction(trans,
524 root->fs_info->extent_root);
525 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400526 BUG_ON(ret);
527
528 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400529 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000530 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500532 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
533 if (ret)
534 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400535
Al Viro2fbe8c82011-07-16 21:38:06 -0400536 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000537 if (IS_ERR(inode)) {
538 ret = PTR_ERR(inode);
539 goto fail;
540 }
541 BUG_ON(!inode);
542 d_instantiate(dentry, inode);
543 ret = 0;
544fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400545 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546 return ret;
547}
548
Sage Weil4260f7c2010-10-29 15:46:43 -0400549/* copy of check_sticky in fs/namei.c()
550* It's inline, so penalty for filesystems that don't use sticky bit is
551* minimal.
552*/
553static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
554{
555 uid_t fsuid = current_fsuid();
556
557 if (!(dir->i_mode & S_ISVTX))
558 return 0;
559 if (inode->i_uid == fsuid)
560 return 0;
561 if (dir->i_uid == fsuid)
562 return 0;
563 return !capable(CAP_FOWNER);
564}
565
566/* copy of may_delete in fs/namei.c()
567 * Check whether we can remove a link victim from directory dir, check
568 * whether the type of victim is right.
569 * 1. We can't do it if dir is read-only (done in permission())
570 * 2. We should have write and exec permissions on dir
571 * 3. We can't remove anything from append-only dir
572 * 4. We can't do anything with immutable dir (done in permission())
573 * 5. If the sticky bit on dir is set we should either
574 * a. be owner of dir, or
575 * b. be owner of victim, or
576 * c. have CAP_FOWNER capability
577 * 6. If the victim is append-only or immutable we can't do antyhing with
578 * links pointing to it.
579 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
580 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
581 * 9. We can't remove a root or mountpoint.
582 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
583 * nfs_async_unlink().
584 */
585
586static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
587{
588 int error;
589
590 if (!victim->d_inode)
591 return -ENOENT;
592
593 BUG_ON(victim->d_parent->d_inode != dir);
594 audit_inode_child(victim, dir);
595
596 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
597 if (error)
598 return error;
599 if (IS_APPEND(dir))
600 return -EPERM;
601 if (btrfs_check_sticky(dir, victim->d_inode)||
602 IS_APPEND(victim->d_inode)||
603 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
604 return -EPERM;
605 if (isdir) {
606 if (!S_ISDIR(victim->d_inode->i_mode))
607 return -ENOTDIR;
608 if (IS_ROOT(victim))
609 return -EBUSY;
610 } else if (S_ISDIR(victim->d_inode->i_mode))
611 return -EISDIR;
612 if (IS_DEADDIR(dir))
613 return -ENOENT;
614 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
615 return -EBUSY;
616 return 0;
617}
618
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400619/* copy of may_create in fs/namei.c() */
620static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
621{
622 if (child->d_inode)
623 return -EEXIST;
624 if (IS_DEADDIR(dir))
625 return -ENOENT;
626 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
627}
628
629/*
630 * Create a new subvolume below @parent. This is largely modeled after
631 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
632 * inside this filesystem so it's quite a bit simpler.
633 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400634static noinline int btrfs_mksubvol(struct path *parent,
635 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400636 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800637 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400638{
Yan, Zheng76dda932009-09-21 16:00:26 -0400639 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400640 struct dentry *dentry;
641 int error;
642
Yan, Zheng76dda932009-09-21 16:00:26 -0400643 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400644
645 dentry = lookup_one_len(name, parent->dentry, namelen);
646 error = PTR_ERR(dentry);
647 if (IS_ERR(dentry))
648 goto out_unlock;
649
650 error = -EEXIST;
651 if (dentry->d_inode)
652 goto out_dput;
653
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400654 error = mnt_want_write(parent->mnt);
655 if (error)
656 goto out_dput;
657
Yan, Zheng76dda932009-09-21 16:00:26 -0400658 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400659 if (error)
660 goto out_drop_write;
661
Yan, Zheng76dda932009-09-21 16:00:26 -0400662 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
663
664 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
665 goto out_up_read;
666
Chris Mason3de45862008-11-17 21:02:50 -0500667 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400668 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800669 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500670 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400671 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400672 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500673 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400674 if (!error)
675 fsnotify_mkdir(dir, dentry);
676out_up_read:
677 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400678out_drop_write:
679 mnt_drop_write(parent->mnt);
680out_dput:
681 dput(dentry);
682out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400683 mutex_unlock(&dir->i_mutex);
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
Liu Bo17ce6ef2012-03-29 09:57:45 -0400788/*
789 * Validaty check of prev em and next em:
790 * 1) no prev/next em
791 * 2) prev/next em is an hole/inline extent
792 */
793static int check_adjacent_extents(struct inode *inode, struct extent_map *em)
794{
795 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
796 struct extent_map *prev = NULL, *next = NULL;
797 int ret = 0;
798
799 read_lock(&em_tree->lock);
800 prev = lookup_extent_mapping(em_tree, em->start - 1, (u64)-1);
801 next = lookup_extent_mapping(em_tree, em->start + em->len, (u64)-1);
802 read_unlock(&em_tree->lock);
803
804 if ((!prev || prev->block_start >= EXTENT_MAP_LAST_BYTE) &&
805 (!next || next->block_start >= EXTENT_MAP_LAST_BYTE))
806 ret = 1;
807 free_extent_map(prev);
808 free_extent_map(next);
809
810 return ret;
811}
812
Chris Mason940100a2010-03-10 10:52:59 -0500813static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500814 int thresh, u64 *last_len, u64 *skip,
815 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500816{
817 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
818 struct extent_map *em = NULL;
819 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
820 int ret = 1;
821
822 /*
Li Zefan008873e2011-09-02 15:57:07 +0800823 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500824 * defragging it
825 */
826 if (start < *defrag_end)
827 return 1;
828
829 *skip = 0;
830
831 /*
832 * hopefully we have this extent in the tree already, try without
833 * the full extent lock
834 */
835 read_lock(&em_tree->lock);
836 em = lookup_extent_mapping(em_tree, start, len);
837 read_unlock(&em_tree->lock);
838
839 if (!em) {
840 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100841 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500842 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100843 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500844
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000845 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500846 return 0;
847 }
848
849 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400850 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500851 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400852 goto out;
853 }
854
855 /* If we have nothing to merge with us, just skip. */
856 if (check_adjacent_extents(inode, em)) {
857 ret = 0;
858 goto out;
859 }
Chris Mason940100a2010-03-10 10:52:59 -0500860
861 /*
862 * we hit a real extent, if it is big don't bother defragging it again
863 */
Chris Mason1e701a32010-03-11 09:42:04 -0500864 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500865 ret = 0;
866
Liu Bo17ce6ef2012-03-29 09:57:45 -0400867out:
Chris Mason940100a2010-03-10 10:52:59 -0500868 /*
869 * last_len ends up being a counter of how many bytes we've defragged.
870 * every time we choose not to defrag an extent, we reset *last_len
871 * so that the next tiny extent will force a defrag.
872 *
873 * The end result of this is that tiny extents before a single big
874 * extent will force at least part of that big extent to be defragged.
875 */
876 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500877 *defrag_end = extent_map_end(em);
878 } else {
879 *last_len = 0;
880 *skip = extent_map_end(em);
881 *defrag_end = 0;
882 }
883
884 free_extent_map(em);
885 return ret;
886}
887
Chris Mason4cb53002011-05-24 15:35:30 -0400888/*
889 * it doesn't do much good to defrag one or two pages
890 * at a time. This pulls in a nice chunk of pages
891 * to COW and defrag.
892 *
893 * It also makes sure the delalloc code has enough
894 * dirty data to avoid making new small extents as part
895 * of the defrag
896 *
897 * It's a good idea to start RA on this range
898 * before calling this.
899 */
900static int cluster_pages_for_defrag(struct inode *inode,
901 struct page **pages,
902 unsigned long start_index,
903 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400904{
Chris Mason4cb53002011-05-24 15:35:30 -0400905 unsigned long file_end;
906 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400907 u64 page_start;
908 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400909 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400910 int ret;
911 int i;
912 int i_done;
913 struct btrfs_ordered_extent *ordered;
914 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800915 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400916 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400917
Chris Mason4cb53002011-05-24 15:35:30 -0400918 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400919 if (!isize || start_index > file_end)
920 return 0;
921
922 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400923
924 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400925 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400926 if (ret)
927 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400928 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800929 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400930
931 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400932 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400933 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800934again:
Josef Bacika94733d2011-07-11 10:47:06 -0400935 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800936 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400937 if (!page)
938 break;
939
Miao Xie600a45e2012-02-16 15:01:24 +0800940 page_start = page_offset(page);
941 page_end = page_start + PAGE_CACHE_SIZE - 1;
942 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100943 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800944 ordered = btrfs_lookup_ordered_extent(inode,
945 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100946 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800947 if (!ordered)
948 break;
949
950 unlock_page(page);
951 btrfs_start_ordered_extent(inode, ordered, 1);
952 btrfs_put_ordered_extent(ordered);
953 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400954 /*
955 * we unlocked the page above, so we need check if
956 * it was released or not.
957 */
958 if (page->mapping != inode->i_mapping) {
959 unlock_page(page);
960 page_cache_release(page);
961 goto again;
962 }
Miao Xie600a45e2012-02-16 15:01:24 +0800963 }
964
Chris Mason4cb53002011-05-24 15:35:30 -0400965 if (!PageUptodate(page)) {
966 btrfs_readpage(NULL, page);
967 lock_page(page);
968 if (!PageUptodate(page)) {
969 unlock_page(page);
970 page_cache_release(page);
971 ret = -EIO;
972 break;
973 }
974 }
Miao Xie600a45e2012-02-16 15:01:24 +0800975
Miao Xie600a45e2012-02-16 15:01:24 +0800976 if (page->mapping != inode->i_mapping) {
977 unlock_page(page);
978 page_cache_release(page);
979 goto again;
980 }
981
Chris Mason4cb53002011-05-24 15:35:30 -0400982 pages[i] = page;
983 i_done++;
984 }
985 if (!i_done || ret)
986 goto out;
987
988 if (!(inode->i_sb->s_flags & MS_ACTIVE))
989 goto out;
990
991 /*
992 * so now we have a nice long stream of locked
993 * and up to date pages, lets wait on them
994 */
995 for (i = 0; i < i_done; i++)
996 wait_on_page_writeback(pages[i]);
997
998 page_start = page_offset(pages[0]);
999 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1000
1001 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001002 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001003 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1004 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1005 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
1006 GFP_NOFS);
1007
Liu Bo1f12bd02012-03-29 09:57:44 -04001008 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001009 spin_lock(&BTRFS_I(inode)->lock);
1010 BTRFS_I(inode)->outstanding_extents++;
1011 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001012 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001013 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001014 }
1015
1016
1017 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
1018 &cached_state);
1019
1020 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1021 page_start, page_end - 1, &cached_state,
1022 GFP_NOFS);
1023
1024 for (i = 0; i < i_done; i++) {
1025 clear_page_dirty_for_io(pages[i]);
1026 ClearPageChecked(pages[i]);
1027 set_page_extent_mapped(pages[i]);
1028 set_page_dirty(pages[i]);
1029 unlock_page(pages[i]);
1030 page_cache_release(pages[i]);
1031 }
1032 return i_done;
1033out:
1034 for (i = 0; i < i_done; i++) {
1035 unlock_page(pages[i]);
1036 page_cache_release(pages[i]);
1037 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001038 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001039 return ret;
1040
1041}
1042
1043int btrfs_defrag_file(struct inode *inode, struct file *file,
1044 struct btrfs_ioctl_defrag_range_args *range,
1045 u64 newer_than, unsigned long max_to_defrag)
1046{
1047 struct btrfs_root *root = BTRFS_I(inode)->root;
1048 struct btrfs_super_block *disk_super;
1049 struct file_ra_state *ra = NULL;
1050 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001051 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001052 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001053 u64 last_len = 0;
1054 u64 skip = 0;
1055 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001056 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001057 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001058 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001059 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001060 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001061 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001062 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001063 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1064 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001065 u64 new_align = ~((u64)128 * 1024 - 1);
1066 struct page **pages = NULL;
1067
1068 if (extent_thresh == 0)
1069 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001070
1071 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1072 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1073 return -EINVAL;
1074 if (range->compress_type)
1075 compress_type = range->compress_type;
1076 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001077
Li Zefan151a31b2011-09-02 15:56:39 +08001078 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001079 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001080
Chris Mason4cb53002011-05-24 15:35:30 -04001081 /*
1082 * if we were not given a file, allocate a readahead
1083 * context
1084 */
1085 if (!file) {
1086 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1087 if (!ra)
1088 return -ENOMEM;
1089 file_ra_state_init(ra, inode->i_mapping);
1090 } else {
1091 ra = &file->f_ra;
1092 }
1093
Li Zefan008873e2011-09-02 15:57:07 +08001094 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001095 GFP_NOFS);
1096 if (!pages) {
1097 ret = -ENOMEM;
1098 goto out_ra;
1099 }
1100
1101 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001102 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001103 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001104 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1105 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001106 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001107 }
1108
Chris Mason4cb53002011-05-24 15:35:30 -04001109 if (newer_than) {
1110 ret = find_new_extents(root, inode, newer_than,
1111 &newer_off, 64 * 1024);
1112 if (!ret) {
1113 range->start = newer_off;
1114 /*
1115 * we always align our defrag to help keep
1116 * the extents in the file evenly spaced
1117 */
1118 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001119 } else
1120 goto out_ra;
1121 } else {
1122 i = range->start >> PAGE_CACHE_SHIFT;
1123 }
1124 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001125 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001126
Li Zefan2a0f7f52011-10-10 15:43:34 -04001127 /*
1128 * make writeback starts from i, so the defrag range can be
1129 * written sequentially.
1130 */
1131 if (i < inode->i_mapping->writeback_index)
1132 inode->i_mapping->writeback_index = i;
1133
Chris Masonf7f43cc2011-10-11 11:41:40 -04001134 while (i <= last_index && defrag_count < max_to_defrag &&
1135 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1136 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001137 /*
1138 * make sure we stop running if someone unmounts
1139 * the FS
1140 */
1141 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1142 break;
1143
Liu Bo66c26892012-03-29 09:57:45 -04001144 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
1145 PAGE_CACHE_SIZE, extent_thresh,
1146 &last_len, &skip, &defrag_end)) {
Chris Mason940100a2010-03-10 10:52:59 -05001147 unsigned long next;
1148 /*
1149 * the should_defrag function tells us how much to skip
1150 * bump our counter by the suggested amount
1151 */
1152 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1153 i = max(i + 1, next);
1154 continue;
1155 }
Li Zefan008873e2011-09-02 15:57:07 +08001156
1157 if (!newer_than) {
1158 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1159 PAGE_CACHE_SHIFT) - i;
1160 cluster = min(cluster, max_cluster);
1161 } else {
1162 cluster = max_cluster;
1163 }
1164
Chris Mason1e701a32010-03-11 09:42:04 -05001165 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001166 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001167
Li Zefan008873e2011-09-02 15:57:07 +08001168 if (i + cluster > ra_index) {
1169 ra_index = max(i, ra_index);
1170 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1171 cluster);
1172 ra_index += max_cluster;
1173 }
Chris Mason940100a2010-03-10 10:52:59 -05001174
Liu Boecb8bea2012-03-29 09:57:44 -04001175 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001176 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001177 if (ret < 0) {
1178 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001179 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001180 }
Chris Mason940100a2010-03-10 10:52:59 -05001181
Chris Mason4cb53002011-05-24 15:35:30 -04001182 defrag_count += ret;
1183 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001184 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001185
1186 if (newer_than) {
1187 if (newer_off == (u64)-1)
1188 break;
1189
Liu Boe1f041e2012-03-29 09:57:45 -04001190 if (ret > 0)
1191 i += ret;
1192
Chris Mason4cb53002011-05-24 15:35:30 -04001193 newer_off = max(newer_off + 1,
1194 (u64)i << PAGE_CACHE_SHIFT);
1195
1196 ret = find_new_extents(root, inode,
1197 newer_than, &newer_off,
1198 64 * 1024);
1199 if (!ret) {
1200 range->start = newer_off;
1201 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001202 } else {
1203 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001204 }
Chris Mason4cb53002011-05-24 15:35:30 -04001205 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001206 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001207 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001208 last_len += ret << PAGE_CACHE_SHIFT;
1209 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001210 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001211 last_len = 0;
1212 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001213 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001214 }
1215
Chris Mason1e701a32010-03-11 09:42:04 -05001216 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1217 filemap_flush(inode->i_mapping);
1218
1219 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1220 /* the filemap_flush will queue IO into the worker threads, but
1221 * we have to make sure the IO is actually started and that
1222 * ordered extents get created before we return
1223 */
1224 atomic_inc(&root->fs_info->async_submit_draining);
1225 while (atomic_read(&root->fs_info->nr_async_submits) ||
1226 atomic_read(&root->fs_info->async_delalloc_pages)) {
1227 wait_event(root->fs_info->async_submit_wait,
1228 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1229 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1230 }
1231 atomic_dec(&root->fs_info->async_submit_draining);
1232
1233 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001234 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001235 mutex_unlock(&inode->i_mutex);
1236 }
1237
David Sterba6c417612011-04-13 15:41:04 +02001238 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001239 features = btrfs_super_incompat_flags(disk_super);
1240 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1241 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1242 btrfs_set_super_incompat_flags(disk_super, features);
1243 }
1244
Diego Calleja60ccf822011-09-01 16:33:57 +02001245 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001246
Chris Mason4cb53002011-05-24 15:35:30 -04001247out_ra:
1248 if (!file)
1249 kfree(ra);
1250 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001251 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001252}
1253
Yan, Zheng76dda932009-09-21 16:00:26 -04001254static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1255 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001256{
1257 u64 new_size;
1258 u64 old_size;
1259 u64 devid = 1;
1260 struct btrfs_ioctl_vol_args *vol_args;
1261 struct btrfs_trans_handle *trans;
1262 struct btrfs_device *device = NULL;
1263 char *sizestr;
1264 char *devstr = NULL;
1265 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001266 int mod = 0;
1267
Yan Zhengc146afa2008-11-12 14:34:12 -05001268 if (root->fs_info->sb->s_flags & MS_RDONLY)
1269 return -EROFS;
1270
Chris Masone441d542009-01-05 16:57:23 -05001271 if (!capable(CAP_SYS_ADMIN))
1272 return -EPERM;
1273
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001274 mutex_lock(&root->fs_info->volume_mutex);
1275 if (root->fs_info->balance_ctl) {
1276 printk(KERN_INFO "btrfs: balance in progress\n");
1277 ret = -EINVAL;
1278 goto out;
1279 }
1280
Li Zefandae7b662009-04-08 15:06:54 +08001281 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001282 if (IS_ERR(vol_args)) {
1283 ret = PTR_ERR(vol_args);
1284 goto out;
1285 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001286
1287 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001288
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001289 sizestr = vol_args->name;
1290 devstr = strchr(sizestr, ':');
1291 if (devstr) {
1292 char *end;
1293 sizestr = devstr + 1;
1294 *devstr = '\0';
1295 devstr = vol_args->name;
1296 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001297 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001298 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001299 }
Yan Zheng2b820322008-11-17 21:11:30 -05001300 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001301 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001302 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001303 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001304 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001305 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001306 }
1307 if (!strcmp(sizestr, "max"))
1308 new_size = device->bdev->bd_inode->i_size;
1309 else {
1310 if (sizestr[0] == '-') {
1311 mod = -1;
1312 sizestr++;
1313 } else if (sizestr[0] == '+') {
1314 mod = 1;
1315 sizestr++;
1316 }
Akinobu Mita91748462010-02-28 10:59:11 +00001317 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001318 if (new_size == 0) {
1319 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001320 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001321 }
1322 }
1323
1324 old_size = device->total_bytes;
1325
1326 if (mod < 0) {
1327 if (new_size > old_size) {
1328 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001329 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330 }
1331 new_size = old_size - new_size;
1332 } else if (mod > 0) {
1333 new_size = old_size + new_size;
1334 }
1335
1336 if (new_size < 256 * 1024 * 1024) {
1337 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001338 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001339 }
1340 if (new_size > device->bdev->bd_inode->i_size) {
1341 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001342 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001343 }
1344
1345 do_div(new_size, root->sectorsize);
1346 new_size *= root->sectorsize;
1347
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001348 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001349 device->name, (unsigned long long)new_size);
1350
1351 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001352 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001353 if (IS_ERR(trans)) {
1354 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001355 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001356 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001357 ret = btrfs_grow_device(trans, device, new_size);
1358 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001359 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001360 ret = btrfs_shrink_device(device, new_size);
1361 }
1362
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001363out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001364 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001365out:
1366 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001367 return ret;
1368}
1369
Sage Weil72fd0322010-10-29 15:41:32 -04001370static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1371 char *name,
1372 unsigned long fd,
1373 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001374 u64 *transid,
1375 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001376{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001377 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001378 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001379 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001380 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001381
Yan Zhengc146afa2008-11-12 14:34:12 -05001382 if (root->fs_info->sb->s_flags & MS_RDONLY)
1383 return -EROFS;
1384
Sage Weil72fd0322010-10-29 15:41:32 -04001385 namelen = strlen(name);
1386 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001387 ret = -EINVAL;
1388 goto out;
1389 }
1390
Chris Mason16780ca2012-02-20 22:14:55 -05001391 if (name[0] == '.' &&
1392 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1393 ret = -EEXIST;
1394 goto out;
1395 }
1396
Chris Mason3de45862008-11-17 21:02:50 -05001397 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001398 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001399 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001400 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001401 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001402 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001403 if (!src_file) {
1404 ret = -EINVAL;
1405 goto out;
1406 }
1407
1408 src_inode = src_file->f_path.dentry->d_inode;
1409 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001410 printk(KERN_INFO "btrfs: Snapshot src from "
1411 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001412 ret = -EINVAL;
1413 fput(src_file);
1414 goto out;
1415 }
Sage Weil72fd0322010-10-29 15:41:32 -04001416 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1417 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001418 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001419 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001420 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421out:
Sage Weil72fd0322010-10-29 15:41:32 -04001422 return ret;
1423}
1424
1425static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001426 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001427{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001428 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001429 int ret;
1430
Li Zefanfa0d2b92010-12-20 15:53:28 +08001431 vol_args = memdup_user(arg, sizeof(*vol_args));
1432 if (IS_ERR(vol_args))
1433 return PTR_ERR(vol_args);
1434 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001435
Li Zefanfa0d2b92010-12-20 15:53:28 +08001436 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001437 vol_args->fd, subvol,
1438 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001439
Li Zefanfa0d2b92010-12-20 15:53:28 +08001440 kfree(vol_args);
1441 return ret;
1442}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001443
Li Zefanfa0d2b92010-12-20 15:53:28 +08001444static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1445 void __user *arg, int subvol)
1446{
1447 struct btrfs_ioctl_vol_args_v2 *vol_args;
1448 int ret;
1449 u64 transid = 0;
1450 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001451 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001452
Li Zefanfa0d2b92010-12-20 15:53:28 +08001453 vol_args = memdup_user(arg, sizeof(*vol_args));
1454 if (IS_ERR(vol_args))
1455 return PTR_ERR(vol_args);
1456 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001457
Li Zefanb83cc962010-12-20 16:04:08 +08001458 if (vol_args->flags &
1459 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1460 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001461 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001462 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001463
1464 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1465 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001466 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1467 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001468
1469 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001470 vol_args->fd, subvol,
1471 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001472
1473 if (ret == 0 && ptr &&
1474 copy_to_user(arg +
1475 offsetof(struct btrfs_ioctl_vol_args_v2,
1476 transid), ptr, sizeof(*ptr)))
1477 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001478out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001479 kfree(vol_args);
1480 return ret;
1481}
1482
Li Zefan0caa1022010-12-20 16:30:25 +08001483static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1484 void __user *arg)
1485{
1486 struct inode *inode = fdentry(file)->d_inode;
1487 struct btrfs_root *root = BTRFS_I(inode)->root;
1488 int ret = 0;
1489 u64 flags = 0;
1490
Li Zefan33345d012011-04-20 10:31:50 +08001491 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001492 return -EINVAL;
1493
1494 down_read(&root->fs_info->subvol_sem);
1495 if (btrfs_root_readonly(root))
1496 flags |= BTRFS_SUBVOL_RDONLY;
1497 up_read(&root->fs_info->subvol_sem);
1498
1499 if (copy_to_user(arg, &flags, sizeof(flags)))
1500 ret = -EFAULT;
1501
1502 return ret;
1503}
1504
1505static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1506 void __user *arg)
1507{
1508 struct inode *inode = fdentry(file)->d_inode;
1509 struct btrfs_root *root = BTRFS_I(inode)->root;
1510 struct btrfs_trans_handle *trans;
1511 u64 root_flags;
1512 u64 flags;
1513 int ret = 0;
1514
1515 if (root->fs_info->sb->s_flags & MS_RDONLY)
1516 return -EROFS;
1517
Li Zefan33345d012011-04-20 10:31:50 +08001518 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001519 return -EINVAL;
1520
1521 if (copy_from_user(&flags, arg, sizeof(flags)))
1522 return -EFAULT;
1523
Li Zefanb4dc2b82011-02-16 06:06:34 +00001524 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001525 return -EINVAL;
1526
1527 if (flags & ~BTRFS_SUBVOL_RDONLY)
1528 return -EOPNOTSUPP;
1529
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001530 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001531 return -EACCES;
1532
Li Zefan0caa1022010-12-20 16:30:25 +08001533 down_write(&root->fs_info->subvol_sem);
1534
1535 /* nothing to do */
1536 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1537 goto out;
1538
1539 root_flags = btrfs_root_flags(&root->root_item);
1540 if (flags & BTRFS_SUBVOL_RDONLY)
1541 btrfs_set_root_flags(&root->root_item,
1542 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1543 else
1544 btrfs_set_root_flags(&root->root_item,
1545 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1546
1547 trans = btrfs_start_transaction(root, 1);
1548 if (IS_ERR(trans)) {
1549 ret = PTR_ERR(trans);
1550 goto out_reset;
1551 }
1552
Li Zefanb4dc2b82011-02-16 06:06:34 +00001553 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001554 &root->root_key, &root->root_item);
1555
1556 btrfs_commit_transaction(trans, root);
1557out_reset:
1558 if (ret)
1559 btrfs_set_root_flags(&root->root_item, root_flags);
1560out:
1561 up_write(&root->fs_info->subvol_sem);
1562 return ret;
1563}
1564
Yan, Zheng76dda932009-09-21 16:00:26 -04001565/*
1566 * helper to check if the subvolume references other subvolumes
1567 */
1568static noinline int may_destroy_subvol(struct btrfs_root *root)
1569{
1570 struct btrfs_path *path;
1571 struct btrfs_key key;
1572 int ret;
1573
1574 path = btrfs_alloc_path();
1575 if (!path)
1576 return -ENOMEM;
1577
1578 key.objectid = root->root_key.objectid;
1579 key.type = BTRFS_ROOT_REF_KEY;
1580 key.offset = (u64)-1;
1581
1582 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1583 &key, path, 0, 0);
1584 if (ret < 0)
1585 goto out;
1586 BUG_ON(ret == 0);
1587
1588 ret = 0;
1589 if (path->slots[0] > 0) {
1590 path->slots[0]--;
1591 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1592 if (key.objectid == root->root_key.objectid &&
1593 key.type == BTRFS_ROOT_REF_KEY)
1594 ret = -ENOTEMPTY;
1595 }
1596out:
1597 btrfs_free_path(path);
1598 return ret;
1599}
1600
Chris Masonac8e9812010-02-28 15:39:26 -05001601static noinline int key_in_sk(struct btrfs_key *key,
1602 struct btrfs_ioctl_search_key *sk)
1603{
Chris Masonabc6e132010-03-18 12:10:08 -04001604 struct btrfs_key test;
1605 int ret;
1606
1607 test.objectid = sk->min_objectid;
1608 test.type = sk->min_type;
1609 test.offset = sk->min_offset;
1610
1611 ret = btrfs_comp_cpu_keys(key, &test);
1612 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001613 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001614
1615 test.objectid = sk->max_objectid;
1616 test.type = sk->max_type;
1617 test.offset = sk->max_offset;
1618
1619 ret = btrfs_comp_cpu_keys(key, &test);
1620 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001621 return 0;
1622 return 1;
1623}
1624
1625static noinline int copy_to_sk(struct btrfs_root *root,
1626 struct btrfs_path *path,
1627 struct btrfs_key *key,
1628 struct btrfs_ioctl_search_key *sk,
1629 char *buf,
1630 unsigned long *sk_offset,
1631 int *num_found)
1632{
1633 u64 found_transid;
1634 struct extent_buffer *leaf;
1635 struct btrfs_ioctl_search_header sh;
1636 unsigned long item_off;
1637 unsigned long item_len;
1638 int nritems;
1639 int i;
1640 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001641 int ret = 0;
1642
1643 leaf = path->nodes[0];
1644 slot = path->slots[0];
1645 nritems = btrfs_header_nritems(leaf);
1646
1647 if (btrfs_header_generation(leaf) > sk->max_transid) {
1648 i = nritems;
1649 goto advance_key;
1650 }
1651 found_transid = btrfs_header_generation(leaf);
1652
1653 for (i = slot; i < nritems; i++) {
1654 item_off = btrfs_item_ptr_offset(leaf, i);
1655 item_len = btrfs_item_size_nr(leaf, i);
1656
1657 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1658 item_len = 0;
1659
1660 if (sizeof(sh) + item_len + *sk_offset >
1661 BTRFS_SEARCH_ARGS_BUFSIZE) {
1662 ret = 1;
1663 goto overflow;
1664 }
1665
1666 btrfs_item_key_to_cpu(leaf, key, i);
1667 if (!key_in_sk(key, sk))
1668 continue;
1669
1670 sh.objectid = key->objectid;
1671 sh.offset = key->offset;
1672 sh.type = key->type;
1673 sh.len = item_len;
1674 sh.transid = found_transid;
1675
1676 /* copy search result header */
1677 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1678 *sk_offset += sizeof(sh);
1679
1680 if (item_len) {
1681 char *p = buf + *sk_offset;
1682 /* copy the item */
1683 read_extent_buffer(leaf, p,
1684 item_off, item_len);
1685 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001686 }
Hugo Millse2156862011-05-14 17:43:41 +00001687 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001688
1689 if (*num_found >= sk->nr_items)
1690 break;
1691 }
1692advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001693 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001694 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1695 key->offset++;
1696 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1697 key->offset = 0;
1698 key->type++;
1699 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1700 key->offset = 0;
1701 key->type = 0;
1702 key->objectid++;
1703 } else
1704 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001705overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001706 return ret;
1707}
1708
1709static noinline int search_ioctl(struct inode *inode,
1710 struct btrfs_ioctl_search_args *args)
1711{
1712 struct btrfs_root *root;
1713 struct btrfs_key key;
1714 struct btrfs_key max_key;
1715 struct btrfs_path *path;
1716 struct btrfs_ioctl_search_key *sk = &args->key;
1717 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1718 int ret;
1719 int num_found = 0;
1720 unsigned long sk_offset = 0;
1721
1722 path = btrfs_alloc_path();
1723 if (!path)
1724 return -ENOMEM;
1725
1726 if (sk->tree_id == 0) {
1727 /* search the root of the inode that was passed */
1728 root = BTRFS_I(inode)->root;
1729 } else {
1730 key.objectid = sk->tree_id;
1731 key.type = BTRFS_ROOT_ITEM_KEY;
1732 key.offset = (u64)-1;
1733 root = btrfs_read_fs_root_no_name(info, &key);
1734 if (IS_ERR(root)) {
1735 printk(KERN_ERR "could not find root %llu\n",
1736 sk->tree_id);
1737 btrfs_free_path(path);
1738 return -ENOENT;
1739 }
1740 }
1741
1742 key.objectid = sk->min_objectid;
1743 key.type = sk->min_type;
1744 key.offset = sk->min_offset;
1745
1746 max_key.objectid = sk->max_objectid;
1747 max_key.type = sk->max_type;
1748 max_key.offset = sk->max_offset;
1749
1750 path->keep_locks = 1;
1751
1752 while(1) {
1753 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1754 sk->min_transid);
1755 if (ret != 0) {
1756 if (ret > 0)
1757 ret = 0;
1758 goto err;
1759 }
1760 ret = copy_to_sk(root, path, &key, sk, args->buf,
1761 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001762 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001763 if (ret || num_found >= sk->nr_items)
1764 break;
1765
1766 }
1767 ret = 0;
1768err:
1769 sk->nr_items = num_found;
1770 btrfs_free_path(path);
1771 return ret;
1772}
1773
1774static noinline int btrfs_ioctl_tree_search(struct file *file,
1775 void __user *argp)
1776{
1777 struct btrfs_ioctl_search_args *args;
1778 struct inode *inode;
1779 int ret;
1780
1781 if (!capable(CAP_SYS_ADMIN))
1782 return -EPERM;
1783
Julia Lawall2354d082010-10-29 15:14:18 -04001784 args = memdup_user(argp, sizeof(*args));
1785 if (IS_ERR(args))
1786 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001787
Chris Masonac8e9812010-02-28 15:39:26 -05001788 inode = fdentry(file)->d_inode;
1789 ret = search_ioctl(inode, args);
1790 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1791 ret = -EFAULT;
1792 kfree(args);
1793 return ret;
1794}
1795
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001796/*
Chris Masonac8e9812010-02-28 15:39:26 -05001797 * Search INODE_REFs to identify path name of 'dirid' directory
1798 * in a 'tree_id' tree. and sets path name to 'name'.
1799 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001800static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1801 u64 tree_id, u64 dirid, char *name)
1802{
1803 struct btrfs_root *root;
1804 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001805 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001806 int ret = -1;
1807 int slot;
1808 int len;
1809 int total_len = 0;
1810 struct btrfs_inode_ref *iref;
1811 struct extent_buffer *l;
1812 struct btrfs_path *path;
1813
1814 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1815 name[0]='\0';
1816 return 0;
1817 }
1818
1819 path = btrfs_alloc_path();
1820 if (!path)
1821 return -ENOMEM;
1822
Chris Masonac8e9812010-02-28 15:39:26 -05001823 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001824
1825 key.objectid = tree_id;
1826 key.type = BTRFS_ROOT_ITEM_KEY;
1827 key.offset = (u64)-1;
1828 root = btrfs_read_fs_root_no_name(info, &key);
1829 if (IS_ERR(root)) {
1830 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001831 ret = -ENOENT;
1832 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001833 }
1834
1835 key.objectid = dirid;
1836 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001837 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001838
1839 while(1) {
1840 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1841 if (ret < 0)
1842 goto out;
1843
1844 l = path->nodes[0];
1845 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001846 if (ret > 0 && slot > 0)
1847 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001848 btrfs_item_key_to_cpu(l, &key, slot);
1849
1850 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001851 key.type != BTRFS_INODE_REF_KEY)) {
1852 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001853 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001854 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001855
1856 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1857 len = btrfs_inode_ref_name_len(l, iref);
1858 ptr -= len + 1;
1859 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001860 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001861 goto out;
1862
1863 *(ptr + len) = '/';
1864 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1865
1866 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1867 break;
1868
David Sterbab3b4aa72011-04-21 01:20:15 +02001869 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001870 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001871 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001872 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001873 }
Chris Masonac8e9812010-02-28 15:39:26 -05001874 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001875 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001876 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001877 name[total_len]='\0';
1878 ret = 0;
1879out:
1880 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001881 return ret;
1882}
1883
1884static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1885 void __user *argp)
1886{
1887 struct btrfs_ioctl_ino_lookup_args *args;
1888 struct inode *inode;
1889 int ret;
1890
1891 if (!capable(CAP_SYS_ADMIN))
1892 return -EPERM;
1893
Julia Lawall2354d082010-10-29 15:14:18 -04001894 args = memdup_user(argp, sizeof(*args));
1895 if (IS_ERR(args))
1896 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001897
Chris Masonac8e9812010-02-28 15:39:26 -05001898 inode = fdentry(file)->d_inode;
1899
Chris Mason1b53ac42010-03-18 12:17:05 -04001900 if (args->treeid == 0)
1901 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1902
Chris Masonac8e9812010-02-28 15:39:26 -05001903 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1904 args->treeid, args->objectid,
1905 args->name);
1906
1907 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1908 ret = -EFAULT;
1909
1910 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001911 return ret;
1912}
1913
Yan, Zheng76dda932009-09-21 16:00:26 -04001914static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1915 void __user *arg)
1916{
1917 struct dentry *parent = fdentry(file);
1918 struct dentry *dentry;
1919 struct inode *dir = parent->d_inode;
1920 struct inode *inode;
1921 struct btrfs_root *root = BTRFS_I(dir)->root;
1922 struct btrfs_root *dest = NULL;
1923 struct btrfs_ioctl_vol_args *vol_args;
1924 struct btrfs_trans_handle *trans;
1925 int namelen;
1926 int ret;
1927 int err = 0;
1928
Yan, Zheng76dda932009-09-21 16:00:26 -04001929 vol_args = memdup_user(arg, sizeof(*vol_args));
1930 if (IS_ERR(vol_args))
1931 return PTR_ERR(vol_args);
1932
1933 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1934 namelen = strlen(vol_args->name);
1935 if (strchr(vol_args->name, '/') ||
1936 strncmp(vol_args->name, "..", namelen) == 0) {
1937 err = -EINVAL;
1938 goto out;
1939 }
1940
Al Viroa561be72011-11-23 11:57:51 -05001941 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001942 if (err)
1943 goto out;
1944
1945 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1946 dentry = lookup_one_len(vol_args->name, parent, namelen);
1947 if (IS_ERR(dentry)) {
1948 err = PTR_ERR(dentry);
1949 goto out_unlock_dir;
1950 }
1951
1952 if (!dentry->d_inode) {
1953 err = -ENOENT;
1954 goto out_dput;
1955 }
1956
1957 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001958 dest = BTRFS_I(inode)->root;
1959 if (!capable(CAP_SYS_ADMIN)){
1960 /*
1961 * Regular user. Only allow this with a special mount
1962 * option, when the user has write+exec access to the
1963 * subvol root, and when rmdir(2) would have been
1964 * allowed.
1965 *
1966 * Note that this is _not_ check that the subvol is
1967 * empty or doesn't contain data that we wouldn't
1968 * otherwise be able to delete.
1969 *
1970 * Users who want to delete empty subvols should try
1971 * rmdir(2).
1972 */
1973 err = -EPERM;
1974 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1975 goto out_dput;
1976
1977 /*
1978 * Do not allow deletion if the parent dir is the same
1979 * as the dir to be deleted. That means the ioctl
1980 * must be called on the dentry referencing the root
1981 * of the subvol, not a random directory contained
1982 * within it.
1983 */
1984 err = -EINVAL;
1985 if (root == dest)
1986 goto out_dput;
1987
1988 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1989 if (err)
1990 goto out_dput;
1991
1992 /* check if subvolume may be deleted by a non-root user */
1993 err = btrfs_may_delete(dir, dentry, 1);
1994 if (err)
1995 goto out_dput;
1996 }
1997
Li Zefan33345d012011-04-20 10:31:50 +08001998 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001999 err = -EINVAL;
2000 goto out_dput;
2001 }
2002
Yan, Zheng76dda932009-09-21 16:00:26 -04002003 mutex_lock(&inode->i_mutex);
2004 err = d_invalidate(dentry);
2005 if (err)
2006 goto out_unlock;
2007
2008 down_write(&root->fs_info->subvol_sem);
2009
2010 err = may_destroy_subvol(dest);
2011 if (err)
2012 goto out_up_write;
2013
Yan, Zhenga22285a2010-05-16 10:48:46 -04002014 trans = btrfs_start_transaction(root, 0);
2015 if (IS_ERR(trans)) {
2016 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002017 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002018 }
2019 trans->block_rsv = &root->fs_info->global_block_rsv;
2020
Yan, Zheng76dda932009-09-21 16:00:26 -04002021 ret = btrfs_unlink_subvol(trans, root, dir,
2022 dest->root_key.objectid,
2023 dentry->d_name.name,
2024 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002025 if (ret) {
2026 err = ret;
2027 btrfs_abort_transaction(trans, root, ret);
2028 goto out_end_trans;
2029 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002030
2031 btrfs_record_root_in_trans(trans, dest);
2032
2033 memset(&dest->root_item.drop_progress, 0,
2034 sizeof(dest->root_item.drop_progress));
2035 dest->root_item.drop_level = 0;
2036 btrfs_set_root_refs(&dest->root_item, 0);
2037
Yan, Zhengd68fc572010-05-16 10:49:58 -04002038 if (!xchg(&dest->orphan_item_inserted, 1)) {
2039 ret = btrfs_insert_orphan_item(trans,
2040 root->fs_info->tree_root,
2041 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002042 if (ret) {
2043 btrfs_abort_transaction(trans, root, ret);
2044 err = ret;
2045 goto out_end_trans;
2046 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002047 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002048out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002049 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002050 if (ret && !err)
2051 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002052 inode->i_flags |= S_DEAD;
2053out_up_write:
2054 up_write(&root->fs_info->subvol_sem);
2055out_unlock:
2056 mutex_unlock(&inode->i_mutex);
2057 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002058 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002059 btrfs_invalidate_inodes(dest);
2060 d_delete(dentry);
2061 }
2062out_dput:
2063 dput(dentry);
2064out_unlock_dir:
2065 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002066 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002067out:
2068 kfree(vol_args);
2069 return err;
2070}
2071
Chris Mason1e701a32010-03-11 09:42:04 -05002072static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002073{
2074 struct inode *inode = fdentry(file)->d_inode;
2075 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002076 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002077 int ret;
2078
Li Zefanb83cc962010-12-20 16:04:08 +08002079 if (btrfs_root_readonly(root))
2080 return -EROFS;
2081
Al Viroa561be72011-11-23 11:57:51 -05002082 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002083 if (ret)
2084 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002085
2086 switch (inode->i_mode & S_IFMT) {
2087 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002088 if (!capable(CAP_SYS_ADMIN)) {
2089 ret = -EPERM;
2090 goto out;
2091 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002092 ret = btrfs_defrag_root(root, 0);
2093 if (ret)
2094 goto out;
2095 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002096 break;
2097 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002098 if (!(file->f_mode & FMODE_WRITE)) {
2099 ret = -EINVAL;
2100 goto out;
2101 }
Chris Mason1e701a32010-03-11 09:42:04 -05002102
2103 range = kzalloc(sizeof(*range), GFP_KERNEL);
2104 if (!range) {
2105 ret = -ENOMEM;
2106 goto out;
2107 }
2108
2109 if (argp) {
2110 if (copy_from_user(range, argp,
2111 sizeof(*range))) {
2112 ret = -EFAULT;
2113 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002114 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002115 }
2116 /* compression requires us to start the IO */
2117 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2118 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2119 range->extent_thresh = (u32)-1;
2120 }
2121 } else {
2122 /* the rest are all set to zero by kzalloc */
2123 range->len = (u64)-1;
2124 }
Chris Mason4cb53002011-05-24 15:35:30 -04002125 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2126 range, 0, 0);
2127 if (ret > 0)
2128 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002129 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002130 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002131 default:
2132 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002133 }
Chris Masone441d542009-01-05 16:57:23 -05002134out:
Al Viro2a79f172011-12-09 08:06:57 -05002135 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002136 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002137}
2138
Christoph Hellwigb2950862008-12-02 09:54:17 -05002139static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002140{
2141 struct btrfs_ioctl_vol_args *vol_args;
2142 int ret;
2143
Chris Masone441d542009-01-05 16:57:23 -05002144 if (!capable(CAP_SYS_ADMIN))
2145 return -EPERM;
2146
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002147 mutex_lock(&root->fs_info->volume_mutex);
2148 if (root->fs_info->balance_ctl) {
2149 printk(KERN_INFO "btrfs: balance in progress\n");
2150 ret = -EINVAL;
2151 goto out;
2152 }
2153
Li Zefandae7b662009-04-08 15:06:54 +08002154 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002155 if (IS_ERR(vol_args)) {
2156 ret = PTR_ERR(vol_args);
2157 goto out;
2158 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002159
Mark Fasheh5516e592008-07-24 12:20:14 -04002160 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002161 ret = btrfs_init_new_device(root, vol_args->name);
2162
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002163 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002164out:
2165 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002166 return ret;
2167}
2168
Christoph Hellwigb2950862008-12-02 09:54:17 -05002169static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002170{
2171 struct btrfs_ioctl_vol_args *vol_args;
2172 int ret;
2173
Chris Masone441d542009-01-05 16:57:23 -05002174 if (!capable(CAP_SYS_ADMIN))
2175 return -EPERM;
2176
Yan Zhengc146afa2008-11-12 14:34:12 -05002177 if (root->fs_info->sb->s_flags & MS_RDONLY)
2178 return -EROFS;
2179
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002180 mutex_lock(&root->fs_info->volume_mutex);
2181 if (root->fs_info->balance_ctl) {
2182 printk(KERN_INFO "btrfs: balance in progress\n");
2183 ret = -EINVAL;
2184 goto out;
2185 }
2186
Li Zefandae7b662009-04-08 15:06:54 +08002187 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002188 if (IS_ERR(vol_args)) {
2189 ret = PTR_ERR(vol_args);
2190 goto out;
2191 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002192
Mark Fasheh5516e592008-07-24 12:20:14 -04002193 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002194 ret = btrfs_rm_device(root, vol_args->name);
2195
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002196 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002197out:
2198 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002199 return ret;
2200}
2201
Jan Schmidt475f6382011-03-11 15:41:01 +01002202static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2203{
Li Zefan027ed2f2011-06-08 08:27:56 +00002204 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002205 struct btrfs_device *device;
2206 struct btrfs_device *next;
2207 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002208 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002209
2210 if (!capable(CAP_SYS_ADMIN))
2211 return -EPERM;
2212
Li Zefan027ed2f2011-06-08 08:27:56 +00002213 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2214 if (!fi_args)
2215 return -ENOMEM;
2216
2217 fi_args->num_devices = fs_devices->num_devices;
2218 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002219
2220 mutex_lock(&fs_devices->device_list_mutex);
2221 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002222 if (device->devid > fi_args->max_id)
2223 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002224 }
2225 mutex_unlock(&fs_devices->device_list_mutex);
2226
Li Zefan027ed2f2011-06-08 08:27:56 +00002227 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2228 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002229
Li Zefan027ed2f2011-06-08 08:27:56 +00002230 kfree(fi_args);
2231 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002232}
2233
2234static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2235{
2236 struct btrfs_ioctl_dev_info_args *di_args;
2237 struct btrfs_device *dev;
2238 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2239 int ret = 0;
2240 char *s_uuid = NULL;
2241 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2242
2243 if (!capable(CAP_SYS_ADMIN))
2244 return -EPERM;
2245
2246 di_args = memdup_user(arg, sizeof(*di_args));
2247 if (IS_ERR(di_args))
2248 return PTR_ERR(di_args);
2249
2250 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2251 s_uuid = di_args->uuid;
2252
2253 mutex_lock(&fs_devices->device_list_mutex);
2254 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2255 mutex_unlock(&fs_devices->device_list_mutex);
2256
2257 if (!dev) {
2258 ret = -ENODEV;
2259 goto out;
2260 }
2261
2262 di_args->devid = dev->devid;
2263 di_args->bytes_used = dev->bytes_used;
2264 di_args->total_bytes = dev->total_bytes;
2265 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002266 if (dev->name)
2267 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2268 else
2269 di_args->path[0] = '\0';
Jan Schmidt475f6382011-03-11 15:41:01 +01002270
2271out:
2272 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2273 ret = -EFAULT;
2274
2275 kfree(di_args);
2276 return ret;
2277}
2278
Yan, Zheng76dda932009-09-21 16:00:26 -04002279static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2280 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002281{
2282 struct inode *inode = fdentry(file)->d_inode;
2283 struct btrfs_root *root = BTRFS_I(inode)->root;
2284 struct file *src_file;
2285 struct inode *src;
2286 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002287 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002288 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002289 char *buf;
2290 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002291 u32 nritems;
2292 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002293 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002294 u64 len = olen;
2295 u64 bs = root->fs_info->sb->s_blocksize;
2296 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002297
Sage Weilc5c9cd42008-11-12 14:32:25 -05002298 /*
2299 * TODO:
2300 * - split compressed inline extents. annoying: we need to
2301 * decompress into destination's address_space (the file offset
2302 * may change, so source mapping won't do), then recompress (or
2303 * otherwise reinsert) a subrange.
2304 * - allow ranges within the same file to be cloned (provided
2305 * they don't overlap)?
2306 */
2307
Chris Masone441d542009-01-05 16:57:23 -05002308 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002309 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002310 return -EINVAL;
2311
Li Zefanb83cc962010-12-20 16:04:08 +08002312 if (btrfs_root_readonly(root))
2313 return -EROFS;
2314
Al Viroa561be72011-11-23 11:57:51 -05002315 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002316 if (ret)
2317 return ret;
2318
Sage Weilc5c9cd42008-11-12 14:32:25 -05002319 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002320 if (!src_file) {
2321 ret = -EBADF;
2322 goto out_drop_write;
2323 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002324
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002325 src = src_file->f_dentry->d_inode;
2326
Sage Weilc5c9cd42008-11-12 14:32:25 -05002327 ret = -EINVAL;
2328 if (src == inode)
2329 goto out_fput;
2330
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002331 /* the src must be open for reading */
2332 if (!(src_file->f_mode & FMODE_READ))
2333 goto out_fput;
2334
Li Zefan0e7b8242011-09-18 10:20:46 -04002335 /* don't make the dst file partly checksummed */
2336 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2337 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2338 goto out_fput;
2339
Yan Zhengae01a0a2008-08-04 23:23:47 -04002340 ret = -EISDIR;
2341 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002342 goto out_fput;
2343
Yan Zhengae01a0a2008-08-04 23:23:47 -04002344 ret = -EXDEV;
2345 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2346 goto out_fput;
2347
2348 ret = -ENOMEM;
2349 buf = vmalloc(btrfs_level_size(root, 0));
2350 if (!buf)
2351 goto out_fput;
2352
2353 path = btrfs_alloc_path();
2354 if (!path) {
2355 vfree(buf);
2356 goto out_fput;
2357 }
2358 path->reada = 2;
2359
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002360 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002361 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2362 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002363 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002364 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2365 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002366 }
2367
Sage Weilc5c9cd42008-11-12 14:32:25 -05002368 /* determine range to clone */
2369 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002370 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002371 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002372 if (len == 0)
2373 olen = len = src->i_size - off;
2374 /* if we extend to eof, continue to block boundary */
2375 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002376 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002377
2378 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002379 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2380 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002381 goto out_unlock;
2382
Li Zefand525e8a2011-09-11 10:52:25 -04002383 if (destoff > inode->i_size) {
2384 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2385 if (ret)
2386 goto out_unlock;
2387 }
2388
Li Zefan71ef0782011-09-18 10:20:46 -04002389 /* truncate page cache pages from target inode range */
2390 truncate_inode_pages_range(&inode->i_data, destoff,
2391 PAGE_CACHE_ALIGN(destoff + len) - 1);
2392
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002393 /* do any pending delalloc/csum calc on src, one way or
2394 another, and lock file content */
2395 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002396 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002397 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002398 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2399 if (!ordered &&
2400 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2401 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002402 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002403 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002404 if (ordered)
2405 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002406 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002407 }
2408
Sage Weilc5c9cd42008-11-12 14:32:25 -05002409 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002410 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002411 key.type = BTRFS_EXTENT_DATA_KEY;
2412 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413
2414 while (1) {
2415 /*
2416 * note the key will change type as we walk through the
2417 * tree.
2418 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002420 if (ret < 0)
2421 goto out;
2422
Yan Zhengae01a0a2008-08-04 23:23:47 -04002423 nritems = btrfs_header_nritems(path->nodes[0]);
2424 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002425 ret = btrfs_next_leaf(root, path);
2426 if (ret < 0)
2427 goto out;
2428 if (ret > 0)
2429 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002430 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002431 }
2432 leaf = path->nodes[0];
2433 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002434
Yan Zhengae01a0a2008-08-04 23:23:47 -04002435 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002436 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002437 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002438 break;
2439
Sage Weilc5c9cd42008-11-12 14:32:25 -05002440 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2441 struct btrfs_file_extent_item *extent;
2442 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002443 u32 size;
2444 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002445 u64 disko = 0, diskl = 0;
2446 u64 datao = 0, datal = 0;
2447 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002448 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002449
2450 size = btrfs_item_size_nr(leaf, slot);
2451 read_extent_buffer(leaf, buf,
2452 btrfs_item_ptr_offset(leaf, slot),
2453 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002454
2455 extent = btrfs_item_ptr(leaf, slot,
2456 struct btrfs_file_extent_item);
2457 comp = btrfs_file_extent_compression(leaf, extent);
2458 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002459 if (type == BTRFS_FILE_EXTENT_REG ||
2460 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002461 disko = btrfs_file_extent_disk_bytenr(leaf,
2462 extent);
2463 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2464 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002465 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002466 datal = btrfs_file_extent_num_bytes(leaf,
2467 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002468 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2469 /* take upper bound, may be compressed */
2470 datal = btrfs_file_extent_ram_bytes(leaf,
2471 extent);
2472 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002473 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002474
Sage Weil050006a2010-10-29 15:37:33 -04002475 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002476 key.offset >= off+len)
2477 goto next;
2478
Zheng Yan31840ae2008-09-23 13:14:14 -04002479 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002480 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002481 if (off <= key.offset)
2482 new_key.offset = key.offset + destoff - off;
2483 else
2484 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002485
Sage Weilb6f34092011-09-20 14:48:51 -04002486 /*
2487 * 1 - adjusting old extent (we may have to split it)
2488 * 1 - add new extent
2489 * 1 - inode update
2490 */
2491 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002492 if (IS_ERR(trans)) {
2493 ret = PTR_ERR(trans);
2494 goto out;
2495 }
2496
Chris Masonc8a894d2009-06-27 21:07:03 -04002497 if (type == BTRFS_FILE_EXTENT_REG ||
2498 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002499 /*
2500 * a | --- range to clone ---| b
2501 * | ------------- extent ------------- |
2502 */
2503
2504 /* substract range b */
2505 if (key.offset + datal > off + len)
2506 datal = off + len - key.offset;
2507
2508 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002509 if (off > key.offset) {
2510 datao += off - key.offset;
2511 datal -= off - key.offset;
2512 }
2513
Yan, Zhenga22285a2010-05-16 10:48:46 -04002514 ret = btrfs_drop_extents(trans, inode,
2515 new_key.offset,
2516 new_key.offset + datal,
2517 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002518 if (ret) {
2519 btrfs_abort_transaction(trans, root,
2520 ret);
2521 btrfs_end_transaction(trans, root);
2522 goto out;
2523 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002524
Sage Weilc5c9cd42008-11-12 14:32:25 -05002525 ret = btrfs_insert_empty_item(trans, root, path,
2526 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002527 if (ret) {
2528 btrfs_abort_transaction(trans, root,
2529 ret);
2530 btrfs_end_transaction(trans, root);
2531 goto out;
2532 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002533
2534 leaf = path->nodes[0];
2535 slot = path->slots[0];
2536 write_extent_buffer(leaf, buf,
2537 btrfs_item_ptr_offset(leaf, slot),
2538 size);
2539
2540 extent = btrfs_item_ptr(leaf, slot,
2541 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002542
Sage Weilc5c9cd42008-11-12 14:32:25 -05002543 /* disko == 0 means it's a hole */
2544 if (!disko)
2545 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002546
2547 btrfs_set_file_extent_offset(leaf, extent,
2548 datao);
2549 btrfs_set_file_extent_num_bytes(leaf, extent,
2550 datal);
2551 if (disko) {
2552 inode_add_bytes(inode, datal);
2553 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002554 disko, diskl, 0,
2555 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002556 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002557 new_key.offset - datao,
2558 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559 if (ret) {
2560 btrfs_abort_transaction(trans,
2561 root,
2562 ret);
2563 btrfs_end_transaction(trans,
2564 root);
2565 goto out;
2566
2567 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002568 }
2569 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2570 u64 skip = 0;
2571 u64 trim = 0;
2572 if (off > key.offset) {
2573 skip = off - key.offset;
2574 new_key.offset += skip;
2575 }
Chris Masond3977122009-01-05 21:25:51 -05002576
Sage Weilc5c9cd42008-11-12 14:32:25 -05002577 if (key.offset + datal > off+len)
2578 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002579
Sage Weilc5c9cd42008-11-12 14:32:25 -05002580 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002581 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002582 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002583 goto out;
2584 }
2585 size -= skip + trim;
2586 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002587
2588 ret = btrfs_drop_extents(trans, inode,
2589 new_key.offset,
2590 new_key.offset + datal,
2591 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002592 if (ret) {
2593 btrfs_abort_transaction(trans, root,
2594 ret);
2595 btrfs_end_transaction(trans, root);
2596 goto out;
2597 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002598
Sage Weilc5c9cd42008-11-12 14:32:25 -05002599 ret = btrfs_insert_empty_item(trans, root, path,
2600 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002601 if (ret) {
2602 btrfs_abort_transaction(trans, root,
2603 ret);
2604 btrfs_end_transaction(trans, root);
2605 goto out;
2606 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002607
2608 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002609 u32 start =
2610 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002611 memmove(buf+start, buf+start+skip,
2612 datal);
2613 }
2614
2615 leaf = path->nodes[0];
2616 slot = path->slots[0];
2617 write_extent_buffer(leaf, buf,
2618 btrfs_item_ptr_offset(leaf, slot),
2619 size);
2620 inode_add_bytes(inode, datal);
2621 }
2622
2623 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002624 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002625
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002626 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002627 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002628
2629 /*
2630 * we round up to the block size at eof when
2631 * determining which extents to clone above,
2632 * but shouldn't round up the file size
2633 */
2634 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002635 if (endoff > destoff+olen)
2636 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002637 if (endoff > inode->i_size)
2638 btrfs_i_size_write(inode, endoff);
2639
Yan, Zhenga22285a2010-05-16 10:48:46 -04002640 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002641 if (ret) {
2642 btrfs_abort_transaction(trans, root, ret);
2643 btrfs_end_transaction(trans, root);
2644 goto out;
2645 }
2646 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002647 }
Chris Masond3977122009-01-05 21:25:51 -05002648next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002649 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002650 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002651 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002652 ret = 0;
2653out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002654 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002655 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002656out_unlock:
2657 mutex_unlock(&src->i_mutex);
2658 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002659 vfree(buf);
2660 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002661out_fput:
2662 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002663out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002664 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002665 return ret;
2666}
2667
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002668static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002669{
2670 struct btrfs_ioctl_clone_range_args args;
2671
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002672 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002673 return -EFAULT;
2674 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2675 args.src_length, args.dest_offset);
2676}
2677
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002678/*
2679 * there are many ways the trans_start and trans_end ioctls can lead
2680 * to deadlocks. They should only be used by applications that
2681 * basically own the machine, and have a very in depth understanding
2682 * of all the possible deadlocks and enospc problems.
2683 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002684static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002685{
2686 struct inode *inode = fdentry(file)->d_inode;
2687 struct btrfs_root *root = BTRFS_I(inode)->root;
2688 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002689 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002690
Sage Weil1ab86ae2009-09-29 18:38:44 -04002691 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002692 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002693 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002694
2695 ret = -EINPROGRESS;
2696 if (file->private_data)
2697 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002698
Li Zefanb83cc962010-12-20 16:04:08 +08002699 ret = -EROFS;
2700 if (btrfs_root_readonly(root))
2701 goto out;
2702
Al Viroa561be72011-11-23 11:57:51 -05002703 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002704 if (ret)
2705 goto out;
2706
Josef Bacika4abeea2011-04-11 17:25:13 -04002707 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002708
Sage Weil1ab86ae2009-09-29 18:38:44 -04002709 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002710 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002711 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002712 goto out_drop;
2713
2714 file->private_data = trans;
2715 return 0;
2716
2717out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002718 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002719 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002720out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002721 return ret;
2722}
2723
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002724static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2725{
2726 struct inode *inode = fdentry(file)->d_inode;
2727 struct btrfs_root *root = BTRFS_I(inode)->root;
2728 struct btrfs_root *new_root;
2729 struct btrfs_dir_item *di;
2730 struct btrfs_trans_handle *trans;
2731 struct btrfs_path *path;
2732 struct btrfs_key location;
2733 struct btrfs_disk_key disk_key;
2734 struct btrfs_super_block *disk_super;
2735 u64 features;
2736 u64 objectid = 0;
2737 u64 dir_id;
2738
2739 if (!capable(CAP_SYS_ADMIN))
2740 return -EPERM;
2741
2742 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2743 return -EFAULT;
2744
2745 if (!objectid)
2746 objectid = root->root_key.objectid;
2747
2748 location.objectid = objectid;
2749 location.type = BTRFS_ROOT_ITEM_KEY;
2750 location.offset = (u64)-1;
2751
2752 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2753 if (IS_ERR(new_root))
2754 return PTR_ERR(new_root);
2755
2756 if (btrfs_root_refs(&new_root->root_item) == 0)
2757 return -ENOENT;
2758
2759 path = btrfs_alloc_path();
2760 if (!path)
2761 return -ENOMEM;
2762 path->leave_spinning = 1;
2763
2764 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002765 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002766 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002767 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002768 }
2769
David Sterba6c417612011-04-13 15:41:04 +02002770 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002771 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2772 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002773 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002774 btrfs_free_path(path);
2775 btrfs_end_transaction(trans, root);
2776 printk(KERN_ERR "Umm, you don't have the default dir item, "
2777 "this isn't going to work\n");
2778 return -ENOENT;
2779 }
2780
2781 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2782 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2783 btrfs_mark_buffer_dirty(path->nodes[0]);
2784 btrfs_free_path(path);
2785
David Sterba6c417612011-04-13 15:41:04 +02002786 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002787 features = btrfs_super_incompat_flags(disk_super);
2788 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2789 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2790 btrfs_set_super_incompat_flags(disk_super, features);
2791 }
2792 btrfs_end_transaction(trans, root);
2793
2794 return 0;
2795}
2796
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002797static void get_block_group_info(struct list_head *groups_list,
2798 struct btrfs_ioctl_space_info *space)
2799{
2800 struct btrfs_block_group_cache *block_group;
2801
2802 space->total_bytes = 0;
2803 space->used_bytes = 0;
2804 space->flags = 0;
2805 list_for_each_entry(block_group, groups_list, list) {
2806 space->flags = block_group->flags;
2807 space->total_bytes += block_group->key.offset;
2808 space->used_bytes +=
2809 btrfs_block_group_used(&block_group->item);
2810 }
2811}
2812
Josef Bacik1406e432010-01-13 18:19:06 +00002813long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2814{
2815 struct btrfs_ioctl_space_args space_args;
2816 struct btrfs_ioctl_space_info space;
2817 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002818 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002819 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002820 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002821 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2822 BTRFS_BLOCK_GROUP_SYSTEM,
2823 BTRFS_BLOCK_GROUP_METADATA,
2824 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2825 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002826 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002827 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002828 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002829 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002830
2831 if (copy_from_user(&space_args,
2832 (struct btrfs_ioctl_space_args __user *)arg,
2833 sizeof(space_args)))
2834 return -EFAULT;
2835
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002836 for (i = 0; i < num_types; i++) {
2837 struct btrfs_space_info *tmp;
2838
2839 info = NULL;
2840 rcu_read_lock();
2841 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2842 list) {
2843 if (tmp->flags == types[i]) {
2844 info = tmp;
2845 break;
2846 }
2847 }
2848 rcu_read_unlock();
2849
2850 if (!info)
2851 continue;
2852
2853 down_read(&info->groups_sem);
2854 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2855 if (!list_empty(&info->block_groups[c]))
2856 slot_count++;
2857 }
2858 up_read(&info->groups_sem);
2859 }
Josef Bacik1406e432010-01-13 18:19:06 +00002860
Chris Mason7fde62b2010-03-16 15:40:10 -04002861 /* space_slots == 0 means they are asking for a count */
2862 if (space_args.space_slots == 0) {
2863 space_args.total_spaces = slot_count;
2864 goto out;
2865 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002866
Dan Rosenberg51788b12011-02-14 16:04:23 -05002867 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002868
Chris Mason7fde62b2010-03-16 15:40:10 -04002869 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002870
Chris Mason7fde62b2010-03-16 15:40:10 -04002871 /* we generally have at most 6 or so space infos, one for each raid
2872 * level. So, a whole page should be more than enough for everyone
2873 */
2874 if (alloc_size > PAGE_CACHE_SIZE)
2875 return -ENOMEM;
2876
2877 space_args.total_spaces = 0;
2878 dest = kmalloc(alloc_size, GFP_NOFS);
2879 if (!dest)
2880 return -ENOMEM;
2881 dest_orig = dest;
2882
2883 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002884 for (i = 0; i < num_types; i++) {
2885 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002886
Dan Rosenberg51788b12011-02-14 16:04:23 -05002887 if (!slot_count)
2888 break;
2889
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002890 info = NULL;
2891 rcu_read_lock();
2892 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2893 list) {
2894 if (tmp->flags == types[i]) {
2895 info = tmp;
2896 break;
2897 }
2898 }
2899 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002900
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002901 if (!info)
2902 continue;
2903 down_read(&info->groups_sem);
2904 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2905 if (!list_empty(&info->block_groups[c])) {
2906 get_block_group_info(&info->block_groups[c],
2907 &space);
2908 memcpy(dest, &space, sizeof(space));
2909 dest++;
2910 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002911 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002912 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002913 if (!slot_count)
2914 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002915 }
2916 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002917 }
Josef Bacik1406e432010-01-13 18:19:06 +00002918
Chris Mason7fde62b2010-03-16 15:40:10 -04002919 user_dest = (struct btrfs_ioctl_space_info *)
2920 (arg + sizeof(struct btrfs_ioctl_space_args));
2921
2922 if (copy_to_user(user_dest, dest_orig, alloc_size))
2923 ret = -EFAULT;
2924
2925 kfree(dest_orig);
2926out:
2927 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002928 ret = -EFAULT;
2929
2930 return ret;
2931}
2932
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002933/*
2934 * there are many ways the trans_start and trans_end ioctls can lead
2935 * to deadlocks. They should only be used by applications that
2936 * basically own the machine, and have a very in depth understanding
2937 * of all the possible deadlocks and enospc problems.
2938 */
2939long btrfs_ioctl_trans_end(struct file *file)
2940{
2941 struct inode *inode = fdentry(file)->d_inode;
2942 struct btrfs_root *root = BTRFS_I(inode)->root;
2943 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002944
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002945 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002946 if (!trans)
2947 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002948 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002949
Sage Weil1ab86ae2009-09-29 18:38:44 -04002950 btrfs_end_transaction(trans, root);
2951
Josef Bacika4abeea2011-04-11 17:25:13 -04002952 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002953
Al Viro2a79f172011-12-09 08:06:57 -05002954 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002955 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002956}
2957
Sage Weil46204592010-10-29 15:41:32 -04002958static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2959{
2960 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2961 struct btrfs_trans_handle *trans;
2962 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002963 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002964
2965 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002966 if (IS_ERR(trans))
2967 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002968 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002969 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002970 if (ret) {
2971 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002972 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002973 }
Sage Weil46204592010-10-29 15:41:32 -04002974
2975 if (argp)
2976 if (copy_to_user(argp, &transid, sizeof(transid)))
2977 return -EFAULT;
2978 return 0;
2979}
2980
2981static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2982{
2983 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2984 u64 transid;
2985
2986 if (argp) {
2987 if (copy_from_user(&transid, argp, sizeof(transid)))
2988 return -EFAULT;
2989 } else {
2990 transid = 0; /* current trans */
2991 }
2992 return btrfs_wait_for_commit(root, transid);
2993}
2994
Jan Schmidt475f6382011-03-11 15:41:01 +01002995static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2996{
2997 int ret;
2998 struct btrfs_ioctl_scrub_args *sa;
2999
3000 if (!capable(CAP_SYS_ADMIN))
3001 return -EPERM;
3002
3003 sa = memdup_user(arg, sizeof(*sa));
3004 if (IS_ERR(sa))
3005 return PTR_ERR(sa);
3006
3007 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003008 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003009
3010 if (copy_to_user(arg, sa, sizeof(*sa)))
3011 ret = -EFAULT;
3012
3013 kfree(sa);
3014 return ret;
3015}
3016
3017static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3018{
3019 if (!capable(CAP_SYS_ADMIN))
3020 return -EPERM;
3021
3022 return btrfs_scrub_cancel(root);
3023}
3024
3025static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3026 void __user *arg)
3027{
3028 struct btrfs_ioctl_scrub_args *sa;
3029 int ret;
3030
3031 if (!capable(CAP_SYS_ADMIN))
3032 return -EPERM;
3033
3034 sa = memdup_user(arg, sizeof(*sa));
3035 if (IS_ERR(sa))
3036 return PTR_ERR(sa);
3037
3038 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3039
3040 if (copy_to_user(arg, sa, sizeof(*sa)))
3041 ret = -EFAULT;
3042
3043 kfree(sa);
3044 return ret;
3045}
3046
Jan Schmidtd7728c92011-07-07 16:48:38 +02003047static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3048{
3049 int ret = 0;
3050 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003051 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003052 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003053 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003054 struct inode_fs_paths *ipath = NULL;
3055 struct btrfs_path *path;
3056
3057 if (!capable(CAP_SYS_ADMIN))
3058 return -EPERM;
3059
3060 path = btrfs_alloc_path();
3061 if (!path) {
3062 ret = -ENOMEM;
3063 goto out;
3064 }
3065
3066 ipa = memdup_user(arg, sizeof(*ipa));
3067 if (IS_ERR(ipa)) {
3068 ret = PTR_ERR(ipa);
3069 ipa = NULL;
3070 goto out;
3071 }
3072
3073 size = min_t(u32, ipa->size, 4096);
3074 ipath = init_ipath(size, root, path);
3075 if (IS_ERR(ipath)) {
3076 ret = PTR_ERR(ipath);
3077 ipath = NULL;
3078 goto out;
3079 }
3080
3081 ret = paths_from_inode(ipa->inum, ipath);
3082 if (ret < 0)
3083 goto out;
3084
3085 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003086 rel_ptr = ipath->fspath->val[i] -
3087 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003088 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003089 }
3090
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003091 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3092 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003093 if (ret) {
3094 ret = -EFAULT;
3095 goto out;
3096 }
3097
3098out:
3099 btrfs_free_path(path);
3100 free_ipath(ipath);
3101 kfree(ipa);
3102
3103 return ret;
3104}
3105
3106static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3107{
3108 struct btrfs_data_container *inodes = ctx;
3109 const size_t c = 3 * sizeof(u64);
3110
3111 if (inodes->bytes_left >= c) {
3112 inodes->bytes_left -= c;
3113 inodes->val[inodes->elem_cnt] = inum;
3114 inodes->val[inodes->elem_cnt + 1] = offset;
3115 inodes->val[inodes->elem_cnt + 2] = root;
3116 inodes->elem_cnt += 3;
3117 } else {
3118 inodes->bytes_missing += c - inodes->bytes_left;
3119 inodes->bytes_left = 0;
3120 inodes->elem_missed += 3;
3121 }
3122
3123 return 0;
3124}
3125
3126static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3127 void __user *arg)
3128{
3129 int ret = 0;
3130 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003131 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003132 struct btrfs_ioctl_logical_ino_args *loi;
3133 struct btrfs_data_container *inodes = NULL;
3134 struct btrfs_path *path = NULL;
3135 struct btrfs_key key;
3136
3137 if (!capable(CAP_SYS_ADMIN))
3138 return -EPERM;
3139
3140 loi = memdup_user(arg, sizeof(*loi));
3141 if (IS_ERR(loi)) {
3142 ret = PTR_ERR(loi);
3143 loi = NULL;
3144 goto out;
3145 }
3146
3147 path = btrfs_alloc_path();
3148 if (!path) {
3149 ret = -ENOMEM;
3150 goto out;
3151 }
3152
3153 size = min_t(u32, loi->size, 4096);
3154 inodes = init_data_container(size);
3155 if (IS_ERR(inodes)) {
3156 ret = PTR_ERR(inodes);
3157 inodes = NULL;
3158 goto out;
3159 }
3160
3161 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003162 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003163
3164 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3165 ret = -ENOENT;
3166 if (ret < 0)
3167 goto out;
3168
Jan Schmidt4692cf52011-12-02 14:56:41 +01003169 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003170 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3171 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003172 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003173
3174 if (ret < 0)
3175 goto out;
3176
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003177 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3178 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003179 if (ret)
3180 ret = -EFAULT;
3181
3182out:
3183 btrfs_free_path(path);
3184 kfree(inodes);
3185 kfree(loi);
3186
3187 return ret;
3188}
3189
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003190void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003191 struct btrfs_ioctl_balance_args *bargs)
3192{
3193 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3194
3195 bargs->flags = bctl->flags;
3196
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003197 if (atomic_read(&fs_info->balance_running))
3198 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3199 if (atomic_read(&fs_info->balance_pause_req))
3200 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003201 if (atomic_read(&fs_info->balance_cancel_req))
3202 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003203
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003204 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3205 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3206 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003207
3208 if (lock) {
3209 spin_lock(&fs_info->balance_lock);
3210 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3211 spin_unlock(&fs_info->balance_lock);
3212 } else {
3213 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3214 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003215}
3216
3217static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3218{
3219 struct btrfs_fs_info *fs_info = root->fs_info;
3220 struct btrfs_ioctl_balance_args *bargs;
3221 struct btrfs_balance_control *bctl;
3222 int ret;
3223
3224 if (!capable(CAP_SYS_ADMIN))
3225 return -EPERM;
3226
3227 if (fs_info->sb->s_flags & MS_RDONLY)
3228 return -EROFS;
3229
3230 mutex_lock(&fs_info->volume_mutex);
3231 mutex_lock(&fs_info->balance_mutex);
3232
3233 if (arg) {
3234 bargs = memdup_user(arg, sizeof(*bargs));
3235 if (IS_ERR(bargs)) {
3236 ret = PTR_ERR(bargs);
3237 goto out;
3238 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003239
3240 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3241 if (!fs_info->balance_ctl) {
3242 ret = -ENOTCONN;
3243 goto out_bargs;
3244 }
3245
3246 bctl = fs_info->balance_ctl;
3247 spin_lock(&fs_info->balance_lock);
3248 bctl->flags |= BTRFS_BALANCE_RESUME;
3249 spin_unlock(&fs_info->balance_lock);
3250
3251 goto do_balance;
3252 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003253 } else {
3254 bargs = NULL;
3255 }
3256
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003257 if (fs_info->balance_ctl) {
3258 ret = -EINPROGRESS;
3259 goto out_bargs;
3260 }
3261
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003262 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3263 if (!bctl) {
3264 ret = -ENOMEM;
3265 goto out_bargs;
3266 }
3267
3268 bctl->fs_info = fs_info;
3269 if (arg) {
3270 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3271 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3272 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3273
3274 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003275 } else {
3276 /* balance everything - no filters */
3277 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003278 }
3279
Ilya Dryomovde322262012-01-16 22:04:49 +02003280do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003281 ret = btrfs_balance(bctl, bargs);
3282 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003283 * bctl is freed in __cancel_balance or in free_fs_info if
3284 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003285 */
3286 if (arg) {
3287 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3288 ret = -EFAULT;
3289 }
3290
3291out_bargs:
3292 kfree(bargs);
3293out:
3294 mutex_unlock(&fs_info->balance_mutex);
3295 mutex_unlock(&fs_info->volume_mutex);
3296 return ret;
3297}
3298
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003299static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3300{
3301 if (!capable(CAP_SYS_ADMIN))
3302 return -EPERM;
3303
3304 switch (cmd) {
3305 case BTRFS_BALANCE_CTL_PAUSE:
3306 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003307 case BTRFS_BALANCE_CTL_CANCEL:
3308 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003309 }
3310
3311 return -EINVAL;
3312}
3313
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003314static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3315 void __user *arg)
3316{
3317 struct btrfs_fs_info *fs_info = root->fs_info;
3318 struct btrfs_ioctl_balance_args *bargs;
3319 int ret = 0;
3320
3321 if (!capable(CAP_SYS_ADMIN))
3322 return -EPERM;
3323
3324 mutex_lock(&fs_info->balance_mutex);
3325 if (!fs_info->balance_ctl) {
3326 ret = -ENOTCONN;
3327 goto out;
3328 }
3329
3330 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3331 if (!bargs) {
3332 ret = -ENOMEM;
3333 goto out;
3334 }
3335
3336 update_ioctl_balance_args(fs_info, 1, bargs);
3337
3338 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3339 ret = -EFAULT;
3340
3341 kfree(bargs);
3342out:
3343 mutex_unlock(&fs_info->balance_mutex);
3344 return ret;
3345}
3346
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003347long btrfs_ioctl(struct file *file, unsigned int
3348 cmd, unsigned long arg)
3349{
3350 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003351 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003352
3353 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003354 case FS_IOC_GETFLAGS:
3355 return btrfs_ioctl_getflags(file, argp);
3356 case FS_IOC_SETFLAGS:
3357 return btrfs_ioctl_setflags(file, argp);
3358 case FS_IOC_GETVERSION:
3359 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003360 case FITRIM:
3361 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003362 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003363 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003364 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003365 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003366 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003367 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003368 case BTRFS_IOC_SNAP_DESTROY:
3369 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003370 case BTRFS_IOC_SUBVOL_GETFLAGS:
3371 return btrfs_ioctl_subvol_getflags(file, argp);
3372 case BTRFS_IOC_SUBVOL_SETFLAGS:
3373 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003374 case BTRFS_IOC_DEFAULT_SUBVOL:
3375 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003376 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003377 return btrfs_ioctl_defrag(file, NULL);
3378 case BTRFS_IOC_DEFRAG_RANGE:
3379 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003380 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003381 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003382 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003383 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003384 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003385 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003386 case BTRFS_IOC_FS_INFO:
3387 return btrfs_ioctl_fs_info(root, argp);
3388 case BTRFS_IOC_DEV_INFO:
3389 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003390 case BTRFS_IOC_BALANCE:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003391 return btrfs_ioctl_balance(root, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003392 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003393 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3394 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003395 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003396 case BTRFS_IOC_TRANS_START:
3397 return btrfs_ioctl_trans_start(file);
3398 case BTRFS_IOC_TRANS_END:
3399 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003400 case BTRFS_IOC_TREE_SEARCH:
3401 return btrfs_ioctl_tree_search(file, argp);
3402 case BTRFS_IOC_INO_LOOKUP:
3403 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003404 case BTRFS_IOC_INO_PATHS:
3405 return btrfs_ioctl_ino_to_path(root, argp);
3406 case BTRFS_IOC_LOGICAL_INO:
3407 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003408 case BTRFS_IOC_SPACE_INFO:
3409 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003410 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003411 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3412 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003413 case BTRFS_IOC_START_SYNC:
3414 return btrfs_ioctl_start_sync(file, argp);
3415 case BTRFS_IOC_WAIT_SYNC:
3416 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003417 case BTRFS_IOC_SCRUB:
3418 return btrfs_ioctl_scrub(root, argp);
3419 case BTRFS_IOC_SCRUB_CANCEL:
3420 return btrfs_ioctl_scrub_cancel(root, argp);
3421 case BTRFS_IOC_SCRUB_PROGRESS:
3422 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003423 case BTRFS_IOC_BALANCE_V2:
3424 return btrfs_ioctl_balance(root, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003425 case BTRFS_IOC_BALANCE_CTL:
3426 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003427 case BTRFS_IOC_BALANCE_PROGRESS:
3428 return btrfs_ioctl_balance_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003429 }
3430
3431 return -ENOTTY;
3432}