blob: b6985d33eedee09c870b6e178b34494802802d5d [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;
Josef Bacik6a912212010-11-20 09:48:00 +0000236 struct dentry *parent = dget_parent(dentry);
237 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400238 int ret;
239 int err;
240 u64 objectid;
241 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500242 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400243
Yan, Zhenga22285a2010-05-16 10:48:46 -0400244 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
245 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000246 if (ret) {
247 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400248 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000249 }
250
251 dir = parent->d_inode;
252
Josef Bacik9ed74f22009-09-11 16:12:44 -0400253 /*
254 * 1 - inode item
255 * 2 - refs
256 * 1 - root item
257 * 2 - dir items
258 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400259 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000260 if (IS_ERR(trans)) {
261 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400262 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000263 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400264
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
266 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400267 if (IS_ERR(leaf)) {
268 ret = PTR_ERR(leaf);
269 goto fail;
270 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400271
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400272 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400273 btrfs_set_header_bytenr(leaf, leaf->start);
274 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400276 btrfs_set_header_owner(leaf, objectid);
277
278 write_extent_buffer(leaf, root->fs_info->fsid,
279 (unsigned long)btrfs_header_fsid(leaf),
280 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400281 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
282 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
283 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400284 btrfs_mark_buffer_dirty(leaf);
285
286 inode_item = &root_item.inode;
287 memset(inode_item, 0, sizeof(*inode_item));
288 inode_item->generation = cpu_to_le64(1);
289 inode_item->size = cpu_to_le64(3);
290 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400291 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400292 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
293
294 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400295 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400296 btrfs_set_root_level(&root_item, 0);
297 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000298 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400299 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400300
301 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
302 root_item.drop_level = 0;
303
Chris Mason925baed2008-06-25 16:01:30 -0400304 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400305 free_extent_buffer(leaf);
306 leaf = NULL;
307
308 btrfs_set_root_dirid(&root_item, new_dirid);
309
310 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400311 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400312 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
313 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
314 &root_item);
315 if (ret)
316 goto fail;
317
Yan, Zheng76dda932009-09-21 16:00:26 -0400318 key.offset = (u64)-1;
319 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
320 BUG_ON(IS_ERR(new_root));
321
322 btrfs_record_root_in_trans(trans, new_root);
323
324 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
325 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400326 /*
327 * insert the directory item
328 */
Chris Mason3de45862008-11-17 21:02:50 -0500329 ret = btrfs_set_inode_index(dir, &index);
330 BUG_ON(ret);
331
332 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400333 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500334 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400335 if (ret)
336 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500337
Yan Zheng52c26172009-01-05 15:43:43 -0500338 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
339 ret = btrfs_update_inode(trans, root, dir);
340 BUG_ON(ret);
341
Chris Mason0660b5a2008-11-17 20:37:39 -0500342 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400343 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500344 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400345
Chris Mason0660b5a2008-11-17 20:37:39 -0500346 BUG_ON(ret);
347
Yan, Zheng76dda932009-09-21 16:00:26 -0400348 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400349fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000350 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400351 if (async_transid) {
352 *async_transid = trans->transid;
353 err = btrfs_commit_transaction_async(trans, root, 1);
354 } else {
355 err = btrfs_commit_transaction(trans, root);
356 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400357 if (err && !ret)
358 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400359 return ret;
360}
361
Sage Weil72fd0322010-10-29 15:41:32 -0400362static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
363 char *name, int namelen, u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400364{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000365 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000366 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400367 struct btrfs_pending_snapshot *pending_snapshot;
368 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000369 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400370
371 if (!root->ref_cows)
372 return -EINVAL;
373
Yan, Zhenga22285a2010-05-16 10:48:46 -0400374 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
375 if (!pending_snapshot)
376 return -ENOMEM;
377
378 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
379 pending_snapshot->dentry = dentry;
380 pending_snapshot->root = root;
381
382 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
383 if (IS_ERR(trans)) {
384 ret = PTR_ERR(trans);
385 goto fail;
386 }
387
388 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
389 BUG_ON(ret);
390
391 list_add(&pending_snapshot->list,
392 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400393 if (async_transid) {
394 *async_transid = trans->transid;
395 ret = btrfs_commit_transaction_async(trans,
396 root->fs_info->extent_root, 1);
397 } else {
398 ret = btrfs_commit_transaction(trans,
399 root->fs_info->extent_root);
400 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400401 BUG_ON(ret);
402
403 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000405 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400406
Yan, Zhenga22285a2010-05-16 10:48:46 -0400407 btrfs_orphan_cleanup(pending_snapshot->snap);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400408
Josef Bacik6a912212010-11-20 09:48:00 +0000409 parent = dget_parent(dentry);
410 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
411 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000412 if (IS_ERR(inode)) {
413 ret = PTR_ERR(inode);
414 goto fail;
415 }
416 BUG_ON(!inode);
417 d_instantiate(dentry, inode);
418 ret = 0;
419fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400420 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 return ret;
422}
423
Sage Weil4260f7c2010-10-29 15:46:43 -0400424/* copy of check_sticky in fs/namei.c()
425* It's inline, so penalty for filesystems that don't use sticky bit is
426* minimal.
427*/
428static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
429{
430 uid_t fsuid = current_fsuid();
431
432 if (!(dir->i_mode & S_ISVTX))
433 return 0;
434 if (inode->i_uid == fsuid)
435 return 0;
436 if (dir->i_uid == fsuid)
437 return 0;
438 return !capable(CAP_FOWNER);
439}
440
441/* copy of may_delete in fs/namei.c()
442 * Check whether we can remove a link victim from directory dir, check
443 * whether the type of victim is right.
444 * 1. We can't do it if dir is read-only (done in permission())
445 * 2. We should have write and exec permissions on dir
446 * 3. We can't remove anything from append-only dir
447 * 4. We can't do anything with immutable dir (done in permission())
448 * 5. If the sticky bit on dir is set we should either
449 * a. be owner of dir, or
450 * b. be owner of victim, or
451 * c. have CAP_FOWNER capability
452 * 6. If the victim is append-only or immutable we can't do antyhing with
453 * links pointing to it.
454 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
455 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
456 * 9. We can't remove a root or mountpoint.
457 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
458 * nfs_async_unlink().
459 */
460
461static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
462{
463 int error;
464
465 if (!victim->d_inode)
466 return -ENOENT;
467
468 BUG_ON(victim->d_parent->d_inode != dir);
469 audit_inode_child(victim, dir);
470
471 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
472 if (error)
473 return error;
474 if (IS_APPEND(dir))
475 return -EPERM;
476 if (btrfs_check_sticky(dir, victim->d_inode)||
477 IS_APPEND(victim->d_inode)||
478 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
479 return -EPERM;
480 if (isdir) {
481 if (!S_ISDIR(victim->d_inode->i_mode))
482 return -ENOTDIR;
483 if (IS_ROOT(victim))
484 return -EBUSY;
485 } else if (S_ISDIR(victim->d_inode->i_mode))
486 return -EISDIR;
487 if (IS_DEADDIR(dir))
488 return -ENOENT;
489 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
490 return -EBUSY;
491 return 0;
492}
493
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400494/* copy of may_create in fs/namei.c() */
495static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
496{
497 if (child->d_inode)
498 return -EEXIST;
499 if (IS_DEADDIR(dir))
500 return -ENOENT;
501 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
502}
503
504/*
505 * Create a new subvolume below @parent. This is largely modeled after
506 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
507 * inside this filesystem so it's quite a bit simpler.
508 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400509static noinline int btrfs_mksubvol(struct path *parent,
510 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400511 struct btrfs_root *snap_src,
512 u64 *async_transid)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400513{
Yan, Zheng76dda932009-09-21 16:00:26 -0400514 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400515 struct dentry *dentry;
516 int error;
517
Yan, Zheng76dda932009-09-21 16:00:26 -0400518 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400519
520 dentry = lookup_one_len(name, parent->dentry, namelen);
521 error = PTR_ERR(dentry);
522 if (IS_ERR(dentry))
523 goto out_unlock;
524
525 error = -EEXIST;
526 if (dentry->d_inode)
527 goto out_dput;
528
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400529 error = mnt_want_write(parent->mnt);
530 if (error)
531 goto out_dput;
532
Yan, Zheng76dda932009-09-21 16:00:26 -0400533 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400534 if (error)
535 goto out_drop_write;
536
Yan, Zheng76dda932009-09-21 16:00:26 -0400537 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
538
539 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
540 goto out_up_read;
541
Chris Mason3de45862008-11-17 21:02:50 -0500542 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400543 error = create_snapshot(snap_src, dentry,
544 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500545 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400546 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400547 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500548 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400549 if (!error)
550 fsnotify_mkdir(dir, dentry);
551out_up_read:
552 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400553out_drop_write:
554 mnt_drop_write(parent->mnt);
555out_dput:
556 dput(dentry);
557out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400558 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400559 return error;
560}
561
Chris Mason940100a2010-03-10 10:52:59 -0500562static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500563 int thresh, u64 *last_len, u64 *skip,
564 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500565{
566 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
567 struct extent_map *em = NULL;
568 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
569 int ret = 1;
570
Chris Mason1e701a32010-03-11 09:42:04 -0500571
572 if (thresh == 0)
573 thresh = 256 * 1024;
574
Chris Mason940100a2010-03-10 10:52:59 -0500575 /*
576 * make sure that once we start defragging and extent, we keep on
577 * defragging it
578 */
579 if (start < *defrag_end)
580 return 1;
581
582 *skip = 0;
583
584 /*
585 * hopefully we have this extent in the tree already, try without
586 * the full extent lock
587 */
588 read_lock(&em_tree->lock);
589 em = lookup_extent_mapping(em_tree, start, len);
590 read_unlock(&em_tree->lock);
591
592 if (!em) {
593 /* get the big lock and read metadata off disk */
594 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
595 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
596 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
597
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000598 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500599 return 0;
600 }
601
602 /* this will cover holes, and inline extents */
603 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
604 ret = 0;
605
606 /*
607 * we hit a real extent, if it is big don't bother defragging it again
608 */
Chris Mason1e701a32010-03-11 09:42:04 -0500609 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500610 ret = 0;
611
612 /*
613 * last_len ends up being a counter of how many bytes we've defragged.
614 * every time we choose not to defrag an extent, we reset *last_len
615 * so that the next tiny extent will force a defrag.
616 *
617 * The end result of this is that tiny extents before a single big
618 * extent will force at least part of that big extent to be defragged.
619 */
620 if (ret) {
621 *last_len += len;
622 *defrag_end = extent_map_end(em);
623 } else {
624 *last_len = 0;
625 *skip = extent_map_end(em);
626 *defrag_end = 0;
627 }
628
629 free_extent_map(em);
630 return ret;
631}
632
Chris Mason1e701a32010-03-11 09:42:04 -0500633static int btrfs_defrag_file(struct file *file,
634 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400635{
636 struct inode *inode = fdentry(file)->d_inode;
637 struct btrfs_root *root = BTRFS_I(inode)->root;
638 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400639 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400640 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800641 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400642 unsigned long last_index;
643 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
644 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800645 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646 u64 page_start;
647 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500648 u64 last_len = 0;
649 u64 skip = 0;
650 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651 unsigned long i;
652 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800653 int compress_type = BTRFS_COMPRESS_ZLIB;
654
655 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
656 if (range->compress_type > BTRFS_COMPRESS_TYPES)
657 return -EINVAL;
658 if (range->compress_type)
659 compress_type = range->compress_type;
660 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400661
Chris Mason940100a2010-03-10 10:52:59 -0500662 if (inode->i_size == 0)
663 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664
Chris Mason1e701a32010-03-11 09:42:04 -0500665 if (range->start + range->len > range->start) {
666 last_index = min_t(u64, inode->i_size - 1,
667 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
668 } else {
669 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
670 }
671
672 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500673 while (i <= last_index) {
674 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500675 PAGE_CACHE_SIZE,
676 range->extent_thresh,
677 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500678 &defrag_end)) {
679 unsigned long next;
680 /*
681 * the should_defrag function tells us how much to skip
682 * bump our counter by the suggested amount
683 */
684 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
685 i = max(i + 1, next);
686 continue;
687 }
688
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400689 if (total_read % ra_pages == 0) {
690 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
691 min(last_index, i + ra_pages - 1));
692 }
693 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500694 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500695 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800696 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500697
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400698 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
699 if (ret)
700 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400701again:
Chris Mason940100a2010-03-10 10:52:59 -0500702 if (inode->i_size == 0 ||
703 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
704 ret = 0;
705 goto err_reservations;
706 }
707
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400708 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400709 if (!page) {
710 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500711 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400712 }
Chris Mason940100a2010-03-10 10:52:59 -0500713
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400714 if (!PageUptodate(page)) {
715 btrfs_readpage(NULL, page);
716 lock_page(page);
717 if (!PageUptodate(page)) {
718 unlock_page(page);
719 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400720 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500721 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400722 }
723 }
724
Chris Mason940100a2010-03-10 10:52:59 -0500725 if (page->mapping != inode->i_mapping) {
726 unlock_page(page);
727 page_cache_release(page);
728 goto again;
729 }
730
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732
Chris Mason940100a2010-03-10 10:52:59 -0500733 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400734 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500735 goto loop_unlock;
736 }
737
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
739 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400740 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400741
742 ordered = btrfs_lookup_ordered_extent(inode, page_start);
743 if (ordered) {
744 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
745 unlock_page(page);
746 page_cache_release(page);
747 btrfs_start_ordered_extent(inode, ordered, 1);
748 btrfs_put_ordered_extent(ordered);
749 goto again;
750 }
751 set_page_extent_mapped(page);
752
Chris Masonf87f0572008-08-01 11:27:23 -0400753 /*
754 * this makes sure page_mkwrite is called on the
755 * page if it is dirtied again later
756 */
757 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500758 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
759 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
760 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400761
Josef Bacik2ac55d42010-02-03 19:33:23 +0000762 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500763 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400764 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400765 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500766
767loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400768 unlock_page(page);
769 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500770 mutex_unlock(&inode->i_mutex);
771
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400772 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500773 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400774 }
775
Chris Mason1e701a32010-03-11 09:42:04 -0500776 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
777 filemap_flush(inode->i_mapping);
778
779 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
780 /* the filemap_flush will queue IO into the worker threads, but
781 * we have to make sure the IO is actually started and that
782 * ordered extents get created before we return
783 */
784 atomic_inc(&root->fs_info->async_submit_draining);
785 while (atomic_read(&root->fs_info->nr_async_submits) ||
786 atomic_read(&root->fs_info->async_delalloc_pages)) {
787 wait_event(root->fs_info->async_submit_wait,
788 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
789 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
790 }
791 atomic_dec(&root->fs_info->async_submit_draining);
792
793 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800794 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500795 mutex_unlock(&inode->i_mutex);
796 }
797
Li Zefan1a419d82010-10-25 15:12:50 +0800798 disk_super = &root->fs_info->super_copy;
799 features = btrfs_super_incompat_flags(disk_super);
800 if (range->compress_type == BTRFS_COMPRESS_LZO) {
801 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
802 btrfs_set_super_incompat_flags(disk_super, features);
803 }
804
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400805 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500806
807err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400808 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
809err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500810 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500811 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400812}
813
Yan, Zheng76dda932009-09-21 16:00:26 -0400814static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
815 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400816{
817 u64 new_size;
818 u64 old_size;
819 u64 devid = 1;
820 struct btrfs_ioctl_vol_args *vol_args;
821 struct btrfs_trans_handle *trans;
822 struct btrfs_device *device = NULL;
823 char *sizestr;
824 char *devstr = NULL;
825 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400826 int mod = 0;
827
Yan Zhengc146afa2008-11-12 14:34:12 -0500828 if (root->fs_info->sb->s_flags & MS_RDONLY)
829 return -EROFS;
830
Chris Masone441d542009-01-05 16:57:23 -0500831 if (!capable(CAP_SYS_ADMIN))
832 return -EPERM;
833
Li Zefandae7b662009-04-08 15:06:54 +0800834 vol_args = memdup_user(arg, sizeof(*vol_args));
835 if (IS_ERR(vol_args))
836 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400837
838 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400839
Chris Mason7d9eb122008-07-08 14:19:17 -0400840 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400841 sizestr = vol_args->name;
842 devstr = strchr(sizestr, ':');
843 if (devstr) {
844 char *end;
845 sizestr = devstr + 1;
846 *devstr = '\0';
847 devstr = vol_args->name;
848 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700849 printk(KERN_INFO "resizing devid %llu\n",
850 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400851 }
Yan Zheng2b820322008-11-17 21:11:30 -0500852 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400853 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700854 printk(KERN_INFO "resizer unable to find device %llu\n",
855 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400856 ret = -EINVAL;
857 goto out_unlock;
858 }
859 if (!strcmp(sizestr, "max"))
860 new_size = device->bdev->bd_inode->i_size;
861 else {
862 if (sizestr[0] == '-') {
863 mod = -1;
864 sizestr++;
865 } else if (sizestr[0] == '+') {
866 mod = 1;
867 sizestr++;
868 }
Akinobu Mita91748462010-02-28 10:59:11 +0000869 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400870 if (new_size == 0) {
871 ret = -EINVAL;
872 goto out_unlock;
873 }
874 }
875
876 old_size = device->total_bytes;
877
878 if (mod < 0) {
879 if (new_size > old_size) {
880 ret = -EINVAL;
881 goto out_unlock;
882 }
883 new_size = old_size - new_size;
884 } else if (mod > 0) {
885 new_size = old_size + new_size;
886 }
887
888 if (new_size < 256 * 1024 * 1024) {
889 ret = -EINVAL;
890 goto out_unlock;
891 }
892 if (new_size > device->bdev->bd_inode->i_size) {
893 ret = -EFBIG;
894 goto out_unlock;
895 }
896
897 do_div(new_size, root->sectorsize);
898 new_size *= root->sectorsize;
899
900 printk(KERN_INFO "new size for %s is %llu\n",
901 device->name, (unsigned long long)new_size);
902
903 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400904 trans = btrfs_start_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400905 ret = btrfs_grow_device(trans, device, new_size);
906 btrfs_commit_transaction(trans, root);
907 } else {
908 ret = btrfs_shrink_device(device, new_size);
909 }
910
911out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400912 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400913 kfree(vol_args);
914 return ret;
915}
916
Sage Weil72fd0322010-10-29 15:41:32 -0400917static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
918 char *name,
919 unsigned long fd,
920 int subvol,
921 u64 *transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400922{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400923 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -0500924 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400925 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500926 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400927
Yan Zhengc146afa2008-11-12 14:34:12 -0500928 if (root->fs_info->sb->s_flags & MS_RDONLY)
929 return -EROFS;
930
Sage Weil72fd0322010-10-29 15:41:32 -0400931 namelen = strlen(name);
932 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400933 ret = -EINVAL;
934 goto out;
935 }
936
Chris Mason3de45862008-11-17 21:02:50 -0500937 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -0400938 ret = btrfs_mksubvol(&file->f_path, name, namelen,
939 NULL, transid);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400940 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500941 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -0400942 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -0500943 if (!src_file) {
944 ret = -EINVAL;
945 goto out;
946 }
947
948 src_inode = src_file->f_path.dentry->d_inode;
949 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500950 printk(KERN_INFO "btrfs: Snapshot src from "
951 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500952 ret = -EINVAL;
953 fput(src_file);
954 goto out;
955 }
Sage Weil72fd0322010-10-29 15:41:32 -0400956 ret = btrfs_mksubvol(&file->f_path, name, namelen,
957 BTRFS_I(src_inode)->root,
958 transid);
Chris Mason3de45862008-11-17 21:02:50 -0500959 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400960 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400961out:
Sage Weil72fd0322010-10-29 15:41:32 -0400962 return ret;
963}
964
965static noinline int btrfs_ioctl_snap_create(struct file *file,
966 void __user *arg, int subvol,
Li Zefanfdfb1e42010-12-10 06:41:56 +0000967 int v2)
Sage Weil72fd0322010-10-29 15:41:32 -0400968{
969 struct btrfs_ioctl_vol_args *vol_args = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +0000970 struct btrfs_ioctl_vol_args_v2 *vol_args_v2 = NULL;
Sage Weil72fd0322010-10-29 15:41:32 -0400971 char *name;
972 u64 fd;
Sage Weil72fd0322010-10-29 15:41:32 -0400973 int ret;
974
Li Zefanfdfb1e42010-12-10 06:41:56 +0000975 if (v2) {
976 u64 transid = 0;
977 u64 *ptr = NULL;
Sage Weil72fd0322010-10-29 15:41:32 -0400978
Li Zefanfdfb1e42010-12-10 06:41:56 +0000979 vol_args_v2 = memdup_user(arg, sizeof(*vol_args_v2));
980 if (IS_ERR(vol_args_v2))
981 return PTR_ERR(vol_args_v2);
982
983 if (vol_args_v2->flags & ~BTRFS_SUBVOL_CREATE_ASYNC) {
984 ret = -EINVAL;
985 goto out;
986 }
987
988 name = vol_args_v2->name;
989 fd = vol_args_v2->fd;
990 vol_args_v2->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
991
992 if (vol_args_v2->flags & BTRFS_SUBVOL_CREATE_ASYNC)
993 ptr = &transid;
Sage Weil75eaa0e2010-12-10 00:36:28 +0000994
995 ret = btrfs_ioctl_snap_create_transid(file, name, fd,
Li Zefanfdfb1e42010-12-10 06:41:56 +0000996 subvol, ptr);
Sage Weil75eaa0e2010-12-10 00:36:28 +0000997
Li Zefanfdfb1e42010-12-10 06:41:56 +0000998 if (ret == 0 && ptr &&
Sage Weil75eaa0e2010-12-10 00:36:28 +0000999 copy_to_user(arg +
Li Zefanfdfb1e42010-12-10 06:41:56 +00001000 offsetof(struct btrfs_ioctl_vol_args_v2,
1001 transid), ptr, sizeof(*ptr)))
Sage Weil75eaa0e2010-12-10 00:36:28 +00001002 ret = -EFAULT;
Sage Weil72fd0322010-10-29 15:41:32 -04001003 } else {
1004 vol_args = memdup_user(arg, sizeof(*vol_args));
1005 if (IS_ERR(vol_args))
1006 return PTR_ERR(vol_args);
1007 name = vol_args->name;
1008 fd = vol_args->fd;
1009 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001010
Sage Weil75eaa0e2010-12-10 00:36:28 +00001011 ret = btrfs_ioctl_snap_create_transid(file, name, fd,
1012 subvol, NULL);
Sage Weil72fd0322010-10-29 15:41:32 -04001013 }
Li Zefanfdfb1e42010-12-10 06:41:56 +00001014out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001015 kfree(vol_args);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001016 kfree(vol_args_v2);
Sage Weil72fd0322010-10-29 15:41:32 -04001017
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001018 return ret;
1019}
1020
Yan, Zheng76dda932009-09-21 16:00:26 -04001021/*
1022 * helper to check if the subvolume references other subvolumes
1023 */
1024static noinline int may_destroy_subvol(struct btrfs_root *root)
1025{
1026 struct btrfs_path *path;
1027 struct btrfs_key key;
1028 int ret;
1029
1030 path = btrfs_alloc_path();
1031 if (!path)
1032 return -ENOMEM;
1033
1034 key.objectid = root->root_key.objectid;
1035 key.type = BTRFS_ROOT_REF_KEY;
1036 key.offset = (u64)-1;
1037
1038 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1039 &key, path, 0, 0);
1040 if (ret < 0)
1041 goto out;
1042 BUG_ON(ret == 0);
1043
1044 ret = 0;
1045 if (path->slots[0] > 0) {
1046 path->slots[0]--;
1047 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1048 if (key.objectid == root->root_key.objectid &&
1049 key.type == BTRFS_ROOT_REF_KEY)
1050 ret = -ENOTEMPTY;
1051 }
1052out:
1053 btrfs_free_path(path);
1054 return ret;
1055}
1056
Chris Masonac8e9812010-02-28 15:39:26 -05001057static noinline int key_in_sk(struct btrfs_key *key,
1058 struct btrfs_ioctl_search_key *sk)
1059{
Chris Masonabc6e132010-03-18 12:10:08 -04001060 struct btrfs_key test;
1061 int ret;
1062
1063 test.objectid = sk->min_objectid;
1064 test.type = sk->min_type;
1065 test.offset = sk->min_offset;
1066
1067 ret = btrfs_comp_cpu_keys(key, &test);
1068 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001069 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001070
1071 test.objectid = sk->max_objectid;
1072 test.type = sk->max_type;
1073 test.offset = sk->max_offset;
1074
1075 ret = btrfs_comp_cpu_keys(key, &test);
1076 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001077 return 0;
1078 return 1;
1079}
1080
1081static noinline int copy_to_sk(struct btrfs_root *root,
1082 struct btrfs_path *path,
1083 struct btrfs_key *key,
1084 struct btrfs_ioctl_search_key *sk,
1085 char *buf,
1086 unsigned long *sk_offset,
1087 int *num_found)
1088{
1089 u64 found_transid;
1090 struct extent_buffer *leaf;
1091 struct btrfs_ioctl_search_header sh;
1092 unsigned long item_off;
1093 unsigned long item_len;
1094 int nritems;
1095 int i;
1096 int slot;
1097 int found = 0;
1098 int ret = 0;
1099
1100 leaf = path->nodes[0];
1101 slot = path->slots[0];
1102 nritems = btrfs_header_nritems(leaf);
1103
1104 if (btrfs_header_generation(leaf) > sk->max_transid) {
1105 i = nritems;
1106 goto advance_key;
1107 }
1108 found_transid = btrfs_header_generation(leaf);
1109
1110 for (i = slot; i < nritems; i++) {
1111 item_off = btrfs_item_ptr_offset(leaf, i);
1112 item_len = btrfs_item_size_nr(leaf, i);
1113
1114 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1115 item_len = 0;
1116
1117 if (sizeof(sh) + item_len + *sk_offset >
1118 BTRFS_SEARCH_ARGS_BUFSIZE) {
1119 ret = 1;
1120 goto overflow;
1121 }
1122
1123 btrfs_item_key_to_cpu(leaf, key, i);
1124 if (!key_in_sk(key, sk))
1125 continue;
1126
1127 sh.objectid = key->objectid;
1128 sh.offset = key->offset;
1129 sh.type = key->type;
1130 sh.len = item_len;
1131 sh.transid = found_transid;
1132
1133 /* copy search result header */
1134 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1135 *sk_offset += sizeof(sh);
1136
1137 if (item_len) {
1138 char *p = buf + *sk_offset;
1139 /* copy the item */
1140 read_extent_buffer(leaf, p,
1141 item_off, item_len);
1142 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001143 }
Chris Mason90fdde12010-03-18 12:14:54 -04001144 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001145
1146 if (*num_found >= sk->nr_items)
1147 break;
1148 }
1149advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001150 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001151 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1152 key->offset++;
1153 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1154 key->offset = 0;
1155 key->type++;
1156 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1157 key->offset = 0;
1158 key->type = 0;
1159 key->objectid++;
1160 } else
1161 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001162overflow:
1163 *num_found += found;
1164 return ret;
1165}
1166
1167static noinline int search_ioctl(struct inode *inode,
1168 struct btrfs_ioctl_search_args *args)
1169{
1170 struct btrfs_root *root;
1171 struct btrfs_key key;
1172 struct btrfs_key max_key;
1173 struct btrfs_path *path;
1174 struct btrfs_ioctl_search_key *sk = &args->key;
1175 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1176 int ret;
1177 int num_found = 0;
1178 unsigned long sk_offset = 0;
1179
1180 path = btrfs_alloc_path();
1181 if (!path)
1182 return -ENOMEM;
1183
1184 if (sk->tree_id == 0) {
1185 /* search the root of the inode that was passed */
1186 root = BTRFS_I(inode)->root;
1187 } else {
1188 key.objectid = sk->tree_id;
1189 key.type = BTRFS_ROOT_ITEM_KEY;
1190 key.offset = (u64)-1;
1191 root = btrfs_read_fs_root_no_name(info, &key);
1192 if (IS_ERR(root)) {
1193 printk(KERN_ERR "could not find root %llu\n",
1194 sk->tree_id);
1195 btrfs_free_path(path);
1196 return -ENOENT;
1197 }
1198 }
1199
1200 key.objectid = sk->min_objectid;
1201 key.type = sk->min_type;
1202 key.offset = sk->min_offset;
1203
1204 max_key.objectid = sk->max_objectid;
1205 max_key.type = sk->max_type;
1206 max_key.offset = sk->max_offset;
1207
1208 path->keep_locks = 1;
1209
1210 while(1) {
1211 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1212 sk->min_transid);
1213 if (ret != 0) {
1214 if (ret > 0)
1215 ret = 0;
1216 goto err;
1217 }
1218 ret = copy_to_sk(root, path, &key, sk, args->buf,
1219 &sk_offset, &num_found);
1220 btrfs_release_path(root, path);
1221 if (ret || num_found >= sk->nr_items)
1222 break;
1223
1224 }
1225 ret = 0;
1226err:
1227 sk->nr_items = num_found;
1228 btrfs_free_path(path);
1229 return ret;
1230}
1231
1232static noinline int btrfs_ioctl_tree_search(struct file *file,
1233 void __user *argp)
1234{
1235 struct btrfs_ioctl_search_args *args;
1236 struct inode *inode;
1237 int ret;
1238
1239 if (!capable(CAP_SYS_ADMIN))
1240 return -EPERM;
1241
Julia Lawall2354d082010-10-29 15:14:18 -04001242 args = memdup_user(argp, sizeof(*args));
1243 if (IS_ERR(args))
1244 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001245
Chris Masonac8e9812010-02-28 15:39:26 -05001246 inode = fdentry(file)->d_inode;
1247 ret = search_ioctl(inode, args);
1248 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1249 ret = -EFAULT;
1250 kfree(args);
1251 return ret;
1252}
1253
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001254/*
Chris Masonac8e9812010-02-28 15:39:26 -05001255 * Search INODE_REFs to identify path name of 'dirid' directory
1256 * in a 'tree_id' tree. and sets path name to 'name'.
1257 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001258static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1259 u64 tree_id, u64 dirid, char *name)
1260{
1261 struct btrfs_root *root;
1262 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001263 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001264 int ret = -1;
1265 int slot;
1266 int len;
1267 int total_len = 0;
1268 struct btrfs_inode_ref *iref;
1269 struct extent_buffer *l;
1270 struct btrfs_path *path;
1271
1272 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1273 name[0]='\0';
1274 return 0;
1275 }
1276
1277 path = btrfs_alloc_path();
1278 if (!path)
1279 return -ENOMEM;
1280
Chris Masonac8e9812010-02-28 15:39:26 -05001281 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001282
1283 key.objectid = tree_id;
1284 key.type = BTRFS_ROOT_ITEM_KEY;
1285 key.offset = (u64)-1;
1286 root = btrfs_read_fs_root_no_name(info, &key);
1287 if (IS_ERR(root)) {
1288 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001289 ret = -ENOENT;
1290 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001291 }
1292
1293 key.objectid = dirid;
1294 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001295 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001296
1297 while(1) {
1298 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1299 if (ret < 0)
1300 goto out;
1301
1302 l = path->nodes[0];
1303 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001304 if (ret > 0 && slot > 0)
1305 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001306 btrfs_item_key_to_cpu(l, &key, slot);
1307
1308 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001309 key.type != BTRFS_INODE_REF_KEY)) {
1310 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001311 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001312 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001313
1314 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1315 len = btrfs_inode_ref_name_len(l, iref);
1316 ptr -= len + 1;
1317 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001318 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001319 goto out;
1320
1321 *(ptr + len) = '/';
1322 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1323
1324 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1325 break;
1326
1327 btrfs_release_path(root, path);
1328 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001329 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001330 dirid = key.objectid;
1331
1332 }
Chris Masonac8e9812010-02-28 15:39:26 -05001333 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001334 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001335 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001336 name[total_len]='\0';
1337 ret = 0;
1338out:
1339 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001340 return ret;
1341}
1342
1343static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1344 void __user *argp)
1345{
1346 struct btrfs_ioctl_ino_lookup_args *args;
1347 struct inode *inode;
1348 int ret;
1349
1350 if (!capable(CAP_SYS_ADMIN))
1351 return -EPERM;
1352
Julia Lawall2354d082010-10-29 15:14:18 -04001353 args = memdup_user(argp, sizeof(*args));
1354 if (IS_ERR(args))
1355 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001356
Chris Masonac8e9812010-02-28 15:39:26 -05001357 inode = fdentry(file)->d_inode;
1358
Chris Mason1b53ac42010-03-18 12:17:05 -04001359 if (args->treeid == 0)
1360 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1361
Chris Masonac8e9812010-02-28 15:39:26 -05001362 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1363 args->treeid, args->objectid,
1364 args->name);
1365
1366 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1367 ret = -EFAULT;
1368
1369 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001370 return ret;
1371}
1372
Yan, Zheng76dda932009-09-21 16:00:26 -04001373static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1374 void __user *arg)
1375{
1376 struct dentry *parent = fdentry(file);
1377 struct dentry *dentry;
1378 struct inode *dir = parent->d_inode;
1379 struct inode *inode;
1380 struct btrfs_root *root = BTRFS_I(dir)->root;
1381 struct btrfs_root *dest = NULL;
1382 struct btrfs_ioctl_vol_args *vol_args;
1383 struct btrfs_trans_handle *trans;
1384 int namelen;
1385 int ret;
1386 int err = 0;
1387
Yan, Zheng76dda932009-09-21 16:00:26 -04001388 vol_args = memdup_user(arg, sizeof(*vol_args));
1389 if (IS_ERR(vol_args))
1390 return PTR_ERR(vol_args);
1391
1392 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1393 namelen = strlen(vol_args->name);
1394 if (strchr(vol_args->name, '/') ||
1395 strncmp(vol_args->name, "..", namelen) == 0) {
1396 err = -EINVAL;
1397 goto out;
1398 }
1399
1400 err = mnt_want_write(file->f_path.mnt);
1401 if (err)
1402 goto out;
1403
1404 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1405 dentry = lookup_one_len(vol_args->name, parent, namelen);
1406 if (IS_ERR(dentry)) {
1407 err = PTR_ERR(dentry);
1408 goto out_unlock_dir;
1409 }
1410
1411 if (!dentry->d_inode) {
1412 err = -ENOENT;
1413 goto out_dput;
1414 }
1415
1416 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001417 dest = BTRFS_I(inode)->root;
1418 if (!capable(CAP_SYS_ADMIN)){
1419 /*
1420 * Regular user. Only allow this with a special mount
1421 * option, when the user has write+exec access to the
1422 * subvol root, and when rmdir(2) would have been
1423 * allowed.
1424 *
1425 * Note that this is _not_ check that the subvol is
1426 * empty or doesn't contain data that we wouldn't
1427 * otherwise be able to delete.
1428 *
1429 * Users who want to delete empty subvols should try
1430 * rmdir(2).
1431 */
1432 err = -EPERM;
1433 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1434 goto out_dput;
1435
1436 /*
1437 * Do not allow deletion if the parent dir is the same
1438 * as the dir to be deleted. That means the ioctl
1439 * must be called on the dentry referencing the root
1440 * of the subvol, not a random directory contained
1441 * within it.
1442 */
1443 err = -EINVAL;
1444 if (root == dest)
1445 goto out_dput;
1446
1447 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1448 if (err)
1449 goto out_dput;
1450
1451 /* check if subvolume may be deleted by a non-root user */
1452 err = btrfs_may_delete(dir, dentry, 1);
1453 if (err)
1454 goto out_dput;
1455 }
1456
Yan, Zheng76dda932009-09-21 16:00:26 -04001457 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1458 err = -EINVAL;
1459 goto out_dput;
1460 }
1461
Yan, Zheng76dda932009-09-21 16:00:26 -04001462 mutex_lock(&inode->i_mutex);
1463 err = d_invalidate(dentry);
1464 if (err)
1465 goto out_unlock;
1466
1467 down_write(&root->fs_info->subvol_sem);
1468
1469 err = may_destroy_subvol(dest);
1470 if (err)
1471 goto out_up_write;
1472
Yan, Zhenga22285a2010-05-16 10:48:46 -04001473 trans = btrfs_start_transaction(root, 0);
1474 if (IS_ERR(trans)) {
1475 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001476 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001477 }
1478 trans->block_rsv = &root->fs_info->global_block_rsv;
1479
Yan, Zheng76dda932009-09-21 16:00:26 -04001480 ret = btrfs_unlink_subvol(trans, root, dir,
1481 dest->root_key.objectid,
1482 dentry->d_name.name,
1483 dentry->d_name.len);
1484 BUG_ON(ret);
1485
1486 btrfs_record_root_in_trans(trans, dest);
1487
1488 memset(&dest->root_item.drop_progress, 0,
1489 sizeof(dest->root_item.drop_progress));
1490 dest->root_item.drop_level = 0;
1491 btrfs_set_root_refs(&dest->root_item, 0);
1492
Yan, Zhengd68fc572010-05-16 10:49:58 -04001493 if (!xchg(&dest->orphan_item_inserted, 1)) {
1494 ret = btrfs_insert_orphan_item(trans,
1495 root->fs_info->tree_root,
1496 dest->root_key.objectid);
1497 BUG_ON(ret);
1498 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001499
Sage Weil531cb132010-10-29 15:41:32 -04001500 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001501 BUG_ON(ret);
1502 inode->i_flags |= S_DEAD;
1503out_up_write:
1504 up_write(&root->fs_info->subvol_sem);
1505out_unlock:
1506 mutex_unlock(&inode->i_mutex);
1507 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001508 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001509 btrfs_invalidate_inodes(dest);
1510 d_delete(dentry);
1511 }
1512out_dput:
1513 dput(dentry);
1514out_unlock_dir:
1515 mutex_unlock(&dir->i_mutex);
1516 mnt_drop_write(file->f_path.mnt);
1517out:
1518 kfree(vol_args);
1519 return err;
1520}
1521
Chris Mason1e701a32010-03-11 09:42:04 -05001522static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001523{
1524 struct inode *inode = fdentry(file)->d_inode;
1525 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001526 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001527 int ret;
1528
1529 ret = mnt_want_write(file->f_path.mnt);
1530 if (ret)
1531 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001532
1533 switch (inode->i_mode & S_IFMT) {
1534 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001535 if (!capable(CAP_SYS_ADMIN)) {
1536 ret = -EPERM;
1537 goto out;
1538 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001539 ret = btrfs_defrag_root(root, 0);
1540 if (ret)
1541 goto out;
1542 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001543 break;
1544 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001545 if (!(file->f_mode & FMODE_WRITE)) {
1546 ret = -EINVAL;
1547 goto out;
1548 }
Chris Mason1e701a32010-03-11 09:42:04 -05001549
1550 range = kzalloc(sizeof(*range), GFP_KERNEL);
1551 if (!range) {
1552 ret = -ENOMEM;
1553 goto out;
1554 }
1555
1556 if (argp) {
1557 if (copy_from_user(range, argp,
1558 sizeof(*range))) {
1559 ret = -EFAULT;
1560 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001561 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001562 }
1563 /* compression requires us to start the IO */
1564 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1565 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1566 range->extent_thresh = (u32)-1;
1567 }
1568 } else {
1569 /* the rest are all set to zero by kzalloc */
1570 range->len = (u64)-1;
1571 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001572 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001573 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001574 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001575 default:
1576 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001577 }
Chris Masone441d542009-01-05 16:57:23 -05001578out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001579 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001580 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001581}
1582
Christoph Hellwigb2950862008-12-02 09:54:17 -05001583static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584{
1585 struct btrfs_ioctl_vol_args *vol_args;
1586 int ret;
1587
Chris Masone441d542009-01-05 16:57:23 -05001588 if (!capable(CAP_SYS_ADMIN))
1589 return -EPERM;
1590
Li Zefandae7b662009-04-08 15:06:54 +08001591 vol_args = memdup_user(arg, sizeof(*vol_args));
1592 if (IS_ERR(vol_args))
1593 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001594
Mark Fasheh5516e592008-07-24 12:20:14 -04001595 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001596 ret = btrfs_init_new_device(root, vol_args->name);
1597
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001598 kfree(vol_args);
1599 return ret;
1600}
1601
Christoph Hellwigb2950862008-12-02 09:54:17 -05001602static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001603{
1604 struct btrfs_ioctl_vol_args *vol_args;
1605 int ret;
1606
Chris Masone441d542009-01-05 16:57:23 -05001607 if (!capable(CAP_SYS_ADMIN))
1608 return -EPERM;
1609
Yan Zhengc146afa2008-11-12 14:34:12 -05001610 if (root->fs_info->sb->s_flags & MS_RDONLY)
1611 return -EROFS;
1612
Li Zefandae7b662009-04-08 15:06:54 +08001613 vol_args = memdup_user(arg, sizeof(*vol_args));
1614 if (IS_ERR(vol_args))
1615 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001616
Mark Fasheh5516e592008-07-24 12:20:14 -04001617 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001618 ret = btrfs_rm_device(root, vol_args->name);
1619
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001620 kfree(vol_args);
1621 return ret;
1622}
1623
Yan, Zheng76dda932009-09-21 16:00:26 -04001624static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1625 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001626{
1627 struct inode *inode = fdentry(file)->d_inode;
1628 struct btrfs_root *root = BTRFS_I(inode)->root;
1629 struct file *src_file;
1630 struct inode *src;
1631 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001632 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001633 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001634 char *buf;
1635 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001636 u32 nritems;
1637 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001638 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001639 u64 len = olen;
1640 u64 bs = root->fs_info->sb->s_blocksize;
1641 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001642
Sage Weilc5c9cd42008-11-12 14:32:25 -05001643 /*
1644 * TODO:
1645 * - split compressed inline extents. annoying: we need to
1646 * decompress into destination's address_space (the file offset
1647 * may change, so source mapping won't do), then recompress (or
1648 * otherwise reinsert) a subrange.
1649 * - allow ranges within the same file to be cloned (provided
1650 * they don't overlap)?
1651 */
1652
Chris Masone441d542009-01-05 16:57:23 -05001653 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001654 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001655 return -EINVAL;
1656
Yan Zhengc146afa2008-11-12 14:34:12 -05001657 ret = mnt_want_write(file->f_path.mnt);
1658 if (ret)
1659 return ret;
1660
Sage Weilc5c9cd42008-11-12 14:32:25 -05001661 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001662 if (!src_file) {
1663 ret = -EBADF;
1664 goto out_drop_write;
1665 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001666
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001667 src = src_file->f_dentry->d_inode;
1668
Sage Weilc5c9cd42008-11-12 14:32:25 -05001669 ret = -EINVAL;
1670 if (src == inode)
1671 goto out_fput;
1672
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001673 /* the src must be open for reading */
1674 if (!(src_file->f_mode & FMODE_READ))
1675 goto out_fput;
1676
Yan Zhengae01a0a2008-08-04 23:23:47 -04001677 ret = -EISDIR;
1678 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001679 goto out_fput;
1680
Yan Zhengae01a0a2008-08-04 23:23:47 -04001681 ret = -EXDEV;
1682 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1683 goto out_fput;
1684
1685 ret = -ENOMEM;
1686 buf = vmalloc(btrfs_level_size(root, 0));
1687 if (!buf)
1688 goto out_fput;
1689
1690 path = btrfs_alloc_path();
1691 if (!path) {
1692 vfree(buf);
1693 goto out_fput;
1694 }
1695 path->reada = 2;
1696
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001697 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001698 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1699 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001700 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001701 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1702 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001703 }
1704
Sage Weilc5c9cd42008-11-12 14:32:25 -05001705 /* determine range to clone */
1706 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001707 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001709 if (len == 0)
1710 olen = len = src->i_size - off;
1711 /* if we extend to eof, continue to block boundary */
1712 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001713 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001714
1715 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001716 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1717 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001718 goto out_unlock;
1719
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001720 /* do any pending delalloc/csum calc on src, one way or
1721 another, and lock file content */
1722 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001723 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001724 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001725 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1726 if (!ordered &&
1727 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1728 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001729 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001730 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001731 if (ordered)
1732 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001733 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001734 }
1735
Sage Weilc5c9cd42008-11-12 14:32:25 -05001736 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001737 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001738 key.type = BTRFS_EXTENT_DATA_KEY;
1739 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001740
1741 while (1) {
1742 /*
1743 * note the key will change type as we walk through the
1744 * tree.
1745 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001746 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001747 if (ret < 0)
1748 goto out;
1749
Yan Zhengae01a0a2008-08-04 23:23:47 -04001750 nritems = btrfs_header_nritems(path->nodes[0]);
1751 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752 ret = btrfs_next_leaf(root, path);
1753 if (ret < 0)
1754 goto out;
1755 if (ret > 0)
1756 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001757 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001758 }
1759 leaf = path->nodes[0];
1760 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001761
Yan Zhengae01a0a2008-08-04 23:23:47 -04001762 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001763 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 key.objectid != src->i_ino)
1765 break;
1766
Sage Weilc5c9cd42008-11-12 14:32:25 -05001767 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1768 struct btrfs_file_extent_item *extent;
1769 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001770 u32 size;
1771 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001772 u64 disko = 0, diskl = 0;
1773 u64 datao = 0, datal = 0;
1774 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001775 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001776
1777 size = btrfs_item_size_nr(leaf, slot);
1778 read_extent_buffer(leaf, buf,
1779 btrfs_item_ptr_offset(leaf, slot),
1780 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001781
1782 extent = btrfs_item_ptr(leaf, slot,
1783 struct btrfs_file_extent_item);
1784 comp = btrfs_file_extent_compression(leaf, extent);
1785 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001786 if (type == BTRFS_FILE_EXTENT_REG ||
1787 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001788 disko = btrfs_file_extent_disk_bytenr(leaf,
1789 extent);
1790 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1791 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001792 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001793 datal = btrfs_file_extent_num_bytes(leaf,
1794 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001795 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1796 /* take upper bound, may be compressed */
1797 datal = btrfs_file_extent_ram_bytes(leaf,
1798 extent);
1799 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001800 btrfs_release_path(root, path);
1801
Sage Weil050006a2010-10-29 15:37:33 -04001802 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001803 key.offset >= off+len)
1804 goto next;
1805
Zheng Yan31840ae2008-09-23 13:14:14 -04001806 memcpy(&new_key, &key, sizeof(new_key));
1807 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001808 new_key.offset = key.offset + destoff - off;
1809
Yan, Zhenga22285a2010-05-16 10:48:46 -04001810 trans = btrfs_start_transaction(root, 1);
1811 if (IS_ERR(trans)) {
1812 ret = PTR_ERR(trans);
1813 goto out;
1814 }
1815
Chris Masonc8a894d2009-06-27 21:07:03 -04001816 if (type == BTRFS_FILE_EXTENT_REG ||
1817 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001818 if (off > key.offset) {
1819 datao += off - key.offset;
1820 datal -= off - key.offset;
1821 }
1822
1823 if (key.offset + datal > off + len)
1824 datal = off + len - key.offset;
1825
1826 ret = btrfs_drop_extents(trans, inode,
1827 new_key.offset,
1828 new_key.offset + datal,
1829 &hint_byte, 1);
1830 BUG_ON(ret);
1831
Sage Weilc5c9cd42008-11-12 14:32:25 -05001832 ret = btrfs_insert_empty_item(trans, root, path,
1833 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001834 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001835
1836 leaf = path->nodes[0];
1837 slot = path->slots[0];
1838 write_extent_buffer(leaf, buf,
1839 btrfs_item_ptr_offset(leaf, slot),
1840 size);
1841
1842 extent = btrfs_item_ptr(leaf, slot,
1843 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001844
Sage Weilc5c9cd42008-11-12 14:32:25 -05001845 /* disko == 0 means it's a hole */
1846 if (!disko)
1847 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001848
1849 btrfs_set_file_extent_offset(leaf, extent,
1850 datao);
1851 btrfs_set_file_extent_num_bytes(leaf, extent,
1852 datal);
1853 if (disko) {
1854 inode_add_bytes(inode, datal);
1855 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001856 disko, diskl, 0,
1857 root->root_key.objectid,
1858 inode->i_ino,
1859 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001860 BUG_ON(ret);
1861 }
1862 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1863 u64 skip = 0;
1864 u64 trim = 0;
1865 if (off > key.offset) {
1866 skip = off - key.offset;
1867 new_key.offset += skip;
1868 }
Chris Masond3977122009-01-05 21:25:51 -05001869
Sage Weilc5c9cd42008-11-12 14:32:25 -05001870 if (key.offset + datal > off+len)
1871 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001872
Sage Weilc5c9cd42008-11-12 14:32:25 -05001873 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001874 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001875 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001876 goto out;
1877 }
1878 size -= skip + trim;
1879 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001880
1881 ret = btrfs_drop_extents(trans, inode,
1882 new_key.offset,
1883 new_key.offset + datal,
1884 &hint_byte, 1);
1885 BUG_ON(ret);
1886
Sage Weilc5c9cd42008-11-12 14:32:25 -05001887 ret = btrfs_insert_empty_item(trans, root, path,
1888 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001889 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001890
1891 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001892 u32 start =
1893 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001894 memmove(buf+start, buf+start+skip,
1895 datal);
1896 }
1897
1898 leaf = path->nodes[0];
1899 slot = path->slots[0];
1900 write_extent_buffer(leaf, buf,
1901 btrfs_item_ptr_offset(leaf, slot),
1902 size);
1903 inode_add_bytes(inode, datal);
1904 }
1905
1906 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001907 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001908
Yan, Zhenga22285a2010-05-16 10:48:46 -04001909 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00001910
1911 /*
1912 * we round up to the block size at eof when
1913 * determining which extents to clone above,
1914 * but shouldn't round up the file size
1915 */
1916 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00001917 if (endoff > destoff+olen)
1918 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00001919 if (endoff > inode->i_size)
1920 btrfs_i_size_write(inode, endoff);
1921
Yan, Zhenga22285a2010-05-16 10:48:46 -04001922 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1923 ret = btrfs_update_inode(trans, root, inode);
1924 BUG_ON(ret);
1925 btrfs_end_transaction(trans, root);
1926 }
Chris Masond3977122009-01-05 21:25:51 -05001927next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001928 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001929 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001930 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001931 ret = 0;
1932out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001933 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001934 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001935out_unlock:
1936 mutex_unlock(&src->i_mutex);
1937 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001938 vfree(buf);
1939 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001940out_fput:
1941 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001942out_drop_write:
1943 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001944 return ret;
1945}
1946
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001947static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001948{
1949 struct btrfs_ioctl_clone_range_args args;
1950
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001951 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001952 return -EFAULT;
1953 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1954 args.src_length, args.dest_offset);
1955}
1956
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001957/*
1958 * there are many ways the trans_start and trans_end ioctls can lead
1959 * to deadlocks. They should only be used by applications that
1960 * basically own the machine, and have a very in depth understanding
1961 * of all the possible deadlocks and enospc problems.
1962 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001963static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001964{
1965 struct inode *inode = fdentry(file)->d_inode;
1966 struct btrfs_root *root = BTRFS_I(inode)->root;
1967 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001968 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001969
Sage Weil1ab86ae2009-09-29 18:38:44 -04001970 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001971 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001972 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001973
1974 ret = -EINPROGRESS;
1975 if (file->private_data)
1976 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001977
Yan Zhengc146afa2008-11-12 14:34:12 -05001978 ret = mnt_want_write(file->f_path.mnt);
1979 if (ret)
1980 goto out;
1981
Sage Weil9ca9ee02008-08-04 10:41:27 -04001982 mutex_lock(&root->fs_info->trans_mutex);
1983 root->fs_info->open_ioctl_trans++;
1984 mutex_unlock(&root->fs_info->trans_mutex);
1985
Sage Weil1ab86ae2009-09-29 18:38:44 -04001986 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001987 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001988 if (!trans)
1989 goto out_drop;
1990
1991 file->private_data = trans;
1992 return 0;
1993
1994out_drop:
1995 mutex_lock(&root->fs_info->trans_mutex);
1996 root->fs_info->open_ioctl_trans--;
1997 mutex_unlock(&root->fs_info->trans_mutex);
1998 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001999out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002000 return ret;
2001}
2002
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002003static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2004{
2005 struct inode *inode = fdentry(file)->d_inode;
2006 struct btrfs_root *root = BTRFS_I(inode)->root;
2007 struct btrfs_root *new_root;
2008 struct btrfs_dir_item *di;
2009 struct btrfs_trans_handle *trans;
2010 struct btrfs_path *path;
2011 struct btrfs_key location;
2012 struct btrfs_disk_key disk_key;
2013 struct btrfs_super_block *disk_super;
2014 u64 features;
2015 u64 objectid = 0;
2016 u64 dir_id;
2017
2018 if (!capable(CAP_SYS_ADMIN))
2019 return -EPERM;
2020
2021 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2022 return -EFAULT;
2023
2024 if (!objectid)
2025 objectid = root->root_key.objectid;
2026
2027 location.objectid = objectid;
2028 location.type = BTRFS_ROOT_ITEM_KEY;
2029 location.offset = (u64)-1;
2030
2031 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2032 if (IS_ERR(new_root))
2033 return PTR_ERR(new_root);
2034
2035 if (btrfs_root_refs(&new_root->root_item) == 0)
2036 return -ENOENT;
2037
2038 path = btrfs_alloc_path();
2039 if (!path)
2040 return -ENOMEM;
2041 path->leave_spinning = 1;
2042
2043 trans = btrfs_start_transaction(root, 1);
2044 if (!trans) {
2045 btrfs_free_path(path);
2046 return -ENOMEM;
2047 }
2048
2049 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2050 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2051 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002052 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002053 btrfs_free_path(path);
2054 btrfs_end_transaction(trans, root);
2055 printk(KERN_ERR "Umm, you don't have the default dir item, "
2056 "this isn't going to work\n");
2057 return -ENOENT;
2058 }
2059
2060 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2061 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2062 btrfs_mark_buffer_dirty(path->nodes[0]);
2063 btrfs_free_path(path);
2064
2065 disk_super = &root->fs_info->super_copy;
2066 features = btrfs_super_incompat_flags(disk_super);
2067 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2068 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2069 btrfs_set_super_incompat_flags(disk_super, features);
2070 }
2071 btrfs_end_transaction(trans, root);
2072
2073 return 0;
2074}
2075
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002076static void get_block_group_info(struct list_head *groups_list,
2077 struct btrfs_ioctl_space_info *space)
2078{
2079 struct btrfs_block_group_cache *block_group;
2080
2081 space->total_bytes = 0;
2082 space->used_bytes = 0;
2083 space->flags = 0;
2084 list_for_each_entry(block_group, groups_list, list) {
2085 space->flags = block_group->flags;
2086 space->total_bytes += block_group->key.offset;
2087 space->used_bytes +=
2088 btrfs_block_group_used(&block_group->item);
2089 }
2090}
2091
Josef Bacik1406e432010-01-13 18:19:06 +00002092long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2093{
2094 struct btrfs_ioctl_space_args space_args;
2095 struct btrfs_ioctl_space_info space;
2096 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002097 struct btrfs_ioctl_space_info *dest_orig;
2098 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002099 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002100 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2101 BTRFS_BLOCK_GROUP_SYSTEM,
2102 BTRFS_BLOCK_GROUP_METADATA,
2103 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2104 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002105 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002106 int ret = 0;
Chris Mason7fde62b2010-03-16 15:40:10 -04002107 int slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002108 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002109
2110 if (copy_from_user(&space_args,
2111 (struct btrfs_ioctl_space_args __user *)arg,
2112 sizeof(space_args)))
2113 return -EFAULT;
2114
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002115 for (i = 0; i < num_types; i++) {
2116 struct btrfs_space_info *tmp;
2117
2118 info = NULL;
2119 rcu_read_lock();
2120 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2121 list) {
2122 if (tmp->flags == types[i]) {
2123 info = tmp;
2124 break;
2125 }
2126 }
2127 rcu_read_unlock();
2128
2129 if (!info)
2130 continue;
2131
2132 down_read(&info->groups_sem);
2133 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2134 if (!list_empty(&info->block_groups[c]))
2135 slot_count++;
2136 }
2137 up_read(&info->groups_sem);
2138 }
Josef Bacik1406e432010-01-13 18:19:06 +00002139
Chris Mason7fde62b2010-03-16 15:40:10 -04002140 /* space_slots == 0 means they are asking for a count */
2141 if (space_args.space_slots == 0) {
2142 space_args.total_spaces = slot_count;
2143 goto out;
2144 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002145
2146 slot_count = min_t(int, space_args.space_slots, slot_count);
2147
Chris Mason7fde62b2010-03-16 15:40:10 -04002148 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002149
Chris Mason7fde62b2010-03-16 15:40:10 -04002150 /* we generally have at most 6 or so space infos, one for each raid
2151 * level. So, a whole page should be more than enough for everyone
2152 */
2153 if (alloc_size > PAGE_CACHE_SIZE)
2154 return -ENOMEM;
2155
2156 space_args.total_spaces = 0;
2157 dest = kmalloc(alloc_size, GFP_NOFS);
2158 if (!dest)
2159 return -ENOMEM;
2160 dest_orig = dest;
2161
2162 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002163 for (i = 0; i < num_types; i++) {
2164 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002165
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002166 info = NULL;
2167 rcu_read_lock();
2168 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2169 list) {
2170 if (tmp->flags == types[i]) {
2171 info = tmp;
2172 break;
2173 }
2174 }
2175 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002176
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002177 if (!info)
2178 continue;
2179 down_read(&info->groups_sem);
2180 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2181 if (!list_empty(&info->block_groups[c])) {
2182 get_block_group_info(&info->block_groups[c],
2183 &space);
2184 memcpy(dest, &space, sizeof(space));
2185 dest++;
2186 space_args.total_spaces++;
2187 }
2188 }
2189 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002190 }
Josef Bacik1406e432010-01-13 18:19:06 +00002191
Chris Mason7fde62b2010-03-16 15:40:10 -04002192 user_dest = (struct btrfs_ioctl_space_info *)
2193 (arg + sizeof(struct btrfs_ioctl_space_args));
2194
2195 if (copy_to_user(user_dest, dest_orig, alloc_size))
2196 ret = -EFAULT;
2197
2198 kfree(dest_orig);
2199out:
2200 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002201 ret = -EFAULT;
2202
2203 return ret;
2204}
2205
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002206/*
2207 * there are many ways the trans_start and trans_end ioctls can lead
2208 * to deadlocks. They should only be used by applications that
2209 * basically own the machine, and have a very in depth understanding
2210 * of all the possible deadlocks and enospc problems.
2211 */
2212long btrfs_ioctl_trans_end(struct file *file)
2213{
2214 struct inode *inode = fdentry(file)->d_inode;
2215 struct btrfs_root *root = BTRFS_I(inode)->root;
2216 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002217
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002218 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002219 if (!trans)
2220 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002221 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002222
Sage Weil1ab86ae2009-09-29 18:38:44 -04002223 btrfs_end_transaction(trans, root);
2224
Sage Weil9ca9ee02008-08-04 10:41:27 -04002225 mutex_lock(&root->fs_info->trans_mutex);
2226 root->fs_info->open_ioctl_trans--;
2227 mutex_unlock(&root->fs_info->trans_mutex);
2228
Sage Weilcfc8ea82008-12-11 16:30:06 -05002229 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002230 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002231}
2232
Sage Weil46204592010-10-29 15:41:32 -04002233static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2234{
2235 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2236 struct btrfs_trans_handle *trans;
2237 u64 transid;
2238
2239 trans = btrfs_start_transaction(root, 0);
2240 transid = trans->transid;
2241 btrfs_commit_transaction_async(trans, root, 0);
2242
2243 if (argp)
2244 if (copy_to_user(argp, &transid, sizeof(transid)))
2245 return -EFAULT;
2246 return 0;
2247}
2248
2249static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2250{
2251 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2252 u64 transid;
2253
2254 if (argp) {
2255 if (copy_from_user(&transid, argp, sizeof(transid)))
2256 return -EFAULT;
2257 } else {
2258 transid = 0; /* current trans */
2259 }
2260 return btrfs_wait_for_commit(root, transid);
2261}
2262
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002263long btrfs_ioctl(struct file *file, unsigned int
2264 cmd, unsigned long arg)
2265{
2266 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002267 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002268
2269 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002270 case FS_IOC_GETFLAGS:
2271 return btrfs_ioctl_getflags(file, argp);
2272 case FS_IOC_SETFLAGS:
2273 return btrfs_ioctl_setflags(file, argp);
2274 case FS_IOC_GETVERSION:
2275 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002276 case BTRFS_IOC_SNAP_CREATE:
Sage Weil72fd0322010-10-29 15:41:32 -04002277 return btrfs_ioctl_snap_create(file, argp, 0, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002278 case BTRFS_IOC_SNAP_CREATE_V2:
Sage Weil72fd0322010-10-29 15:41:32 -04002279 return btrfs_ioctl_snap_create(file, argp, 0, 1);
Chris Mason3de45862008-11-17 21:02:50 -05002280 case BTRFS_IOC_SUBVOL_CREATE:
Sage Weil72fd0322010-10-29 15:41:32 -04002281 return btrfs_ioctl_snap_create(file, argp, 1, 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04002282 case BTRFS_IOC_SNAP_DESTROY:
2283 return btrfs_ioctl_snap_destroy(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002284 case BTRFS_IOC_DEFAULT_SUBVOL:
2285 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002286 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002287 return btrfs_ioctl_defrag(file, NULL);
2288 case BTRFS_IOC_DEFRAG_RANGE:
2289 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002290 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002291 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002292 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002293 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002294 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002295 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002296 case BTRFS_IOC_BALANCE:
2297 return btrfs_balance(root->fs_info->dev_root);
2298 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002299 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2300 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002301 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002302 case BTRFS_IOC_TRANS_START:
2303 return btrfs_ioctl_trans_start(file);
2304 case BTRFS_IOC_TRANS_END:
2305 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002306 case BTRFS_IOC_TREE_SEARCH:
2307 return btrfs_ioctl_tree_search(file, argp);
2308 case BTRFS_IOC_INO_LOOKUP:
2309 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002310 case BTRFS_IOC_SPACE_INFO:
2311 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002312 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002313 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2314 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002315 case BTRFS_IOC_START_SYNC:
2316 return btrfs_ioctl_start_sync(file, argp);
2317 case BTRFS_IOC_WAIT_SYNC:
2318 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002319 }
2320
2321 return -ENOTTY;
2322}