blob: 81b47bd8a55a67cd4531c3181a5d0990b675c651 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
48#include "ioctl.h"
49#include "print-tree.h"
50#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040051#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
Christoph Hellwig6cbff002009-04-17 10:37:41 +020053/* Mask out flags that are inappropriate for the given type of inode. */
54static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
55{
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
62}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/*
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 */
67static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
68{
69 unsigned int iflags = 0;
70
71 if (flags & BTRFS_INODE_SYNC)
72 iflags |= FS_SYNC_FL;
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
83
84 return iflags;
85}
86
87/*
88 * Update inode->i_flags based on the btrfs internal flags.
89 */
90void btrfs_update_iflags(struct inode *inode)
91{
92 struct btrfs_inode *ip = BTRFS_I(inode);
93
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
95
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
106}
107
108/*
109 * Inherit flags from the parent inode.
110 *
111 * Unlike extN we don't have any flags we don't want to inherit currently.
112 */
113void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
114{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400115 unsigned int flags;
116
117 if (!dir)
118 return;
119
120 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200121
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
126
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
129}
130
131static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
132{
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
135
136 if (copy_to_user(arg, &flags, sizeof(flags)))
137 return -EFAULT;
138 return 0;
139}
140
141static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
142{
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
148 int ret;
149
150 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT;
152
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
156 return -EOPNOTSUPP;
157
158 if (!is_owner_or_cap(inode))
159 return -EACCES;
160
161 mutex_lock(&inode->i_mutex);
162
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = -EPERM;
168 goto out_unlock;
169 }
170 }
171
172 ret = mnt_want_write(file->f_path.mnt);
173 if (ret)
174 goto out_unlock;
175
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
178 else
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 else
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
186 else
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
190 else
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
194 else
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
200
201
202 trans = btrfs_join_transaction(root, 1);
203 BUG_ON(!trans);
204
205 ret = btrfs_update_inode(trans, root, inode);
206 BUG_ON(ret);
207
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
211
212 mnt_drop_write(file->f_path.mnt);
213 out_unlock:
214 mutex_unlock(&inode->i_mutex);
215 return 0;
216}
217
218static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
219{
220 struct inode *inode = file->f_path.dentry->d_inode;
221
222 return put_user(inode->i_generation, arg);
223}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400224
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400225static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400227 char *name, int namelen,
228 u64 *async_transid)
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
Yan, Zhenga22285a2010-05-16 10:48:46 -0400243 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
244 0, &objectid);
245 if (ret)
246 return ret;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400247 /*
248 * 1 - inode item
249 * 2 - refs
250 * 1 - root item
251 * 2 - dir items
252 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400253 trans = btrfs_start_transaction(root, 6);
254 if (IS_ERR(trans))
255 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400256
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400257 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
258 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400259 if (IS_ERR(leaf)) {
260 ret = PTR_ERR(leaf);
261 goto fail;
262 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400263
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400264 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400265 btrfs_set_header_bytenr(leaf, leaf->start);
266 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400267 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400268 btrfs_set_header_owner(leaf, objectid);
269
270 write_extent_buffer(leaf, root->fs_info->fsid,
271 (unsigned long)btrfs_header_fsid(leaf),
272 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400273 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
274 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
275 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400276 btrfs_mark_buffer_dirty(leaf);
277
278 inode_item = &root_item.inode;
279 memset(inode_item, 0, sizeof(*inode_item));
280 inode_item->generation = cpu_to_le64(1);
281 inode_item->size = cpu_to_le64(3);
282 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400283 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400284 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
285
286 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400287 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400288 btrfs_set_root_level(&root_item, 0);
289 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000290 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400291 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400292
293 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
294 root_item.drop_level = 0;
295
Chris Mason925baed2008-06-25 16:01:30 -0400296 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400297 free_extent_buffer(leaf);
298 leaf = NULL;
299
300 btrfs_set_root_dirid(&root_item, new_dirid);
301
302 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400303 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400304 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
305 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
306 &root_item);
307 if (ret)
308 goto fail;
309
Yan, Zheng76dda932009-09-21 16:00:26 -0400310 key.offset = (u64)-1;
311 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
312 BUG_ON(IS_ERR(new_root));
313
314 btrfs_record_root_in_trans(trans, new_root);
315
316 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
317 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400318 /*
319 * insert the directory item
320 */
Chris Mason3de45862008-11-17 21:02:50 -0500321 ret = btrfs_set_inode_index(dir, &index);
322 BUG_ON(ret);
323
324 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400325 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500326 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400327 if (ret)
328 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500329
Yan Zheng52c26172009-01-05 15:43:43 -0500330 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
331 ret = btrfs_update_inode(trans, root, dir);
332 BUG_ON(ret);
333
Chris Mason0660b5a2008-11-17 20:37:39 -0500334 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400335 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500336 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400337
Chris Mason0660b5a2008-11-17 20:37:39 -0500338 BUG_ON(ret);
339
Yan, Zheng76dda932009-09-21 16:00:26 -0400340 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400341fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400342 if (async_transid) {
343 *async_transid = trans->transid;
344 err = btrfs_commit_transaction_async(trans, root, 1);
345 } else {
346 err = btrfs_commit_transaction(trans, root);
347 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348 if (err && !ret)
349 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400350 return ret;
351}
352
Sage Weil72fd0322010-10-29 15:41:32 -0400353static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
354 char *name, int namelen, u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400355{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000356 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400357 struct btrfs_pending_snapshot *pending_snapshot;
358 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000359 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400360
361 if (!root->ref_cows)
362 return -EINVAL;
363
Yan, Zhenga22285a2010-05-16 10:48:46 -0400364 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
365 if (!pending_snapshot)
366 return -ENOMEM;
367
368 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
369 pending_snapshot->dentry = dentry;
370 pending_snapshot->root = root;
371
372 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
373 if (IS_ERR(trans)) {
374 ret = PTR_ERR(trans);
375 goto fail;
376 }
377
378 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
379 BUG_ON(ret);
380
381 list_add(&pending_snapshot->list,
382 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400383 if (async_transid) {
384 *async_transid = trans->transid;
385 ret = btrfs_commit_transaction_async(trans,
386 root->fs_info->extent_root, 1);
387 } else {
388 ret = btrfs_commit_transaction(trans,
389 root->fs_info->extent_root);
390 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400391 BUG_ON(ret);
392
393 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400394 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000395 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400396
Yan, Zhenga22285a2010-05-16 10:48:46 -0400397 btrfs_orphan_cleanup(pending_snapshot->snap);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
400 if (IS_ERR(inode)) {
401 ret = PTR_ERR(inode);
402 goto fail;
403 }
404 BUG_ON(!inode);
405 d_instantiate(dentry, inode);
406 ret = 0;
407fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400408 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409 return ret;
410}
411
Sage Weil4260f7c2010-10-29 15:46:43 -0400412/* copy of check_sticky in fs/namei.c()
413* It's inline, so penalty for filesystems that don't use sticky bit is
414* minimal.
415*/
416static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
417{
418 uid_t fsuid = current_fsuid();
419
420 if (!(dir->i_mode & S_ISVTX))
421 return 0;
422 if (inode->i_uid == fsuid)
423 return 0;
424 if (dir->i_uid == fsuid)
425 return 0;
426 return !capable(CAP_FOWNER);
427}
428
429/* copy of may_delete in fs/namei.c()
430 * Check whether we can remove a link victim from directory dir, check
431 * whether the type of victim is right.
432 * 1. We can't do it if dir is read-only (done in permission())
433 * 2. We should have write and exec permissions on dir
434 * 3. We can't remove anything from append-only dir
435 * 4. We can't do anything with immutable dir (done in permission())
436 * 5. If the sticky bit on dir is set we should either
437 * a. be owner of dir, or
438 * b. be owner of victim, or
439 * c. have CAP_FOWNER capability
440 * 6. If the victim is append-only or immutable we can't do antyhing with
441 * links pointing to it.
442 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
443 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
444 * 9. We can't remove a root or mountpoint.
445 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
446 * nfs_async_unlink().
447 */
448
449static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
450{
451 int error;
452
453 if (!victim->d_inode)
454 return -ENOENT;
455
456 BUG_ON(victim->d_parent->d_inode != dir);
457 audit_inode_child(victim, dir);
458
459 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
460 if (error)
461 return error;
462 if (IS_APPEND(dir))
463 return -EPERM;
464 if (btrfs_check_sticky(dir, victim->d_inode)||
465 IS_APPEND(victim->d_inode)||
466 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
467 return -EPERM;
468 if (isdir) {
469 if (!S_ISDIR(victim->d_inode->i_mode))
470 return -ENOTDIR;
471 if (IS_ROOT(victim))
472 return -EBUSY;
473 } else if (S_ISDIR(victim->d_inode->i_mode))
474 return -EISDIR;
475 if (IS_DEADDIR(dir))
476 return -ENOENT;
477 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
478 return -EBUSY;
479 return 0;
480}
481
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400482/* copy of may_create in fs/namei.c() */
483static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
484{
485 if (child->d_inode)
486 return -EEXIST;
487 if (IS_DEADDIR(dir))
488 return -ENOENT;
489 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
490}
491
492/*
493 * Create a new subvolume below @parent. This is largely modeled after
494 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
495 * inside this filesystem so it's quite a bit simpler.
496 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400497static noinline int btrfs_mksubvol(struct path *parent,
498 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400499 struct btrfs_root *snap_src,
500 u64 *async_transid)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400501{
Yan, Zheng76dda932009-09-21 16:00:26 -0400502 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400503 struct dentry *dentry;
504 int error;
505
Yan, Zheng76dda932009-09-21 16:00:26 -0400506 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400507
508 dentry = lookup_one_len(name, parent->dentry, namelen);
509 error = PTR_ERR(dentry);
510 if (IS_ERR(dentry))
511 goto out_unlock;
512
513 error = -EEXIST;
514 if (dentry->d_inode)
515 goto out_dput;
516
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400517 error = mnt_want_write(parent->mnt);
518 if (error)
519 goto out_dput;
520
Yan, Zheng76dda932009-09-21 16:00:26 -0400521 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400522 if (error)
523 goto out_drop_write;
524
Yan, Zheng76dda932009-09-21 16:00:26 -0400525 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
526
527 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
528 goto out_up_read;
529
Chris Mason3de45862008-11-17 21:02:50 -0500530 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400531 error = create_snapshot(snap_src, dentry,
532 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500533 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400534 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400535 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500536 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400537 if (!error)
538 fsnotify_mkdir(dir, dentry);
539out_up_read:
540 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400541out_drop_write:
542 mnt_drop_write(parent->mnt);
543out_dput:
544 dput(dentry);
545out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400546 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400547 return error;
548}
549
Chris Mason940100a2010-03-10 10:52:59 -0500550static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500551 int thresh, u64 *last_len, u64 *skip,
552 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500553{
554 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
555 struct extent_map *em = NULL;
556 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
557 int ret = 1;
558
Chris Mason1e701a32010-03-11 09:42:04 -0500559
560 if (thresh == 0)
561 thresh = 256 * 1024;
562
Chris Mason940100a2010-03-10 10:52:59 -0500563 /*
564 * make sure that once we start defragging and extent, we keep on
565 * defragging it
566 */
567 if (start < *defrag_end)
568 return 1;
569
570 *skip = 0;
571
572 /*
573 * hopefully we have this extent in the tree already, try without
574 * the full extent lock
575 */
576 read_lock(&em_tree->lock);
577 em = lookup_extent_mapping(em_tree, start, len);
578 read_unlock(&em_tree->lock);
579
580 if (!em) {
581 /* get the big lock and read metadata off disk */
582 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
583 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
584 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
585
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000586 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500587 return 0;
588 }
589
590 /* this will cover holes, and inline extents */
591 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
592 ret = 0;
593
594 /*
595 * we hit a real extent, if it is big don't bother defragging it again
596 */
Chris Mason1e701a32010-03-11 09:42:04 -0500597 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500598 ret = 0;
599
600 /*
601 * last_len ends up being a counter of how many bytes we've defragged.
602 * every time we choose not to defrag an extent, we reset *last_len
603 * so that the next tiny extent will force a defrag.
604 *
605 * The end result of this is that tiny extents before a single big
606 * extent will force at least part of that big extent to be defragged.
607 */
608 if (ret) {
609 *last_len += len;
610 *defrag_end = extent_map_end(em);
611 } else {
612 *last_len = 0;
613 *skip = extent_map_end(em);
614 *defrag_end = 0;
615 }
616
617 free_extent_map(em);
618 return ret;
619}
620
Chris Mason1e701a32010-03-11 09:42:04 -0500621static int btrfs_defrag_file(struct file *file,
622 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623{
624 struct inode *inode = fdentry(file)->d_inode;
625 struct btrfs_root *root = BTRFS_I(inode)->root;
626 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400627 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400628 struct page *page;
629 unsigned long last_index;
630 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
631 unsigned long total_read = 0;
632 u64 page_start;
633 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500634 u64 last_len = 0;
635 u64 skip = 0;
636 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400637 unsigned long i;
638 int ret;
639
Chris Mason940100a2010-03-10 10:52:59 -0500640 if (inode->i_size == 0)
641 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400642
Chris Mason1e701a32010-03-11 09:42:04 -0500643 if (range->start + range->len > range->start) {
644 last_index = min_t(u64, inode->i_size - 1,
645 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
646 } else {
647 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
648 }
649
650 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500651 while (i <= last_index) {
652 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500653 PAGE_CACHE_SIZE,
654 range->extent_thresh,
655 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500656 &defrag_end)) {
657 unsigned long next;
658 /*
659 * the should_defrag function tells us how much to skip
660 * bump our counter by the suggested amount
661 */
662 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
663 i = max(i + 1, next);
664 continue;
665 }
666
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400667 if (total_read % ra_pages == 0) {
668 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
669 min(last_index, i + ra_pages - 1));
670 }
671 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500672 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500673 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
674 BTRFS_I(inode)->force_compress = 1;
Chris Mason940100a2010-03-10 10:52:59 -0500675
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400676 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
677 if (ret)
678 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400679again:
Chris Mason940100a2010-03-10 10:52:59 -0500680 if (inode->i_size == 0 ||
681 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
682 ret = 0;
683 goto err_reservations;
684 }
685
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400686 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400687 if (!page) {
688 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500689 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 }
Chris Mason940100a2010-03-10 10:52:59 -0500691
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400692 if (!PageUptodate(page)) {
693 btrfs_readpage(NULL, page);
694 lock_page(page);
695 if (!PageUptodate(page)) {
696 unlock_page(page);
697 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400698 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500699 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400700 }
701 }
702
Chris Mason940100a2010-03-10 10:52:59 -0500703 if (page->mapping != inode->i_mapping) {
704 unlock_page(page);
705 page_cache_release(page);
706 goto again;
707 }
708
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400709 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400710
Chris Mason940100a2010-03-10 10:52:59 -0500711 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400712 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500713 goto loop_unlock;
714 }
715
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400716 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
717 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400718 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400719
720 ordered = btrfs_lookup_ordered_extent(inode, page_start);
721 if (ordered) {
722 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
723 unlock_page(page);
724 page_cache_release(page);
725 btrfs_start_ordered_extent(inode, ordered, 1);
726 btrfs_put_ordered_extent(ordered);
727 goto again;
728 }
729 set_page_extent_mapped(page);
730
Chris Masonf87f0572008-08-01 11:27:23 -0400731 /*
732 * this makes sure page_mkwrite is called on the
733 * page if it is dirtied again later
734 */
735 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500736 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
737 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
738 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400739
Josef Bacik2ac55d42010-02-03 19:33:23 +0000740 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500741 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400742 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400743 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500744
745loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400746 unlock_page(page);
747 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500748 mutex_unlock(&inode->i_mutex);
749
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400750 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500751 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752 }
753
Chris Mason1e701a32010-03-11 09:42:04 -0500754 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
755 filemap_flush(inode->i_mapping);
756
757 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
758 /* the filemap_flush will queue IO into the worker threads, but
759 * we have to make sure the IO is actually started and that
760 * ordered extents get created before we return
761 */
762 atomic_inc(&root->fs_info->async_submit_draining);
763 while (atomic_read(&root->fs_info->nr_async_submits) ||
764 atomic_read(&root->fs_info->async_delalloc_pages)) {
765 wait_event(root->fs_info->async_submit_wait,
766 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
767 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
768 }
769 atomic_dec(&root->fs_info->async_submit_draining);
770
771 mutex_lock(&inode->i_mutex);
772 BTRFS_I(inode)->force_compress = 0;
773 mutex_unlock(&inode->i_mutex);
774 }
775
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400776 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500777
778err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400779 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
780err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500781 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500782 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400783}
784
Yan, Zheng76dda932009-09-21 16:00:26 -0400785static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
786 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400787{
788 u64 new_size;
789 u64 old_size;
790 u64 devid = 1;
791 struct btrfs_ioctl_vol_args *vol_args;
792 struct btrfs_trans_handle *trans;
793 struct btrfs_device *device = NULL;
794 char *sizestr;
795 char *devstr = NULL;
796 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400797 int mod = 0;
798
Yan Zhengc146afa2008-11-12 14:34:12 -0500799 if (root->fs_info->sb->s_flags & MS_RDONLY)
800 return -EROFS;
801
Chris Masone441d542009-01-05 16:57:23 -0500802 if (!capable(CAP_SYS_ADMIN))
803 return -EPERM;
804
Li Zefandae7b662009-04-08 15:06:54 +0800805 vol_args = memdup_user(arg, sizeof(*vol_args));
806 if (IS_ERR(vol_args))
807 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400808
809 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400810
Chris Mason7d9eb122008-07-08 14:19:17 -0400811 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400812 sizestr = vol_args->name;
813 devstr = strchr(sizestr, ':');
814 if (devstr) {
815 char *end;
816 sizestr = devstr + 1;
817 *devstr = '\0';
818 devstr = vol_args->name;
819 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700820 printk(KERN_INFO "resizing devid %llu\n",
821 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400822 }
Yan Zheng2b820322008-11-17 21:11:30 -0500823 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400824 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700825 printk(KERN_INFO "resizer unable to find device %llu\n",
826 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400827 ret = -EINVAL;
828 goto out_unlock;
829 }
830 if (!strcmp(sizestr, "max"))
831 new_size = device->bdev->bd_inode->i_size;
832 else {
833 if (sizestr[0] == '-') {
834 mod = -1;
835 sizestr++;
836 } else if (sizestr[0] == '+') {
837 mod = 1;
838 sizestr++;
839 }
Akinobu Mita91748462010-02-28 10:59:11 +0000840 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400841 if (new_size == 0) {
842 ret = -EINVAL;
843 goto out_unlock;
844 }
845 }
846
847 old_size = device->total_bytes;
848
849 if (mod < 0) {
850 if (new_size > old_size) {
851 ret = -EINVAL;
852 goto out_unlock;
853 }
854 new_size = old_size - new_size;
855 } else if (mod > 0) {
856 new_size = old_size + new_size;
857 }
858
859 if (new_size < 256 * 1024 * 1024) {
860 ret = -EINVAL;
861 goto out_unlock;
862 }
863 if (new_size > device->bdev->bd_inode->i_size) {
864 ret = -EFBIG;
865 goto out_unlock;
866 }
867
868 do_div(new_size, root->sectorsize);
869 new_size *= root->sectorsize;
870
871 printk(KERN_INFO "new size for %s is %llu\n",
872 device->name, (unsigned long long)new_size);
873
874 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400875 trans = btrfs_start_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400876 ret = btrfs_grow_device(trans, device, new_size);
877 btrfs_commit_transaction(trans, root);
878 } else {
879 ret = btrfs_shrink_device(device, new_size);
880 }
881
882out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400883 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400884 kfree(vol_args);
885 return ret;
886}
887
Sage Weil72fd0322010-10-29 15:41:32 -0400888static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
889 char *name,
890 unsigned long fd,
891 int subvol,
892 u64 *transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400893{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400894 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -0500895 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400896 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500897 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400898
Yan Zhengc146afa2008-11-12 14:34:12 -0500899 if (root->fs_info->sb->s_flags & MS_RDONLY)
900 return -EROFS;
901
Sage Weil72fd0322010-10-29 15:41:32 -0400902 namelen = strlen(name);
903 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400904 ret = -EINVAL;
905 goto out;
906 }
907
Chris Mason3de45862008-11-17 21:02:50 -0500908 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -0400909 ret = btrfs_mksubvol(&file->f_path, name, namelen,
910 NULL, transid);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400911 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500912 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -0400913 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -0500914 if (!src_file) {
915 ret = -EINVAL;
916 goto out;
917 }
918
919 src_inode = src_file->f_path.dentry->d_inode;
920 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500921 printk(KERN_INFO "btrfs: Snapshot src from "
922 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500923 ret = -EINVAL;
924 fput(src_file);
925 goto out;
926 }
Sage Weil72fd0322010-10-29 15:41:32 -0400927 ret = btrfs_mksubvol(&file->f_path, name, namelen,
928 BTRFS_I(src_inode)->root,
929 transid);
Chris Mason3de45862008-11-17 21:02:50 -0500930 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400931 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400932out:
Sage Weil72fd0322010-10-29 15:41:32 -0400933 return ret;
934}
935
936static noinline int btrfs_ioctl_snap_create(struct file *file,
937 void __user *arg, int subvol,
938 int async)
939{
940 struct btrfs_ioctl_vol_args *vol_args = NULL;
941 struct btrfs_ioctl_async_vol_args *async_vol_args = NULL;
942 char *name;
943 u64 fd;
944 u64 transid = 0;
945 int ret;
946
947 if (async) {
948 async_vol_args = memdup_user(arg, sizeof(*async_vol_args));
949 if (IS_ERR(async_vol_args))
950 return PTR_ERR(async_vol_args);
951
952 name = async_vol_args->name;
953 fd = async_vol_args->fd;
954 async_vol_args->name[BTRFS_SNAPSHOT_NAME_MAX] = '\0';
955 } else {
956 vol_args = memdup_user(arg, sizeof(*vol_args));
957 if (IS_ERR(vol_args))
958 return PTR_ERR(vol_args);
959 name = vol_args->name;
960 fd = vol_args->fd;
961 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
962 }
963
964 ret = btrfs_ioctl_snap_create_transid(file, name, fd,
965 subvol, &transid);
966
967 if (!ret && async) {
968 if (copy_to_user(arg +
969 offsetof(struct btrfs_ioctl_async_vol_args,
970 transid), &transid, sizeof(transid)))
971 return -EFAULT;
972 }
973
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400974 kfree(vol_args);
Sage Weil72fd0322010-10-29 15:41:32 -0400975 kfree(async_vol_args);
976
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400977 return ret;
978}
979
Yan, Zheng76dda932009-09-21 16:00:26 -0400980/*
981 * helper to check if the subvolume references other subvolumes
982 */
983static noinline int may_destroy_subvol(struct btrfs_root *root)
984{
985 struct btrfs_path *path;
986 struct btrfs_key key;
987 int ret;
988
989 path = btrfs_alloc_path();
990 if (!path)
991 return -ENOMEM;
992
993 key.objectid = root->root_key.objectid;
994 key.type = BTRFS_ROOT_REF_KEY;
995 key.offset = (u64)-1;
996
997 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
998 &key, path, 0, 0);
999 if (ret < 0)
1000 goto out;
1001 BUG_ON(ret == 0);
1002
1003 ret = 0;
1004 if (path->slots[0] > 0) {
1005 path->slots[0]--;
1006 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1007 if (key.objectid == root->root_key.objectid &&
1008 key.type == BTRFS_ROOT_REF_KEY)
1009 ret = -ENOTEMPTY;
1010 }
1011out:
1012 btrfs_free_path(path);
1013 return ret;
1014}
1015
Chris Masonac8e9812010-02-28 15:39:26 -05001016static noinline int key_in_sk(struct btrfs_key *key,
1017 struct btrfs_ioctl_search_key *sk)
1018{
Chris Masonabc6e132010-03-18 12:10:08 -04001019 struct btrfs_key test;
1020 int ret;
1021
1022 test.objectid = sk->min_objectid;
1023 test.type = sk->min_type;
1024 test.offset = sk->min_offset;
1025
1026 ret = btrfs_comp_cpu_keys(key, &test);
1027 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001028 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001029
1030 test.objectid = sk->max_objectid;
1031 test.type = sk->max_type;
1032 test.offset = sk->max_offset;
1033
1034 ret = btrfs_comp_cpu_keys(key, &test);
1035 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001036 return 0;
1037 return 1;
1038}
1039
1040static noinline int copy_to_sk(struct btrfs_root *root,
1041 struct btrfs_path *path,
1042 struct btrfs_key *key,
1043 struct btrfs_ioctl_search_key *sk,
1044 char *buf,
1045 unsigned long *sk_offset,
1046 int *num_found)
1047{
1048 u64 found_transid;
1049 struct extent_buffer *leaf;
1050 struct btrfs_ioctl_search_header sh;
1051 unsigned long item_off;
1052 unsigned long item_len;
1053 int nritems;
1054 int i;
1055 int slot;
1056 int found = 0;
1057 int ret = 0;
1058
1059 leaf = path->nodes[0];
1060 slot = path->slots[0];
1061 nritems = btrfs_header_nritems(leaf);
1062
1063 if (btrfs_header_generation(leaf) > sk->max_transid) {
1064 i = nritems;
1065 goto advance_key;
1066 }
1067 found_transid = btrfs_header_generation(leaf);
1068
1069 for (i = slot; i < nritems; i++) {
1070 item_off = btrfs_item_ptr_offset(leaf, i);
1071 item_len = btrfs_item_size_nr(leaf, i);
1072
1073 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1074 item_len = 0;
1075
1076 if (sizeof(sh) + item_len + *sk_offset >
1077 BTRFS_SEARCH_ARGS_BUFSIZE) {
1078 ret = 1;
1079 goto overflow;
1080 }
1081
1082 btrfs_item_key_to_cpu(leaf, key, i);
1083 if (!key_in_sk(key, sk))
1084 continue;
1085
1086 sh.objectid = key->objectid;
1087 sh.offset = key->offset;
1088 sh.type = key->type;
1089 sh.len = item_len;
1090 sh.transid = found_transid;
1091
1092 /* copy search result header */
1093 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1094 *sk_offset += sizeof(sh);
1095
1096 if (item_len) {
1097 char *p = buf + *sk_offset;
1098 /* copy the item */
1099 read_extent_buffer(leaf, p,
1100 item_off, item_len);
1101 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001102 }
Chris Mason90fdde12010-03-18 12:14:54 -04001103 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001104
1105 if (*num_found >= sk->nr_items)
1106 break;
1107 }
1108advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001109 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001110 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1111 key->offset++;
1112 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1113 key->offset = 0;
1114 key->type++;
1115 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1116 key->offset = 0;
1117 key->type = 0;
1118 key->objectid++;
1119 } else
1120 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001121overflow:
1122 *num_found += found;
1123 return ret;
1124}
1125
1126static noinline int search_ioctl(struct inode *inode,
1127 struct btrfs_ioctl_search_args *args)
1128{
1129 struct btrfs_root *root;
1130 struct btrfs_key key;
1131 struct btrfs_key max_key;
1132 struct btrfs_path *path;
1133 struct btrfs_ioctl_search_key *sk = &args->key;
1134 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1135 int ret;
1136 int num_found = 0;
1137 unsigned long sk_offset = 0;
1138
1139 path = btrfs_alloc_path();
1140 if (!path)
1141 return -ENOMEM;
1142
1143 if (sk->tree_id == 0) {
1144 /* search the root of the inode that was passed */
1145 root = BTRFS_I(inode)->root;
1146 } else {
1147 key.objectid = sk->tree_id;
1148 key.type = BTRFS_ROOT_ITEM_KEY;
1149 key.offset = (u64)-1;
1150 root = btrfs_read_fs_root_no_name(info, &key);
1151 if (IS_ERR(root)) {
1152 printk(KERN_ERR "could not find root %llu\n",
1153 sk->tree_id);
1154 btrfs_free_path(path);
1155 return -ENOENT;
1156 }
1157 }
1158
1159 key.objectid = sk->min_objectid;
1160 key.type = sk->min_type;
1161 key.offset = sk->min_offset;
1162
1163 max_key.objectid = sk->max_objectid;
1164 max_key.type = sk->max_type;
1165 max_key.offset = sk->max_offset;
1166
1167 path->keep_locks = 1;
1168
1169 while(1) {
1170 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1171 sk->min_transid);
1172 if (ret != 0) {
1173 if (ret > 0)
1174 ret = 0;
1175 goto err;
1176 }
1177 ret = copy_to_sk(root, path, &key, sk, args->buf,
1178 &sk_offset, &num_found);
1179 btrfs_release_path(root, path);
1180 if (ret || num_found >= sk->nr_items)
1181 break;
1182
1183 }
1184 ret = 0;
1185err:
1186 sk->nr_items = num_found;
1187 btrfs_free_path(path);
1188 return ret;
1189}
1190
1191static noinline int btrfs_ioctl_tree_search(struct file *file,
1192 void __user *argp)
1193{
1194 struct btrfs_ioctl_search_args *args;
1195 struct inode *inode;
1196 int ret;
1197
1198 if (!capable(CAP_SYS_ADMIN))
1199 return -EPERM;
1200
Julia Lawall2354d082010-10-29 15:14:18 -04001201 args = memdup_user(argp, sizeof(*args));
1202 if (IS_ERR(args))
1203 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001204
Chris Masonac8e9812010-02-28 15:39:26 -05001205 inode = fdentry(file)->d_inode;
1206 ret = search_ioctl(inode, args);
1207 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1208 ret = -EFAULT;
1209 kfree(args);
1210 return ret;
1211}
1212
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001213/*
Chris Masonac8e9812010-02-28 15:39:26 -05001214 * Search INODE_REFs to identify path name of 'dirid' directory
1215 * in a 'tree_id' tree. and sets path name to 'name'.
1216 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001217static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1218 u64 tree_id, u64 dirid, char *name)
1219{
1220 struct btrfs_root *root;
1221 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001222 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001223 int ret = -1;
1224 int slot;
1225 int len;
1226 int total_len = 0;
1227 struct btrfs_inode_ref *iref;
1228 struct extent_buffer *l;
1229 struct btrfs_path *path;
1230
1231 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1232 name[0]='\0';
1233 return 0;
1234 }
1235
1236 path = btrfs_alloc_path();
1237 if (!path)
1238 return -ENOMEM;
1239
Chris Masonac8e9812010-02-28 15:39:26 -05001240 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001241
1242 key.objectid = tree_id;
1243 key.type = BTRFS_ROOT_ITEM_KEY;
1244 key.offset = (u64)-1;
1245 root = btrfs_read_fs_root_no_name(info, &key);
1246 if (IS_ERR(root)) {
1247 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001248 ret = -ENOENT;
1249 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001250 }
1251
1252 key.objectid = dirid;
1253 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001254 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001255
1256 while(1) {
1257 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1258 if (ret < 0)
1259 goto out;
1260
1261 l = path->nodes[0];
1262 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001263 if (ret > 0 && slot > 0)
1264 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001265 btrfs_item_key_to_cpu(l, &key, slot);
1266
1267 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001268 key.type != BTRFS_INODE_REF_KEY)) {
1269 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001270 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001271 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001272
1273 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1274 len = btrfs_inode_ref_name_len(l, iref);
1275 ptr -= len + 1;
1276 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001277 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001278 goto out;
1279
1280 *(ptr + len) = '/';
1281 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1282
1283 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1284 break;
1285
1286 btrfs_release_path(root, path);
1287 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001288 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001289 dirid = key.objectid;
1290
1291 }
Chris Masonac8e9812010-02-28 15:39:26 -05001292 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001293 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001294 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001295 name[total_len]='\0';
1296 ret = 0;
1297out:
1298 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001299 return ret;
1300}
1301
1302static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1303 void __user *argp)
1304{
1305 struct btrfs_ioctl_ino_lookup_args *args;
1306 struct inode *inode;
1307 int ret;
1308
1309 if (!capable(CAP_SYS_ADMIN))
1310 return -EPERM;
1311
Julia Lawall2354d082010-10-29 15:14:18 -04001312 args = memdup_user(argp, sizeof(*args));
1313 if (IS_ERR(args))
1314 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001315
Chris Masonac8e9812010-02-28 15:39:26 -05001316 inode = fdentry(file)->d_inode;
1317
Chris Mason1b53ac42010-03-18 12:17:05 -04001318 if (args->treeid == 0)
1319 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1320
Chris Masonac8e9812010-02-28 15:39:26 -05001321 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1322 args->treeid, args->objectid,
1323 args->name);
1324
1325 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1326 ret = -EFAULT;
1327
1328 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001329 return ret;
1330}
1331
Yan, Zheng76dda932009-09-21 16:00:26 -04001332static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1333 void __user *arg)
1334{
1335 struct dentry *parent = fdentry(file);
1336 struct dentry *dentry;
1337 struct inode *dir = parent->d_inode;
1338 struct inode *inode;
1339 struct btrfs_root *root = BTRFS_I(dir)->root;
1340 struct btrfs_root *dest = NULL;
1341 struct btrfs_ioctl_vol_args *vol_args;
1342 struct btrfs_trans_handle *trans;
1343 int namelen;
1344 int ret;
1345 int err = 0;
1346
Yan, Zheng76dda932009-09-21 16:00:26 -04001347 vol_args = memdup_user(arg, sizeof(*vol_args));
1348 if (IS_ERR(vol_args))
1349 return PTR_ERR(vol_args);
1350
1351 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1352 namelen = strlen(vol_args->name);
1353 if (strchr(vol_args->name, '/') ||
1354 strncmp(vol_args->name, "..", namelen) == 0) {
1355 err = -EINVAL;
1356 goto out;
1357 }
1358
1359 err = mnt_want_write(file->f_path.mnt);
1360 if (err)
1361 goto out;
1362
1363 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1364 dentry = lookup_one_len(vol_args->name, parent, namelen);
1365 if (IS_ERR(dentry)) {
1366 err = PTR_ERR(dentry);
1367 goto out_unlock_dir;
1368 }
1369
1370 if (!dentry->d_inode) {
1371 err = -ENOENT;
1372 goto out_dput;
1373 }
1374
1375 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001376 dest = BTRFS_I(inode)->root;
1377 if (!capable(CAP_SYS_ADMIN)){
1378 /*
1379 * Regular user. Only allow this with a special mount
1380 * option, when the user has write+exec access to the
1381 * subvol root, and when rmdir(2) would have been
1382 * allowed.
1383 *
1384 * Note that this is _not_ check that the subvol is
1385 * empty or doesn't contain data that we wouldn't
1386 * otherwise be able to delete.
1387 *
1388 * Users who want to delete empty subvols should try
1389 * rmdir(2).
1390 */
1391 err = -EPERM;
1392 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1393 goto out_dput;
1394
1395 /*
1396 * Do not allow deletion if the parent dir is the same
1397 * as the dir to be deleted. That means the ioctl
1398 * must be called on the dentry referencing the root
1399 * of the subvol, not a random directory contained
1400 * within it.
1401 */
1402 err = -EINVAL;
1403 if (root == dest)
1404 goto out_dput;
1405
1406 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1407 if (err)
1408 goto out_dput;
1409
1410 /* check if subvolume may be deleted by a non-root user */
1411 err = btrfs_may_delete(dir, dentry, 1);
1412 if (err)
1413 goto out_dput;
1414 }
1415
Yan, Zheng76dda932009-09-21 16:00:26 -04001416 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1417 err = -EINVAL;
1418 goto out_dput;
1419 }
1420
Yan, Zheng76dda932009-09-21 16:00:26 -04001421 mutex_lock(&inode->i_mutex);
1422 err = d_invalidate(dentry);
1423 if (err)
1424 goto out_unlock;
1425
1426 down_write(&root->fs_info->subvol_sem);
1427
1428 err = may_destroy_subvol(dest);
1429 if (err)
1430 goto out_up_write;
1431
Yan, Zhenga22285a2010-05-16 10:48:46 -04001432 trans = btrfs_start_transaction(root, 0);
1433 if (IS_ERR(trans)) {
1434 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001435 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001436 }
1437 trans->block_rsv = &root->fs_info->global_block_rsv;
1438
Yan, Zheng76dda932009-09-21 16:00:26 -04001439 ret = btrfs_unlink_subvol(trans, root, dir,
1440 dest->root_key.objectid,
1441 dentry->d_name.name,
1442 dentry->d_name.len);
1443 BUG_ON(ret);
1444
1445 btrfs_record_root_in_trans(trans, dest);
1446
1447 memset(&dest->root_item.drop_progress, 0,
1448 sizeof(dest->root_item.drop_progress));
1449 dest->root_item.drop_level = 0;
1450 btrfs_set_root_refs(&dest->root_item, 0);
1451
Yan, Zhengd68fc572010-05-16 10:49:58 -04001452 if (!xchg(&dest->orphan_item_inserted, 1)) {
1453 ret = btrfs_insert_orphan_item(trans,
1454 root->fs_info->tree_root,
1455 dest->root_key.objectid);
1456 BUG_ON(ret);
1457 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001458
Sage Weil531cb132010-10-29 15:41:32 -04001459 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001460 BUG_ON(ret);
1461 inode->i_flags |= S_DEAD;
1462out_up_write:
1463 up_write(&root->fs_info->subvol_sem);
1464out_unlock:
1465 mutex_unlock(&inode->i_mutex);
1466 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001467 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001468 btrfs_invalidate_inodes(dest);
1469 d_delete(dentry);
1470 }
1471out_dput:
1472 dput(dentry);
1473out_unlock_dir:
1474 mutex_unlock(&dir->i_mutex);
1475 mnt_drop_write(file->f_path.mnt);
1476out:
1477 kfree(vol_args);
1478 return err;
1479}
1480
Chris Mason1e701a32010-03-11 09:42:04 -05001481static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001482{
1483 struct inode *inode = fdentry(file)->d_inode;
1484 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001485 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001486 int ret;
1487
1488 ret = mnt_want_write(file->f_path.mnt);
1489 if (ret)
1490 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001491
1492 switch (inode->i_mode & S_IFMT) {
1493 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001494 if (!capable(CAP_SYS_ADMIN)) {
1495 ret = -EPERM;
1496 goto out;
1497 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001498 ret = btrfs_defrag_root(root, 0);
1499 if (ret)
1500 goto out;
1501 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001502 break;
1503 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001504 if (!(file->f_mode & FMODE_WRITE)) {
1505 ret = -EINVAL;
1506 goto out;
1507 }
Chris Mason1e701a32010-03-11 09:42:04 -05001508
1509 range = kzalloc(sizeof(*range), GFP_KERNEL);
1510 if (!range) {
1511 ret = -ENOMEM;
1512 goto out;
1513 }
1514
1515 if (argp) {
1516 if (copy_from_user(range, argp,
1517 sizeof(*range))) {
1518 ret = -EFAULT;
1519 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001520 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001521 }
1522 /* compression requires us to start the IO */
1523 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1524 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1525 range->extent_thresh = (u32)-1;
1526 }
1527 } else {
1528 /* the rest are all set to zero by kzalloc */
1529 range->len = (u64)-1;
1530 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001531 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001532 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001533 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001534 default:
1535 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001536 }
Chris Masone441d542009-01-05 16:57:23 -05001537out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001538 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001539 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001540}
1541
Christoph Hellwigb2950862008-12-02 09:54:17 -05001542static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543{
1544 struct btrfs_ioctl_vol_args *vol_args;
1545 int ret;
1546
Chris Masone441d542009-01-05 16:57:23 -05001547 if (!capable(CAP_SYS_ADMIN))
1548 return -EPERM;
1549
Li Zefandae7b662009-04-08 15:06:54 +08001550 vol_args = memdup_user(arg, sizeof(*vol_args));
1551 if (IS_ERR(vol_args))
1552 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553
Mark Fasheh5516e592008-07-24 12:20:14 -04001554 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001555 ret = btrfs_init_new_device(root, vol_args->name);
1556
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001557 kfree(vol_args);
1558 return ret;
1559}
1560
Christoph Hellwigb2950862008-12-02 09:54:17 -05001561static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001562{
1563 struct btrfs_ioctl_vol_args *vol_args;
1564 int ret;
1565
Chris Masone441d542009-01-05 16:57:23 -05001566 if (!capable(CAP_SYS_ADMIN))
1567 return -EPERM;
1568
Yan Zhengc146afa2008-11-12 14:34:12 -05001569 if (root->fs_info->sb->s_flags & MS_RDONLY)
1570 return -EROFS;
1571
Li Zefandae7b662009-04-08 15:06:54 +08001572 vol_args = memdup_user(arg, sizeof(*vol_args));
1573 if (IS_ERR(vol_args))
1574 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001575
Mark Fasheh5516e592008-07-24 12:20:14 -04001576 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001577 ret = btrfs_rm_device(root, vol_args->name);
1578
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001579 kfree(vol_args);
1580 return ret;
1581}
1582
Yan, Zheng76dda932009-09-21 16:00:26 -04001583static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1584 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001585{
1586 struct inode *inode = fdentry(file)->d_inode;
1587 struct btrfs_root *root = BTRFS_I(inode)->root;
1588 struct file *src_file;
1589 struct inode *src;
1590 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001591 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001592 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001593 char *buf;
1594 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001595 u32 nritems;
1596 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001597 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001598 u64 len = olen;
1599 u64 bs = root->fs_info->sb->s_blocksize;
1600 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001601
Sage Weilc5c9cd42008-11-12 14:32:25 -05001602 /*
1603 * TODO:
1604 * - split compressed inline extents. annoying: we need to
1605 * decompress into destination's address_space (the file offset
1606 * may change, so source mapping won't do), then recompress (or
1607 * otherwise reinsert) a subrange.
1608 * - allow ranges within the same file to be cloned (provided
1609 * they don't overlap)?
1610 */
1611
Chris Masone441d542009-01-05 16:57:23 -05001612 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001613 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001614 return -EINVAL;
1615
Yan Zhengc146afa2008-11-12 14:34:12 -05001616 ret = mnt_want_write(file->f_path.mnt);
1617 if (ret)
1618 return ret;
1619
Sage Weilc5c9cd42008-11-12 14:32:25 -05001620 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001621 if (!src_file) {
1622 ret = -EBADF;
1623 goto out_drop_write;
1624 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001625
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626 src = src_file->f_dentry->d_inode;
1627
Sage Weilc5c9cd42008-11-12 14:32:25 -05001628 ret = -EINVAL;
1629 if (src == inode)
1630 goto out_fput;
1631
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001632 /* the src must be open for reading */
1633 if (!(src_file->f_mode & FMODE_READ))
1634 goto out_fput;
1635
Yan Zhengae01a0a2008-08-04 23:23:47 -04001636 ret = -EISDIR;
1637 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001638 goto out_fput;
1639
Yan Zhengae01a0a2008-08-04 23:23:47 -04001640 ret = -EXDEV;
1641 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1642 goto out_fput;
1643
1644 ret = -ENOMEM;
1645 buf = vmalloc(btrfs_level_size(root, 0));
1646 if (!buf)
1647 goto out_fput;
1648
1649 path = btrfs_alloc_path();
1650 if (!path) {
1651 vfree(buf);
1652 goto out_fput;
1653 }
1654 path->reada = 2;
1655
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001656 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001657 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1658 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001659 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001660 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1661 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001662 }
1663
Sage Weilc5c9cd42008-11-12 14:32:25 -05001664 /* determine range to clone */
1665 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001666 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001667 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001668 if (len == 0)
1669 olen = len = src->i_size - off;
1670 /* if we extend to eof, continue to block boundary */
1671 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001672 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001673
1674 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001675 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1676 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001677 goto out_unlock;
1678
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001679 /* do any pending delalloc/csum calc on src, one way or
1680 another, and lock file content */
1681 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001682 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001683 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001684 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1685 if (!ordered &&
1686 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1687 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001688 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001689 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001690 if (ordered)
1691 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001692 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001693 }
1694
Sage Weilc5c9cd42008-11-12 14:32:25 -05001695 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001697 key.type = BTRFS_EXTENT_DATA_KEY;
1698 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001699
1700 while (1) {
1701 /*
1702 * note the key will change type as we walk through the
1703 * tree.
1704 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001705 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 if (ret < 0)
1707 goto out;
1708
Yan Zhengae01a0a2008-08-04 23:23:47 -04001709 nritems = btrfs_header_nritems(path->nodes[0]);
1710 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001711 ret = btrfs_next_leaf(root, path);
1712 if (ret < 0)
1713 goto out;
1714 if (ret > 0)
1715 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001716 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001717 }
1718 leaf = path->nodes[0];
1719 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720
Yan Zhengae01a0a2008-08-04 23:23:47 -04001721 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001722 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001723 key.objectid != src->i_ino)
1724 break;
1725
Sage Weilc5c9cd42008-11-12 14:32:25 -05001726 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1727 struct btrfs_file_extent_item *extent;
1728 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001729 u32 size;
1730 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001731 u64 disko = 0, diskl = 0;
1732 u64 datao = 0, datal = 0;
1733 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001734 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001735
1736 size = btrfs_item_size_nr(leaf, slot);
1737 read_extent_buffer(leaf, buf,
1738 btrfs_item_ptr_offset(leaf, slot),
1739 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001740
1741 extent = btrfs_item_ptr(leaf, slot,
1742 struct btrfs_file_extent_item);
1743 comp = btrfs_file_extent_compression(leaf, extent);
1744 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001745 if (type == BTRFS_FILE_EXTENT_REG ||
1746 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001747 disko = btrfs_file_extent_disk_bytenr(leaf,
1748 extent);
1749 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1750 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001751 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001752 datal = btrfs_file_extent_num_bytes(leaf,
1753 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001754 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1755 /* take upper bound, may be compressed */
1756 datal = btrfs_file_extent_ram_bytes(leaf,
1757 extent);
1758 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001759 btrfs_release_path(root, path);
1760
Sage Weil050006a2010-10-29 15:37:33 -04001761 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001762 key.offset >= off+len)
1763 goto next;
1764
Zheng Yan31840ae2008-09-23 13:14:14 -04001765 memcpy(&new_key, &key, sizeof(new_key));
1766 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001767 new_key.offset = key.offset + destoff - off;
1768
Yan, Zhenga22285a2010-05-16 10:48:46 -04001769 trans = btrfs_start_transaction(root, 1);
1770 if (IS_ERR(trans)) {
1771 ret = PTR_ERR(trans);
1772 goto out;
1773 }
1774
Chris Masonc8a894d2009-06-27 21:07:03 -04001775 if (type == BTRFS_FILE_EXTENT_REG ||
1776 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001777 if (off > key.offset) {
1778 datao += off - key.offset;
1779 datal -= off - key.offset;
1780 }
1781
1782 if (key.offset + datal > off + len)
1783 datal = off + len - key.offset;
1784
1785 ret = btrfs_drop_extents(trans, inode,
1786 new_key.offset,
1787 new_key.offset + datal,
1788 &hint_byte, 1);
1789 BUG_ON(ret);
1790
Sage Weilc5c9cd42008-11-12 14:32:25 -05001791 ret = btrfs_insert_empty_item(trans, root, path,
1792 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001793 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001794
1795 leaf = path->nodes[0];
1796 slot = path->slots[0];
1797 write_extent_buffer(leaf, buf,
1798 btrfs_item_ptr_offset(leaf, slot),
1799 size);
1800
1801 extent = btrfs_item_ptr(leaf, slot,
1802 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001803
Sage Weilc5c9cd42008-11-12 14:32:25 -05001804 /* disko == 0 means it's a hole */
1805 if (!disko)
1806 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001807
1808 btrfs_set_file_extent_offset(leaf, extent,
1809 datao);
1810 btrfs_set_file_extent_num_bytes(leaf, extent,
1811 datal);
1812 if (disko) {
1813 inode_add_bytes(inode, datal);
1814 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001815 disko, diskl, 0,
1816 root->root_key.objectid,
1817 inode->i_ino,
1818 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001819 BUG_ON(ret);
1820 }
1821 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1822 u64 skip = 0;
1823 u64 trim = 0;
1824 if (off > key.offset) {
1825 skip = off - key.offset;
1826 new_key.offset += skip;
1827 }
Chris Masond3977122009-01-05 21:25:51 -05001828
Sage Weilc5c9cd42008-11-12 14:32:25 -05001829 if (key.offset + datal > off+len)
1830 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001831
Sage Weilc5c9cd42008-11-12 14:32:25 -05001832 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001833 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001834 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001835 goto out;
1836 }
1837 size -= skip + trim;
1838 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001839
1840 ret = btrfs_drop_extents(trans, inode,
1841 new_key.offset,
1842 new_key.offset + datal,
1843 &hint_byte, 1);
1844 BUG_ON(ret);
1845
Sage Weilc5c9cd42008-11-12 14:32:25 -05001846 ret = btrfs_insert_empty_item(trans, root, path,
1847 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001848 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001849
1850 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001851 u32 start =
1852 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001853 memmove(buf+start, buf+start+skip,
1854 datal);
1855 }
1856
1857 leaf = path->nodes[0];
1858 slot = path->slots[0];
1859 write_extent_buffer(leaf, buf,
1860 btrfs_item_ptr_offset(leaf, slot),
1861 size);
1862 inode_add_bytes(inode, datal);
1863 }
1864
1865 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001866 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001867
Yan, Zhenga22285a2010-05-16 10:48:46 -04001868 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00001869
1870 /*
1871 * we round up to the block size at eof when
1872 * determining which extents to clone above,
1873 * but shouldn't round up the file size
1874 */
1875 endoff = new_key.offset + datal;
1876 if (endoff > off+olen)
1877 endoff = off+olen;
1878 if (endoff > inode->i_size)
1879 btrfs_i_size_write(inode, endoff);
1880
Yan, Zhenga22285a2010-05-16 10:48:46 -04001881 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1882 ret = btrfs_update_inode(trans, root, inode);
1883 BUG_ON(ret);
1884 btrfs_end_transaction(trans, root);
1885 }
Chris Masond3977122009-01-05 21:25:51 -05001886next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001887 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001888 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001889 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001890 ret = 0;
1891out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001892 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001893 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001894out_unlock:
1895 mutex_unlock(&src->i_mutex);
1896 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001897 vfree(buf);
1898 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001899out_fput:
1900 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001901out_drop_write:
1902 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001903 return ret;
1904}
1905
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001906static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001907{
1908 struct btrfs_ioctl_clone_range_args args;
1909
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001910 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001911 return -EFAULT;
1912 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1913 args.src_length, args.dest_offset);
1914}
1915
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001916/*
1917 * there are many ways the trans_start and trans_end ioctls can lead
1918 * to deadlocks. They should only be used by applications that
1919 * basically own the machine, and have a very in depth understanding
1920 * of all the possible deadlocks and enospc problems.
1921 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001922static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001923{
1924 struct inode *inode = fdentry(file)->d_inode;
1925 struct btrfs_root *root = BTRFS_I(inode)->root;
1926 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001927 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001928
Sage Weil1ab86ae2009-09-29 18:38:44 -04001929 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001930 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001931 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001932
1933 ret = -EINPROGRESS;
1934 if (file->private_data)
1935 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001936
Yan Zhengc146afa2008-11-12 14:34:12 -05001937 ret = mnt_want_write(file->f_path.mnt);
1938 if (ret)
1939 goto out;
1940
Sage Weil9ca9ee02008-08-04 10:41:27 -04001941 mutex_lock(&root->fs_info->trans_mutex);
1942 root->fs_info->open_ioctl_trans++;
1943 mutex_unlock(&root->fs_info->trans_mutex);
1944
Sage Weil1ab86ae2009-09-29 18:38:44 -04001945 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001946 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001947 if (!trans)
1948 goto out_drop;
1949
1950 file->private_data = trans;
1951 return 0;
1952
1953out_drop:
1954 mutex_lock(&root->fs_info->trans_mutex);
1955 root->fs_info->open_ioctl_trans--;
1956 mutex_unlock(&root->fs_info->trans_mutex);
1957 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001958out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001959 return ret;
1960}
1961
Josef Bacik6ef5ed02009-12-11 21:11:29 +00001962static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
1963{
1964 struct inode *inode = fdentry(file)->d_inode;
1965 struct btrfs_root *root = BTRFS_I(inode)->root;
1966 struct btrfs_root *new_root;
1967 struct btrfs_dir_item *di;
1968 struct btrfs_trans_handle *trans;
1969 struct btrfs_path *path;
1970 struct btrfs_key location;
1971 struct btrfs_disk_key disk_key;
1972 struct btrfs_super_block *disk_super;
1973 u64 features;
1974 u64 objectid = 0;
1975 u64 dir_id;
1976
1977 if (!capable(CAP_SYS_ADMIN))
1978 return -EPERM;
1979
1980 if (copy_from_user(&objectid, argp, sizeof(objectid)))
1981 return -EFAULT;
1982
1983 if (!objectid)
1984 objectid = root->root_key.objectid;
1985
1986 location.objectid = objectid;
1987 location.type = BTRFS_ROOT_ITEM_KEY;
1988 location.offset = (u64)-1;
1989
1990 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
1991 if (IS_ERR(new_root))
1992 return PTR_ERR(new_root);
1993
1994 if (btrfs_root_refs(&new_root->root_item) == 0)
1995 return -ENOENT;
1996
1997 path = btrfs_alloc_path();
1998 if (!path)
1999 return -ENOMEM;
2000 path->leave_spinning = 1;
2001
2002 trans = btrfs_start_transaction(root, 1);
2003 if (!trans) {
2004 btrfs_free_path(path);
2005 return -ENOMEM;
2006 }
2007
2008 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2009 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2010 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002011 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002012 btrfs_free_path(path);
2013 btrfs_end_transaction(trans, root);
2014 printk(KERN_ERR "Umm, you don't have the default dir item, "
2015 "this isn't going to work\n");
2016 return -ENOENT;
2017 }
2018
2019 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2020 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2021 btrfs_mark_buffer_dirty(path->nodes[0]);
2022 btrfs_free_path(path);
2023
2024 disk_super = &root->fs_info->super_copy;
2025 features = btrfs_super_incompat_flags(disk_super);
2026 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2027 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2028 btrfs_set_super_incompat_flags(disk_super, features);
2029 }
2030 btrfs_end_transaction(trans, root);
2031
2032 return 0;
2033}
2034
Josef Bacikbf5fc092010-09-29 11:22:36 -04002035static void get_block_group_info(struct list_head *groups_list,
2036 struct btrfs_ioctl_space_info *space)
2037{
2038 struct btrfs_block_group_cache *block_group;
2039
2040 space->total_bytes = 0;
2041 space->used_bytes = 0;
2042 space->flags = 0;
2043 list_for_each_entry(block_group, groups_list, list) {
2044 space->flags = block_group->flags;
2045 space->total_bytes += block_group->key.offset;
2046 space->used_bytes +=
2047 btrfs_block_group_used(&block_group->item);
2048 }
2049}
2050
Josef Bacik1406e432010-01-13 18:19:06 +00002051long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2052{
2053 struct btrfs_ioctl_space_args space_args;
2054 struct btrfs_ioctl_space_info space;
2055 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002056 struct btrfs_ioctl_space_info *dest_orig;
2057 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002058 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002059 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2060 BTRFS_BLOCK_GROUP_SYSTEM,
2061 BTRFS_BLOCK_GROUP_METADATA,
2062 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2063 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002064 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002065 int ret = 0;
Chris Mason7fde62b2010-03-16 15:40:10 -04002066 int slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002067 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002068
2069 if (copy_from_user(&space_args,
2070 (struct btrfs_ioctl_space_args __user *)arg,
2071 sizeof(space_args)))
2072 return -EFAULT;
2073
Josef Bacikbf5fc092010-09-29 11:22:36 -04002074 for (i = 0; i < num_types; i++) {
2075 struct btrfs_space_info *tmp;
2076
2077 info = NULL;
2078 rcu_read_lock();
2079 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2080 list) {
2081 if (tmp->flags == types[i]) {
2082 info = tmp;
2083 break;
2084 }
2085 }
2086 rcu_read_unlock();
2087
2088 if (!info)
2089 continue;
2090
2091 down_read(&info->groups_sem);
2092 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2093 if (!list_empty(&info->block_groups[c]))
2094 slot_count++;
2095 }
2096 up_read(&info->groups_sem);
2097 }
Josef Bacik1406e432010-01-13 18:19:06 +00002098
Chris Mason7fde62b2010-03-16 15:40:10 -04002099 /* space_slots == 0 means they are asking for a count */
2100 if (space_args.space_slots == 0) {
2101 space_args.total_spaces = slot_count;
2102 goto out;
2103 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002104
2105 slot_count = min_t(int, space_args.space_slots, slot_count);
2106
Chris Mason7fde62b2010-03-16 15:40:10 -04002107 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002108
Chris Mason7fde62b2010-03-16 15:40:10 -04002109 /* we generally have at most 6 or so space infos, one for each raid
2110 * level. So, a whole page should be more than enough for everyone
2111 */
2112 if (alloc_size > PAGE_CACHE_SIZE)
2113 return -ENOMEM;
2114
2115 space_args.total_spaces = 0;
2116 dest = kmalloc(alloc_size, GFP_NOFS);
2117 if (!dest)
2118 return -ENOMEM;
2119 dest_orig = dest;
2120
2121 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002122 for (i = 0; i < num_types; i++) {
2123 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002124
Josef Bacikbf5fc092010-09-29 11:22:36 -04002125 info = NULL;
2126 rcu_read_lock();
2127 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2128 list) {
2129 if (tmp->flags == types[i]) {
2130 info = tmp;
2131 break;
2132 }
2133 }
2134 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002135
Josef Bacikbf5fc092010-09-29 11:22:36 -04002136 if (!info)
2137 continue;
2138 down_read(&info->groups_sem);
2139 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2140 if (!list_empty(&info->block_groups[c])) {
2141 get_block_group_info(&info->block_groups[c],
2142 &space);
2143 memcpy(dest, &space, sizeof(space));
2144 dest++;
2145 space_args.total_spaces++;
2146 }
2147 }
2148 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002149 }
Josef Bacik1406e432010-01-13 18:19:06 +00002150
Chris Mason7fde62b2010-03-16 15:40:10 -04002151 user_dest = (struct btrfs_ioctl_space_info *)
2152 (arg + sizeof(struct btrfs_ioctl_space_args));
2153
2154 if (copy_to_user(user_dest, dest_orig, alloc_size))
2155 ret = -EFAULT;
2156
2157 kfree(dest_orig);
2158out:
2159 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002160 ret = -EFAULT;
2161
2162 return ret;
2163}
2164
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002165/*
2166 * there are many ways the trans_start and trans_end ioctls can lead
2167 * to deadlocks. They should only be used by applications that
2168 * basically own the machine, and have a very in depth understanding
2169 * of all the possible deadlocks and enospc problems.
2170 */
2171long btrfs_ioctl_trans_end(struct file *file)
2172{
2173 struct inode *inode = fdentry(file)->d_inode;
2174 struct btrfs_root *root = BTRFS_I(inode)->root;
2175 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002176
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002177 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002178 if (!trans)
2179 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002180 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002181
Sage Weil1ab86ae2009-09-29 18:38:44 -04002182 btrfs_end_transaction(trans, root);
2183
Sage Weil9ca9ee02008-08-04 10:41:27 -04002184 mutex_lock(&root->fs_info->trans_mutex);
2185 root->fs_info->open_ioctl_trans--;
2186 mutex_unlock(&root->fs_info->trans_mutex);
2187
Sage Weilcfc8ea82008-12-11 16:30:06 -05002188 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002189 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002190}
2191
Sage Weil46204592010-10-29 15:41:32 -04002192static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2193{
2194 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2195 struct btrfs_trans_handle *trans;
2196 u64 transid;
2197
2198 trans = btrfs_start_transaction(root, 0);
2199 transid = trans->transid;
2200 btrfs_commit_transaction_async(trans, root, 0);
2201
2202 if (argp)
2203 if (copy_to_user(argp, &transid, sizeof(transid)))
2204 return -EFAULT;
2205 return 0;
2206}
2207
2208static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2209{
2210 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2211 u64 transid;
2212
2213 if (argp) {
2214 if (copy_from_user(&transid, argp, sizeof(transid)))
2215 return -EFAULT;
2216 } else {
2217 transid = 0; /* current trans */
2218 }
2219 return btrfs_wait_for_commit(root, transid);
2220}
2221
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002222long btrfs_ioctl(struct file *file, unsigned int
2223 cmd, unsigned long arg)
2224{
2225 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002226 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002227
2228 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002229 case FS_IOC_GETFLAGS:
2230 return btrfs_ioctl_getflags(file, argp);
2231 case FS_IOC_SETFLAGS:
2232 return btrfs_ioctl_setflags(file, argp);
2233 case FS_IOC_GETVERSION:
2234 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002235 case BTRFS_IOC_SNAP_CREATE:
Sage Weil72fd0322010-10-29 15:41:32 -04002236 return btrfs_ioctl_snap_create(file, argp, 0, 0);
2237 case BTRFS_IOC_SNAP_CREATE_ASYNC:
2238 return btrfs_ioctl_snap_create(file, argp, 0, 1);
Chris Mason3de45862008-11-17 21:02:50 -05002239 case BTRFS_IOC_SUBVOL_CREATE:
Sage Weil72fd0322010-10-29 15:41:32 -04002240 return btrfs_ioctl_snap_create(file, argp, 1, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04002241 case BTRFS_IOC_SNAP_DESTROY:
2242 return btrfs_ioctl_snap_destroy(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002243 case BTRFS_IOC_DEFAULT_SUBVOL:
2244 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002245 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002246 return btrfs_ioctl_defrag(file, NULL);
2247 case BTRFS_IOC_DEFRAG_RANGE:
2248 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002249 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002250 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002251 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002252 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002253 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002254 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002255 case BTRFS_IOC_BALANCE:
2256 return btrfs_balance(root->fs_info->dev_root);
2257 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002258 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2259 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002260 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002261 case BTRFS_IOC_TRANS_START:
2262 return btrfs_ioctl_trans_start(file);
2263 case BTRFS_IOC_TRANS_END:
2264 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002265 case BTRFS_IOC_TREE_SEARCH:
2266 return btrfs_ioctl_tree_search(file, argp);
2267 case BTRFS_IOC_INO_LOOKUP:
2268 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002269 case BTRFS_IOC_SPACE_INFO:
2270 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002271 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002272 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2273 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002274 case BTRFS_IOC_START_SYNC:
2275 return btrfs_ioctl_start_sync(file, argp);
2276 case BTRFS_IOC_WAIT_SYNC:
2277 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002278 }
2279
2280 return -ENOTTY;
2281}