blob: e2d8e90259b05c2499a1f9cbc54ac9cf529ecbe0 [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{
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,
227 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400228{
229 struct btrfs_trans_handle *trans;
230 struct btrfs_key key;
231 struct btrfs_root_item root_item;
232 struct btrfs_inode_item *inode_item;
233 struct extent_buffer *leaf;
234 struct btrfs_root *new_root = root;
235 struct inode *dir;
236 int ret;
237 int err;
238 u64 objectid;
239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500240 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400241 unsigned long nr = 1;
242
Josef Bacik6a632092009-02-20 11:00:09 -0500243 ret = btrfs_check_metadata_free_space(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400244 if (ret)
245 goto fail_commit;
246
247 trans = btrfs_start_transaction(root, 1);
248 BUG_ON(!trans);
249
250 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
251 0, &objectid);
252 if (ret)
253 goto fail;
254
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
256 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400257 if (IS_ERR(leaf)) {
258 ret = PTR_ERR(leaf);
259 goto fail;
260 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400261
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400262 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400263 btrfs_set_header_bytenr(leaf, leaf->start);
264 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400266 btrfs_set_header_owner(leaf, objectid);
267
268 write_extent_buffer(leaf, root->fs_info->fsid,
269 (unsigned long)btrfs_header_fsid(leaf),
270 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400271 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
272 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
273 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400274 btrfs_mark_buffer_dirty(leaf);
275
276 inode_item = &root_item.inode;
277 memset(inode_item, 0, sizeof(*inode_item));
278 inode_item->generation = cpu_to_le64(1);
279 inode_item->size = cpu_to_le64(3);
280 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400281 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400282 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
283
284 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400285 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400286 btrfs_set_root_level(&root_item, 0);
287 btrfs_set_root_refs(&root_item, 1);
288 btrfs_set_root_used(&root_item, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400289 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400290
291 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
292 root_item.drop_level = 0;
293
Chris Mason925baed2008-06-25 16:01:30 -0400294 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400295 free_extent_buffer(leaf);
296 leaf = NULL;
297
298 btrfs_set_root_dirid(&root_item, new_dirid);
299
300 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400301 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400302 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
303 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
304 &root_item);
305 if (ret)
306 goto fail;
307
308 /*
309 * insert the directory item
310 */
311 key.offset = (u64)-1;
Chris Mason3de45862008-11-17 21:02:50 -0500312 dir = dentry->d_parent->d_inode;
313 ret = btrfs_set_inode_index(dir, &index);
314 BUG_ON(ret);
315
316 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400317 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500318 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400319 if (ret)
320 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500321
Yan Zheng52c26172009-01-05 15:43:43 -0500322 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
323 ret = btrfs_update_inode(trans, root, dir);
324 BUG_ON(ret);
325
Chris Mason0660b5a2008-11-17 20:37:39 -0500326 /* add the backref first */
327 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
328 objectid, BTRFS_ROOT_BACKREF_KEY,
329 root->root_key.objectid,
330 dir->i_ino, index, name, namelen);
331
332 BUG_ON(ret);
333
334 /* now add the forward ref */
335 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
336 root->root_key.objectid, BTRFS_ROOT_REF_KEY,
337 objectid,
338 dir->i_ino, index, name, namelen);
339
340 BUG_ON(ret);
341
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400342 ret = btrfs_commit_transaction(trans, root);
343 if (ret)
344 goto fail_commit;
345
Chris Mason3de45862008-11-17 21:02:50 -0500346 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400347 BUG_ON(!new_root);
348
349 trans = btrfs_start_transaction(new_root, 1);
350 BUG_ON(!trans);
351
Yan Zhengd2fb3432008-12-11 16:30:39 -0500352 ret = btrfs_create_subvol_root(trans, new_root, dentry, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353 BTRFS_I(dir)->block_group);
354 if (ret)
355 goto fail;
356
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400357fail:
358 nr = trans->blocks_used;
359 err = btrfs_commit_transaction(trans, new_root);
360 if (err && !ret)
361 ret = err;
362fail_commit:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400363 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400364 return ret;
365}
366
Chris Mason3de45862008-11-17 21:02:50 -0500367static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
368 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400369{
370 struct btrfs_pending_snapshot *pending_snapshot;
371 struct btrfs_trans_handle *trans;
Chris Mason3de45862008-11-17 21:02:50 -0500372 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400373 int err;
374 unsigned long nr = 0;
375
376 if (!root->ref_cows)
377 return -EINVAL;
378
Josef Bacik6a632092009-02-20 11:00:09 -0500379 ret = btrfs_check_metadata_free_space(root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 if (ret)
381 goto fail_unlock;
382
Chris Mason3de45862008-11-17 21:02:50 -0500383 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400384 if (!pending_snapshot) {
385 ret = -ENOMEM;
386 goto fail_unlock;
387 }
388 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
389 if (!pending_snapshot->name) {
390 ret = -ENOMEM;
391 kfree(pending_snapshot);
392 goto fail_unlock;
393 }
394 memcpy(pending_snapshot->name, name, namelen);
395 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500396 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 trans = btrfs_start_transaction(root, 1);
398 BUG_ON(!trans);
399 pending_snapshot->root = root;
400 list_add(&pending_snapshot->list,
401 &trans->transaction->pending_snapshots);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402 err = btrfs_commit_transaction(trans, root);
403
404fail_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400405 btrfs_btree_balance_dirty(root, nr);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400406 return ret;
407}
408
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400409/* copy of may_create in fs/namei.c() */
410static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
411{
412 if (child->d_inode)
413 return -EEXIST;
414 if (IS_DEADDIR(dir))
415 return -ENOENT;
416 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
417}
418
419/*
420 * Create a new subvolume below @parent. This is largely modeled after
421 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
422 * inside this filesystem so it's quite a bit simpler.
423 */
424static noinline int btrfs_mksubvol(struct path *parent, char *name,
Chris Mason3de45862008-11-17 21:02:50 -0500425 int mode, int namelen,
426 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400427{
428 struct dentry *dentry;
429 int error;
430
431 mutex_lock_nested(&parent->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
432
433 dentry = lookup_one_len(name, parent->dentry, namelen);
434 error = PTR_ERR(dentry);
435 if (IS_ERR(dentry))
436 goto out_unlock;
437
438 error = -EEXIST;
439 if (dentry->d_inode)
440 goto out_dput;
441
442 if (!IS_POSIXACL(parent->dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -0400443 mode &= ~current_umask();
Chris Mason3de45862008-11-17 21:02:50 -0500444
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400445 error = mnt_want_write(parent->mnt);
446 if (error)
447 goto out_dput;
448
449 error = btrfs_may_create(parent->dentry->d_inode, dentry);
450 if (error)
451 goto out_drop_write;
452
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400453 /*
454 * Actually perform the low-level subvolume creation after all
455 * this VFS fuzz.
456 *
457 * Eventually we want to pass in an inode under which we create this
458 * subvolume, but for now all are under the filesystem root.
459 *
460 * Also we should pass on the mode eventually to allow creating new
461 * subvolume with specific mode bits.
462 */
Chris Mason3de45862008-11-17 21:02:50 -0500463 if (snap_src) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500464 struct dentry *dir = dentry->d_parent;
465 struct dentry *test = dir->d_parent;
466 struct btrfs_path *path = btrfs_alloc_path();
467 int ret;
468 u64 test_oid;
469 u64 parent_oid = BTRFS_I(dir->d_inode)->root->root_key.objectid;
470
471 test_oid = snap_src->root_key.objectid;
472
473 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
474 path, parent_oid, test_oid);
475 if (ret == 0)
476 goto create;
477 btrfs_release_path(snap_src->fs_info->tree_root, path);
478
479 /* we need to make sure we aren't creating a directory loop
480 * by taking a snapshot of something that has our current
481 * subvol in its directory tree. So, this loops through
482 * the dentries and checks the forward refs for each subvolume
483 * to see if is references the subvolume where we are
484 * placing this new snapshot.
485 */
Chris Masond3977122009-01-05 21:25:51 -0500486 while (1) {
Chris Masonea9e8b12008-11-17 21:14:24 -0500487 if (!test ||
488 dir == snap_src->fs_info->sb->s_root ||
489 test == snap_src->fs_info->sb->s_root ||
490 test->d_inode->i_sb != snap_src->fs_info->sb) {
491 break;
492 }
493 if (S_ISLNK(test->d_inode->i_mode)) {
Chris Masond3977122009-01-05 21:25:51 -0500494 printk(KERN_INFO "Btrfs symlink in snapshot "
495 "path, failed\n");
Chris Masonea9e8b12008-11-17 21:14:24 -0500496 error = -EMLINK;
497 btrfs_free_path(path);
498 goto out_drop_write;
499 }
500 test_oid =
501 BTRFS_I(test->d_inode)->root->root_key.objectid;
502 ret = btrfs_find_root_ref(snap_src->fs_info->tree_root,
503 path, test_oid, parent_oid);
504 if (ret == 0) {
Chris Masond3977122009-01-05 21:25:51 -0500505 printk(KERN_INFO "Btrfs snapshot creation "
506 "failed, looping\n");
Chris Masonea9e8b12008-11-17 21:14:24 -0500507 error = -EMLINK;
508 btrfs_free_path(path);
509 goto out_drop_write;
510 }
511 btrfs_release_path(snap_src->fs_info->tree_root, path);
512 test = test->d_parent;
513 }
514create:
515 btrfs_free_path(path);
Chris Mason3de45862008-11-17 21:02:50 -0500516 error = create_snapshot(snap_src, dentry, name, namelen);
517 } else {
518 error = create_subvol(BTRFS_I(parent->dentry->d_inode)->root,
519 dentry, name, namelen);
520 }
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400521 if (error)
522 goto out_drop_write;
523
524 fsnotify_mkdir(parent->dentry->d_inode, dentry);
525out_drop_write:
526 mnt_drop_write(parent->mnt);
527out_dput:
528 dput(dentry);
529out_unlock:
530 mutex_unlock(&parent->dentry->d_inode->i_mutex);
531 return error;
532}
533
534
Christoph Hellwigb2950862008-12-02 09:54:17 -0500535static int btrfs_defrag_file(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400536{
537 struct inode *inode = fdentry(file)->d_inode;
538 struct btrfs_root *root = BTRFS_I(inode)->root;
539 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400540 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400541 struct page *page;
542 unsigned long last_index;
543 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
544 unsigned long total_read = 0;
545 u64 page_start;
546 u64 page_end;
547 unsigned long i;
548 int ret;
549
Josef Bacik6a632092009-02-20 11:00:09 -0500550 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400551 if (ret)
552 return -ENOSPC;
553
554 mutex_lock(&inode->i_mutex);
555 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
556 for (i = 0; i <= last_index; i++) {
557 if (total_read % ra_pages == 0) {
558 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
559 min(last_index, i + ra_pages - 1));
560 }
561 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400562again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400563 page = grab_cache_page(inode->i_mapping, i);
564 if (!page)
565 goto out_unlock;
566 if (!PageUptodate(page)) {
567 btrfs_readpage(NULL, page);
568 lock_page(page);
569 if (!PageUptodate(page)) {
570 unlock_page(page);
571 page_cache_release(page);
572 goto out_unlock;
573 }
574 }
575
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400576 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400577
578 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
579 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400581
582 ordered = btrfs_lookup_ordered_extent(inode, page_start);
583 if (ordered) {
584 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
585 unlock_page(page);
586 page_cache_release(page);
587 btrfs_start_ordered_extent(inode, ordered, 1);
588 btrfs_put_ordered_extent(ordered);
589 goto again;
590 }
591 set_page_extent_mapped(page);
592
Chris Masonf87f0572008-08-01 11:27:23 -0400593 /*
594 * this makes sure page_mkwrite is called on the
595 * page if it is dirtied again later
596 */
597 clear_page_dirty_for_io(page);
598
Chris Masonea8c2812008-08-04 23:17:27 -0400599 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400600 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400601 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400602 unlock_page(page);
603 page_cache_release(page);
604 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
605 }
606
607out_unlock:
608 mutex_unlock(&inode->i_mutex);
609 return 0;
610}
611
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400612static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
613{
614 u64 new_size;
615 u64 old_size;
616 u64 devid = 1;
617 struct btrfs_ioctl_vol_args *vol_args;
618 struct btrfs_trans_handle *trans;
619 struct btrfs_device *device = NULL;
620 char *sizestr;
621 char *devstr = NULL;
622 int ret = 0;
623 int namelen;
624 int mod = 0;
625
Yan Zhengc146afa2008-11-12 14:34:12 -0500626 if (root->fs_info->sb->s_flags & MS_RDONLY)
627 return -EROFS;
628
Chris Masone441d542009-01-05 16:57:23 -0500629 if (!capable(CAP_SYS_ADMIN))
630 return -EPERM;
631
Li Zefandae7b662009-04-08 15:06:54 +0800632 vol_args = memdup_user(arg, sizeof(*vol_args));
633 if (IS_ERR(vol_args))
634 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400635
636 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400637 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400638
Chris Mason7d9eb122008-07-08 14:19:17 -0400639 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400640 sizestr = vol_args->name;
641 devstr = strchr(sizestr, ':');
642 if (devstr) {
643 char *end;
644 sizestr = devstr + 1;
645 *devstr = '\0';
646 devstr = vol_args->name;
647 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700648 printk(KERN_INFO "resizing devid %llu\n",
649 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400650 }
Yan Zheng2b820322008-11-17 21:11:30 -0500651 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400652 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700653 printk(KERN_INFO "resizer unable to find device %llu\n",
654 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400655 ret = -EINVAL;
656 goto out_unlock;
657 }
658 if (!strcmp(sizestr, "max"))
659 new_size = device->bdev->bd_inode->i_size;
660 else {
661 if (sizestr[0] == '-') {
662 mod = -1;
663 sizestr++;
664 } else if (sizestr[0] == '+') {
665 mod = 1;
666 sizestr++;
667 }
668 new_size = btrfs_parse_size(sizestr);
669 if (new_size == 0) {
670 ret = -EINVAL;
671 goto out_unlock;
672 }
673 }
674
675 old_size = device->total_bytes;
676
677 if (mod < 0) {
678 if (new_size > old_size) {
679 ret = -EINVAL;
680 goto out_unlock;
681 }
682 new_size = old_size - new_size;
683 } else if (mod > 0) {
684 new_size = old_size + new_size;
685 }
686
687 if (new_size < 256 * 1024 * 1024) {
688 ret = -EINVAL;
689 goto out_unlock;
690 }
691 if (new_size > device->bdev->bd_inode->i_size) {
692 ret = -EFBIG;
693 goto out_unlock;
694 }
695
696 do_div(new_size, root->sectorsize);
697 new_size *= root->sectorsize;
698
699 printk(KERN_INFO "new size for %s is %llu\n",
700 device->name, (unsigned long long)new_size);
701
702 if (new_size > old_size) {
703 trans = btrfs_start_transaction(root, 1);
704 ret = btrfs_grow_device(trans, device, new_size);
705 btrfs_commit_transaction(trans, root);
706 } else {
707 ret = btrfs_shrink_device(device, new_size);
708 }
709
710out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400711 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400712 kfree(vol_args);
713 return ret;
714}
715
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400716static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500717 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400718{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400719 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400720 struct btrfs_ioctl_vol_args *vol_args;
721 struct btrfs_dir_item *di;
722 struct btrfs_path *path;
Chris Mason3de45862008-11-17 21:02:50 -0500723 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400724 u64 root_dirid;
725 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500726 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400727
Yan Zhengc146afa2008-11-12 14:34:12 -0500728 if (root->fs_info->sb->s_flags & MS_RDONLY)
729 return -EROFS;
730
Li Zefandae7b662009-04-08 15:06:54 +0800731 vol_args = memdup_user(arg, sizeof(*vol_args));
732 if (IS_ERR(vol_args))
733 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400734
Mark Fasheh5516e592008-07-24 12:20:14 -0400735 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400736 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400737 if (strchr(vol_args->name, '/')) {
738 ret = -EINVAL;
739 goto out;
740 }
741
742 path = btrfs_alloc_path();
743 if (!path) {
744 ret = -ENOMEM;
745 goto out;
746 }
747
748 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400749 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
750 path, root_dirid,
751 vol_args->name, namelen, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400752 btrfs_free_path(path);
753
754 if (di && !IS_ERR(di)) {
755 ret = -EEXIST;
756 goto out;
757 }
758
759 if (IS_ERR(di)) {
760 ret = PTR_ERR(di);
761 goto out;
762 }
763
Chris Mason3de45862008-11-17 21:02:50 -0500764 if (subvol) {
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400765 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
766 file->f_path.dentry->d_inode->i_mode,
Chris Mason3de45862008-11-17 21:02:50 -0500767 namelen, NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400768 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500769 struct inode *src_inode;
770 src_file = fget(vol_args->fd);
771 if (!src_file) {
772 ret = -EINVAL;
773 goto out;
774 }
775
776 src_inode = src_file->f_path.dentry->d_inode;
777 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500778 printk(KERN_INFO "btrfs: Snapshot src from "
779 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500780 ret = -EINVAL;
781 fput(src_file);
782 goto out;
783 }
784 ret = btrfs_mksubvol(&file->f_path, vol_args->name,
785 file->f_path.dentry->d_inode->i_mode,
786 namelen, BTRFS_I(src_inode)->root);
787 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400788 }
789
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400790out:
791 kfree(vol_args);
792 return ret;
793}
794
795static int btrfs_ioctl_defrag(struct file *file)
796{
797 struct inode *inode = fdentry(file)->d_inode;
798 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500799 int ret;
800
801 ret = mnt_want_write(file->f_path.mnt);
802 if (ret)
803 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400804
805 switch (inode->i_mode & S_IFMT) {
806 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -0500807 if (!capable(CAP_SYS_ADMIN)) {
808 ret = -EPERM;
809 goto out;
810 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400811 btrfs_defrag_root(root, 0);
812 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400813 break;
814 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -0500815 if (!(file->f_mode & FMODE_WRITE)) {
816 ret = -EINVAL;
817 goto out;
818 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400819 btrfs_defrag_file(file);
820 break;
821 }
Chris Masone441d542009-01-05 16:57:23 -0500822out:
Yan Zhengab67b7c2008-12-19 10:58:39 -0500823 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -0500824 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400825}
826
Christoph Hellwigb2950862008-12-02 09:54:17 -0500827static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400828{
829 struct btrfs_ioctl_vol_args *vol_args;
830 int ret;
831
Chris Masone441d542009-01-05 16:57:23 -0500832 if (!capable(CAP_SYS_ADMIN))
833 return -EPERM;
834
Li Zefandae7b662009-04-08 15:06:54 +0800835 vol_args = memdup_user(arg, sizeof(*vol_args));
836 if (IS_ERR(vol_args))
837 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400838
Mark Fasheh5516e592008-07-24 12:20:14 -0400839 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400840 ret = btrfs_init_new_device(root, vol_args->name);
841
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400842 kfree(vol_args);
843 return ret;
844}
845
Christoph Hellwigb2950862008-12-02 09:54:17 -0500846static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400847{
848 struct btrfs_ioctl_vol_args *vol_args;
849 int ret;
850
Chris Masone441d542009-01-05 16:57:23 -0500851 if (!capable(CAP_SYS_ADMIN))
852 return -EPERM;
853
Yan Zhengc146afa2008-11-12 14:34:12 -0500854 if (root->fs_info->sb->s_flags & MS_RDONLY)
855 return -EROFS;
856
Li Zefandae7b662009-04-08 15:06:54 +0800857 vol_args = memdup_user(arg, sizeof(*vol_args));
858 if (IS_ERR(vol_args))
859 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400860
Mark Fasheh5516e592008-07-24 12:20:14 -0400861 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400862 ret = btrfs_rm_device(root, vol_args->name);
863
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400864 kfree(vol_args);
865 return ret;
866}
867
Christoph Hellwigb2950862008-12-02 09:54:17 -0500868static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
869 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400870{
871 struct inode *inode = fdentry(file)->d_inode;
872 struct btrfs_root *root = BTRFS_I(inode)->root;
873 struct file *src_file;
874 struct inode *src;
875 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400876 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400877 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400878 char *buf;
879 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400880 u32 nritems;
881 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400882 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500883 u64 len = olen;
884 u64 bs = root->fs_info->sb->s_blocksize;
885 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -0500886
Sage Weilc5c9cd42008-11-12 14:32:25 -0500887 /*
888 * TODO:
889 * - split compressed inline extents. annoying: we need to
890 * decompress into destination's address_space (the file offset
891 * may change, so source mapping won't do), then recompress (or
892 * otherwise reinsert) a subrange.
893 * - allow ranges within the same file to be cloned (provided
894 * they don't overlap)?
895 */
896
Chris Masone441d542009-01-05 16:57:23 -0500897 /* the destination must be opened for writing */
898 if (!(file->f_mode & FMODE_WRITE))
899 return -EINVAL;
900
Yan Zhengc146afa2008-11-12 14:34:12 -0500901 ret = mnt_want_write(file->f_path.mnt);
902 if (ret)
903 return ret;
904
Sage Weilc5c9cd42008-11-12 14:32:25 -0500905 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -0500906 if (!src_file) {
907 ret = -EBADF;
908 goto out_drop_write;
909 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400910 src = src_file->f_dentry->d_inode;
911
Sage Weilc5c9cd42008-11-12 14:32:25 -0500912 ret = -EINVAL;
913 if (src == inode)
914 goto out_fput;
915
Yan Zhengae01a0a2008-08-04 23:23:47 -0400916 ret = -EISDIR;
917 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400918 goto out_fput;
919
Yan Zhengae01a0a2008-08-04 23:23:47 -0400920 ret = -EXDEV;
921 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
922 goto out_fput;
923
924 ret = -ENOMEM;
925 buf = vmalloc(btrfs_level_size(root, 0));
926 if (!buf)
927 goto out_fput;
928
929 path = btrfs_alloc_path();
930 if (!path) {
931 vfree(buf);
932 goto out_fput;
933 }
934 path->reada = 2;
935
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400936 if (inode < src) {
937 mutex_lock(&inode->i_mutex);
938 mutex_lock(&src->i_mutex);
939 } else {
940 mutex_lock(&src->i_mutex);
941 mutex_lock(&inode->i_mutex);
942 }
943
Sage Weilc5c9cd42008-11-12 14:32:25 -0500944 /* determine range to clone */
945 ret = -EINVAL;
946 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400947 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500948 if (len == 0)
949 olen = len = src->i_size - off;
950 /* if we extend to eof, continue to block boundary */
951 if (off + len == src->i_size)
952 len = ((src->i_size + bs-1) & ~(bs-1))
953 - off;
954
955 /* verify the end result is block aligned */
956 if ((off & (bs-1)) ||
957 ((off + len) & (bs-1)))
958 goto out_unlock;
959
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400960 /* do any pending delalloc/csum calc on src, one way or
961 another, and lock file content */
962 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400963 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500964 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
965 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400966 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400967 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500968 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -0400969 if (ordered)
970 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500971 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400972 }
973
Yan Zhengae01a0a2008-08-04 23:23:47 -0400974 trans = btrfs_start_transaction(root, 1);
975 BUG_ON(!trans);
976
Sage Weilc5c9cd42008-11-12 14:32:25 -0500977 /* punch hole in destination first */
Chris Masone980b502009-04-24 14:39:24 -0400978 btrfs_drop_extents(trans, root, inode, off, off + len,
Chris Masona1ed8352009-09-11 12:27:37 -0400979 off + len, 0, &hint_byte, 1);
Sage Weilc5c9cd42008-11-12 14:32:25 -0500980
981 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400982 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400983 key.type = BTRFS_EXTENT_DATA_KEY;
984 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400985
986 while (1) {
987 /*
988 * note the key will change type as we walk through the
989 * tree.
990 */
991 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
992 if (ret < 0)
993 goto out;
994
Yan Zhengae01a0a2008-08-04 23:23:47 -0400995 nritems = btrfs_header_nritems(path->nodes[0]);
996 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400997 ret = btrfs_next_leaf(root, path);
998 if (ret < 0)
999 goto out;
1000 if (ret > 0)
1001 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001002 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001003 }
1004 leaf = path->nodes[0];
1005 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006
Yan Zhengae01a0a2008-08-04 23:23:47 -04001007 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001008 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001009 key.objectid != src->i_ino)
1010 break;
1011
Sage Weilc5c9cd42008-11-12 14:32:25 -05001012 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1013 struct btrfs_file_extent_item *extent;
1014 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001015 u32 size;
1016 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001017 u64 disko = 0, diskl = 0;
1018 u64 datao = 0, datal = 0;
1019 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -04001020
1021 size = btrfs_item_size_nr(leaf, slot);
1022 read_extent_buffer(leaf, buf,
1023 btrfs_item_ptr_offset(leaf, slot),
1024 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001025
1026 extent = btrfs_item_ptr(leaf, slot,
1027 struct btrfs_file_extent_item);
1028 comp = btrfs_file_extent_compression(leaf, extent);
1029 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001030 if (type == BTRFS_FILE_EXTENT_REG ||
1031 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001032 disko = btrfs_file_extent_disk_bytenr(leaf,
1033 extent);
1034 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1035 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001036 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001037 datal = btrfs_file_extent_num_bytes(leaf,
1038 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001039 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1040 /* take upper bound, may be compressed */
1041 datal = btrfs_file_extent_ram_bytes(leaf,
1042 extent);
1043 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001044 btrfs_release_path(root, path);
1045
Sage Weilc5c9cd42008-11-12 14:32:25 -05001046 if (key.offset + datal < off ||
1047 key.offset >= off+len)
1048 goto next;
1049
Zheng Yan31840ae2008-09-23 13:14:14 -04001050 memcpy(&new_key, &key, sizeof(new_key));
1051 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001052 new_key.offset = key.offset + destoff - off;
1053
Chris Masonc8a894d2009-06-27 21:07:03 -04001054 if (type == BTRFS_FILE_EXTENT_REG ||
1055 type == BTRFS_FILE_EXTENT_PREALLOC) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001056 ret = btrfs_insert_empty_item(trans, root, path,
1057 &new_key, size);
1058 if (ret)
1059 goto out;
1060
1061 leaf = path->nodes[0];
1062 slot = path->slots[0];
1063 write_extent_buffer(leaf, buf,
1064 btrfs_item_ptr_offset(leaf, slot),
1065 size);
1066
1067 extent = btrfs_item_ptr(leaf, slot,
1068 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001069
1070 if (off > key.offset) {
1071 datao += off - key.offset;
1072 datal -= off - key.offset;
1073 }
1074 if (key.offset + datao + datal + key.offset >
1075 off + len)
1076 datal = off + len - key.offset - datao;
1077 /* disko == 0 means it's a hole */
1078 if (!disko)
1079 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001080
1081 btrfs_set_file_extent_offset(leaf, extent,
1082 datao);
1083 btrfs_set_file_extent_num_bytes(leaf, extent,
1084 datal);
1085 if (disko) {
1086 inode_add_bytes(inode, datal);
1087 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088 disko, diskl, 0,
1089 root->root_key.objectid,
1090 inode->i_ino,
1091 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001092 BUG_ON(ret);
1093 }
1094 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1095 u64 skip = 0;
1096 u64 trim = 0;
1097 if (off > key.offset) {
1098 skip = off - key.offset;
1099 new_key.offset += skip;
1100 }
Chris Masond3977122009-01-05 21:25:51 -05001101
Sage Weilc5c9cd42008-11-12 14:32:25 -05001102 if (key.offset + datal > off+len)
1103 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001104
Sage Weilc5c9cd42008-11-12 14:32:25 -05001105 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001106 ret = -EINVAL;
1107 goto out;
1108 }
1109 size -= skip + trim;
1110 datal -= skip + trim;
1111 ret = btrfs_insert_empty_item(trans, root, path,
1112 &new_key, size);
1113 if (ret)
1114 goto out;
1115
1116 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001117 u32 start =
1118 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001119 memmove(buf+start, buf+start+skip,
1120 datal);
1121 }
1122
1123 leaf = path->nodes[0];
1124 slot = path->slots[0];
1125 write_extent_buffer(leaf, buf,
1126 btrfs_item_ptr_offset(leaf, slot),
1127 size);
1128 inode_add_bytes(inode, datal);
1129 }
1130
1131 btrfs_mark_buffer_dirty(leaf);
1132 }
1133
Chris Masond3977122009-01-05 21:25:51 -05001134next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001135 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001136 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001137 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001138 ret = 0;
1139out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001140 btrfs_release_path(root, path);
1141 if (ret == 0) {
1142 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001143 if (destoff + olen > inode->i_size)
1144 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001145 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1146 ret = btrfs_update_inode(trans, root, inode);
1147 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001148 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001149 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001150 if (ret)
1151 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001152out_unlock:
1153 mutex_unlock(&src->i_mutex);
1154 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001155 vfree(buf);
1156 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001157out_fput:
1158 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001159out_drop_write:
1160 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001161 return ret;
1162}
1163
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001164static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001165{
1166 struct btrfs_ioctl_clone_range_args args;
1167
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001168 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001169 return -EFAULT;
1170 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1171 args.src_length, args.dest_offset);
1172}
1173
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001174/*
1175 * there are many ways the trans_start and trans_end ioctls can lead
1176 * to deadlocks. They should only be used by applications that
1177 * basically own the machine, and have a very in depth understanding
1178 * of all the possible deadlocks and enospc problems.
1179 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001180static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001181{
1182 struct inode *inode = fdentry(file)->d_inode;
1183 struct btrfs_root *root = BTRFS_I(inode)->root;
1184 struct btrfs_trans_handle *trans;
1185 int ret = 0;
1186
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001187 if (!capable(CAP_SYS_ADMIN))
1188 return -EPERM;
1189
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001190 if (file->private_data) {
1191 ret = -EINPROGRESS;
1192 goto out;
1193 }
Sage Weil9ca9ee02008-08-04 10:41:27 -04001194
Yan Zhengc146afa2008-11-12 14:34:12 -05001195 ret = mnt_want_write(file->f_path.mnt);
1196 if (ret)
1197 goto out;
1198
Sage Weil9ca9ee02008-08-04 10:41:27 -04001199 mutex_lock(&root->fs_info->trans_mutex);
1200 root->fs_info->open_ioctl_trans++;
1201 mutex_unlock(&root->fs_info->trans_mutex);
1202
1203 trans = btrfs_start_ioctl_transaction(root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001204 if (trans)
1205 file->private_data = trans;
1206 else
1207 ret = -ENOMEM;
1208 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1209out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001210 return ret;
1211}
1212
1213/*
1214 * there are many ways the trans_start and trans_end ioctls can lead
1215 * to deadlocks. They should only be used by applications that
1216 * basically own the machine, and have a very in depth understanding
1217 * of all the possible deadlocks and enospc problems.
1218 */
1219long btrfs_ioctl_trans_end(struct file *file)
1220{
1221 struct inode *inode = fdentry(file)->d_inode;
1222 struct btrfs_root *root = BTRFS_I(inode)->root;
1223 struct btrfs_trans_handle *trans;
1224 int ret = 0;
1225
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001226 trans = file->private_data;
1227 if (!trans) {
1228 ret = -EINVAL;
1229 goto out;
1230 }
1231 btrfs_end_transaction(trans, root);
Christoph Hellwigb2141072008-09-05 16:43:31 -04001232 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001233
1234 mutex_lock(&root->fs_info->trans_mutex);
1235 root->fs_info->open_ioctl_trans--;
1236 mutex_unlock(&root->fs_info->trans_mutex);
1237
Sage Weilcfc8ea82008-12-11 16:30:06 -05001238 mnt_drop_write(file->f_path.mnt);
1239
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001240out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001241 return ret;
1242}
1243
1244long btrfs_ioctl(struct file *file, unsigned int
1245 cmd, unsigned long arg)
1246{
1247 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001248 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001249
1250 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001251 case FS_IOC_GETFLAGS:
1252 return btrfs_ioctl_getflags(file, argp);
1253 case FS_IOC_SETFLAGS:
1254 return btrfs_ioctl_setflags(file, argp);
1255 case FS_IOC_GETVERSION:
1256 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001257 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001258 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001259 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001260 return btrfs_ioctl_snap_create(file, argp, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001261 case BTRFS_IOC_DEFRAG:
1262 return btrfs_ioctl_defrag(file);
1263 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001264 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001265 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001266 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001267 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001268 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001269 case BTRFS_IOC_BALANCE:
1270 return btrfs_balance(root->fs_info->dev_root);
1271 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001272 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1273 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001274 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001275 case BTRFS_IOC_TRANS_START:
1276 return btrfs_ioctl_trans_start(file);
1277 case BTRFS_IOC_TRANS_END:
1278 return btrfs_ioctl_trans_end(file);
1279 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001280 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1281 return 0;
1282 }
1283
1284 return -ENOTTY;
1285}