blob: 97a97839a867f1c0773f8a684a1289ae185d0eb6 [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
Josef Bacik9ed74f22009-09-11 16:12:44 -0400242 /*
243 * 1 - inode item
244 * 2 - refs
245 * 1 - root item
246 * 2 - dir items
247 */
248 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400249 if (ret)
Yan, Zheng76dda932009-09-21 16:00:26 -0400250 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400251
252 trans = btrfs_start_transaction(root, 1);
253 BUG_ON(!trans);
254
255 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
256 0, &objectid);
257 if (ret)
258 goto fail;
259
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
261 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400262 if (IS_ERR(leaf)) {
263 ret = PTR_ERR(leaf);
264 goto fail;
265 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400266
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400267 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400268 btrfs_set_header_bytenr(leaf, leaf->start);
269 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400270 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400271 btrfs_set_header_owner(leaf, objectid);
272
273 write_extent_buffer(leaf, root->fs_info->fsid,
274 (unsigned long)btrfs_header_fsid(leaf),
275 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400276 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
277 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
278 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400279 btrfs_mark_buffer_dirty(leaf);
280
281 inode_item = &root_item.inode;
282 memset(inode_item, 0, sizeof(*inode_item));
283 inode_item->generation = cpu_to_le64(1);
284 inode_item->size = cpu_to_le64(3);
285 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400286 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400287 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
288
289 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400290 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400291 btrfs_set_root_level(&root_item, 0);
292 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000293 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400294 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400295
296 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
297 root_item.drop_level = 0;
298
Chris Mason925baed2008-06-25 16:01:30 -0400299 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400300 free_extent_buffer(leaf);
301 leaf = NULL;
302
303 btrfs_set_root_dirid(&root_item, new_dirid);
304
305 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400306 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400307 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
308 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
309 &root_item);
310 if (ret)
311 goto fail;
312
Yan, Zheng76dda932009-09-21 16:00:26 -0400313 key.offset = (u64)-1;
314 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
315 BUG_ON(IS_ERR(new_root));
316
317 btrfs_record_root_in_trans(trans, new_root);
318
319 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
320 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400321 /*
322 * insert the directory item
323 */
Chris Mason3de45862008-11-17 21:02:50 -0500324 ret = btrfs_set_inode_index(dir, &index);
325 BUG_ON(ret);
326
327 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400328 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500329 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400330 if (ret)
331 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500332
Yan Zheng52c26172009-01-05 15:43:43 -0500333 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
334 ret = btrfs_update_inode(trans, root, dir);
335 BUG_ON(ret);
336
Chris Mason0660b5a2008-11-17 20:37:39 -0500337 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400338 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500339 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400340
Chris Mason0660b5a2008-11-17 20:37:39 -0500341 BUG_ON(ret);
342
Yan, Zheng76dda932009-09-21 16:00:26 -0400343 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400344fail:
Yan, Zheng76dda932009-09-21 16:00:26 -0400345 err = btrfs_commit_transaction(trans, root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400346 if (err && !ret)
347 ret = err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400348
349 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400350 return ret;
351}
352
Chris Mason3de45862008-11-17 21:02:50 -0500353static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
354 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400355{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000356 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400357 struct btrfs_pending_snapshot *pending_snapshot;
358 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000359 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400360
361 if (!root->ref_cows)
362 return -EINVAL;
363
Josef Bacik9ed74f22009-09-11 16:12:44 -0400364 /*
365 * 1 - inode item
366 * 2 - refs
367 * 1 - root item
368 * 2 - dir items
369 */
370 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000372 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400373
Chris Mason3de45862008-11-17 21:02:50 -0500374 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375 if (!pending_snapshot) {
376 ret = -ENOMEM;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400377 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000378 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379 }
380 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
381 if (!pending_snapshot->name) {
382 ret = -ENOMEM;
383 kfree(pending_snapshot);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400384 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000385 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400386 }
387 memcpy(pending_snapshot->name, name, namelen);
388 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500389 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400390 trans = btrfs_start_transaction(root, 1);
391 BUG_ON(!trans);
392 pending_snapshot->root = root;
393 list_add(&pending_snapshot->list,
394 &trans->transaction->pending_snapshots);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000395 ret = btrfs_commit_transaction(trans, root);
396 BUG_ON(ret);
397 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
400 if (IS_ERR(inode)) {
401 ret = PTR_ERR(inode);
402 goto fail;
403 }
404 BUG_ON(!inode);
405 d_instantiate(dentry, inode);
406 ret = 0;
407fail:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400408 return ret;
409}
410
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400411/* copy of may_create in fs/namei.c() */
412static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
413{
414 if (child->d_inode)
415 return -EEXIST;
416 if (IS_DEADDIR(dir))
417 return -ENOENT;
418 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
419}
420
421/*
422 * Create a new subvolume below @parent. This is largely modeled after
423 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
424 * inside this filesystem so it's quite a bit simpler.
425 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400426static noinline int btrfs_mksubvol(struct path *parent,
427 char *name, int namelen,
Chris Mason3de45862008-11-17 21:02:50 -0500428 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400429{
Yan, Zheng76dda932009-09-21 16:00:26 -0400430 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400431 struct dentry *dentry;
432 int error;
433
Yan, Zheng76dda932009-09-21 16:00:26 -0400434 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400435
436 dentry = lookup_one_len(name, parent->dentry, namelen);
437 error = PTR_ERR(dentry);
438 if (IS_ERR(dentry))
439 goto out_unlock;
440
441 error = -EEXIST;
442 if (dentry->d_inode)
443 goto out_dput;
444
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400445 error = mnt_want_write(parent->mnt);
446 if (error)
447 goto out_dput;
448
Yan, Zheng76dda932009-09-21 16:00:26 -0400449 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400450 if (error)
451 goto out_drop_write;
452
Yan, Zheng76dda932009-09-21 16:00:26 -0400453 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
454
455 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
456 goto out_up_read;
457
Chris Mason3de45862008-11-17 21:02:50 -0500458 if (snap_src) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400459 error = create_snapshot(snap_src, dentry,
460 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500461 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400462 error = create_subvol(BTRFS_I(dir)->root, dentry,
463 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500464 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400465 if (!error)
466 fsnotify_mkdir(dir, dentry);
467out_up_read:
468 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400469out_drop_write:
470 mnt_drop_write(parent->mnt);
471out_dput:
472 dput(dentry);
473out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400474 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400475 return error;
476}
477
Chris Mason940100a2010-03-10 10:52:59 -0500478static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500479 int thresh, u64 *last_len, u64 *skip,
480 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500481{
482 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
483 struct extent_map *em = NULL;
484 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
485 int ret = 1;
486
Chris Mason1e701a32010-03-11 09:42:04 -0500487
488 if (thresh == 0)
489 thresh = 256 * 1024;
490
Chris Mason940100a2010-03-10 10:52:59 -0500491 /*
492 * make sure that once we start defragging and extent, we keep on
493 * defragging it
494 */
495 if (start < *defrag_end)
496 return 1;
497
498 *skip = 0;
499
500 /*
501 * hopefully we have this extent in the tree already, try without
502 * the full extent lock
503 */
504 read_lock(&em_tree->lock);
505 em = lookup_extent_mapping(em_tree, start, len);
506 read_unlock(&em_tree->lock);
507
508 if (!em) {
509 /* get the big lock and read metadata off disk */
510 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
511 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
512 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
513
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000514 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500515 return 0;
516 }
517
518 /* this will cover holes, and inline extents */
519 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
520 ret = 0;
521
522 /*
523 * we hit a real extent, if it is big don't bother defragging it again
524 */
Chris Mason1e701a32010-03-11 09:42:04 -0500525 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500526 ret = 0;
527
528 /*
529 * last_len ends up being a counter of how many bytes we've defragged.
530 * every time we choose not to defrag an extent, we reset *last_len
531 * so that the next tiny extent will force a defrag.
532 *
533 * The end result of this is that tiny extents before a single big
534 * extent will force at least part of that big extent to be defragged.
535 */
536 if (ret) {
537 *last_len += len;
538 *defrag_end = extent_map_end(em);
539 } else {
540 *last_len = 0;
541 *skip = extent_map_end(em);
542 *defrag_end = 0;
543 }
544
545 free_extent_map(em);
546 return ret;
547}
548
Chris Mason1e701a32010-03-11 09:42:04 -0500549static int btrfs_defrag_file(struct file *file,
550 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400551{
552 struct inode *inode = fdentry(file)->d_inode;
553 struct btrfs_root *root = BTRFS_I(inode)->root;
554 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400555 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400556 struct page *page;
557 unsigned long last_index;
558 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
559 unsigned long total_read = 0;
560 u64 page_start;
561 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500562 u64 last_len = 0;
563 u64 skip = 0;
564 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400565 unsigned long i;
566 int ret;
567
Chris Mason940100a2010-03-10 10:52:59 -0500568 if (inode->i_size == 0)
569 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400570
Chris Mason1e701a32010-03-11 09:42:04 -0500571 if (range->start + range->len > range->start) {
572 last_index = min_t(u64, inode->i_size - 1,
573 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
574 } else {
575 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
576 }
577
578 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500579 while (i <= last_index) {
580 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500581 PAGE_CACHE_SIZE,
582 range->extent_thresh,
583 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500584 &defrag_end)) {
585 unsigned long next;
586 /*
587 * the should_defrag function tells us how much to skip
588 * bump our counter by the suggested amount
589 */
590 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
591 i = max(i + 1, next);
592 continue;
593 }
594
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400595 if (total_read % ra_pages == 0) {
596 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
597 min(last_index, i + ra_pages - 1));
598 }
599 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500600 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500601 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
602 BTRFS_I(inode)->force_compress = 1;
Chris Mason940100a2010-03-10 10:52:59 -0500603
604 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
605 if (ret) {
606 ret = -ENOSPC;
607 break;
608 }
609
610 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
611 if (ret) {
612 btrfs_free_reserved_data_space(root, inode,
613 PAGE_CACHE_SIZE);
614 ret = -ENOSPC;
615 break;
616 }
Chris Mason3eaa2882008-07-24 11:57:52 -0400617again:
Chris Mason940100a2010-03-10 10:52:59 -0500618 if (inode->i_size == 0 ||
619 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
620 ret = 0;
621 goto err_reservations;
622 }
623
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400624 page = grab_cache_page(inode->i_mapping, i);
625 if (!page)
Chris Mason940100a2010-03-10 10:52:59 -0500626 goto err_reservations;
627
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400628 if (!PageUptodate(page)) {
629 btrfs_readpage(NULL, page);
630 lock_page(page);
631 if (!PageUptodate(page)) {
632 unlock_page(page);
633 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500634 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400635 }
636 }
637
Chris Mason940100a2010-03-10 10:52:59 -0500638 if (page->mapping != inode->i_mapping) {
639 unlock_page(page);
640 page_cache_release(page);
641 goto again;
642 }
643
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400644 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400645
Chris Mason940100a2010-03-10 10:52:59 -0500646 if (PageDirty(page)) {
647 btrfs_free_reserved_data_space(root, inode,
648 PAGE_CACHE_SIZE);
649 goto loop_unlock;
650 }
651
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
653 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400654 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400655
656 ordered = btrfs_lookup_ordered_extent(inode, page_start);
657 if (ordered) {
658 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
659 unlock_page(page);
660 page_cache_release(page);
661 btrfs_start_ordered_extent(inode, ordered, 1);
662 btrfs_put_ordered_extent(ordered);
663 goto again;
664 }
665 set_page_extent_mapped(page);
666
Chris Masonf87f0572008-08-01 11:27:23 -0400667 /*
668 * this makes sure page_mkwrite is called on the
669 * page if it is dirtied again later
670 */
671 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500672 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
673 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
674 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400675
Josef Bacik2ac55d42010-02-03 19:33:23 +0000676 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500677 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400678 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400679 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500680
681loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400682 unlock_page(page);
683 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500684 mutex_unlock(&inode->i_mutex);
685
686 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400687 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500688 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400689 }
690
Chris Mason1e701a32010-03-11 09:42:04 -0500691 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
692 filemap_flush(inode->i_mapping);
693
694 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
695 /* the filemap_flush will queue IO into the worker threads, but
696 * we have to make sure the IO is actually started and that
697 * ordered extents get created before we return
698 */
699 atomic_inc(&root->fs_info->async_submit_draining);
700 while (atomic_read(&root->fs_info->nr_async_submits) ||
701 atomic_read(&root->fs_info->async_delalloc_pages)) {
702 wait_event(root->fs_info->async_submit_wait,
703 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
704 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
705 }
706 atomic_dec(&root->fs_info->async_submit_draining);
707
708 mutex_lock(&inode->i_mutex);
709 BTRFS_I(inode)->force_compress = 0;
710 mutex_unlock(&inode->i_mutex);
711 }
712
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400713 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500714
715err_reservations:
716 mutex_unlock(&inode->i_mutex);
717 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
718 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
719 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400720}
721
Yan, Zheng76dda932009-09-21 16:00:26 -0400722static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
723 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400724{
725 u64 new_size;
726 u64 old_size;
727 u64 devid = 1;
728 struct btrfs_ioctl_vol_args *vol_args;
729 struct btrfs_trans_handle *trans;
730 struct btrfs_device *device = NULL;
731 char *sizestr;
732 char *devstr = NULL;
733 int ret = 0;
734 int namelen;
735 int mod = 0;
736
Yan Zhengc146afa2008-11-12 14:34:12 -0500737 if (root->fs_info->sb->s_flags & MS_RDONLY)
738 return -EROFS;
739
Chris Masone441d542009-01-05 16:57:23 -0500740 if (!capable(CAP_SYS_ADMIN))
741 return -EPERM;
742
Li Zefandae7b662009-04-08 15:06:54 +0800743 vol_args = memdup_user(arg, sizeof(*vol_args));
744 if (IS_ERR(vol_args))
745 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400746
747 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749
Chris Mason7d9eb122008-07-08 14:19:17 -0400750 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400751 sizestr = vol_args->name;
752 devstr = strchr(sizestr, ':');
753 if (devstr) {
754 char *end;
755 sizestr = devstr + 1;
756 *devstr = '\0';
757 devstr = vol_args->name;
758 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700759 printk(KERN_INFO "resizing devid %llu\n",
760 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761 }
Yan Zheng2b820322008-11-17 21:11:30 -0500762 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400763 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700764 printk(KERN_INFO "resizer unable to find device %llu\n",
765 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400766 ret = -EINVAL;
767 goto out_unlock;
768 }
769 if (!strcmp(sizestr, "max"))
770 new_size = device->bdev->bd_inode->i_size;
771 else {
772 if (sizestr[0] == '-') {
773 mod = -1;
774 sizestr++;
775 } else if (sizestr[0] == '+') {
776 mod = 1;
777 sizestr++;
778 }
Akinobu Mita91748462010-02-28 10:59:11 +0000779 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400780 if (new_size == 0) {
781 ret = -EINVAL;
782 goto out_unlock;
783 }
784 }
785
786 old_size = device->total_bytes;
787
788 if (mod < 0) {
789 if (new_size > old_size) {
790 ret = -EINVAL;
791 goto out_unlock;
792 }
793 new_size = old_size - new_size;
794 } else if (mod > 0) {
795 new_size = old_size + new_size;
796 }
797
798 if (new_size < 256 * 1024 * 1024) {
799 ret = -EINVAL;
800 goto out_unlock;
801 }
802 if (new_size > device->bdev->bd_inode->i_size) {
803 ret = -EFBIG;
804 goto out_unlock;
805 }
806
807 do_div(new_size, root->sectorsize);
808 new_size *= root->sectorsize;
809
810 printk(KERN_INFO "new size for %s is %llu\n",
811 device->name, (unsigned long long)new_size);
812
813 if (new_size > old_size) {
814 trans = btrfs_start_transaction(root, 1);
815 ret = btrfs_grow_device(trans, device, new_size);
816 btrfs_commit_transaction(trans, root);
817 } else {
818 ret = btrfs_shrink_device(device, new_size);
819 }
820
821out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400822 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400823 kfree(vol_args);
824 return ret;
825}
826
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400827static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500828 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400829{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400830 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400831 struct btrfs_ioctl_vol_args *vol_args;
Chris Mason3de45862008-11-17 21:02:50 -0500832 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400833 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500834 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400835
Yan Zhengc146afa2008-11-12 14:34:12 -0500836 if (root->fs_info->sb->s_flags & MS_RDONLY)
837 return -EROFS;
838
Li Zefandae7b662009-04-08 15:06:54 +0800839 vol_args = memdup_user(arg, sizeof(*vol_args));
840 if (IS_ERR(vol_args))
841 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400842
Mark Fasheh5516e592008-07-24 12:20:14 -0400843 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400844 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400845 if (strchr(vol_args->name, '/')) {
846 ret = -EINVAL;
847 goto out;
848 }
849
Chris Mason3de45862008-11-17 21:02:50 -0500850 if (subvol) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400851 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
852 NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400853 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500854 struct inode *src_inode;
855 src_file = fget(vol_args->fd);
856 if (!src_file) {
857 ret = -EINVAL;
858 goto out;
859 }
860
861 src_inode = src_file->f_path.dentry->d_inode;
862 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500863 printk(KERN_INFO "btrfs: Snapshot src from "
864 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500865 ret = -EINVAL;
866 fput(src_file);
867 goto out;
868 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400869 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
870 BTRFS_I(src_inode)->root);
Chris Mason3de45862008-11-17 21:02:50 -0500871 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400872 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400873out:
874 kfree(vol_args);
875 return ret;
876}
877
Yan, Zheng76dda932009-09-21 16:00:26 -0400878/*
879 * helper to check if the subvolume references other subvolumes
880 */
881static noinline int may_destroy_subvol(struct btrfs_root *root)
882{
883 struct btrfs_path *path;
884 struct btrfs_key key;
885 int ret;
886
887 path = btrfs_alloc_path();
888 if (!path)
889 return -ENOMEM;
890
891 key.objectid = root->root_key.objectid;
892 key.type = BTRFS_ROOT_REF_KEY;
893 key.offset = (u64)-1;
894
895 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
896 &key, path, 0, 0);
897 if (ret < 0)
898 goto out;
899 BUG_ON(ret == 0);
900
901 ret = 0;
902 if (path->slots[0] > 0) {
903 path->slots[0]--;
904 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
905 if (key.objectid == root->root_key.objectid &&
906 key.type == BTRFS_ROOT_REF_KEY)
907 ret = -ENOTEMPTY;
908 }
909out:
910 btrfs_free_path(path);
911 return ret;
912}
913
Chris Masonac8e9812010-02-28 15:39:26 -0500914static noinline int key_in_sk(struct btrfs_key *key,
915 struct btrfs_ioctl_search_key *sk)
916{
Chris Masonabc6e132010-03-18 12:10:08 -0400917 struct btrfs_key test;
918 int ret;
919
920 test.objectid = sk->min_objectid;
921 test.type = sk->min_type;
922 test.offset = sk->min_offset;
923
924 ret = btrfs_comp_cpu_keys(key, &test);
925 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -0500926 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -0400927
928 test.objectid = sk->max_objectid;
929 test.type = sk->max_type;
930 test.offset = sk->max_offset;
931
932 ret = btrfs_comp_cpu_keys(key, &test);
933 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -0500934 return 0;
935 return 1;
936}
937
938static noinline int copy_to_sk(struct btrfs_root *root,
939 struct btrfs_path *path,
940 struct btrfs_key *key,
941 struct btrfs_ioctl_search_key *sk,
942 char *buf,
943 unsigned long *sk_offset,
944 int *num_found)
945{
946 u64 found_transid;
947 struct extent_buffer *leaf;
948 struct btrfs_ioctl_search_header sh;
949 unsigned long item_off;
950 unsigned long item_len;
951 int nritems;
952 int i;
953 int slot;
954 int found = 0;
955 int ret = 0;
956
957 leaf = path->nodes[0];
958 slot = path->slots[0];
959 nritems = btrfs_header_nritems(leaf);
960
961 if (btrfs_header_generation(leaf) > sk->max_transid) {
962 i = nritems;
963 goto advance_key;
964 }
965 found_transid = btrfs_header_generation(leaf);
966
967 for (i = slot; i < nritems; i++) {
968 item_off = btrfs_item_ptr_offset(leaf, i);
969 item_len = btrfs_item_size_nr(leaf, i);
970
971 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
972 item_len = 0;
973
974 if (sizeof(sh) + item_len + *sk_offset >
975 BTRFS_SEARCH_ARGS_BUFSIZE) {
976 ret = 1;
977 goto overflow;
978 }
979
980 btrfs_item_key_to_cpu(leaf, key, i);
981 if (!key_in_sk(key, sk))
982 continue;
983
984 sh.objectid = key->objectid;
985 sh.offset = key->offset;
986 sh.type = key->type;
987 sh.len = item_len;
988 sh.transid = found_transid;
989
990 /* copy search result header */
991 memcpy(buf + *sk_offset, &sh, sizeof(sh));
992 *sk_offset += sizeof(sh);
993
994 if (item_len) {
995 char *p = buf + *sk_offset;
996 /* copy the item */
997 read_extent_buffer(leaf, p,
998 item_off, item_len);
999 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001000 }
Chris Mason90fdde12010-03-18 12:14:54 -04001001 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001002
1003 if (*num_found >= sk->nr_items)
1004 break;
1005 }
1006advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001007 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001008 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1009 key->offset++;
1010 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1011 key->offset = 0;
1012 key->type++;
1013 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1014 key->offset = 0;
1015 key->type = 0;
1016 key->objectid++;
1017 } else
1018 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001019overflow:
1020 *num_found += found;
1021 return ret;
1022}
1023
1024static noinline int search_ioctl(struct inode *inode,
1025 struct btrfs_ioctl_search_args *args)
1026{
1027 struct btrfs_root *root;
1028 struct btrfs_key key;
1029 struct btrfs_key max_key;
1030 struct btrfs_path *path;
1031 struct btrfs_ioctl_search_key *sk = &args->key;
1032 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1033 int ret;
1034 int num_found = 0;
1035 unsigned long sk_offset = 0;
1036
1037 path = btrfs_alloc_path();
1038 if (!path)
1039 return -ENOMEM;
1040
1041 if (sk->tree_id == 0) {
1042 /* search the root of the inode that was passed */
1043 root = BTRFS_I(inode)->root;
1044 } else {
1045 key.objectid = sk->tree_id;
1046 key.type = BTRFS_ROOT_ITEM_KEY;
1047 key.offset = (u64)-1;
1048 root = btrfs_read_fs_root_no_name(info, &key);
1049 if (IS_ERR(root)) {
1050 printk(KERN_ERR "could not find root %llu\n",
1051 sk->tree_id);
1052 btrfs_free_path(path);
1053 return -ENOENT;
1054 }
1055 }
1056
1057 key.objectid = sk->min_objectid;
1058 key.type = sk->min_type;
1059 key.offset = sk->min_offset;
1060
1061 max_key.objectid = sk->max_objectid;
1062 max_key.type = sk->max_type;
1063 max_key.offset = sk->max_offset;
1064
1065 path->keep_locks = 1;
1066
1067 while(1) {
1068 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1069 sk->min_transid);
1070 if (ret != 0) {
1071 if (ret > 0)
1072 ret = 0;
1073 goto err;
1074 }
1075 ret = copy_to_sk(root, path, &key, sk, args->buf,
1076 &sk_offset, &num_found);
1077 btrfs_release_path(root, path);
1078 if (ret || num_found >= sk->nr_items)
1079 break;
1080
1081 }
1082 ret = 0;
1083err:
1084 sk->nr_items = num_found;
1085 btrfs_free_path(path);
1086 return ret;
1087}
1088
1089static noinline int btrfs_ioctl_tree_search(struct file *file,
1090 void __user *argp)
1091{
1092 struct btrfs_ioctl_search_args *args;
1093 struct inode *inode;
1094 int ret;
1095
1096 if (!capable(CAP_SYS_ADMIN))
1097 return -EPERM;
1098
1099 args = kmalloc(sizeof(*args), GFP_KERNEL);
1100 if (!args)
1101 return -ENOMEM;
1102
1103 if (copy_from_user(args, argp, sizeof(*args))) {
1104 kfree(args);
1105 return -EFAULT;
1106 }
1107 inode = fdentry(file)->d_inode;
1108 ret = search_ioctl(inode, args);
1109 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1110 ret = -EFAULT;
1111 kfree(args);
1112 return ret;
1113}
1114
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001115/*
Chris Masonac8e9812010-02-28 15:39:26 -05001116 * Search INODE_REFs to identify path name of 'dirid' directory
1117 * in a 'tree_id' tree. and sets path name to 'name'.
1118 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001119static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1120 u64 tree_id, u64 dirid, char *name)
1121{
1122 struct btrfs_root *root;
1123 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001124 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001125 int ret = -1;
1126 int slot;
1127 int len;
1128 int total_len = 0;
1129 struct btrfs_inode_ref *iref;
1130 struct extent_buffer *l;
1131 struct btrfs_path *path;
1132
1133 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1134 name[0]='\0';
1135 return 0;
1136 }
1137
1138 path = btrfs_alloc_path();
1139 if (!path)
1140 return -ENOMEM;
1141
Chris Masonac8e9812010-02-28 15:39:26 -05001142 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001143
1144 key.objectid = tree_id;
1145 key.type = BTRFS_ROOT_ITEM_KEY;
1146 key.offset = (u64)-1;
1147 root = btrfs_read_fs_root_no_name(info, &key);
1148 if (IS_ERR(root)) {
1149 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001150 ret = -ENOENT;
1151 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001152 }
1153
1154 key.objectid = dirid;
1155 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001156 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001157
1158 while(1) {
1159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1160 if (ret < 0)
1161 goto out;
1162
1163 l = path->nodes[0];
1164 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001165 if (ret > 0 && slot > 0)
1166 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001167 btrfs_item_key_to_cpu(l, &key, slot);
1168
1169 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001170 key.type != BTRFS_INODE_REF_KEY)) {
1171 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001172 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001173 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001174
1175 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1176 len = btrfs_inode_ref_name_len(l, iref);
1177 ptr -= len + 1;
1178 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001179 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001180 goto out;
1181
1182 *(ptr + len) = '/';
1183 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1184
1185 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1186 break;
1187
1188 btrfs_release_path(root, path);
1189 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001190 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001191 dirid = key.objectid;
1192
1193 }
Chris Masonac8e9812010-02-28 15:39:26 -05001194 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001195 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001196 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001197 name[total_len]='\0';
1198 ret = 0;
1199out:
1200 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001201 return ret;
1202}
1203
1204static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1205 void __user *argp)
1206{
1207 struct btrfs_ioctl_ino_lookup_args *args;
1208 struct inode *inode;
1209 int ret;
1210
1211 if (!capable(CAP_SYS_ADMIN))
1212 return -EPERM;
1213
1214 args = kmalloc(sizeof(*args), GFP_KERNEL);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001215 if (!args)
1216 return -ENOMEM;
1217
Chris Masonac8e9812010-02-28 15:39:26 -05001218 if (copy_from_user(args, argp, sizeof(*args))) {
1219 kfree(args);
1220 return -EFAULT;
1221 }
1222 inode = fdentry(file)->d_inode;
1223
Chris Mason1b53ac42010-03-18 12:17:05 -04001224 if (args->treeid == 0)
1225 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1226
Chris Masonac8e9812010-02-28 15:39:26 -05001227 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1228 args->treeid, args->objectid,
1229 args->name);
1230
1231 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1232 ret = -EFAULT;
1233
1234 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001235 return ret;
1236}
1237
Yan, Zheng76dda932009-09-21 16:00:26 -04001238static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1239 void __user *arg)
1240{
1241 struct dentry *parent = fdentry(file);
1242 struct dentry *dentry;
1243 struct inode *dir = parent->d_inode;
1244 struct inode *inode;
1245 struct btrfs_root *root = BTRFS_I(dir)->root;
1246 struct btrfs_root *dest = NULL;
1247 struct btrfs_ioctl_vol_args *vol_args;
1248 struct btrfs_trans_handle *trans;
1249 int namelen;
1250 int ret;
1251 int err = 0;
1252
1253 if (!capable(CAP_SYS_ADMIN))
1254 return -EPERM;
1255
1256 vol_args = memdup_user(arg, sizeof(*vol_args));
1257 if (IS_ERR(vol_args))
1258 return PTR_ERR(vol_args);
1259
1260 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1261 namelen = strlen(vol_args->name);
1262 if (strchr(vol_args->name, '/') ||
1263 strncmp(vol_args->name, "..", namelen) == 0) {
1264 err = -EINVAL;
1265 goto out;
1266 }
1267
1268 err = mnt_want_write(file->f_path.mnt);
1269 if (err)
1270 goto out;
1271
1272 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1273 dentry = lookup_one_len(vol_args->name, parent, namelen);
1274 if (IS_ERR(dentry)) {
1275 err = PTR_ERR(dentry);
1276 goto out_unlock_dir;
1277 }
1278
1279 if (!dentry->d_inode) {
1280 err = -ENOENT;
1281 goto out_dput;
1282 }
1283
1284 inode = dentry->d_inode;
1285 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1286 err = -EINVAL;
1287 goto out_dput;
1288 }
1289
1290 dest = BTRFS_I(inode)->root;
1291
1292 mutex_lock(&inode->i_mutex);
1293 err = d_invalidate(dentry);
1294 if (err)
1295 goto out_unlock;
1296
1297 down_write(&root->fs_info->subvol_sem);
1298
1299 err = may_destroy_subvol(dest);
1300 if (err)
1301 goto out_up_write;
1302
1303 trans = btrfs_start_transaction(root, 1);
1304 ret = btrfs_unlink_subvol(trans, root, dir,
1305 dest->root_key.objectid,
1306 dentry->d_name.name,
1307 dentry->d_name.len);
1308 BUG_ON(ret);
1309
1310 btrfs_record_root_in_trans(trans, dest);
1311
1312 memset(&dest->root_item.drop_progress, 0,
1313 sizeof(dest->root_item.drop_progress));
1314 dest->root_item.drop_level = 0;
1315 btrfs_set_root_refs(&dest->root_item, 0);
1316
1317 ret = btrfs_insert_orphan_item(trans,
1318 root->fs_info->tree_root,
1319 dest->root_key.objectid);
1320 BUG_ON(ret);
1321
1322 ret = btrfs_commit_transaction(trans, root);
1323 BUG_ON(ret);
1324 inode->i_flags |= S_DEAD;
1325out_up_write:
1326 up_write(&root->fs_info->subvol_sem);
1327out_unlock:
1328 mutex_unlock(&inode->i_mutex);
1329 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001330 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001331 btrfs_invalidate_inodes(dest);
1332 d_delete(dentry);
1333 }
1334out_dput:
1335 dput(dentry);
1336out_unlock_dir:
1337 mutex_unlock(&dir->i_mutex);
1338 mnt_drop_write(file->f_path.mnt);
1339out:
1340 kfree(vol_args);
1341 return err;
1342}
1343
Chris Mason1e701a32010-03-11 09:42:04 -05001344static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001345{
1346 struct inode *inode = fdentry(file)->d_inode;
1347 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001348 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001349 int ret;
1350
1351 ret = mnt_want_write(file->f_path.mnt);
1352 if (ret)
1353 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001354
1355 switch (inode->i_mode & S_IFMT) {
1356 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001357 if (!capable(CAP_SYS_ADMIN)) {
1358 ret = -EPERM;
1359 goto out;
1360 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001361 btrfs_defrag_root(root, 0);
1362 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001363 break;
1364 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001365 if (!(file->f_mode & FMODE_WRITE)) {
1366 ret = -EINVAL;
1367 goto out;
1368 }
Chris Mason1e701a32010-03-11 09:42:04 -05001369
1370 range = kzalloc(sizeof(*range), GFP_KERNEL);
1371 if (!range) {
1372 ret = -ENOMEM;
1373 goto out;
1374 }
1375
1376 if (argp) {
1377 if (copy_from_user(range, argp,
1378 sizeof(*range))) {
1379 ret = -EFAULT;
1380 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001381 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001382 }
1383 /* compression requires us to start the IO */
1384 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1385 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1386 range->extent_thresh = (u32)-1;
1387 }
1388 } else {
1389 /* the rest are all set to zero by kzalloc */
1390 range->len = (u64)-1;
1391 }
1392 btrfs_defrag_file(file, range);
1393 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001394 break;
1395 }
Chris Masone441d542009-01-05 16:57:23 -05001396out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001397 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001398 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001399}
1400
Christoph Hellwigb2950862008-12-02 09:54:17 -05001401static long btrfs_ioctl_add_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
Li Zefandae7b662009-04-08 15:06:54 +08001409 vol_args = memdup_user(arg, sizeof(*vol_args));
1410 if (IS_ERR(vol_args))
1411 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001412
Mark Fasheh5516e592008-07-24 12:20:14 -04001413 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001414 ret = btrfs_init_new_device(root, vol_args->name);
1415
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001416 kfree(vol_args);
1417 return ret;
1418}
1419
Christoph Hellwigb2950862008-12-02 09:54:17 -05001420static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421{
1422 struct btrfs_ioctl_vol_args *vol_args;
1423 int ret;
1424
Chris Masone441d542009-01-05 16:57:23 -05001425 if (!capable(CAP_SYS_ADMIN))
1426 return -EPERM;
1427
Yan Zhengc146afa2008-11-12 14:34:12 -05001428 if (root->fs_info->sb->s_flags & MS_RDONLY)
1429 return -EROFS;
1430
Li Zefandae7b662009-04-08 15:06:54 +08001431 vol_args = memdup_user(arg, sizeof(*vol_args));
1432 if (IS_ERR(vol_args))
1433 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001434
Mark Fasheh5516e592008-07-24 12:20:14 -04001435 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001436 ret = btrfs_rm_device(root, vol_args->name);
1437
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001438 kfree(vol_args);
1439 return ret;
1440}
1441
Yan, Zheng76dda932009-09-21 16:00:26 -04001442static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1443 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001444{
1445 struct inode *inode = fdentry(file)->d_inode;
1446 struct btrfs_root *root = BTRFS_I(inode)->root;
1447 struct file *src_file;
1448 struct inode *src;
1449 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001450 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001451 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001452 char *buf;
1453 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001454 u32 nritems;
1455 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001456 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001457 u64 len = olen;
1458 u64 bs = root->fs_info->sb->s_blocksize;
1459 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001460
Sage Weilc5c9cd42008-11-12 14:32:25 -05001461 /*
1462 * TODO:
1463 * - split compressed inline extents. annoying: we need to
1464 * decompress into destination's address_space (the file offset
1465 * may change, so source mapping won't do), then recompress (or
1466 * otherwise reinsert) a subrange.
1467 * - allow ranges within the same file to be cloned (provided
1468 * they don't overlap)?
1469 */
1470
Chris Masone441d542009-01-05 16:57:23 -05001471 /* the destination must be opened for writing */
1472 if (!(file->f_mode & FMODE_WRITE))
1473 return -EINVAL;
1474
Yan Zhengc146afa2008-11-12 14:34:12 -05001475 ret = mnt_want_write(file->f_path.mnt);
1476 if (ret)
1477 return ret;
1478
Sage Weilc5c9cd42008-11-12 14:32:25 -05001479 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001480 if (!src_file) {
1481 ret = -EBADF;
1482 goto out_drop_write;
1483 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001484
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001485 src = src_file->f_dentry->d_inode;
1486
Sage Weilc5c9cd42008-11-12 14:32:25 -05001487 ret = -EINVAL;
1488 if (src == inode)
1489 goto out_fput;
1490
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001491 /* the src must be open for reading */
1492 if (!(src_file->f_mode & FMODE_READ))
1493 goto out_fput;
1494
Yan Zhengae01a0a2008-08-04 23:23:47 -04001495 ret = -EISDIR;
1496 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001497 goto out_fput;
1498
Yan Zhengae01a0a2008-08-04 23:23:47 -04001499 ret = -EXDEV;
1500 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1501 goto out_fput;
1502
1503 ret = -ENOMEM;
1504 buf = vmalloc(btrfs_level_size(root, 0));
1505 if (!buf)
1506 goto out_fput;
1507
1508 path = btrfs_alloc_path();
1509 if (!path) {
1510 vfree(buf);
1511 goto out_fput;
1512 }
1513 path->reada = 2;
1514
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001515 if (inode < src) {
1516 mutex_lock(&inode->i_mutex);
1517 mutex_lock(&src->i_mutex);
1518 } else {
1519 mutex_lock(&src->i_mutex);
1520 mutex_lock(&inode->i_mutex);
1521 }
1522
Sage Weilc5c9cd42008-11-12 14:32:25 -05001523 /* determine range to clone */
1524 ret = -EINVAL;
1525 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001526 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001527 if (len == 0)
1528 olen = len = src->i_size - off;
1529 /* if we extend to eof, continue to block boundary */
1530 if (off + len == src->i_size)
1531 len = ((src->i_size + bs-1) & ~(bs-1))
1532 - off;
1533
1534 /* verify the end result is block aligned */
1535 if ((off & (bs-1)) ||
1536 ((off + len) & (bs-1)))
1537 goto out_unlock;
1538
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001539 /* do any pending delalloc/csum calc on src, one way or
1540 another, and lock file content */
1541 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001542 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001543 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1544 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001545 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001546 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001547 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001548 if (ordered)
1549 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001550 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001551 }
1552
Yan Zhengae01a0a2008-08-04 23:23:47 -04001553 trans = btrfs_start_transaction(root, 1);
1554 BUG_ON(!trans);
1555
Sage Weilc5c9cd42008-11-12 14:32:25 -05001556 /* punch hole in destination first */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001557 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001558
1559 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001560 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001561 key.type = BTRFS_EXTENT_DATA_KEY;
1562 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001563
1564 while (1) {
1565 /*
1566 * note the key will change type as we walk through the
1567 * tree.
1568 */
1569 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1570 if (ret < 0)
1571 goto out;
1572
Yan Zhengae01a0a2008-08-04 23:23:47 -04001573 nritems = btrfs_header_nritems(path->nodes[0]);
1574 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001575 ret = btrfs_next_leaf(root, path);
1576 if (ret < 0)
1577 goto out;
1578 if (ret > 0)
1579 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001580 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001581 }
1582 leaf = path->nodes[0];
1583 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584
Yan Zhengae01a0a2008-08-04 23:23:47 -04001585 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001586 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001587 key.objectid != src->i_ino)
1588 break;
1589
Sage Weilc5c9cd42008-11-12 14:32:25 -05001590 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1591 struct btrfs_file_extent_item *extent;
1592 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001593 u32 size;
1594 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001595 u64 disko = 0, diskl = 0;
1596 u64 datao = 0, datal = 0;
1597 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -04001598
1599 size = btrfs_item_size_nr(leaf, slot);
1600 read_extent_buffer(leaf, buf,
1601 btrfs_item_ptr_offset(leaf, slot),
1602 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001603
1604 extent = btrfs_item_ptr(leaf, slot,
1605 struct btrfs_file_extent_item);
1606 comp = btrfs_file_extent_compression(leaf, extent);
1607 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001608 if (type == BTRFS_FILE_EXTENT_REG ||
1609 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001610 disko = btrfs_file_extent_disk_bytenr(leaf,
1611 extent);
1612 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1613 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001614 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001615 datal = btrfs_file_extent_num_bytes(leaf,
1616 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001617 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1618 /* take upper bound, may be compressed */
1619 datal = btrfs_file_extent_ram_bytes(leaf,
1620 extent);
1621 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001622 btrfs_release_path(root, path);
1623
Sage Weilc5c9cd42008-11-12 14:32:25 -05001624 if (key.offset + datal < off ||
1625 key.offset >= off+len)
1626 goto next;
1627
Zheng Yan31840ae2008-09-23 13:14:14 -04001628 memcpy(&new_key, &key, sizeof(new_key));
1629 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001630 new_key.offset = key.offset + destoff - off;
1631
Chris Masonc8a894d2009-06-27 21:07:03 -04001632 if (type == BTRFS_FILE_EXTENT_REG ||
1633 type == BTRFS_FILE_EXTENT_PREALLOC) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001634 ret = btrfs_insert_empty_item(trans, root, path,
1635 &new_key, size);
1636 if (ret)
1637 goto out;
1638
1639 leaf = path->nodes[0];
1640 slot = path->slots[0];
1641 write_extent_buffer(leaf, buf,
1642 btrfs_item_ptr_offset(leaf, slot),
1643 size);
1644
1645 extent = btrfs_item_ptr(leaf, slot,
1646 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001647
1648 if (off > key.offset) {
1649 datao += off - key.offset;
1650 datal -= off - key.offset;
1651 }
Chris Masonac6889c2009-10-09 11:29:53 -04001652
1653 if (key.offset + datal > off + len)
1654 datal = off + len - key.offset;
1655
Sage Weilc5c9cd42008-11-12 14:32:25 -05001656 /* disko == 0 means it's a hole */
1657 if (!disko)
1658 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001659
1660 btrfs_set_file_extent_offset(leaf, extent,
1661 datao);
1662 btrfs_set_file_extent_num_bytes(leaf, extent,
1663 datal);
1664 if (disko) {
1665 inode_add_bytes(inode, datal);
1666 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001667 disko, diskl, 0,
1668 root->root_key.objectid,
1669 inode->i_ino,
1670 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001671 BUG_ON(ret);
1672 }
1673 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1674 u64 skip = 0;
1675 u64 trim = 0;
1676 if (off > key.offset) {
1677 skip = off - key.offset;
1678 new_key.offset += skip;
1679 }
Chris Masond3977122009-01-05 21:25:51 -05001680
Sage Weilc5c9cd42008-11-12 14:32:25 -05001681 if (key.offset + datal > off+len)
1682 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001683
Sage Weilc5c9cd42008-11-12 14:32:25 -05001684 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001685 ret = -EINVAL;
1686 goto out;
1687 }
1688 size -= skip + trim;
1689 datal -= skip + trim;
1690 ret = btrfs_insert_empty_item(trans, root, path,
1691 &new_key, size);
1692 if (ret)
1693 goto out;
1694
1695 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001696 u32 start =
1697 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001698 memmove(buf+start, buf+start+skip,
1699 datal);
1700 }
1701
1702 leaf = path->nodes[0];
1703 slot = path->slots[0];
1704 write_extent_buffer(leaf, buf,
1705 btrfs_item_ptr_offset(leaf, slot),
1706 size);
1707 inode_add_bytes(inode, datal);
1708 }
1709
1710 btrfs_mark_buffer_dirty(leaf);
1711 }
1712
Chris Masond3977122009-01-05 21:25:51 -05001713next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001714 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001715 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001716 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001717 ret = 0;
1718out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001719 btrfs_release_path(root, path);
1720 if (ret == 0) {
1721 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001722 if (destoff + olen > inode->i_size)
1723 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001724 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1725 ret = btrfs_update_inode(trans, root, inode);
1726 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001727 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001728 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001729 if (ret)
1730 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731out_unlock:
1732 mutex_unlock(&src->i_mutex);
1733 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001734 vfree(buf);
1735 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001736out_fput:
1737 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001738out_drop_write:
1739 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001740 return ret;
1741}
1742
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001743static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001744{
1745 struct btrfs_ioctl_clone_range_args args;
1746
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001747 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001748 return -EFAULT;
1749 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1750 args.src_length, args.dest_offset);
1751}
1752
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001753/*
1754 * there are many ways the trans_start and trans_end ioctls can lead
1755 * to deadlocks. They should only be used by applications that
1756 * basically own the machine, and have a very in depth understanding
1757 * of all the possible deadlocks and enospc problems.
1758 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001759static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001760{
1761 struct inode *inode = fdentry(file)->d_inode;
1762 struct btrfs_root *root = BTRFS_I(inode)->root;
1763 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001764 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001765
Sage Weil1ab86ae2009-09-29 18:38:44 -04001766 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001767 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001768 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001769
1770 ret = -EINPROGRESS;
1771 if (file->private_data)
1772 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001773
Yan Zhengc146afa2008-11-12 14:34:12 -05001774 ret = mnt_want_write(file->f_path.mnt);
1775 if (ret)
1776 goto out;
1777
Sage Weil9ca9ee02008-08-04 10:41:27 -04001778 mutex_lock(&root->fs_info->trans_mutex);
1779 root->fs_info->open_ioctl_trans++;
1780 mutex_unlock(&root->fs_info->trans_mutex);
1781
Sage Weil1ab86ae2009-09-29 18:38:44 -04001782 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001783 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001784 if (!trans)
1785 goto out_drop;
1786
1787 file->private_data = trans;
1788 return 0;
1789
1790out_drop:
1791 mutex_lock(&root->fs_info->trans_mutex);
1792 root->fs_info->open_ioctl_trans--;
1793 mutex_unlock(&root->fs_info->trans_mutex);
1794 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001795out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001796 return ret;
1797}
1798
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001799static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1800{
1801 struct inode *inode = fdentry(file)->d_inode;
1802 struct btrfs_root *root = BTRFS_I(inode)->root;
1803 struct btrfs_root *new_root;
1804 struct btrfs_dir_item *di;
1805 struct btrfs_trans_handle *trans;
1806 struct btrfs_path *path;
1807 struct btrfs_key location;
1808 struct btrfs_disk_key disk_key;
1809 struct btrfs_super_block *disk_super;
1810 u64 features;
1811 u64 objectid = 0;
1812 u64 dir_id;
1813
1814 if (!capable(CAP_SYS_ADMIN))
1815 return -EPERM;
1816
1817 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1818 return -EFAULT;
1819
1820 if (!objectid)
1821 objectid = root->root_key.objectid;
1822
1823 location.objectid = objectid;
1824 location.type = BTRFS_ROOT_ITEM_KEY;
1825 location.offset = (u64)-1;
1826
1827 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1828 if (IS_ERR(new_root))
1829 return PTR_ERR(new_root);
1830
1831 if (btrfs_root_refs(&new_root->root_item) == 0)
1832 return -ENOENT;
1833
1834 path = btrfs_alloc_path();
1835 if (!path)
1836 return -ENOMEM;
1837 path->leave_spinning = 1;
1838
1839 trans = btrfs_start_transaction(root, 1);
1840 if (!trans) {
1841 btrfs_free_path(path);
1842 return -ENOMEM;
1843 }
1844
1845 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1846 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1847 dir_id, "default", 7, 1);
1848 if (!di) {
1849 btrfs_free_path(path);
1850 btrfs_end_transaction(trans, root);
1851 printk(KERN_ERR "Umm, you don't have the default dir item, "
1852 "this isn't going to work\n");
1853 return -ENOENT;
1854 }
1855
1856 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1857 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1858 btrfs_mark_buffer_dirty(path->nodes[0]);
1859 btrfs_free_path(path);
1860
1861 disk_super = &root->fs_info->super_copy;
1862 features = btrfs_super_incompat_flags(disk_super);
1863 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1864 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1865 btrfs_set_super_incompat_flags(disk_super, features);
1866 }
1867 btrfs_end_transaction(trans, root);
1868
1869 return 0;
1870}
1871
Josef Bacik1406e432010-01-13 18:19:06 +00001872long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1873{
1874 struct btrfs_ioctl_space_args space_args;
1875 struct btrfs_ioctl_space_info space;
1876 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04001877 struct btrfs_ioctl_space_info *dest_orig;
1878 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00001879 struct btrfs_space_info *info;
Chris Mason7fde62b2010-03-16 15:40:10 -04001880 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00001881 int ret = 0;
Chris Mason7fde62b2010-03-16 15:40:10 -04001882 int slot_count = 0;
Josef Bacik1406e432010-01-13 18:19:06 +00001883
1884 if (copy_from_user(&space_args,
1885 (struct btrfs_ioctl_space_args __user *)arg,
1886 sizeof(space_args)))
1887 return -EFAULT;
1888
Chris Mason7fde62b2010-03-16 15:40:10 -04001889 /* first we count slots */
1890 rcu_read_lock();
1891 list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
1892 slot_count++;
1893 rcu_read_unlock();
Josef Bacik1406e432010-01-13 18:19:06 +00001894
Chris Mason7fde62b2010-03-16 15:40:10 -04001895 /* space_slots == 0 means they are asking for a count */
1896 if (space_args.space_slots == 0) {
1897 space_args.total_spaces = slot_count;
1898 goto out;
1899 }
1900 alloc_size = sizeof(*dest) * slot_count;
1901 /* we generally have at most 6 or so space infos, one for each raid
1902 * level. So, a whole page should be more than enough for everyone
1903 */
1904 if (alloc_size > PAGE_CACHE_SIZE)
1905 return -ENOMEM;
1906
1907 space_args.total_spaces = 0;
1908 dest = kmalloc(alloc_size, GFP_NOFS);
1909 if (!dest)
1910 return -ENOMEM;
1911 dest_orig = dest;
1912
1913 /* now we have a buffer to copy into */
Josef Bacik1406e432010-01-13 18:19:06 +00001914 rcu_read_lock();
1915 list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
Chris Mason7fde62b2010-03-16 15:40:10 -04001916 /* make sure we don't copy more than we allocated
1917 * in our buffer
1918 */
1919 if (slot_count == 0)
1920 break;
1921 slot_count--;
1922
1923 /* make sure userland has enough room in their buffer */
Josef Bacik1406e432010-01-13 18:19:06 +00001924 if (space_args.total_spaces >= space_args.space_slots)
1925 break;
Chris Mason7fde62b2010-03-16 15:40:10 -04001926
Josef Bacik1406e432010-01-13 18:19:06 +00001927 space.flags = info->flags;
1928 space.total_bytes = info->total_bytes;
1929 space.used_bytes = info->bytes_used;
Chris Mason7fde62b2010-03-16 15:40:10 -04001930 memcpy(dest, &space, sizeof(space));
Josef Bacik1406e432010-01-13 18:19:06 +00001931 dest++;
1932 space_args.total_spaces++;
1933 }
1934 rcu_read_unlock();
1935
Chris Mason7fde62b2010-03-16 15:40:10 -04001936 user_dest = (struct btrfs_ioctl_space_info *)
1937 (arg + sizeof(struct btrfs_ioctl_space_args));
1938
1939 if (copy_to_user(user_dest, dest_orig, alloc_size))
1940 ret = -EFAULT;
1941
1942 kfree(dest_orig);
1943out:
1944 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00001945 ret = -EFAULT;
1946
1947 return ret;
1948}
1949
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001950/*
1951 * there are many ways the trans_start and trans_end ioctls can lead
1952 * to deadlocks. They should only be used by applications that
1953 * basically own the machine, and have a very in depth understanding
1954 * of all the possible deadlocks and enospc problems.
1955 */
1956long btrfs_ioctl_trans_end(struct file *file)
1957{
1958 struct inode *inode = fdentry(file)->d_inode;
1959 struct btrfs_root *root = BTRFS_I(inode)->root;
1960 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001961
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001962 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001963 if (!trans)
1964 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04001965 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001966
Sage Weil1ab86ae2009-09-29 18:38:44 -04001967 btrfs_end_transaction(trans, root);
1968
Sage Weil9ca9ee02008-08-04 10:41:27 -04001969 mutex_lock(&root->fs_info->trans_mutex);
1970 root->fs_info->open_ioctl_trans--;
1971 mutex_unlock(&root->fs_info->trans_mutex);
1972
Sage Weilcfc8ea82008-12-11 16:30:06 -05001973 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001974 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001975}
1976
1977long btrfs_ioctl(struct file *file, unsigned int
1978 cmd, unsigned long arg)
1979{
1980 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001981 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001982
1983 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001984 case FS_IOC_GETFLAGS:
1985 return btrfs_ioctl_getflags(file, argp);
1986 case FS_IOC_SETFLAGS:
1987 return btrfs_ioctl_setflags(file, argp);
1988 case FS_IOC_GETVERSION:
1989 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001990 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001991 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001992 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001993 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04001994 case BTRFS_IOC_SNAP_DESTROY:
1995 return btrfs_ioctl_snap_destroy(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001996 case BTRFS_IOC_DEFAULT_SUBVOL:
1997 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001998 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05001999 return btrfs_ioctl_defrag(file, NULL);
2000 case BTRFS_IOC_DEFRAG_RANGE:
2001 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002002 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002003 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002004 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002005 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002006 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002007 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002008 case BTRFS_IOC_BALANCE:
2009 return btrfs_balance(root->fs_info->dev_root);
2010 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002011 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2012 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002013 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002014 case BTRFS_IOC_TRANS_START:
2015 return btrfs_ioctl_trans_start(file);
2016 case BTRFS_IOC_TRANS_END:
2017 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002018 case BTRFS_IOC_TREE_SEARCH:
2019 return btrfs_ioctl_tree_search(file, argp);
2020 case BTRFS_IOC_INO_LOOKUP:
2021 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002022 case BTRFS_IOC_SPACE_INFO:
2023 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002024 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002025 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2026 return 0;
2027 }
2028
2029 return -ENOTTY;
2030}