blob: 8079ebfeaf503e50add674c895f1d110089ae0c5 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
48#include "ioctl.h"
49#include "print-tree.h"
50#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040051#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
Christoph Hellwig6cbff002009-04-17 10:37:41 +020053/* Mask out flags that are inappropriate for the given type of inode. */
54static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
55{
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
62}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/*
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 */
67static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
68{
69 unsigned int iflags = 0;
70
71 if (flags & BTRFS_INODE_SYNC)
72 iflags |= FS_SYNC_FL;
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
83
84 return iflags;
85}
86
87/*
88 * Update inode->i_flags based on the btrfs internal flags.
89 */
90void btrfs_update_iflags(struct inode *inode)
91{
92 struct btrfs_inode *ip = BTRFS_I(inode);
93
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
95
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
106}
107
108/*
109 * Inherit flags from the parent inode.
110 *
111 * Unlike extN we don't have any flags we don't want to inherit currently.
112 */
113void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
114{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400115 unsigned int flags;
116
117 if (!dir)
118 return;
119
120 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200121
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
126
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
129}
130
131static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
132{
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
135
136 if (copy_to_user(arg, &flags, sizeof(flags)))
137 return -EFAULT;
138 return 0;
139}
140
141static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
142{
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
148 int ret;
149
150 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT;
152
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
156 return -EOPNOTSUPP;
157
158 if (!is_owner_or_cap(inode))
159 return -EACCES;
160
161 mutex_lock(&inode->i_mutex);
162
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = -EPERM;
168 goto out_unlock;
169 }
170 }
171
172 ret = mnt_want_write(file->f_path.mnt);
173 if (ret)
174 goto out_unlock;
175
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
178 else
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 else
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
186 else
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
190 else
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
194 else
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
200
201
202 trans = btrfs_join_transaction(root, 1);
203 BUG_ON(!trans);
204
205 ret = btrfs_update_inode(trans, root, inode);
206 BUG_ON(ret);
207
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
211
212 mnt_drop_write(file->f_path.mnt);
213 out_unlock:
214 mutex_unlock(&inode->i_mutex);
215 return 0;
216}
217
218static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
219{
220 struct inode *inode = file->f_path.dentry->d_inode;
221
222 return put_user(inode->i_generation, arg);
223}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400224
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400225static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
227 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400228{
229 struct btrfs_trans_handle *trans;
230 struct btrfs_key key;
231 struct btrfs_root_item root_item;
232 struct btrfs_inode_item *inode_item;
233 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400234 struct btrfs_root *new_root;
235 struct inode *dir = dentry->d_parent->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400236 int ret;
237 int err;
238 u64 objectid;
239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500240 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400241
Yan, Zhenga22285a2010-05-16 10:48:46 -0400242 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
243 0, &objectid);
244 if (ret)
245 return ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400246 /*
247 * 1 - inode item
248 * 2 - refs
249 * 1 - root item
250 * 2 - dir items
251 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400252 trans = btrfs_start_transaction(root, 6);
253 if (IS_ERR(trans))
254 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400255
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400256 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
257 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400258 if (IS_ERR(leaf)) {
259 ret = PTR_ERR(leaf);
260 goto fail;
261 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400262
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400264 btrfs_set_header_bytenr(leaf, leaf->start);
265 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400267 btrfs_set_header_owner(leaf, objectid);
268
269 write_extent_buffer(leaf, root->fs_info->fsid,
270 (unsigned long)btrfs_header_fsid(leaf),
271 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400272 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
273 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
274 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400275 btrfs_mark_buffer_dirty(leaf);
276
277 inode_item = &root_item.inode;
278 memset(inode_item, 0, sizeof(*inode_item));
279 inode_item->generation = cpu_to_le64(1);
280 inode_item->size = cpu_to_le64(3);
281 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400282 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400283 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
284
285 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400286 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400287 btrfs_set_root_level(&root_item, 0);
288 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000289 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400290 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400291
292 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
293 root_item.drop_level = 0;
294
Chris Mason925baed2008-06-25 16:01:30 -0400295 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400296 free_extent_buffer(leaf);
297 leaf = NULL;
298
299 btrfs_set_root_dirid(&root_item, new_dirid);
300
301 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400302 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400303 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
304 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
305 &root_item);
306 if (ret)
307 goto fail;
308
Yan, Zheng76dda932009-09-21 16:00:26 -0400309 key.offset = (u64)-1;
310 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
311 BUG_ON(IS_ERR(new_root));
312
313 btrfs_record_root_in_trans(trans, new_root);
314
315 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
316 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400317 /*
318 * insert the directory item
319 */
Chris Mason3de45862008-11-17 21:02:50 -0500320 ret = btrfs_set_inode_index(dir, &index);
321 BUG_ON(ret);
322
323 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400324 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500325 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400326 if (ret)
327 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500328
Yan Zheng52c26172009-01-05 15:43:43 -0500329 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
330 ret = btrfs_update_inode(trans, root, dir);
331 BUG_ON(ret);
332
Chris Mason0660b5a2008-11-17 20:37:39 -0500333 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400334 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500335 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400336
Chris Mason0660b5a2008-11-17 20:37:39 -0500337 BUG_ON(ret);
338
Yan, Zheng76dda932009-09-21 16:00:26 -0400339 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400340fail:
Yan, Zheng76dda932009-09-21 16:00:26 -0400341 err = btrfs_commit_transaction(trans, root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400342 if (err && !ret)
343 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400344 return ret;
345}
346
Yan, Zhenga22285a2010-05-16 10:48:46 -0400347static int create_snapshot(struct btrfs_root *root, struct dentry *dentry)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000349 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400350 struct btrfs_pending_snapshot *pending_snapshot;
351 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000352 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353
354 if (!root->ref_cows)
355 return -EINVAL;
356
Yan, Zhenga22285a2010-05-16 10:48:46 -0400357 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
358 if (!pending_snapshot)
359 return -ENOMEM;
360
361 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
362 pending_snapshot->dentry = dentry;
363 pending_snapshot->root = root;
364
365 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
366 if (IS_ERR(trans)) {
367 ret = PTR_ERR(trans);
368 goto fail;
369 }
370
371 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
372 BUG_ON(ret);
373
374 list_add(&pending_snapshot->list,
375 &trans->transaction->pending_snapshots);
376 ret = btrfs_commit_transaction(trans, root->fs_info->extent_root);
377 BUG_ON(ret);
378
379 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000381 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382
Yan, Zhenga22285a2010-05-16 10:48:46 -0400383 btrfs_orphan_cleanup(pending_snapshot->snap);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400384
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000385 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
386 if (IS_ERR(inode)) {
387 ret = PTR_ERR(inode);
388 goto fail;
389 }
390 BUG_ON(!inode);
391 d_instantiate(dentry, inode);
392 ret = 0;
393fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400394 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400395 return ret;
396}
397
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400398/* copy of may_create in fs/namei.c() */
399static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
400{
401 if (child->d_inode)
402 return -EEXIST;
403 if (IS_DEADDIR(dir))
404 return -ENOENT;
405 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
406}
407
408/*
409 * Create a new subvolume below @parent. This is largely modeled after
410 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
411 * inside this filesystem so it's quite a bit simpler.
412 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400413static noinline int btrfs_mksubvol(struct path *parent,
414 char *name, int namelen,
Chris Mason3de45862008-11-17 21:02:50 -0500415 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400416{
Yan, Zheng76dda932009-09-21 16:00:26 -0400417 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400418 struct dentry *dentry;
419 int error;
420
Yan, Zheng76dda932009-09-21 16:00:26 -0400421 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400422
423 dentry = lookup_one_len(name, parent->dentry, namelen);
424 error = PTR_ERR(dentry);
425 if (IS_ERR(dentry))
426 goto out_unlock;
427
428 error = -EEXIST;
429 if (dentry->d_inode)
430 goto out_dput;
431
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400432 error = mnt_want_write(parent->mnt);
433 if (error)
434 goto out_dput;
435
Yan, Zheng76dda932009-09-21 16:00:26 -0400436 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400437 if (error)
438 goto out_drop_write;
439
Yan, Zheng76dda932009-09-21 16:00:26 -0400440 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
441
442 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
443 goto out_up_read;
444
Chris Mason3de45862008-11-17 21:02:50 -0500445 if (snap_src) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400446 error = create_snapshot(snap_src, dentry);
Chris Mason3de45862008-11-17 21:02:50 -0500447 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400448 error = create_subvol(BTRFS_I(dir)->root, dentry,
449 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500450 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400451 if (!error)
452 fsnotify_mkdir(dir, dentry);
453out_up_read:
454 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400455out_drop_write:
456 mnt_drop_write(parent->mnt);
457out_dput:
458 dput(dentry);
459out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400460 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400461 return error;
462}
463
Chris Mason940100a2010-03-10 10:52:59 -0500464static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500465 int thresh, u64 *last_len, u64 *skip,
466 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500467{
468 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
469 struct extent_map *em = NULL;
470 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
471 int ret = 1;
472
Chris Mason1e701a32010-03-11 09:42:04 -0500473
474 if (thresh == 0)
475 thresh = 256 * 1024;
476
Chris Mason940100a2010-03-10 10:52:59 -0500477 /*
478 * make sure that once we start defragging and extent, we keep on
479 * defragging it
480 */
481 if (start < *defrag_end)
482 return 1;
483
484 *skip = 0;
485
486 /*
487 * hopefully we have this extent in the tree already, try without
488 * the full extent lock
489 */
490 read_lock(&em_tree->lock);
491 em = lookup_extent_mapping(em_tree, start, len);
492 read_unlock(&em_tree->lock);
493
494 if (!em) {
495 /* get the big lock and read metadata off disk */
496 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
497 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
498 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
499
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000500 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500501 return 0;
502 }
503
504 /* this will cover holes, and inline extents */
505 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
506 ret = 0;
507
508 /*
509 * we hit a real extent, if it is big don't bother defragging it again
510 */
Chris Mason1e701a32010-03-11 09:42:04 -0500511 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500512 ret = 0;
513
514 /*
515 * last_len ends up being a counter of how many bytes we've defragged.
516 * every time we choose not to defrag an extent, we reset *last_len
517 * so that the next tiny extent will force a defrag.
518 *
519 * The end result of this is that tiny extents before a single big
520 * extent will force at least part of that big extent to be defragged.
521 */
522 if (ret) {
523 *last_len += len;
524 *defrag_end = extent_map_end(em);
525 } else {
526 *last_len = 0;
527 *skip = extent_map_end(em);
528 *defrag_end = 0;
529 }
530
531 free_extent_map(em);
532 return ret;
533}
534
Chris Mason1e701a32010-03-11 09:42:04 -0500535static int btrfs_defrag_file(struct file *file,
536 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400537{
538 struct inode *inode = fdentry(file)->d_inode;
539 struct btrfs_root *root = BTRFS_I(inode)->root;
540 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400541 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400542 struct page *page;
543 unsigned long last_index;
544 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
545 unsigned long total_read = 0;
546 u64 page_start;
547 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500548 u64 last_len = 0;
549 u64 skip = 0;
550 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400551 unsigned long i;
552 int ret;
553
Chris Mason940100a2010-03-10 10:52:59 -0500554 if (inode->i_size == 0)
555 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400556
Chris Mason1e701a32010-03-11 09:42:04 -0500557 if (range->start + range->len > range->start) {
558 last_index = min_t(u64, inode->i_size - 1,
559 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
560 } else {
561 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
562 }
563
564 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500565 while (i <= last_index) {
566 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500567 PAGE_CACHE_SIZE,
568 range->extent_thresh,
569 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500570 &defrag_end)) {
571 unsigned long next;
572 /*
573 * the should_defrag function tells us how much to skip
574 * bump our counter by the suggested amount
575 */
576 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
577 i = max(i + 1, next);
578 continue;
579 }
580
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400581 if (total_read % ra_pages == 0) {
582 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
583 min(last_index, i + ra_pages - 1));
584 }
585 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500586 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500587 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
588 BTRFS_I(inode)->force_compress = 1;
Chris Mason940100a2010-03-10 10:52:59 -0500589
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400590 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
591 if (ret)
592 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400593again:
Chris Mason940100a2010-03-10 10:52:59 -0500594 if (inode->i_size == 0 ||
595 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
596 ret = 0;
597 goto err_reservations;
598 }
599
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400600 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400601 if (!page) {
602 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500603 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400604 }
Chris Mason940100a2010-03-10 10:52:59 -0500605
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400606 if (!PageUptodate(page)) {
607 btrfs_readpage(NULL, page);
608 lock_page(page);
609 if (!PageUptodate(page)) {
610 unlock_page(page);
611 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400612 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500613 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400614 }
615 }
616
Chris Mason940100a2010-03-10 10:52:59 -0500617 if (page->mapping != inode->i_mapping) {
618 unlock_page(page);
619 page_cache_release(page);
620 goto again;
621 }
622
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400624
Chris Mason940100a2010-03-10 10:52:59 -0500625 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400626 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500627 goto loop_unlock;
628 }
629
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400630 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
631 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400632 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400633
634 ordered = btrfs_lookup_ordered_extent(inode, page_start);
635 if (ordered) {
636 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
637 unlock_page(page);
638 page_cache_release(page);
639 btrfs_start_ordered_extent(inode, ordered, 1);
640 btrfs_put_ordered_extent(ordered);
641 goto again;
642 }
643 set_page_extent_mapped(page);
644
Chris Masonf87f0572008-08-01 11:27:23 -0400645 /*
646 * this makes sure page_mkwrite is called on the
647 * page if it is dirtied again later
648 */
649 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500650 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
651 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
652 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400653
Josef Bacik2ac55d42010-02-03 19:33:23 +0000654 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500655 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400657 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500658
659loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400660 unlock_page(page);
661 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500662 mutex_unlock(&inode->i_mutex);
663
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500665 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666 }
667
Chris Mason1e701a32010-03-11 09:42:04 -0500668 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
669 filemap_flush(inode->i_mapping);
670
671 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
672 /* the filemap_flush will queue IO into the worker threads, but
673 * we have to make sure the IO is actually started and that
674 * ordered extents get created before we return
675 */
676 atomic_inc(&root->fs_info->async_submit_draining);
677 while (atomic_read(&root->fs_info->nr_async_submits) ||
678 atomic_read(&root->fs_info->async_delalloc_pages)) {
679 wait_event(root->fs_info->async_submit_wait,
680 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
681 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
682 }
683 atomic_dec(&root->fs_info->async_submit_draining);
684
685 mutex_lock(&inode->i_mutex);
686 BTRFS_I(inode)->force_compress = 0;
687 mutex_unlock(&inode->i_mutex);
688 }
689
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400690 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500691
692err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400693 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
694err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500695 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500696 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400697}
698
Yan, Zheng76dda932009-09-21 16:00:26 -0400699static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
700 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400701{
702 u64 new_size;
703 u64 old_size;
704 u64 devid = 1;
705 struct btrfs_ioctl_vol_args *vol_args;
706 struct btrfs_trans_handle *trans;
707 struct btrfs_device *device = NULL;
708 char *sizestr;
709 char *devstr = NULL;
710 int ret = 0;
711 int namelen;
712 int mod = 0;
713
Yan Zhengc146afa2008-11-12 14:34:12 -0500714 if (root->fs_info->sb->s_flags & MS_RDONLY)
715 return -EROFS;
716
Chris Masone441d542009-01-05 16:57:23 -0500717 if (!capable(CAP_SYS_ADMIN))
718 return -EPERM;
719
Li Zefandae7b662009-04-08 15:06:54 +0800720 vol_args = memdup_user(arg, sizeof(*vol_args));
721 if (IS_ERR(vol_args))
722 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400723
724 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400725 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726
Chris Mason7d9eb122008-07-08 14:19:17 -0400727 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400728 sizestr = vol_args->name;
729 devstr = strchr(sizestr, ':');
730 if (devstr) {
731 char *end;
732 sizestr = devstr + 1;
733 *devstr = '\0';
734 devstr = vol_args->name;
735 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700736 printk(KERN_INFO "resizing devid %llu\n",
737 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 }
Yan Zheng2b820322008-11-17 21:11:30 -0500739 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700741 printk(KERN_INFO "resizer unable to find device %llu\n",
742 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743 ret = -EINVAL;
744 goto out_unlock;
745 }
746 if (!strcmp(sizestr, "max"))
747 new_size = device->bdev->bd_inode->i_size;
748 else {
749 if (sizestr[0] == '-') {
750 mod = -1;
751 sizestr++;
752 } else if (sizestr[0] == '+') {
753 mod = 1;
754 sizestr++;
755 }
Akinobu Mita91748462010-02-28 10:59:11 +0000756 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400757 if (new_size == 0) {
758 ret = -EINVAL;
759 goto out_unlock;
760 }
761 }
762
763 old_size = device->total_bytes;
764
765 if (mod < 0) {
766 if (new_size > old_size) {
767 ret = -EINVAL;
768 goto out_unlock;
769 }
770 new_size = old_size - new_size;
771 } else if (mod > 0) {
772 new_size = old_size + new_size;
773 }
774
775 if (new_size < 256 * 1024 * 1024) {
776 ret = -EINVAL;
777 goto out_unlock;
778 }
779 if (new_size > device->bdev->bd_inode->i_size) {
780 ret = -EFBIG;
781 goto out_unlock;
782 }
783
784 do_div(new_size, root->sectorsize);
785 new_size *= root->sectorsize;
786
787 printk(KERN_INFO "new size for %s is %llu\n",
788 device->name, (unsigned long long)new_size);
789
790 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400791 trans = btrfs_start_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400792 ret = btrfs_grow_device(trans, device, new_size);
793 btrfs_commit_transaction(trans, root);
794 } else {
795 ret = btrfs_shrink_device(device, new_size);
796 }
797
798out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400799 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400800 kfree(vol_args);
801 return ret;
802}
803
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400804static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500805 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400806{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400807 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400808 struct btrfs_ioctl_vol_args *vol_args;
Chris Mason3de45862008-11-17 21:02:50 -0500809 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400810 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500811 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400812
Yan Zhengc146afa2008-11-12 14:34:12 -0500813 if (root->fs_info->sb->s_flags & MS_RDONLY)
814 return -EROFS;
815
Li Zefandae7b662009-04-08 15:06:54 +0800816 vol_args = memdup_user(arg, sizeof(*vol_args));
817 if (IS_ERR(vol_args))
818 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400819
Mark Fasheh5516e592008-07-24 12:20:14 -0400820 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400821 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400822 if (strchr(vol_args->name, '/')) {
823 ret = -EINVAL;
824 goto out;
825 }
826
Chris Mason3de45862008-11-17 21:02:50 -0500827 if (subvol) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400828 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
829 NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400830 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500831 struct inode *src_inode;
832 src_file = fget(vol_args->fd);
833 if (!src_file) {
834 ret = -EINVAL;
835 goto out;
836 }
837
838 src_inode = src_file->f_path.dentry->d_inode;
839 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500840 printk(KERN_INFO "btrfs: Snapshot src from "
841 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500842 ret = -EINVAL;
843 fput(src_file);
844 goto out;
845 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400846 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
847 BTRFS_I(src_inode)->root);
Chris Mason3de45862008-11-17 21:02:50 -0500848 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400849 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400850out:
851 kfree(vol_args);
852 return ret;
853}
854
Yan, Zheng76dda932009-09-21 16:00:26 -0400855/*
856 * helper to check if the subvolume references other subvolumes
857 */
858static noinline int may_destroy_subvol(struct btrfs_root *root)
859{
860 struct btrfs_path *path;
861 struct btrfs_key key;
862 int ret;
863
864 path = btrfs_alloc_path();
865 if (!path)
866 return -ENOMEM;
867
868 key.objectid = root->root_key.objectid;
869 key.type = BTRFS_ROOT_REF_KEY;
870 key.offset = (u64)-1;
871
872 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
873 &key, path, 0, 0);
874 if (ret < 0)
875 goto out;
876 BUG_ON(ret == 0);
877
878 ret = 0;
879 if (path->slots[0] > 0) {
880 path->slots[0]--;
881 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
882 if (key.objectid == root->root_key.objectid &&
883 key.type == BTRFS_ROOT_REF_KEY)
884 ret = -ENOTEMPTY;
885 }
886out:
887 btrfs_free_path(path);
888 return ret;
889}
890
Chris Masonac8e9812010-02-28 15:39:26 -0500891static noinline int key_in_sk(struct btrfs_key *key,
892 struct btrfs_ioctl_search_key *sk)
893{
Chris Masonabc6e132010-03-18 12:10:08 -0400894 struct btrfs_key test;
895 int ret;
896
897 test.objectid = sk->min_objectid;
898 test.type = sk->min_type;
899 test.offset = sk->min_offset;
900
901 ret = btrfs_comp_cpu_keys(key, &test);
902 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -0500903 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -0400904
905 test.objectid = sk->max_objectid;
906 test.type = sk->max_type;
907 test.offset = sk->max_offset;
908
909 ret = btrfs_comp_cpu_keys(key, &test);
910 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -0500911 return 0;
912 return 1;
913}
914
915static noinline int copy_to_sk(struct btrfs_root *root,
916 struct btrfs_path *path,
917 struct btrfs_key *key,
918 struct btrfs_ioctl_search_key *sk,
919 char *buf,
920 unsigned long *sk_offset,
921 int *num_found)
922{
923 u64 found_transid;
924 struct extent_buffer *leaf;
925 struct btrfs_ioctl_search_header sh;
926 unsigned long item_off;
927 unsigned long item_len;
928 int nritems;
929 int i;
930 int slot;
931 int found = 0;
932 int ret = 0;
933
934 leaf = path->nodes[0];
935 slot = path->slots[0];
936 nritems = btrfs_header_nritems(leaf);
937
938 if (btrfs_header_generation(leaf) > sk->max_transid) {
939 i = nritems;
940 goto advance_key;
941 }
942 found_transid = btrfs_header_generation(leaf);
943
944 for (i = slot; i < nritems; i++) {
945 item_off = btrfs_item_ptr_offset(leaf, i);
946 item_len = btrfs_item_size_nr(leaf, i);
947
948 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
949 item_len = 0;
950
951 if (sizeof(sh) + item_len + *sk_offset >
952 BTRFS_SEARCH_ARGS_BUFSIZE) {
953 ret = 1;
954 goto overflow;
955 }
956
957 btrfs_item_key_to_cpu(leaf, key, i);
958 if (!key_in_sk(key, sk))
959 continue;
960
961 sh.objectid = key->objectid;
962 sh.offset = key->offset;
963 sh.type = key->type;
964 sh.len = item_len;
965 sh.transid = found_transid;
966
967 /* copy search result header */
968 memcpy(buf + *sk_offset, &sh, sizeof(sh));
969 *sk_offset += sizeof(sh);
970
971 if (item_len) {
972 char *p = buf + *sk_offset;
973 /* copy the item */
974 read_extent_buffer(leaf, p,
975 item_off, item_len);
976 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -0500977 }
Chris Mason90fdde12010-03-18 12:14:54 -0400978 found++;
Chris Masonac8e9812010-02-28 15:39:26 -0500979
980 if (*num_found >= sk->nr_items)
981 break;
982 }
983advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -0500984 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -0400985 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
986 key->offset++;
987 else if (key->type < (u8)-1 && key->type < sk->max_type) {
988 key->offset = 0;
989 key->type++;
990 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
991 key->offset = 0;
992 key->type = 0;
993 key->objectid++;
994 } else
995 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -0500996overflow:
997 *num_found += found;
998 return ret;
999}
1000
1001static noinline int search_ioctl(struct inode *inode,
1002 struct btrfs_ioctl_search_args *args)
1003{
1004 struct btrfs_root *root;
1005 struct btrfs_key key;
1006 struct btrfs_key max_key;
1007 struct btrfs_path *path;
1008 struct btrfs_ioctl_search_key *sk = &args->key;
1009 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1010 int ret;
1011 int num_found = 0;
1012 unsigned long sk_offset = 0;
1013
1014 path = btrfs_alloc_path();
1015 if (!path)
1016 return -ENOMEM;
1017
1018 if (sk->tree_id == 0) {
1019 /* search the root of the inode that was passed */
1020 root = BTRFS_I(inode)->root;
1021 } else {
1022 key.objectid = sk->tree_id;
1023 key.type = BTRFS_ROOT_ITEM_KEY;
1024 key.offset = (u64)-1;
1025 root = btrfs_read_fs_root_no_name(info, &key);
1026 if (IS_ERR(root)) {
1027 printk(KERN_ERR "could not find root %llu\n",
1028 sk->tree_id);
1029 btrfs_free_path(path);
1030 return -ENOENT;
1031 }
1032 }
1033
1034 key.objectid = sk->min_objectid;
1035 key.type = sk->min_type;
1036 key.offset = sk->min_offset;
1037
1038 max_key.objectid = sk->max_objectid;
1039 max_key.type = sk->max_type;
1040 max_key.offset = sk->max_offset;
1041
1042 path->keep_locks = 1;
1043
1044 while(1) {
1045 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1046 sk->min_transid);
1047 if (ret != 0) {
1048 if (ret > 0)
1049 ret = 0;
1050 goto err;
1051 }
1052 ret = copy_to_sk(root, path, &key, sk, args->buf,
1053 &sk_offset, &num_found);
1054 btrfs_release_path(root, path);
1055 if (ret || num_found >= sk->nr_items)
1056 break;
1057
1058 }
1059 ret = 0;
1060err:
1061 sk->nr_items = num_found;
1062 btrfs_free_path(path);
1063 return ret;
1064}
1065
1066static noinline int btrfs_ioctl_tree_search(struct file *file,
1067 void __user *argp)
1068{
1069 struct btrfs_ioctl_search_args *args;
1070 struct inode *inode;
1071 int ret;
1072
1073 if (!capable(CAP_SYS_ADMIN))
1074 return -EPERM;
1075
Julia Lawall2354d082010-10-29 15:14:18 -04001076 args = memdup_user(argp, sizeof(*args));
1077 if (IS_ERR(args))
1078 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001079
Chris Masonac8e9812010-02-28 15:39:26 -05001080 inode = fdentry(file)->d_inode;
1081 ret = search_ioctl(inode, args);
1082 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1083 ret = -EFAULT;
1084 kfree(args);
1085 return ret;
1086}
1087
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001088/*
Chris Masonac8e9812010-02-28 15:39:26 -05001089 * Search INODE_REFs to identify path name of 'dirid' directory
1090 * in a 'tree_id' tree. and sets path name to 'name'.
1091 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001092static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1093 u64 tree_id, u64 dirid, char *name)
1094{
1095 struct btrfs_root *root;
1096 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001097 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001098 int ret = -1;
1099 int slot;
1100 int len;
1101 int total_len = 0;
1102 struct btrfs_inode_ref *iref;
1103 struct extent_buffer *l;
1104 struct btrfs_path *path;
1105
1106 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1107 name[0]='\0';
1108 return 0;
1109 }
1110
1111 path = btrfs_alloc_path();
1112 if (!path)
1113 return -ENOMEM;
1114
Chris Masonac8e9812010-02-28 15:39:26 -05001115 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001116
1117 key.objectid = tree_id;
1118 key.type = BTRFS_ROOT_ITEM_KEY;
1119 key.offset = (u64)-1;
1120 root = btrfs_read_fs_root_no_name(info, &key);
1121 if (IS_ERR(root)) {
1122 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001123 ret = -ENOENT;
1124 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001125 }
1126
1127 key.objectid = dirid;
1128 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001129 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001130
1131 while(1) {
1132 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1133 if (ret < 0)
1134 goto out;
1135
1136 l = path->nodes[0];
1137 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001138 if (ret > 0 && slot > 0)
1139 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001140 btrfs_item_key_to_cpu(l, &key, slot);
1141
1142 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001143 key.type != BTRFS_INODE_REF_KEY)) {
1144 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001145 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001146 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001147
1148 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1149 len = btrfs_inode_ref_name_len(l, iref);
1150 ptr -= len + 1;
1151 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001152 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001153 goto out;
1154
1155 *(ptr + len) = '/';
1156 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1157
1158 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1159 break;
1160
1161 btrfs_release_path(root, path);
1162 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001163 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001164 dirid = key.objectid;
1165
1166 }
Chris Masonac8e9812010-02-28 15:39:26 -05001167 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001168 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001169 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001170 name[total_len]='\0';
1171 ret = 0;
1172out:
1173 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001174 return ret;
1175}
1176
1177static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1178 void __user *argp)
1179{
1180 struct btrfs_ioctl_ino_lookup_args *args;
1181 struct inode *inode;
1182 int ret;
1183
1184 if (!capable(CAP_SYS_ADMIN))
1185 return -EPERM;
1186
Julia Lawall2354d082010-10-29 15:14:18 -04001187 args = memdup_user(argp, sizeof(*args));
1188 if (IS_ERR(args))
1189 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001190
Chris Masonac8e9812010-02-28 15:39:26 -05001191 inode = fdentry(file)->d_inode;
1192
Chris Mason1b53ac42010-03-18 12:17:05 -04001193 if (args->treeid == 0)
1194 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1195
Chris Masonac8e9812010-02-28 15:39:26 -05001196 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1197 args->treeid, args->objectid,
1198 args->name);
1199
1200 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1201 ret = -EFAULT;
1202
1203 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001204 return ret;
1205}
1206
Yan, Zheng76dda932009-09-21 16:00:26 -04001207static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1208 void __user *arg)
1209{
1210 struct dentry *parent = fdentry(file);
1211 struct dentry *dentry;
1212 struct inode *dir = parent->d_inode;
1213 struct inode *inode;
1214 struct btrfs_root *root = BTRFS_I(dir)->root;
1215 struct btrfs_root *dest = NULL;
1216 struct btrfs_ioctl_vol_args *vol_args;
1217 struct btrfs_trans_handle *trans;
1218 int namelen;
1219 int ret;
1220 int err = 0;
1221
1222 if (!capable(CAP_SYS_ADMIN))
1223 return -EPERM;
1224
1225 vol_args = memdup_user(arg, sizeof(*vol_args));
1226 if (IS_ERR(vol_args))
1227 return PTR_ERR(vol_args);
1228
1229 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1230 namelen = strlen(vol_args->name);
1231 if (strchr(vol_args->name, '/') ||
1232 strncmp(vol_args->name, "..", namelen) == 0) {
1233 err = -EINVAL;
1234 goto out;
1235 }
1236
1237 err = mnt_want_write(file->f_path.mnt);
1238 if (err)
1239 goto out;
1240
1241 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1242 dentry = lookup_one_len(vol_args->name, parent, namelen);
1243 if (IS_ERR(dentry)) {
1244 err = PTR_ERR(dentry);
1245 goto out_unlock_dir;
1246 }
1247
1248 if (!dentry->d_inode) {
1249 err = -ENOENT;
1250 goto out_dput;
1251 }
1252
1253 inode = dentry->d_inode;
1254 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1255 err = -EINVAL;
1256 goto out_dput;
1257 }
1258
1259 dest = BTRFS_I(inode)->root;
1260
1261 mutex_lock(&inode->i_mutex);
1262 err = d_invalidate(dentry);
1263 if (err)
1264 goto out_unlock;
1265
1266 down_write(&root->fs_info->subvol_sem);
1267
1268 err = may_destroy_subvol(dest);
1269 if (err)
1270 goto out_up_write;
1271
Yan, Zhenga22285a2010-05-16 10:48:46 -04001272 trans = btrfs_start_transaction(root, 0);
1273 if (IS_ERR(trans)) {
1274 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001275 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001276 }
1277 trans->block_rsv = &root->fs_info->global_block_rsv;
1278
Yan, Zheng76dda932009-09-21 16:00:26 -04001279 ret = btrfs_unlink_subvol(trans, root, dir,
1280 dest->root_key.objectid,
1281 dentry->d_name.name,
1282 dentry->d_name.len);
1283 BUG_ON(ret);
1284
1285 btrfs_record_root_in_trans(trans, dest);
1286
1287 memset(&dest->root_item.drop_progress, 0,
1288 sizeof(dest->root_item.drop_progress));
1289 dest->root_item.drop_level = 0;
1290 btrfs_set_root_refs(&dest->root_item, 0);
1291
Yan, Zhengd68fc572010-05-16 10:49:58 -04001292 if (!xchg(&dest->orphan_item_inserted, 1)) {
1293 ret = btrfs_insert_orphan_item(trans,
1294 root->fs_info->tree_root,
1295 dest->root_key.objectid);
1296 BUG_ON(ret);
1297 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001298
1299 ret = btrfs_commit_transaction(trans, root);
1300 BUG_ON(ret);
1301 inode->i_flags |= S_DEAD;
1302out_up_write:
1303 up_write(&root->fs_info->subvol_sem);
1304out_unlock:
1305 mutex_unlock(&inode->i_mutex);
1306 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001307 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001308 btrfs_invalidate_inodes(dest);
1309 d_delete(dentry);
1310 }
1311out_dput:
1312 dput(dentry);
1313out_unlock_dir:
1314 mutex_unlock(&dir->i_mutex);
1315 mnt_drop_write(file->f_path.mnt);
1316out:
1317 kfree(vol_args);
1318 return err;
1319}
1320
Chris Mason1e701a32010-03-11 09:42:04 -05001321static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001322{
1323 struct inode *inode = fdentry(file)->d_inode;
1324 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001325 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001326 int ret;
1327
1328 ret = mnt_want_write(file->f_path.mnt);
1329 if (ret)
1330 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001331
1332 switch (inode->i_mode & S_IFMT) {
1333 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001334 if (!capable(CAP_SYS_ADMIN)) {
1335 ret = -EPERM;
1336 goto out;
1337 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001338 ret = btrfs_defrag_root(root, 0);
1339 if (ret)
1340 goto out;
1341 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001342 break;
1343 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001344 if (!(file->f_mode & FMODE_WRITE)) {
1345 ret = -EINVAL;
1346 goto out;
1347 }
Chris Mason1e701a32010-03-11 09:42:04 -05001348
1349 range = kzalloc(sizeof(*range), GFP_KERNEL);
1350 if (!range) {
1351 ret = -ENOMEM;
1352 goto out;
1353 }
1354
1355 if (argp) {
1356 if (copy_from_user(range, argp,
1357 sizeof(*range))) {
1358 ret = -EFAULT;
1359 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001360 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001361 }
1362 /* compression requires us to start the IO */
1363 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1364 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1365 range->extent_thresh = (u32)-1;
1366 }
1367 } else {
1368 /* the rest are all set to zero by kzalloc */
1369 range->len = (u64)-1;
1370 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001371 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001372 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001373 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001374 default:
1375 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001376 }
Chris Masone441d542009-01-05 16:57:23 -05001377out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001378 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001379 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001380}
1381
Christoph Hellwigb2950862008-12-02 09:54:17 -05001382static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383{
1384 struct btrfs_ioctl_vol_args *vol_args;
1385 int ret;
1386
Chris Masone441d542009-01-05 16:57:23 -05001387 if (!capable(CAP_SYS_ADMIN))
1388 return -EPERM;
1389
Li Zefandae7b662009-04-08 15:06:54 +08001390 vol_args = memdup_user(arg, sizeof(*vol_args));
1391 if (IS_ERR(vol_args))
1392 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001393
Mark Fasheh5516e592008-07-24 12:20:14 -04001394 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001395 ret = btrfs_init_new_device(root, vol_args->name);
1396
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001397 kfree(vol_args);
1398 return ret;
1399}
1400
Christoph Hellwigb2950862008-12-02 09:54:17 -05001401static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001402{
1403 struct btrfs_ioctl_vol_args *vol_args;
1404 int ret;
1405
Chris Masone441d542009-01-05 16:57:23 -05001406 if (!capable(CAP_SYS_ADMIN))
1407 return -EPERM;
1408
Yan Zhengc146afa2008-11-12 14:34:12 -05001409 if (root->fs_info->sb->s_flags & MS_RDONLY)
1410 return -EROFS;
1411
Li Zefandae7b662009-04-08 15:06:54 +08001412 vol_args = memdup_user(arg, sizeof(*vol_args));
1413 if (IS_ERR(vol_args))
1414 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001415
Mark Fasheh5516e592008-07-24 12:20:14 -04001416 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001417 ret = btrfs_rm_device(root, vol_args->name);
1418
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001419 kfree(vol_args);
1420 return ret;
1421}
1422
Yan, Zheng76dda932009-09-21 16:00:26 -04001423static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1424 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001425{
1426 struct inode *inode = fdentry(file)->d_inode;
1427 struct btrfs_root *root = BTRFS_I(inode)->root;
1428 struct file *src_file;
1429 struct inode *src;
1430 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001432 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001433 char *buf;
1434 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435 u32 nritems;
1436 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001437 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001438 u64 len = olen;
1439 u64 bs = root->fs_info->sb->s_blocksize;
1440 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001441
Sage Weilc5c9cd42008-11-12 14:32:25 -05001442 /*
1443 * TODO:
1444 * - split compressed inline extents. annoying: we need to
1445 * decompress into destination's address_space (the file offset
1446 * may change, so source mapping won't do), then recompress (or
1447 * otherwise reinsert) a subrange.
1448 * - allow ranges within the same file to be cloned (provided
1449 * they don't overlap)?
1450 */
1451
Chris Masone441d542009-01-05 16:57:23 -05001452 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001453 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001454 return -EINVAL;
1455
Yan Zhengc146afa2008-11-12 14:34:12 -05001456 ret = mnt_want_write(file->f_path.mnt);
1457 if (ret)
1458 return ret;
1459
Sage Weilc5c9cd42008-11-12 14:32:25 -05001460 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001461 if (!src_file) {
1462 ret = -EBADF;
1463 goto out_drop_write;
1464 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001465
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001466 src = src_file->f_dentry->d_inode;
1467
Sage Weilc5c9cd42008-11-12 14:32:25 -05001468 ret = -EINVAL;
1469 if (src == inode)
1470 goto out_fput;
1471
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001472 /* the src must be open for reading */
1473 if (!(src_file->f_mode & FMODE_READ))
1474 goto out_fput;
1475
Yan Zhengae01a0a2008-08-04 23:23:47 -04001476 ret = -EISDIR;
1477 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001478 goto out_fput;
1479
Yan Zhengae01a0a2008-08-04 23:23:47 -04001480 ret = -EXDEV;
1481 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1482 goto out_fput;
1483
1484 ret = -ENOMEM;
1485 buf = vmalloc(btrfs_level_size(root, 0));
1486 if (!buf)
1487 goto out_fput;
1488
1489 path = btrfs_alloc_path();
1490 if (!path) {
1491 vfree(buf);
1492 goto out_fput;
1493 }
1494 path->reada = 2;
1495
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001496 if (inode < src) {
1497 mutex_lock(&inode->i_mutex);
1498 mutex_lock(&src->i_mutex);
1499 } else {
1500 mutex_lock(&src->i_mutex);
1501 mutex_lock(&inode->i_mutex);
1502 }
1503
Sage Weilc5c9cd42008-11-12 14:32:25 -05001504 /* determine range to clone */
1505 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001506 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001508 if (len == 0)
1509 olen = len = src->i_size - off;
1510 /* if we extend to eof, continue to block boundary */
1511 if (off + len == src->i_size)
1512 len = ((src->i_size + bs-1) & ~(bs-1))
1513 - off;
1514
1515 /* verify the end result is block aligned */
1516 if ((off & (bs-1)) ||
1517 ((off + len) & (bs-1)))
1518 goto out_unlock;
1519
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001520 /* do any pending delalloc/csum calc on src, one way or
1521 another, and lock file content */
1522 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001523 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001524 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1525 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001526 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001527 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001528 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001529 if (ordered)
1530 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001531 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001532 }
1533
Sage Weilc5c9cd42008-11-12 14:32:25 -05001534 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001536 key.type = BTRFS_EXTENT_DATA_KEY;
1537 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001538
1539 while (1) {
1540 /*
1541 * note the key will change type as we walk through the
1542 * tree.
1543 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001544 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001545 if (ret < 0)
1546 goto out;
1547
Yan Zhengae01a0a2008-08-04 23:23:47 -04001548 nritems = btrfs_header_nritems(path->nodes[0]);
1549 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001550 ret = btrfs_next_leaf(root, path);
1551 if (ret < 0)
1552 goto out;
1553 if (ret > 0)
1554 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001555 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001556 }
1557 leaf = path->nodes[0];
1558 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001559
Yan Zhengae01a0a2008-08-04 23:23:47 -04001560 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001561 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001562 key.objectid != src->i_ino)
1563 break;
1564
Sage Weilc5c9cd42008-11-12 14:32:25 -05001565 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1566 struct btrfs_file_extent_item *extent;
1567 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001568 u32 size;
1569 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001570 u64 disko = 0, diskl = 0;
1571 u64 datao = 0, datal = 0;
1572 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001573 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001574
1575 size = btrfs_item_size_nr(leaf, slot);
1576 read_extent_buffer(leaf, buf,
1577 btrfs_item_ptr_offset(leaf, slot),
1578 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001579
1580 extent = btrfs_item_ptr(leaf, slot,
1581 struct btrfs_file_extent_item);
1582 comp = btrfs_file_extent_compression(leaf, extent);
1583 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001584 if (type == BTRFS_FILE_EXTENT_REG ||
1585 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001586 disko = btrfs_file_extent_disk_bytenr(leaf,
1587 extent);
1588 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1589 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001590 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001591 datal = btrfs_file_extent_num_bytes(leaf,
1592 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001593 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1594 /* take upper bound, may be compressed */
1595 datal = btrfs_file_extent_ram_bytes(leaf,
1596 extent);
1597 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001598 btrfs_release_path(root, path);
1599
Sage Weilc5c9cd42008-11-12 14:32:25 -05001600 if (key.offset + datal < off ||
1601 key.offset >= off+len)
1602 goto next;
1603
Zheng Yan31840ae2008-09-23 13:14:14 -04001604 memcpy(&new_key, &key, sizeof(new_key));
1605 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001606 new_key.offset = key.offset + destoff - off;
1607
Yan, Zhenga22285a2010-05-16 10:48:46 -04001608 trans = btrfs_start_transaction(root, 1);
1609 if (IS_ERR(trans)) {
1610 ret = PTR_ERR(trans);
1611 goto out;
1612 }
1613
Chris Masonc8a894d2009-06-27 21:07:03 -04001614 if (type == BTRFS_FILE_EXTENT_REG ||
1615 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001616 if (off > key.offset) {
1617 datao += off - key.offset;
1618 datal -= off - key.offset;
1619 }
1620
1621 if (key.offset + datal > off + len)
1622 datal = off + len - key.offset;
1623
1624 ret = btrfs_drop_extents(trans, inode,
1625 new_key.offset,
1626 new_key.offset + datal,
1627 &hint_byte, 1);
1628 BUG_ON(ret);
1629
Sage Weilc5c9cd42008-11-12 14:32:25 -05001630 ret = btrfs_insert_empty_item(trans, root, path,
1631 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001632 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001633
1634 leaf = path->nodes[0];
1635 slot = path->slots[0];
1636 write_extent_buffer(leaf, buf,
1637 btrfs_item_ptr_offset(leaf, slot),
1638 size);
1639
1640 extent = btrfs_item_ptr(leaf, slot,
1641 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001642
Sage Weilc5c9cd42008-11-12 14:32:25 -05001643 /* disko == 0 means it's a hole */
1644 if (!disko)
1645 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001646
1647 btrfs_set_file_extent_offset(leaf, extent,
1648 datao);
1649 btrfs_set_file_extent_num_bytes(leaf, extent,
1650 datal);
1651 if (disko) {
1652 inode_add_bytes(inode, datal);
1653 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001654 disko, diskl, 0,
1655 root->root_key.objectid,
1656 inode->i_ino,
1657 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001658 BUG_ON(ret);
1659 }
1660 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1661 u64 skip = 0;
1662 u64 trim = 0;
1663 if (off > key.offset) {
1664 skip = off - key.offset;
1665 new_key.offset += skip;
1666 }
Chris Masond3977122009-01-05 21:25:51 -05001667
Sage Weilc5c9cd42008-11-12 14:32:25 -05001668 if (key.offset + datal > off+len)
1669 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001670
Sage Weilc5c9cd42008-11-12 14:32:25 -05001671 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001672 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001673 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001674 goto out;
1675 }
1676 size -= skip + trim;
1677 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001678
1679 ret = btrfs_drop_extents(trans, inode,
1680 new_key.offset,
1681 new_key.offset + datal,
1682 &hint_byte, 1);
1683 BUG_ON(ret);
1684
Sage Weilc5c9cd42008-11-12 14:32:25 -05001685 ret = btrfs_insert_empty_item(trans, root, path,
1686 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001687 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001688
1689 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001690 u32 start =
1691 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001692 memmove(buf+start, buf+start+skip,
1693 datal);
1694 }
1695
1696 leaf = path->nodes[0];
1697 slot = path->slots[0];
1698 write_extent_buffer(leaf, buf,
1699 btrfs_item_ptr_offset(leaf, slot),
1700 size);
1701 inode_add_bytes(inode, datal);
1702 }
1703
1704 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001705 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001706
Yan, Zhenga22285a2010-05-16 10:48:46 -04001707 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00001708
1709 /*
1710 * we round up to the block size at eof when
1711 * determining which extents to clone above,
1712 * but shouldn't round up the file size
1713 */
1714 endoff = new_key.offset + datal;
1715 if (endoff > off+olen)
1716 endoff = off+olen;
1717 if (endoff > inode->i_size)
1718 btrfs_i_size_write(inode, endoff);
1719
Yan, Zhenga22285a2010-05-16 10:48:46 -04001720 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1721 ret = btrfs_update_inode(trans, root, inode);
1722 BUG_ON(ret);
1723 btrfs_end_transaction(trans, root);
1724 }
Chris Masond3977122009-01-05 21:25:51 -05001725next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001726 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001727 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001728 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001729 ret = 0;
1730out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001731 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001732 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001733out_unlock:
1734 mutex_unlock(&src->i_mutex);
1735 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001736 vfree(buf);
1737 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001738out_fput:
1739 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001740out_drop_write:
1741 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001742 return ret;
1743}
1744
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001745static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001746{
1747 struct btrfs_ioctl_clone_range_args args;
1748
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001749 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001750 return -EFAULT;
1751 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1752 args.src_length, args.dest_offset);
1753}
1754
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001755/*
1756 * there are many ways the trans_start and trans_end ioctls can lead
1757 * to deadlocks. They should only be used by applications that
1758 * basically own the machine, and have a very in depth understanding
1759 * of all the possible deadlocks and enospc problems.
1760 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001761static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001762{
1763 struct inode *inode = fdentry(file)->d_inode;
1764 struct btrfs_root *root = BTRFS_I(inode)->root;
1765 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001766 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001767
Sage Weil1ab86ae2009-09-29 18:38:44 -04001768 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001769 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001770 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001771
1772 ret = -EINPROGRESS;
1773 if (file->private_data)
1774 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001775
Yan Zhengc146afa2008-11-12 14:34:12 -05001776 ret = mnt_want_write(file->f_path.mnt);
1777 if (ret)
1778 goto out;
1779
Sage Weil9ca9ee02008-08-04 10:41:27 -04001780 mutex_lock(&root->fs_info->trans_mutex);
1781 root->fs_info->open_ioctl_trans++;
1782 mutex_unlock(&root->fs_info->trans_mutex);
1783
Sage Weil1ab86ae2009-09-29 18:38:44 -04001784 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001785 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001786 if (!trans)
1787 goto out_drop;
1788
1789 file->private_data = trans;
1790 return 0;
1791
1792out_drop:
1793 mutex_lock(&root->fs_info->trans_mutex);
1794 root->fs_info->open_ioctl_trans--;
1795 mutex_unlock(&root->fs_info->trans_mutex);
1796 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001797out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001798 return ret;
1799}
1800
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001801static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1802{
1803 struct inode *inode = fdentry(file)->d_inode;
1804 struct btrfs_root *root = BTRFS_I(inode)->root;
1805 struct btrfs_root *new_root;
1806 struct btrfs_dir_item *di;
1807 struct btrfs_trans_handle *trans;
1808 struct btrfs_path *path;
1809 struct btrfs_key location;
1810 struct btrfs_disk_key disk_key;
1811 struct btrfs_super_block *disk_super;
1812 u64 features;
1813 u64 objectid = 0;
1814 u64 dir_id;
1815
1816 if (!capable(CAP_SYS_ADMIN))
1817 return -EPERM;
1818
1819 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1820 return -EFAULT;
1821
1822 if (!objectid)
1823 objectid = root->root_key.objectid;
1824
1825 location.objectid = objectid;
1826 location.type = BTRFS_ROOT_ITEM_KEY;
1827 location.offset = (u64)-1;
1828
1829 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1830 if (IS_ERR(new_root))
1831 return PTR_ERR(new_root);
1832
1833 if (btrfs_root_refs(&new_root->root_item) == 0)
1834 return -ENOENT;
1835
1836 path = btrfs_alloc_path();
1837 if (!path)
1838 return -ENOMEM;
1839 path->leave_spinning = 1;
1840
1841 trans = btrfs_start_transaction(root, 1);
1842 if (!trans) {
1843 btrfs_free_path(path);
1844 return -ENOMEM;
1845 }
1846
1847 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1848 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1849 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00001850 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001851 btrfs_free_path(path);
1852 btrfs_end_transaction(trans, root);
1853 printk(KERN_ERR "Umm, you don't have the default dir item, "
1854 "this isn't going to work\n");
1855 return -ENOENT;
1856 }
1857
1858 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1859 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1860 btrfs_mark_buffer_dirty(path->nodes[0]);
1861 btrfs_free_path(path);
1862
1863 disk_super = &root->fs_info->super_copy;
1864 features = btrfs_super_incompat_flags(disk_super);
1865 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1866 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1867 btrfs_set_super_incompat_flags(disk_super, features);
1868 }
1869 btrfs_end_transaction(trans, root);
1870
1871 return 0;
1872}
1873
Josef Bacikbf5fc092010-09-29 11:22:36 -04001874static void get_block_group_info(struct list_head *groups_list,
1875 struct btrfs_ioctl_space_info *space)
1876{
1877 struct btrfs_block_group_cache *block_group;
1878
1879 space->total_bytes = 0;
1880 space->used_bytes = 0;
1881 space->flags = 0;
1882 list_for_each_entry(block_group, groups_list, list) {
1883 space->flags = block_group->flags;
1884 space->total_bytes += block_group->key.offset;
1885 space->used_bytes +=
1886 btrfs_block_group_used(&block_group->item);
1887 }
1888}
1889
Josef Bacik1406e432010-01-13 18:19:06 +00001890long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1891{
1892 struct btrfs_ioctl_space_args space_args;
1893 struct btrfs_ioctl_space_info space;
1894 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04001895 struct btrfs_ioctl_space_info *dest_orig;
1896 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00001897 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04001898 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
1899 BTRFS_BLOCK_GROUP_SYSTEM,
1900 BTRFS_BLOCK_GROUP_METADATA,
1901 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
1902 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04001903 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00001904 int ret = 0;
Chris Mason7fde62b2010-03-16 15:40:10 -04001905 int slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04001906 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00001907
1908 if (copy_from_user(&space_args,
1909 (struct btrfs_ioctl_space_args __user *)arg,
1910 sizeof(space_args)))
1911 return -EFAULT;
1912
Josef Bacikbf5fc092010-09-29 11:22:36 -04001913 for (i = 0; i < num_types; i++) {
1914 struct btrfs_space_info *tmp;
1915
1916 info = NULL;
1917 rcu_read_lock();
1918 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
1919 list) {
1920 if (tmp->flags == types[i]) {
1921 info = tmp;
1922 break;
1923 }
1924 }
1925 rcu_read_unlock();
1926
1927 if (!info)
1928 continue;
1929
1930 down_read(&info->groups_sem);
1931 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
1932 if (!list_empty(&info->block_groups[c]))
1933 slot_count++;
1934 }
1935 up_read(&info->groups_sem);
1936 }
Josef Bacik1406e432010-01-13 18:19:06 +00001937
Chris Mason7fde62b2010-03-16 15:40:10 -04001938 /* space_slots == 0 means they are asking for a count */
1939 if (space_args.space_slots == 0) {
1940 space_args.total_spaces = slot_count;
1941 goto out;
1942 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04001943
1944 slot_count = min_t(int, space_args.space_slots, slot_count);
1945
Chris Mason7fde62b2010-03-16 15:40:10 -04001946 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04001947
Chris Mason7fde62b2010-03-16 15:40:10 -04001948 /* we generally have at most 6 or so space infos, one for each raid
1949 * level. So, a whole page should be more than enough for everyone
1950 */
1951 if (alloc_size > PAGE_CACHE_SIZE)
1952 return -ENOMEM;
1953
1954 space_args.total_spaces = 0;
1955 dest = kmalloc(alloc_size, GFP_NOFS);
1956 if (!dest)
1957 return -ENOMEM;
1958 dest_orig = dest;
1959
1960 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04001961 for (i = 0; i < num_types; i++) {
1962 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04001963
Josef Bacikbf5fc092010-09-29 11:22:36 -04001964 info = NULL;
1965 rcu_read_lock();
1966 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
1967 list) {
1968 if (tmp->flags == types[i]) {
1969 info = tmp;
1970 break;
1971 }
1972 }
1973 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04001974
Josef Bacikbf5fc092010-09-29 11:22:36 -04001975 if (!info)
1976 continue;
1977 down_read(&info->groups_sem);
1978 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
1979 if (!list_empty(&info->block_groups[c])) {
1980 get_block_group_info(&info->block_groups[c],
1981 &space);
1982 memcpy(dest, &space, sizeof(space));
1983 dest++;
1984 space_args.total_spaces++;
1985 }
1986 }
1987 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00001988 }
Josef Bacik1406e432010-01-13 18:19:06 +00001989
Chris Mason7fde62b2010-03-16 15:40:10 -04001990 user_dest = (struct btrfs_ioctl_space_info *)
1991 (arg + sizeof(struct btrfs_ioctl_space_args));
1992
1993 if (copy_to_user(user_dest, dest_orig, alloc_size))
1994 ret = -EFAULT;
1995
1996 kfree(dest_orig);
1997out:
1998 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00001999 ret = -EFAULT;
2000
2001 return ret;
2002}
2003
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002004/*
2005 * there are many ways the trans_start and trans_end ioctls can lead
2006 * to deadlocks. They should only be used by applications that
2007 * basically own the machine, and have a very in depth understanding
2008 * of all the possible deadlocks and enospc problems.
2009 */
2010long btrfs_ioctl_trans_end(struct file *file)
2011{
2012 struct inode *inode = fdentry(file)->d_inode;
2013 struct btrfs_root *root = BTRFS_I(inode)->root;
2014 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002015
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002016 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002017 if (!trans)
2018 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002019 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002020
Sage Weil1ab86ae2009-09-29 18:38:44 -04002021 btrfs_end_transaction(trans, root);
2022
Sage Weil9ca9ee02008-08-04 10:41:27 -04002023 mutex_lock(&root->fs_info->trans_mutex);
2024 root->fs_info->open_ioctl_trans--;
2025 mutex_unlock(&root->fs_info->trans_mutex);
2026
Sage Weilcfc8ea82008-12-11 16:30:06 -05002027 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002028 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002029}
2030
2031long btrfs_ioctl(struct file *file, unsigned int
2032 cmd, unsigned long arg)
2033{
2034 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002035 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002036
2037 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002038 case FS_IOC_GETFLAGS:
2039 return btrfs_ioctl_getflags(file, argp);
2040 case FS_IOC_SETFLAGS:
2041 return btrfs_ioctl_setflags(file, argp);
2042 case FS_IOC_GETVERSION:
2043 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002044 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002045 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002046 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002047 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002048 case BTRFS_IOC_SNAP_DESTROY:
2049 return btrfs_ioctl_snap_destroy(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002050 case BTRFS_IOC_DEFAULT_SUBVOL:
2051 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002052 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002053 return btrfs_ioctl_defrag(file, NULL);
2054 case BTRFS_IOC_DEFRAG_RANGE:
2055 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002056 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002057 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002058 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002059 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002060 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002061 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002062 case BTRFS_IOC_BALANCE:
2063 return btrfs_balance(root->fs_info->dev_root);
2064 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002065 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2066 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002067 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002068 case BTRFS_IOC_TRANS_START:
2069 return btrfs_ioctl_trans_start(file);
2070 case BTRFS_IOC_TRANS_END:
2071 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002072 case BTRFS_IOC_TREE_SEARCH:
2073 return btrfs_ioctl_tree_search(file, argp);
2074 case BTRFS_IOC_INO_LOOKUP:
2075 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002076 case BTRFS_IOC_SPACE_INFO:
2077 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002078 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002079 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2080 return 0;
2081 }
2082
2083 return -ENOTTY;
2084}