blob: 9b3d73a0fdc85a26d0ea6870fa9ecd7bef8a31cd [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"
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +000052#include "ctree.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040053
Christoph Hellwig6cbff002009-04-17 10:37:41 +020054/* Mask out flags that are inappropriate for the given type of inode. */
55static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
56{
57 if (S_ISDIR(mode))
58 return flags;
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
61 else
62 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
63}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040064
Christoph Hellwig6cbff002009-04-17 10:37:41 +020065/*
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 */
68static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69{
70 unsigned int iflags = 0;
71
72 if (flags & BTRFS_INODE_SYNC)
73 iflags |= FS_SYNC_FL;
74 if (flags & BTRFS_INODE_IMMUTABLE)
75 iflags |= FS_IMMUTABLE_FL;
76 if (flags & BTRFS_INODE_APPEND)
77 iflags |= FS_APPEND_FL;
78 if (flags & BTRFS_INODE_NODUMP)
79 iflags |= FS_NODUMP_FL;
80 if (flags & BTRFS_INODE_NOATIME)
81 iflags |= FS_NOATIME_FL;
82 if (flags & BTRFS_INODE_DIRSYNC)
83 iflags |= FS_DIRSYNC_FL;
84
85 return iflags;
86}
87
88/*
89 * Update inode->i_flags based on the btrfs internal flags.
90 */
91void btrfs_update_iflags(struct inode *inode)
92{
93 struct btrfs_inode *ip = BTRFS_I(inode);
94
95 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96
97 if (ip->flags & BTRFS_INODE_SYNC)
98 inode->i_flags |= S_SYNC;
99 if (ip->flags & BTRFS_INODE_IMMUTABLE)
100 inode->i_flags |= S_IMMUTABLE;
101 if (ip->flags & BTRFS_INODE_APPEND)
102 inode->i_flags |= S_APPEND;
103 if (ip->flags & BTRFS_INODE_NOATIME)
104 inode->i_flags |= S_NOATIME;
105 if (ip->flags & BTRFS_INODE_DIRSYNC)
106 inode->i_flags |= S_DIRSYNC;
107}
108
109/*
110 * Inherit flags from the parent inode.
111 *
112 * Unlike extN we don't have any flags we don't want to inherit currently.
113 */
114void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400116 unsigned int flags;
117
118 if (!dir)
119 return;
120
121 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122
123 if (S_ISREG(inode->i_mode))
124 flags &= ~BTRFS_INODE_DIRSYNC;
125 else if (!S_ISDIR(inode->i_mode))
126 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127
128 BTRFS_I(inode)->flags = flags;
129 btrfs_update_iflags(inode);
130}
131
132static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133{
134 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
135 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136
137 if (copy_to_user(arg, &flags, sizeof(flags)))
138 return -EFAULT;
139 return 0;
140}
141
142static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
143{
144 struct inode *inode = file->f_path.dentry->d_inode;
145 struct btrfs_inode *ip = BTRFS_I(inode);
146 struct btrfs_root *root = ip->root;
147 struct btrfs_trans_handle *trans;
148 unsigned int flags, oldflags;
149 int ret;
150
151 if (copy_from_user(&flags, arg, sizeof(flags)))
152 return -EFAULT;
153
154 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
155 FS_NOATIME_FL | FS_NODUMP_FL | \
156 FS_SYNC_FL | FS_DIRSYNC_FL))
157 return -EOPNOTSUPP;
158
159 if (!is_owner_or_cap(inode))
160 return -EACCES;
161
162 mutex_lock(&inode->i_mutex);
163
164 flags = btrfs_mask_flags(inode->i_mode, flags);
165 oldflags = btrfs_flags_to_ioctl(ip->flags);
166 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
167 if (!capable(CAP_LINUX_IMMUTABLE)) {
168 ret = -EPERM;
169 goto out_unlock;
170 }
171 }
172
173 ret = mnt_want_write(file->f_path.mnt);
174 if (ret)
175 goto out_unlock;
176
177 if (flags & FS_SYNC_FL)
178 ip->flags |= BTRFS_INODE_SYNC;
179 else
180 ip->flags &= ~BTRFS_INODE_SYNC;
181 if (flags & FS_IMMUTABLE_FL)
182 ip->flags |= BTRFS_INODE_IMMUTABLE;
183 else
184 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
185 if (flags & FS_APPEND_FL)
186 ip->flags |= BTRFS_INODE_APPEND;
187 else
188 ip->flags &= ~BTRFS_INODE_APPEND;
189 if (flags & FS_NODUMP_FL)
190 ip->flags |= BTRFS_INODE_NODUMP;
191 else
192 ip->flags &= ~BTRFS_INODE_NODUMP;
193 if (flags & FS_NOATIME_FL)
194 ip->flags |= BTRFS_INODE_NOATIME;
195 else
196 ip->flags &= ~BTRFS_INODE_NOATIME;
197 if (flags & FS_DIRSYNC_FL)
198 ip->flags |= BTRFS_INODE_DIRSYNC;
199 else
200 ip->flags &= ~BTRFS_INODE_DIRSYNC;
201
202
203 trans = btrfs_join_transaction(root, 1);
204 BUG_ON(!trans);
205
206 ret = btrfs_update_inode(trans, root, inode);
207 BUG_ON(ret);
208
209 btrfs_update_iflags(inode);
210 inode->i_ctime = CURRENT_TIME;
211 btrfs_end_transaction(trans, root);
212
213 mnt_drop_write(file->f_path.mnt);
214 out_unlock:
215 mutex_unlock(&inode->i_mutex);
216 return 0;
217}
218
219static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
220{
221 struct inode *inode = file->f_path.dentry->d_inode;
222
223 return put_user(inode->i_generation, arg);
224}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400225
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400226static noinline int create_subvol(struct btrfs_root *root,
227 struct dentry *dentry,
228 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400229{
230 struct btrfs_trans_handle *trans;
231 struct btrfs_key key;
232 struct btrfs_root_item root_item;
233 struct btrfs_inode_item *inode_item;
234 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400235 struct btrfs_root *new_root;
236 struct inode *dir = dentry->d_parent->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400237 int ret;
238 int err;
239 u64 objectid;
240 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500241 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400242
Josef Bacik9ed74f22009-09-11 16:12:44 -0400243 /*
244 * 1 - inode item
245 * 2 - refs
246 * 1 - root item
247 * 2 - dir items
248 */
249 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400250 if (ret)
Yan, Zheng76dda932009-09-21 16:00:26 -0400251 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400252
253 trans = btrfs_start_transaction(root, 1);
254 BUG_ON(!trans);
255
256 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
257 0, &objectid);
258 if (ret)
259 goto fail;
260
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
262 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400263 if (IS_ERR(leaf)) {
264 ret = PTR_ERR(leaf);
265 goto fail;
266 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400267
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400268 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400269 btrfs_set_header_bytenr(leaf, leaf->start);
270 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400271 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400272 btrfs_set_header_owner(leaf, objectid);
273
274 write_extent_buffer(leaf, root->fs_info->fsid,
275 (unsigned long)btrfs_header_fsid(leaf),
276 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400277 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
278 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
279 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400280 btrfs_mark_buffer_dirty(leaf);
281
282 inode_item = &root_item.inode;
283 memset(inode_item, 0, sizeof(*inode_item));
284 inode_item->generation = cpu_to_le64(1);
285 inode_item->size = cpu_to_le64(3);
286 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400287 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400288 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
289
290 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400291 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400292 btrfs_set_root_level(&root_item, 0);
293 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000294 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400295 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400296
297 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
298 root_item.drop_level = 0;
299
Chris Mason925baed2008-06-25 16:01:30 -0400300 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400301 free_extent_buffer(leaf);
302 leaf = NULL;
303
304 btrfs_set_root_dirid(&root_item, new_dirid);
305
306 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400307 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400308 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
309 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
310 &root_item);
311 if (ret)
312 goto fail;
313
Yan, Zheng76dda932009-09-21 16:00:26 -0400314 key.offset = (u64)-1;
315 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
316 BUG_ON(IS_ERR(new_root));
317
318 btrfs_record_root_in_trans(trans, new_root);
319
320 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
321 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400322 /*
323 * insert the directory item
324 */
Chris Mason3de45862008-11-17 21:02:50 -0500325 ret = btrfs_set_inode_index(dir, &index);
326 BUG_ON(ret);
327
328 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400329 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500330 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400331 if (ret)
332 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500333
Yan Zheng52c26172009-01-05 15:43:43 -0500334 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
335 ret = btrfs_update_inode(trans, root, dir);
336 BUG_ON(ret);
337
Chris Mason0660b5a2008-11-17 20:37:39 -0500338 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400339 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500340 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400341
Chris Mason0660b5a2008-11-17 20:37:39 -0500342 BUG_ON(ret);
343
Yan, Zheng76dda932009-09-21 16:00:26 -0400344 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400345fail:
Yan, Zheng76dda932009-09-21 16:00:26 -0400346 err = btrfs_commit_transaction(trans, root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400347 if (err && !ret)
348 ret = err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400349
350 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400351 return ret;
352}
353
Chris Mason3de45862008-11-17 21:02:50 -0500354static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
355 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400356{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000357 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400358 struct btrfs_pending_snapshot *pending_snapshot;
359 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000360 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400361
362 if (!root->ref_cows)
363 return -EINVAL;
364
Josef Bacik9ed74f22009-09-11 16:12:44 -0400365 /*
366 * 1 - inode item
367 * 2 - refs
368 * 1 - root item
369 * 2 - dir items
370 */
371 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400372 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000373 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400374
Chris Mason3de45862008-11-17 21:02:50 -0500375 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376 if (!pending_snapshot) {
377 ret = -ENOMEM;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400378 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000379 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 }
381 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
382 if (!pending_snapshot->name) {
383 ret = -ENOMEM;
384 kfree(pending_snapshot);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400385 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000386 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400387 }
388 memcpy(pending_snapshot->name, name, namelen);
389 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500390 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400391 trans = btrfs_start_transaction(root, 1);
392 BUG_ON(!trans);
393 pending_snapshot->root = root;
394 list_add(&pending_snapshot->list,
395 &trans->transaction->pending_snapshots);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000396 ret = btrfs_commit_transaction(trans, root);
397 BUG_ON(ret);
398 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400399
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000400 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
401 if (IS_ERR(inode)) {
402 ret = PTR_ERR(inode);
403 goto fail;
404 }
405 BUG_ON(!inode);
406 d_instantiate(dentry, inode);
407 ret = 0;
408fail:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409 return ret;
410}
411
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400412/* copy of may_create in fs/namei.c() */
413static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
414{
415 if (child->d_inode)
416 return -EEXIST;
417 if (IS_DEADDIR(dir))
418 return -ENOENT;
419 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
420}
421
422/*
423 * Create a new subvolume below @parent. This is largely modeled after
424 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
425 * inside this filesystem so it's quite a bit simpler.
426 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400427static noinline int btrfs_mksubvol(struct path *parent,
428 char *name, int namelen,
Chris Mason3de45862008-11-17 21:02:50 -0500429 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400430{
Yan, Zheng76dda932009-09-21 16:00:26 -0400431 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400432 struct dentry *dentry;
433 int error;
434
Yan, Zheng76dda932009-09-21 16:00:26 -0400435 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400436
437 dentry = lookup_one_len(name, parent->dentry, namelen);
438 error = PTR_ERR(dentry);
439 if (IS_ERR(dentry))
440 goto out_unlock;
441
442 error = -EEXIST;
443 if (dentry->d_inode)
444 goto out_dput;
445
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400446 error = mnt_want_write(parent->mnt);
447 if (error)
448 goto out_dput;
449
Yan, Zheng76dda932009-09-21 16:00:26 -0400450 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400451 if (error)
452 goto out_drop_write;
453
Yan, Zheng76dda932009-09-21 16:00:26 -0400454 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
455
456 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
457 goto out_up_read;
458
Chris Mason3de45862008-11-17 21:02:50 -0500459 if (snap_src) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400460 error = create_snapshot(snap_src, dentry,
461 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500462 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400463 error = create_subvol(BTRFS_I(dir)->root, dentry,
464 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500465 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400466 if (!error)
467 fsnotify_mkdir(dir, dentry);
468out_up_read:
469 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400470out_drop_write:
471 mnt_drop_write(parent->mnt);
472out_dput:
473 dput(dentry);
474out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400475 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400476 return error;
477}
478
Chris Mason940100a2010-03-10 10:52:59 -0500479static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500480 int thresh, u64 *last_len, u64 *skip,
481 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500482{
483 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
484 struct extent_map *em = NULL;
485 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
486 int ret = 1;
487
Chris Mason1e701a32010-03-11 09:42:04 -0500488
489 if (thresh == 0)
490 thresh = 256 * 1024;
491
Chris Mason940100a2010-03-10 10:52:59 -0500492 /*
493 * make sure that once we start defragging and extent, we keep on
494 * defragging it
495 */
496 if (start < *defrag_end)
497 return 1;
498
499 *skip = 0;
500
501 /*
502 * hopefully we have this extent in the tree already, try without
503 * the full extent lock
504 */
505 read_lock(&em_tree->lock);
506 em = lookup_extent_mapping(em_tree, start, len);
507 read_unlock(&em_tree->lock);
508
509 if (!em) {
510 /* get the big lock and read metadata off disk */
511 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
512 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
513 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
514
515 if (!em)
516 return 0;
517 }
518
519 /* this will cover holes, and inline extents */
520 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
521 ret = 0;
522
523 /*
524 * we hit a real extent, if it is big don't bother defragging it again
525 */
Chris Mason1e701a32010-03-11 09:42:04 -0500526 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500527 ret = 0;
528
529 /*
530 * last_len ends up being a counter of how many bytes we've defragged.
531 * every time we choose not to defrag an extent, we reset *last_len
532 * so that the next tiny extent will force a defrag.
533 *
534 * The end result of this is that tiny extents before a single big
535 * extent will force at least part of that big extent to be defragged.
536 */
537 if (ret) {
538 *last_len += len;
539 *defrag_end = extent_map_end(em);
540 } else {
541 *last_len = 0;
542 *skip = extent_map_end(em);
543 *defrag_end = 0;
544 }
545
546 free_extent_map(em);
547 return ret;
548}
549
Chris Mason1e701a32010-03-11 09:42:04 -0500550static int btrfs_defrag_file(struct file *file,
551 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400552{
553 struct inode *inode = fdentry(file)->d_inode;
554 struct btrfs_root *root = BTRFS_I(inode)->root;
555 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400556 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 struct page *page;
558 unsigned long last_index;
559 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
560 unsigned long total_read = 0;
561 u64 page_start;
562 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500563 u64 last_len = 0;
564 u64 skip = 0;
565 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400566 unsigned long i;
567 int ret;
568
Chris Mason940100a2010-03-10 10:52:59 -0500569 if (inode->i_size == 0)
570 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400571
Chris Mason1e701a32010-03-11 09:42:04 -0500572 if (range->start + range->len > range->start) {
573 last_index = min_t(u64, inode->i_size - 1,
574 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
575 } else {
576 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
577 }
578
579 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500580 while (i <= last_index) {
581 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500582 PAGE_CACHE_SIZE,
583 range->extent_thresh,
584 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500585 &defrag_end)) {
586 unsigned long next;
587 /*
588 * the should_defrag function tells us how much to skip
589 * bump our counter by the suggested amount
590 */
591 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
592 i = max(i + 1, next);
593 continue;
594 }
595
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400596 if (total_read % ra_pages == 0) {
597 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
598 min(last_index, i + ra_pages - 1));
599 }
600 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500601 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500602 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
603 BTRFS_I(inode)->force_compress = 1;
Chris Mason940100a2010-03-10 10:52:59 -0500604
605 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
606 if (ret) {
607 ret = -ENOSPC;
608 break;
609 }
610
611 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
612 if (ret) {
613 btrfs_free_reserved_data_space(root, inode,
614 PAGE_CACHE_SIZE);
615 ret = -ENOSPC;
616 break;
617 }
Chris Mason3eaa2882008-07-24 11:57:52 -0400618again:
Chris Mason940100a2010-03-10 10:52:59 -0500619 if (inode->i_size == 0 ||
620 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
621 ret = 0;
622 goto err_reservations;
623 }
624
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400625 page = grab_cache_page(inode->i_mapping, i);
626 if (!page)
Chris Mason940100a2010-03-10 10:52:59 -0500627 goto err_reservations;
628
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400629 if (!PageUptodate(page)) {
630 btrfs_readpage(NULL, page);
631 lock_page(page);
632 if (!PageUptodate(page)) {
633 unlock_page(page);
634 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500635 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400636 }
637 }
638
Chris Mason940100a2010-03-10 10:52:59 -0500639 if (page->mapping != inode->i_mapping) {
640 unlock_page(page);
641 page_cache_release(page);
642 goto again;
643 }
644
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400645 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646
Chris Mason940100a2010-03-10 10:52:59 -0500647 if (PageDirty(page)) {
648 btrfs_free_reserved_data_space(root, inode,
649 PAGE_CACHE_SIZE);
650 goto loop_unlock;
651 }
652
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400653 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
654 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400655 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400656
657 ordered = btrfs_lookup_ordered_extent(inode, page_start);
658 if (ordered) {
659 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
660 unlock_page(page);
661 page_cache_release(page);
662 btrfs_start_ordered_extent(inode, ordered, 1);
663 btrfs_put_ordered_extent(ordered);
664 goto again;
665 }
666 set_page_extent_mapped(page);
667
Chris Masonf87f0572008-08-01 11:27:23 -0400668 /*
669 * this makes sure page_mkwrite is called on the
670 * page if it is dirtied again later
671 */
672 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500673 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
674 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
675 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400676
Josef Bacik2ac55d42010-02-03 19:33:23 +0000677 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500678 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400679 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400680 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500681
682loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400683 unlock_page(page);
684 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500685 mutex_unlock(&inode->i_mutex);
686
687 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400688 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500689 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400690 }
691
Chris Mason1e701a32010-03-11 09:42:04 -0500692 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
693 filemap_flush(inode->i_mapping);
694
695 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
696 /* the filemap_flush will queue IO into the worker threads, but
697 * we have to make sure the IO is actually started and that
698 * ordered extents get created before we return
699 */
700 atomic_inc(&root->fs_info->async_submit_draining);
701 while (atomic_read(&root->fs_info->nr_async_submits) ||
702 atomic_read(&root->fs_info->async_delalloc_pages)) {
703 wait_event(root->fs_info->async_submit_wait,
704 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
705 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
706 }
707 atomic_dec(&root->fs_info->async_submit_draining);
708
709 mutex_lock(&inode->i_mutex);
710 BTRFS_I(inode)->force_compress = 0;
711 mutex_unlock(&inode->i_mutex);
712 }
713
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400714 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500715
716err_reservations:
717 mutex_unlock(&inode->i_mutex);
718 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
719 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
720 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400721}
722
Yan, Zheng76dda932009-09-21 16:00:26 -0400723static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
724 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400725{
726 u64 new_size;
727 u64 old_size;
728 u64 devid = 1;
729 struct btrfs_ioctl_vol_args *vol_args;
730 struct btrfs_trans_handle *trans;
731 struct btrfs_device *device = NULL;
732 char *sizestr;
733 char *devstr = NULL;
734 int ret = 0;
735 int namelen;
736 int mod = 0;
737
Yan Zhengc146afa2008-11-12 14:34:12 -0500738 if (root->fs_info->sb->s_flags & MS_RDONLY)
739 return -EROFS;
740
Chris Masone441d542009-01-05 16:57:23 -0500741 if (!capable(CAP_SYS_ADMIN))
742 return -EPERM;
743
Li Zefandae7b662009-04-08 15:06:54 +0800744 vol_args = memdup_user(arg, sizeof(*vol_args));
745 if (IS_ERR(vol_args))
746 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400747
748 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400750
Chris Mason7d9eb122008-07-08 14:19:17 -0400751 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752 sizestr = vol_args->name;
753 devstr = strchr(sizestr, ':');
754 if (devstr) {
755 char *end;
756 sizestr = devstr + 1;
757 *devstr = '\0';
758 devstr = vol_args->name;
759 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700760 printk(KERN_INFO "resizing devid %llu\n",
761 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400762 }
Yan Zheng2b820322008-11-17 21:11:30 -0500763 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400764 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700765 printk(KERN_INFO "resizer unable to find device %llu\n",
766 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400767 ret = -EINVAL;
768 goto out_unlock;
769 }
770 if (!strcmp(sizestr, "max"))
771 new_size = device->bdev->bd_inode->i_size;
772 else {
773 if (sizestr[0] == '-') {
774 mod = -1;
775 sizestr++;
776 } else if (sizestr[0] == '+') {
777 mod = 1;
778 sizestr++;
779 }
Akinobu Mita91748462010-02-28 10:59:11 +0000780 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400781 if (new_size == 0) {
782 ret = -EINVAL;
783 goto out_unlock;
784 }
785 }
786
787 old_size = device->total_bytes;
788
789 if (mod < 0) {
790 if (new_size > old_size) {
791 ret = -EINVAL;
792 goto out_unlock;
793 }
794 new_size = old_size - new_size;
795 } else if (mod > 0) {
796 new_size = old_size + new_size;
797 }
798
799 if (new_size < 256 * 1024 * 1024) {
800 ret = -EINVAL;
801 goto out_unlock;
802 }
803 if (new_size > device->bdev->bd_inode->i_size) {
804 ret = -EFBIG;
805 goto out_unlock;
806 }
807
808 do_div(new_size, root->sectorsize);
809 new_size *= root->sectorsize;
810
811 printk(KERN_INFO "new size for %s is %llu\n",
812 device->name, (unsigned long long)new_size);
813
814 if (new_size > old_size) {
815 trans = btrfs_start_transaction(root, 1);
816 ret = btrfs_grow_device(trans, device, new_size);
817 btrfs_commit_transaction(trans, root);
818 } else {
819 ret = btrfs_shrink_device(device, new_size);
820 }
821
822out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400823 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400824 kfree(vol_args);
825 return ret;
826}
827
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400828static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500829 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400830{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400831 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400832 struct btrfs_ioctl_vol_args *vol_args;
Chris Mason3de45862008-11-17 21:02:50 -0500833 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400834 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500835 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400836
Yan Zhengc146afa2008-11-12 14:34:12 -0500837 if (root->fs_info->sb->s_flags & MS_RDONLY)
838 return -EROFS;
839
Li Zefandae7b662009-04-08 15:06:54 +0800840 vol_args = memdup_user(arg, sizeof(*vol_args));
841 if (IS_ERR(vol_args))
842 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400843
Mark Fasheh5516e592008-07-24 12:20:14 -0400844 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400845 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400846 if (strchr(vol_args->name, '/')) {
847 ret = -EINVAL;
848 goto out;
849 }
850
Chris Mason3de45862008-11-17 21:02:50 -0500851 if (subvol) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400852 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
853 NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400854 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500855 struct inode *src_inode;
856 src_file = fget(vol_args->fd);
857 if (!src_file) {
858 ret = -EINVAL;
859 goto out;
860 }
861
862 src_inode = src_file->f_path.dentry->d_inode;
863 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500864 printk(KERN_INFO "btrfs: Snapshot src from "
865 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500866 ret = -EINVAL;
867 fput(src_file);
868 goto out;
869 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400870 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
871 BTRFS_I(src_inode)->root);
Chris Mason3de45862008-11-17 21:02:50 -0500872 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400873 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400874out:
875 kfree(vol_args);
876 return ret;
877}
878
Yan, Zheng76dda932009-09-21 16:00:26 -0400879/*
880 * helper to check if the subvolume references other subvolumes
881 */
882static noinline int may_destroy_subvol(struct btrfs_root *root)
883{
884 struct btrfs_path *path;
885 struct btrfs_key key;
886 int ret;
887
888 path = btrfs_alloc_path();
889 if (!path)
890 return -ENOMEM;
891
892 key.objectid = root->root_key.objectid;
893 key.type = BTRFS_ROOT_REF_KEY;
894 key.offset = (u64)-1;
895
896 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
897 &key, path, 0, 0);
898 if (ret < 0)
899 goto out;
900 BUG_ON(ret == 0);
901
902 ret = 0;
903 if (path->slots[0] > 0) {
904 path->slots[0]--;
905 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
906 if (key.objectid == root->root_key.objectid &&
907 key.type == BTRFS_ROOT_REF_KEY)
908 ret = -ENOTEMPTY;
909 }
910out:
911 btrfs_free_path(path);
912 return ret;
913}
914
Chris Masonac8e9812010-02-28 15:39:26 -0500915static noinline int key_in_sk(struct btrfs_key *key,
916 struct btrfs_ioctl_search_key *sk)
917{
Chris Masonabc6e132010-03-18 12:10:08 -0400918 struct btrfs_key test;
919 int ret;
920
921 test.objectid = sk->min_objectid;
922 test.type = sk->min_type;
923 test.offset = sk->min_offset;
924
925 ret = btrfs_comp_cpu_keys(key, &test);
926 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -0500927 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -0400928
929 test.objectid = sk->max_objectid;
930 test.type = sk->max_type;
931 test.offset = sk->max_offset;
932
933 ret = btrfs_comp_cpu_keys(key, &test);
934 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -0500935 return 0;
936 return 1;
937}
938
939static noinline int copy_to_sk(struct btrfs_root *root,
940 struct btrfs_path *path,
941 struct btrfs_key *key,
942 struct btrfs_ioctl_search_key *sk,
943 char *buf,
944 unsigned long *sk_offset,
945 int *num_found)
946{
947 u64 found_transid;
948 struct extent_buffer *leaf;
949 struct btrfs_ioctl_search_header sh;
950 unsigned long item_off;
951 unsigned long item_len;
952 int nritems;
953 int i;
954 int slot;
955 int found = 0;
956 int ret = 0;
957
958 leaf = path->nodes[0];
959 slot = path->slots[0];
960 nritems = btrfs_header_nritems(leaf);
961
962 if (btrfs_header_generation(leaf) > sk->max_transid) {
963 i = nritems;
964 goto advance_key;
965 }
966 found_transid = btrfs_header_generation(leaf);
967
968 for (i = slot; i < nritems; i++) {
969 item_off = btrfs_item_ptr_offset(leaf, i);
970 item_len = btrfs_item_size_nr(leaf, i);
971
972 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
973 item_len = 0;
974
975 if (sizeof(sh) + item_len + *sk_offset >
976 BTRFS_SEARCH_ARGS_BUFSIZE) {
977 ret = 1;
978 goto overflow;
979 }
980
981 btrfs_item_key_to_cpu(leaf, key, i);
982 if (!key_in_sk(key, sk))
983 continue;
984
985 sh.objectid = key->objectid;
986 sh.offset = key->offset;
987 sh.type = key->type;
988 sh.len = item_len;
989 sh.transid = found_transid;
990
991 /* copy search result header */
992 memcpy(buf + *sk_offset, &sh, sizeof(sh));
993 *sk_offset += sizeof(sh);
994
995 if (item_len) {
996 char *p = buf + *sk_offset;
997 /* copy the item */
998 read_extent_buffer(leaf, p,
999 item_off, item_len);
1000 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001001 }
Chris Mason90fdde12010-03-18 12:14:54 -04001002 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001003
1004 if (*num_found >= sk->nr_items)
1005 break;
1006 }
1007advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001008 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001009 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1010 key->offset++;
1011 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1012 key->offset = 0;
1013 key->type++;
1014 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1015 key->offset = 0;
1016 key->type = 0;
1017 key->objectid++;
1018 } else
1019 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001020overflow:
1021 *num_found += found;
1022 return ret;
1023}
1024
1025static noinline int search_ioctl(struct inode *inode,
1026 struct btrfs_ioctl_search_args *args)
1027{
1028 struct btrfs_root *root;
1029 struct btrfs_key key;
1030 struct btrfs_key max_key;
1031 struct btrfs_path *path;
1032 struct btrfs_ioctl_search_key *sk = &args->key;
1033 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1034 int ret;
1035 int num_found = 0;
1036 unsigned long sk_offset = 0;
1037
1038 path = btrfs_alloc_path();
1039 if (!path)
1040 return -ENOMEM;
1041
1042 if (sk->tree_id == 0) {
1043 /* search the root of the inode that was passed */
1044 root = BTRFS_I(inode)->root;
1045 } else {
1046 key.objectid = sk->tree_id;
1047 key.type = BTRFS_ROOT_ITEM_KEY;
1048 key.offset = (u64)-1;
1049 root = btrfs_read_fs_root_no_name(info, &key);
1050 if (IS_ERR(root)) {
1051 printk(KERN_ERR "could not find root %llu\n",
1052 sk->tree_id);
1053 btrfs_free_path(path);
1054 return -ENOENT;
1055 }
1056 }
1057
1058 key.objectid = sk->min_objectid;
1059 key.type = sk->min_type;
1060 key.offset = sk->min_offset;
1061
1062 max_key.objectid = sk->max_objectid;
1063 max_key.type = sk->max_type;
1064 max_key.offset = sk->max_offset;
1065
1066 path->keep_locks = 1;
1067
1068 while(1) {
1069 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1070 sk->min_transid);
1071 if (ret != 0) {
1072 if (ret > 0)
1073 ret = 0;
1074 goto err;
1075 }
1076 ret = copy_to_sk(root, path, &key, sk, args->buf,
1077 &sk_offset, &num_found);
1078 btrfs_release_path(root, path);
1079 if (ret || num_found >= sk->nr_items)
1080 break;
1081
1082 }
1083 ret = 0;
1084err:
1085 sk->nr_items = num_found;
1086 btrfs_free_path(path);
1087 return ret;
1088}
1089
1090static noinline int btrfs_ioctl_tree_search(struct file *file,
1091 void __user *argp)
1092{
1093 struct btrfs_ioctl_search_args *args;
1094 struct inode *inode;
1095 int ret;
1096
1097 if (!capable(CAP_SYS_ADMIN))
1098 return -EPERM;
1099
1100 args = kmalloc(sizeof(*args), GFP_KERNEL);
1101 if (!args)
1102 return -ENOMEM;
1103
1104 if (copy_from_user(args, argp, sizeof(*args))) {
1105 kfree(args);
1106 return -EFAULT;
1107 }
1108 inode = fdentry(file)->d_inode;
1109 ret = search_ioctl(inode, args);
1110 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1111 ret = -EFAULT;
1112 kfree(args);
1113 return ret;
1114}
1115
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001116/*
Chris Masonac8e9812010-02-28 15:39:26 -05001117 * Search INODE_REFs to identify path name of 'dirid' directory
1118 * in a 'tree_id' tree. and sets path name to 'name'.
1119 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001120static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1121 u64 tree_id, u64 dirid, char *name)
1122{
1123 struct btrfs_root *root;
1124 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001125 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001126 int ret = -1;
1127 int slot;
1128 int len;
1129 int total_len = 0;
1130 struct btrfs_inode_ref *iref;
1131 struct extent_buffer *l;
1132 struct btrfs_path *path;
1133
1134 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1135 name[0]='\0';
1136 return 0;
1137 }
1138
1139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
1142
Chris Masonac8e9812010-02-28 15:39:26 -05001143 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001144
1145 key.objectid = tree_id;
1146 key.type = BTRFS_ROOT_ITEM_KEY;
1147 key.offset = (u64)-1;
1148 root = btrfs_read_fs_root_no_name(info, &key);
1149 if (IS_ERR(root)) {
1150 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001151 ret = -ENOENT;
1152 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001153 }
1154
1155 key.objectid = dirid;
1156 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001157 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001158
1159 while(1) {
1160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1161 if (ret < 0)
1162 goto out;
1163
1164 l = path->nodes[0];
1165 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001166 if (ret > 0 && slot > 0)
1167 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001168 btrfs_item_key_to_cpu(l, &key, slot);
1169
1170 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001171 key.type != BTRFS_INODE_REF_KEY)) {
1172 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001173 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001174 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001175
1176 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1177 len = btrfs_inode_ref_name_len(l, iref);
1178 ptr -= len + 1;
1179 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001180 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001181 goto out;
1182
1183 *(ptr + len) = '/';
1184 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1185
1186 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1187 break;
1188
1189 btrfs_release_path(root, path);
1190 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001191 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001192 dirid = key.objectid;
1193
1194 }
Chris Masonac8e9812010-02-28 15:39:26 -05001195 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001196 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001197 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001198 name[total_len]='\0';
1199 ret = 0;
1200out:
1201 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001202 return ret;
1203}
1204
1205static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1206 void __user *argp)
1207{
1208 struct btrfs_ioctl_ino_lookup_args *args;
1209 struct inode *inode;
1210 int ret;
1211
1212 if (!capable(CAP_SYS_ADMIN))
1213 return -EPERM;
1214
1215 args = kmalloc(sizeof(*args), GFP_KERNEL);
1216 if (copy_from_user(args, argp, sizeof(*args))) {
1217 kfree(args);
1218 return -EFAULT;
1219 }
1220 inode = fdentry(file)->d_inode;
1221
Chris Mason1b53ac42010-03-18 12:17:05 -04001222 if (args->treeid == 0)
1223 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1224
Chris Masonac8e9812010-02-28 15:39:26 -05001225 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1226 args->treeid, args->objectid,
1227 args->name);
1228
1229 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1230 ret = -EFAULT;
1231
1232 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001233 return ret;
1234}
1235
Yan, Zheng76dda932009-09-21 16:00:26 -04001236static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1237 void __user *arg)
1238{
1239 struct dentry *parent = fdentry(file);
1240 struct dentry *dentry;
1241 struct inode *dir = parent->d_inode;
1242 struct inode *inode;
1243 struct btrfs_root *root = BTRFS_I(dir)->root;
1244 struct btrfs_root *dest = NULL;
1245 struct btrfs_ioctl_vol_args *vol_args;
1246 struct btrfs_trans_handle *trans;
1247 int namelen;
1248 int ret;
1249 int err = 0;
1250
1251 if (!capable(CAP_SYS_ADMIN))
1252 return -EPERM;
1253
1254 vol_args = memdup_user(arg, sizeof(*vol_args));
1255 if (IS_ERR(vol_args))
1256 return PTR_ERR(vol_args);
1257
1258 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1259 namelen = strlen(vol_args->name);
1260 if (strchr(vol_args->name, '/') ||
1261 strncmp(vol_args->name, "..", namelen) == 0) {
1262 err = -EINVAL;
1263 goto out;
1264 }
1265
1266 err = mnt_want_write(file->f_path.mnt);
1267 if (err)
1268 goto out;
1269
1270 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1271 dentry = lookup_one_len(vol_args->name, parent, namelen);
1272 if (IS_ERR(dentry)) {
1273 err = PTR_ERR(dentry);
1274 goto out_unlock_dir;
1275 }
1276
1277 if (!dentry->d_inode) {
1278 err = -ENOENT;
1279 goto out_dput;
1280 }
1281
1282 inode = dentry->d_inode;
1283 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1284 err = -EINVAL;
1285 goto out_dput;
1286 }
1287
1288 dest = BTRFS_I(inode)->root;
1289
1290 mutex_lock(&inode->i_mutex);
1291 err = d_invalidate(dentry);
1292 if (err)
1293 goto out_unlock;
1294
1295 down_write(&root->fs_info->subvol_sem);
1296
1297 err = may_destroy_subvol(dest);
1298 if (err)
1299 goto out_up_write;
1300
1301 trans = btrfs_start_transaction(root, 1);
1302 ret = btrfs_unlink_subvol(trans, root, dir,
1303 dest->root_key.objectid,
1304 dentry->d_name.name,
1305 dentry->d_name.len);
1306 BUG_ON(ret);
1307
1308 btrfs_record_root_in_trans(trans, dest);
1309
1310 memset(&dest->root_item.drop_progress, 0,
1311 sizeof(dest->root_item.drop_progress));
1312 dest->root_item.drop_level = 0;
1313 btrfs_set_root_refs(&dest->root_item, 0);
1314
1315 ret = btrfs_insert_orphan_item(trans,
1316 root->fs_info->tree_root,
1317 dest->root_key.objectid);
1318 BUG_ON(ret);
1319
1320 ret = btrfs_commit_transaction(trans, root);
1321 BUG_ON(ret);
1322 inode->i_flags |= S_DEAD;
1323out_up_write:
1324 up_write(&root->fs_info->subvol_sem);
1325out_unlock:
1326 mutex_unlock(&inode->i_mutex);
1327 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001328 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001329 btrfs_invalidate_inodes(dest);
1330 d_delete(dentry);
1331 }
1332out_dput:
1333 dput(dentry);
1334out_unlock_dir:
1335 mutex_unlock(&dir->i_mutex);
1336 mnt_drop_write(file->f_path.mnt);
1337out:
1338 kfree(vol_args);
1339 return err;
1340}
1341
Chris Mason1e701a32010-03-11 09:42:04 -05001342static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001343{
1344 struct inode *inode = fdentry(file)->d_inode;
1345 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001346 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001347 int ret;
1348
1349 ret = mnt_want_write(file->f_path.mnt);
1350 if (ret)
1351 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001352
1353 switch (inode->i_mode & S_IFMT) {
1354 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001355 if (!capable(CAP_SYS_ADMIN)) {
1356 ret = -EPERM;
1357 goto out;
1358 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001359 btrfs_defrag_root(root, 0);
1360 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001361 break;
1362 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001363 if (!(file->f_mode & FMODE_WRITE)) {
1364 ret = -EINVAL;
1365 goto out;
1366 }
Chris Mason1e701a32010-03-11 09:42:04 -05001367
1368 range = kzalloc(sizeof(*range), GFP_KERNEL);
1369 if (!range) {
1370 ret = -ENOMEM;
1371 goto out;
1372 }
1373
1374 if (argp) {
1375 if (copy_from_user(range, argp,
1376 sizeof(*range))) {
1377 ret = -EFAULT;
1378 kfree(range);
1379 }
1380 /* compression requires us to start the IO */
1381 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1382 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1383 range->extent_thresh = (u32)-1;
1384 }
1385 } else {
1386 /* the rest are all set to zero by kzalloc */
1387 range->len = (u64)-1;
1388 }
1389 btrfs_defrag_file(file, range);
1390 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001391 break;
1392 }
Chris Masone441d542009-01-05 16:57:23 -05001393out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001394 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001395 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001396}
1397
Christoph Hellwigb2950862008-12-02 09:54:17 -05001398static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001399{
1400 struct btrfs_ioctl_vol_args *vol_args;
1401 int ret;
1402
Chris Masone441d542009-01-05 16:57:23 -05001403 if (!capable(CAP_SYS_ADMIN))
1404 return -EPERM;
1405
Li Zefandae7b662009-04-08 15:06:54 +08001406 vol_args = memdup_user(arg, sizeof(*vol_args));
1407 if (IS_ERR(vol_args))
1408 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409
Mark Fasheh5516e592008-07-24 12:20:14 -04001410 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001411 ret = btrfs_init_new_device(root, vol_args->name);
1412
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001413 kfree(vol_args);
1414 return ret;
1415}
1416
Christoph Hellwigb2950862008-12-02 09:54:17 -05001417static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001418{
1419 struct btrfs_ioctl_vol_args *vol_args;
1420 int ret;
1421
Chris Masone441d542009-01-05 16:57:23 -05001422 if (!capable(CAP_SYS_ADMIN))
1423 return -EPERM;
1424
Yan Zhengc146afa2008-11-12 14:34:12 -05001425 if (root->fs_info->sb->s_flags & MS_RDONLY)
1426 return -EROFS;
1427
Li Zefandae7b662009-04-08 15:06:54 +08001428 vol_args = memdup_user(arg, sizeof(*vol_args));
1429 if (IS_ERR(vol_args))
1430 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001431
Mark Fasheh5516e592008-07-24 12:20:14 -04001432 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433 ret = btrfs_rm_device(root, vol_args->name);
1434
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001435 kfree(vol_args);
1436 return ret;
1437}
1438
Yan, Zheng76dda932009-09-21 16:00:26 -04001439static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1440 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001441{
1442 struct inode *inode = fdentry(file)->d_inode;
1443 struct btrfs_root *root = BTRFS_I(inode)->root;
1444 struct file *src_file;
1445 struct inode *src;
1446 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001447 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001448 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001449 char *buf;
1450 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001451 u32 nritems;
1452 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001453 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001454 u64 len = olen;
1455 u64 bs = root->fs_info->sb->s_blocksize;
1456 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001457
Sage Weilc5c9cd42008-11-12 14:32:25 -05001458 /*
1459 * TODO:
1460 * - split compressed inline extents. annoying: we need to
1461 * decompress into destination's address_space (the file offset
1462 * may change, so source mapping won't do), then recompress (or
1463 * otherwise reinsert) a subrange.
1464 * - allow ranges within the same file to be cloned (provided
1465 * they don't overlap)?
1466 */
1467
Chris Masone441d542009-01-05 16:57:23 -05001468 /* the destination must be opened for writing */
1469 if (!(file->f_mode & FMODE_WRITE))
1470 return -EINVAL;
1471
Yan Zhengc146afa2008-11-12 14:34:12 -05001472 ret = mnt_want_write(file->f_path.mnt);
1473 if (ret)
1474 return ret;
1475
Sage Weilc5c9cd42008-11-12 14:32:25 -05001476 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001477 if (!src_file) {
1478 ret = -EBADF;
1479 goto out_drop_write;
1480 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001481 src = src_file->f_dentry->d_inode;
1482
Sage Weilc5c9cd42008-11-12 14:32:25 -05001483 ret = -EINVAL;
1484 if (src == inode)
1485 goto out_fput;
1486
Yan Zhengae01a0a2008-08-04 23:23:47 -04001487 ret = -EISDIR;
1488 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001489 goto out_fput;
1490
Yan Zhengae01a0a2008-08-04 23:23:47 -04001491 ret = -EXDEV;
1492 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1493 goto out_fput;
1494
1495 ret = -ENOMEM;
1496 buf = vmalloc(btrfs_level_size(root, 0));
1497 if (!buf)
1498 goto out_fput;
1499
1500 path = btrfs_alloc_path();
1501 if (!path) {
1502 vfree(buf);
1503 goto out_fput;
1504 }
1505 path->reada = 2;
1506
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001507 if (inode < src) {
1508 mutex_lock(&inode->i_mutex);
1509 mutex_lock(&src->i_mutex);
1510 } else {
1511 mutex_lock(&src->i_mutex);
1512 mutex_lock(&inode->i_mutex);
1513 }
1514
Sage Weilc5c9cd42008-11-12 14:32:25 -05001515 /* determine range to clone */
1516 ret = -EINVAL;
1517 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001518 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001519 if (len == 0)
1520 olen = len = src->i_size - off;
1521 /* if we extend to eof, continue to block boundary */
1522 if (off + len == src->i_size)
1523 len = ((src->i_size + bs-1) & ~(bs-1))
1524 - off;
1525
1526 /* verify the end result is block aligned */
1527 if ((off & (bs-1)) ||
1528 ((off + len) & (bs-1)))
1529 goto out_unlock;
1530
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001531 /* do any pending delalloc/csum calc on src, one way or
1532 another, and lock file content */
1533 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001534 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001535 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1536 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001537 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001538 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001539 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001540 if (ordered)
1541 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001542 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543 }
1544
Yan Zhengae01a0a2008-08-04 23:23:47 -04001545 trans = btrfs_start_transaction(root, 1);
1546 BUG_ON(!trans);
1547
Sage Weilc5c9cd42008-11-12 14:32:25 -05001548 /* punch hole in destination first */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001549 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001550
1551 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001552 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001553 key.type = BTRFS_EXTENT_DATA_KEY;
1554 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001555
1556 while (1) {
1557 /*
1558 * note the key will change type as we walk through the
1559 * tree.
1560 */
1561 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1562 if (ret < 0)
1563 goto out;
1564
Yan Zhengae01a0a2008-08-04 23:23:47 -04001565 nritems = btrfs_header_nritems(path->nodes[0]);
1566 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001567 ret = btrfs_next_leaf(root, path);
1568 if (ret < 0)
1569 goto out;
1570 if (ret > 0)
1571 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001572 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573 }
1574 leaf = path->nodes[0];
1575 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001576
Yan Zhengae01a0a2008-08-04 23:23:47 -04001577 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001578 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001579 key.objectid != src->i_ino)
1580 break;
1581
Sage Weilc5c9cd42008-11-12 14:32:25 -05001582 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1583 struct btrfs_file_extent_item *extent;
1584 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001585 u32 size;
1586 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001587 u64 disko = 0, diskl = 0;
1588 u64 datao = 0, datal = 0;
1589 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -04001590
1591 size = btrfs_item_size_nr(leaf, slot);
1592 read_extent_buffer(leaf, buf,
1593 btrfs_item_ptr_offset(leaf, slot),
1594 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001595
1596 extent = btrfs_item_ptr(leaf, slot,
1597 struct btrfs_file_extent_item);
1598 comp = btrfs_file_extent_compression(leaf, extent);
1599 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001600 if (type == BTRFS_FILE_EXTENT_REG ||
1601 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001602 disko = btrfs_file_extent_disk_bytenr(leaf,
1603 extent);
1604 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1605 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001606 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001607 datal = btrfs_file_extent_num_bytes(leaf,
1608 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001609 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1610 /* take upper bound, may be compressed */
1611 datal = btrfs_file_extent_ram_bytes(leaf,
1612 extent);
1613 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001614 btrfs_release_path(root, path);
1615
Sage Weilc5c9cd42008-11-12 14:32:25 -05001616 if (key.offset + datal < off ||
1617 key.offset >= off+len)
1618 goto next;
1619
Zheng Yan31840ae2008-09-23 13:14:14 -04001620 memcpy(&new_key, &key, sizeof(new_key));
1621 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001622 new_key.offset = key.offset + destoff - off;
1623
Chris Masonc8a894d2009-06-27 21:07:03 -04001624 if (type == BTRFS_FILE_EXTENT_REG ||
1625 type == BTRFS_FILE_EXTENT_PREALLOC) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001626 ret = btrfs_insert_empty_item(trans, root, path,
1627 &new_key, size);
1628 if (ret)
1629 goto out;
1630
1631 leaf = path->nodes[0];
1632 slot = path->slots[0];
1633 write_extent_buffer(leaf, buf,
1634 btrfs_item_ptr_offset(leaf, slot),
1635 size);
1636
1637 extent = btrfs_item_ptr(leaf, slot,
1638 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001639
1640 if (off > key.offset) {
1641 datao += off - key.offset;
1642 datal -= off - key.offset;
1643 }
Chris Masonac6889c2009-10-09 11:29:53 -04001644
1645 if (key.offset + datal > off + len)
1646 datal = off + len - key.offset;
1647
Sage Weilc5c9cd42008-11-12 14:32:25 -05001648 /* disko == 0 means it's a hole */
1649 if (!disko)
1650 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001651
1652 btrfs_set_file_extent_offset(leaf, extent,
1653 datao);
1654 btrfs_set_file_extent_num_bytes(leaf, extent,
1655 datal);
1656 if (disko) {
1657 inode_add_bytes(inode, datal);
1658 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659 disko, diskl, 0,
1660 root->root_key.objectid,
1661 inode->i_ino,
1662 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001663 BUG_ON(ret);
1664 }
1665 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1666 u64 skip = 0;
1667 u64 trim = 0;
1668 if (off > key.offset) {
1669 skip = off - key.offset;
1670 new_key.offset += skip;
1671 }
Chris Masond3977122009-01-05 21:25:51 -05001672
Sage Weilc5c9cd42008-11-12 14:32:25 -05001673 if (key.offset + datal > off+len)
1674 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001675
Sage Weilc5c9cd42008-11-12 14:32:25 -05001676 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001677 ret = -EINVAL;
1678 goto out;
1679 }
1680 size -= skip + trim;
1681 datal -= skip + trim;
1682 ret = btrfs_insert_empty_item(trans, root, path,
1683 &new_key, size);
1684 if (ret)
1685 goto out;
1686
1687 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001688 u32 start =
1689 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001690 memmove(buf+start, buf+start+skip,
1691 datal);
1692 }
1693
1694 leaf = path->nodes[0];
1695 slot = path->slots[0];
1696 write_extent_buffer(leaf, buf,
1697 btrfs_item_ptr_offset(leaf, slot),
1698 size);
1699 inode_add_bytes(inode, datal);
1700 }
1701
1702 btrfs_mark_buffer_dirty(leaf);
1703 }
1704
Chris Masond3977122009-01-05 21:25:51 -05001705next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001706 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709 ret = 0;
1710out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001711 btrfs_release_path(root, path);
1712 if (ret == 0) {
1713 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001714 if (destoff + olen > inode->i_size)
1715 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001716 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1717 ret = btrfs_update_inode(trans, root, inode);
1718 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001720 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001721 if (ret)
1722 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001723out_unlock:
1724 mutex_unlock(&src->i_mutex);
1725 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001726 vfree(buf);
1727 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001728out_fput:
1729 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001730out_drop_write:
1731 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001732 return ret;
1733}
1734
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001735static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001736{
1737 struct btrfs_ioctl_clone_range_args args;
1738
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001739 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001740 return -EFAULT;
1741 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1742 args.src_length, args.dest_offset);
1743}
1744
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001745/*
1746 * there are many ways the trans_start and trans_end ioctls can lead
1747 * to deadlocks. They should only be used by applications that
1748 * basically own the machine, and have a very in depth understanding
1749 * of all the possible deadlocks and enospc problems.
1750 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001751static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752{
1753 struct inode *inode = fdentry(file)->d_inode;
1754 struct btrfs_root *root = BTRFS_I(inode)->root;
1755 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001756 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001757
Sage Weil1ab86ae2009-09-29 18:38:44 -04001758 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001759 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001760 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001761
1762 ret = -EINPROGRESS;
1763 if (file->private_data)
1764 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001765
Yan Zhengc146afa2008-11-12 14:34:12 -05001766 ret = mnt_want_write(file->f_path.mnt);
1767 if (ret)
1768 goto out;
1769
Sage Weil9ca9ee02008-08-04 10:41:27 -04001770 mutex_lock(&root->fs_info->trans_mutex);
1771 root->fs_info->open_ioctl_trans++;
1772 mutex_unlock(&root->fs_info->trans_mutex);
1773
Sage Weil1ab86ae2009-09-29 18:38:44 -04001774 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001775 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001776 if (!trans)
1777 goto out_drop;
1778
1779 file->private_data = trans;
1780 return 0;
1781
1782out_drop:
1783 mutex_lock(&root->fs_info->trans_mutex);
1784 root->fs_info->open_ioctl_trans--;
1785 mutex_unlock(&root->fs_info->trans_mutex);
1786 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001787out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001788 return ret;
1789}
1790
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001791static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1792{
1793 struct inode *inode = fdentry(file)->d_inode;
1794 struct btrfs_root *root = BTRFS_I(inode)->root;
1795 struct btrfs_root *new_root;
1796 struct btrfs_dir_item *di;
1797 struct btrfs_trans_handle *trans;
1798 struct btrfs_path *path;
1799 struct btrfs_key location;
1800 struct btrfs_disk_key disk_key;
1801 struct btrfs_super_block *disk_super;
1802 u64 features;
1803 u64 objectid = 0;
1804 u64 dir_id;
1805
1806 if (!capable(CAP_SYS_ADMIN))
1807 return -EPERM;
1808
1809 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1810 return -EFAULT;
1811
1812 if (!objectid)
1813 objectid = root->root_key.objectid;
1814
1815 location.objectid = objectid;
1816 location.type = BTRFS_ROOT_ITEM_KEY;
1817 location.offset = (u64)-1;
1818
1819 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1820 if (IS_ERR(new_root))
1821 return PTR_ERR(new_root);
1822
1823 if (btrfs_root_refs(&new_root->root_item) == 0)
1824 return -ENOENT;
1825
1826 path = btrfs_alloc_path();
1827 if (!path)
1828 return -ENOMEM;
1829 path->leave_spinning = 1;
1830
1831 trans = btrfs_start_transaction(root, 1);
1832 if (!trans) {
1833 btrfs_free_path(path);
1834 return -ENOMEM;
1835 }
1836
1837 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
1838 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
1839 dir_id, "default", 7, 1);
1840 if (!di) {
1841 btrfs_free_path(path);
1842 btrfs_end_transaction(trans, root);
1843 printk(KERN_ERR "Umm, you don't have the default dir item, "
1844 "this isn't going to work\n");
1845 return -ENOENT;
1846 }
1847
1848 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
1849 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
1850 btrfs_mark_buffer_dirty(path->nodes[0]);
1851 btrfs_free_path(path);
1852
1853 disk_super = &root->fs_info->super_copy;
1854 features = btrfs_super_incompat_flags(disk_super);
1855 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
1856 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
1857 btrfs_set_super_incompat_flags(disk_super, features);
1858 }
1859 btrfs_end_transaction(trans, root);
1860
1861 return 0;
1862}
1863
Josef Bacik1406e432010-01-13 18:19:06 +00001864long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1865{
1866 struct btrfs_ioctl_space_args space_args;
1867 struct btrfs_ioctl_space_info space;
1868 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04001869 struct btrfs_ioctl_space_info *dest_orig;
1870 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00001871 struct btrfs_space_info *info;
Chris Mason7fde62b2010-03-16 15:40:10 -04001872 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00001873 int ret = 0;
Chris Mason7fde62b2010-03-16 15:40:10 -04001874 int slot_count = 0;
Josef Bacik1406e432010-01-13 18:19:06 +00001875
1876 if (copy_from_user(&space_args,
1877 (struct btrfs_ioctl_space_args __user *)arg,
1878 sizeof(space_args)))
1879 return -EFAULT;
1880
Chris Mason7fde62b2010-03-16 15:40:10 -04001881 /* first we count slots */
1882 rcu_read_lock();
1883 list_for_each_entry_rcu(info, &root->fs_info->space_info, list)
1884 slot_count++;
1885 rcu_read_unlock();
Josef Bacik1406e432010-01-13 18:19:06 +00001886
Chris Mason7fde62b2010-03-16 15:40:10 -04001887 /* space_slots == 0 means they are asking for a count */
1888 if (space_args.space_slots == 0) {
1889 space_args.total_spaces = slot_count;
1890 goto out;
1891 }
1892 alloc_size = sizeof(*dest) * slot_count;
1893 /* we generally have at most 6 or so space infos, one for each raid
1894 * level. So, a whole page should be more than enough for everyone
1895 */
1896 if (alloc_size > PAGE_CACHE_SIZE)
1897 return -ENOMEM;
1898
1899 space_args.total_spaces = 0;
1900 dest = kmalloc(alloc_size, GFP_NOFS);
1901 if (!dest)
1902 return -ENOMEM;
1903 dest_orig = dest;
1904
1905 /* now we have a buffer to copy into */
Josef Bacik1406e432010-01-13 18:19:06 +00001906 rcu_read_lock();
1907 list_for_each_entry_rcu(info, &root->fs_info->space_info, list) {
Chris Mason7fde62b2010-03-16 15:40:10 -04001908 /* make sure we don't copy more than we allocated
1909 * in our buffer
1910 */
1911 if (slot_count == 0)
1912 break;
1913 slot_count--;
1914
1915 /* make sure userland has enough room in their buffer */
Josef Bacik1406e432010-01-13 18:19:06 +00001916 if (space_args.total_spaces >= space_args.space_slots)
1917 break;
Chris Mason7fde62b2010-03-16 15:40:10 -04001918
Josef Bacik1406e432010-01-13 18:19:06 +00001919 space.flags = info->flags;
1920 space.total_bytes = info->total_bytes;
1921 space.used_bytes = info->bytes_used;
Chris Mason7fde62b2010-03-16 15:40:10 -04001922 memcpy(dest, &space, sizeof(space));
Josef Bacik1406e432010-01-13 18:19:06 +00001923 dest++;
1924 space_args.total_spaces++;
1925 }
1926 rcu_read_unlock();
1927
Chris Mason7fde62b2010-03-16 15:40:10 -04001928 user_dest = (struct btrfs_ioctl_space_info *)
1929 (arg + sizeof(struct btrfs_ioctl_space_args));
1930
1931 if (copy_to_user(user_dest, dest_orig, alloc_size))
1932 ret = -EFAULT;
1933
1934 kfree(dest_orig);
1935out:
1936 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00001937 ret = -EFAULT;
1938
1939 return ret;
1940}
1941
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001942/*
1943 * there are many ways the trans_start and trans_end ioctls can lead
1944 * to deadlocks. They should only be used by applications that
1945 * basically own the machine, and have a very in depth understanding
1946 * of all the possible deadlocks and enospc problems.
1947 */
1948long btrfs_ioctl_trans_end(struct file *file)
1949{
1950 struct inode *inode = fdentry(file)->d_inode;
1951 struct btrfs_root *root = BTRFS_I(inode)->root;
1952 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001953
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001954 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001955 if (!trans)
1956 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04001957 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001958
Sage Weil1ab86ae2009-09-29 18:38:44 -04001959 btrfs_end_transaction(trans, root);
1960
Sage Weil9ca9ee02008-08-04 10:41:27 -04001961 mutex_lock(&root->fs_info->trans_mutex);
1962 root->fs_info->open_ioctl_trans--;
1963 mutex_unlock(&root->fs_info->trans_mutex);
1964
Sage Weilcfc8ea82008-12-11 16:30:06 -05001965 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001966 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001967}
1968
1969long btrfs_ioctl(struct file *file, unsigned int
1970 cmd, unsigned long arg)
1971{
1972 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001973 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001974
1975 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001976 case FS_IOC_GETFLAGS:
1977 return btrfs_ioctl_getflags(file, argp);
1978 case FS_IOC_SETFLAGS:
1979 return btrfs_ioctl_setflags(file, argp);
1980 case FS_IOC_GETVERSION:
1981 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001982 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001983 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001984 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001985 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04001986 case BTRFS_IOC_SNAP_DESTROY:
1987 return btrfs_ioctl_snap_destroy(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001988 case BTRFS_IOC_DEFAULT_SUBVOL:
1989 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001990 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05001991 return btrfs_ioctl_defrag(file, NULL);
1992 case BTRFS_IOC_DEFRAG_RANGE:
1993 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001994 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001995 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001996 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001997 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001998 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001999 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002000 case BTRFS_IOC_BALANCE:
2001 return btrfs_balance(root->fs_info->dev_root);
2002 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002003 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2004 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002005 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002006 case BTRFS_IOC_TRANS_START:
2007 return btrfs_ioctl_trans_start(file);
2008 case BTRFS_IOC_TRANS_END:
2009 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002010 case BTRFS_IOC_TREE_SEARCH:
2011 return btrfs_ioctl_tree_search(file, argp);
2012 case BTRFS_IOC_INO_LOOKUP:
2013 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002014 case BTRFS_IOC_SPACE_INFO:
2015 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002016 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002017 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2018 return 0;
2019 }
2020
2021 return -ENOTTY;
2022}