blob: 926332a73cdefd957cf1ef483544f460d9f1278a [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>
30#include <linux/smp_lock.h>
31#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040032#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040033#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040034#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040035#include <linux/swap.h>
36#include <linux/writeback.h>
37#include <linux/statfs.h>
38#include <linux/compat.h>
39#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040040#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040041#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040042#include <linux/vmalloc.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{
115 unsigned int flags = BTRFS_I(dir)->flags;
116
117 if (S_ISREG(inode->i_mode))
118 flags &= ~BTRFS_INODE_DIRSYNC;
119 else if (!S_ISDIR(inode->i_mode))
120 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
121
122 BTRFS_I(inode)->flags = flags;
123 btrfs_update_iflags(inode);
124}
125
126static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
127{
128 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
129 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
130
131 if (copy_to_user(arg, &flags, sizeof(flags)))
132 return -EFAULT;
133 return 0;
134}
135
136static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
137{
138 struct inode *inode = file->f_path.dentry->d_inode;
139 struct btrfs_inode *ip = BTRFS_I(inode);
140 struct btrfs_root *root = ip->root;
141 struct btrfs_trans_handle *trans;
142 unsigned int flags, oldflags;
143 int ret;
144
145 if (copy_from_user(&flags, arg, sizeof(flags)))
146 return -EFAULT;
147
148 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
149 FS_NOATIME_FL | FS_NODUMP_FL | \
150 FS_SYNC_FL | FS_DIRSYNC_FL))
151 return -EOPNOTSUPP;
152
153 if (!is_owner_or_cap(inode))
154 return -EACCES;
155
156 mutex_lock(&inode->i_mutex);
157
158 flags = btrfs_mask_flags(inode->i_mode, flags);
159 oldflags = btrfs_flags_to_ioctl(ip->flags);
160 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
161 if (!capable(CAP_LINUX_IMMUTABLE)) {
162 ret = -EPERM;
163 goto out_unlock;
164 }
165 }
166
167 ret = mnt_want_write(file->f_path.mnt);
168 if (ret)
169 goto out_unlock;
170
171 if (flags & FS_SYNC_FL)
172 ip->flags |= BTRFS_INODE_SYNC;
173 else
174 ip->flags &= ~BTRFS_INODE_SYNC;
175 if (flags & FS_IMMUTABLE_FL)
176 ip->flags |= BTRFS_INODE_IMMUTABLE;
177 else
178 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
179 if (flags & FS_APPEND_FL)
180 ip->flags |= BTRFS_INODE_APPEND;
181 else
182 ip->flags &= ~BTRFS_INODE_APPEND;
183 if (flags & FS_NODUMP_FL)
184 ip->flags |= BTRFS_INODE_NODUMP;
185 else
186 ip->flags &= ~BTRFS_INODE_NODUMP;
187 if (flags & FS_NOATIME_FL)
188 ip->flags |= BTRFS_INODE_NOATIME;
189 else
190 ip->flags &= ~BTRFS_INODE_NOATIME;
191 if (flags & FS_DIRSYNC_FL)
192 ip->flags |= BTRFS_INODE_DIRSYNC;
193 else
194 ip->flags &= ~BTRFS_INODE_DIRSYNC;
195
196
197 trans = btrfs_join_transaction(root, 1);
198 BUG_ON(!trans);
199
200 ret = btrfs_update_inode(trans, root, inode);
201 BUG_ON(ret);
202
203 btrfs_update_iflags(inode);
204 inode->i_ctime = CURRENT_TIME;
205 btrfs_end_transaction(trans, root);
206
207 mnt_drop_write(file->f_path.mnt);
208 out_unlock:
209 mutex_unlock(&inode->i_mutex);
210 return 0;
211}
212
213static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
214{
215 struct inode *inode = file->f_path.dentry->d_inode;
216
217 return put_user(inode->i_generation, arg);
218}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400219
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400220static noinline int create_subvol(struct btrfs_root *root,
221 struct dentry *dentry,
222 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400223{
224 struct btrfs_trans_handle *trans;
225 struct btrfs_key key;
226 struct btrfs_root_item root_item;
227 struct btrfs_inode_item *inode_item;
228 struct extent_buffer *leaf;
229 struct btrfs_root *new_root = root;
230 struct inode *dir;
231 int ret;
232 int err;
233 u64 objectid;
234 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500235 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400236 unsigned long nr = 1;
237
Josef Bacik6a632092009-02-20 11:00:09 -0500238 ret = btrfs_check_metadata_free_space(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400239 if (ret)
240 goto fail_commit;
241
242 trans = btrfs_start_transaction(root, 1);
243 BUG_ON(!trans);
244
245 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
246 0, &objectid);
247 if (ret)
248 goto fail;
249
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400250 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
251 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400252 if (IS_ERR(leaf)) {
253 ret = PTR_ERR(leaf);
254 goto fail;
255 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400256
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400257 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400258 btrfs_set_header_bytenr(leaf, leaf->start);
259 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400261 btrfs_set_header_owner(leaf, objectid);
262
263 write_extent_buffer(leaf, root->fs_info->fsid,
264 (unsigned long)btrfs_header_fsid(leaf),
265 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
267 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
268 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400269 btrfs_mark_buffer_dirty(leaf);
270
271 inode_item = &root_item.inode;
272 memset(inode_item, 0, sizeof(*inode_item));
273 inode_item->generation = cpu_to_le64(1);
274 inode_item->size = cpu_to_le64(3);
275 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400276 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400277 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
278
279 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400280 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400281 btrfs_set_root_level(&root_item, 0);
282 btrfs_set_root_refs(&root_item, 1);
283 btrfs_set_root_used(&root_item, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400284 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400285
286 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
287 root_item.drop_level = 0;
288
Chris Mason925baed2008-06-25 16:01:30 -0400289 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400290 free_extent_buffer(leaf);
291 leaf = NULL;
292
293 btrfs_set_root_dirid(&root_item, new_dirid);
294
295 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400296 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400297 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
298 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
299 &root_item);
300 if (ret)
301 goto fail;
302
303 /*
304 * insert the directory item
305 */
306 key.offset = (u64)-1;
Chris Mason3de45862008-11-17 21:02:50 -0500307 dir = dentry->d_parent->d_inode;
308 ret = btrfs_set_inode_index(dir, &index);
309 BUG_ON(ret);
310
311 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400312 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500313 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400314 if (ret)
315 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500316
Yan Zheng52c26172009-01-05 15:43:43 -0500317 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
318 ret = btrfs_update_inode(trans, root, dir);
319 BUG_ON(ret);
320
Chris Mason0660b5a2008-11-17 20:37:39 -0500321 /* add the backref first */
322 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
323 objectid, BTRFS_ROOT_BACKREF_KEY,
324 root->root_key.objectid,
325 dir->i_ino, index, name, namelen);
326
327 BUG_ON(ret);
328
329 /* now add the forward ref */
330 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
331 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
332 objectid,
333 dir->i_ino, index, name, namelen);
334
335 BUG_ON(ret);
336
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400337 ret = btrfs_commit_transaction(trans, root);
338 if (ret)
339 goto fail_commit;
340
Chris Mason3de45862008-11-17 21:02:50 -0500341 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400342 BUG_ON(!new_root);
343
344 trans = btrfs_start_transaction(new_root, 1);
345 BUG_ON(!trans);
346
Yan Zhengd2fb3432008-12-11 16:30:39 -0500347 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348 BTRFS_I(dir)->block_group);
349 if (ret)
350 goto fail;
351
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400352fail:
353 nr = trans->blocks_used;
354 err = btrfs_commit_transaction(trans, new_root);
355 if (err && !ret)
356 ret = err;
357fail_commit:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400358 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400359 return ret;
360}
361
Chris Mason3de45862008-11-17 21:02:50 -0500362static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
363 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400364{
365 struct btrfs_pending_snapshot *pending_snapshot;
366 struct btrfs_trans_handle *trans;
Chris Mason3de45862008-11-17 21:02:50 -0500367 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400368 int err;
369 unsigned long nr = 0;
370
371 if (!root->ref_cows)
372 return -EINVAL;
373
Josef Bacik6a632092009-02-20 11:00:09 -0500374 ret = btrfs_check_metadata_free_space(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375 if (ret)
376 goto fail_unlock;
377
Chris Mason3de45862008-11-17 21:02:50 -0500378 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379 if (!pending_snapshot) {
380 ret = -ENOMEM;
381 goto fail_unlock;
382 }
383 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
384 if (!pending_snapshot->name) {
385 ret = -ENOMEM;
386 kfree(pending_snapshot);
387 goto fail_unlock;
388 }
389 memcpy(pending_snapshot->name, name, namelen);
390 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500391 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392 trans = btrfs_start_transaction(root, 1);
393 BUG_ON(!trans);
394 pending_snapshot->root = root;
395 list_add(&pending_snapshot->list,
396 &trans->transaction->pending_snapshots);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 err = btrfs_commit_transaction(trans, root);
398
399fail_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400400 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400401 return ret;
402}
403
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400404/* copy of may_create in fs/namei.c() */
405static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
406{
407 if (child->d_inode)
408 return -EEXIST;
409 if (IS_DEADDIR(dir))
410 return -ENOENT;
411 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
412}
413
414/*
415 * Create a new subvolume below @parent. This is largely modeled after
416 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
417 * inside this filesystem so it's quite a bit simpler.
418 */
419static noinline int btrfs_mksubvol(struct path *parent, char *name,
Chris Mason3de45862008-11-17 21:02:50 -0500420 int mode, int namelen,
421 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400422{
423 struct dentry *dentry;
424 int error;
425
426 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
427
428 dentry = lookup_one_len(name, parent->dentry, namelen);
429 error = PTR_ERR(dentry);
430 if (IS_ERR(dentry))
431 goto out_unlock;
432
433 error = -EEXIST;
434 if (dentry->d_inode)
435 goto out_dput;
436
437 if (!IS_POSIXACL(parent->dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -0400438 mode &= ~current_umask();
Chris Mason3de45862008-11-17 21:02:50 -0500439
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400440 error = mnt_want_write(parent->mnt);
441 if (error)
442 goto out_dput;
443
444 error = btrfs_may_create(parent->dentry->d_inode, dentry);
445 if (error)
446 goto out_drop_write;
447
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400448 /*
449 * Actually perform the low-level subvolume creation after all
450 * this VFS fuzz.
451 *
452 * Eventually we want to pass in an inode under which we create this
453 * subvolume, but for now all are under the filesystem root.
454 *
455 * Also we should pass on the mode eventually to allow creating new
456 * subvolume with specific mode bits.
457 */
Chris Mason3de45862008-11-17 21:02:50 -0500458 if (snap_src) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500459 struct dentry *dir = dentry->d_parent;
460 struct dentry *test = dir->d_parent;
461 struct btrfs_path *path = btrfs_alloc_path();
462 int ret;
463 u64 test_oid;
464 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
465
466 test_oid = snap_src->root_key.objectid;
467
468 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
469 path, parent_oid, test_oid);
470 if (ret == 0)
471 goto create;
472 btrfs_release_path(snap_src->fs_info->tree_root, path);
473
474 /* we need to make sure we aren't creating a directory loop
475 * by taking a snapshot of something that has our current
476 * subvol in its directory tree. So, this loops through
477 * the dentries and checks the forward refs for each subvolume
478 * to see if is references the subvolume where we are
479 * placing this new snapshot.
480 */
Chris Masond3977122009-01-05 21:25:51 -0500481 while (1) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500482 if (!test ||
483 dir == snap_src->fs_info->sb->s_root ||
484 test == snap_src->fs_info->sb->s_root ||
485 test->d_inode->i_sb != snap_src->fs_info->sb) {
486 break;
487 }
488 if (S_ISLNK(test->d_inode->i_mode)) {
Chris Masond3977122009-01-05 21:25:51 -0500489 printk(KERN_INFO "Btrfs symlink in snapshot "
490 "path, failed\n");
Chris Masonea9e8b12008-11-17 21:14:24 -0500491 error = -EMLINK;
492 btrfs_free_path(path);
493 goto out_drop_write;
494 }
495 test_oid =
496 BTRFS_I(test->d_inode)->root->root_key.objectid;
497 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
498 path, test_oid, parent_oid);
499 if (ret == 0) {
Chris Masond3977122009-01-05 21:25:51 -0500500 printk(KERN_INFO "Btrfs snapshot creation "
501 "failed, looping\n");
Chris Masonea9e8b12008-11-17 21:14:24 -0500502 error = -EMLINK;
503 btrfs_free_path(path);
504 goto out_drop_write;
505 }
506 btrfs_release_path(snap_src->fs_info->tree_root, path);
507 test = test->d_parent;
508 }
509create:
510 btrfs_free_path(path);
Chris Mason3de45862008-11-17 21:02:50 -0500511 error = create_snapshot(snap_src, dentry, name, namelen);
512 } else {
513 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
514 dentry, name, namelen);
515 }
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400516 if (error)
517 goto out_drop_write;
518
519 fsnotify_mkdir(parent->dentry->d_inode, dentry);
520out_drop_write:
521 mnt_drop_write(parent->mnt);
522out_dput:
523 dput(dentry);
524out_unlock:
525 mutex_unlock(&parent->dentry->d_inode->i_mutex);
526 return error;
527}
528
529
Christoph Hellwigb2950862008-12-02 09:54:17 -0500530static int btrfs_defrag_file(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531{
532 struct inode *inode = fdentry(file)->d_inode;
533 struct btrfs_root *root = BTRFS_I(inode)->root;
534 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400535 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400536 struct page *page;
537 unsigned long last_index;
538 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
539 unsigned long total_read = 0;
540 u64 page_start;
541 u64 page_end;
542 unsigned long i;
543 int ret;
544
Josef Bacik6a632092009-02-20 11:00:09 -0500545 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400546 if (ret)
547 return -ENOSPC;
548
549 mutex_lock(&inode->i_mutex);
550 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
551 for (i = 0; i <= last_index; i++) {
552 if (total_read % ra_pages == 0) {
553 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
554 min(last_index, i + ra_pages - 1));
555 }
556 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400557again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558 page = grab_cache_page(inode->i_mapping, i);
559 if (!page)
560 goto out_unlock;
561 if (!PageUptodate(page)) {
562 btrfs_readpage(NULL, page);
563 lock_page(page);
564 if (!PageUptodate(page)) {
565 unlock_page(page);
566 page_cache_release(page);
567 goto out_unlock;
568 }
569 }
570
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400571 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400572
573 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
574 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400575 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400576
577 ordered = btrfs_lookup_ordered_extent(inode, page_start);
578 if (ordered) {
579 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
580 unlock_page(page);
581 page_cache_release(page);
582 btrfs_start_ordered_extent(inode, ordered, 1);
583 btrfs_put_ordered_extent(ordered);
584 goto again;
585 }
586 set_page_extent_mapped(page);
587
Chris Masonf87f0572008-08-01 11:27:23 -0400588 /*
589 * this makes sure page_mkwrite is called on the
590 * page if it is dirtied again later
591 */
592 clear_page_dirty_for_io(page);
593
Chris Masonea8c2812008-08-04 23:17:27 -0400594 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400595
596 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
597 set_page_dirty(page);
598 unlock_page(page);
599 page_cache_release(page);
600 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
601 }
602
603out_unlock:
604 mutex_unlock(&inode->i_mutex);
605 return 0;
606}
607
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400608static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
609{
610 u64 new_size;
611 u64 old_size;
612 u64 devid = 1;
613 struct btrfs_ioctl_vol_args *vol_args;
614 struct btrfs_trans_handle *trans;
615 struct btrfs_device *device = NULL;
616 char *sizestr;
617 char *devstr = NULL;
618 int ret = 0;
619 int namelen;
620 int mod = 0;
621
Yan Zhengc146afa2008-11-12 14:34:12 -0500622 if (root->fs_info->sb->s_flags & MS_RDONLY)
623 return -EROFS;
624
Chris Masone441d542009-01-05 16:57:23 -0500625 if (!capable(CAP_SYS_ADMIN))
626 return -EPERM;
627
Li Zefandae7b662009-04-08 15:06:54 +0800628 vol_args = memdup_user(arg, sizeof(*vol_args));
629 if (IS_ERR(vol_args))
630 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400631
632 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400633 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400634
Chris Mason7d9eb122008-07-08 14:19:17 -0400635 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400636 sizestr = vol_args->name;
637 devstr = strchr(sizestr, ':');
638 if (devstr) {
639 char *end;
640 sizestr = devstr + 1;
641 *devstr = '\0';
642 devstr = vol_args->name;
643 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700644 printk(KERN_INFO "resizing devid %llu\n",
645 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400646 }
Yan Zheng2b820322008-11-17 21:11:30 -0500647 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400648 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700649 printk(KERN_INFO "resizer unable to find device %llu\n",
650 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400651 ret = -EINVAL;
652 goto out_unlock;
653 }
654 if (!strcmp(sizestr, "max"))
655 new_size = device->bdev->bd_inode->i_size;
656 else {
657 if (sizestr[0] == '-') {
658 mod = -1;
659 sizestr++;
660 } else if (sizestr[0] == '+') {
661 mod = 1;
662 sizestr++;
663 }
664 new_size = btrfs_parse_size(sizestr);
665 if (new_size == 0) {
666 ret = -EINVAL;
667 goto out_unlock;
668 }
669 }
670
671 old_size = device->total_bytes;
672
673 if (mod < 0) {
674 if (new_size > old_size) {
675 ret = -EINVAL;
676 goto out_unlock;
677 }
678 new_size = old_size - new_size;
679 } else if (mod > 0) {
680 new_size = old_size + new_size;
681 }
682
683 if (new_size < 256 * 1024 * 1024) {
684 ret = -EINVAL;
685 goto out_unlock;
686 }
687 if (new_size > device->bdev->bd_inode->i_size) {
688 ret = -EFBIG;
689 goto out_unlock;
690 }
691
692 do_div(new_size, root->sectorsize);
693 new_size *= root->sectorsize;
694
695 printk(KERN_INFO "new size for %s is %llu\n",
696 device->name, (unsigned long long)new_size);
697
698 if (new_size > old_size) {
699 trans = btrfs_start_transaction(root, 1);
700 ret = btrfs_grow_device(trans, device, new_size);
701 btrfs_commit_transaction(trans, root);
702 } else {
703 ret = btrfs_shrink_device(device, new_size);
704 }
705
706out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400707 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400708 kfree(vol_args);
709 return ret;
710}
711
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400712static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500713 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400714{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400715 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400716 struct btrfs_ioctl_vol_args *vol_args;
717 struct btrfs_dir_item *di;
718 struct btrfs_path *path;
Chris Mason3de45862008-11-17 21:02:50 -0500719 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400720 u64 root_dirid;
721 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500722 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400723
Yan Zhengc146afa2008-11-12 14:34:12 -0500724 if (root->fs_info->sb->s_flags & MS_RDONLY)
725 return -EROFS;
726
Li Zefandae7b662009-04-08 15:06:54 +0800727 vol_args = memdup_user(arg, sizeof(*vol_args));
728 if (IS_ERR(vol_args))
729 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400730
Mark Fasheh5516e592008-07-24 12:20:14 -0400731 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400733 if (strchr(vol_args->name, '/')) {
734 ret = -EINVAL;
735 goto out;
736 }
737
738 path = btrfs_alloc_path();
739 if (!path) {
740 ret = -ENOMEM;
741 goto out;
742 }
743
744 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
746 path, root_dirid,
747 vol_args->name, namelen, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748 btrfs_free_path(path);
749
750 if (di && !IS_ERR(di)) {
751 ret = -EEXIST;
752 goto out;
753 }
754
755 if (IS_ERR(di)) {
756 ret = PTR_ERR(di);
757 goto out;
758 }
759
Chris Mason3de45862008-11-17 21:02:50 -0500760 if (subvol) {
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400761 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
762 file->f_path.dentry->d_inode->i_mode,
Chris Mason3de45862008-11-17 21:02:50 -0500763 namelen, NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400764 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500765 struct inode *src_inode;
766 src_file = fget(vol_args->fd);
767 if (!src_file) {
768 ret = -EINVAL;
769 goto out;
770 }
771
772 src_inode = src_file->f_path.dentry->d_inode;
773 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500774 printk(KERN_INFO "btrfs: Snapshot src from "
775 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500776 ret = -EINVAL;
777 fput(src_file);
778 goto out;
779 }
780 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
781 file->f_path.dentry->d_inode->i_mode,
782 namelen, BTRFS_I(src_inode)->root);
783 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400784 }
785
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400786out:
787 kfree(vol_args);
788 return ret;
789}
790
791static int btrfs_ioctl_defrag(struct file *file)
792{
793 struct inode *inode = fdentry(file)->d_inode;
794 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500795 int ret;
796
797 ret = mnt_want_write(file->f_path.mnt);
798 if (ret)
799 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400800
801 switch (inode->i_mode & S_IFMT) {
802 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -0500803 if (!capable(CAP_SYS_ADMIN)) {
804 ret = -EPERM;
805 goto out;
806 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400807 btrfs_defrag_root(root, 0);
808 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400809 break;
810 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -0500811 if (!(file->f_mode & FMODE_WRITE)) {
812 ret = -EINVAL;
813 goto out;
814 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400815 btrfs_defrag_file(file);
816 break;
817 }
Chris Masone441d542009-01-05 16:57:23 -0500818out:
Yan Zhengab67b7c2008-12-19 10:58:39 -0500819 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -0500820 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400821}
822
Christoph Hellwigb2950862008-12-02 09:54:17 -0500823static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400824{
825 struct btrfs_ioctl_vol_args *vol_args;
826 int ret;
827
Chris Masone441d542009-01-05 16:57:23 -0500828 if (!capable(CAP_SYS_ADMIN))
829 return -EPERM;
830
Li Zefandae7b662009-04-08 15:06:54 +0800831 vol_args = memdup_user(arg, sizeof(*vol_args));
832 if (IS_ERR(vol_args))
833 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400834
Mark Fasheh5516e592008-07-24 12:20:14 -0400835 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400836 ret = btrfs_init_new_device(root, vol_args->name);
837
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400838 kfree(vol_args);
839 return ret;
840}
841
Christoph Hellwigb2950862008-12-02 09:54:17 -0500842static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400843{
844 struct btrfs_ioctl_vol_args *vol_args;
845 int ret;
846
Chris Masone441d542009-01-05 16:57:23 -0500847 if (!capable(CAP_SYS_ADMIN))
848 return -EPERM;
849
Yan Zhengc146afa2008-11-12 14:34:12 -0500850 if (root->fs_info->sb->s_flags & MS_RDONLY)
851 return -EROFS;
852
Li Zefandae7b662009-04-08 15:06:54 +0800853 vol_args = memdup_user(arg, sizeof(*vol_args));
854 if (IS_ERR(vol_args))
855 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400856
Mark Fasheh5516e592008-07-24 12:20:14 -0400857 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 ret = btrfs_rm_device(root, vol_args->name);
859
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400860 kfree(vol_args);
861 return ret;
862}
863
Christoph Hellwigb2950862008-12-02 09:54:17 -0500864static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
865 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400866{
867 struct inode *inode = fdentry(file)->d_inode;
868 struct btrfs_root *root = BTRFS_I(inode)->root;
869 struct file *src_file;
870 struct inode *src;
871 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400872 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400873 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400874 char *buf;
875 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400876 u32 nritems;
877 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400878 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500879 u64 len = olen;
880 u64 bs = root->fs_info->sb->s_blocksize;
881 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -0500882
Sage Weilc5c9cd42008-11-12 14:32:25 -0500883 /*
884 * TODO:
885 * - split compressed inline extents. annoying: we need to
886 * decompress into destination's address_space (the file offset
887 * may change, so source mapping won't do), then recompress (or
888 * otherwise reinsert) a subrange.
889 * - allow ranges within the same file to be cloned (provided
890 * they don't overlap)?
891 */
892
Chris Masone441d542009-01-05 16:57:23 -0500893 /* the destination must be opened for writing */
894 if (!(file->f_mode & FMODE_WRITE))
895 return -EINVAL;
896
Yan Zhengc146afa2008-11-12 14:34:12 -0500897 ret = mnt_want_write(file->f_path.mnt);
898 if (ret)
899 return ret;
900
Sage Weilc5c9cd42008-11-12 14:32:25 -0500901 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -0500902 if (!src_file) {
903 ret = -EBADF;
904 goto out_drop_write;
905 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400906 src = src_file->f_dentry->d_inode;
907
Sage Weilc5c9cd42008-11-12 14:32:25 -0500908 ret = -EINVAL;
909 if (src == inode)
910 goto out_fput;
911
Yan Zhengae01a0a2008-08-04 23:23:47 -0400912 ret = -EISDIR;
913 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400914 goto out_fput;
915
Yan Zhengae01a0a2008-08-04 23:23:47 -0400916 ret = -EXDEV;
917 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
918 goto out_fput;
919
920 ret = -ENOMEM;
921 buf = vmalloc(btrfs_level_size(root, 0));
922 if (!buf)
923 goto out_fput;
924
925 path = btrfs_alloc_path();
926 if (!path) {
927 vfree(buf);
928 goto out_fput;
929 }
930 path->reada = 2;
931
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400932 if (inode < src) {
933 mutex_lock(&inode->i_mutex);
934 mutex_lock(&src->i_mutex);
935 } else {
936 mutex_lock(&src->i_mutex);
937 mutex_lock(&inode->i_mutex);
938 }
939
Sage Weilc5c9cd42008-11-12 14:32:25 -0500940 /* determine range to clone */
941 ret = -EINVAL;
942 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400943 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500944 if (len == 0)
945 olen = len = src->i_size - off;
946 /* if we extend to eof, continue to block boundary */
947 if (off + len == src->i_size)
948 len = ((src->i_size + bs-1) & ~(bs-1))
949 - off;
950
951 /* verify the end result is block aligned */
952 if ((off & (bs-1)) ||
953 ((off + len) & (bs-1)))
954 goto out_unlock;
955
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400956 /* do any pending delalloc/csum calc on src, one way or
957 another, and lock file content */
958 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400959 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500960 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
961 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400962 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400963 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500964 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400965 if (ordered)
966 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500967 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400968 }
969
Yan Zhengae01a0a2008-08-04 23:23:47 -0400970 trans = btrfs_start_transaction(root, 1);
971 BUG_ON(!trans);
972
Sage Weilc5c9cd42008-11-12 14:32:25 -0500973 /* punch hole in destination first */
Chris Masone980b502009-04-24 14:39:24 -0400974 btrfs_drop_extents(trans, root, inode, off, off + len,
975 off + len, 0, &hint_byte);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500976
977 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400978 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400979 key.type = BTRFS_EXTENT_DATA_KEY;
980 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400981
982 while (1) {
983 /*
984 * note the key will change type as we walk through the
985 * tree.
986 */
987 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
988 if (ret < 0)
989 goto out;
990
Yan Zhengae01a0a2008-08-04 23:23:47 -0400991 nritems = btrfs_header_nritems(path->nodes[0]);
992 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400993 ret = btrfs_next_leaf(root, path);
994 if (ret < 0)
995 goto out;
996 if (ret > 0)
997 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400998 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400999 }
1000 leaf = path->nodes[0];
1001 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001002
Yan Zhengae01a0a2008-08-04 23:23:47 -04001003 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001004 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001005 key.objectid != src->i_ino)
1006 break;
1007
Sage Weilc5c9cd42008-11-12 14:32:25 -05001008 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1009 struct btrfs_file_extent_item *extent;
1010 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001011 u32 size;
1012 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001013 u64 disko = 0, diskl = 0;
1014 u64 datao = 0, datal = 0;
1015 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -04001016
1017 size = btrfs_item_size_nr(leaf, slot);
1018 read_extent_buffer(leaf, buf,
1019 btrfs_item_ptr_offset(leaf, slot),
1020 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001021
1022 extent = btrfs_item_ptr(leaf, slot,
1023 struct btrfs_file_extent_item);
1024 comp = btrfs_file_extent_compression(leaf, extent);
1025 type = btrfs_file_extent_type(leaf, extent);
1026 if (type == BTRFS_FILE_EXTENT_REG) {
Chris Masond3977122009-01-05 21:25:51 -05001027 disko = btrfs_file_extent_disk_bytenr(leaf,
1028 extent);
1029 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1030 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001031 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001032 datal = btrfs_file_extent_num_bytes(leaf,
1033 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001034 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1035 /* take upper bound, may be compressed */
1036 datal = btrfs_file_extent_ram_bytes(leaf,
1037 extent);
1038 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001039 btrfs_release_path(root, path);
1040
Sage Weilc5c9cd42008-11-12 14:32:25 -05001041 if (key.offset + datal < off ||
1042 key.offset >= off+len)
1043 goto next;
1044
Zheng Yan31840ae2008-09-23 13:14:14 -04001045 memcpy(&new_key, &key, sizeof(new_key));
1046 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001047 new_key.offset = key.offset + destoff - off;
1048
1049 if (type == BTRFS_FILE_EXTENT_REG) {
1050 ret = btrfs_insert_empty_item(trans, root, path,
1051 &new_key, size);
1052 if (ret)
1053 goto out;
1054
1055 leaf = path->nodes[0];
1056 slot = path->slots[0];
1057 write_extent_buffer(leaf, buf,
1058 btrfs_item_ptr_offset(leaf, slot),
1059 size);
1060
1061 extent = btrfs_item_ptr(leaf, slot,
1062 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001063
1064 if (off > key.offset) {
1065 datao += off - key.offset;
1066 datal -= off - key.offset;
1067 }
1068 if (key.offset + datao + datal + key.offset >
1069 off + len)
1070 datal = off + len - key.offset - datao;
1071 /* disko == 0 means it's a hole */
1072 if (!disko)
1073 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001074
1075 btrfs_set_file_extent_offset(leaf, extent,
1076 datao);
1077 btrfs_set_file_extent_num_bytes(leaf, extent,
1078 datal);
1079 if (disko) {
1080 inode_add_bytes(inode, datal);
1081 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 disko, diskl, 0,
1083 root->root_key.objectid,
1084 inode->i_ino,
1085 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001086 BUG_ON(ret);
1087 }
1088 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1089 u64 skip = 0;
1090 u64 trim = 0;
1091 if (off > key.offset) {
1092 skip = off - key.offset;
1093 new_key.offset += skip;
1094 }
Chris Masond3977122009-01-05 21:25:51 -05001095
Sage Weilc5c9cd42008-11-12 14:32:25 -05001096 if (key.offset + datal > off+len)
1097 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001098
Sage Weilc5c9cd42008-11-12 14:32:25 -05001099 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001100 ret = -EINVAL;
1101 goto out;
1102 }
1103 size -= skip + trim;
1104 datal -= skip + trim;
1105 ret = btrfs_insert_empty_item(trans, root, path,
1106 &new_key, size);
1107 if (ret)
1108 goto out;
1109
1110 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001111 u32 start =
1112 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001113 memmove(buf+start, buf+start+skip,
1114 datal);
1115 }
1116
1117 leaf = path->nodes[0];
1118 slot = path->slots[0];
1119 write_extent_buffer(leaf, buf,
1120 btrfs_item_ptr_offset(leaf, slot),
1121 size);
1122 inode_add_bytes(inode, datal);
1123 }
1124
1125 btrfs_mark_buffer_dirty(leaf);
1126 }
1127
Chris Masond3977122009-01-05 21:25:51 -05001128next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001129 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001130 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001131 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001132 ret = 0;
1133out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001134 btrfs_release_path(root, path);
1135 if (ret == 0) {
1136 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001137 if (destoff + olen > inode->i_size)
1138 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001139 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1140 ret = btrfs_update_inode(trans, root, inode);
1141 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001142 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001143 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001144 if (ret)
1145 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001146out_unlock:
1147 mutex_unlock(&src->i_mutex);
1148 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001149 vfree(buf);
1150 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001151out_fput:
1152 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001153out_drop_write:
1154 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001155 return ret;
1156}
1157
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001158static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001159{
1160 struct btrfs_ioctl_clone_range_args args;
1161
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001162 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001163 return -EFAULT;
1164 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1165 args.src_length, args.dest_offset);
1166}
1167
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001168/*
1169 * there are many ways the trans_start and trans_end ioctls can lead
1170 * to deadlocks. They should only be used by applications that
1171 * basically own the machine, and have a very in depth understanding
1172 * of all the possible deadlocks and enospc problems.
1173 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001174static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001175{
1176 struct inode *inode = fdentry(file)->d_inode;
1177 struct btrfs_root *root = BTRFS_I(inode)->root;
1178 struct btrfs_trans_handle *trans;
1179 int ret = 0;
1180
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001181 if (!capable(CAP_SYS_ADMIN))
1182 return -EPERM;
1183
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001184 if (file->private_data) {
1185 ret = -EINPROGRESS;
1186 goto out;
1187 }
Sage Weil9ca9ee02008-08-04 10:41:27 -04001188
Yan Zhengc146afa2008-11-12 14:34:12 -05001189 ret = mnt_want_write(file->f_path.mnt);
1190 if (ret)
1191 goto out;
1192
Sage Weil9ca9ee02008-08-04 10:41:27 -04001193 mutex_lock(&root->fs_info->trans_mutex);
1194 root->fs_info->open_ioctl_trans++;
1195 mutex_unlock(&root->fs_info->trans_mutex);
1196
1197 trans = btrfs_start_ioctl_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001198 if (trans)
1199 file->private_data = trans;
1200 else
1201 ret = -ENOMEM;
1202 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1203out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001204 return ret;
1205}
1206
1207/*
1208 * there are many ways the trans_start and trans_end ioctls can lead
1209 * to deadlocks. They should only be used by applications that
1210 * basically own the machine, and have a very in depth understanding
1211 * of all the possible deadlocks and enospc problems.
1212 */
1213long btrfs_ioctl_trans_end(struct file *file)
1214{
1215 struct inode *inode = fdentry(file)->d_inode;
1216 struct btrfs_root *root = BTRFS_I(inode)->root;
1217 struct btrfs_trans_handle *trans;
1218 int ret = 0;
1219
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001220 trans = file->private_data;
1221 if (!trans) {
1222 ret = -EINVAL;
1223 goto out;
1224 }
1225 btrfs_end_transaction(trans, root);
Christoph Hellwigb2141072008-09-05 16:43:31 -04001226 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001227
1228 mutex_lock(&root->fs_info->trans_mutex);
1229 root->fs_info->open_ioctl_trans--;
1230 mutex_unlock(&root->fs_info->trans_mutex);
1231
Sage Weilcfc8ea82008-12-11 16:30:06 -05001232 mnt_drop_write(file->f_path.mnt);
1233
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001234out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001235 return ret;
1236}
1237
1238long btrfs_ioctl(struct file *file, unsigned int
1239 cmd, unsigned long arg)
1240{
1241 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001242 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001243
1244 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001245 case FS_IOC_GETFLAGS:
1246 return btrfs_ioctl_getflags(file, argp);
1247 case FS_IOC_SETFLAGS:
1248 return btrfs_ioctl_setflags(file, argp);
1249 case FS_IOC_GETVERSION:
1250 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001251 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001252 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001253 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001254 return btrfs_ioctl_snap_create(file, argp, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001255 case BTRFS_IOC_DEFRAG:
1256 return btrfs_ioctl_defrag(file);
1257 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001258 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001259 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001260 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001261 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001262 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001263 case BTRFS_IOC_BALANCE:
1264 return btrfs_balance(root->fs_info->dev_root);
1265 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001266 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1267 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001268 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001269 case BTRFS_IOC_TRANS_START:
1270 return btrfs_ioctl_trans_start(file);
1271 case BTRFS_IOC_TRANS_END:
1272 return btrfs_ioctl_trans_end(file);
1273 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001274 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1275 return 0;
1276 }
1277
1278 return -ENOTTY;
1279}