blob: 3d8ab27622cd7fd3463409c1c5b3cb2e8f7ca020 [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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-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 Zefanf062abf02011-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);
liubo2d4e6f6ad2011-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 Bacikd82a6f1d2011-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 Lawall2354d08f2010-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 Lawall2354d08f2010-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));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002266 if (dev->name) {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002267 strncpy(di_args->path, dev->name, sizeof(di_args->path));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002268 di_args->path[sizeof(di_args->path) - 1] = 0;
2269 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002270 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002271 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002272
2273out:
2274 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2275 ret = -EFAULT;
2276
2277 kfree(di_args);
2278 return ret;
2279}
2280
Yan, Zheng76dda932009-09-21 16:00:26 -04002281static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2282 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002283{
2284 struct inode *inode = fdentry(file)->d_inode;
2285 struct btrfs_root *root = BTRFS_I(inode)->root;
2286 struct file *src_file;
2287 struct inode *src;
2288 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002289 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002290 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002291 char *buf;
2292 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002293 u32 nritems;
2294 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002295 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002296 u64 len = olen;
2297 u64 bs = root->fs_info->sb->s_blocksize;
2298 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002299
Sage Weilc5c9cd42008-11-12 14:32:25 -05002300 /*
2301 * TODO:
2302 * - split compressed inline extents. annoying: we need to
2303 * decompress into destination's address_space (the file offset
2304 * may change, so source mapping won't do), then recompress (or
2305 * otherwise reinsert) a subrange.
2306 * - allow ranges within the same file to be cloned (provided
2307 * they don't overlap)?
2308 */
2309
Chris Masone441d542009-01-05 16:57:23 -05002310 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002311 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002312 return -EINVAL;
2313
Li Zefanb83cc962010-12-20 16:04:08 +08002314 if (btrfs_root_readonly(root))
2315 return -EROFS;
2316
Al Viroa561be72011-11-23 11:57:51 -05002317 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002318 if (ret)
2319 return ret;
2320
Sage Weilc5c9cd42008-11-12 14:32:25 -05002321 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002322 if (!src_file) {
2323 ret = -EBADF;
2324 goto out_drop_write;
2325 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002326
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002327 src = src_file->f_dentry->d_inode;
2328
Sage Weilc5c9cd42008-11-12 14:32:25 -05002329 ret = -EINVAL;
2330 if (src == inode)
2331 goto out_fput;
2332
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002333 /* the src must be open for reading */
2334 if (!(src_file->f_mode & FMODE_READ))
2335 goto out_fput;
2336
Li Zefan0e7b8242011-09-18 10:20:46 -04002337 /* don't make the dst file partly checksummed */
2338 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2339 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2340 goto out_fput;
2341
Yan Zhengae01a0a2008-08-04 23:23:47 -04002342 ret = -EISDIR;
2343 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002344 goto out_fput;
2345
Yan Zhengae01a0a2008-08-04 23:23:47 -04002346 ret = -EXDEV;
2347 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2348 goto out_fput;
2349
2350 ret = -ENOMEM;
2351 buf = vmalloc(btrfs_level_size(root, 0));
2352 if (!buf)
2353 goto out_fput;
2354
2355 path = btrfs_alloc_path();
2356 if (!path) {
2357 vfree(buf);
2358 goto out_fput;
2359 }
2360 path->reada = 2;
2361
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002362 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002363 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2364 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002365 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002366 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2367 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002368 }
2369
Sage Weilc5c9cd42008-11-12 14:32:25 -05002370 /* determine range to clone */
2371 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002372 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002373 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002374 if (len == 0)
2375 olen = len = src->i_size - off;
2376 /* if we extend to eof, continue to block boundary */
2377 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002378 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002379
2380 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002381 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2382 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002383 goto out_unlock;
2384
Li Zefand525e8a2011-09-11 10:52:25 -04002385 if (destoff > inode->i_size) {
2386 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2387 if (ret)
2388 goto out_unlock;
2389 }
2390
Li Zefan71ef0782011-09-18 10:20:46 -04002391 /* truncate page cache pages from target inode range */
2392 truncate_inode_pages_range(&inode->i_data, destoff,
2393 PAGE_CACHE_ALIGN(destoff + len) - 1);
2394
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002395 /* do any pending delalloc/csum calc on src, one way or
2396 another, and lock file content */
2397 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002398 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002399 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002400 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2401 if (!ordered &&
2402 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2403 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002404 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002405 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002406 if (ordered)
2407 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002408 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002409 }
2410
Sage Weilc5c9cd42008-11-12 14:32:25 -05002411 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002412 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002413 key.type = BTRFS_EXTENT_DATA_KEY;
2414 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002415
2416 while (1) {
2417 /*
2418 * note the key will change type as we walk through the
2419 * tree.
2420 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002421 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002422 if (ret < 0)
2423 goto out;
2424
Yan Zhengae01a0a2008-08-04 23:23:47 -04002425 nritems = btrfs_header_nritems(path->nodes[0]);
2426 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002427 ret = btrfs_next_leaf(root, path);
2428 if (ret < 0)
2429 goto out;
2430 if (ret > 0)
2431 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002432 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002433 }
2434 leaf = path->nodes[0];
2435 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002436
Yan Zhengae01a0a2008-08-04 23:23:47 -04002437 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002438 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002439 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002440 break;
2441
Sage Weilc5c9cd42008-11-12 14:32:25 -05002442 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2443 struct btrfs_file_extent_item *extent;
2444 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002445 u32 size;
2446 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002447 u64 disko = 0, diskl = 0;
2448 u64 datao = 0, datal = 0;
2449 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002450 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002451
2452 size = btrfs_item_size_nr(leaf, slot);
2453 read_extent_buffer(leaf, buf,
2454 btrfs_item_ptr_offset(leaf, slot),
2455 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002456
2457 extent = btrfs_item_ptr(leaf, slot,
2458 struct btrfs_file_extent_item);
2459 comp = btrfs_file_extent_compression(leaf, extent);
2460 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002461 if (type == BTRFS_FILE_EXTENT_REG ||
2462 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002463 disko = btrfs_file_extent_disk_bytenr(leaf,
2464 extent);
2465 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2466 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002467 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002468 datal = btrfs_file_extent_num_bytes(leaf,
2469 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002470 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2471 /* take upper bound, may be compressed */
2472 datal = btrfs_file_extent_ram_bytes(leaf,
2473 extent);
2474 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002475 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002476
Sage Weil050006a2010-10-29 15:37:33 -04002477 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002478 key.offset >= off+len)
2479 goto next;
2480
Zheng Yan31840ae2008-09-23 13:14:14 -04002481 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002482 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002483 if (off <= key.offset)
2484 new_key.offset = key.offset + destoff - off;
2485 else
2486 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002487
Sage Weilb6f34092011-09-20 14:48:51 -04002488 /*
2489 * 1 - adjusting old extent (we may have to split it)
2490 * 1 - add new extent
2491 * 1 - inode update
2492 */
2493 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002494 if (IS_ERR(trans)) {
2495 ret = PTR_ERR(trans);
2496 goto out;
2497 }
2498
Chris Masonc8a894d2009-06-27 21:07:03 -04002499 if (type == BTRFS_FILE_EXTENT_REG ||
2500 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002501 /*
2502 * a | --- range to clone ---| b
2503 * | ------------- extent ------------- |
2504 */
2505
2506 /* substract range b */
2507 if (key.offset + datal > off + len)
2508 datal = off + len - key.offset;
2509
2510 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002511 if (off > key.offset) {
2512 datao += off - key.offset;
2513 datal -= off - key.offset;
2514 }
2515
Yan, Zhenga22285a2010-05-16 10:48:46 -04002516 ret = btrfs_drop_extents(trans, inode,
2517 new_key.offset,
2518 new_key.offset + datal,
2519 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002520 if (ret) {
2521 btrfs_abort_transaction(trans, root,
2522 ret);
2523 btrfs_end_transaction(trans, root);
2524 goto out;
2525 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002526
Sage Weilc5c9cd42008-11-12 14:32:25 -05002527 ret = btrfs_insert_empty_item(trans, root, path,
2528 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002529 if (ret) {
2530 btrfs_abort_transaction(trans, root,
2531 ret);
2532 btrfs_end_transaction(trans, root);
2533 goto out;
2534 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002535
2536 leaf = path->nodes[0];
2537 slot = path->slots[0];
2538 write_extent_buffer(leaf, buf,
2539 btrfs_item_ptr_offset(leaf, slot),
2540 size);
2541
2542 extent = btrfs_item_ptr(leaf, slot,
2543 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002544
Sage Weilc5c9cd42008-11-12 14:32:25 -05002545 /* disko == 0 means it's a hole */
2546 if (!disko)
2547 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002548
2549 btrfs_set_file_extent_offset(leaf, extent,
2550 datao);
2551 btrfs_set_file_extent_num_bytes(leaf, extent,
2552 datal);
2553 if (disko) {
2554 inode_add_bytes(inode, datal);
2555 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002556 disko, diskl, 0,
2557 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002558 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002559 new_key.offset - datao,
2560 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002561 if (ret) {
2562 btrfs_abort_transaction(trans,
2563 root,
2564 ret);
2565 btrfs_end_transaction(trans,
2566 root);
2567 goto out;
2568
2569 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002570 }
2571 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2572 u64 skip = 0;
2573 u64 trim = 0;
2574 if (off > key.offset) {
2575 skip = off - key.offset;
2576 new_key.offset += skip;
2577 }
Chris Masond3977122009-01-05 21:25:51 -05002578
Sage Weilc5c9cd42008-11-12 14:32:25 -05002579 if (key.offset + datal > off+len)
2580 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002581
Sage Weilc5c9cd42008-11-12 14:32:25 -05002582 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002583 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002584 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002585 goto out;
2586 }
2587 size -= skip + trim;
2588 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002589
2590 ret = btrfs_drop_extents(trans, inode,
2591 new_key.offset,
2592 new_key.offset + datal,
2593 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002594 if (ret) {
2595 btrfs_abort_transaction(trans, root,
2596 ret);
2597 btrfs_end_transaction(trans, root);
2598 goto out;
2599 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002600
Sage Weilc5c9cd42008-11-12 14:32:25 -05002601 ret = btrfs_insert_empty_item(trans, root, path,
2602 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002603 if (ret) {
2604 btrfs_abort_transaction(trans, root,
2605 ret);
2606 btrfs_end_transaction(trans, root);
2607 goto out;
2608 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002609
2610 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002611 u32 start =
2612 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002613 memmove(buf+start, buf+start+skip,
2614 datal);
2615 }
2616
2617 leaf = path->nodes[0];
2618 slot = path->slots[0];
2619 write_extent_buffer(leaf, buf,
2620 btrfs_item_ptr_offset(leaf, slot),
2621 size);
2622 inode_add_bytes(inode, datal);
2623 }
2624
2625 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002626 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002627
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002628 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002629 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002630
2631 /*
2632 * we round up to the block size at eof when
2633 * determining which extents to clone above,
2634 * but shouldn't round up the file size
2635 */
2636 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002637 if (endoff > destoff+olen)
2638 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002639 if (endoff > inode->i_size)
2640 btrfs_i_size_write(inode, endoff);
2641
Yan, Zhenga22285a2010-05-16 10:48:46 -04002642 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002643 if (ret) {
2644 btrfs_abort_transaction(trans, root, ret);
2645 btrfs_end_transaction(trans, root);
2646 goto out;
2647 }
2648 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002649 }
Chris Masond3977122009-01-05 21:25:51 -05002650next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002651 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002652 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002653 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002654 ret = 0;
2655out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002656 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002657 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002658out_unlock:
2659 mutex_unlock(&src->i_mutex);
2660 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002661 vfree(buf);
2662 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002663out_fput:
2664 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002665out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002666 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002667 return ret;
2668}
2669
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002670static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002671{
2672 struct btrfs_ioctl_clone_range_args args;
2673
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002674 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002675 return -EFAULT;
2676 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2677 args.src_length, args.dest_offset);
2678}
2679
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002680/*
2681 * there are many ways the trans_start and trans_end ioctls can lead
2682 * to deadlocks. They should only be used by applications that
2683 * basically own the machine, and have a very in depth understanding
2684 * of all the possible deadlocks and enospc problems.
2685 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002686static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002687{
2688 struct inode *inode = fdentry(file)->d_inode;
2689 struct btrfs_root *root = BTRFS_I(inode)->root;
2690 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002691 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002692
Sage Weil1ab86ae2009-09-29 18:38:44 -04002693 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002694 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002695 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002696
2697 ret = -EINPROGRESS;
2698 if (file->private_data)
2699 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002700
Li Zefanb83cc962010-12-20 16:04:08 +08002701 ret = -EROFS;
2702 if (btrfs_root_readonly(root))
2703 goto out;
2704
Al Viroa561be72011-11-23 11:57:51 -05002705 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002706 if (ret)
2707 goto out;
2708
Josef Bacika4abeea2011-04-11 17:25:13 -04002709 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002710
Sage Weil1ab86ae2009-09-29 18:38:44 -04002711 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002712 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002713 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002714 goto out_drop;
2715
2716 file->private_data = trans;
2717 return 0;
2718
2719out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002720 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002721 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002722out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002723 return ret;
2724}
2725
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002726static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2727{
2728 struct inode *inode = fdentry(file)->d_inode;
2729 struct btrfs_root *root = BTRFS_I(inode)->root;
2730 struct btrfs_root *new_root;
2731 struct btrfs_dir_item *di;
2732 struct btrfs_trans_handle *trans;
2733 struct btrfs_path *path;
2734 struct btrfs_key location;
2735 struct btrfs_disk_key disk_key;
2736 struct btrfs_super_block *disk_super;
2737 u64 features;
2738 u64 objectid = 0;
2739 u64 dir_id;
2740
2741 if (!capable(CAP_SYS_ADMIN))
2742 return -EPERM;
2743
2744 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2745 return -EFAULT;
2746
2747 if (!objectid)
2748 objectid = root->root_key.objectid;
2749
2750 location.objectid = objectid;
2751 location.type = BTRFS_ROOT_ITEM_KEY;
2752 location.offset = (u64)-1;
2753
2754 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2755 if (IS_ERR(new_root))
2756 return PTR_ERR(new_root);
2757
2758 if (btrfs_root_refs(&new_root->root_item) == 0)
2759 return -ENOENT;
2760
2761 path = btrfs_alloc_path();
2762 if (!path)
2763 return -ENOMEM;
2764 path->leave_spinning = 1;
2765
2766 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002767 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002768 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002769 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002770 }
2771
David Sterba6c417612011-04-13 15:41:04 +02002772 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002773 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2774 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002775 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002776 btrfs_free_path(path);
2777 btrfs_end_transaction(trans, root);
2778 printk(KERN_ERR "Umm, you don't have the default dir item, "
2779 "this isn't going to work\n");
2780 return -ENOENT;
2781 }
2782
2783 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2784 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2785 btrfs_mark_buffer_dirty(path->nodes[0]);
2786 btrfs_free_path(path);
2787
David Sterba6c417612011-04-13 15:41:04 +02002788 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002789 features = btrfs_super_incompat_flags(disk_super);
2790 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2791 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2792 btrfs_set_super_incompat_flags(disk_super, features);
2793 }
2794 btrfs_end_transaction(trans, root);
2795
2796 return 0;
2797}
2798
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002799static void get_block_group_info(struct list_head *groups_list,
2800 struct btrfs_ioctl_space_info *space)
2801{
2802 struct btrfs_block_group_cache *block_group;
2803
2804 space->total_bytes = 0;
2805 space->used_bytes = 0;
2806 space->flags = 0;
2807 list_for_each_entry(block_group, groups_list, list) {
2808 space->flags = block_group->flags;
2809 space->total_bytes += block_group->key.offset;
2810 space->used_bytes +=
2811 btrfs_block_group_used(&block_group->item);
2812 }
2813}
2814
Josef Bacik1406e432010-01-13 18:19:06 +00002815long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2816{
2817 struct btrfs_ioctl_space_args space_args;
2818 struct btrfs_ioctl_space_info space;
2819 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002820 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002821 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002822 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002823 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2824 BTRFS_BLOCK_GROUP_SYSTEM,
2825 BTRFS_BLOCK_GROUP_METADATA,
2826 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2827 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002828 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002829 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002830 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002831 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002832
2833 if (copy_from_user(&space_args,
2834 (struct btrfs_ioctl_space_args __user *)arg,
2835 sizeof(space_args)))
2836 return -EFAULT;
2837
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002838 for (i = 0; i < num_types; i++) {
2839 struct btrfs_space_info *tmp;
2840
2841 info = NULL;
2842 rcu_read_lock();
2843 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2844 list) {
2845 if (tmp->flags == types[i]) {
2846 info = tmp;
2847 break;
2848 }
2849 }
2850 rcu_read_unlock();
2851
2852 if (!info)
2853 continue;
2854
2855 down_read(&info->groups_sem);
2856 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2857 if (!list_empty(&info->block_groups[c]))
2858 slot_count++;
2859 }
2860 up_read(&info->groups_sem);
2861 }
Josef Bacik1406e432010-01-13 18:19:06 +00002862
Chris Mason7fde62b2010-03-16 15:40:10 -04002863 /* space_slots == 0 means they are asking for a count */
2864 if (space_args.space_slots == 0) {
2865 space_args.total_spaces = slot_count;
2866 goto out;
2867 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002868
Dan Rosenberg51788b12011-02-14 16:04:23 -05002869 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002870
Chris Mason7fde62b2010-03-16 15:40:10 -04002871 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002872
Chris Mason7fde62b2010-03-16 15:40:10 -04002873 /* we generally have at most 6 or so space infos, one for each raid
2874 * level. So, a whole page should be more than enough for everyone
2875 */
2876 if (alloc_size > PAGE_CACHE_SIZE)
2877 return -ENOMEM;
2878
2879 space_args.total_spaces = 0;
2880 dest = kmalloc(alloc_size, GFP_NOFS);
2881 if (!dest)
2882 return -ENOMEM;
2883 dest_orig = dest;
2884
2885 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002886 for (i = 0; i < num_types; i++) {
2887 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002888
Dan Rosenberg51788b12011-02-14 16:04:23 -05002889 if (!slot_count)
2890 break;
2891
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002892 info = NULL;
2893 rcu_read_lock();
2894 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2895 list) {
2896 if (tmp->flags == types[i]) {
2897 info = tmp;
2898 break;
2899 }
2900 }
2901 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002902
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002903 if (!info)
2904 continue;
2905 down_read(&info->groups_sem);
2906 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2907 if (!list_empty(&info->block_groups[c])) {
2908 get_block_group_info(&info->block_groups[c],
2909 &space);
2910 memcpy(dest, &space, sizeof(space));
2911 dest++;
2912 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002913 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002914 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002915 if (!slot_count)
2916 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002917 }
2918 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002919 }
Josef Bacik1406e432010-01-13 18:19:06 +00002920
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08002921 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04002922 (arg + sizeof(struct btrfs_ioctl_space_args));
2923
2924 if (copy_to_user(user_dest, dest_orig, alloc_size))
2925 ret = -EFAULT;
2926
2927 kfree(dest_orig);
2928out:
2929 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002930 ret = -EFAULT;
2931
2932 return ret;
2933}
2934
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002935/*
2936 * there are many ways the trans_start and trans_end ioctls can lead
2937 * to deadlocks. They should only be used by applications that
2938 * basically own the machine, and have a very in depth understanding
2939 * of all the possible deadlocks and enospc problems.
2940 */
2941long btrfs_ioctl_trans_end(struct file *file)
2942{
2943 struct inode *inode = fdentry(file)->d_inode;
2944 struct btrfs_root *root = BTRFS_I(inode)->root;
2945 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002946
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002947 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002948 if (!trans)
2949 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002950 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002951
Sage Weil1ab86ae2009-09-29 18:38:44 -04002952 btrfs_end_transaction(trans, root);
2953
Josef Bacika4abeea2011-04-11 17:25:13 -04002954 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002955
Al Viro2a79f172011-12-09 08:06:57 -05002956 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002957 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002958}
2959
Sage Weil46204592010-10-29 15:41:32 -04002960static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2961{
2962 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2963 struct btrfs_trans_handle *trans;
2964 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002965 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002966
2967 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002968 if (IS_ERR(trans))
2969 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002970 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002971 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002972 if (ret) {
2973 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002974 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002975 }
Sage Weil46204592010-10-29 15:41:32 -04002976
2977 if (argp)
2978 if (copy_to_user(argp, &transid, sizeof(transid)))
2979 return -EFAULT;
2980 return 0;
2981}
2982
2983static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2984{
2985 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2986 u64 transid;
2987
2988 if (argp) {
2989 if (copy_from_user(&transid, argp, sizeof(transid)))
2990 return -EFAULT;
2991 } else {
2992 transid = 0; /* current trans */
2993 }
2994 return btrfs_wait_for_commit(root, transid);
2995}
2996
Jan Schmidt475f6382011-03-11 15:41:01 +01002997static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2998{
2999 int ret;
3000 struct btrfs_ioctl_scrub_args *sa;
3001
3002 if (!capable(CAP_SYS_ADMIN))
3003 return -EPERM;
3004
3005 sa = memdup_user(arg, sizeof(*sa));
3006 if (IS_ERR(sa))
3007 return PTR_ERR(sa);
3008
3009 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003010 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003011
3012 if (copy_to_user(arg, sa, sizeof(*sa)))
3013 ret = -EFAULT;
3014
3015 kfree(sa);
3016 return ret;
3017}
3018
3019static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3020{
3021 if (!capable(CAP_SYS_ADMIN))
3022 return -EPERM;
3023
3024 return btrfs_scrub_cancel(root);
3025}
3026
3027static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3028 void __user *arg)
3029{
3030 struct btrfs_ioctl_scrub_args *sa;
3031 int ret;
3032
3033 if (!capable(CAP_SYS_ADMIN))
3034 return -EPERM;
3035
3036 sa = memdup_user(arg, sizeof(*sa));
3037 if (IS_ERR(sa))
3038 return PTR_ERR(sa);
3039
3040 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3041
3042 if (copy_to_user(arg, sa, sizeof(*sa)))
3043 ret = -EFAULT;
3044
3045 kfree(sa);
3046 return ret;
3047}
3048
Jan Schmidtd7728c92011-07-07 16:48:38 +02003049static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3050{
3051 int ret = 0;
3052 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003053 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003054 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003055 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003056 struct inode_fs_paths *ipath = NULL;
3057 struct btrfs_path *path;
3058
3059 if (!capable(CAP_SYS_ADMIN))
3060 return -EPERM;
3061
3062 path = btrfs_alloc_path();
3063 if (!path) {
3064 ret = -ENOMEM;
3065 goto out;
3066 }
3067
3068 ipa = memdup_user(arg, sizeof(*ipa));
3069 if (IS_ERR(ipa)) {
3070 ret = PTR_ERR(ipa);
3071 ipa = NULL;
3072 goto out;
3073 }
3074
3075 size = min_t(u32, ipa->size, 4096);
3076 ipath = init_ipath(size, root, path);
3077 if (IS_ERR(ipath)) {
3078 ret = PTR_ERR(ipath);
3079 ipath = NULL;
3080 goto out;
3081 }
3082
3083 ret = paths_from_inode(ipa->inum, ipath);
3084 if (ret < 0)
3085 goto out;
3086
3087 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003088 rel_ptr = ipath->fspath->val[i] -
3089 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003090 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003091 }
3092
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003093 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3094 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003095 if (ret) {
3096 ret = -EFAULT;
3097 goto out;
3098 }
3099
3100out:
3101 btrfs_free_path(path);
3102 free_ipath(ipath);
3103 kfree(ipa);
3104
3105 return ret;
3106}
3107
3108static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3109{
3110 struct btrfs_data_container *inodes = ctx;
3111 const size_t c = 3 * sizeof(u64);
3112
3113 if (inodes->bytes_left >= c) {
3114 inodes->bytes_left -= c;
3115 inodes->val[inodes->elem_cnt] = inum;
3116 inodes->val[inodes->elem_cnt + 1] = offset;
3117 inodes->val[inodes->elem_cnt + 2] = root;
3118 inodes->elem_cnt += 3;
3119 } else {
3120 inodes->bytes_missing += c - inodes->bytes_left;
3121 inodes->bytes_left = 0;
3122 inodes->elem_missed += 3;
3123 }
3124
3125 return 0;
3126}
3127
3128static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3129 void __user *arg)
3130{
3131 int ret = 0;
3132 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003133 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003134 struct btrfs_ioctl_logical_ino_args *loi;
3135 struct btrfs_data_container *inodes = NULL;
3136 struct btrfs_path *path = NULL;
3137 struct btrfs_key key;
3138
3139 if (!capable(CAP_SYS_ADMIN))
3140 return -EPERM;
3141
3142 loi = memdup_user(arg, sizeof(*loi));
3143 if (IS_ERR(loi)) {
3144 ret = PTR_ERR(loi);
3145 loi = NULL;
3146 goto out;
3147 }
3148
3149 path = btrfs_alloc_path();
3150 if (!path) {
3151 ret = -ENOMEM;
3152 goto out;
3153 }
3154
3155 size = min_t(u32, loi->size, 4096);
3156 inodes = init_data_container(size);
3157 if (IS_ERR(inodes)) {
3158 ret = PTR_ERR(inodes);
3159 inodes = NULL;
3160 goto out;
3161 }
3162
3163 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003164 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003165
3166 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3167 ret = -ENOENT;
3168 if (ret < 0)
3169 goto out;
3170
Jan Schmidt4692cf52011-12-02 14:56:41 +01003171 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003172 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3173 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003174 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003175
3176 if (ret < 0)
3177 goto out;
3178
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003179 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3180 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003181 if (ret)
3182 ret = -EFAULT;
3183
3184out:
3185 btrfs_free_path(path);
3186 kfree(inodes);
3187 kfree(loi);
3188
3189 return ret;
3190}
3191
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003192void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003193 struct btrfs_ioctl_balance_args *bargs)
3194{
3195 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3196
3197 bargs->flags = bctl->flags;
3198
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003199 if (atomic_read(&fs_info->balance_running))
3200 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3201 if (atomic_read(&fs_info->balance_pause_req))
3202 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003203 if (atomic_read(&fs_info->balance_cancel_req))
3204 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003205
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003206 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3207 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3208 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003209
3210 if (lock) {
3211 spin_lock(&fs_info->balance_lock);
3212 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3213 spin_unlock(&fs_info->balance_lock);
3214 } else {
3215 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3216 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003217}
3218
3219static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg)
3220{
3221 struct btrfs_fs_info *fs_info = root->fs_info;
3222 struct btrfs_ioctl_balance_args *bargs;
3223 struct btrfs_balance_control *bctl;
3224 int ret;
3225
3226 if (!capable(CAP_SYS_ADMIN))
3227 return -EPERM;
3228
3229 if (fs_info->sb->s_flags & MS_RDONLY)
3230 return -EROFS;
3231
3232 mutex_lock(&fs_info->volume_mutex);
3233 mutex_lock(&fs_info->balance_mutex);
3234
3235 if (arg) {
3236 bargs = memdup_user(arg, sizeof(*bargs));
3237 if (IS_ERR(bargs)) {
3238 ret = PTR_ERR(bargs);
3239 goto out;
3240 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003241
3242 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3243 if (!fs_info->balance_ctl) {
3244 ret = -ENOTCONN;
3245 goto out_bargs;
3246 }
3247
3248 bctl = fs_info->balance_ctl;
3249 spin_lock(&fs_info->balance_lock);
3250 bctl->flags |= BTRFS_BALANCE_RESUME;
3251 spin_unlock(&fs_info->balance_lock);
3252
3253 goto do_balance;
3254 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003255 } else {
3256 bargs = NULL;
3257 }
3258
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003259 if (fs_info->balance_ctl) {
3260 ret = -EINPROGRESS;
3261 goto out_bargs;
3262 }
3263
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003264 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3265 if (!bctl) {
3266 ret = -ENOMEM;
3267 goto out_bargs;
3268 }
3269
3270 bctl->fs_info = fs_info;
3271 if (arg) {
3272 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3273 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3274 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3275
3276 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003277 } else {
3278 /* balance everything - no filters */
3279 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003280 }
3281
Ilya Dryomovde322262012-01-16 22:04:49 +02003282do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003283 ret = btrfs_balance(bctl, bargs);
3284 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003285 * bctl is freed in __cancel_balance or in free_fs_info if
3286 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003287 */
3288 if (arg) {
3289 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3290 ret = -EFAULT;
3291 }
3292
3293out_bargs:
3294 kfree(bargs);
3295out:
3296 mutex_unlock(&fs_info->balance_mutex);
3297 mutex_unlock(&fs_info->volume_mutex);
3298 return ret;
3299}
3300
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003301static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3302{
3303 if (!capable(CAP_SYS_ADMIN))
3304 return -EPERM;
3305
3306 switch (cmd) {
3307 case BTRFS_BALANCE_CTL_PAUSE:
3308 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003309 case BTRFS_BALANCE_CTL_CANCEL:
3310 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003311 }
3312
3313 return -EINVAL;
3314}
3315
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003316static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3317 void __user *arg)
3318{
3319 struct btrfs_fs_info *fs_info = root->fs_info;
3320 struct btrfs_ioctl_balance_args *bargs;
3321 int ret = 0;
3322
3323 if (!capable(CAP_SYS_ADMIN))
3324 return -EPERM;
3325
3326 mutex_lock(&fs_info->balance_mutex);
3327 if (!fs_info->balance_ctl) {
3328 ret = -ENOTCONN;
3329 goto out;
3330 }
3331
3332 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3333 if (!bargs) {
3334 ret = -ENOMEM;
3335 goto out;
3336 }
3337
3338 update_ioctl_balance_args(fs_info, 1, bargs);
3339
3340 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3341 ret = -EFAULT;
3342
3343 kfree(bargs);
3344out:
3345 mutex_unlock(&fs_info->balance_mutex);
3346 return ret;
3347}
3348
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003349long btrfs_ioctl(struct file *file, unsigned int
3350 cmd, unsigned long arg)
3351{
3352 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003353 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003354
3355 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003356 case FS_IOC_GETFLAGS:
3357 return btrfs_ioctl_getflags(file, argp);
3358 case FS_IOC_SETFLAGS:
3359 return btrfs_ioctl_setflags(file, argp);
3360 case FS_IOC_GETVERSION:
3361 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003362 case FITRIM:
3363 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003364 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003365 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003366 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003367 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003368 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003369 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003370 case BTRFS_IOC_SNAP_DESTROY:
3371 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003372 case BTRFS_IOC_SUBVOL_GETFLAGS:
3373 return btrfs_ioctl_subvol_getflags(file, argp);
3374 case BTRFS_IOC_SUBVOL_SETFLAGS:
3375 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003376 case BTRFS_IOC_DEFAULT_SUBVOL:
3377 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003378 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003379 return btrfs_ioctl_defrag(file, NULL);
3380 case BTRFS_IOC_DEFRAG_RANGE:
3381 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003382 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003383 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003384 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003385 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003386 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003387 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003388 case BTRFS_IOC_FS_INFO:
3389 return btrfs_ioctl_fs_info(root, argp);
3390 case BTRFS_IOC_DEV_INFO:
3391 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003392 case BTRFS_IOC_BALANCE:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003393 return btrfs_ioctl_balance(root, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003394 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003395 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3396 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003397 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003398 case BTRFS_IOC_TRANS_START:
3399 return btrfs_ioctl_trans_start(file);
3400 case BTRFS_IOC_TRANS_END:
3401 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003402 case BTRFS_IOC_TREE_SEARCH:
3403 return btrfs_ioctl_tree_search(file, argp);
3404 case BTRFS_IOC_INO_LOOKUP:
3405 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003406 case BTRFS_IOC_INO_PATHS:
3407 return btrfs_ioctl_ino_to_path(root, argp);
3408 case BTRFS_IOC_LOGICAL_INO:
3409 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003410 case BTRFS_IOC_SPACE_INFO:
3411 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003412 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003413 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3414 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003415 case BTRFS_IOC_START_SYNC:
3416 return btrfs_ioctl_start_sync(file, argp);
3417 case BTRFS_IOC_WAIT_SYNC:
3418 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003419 case BTRFS_IOC_SCRUB:
3420 return btrfs_ioctl_scrub(root, argp);
3421 case BTRFS_IOC_SCRUB_CANCEL:
3422 return btrfs_ioctl_scrub_cancel(root, argp);
3423 case BTRFS_IOC_SCRUB_PROGRESS:
3424 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003425 case BTRFS_IOC_BALANCE_V2:
3426 return btrfs_ioctl_balance(root, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003427 case BTRFS_IOC_BALANCE_CTL:
3428 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003429 case BTRFS_IOC_BALANCE_PROGRESS:
3430 return btrfs_ioctl_balance_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003431 }
3432
3433 return -ENOTTY;
3434}