blob: be2d4f6aaa5eef34d877002bcbf524813f0a0405 [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
Yan, Zhenga22285a2010-05-16 10:48:46 -0400412 btrfs_orphan_cleanup(pending_snapshot->snap);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413
Josef Bacik6a912212010-11-20 09:48:00 +0000414 parent = dget_parent(dentry);
415 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
416 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000417 if (IS_ERR(inode)) {
418 ret = PTR_ERR(inode);
419 goto fail;
420 }
421 BUG_ON(!inode);
422 d_instantiate(dentry, inode);
423 ret = 0;
424fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400425 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400426 return ret;
427}
428
Sage Weil4260f7c2010-10-29 15:46:43 -0400429/* copy of check_sticky in fs/namei.c()
430* It's inline, so penalty for filesystems that don't use sticky bit is
431* minimal.
432*/
433static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
434{
435 uid_t fsuid = current_fsuid();
436
437 if (!(dir->i_mode & S_ISVTX))
438 return 0;
439 if (inode->i_uid == fsuid)
440 return 0;
441 if (dir->i_uid == fsuid)
442 return 0;
443 return !capable(CAP_FOWNER);
444}
445
446/* copy of may_delete in fs/namei.c()
447 * Check whether we can remove a link victim from directory dir, check
448 * whether the type of victim is right.
449 * 1. We can't do it if dir is read-only (done in permission())
450 * 2. We should have write and exec permissions on dir
451 * 3. We can't remove anything from append-only dir
452 * 4. We can't do anything with immutable dir (done in permission())
453 * 5. If the sticky bit on dir is set we should either
454 * a. be owner of dir, or
455 * b. be owner of victim, or
456 * c. have CAP_FOWNER capability
457 * 6. If the victim is append-only or immutable we can't do antyhing with
458 * links pointing to it.
459 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
460 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
461 * 9. We can't remove a root or mountpoint.
462 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
463 * nfs_async_unlink().
464 */
465
466static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
467{
468 int error;
469
470 if (!victim->d_inode)
471 return -ENOENT;
472
473 BUG_ON(victim->d_parent->d_inode != dir);
474 audit_inode_child(victim, dir);
475
476 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
477 if (error)
478 return error;
479 if (IS_APPEND(dir))
480 return -EPERM;
481 if (btrfs_check_sticky(dir, victim->d_inode)||
482 IS_APPEND(victim->d_inode)||
483 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
484 return -EPERM;
485 if (isdir) {
486 if (!S_ISDIR(victim->d_inode->i_mode))
487 return -ENOTDIR;
488 if (IS_ROOT(victim))
489 return -EBUSY;
490 } else if (S_ISDIR(victim->d_inode->i_mode))
491 return -EISDIR;
492 if (IS_DEADDIR(dir))
493 return -ENOENT;
494 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
495 return -EBUSY;
496 return 0;
497}
498
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400499/* copy of may_create in fs/namei.c() */
500static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
501{
502 if (child->d_inode)
503 return -EEXIST;
504 if (IS_DEADDIR(dir))
505 return -ENOENT;
506 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
507}
508
509/*
510 * Create a new subvolume below @parent. This is largely modeled after
511 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
512 * inside this filesystem so it's quite a bit simpler.
513 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400514static noinline int btrfs_mksubvol(struct path *parent,
515 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400516 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800517 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400518{
Yan, Zheng76dda932009-09-21 16:00:26 -0400519 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400520 struct dentry *dentry;
521 int error;
522
Yan, Zheng76dda932009-09-21 16:00:26 -0400523 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400524
525 dentry = lookup_one_len(name, parent->dentry, namelen);
526 error = PTR_ERR(dentry);
527 if (IS_ERR(dentry))
528 goto out_unlock;
529
530 error = -EEXIST;
531 if (dentry->d_inode)
532 goto out_dput;
533
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400534 error = mnt_want_write(parent->mnt);
535 if (error)
536 goto out_dput;
537
Yan, Zheng76dda932009-09-21 16:00:26 -0400538 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400539 if (error)
540 goto out_drop_write;
541
Yan, Zheng76dda932009-09-21 16:00:26 -0400542 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
543
544 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
545 goto out_up_read;
546
Chris Mason3de45862008-11-17 21:02:50 -0500547 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400548 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800549 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500550 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400551 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400552 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500553 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400554 if (!error)
555 fsnotify_mkdir(dir, dentry);
556out_up_read:
557 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400558out_drop_write:
559 mnt_drop_write(parent->mnt);
560out_dput:
561 dput(dentry);
562out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400563 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400564 return error;
565}
566
Chris Mason940100a2010-03-10 10:52:59 -0500567static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500568 int thresh, u64 *last_len, u64 *skip,
569 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500570{
571 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
572 struct extent_map *em = NULL;
573 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
574 int ret = 1;
575
Chris Mason1e701a32010-03-11 09:42:04 -0500576
577 if (thresh == 0)
578 thresh = 256 * 1024;
579
Chris Mason940100a2010-03-10 10:52:59 -0500580 /*
581 * make sure that once we start defragging and extent, we keep on
582 * defragging it
583 */
584 if (start < *defrag_end)
585 return 1;
586
587 *skip = 0;
588
589 /*
590 * hopefully we have this extent in the tree already, try without
591 * the full extent lock
592 */
593 read_lock(&em_tree->lock);
594 em = lookup_extent_mapping(em_tree, start, len);
595 read_unlock(&em_tree->lock);
596
597 if (!em) {
598 /* get the big lock and read metadata off disk */
599 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
600 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
601 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
602
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000603 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500604 return 0;
605 }
606
607 /* this will cover holes, and inline extents */
608 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
609 ret = 0;
610
611 /*
612 * we hit a real extent, if it is big don't bother defragging it again
613 */
Chris Mason1e701a32010-03-11 09:42:04 -0500614 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500615 ret = 0;
616
617 /*
618 * last_len ends up being a counter of how many bytes we've defragged.
619 * every time we choose not to defrag an extent, we reset *last_len
620 * so that the next tiny extent will force a defrag.
621 *
622 * The end result of this is that tiny extents before a single big
623 * extent will force at least part of that big extent to be defragged.
624 */
625 if (ret) {
626 *last_len += len;
627 *defrag_end = extent_map_end(em);
628 } else {
629 *last_len = 0;
630 *skip = extent_map_end(em);
631 *defrag_end = 0;
632 }
633
634 free_extent_map(em);
635 return ret;
636}
637
Chris Mason1e701a32010-03-11 09:42:04 -0500638static int btrfs_defrag_file(struct file *file,
639 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400640{
641 struct inode *inode = fdentry(file)->d_inode;
642 struct btrfs_root *root = BTRFS_I(inode)->root;
643 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400644 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400645 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800646 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400647 unsigned long last_index;
648 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
649 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800650 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651 u64 page_start;
652 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500653 u64 last_len = 0;
654 u64 skip = 0;
655 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 unsigned long i;
657 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800658 int compress_type = BTRFS_COMPRESS_ZLIB;
659
660 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
661 if (range->compress_type > BTRFS_COMPRESS_TYPES)
662 return -EINVAL;
663 if (range->compress_type)
664 compress_type = range->compress_type;
665 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666
Chris Mason940100a2010-03-10 10:52:59 -0500667 if (inode->i_size == 0)
668 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400669
Chris Mason1e701a32010-03-11 09:42:04 -0500670 if (range->start + range->len > range->start) {
671 last_index = min_t(u64, inode->i_size - 1,
672 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
673 } else {
674 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
675 }
676
677 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500678 while (i <= last_index) {
679 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500680 PAGE_CACHE_SIZE,
681 range->extent_thresh,
682 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500683 &defrag_end)) {
684 unsigned long next;
685 /*
686 * the should_defrag function tells us how much to skip
687 * bump our counter by the suggested amount
688 */
689 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
690 i = max(i + 1, next);
691 continue;
692 }
693
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400694 if (total_read % ra_pages == 0) {
695 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
696 min(last_index, i + ra_pages - 1));
697 }
698 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500699 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500700 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800701 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500702
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400703 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
704 if (ret)
705 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400706again:
Chris Mason940100a2010-03-10 10:52:59 -0500707 if (inode->i_size == 0 ||
708 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
709 ret = 0;
710 goto err_reservations;
711 }
712
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400713 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400714 if (!page) {
715 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500716 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400717 }
Chris Mason940100a2010-03-10 10:52:59 -0500718
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400719 if (!PageUptodate(page)) {
720 btrfs_readpage(NULL, page);
721 lock_page(page);
722 if (!PageUptodate(page)) {
723 unlock_page(page);
724 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400725 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500726 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400727 }
728 }
729
Chris Mason940100a2010-03-10 10:52:59 -0500730 if (page->mapping != inode->i_mapping) {
731 unlock_page(page);
732 page_cache_release(page);
733 goto again;
734 }
735
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400736 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400737
Chris Mason940100a2010-03-10 10:52:59 -0500738 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400739 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500740 goto loop_unlock;
741 }
742
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
744 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400746
747 ordered = btrfs_lookup_ordered_extent(inode, page_start);
748 if (ordered) {
749 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
750 unlock_page(page);
751 page_cache_release(page);
752 btrfs_start_ordered_extent(inode, ordered, 1);
753 btrfs_put_ordered_extent(ordered);
754 goto again;
755 }
756 set_page_extent_mapped(page);
757
Chris Masonf87f0572008-08-01 11:27:23 -0400758 /*
759 * this makes sure page_mkwrite is called on the
760 * page if it is dirtied again later
761 */
762 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500763 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
764 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
765 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400766
Josef Bacik2ac55d42010-02-03 19:33:23 +0000767 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500768 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400769 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400770 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500771
772loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400773 unlock_page(page);
774 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500775 mutex_unlock(&inode->i_mutex);
776
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400777 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500778 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400779 }
780
Chris Mason1e701a32010-03-11 09:42:04 -0500781 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
782 filemap_flush(inode->i_mapping);
783
784 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
785 /* the filemap_flush will queue IO into the worker threads, but
786 * we have to make sure the IO is actually started and that
787 * ordered extents get created before we return
788 */
789 atomic_inc(&root->fs_info->async_submit_draining);
790 while (atomic_read(&root->fs_info->nr_async_submits) ||
791 atomic_read(&root->fs_info->async_delalloc_pages)) {
792 wait_event(root->fs_info->async_submit_wait,
793 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
794 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
795 }
796 atomic_dec(&root->fs_info->async_submit_draining);
797
798 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800799 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500800 mutex_unlock(&inode->i_mutex);
801 }
802
Li Zefan1a419d82010-10-25 15:12:50 +0800803 disk_super = &root->fs_info->super_copy;
804 features = btrfs_super_incompat_flags(disk_super);
805 if (range->compress_type == BTRFS_COMPRESS_LZO) {
806 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
807 btrfs_set_super_incompat_flags(disk_super, features);
808 }
809
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400810 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500811
812err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400813 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
814err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500815 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500816 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400817}
818
Yan, Zheng76dda932009-09-21 16:00:26 -0400819static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
820 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400821{
822 u64 new_size;
823 u64 old_size;
824 u64 devid = 1;
825 struct btrfs_ioctl_vol_args *vol_args;
826 struct btrfs_trans_handle *trans;
827 struct btrfs_device *device = NULL;
828 char *sizestr;
829 char *devstr = NULL;
830 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400831 int mod = 0;
832
Yan Zhengc146afa2008-11-12 14:34:12 -0500833 if (root->fs_info->sb->s_flags & MS_RDONLY)
834 return -EROFS;
835
Chris Masone441d542009-01-05 16:57:23 -0500836 if (!capable(CAP_SYS_ADMIN))
837 return -EPERM;
838
Li Zefandae7b662009-04-08 15:06:54 +0800839 vol_args = memdup_user(arg, sizeof(*vol_args));
840 if (IS_ERR(vol_args))
841 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400842
843 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400844
Chris Mason7d9eb122008-07-08 14:19:17 -0400845 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400846 sizestr = vol_args->name;
847 devstr = strchr(sizestr, ':');
848 if (devstr) {
849 char *end;
850 sizestr = devstr + 1;
851 *devstr = '\0';
852 devstr = vol_args->name;
853 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700854 printk(KERN_INFO "resizing devid %llu\n",
855 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400856 }
Yan Zheng2b820322008-11-17 21:11:30 -0500857 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700859 printk(KERN_INFO "resizer unable to find device %llu\n",
860 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861 ret = -EINVAL;
862 goto out_unlock;
863 }
864 if (!strcmp(sizestr, "max"))
865 new_size = device->bdev->bd_inode->i_size;
866 else {
867 if (sizestr[0] == '-') {
868 mod = -1;
869 sizestr++;
870 } else if (sizestr[0] == '+') {
871 mod = 1;
872 sizestr++;
873 }
Akinobu Mita91748462010-02-28 10:59:11 +0000874 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400875 if (new_size == 0) {
876 ret = -EINVAL;
877 goto out_unlock;
878 }
879 }
880
881 old_size = device->total_bytes;
882
883 if (mod < 0) {
884 if (new_size > old_size) {
885 ret = -EINVAL;
886 goto out_unlock;
887 }
888 new_size = old_size - new_size;
889 } else if (mod > 0) {
890 new_size = old_size + new_size;
891 }
892
893 if (new_size < 256 * 1024 * 1024) {
894 ret = -EINVAL;
895 goto out_unlock;
896 }
897 if (new_size > device->bdev->bd_inode->i_size) {
898 ret = -EFBIG;
899 goto out_unlock;
900 }
901
902 do_div(new_size, root->sectorsize);
903 new_size *= root->sectorsize;
904
905 printk(KERN_INFO "new size for %s is %llu\n",
906 device->name, (unsigned long long)new_size);
907
908 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400909 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +0000910 if (IS_ERR(trans)) {
911 ret = PTR_ERR(trans);
912 goto out_unlock;
913 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400914 ret = btrfs_grow_device(trans, device, new_size);
915 btrfs_commit_transaction(trans, root);
916 } else {
917 ret = btrfs_shrink_device(device, new_size);
918 }
919
920out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400921 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400922 kfree(vol_args);
923 return ret;
924}
925
Sage Weil72fd0322010-10-29 15:41:32 -0400926static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
927 char *name,
928 unsigned long fd,
929 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +0800930 u64 *transid,
931 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400932{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400933 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -0500934 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400935 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500936 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400937
Yan Zhengc146afa2008-11-12 14:34:12 -0500938 if (root->fs_info->sb->s_flags & MS_RDONLY)
939 return -EROFS;
940
Sage Weil72fd0322010-10-29 15:41:32 -0400941 namelen = strlen(name);
942 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400943 ret = -EINVAL;
944 goto out;
945 }
946
Chris Mason3de45862008-11-17 21:02:50 -0500947 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -0400948 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +0800949 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400950 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500951 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -0400952 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -0500953 if (!src_file) {
954 ret = -EINVAL;
955 goto out;
956 }
957
958 src_inode = src_file->f_path.dentry->d_inode;
959 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500960 printk(KERN_INFO "btrfs: Snapshot src from "
961 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500962 ret = -EINVAL;
963 fput(src_file);
964 goto out;
965 }
Sage Weil72fd0322010-10-29 15:41:32 -0400966 ret = btrfs_mksubvol(&file->f_path, name, namelen,
967 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +0800968 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500969 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400970 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400971out:
Sage Weil72fd0322010-10-29 15:41:32 -0400972 return ret;
973}
974
975static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +0800976 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -0400977{
Li Zefanfa0d2b92010-12-20 15:53:28 +0800978 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -0400979 int ret;
980
Li Zefanfa0d2b92010-12-20 15:53:28 +0800981 vol_args = memdup_user(arg, sizeof(*vol_args));
982 if (IS_ERR(vol_args))
983 return PTR_ERR(vol_args);
984 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -0400985
Li Zefanfa0d2b92010-12-20 15:53:28 +0800986 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +0800987 vol_args->fd, subvol,
988 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +0000989
Li Zefanfa0d2b92010-12-20 15:53:28 +0800990 kfree(vol_args);
991 return ret;
992}
Li Zefanfdfb1e42010-12-10 06:41:56 +0000993
Li Zefanfa0d2b92010-12-20 15:53:28 +0800994static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
995 void __user *arg, int subvol)
996{
997 struct btrfs_ioctl_vol_args_v2 *vol_args;
998 int ret;
999 u64 transid = 0;
1000 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001001 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001002
Li Zefanfa0d2b92010-12-20 15:53:28 +08001003 vol_args = memdup_user(arg, sizeof(*vol_args));
1004 if (IS_ERR(vol_args))
1005 return PTR_ERR(vol_args);
1006 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001007
Li Zefanb83cc962010-12-20 16:04:08 +08001008 if (vol_args->flags &
1009 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1010 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001011 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001012 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001013
1014 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1015 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001016 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1017 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001018
1019 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001020 vol_args->fd, subvol,
1021 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001022
1023 if (ret == 0 && ptr &&
1024 copy_to_user(arg +
1025 offsetof(struct btrfs_ioctl_vol_args_v2,
1026 transid), ptr, sizeof(*ptr)))
1027 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001028out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001029 kfree(vol_args);
1030 return ret;
1031}
1032
Li Zefan0caa1022010-12-20 16:30:25 +08001033static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1034 void __user *arg)
1035{
1036 struct inode *inode = fdentry(file)->d_inode;
1037 struct btrfs_root *root = BTRFS_I(inode)->root;
1038 int ret = 0;
1039 u64 flags = 0;
1040
1041 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1042 return -EINVAL;
1043
1044 down_read(&root->fs_info->subvol_sem);
1045 if (btrfs_root_readonly(root))
1046 flags |= BTRFS_SUBVOL_RDONLY;
1047 up_read(&root->fs_info->subvol_sem);
1048
1049 if (copy_to_user(arg, &flags, sizeof(flags)))
1050 ret = -EFAULT;
1051
1052 return ret;
1053}
1054
1055static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1056 void __user *arg)
1057{
1058 struct inode *inode = fdentry(file)->d_inode;
1059 struct btrfs_root *root = BTRFS_I(inode)->root;
1060 struct btrfs_trans_handle *trans;
1061 u64 root_flags;
1062 u64 flags;
1063 int ret = 0;
1064
1065 if (root->fs_info->sb->s_flags & MS_RDONLY)
1066 return -EROFS;
1067
1068 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1069 return -EINVAL;
1070
1071 if (copy_from_user(&flags, arg, sizeof(flags)))
1072 return -EFAULT;
1073
1074 if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
1075 return -EINVAL;
1076
1077 if (flags & ~BTRFS_SUBVOL_RDONLY)
1078 return -EOPNOTSUPP;
1079
1080 down_write(&root->fs_info->subvol_sem);
1081
1082 /* nothing to do */
1083 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1084 goto out;
1085
1086 root_flags = btrfs_root_flags(&root->root_item);
1087 if (flags & BTRFS_SUBVOL_RDONLY)
1088 btrfs_set_root_flags(&root->root_item,
1089 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1090 else
1091 btrfs_set_root_flags(&root->root_item,
1092 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1093
1094 trans = btrfs_start_transaction(root, 1);
1095 if (IS_ERR(trans)) {
1096 ret = PTR_ERR(trans);
1097 goto out_reset;
1098 }
1099
1100 ret = btrfs_update_root(trans, root,
1101 &root->root_key, &root->root_item);
1102
1103 btrfs_commit_transaction(trans, root);
1104out_reset:
1105 if (ret)
1106 btrfs_set_root_flags(&root->root_item, root_flags);
1107out:
1108 up_write(&root->fs_info->subvol_sem);
1109 return ret;
1110}
1111
Yan, Zheng76dda932009-09-21 16:00:26 -04001112/*
1113 * helper to check if the subvolume references other subvolumes
1114 */
1115static noinline int may_destroy_subvol(struct btrfs_root *root)
1116{
1117 struct btrfs_path *path;
1118 struct btrfs_key key;
1119 int ret;
1120
1121 path = btrfs_alloc_path();
1122 if (!path)
1123 return -ENOMEM;
1124
1125 key.objectid = root->root_key.objectid;
1126 key.type = BTRFS_ROOT_REF_KEY;
1127 key.offset = (u64)-1;
1128
1129 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1130 &key, path, 0, 0);
1131 if (ret < 0)
1132 goto out;
1133 BUG_ON(ret == 0);
1134
1135 ret = 0;
1136 if (path->slots[0] > 0) {
1137 path->slots[0]--;
1138 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1139 if (key.objectid == root->root_key.objectid &&
1140 key.type == BTRFS_ROOT_REF_KEY)
1141 ret = -ENOTEMPTY;
1142 }
1143out:
1144 btrfs_free_path(path);
1145 return ret;
1146}
1147
Chris Masonac8e9812010-02-28 15:39:26 -05001148static noinline int key_in_sk(struct btrfs_key *key,
1149 struct btrfs_ioctl_search_key *sk)
1150{
Chris Masonabc6e132010-03-18 12:10:08 -04001151 struct btrfs_key test;
1152 int ret;
1153
1154 test.objectid = sk->min_objectid;
1155 test.type = sk->min_type;
1156 test.offset = sk->min_offset;
1157
1158 ret = btrfs_comp_cpu_keys(key, &test);
1159 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001160 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001161
1162 test.objectid = sk->max_objectid;
1163 test.type = sk->max_type;
1164 test.offset = sk->max_offset;
1165
1166 ret = btrfs_comp_cpu_keys(key, &test);
1167 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001168 return 0;
1169 return 1;
1170}
1171
1172static noinline int copy_to_sk(struct btrfs_root *root,
1173 struct btrfs_path *path,
1174 struct btrfs_key *key,
1175 struct btrfs_ioctl_search_key *sk,
1176 char *buf,
1177 unsigned long *sk_offset,
1178 int *num_found)
1179{
1180 u64 found_transid;
1181 struct extent_buffer *leaf;
1182 struct btrfs_ioctl_search_header sh;
1183 unsigned long item_off;
1184 unsigned long item_len;
1185 int nritems;
1186 int i;
1187 int slot;
1188 int found = 0;
1189 int ret = 0;
1190
1191 leaf = path->nodes[0];
1192 slot = path->slots[0];
1193 nritems = btrfs_header_nritems(leaf);
1194
1195 if (btrfs_header_generation(leaf) > sk->max_transid) {
1196 i = nritems;
1197 goto advance_key;
1198 }
1199 found_transid = btrfs_header_generation(leaf);
1200
1201 for (i = slot; i < nritems; i++) {
1202 item_off = btrfs_item_ptr_offset(leaf, i);
1203 item_len = btrfs_item_size_nr(leaf, i);
1204
1205 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1206 item_len = 0;
1207
1208 if (sizeof(sh) + item_len + *sk_offset >
1209 BTRFS_SEARCH_ARGS_BUFSIZE) {
1210 ret = 1;
1211 goto overflow;
1212 }
1213
1214 btrfs_item_key_to_cpu(leaf, key, i);
1215 if (!key_in_sk(key, sk))
1216 continue;
1217
1218 sh.objectid = key->objectid;
1219 sh.offset = key->offset;
1220 sh.type = key->type;
1221 sh.len = item_len;
1222 sh.transid = found_transid;
1223
1224 /* copy search result header */
1225 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1226 *sk_offset += sizeof(sh);
1227
1228 if (item_len) {
1229 char *p = buf + *sk_offset;
1230 /* copy the item */
1231 read_extent_buffer(leaf, p,
1232 item_off, item_len);
1233 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001234 }
Chris Mason90fdde12010-03-18 12:14:54 -04001235 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001236
1237 if (*num_found >= sk->nr_items)
1238 break;
1239 }
1240advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001241 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001242 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1243 key->offset++;
1244 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1245 key->offset = 0;
1246 key->type++;
1247 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1248 key->offset = 0;
1249 key->type = 0;
1250 key->objectid++;
1251 } else
1252 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001253overflow:
1254 *num_found += found;
1255 return ret;
1256}
1257
1258static noinline int search_ioctl(struct inode *inode,
1259 struct btrfs_ioctl_search_args *args)
1260{
1261 struct btrfs_root *root;
1262 struct btrfs_key key;
1263 struct btrfs_key max_key;
1264 struct btrfs_path *path;
1265 struct btrfs_ioctl_search_key *sk = &args->key;
1266 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1267 int ret;
1268 int num_found = 0;
1269 unsigned long sk_offset = 0;
1270
1271 path = btrfs_alloc_path();
1272 if (!path)
1273 return -ENOMEM;
1274
1275 if (sk->tree_id == 0) {
1276 /* search the root of the inode that was passed */
1277 root = BTRFS_I(inode)->root;
1278 } else {
1279 key.objectid = sk->tree_id;
1280 key.type = BTRFS_ROOT_ITEM_KEY;
1281 key.offset = (u64)-1;
1282 root = btrfs_read_fs_root_no_name(info, &key);
1283 if (IS_ERR(root)) {
1284 printk(KERN_ERR "could not find root %llu\n",
1285 sk->tree_id);
1286 btrfs_free_path(path);
1287 return -ENOENT;
1288 }
1289 }
1290
1291 key.objectid = sk->min_objectid;
1292 key.type = sk->min_type;
1293 key.offset = sk->min_offset;
1294
1295 max_key.objectid = sk->max_objectid;
1296 max_key.type = sk->max_type;
1297 max_key.offset = sk->max_offset;
1298
1299 path->keep_locks = 1;
1300
1301 while(1) {
1302 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1303 sk->min_transid);
1304 if (ret != 0) {
1305 if (ret > 0)
1306 ret = 0;
1307 goto err;
1308 }
1309 ret = copy_to_sk(root, path, &key, sk, args->buf,
1310 &sk_offset, &num_found);
1311 btrfs_release_path(root, path);
1312 if (ret || num_found >= sk->nr_items)
1313 break;
1314
1315 }
1316 ret = 0;
1317err:
1318 sk->nr_items = num_found;
1319 btrfs_free_path(path);
1320 return ret;
1321}
1322
1323static noinline int btrfs_ioctl_tree_search(struct file *file,
1324 void __user *argp)
1325{
1326 struct btrfs_ioctl_search_args *args;
1327 struct inode *inode;
1328 int ret;
1329
1330 if (!capable(CAP_SYS_ADMIN))
1331 return -EPERM;
1332
Julia Lawall2354d082010-10-29 15:14:18 -04001333 args = memdup_user(argp, sizeof(*args));
1334 if (IS_ERR(args))
1335 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001336
Chris Masonac8e9812010-02-28 15:39:26 -05001337 inode = fdentry(file)->d_inode;
1338 ret = search_ioctl(inode, args);
1339 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1340 ret = -EFAULT;
1341 kfree(args);
1342 return ret;
1343}
1344
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001345/*
Chris Masonac8e9812010-02-28 15:39:26 -05001346 * Search INODE_REFs to identify path name of 'dirid' directory
1347 * in a 'tree_id' tree. and sets path name to 'name'.
1348 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001349static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1350 u64 tree_id, u64 dirid, char *name)
1351{
1352 struct btrfs_root *root;
1353 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001354 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001355 int ret = -1;
1356 int slot;
1357 int len;
1358 int total_len = 0;
1359 struct btrfs_inode_ref *iref;
1360 struct extent_buffer *l;
1361 struct btrfs_path *path;
1362
1363 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1364 name[0]='\0';
1365 return 0;
1366 }
1367
1368 path = btrfs_alloc_path();
1369 if (!path)
1370 return -ENOMEM;
1371
Chris Masonac8e9812010-02-28 15:39:26 -05001372 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001373
1374 key.objectid = tree_id;
1375 key.type = BTRFS_ROOT_ITEM_KEY;
1376 key.offset = (u64)-1;
1377 root = btrfs_read_fs_root_no_name(info, &key);
1378 if (IS_ERR(root)) {
1379 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001380 ret = -ENOENT;
1381 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001382 }
1383
1384 key.objectid = dirid;
1385 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001386 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001387
1388 while(1) {
1389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1390 if (ret < 0)
1391 goto out;
1392
1393 l = path->nodes[0];
1394 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001395 if (ret > 0 && slot > 0)
1396 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001397 btrfs_item_key_to_cpu(l, &key, slot);
1398
1399 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001400 key.type != BTRFS_INODE_REF_KEY)) {
1401 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001402 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001403 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001404
1405 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1406 len = btrfs_inode_ref_name_len(l, iref);
1407 ptr -= len + 1;
1408 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001409 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001410 goto out;
1411
1412 *(ptr + len) = '/';
1413 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1414
1415 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1416 break;
1417
1418 btrfs_release_path(root, path);
1419 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001420 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001421 dirid = key.objectid;
1422
1423 }
Chris Masonac8e9812010-02-28 15:39:26 -05001424 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001425 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001426 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001427 name[total_len]='\0';
1428 ret = 0;
1429out:
1430 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001431 return ret;
1432}
1433
1434static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1435 void __user *argp)
1436{
1437 struct btrfs_ioctl_ino_lookup_args *args;
1438 struct inode *inode;
1439 int ret;
1440
1441 if (!capable(CAP_SYS_ADMIN))
1442 return -EPERM;
1443
Julia Lawall2354d082010-10-29 15:14:18 -04001444 args = memdup_user(argp, sizeof(*args));
1445 if (IS_ERR(args))
1446 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001447
Chris Masonac8e9812010-02-28 15:39:26 -05001448 inode = fdentry(file)->d_inode;
1449
Chris Mason1b53ac42010-03-18 12:17:05 -04001450 if (args->treeid == 0)
1451 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1452
Chris Masonac8e9812010-02-28 15:39:26 -05001453 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1454 args->treeid, args->objectid,
1455 args->name);
1456
1457 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1458 ret = -EFAULT;
1459
1460 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001461 return ret;
1462}
1463
Yan, Zheng76dda932009-09-21 16:00:26 -04001464static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1465 void __user *arg)
1466{
1467 struct dentry *parent = fdentry(file);
1468 struct dentry *dentry;
1469 struct inode *dir = parent->d_inode;
1470 struct inode *inode;
1471 struct btrfs_root *root = BTRFS_I(dir)->root;
1472 struct btrfs_root *dest = NULL;
1473 struct btrfs_ioctl_vol_args *vol_args;
1474 struct btrfs_trans_handle *trans;
1475 int namelen;
1476 int ret;
1477 int err = 0;
1478
Yan, Zheng76dda932009-09-21 16:00:26 -04001479 vol_args = memdup_user(arg, sizeof(*vol_args));
1480 if (IS_ERR(vol_args))
1481 return PTR_ERR(vol_args);
1482
1483 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1484 namelen = strlen(vol_args->name);
1485 if (strchr(vol_args->name, '/') ||
1486 strncmp(vol_args->name, "..", namelen) == 0) {
1487 err = -EINVAL;
1488 goto out;
1489 }
1490
1491 err = mnt_want_write(file->f_path.mnt);
1492 if (err)
1493 goto out;
1494
1495 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1496 dentry = lookup_one_len(vol_args->name, parent, namelen);
1497 if (IS_ERR(dentry)) {
1498 err = PTR_ERR(dentry);
1499 goto out_unlock_dir;
1500 }
1501
1502 if (!dentry->d_inode) {
1503 err = -ENOENT;
1504 goto out_dput;
1505 }
1506
1507 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001508 dest = BTRFS_I(inode)->root;
1509 if (!capable(CAP_SYS_ADMIN)){
1510 /*
1511 * Regular user. Only allow this with a special mount
1512 * option, when the user has write+exec access to the
1513 * subvol root, and when rmdir(2) would have been
1514 * allowed.
1515 *
1516 * Note that this is _not_ check that the subvol is
1517 * empty or doesn't contain data that we wouldn't
1518 * otherwise be able to delete.
1519 *
1520 * Users who want to delete empty subvols should try
1521 * rmdir(2).
1522 */
1523 err = -EPERM;
1524 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1525 goto out_dput;
1526
1527 /*
1528 * Do not allow deletion if the parent dir is the same
1529 * as the dir to be deleted. That means the ioctl
1530 * must be called on the dentry referencing the root
1531 * of the subvol, not a random directory contained
1532 * within it.
1533 */
1534 err = -EINVAL;
1535 if (root == dest)
1536 goto out_dput;
1537
1538 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1539 if (err)
1540 goto out_dput;
1541
1542 /* check if subvolume may be deleted by a non-root user */
1543 err = btrfs_may_delete(dir, dentry, 1);
1544 if (err)
1545 goto out_dput;
1546 }
1547
Yan, Zheng76dda932009-09-21 16:00:26 -04001548 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1549 err = -EINVAL;
1550 goto out_dput;
1551 }
1552
Yan, Zheng76dda932009-09-21 16:00:26 -04001553 mutex_lock(&inode->i_mutex);
1554 err = d_invalidate(dentry);
1555 if (err)
1556 goto out_unlock;
1557
1558 down_write(&root->fs_info->subvol_sem);
1559
1560 err = may_destroy_subvol(dest);
1561 if (err)
1562 goto out_up_write;
1563
Yan, Zhenga22285a2010-05-16 10:48:46 -04001564 trans = btrfs_start_transaction(root, 0);
1565 if (IS_ERR(trans)) {
1566 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001567 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001568 }
1569 trans->block_rsv = &root->fs_info->global_block_rsv;
1570
Yan, Zheng76dda932009-09-21 16:00:26 -04001571 ret = btrfs_unlink_subvol(trans, root, dir,
1572 dest->root_key.objectid,
1573 dentry->d_name.name,
1574 dentry->d_name.len);
1575 BUG_ON(ret);
1576
1577 btrfs_record_root_in_trans(trans, dest);
1578
1579 memset(&dest->root_item.drop_progress, 0,
1580 sizeof(dest->root_item.drop_progress));
1581 dest->root_item.drop_level = 0;
1582 btrfs_set_root_refs(&dest->root_item, 0);
1583
Yan, Zhengd68fc572010-05-16 10:49:58 -04001584 if (!xchg(&dest->orphan_item_inserted, 1)) {
1585 ret = btrfs_insert_orphan_item(trans,
1586 root->fs_info->tree_root,
1587 dest->root_key.objectid);
1588 BUG_ON(ret);
1589 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001590
Sage Weil531cb132010-10-29 15:41:32 -04001591 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001592 BUG_ON(ret);
1593 inode->i_flags |= S_DEAD;
1594out_up_write:
1595 up_write(&root->fs_info->subvol_sem);
1596out_unlock:
1597 mutex_unlock(&inode->i_mutex);
1598 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001599 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001600 btrfs_invalidate_inodes(dest);
1601 d_delete(dentry);
1602 }
1603out_dput:
1604 dput(dentry);
1605out_unlock_dir:
1606 mutex_unlock(&dir->i_mutex);
1607 mnt_drop_write(file->f_path.mnt);
1608out:
1609 kfree(vol_args);
1610 return err;
1611}
1612
Chris Mason1e701a32010-03-11 09:42:04 -05001613static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001614{
1615 struct inode *inode = fdentry(file)->d_inode;
1616 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001617 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001618 int ret;
1619
Li Zefanb83cc962010-12-20 16:04:08 +08001620 if (btrfs_root_readonly(root))
1621 return -EROFS;
1622
Yan Zhengc146afa2008-11-12 14:34:12 -05001623 ret = mnt_want_write(file->f_path.mnt);
1624 if (ret)
1625 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626
1627 switch (inode->i_mode & S_IFMT) {
1628 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001629 if (!capable(CAP_SYS_ADMIN)) {
1630 ret = -EPERM;
1631 goto out;
1632 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001633 ret = btrfs_defrag_root(root, 0);
1634 if (ret)
1635 goto out;
1636 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001637 break;
1638 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001639 if (!(file->f_mode & FMODE_WRITE)) {
1640 ret = -EINVAL;
1641 goto out;
1642 }
Chris Mason1e701a32010-03-11 09:42:04 -05001643
1644 range = kzalloc(sizeof(*range), GFP_KERNEL);
1645 if (!range) {
1646 ret = -ENOMEM;
1647 goto out;
1648 }
1649
1650 if (argp) {
1651 if (copy_from_user(range, argp,
1652 sizeof(*range))) {
1653 ret = -EFAULT;
1654 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001655 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001656 }
1657 /* compression requires us to start the IO */
1658 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1659 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1660 range->extent_thresh = (u32)-1;
1661 }
1662 } else {
1663 /* the rest are all set to zero by kzalloc */
1664 range->len = (u64)-1;
1665 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001666 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001667 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001668 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001669 default:
1670 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001671 }
Chris Masone441d542009-01-05 16:57:23 -05001672out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001673 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001674 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001675}
1676
Christoph Hellwigb2950862008-12-02 09:54:17 -05001677static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001678{
1679 struct btrfs_ioctl_vol_args *vol_args;
1680 int ret;
1681
Chris Masone441d542009-01-05 16:57:23 -05001682 if (!capable(CAP_SYS_ADMIN))
1683 return -EPERM;
1684
Li Zefandae7b662009-04-08 15:06:54 +08001685 vol_args = memdup_user(arg, sizeof(*vol_args));
1686 if (IS_ERR(vol_args))
1687 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001688
Mark Fasheh5516e592008-07-24 12:20:14 -04001689 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001690 ret = btrfs_init_new_device(root, vol_args->name);
1691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001692 kfree(vol_args);
1693 return ret;
1694}
1695
Christoph Hellwigb2950862008-12-02 09:54:17 -05001696static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001697{
1698 struct btrfs_ioctl_vol_args *vol_args;
1699 int ret;
1700
Chris Masone441d542009-01-05 16:57:23 -05001701 if (!capable(CAP_SYS_ADMIN))
1702 return -EPERM;
1703
Yan Zhengc146afa2008-11-12 14:34:12 -05001704 if (root->fs_info->sb->s_flags & MS_RDONLY)
1705 return -EROFS;
1706
Li Zefandae7b662009-04-08 15:06:54 +08001707 vol_args = memdup_user(arg, sizeof(*vol_args));
1708 if (IS_ERR(vol_args))
1709 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001710
Mark Fasheh5516e592008-07-24 12:20:14 -04001711 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001712 ret = btrfs_rm_device(root, vol_args->name);
1713
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001714 kfree(vol_args);
1715 return ret;
1716}
1717
Yan, Zheng76dda932009-09-21 16:00:26 -04001718static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1719 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720{
1721 struct inode *inode = fdentry(file)->d_inode;
1722 struct btrfs_root *root = BTRFS_I(inode)->root;
1723 struct file *src_file;
1724 struct inode *src;
1725 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001726 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001727 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001728 char *buf;
1729 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 u32 nritems;
1731 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001732 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001733 u64 len = olen;
1734 u64 bs = root->fs_info->sb->s_blocksize;
1735 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001736
Sage Weilc5c9cd42008-11-12 14:32:25 -05001737 /*
1738 * TODO:
1739 * - split compressed inline extents. annoying: we need to
1740 * decompress into destination's address_space (the file offset
1741 * may change, so source mapping won't do), then recompress (or
1742 * otherwise reinsert) a subrange.
1743 * - allow ranges within the same file to be cloned (provided
1744 * they don't overlap)?
1745 */
1746
Chris Masone441d542009-01-05 16:57:23 -05001747 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001748 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001749 return -EINVAL;
1750
Li Zefanb83cc962010-12-20 16:04:08 +08001751 if (btrfs_root_readonly(root))
1752 return -EROFS;
1753
Yan Zhengc146afa2008-11-12 14:34:12 -05001754 ret = mnt_want_write(file->f_path.mnt);
1755 if (ret)
1756 return ret;
1757
Sage Weilc5c9cd42008-11-12 14:32:25 -05001758 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001759 if (!src_file) {
1760 ret = -EBADF;
1761 goto out_drop_write;
1762 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001763
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 src = src_file->f_dentry->d_inode;
1765
Sage Weilc5c9cd42008-11-12 14:32:25 -05001766 ret = -EINVAL;
1767 if (src == inode)
1768 goto out_fput;
1769
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001770 /* the src must be open for reading */
1771 if (!(src_file->f_mode & FMODE_READ))
1772 goto out_fput;
1773
Yan Zhengae01a0a2008-08-04 23:23:47 -04001774 ret = -EISDIR;
1775 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001776 goto out_fput;
1777
Yan Zhengae01a0a2008-08-04 23:23:47 -04001778 ret = -EXDEV;
1779 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1780 goto out_fput;
1781
1782 ret = -ENOMEM;
1783 buf = vmalloc(btrfs_level_size(root, 0));
1784 if (!buf)
1785 goto out_fput;
1786
1787 path = btrfs_alloc_path();
1788 if (!path) {
1789 vfree(buf);
1790 goto out_fput;
1791 }
1792 path->reada = 2;
1793
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001794 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001795 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1796 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001797 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001798 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1799 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001800 }
1801
Sage Weilc5c9cd42008-11-12 14:32:25 -05001802 /* determine range to clone */
1803 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001804 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001805 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001806 if (len == 0)
1807 olen = len = src->i_size - off;
1808 /* if we extend to eof, continue to block boundary */
1809 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001810 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001811
1812 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001813 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1814 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001815 goto out_unlock;
1816
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001817 /* do any pending delalloc/csum calc on src, one way or
1818 another, and lock file content */
1819 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001820 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001821 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001822 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1823 if (!ordered &&
1824 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1825 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001826 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001827 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001828 if (ordered)
1829 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001830 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001831 }
1832
Sage Weilc5c9cd42008-11-12 14:32:25 -05001833 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001834 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001835 key.type = BTRFS_EXTENT_DATA_KEY;
1836 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001837
1838 while (1) {
1839 /*
1840 * note the key will change type as we walk through the
1841 * tree.
1842 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001843 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001844 if (ret < 0)
1845 goto out;
1846
Yan Zhengae01a0a2008-08-04 23:23:47 -04001847 nritems = btrfs_header_nritems(path->nodes[0]);
1848 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001849 ret = btrfs_next_leaf(root, path);
1850 if (ret < 0)
1851 goto out;
1852 if (ret > 0)
1853 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001854 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001855 }
1856 leaf = path->nodes[0];
1857 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001858
Yan Zhengae01a0a2008-08-04 23:23:47 -04001859 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001860 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001861 key.objectid != src->i_ino)
1862 break;
1863
Sage Weilc5c9cd42008-11-12 14:32:25 -05001864 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1865 struct btrfs_file_extent_item *extent;
1866 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001867 u32 size;
1868 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001869 u64 disko = 0, diskl = 0;
1870 u64 datao = 0, datal = 0;
1871 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001872 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001873
1874 size = btrfs_item_size_nr(leaf, slot);
1875 read_extent_buffer(leaf, buf,
1876 btrfs_item_ptr_offset(leaf, slot),
1877 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001878
1879 extent = btrfs_item_ptr(leaf, slot,
1880 struct btrfs_file_extent_item);
1881 comp = btrfs_file_extent_compression(leaf, extent);
1882 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001883 if (type == BTRFS_FILE_EXTENT_REG ||
1884 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001885 disko = btrfs_file_extent_disk_bytenr(leaf,
1886 extent);
1887 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1888 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001889 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001890 datal = btrfs_file_extent_num_bytes(leaf,
1891 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001892 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1893 /* take upper bound, may be compressed */
1894 datal = btrfs_file_extent_ram_bytes(leaf,
1895 extent);
1896 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001897 btrfs_release_path(root, path);
1898
Sage Weil050006a2010-10-29 15:37:33 -04001899 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001900 key.offset >= off+len)
1901 goto next;
1902
Zheng Yan31840ae2008-09-23 13:14:14 -04001903 memcpy(&new_key, &key, sizeof(new_key));
1904 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001905 if (off <= key.offset)
1906 new_key.offset = key.offset + destoff - off;
1907 else
1908 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001909
Yan, Zhenga22285a2010-05-16 10:48:46 -04001910 trans = btrfs_start_transaction(root, 1);
1911 if (IS_ERR(trans)) {
1912 ret = PTR_ERR(trans);
1913 goto out;
1914 }
1915
Chris Masonc8a894d2009-06-27 21:07:03 -04001916 if (type == BTRFS_FILE_EXTENT_REG ||
1917 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001918 if (off > key.offset) {
1919 datao += off - key.offset;
1920 datal -= off - key.offset;
1921 }
1922
1923 if (key.offset + datal > off + len)
1924 datal = off + len - key.offset;
1925
1926 ret = btrfs_drop_extents(trans, inode,
1927 new_key.offset,
1928 new_key.offset + datal,
1929 &hint_byte, 1);
1930 BUG_ON(ret);
1931
Sage Weilc5c9cd42008-11-12 14:32:25 -05001932 ret = btrfs_insert_empty_item(trans, root, path,
1933 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001934 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001935
1936 leaf = path->nodes[0];
1937 slot = path->slots[0];
1938 write_extent_buffer(leaf, buf,
1939 btrfs_item_ptr_offset(leaf, slot),
1940 size);
1941
1942 extent = btrfs_item_ptr(leaf, slot,
1943 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001944
Sage Weilc5c9cd42008-11-12 14:32:25 -05001945 /* disko == 0 means it's a hole */
1946 if (!disko)
1947 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001948
1949 btrfs_set_file_extent_offset(leaf, extent,
1950 datao);
1951 btrfs_set_file_extent_num_bytes(leaf, extent,
1952 datal);
1953 if (disko) {
1954 inode_add_bytes(inode, datal);
1955 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001956 disko, diskl, 0,
1957 root->root_key.objectid,
1958 inode->i_ino,
1959 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001960 BUG_ON(ret);
1961 }
1962 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1963 u64 skip = 0;
1964 u64 trim = 0;
1965 if (off > key.offset) {
1966 skip = off - key.offset;
1967 new_key.offset += skip;
1968 }
Chris Masond3977122009-01-05 21:25:51 -05001969
Sage Weilc5c9cd42008-11-12 14:32:25 -05001970 if (key.offset + datal > off+len)
1971 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001972
Sage Weilc5c9cd42008-11-12 14:32:25 -05001973 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001974 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001975 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001976 goto out;
1977 }
1978 size -= skip + trim;
1979 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001980
1981 ret = btrfs_drop_extents(trans, inode,
1982 new_key.offset,
1983 new_key.offset + datal,
1984 &hint_byte, 1);
1985 BUG_ON(ret);
1986
Sage Weilc5c9cd42008-11-12 14:32:25 -05001987 ret = btrfs_insert_empty_item(trans, root, path,
1988 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001989 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001990
1991 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001992 u32 start =
1993 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001994 memmove(buf+start, buf+start+skip,
1995 datal);
1996 }
1997
1998 leaf = path->nodes[0];
1999 slot = path->slots[0];
2000 write_extent_buffer(leaf, buf,
2001 btrfs_item_ptr_offset(leaf, slot),
2002 size);
2003 inode_add_bytes(inode, datal);
2004 }
2005
2006 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002007 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002008
Yan, Zhenga22285a2010-05-16 10:48:46 -04002009 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002010
2011 /*
2012 * we round up to the block size at eof when
2013 * determining which extents to clone above,
2014 * but shouldn't round up the file size
2015 */
2016 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002017 if (endoff > destoff+olen)
2018 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002019 if (endoff > inode->i_size)
2020 btrfs_i_size_write(inode, endoff);
2021
Yan, Zhenga22285a2010-05-16 10:48:46 -04002022 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2023 ret = btrfs_update_inode(trans, root, inode);
2024 BUG_ON(ret);
2025 btrfs_end_transaction(trans, root);
2026 }
Chris Masond3977122009-01-05 21:25:51 -05002027next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002028 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002029 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002030 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002031 ret = 0;
2032out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002033 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002034 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002035out_unlock:
2036 mutex_unlock(&src->i_mutex);
2037 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002038 vfree(buf);
2039 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002040out_fput:
2041 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002042out_drop_write:
2043 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002044 return ret;
2045}
2046
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002047static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002048{
2049 struct btrfs_ioctl_clone_range_args args;
2050
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002051 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002052 return -EFAULT;
2053 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2054 args.src_length, args.dest_offset);
2055}
2056
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002057/*
2058 * there are many ways the trans_start and trans_end ioctls can lead
2059 * to deadlocks. They should only be used by applications that
2060 * basically own the machine, and have a very in depth understanding
2061 * of all the possible deadlocks and enospc problems.
2062 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002063static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002064{
2065 struct inode *inode = fdentry(file)->d_inode;
2066 struct btrfs_root *root = BTRFS_I(inode)->root;
2067 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002068 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002069
Sage Weil1ab86ae2009-09-29 18:38:44 -04002070 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002071 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002072 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002073
2074 ret = -EINPROGRESS;
2075 if (file->private_data)
2076 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002077
Li Zefanb83cc962010-12-20 16:04:08 +08002078 ret = -EROFS;
2079 if (btrfs_root_readonly(root))
2080 goto out;
2081
Yan Zhengc146afa2008-11-12 14:34:12 -05002082 ret = mnt_want_write(file->f_path.mnt);
2083 if (ret)
2084 goto out;
2085
Sage Weil9ca9ee02008-08-04 10:41:27 -04002086 mutex_lock(&root->fs_info->trans_mutex);
2087 root->fs_info->open_ioctl_trans++;
2088 mutex_unlock(&root->fs_info->trans_mutex);
2089
Sage Weil1ab86ae2009-09-29 18:38:44 -04002090 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002091 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002092 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002093 goto out_drop;
2094
2095 file->private_data = trans;
2096 return 0;
2097
2098out_drop:
2099 mutex_lock(&root->fs_info->trans_mutex);
2100 root->fs_info->open_ioctl_trans--;
2101 mutex_unlock(&root->fs_info->trans_mutex);
2102 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002103out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002104 return ret;
2105}
2106
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002107static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2108{
2109 struct inode *inode = fdentry(file)->d_inode;
2110 struct btrfs_root *root = BTRFS_I(inode)->root;
2111 struct btrfs_root *new_root;
2112 struct btrfs_dir_item *di;
2113 struct btrfs_trans_handle *trans;
2114 struct btrfs_path *path;
2115 struct btrfs_key location;
2116 struct btrfs_disk_key disk_key;
2117 struct btrfs_super_block *disk_super;
2118 u64 features;
2119 u64 objectid = 0;
2120 u64 dir_id;
2121
2122 if (!capable(CAP_SYS_ADMIN))
2123 return -EPERM;
2124
2125 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2126 return -EFAULT;
2127
2128 if (!objectid)
2129 objectid = root->root_key.objectid;
2130
2131 location.objectid = objectid;
2132 location.type = BTRFS_ROOT_ITEM_KEY;
2133 location.offset = (u64)-1;
2134
2135 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2136 if (IS_ERR(new_root))
2137 return PTR_ERR(new_root);
2138
2139 if (btrfs_root_refs(&new_root->root_item) == 0)
2140 return -ENOENT;
2141
2142 path = btrfs_alloc_path();
2143 if (!path)
2144 return -ENOMEM;
2145 path->leave_spinning = 1;
2146
2147 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002148 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002149 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002150 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002151 }
2152
2153 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2154 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2155 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002156 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002157 btrfs_free_path(path);
2158 btrfs_end_transaction(trans, root);
2159 printk(KERN_ERR "Umm, you don't have the default dir item, "
2160 "this isn't going to work\n");
2161 return -ENOENT;
2162 }
2163
2164 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2165 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2166 btrfs_mark_buffer_dirty(path->nodes[0]);
2167 btrfs_free_path(path);
2168
2169 disk_super = &root->fs_info->super_copy;
2170 features = btrfs_super_incompat_flags(disk_super);
2171 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2172 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2173 btrfs_set_super_incompat_flags(disk_super, features);
2174 }
2175 btrfs_end_transaction(trans, root);
2176
2177 return 0;
2178}
2179
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002180static void get_block_group_info(struct list_head *groups_list,
2181 struct btrfs_ioctl_space_info *space)
2182{
2183 struct btrfs_block_group_cache *block_group;
2184
2185 space->total_bytes = 0;
2186 space->used_bytes = 0;
2187 space->flags = 0;
2188 list_for_each_entry(block_group, groups_list, list) {
2189 space->flags = block_group->flags;
2190 space->total_bytes += block_group->key.offset;
2191 space->used_bytes +=
2192 btrfs_block_group_used(&block_group->item);
2193 }
2194}
2195
Josef Bacik1406e432010-01-13 18:19:06 +00002196long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2197{
2198 struct btrfs_ioctl_space_args space_args;
2199 struct btrfs_ioctl_space_info space;
2200 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002201 struct btrfs_ioctl_space_info *dest_orig;
2202 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002203 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002204 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2205 BTRFS_BLOCK_GROUP_SYSTEM,
2206 BTRFS_BLOCK_GROUP_METADATA,
2207 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2208 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002209 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002210 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002211 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002212 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002213
2214 if (copy_from_user(&space_args,
2215 (struct btrfs_ioctl_space_args __user *)arg,
2216 sizeof(space_args)))
2217 return -EFAULT;
2218
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002219 for (i = 0; i < num_types; i++) {
2220 struct btrfs_space_info *tmp;
2221
2222 info = NULL;
2223 rcu_read_lock();
2224 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2225 list) {
2226 if (tmp->flags == types[i]) {
2227 info = tmp;
2228 break;
2229 }
2230 }
2231 rcu_read_unlock();
2232
2233 if (!info)
2234 continue;
2235
2236 down_read(&info->groups_sem);
2237 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2238 if (!list_empty(&info->block_groups[c]))
2239 slot_count++;
2240 }
2241 up_read(&info->groups_sem);
2242 }
Josef Bacik1406e432010-01-13 18:19:06 +00002243
Chris Mason7fde62b2010-03-16 15:40:10 -04002244 /* space_slots == 0 means they are asking for a count */
2245 if (space_args.space_slots == 0) {
2246 space_args.total_spaces = slot_count;
2247 goto out;
2248 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002249
Dan Rosenberg51788b12011-02-14 16:04:23 -05002250 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002251
Chris Mason7fde62b2010-03-16 15:40:10 -04002252 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002253
Chris Mason7fde62b2010-03-16 15:40:10 -04002254 /* we generally have at most 6 or so space infos, one for each raid
2255 * level. So, a whole page should be more than enough for everyone
2256 */
2257 if (alloc_size > PAGE_CACHE_SIZE)
2258 return -ENOMEM;
2259
2260 space_args.total_spaces = 0;
2261 dest = kmalloc(alloc_size, GFP_NOFS);
2262 if (!dest)
2263 return -ENOMEM;
2264 dest_orig = dest;
2265
2266 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002267 for (i = 0; i < num_types; i++) {
2268 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002269
Dan Rosenberg51788b12011-02-14 16:04:23 -05002270 if (!slot_count)
2271 break;
2272
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002273 info = NULL;
2274 rcu_read_lock();
2275 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2276 list) {
2277 if (tmp->flags == types[i]) {
2278 info = tmp;
2279 break;
2280 }
2281 }
2282 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002283
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002284 if (!info)
2285 continue;
2286 down_read(&info->groups_sem);
2287 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2288 if (!list_empty(&info->block_groups[c])) {
2289 get_block_group_info(&info->block_groups[c],
2290 &space);
2291 memcpy(dest, &space, sizeof(space));
2292 dest++;
2293 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002294 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002295 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002296 if (!slot_count)
2297 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002298 }
2299 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002300 }
Josef Bacik1406e432010-01-13 18:19:06 +00002301
Chris Mason7fde62b2010-03-16 15:40:10 -04002302 user_dest = (struct btrfs_ioctl_space_info *)
2303 (arg + sizeof(struct btrfs_ioctl_space_args));
2304
2305 if (copy_to_user(user_dest, dest_orig, alloc_size))
2306 ret = -EFAULT;
2307
2308 kfree(dest_orig);
2309out:
2310 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002311 ret = -EFAULT;
2312
2313 return ret;
2314}
2315
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002316/*
2317 * there are many ways the trans_start and trans_end ioctls can lead
2318 * to deadlocks. They should only be used by applications that
2319 * basically own the machine, and have a very in depth understanding
2320 * of all the possible deadlocks and enospc problems.
2321 */
2322long btrfs_ioctl_trans_end(struct file *file)
2323{
2324 struct inode *inode = fdentry(file)->d_inode;
2325 struct btrfs_root *root = BTRFS_I(inode)->root;
2326 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002327
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002328 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002329 if (!trans)
2330 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002331 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002332
Sage Weil1ab86ae2009-09-29 18:38:44 -04002333 btrfs_end_transaction(trans, root);
2334
Sage Weil9ca9ee02008-08-04 10:41:27 -04002335 mutex_lock(&root->fs_info->trans_mutex);
2336 root->fs_info->open_ioctl_trans--;
2337 mutex_unlock(&root->fs_info->trans_mutex);
2338
Sage Weilcfc8ea82008-12-11 16:30:06 -05002339 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002340 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002341}
2342
Sage Weil46204592010-10-29 15:41:32 -04002343static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2344{
2345 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2346 struct btrfs_trans_handle *trans;
2347 u64 transid;
2348
2349 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002350 if (IS_ERR(trans))
2351 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002352 transid = trans->transid;
2353 btrfs_commit_transaction_async(trans, root, 0);
2354
2355 if (argp)
2356 if (copy_to_user(argp, &transid, sizeof(transid)))
2357 return -EFAULT;
2358 return 0;
2359}
2360
2361static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2362{
2363 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2364 u64 transid;
2365
2366 if (argp) {
2367 if (copy_from_user(&transid, argp, sizeof(transid)))
2368 return -EFAULT;
2369 } else {
2370 transid = 0; /* current trans */
2371 }
2372 return btrfs_wait_for_commit(root, transid);
2373}
2374
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002375long btrfs_ioctl(struct file *file, unsigned int
2376 cmd, unsigned long arg)
2377{
2378 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002379 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002380
2381 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002382 case FS_IOC_GETFLAGS:
2383 return btrfs_ioctl_getflags(file, argp);
2384 case FS_IOC_SETFLAGS:
2385 return btrfs_ioctl_setflags(file, argp);
2386 case FS_IOC_GETVERSION:
2387 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002388 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002389 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002390 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002391 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002392 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002393 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002394 case BTRFS_IOC_SNAP_DESTROY:
2395 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002396 case BTRFS_IOC_SUBVOL_GETFLAGS:
2397 return btrfs_ioctl_subvol_getflags(file, argp);
2398 case BTRFS_IOC_SUBVOL_SETFLAGS:
2399 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002400 case BTRFS_IOC_DEFAULT_SUBVOL:
2401 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002402 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002403 return btrfs_ioctl_defrag(file, NULL);
2404 case BTRFS_IOC_DEFRAG_RANGE:
2405 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002406 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002407 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002408 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002409 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002411 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002412 case BTRFS_IOC_BALANCE:
2413 return btrfs_balance(root->fs_info->dev_root);
2414 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002415 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2416 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002417 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002418 case BTRFS_IOC_TRANS_START:
2419 return btrfs_ioctl_trans_start(file);
2420 case BTRFS_IOC_TRANS_END:
2421 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002422 case BTRFS_IOC_TREE_SEARCH:
2423 return btrfs_ioctl_tree_search(file, argp);
2424 case BTRFS_IOC_INO_LOOKUP:
2425 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002426 case BTRFS_IOC_SPACE_INFO:
2427 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002428 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002429 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2430 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002431 case BTRFS_IOC_START_SYNC:
2432 return btrfs_ioctl_start_sync(file, argp);
2433 case BTRFS_IOC_WAIT_SYNC:
2434 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002435 }
2436
2437 return -ENOTTY;
2438}