blob: 88d3cb2eaf75ecf10d660f83bacb9543d22fb007 [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
Li Zefanb83cc962010-12-20 16:04:08 +0800150 if (btrfs_root_readonly(root))
151 return -EROFS;
152
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200153 if (copy_from_user(&flags, arg, sizeof(flags)))
154 return -EFAULT;
155
156 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
157 FS_NOATIME_FL | FS_NODUMP_FL | \
158 FS_SYNC_FL | FS_DIRSYNC_FL))
159 return -EOPNOTSUPP;
160
161 if (!is_owner_or_cap(inode))
162 return -EACCES;
163
164 mutex_lock(&inode->i_mutex);
165
166 flags = btrfs_mask_flags(inode->i_mode, flags);
167 oldflags = btrfs_flags_to_ioctl(ip->flags);
168 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
169 if (!capable(CAP_LINUX_IMMUTABLE)) {
170 ret = -EPERM;
171 goto out_unlock;
172 }
173 }
174
175 ret = mnt_want_write(file->f_path.mnt);
176 if (ret)
177 goto out_unlock;
178
179 if (flags & FS_SYNC_FL)
180 ip->flags |= BTRFS_INODE_SYNC;
181 else
182 ip->flags &= ~BTRFS_INODE_SYNC;
183 if (flags & FS_IMMUTABLE_FL)
184 ip->flags |= BTRFS_INODE_IMMUTABLE;
185 else
186 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
187 if (flags & FS_APPEND_FL)
188 ip->flags |= BTRFS_INODE_APPEND;
189 else
190 ip->flags &= ~BTRFS_INODE_APPEND;
191 if (flags & FS_NODUMP_FL)
192 ip->flags |= BTRFS_INODE_NODUMP;
193 else
194 ip->flags &= ~BTRFS_INODE_NODUMP;
195 if (flags & FS_NOATIME_FL)
196 ip->flags |= BTRFS_INODE_NOATIME;
197 else
198 ip->flags &= ~BTRFS_INODE_NOATIME;
199 if (flags & FS_DIRSYNC_FL)
200 ip->flags |= BTRFS_INODE_DIRSYNC;
201 else
202 ip->flags &= ~BTRFS_INODE_DIRSYNC;
203
204
205 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000206 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200207
208 ret = btrfs_update_inode(trans, root, inode);
209 BUG_ON(ret);
210
211 btrfs_update_iflags(inode);
212 inode->i_ctime = CURRENT_TIME;
213 btrfs_end_transaction(trans, root);
214
215 mnt_drop_write(file->f_path.mnt);
216 out_unlock:
217 mutex_unlock(&inode->i_mutex);
218 return 0;
219}
220
221static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
222{
223 struct inode *inode = file->f_path.dentry->d_inode;
224
225 return put_user(inode->i_generation, arg);
226}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400227
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400228static noinline int create_subvol(struct btrfs_root *root,
229 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400230 char *name, int namelen,
231 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400232{
233 struct btrfs_trans_handle *trans;
234 struct btrfs_key key;
235 struct btrfs_root_item root_item;
236 struct btrfs_inode_item *inode_item;
237 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400238 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000239 struct dentry *parent = dget_parent(dentry);
240 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400241 int ret;
242 int err;
243 u64 objectid;
244 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500245 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400246
Yan, Zhenga22285a2010-05-16 10:48:46 -0400247 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
248 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000249 if (ret) {
250 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400251 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000252 }
253
254 dir = parent->d_inode;
255
Josef Bacik9ed74f22009-09-11 16:12:44 -0400256 /*
257 * 1 - inode item
258 * 2 - refs
259 * 1 - root item
260 * 2 - dir items
261 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400262 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000263 if (IS_ERR(trans)) {
264 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400265 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000266 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400267
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400268 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
269 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400270 if (IS_ERR(leaf)) {
271 ret = PTR_ERR(leaf);
272 goto fail;
273 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400274
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400276 btrfs_set_header_bytenr(leaf, leaf->start);
277 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400278 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400279 btrfs_set_header_owner(leaf, objectid);
280
281 write_extent_buffer(leaf, root->fs_info->fsid,
282 (unsigned long)btrfs_header_fsid(leaf),
283 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
285 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
286 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400287 btrfs_mark_buffer_dirty(leaf);
288
289 inode_item = &root_item.inode;
290 memset(inode_item, 0, sizeof(*inode_item));
291 inode_item->generation = cpu_to_le64(1);
292 inode_item->size = cpu_to_le64(3);
293 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400294 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400295 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
296
297 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400298 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400299 btrfs_set_root_level(&root_item, 0);
300 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000301 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400302 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400303
304 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
305 root_item.drop_level = 0;
306
Chris Mason925baed2008-06-25 16:01:30 -0400307 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400308 free_extent_buffer(leaf);
309 leaf = NULL;
310
311 btrfs_set_root_dirid(&root_item, new_dirid);
312
313 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400314 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400315 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
316 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
317 &root_item);
318 if (ret)
319 goto fail;
320
Yan, Zheng76dda932009-09-21 16:00:26 -0400321 key.offset = (u64)-1;
322 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
323 BUG_ON(IS_ERR(new_root));
324
325 btrfs_record_root_in_trans(trans, new_root);
326
327 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
328 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400329 /*
330 * insert the directory item
331 */
Chris Mason3de45862008-11-17 21:02:50 -0500332 ret = btrfs_set_inode_index(dir, &index);
333 BUG_ON(ret);
334
335 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400336 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500337 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400338 if (ret)
339 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500340
Yan Zheng52c26172009-01-05 15:43:43 -0500341 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
342 ret = btrfs_update_inode(trans, root, dir);
343 BUG_ON(ret);
344
Chris Mason0660b5a2008-11-17 20:37:39 -0500345 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400346 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500347 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400348
Chris Mason0660b5a2008-11-17 20:37:39 -0500349 BUG_ON(ret);
350
Yan, Zheng76dda932009-09-21 16:00:26 -0400351 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400352fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000353 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400354 if (async_transid) {
355 *async_transid = trans->transid;
356 err = btrfs_commit_transaction_async(trans, root, 1);
357 } else {
358 err = btrfs_commit_transaction(trans, root);
359 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400360 if (err && !ret)
361 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400362 return ret;
363}
364
Sage Weil72fd0322010-10-29 15:41:32 -0400365static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800366 char *name, int namelen, u64 *async_transid,
367 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000369 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000370 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 struct btrfs_pending_snapshot *pending_snapshot;
372 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000373 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400374
375 if (!root->ref_cows)
376 return -EINVAL;
377
Yan, Zhenga22285a2010-05-16 10:48:46 -0400378 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
379 if (!pending_snapshot)
380 return -ENOMEM;
381
382 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
383 pending_snapshot->dentry = dentry;
384 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800385 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400386
387 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
388 if (IS_ERR(trans)) {
389 ret = PTR_ERR(trans);
390 goto fail;
391 }
392
393 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
394 BUG_ON(ret);
395
396 list_add(&pending_snapshot->list,
397 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400398 if (async_transid) {
399 *async_transid = trans->transid;
400 ret = btrfs_commit_transaction_async(trans,
401 root->fs_info->extent_root, 1);
402 } else {
403 ret = btrfs_commit_transaction(trans,
404 root->fs_info->extent_root);
405 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400406 BUG_ON(ret);
407
408 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000410 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400411
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500412 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
413 if (ret)
414 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400415
Josef Bacik6a912212010-11-20 09:48:00 +0000416 parent = dget_parent(dentry);
417 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
418 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000419 if (IS_ERR(inode)) {
420 ret = PTR_ERR(inode);
421 goto fail;
422 }
423 BUG_ON(!inode);
424 d_instantiate(dentry, inode);
425 ret = 0;
426fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400427 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400428 return ret;
429}
430
Sage Weil4260f7c2010-10-29 15:46:43 -0400431/* copy of check_sticky in fs/namei.c()
432* It's inline, so penalty for filesystems that don't use sticky bit is
433* minimal.
434*/
435static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
436{
437 uid_t fsuid = current_fsuid();
438
439 if (!(dir->i_mode & S_ISVTX))
440 return 0;
441 if (inode->i_uid == fsuid)
442 return 0;
443 if (dir->i_uid == fsuid)
444 return 0;
445 return !capable(CAP_FOWNER);
446}
447
448/* copy of may_delete in fs/namei.c()
449 * Check whether we can remove a link victim from directory dir, check
450 * whether the type of victim is right.
451 * 1. We can't do it if dir is read-only (done in permission())
452 * 2. We should have write and exec permissions on dir
453 * 3. We can't remove anything from append-only dir
454 * 4. We can't do anything with immutable dir (done in permission())
455 * 5. If the sticky bit on dir is set we should either
456 * a. be owner of dir, or
457 * b. be owner of victim, or
458 * c. have CAP_FOWNER capability
459 * 6. If the victim is append-only or immutable we can't do antyhing with
460 * links pointing to it.
461 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
462 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
463 * 9. We can't remove a root or mountpoint.
464 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
465 * nfs_async_unlink().
466 */
467
468static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
469{
470 int error;
471
472 if (!victim->d_inode)
473 return -ENOENT;
474
475 BUG_ON(victim->d_parent->d_inode != dir);
476 audit_inode_child(victim, dir);
477
478 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
479 if (error)
480 return error;
481 if (IS_APPEND(dir))
482 return -EPERM;
483 if (btrfs_check_sticky(dir, victim->d_inode)||
484 IS_APPEND(victim->d_inode)||
485 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
486 return -EPERM;
487 if (isdir) {
488 if (!S_ISDIR(victim->d_inode->i_mode))
489 return -ENOTDIR;
490 if (IS_ROOT(victim))
491 return -EBUSY;
492 } else if (S_ISDIR(victim->d_inode->i_mode))
493 return -EISDIR;
494 if (IS_DEADDIR(dir))
495 return -ENOENT;
496 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
497 return -EBUSY;
498 return 0;
499}
500
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400501/* copy of may_create in fs/namei.c() */
502static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
503{
504 if (child->d_inode)
505 return -EEXIST;
506 if (IS_DEADDIR(dir))
507 return -ENOENT;
508 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
509}
510
511/*
512 * Create a new subvolume below @parent. This is largely modeled after
513 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
514 * inside this filesystem so it's quite a bit simpler.
515 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400516static noinline int btrfs_mksubvol(struct path *parent,
517 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400518 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800519 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400520{
Yan, Zheng76dda932009-09-21 16:00:26 -0400521 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400522 struct dentry *dentry;
523 int error;
524
Yan, Zheng76dda932009-09-21 16:00:26 -0400525 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400526
527 dentry = lookup_one_len(name, parent->dentry, namelen);
528 error = PTR_ERR(dentry);
529 if (IS_ERR(dentry))
530 goto out_unlock;
531
532 error = -EEXIST;
533 if (dentry->d_inode)
534 goto out_dput;
535
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400536 error = mnt_want_write(parent->mnt);
537 if (error)
538 goto out_dput;
539
Yan, Zheng76dda932009-09-21 16:00:26 -0400540 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400541 if (error)
542 goto out_drop_write;
543
Yan, Zheng76dda932009-09-21 16:00:26 -0400544 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
545
546 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
547 goto out_up_read;
548
Chris Mason3de45862008-11-17 21:02:50 -0500549 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400550 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800551 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500552 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400553 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400554 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500555 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400556 if (!error)
557 fsnotify_mkdir(dir, dentry);
558out_up_read:
559 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400560out_drop_write:
561 mnt_drop_write(parent->mnt);
562out_dput:
563 dput(dentry);
564out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400565 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400566 return error;
567}
568
Chris Mason940100a2010-03-10 10:52:59 -0500569static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500570 int thresh, u64 *last_len, u64 *skip,
571 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500572{
573 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
574 struct extent_map *em = NULL;
575 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
576 int ret = 1;
577
Chris Mason1e701a32010-03-11 09:42:04 -0500578
579 if (thresh == 0)
580 thresh = 256 * 1024;
581
Chris Mason940100a2010-03-10 10:52:59 -0500582 /*
583 * make sure that once we start defragging and extent, we keep on
584 * defragging it
585 */
586 if (start < *defrag_end)
587 return 1;
588
589 *skip = 0;
590
591 /*
592 * hopefully we have this extent in the tree already, try without
593 * the full extent lock
594 */
595 read_lock(&em_tree->lock);
596 em = lookup_extent_mapping(em_tree, start, len);
597 read_unlock(&em_tree->lock);
598
599 if (!em) {
600 /* get the big lock and read metadata off disk */
601 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
602 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
603 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
604
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000605 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500606 return 0;
607 }
608
609 /* this will cover holes, and inline extents */
610 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
611 ret = 0;
612
613 /*
614 * we hit a real extent, if it is big don't bother defragging it again
615 */
Chris Mason1e701a32010-03-11 09:42:04 -0500616 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500617 ret = 0;
618
619 /*
620 * last_len ends up being a counter of how many bytes we've defragged.
621 * every time we choose not to defrag an extent, we reset *last_len
622 * so that the next tiny extent will force a defrag.
623 *
624 * The end result of this is that tiny extents before a single big
625 * extent will force at least part of that big extent to be defragged.
626 */
627 if (ret) {
628 *last_len += len;
629 *defrag_end = extent_map_end(em);
630 } else {
631 *last_len = 0;
632 *skip = extent_map_end(em);
633 *defrag_end = 0;
634 }
635
636 free_extent_map(em);
637 return ret;
638}
639
Chris Mason1e701a32010-03-11 09:42:04 -0500640static int btrfs_defrag_file(struct file *file,
641 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400642{
643 struct inode *inode = fdentry(file)->d_inode;
644 struct btrfs_root *root = BTRFS_I(inode)->root;
645 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400646 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400647 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800648 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400649 unsigned long last_index;
650 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
651 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800652 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653 u64 page_start;
654 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500655 u64 last_len = 0;
656 u64 skip = 0;
657 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400658 unsigned long i;
659 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800660 int compress_type = BTRFS_COMPRESS_ZLIB;
661
662 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
663 if (range->compress_type > BTRFS_COMPRESS_TYPES)
664 return -EINVAL;
665 if (range->compress_type)
666 compress_type = range->compress_type;
667 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400668
Chris Mason940100a2010-03-10 10:52:59 -0500669 if (inode->i_size == 0)
670 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400671
Chris Mason1e701a32010-03-11 09:42:04 -0500672 if (range->start + range->len > range->start) {
673 last_index = min_t(u64, inode->i_size - 1,
674 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
675 } else {
676 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
677 }
678
679 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500680 while (i <= last_index) {
681 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500682 PAGE_CACHE_SIZE,
683 range->extent_thresh,
684 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500685 &defrag_end)) {
686 unsigned long next;
687 /*
688 * the should_defrag function tells us how much to skip
689 * bump our counter by the suggested amount
690 */
691 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
692 i = max(i + 1, next);
693 continue;
694 }
695
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400696 if (total_read % ra_pages == 0) {
697 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
698 min(last_index, i + ra_pages - 1));
699 }
700 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500701 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500702 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800703 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500704
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400705 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
706 if (ret)
707 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400708again:
Chris Mason940100a2010-03-10 10:52:59 -0500709 if (inode->i_size == 0 ||
710 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
711 ret = 0;
712 goto err_reservations;
713 }
714
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400715 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400716 if (!page) {
717 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500718 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400719 }
Chris Mason940100a2010-03-10 10:52:59 -0500720
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400721 if (!PageUptodate(page)) {
722 btrfs_readpage(NULL, page);
723 lock_page(page);
724 if (!PageUptodate(page)) {
725 unlock_page(page);
726 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400727 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500728 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729 }
730 }
731
Chris Mason940100a2010-03-10 10:52:59 -0500732 if (page->mapping != inode->i_mapping) {
733 unlock_page(page);
734 page_cache_release(page);
735 goto again;
736 }
737
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400739
Chris Mason940100a2010-03-10 10:52:59 -0500740 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400741 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500742 goto loop_unlock;
743 }
744
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
746 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400748
749 ordered = btrfs_lookup_ordered_extent(inode, page_start);
750 if (ordered) {
751 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
752 unlock_page(page);
753 page_cache_release(page);
754 btrfs_start_ordered_extent(inode, ordered, 1);
755 btrfs_put_ordered_extent(ordered);
756 goto again;
757 }
758 set_page_extent_mapped(page);
759
Chris Masonf87f0572008-08-01 11:27:23 -0400760 /*
761 * this makes sure page_mkwrite is called on the
762 * page if it is dirtied again later
763 */
764 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500765 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
766 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
767 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400768
Josef Bacik2ac55d42010-02-03 19:33:23 +0000769 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500770 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400771 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400772 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500773
774loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400775 unlock_page(page);
776 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500777 mutex_unlock(&inode->i_mutex);
778
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400779 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500780 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400781 }
782
Chris Mason1e701a32010-03-11 09:42:04 -0500783 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
784 filemap_flush(inode->i_mapping);
785
786 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
787 /* the filemap_flush will queue IO into the worker threads, but
788 * we have to make sure the IO is actually started and that
789 * ordered extents get created before we return
790 */
791 atomic_inc(&root->fs_info->async_submit_draining);
792 while (atomic_read(&root->fs_info->nr_async_submits) ||
793 atomic_read(&root->fs_info->async_delalloc_pages)) {
794 wait_event(root->fs_info->async_submit_wait,
795 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
796 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
797 }
798 atomic_dec(&root->fs_info->async_submit_draining);
799
800 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800801 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500802 mutex_unlock(&inode->i_mutex);
803 }
804
Li Zefan1a419d82010-10-25 15:12:50 +0800805 disk_super = &root->fs_info->super_copy;
806 features = btrfs_super_incompat_flags(disk_super);
807 if (range->compress_type == BTRFS_COMPRESS_LZO) {
808 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
809 btrfs_set_super_incompat_flags(disk_super, features);
810 }
811
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400812 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500813
814err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400815 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
816err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500817 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500818 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400819}
820
Yan, Zheng76dda932009-09-21 16:00:26 -0400821static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
822 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400823{
824 u64 new_size;
825 u64 old_size;
826 u64 devid = 1;
827 struct btrfs_ioctl_vol_args *vol_args;
828 struct btrfs_trans_handle *trans;
829 struct btrfs_device *device = NULL;
830 char *sizestr;
831 char *devstr = NULL;
832 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400833 int mod = 0;
834
Yan Zhengc146afa2008-11-12 14:34:12 -0500835 if (root->fs_info->sb->s_flags & MS_RDONLY)
836 return -EROFS;
837
Chris Masone441d542009-01-05 16:57:23 -0500838 if (!capable(CAP_SYS_ADMIN))
839 return -EPERM;
840
Li Zefandae7b662009-04-08 15:06:54 +0800841 vol_args = memdup_user(arg, sizeof(*vol_args));
842 if (IS_ERR(vol_args))
843 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400844
845 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400846
Chris Mason7d9eb122008-07-08 14:19:17 -0400847 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400848 sizestr = vol_args->name;
849 devstr = strchr(sizestr, ':');
850 if (devstr) {
851 char *end;
852 sizestr = devstr + 1;
853 *devstr = '\0';
854 devstr = vol_args->name;
855 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700856 printk(KERN_INFO "resizing devid %llu\n",
857 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 }
Yan Zheng2b820322008-11-17 21:11:30 -0500859 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400860 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700861 printk(KERN_INFO "resizer unable to find device %llu\n",
862 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400863 ret = -EINVAL;
864 goto out_unlock;
865 }
866 if (!strcmp(sizestr, "max"))
867 new_size = device->bdev->bd_inode->i_size;
868 else {
869 if (sizestr[0] == '-') {
870 mod = -1;
871 sizestr++;
872 } else if (sizestr[0] == '+') {
873 mod = 1;
874 sizestr++;
875 }
Akinobu Mita91748462010-02-28 10:59:11 +0000876 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400877 if (new_size == 0) {
878 ret = -EINVAL;
879 goto out_unlock;
880 }
881 }
882
883 old_size = device->total_bytes;
884
885 if (mod < 0) {
886 if (new_size > old_size) {
887 ret = -EINVAL;
888 goto out_unlock;
889 }
890 new_size = old_size - new_size;
891 } else if (mod > 0) {
892 new_size = old_size + new_size;
893 }
894
895 if (new_size < 256 * 1024 * 1024) {
896 ret = -EINVAL;
897 goto out_unlock;
898 }
899 if (new_size > device->bdev->bd_inode->i_size) {
900 ret = -EFBIG;
901 goto out_unlock;
902 }
903
904 do_div(new_size, root->sectorsize);
905 new_size *= root->sectorsize;
906
907 printk(KERN_INFO "new size for %s is %llu\n",
908 device->name, (unsigned long long)new_size);
909
910 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400911 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +0000912 if (IS_ERR(trans)) {
913 ret = PTR_ERR(trans);
914 goto out_unlock;
915 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400916 ret = btrfs_grow_device(trans, device, new_size);
917 btrfs_commit_transaction(trans, root);
918 } else {
919 ret = btrfs_shrink_device(device, new_size);
920 }
921
922out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400923 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400924 kfree(vol_args);
925 return ret;
926}
927
Sage Weil72fd0322010-10-29 15:41:32 -0400928static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
929 char *name,
930 unsigned long fd,
931 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +0800932 u64 *transid,
933 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400934{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400935 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -0500936 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400937 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500938 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400939
Yan Zhengc146afa2008-11-12 14:34:12 -0500940 if (root->fs_info->sb->s_flags & MS_RDONLY)
941 return -EROFS;
942
Sage Weil72fd0322010-10-29 15:41:32 -0400943 namelen = strlen(name);
944 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400945 ret = -EINVAL;
946 goto out;
947 }
948
Chris Mason3de45862008-11-17 21:02:50 -0500949 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -0400950 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +0800951 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400952 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500953 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -0400954 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -0500955 if (!src_file) {
956 ret = -EINVAL;
957 goto out;
958 }
959
960 src_inode = src_file->f_path.dentry->d_inode;
961 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500962 printk(KERN_INFO "btrfs: Snapshot src from "
963 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500964 ret = -EINVAL;
965 fput(src_file);
966 goto out;
967 }
Sage Weil72fd0322010-10-29 15:41:32 -0400968 ret = btrfs_mksubvol(&file->f_path, name, namelen,
969 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +0800970 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500971 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400972 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400973out:
Sage Weil72fd0322010-10-29 15:41:32 -0400974 return ret;
975}
976
977static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +0800978 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -0400979{
Li Zefanfa0d2b92010-12-20 15:53:28 +0800980 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -0400981 int ret;
982
Li Zefanfa0d2b92010-12-20 15:53:28 +0800983 vol_args = memdup_user(arg, sizeof(*vol_args));
984 if (IS_ERR(vol_args))
985 return PTR_ERR(vol_args);
986 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -0400987
Li Zefanfa0d2b92010-12-20 15:53:28 +0800988 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +0800989 vol_args->fd, subvol,
990 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +0000991
Li Zefanfa0d2b92010-12-20 15:53:28 +0800992 kfree(vol_args);
993 return ret;
994}
Li Zefanfdfb1e42010-12-10 06:41:56 +0000995
Li Zefanfa0d2b92010-12-20 15:53:28 +0800996static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
997 void __user *arg, int subvol)
998{
999 struct btrfs_ioctl_vol_args_v2 *vol_args;
1000 int ret;
1001 u64 transid = 0;
1002 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001003 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001004
Li Zefanfa0d2b92010-12-20 15:53:28 +08001005 vol_args = memdup_user(arg, sizeof(*vol_args));
1006 if (IS_ERR(vol_args))
1007 return PTR_ERR(vol_args);
1008 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001009
Li Zefanb83cc962010-12-20 16:04:08 +08001010 if (vol_args->flags &
1011 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1012 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001013 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001014 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001015
1016 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1017 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001018 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1019 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001020
1021 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001022 vol_args->fd, subvol,
1023 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001024
1025 if (ret == 0 && ptr &&
1026 copy_to_user(arg +
1027 offsetof(struct btrfs_ioctl_vol_args_v2,
1028 transid), ptr, sizeof(*ptr)))
1029 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001030out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001031 kfree(vol_args);
1032 return ret;
1033}
1034
Li Zefan0caa1022010-12-20 16:30:25 +08001035static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1036 void __user *arg)
1037{
1038 struct inode *inode = fdentry(file)->d_inode;
1039 struct btrfs_root *root = BTRFS_I(inode)->root;
1040 int ret = 0;
1041 u64 flags = 0;
1042
1043 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1044 return -EINVAL;
1045
1046 down_read(&root->fs_info->subvol_sem);
1047 if (btrfs_root_readonly(root))
1048 flags |= BTRFS_SUBVOL_RDONLY;
1049 up_read(&root->fs_info->subvol_sem);
1050
1051 if (copy_to_user(arg, &flags, sizeof(flags)))
1052 ret = -EFAULT;
1053
1054 return ret;
1055}
1056
1057static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1058 void __user *arg)
1059{
1060 struct inode *inode = fdentry(file)->d_inode;
1061 struct btrfs_root *root = BTRFS_I(inode)->root;
1062 struct btrfs_trans_handle *trans;
1063 u64 root_flags;
1064 u64 flags;
1065 int ret = 0;
1066
1067 if (root->fs_info->sb->s_flags & MS_RDONLY)
1068 return -EROFS;
1069
1070 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1071 return -EINVAL;
1072
1073 if (copy_from_user(&flags, arg, sizeof(flags)))
1074 return -EFAULT;
1075
Li Zefanb4dc2b82011-02-16 06:06:34 +00001076 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001077 return -EINVAL;
1078
1079 if (flags & ~BTRFS_SUBVOL_RDONLY)
1080 return -EOPNOTSUPP;
1081
Li Zefanb4dc2b82011-02-16 06:06:34 +00001082 if (!is_owner_or_cap(inode))
1083 return -EACCES;
1084
Li Zefan0caa1022010-12-20 16:30:25 +08001085 down_write(&root->fs_info->subvol_sem);
1086
1087 /* nothing to do */
1088 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1089 goto out;
1090
1091 root_flags = btrfs_root_flags(&root->root_item);
1092 if (flags & BTRFS_SUBVOL_RDONLY)
1093 btrfs_set_root_flags(&root->root_item,
1094 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1095 else
1096 btrfs_set_root_flags(&root->root_item,
1097 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1098
1099 trans = btrfs_start_transaction(root, 1);
1100 if (IS_ERR(trans)) {
1101 ret = PTR_ERR(trans);
1102 goto out_reset;
1103 }
1104
Li Zefanb4dc2b82011-02-16 06:06:34 +00001105 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001106 &root->root_key, &root->root_item);
1107
1108 btrfs_commit_transaction(trans, root);
1109out_reset:
1110 if (ret)
1111 btrfs_set_root_flags(&root->root_item, root_flags);
1112out:
1113 up_write(&root->fs_info->subvol_sem);
1114 return ret;
1115}
1116
Yan, Zheng76dda932009-09-21 16:00:26 -04001117/*
1118 * helper to check if the subvolume references other subvolumes
1119 */
1120static noinline int may_destroy_subvol(struct btrfs_root *root)
1121{
1122 struct btrfs_path *path;
1123 struct btrfs_key key;
1124 int ret;
1125
1126 path = btrfs_alloc_path();
1127 if (!path)
1128 return -ENOMEM;
1129
1130 key.objectid = root->root_key.objectid;
1131 key.type = BTRFS_ROOT_REF_KEY;
1132 key.offset = (u64)-1;
1133
1134 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1135 &key, path, 0, 0);
1136 if (ret < 0)
1137 goto out;
1138 BUG_ON(ret == 0);
1139
1140 ret = 0;
1141 if (path->slots[0] > 0) {
1142 path->slots[0]--;
1143 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1144 if (key.objectid == root->root_key.objectid &&
1145 key.type == BTRFS_ROOT_REF_KEY)
1146 ret = -ENOTEMPTY;
1147 }
1148out:
1149 btrfs_free_path(path);
1150 return ret;
1151}
1152
Chris Masonac8e9812010-02-28 15:39:26 -05001153static noinline int key_in_sk(struct btrfs_key *key,
1154 struct btrfs_ioctl_search_key *sk)
1155{
Chris Masonabc6e132010-03-18 12:10:08 -04001156 struct btrfs_key test;
1157 int ret;
1158
1159 test.objectid = sk->min_objectid;
1160 test.type = sk->min_type;
1161 test.offset = sk->min_offset;
1162
1163 ret = btrfs_comp_cpu_keys(key, &test);
1164 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001165 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001166
1167 test.objectid = sk->max_objectid;
1168 test.type = sk->max_type;
1169 test.offset = sk->max_offset;
1170
1171 ret = btrfs_comp_cpu_keys(key, &test);
1172 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001173 return 0;
1174 return 1;
1175}
1176
1177static noinline int copy_to_sk(struct btrfs_root *root,
1178 struct btrfs_path *path,
1179 struct btrfs_key *key,
1180 struct btrfs_ioctl_search_key *sk,
1181 char *buf,
1182 unsigned long *sk_offset,
1183 int *num_found)
1184{
1185 u64 found_transid;
1186 struct extent_buffer *leaf;
1187 struct btrfs_ioctl_search_header sh;
1188 unsigned long item_off;
1189 unsigned long item_len;
1190 int nritems;
1191 int i;
1192 int slot;
1193 int found = 0;
1194 int ret = 0;
1195
1196 leaf = path->nodes[0];
1197 slot = path->slots[0];
1198 nritems = btrfs_header_nritems(leaf);
1199
1200 if (btrfs_header_generation(leaf) > sk->max_transid) {
1201 i = nritems;
1202 goto advance_key;
1203 }
1204 found_transid = btrfs_header_generation(leaf);
1205
1206 for (i = slot; i < nritems; i++) {
1207 item_off = btrfs_item_ptr_offset(leaf, i);
1208 item_len = btrfs_item_size_nr(leaf, i);
1209
1210 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1211 item_len = 0;
1212
1213 if (sizeof(sh) + item_len + *sk_offset >
1214 BTRFS_SEARCH_ARGS_BUFSIZE) {
1215 ret = 1;
1216 goto overflow;
1217 }
1218
1219 btrfs_item_key_to_cpu(leaf, key, i);
1220 if (!key_in_sk(key, sk))
1221 continue;
1222
1223 sh.objectid = key->objectid;
1224 sh.offset = key->offset;
1225 sh.type = key->type;
1226 sh.len = item_len;
1227 sh.transid = found_transid;
1228
1229 /* copy search result header */
1230 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1231 *sk_offset += sizeof(sh);
1232
1233 if (item_len) {
1234 char *p = buf + *sk_offset;
1235 /* copy the item */
1236 read_extent_buffer(leaf, p,
1237 item_off, item_len);
1238 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001239 }
Chris Mason90fdde12010-03-18 12:14:54 -04001240 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001241
1242 if (*num_found >= sk->nr_items)
1243 break;
1244 }
1245advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001246 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001247 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1248 key->offset++;
1249 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1250 key->offset = 0;
1251 key->type++;
1252 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1253 key->offset = 0;
1254 key->type = 0;
1255 key->objectid++;
1256 } else
1257 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001258overflow:
1259 *num_found += found;
1260 return ret;
1261}
1262
1263static noinline int search_ioctl(struct inode *inode,
1264 struct btrfs_ioctl_search_args *args)
1265{
1266 struct btrfs_root *root;
1267 struct btrfs_key key;
1268 struct btrfs_key max_key;
1269 struct btrfs_path *path;
1270 struct btrfs_ioctl_search_key *sk = &args->key;
1271 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1272 int ret;
1273 int num_found = 0;
1274 unsigned long sk_offset = 0;
1275
1276 path = btrfs_alloc_path();
1277 if (!path)
1278 return -ENOMEM;
1279
1280 if (sk->tree_id == 0) {
1281 /* search the root of the inode that was passed */
1282 root = BTRFS_I(inode)->root;
1283 } else {
1284 key.objectid = sk->tree_id;
1285 key.type = BTRFS_ROOT_ITEM_KEY;
1286 key.offset = (u64)-1;
1287 root = btrfs_read_fs_root_no_name(info, &key);
1288 if (IS_ERR(root)) {
1289 printk(KERN_ERR "could not find root %llu\n",
1290 sk->tree_id);
1291 btrfs_free_path(path);
1292 return -ENOENT;
1293 }
1294 }
1295
1296 key.objectid = sk->min_objectid;
1297 key.type = sk->min_type;
1298 key.offset = sk->min_offset;
1299
1300 max_key.objectid = sk->max_objectid;
1301 max_key.type = sk->max_type;
1302 max_key.offset = sk->max_offset;
1303
1304 path->keep_locks = 1;
1305
1306 while(1) {
1307 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1308 sk->min_transid);
1309 if (ret != 0) {
1310 if (ret > 0)
1311 ret = 0;
1312 goto err;
1313 }
1314 ret = copy_to_sk(root, path, &key, sk, args->buf,
1315 &sk_offset, &num_found);
1316 btrfs_release_path(root, path);
1317 if (ret || num_found >= sk->nr_items)
1318 break;
1319
1320 }
1321 ret = 0;
1322err:
1323 sk->nr_items = num_found;
1324 btrfs_free_path(path);
1325 return ret;
1326}
1327
1328static noinline int btrfs_ioctl_tree_search(struct file *file,
1329 void __user *argp)
1330{
1331 struct btrfs_ioctl_search_args *args;
1332 struct inode *inode;
1333 int ret;
1334
1335 if (!capable(CAP_SYS_ADMIN))
1336 return -EPERM;
1337
Julia Lawall2354d082010-10-29 15:14:18 -04001338 args = memdup_user(argp, sizeof(*args));
1339 if (IS_ERR(args))
1340 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001341
Chris Masonac8e9812010-02-28 15:39:26 -05001342 inode = fdentry(file)->d_inode;
1343 ret = search_ioctl(inode, args);
1344 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1345 ret = -EFAULT;
1346 kfree(args);
1347 return ret;
1348}
1349
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001350/*
Chris Masonac8e9812010-02-28 15:39:26 -05001351 * Search INODE_REFs to identify path name of 'dirid' directory
1352 * in a 'tree_id' tree. and sets path name to 'name'.
1353 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001354static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1355 u64 tree_id, u64 dirid, char *name)
1356{
1357 struct btrfs_root *root;
1358 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001359 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001360 int ret = -1;
1361 int slot;
1362 int len;
1363 int total_len = 0;
1364 struct btrfs_inode_ref *iref;
1365 struct extent_buffer *l;
1366 struct btrfs_path *path;
1367
1368 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1369 name[0]='\0';
1370 return 0;
1371 }
1372
1373 path = btrfs_alloc_path();
1374 if (!path)
1375 return -ENOMEM;
1376
Chris Masonac8e9812010-02-28 15:39:26 -05001377 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001378
1379 key.objectid = tree_id;
1380 key.type = BTRFS_ROOT_ITEM_KEY;
1381 key.offset = (u64)-1;
1382 root = btrfs_read_fs_root_no_name(info, &key);
1383 if (IS_ERR(root)) {
1384 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001385 ret = -ENOENT;
1386 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001387 }
1388
1389 key.objectid = dirid;
1390 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001391 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001392
1393 while(1) {
1394 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1395 if (ret < 0)
1396 goto out;
1397
1398 l = path->nodes[0];
1399 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001400 if (ret > 0 && slot > 0)
1401 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001402 btrfs_item_key_to_cpu(l, &key, slot);
1403
1404 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001405 key.type != BTRFS_INODE_REF_KEY)) {
1406 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001407 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001408 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001409
1410 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1411 len = btrfs_inode_ref_name_len(l, iref);
1412 ptr -= len + 1;
1413 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001414 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001415 goto out;
1416
1417 *(ptr + len) = '/';
1418 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1419
1420 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1421 break;
1422
1423 btrfs_release_path(root, path);
1424 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001425 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001426 dirid = key.objectid;
1427
1428 }
Chris Masonac8e9812010-02-28 15:39:26 -05001429 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001430 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001431 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001432 name[total_len]='\0';
1433 ret = 0;
1434out:
1435 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001436 return ret;
1437}
1438
1439static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1440 void __user *argp)
1441{
1442 struct btrfs_ioctl_ino_lookup_args *args;
1443 struct inode *inode;
1444 int ret;
1445
1446 if (!capable(CAP_SYS_ADMIN))
1447 return -EPERM;
1448
Julia Lawall2354d082010-10-29 15:14:18 -04001449 args = memdup_user(argp, sizeof(*args));
1450 if (IS_ERR(args))
1451 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001452
Chris Masonac8e9812010-02-28 15:39:26 -05001453 inode = fdentry(file)->d_inode;
1454
Chris Mason1b53ac42010-03-18 12:17:05 -04001455 if (args->treeid == 0)
1456 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1457
Chris Masonac8e9812010-02-28 15:39:26 -05001458 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1459 args->treeid, args->objectid,
1460 args->name);
1461
1462 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1463 ret = -EFAULT;
1464
1465 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001466 return ret;
1467}
1468
Yan, Zheng76dda932009-09-21 16:00:26 -04001469static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1470 void __user *arg)
1471{
1472 struct dentry *parent = fdentry(file);
1473 struct dentry *dentry;
1474 struct inode *dir = parent->d_inode;
1475 struct inode *inode;
1476 struct btrfs_root *root = BTRFS_I(dir)->root;
1477 struct btrfs_root *dest = NULL;
1478 struct btrfs_ioctl_vol_args *vol_args;
1479 struct btrfs_trans_handle *trans;
1480 int namelen;
1481 int ret;
1482 int err = 0;
1483
Yan, Zheng76dda932009-09-21 16:00:26 -04001484 vol_args = memdup_user(arg, sizeof(*vol_args));
1485 if (IS_ERR(vol_args))
1486 return PTR_ERR(vol_args);
1487
1488 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1489 namelen = strlen(vol_args->name);
1490 if (strchr(vol_args->name, '/') ||
1491 strncmp(vol_args->name, "..", namelen) == 0) {
1492 err = -EINVAL;
1493 goto out;
1494 }
1495
1496 err = mnt_want_write(file->f_path.mnt);
1497 if (err)
1498 goto out;
1499
1500 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1501 dentry = lookup_one_len(vol_args->name, parent, namelen);
1502 if (IS_ERR(dentry)) {
1503 err = PTR_ERR(dentry);
1504 goto out_unlock_dir;
1505 }
1506
1507 if (!dentry->d_inode) {
1508 err = -ENOENT;
1509 goto out_dput;
1510 }
1511
1512 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001513 dest = BTRFS_I(inode)->root;
1514 if (!capable(CAP_SYS_ADMIN)){
1515 /*
1516 * Regular user. Only allow this with a special mount
1517 * option, when the user has write+exec access to the
1518 * subvol root, and when rmdir(2) would have been
1519 * allowed.
1520 *
1521 * Note that this is _not_ check that the subvol is
1522 * empty or doesn't contain data that we wouldn't
1523 * otherwise be able to delete.
1524 *
1525 * Users who want to delete empty subvols should try
1526 * rmdir(2).
1527 */
1528 err = -EPERM;
1529 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1530 goto out_dput;
1531
1532 /*
1533 * Do not allow deletion if the parent dir is the same
1534 * as the dir to be deleted. That means the ioctl
1535 * must be called on the dentry referencing the root
1536 * of the subvol, not a random directory contained
1537 * within it.
1538 */
1539 err = -EINVAL;
1540 if (root == dest)
1541 goto out_dput;
1542
1543 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1544 if (err)
1545 goto out_dput;
1546
1547 /* check if subvolume may be deleted by a non-root user */
1548 err = btrfs_may_delete(dir, dentry, 1);
1549 if (err)
1550 goto out_dput;
1551 }
1552
Yan, Zheng76dda932009-09-21 16:00:26 -04001553 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1554 err = -EINVAL;
1555 goto out_dput;
1556 }
1557
Yan, Zheng76dda932009-09-21 16:00:26 -04001558 mutex_lock(&inode->i_mutex);
1559 err = d_invalidate(dentry);
1560 if (err)
1561 goto out_unlock;
1562
1563 down_write(&root->fs_info->subvol_sem);
1564
1565 err = may_destroy_subvol(dest);
1566 if (err)
1567 goto out_up_write;
1568
Yan, Zhenga22285a2010-05-16 10:48:46 -04001569 trans = btrfs_start_transaction(root, 0);
1570 if (IS_ERR(trans)) {
1571 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001572 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001573 }
1574 trans->block_rsv = &root->fs_info->global_block_rsv;
1575
Yan, Zheng76dda932009-09-21 16:00:26 -04001576 ret = btrfs_unlink_subvol(trans, root, dir,
1577 dest->root_key.objectid,
1578 dentry->d_name.name,
1579 dentry->d_name.len);
1580 BUG_ON(ret);
1581
1582 btrfs_record_root_in_trans(trans, dest);
1583
1584 memset(&dest->root_item.drop_progress, 0,
1585 sizeof(dest->root_item.drop_progress));
1586 dest->root_item.drop_level = 0;
1587 btrfs_set_root_refs(&dest->root_item, 0);
1588
Yan, Zhengd68fc572010-05-16 10:49:58 -04001589 if (!xchg(&dest->orphan_item_inserted, 1)) {
1590 ret = btrfs_insert_orphan_item(trans,
1591 root->fs_info->tree_root,
1592 dest->root_key.objectid);
1593 BUG_ON(ret);
1594 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001595
Sage Weil531cb132010-10-29 15:41:32 -04001596 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001597 BUG_ON(ret);
1598 inode->i_flags |= S_DEAD;
1599out_up_write:
1600 up_write(&root->fs_info->subvol_sem);
1601out_unlock:
1602 mutex_unlock(&inode->i_mutex);
1603 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001604 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001605 btrfs_invalidate_inodes(dest);
1606 d_delete(dentry);
1607 }
1608out_dput:
1609 dput(dentry);
1610out_unlock_dir:
1611 mutex_unlock(&dir->i_mutex);
1612 mnt_drop_write(file->f_path.mnt);
1613out:
1614 kfree(vol_args);
1615 return err;
1616}
1617
Chris Mason1e701a32010-03-11 09:42:04 -05001618static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001619{
1620 struct inode *inode = fdentry(file)->d_inode;
1621 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001622 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001623 int ret;
1624
Li Zefanb83cc962010-12-20 16:04:08 +08001625 if (btrfs_root_readonly(root))
1626 return -EROFS;
1627
Yan Zhengc146afa2008-11-12 14:34:12 -05001628 ret = mnt_want_write(file->f_path.mnt);
1629 if (ret)
1630 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001631
1632 switch (inode->i_mode & S_IFMT) {
1633 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001634 if (!capable(CAP_SYS_ADMIN)) {
1635 ret = -EPERM;
1636 goto out;
1637 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001638 ret = btrfs_defrag_root(root, 0);
1639 if (ret)
1640 goto out;
1641 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001642 break;
1643 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001644 if (!(file->f_mode & FMODE_WRITE)) {
1645 ret = -EINVAL;
1646 goto out;
1647 }
Chris Mason1e701a32010-03-11 09:42:04 -05001648
1649 range = kzalloc(sizeof(*range), GFP_KERNEL);
1650 if (!range) {
1651 ret = -ENOMEM;
1652 goto out;
1653 }
1654
1655 if (argp) {
1656 if (copy_from_user(range, argp,
1657 sizeof(*range))) {
1658 ret = -EFAULT;
1659 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001660 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001661 }
1662 /* compression requires us to start the IO */
1663 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1664 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1665 range->extent_thresh = (u32)-1;
1666 }
1667 } else {
1668 /* the rest are all set to zero by kzalloc */
1669 range->len = (u64)-1;
1670 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001671 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001672 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001673 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001674 default:
1675 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001676 }
Chris Masone441d542009-01-05 16:57:23 -05001677out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001678 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001679 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001680}
1681
Christoph Hellwigb2950862008-12-02 09:54:17 -05001682static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001683{
1684 struct btrfs_ioctl_vol_args *vol_args;
1685 int ret;
1686
Chris Masone441d542009-01-05 16:57:23 -05001687 if (!capable(CAP_SYS_ADMIN))
1688 return -EPERM;
1689
Li Zefandae7b662009-04-08 15:06:54 +08001690 vol_args = memdup_user(arg, sizeof(*vol_args));
1691 if (IS_ERR(vol_args))
1692 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693
Mark Fasheh5516e592008-07-24 12:20:14 -04001694 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001695 ret = btrfs_init_new_device(root, vol_args->name);
1696
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001697 kfree(vol_args);
1698 return ret;
1699}
1700
Christoph Hellwigb2950862008-12-02 09:54:17 -05001701static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001702{
1703 struct btrfs_ioctl_vol_args *vol_args;
1704 int ret;
1705
Chris Masone441d542009-01-05 16:57:23 -05001706 if (!capable(CAP_SYS_ADMIN))
1707 return -EPERM;
1708
Yan Zhengc146afa2008-11-12 14:34:12 -05001709 if (root->fs_info->sb->s_flags & MS_RDONLY)
1710 return -EROFS;
1711
Li Zefandae7b662009-04-08 15:06:54 +08001712 vol_args = memdup_user(arg, sizeof(*vol_args));
1713 if (IS_ERR(vol_args))
1714 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001715
Mark Fasheh5516e592008-07-24 12:20:14 -04001716 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001717 ret = btrfs_rm_device(root, vol_args->name);
1718
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719 kfree(vol_args);
1720 return ret;
1721}
1722
Yan, Zheng76dda932009-09-21 16:00:26 -04001723static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1724 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001725{
1726 struct inode *inode = fdentry(file)->d_inode;
1727 struct btrfs_root *root = BTRFS_I(inode)->root;
1728 struct file *src_file;
1729 struct inode *src;
1730 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001731 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001732 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001733 char *buf;
1734 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001735 u32 nritems;
1736 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001737 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001738 u64 len = olen;
1739 u64 bs = root->fs_info->sb->s_blocksize;
1740 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001741
Sage Weilc5c9cd42008-11-12 14:32:25 -05001742 /*
1743 * TODO:
1744 * - split compressed inline extents. annoying: we need to
1745 * decompress into destination's address_space (the file offset
1746 * may change, so source mapping won't do), then recompress (or
1747 * otherwise reinsert) a subrange.
1748 * - allow ranges within the same file to be cloned (provided
1749 * they don't overlap)?
1750 */
1751
Chris Masone441d542009-01-05 16:57:23 -05001752 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001753 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001754 return -EINVAL;
1755
Li Zefanb83cc962010-12-20 16:04:08 +08001756 if (btrfs_root_readonly(root))
1757 return -EROFS;
1758
Yan Zhengc146afa2008-11-12 14:34:12 -05001759 ret = mnt_want_write(file->f_path.mnt);
1760 if (ret)
1761 return ret;
1762
Sage Weilc5c9cd42008-11-12 14:32:25 -05001763 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001764 if (!src_file) {
1765 ret = -EBADF;
1766 goto out_drop_write;
1767 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001768
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001769 src = src_file->f_dentry->d_inode;
1770
Sage Weilc5c9cd42008-11-12 14:32:25 -05001771 ret = -EINVAL;
1772 if (src == inode)
1773 goto out_fput;
1774
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001775 /* the src must be open for reading */
1776 if (!(src_file->f_mode & FMODE_READ))
1777 goto out_fput;
1778
Yan Zhengae01a0a2008-08-04 23:23:47 -04001779 ret = -EISDIR;
1780 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001781 goto out_fput;
1782
Yan Zhengae01a0a2008-08-04 23:23:47 -04001783 ret = -EXDEV;
1784 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1785 goto out_fput;
1786
1787 ret = -ENOMEM;
1788 buf = vmalloc(btrfs_level_size(root, 0));
1789 if (!buf)
1790 goto out_fput;
1791
1792 path = btrfs_alloc_path();
1793 if (!path) {
1794 vfree(buf);
1795 goto out_fput;
1796 }
1797 path->reada = 2;
1798
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001799 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001800 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1801 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001802 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001803 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1804 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001805 }
1806
Sage Weilc5c9cd42008-11-12 14:32:25 -05001807 /* determine range to clone */
1808 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001809 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001810 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001811 if (len == 0)
1812 olen = len = src->i_size - off;
1813 /* if we extend to eof, continue to block boundary */
1814 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001815 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001816
1817 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001818 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1819 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001820 goto out_unlock;
1821
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001822 /* do any pending delalloc/csum calc on src, one way or
1823 another, and lock file content */
1824 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001825 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001826 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001827 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1828 if (!ordered &&
1829 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1830 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001831 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001832 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001833 if (ordered)
1834 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001835 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001836 }
1837
Sage Weilc5c9cd42008-11-12 14:32:25 -05001838 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001839 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001840 key.type = BTRFS_EXTENT_DATA_KEY;
1841 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001842
1843 while (1) {
1844 /*
1845 * note the key will change type as we walk through the
1846 * tree.
1847 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001848 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001849 if (ret < 0)
1850 goto out;
1851
Yan Zhengae01a0a2008-08-04 23:23:47 -04001852 nritems = btrfs_header_nritems(path->nodes[0]);
1853 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001854 ret = btrfs_next_leaf(root, path);
1855 if (ret < 0)
1856 goto out;
1857 if (ret > 0)
1858 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001859 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001860 }
1861 leaf = path->nodes[0];
1862 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001863
Yan Zhengae01a0a2008-08-04 23:23:47 -04001864 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001865 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001866 key.objectid != src->i_ino)
1867 break;
1868
Sage Weilc5c9cd42008-11-12 14:32:25 -05001869 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1870 struct btrfs_file_extent_item *extent;
1871 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001872 u32 size;
1873 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001874 u64 disko = 0, diskl = 0;
1875 u64 datao = 0, datal = 0;
1876 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001877 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001878
1879 size = btrfs_item_size_nr(leaf, slot);
1880 read_extent_buffer(leaf, buf,
1881 btrfs_item_ptr_offset(leaf, slot),
1882 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001883
1884 extent = btrfs_item_ptr(leaf, slot,
1885 struct btrfs_file_extent_item);
1886 comp = btrfs_file_extent_compression(leaf, extent);
1887 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001888 if (type == BTRFS_FILE_EXTENT_REG ||
1889 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001890 disko = btrfs_file_extent_disk_bytenr(leaf,
1891 extent);
1892 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1893 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001894 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001895 datal = btrfs_file_extent_num_bytes(leaf,
1896 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001897 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1898 /* take upper bound, may be compressed */
1899 datal = btrfs_file_extent_ram_bytes(leaf,
1900 extent);
1901 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001902 btrfs_release_path(root, path);
1903
Sage Weil050006a2010-10-29 15:37:33 -04001904 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001905 key.offset >= off+len)
1906 goto next;
1907
Zheng Yan31840ae2008-09-23 13:14:14 -04001908 memcpy(&new_key, &key, sizeof(new_key));
1909 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001910 if (off <= key.offset)
1911 new_key.offset = key.offset + destoff - off;
1912 else
1913 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001914
Yan, Zhenga22285a2010-05-16 10:48:46 -04001915 trans = btrfs_start_transaction(root, 1);
1916 if (IS_ERR(trans)) {
1917 ret = PTR_ERR(trans);
1918 goto out;
1919 }
1920
Chris Masonc8a894d2009-06-27 21:07:03 -04001921 if (type == BTRFS_FILE_EXTENT_REG ||
1922 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001923 if (off > key.offset) {
1924 datao += off - key.offset;
1925 datal -= off - key.offset;
1926 }
1927
1928 if (key.offset + datal > off + len)
1929 datal = off + len - key.offset;
1930
1931 ret = btrfs_drop_extents(trans, inode,
1932 new_key.offset,
1933 new_key.offset + datal,
1934 &hint_byte, 1);
1935 BUG_ON(ret);
1936
Sage Weilc5c9cd42008-11-12 14:32:25 -05001937 ret = btrfs_insert_empty_item(trans, root, path,
1938 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001939 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001940
1941 leaf = path->nodes[0];
1942 slot = path->slots[0];
1943 write_extent_buffer(leaf, buf,
1944 btrfs_item_ptr_offset(leaf, slot),
1945 size);
1946
1947 extent = btrfs_item_ptr(leaf, slot,
1948 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001949
Sage Weilc5c9cd42008-11-12 14:32:25 -05001950 /* disko == 0 means it's a hole */
1951 if (!disko)
1952 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001953
1954 btrfs_set_file_extent_offset(leaf, extent,
1955 datao);
1956 btrfs_set_file_extent_num_bytes(leaf, extent,
1957 datal);
1958 if (disko) {
1959 inode_add_bytes(inode, datal);
1960 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001961 disko, diskl, 0,
1962 root->root_key.objectid,
1963 inode->i_ino,
1964 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001965 BUG_ON(ret);
1966 }
1967 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1968 u64 skip = 0;
1969 u64 trim = 0;
1970 if (off > key.offset) {
1971 skip = off - key.offset;
1972 new_key.offset += skip;
1973 }
Chris Masond3977122009-01-05 21:25:51 -05001974
Sage Weilc5c9cd42008-11-12 14:32:25 -05001975 if (key.offset + datal > off+len)
1976 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001977
Sage Weilc5c9cd42008-11-12 14:32:25 -05001978 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001979 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001980 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001981 goto out;
1982 }
1983 size -= skip + trim;
1984 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001985
1986 ret = btrfs_drop_extents(trans, inode,
1987 new_key.offset,
1988 new_key.offset + datal,
1989 &hint_byte, 1);
1990 BUG_ON(ret);
1991
Sage Weilc5c9cd42008-11-12 14:32:25 -05001992 ret = btrfs_insert_empty_item(trans, root, path,
1993 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001994 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001995
1996 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001997 u32 start =
1998 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001999 memmove(buf+start, buf+start+skip,
2000 datal);
2001 }
2002
2003 leaf = path->nodes[0];
2004 slot = path->slots[0];
2005 write_extent_buffer(leaf, buf,
2006 btrfs_item_ptr_offset(leaf, slot),
2007 size);
2008 inode_add_bytes(inode, datal);
2009 }
2010
2011 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002012 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002013
Yan, Zhenga22285a2010-05-16 10:48:46 -04002014 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002015
2016 /*
2017 * we round up to the block size at eof when
2018 * determining which extents to clone above,
2019 * but shouldn't round up the file size
2020 */
2021 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002022 if (endoff > destoff+olen)
2023 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002024 if (endoff > inode->i_size)
2025 btrfs_i_size_write(inode, endoff);
2026
Yan, Zhenga22285a2010-05-16 10:48:46 -04002027 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2028 ret = btrfs_update_inode(trans, root, inode);
2029 BUG_ON(ret);
2030 btrfs_end_transaction(trans, root);
2031 }
Chris Masond3977122009-01-05 21:25:51 -05002032next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002033 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002034 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002035 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002036 ret = 0;
2037out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002038 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002039 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002040out_unlock:
2041 mutex_unlock(&src->i_mutex);
2042 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002043 vfree(buf);
2044 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002045out_fput:
2046 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002047out_drop_write:
2048 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002049 return ret;
2050}
2051
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002052static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002053{
2054 struct btrfs_ioctl_clone_range_args args;
2055
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002056 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002057 return -EFAULT;
2058 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2059 args.src_length, args.dest_offset);
2060}
2061
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002062/*
2063 * there are many ways the trans_start and trans_end ioctls can lead
2064 * to deadlocks. They should only be used by applications that
2065 * basically own the machine, and have a very in depth understanding
2066 * of all the possible deadlocks and enospc problems.
2067 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002068static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002069{
2070 struct inode *inode = fdentry(file)->d_inode;
2071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002073 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002074
Sage Weil1ab86ae2009-09-29 18:38:44 -04002075 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002076 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002077 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002078
2079 ret = -EINPROGRESS;
2080 if (file->private_data)
2081 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002082
Li Zefanb83cc962010-12-20 16:04:08 +08002083 ret = -EROFS;
2084 if (btrfs_root_readonly(root))
2085 goto out;
2086
Yan Zhengc146afa2008-11-12 14:34:12 -05002087 ret = mnt_want_write(file->f_path.mnt);
2088 if (ret)
2089 goto out;
2090
Sage Weil9ca9ee02008-08-04 10:41:27 -04002091 mutex_lock(&root->fs_info->trans_mutex);
2092 root->fs_info->open_ioctl_trans++;
2093 mutex_unlock(&root->fs_info->trans_mutex);
2094
Sage Weil1ab86ae2009-09-29 18:38:44 -04002095 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002096 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002097 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002098 goto out_drop;
2099
2100 file->private_data = trans;
2101 return 0;
2102
2103out_drop:
2104 mutex_lock(&root->fs_info->trans_mutex);
2105 root->fs_info->open_ioctl_trans--;
2106 mutex_unlock(&root->fs_info->trans_mutex);
2107 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002108out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002109 return ret;
2110}
2111
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002112static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2113{
2114 struct inode *inode = fdentry(file)->d_inode;
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct btrfs_root *new_root;
2117 struct btrfs_dir_item *di;
2118 struct btrfs_trans_handle *trans;
2119 struct btrfs_path *path;
2120 struct btrfs_key location;
2121 struct btrfs_disk_key disk_key;
2122 struct btrfs_super_block *disk_super;
2123 u64 features;
2124 u64 objectid = 0;
2125 u64 dir_id;
2126
2127 if (!capable(CAP_SYS_ADMIN))
2128 return -EPERM;
2129
2130 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2131 return -EFAULT;
2132
2133 if (!objectid)
2134 objectid = root->root_key.objectid;
2135
2136 location.objectid = objectid;
2137 location.type = BTRFS_ROOT_ITEM_KEY;
2138 location.offset = (u64)-1;
2139
2140 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2141 if (IS_ERR(new_root))
2142 return PTR_ERR(new_root);
2143
2144 if (btrfs_root_refs(&new_root->root_item) == 0)
2145 return -ENOENT;
2146
2147 path = btrfs_alloc_path();
2148 if (!path)
2149 return -ENOMEM;
2150 path->leave_spinning = 1;
2151
2152 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002153 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002154 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002155 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002156 }
2157
2158 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2159 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2160 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002161 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002162 btrfs_free_path(path);
2163 btrfs_end_transaction(trans, root);
2164 printk(KERN_ERR "Umm, you don't have the default dir item, "
2165 "this isn't going to work\n");
2166 return -ENOENT;
2167 }
2168
2169 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2170 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2171 btrfs_mark_buffer_dirty(path->nodes[0]);
2172 btrfs_free_path(path);
2173
2174 disk_super = &root->fs_info->super_copy;
2175 features = btrfs_super_incompat_flags(disk_super);
2176 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2177 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2178 btrfs_set_super_incompat_flags(disk_super, features);
2179 }
2180 btrfs_end_transaction(trans, root);
2181
2182 return 0;
2183}
2184
Josef Bacikbf5fc092010-09-29 11:22:36 -04002185static void get_block_group_info(struct list_head *groups_list,
2186 struct btrfs_ioctl_space_info *space)
2187{
2188 struct btrfs_block_group_cache *block_group;
2189
2190 space->total_bytes = 0;
2191 space->used_bytes = 0;
2192 space->flags = 0;
2193 list_for_each_entry(block_group, groups_list, list) {
2194 space->flags = block_group->flags;
2195 space->total_bytes += block_group->key.offset;
2196 space->used_bytes +=
2197 btrfs_block_group_used(&block_group->item);
2198 }
2199}
2200
Josef Bacik1406e432010-01-13 18:19:06 +00002201long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2202{
2203 struct btrfs_ioctl_space_args space_args;
2204 struct btrfs_ioctl_space_info space;
2205 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002206 struct btrfs_ioctl_space_info *dest_orig;
2207 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002208 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002209 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2210 BTRFS_BLOCK_GROUP_SYSTEM,
2211 BTRFS_BLOCK_GROUP_METADATA,
2212 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2213 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002214 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002215 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002216 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002217 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002218
2219 if (copy_from_user(&space_args,
2220 (struct btrfs_ioctl_space_args __user *)arg,
2221 sizeof(space_args)))
2222 return -EFAULT;
2223
Josef Bacikbf5fc092010-09-29 11:22:36 -04002224 for (i = 0; i < num_types; i++) {
2225 struct btrfs_space_info *tmp;
2226
2227 info = NULL;
2228 rcu_read_lock();
2229 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2230 list) {
2231 if (tmp->flags == types[i]) {
2232 info = tmp;
2233 break;
2234 }
2235 }
2236 rcu_read_unlock();
2237
2238 if (!info)
2239 continue;
2240
2241 down_read(&info->groups_sem);
2242 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2243 if (!list_empty(&info->block_groups[c]))
2244 slot_count++;
2245 }
2246 up_read(&info->groups_sem);
2247 }
Josef Bacik1406e432010-01-13 18:19:06 +00002248
Chris Mason7fde62b2010-03-16 15:40:10 -04002249 /* space_slots == 0 means they are asking for a count */
2250 if (space_args.space_slots == 0) {
2251 space_args.total_spaces = slot_count;
2252 goto out;
2253 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002254
Dan Rosenberg51788b12011-02-14 16:04:23 -05002255 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002256
Chris Mason7fde62b2010-03-16 15:40:10 -04002257 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002258
Chris Mason7fde62b2010-03-16 15:40:10 -04002259 /* we generally have at most 6 or so space infos, one for each raid
2260 * level. So, a whole page should be more than enough for everyone
2261 */
2262 if (alloc_size > PAGE_CACHE_SIZE)
2263 return -ENOMEM;
2264
2265 space_args.total_spaces = 0;
2266 dest = kmalloc(alloc_size, GFP_NOFS);
2267 if (!dest)
2268 return -ENOMEM;
2269 dest_orig = dest;
2270
2271 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002272 for (i = 0; i < num_types; i++) {
2273 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002274
Dan Rosenberg51788b12011-02-14 16:04:23 -05002275 if (!slot_count)
2276 break;
2277
Josef Bacikbf5fc092010-09-29 11:22:36 -04002278 info = NULL;
2279 rcu_read_lock();
2280 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2281 list) {
2282 if (tmp->flags == types[i]) {
2283 info = tmp;
2284 break;
2285 }
2286 }
2287 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002288
Josef Bacikbf5fc092010-09-29 11:22:36 -04002289 if (!info)
2290 continue;
2291 down_read(&info->groups_sem);
2292 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2293 if (!list_empty(&info->block_groups[c])) {
2294 get_block_group_info(&info->block_groups[c],
2295 &space);
2296 memcpy(dest, &space, sizeof(space));
2297 dest++;
2298 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002299 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002300 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002301 if (!slot_count)
2302 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002303 }
2304 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002305 }
Josef Bacik1406e432010-01-13 18:19:06 +00002306
Chris Mason7fde62b2010-03-16 15:40:10 -04002307 user_dest = (struct btrfs_ioctl_space_info *)
2308 (arg + sizeof(struct btrfs_ioctl_space_args));
2309
2310 if (copy_to_user(user_dest, dest_orig, alloc_size))
2311 ret = -EFAULT;
2312
2313 kfree(dest_orig);
2314out:
2315 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002316 ret = -EFAULT;
2317
2318 return ret;
2319}
2320
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002321/*
2322 * there are many ways the trans_start and trans_end ioctls can lead
2323 * to deadlocks. They should only be used by applications that
2324 * basically own the machine, and have a very in depth understanding
2325 * of all the possible deadlocks and enospc problems.
2326 */
2327long btrfs_ioctl_trans_end(struct file *file)
2328{
2329 struct inode *inode = fdentry(file)->d_inode;
2330 struct btrfs_root *root = BTRFS_I(inode)->root;
2331 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002332
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002333 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002334 if (!trans)
2335 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002336 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002337
Sage Weil1ab86ae2009-09-29 18:38:44 -04002338 btrfs_end_transaction(trans, root);
2339
Sage Weil9ca9ee02008-08-04 10:41:27 -04002340 mutex_lock(&root->fs_info->trans_mutex);
2341 root->fs_info->open_ioctl_trans--;
2342 mutex_unlock(&root->fs_info->trans_mutex);
2343
Sage Weilcfc8ea82008-12-11 16:30:06 -05002344 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002345 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002346}
2347
Sage Weil46204592010-10-29 15:41:32 -04002348static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2349{
2350 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2351 struct btrfs_trans_handle *trans;
2352 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002353 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002354
2355 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002356 if (IS_ERR(trans))
2357 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002358 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002359 ret = btrfs_commit_transaction_async(trans, root, 0);
2360 if (ret)
2361 return ret;
Sage Weil46204592010-10-29 15:41:32 -04002362
2363 if (argp)
2364 if (copy_to_user(argp, &transid, sizeof(transid)))
2365 return -EFAULT;
2366 return 0;
2367}
2368
2369static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2370{
2371 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2372 u64 transid;
2373
2374 if (argp) {
2375 if (copy_from_user(&transid, argp, sizeof(transid)))
2376 return -EFAULT;
2377 } else {
2378 transid = 0; /* current trans */
2379 }
2380 return btrfs_wait_for_commit(root, transid);
2381}
2382
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002383long btrfs_ioctl(struct file *file, unsigned int
2384 cmd, unsigned long arg)
2385{
2386 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002387 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002388
2389 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002390 case FS_IOC_GETFLAGS:
2391 return btrfs_ioctl_getflags(file, argp);
2392 case FS_IOC_SETFLAGS:
2393 return btrfs_ioctl_setflags(file, argp);
2394 case FS_IOC_GETVERSION:
2395 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002396 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002397 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002398 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002399 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002400 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002401 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002402 case BTRFS_IOC_SNAP_DESTROY:
2403 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002404 case BTRFS_IOC_SUBVOL_GETFLAGS:
2405 return btrfs_ioctl_subvol_getflags(file, argp);
2406 case BTRFS_IOC_SUBVOL_SETFLAGS:
2407 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002408 case BTRFS_IOC_DEFAULT_SUBVOL:
2409 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002411 return btrfs_ioctl_defrag(file, NULL);
2412 case BTRFS_IOC_DEFRAG_RANGE:
2413 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002414 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002415 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002416 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002417 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002418 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002419 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002420 case BTRFS_IOC_BALANCE:
2421 return btrfs_balance(root->fs_info->dev_root);
2422 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002423 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2424 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002425 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002426 case BTRFS_IOC_TRANS_START:
2427 return btrfs_ioctl_trans_start(file);
2428 case BTRFS_IOC_TRANS_END:
2429 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002430 case BTRFS_IOC_TREE_SEARCH:
2431 return btrfs_ioctl_tree_search(file, argp);
2432 case BTRFS_IOC_INO_LOOKUP:
2433 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002434 case BTRFS_IOC_SPACE_INFO:
2435 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002436 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002437 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2438 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002439 case BTRFS_IOC_START_SYNC:
2440 return btrfs_ioctl_start_sync(file, argp);
2441 case BTRFS_IOC_WAIT_SYNC:
2442 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002443 }
2444
2445 return -ENOTTY;
2446}