blob: 5441ff1480fdbbd9ce9fe4c9caa6fc0c62392a3c [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;
179
Li Zefanb83cc962010-12-20 16:04:08 +0800180 if (btrfs_root_readonly(root))
181 return -EROFS;
182
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183 if (copy_from_user(&flags, arg, sizeof(flags)))
184 return -EFAULT;
185
Liu Bo75e7cb72011-03-22 10:12:20 +0000186 ret = check_flags(flags);
187 if (ret)
188 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200189
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700190 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191 return -EACCES;
192
193 mutex_lock(&inode->i_mutex);
194
195 flags = btrfs_mask_flags(inode->i_mode, flags);
196 oldflags = btrfs_flags_to_ioctl(ip->flags);
197 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
198 if (!capable(CAP_LINUX_IMMUTABLE)) {
199 ret = -EPERM;
200 goto out_unlock;
201 }
202 }
203
Al Viroa561be72011-11-23 11:57:51 -0500204 ret = mnt_want_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200205 if (ret)
206 goto out_unlock;
207
208 if (flags & FS_SYNC_FL)
209 ip->flags |= BTRFS_INODE_SYNC;
210 else
211 ip->flags &= ~BTRFS_INODE_SYNC;
212 if (flags & FS_IMMUTABLE_FL)
213 ip->flags |= BTRFS_INODE_IMMUTABLE;
214 else
215 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
216 if (flags & FS_APPEND_FL)
217 ip->flags |= BTRFS_INODE_APPEND;
218 else
219 ip->flags &= ~BTRFS_INODE_APPEND;
220 if (flags & FS_NODUMP_FL)
221 ip->flags |= BTRFS_INODE_NODUMP;
222 else
223 ip->flags &= ~BTRFS_INODE_NODUMP;
224 if (flags & FS_NOATIME_FL)
225 ip->flags |= BTRFS_INODE_NOATIME;
226 else
227 ip->flags &= ~BTRFS_INODE_NOATIME;
228 if (flags & FS_DIRSYNC_FL)
229 ip->flags |= BTRFS_INODE_DIRSYNC;
230 else
231 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000232 if (flags & FS_NOCOW_FL)
233 ip->flags |= BTRFS_INODE_NODATACOW;
234 else
235 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200236
Liu Bo75e7cb72011-03-22 10:12:20 +0000237 /*
238 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
239 * flag may be changed automatically if compression code won't make
240 * things smaller.
241 */
242 if (flags & FS_NOCOMP_FL) {
243 ip->flags &= ~BTRFS_INODE_COMPRESS;
244 ip->flags |= BTRFS_INODE_NOCOMPRESS;
245 } else if (flags & FS_COMPR_FL) {
246 ip->flags |= BTRFS_INODE_COMPRESS;
247 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000248 } else {
249 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000250 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200251
Josef Bacik7a7eaa42011-04-13 12:54:33 -0400252 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000253 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200254
Li Zefan306424c2011-12-14 20:12:02 -0500255 btrfs_update_iflags(inode);
256 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200257 ret = btrfs_update_inode(trans, root, inode);
258 BUG_ON(ret);
259
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200260 btrfs_end_transaction(trans, root);
261
Al Viro2a79f172011-12-09 08:06:57 -0500262 mnt_drop_write_file(file);
liubo2d4e6f6a2011-02-24 09:38:16 +0000263
264 ret = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200265 out_unlock:
266 mutex_unlock(&inode->i_mutex);
liubo2d4e6f6a2011-02-24 09:38:16 +0000267 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200268}
269
270static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
271{
272 struct inode *inode = file->f_path.dentry->d_inode;
273
274 return put_user(inode->i_generation, arg);
275}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400276
Li Dongyangf7039b12011-03-24 10:24:28 +0000277static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
278{
279 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
280 struct btrfs_fs_info *fs_info = root->fs_info;
281 struct btrfs_device *device;
282 struct request_queue *q;
283 struct fstrim_range range;
284 u64 minlen = ULLONG_MAX;
285 u64 num_devices = 0;
David Sterba6c417612011-04-13 15:41:04 +0200286 u64 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000287 int ret;
288
289 if (!capable(CAP_SYS_ADMIN))
290 return -EPERM;
291
Xiao Guangrong1f781602011-04-20 10:09:16 +0000292 rcu_read_lock();
293 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
294 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000295 if (!device->bdev)
296 continue;
297 q = bdev_get_queue(device->bdev);
298 if (blk_queue_discard(q)) {
299 num_devices++;
300 minlen = min((u64)q->limits.discard_granularity,
301 minlen);
302 }
303 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000304 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200305
Li Dongyangf7039b12011-03-24 10:24:28 +0000306 if (!num_devices)
307 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000308 if (copy_from_user(&range, arg, sizeof(range)))
309 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200310 if (range.start > total_bytes)
311 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000312
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200313 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000314 range.minlen = max(range.minlen, minlen);
315 ret = btrfs_trim_fs(root, &range);
316 if (ret < 0)
317 return ret;
318
319 if (copy_to_user(arg, &range, sizeof(range)))
320 return -EFAULT;
321
322 return 0;
323}
324
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400325static noinline int create_subvol(struct btrfs_root *root,
326 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400327 char *name, int namelen,
328 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400329{
330 struct btrfs_trans_handle *trans;
331 struct btrfs_key key;
332 struct btrfs_root_item root_item;
333 struct btrfs_inode_item *inode_item;
334 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400335 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400336 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000337 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400338 int ret;
339 int err;
340 u64 objectid;
341 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500342 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400343
Li Zefan581bb052011-04-20 10:06:11 +0800344 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400345 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400346 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000347
348 dir = parent->d_inode;
349
Josef Bacik9ed74f22009-09-11 16:12:44 -0400350 /*
351 * 1 - inode item
352 * 2 - refs
353 * 1 - root item
354 * 2 - dir items
355 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400356 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400357 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400358 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400359
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
361 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400362 if (IS_ERR(leaf)) {
363 ret = PTR_ERR(leaf);
364 goto fail;
365 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400366
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400367 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368 btrfs_set_header_bytenr(leaf, leaf->start);
369 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 btrfs_set_header_owner(leaf, objectid);
372
373 write_extent_buffer(leaf, root->fs_info->fsid,
374 (unsigned long)btrfs_header_fsid(leaf),
375 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400376 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
377 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
378 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379 btrfs_mark_buffer_dirty(leaf);
380
381 inode_item = &root_item.inode;
382 memset(inode_item, 0, sizeof(*inode_item));
383 inode_item->generation = cpu_to_le64(1);
384 inode_item->size = cpu_to_le64(3);
385 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400386 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400387 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
388
Li Zefan08fe4db2011-03-28 02:01:25 +0000389 root_item.flags = 0;
390 root_item.byte_limit = 0;
391 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
392
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400393 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400394 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400395 btrfs_set_root_level(&root_item, 0);
396 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000397 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400398 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400399
400 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
401 root_item.drop_level = 0;
402
Chris Mason925baed2008-06-25 16:01:30 -0400403 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 free_extent_buffer(leaf);
405 leaf = NULL;
406
407 btrfs_set_root_dirid(&root_item, new_dirid);
408
409 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400410 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400411 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
412 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
413 &root_item);
414 if (ret)
415 goto fail;
416
Yan, Zheng76dda932009-09-21 16:00:26 -0400417 key.offset = (u64)-1;
418 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
419 BUG_ON(IS_ERR(new_root));
420
421 btrfs_record_root_in_trans(trans, new_root);
422
Josef Bacikd82a6f12011-05-11 15:26:06 -0400423 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400424 /*
425 * insert the directory item
426 */
Chris Mason3de45862008-11-17 21:02:50 -0500427 ret = btrfs_set_inode_index(dir, &index);
428 BUG_ON(ret);
429
430 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800431 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500432 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433 if (ret)
434 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500435
Yan Zheng52c26172009-01-05 15:43:43 -0500436 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
437 ret = btrfs_update_inode(trans, root, dir);
438 BUG_ON(ret);
439
Chris Mason0660b5a2008-11-17 20:37:39 -0500440 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400441 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800442 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400443
Chris Mason0660b5a2008-11-17 20:37:39 -0500444 BUG_ON(ret);
445
Yan, Zheng76dda932009-09-21 16:00:26 -0400446 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400447fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400448 if (async_transid) {
449 *async_transid = trans->transid;
450 err = btrfs_commit_transaction_async(trans, root, 1);
451 } else {
452 err = btrfs_commit_transaction(trans, root);
453 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400454 if (err && !ret)
455 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456 return ret;
457}
458
Sage Weil72fd0322010-10-29 15:41:32 -0400459static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800460 char *name, int namelen, u64 *async_transid,
461 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400462{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000463 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 struct btrfs_pending_snapshot *pending_snapshot;
465 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000466 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400467
468 if (!root->ref_cows)
469 return -EINVAL;
470
Yan, Zhenga22285a2010-05-16 10:48:46 -0400471 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
472 if (!pending_snapshot)
473 return -ENOMEM;
474
475 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
476 pending_snapshot->dentry = dentry;
477 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800478 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400479
480 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
481 if (IS_ERR(trans)) {
482 ret = PTR_ERR(trans);
483 goto fail;
484 }
485
486 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
487 BUG_ON(ret);
488
Josef Bacik83515832011-06-14 15:16:14 -0400489 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400490 list_add(&pending_snapshot->list,
491 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400492 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400493 if (async_transid) {
494 *async_transid = trans->transid;
495 ret = btrfs_commit_transaction_async(trans,
496 root->fs_info->extent_root, 1);
497 } else {
498 ret = btrfs_commit_transaction(trans,
499 root->fs_info->extent_root);
500 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400501 BUG_ON(ret);
502
503 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400504 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000505 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400506
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500507 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
508 if (ret)
509 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400510
Al Viro2fbe8c82011-07-16 21:38:06 -0400511 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000512 if (IS_ERR(inode)) {
513 ret = PTR_ERR(inode);
514 goto fail;
515 }
516 BUG_ON(!inode);
517 d_instantiate(dentry, inode);
518 ret = 0;
519fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400520 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400521 return ret;
522}
523
Sage Weil4260f7c2010-10-29 15:46:43 -0400524/* copy of check_sticky in fs/namei.c()
525* It's inline, so penalty for filesystems that don't use sticky bit is
526* minimal.
527*/
528static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
529{
530 uid_t fsuid = current_fsuid();
531
532 if (!(dir->i_mode & S_ISVTX))
533 return 0;
534 if (inode->i_uid == fsuid)
535 return 0;
536 if (dir->i_uid == fsuid)
537 return 0;
538 return !capable(CAP_FOWNER);
539}
540
541/* copy of may_delete in fs/namei.c()
542 * Check whether we can remove a link victim from directory dir, check
543 * whether the type of victim is right.
544 * 1. We can't do it if dir is read-only (done in permission())
545 * 2. We should have write and exec permissions on dir
546 * 3. We can't remove anything from append-only dir
547 * 4. We can't do anything with immutable dir (done in permission())
548 * 5. If the sticky bit on dir is set we should either
549 * a. be owner of dir, or
550 * b. be owner of victim, or
551 * c. have CAP_FOWNER capability
552 * 6. If the victim is append-only or immutable we can't do antyhing with
553 * links pointing to it.
554 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
555 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
556 * 9. We can't remove a root or mountpoint.
557 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
558 * nfs_async_unlink().
559 */
560
561static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
562{
563 int error;
564
565 if (!victim->d_inode)
566 return -ENOENT;
567
568 BUG_ON(victim->d_parent->d_inode != dir);
569 audit_inode_child(victim, dir);
570
571 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
572 if (error)
573 return error;
574 if (IS_APPEND(dir))
575 return -EPERM;
576 if (btrfs_check_sticky(dir, victim->d_inode)||
577 IS_APPEND(victim->d_inode)||
578 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
579 return -EPERM;
580 if (isdir) {
581 if (!S_ISDIR(victim->d_inode->i_mode))
582 return -ENOTDIR;
583 if (IS_ROOT(victim))
584 return -EBUSY;
585 } else if (S_ISDIR(victim->d_inode->i_mode))
586 return -EISDIR;
587 if (IS_DEADDIR(dir))
588 return -ENOENT;
589 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
590 return -EBUSY;
591 return 0;
592}
593
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400594/* copy of may_create in fs/namei.c() */
595static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
596{
597 if (child->d_inode)
598 return -EEXIST;
599 if (IS_DEADDIR(dir))
600 return -ENOENT;
601 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
602}
603
604/*
605 * Create a new subvolume below @parent. This is largely modeled after
606 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
607 * inside this filesystem so it's quite a bit simpler.
608 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400609static noinline int btrfs_mksubvol(struct path *parent,
610 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400611 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800612 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400613{
Yan, Zheng76dda932009-09-21 16:00:26 -0400614 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400615 struct dentry *dentry;
616 int error;
617
Yan, Zheng76dda932009-09-21 16:00:26 -0400618 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400619
620 dentry = lookup_one_len(name, parent->dentry, namelen);
621 error = PTR_ERR(dentry);
622 if (IS_ERR(dentry))
623 goto out_unlock;
624
625 error = -EEXIST;
626 if (dentry->d_inode)
627 goto out_dput;
628
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400629 error = mnt_want_write(parent->mnt);
630 if (error)
631 goto out_dput;
632
Yan, Zheng76dda932009-09-21 16:00:26 -0400633 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400634 if (error)
635 goto out_drop_write;
636
Yan, Zheng76dda932009-09-21 16:00:26 -0400637 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
638
639 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
640 goto out_up_read;
641
Chris Mason3de45862008-11-17 21:02:50 -0500642 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400643 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800644 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500645 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400646 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400647 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500648 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400649 if (!error)
650 fsnotify_mkdir(dir, dentry);
651out_up_read:
652 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400653out_drop_write:
654 mnt_drop_write(parent->mnt);
655out_dput:
656 dput(dentry);
657out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400658 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400659 return error;
660}
661
Chris Mason4cb53002011-05-24 15:35:30 -0400662/*
663 * When we're defragging a range, we don't want to kick it off again
664 * if it is really just waiting for delalloc to send it down.
665 * If we find a nice big extent or delalloc range for the bytes in the
666 * file you want to defrag, we return 0 to let you know to skip this
667 * part of the file
668 */
669static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
670{
671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
672 struct extent_map *em = NULL;
673 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
674 u64 end;
675
676 read_lock(&em_tree->lock);
677 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
678 read_unlock(&em_tree->lock);
679
680 if (em) {
681 end = extent_map_end(em);
682 free_extent_map(em);
683 if (end - offset > thresh)
684 return 0;
685 }
686 /* if we already have a nice delalloc here, just stop */
687 thresh /= 2;
688 end = count_range_bits(io_tree, &offset, offset + thresh,
689 thresh, EXTENT_DELALLOC, 1);
690 if (end >= thresh)
691 return 0;
692 return 1;
693}
694
695/*
696 * helper function to walk through a file and find extents
697 * newer than a specific transid, and smaller than thresh.
698 *
699 * This is used by the defragging code to find new and small
700 * extents
701 */
702static int find_new_extents(struct btrfs_root *root,
703 struct inode *inode, u64 newer_than,
704 u64 *off, int thresh)
705{
706 struct btrfs_path *path;
707 struct btrfs_key min_key;
708 struct btrfs_key max_key;
709 struct extent_buffer *leaf;
710 struct btrfs_file_extent_item *extent;
711 int type;
712 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000713 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400714
715 path = btrfs_alloc_path();
716 if (!path)
717 return -ENOMEM;
718
David Sterbaa4689d22011-05-31 17:08:14 +0000719 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400720 min_key.type = BTRFS_EXTENT_DATA_KEY;
721 min_key.offset = *off;
722
David Sterbaa4689d22011-05-31 17:08:14 +0000723 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400724 max_key.type = (u8)-1;
725 max_key.offset = (u64)-1;
726
727 path->keep_locks = 1;
728
729 while(1) {
730 ret = btrfs_search_forward(root, &min_key, &max_key,
731 path, 0, newer_than);
732 if (ret != 0)
733 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000734 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400735 goto none;
736 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
737 goto none;
738
739 leaf = path->nodes[0];
740 extent = btrfs_item_ptr(leaf, path->slots[0],
741 struct btrfs_file_extent_item);
742
743 type = btrfs_file_extent_type(leaf, extent);
744 if (type == BTRFS_FILE_EXTENT_REG &&
745 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
746 check_defrag_in_cache(inode, min_key.offset, thresh)) {
747 *off = min_key.offset;
748 btrfs_free_path(path);
749 return 0;
750 }
751
752 if (min_key.offset == (u64)-1)
753 goto none;
754
755 min_key.offset++;
756 btrfs_release_path(path);
757 }
758none:
759 btrfs_free_path(path);
760 return -ENOENT;
761}
762
Chris Mason940100a2010-03-10 10:52:59 -0500763static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500764 int thresh, u64 *last_len, u64 *skip,
765 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500766{
767 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
768 struct extent_map *em = NULL;
769 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
770 int ret = 1;
771
772 /*
Li Zefan008873e2011-09-02 15:57:07 +0800773 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500774 * defragging it
775 */
776 if (start < *defrag_end)
777 return 1;
778
779 *skip = 0;
780
781 /*
782 * hopefully we have this extent in the tree already, try without
783 * the full extent lock
784 */
785 read_lock(&em_tree->lock);
786 em = lookup_extent_mapping(em_tree, start, len);
787 read_unlock(&em_tree->lock);
788
789 if (!em) {
790 /* get the big lock and read metadata off disk */
791 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
792 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
793 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
794
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000795 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500796 return 0;
797 }
798
799 /* this will cover holes, and inline extents */
800 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
801 ret = 0;
802
803 /*
804 * we hit a real extent, if it is big don't bother defragging it again
805 */
Chris Mason1e701a32010-03-11 09:42:04 -0500806 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500807 ret = 0;
808
809 /*
810 * last_len ends up being a counter of how many bytes we've defragged.
811 * every time we choose not to defrag an extent, we reset *last_len
812 * so that the next tiny extent will force a defrag.
813 *
814 * The end result of this is that tiny extents before a single big
815 * extent will force at least part of that big extent to be defragged.
816 */
817 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500818 *defrag_end = extent_map_end(em);
819 } else {
820 *last_len = 0;
821 *skip = extent_map_end(em);
822 *defrag_end = 0;
823 }
824
825 free_extent_map(em);
826 return ret;
827}
828
Chris Mason4cb53002011-05-24 15:35:30 -0400829/*
830 * it doesn't do much good to defrag one or two pages
831 * at a time. This pulls in a nice chunk of pages
832 * to COW and defrag.
833 *
834 * It also makes sure the delalloc code has enough
835 * dirty data to avoid making new small extents as part
836 * of the defrag
837 *
838 * It's a good idea to start RA on this range
839 * before calling this.
840 */
841static int cluster_pages_for_defrag(struct inode *inode,
842 struct page **pages,
843 unsigned long start_index,
844 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400845{
Chris Mason4cb53002011-05-24 15:35:30 -0400846 unsigned long file_end;
847 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400848 u64 page_start;
849 u64 page_end;
Chris Mason4cb53002011-05-24 15:35:30 -0400850 int ret;
851 int i;
852 int i_done;
853 struct btrfs_ordered_extent *ordered;
854 struct extent_state *cached_state = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400855 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400856
857 if (isize == 0)
858 return 0;
859 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
860
Josef Bacik660d3f62011-12-09 11:18:51 -0500861 mutex_lock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -0400862 ret = btrfs_delalloc_reserve_space(inode,
863 num_pages << PAGE_CACHE_SHIFT);
Josef Bacik660d3f62011-12-09 11:18:51 -0500864 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -0400865 if (ret)
866 return ret;
867again:
868 ret = 0;
869 i_done = 0;
870
871 /* step one, lock all the pages */
872 for (i = 0; i < num_pages; i++) {
873 struct page *page;
Josef Bacika94733d2011-07-11 10:47:06 -0400874 page = find_or_create_page(inode->i_mapping,
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400875 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400876 if (!page)
877 break;
878
879 if (!PageUptodate(page)) {
880 btrfs_readpage(NULL, page);
881 lock_page(page);
882 if (!PageUptodate(page)) {
883 unlock_page(page);
884 page_cache_release(page);
885 ret = -EIO;
886 break;
887 }
888 }
889 isize = i_size_read(inode);
890 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
891 if (!isize || page->index > file_end ||
892 page->mapping != inode->i_mapping) {
893 /* whoops, we blew past eof, skip this page */
894 unlock_page(page);
895 page_cache_release(page);
896 break;
897 }
898 pages[i] = page;
899 i_done++;
900 }
901 if (!i_done || ret)
902 goto out;
903
904 if (!(inode->i_sb->s_flags & MS_ACTIVE))
905 goto out;
906
907 /*
908 * so now we have a nice long stream of locked
909 * and up to date pages, lets wait on them
910 */
911 for (i = 0; i < i_done; i++)
912 wait_on_page_writeback(pages[i]);
913
914 page_start = page_offset(pages[0]);
915 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
916
917 lock_extent_bits(&BTRFS_I(inode)->io_tree,
918 page_start, page_end - 1, 0, &cached_state,
919 GFP_NOFS);
920 ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
921 if (ordered &&
922 ordered->file_offset + ordered->len > page_start &&
923 ordered->file_offset < page_end) {
924 btrfs_put_ordered_extent(ordered);
925 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
926 page_start, page_end - 1,
927 &cached_state, GFP_NOFS);
928 for (i = 0; i < i_done; i++) {
929 unlock_page(pages[i]);
930 page_cache_release(pages[i]);
931 }
932 btrfs_wait_ordered_range(inode, page_start,
933 page_end - page_start);
934 goto again;
935 }
936 if (ordered)
937 btrfs_put_ordered_extent(ordered);
938
939 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
940 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
941 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
942 GFP_NOFS);
943
944 if (i_done != num_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +0000945 spin_lock(&BTRFS_I(inode)->lock);
946 BTRFS_I(inode)->outstanding_extents++;
947 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -0400948 btrfs_delalloc_release_space(inode,
949 (num_pages - i_done) << PAGE_CACHE_SHIFT);
950 }
951
952
953 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
954 &cached_state);
955
956 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
957 page_start, page_end - 1, &cached_state,
958 GFP_NOFS);
959
960 for (i = 0; i < i_done; i++) {
961 clear_page_dirty_for_io(pages[i]);
962 ClearPageChecked(pages[i]);
963 set_page_extent_mapped(pages[i]);
964 set_page_dirty(pages[i]);
965 unlock_page(pages[i]);
966 page_cache_release(pages[i]);
967 }
968 return i_done;
969out:
970 for (i = 0; i < i_done; i++) {
971 unlock_page(pages[i]);
972 page_cache_release(pages[i]);
973 }
974 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
975 return ret;
976
977}
978
979int btrfs_defrag_file(struct inode *inode, struct file *file,
980 struct btrfs_ioctl_defrag_range_args *range,
981 u64 newer_than, unsigned long max_to_defrag)
982{
983 struct btrfs_root *root = BTRFS_I(inode)->root;
984 struct btrfs_super_block *disk_super;
985 struct file_ra_state *ra = NULL;
986 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +0800987 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400988 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -0500989 u64 last_len = 0;
990 u64 skip = 0;
991 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400992 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400993 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +0800994 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400995 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400996 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800997 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -0400998 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +0800999 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1000 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001001 u64 new_align = ~((u64)128 * 1024 - 1);
1002 struct page **pages = NULL;
1003
1004 if (extent_thresh == 0)
1005 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001006
1007 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1008 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1009 return -EINVAL;
1010 if (range->compress_type)
1011 compress_type = range->compress_type;
1012 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001013
Li Zefan151a31b2011-09-02 15:56:39 +08001014 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001015 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001016
Chris Mason4cb53002011-05-24 15:35:30 -04001017 /*
1018 * if we were not given a file, allocate a readahead
1019 * context
1020 */
1021 if (!file) {
1022 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1023 if (!ra)
1024 return -ENOMEM;
1025 file_ra_state_init(ra, inode->i_mapping);
1026 } else {
1027 ra = &file->f_ra;
1028 }
1029
Li Zefan008873e2011-09-02 15:57:07 +08001030 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001031 GFP_NOFS);
1032 if (!pages) {
1033 ret = -ENOMEM;
1034 goto out_ra;
1035 }
1036
1037 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001038 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001039 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001040 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1041 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001042 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001043 }
1044
Chris Mason4cb53002011-05-24 15:35:30 -04001045 if (newer_than) {
1046 ret = find_new_extents(root, inode, newer_than,
1047 &newer_off, 64 * 1024);
1048 if (!ret) {
1049 range->start = newer_off;
1050 /*
1051 * we always align our defrag to help keep
1052 * the extents in the file evenly spaced
1053 */
1054 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001055 } else
1056 goto out_ra;
1057 } else {
1058 i = range->start >> PAGE_CACHE_SHIFT;
1059 }
1060 if (!max_to_defrag)
Li Zefan5ca49662011-09-02 15:56:55 +08001061 max_to_defrag = last_index;
Chris Mason4cb53002011-05-24 15:35:30 -04001062
Li Zefan2a0f7f52011-10-10 15:43:34 -04001063 /*
1064 * make writeback starts from i, so the defrag range can be
1065 * written sequentially.
1066 */
1067 if (i < inode->i_mapping->writeback_index)
1068 inode->i_mapping->writeback_index = i;
1069
Chris Masonf7f43cc2011-10-11 11:41:40 -04001070 while (i <= last_index && defrag_count < max_to_defrag &&
1071 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1072 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001073 /*
1074 * make sure we stop running if someone unmounts
1075 * the FS
1076 */
1077 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1078 break;
1079
1080 if (!newer_than &&
1081 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -05001082 PAGE_CACHE_SIZE,
Chris Mason4cb53002011-05-24 15:35:30 -04001083 extent_thresh,
Chris Mason1e701a32010-03-11 09:42:04 -05001084 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -05001085 &defrag_end)) {
1086 unsigned long next;
1087 /*
1088 * the should_defrag function tells us how much to skip
1089 * bump our counter by the suggested amount
1090 */
1091 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1092 i = max(i + 1, next);
1093 continue;
1094 }
Li Zefan008873e2011-09-02 15:57:07 +08001095
1096 if (!newer_than) {
1097 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1098 PAGE_CACHE_SHIFT) - i;
1099 cluster = min(cluster, max_cluster);
1100 } else {
1101 cluster = max_cluster;
1102 }
1103
Chris Mason1e701a32010-03-11 09:42:04 -05001104 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001105 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001106
Li Zefan008873e2011-09-02 15:57:07 +08001107 if (i + cluster > ra_index) {
1108 ra_index = max(i, ra_index);
1109 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1110 cluster);
1111 ra_index += max_cluster;
1112 }
Chris Mason940100a2010-03-10 10:52:59 -05001113
Li Zefan008873e2011-09-02 15:57:07 +08001114 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Chris Mason4cb53002011-05-24 15:35:30 -04001115 if (ret < 0)
1116 goto out_ra;
Chris Mason940100a2010-03-10 10:52:59 -05001117
Chris Mason4cb53002011-05-24 15:35:30 -04001118 defrag_count += ret;
1119 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Chris Mason4cb53002011-05-24 15:35:30 -04001120
1121 if (newer_than) {
1122 if (newer_off == (u64)-1)
1123 break;
1124
1125 newer_off = max(newer_off + 1,
1126 (u64)i << PAGE_CACHE_SHIFT);
1127
1128 ret = find_new_extents(root, inode,
1129 newer_than, &newer_off,
1130 64 * 1024);
1131 if (!ret) {
1132 range->start = newer_off;
1133 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001134 } else {
1135 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001136 }
Chris Mason4cb53002011-05-24 15:35:30 -04001137 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001138 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001139 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001140 last_len += ret << PAGE_CACHE_SHIFT;
1141 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001142 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001143 last_len = 0;
1144 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001145 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001146 }
1147
Chris Mason1e701a32010-03-11 09:42:04 -05001148 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1149 filemap_flush(inode->i_mapping);
1150
1151 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1152 /* the filemap_flush will queue IO into the worker threads, but
1153 * we have to make sure the IO is actually started and that
1154 * ordered extents get created before we return
1155 */
1156 atomic_inc(&root->fs_info->async_submit_draining);
1157 while (atomic_read(&root->fs_info->nr_async_submits) ||
1158 atomic_read(&root->fs_info->async_delalloc_pages)) {
1159 wait_event(root->fs_info->async_submit_wait,
1160 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1161 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1162 }
1163 atomic_dec(&root->fs_info->async_submit_draining);
1164
1165 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001166 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001167 mutex_unlock(&inode->i_mutex);
1168 }
1169
David Sterba6c417612011-04-13 15:41:04 +02001170 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001171 features = btrfs_super_incompat_flags(disk_super);
1172 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1173 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1174 btrfs_set_super_incompat_flags(disk_super, features);
1175 }
1176
Diego Calleja60ccf822011-09-01 16:33:57 +02001177 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001178
Chris Mason4cb53002011-05-24 15:35:30 -04001179out_ra:
1180 if (!file)
1181 kfree(ra);
1182 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001183 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001184}
1185
Yan, Zheng76dda932009-09-21 16:00:26 -04001186static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1187 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001188{
1189 u64 new_size;
1190 u64 old_size;
1191 u64 devid = 1;
1192 struct btrfs_ioctl_vol_args *vol_args;
1193 struct btrfs_trans_handle *trans;
1194 struct btrfs_device *device = NULL;
1195 char *sizestr;
1196 char *devstr = NULL;
1197 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001198 int mod = 0;
1199
Yan Zhengc146afa2008-11-12 14:34:12 -05001200 if (root->fs_info->sb->s_flags & MS_RDONLY)
1201 return -EROFS;
1202
Chris Masone441d542009-01-05 16:57:23 -05001203 if (!capable(CAP_SYS_ADMIN))
1204 return -EPERM;
1205
Li Zefandae7b662009-04-08 15:06:54 +08001206 vol_args = memdup_user(arg, sizeof(*vol_args));
1207 if (IS_ERR(vol_args))
1208 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -04001209
1210 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001211
Chris Mason7d9eb122008-07-08 14:19:17 -04001212 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001213 sizestr = vol_args->name;
1214 devstr = strchr(sizestr, ':');
1215 if (devstr) {
1216 char *end;
1217 sizestr = devstr + 1;
1218 *devstr = '\0';
1219 devstr = vol_args->name;
1220 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001221 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001222 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001223 }
Yan Zheng2b820322008-11-17 21:11:30 -05001224 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001225 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001226 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001227 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001228 ret = -EINVAL;
1229 goto out_unlock;
1230 }
1231 if (!strcmp(sizestr, "max"))
1232 new_size = device->bdev->bd_inode->i_size;
1233 else {
1234 if (sizestr[0] == '-') {
1235 mod = -1;
1236 sizestr++;
1237 } else if (sizestr[0] == '+') {
1238 mod = 1;
1239 sizestr++;
1240 }
Akinobu Mita91748462010-02-28 10:59:11 +00001241 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001242 if (new_size == 0) {
1243 ret = -EINVAL;
1244 goto out_unlock;
1245 }
1246 }
1247
1248 old_size = device->total_bytes;
1249
1250 if (mod < 0) {
1251 if (new_size > old_size) {
1252 ret = -EINVAL;
1253 goto out_unlock;
1254 }
1255 new_size = old_size - new_size;
1256 } else if (mod > 0) {
1257 new_size = old_size + new_size;
1258 }
1259
1260 if (new_size < 256 * 1024 * 1024) {
1261 ret = -EINVAL;
1262 goto out_unlock;
1263 }
1264 if (new_size > device->bdev->bd_inode->i_size) {
1265 ret = -EFBIG;
1266 goto out_unlock;
1267 }
1268
1269 do_div(new_size, root->sectorsize);
1270 new_size *= root->sectorsize;
1271
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001272 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001273 device->name, (unsigned long long)new_size);
1274
1275 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001276 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001277 if (IS_ERR(trans)) {
1278 ret = PTR_ERR(trans);
1279 goto out_unlock;
1280 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001281 ret = btrfs_grow_device(trans, device, new_size);
1282 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001283 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001284 ret = btrfs_shrink_device(device, new_size);
1285 }
1286
1287out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001288 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001289 kfree(vol_args);
1290 return ret;
1291}
1292
Sage Weil72fd0322010-10-29 15:41:32 -04001293static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1294 char *name,
1295 unsigned long fd,
1296 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001297 u64 *transid,
1298 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001299{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001300 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001301 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001303 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001304
Yan Zhengc146afa2008-11-12 14:34:12 -05001305 if (root->fs_info->sb->s_flags & MS_RDONLY)
1306 return -EROFS;
1307
Sage Weil72fd0322010-10-29 15:41:32 -04001308 namelen = strlen(name);
1309 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001310 ret = -EINVAL;
1311 goto out;
1312 }
1313
Chris Mason3de45862008-11-17 21:02:50 -05001314 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001315 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001316 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001317 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001318 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001319 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001320 if (!src_file) {
1321 ret = -EINVAL;
1322 goto out;
1323 }
1324
1325 src_inode = src_file->f_path.dentry->d_inode;
1326 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001327 printk(KERN_INFO "btrfs: Snapshot src from "
1328 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001329 ret = -EINVAL;
1330 fput(src_file);
1331 goto out;
1332 }
Sage Weil72fd0322010-10-29 15:41:32 -04001333 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1334 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001335 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001336 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001337 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001338out:
Sage Weil72fd0322010-10-29 15:41:32 -04001339 return ret;
1340}
1341
1342static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001343 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001344{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001345 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001346 int ret;
1347
Li Zefanfa0d2b92010-12-20 15:53:28 +08001348 vol_args = memdup_user(arg, sizeof(*vol_args));
1349 if (IS_ERR(vol_args))
1350 return PTR_ERR(vol_args);
1351 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001352
Li Zefanfa0d2b92010-12-20 15:53:28 +08001353 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001354 vol_args->fd, subvol,
1355 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001356
Li Zefanfa0d2b92010-12-20 15:53:28 +08001357 kfree(vol_args);
1358 return ret;
1359}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001360
Li Zefanfa0d2b92010-12-20 15:53:28 +08001361static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1362 void __user *arg, int subvol)
1363{
1364 struct btrfs_ioctl_vol_args_v2 *vol_args;
1365 int ret;
1366 u64 transid = 0;
1367 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001368 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001369
Li Zefanfa0d2b92010-12-20 15:53:28 +08001370 vol_args = memdup_user(arg, sizeof(*vol_args));
1371 if (IS_ERR(vol_args))
1372 return PTR_ERR(vol_args);
1373 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001374
Li Zefanb83cc962010-12-20 16:04:08 +08001375 if (vol_args->flags &
1376 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1377 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001378 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001379 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001380
1381 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1382 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001383 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1384 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001385
1386 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001387 vol_args->fd, subvol,
1388 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001389
1390 if (ret == 0 && ptr &&
1391 copy_to_user(arg +
1392 offsetof(struct btrfs_ioctl_vol_args_v2,
1393 transid), ptr, sizeof(*ptr)))
1394 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001395out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001396 kfree(vol_args);
1397 return ret;
1398}
1399
Li Zefan0caa1022010-12-20 16:30:25 +08001400static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1401 void __user *arg)
1402{
1403 struct inode *inode = fdentry(file)->d_inode;
1404 struct btrfs_root *root = BTRFS_I(inode)->root;
1405 int ret = 0;
1406 u64 flags = 0;
1407
Li Zefan33345d012011-04-20 10:31:50 +08001408 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001409 return -EINVAL;
1410
1411 down_read(&root->fs_info->subvol_sem);
1412 if (btrfs_root_readonly(root))
1413 flags |= BTRFS_SUBVOL_RDONLY;
1414 up_read(&root->fs_info->subvol_sem);
1415
1416 if (copy_to_user(arg, &flags, sizeof(flags)))
1417 ret = -EFAULT;
1418
1419 return ret;
1420}
1421
1422static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1423 void __user *arg)
1424{
1425 struct inode *inode = fdentry(file)->d_inode;
1426 struct btrfs_root *root = BTRFS_I(inode)->root;
1427 struct btrfs_trans_handle *trans;
1428 u64 root_flags;
1429 u64 flags;
1430 int ret = 0;
1431
1432 if (root->fs_info->sb->s_flags & MS_RDONLY)
1433 return -EROFS;
1434
Li Zefan33345d012011-04-20 10:31:50 +08001435 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001436 return -EINVAL;
1437
1438 if (copy_from_user(&flags, arg, sizeof(flags)))
1439 return -EFAULT;
1440
Li Zefanb4dc2b82011-02-16 06:06:34 +00001441 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001442 return -EINVAL;
1443
1444 if (flags & ~BTRFS_SUBVOL_RDONLY)
1445 return -EOPNOTSUPP;
1446
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001447 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001448 return -EACCES;
1449
Li Zefan0caa1022010-12-20 16:30:25 +08001450 down_write(&root->fs_info->subvol_sem);
1451
1452 /* nothing to do */
1453 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1454 goto out;
1455
1456 root_flags = btrfs_root_flags(&root->root_item);
1457 if (flags & BTRFS_SUBVOL_RDONLY)
1458 btrfs_set_root_flags(&root->root_item,
1459 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1460 else
1461 btrfs_set_root_flags(&root->root_item,
1462 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1463
1464 trans = btrfs_start_transaction(root, 1);
1465 if (IS_ERR(trans)) {
1466 ret = PTR_ERR(trans);
1467 goto out_reset;
1468 }
1469
Li Zefanb4dc2b82011-02-16 06:06:34 +00001470 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001471 &root->root_key, &root->root_item);
1472
1473 btrfs_commit_transaction(trans, root);
1474out_reset:
1475 if (ret)
1476 btrfs_set_root_flags(&root->root_item, root_flags);
1477out:
1478 up_write(&root->fs_info->subvol_sem);
1479 return ret;
1480}
1481
Yan, Zheng76dda932009-09-21 16:00:26 -04001482/*
1483 * helper to check if the subvolume references other subvolumes
1484 */
1485static noinline int may_destroy_subvol(struct btrfs_root *root)
1486{
1487 struct btrfs_path *path;
1488 struct btrfs_key key;
1489 int ret;
1490
1491 path = btrfs_alloc_path();
1492 if (!path)
1493 return -ENOMEM;
1494
1495 key.objectid = root->root_key.objectid;
1496 key.type = BTRFS_ROOT_REF_KEY;
1497 key.offset = (u64)-1;
1498
1499 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1500 &key, path, 0, 0);
1501 if (ret < 0)
1502 goto out;
1503 BUG_ON(ret == 0);
1504
1505 ret = 0;
1506 if (path->slots[0] > 0) {
1507 path->slots[0]--;
1508 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1509 if (key.objectid == root->root_key.objectid &&
1510 key.type == BTRFS_ROOT_REF_KEY)
1511 ret = -ENOTEMPTY;
1512 }
1513out:
1514 btrfs_free_path(path);
1515 return ret;
1516}
1517
Chris Masonac8e9812010-02-28 15:39:26 -05001518static noinline int key_in_sk(struct btrfs_key *key,
1519 struct btrfs_ioctl_search_key *sk)
1520{
Chris Masonabc6e132010-03-18 12:10:08 -04001521 struct btrfs_key test;
1522 int ret;
1523
1524 test.objectid = sk->min_objectid;
1525 test.type = sk->min_type;
1526 test.offset = sk->min_offset;
1527
1528 ret = btrfs_comp_cpu_keys(key, &test);
1529 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001530 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001531
1532 test.objectid = sk->max_objectid;
1533 test.type = sk->max_type;
1534 test.offset = sk->max_offset;
1535
1536 ret = btrfs_comp_cpu_keys(key, &test);
1537 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001538 return 0;
1539 return 1;
1540}
1541
1542static noinline int copy_to_sk(struct btrfs_root *root,
1543 struct btrfs_path *path,
1544 struct btrfs_key *key,
1545 struct btrfs_ioctl_search_key *sk,
1546 char *buf,
1547 unsigned long *sk_offset,
1548 int *num_found)
1549{
1550 u64 found_transid;
1551 struct extent_buffer *leaf;
1552 struct btrfs_ioctl_search_header sh;
1553 unsigned long item_off;
1554 unsigned long item_len;
1555 int nritems;
1556 int i;
1557 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001558 int ret = 0;
1559
1560 leaf = path->nodes[0];
1561 slot = path->slots[0];
1562 nritems = btrfs_header_nritems(leaf);
1563
1564 if (btrfs_header_generation(leaf) > sk->max_transid) {
1565 i = nritems;
1566 goto advance_key;
1567 }
1568 found_transid = btrfs_header_generation(leaf);
1569
1570 for (i = slot; i < nritems; i++) {
1571 item_off = btrfs_item_ptr_offset(leaf, i);
1572 item_len = btrfs_item_size_nr(leaf, i);
1573
1574 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1575 item_len = 0;
1576
1577 if (sizeof(sh) + item_len + *sk_offset >
1578 BTRFS_SEARCH_ARGS_BUFSIZE) {
1579 ret = 1;
1580 goto overflow;
1581 }
1582
1583 btrfs_item_key_to_cpu(leaf, key, i);
1584 if (!key_in_sk(key, sk))
1585 continue;
1586
1587 sh.objectid = key->objectid;
1588 sh.offset = key->offset;
1589 sh.type = key->type;
1590 sh.len = item_len;
1591 sh.transid = found_transid;
1592
1593 /* copy search result header */
1594 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1595 *sk_offset += sizeof(sh);
1596
1597 if (item_len) {
1598 char *p = buf + *sk_offset;
1599 /* copy the item */
1600 read_extent_buffer(leaf, p,
1601 item_off, item_len);
1602 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001603 }
Hugo Millse2156862011-05-14 17:43:41 +00001604 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001605
1606 if (*num_found >= sk->nr_items)
1607 break;
1608 }
1609advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001610 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001611 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1612 key->offset++;
1613 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1614 key->offset = 0;
1615 key->type++;
1616 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1617 key->offset = 0;
1618 key->type = 0;
1619 key->objectid++;
1620 } else
1621 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001622overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001623 return ret;
1624}
1625
1626static noinline int search_ioctl(struct inode *inode,
1627 struct btrfs_ioctl_search_args *args)
1628{
1629 struct btrfs_root *root;
1630 struct btrfs_key key;
1631 struct btrfs_key max_key;
1632 struct btrfs_path *path;
1633 struct btrfs_ioctl_search_key *sk = &args->key;
1634 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1635 int ret;
1636 int num_found = 0;
1637 unsigned long sk_offset = 0;
1638
1639 path = btrfs_alloc_path();
1640 if (!path)
1641 return -ENOMEM;
1642
1643 if (sk->tree_id == 0) {
1644 /* search the root of the inode that was passed */
1645 root = BTRFS_I(inode)->root;
1646 } else {
1647 key.objectid = sk->tree_id;
1648 key.type = BTRFS_ROOT_ITEM_KEY;
1649 key.offset = (u64)-1;
1650 root = btrfs_read_fs_root_no_name(info, &key);
1651 if (IS_ERR(root)) {
1652 printk(KERN_ERR "could not find root %llu\n",
1653 sk->tree_id);
1654 btrfs_free_path(path);
1655 return -ENOENT;
1656 }
1657 }
1658
1659 key.objectid = sk->min_objectid;
1660 key.type = sk->min_type;
1661 key.offset = sk->min_offset;
1662
1663 max_key.objectid = sk->max_objectid;
1664 max_key.type = sk->max_type;
1665 max_key.offset = sk->max_offset;
1666
1667 path->keep_locks = 1;
1668
1669 while(1) {
1670 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1671 sk->min_transid);
1672 if (ret != 0) {
1673 if (ret > 0)
1674 ret = 0;
1675 goto err;
1676 }
1677 ret = copy_to_sk(root, path, &key, sk, args->buf,
1678 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001679 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001680 if (ret || num_found >= sk->nr_items)
1681 break;
1682
1683 }
1684 ret = 0;
1685err:
1686 sk->nr_items = num_found;
1687 btrfs_free_path(path);
1688 return ret;
1689}
1690
1691static noinline int btrfs_ioctl_tree_search(struct file *file,
1692 void __user *argp)
1693{
1694 struct btrfs_ioctl_search_args *args;
1695 struct inode *inode;
1696 int ret;
1697
1698 if (!capable(CAP_SYS_ADMIN))
1699 return -EPERM;
1700
Julia Lawall2354d082010-10-29 15:14:18 -04001701 args = memdup_user(argp, sizeof(*args));
1702 if (IS_ERR(args))
1703 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001704
Chris Masonac8e9812010-02-28 15:39:26 -05001705 inode = fdentry(file)->d_inode;
1706 ret = search_ioctl(inode, args);
1707 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1708 ret = -EFAULT;
1709 kfree(args);
1710 return ret;
1711}
1712
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001713/*
Chris Masonac8e9812010-02-28 15:39:26 -05001714 * Search INODE_REFs to identify path name of 'dirid' directory
1715 * in a 'tree_id' tree. and sets path name to 'name'.
1716 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001717static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1718 u64 tree_id, u64 dirid, char *name)
1719{
1720 struct btrfs_root *root;
1721 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001722 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001723 int ret = -1;
1724 int slot;
1725 int len;
1726 int total_len = 0;
1727 struct btrfs_inode_ref *iref;
1728 struct extent_buffer *l;
1729 struct btrfs_path *path;
1730
1731 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1732 name[0]='\0';
1733 return 0;
1734 }
1735
1736 path = btrfs_alloc_path();
1737 if (!path)
1738 return -ENOMEM;
1739
Chris Masonac8e9812010-02-28 15:39:26 -05001740 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001741
1742 key.objectid = tree_id;
1743 key.type = BTRFS_ROOT_ITEM_KEY;
1744 key.offset = (u64)-1;
1745 root = btrfs_read_fs_root_no_name(info, &key);
1746 if (IS_ERR(root)) {
1747 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001748 ret = -ENOENT;
1749 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001750 }
1751
1752 key.objectid = dirid;
1753 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001754 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001755
1756 while(1) {
1757 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1758 if (ret < 0)
1759 goto out;
1760
1761 l = path->nodes[0];
1762 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001763 if (ret > 0 && slot > 0)
1764 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001765 btrfs_item_key_to_cpu(l, &key, slot);
1766
1767 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001768 key.type != BTRFS_INODE_REF_KEY)) {
1769 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001770 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001771 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001772
1773 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1774 len = btrfs_inode_ref_name_len(l, iref);
1775 ptr -= len + 1;
1776 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001777 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001778 goto out;
1779
1780 *(ptr + len) = '/';
1781 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1782
1783 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1784 break;
1785
David Sterbab3b4aa72011-04-21 01:20:15 +02001786 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001787 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001788 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001789 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001790 }
Chris Masonac8e9812010-02-28 15:39:26 -05001791 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001792 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001793 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001794 name[total_len]='\0';
1795 ret = 0;
1796out:
1797 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001798 return ret;
1799}
1800
1801static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1802 void __user *argp)
1803{
1804 struct btrfs_ioctl_ino_lookup_args *args;
1805 struct inode *inode;
1806 int ret;
1807
1808 if (!capable(CAP_SYS_ADMIN))
1809 return -EPERM;
1810
Julia Lawall2354d082010-10-29 15:14:18 -04001811 args = memdup_user(argp, sizeof(*args));
1812 if (IS_ERR(args))
1813 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001814
Chris Masonac8e9812010-02-28 15:39:26 -05001815 inode = fdentry(file)->d_inode;
1816
Chris Mason1b53ac42010-03-18 12:17:05 -04001817 if (args->treeid == 0)
1818 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1819
Chris Masonac8e9812010-02-28 15:39:26 -05001820 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1821 args->treeid, args->objectid,
1822 args->name);
1823
1824 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1825 ret = -EFAULT;
1826
1827 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001828 return ret;
1829}
1830
Yan, Zheng76dda932009-09-21 16:00:26 -04001831static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1832 void __user *arg)
1833{
1834 struct dentry *parent = fdentry(file);
1835 struct dentry *dentry;
1836 struct inode *dir = parent->d_inode;
1837 struct inode *inode;
1838 struct btrfs_root *root = BTRFS_I(dir)->root;
1839 struct btrfs_root *dest = NULL;
1840 struct btrfs_ioctl_vol_args *vol_args;
1841 struct btrfs_trans_handle *trans;
1842 int namelen;
1843 int ret;
1844 int err = 0;
1845
Yan, Zheng76dda932009-09-21 16:00:26 -04001846 vol_args = memdup_user(arg, sizeof(*vol_args));
1847 if (IS_ERR(vol_args))
1848 return PTR_ERR(vol_args);
1849
1850 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1851 namelen = strlen(vol_args->name);
1852 if (strchr(vol_args->name, '/') ||
1853 strncmp(vol_args->name, "..", namelen) == 0) {
1854 err = -EINVAL;
1855 goto out;
1856 }
1857
Al Viroa561be72011-11-23 11:57:51 -05001858 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001859 if (err)
1860 goto out;
1861
1862 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1863 dentry = lookup_one_len(vol_args->name, parent, namelen);
1864 if (IS_ERR(dentry)) {
1865 err = PTR_ERR(dentry);
1866 goto out_unlock_dir;
1867 }
1868
1869 if (!dentry->d_inode) {
1870 err = -ENOENT;
1871 goto out_dput;
1872 }
1873
1874 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001875 dest = BTRFS_I(inode)->root;
1876 if (!capable(CAP_SYS_ADMIN)){
1877 /*
1878 * Regular user. Only allow this with a special mount
1879 * option, when the user has write+exec access to the
1880 * subvol root, and when rmdir(2) would have been
1881 * allowed.
1882 *
1883 * Note that this is _not_ check that the subvol is
1884 * empty or doesn't contain data that we wouldn't
1885 * otherwise be able to delete.
1886 *
1887 * Users who want to delete empty subvols should try
1888 * rmdir(2).
1889 */
1890 err = -EPERM;
1891 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1892 goto out_dput;
1893
1894 /*
1895 * Do not allow deletion if the parent dir is the same
1896 * as the dir to be deleted. That means the ioctl
1897 * must be called on the dentry referencing the root
1898 * of the subvol, not a random directory contained
1899 * within it.
1900 */
1901 err = -EINVAL;
1902 if (root == dest)
1903 goto out_dput;
1904
1905 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1906 if (err)
1907 goto out_dput;
1908
1909 /* check if subvolume may be deleted by a non-root user */
1910 err = btrfs_may_delete(dir, dentry, 1);
1911 if (err)
1912 goto out_dput;
1913 }
1914
Li Zefan33345d012011-04-20 10:31:50 +08001915 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001916 err = -EINVAL;
1917 goto out_dput;
1918 }
1919
Yan, Zheng76dda932009-09-21 16:00:26 -04001920 mutex_lock(&inode->i_mutex);
1921 err = d_invalidate(dentry);
1922 if (err)
1923 goto out_unlock;
1924
1925 down_write(&root->fs_info->subvol_sem);
1926
1927 err = may_destroy_subvol(dest);
1928 if (err)
1929 goto out_up_write;
1930
Yan, Zhenga22285a2010-05-16 10:48:46 -04001931 trans = btrfs_start_transaction(root, 0);
1932 if (IS_ERR(trans)) {
1933 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001934 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001935 }
1936 trans->block_rsv = &root->fs_info->global_block_rsv;
1937
Yan, Zheng76dda932009-09-21 16:00:26 -04001938 ret = btrfs_unlink_subvol(trans, root, dir,
1939 dest->root_key.objectid,
1940 dentry->d_name.name,
1941 dentry->d_name.len);
1942 BUG_ON(ret);
1943
1944 btrfs_record_root_in_trans(trans, dest);
1945
1946 memset(&dest->root_item.drop_progress, 0,
1947 sizeof(dest->root_item.drop_progress));
1948 dest->root_item.drop_level = 0;
1949 btrfs_set_root_refs(&dest->root_item, 0);
1950
Yan, Zhengd68fc572010-05-16 10:49:58 -04001951 if (!xchg(&dest->orphan_item_inserted, 1)) {
1952 ret = btrfs_insert_orphan_item(trans,
1953 root->fs_info->tree_root,
1954 dest->root_key.objectid);
1955 BUG_ON(ret);
1956 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001957
Sage Weil531cb132010-10-29 15:41:32 -04001958 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001959 BUG_ON(ret);
1960 inode->i_flags |= S_DEAD;
1961out_up_write:
1962 up_write(&root->fs_info->subvol_sem);
1963out_unlock:
1964 mutex_unlock(&inode->i_mutex);
1965 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001966 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001967 btrfs_invalidate_inodes(dest);
1968 d_delete(dentry);
1969 }
1970out_dput:
1971 dput(dentry);
1972out_unlock_dir:
1973 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05001974 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001975out:
1976 kfree(vol_args);
1977 return err;
1978}
1979
Chris Mason1e701a32010-03-11 09:42:04 -05001980static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001981{
1982 struct inode *inode = fdentry(file)->d_inode;
1983 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001984 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001985 int ret;
1986
Li Zefanb83cc962010-12-20 16:04:08 +08001987 if (btrfs_root_readonly(root))
1988 return -EROFS;
1989
Al Viroa561be72011-11-23 11:57:51 -05001990 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05001991 if (ret)
1992 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001993
1994 switch (inode->i_mode & S_IFMT) {
1995 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001996 if (!capable(CAP_SYS_ADMIN)) {
1997 ret = -EPERM;
1998 goto out;
1999 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002000 ret = btrfs_defrag_root(root, 0);
2001 if (ret)
2002 goto out;
2003 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002004 break;
2005 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002006 if (!(file->f_mode & FMODE_WRITE)) {
2007 ret = -EINVAL;
2008 goto out;
2009 }
Chris Mason1e701a32010-03-11 09:42:04 -05002010
2011 range = kzalloc(sizeof(*range), GFP_KERNEL);
2012 if (!range) {
2013 ret = -ENOMEM;
2014 goto out;
2015 }
2016
2017 if (argp) {
2018 if (copy_from_user(range, argp,
2019 sizeof(*range))) {
2020 ret = -EFAULT;
2021 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002022 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002023 }
2024 /* compression requires us to start the IO */
2025 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2026 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2027 range->extent_thresh = (u32)-1;
2028 }
2029 } else {
2030 /* the rest are all set to zero by kzalloc */
2031 range->len = (u64)-1;
2032 }
Chris Mason4cb53002011-05-24 15:35:30 -04002033 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2034 range, 0, 0);
2035 if (ret > 0)
2036 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002037 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002038 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002039 default:
2040 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002041 }
Chris Masone441d542009-01-05 16:57:23 -05002042out:
Al Viro2a79f172011-12-09 08:06:57 -05002043 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002044 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002045}
2046
Christoph Hellwigb2950862008-12-02 09:54:17 -05002047static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002048{
2049 struct btrfs_ioctl_vol_args *vol_args;
2050 int ret;
2051
Chris Masone441d542009-01-05 16:57:23 -05002052 if (!capable(CAP_SYS_ADMIN))
2053 return -EPERM;
2054
Li Zefandae7b662009-04-08 15:06:54 +08002055 vol_args = memdup_user(arg, sizeof(*vol_args));
2056 if (IS_ERR(vol_args))
2057 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002058
Mark Fasheh5516e592008-07-24 12:20:14 -04002059 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002060 ret = btrfs_init_new_device(root, vol_args->name);
2061
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002062 kfree(vol_args);
2063 return ret;
2064}
2065
Christoph Hellwigb2950862008-12-02 09:54:17 -05002066static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002067{
2068 struct btrfs_ioctl_vol_args *vol_args;
2069 int ret;
2070
Chris Masone441d542009-01-05 16:57:23 -05002071 if (!capable(CAP_SYS_ADMIN))
2072 return -EPERM;
2073
Yan Zhengc146afa2008-11-12 14:34:12 -05002074 if (root->fs_info->sb->s_flags & MS_RDONLY)
2075 return -EROFS;
2076
Li Zefandae7b662009-04-08 15:06:54 +08002077 vol_args = memdup_user(arg, sizeof(*vol_args));
2078 if (IS_ERR(vol_args))
2079 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002080
Mark Fasheh5516e592008-07-24 12:20:14 -04002081 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002082 ret = btrfs_rm_device(root, vol_args->name);
2083
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002084 kfree(vol_args);
2085 return ret;
2086}
2087
Jan Schmidt475f6382011-03-11 15:41:01 +01002088static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2089{
Li Zefan027ed2f2011-06-08 08:27:56 +00002090 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002091 struct btrfs_device *device;
2092 struct btrfs_device *next;
2093 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002094 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002095
2096 if (!capable(CAP_SYS_ADMIN))
2097 return -EPERM;
2098
Li Zefan027ed2f2011-06-08 08:27:56 +00002099 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2100 if (!fi_args)
2101 return -ENOMEM;
2102
2103 fi_args->num_devices = fs_devices->num_devices;
2104 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002105
2106 mutex_lock(&fs_devices->device_list_mutex);
2107 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002108 if (device->devid > fi_args->max_id)
2109 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002110 }
2111 mutex_unlock(&fs_devices->device_list_mutex);
2112
Li Zefan027ed2f2011-06-08 08:27:56 +00002113 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2114 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002115
Li Zefan027ed2f2011-06-08 08:27:56 +00002116 kfree(fi_args);
2117 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002118}
2119
2120static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2121{
2122 struct btrfs_ioctl_dev_info_args *di_args;
2123 struct btrfs_device *dev;
2124 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2125 int ret = 0;
2126 char *s_uuid = NULL;
2127 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2128
2129 if (!capable(CAP_SYS_ADMIN))
2130 return -EPERM;
2131
2132 di_args = memdup_user(arg, sizeof(*di_args));
2133 if (IS_ERR(di_args))
2134 return PTR_ERR(di_args);
2135
2136 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2137 s_uuid = di_args->uuid;
2138
2139 mutex_lock(&fs_devices->device_list_mutex);
2140 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2141 mutex_unlock(&fs_devices->device_list_mutex);
2142
2143 if (!dev) {
2144 ret = -ENODEV;
2145 goto out;
2146 }
2147
2148 di_args->devid = dev->devid;
2149 di_args->bytes_used = dev->bytes_used;
2150 di_args->total_bytes = dev->total_bytes;
2151 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2152 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2153
2154out:
2155 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2156 ret = -EFAULT;
2157
2158 kfree(di_args);
2159 return ret;
2160}
2161
Yan, Zheng76dda932009-09-21 16:00:26 -04002162static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2163 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002164{
2165 struct inode *inode = fdentry(file)->d_inode;
2166 struct btrfs_root *root = BTRFS_I(inode)->root;
2167 struct file *src_file;
2168 struct inode *src;
2169 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002170 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002171 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002172 char *buf;
2173 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002174 u32 nritems;
2175 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002176 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002177 u64 len = olen;
2178 u64 bs = root->fs_info->sb->s_blocksize;
2179 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002180
Sage Weilc5c9cd42008-11-12 14:32:25 -05002181 /*
2182 * TODO:
2183 * - split compressed inline extents. annoying: we need to
2184 * decompress into destination's address_space (the file offset
2185 * may change, so source mapping won't do), then recompress (or
2186 * otherwise reinsert) a subrange.
2187 * - allow ranges within the same file to be cloned (provided
2188 * they don't overlap)?
2189 */
2190
Chris Masone441d542009-01-05 16:57:23 -05002191 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002192 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002193 return -EINVAL;
2194
Li Zefanb83cc962010-12-20 16:04:08 +08002195 if (btrfs_root_readonly(root))
2196 return -EROFS;
2197
Al Viroa561be72011-11-23 11:57:51 -05002198 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002199 if (ret)
2200 return ret;
2201
Sage Weilc5c9cd42008-11-12 14:32:25 -05002202 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002203 if (!src_file) {
2204 ret = -EBADF;
2205 goto out_drop_write;
2206 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002207
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002208 src = src_file->f_dentry->d_inode;
2209
Sage Weilc5c9cd42008-11-12 14:32:25 -05002210 ret = -EINVAL;
2211 if (src == inode)
2212 goto out_fput;
2213
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002214 /* the src must be open for reading */
2215 if (!(src_file->f_mode & FMODE_READ))
2216 goto out_fput;
2217
Li Zefan0e7b8242011-09-18 10:20:46 -04002218 /* don't make the dst file partly checksummed */
2219 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2220 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2221 goto out_fput;
2222
Yan Zhengae01a0a2008-08-04 23:23:47 -04002223 ret = -EISDIR;
2224 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002225 goto out_fput;
2226
Yan Zhengae01a0a2008-08-04 23:23:47 -04002227 ret = -EXDEV;
2228 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2229 goto out_fput;
2230
2231 ret = -ENOMEM;
2232 buf = vmalloc(btrfs_level_size(root, 0));
2233 if (!buf)
2234 goto out_fput;
2235
2236 path = btrfs_alloc_path();
2237 if (!path) {
2238 vfree(buf);
2239 goto out_fput;
2240 }
2241 path->reada = 2;
2242
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002243 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002244 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2245 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002246 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002247 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2248 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002249 }
2250
Sage Weilc5c9cd42008-11-12 14:32:25 -05002251 /* determine range to clone */
2252 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002253 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002254 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002255 if (len == 0)
2256 olen = len = src->i_size - off;
2257 /* if we extend to eof, continue to block boundary */
2258 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002259 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002260
2261 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002262 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2263 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002264 goto out_unlock;
2265
Li Zefand525e8a2011-09-11 10:52:25 -04002266 if (destoff > inode->i_size) {
2267 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2268 if (ret)
2269 goto out_unlock;
2270 }
2271
Li Zefan71ef0782011-09-18 10:20:46 -04002272 /* truncate page cache pages from target inode range */
2273 truncate_inode_pages_range(&inode->i_data, destoff,
2274 PAGE_CACHE_ALIGN(destoff + len) - 1);
2275
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002276 /* do any pending delalloc/csum calc on src, one way or
2277 another, and lock file content */
2278 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002279 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002280 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04002281 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2282 if (!ordered &&
2283 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2284 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002285 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002286 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002287 if (ordered)
2288 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002289 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002290 }
2291
Sage Weilc5c9cd42008-11-12 14:32:25 -05002292 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002293 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002294 key.type = BTRFS_EXTENT_DATA_KEY;
2295 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002296
2297 while (1) {
2298 /*
2299 * note the key will change type as we walk through the
2300 * tree.
2301 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002303 if (ret < 0)
2304 goto out;
2305
Yan Zhengae01a0a2008-08-04 23:23:47 -04002306 nritems = btrfs_header_nritems(path->nodes[0]);
2307 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002308 ret = btrfs_next_leaf(root, path);
2309 if (ret < 0)
2310 goto out;
2311 if (ret > 0)
2312 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002313 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002314 }
2315 leaf = path->nodes[0];
2316 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002317
Yan Zhengae01a0a2008-08-04 23:23:47 -04002318 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002319 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002320 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002321 break;
2322
Sage Weilc5c9cd42008-11-12 14:32:25 -05002323 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2324 struct btrfs_file_extent_item *extent;
2325 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002326 u32 size;
2327 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002328 u64 disko = 0, diskl = 0;
2329 u64 datao = 0, datal = 0;
2330 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002331 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002332
2333 size = btrfs_item_size_nr(leaf, slot);
2334 read_extent_buffer(leaf, buf,
2335 btrfs_item_ptr_offset(leaf, slot),
2336 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002337
2338 extent = btrfs_item_ptr(leaf, slot,
2339 struct btrfs_file_extent_item);
2340 comp = btrfs_file_extent_compression(leaf, extent);
2341 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002342 if (type == BTRFS_FILE_EXTENT_REG ||
2343 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002344 disko = btrfs_file_extent_disk_bytenr(leaf,
2345 extent);
2346 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2347 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002348 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002349 datal = btrfs_file_extent_num_bytes(leaf,
2350 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002351 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2352 /* take upper bound, may be compressed */
2353 datal = btrfs_file_extent_ram_bytes(leaf,
2354 extent);
2355 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002356 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002357
Sage Weil050006a2010-10-29 15:37:33 -04002358 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002359 key.offset >= off+len)
2360 goto next;
2361
Zheng Yan31840ae2008-09-23 13:14:14 -04002362 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002363 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002364 if (off <= key.offset)
2365 new_key.offset = key.offset + destoff - off;
2366 else
2367 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002368
Sage Weilb6f34092011-09-20 14:48:51 -04002369 /*
2370 * 1 - adjusting old extent (we may have to split it)
2371 * 1 - add new extent
2372 * 1 - inode update
2373 */
2374 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002375 if (IS_ERR(trans)) {
2376 ret = PTR_ERR(trans);
2377 goto out;
2378 }
2379
Chris Masonc8a894d2009-06-27 21:07:03 -04002380 if (type == BTRFS_FILE_EXTENT_REG ||
2381 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002382 /*
2383 * a | --- range to clone ---| b
2384 * | ------------- extent ------------- |
2385 */
2386
2387 /* substract range b */
2388 if (key.offset + datal > off + len)
2389 datal = off + len - key.offset;
2390
2391 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002392 if (off > key.offset) {
2393 datao += off - key.offset;
2394 datal -= off - key.offset;
2395 }
2396
Yan, Zhenga22285a2010-05-16 10:48:46 -04002397 ret = btrfs_drop_extents(trans, inode,
2398 new_key.offset,
2399 new_key.offset + datal,
2400 &hint_byte, 1);
2401 BUG_ON(ret);
2402
Sage Weilc5c9cd42008-11-12 14:32:25 -05002403 ret = btrfs_insert_empty_item(trans, root, path,
2404 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002405 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002406
2407 leaf = path->nodes[0];
2408 slot = path->slots[0];
2409 write_extent_buffer(leaf, buf,
2410 btrfs_item_ptr_offset(leaf, slot),
2411 size);
2412
2413 extent = btrfs_item_ptr(leaf, slot,
2414 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002415
Sage Weilc5c9cd42008-11-12 14:32:25 -05002416 /* disko == 0 means it's a hole */
2417 if (!disko)
2418 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002419
2420 btrfs_set_file_extent_offset(leaf, extent,
2421 datao);
2422 btrfs_set_file_extent_num_bytes(leaf, extent,
2423 datal);
2424 if (disko) {
2425 inode_add_bytes(inode, datal);
2426 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002427 disko, diskl, 0,
2428 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002429 btrfs_ino(inode),
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002430 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002431 BUG_ON(ret);
2432 }
2433 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2434 u64 skip = 0;
2435 u64 trim = 0;
2436 if (off > key.offset) {
2437 skip = off - key.offset;
2438 new_key.offset += skip;
2439 }
Chris Masond3977122009-01-05 21:25:51 -05002440
Sage Weilc5c9cd42008-11-12 14:32:25 -05002441 if (key.offset + datal > off+len)
2442 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002443
Sage Weilc5c9cd42008-11-12 14:32:25 -05002444 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002445 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002446 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002447 goto out;
2448 }
2449 size -= skip + trim;
2450 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002451
2452 ret = btrfs_drop_extents(trans, inode,
2453 new_key.offset,
2454 new_key.offset + datal,
2455 &hint_byte, 1);
2456 BUG_ON(ret);
2457
Sage Weilc5c9cd42008-11-12 14:32:25 -05002458 ret = btrfs_insert_empty_item(trans, root, path,
2459 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002460 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002461
2462 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002463 u32 start =
2464 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002465 memmove(buf+start, buf+start+skip,
2466 datal);
2467 }
2468
2469 leaf = path->nodes[0];
2470 slot = path->slots[0];
2471 write_extent_buffer(leaf, buf,
2472 btrfs_item_ptr_offset(leaf, slot),
2473 size);
2474 inode_add_bytes(inode, datal);
2475 }
2476
2477 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002478 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002479
Yan, Zhenga22285a2010-05-16 10:48:46 -04002480 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002481
2482 /*
2483 * we round up to the block size at eof when
2484 * determining which extents to clone above,
2485 * but shouldn't round up the file size
2486 */
2487 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002488 if (endoff > destoff+olen)
2489 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002490 if (endoff > inode->i_size)
2491 btrfs_i_size_write(inode, endoff);
2492
Yan, Zhenga22285a2010-05-16 10:48:46 -04002493 ret = btrfs_update_inode(trans, root, inode);
2494 BUG_ON(ret);
2495 btrfs_end_transaction(trans, root);
2496 }
Chris Masond3977122009-01-05 21:25:51 -05002497next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002498 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002499 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002500 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501 ret = 0;
2502out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002503 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002504 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002505out_unlock:
2506 mutex_unlock(&src->i_mutex);
2507 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002508 vfree(buf);
2509 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002510out_fput:
2511 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002512out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002513 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002514 return ret;
2515}
2516
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002517static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002518{
2519 struct btrfs_ioctl_clone_range_args args;
2520
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002521 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002522 return -EFAULT;
2523 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2524 args.src_length, args.dest_offset);
2525}
2526
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002527/*
2528 * there are many ways the trans_start and trans_end ioctls can lead
2529 * to deadlocks. They should only be used by applications that
2530 * basically own the machine, and have a very in depth understanding
2531 * of all the possible deadlocks and enospc problems.
2532 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002533static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002534{
2535 struct inode *inode = fdentry(file)->d_inode;
2536 struct btrfs_root *root = BTRFS_I(inode)->root;
2537 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002538 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002539
Sage Weil1ab86ae2009-09-29 18:38:44 -04002540 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002541 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002542 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002543
2544 ret = -EINPROGRESS;
2545 if (file->private_data)
2546 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002547
Li Zefanb83cc962010-12-20 16:04:08 +08002548 ret = -EROFS;
2549 if (btrfs_root_readonly(root))
2550 goto out;
2551
Al Viroa561be72011-11-23 11:57:51 -05002552 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002553 if (ret)
2554 goto out;
2555
Josef Bacika4abeea2011-04-11 17:25:13 -04002556 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002557
Sage Weil1ab86ae2009-09-29 18:38:44 -04002558 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002559 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002560 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002561 goto out_drop;
2562
2563 file->private_data = trans;
2564 return 0;
2565
2566out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002567 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002568 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002569out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002570 return ret;
2571}
2572
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002573static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2574{
2575 struct inode *inode = fdentry(file)->d_inode;
2576 struct btrfs_root *root = BTRFS_I(inode)->root;
2577 struct btrfs_root *new_root;
2578 struct btrfs_dir_item *di;
2579 struct btrfs_trans_handle *trans;
2580 struct btrfs_path *path;
2581 struct btrfs_key location;
2582 struct btrfs_disk_key disk_key;
2583 struct btrfs_super_block *disk_super;
2584 u64 features;
2585 u64 objectid = 0;
2586 u64 dir_id;
2587
2588 if (!capable(CAP_SYS_ADMIN))
2589 return -EPERM;
2590
2591 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2592 return -EFAULT;
2593
2594 if (!objectid)
2595 objectid = root->root_key.objectid;
2596
2597 location.objectid = objectid;
2598 location.type = BTRFS_ROOT_ITEM_KEY;
2599 location.offset = (u64)-1;
2600
2601 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2602 if (IS_ERR(new_root))
2603 return PTR_ERR(new_root);
2604
2605 if (btrfs_root_refs(&new_root->root_item) == 0)
2606 return -ENOENT;
2607
2608 path = btrfs_alloc_path();
2609 if (!path)
2610 return -ENOMEM;
2611 path->leave_spinning = 1;
2612
2613 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002614 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002615 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002616 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002617 }
2618
David Sterba6c417612011-04-13 15:41:04 +02002619 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002620 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2621 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002622 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002623 btrfs_free_path(path);
2624 btrfs_end_transaction(trans, root);
2625 printk(KERN_ERR "Umm, you don't have the default dir item, "
2626 "this isn't going to work\n");
2627 return -ENOENT;
2628 }
2629
2630 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2631 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2632 btrfs_mark_buffer_dirty(path->nodes[0]);
2633 btrfs_free_path(path);
2634
David Sterba6c417612011-04-13 15:41:04 +02002635 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002636 features = btrfs_super_incompat_flags(disk_super);
2637 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2638 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2639 btrfs_set_super_incompat_flags(disk_super, features);
2640 }
2641 btrfs_end_transaction(trans, root);
2642
2643 return 0;
2644}
2645
Josef Bacikbf5fc092010-09-29 11:22:36 -04002646static void get_block_group_info(struct list_head *groups_list,
2647 struct btrfs_ioctl_space_info *space)
2648{
2649 struct btrfs_block_group_cache *block_group;
2650
2651 space->total_bytes = 0;
2652 space->used_bytes = 0;
2653 space->flags = 0;
2654 list_for_each_entry(block_group, groups_list, list) {
2655 space->flags = block_group->flags;
2656 space->total_bytes += block_group->key.offset;
2657 space->used_bytes +=
2658 btrfs_block_group_used(&block_group->item);
2659 }
2660}
2661
Josef Bacik1406e432010-01-13 18:19:06 +00002662long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2663{
2664 struct btrfs_ioctl_space_args space_args;
2665 struct btrfs_ioctl_space_info space;
2666 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002667 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002668 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002669 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002670 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2671 BTRFS_BLOCK_GROUP_SYSTEM,
2672 BTRFS_BLOCK_GROUP_METADATA,
2673 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2674 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002675 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002676 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002677 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002678 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002679
2680 if (copy_from_user(&space_args,
2681 (struct btrfs_ioctl_space_args __user *)arg,
2682 sizeof(space_args)))
2683 return -EFAULT;
2684
Josef Bacikbf5fc092010-09-29 11:22:36 -04002685 for (i = 0; i < num_types; i++) {
2686 struct btrfs_space_info *tmp;
2687
2688 info = NULL;
2689 rcu_read_lock();
2690 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2691 list) {
2692 if (tmp->flags == types[i]) {
2693 info = tmp;
2694 break;
2695 }
2696 }
2697 rcu_read_unlock();
2698
2699 if (!info)
2700 continue;
2701
2702 down_read(&info->groups_sem);
2703 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2704 if (!list_empty(&info->block_groups[c]))
2705 slot_count++;
2706 }
2707 up_read(&info->groups_sem);
2708 }
Josef Bacik1406e432010-01-13 18:19:06 +00002709
Chris Mason7fde62b2010-03-16 15:40:10 -04002710 /* space_slots == 0 means they are asking for a count */
2711 if (space_args.space_slots == 0) {
2712 space_args.total_spaces = slot_count;
2713 goto out;
2714 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002715
Dan Rosenberg51788b12011-02-14 16:04:23 -05002716 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002717
Chris Mason7fde62b2010-03-16 15:40:10 -04002718 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002719
Chris Mason7fde62b2010-03-16 15:40:10 -04002720 /* we generally have at most 6 or so space infos, one for each raid
2721 * level. So, a whole page should be more than enough for everyone
2722 */
2723 if (alloc_size > PAGE_CACHE_SIZE)
2724 return -ENOMEM;
2725
2726 space_args.total_spaces = 0;
2727 dest = kmalloc(alloc_size, GFP_NOFS);
2728 if (!dest)
2729 return -ENOMEM;
2730 dest_orig = dest;
2731
2732 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002733 for (i = 0; i < num_types; i++) {
2734 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002735
Dan Rosenberg51788b12011-02-14 16:04:23 -05002736 if (!slot_count)
2737 break;
2738
Josef Bacikbf5fc092010-09-29 11:22:36 -04002739 info = NULL;
2740 rcu_read_lock();
2741 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2742 list) {
2743 if (tmp->flags == types[i]) {
2744 info = tmp;
2745 break;
2746 }
2747 }
2748 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002749
Josef Bacikbf5fc092010-09-29 11:22:36 -04002750 if (!info)
2751 continue;
2752 down_read(&info->groups_sem);
2753 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2754 if (!list_empty(&info->block_groups[c])) {
2755 get_block_group_info(&info->block_groups[c],
2756 &space);
2757 memcpy(dest, &space, sizeof(space));
2758 dest++;
2759 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002760 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002761 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002762 if (!slot_count)
2763 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002764 }
2765 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002766 }
Josef Bacik1406e432010-01-13 18:19:06 +00002767
Chris Mason7fde62b2010-03-16 15:40:10 -04002768 user_dest = (struct btrfs_ioctl_space_info *)
2769 (arg + sizeof(struct btrfs_ioctl_space_args));
2770
2771 if (copy_to_user(user_dest, dest_orig, alloc_size))
2772 ret = -EFAULT;
2773
2774 kfree(dest_orig);
2775out:
2776 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002777 ret = -EFAULT;
2778
2779 return ret;
2780}
2781
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002782/*
2783 * there are many ways the trans_start and trans_end ioctls can lead
2784 * to deadlocks. They should only be used by applications that
2785 * basically own the machine, and have a very in depth understanding
2786 * of all the possible deadlocks and enospc problems.
2787 */
2788long btrfs_ioctl_trans_end(struct file *file)
2789{
2790 struct inode *inode = fdentry(file)->d_inode;
2791 struct btrfs_root *root = BTRFS_I(inode)->root;
2792 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002793
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002794 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002795 if (!trans)
2796 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002797 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002798
Sage Weil1ab86ae2009-09-29 18:38:44 -04002799 btrfs_end_transaction(trans, root);
2800
Josef Bacika4abeea2011-04-11 17:25:13 -04002801 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002802
Al Viro2a79f172011-12-09 08:06:57 -05002803 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002804 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002805}
2806
Sage Weil46204592010-10-29 15:41:32 -04002807static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2808{
2809 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2810 struct btrfs_trans_handle *trans;
2811 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002812 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002813
2814 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002815 if (IS_ERR(trans))
2816 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002817 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002818 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002819 if (ret) {
2820 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002821 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002822 }
Sage Weil46204592010-10-29 15:41:32 -04002823
2824 if (argp)
2825 if (copy_to_user(argp, &transid, sizeof(transid)))
2826 return -EFAULT;
2827 return 0;
2828}
2829
2830static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2831{
2832 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2833 u64 transid;
2834
2835 if (argp) {
2836 if (copy_from_user(&transid, argp, sizeof(transid)))
2837 return -EFAULT;
2838 } else {
2839 transid = 0; /* current trans */
2840 }
2841 return btrfs_wait_for_commit(root, transid);
2842}
2843
Jan Schmidt475f6382011-03-11 15:41:01 +01002844static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2845{
2846 int ret;
2847 struct btrfs_ioctl_scrub_args *sa;
2848
2849 if (!capable(CAP_SYS_ADMIN))
2850 return -EPERM;
2851
2852 sa = memdup_user(arg, sizeof(*sa));
2853 if (IS_ERR(sa))
2854 return PTR_ERR(sa);
2855
2856 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01002857 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01002858
2859 if (copy_to_user(arg, sa, sizeof(*sa)))
2860 ret = -EFAULT;
2861
2862 kfree(sa);
2863 return ret;
2864}
2865
2866static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2867{
2868 if (!capable(CAP_SYS_ADMIN))
2869 return -EPERM;
2870
2871 return btrfs_scrub_cancel(root);
2872}
2873
2874static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2875 void __user *arg)
2876{
2877 struct btrfs_ioctl_scrub_args *sa;
2878 int ret;
2879
2880 if (!capable(CAP_SYS_ADMIN))
2881 return -EPERM;
2882
2883 sa = memdup_user(arg, sizeof(*sa));
2884 if (IS_ERR(sa))
2885 return PTR_ERR(sa);
2886
2887 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2888
2889 if (copy_to_user(arg, sa, sizeof(*sa)))
2890 ret = -EFAULT;
2891
2892 kfree(sa);
2893 return ret;
2894}
2895
Jan Schmidtd7728c92011-07-07 16:48:38 +02002896static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2897{
2898 int ret = 0;
2899 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04002900 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002901 int size;
Chris Mason806468f2011-11-06 03:07:10 -05002902 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002903 struct inode_fs_paths *ipath = NULL;
2904 struct btrfs_path *path;
2905
2906 if (!capable(CAP_SYS_ADMIN))
2907 return -EPERM;
2908
2909 path = btrfs_alloc_path();
2910 if (!path) {
2911 ret = -ENOMEM;
2912 goto out;
2913 }
2914
2915 ipa = memdup_user(arg, sizeof(*ipa));
2916 if (IS_ERR(ipa)) {
2917 ret = PTR_ERR(ipa);
2918 ipa = NULL;
2919 goto out;
2920 }
2921
2922 size = min_t(u32, ipa->size, 4096);
2923 ipath = init_ipath(size, root, path);
2924 if (IS_ERR(ipath)) {
2925 ret = PTR_ERR(ipath);
2926 ipath = NULL;
2927 goto out;
2928 }
2929
2930 ret = paths_from_inode(ipa->inum, ipath);
2931 if (ret < 0)
2932 goto out;
2933
2934 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002935 rel_ptr = ipath->fspath->val[i] -
2936 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04002937 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002938 }
2939
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002940 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2941 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02002942 if (ret) {
2943 ret = -EFAULT;
2944 goto out;
2945 }
2946
2947out:
2948 btrfs_free_path(path);
2949 free_ipath(ipath);
2950 kfree(ipa);
2951
2952 return ret;
2953}
2954
2955static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
2956{
2957 struct btrfs_data_container *inodes = ctx;
2958 const size_t c = 3 * sizeof(u64);
2959
2960 if (inodes->bytes_left >= c) {
2961 inodes->bytes_left -= c;
2962 inodes->val[inodes->elem_cnt] = inum;
2963 inodes->val[inodes->elem_cnt + 1] = offset;
2964 inodes->val[inodes->elem_cnt + 2] = root;
2965 inodes->elem_cnt += 3;
2966 } else {
2967 inodes->bytes_missing += c - inodes->bytes_left;
2968 inodes->bytes_left = 0;
2969 inodes->elem_missed += 3;
2970 }
2971
2972 return 0;
2973}
2974
2975static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
2976 void __user *arg)
2977{
2978 int ret = 0;
2979 int size;
2980 u64 extent_offset;
2981 struct btrfs_ioctl_logical_ino_args *loi;
2982 struct btrfs_data_container *inodes = NULL;
2983 struct btrfs_path *path = NULL;
2984 struct btrfs_key key;
2985
2986 if (!capable(CAP_SYS_ADMIN))
2987 return -EPERM;
2988
2989 loi = memdup_user(arg, sizeof(*loi));
2990 if (IS_ERR(loi)) {
2991 ret = PTR_ERR(loi);
2992 loi = NULL;
2993 goto out;
2994 }
2995
2996 path = btrfs_alloc_path();
2997 if (!path) {
2998 ret = -ENOMEM;
2999 goto out;
3000 }
3001
3002 size = min_t(u32, loi->size, 4096);
3003 inodes = init_data_container(size);
3004 if (IS_ERR(inodes)) {
3005 ret = PTR_ERR(inodes);
3006 inodes = NULL;
3007 goto out;
3008 }
3009
3010 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
3011
3012 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3013 ret = -ENOENT;
3014 if (ret < 0)
3015 goto out;
3016
3017 extent_offset = loi->logical - key.objectid;
3018 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
3019 extent_offset, build_ino_list, inodes);
3020
3021 if (ret < 0)
3022 goto out;
3023
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003024 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3025 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003026 if (ret)
3027 ret = -EFAULT;
3028
3029out:
3030 btrfs_free_path(path);
3031 kfree(inodes);
3032 kfree(loi);
3033
3034 return ret;
3035}
3036
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003037long btrfs_ioctl(struct file *file, unsigned int
3038 cmd, unsigned long arg)
3039{
3040 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003041 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003042
3043 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003044 case FS_IOC_GETFLAGS:
3045 return btrfs_ioctl_getflags(file, argp);
3046 case FS_IOC_SETFLAGS:
3047 return btrfs_ioctl_setflags(file, argp);
3048 case FS_IOC_GETVERSION:
3049 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003050 case FITRIM:
3051 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003052 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003053 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003054 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003055 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003056 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003057 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003058 case BTRFS_IOC_SNAP_DESTROY:
3059 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003060 case BTRFS_IOC_SUBVOL_GETFLAGS:
3061 return btrfs_ioctl_subvol_getflags(file, argp);
3062 case BTRFS_IOC_SUBVOL_SETFLAGS:
3063 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003064 case BTRFS_IOC_DEFAULT_SUBVOL:
3065 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003066 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003067 return btrfs_ioctl_defrag(file, NULL);
3068 case BTRFS_IOC_DEFRAG_RANGE:
3069 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003070 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003071 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003072 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003073 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003074 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003075 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003076 case BTRFS_IOC_FS_INFO:
3077 return btrfs_ioctl_fs_info(root, argp);
3078 case BTRFS_IOC_DEV_INFO:
3079 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003080 case BTRFS_IOC_BALANCE:
3081 return btrfs_balance(root->fs_info->dev_root);
3082 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003083 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3084 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003085 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003086 case BTRFS_IOC_TRANS_START:
3087 return btrfs_ioctl_trans_start(file);
3088 case BTRFS_IOC_TRANS_END:
3089 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003090 case BTRFS_IOC_TREE_SEARCH:
3091 return btrfs_ioctl_tree_search(file, argp);
3092 case BTRFS_IOC_INO_LOOKUP:
3093 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003094 case BTRFS_IOC_INO_PATHS:
3095 return btrfs_ioctl_ino_to_path(root, argp);
3096 case BTRFS_IOC_LOGICAL_INO:
3097 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003098 case BTRFS_IOC_SPACE_INFO:
3099 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003100 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003101 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3102 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003103 case BTRFS_IOC_START_SYNC:
3104 return btrfs_ioctl_start_sync(file, argp);
3105 case BTRFS_IOC_WAIT_SYNC:
3106 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003107 case BTRFS_IOC_SCRUB:
3108 return btrfs_ioctl_scrub(root, argp);
3109 case BTRFS_IOC_SCRUB_CANCEL:
3110 return btrfs_ioctl_scrub_cancel(root, argp);
3111 case BTRFS_IOC_SCRUB_PROGRESS:
3112 return btrfs_ioctl_scrub_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003113 }
3114
3115 return -ENOTTY;
3116}