blob: 645a17927a8f7c8edf9b9b1866695f3ab6982b23 [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040043#include "ctree.h"
44#include "disk-io.h"
45#include "transaction.h"
46#include "btrfs_inode.h"
47#include "ioctl.h"
48#include "print-tree.h"
49#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040050#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051
Christoph Hellwig6cbff002009-04-17 10:37:41 +020052/* Mask out flags that are inappropriate for the given type of inode. */
53static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
54{
55 if (S_ISDIR(mode))
56 return flags;
57 else if (S_ISREG(mode))
58 return flags & ~FS_DIRSYNC_FL;
59 else
60 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
61}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040062
Christoph Hellwig6cbff002009-04-17 10:37:41 +020063/*
64 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
65 */
66static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
67{
68 unsigned int iflags = 0;
69
70 if (flags & BTRFS_INODE_SYNC)
71 iflags |= FS_SYNC_FL;
72 if (flags & BTRFS_INODE_IMMUTABLE)
73 iflags |= FS_IMMUTABLE_FL;
74 if (flags & BTRFS_INODE_APPEND)
75 iflags |= FS_APPEND_FL;
76 if (flags & BTRFS_INODE_NODUMP)
77 iflags |= FS_NODUMP_FL;
78 if (flags & BTRFS_INODE_NOATIME)
79 iflags |= FS_NOATIME_FL;
80 if (flags & BTRFS_INODE_DIRSYNC)
81 iflags |= FS_DIRSYNC_FL;
82
83 return iflags;
84}
85
86/*
87 * Update inode->i_flags based on the btrfs internal flags.
88 */
89void btrfs_update_iflags(struct inode *inode)
90{
91 struct btrfs_inode *ip = BTRFS_I(inode);
92
93 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
94
95 if (ip->flags & BTRFS_INODE_SYNC)
96 inode->i_flags |= S_SYNC;
97 if (ip->flags & BTRFS_INODE_IMMUTABLE)
98 inode->i_flags |= S_IMMUTABLE;
99 if (ip->flags & BTRFS_INODE_APPEND)
100 inode->i_flags |= S_APPEND;
101 if (ip->flags & BTRFS_INODE_NOATIME)
102 inode->i_flags |= S_NOATIME;
103 if (ip->flags & BTRFS_INODE_DIRSYNC)
104 inode->i_flags |= S_DIRSYNC;
105}
106
107/*
108 * Inherit flags from the parent inode.
109 *
110 * Unlike extN we don't have any flags we don't want to inherit currently.
111 */
112void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
113{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400114 unsigned int flags;
115
116 if (!dir)
117 return;
118
119 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200120
121 if (S_ISREG(inode->i_mode))
122 flags &= ~BTRFS_INODE_DIRSYNC;
123 else if (!S_ISDIR(inode->i_mode))
124 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
125
126 BTRFS_I(inode)->flags = flags;
127 btrfs_update_iflags(inode);
128}
129
130static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
131{
132 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
133 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
134
135 if (copy_to_user(arg, &flags, sizeof(flags)))
136 return -EFAULT;
137 return 0;
138}
139
140static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
141{
142 struct inode *inode = file->f_path.dentry->d_inode;
143 struct btrfs_inode *ip = BTRFS_I(inode);
144 struct btrfs_root *root = ip->root;
145 struct btrfs_trans_handle *trans;
146 unsigned int flags, oldflags;
147 int ret;
148
149 if (copy_from_user(&flags, arg, sizeof(flags)))
150 return -EFAULT;
151
152 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
153 FS_NOATIME_FL | FS_NODUMP_FL | \
154 FS_SYNC_FL | FS_DIRSYNC_FL))
155 return -EOPNOTSUPP;
156
157 if (!is_owner_or_cap(inode))
158 return -EACCES;
159
160 mutex_lock(&inode->i_mutex);
161
162 flags = btrfs_mask_flags(inode->i_mode, flags);
163 oldflags = btrfs_flags_to_ioctl(ip->flags);
164 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
165 if (!capable(CAP_LINUX_IMMUTABLE)) {
166 ret = -EPERM;
167 goto out_unlock;
168 }
169 }
170
171 ret = mnt_want_write(file->f_path.mnt);
172 if (ret)
173 goto out_unlock;
174
175 if (flags & FS_SYNC_FL)
176 ip->flags |= BTRFS_INODE_SYNC;
177 else
178 ip->flags &= ~BTRFS_INODE_SYNC;
179 if (flags & FS_IMMUTABLE_FL)
180 ip->flags |= BTRFS_INODE_IMMUTABLE;
181 else
182 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
183 if (flags & FS_APPEND_FL)
184 ip->flags |= BTRFS_INODE_APPEND;
185 else
186 ip->flags &= ~BTRFS_INODE_APPEND;
187 if (flags & FS_NODUMP_FL)
188 ip->flags |= BTRFS_INODE_NODUMP;
189 else
190 ip->flags &= ~BTRFS_INODE_NODUMP;
191 if (flags & FS_NOATIME_FL)
192 ip->flags |= BTRFS_INODE_NOATIME;
193 else
194 ip->flags &= ~BTRFS_INODE_NOATIME;
195 if (flags & FS_DIRSYNC_FL)
196 ip->flags |= BTRFS_INODE_DIRSYNC;
197 else
198 ip->flags &= ~BTRFS_INODE_DIRSYNC;
199
200
201 trans = btrfs_join_transaction(root, 1);
202 BUG_ON(!trans);
203
204 ret = btrfs_update_inode(trans, root, inode);
205 BUG_ON(ret);
206
207 btrfs_update_iflags(inode);
208 inode->i_ctime = CURRENT_TIME;
209 btrfs_end_transaction(trans, root);
210
211 mnt_drop_write(file->f_path.mnt);
212 out_unlock:
213 mutex_unlock(&inode->i_mutex);
214 return 0;
215}
216
217static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
218{
219 struct inode *inode = file->f_path.dentry->d_inode;
220
221 return put_user(inode->i_generation, arg);
222}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400223
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400224static noinline int create_subvol(struct btrfs_root *root,
225 struct dentry *dentry,
226 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400227{
228 struct btrfs_trans_handle *trans;
229 struct btrfs_key key;
230 struct btrfs_root_item root_item;
231 struct btrfs_inode_item *inode_item;
232 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400233 struct btrfs_root *new_root;
234 struct inode *dir = dentry->d_parent->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400235 int ret;
236 int err;
237 u64 objectid;
238 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500239 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400240
Josef Bacik9ed74f22009-09-11 16:12:44 -0400241 /*
242 * 1 - inode item
243 * 2 - refs
244 * 1 - root item
245 * 2 - dir items
246 */
247 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400248 if (ret)
Yan, Zheng76dda932009-09-21 16:00:26 -0400249 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400250
251 trans = btrfs_start_transaction(root, 1);
252 BUG_ON(!trans);
253
254 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
255 0, &objectid);
256 if (ret)
257 goto fail;
258
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400259 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
260 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400261 if (IS_ERR(leaf)) {
262 ret = PTR_ERR(leaf);
263 goto fail;
264 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400267 btrfs_set_header_bytenr(leaf, leaf->start);
268 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400270 btrfs_set_header_owner(leaf, objectid);
271
272 write_extent_buffer(leaf, root->fs_info->fsid,
273 (unsigned long)btrfs_header_fsid(leaf),
274 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
276 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
277 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400278 btrfs_mark_buffer_dirty(leaf);
279
280 inode_item = &root_item.inode;
281 memset(inode_item, 0, sizeof(*inode_item));
282 inode_item->generation = cpu_to_le64(1);
283 inode_item->size = cpu_to_le64(3);
284 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400285 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400286 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
287
288 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400289 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400290 btrfs_set_root_level(&root_item, 0);
291 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000292 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400293 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400294
295 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
296 root_item.drop_level = 0;
297
Chris Mason925baed2008-06-25 16:01:30 -0400298 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400299 free_extent_buffer(leaf);
300 leaf = NULL;
301
302 btrfs_set_root_dirid(&root_item, new_dirid);
303
304 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400305 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400306 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
307 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
308 &root_item);
309 if (ret)
310 goto fail;
311
Yan, Zheng76dda932009-09-21 16:00:26 -0400312 key.offset = (u64)-1;
313 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
314 BUG_ON(IS_ERR(new_root));
315
316 btrfs_record_root_in_trans(trans, new_root);
317
318 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
319 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400320 /*
321 * insert the directory item
322 */
Chris Mason3de45862008-11-17 21:02:50 -0500323 ret = btrfs_set_inode_index(dir, &index);
324 BUG_ON(ret);
325
326 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400327 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500328 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400329 if (ret)
330 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500331
Yan Zheng52c26172009-01-05 15:43:43 -0500332 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
333 ret = btrfs_update_inode(trans, root, dir);
334 BUG_ON(ret);
335
Chris Mason0660b5a2008-11-17 20:37:39 -0500336 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400337 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500338 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400339
Chris Mason0660b5a2008-11-17 20:37:39 -0500340 BUG_ON(ret);
341
Yan, Zheng76dda932009-09-21 16:00:26 -0400342 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400343fail:
Yan, Zheng76dda932009-09-21 16:00:26 -0400344 err = btrfs_commit_transaction(trans, root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400345 if (err && !ret)
346 ret = err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400347
348 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400349 return ret;
350}
351
Chris Mason3de45862008-11-17 21:02:50 -0500352static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
353 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400354{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000355 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400356 struct btrfs_pending_snapshot *pending_snapshot;
357 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000358 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400359
360 if (!root->ref_cows)
361 return -EINVAL;
362
Josef Bacik9ed74f22009-09-11 16:12:44 -0400363 /*
364 * 1 - inode item
365 * 2 - refs
366 * 1 - root item
367 * 2 - dir items
368 */
369 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400370 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000371 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400372
Chris Mason3de45862008-11-17 21:02:50 -0500373 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400374 if (!pending_snapshot) {
375 ret = -ENOMEM;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400376 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000377 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400378 }
379 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
380 if (!pending_snapshot->name) {
381 ret = -ENOMEM;
382 kfree(pending_snapshot);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400383 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000384 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400385 }
386 memcpy(pending_snapshot->name, name, namelen);
387 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500388 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 trans = btrfs_start_transaction(root, 1);
390 BUG_ON(!trans);
391 pending_snapshot->root = root;
392 list_add(&pending_snapshot->list,
393 &trans->transaction->pending_snapshots);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000394 ret = btrfs_commit_transaction(trans, root);
395 BUG_ON(ret);
396 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000398 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
399 if (IS_ERR(inode)) {
400 ret = PTR_ERR(inode);
401 goto fail;
402 }
403 BUG_ON(!inode);
404 d_instantiate(dentry, inode);
405 ret = 0;
406fail:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400407 return ret;
408}
409
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400410/* copy of may_create in fs/namei.c() */
411static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
412{
413 if (child->d_inode)
414 return -EEXIST;
415 if (IS_DEADDIR(dir))
416 return -ENOENT;
417 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
418}
419
420/*
421 * Create a new subvolume below @parent. This is largely modeled after
422 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
423 * inside this filesystem so it's quite a bit simpler.
424 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400425static noinline int btrfs_mksubvol(struct path *parent,
426 char *name, int namelen,
Chris Mason3de45862008-11-17 21:02:50 -0500427 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400428{
Yan, Zheng76dda932009-09-21 16:00:26 -0400429 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400430 struct dentry *dentry;
431 int error;
432
Yan, Zheng76dda932009-09-21 16:00:26 -0400433 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400434
435 dentry = lookup_one_len(name, parent->dentry, namelen);
436 error = PTR_ERR(dentry);
437 if (IS_ERR(dentry))
438 goto out_unlock;
439
440 error = -EEXIST;
441 if (dentry->d_inode)
442 goto out_dput;
443
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400444 error = mnt_want_write(parent->mnt);
445 if (error)
446 goto out_dput;
447
Yan, Zheng76dda932009-09-21 16:00:26 -0400448 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400449 if (error)
450 goto out_drop_write;
451
Yan, Zheng76dda932009-09-21 16:00:26 -0400452 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
453
454 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
455 goto out_up_read;
456
Chris Mason3de45862008-11-17 21:02:50 -0500457 if (snap_src) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400458 error = create_snapshot(snap_src, dentry,
459 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500460 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400461 error = create_subvol(BTRFS_I(dir)->root, dentry,
462 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500463 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400464 if (!error)
465 fsnotify_mkdir(dir, dentry);
466out_up_read:
467 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400468out_drop_write:
469 mnt_drop_write(parent->mnt);
470out_dput:
471 dput(dentry);
472out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400473 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400474 return error;
475}
476
Christoph Hellwigb2950862008-12-02 09:54:17 -0500477static int btrfs_defrag_file(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400478{
479 struct inode *inode = fdentry(file)->d_inode;
480 struct btrfs_root *root = BTRFS_I(inode)->root;
481 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400482 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400483 struct page *page;
484 unsigned long last_index;
485 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
486 unsigned long total_read = 0;
487 u64 page_start;
488 u64 page_end;
489 unsigned long i;
490 int ret;
491
Josef Bacik6a632092009-02-20 11:00:09 -0500492 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400493 if (ret)
494 return -ENOSPC;
495
496 mutex_lock(&inode->i_mutex);
497 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
498 for (i = 0; i <= last_index; i++) {
499 if (total_read % ra_pages == 0) {
500 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
501 min(last_index, i + ra_pages - 1));
502 }
503 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400504again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400505 page = grab_cache_page(inode->i_mapping, i);
506 if (!page)
507 goto out_unlock;
508 if (!PageUptodate(page)) {
509 btrfs_readpage(NULL, page);
510 lock_page(page);
511 if (!PageUptodate(page)) {
512 unlock_page(page);
513 page_cache_release(page);
514 goto out_unlock;
515 }
516 }
517
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400518 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519
520 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
521 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400522 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400523
524 ordered = btrfs_lookup_ordered_extent(inode, page_start);
525 if (ordered) {
526 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
527 unlock_page(page);
528 page_cache_release(page);
529 btrfs_start_ordered_extent(inode, ordered, 1);
530 btrfs_put_ordered_extent(ordered);
531 goto again;
532 }
533 set_page_extent_mapped(page);
534
Chris Masonf87f0572008-08-01 11:27:23 -0400535 /*
536 * this makes sure page_mkwrite is called on the
537 * page if it is dirtied again later
538 */
539 clear_page_dirty_for_io(page);
540
Chris Masonea8c2812008-08-04 23:17:27 -0400541 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400542 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400543 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400544 unlock_page(page);
545 page_cache_release(page);
546 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
547 }
548
549out_unlock:
550 mutex_unlock(&inode->i_mutex);
551 return 0;
552}
553
Yan, Zheng76dda932009-09-21 16:00:26 -0400554static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
555 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400556{
557 u64 new_size;
558 u64 old_size;
559 u64 devid = 1;
560 struct btrfs_ioctl_vol_args *vol_args;
561 struct btrfs_trans_handle *trans;
562 struct btrfs_device *device = NULL;
563 char *sizestr;
564 char *devstr = NULL;
565 int ret = 0;
566 int namelen;
567 int mod = 0;
568
Yan Zhengc146afa2008-11-12 14:34:12 -0500569 if (root->fs_info->sb->s_flags & MS_RDONLY)
570 return -EROFS;
571
Chris Masone441d542009-01-05 16:57:23 -0500572 if (!capable(CAP_SYS_ADMIN))
573 return -EPERM;
574
Li Zefandae7b662009-04-08 15:06:54 +0800575 vol_args = memdup_user(arg, sizeof(*vol_args));
576 if (IS_ERR(vol_args))
577 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400578
579 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400580 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400581
Chris Mason7d9eb122008-07-08 14:19:17 -0400582 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400583 sizestr = vol_args->name;
584 devstr = strchr(sizestr, ':');
585 if (devstr) {
586 char *end;
587 sizestr = devstr + 1;
588 *devstr = '\0';
589 devstr = vol_args->name;
590 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700591 printk(KERN_INFO "resizing devid %llu\n",
592 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400593 }
Yan Zheng2b820322008-11-17 21:11:30 -0500594 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400595 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700596 printk(KERN_INFO "resizer unable to find device %llu\n",
597 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400598 ret = -EINVAL;
599 goto out_unlock;
600 }
601 if (!strcmp(sizestr, "max"))
602 new_size = device->bdev->bd_inode->i_size;
603 else {
604 if (sizestr[0] == '-') {
605 mod = -1;
606 sizestr++;
607 } else if (sizestr[0] == '+') {
608 mod = 1;
609 sizestr++;
610 }
611 new_size = btrfs_parse_size(sizestr);
612 if (new_size == 0) {
613 ret = -EINVAL;
614 goto out_unlock;
615 }
616 }
617
618 old_size = device->total_bytes;
619
620 if (mod < 0) {
621 if (new_size > old_size) {
622 ret = -EINVAL;
623 goto out_unlock;
624 }
625 new_size = old_size - new_size;
626 } else if (mod > 0) {
627 new_size = old_size + new_size;
628 }
629
630 if (new_size < 256 * 1024 * 1024) {
631 ret = -EINVAL;
632 goto out_unlock;
633 }
634 if (new_size > device->bdev->bd_inode->i_size) {
635 ret = -EFBIG;
636 goto out_unlock;
637 }
638
639 do_div(new_size, root->sectorsize);
640 new_size *= root->sectorsize;
641
642 printk(KERN_INFO "new size for %s is %llu\n",
643 device->name, (unsigned long long)new_size);
644
645 if (new_size > old_size) {
646 trans = btrfs_start_transaction(root, 1);
647 ret = btrfs_grow_device(trans, device, new_size);
648 btrfs_commit_transaction(trans, root);
649 } else {
650 ret = btrfs_shrink_device(device, new_size);
651 }
652
653out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400654 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400655 kfree(vol_args);
656 return ret;
657}
658
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400659static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500660 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400661{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400662 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400663 struct btrfs_ioctl_vol_args *vol_args;
Chris Mason3de45862008-11-17 21:02:50 -0500664 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400665 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500666 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400667
Yan Zhengc146afa2008-11-12 14:34:12 -0500668 if (root->fs_info->sb->s_flags & MS_RDONLY)
669 return -EROFS;
670
Li Zefandae7b662009-04-08 15:06:54 +0800671 vol_args = memdup_user(arg, sizeof(*vol_args));
672 if (IS_ERR(vol_args))
673 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400674
Mark Fasheh5516e592008-07-24 12:20:14 -0400675 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400676 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400677 if (strchr(vol_args->name, '/')) {
678 ret = -EINVAL;
679 goto out;
680 }
681
Chris Mason3de45862008-11-17 21:02:50 -0500682 if (subvol) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400683 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
684 NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400685 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500686 struct inode *src_inode;
687 src_file = fget(vol_args->fd);
688 if (!src_file) {
689 ret = -EINVAL;
690 goto out;
691 }
692
693 src_inode = src_file->f_path.dentry->d_inode;
694 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500695 printk(KERN_INFO "btrfs: Snapshot src from "
696 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500697 ret = -EINVAL;
698 fput(src_file);
699 goto out;
700 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400701 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
702 BTRFS_I(src_inode)->root);
Chris Mason3de45862008-11-17 21:02:50 -0500703 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400704 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400705out:
706 kfree(vol_args);
707 return ret;
708}
709
Yan, Zheng76dda932009-09-21 16:00:26 -0400710/*
711 * helper to check if the subvolume references other subvolumes
712 */
713static noinline int may_destroy_subvol(struct btrfs_root *root)
714{
715 struct btrfs_path *path;
716 struct btrfs_key key;
717 int ret;
718
719 path = btrfs_alloc_path();
720 if (!path)
721 return -ENOMEM;
722
723 key.objectid = root->root_key.objectid;
724 key.type = BTRFS_ROOT_REF_KEY;
725 key.offset = (u64)-1;
726
727 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
728 &key, path, 0, 0);
729 if (ret < 0)
730 goto out;
731 BUG_ON(ret == 0);
732
733 ret = 0;
734 if (path->slots[0] > 0) {
735 path->slots[0]--;
736 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
737 if (key.objectid == root->root_key.objectid &&
738 key.type == BTRFS_ROOT_REF_KEY)
739 ret = -ENOTEMPTY;
740 }
741out:
742 btrfs_free_path(path);
743 return ret;
744}
745
746static noinline int btrfs_ioctl_snap_destroy(struct file *file,
747 void __user *arg)
748{
749 struct dentry *parent = fdentry(file);
750 struct dentry *dentry;
751 struct inode *dir = parent->d_inode;
752 struct inode *inode;
753 struct btrfs_root *root = BTRFS_I(dir)->root;
754 struct btrfs_root *dest = NULL;
755 struct btrfs_ioctl_vol_args *vol_args;
756 struct btrfs_trans_handle *trans;
757 int namelen;
758 int ret;
759 int err = 0;
760
761 if (!capable(CAP_SYS_ADMIN))
762 return -EPERM;
763
764 vol_args = memdup_user(arg, sizeof(*vol_args));
765 if (IS_ERR(vol_args))
766 return PTR_ERR(vol_args);
767
768 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
769 namelen = strlen(vol_args->name);
770 if (strchr(vol_args->name, '/') ||
771 strncmp(vol_args->name, "..", namelen) == 0) {
772 err = -EINVAL;
773 goto out;
774 }
775
776 err = mnt_want_write(file->f_path.mnt);
777 if (err)
778 goto out;
779
780 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
781 dentry = lookup_one_len(vol_args->name, parent, namelen);
782 if (IS_ERR(dentry)) {
783 err = PTR_ERR(dentry);
784 goto out_unlock_dir;
785 }
786
787 if (!dentry->d_inode) {
788 err = -ENOENT;
789 goto out_dput;
790 }
791
792 inode = dentry->d_inode;
793 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
794 err = -EINVAL;
795 goto out_dput;
796 }
797
798 dest = BTRFS_I(inode)->root;
799
800 mutex_lock(&inode->i_mutex);
801 err = d_invalidate(dentry);
802 if (err)
803 goto out_unlock;
804
805 down_write(&root->fs_info->subvol_sem);
806
807 err = may_destroy_subvol(dest);
808 if (err)
809 goto out_up_write;
810
811 trans = btrfs_start_transaction(root, 1);
812 ret = btrfs_unlink_subvol(trans, root, dir,
813 dest->root_key.objectid,
814 dentry->d_name.name,
815 dentry->d_name.len);
816 BUG_ON(ret);
817
818 btrfs_record_root_in_trans(trans, dest);
819
820 memset(&dest->root_item.drop_progress, 0,
821 sizeof(dest->root_item.drop_progress));
822 dest->root_item.drop_level = 0;
823 btrfs_set_root_refs(&dest->root_item, 0);
824
825 ret = btrfs_insert_orphan_item(trans,
826 root->fs_info->tree_root,
827 dest->root_key.objectid);
828 BUG_ON(ret);
829
830 ret = btrfs_commit_transaction(trans, root);
831 BUG_ON(ret);
832 inode->i_flags |= S_DEAD;
833out_up_write:
834 up_write(&root->fs_info->subvol_sem);
835out_unlock:
836 mutex_unlock(&inode->i_mutex);
837 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -0400838 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -0400839 btrfs_invalidate_inodes(dest);
840 d_delete(dentry);
841 }
842out_dput:
843 dput(dentry);
844out_unlock_dir:
845 mutex_unlock(&dir->i_mutex);
846 mnt_drop_write(file->f_path.mnt);
847out:
848 kfree(vol_args);
849 return err;
850}
851
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400852static int btrfs_ioctl_defrag(struct file *file)
853{
854 struct inode *inode = fdentry(file)->d_inode;
855 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500856 int ret;
857
858 ret = mnt_want_write(file->f_path.mnt);
859 if (ret)
860 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861
862 switch (inode->i_mode & S_IFMT) {
863 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -0500864 if (!capable(CAP_SYS_ADMIN)) {
865 ret = -EPERM;
866 goto out;
867 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400868 btrfs_defrag_root(root, 0);
869 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400870 break;
871 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -0500872 if (!(file->f_mode & FMODE_WRITE)) {
873 ret = -EINVAL;
874 goto out;
875 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400876 btrfs_defrag_file(file);
877 break;
878 }
Chris Masone441d542009-01-05 16:57:23 -0500879out:
Yan Zhengab67b7c2008-12-19 10:58:39 -0500880 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -0500881 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400882}
883
Christoph Hellwigb2950862008-12-02 09:54:17 -0500884static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400885{
886 struct btrfs_ioctl_vol_args *vol_args;
887 int ret;
888
Chris Masone441d542009-01-05 16:57:23 -0500889 if (!capable(CAP_SYS_ADMIN))
890 return -EPERM;
891
Li Zefandae7b662009-04-08 15:06:54 +0800892 vol_args = memdup_user(arg, sizeof(*vol_args));
893 if (IS_ERR(vol_args))
894 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400895
Mark Fasheh5516e592008-07-24 12:20:14 -0400896 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400897 ret = btrfs_init_new_device(root, vol_args->name);
898
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400899 kfree(vol_args);
900 return ret;
901}
902
Christoph Hellwigb2950862008-12-02 09:54:17 -0500903static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400904{
905 struct btrfs_ioctl_vol_args *vol_args;
906 int ret;
907
Chris Masone441d542009-01-05 16:57:23 -0500908 if (!capable(CAP_SYS_ADMIN))
909 return -EPERM;
910
Yan Zhengc146afa2008-11-12 14:34:12 -0500911 if (root->fs_info->sb->s_flags & MS_RDONLY)
912 return -EROFS;
913
Li Zefandae7b662009-04-08 15:06:54 +0800914 vol_args = memdup_user(arg, sizeof(*vol_args));
915 if (IS_ERR(vol_args))
916 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400917
Mark Fasheh5516e592008-07-24 12:20:14 -0400918 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400919 ret = btrfs_rm_device(root, vol_args->name);
920
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400921 kfree(vol_args);
922 return ret;
923}
924
Yan, Zheng76dda932009-09-21 16:00:26 -0400925static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
926 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400927{
928 struct inode *inode = fdentry(file)->d_inode;
929 struct btrfs_root *root = BTRFS_I(inode)->root;
930 struct file *src_file;
931 struct inode *src;
932 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400933 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400934 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400935 char *buf;
936 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400937 u32 nritems;
938 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -0400939 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -0500940 u64 len = olen;
941 u64 bs = root->fs_info->sb->s_blocksize;
942 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -0500943
Sage Weilc5c9cd42008-11-12 14:32:25 -0500944 /*
945 * TODO:
946 * - split compressed inline extents. annoying: we need to
947 * decompress into destination's address_space (the file offset
948 * may change, so source mapping won't do), then recompress (or
949 * otherwise reinsert) a subrange.
950 * - allow ranges within the same file to be cloned (provided
951 * they don't overlap)?
952 */
953
Chris Masone441d542009-01-05 16:57:23 -0500954 /* the destination must be opened for writing */
955 if (!(file->f_mode & FMODE_WRITE))
956 return -EINVAL;
957
Yan Zhengc146afa2008-11-12 14:34:12 -0500958 ret = mnt_want_write(file->f_path.mnt);
959 if (ret)
960 return ret;
961
Sage Weilc5c9cd42008-11-12 14:32:25 -0500962 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -0500963 if (!src_file) {
964 ret = -EBADF;
965 goto out_drop_write;
966 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400967 src = src_file->f_dentry->d_inode;
968
Sage Weilc5c9cd42008-11-12 14:32:25 -0500969 ret = -EINVAL;
970 if (src == inode)
971 goto out_fput;
972
Yan Zhengae01a0a2008-08-04 23:23:47 -0400973 ret = -EISDIR;
974 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400975 goto out_fput;
976
Yan Zhengae01a0a2008-08-04 23:23:47 -0400977 ret = -EXDEV;
978 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
979 goto out_fput;
980
981 ret = -ENOMEM;
982 buf = vmalloc(btrfs_level_size(root, 0));
983 if (!buf)
984 goto out_fput;
985
986 path = btrfs_alloc_path();
987 if (!path) {
988 vfree(buf);
989 goto out_fput;
990 }
991 path->reada = 2;
992
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400993 if (inode < src) {
994 mutex_lock(&inode->i_mutex);
995 mutex_lock(&src->i_mutex);
996 } else {
997 mutex_lock(&src->i_mutex);
998 mutex_lock(&inode->i_mutex);
999 }
1000
Sage Weilc5c9cd42008-11-12 14:32:25 -05001001 /* determine range to clone */
1002 ret = -EINVAL;
1003 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001004 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001005 if (len == 0)
1006 olen = len = src->i_size - off;
1007 /* if we extend to eof, continue to block boundary */
1008 if (off + len == src->i_size)
1009 len = ((src->i_size + bs-1) & ~(bs-1))
1010 - off;
1011
1012 /* verify the end result is block aligned */
1013 if ((off & (bs-1)) ||
1014 ((off + len) & (bs-1)))
1015 goto out_unlock;
1016
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001017 /* do any pending delalloc/csum calc on src, one way or
1018 another, and lock file content */
1019 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001020 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001021 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1022 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001023 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001024 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001025 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001026 if (ordered)
1027 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001028 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001029 }
1030
Yan Zhengae01a0a2008-08-04 23:23:47 -04001031 trans = btrfs_start_transaction(root, 1);
1032 BUG_ON(!trans);
1033
Sage Weilc5c9cd42008-11-12 14:32:25 -05001034 /* punch hole in destination first */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001035 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001036
1037 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001038 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001039 key.type = BTRFS_EXTENT_DATA_KEY;
1040 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001041
1042 while (1) {
1043 /*
1044 * note the key will change type as we walk through the
1045 * tree.
1046 */
1047 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1048 if (ret < 0)
1049 goto out;
1050
Yan Zhengae01a0a2008-08-04 23:23:47 -04001051 nritems = btrfs_header_nritems(path->nodes[0]);
1052 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001053 ret = btrfs_next_leaf(root, path);
1054 if (ret < 0)
1055 goto out;
1056 if (ret > 0)
1057 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001058 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001059 }
1060 leaf = path->nodes[0];
1061 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001062
Yan Zhengae01a0a2008-08-04 23:23:47 -04001063 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001064 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001065 key.objectid != src->i_ino)
1066 break;
1067
Sage Weilc5c9cd42008-11-12 14:32:25 -05001068 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1069 struct btrfs_file_extent_item *extent;
1070 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001071 u32 size;
1072 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001073 u64 disko = 0, diskl = 0;
1074 u64 datao = 0, datal = 0;
1075 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -04001076
1077 size = btrfs_item_size_nr(leaf, slot);
1078 read_extent_buffer(leaf, buf,
1079 btrfs_item_ptr_offset(leaf, slot),
1080 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001081
1082 extent = btrfs_item_ptr(leaf, slot,
1083 struct btrfs_file_extent_item);
1084 comp = btrfs_file_extent_compression(leaf, extent);
1085 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001086 if (type == BTRFS_FILE_EXTENT_REG ||
1087 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001088 disko = btrfs_file_extent_disk_bytenr(leaf,
1089 extent);
1090 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1091 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001092 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001093 datal = btrfs_file_extent_num_bytes(leaf,
1094 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001095 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1096 /* take upper bound, may be compressed */
1097 datal = btrfs_file_extent_ram_bytes(leaf,
1098 extent);
1099 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001100 btrfs_release_path(root, path);
1101
Sage Weilc5c9cd42008-11-12 14:32:25 -05001102 if (key.offset + datal < off ||
1103 key.offset >= off+len)
1104 goto next;
1105
Zheng Yan31840ae2008-09-23 13:14:14 -04001106 memcpy(&new_key, &key, sizeof(new_key));
1107 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001108 new_key.offset = key.offset + destoff - off;
1109
Chris Masonc8a894d2009-06-27 21:07:03 -04001110 if (type == BTRFS_FILE_EXTENT_REG ||
1111 type == BTRFS_FILE_EXTENT_PREALLOC) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001112 ret = btrfs_insert_empty_item(trans, root, path,
1113 &new_key, size);
1114 if (ret)
1115 goto out;
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
1123 extent = btrfs_item_ptr(leaf, slot,
1124 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001125
1126 if (off > key.offset) {
1127 datao += off - key.offset;
1128 datal -= off - key.offset;
1129 }
Chris Masonac6889c2009-10-09 11:29:53 -04001130
1131 if (key.offset + datal > off + len)
1132 datal = off + len - key.offset;
1133
Sage Weilc5c9cd42008-11-12 14:32:25 -05001134 /* disko == 0 means it's a hole */
1135 if (!disko)
1136 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001137
1138 btrfs_set_file_extent_offset(leaf, extent,
1139 datao);
1140 btrfs_set_file_extent_num_bytes(leaf, extent,
1141 datal);
1142 if (disko) {
1143 inode_add_bytes(inode, datal);
1144 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145 disko, diskl, 0,
1146 root->root_key.objectid,
1147 inode->i_ino,
1148 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001149 BUG_ON(ret);
1150 }
1151 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1152 u64 skip = 0;
1153 u64 trim = 0;
1154 if (off > key.offset) {
1155 skip = off - key.offset;
1156 new_key.offset += skip;
1157 }
Chris Masond3977122009-01-05 21:25:51 -05001158
Sage Weilc5c9cd42008-11-12 14:32:25 -05001159 if (key.offset + datal > off+len)
1160 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001161
Sage Weilc5c9cd42008-11-12 14:32:25 -05001162 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001163 ret = -EINVAL;
1164 goto out;
1165 }
1166 size -= skip + trim;
1167 datal -= skip + trim;
1168 ret = btrfs_insert_empty_item(trans, root, path,
1169 &new_key, size);
1170 if (ret)
1171 goto out;
1172
1173 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001174 u32 start =
1175 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001176 memmove(buf+start, buf+start+skip,
1177 datal);
1178 }
1179
1180 leaf = path->nodes[0];
1181 slot = path->slots[0];
1182 write_extent_buffer(leaf, buf,
1183 btrfs_item_ptr_offset(leaf, slot),
1184 size);
1185 inode_add_bytes(inode, datal);
1186 }
1187
1188 btrfs_mark_buffer_dirty(leaf);
1189 }
1190
Chris Masond3977122009-01-05 21:25:51 -05001191next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001192 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001193 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001194 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001195 ret = 0;
1196out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001197 btrfs_release_path(root, path);
1198 if (ret == 0) {
1199 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001200 if (destoff + olen > inode->i_size)
1201 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001202 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1203 ret = btrfs_update_inode(trans, root, inode);
1204 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001205 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001206 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001207 if (ret)
1208 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001209out_unlock:
1210 mutex_unlock(&src->i_mutex);
1211 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001212 vfree(buf);
1213 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001214out_fput:
1215 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001216out_drop_write:
1217 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001218 return ret;
1219}
1220
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001221static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001222{
1223 struct btrfs_ioctl_clone_range_args args;
1224
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001225 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001226 return -EFAULT;
1227 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1228 args.src_length, args.dest_offset);
1229}
1230
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001231/*
1232 * there are many ways the trans_start and trans_end ioctls can lead
1233 * to deadlocks. They should only be used by applications that
1234 * basically own the machine, and have a very in depth understanding
1235 * of all the possible deadlocks and enospc problems.
1236 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001237static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001238{
1239 struct inode *inode = fdentry(file)->d_inode;
1240 struct btrfs_root *root = BTRFS_I(inode)->root;
1241 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001242 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001243
Sage Weil1ab86ae2009-09-29 18:38:44 -04001244 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001245 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001246 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001247
1248 ret = -EINPROGRESS;
1249 if (file->private_data)
1250 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001251
Yan Zhengc146afa2008-11-12 14:34:12 -05001252 ret = mnt_want_write(file->f_path.mnt);
1253 if (ret)
1254 goto out;
1255
Sage Weil9ca9ee02008-08-04 10:41:27 -04001256 mutex_lock(&root->fs_info->trans_mutex);
1257 root->fs_info->open_ioctl_trans++;
1258 mutex_unlock(&root->fs_info->trans_mutex);
1259
Sage Weil1ab86ae2009-09-29 18:38:44 -04001260 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001261 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001262 if (!trans)
1263 goto out_drop;
1264
1265 file->private_data = trans;
1266 return 0;
1267
1268out_drop:
1269 mutex_lock(&root->fs_info->trans_mutex);
1270 root->fs_info->open_ioctl_trans--;
1271 mutex_unlock(&root->fs_info->trans_mutex);
1272 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001273out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001274 return ret;
1275}
1276
1277/*
1278 * there are many ways the trans_start and trans_end ioctls can lead
1279 * to deadlocks. They should only be used by applications that
1280 * basically own the machine, and have a very in depth understanding
1281 * of all the possible deadlocks and enospc problems.
1282 */
1283long btrfs_ioctl_trans_end(struct file *file)
1284{
1285 struct inode *inode = fdentry(file)->d_inode;
1286 struct btrfs_root *root = BTRFS_I(inode)->root;
1287 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001288
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001289 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001290 if (!trans)
1291 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04001292 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001293
Sage Weil1ab86ae2009-09-29 18:38:44 -04001294 btrfs_end_transaction(trans, root);
1295
Sage Weil9ca9ee02008-08-04 10:41:27 -04001296 mutex_lock(&root->fs_info->trans_mutex);
1297 root->fs_info->open_ioctl_trans--;
1298 mutex_unlock(&root->fs_info->trans_mutex);
1299
Sage Weilcfc8ea82008-12-11 16:30:06 -05001300 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001301 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302}
1303
1304long btrfs_ioctl(struct file *file, unsigned int
1305 cmd, unsigned long arg)
1306{
1307 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001308 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001309
1310 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001311 case FS_IOC_GETFLAGS:
1312 return btrfs_ioctl_getflags(file, argp);
1313 case FS_IOC_SETFLAGS:
1314 return btrfs_ioctl_setflags(file, argp);
1315 case FS_IOC_GETVERSION:
1316 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001317 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001318 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001319 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001320 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04001321 case BTRFS_IOC_SNAP_DESTROY:
1322 return btrfs_ioctl_snap_destroy(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001323 case BTRFS_IOC_DEFRAG:
1324 return btrfs_ioctl_defrag(file);
1325 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001326 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001327 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001328 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001329 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001330 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001331 case BTRFS_IOC_BALANCE:
1332 return btrfs_balance(root->fs_info->dev_root);
1333 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001334 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1335 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001336 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001337 case BTRFS_IOC_TRANS_START:
1338 return btrfs_ioctl_trans_start(file);
1339 case BTRFS_IOC_TRANS_END:
1340 return btrfs_ioctl_trans_end(file);
1341 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001342 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1343 return 0;
1344 }
1345
1346 return -ENOTTY;
1347}