blob: 32c980ae0f1c63a601681df1273fa9c6bb4db1cd [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050043#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040044#include "ctree.h"
45#include "disk-io.h"
46#include "transaction.h"
47#include "btrfs_inode.h"
48#include "ioctl.h"
49#include "print-tree.h"
50#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040051#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
Christoph Hellwig6cbff002009-04-17 10:37:41 +020053/* Mask out flags that are inappropriate for the given type of inode. */
54static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
55{
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
62}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/*
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 */
67static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
68{
69 unsigned int iflags = 0;
70
71 if (flags & BTRFS_INODE_SYNC)
72 iflags |= FS_SYNC_FL;
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
83
84 return iflags;
85}
86
87/*
88 * Update inode->i_flags based on the btrfs internal flags.
89 */
90void btrfs_update_iflags(struct inode *inode)
91{
92 struct btrfs_inode *ip = BTRFS_I(inode);
93
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
95
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
106}
107
108/*
109 * Inherit flags from the parent inode.
110 *
111 * Unlike extN we don't have any flags we don't want to inherit currently.
112 */
113void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
114{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400115 unsigned int flags;
116
117 if (!dir)
118 return;
119
120 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200121
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
126
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
129}
130
131static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
132{
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
135
136 if (copy_to_user(arg, &flags, sizeof(flags)))
137 return -EFAULT;
138 return 0;
139}
140
Liu Bo75e7cb72011-03-22 10:12:20 +0000141static int check_flags(unsigned int flags)
142{
143 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
144 FS_NOATIME_FL | FS_NODUMP_FL | \
145 FS_SYNC_FL | FS_DIRSYNC_FL | \
146 FS_NOCOMP_FL | FS_COMPR_FL | \
147 FS_NOCOW_FL | FS_COW_FL))
148 return -EOPNOTSUPP;
149
150 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
151 return -EINVAL;
152
153 if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL))
154 return -EINVAL;
155
156 return 0;
157}
158
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200159static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
160{
161 struct inode *inode = file->f_path.dentry->d_inode;
162 struct btrfs_inode *ip = BTRFS_I(inode);
163 struct btrfs_root *root = ip->root;
164 struct btrfs_trans_handle *trans;
165 unsigned int flags, oldflags;
166 int ret;
167
Li Zefanb83cc962010-12-20 16:04:08 +0800168 if (btrfs_root_readonly(root))
169 return -EROFS;
170
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200171 if (copy_from_user(&flags, arg, sizeof(flags)))
172 return -EFAULT;
173
Liu Bo75e7cb72011-03-22 10:12:20 +0000174 ret = check_flags(flags);
175 if (ret)
176 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200177
178 if (!is_owner_or_cap(inode))
179 return -EACCES;
180
181 mutex_lock(&inode->i_mutex);
182
183 flags = btrfs_mask_flags(inode->i_mode, flags);
184 oldflags = btrfs_flags_to_ioctl(ip->flags);
185 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
186 if (!capable(CAP_LINUX_IMMUTABLE)) {
187 ret = -EPERM;
188 goto out_unlock;
189 }
190 }
191
192 ret = mnt_want_write(file->f_path.mnt);
193 if (ret)
194 goto out_unlock;
195
196 if (flags & FS_SYNC_FL)
197 ip->flags |= BTRFS_INODE_SYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_SYNC;
200 if (flags & FS_IMMUTABLE_FL)
201 ip->flags |= BTRFS_INODE_IMMUTABLE;
202 else
203 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
204 if (flags & FS_APPEND_FL)
205 ip->flags |= BTRFS_INODE_APPEND;
206 else
207 ip->flags &= ~BTRFS_INODE_APPEND;
208 if (flags & FS_NODUMP_FL)
209 ip->flags |= BTRFS_INODE_NODUMP;
210 else
211 ip->flags &= ~BTRFS_INODE_NODUMP;
212 if (flags & FS_NOATIME_FL)
213 ip->flags |= BTRFS_INODE_NOATIME;
214 else
215 ip->flags &= ~BTRFS_INODE_NOATIME;
216 if (flags & FS_DIRSYNC_FL)
217 ip->flags |= BTRFS_INODE_DIRSYNC;
218 else
219 ip->flags &= ~BTRFS_INODE_DIRSYNC;
220
Liu Bo75e7cb72011-03-22 10:12:20 +0000221 /*
222 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
223 * flag may be changed automatically if compression code won't make
224 * things smaller.
225 */
226 if (flags & FS_NOCOMP_FL) {
227 ip->flags &= ~BTRFS_INODE_COMPRESS;
228 ip->flags |= BTRFS_INODE_NOCOMPRESS;
229 } else if (flags & FS_COMPR_FL) {
230 ip->flags |= BTRFS_INODE_COMPRESS;
231 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
232 }
233 if (flags & FS_NOCOW_FL)
234 ip->flags |= BTRFS_INODE_NODATACOW;
235 else if (flags & FS_COW_FL)
236 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200237
238 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000239 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200240
241 ret = btrfs_update_inode(trans, root, inode);
242 BUG_ON(ret);
243
244 btrfs_update_iflags(inode);
245 inode->i_ctime = CURRENT_TIME;
246 btrfs_end_transaction(trans, root);
247
248 mnt_drop_write(file->f_path.mnt);
249 out_unlock:
250 mutex_unlock(&inode->i_mutex);
251 return 0;
252}
253
254static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
255{
256 struct inode *inode = file->f_path.dentry->d_inode;
257
258 return put_user(inode->i_generation, arg);
259}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400260
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400261static noinline int create_subvol(struct btrfs_root *root,
262 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400263 char *name, int namelen,
264 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400265{
266 struct btrfs_trans_handle *trans;
267 struct btrfs_key key;
268 struct btrfs_root_item root_item;
269 struct btrfs_inode_item *inode_item;
270 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400271 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000272 struct dentry *parent = dget_parent(dentry);
273 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400274 int ret;
275 int err;
276 u64 objectid;
277 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500278 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400279
Yan, Zhenga22285a2010-05-16 10:48:46 -0400280 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
281 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000282 if (ret) {
283 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400284 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000285 }
286
287 dir = parent->d_inode;
288
Josef Bacik9ed74f22009-09-11 16:12:44 -0400289 /*
290 * 1 - inode item
291 * 2 - refs
292 * 1 - root item
293 * 2 - dir items
294 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400295 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000296 if (IS_ERR(trans)) {
297 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400298 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000299 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400300
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400301 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
302 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400303 if (IS_ERR(leaf)) {
304 ret = PTR_ERR(leaf);
305 goto fail;
306 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400307
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400308 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400309 btrfs_set_header_bytenr(leaf, leaf->start);
310 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400311 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400312 btrfs_set_header_owner(leaf, objectid);
313
314 write_extent_buffer(leaf, root->fs_info->fsid,
315 (unsigned long)btrfs_header_fsid(leaf),
316 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400317 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
318 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
319 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400320 btrfs_mark_buffer_dirty(leaf);
321
322 inode_item = &root_item.inode;
323 memset(inode_item, 0, sizeof(*inode_item));
324 inode_item->generation = cpu_to_le64(1);
325 inode_item->size = cpu_to_le64(3);
326 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400327 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400328 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
329
330 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400331 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400332 btrfs_set_root_level(&root_item, 0);
333 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000334 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400335 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400336
337 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
338 root_item.drop_level = 0;
339
Chris Mason925baed2008-06-25 16:01:30 -0400340 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400341 free_extent_buffer(leaf);
342 leaf = NULL;
343
344 btrfs_set_root_dirid(&root_item, new_dirid);
345
346 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400347 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
349 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
350 &root_item);
351 if (ret)
352 goto fail;
353
Yan, Zheng76dda932009-09-21 16:00:26 -0400354 key.offset = (u64)-1;
355 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
356 BUG_ON(IS_ERR(new_root));
357
358 btrfs_record_root_in_trans(trans, new_root);
359
360 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
361 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400362 /*
363 * insert the directory item
364 */
Chris Mason3de45862008-11-17 21:02:50 -0500365 ret = btrfs_set_inode_index(dir, &index);
366 BUG_ON(ret);
367
368 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400369 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500370 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 if (ret)
372 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500373
Yan Zheng52c26172009-01-05 15:43:43 -0500374 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
375 ret = btrfs_update_inode(trans, root, dir);
376 BUG_ON(ret);
377
Chris Mason0660b5a2008-11-17 20:37:39 -0500378 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400379 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500380 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400381
Chris Mason0660b5a2008-11-17 20:37:39 -0500382 BUG_ON(ret);
383
Yan, Zheng76dda932009-09-21 16:00:26 -0400384 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400385fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000386 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400387 if (async_transid) {
388 *async_transid = trans->transid;
389 err = btrfs_commit_transaction_async(trans, root, 1);
390 } else {
391 err = btrfs_commit_transaction(trans, root);
392 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400393 if (err && !ret)
394 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400395 return ret;
396}
397
Sage Weil72fd0322010-10-29 15:41:32 -0400398static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800399 char *name, int namelen, u64 *async_transid,
400 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400401{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000402 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000403 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 struct btrfs_pending_snapshot *pending_snapshot;
405 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000406 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400407
408 if (!root->ref_cows)
409 return -EINVAL;
410
Yan, Zhenga22285a2010-05-16 10:48:46 -0400411 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
412 if (!pending_snapshot)
413 return -ENOMEM;
414
415 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
416 pending_snapshot->dentry = dentry;
417 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800418 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400419
420 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
421 if (IS_ERR(trans)) {
422 ret = PTR_ERR(trans);
423 goto fail;
424 }
425
426 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
427 BUG_ON(ret);
428
429 list_add(&pending_snapshot->list,
430 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400431 if (async_transid) {
432 *async_transid = trans->transid;
433 ret = btrfs_commit_transaction_async(trans,
434 root->fs_info->extent_root, 1);
435 } else {
436 ret = btrfs_commit_transaction(trans,
437 root->fs_info->extent_root);
438 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400439 BUG_ON(ret);
440
441 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000443 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400444
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500445 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
446 if (ret)
447 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448
Josef Bacik6a912212010-11-20 09:48:00 +0000449 parent = dget_parent(dentry);
450 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
451 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000452 if (IS_ERR(inode)) {
453 ret = PTR_ERR(inode);
454 goto fail;
455 }
456 BUG_ON(!inode);
457 d_instantiate(dentry, inode);
458 ret = 0;
459fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400460 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461 return ret;
462}
463
Sage Weil4260f7c2010-10-29 15:46:43 -0400464/* copy of check_sticky in fs/namei.c()
465* It's inline, so penalty for filesystems that don't use sticky bit is
466* minimal.
467*/
468static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
469{
470 uid_t fsuid = current_fsuid();
471
472 if (!(dir->i_mode & S_ISVTX))
473 return 0;
474 if (inode->i_uid == fsuid)
475 return 0;
476 if (dir->i_uid == fsuid)
477 return 0;
478 return !capable(CAP_FOWNER);
479}
480
481/* copy of may_delete in fs/namei.c()
482 * Check whether we can remove a link victim from directory dir, check
483 * whether the type of victim is right.
484 * 1. We can't do it if dir is read-only (done in permission())
485 * 2. We should have write and exec permissions on dir
486 * 3. We can't remove anything from append-only dir
487 * 4. We can't do anything with immutable dir (done in permission())
488 * 5. If the sticky bit on dir is set we should either
489 * a. be owner of dir, or
490 * b. be owner of victim, or
491 * c. have CAP_FOWNER capability
492 * 6. If the victim is append-only or immutable we can't do antyhing with
493 * links pointing to it.
494 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
495 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
496 * 9. We can't remove a root or mountpoint.
497 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
498 * nfs_async_unlink().
499 */
500
501static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
502{
503 int error;
504
505 if (!victim->d_inode)
506 return -ENOENT;
507
508 BUG_ON(victim->d_parent->d_inode != dir);
509 audit_inode_child(victim, dir);
510
511 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
512 if (error)
513 return error;
514 if (IS_APPEND(dir))
515 return -EPERM;
516 if (btrfs_check_sticky(dir, victim->d_inode)||
517 IS_APPEND(victim->d_inode)||
518 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
519 return -EPERM;
520 if (isdir) {
521 if (!S_ISDIR(victim->d_inode->i_mode))
522 return -ENOTDIR;
523 if (IS_ROOT(victim))
524 return -EBUSY;
525 } else if (S_ISDIR(victim->d_inode->i_mode))
526 return -EISDIR;
527 if (IS_DEADDIR(dir))
528 return -ENOENT;
529 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
530 return -EBUSY;
531 return 0;
532}
533
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400534/* copy of may_create in fs/namei.c() */
535static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
536{
537 if (child->d_inode)
538 return -EEXIST;
539 if (IS_DEADDIR(dir))
540 return -ENOENT;
541 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
542}
543
544/*
545 * Create a new subvolume below @parent. This is largely modeled after
546 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
547 * inside this filesystem so it's quite a bit simpler.
548 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400549static noinline int btrfs_mksubvol(struct path *parent,
550 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400551 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800552 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400553{
Yan, Zheng76dda932009-09-21 16:00:26 -0400554 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400555 struct dentry *dentry;
556 int error;
557
Yan, Zheng76dda932009-09-21 16:00:26 -0400558 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400559
560 dentry = lookup_one_len(name, parent->dentry, namelen);
561 error = PTR_ERR(dentry);
562 if (IS_ERR(dentry))
563 goto out_unlock;
564
565 error = -EEXIST;
566 if (dentry->d_inode)
567 goto out_dput;
568
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400569 error = mnt_want_write(parent->mnt);
570 if (error)
571 goto out_dput;
572
Yan, Zheng76dda932009-09-21 16:00:26 -0400573 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400574 if (error)
575 goto out_drop_write;
576
Yan, Zheng76dda932009-09-21 16:00:26 -0400577 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
578
579 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
580 goto out_up_read;
581
Chris Mason3de45862008-11-17 21:02:50 -0500582 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400583 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800584 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500585 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400586 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400587 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500588 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400589 if (!error)
590 fsnotify_mkdir(dir, dentry);
591out_up_read:
592 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400593out_drop_write:
594 mnt_drop_write(parent->mnt);
595out_dput:
596 dput(dentry);
597out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400598 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400599 return error;
600}
601
Chris Mason940100a2010-03-10 10:52:59 -0500602static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500603 int thresh, u64 *last_len, u64 *skip,
604 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500605{
606 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
607 struct extent_map *em = NULL;
608 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
609 int ret = 1;
610
Chris Mason1e701a32010-03-11 09:42:04 -0500611
612 if (thresh == 0)
613 thresh = 256 * 1024;
614
Chris Mason940100a2010-03-10 10:52:59 -0500615 /*
616 * make sure that once we start defragging and extent, we keep on
617 * defragging it
618 */
619 if (start < *defrag_end)
620 return 1;
621
622 *skip = 0;
623
624 /*
625 * hopefully we have this extent in the tree already, try without
626 * the full extent lock
627 */
628 read_lock(&em_tree->lock);
629 em = lookup_extent_mapping(em_tree, start, len);
630 read_unlock(&em_tree->lock);
631
632 if (!em) {
633 /* get the big lock and read metadata off disk */
634 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
635 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
636 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
637
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000638 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500639 return 0;
640 }
641
642 /* this will cover holes, and inline extents */
643 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
644 ret = 0;
645
646 /*
647 * we hit a real extent, if it is big don't bother defragging it again
648 */
Chris Mason1e701a32010-03-11 09:42:04 -0500649 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500650 ret = 0;
651
652 /*
653 * last_len ends up being a counter of how many bytes we've defragged.
654 * every time we choose not to defrag an extent, we reset *last_len
655 * so that the next tiny extent will force a defrag.
656 *
657 * The end result of this is that tiny extents before a single big
658 * extent will force at least part of that big extent to be defragged.
659 */
660 if (ret) {
661 *last_len += len;
662 *defrag_end = extent_map_end(em);
663 } else {
664 *last_len = 0;
665 *skip = extent_map_end(em);
666 *defrag_end = 0;
667 }
668
669 free_extent_map(em);
670 return ret;
671}
672
Chris Mason1e701a32010-03-11 09:42:04 -0500673static int btrfs_defrag_file(struct file *file,
674 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400675{
676 struct inode *inode = fdentry(file)->d_inode;
677 struct btrfs_root *root = BTRFS_I(inode)->root;
678 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400679 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400680 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800681 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400682 unsigned long last_index;
683 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
684 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800685 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400686 u64 page_start;
687 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500688 u64 last_len = 0;
689 u64 skip = 0;
690 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400691 unsigned long i;
692 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800693 int compress_type = BTRFS_COMPRESS_ZLIB;
694
695 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
696 if (range->compress_type > BTRFS_COMPRESS_TYPES)
697 return -EINVAL;
698 if (range->compress_type)
699 compress_type = range->compress_type;
700 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400701
Chris Mason940100a2010-03-10 10:52:59 -0500702 if (inode->i_size == 0)
703 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400704
Chris Mason1e701a32010-03-11 09:42:04 -0500705 if (range->start + range->len > range->start) {
706 last_index = min_t(u64, inode->i_size - 1,
707 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
708 } else {
709 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
710 }
711
712 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500713 while (i <= last_index) {
714 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500715 PAGE_CACHE_SIZE,
716 range->extent_thresh,
717 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500718 &defrag_end)) {
719 unsigned long next;
720 /*
721 * the should_defrag function tells us how much to skip
722 * bump our counter by the suggested amount
723 */
724 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
725 i = max(i + 1, next);
726 continue;
727 }
728
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729 if (total_read % ra_pages == 0) {
730 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
731 min(last_index, i + ra_pages - 1));
732 }
733 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500734 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500735 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800736 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500737
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400738 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
739 if (ret)
740 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400741again:
Chris Mason940100a2010-03-10 10:52:59 -0500742 if (inode->i_size == 0 ||
743 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
744 ret = 0;
745 goto err_reservations;
746 }
747
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400749 if (!page) {
750 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500751 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400752 }
Chris Mason940100a2010-03-10 10:52:59 -0500753
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754 if (!PageUptodate(page)) {
755 btrfs_readpage(NULL, page);
756 lock_page(page);
757 if (!PageUptodate(page)) {
758 unlock_page(page);
759 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400760 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500761 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400762 }
763 }
764
Chris Mason940100a2010-03-10 10:52:59 -0500765 if (page->mapping != inode->i_mapping) {
766 unlock_page(page);
767 page_cache_release(page);
768 goto again;
769 }
770
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400771 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400772
Chris Mason940100a2010-03-10 10:52:59 -0500773 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400774 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500775 goto loop_unlock;
776 }
777
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400778 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
779 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400780 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400781
782 ordered = btrfs_lookup_ordered_extent(inode, page_start);
783 if (ordered) {
784 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
785 unlock_page(page);
786 page_cache_release(page);
787 btrfs_start_ordered_extent(inode, ordered, 1);
788 btrfs_put_ordered_extent(ordered);
789 goto again;
790 }
791 set_page_extent_mapped(page);
792
Chris Masonf87f0572008-08-01 11:27:23 -0400793 /*
794 * this makes sure page_mkwrite is called on the
795 * page if it is dirtied again later
796 */
797 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500798 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
799 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
800 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400801
Josef Bacik2ac55d42010-02-03 19:33:23 +0000802 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500803 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400804 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400805 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500806
807loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400808 unlock_page(page);
809 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500810 mutex_unlock(&inode->i_mutex);
811
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400812 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500813 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400814 }
815
Chris Mason1e701a32010-03-11 09:42:04 -0500816 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
817 filemap_flush(inode->i_mapping);
818
819 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
820 /* the filemap_flush will queue IO into the worker threads, but
821 * we have to make sure the IO is actually started and that
822 * ordered extents get created before we return
823 */
824 atomic_inc(&root->fs_info->async_submit_draining);
825 while (atomic_read(&root->fs_info->nr_async_submits) ||
826 atomic_read(&root->fs_info->async_delalloc_pages)) {
827 wait_event(root->fs_info->async_submit_wait,
828 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
829 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
830 }
831 atomic_dec(&root->fs_info->async_submit_draining);
832
833 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800834 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500835 mutex_unlock(&inode->i_mutex);
836 }
837
Li Zefan1a419d82010-10-25 15:12:50 +0800838 disk_super = &root->fs_info->super_copy;
839 features = btrfs_super_incompat_flags(disk_super);
840 if (range->compress_type == BTRFS_COMPRESS_LZO) {
841 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
842 btrfs_set_super_incompat_flags(disk_super, features);
843 }
844
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400845 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500846
847err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400848 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
849err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500850 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500851 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400852}
853
Yan, Zheng76dda932009-09-21 16:00:26 -0400854static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
855 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400856{
857 u64 new_size;
858 u64 old_size;
859 u64 devid = 1;
860 struct btrfs_ioctl_vol_args *vol_args;
861 struct btrfs_trans_handle *trans;
862 struct btrfs_device *device = NULL;
863 char *sizestr;
864 char *devstr = NULL;
865 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400866 int mod = 0;
867
Yan Zhengc146afa2008-11-12 14:34:12 -0500868 if (root->fs_info->sb->s_flags & MS_RDONLY)
869 return -EROFS;
870
Chris Masone441d542009-01-05 16:57:23 -0500871 if (!capable(CAP_SYS_ADMIN))
872 return -EPERM;
873
Li Zefandae7b662009-04-08 15:06:54 +0800874 vol_args = memdup_user(arg, sizeof(*vol_args));
875 if (IS_ERR(vol_args))
876 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400877
878 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400879
Chris Mason7d9eb122008-07-08 14:19:17 -0400880 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400881 sizestr = vol_args->name;
882 devstr = strchr(sizestr, ':');
883 if (devstr) {
884 char *end;
885 sizestr = devstr + 1;
886 *devstr = '\0';
887 devstr = vol_args->name;
888 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700889 printk(KERN_INFO "resizing devid %llu\n",
890 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400891 }
Yan Zheng2b820322008-11-17 21:11:30 -0500892 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400893 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700894 printk(KERN_INFO "resizer unable to find device %llu\n",
895 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400896 ret = -EINVAL;
897 goto out_unlock;
898 }
899 if (!strcmp(sizestr, "max"))
900 new_size = device->bdev->bd_inode->i_size;
901 else {
902 if (sizestr[0] == '-') {
903 mod = -1;
904 sizestr++;
905 } else if (sizestr[0] == '+') {
906 mod = 1;
907 sizestr++;
908 }
Akinobu Mita91748462010-02-28 10:59:11 +0000909 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400910 if (new_size == 0) {
911 ret = -EINVAL;
912 goto out_unlock;
913 }
914 }
915
916 old_size = device->total_bytes;
917
918 if (mod < 0) {
919 if (new_size > old_size) {
920 ret = -EINVAL;
921 goto out_unlock;
922 }
923 new_size = old_size - new_size;
924 } else if (mod > 0) {
925 new_size = old_size + new_size;
926 }
927
928 if (new_size < 256 * 1024 * 1024) {
929 ret = -EINVAL;
930 goto out_unlock;
931 }
932 if (new_size > device->bdev->bd_inode->i_size) {
933 ret = -EFBIG;
934 goto out_unlock;
935 }
936
937 do_div(new_size, root->sectorsize);
938 new_size *= root->sectorsize;
939
940 printk(KERN_INFO "new size for %s is %llu\n",
941 device->name, (unsigned long long)new_size);
942
943 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400944 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +0000945 if (IS_ERR(trans)) {
946 ret = PTR_ERR(trans);
947 goto out_unlock;
948 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400949 ret = btrfs_grow_device(trans, device, new_size);
950 btrfs_commit_transaction(trans, root);
951 } else {
952 ret = btrfs_shrink_device(device, new_size);
953 }
954
955out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400956 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400957 kfree(vol_args);
958 return ret;
959}
960
Sage Weil72fd0322010-10-29 15:41:32 -0400961static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
962 char *name,
963 unsigned long fd,
964 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +0800965 u64 *transid,
966 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400967{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400968 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -0500969 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400970 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500971 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400972
Yan Zhengc146afa2008-11-12 14:34:12 -0500973 if (root->fs_info->sb->s_flags & MS_RDONLY)
974 return -EROFS;
975
Sage Weil72fd0322010-10-29 15:41:32 -0400976 namelen = strlen(name);
977 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400978 ret = -EINVAL;
979 goto out;
980 }
981
Chris Mason3de45862008-11-17 21:02:50 -0500982 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -0400983 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +0800984 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400985 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500986 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -0400987 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -0500988 if (!src_file) {
989 ret = -EINVAL;
990 goto out;
991 }
992
993 src_inode = src_file->f_path.dentry->d_inode;
994 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500995 printk(KERN_INFO "btrfs: Snapshot src from "
996 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500997 ret = -EINVAL;
998 fput(src_file);
999 goto out;
1000 }
Sage Weil72fd0322010-10-29 15:41:32 -04001001 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1002 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001003 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001004 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001005 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006out:
Sage Weil72fd0322010-10-29 15:41:32 -04001007 return ret;
1008}
1009
1010static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001011 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001012{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001013 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001014 int ret;
1015
Li Zefanfa0d2b92010-12-20 15:53:28 +08001016 vol_args = memdup_user(arg, sizeof(*vol_args));
1017 if (IS_ERR(vol_args))
1018 return PTR_ERR(vol_args);
1019 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001020
Li Zefanfa0d2b92010-12-20 15:53:28 +08001021 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001022 vol_args->fd, subvol,
1023 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001024
Li Zefanfa0d2b92010-12-20 15:53:28 +08001025 kfree(vol_args);
1026 return ret;
1027}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001028
Li Zefanfa0d2b92010-12-20 15:53:28 +08001029static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1030 void __user *arg, int subvol)
1031{
1032 struct btrfs_ioctl_vol_args_v2 *vol_args;
1033 int ret;
1034 u64 transid = 0;
1035 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001036 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001037
Li Zefanfa0d2b92010-12-20 15:53:28 +08001038 vol_args = memdup_user(arg, sizeof(*vol_args));
1039 if (IS_ERR(vol_args))
1040 return PTR_ERR(vol_args);
1041 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001042
Li Zefanb83cc962010-12-20 16:04:08 +08001043 if (vol_args->flags &
1044 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1045 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001046 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001047 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001048
1049 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1050 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001051 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1052 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001053
1054 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001055 vol_args->fd, subvol,
1056 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001057
1058 if (ret == 0 && ptr &&
1059 copy_to_user(arg +
1060 offsetof(struct btrfs_ioctl_vol_args_v2,
1061 transid), ptr, sizeof(*ptr)))
1062 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001063out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001064 kfree(vol_args);
1065 return ret;
1066}
1067
Li Zefan0caa1022010-12-20 16:30:25 +08001068static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1069 void __user *arg)
1070{
1071 struct inode *inode = fdentry(file)->d_inode;
1072 struct btrfs_root *root = BTRFS_I(inode)->root;
1073 int ret = 0;
1074 u64 flags = 0;
1075
1076 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1077 return -EINVAL;
1078
1079 down_read(&root->fs_info->subvol_sem);
1080 if (btrfs_root_readonly(root))
1081 flags |= BTRFS_SUBVOL_RDONLY;
1082 up_read(&root->fs_info->subvol_sem);
1083
1084 if (copy_to_user(arg, &flags, sizeof(flags)))
1085 ret = -EFAULT;
1086
1087 return ret;
1088}
1089
1090static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1091 void __user *arg)
1092{
1093 struct inode *inode = fdentry(file)->d_inode;
1094 struct btrfs_root *root = BTRFS_I(inode)->root;
1095 struct btrfs_trans_handle *trans;
1096 u64 root_flags;
1097 u64 flags;
1098 int ret = 0;
1099
1100 if (root->fs_info->sb->s_flags & MS_RDONLY)
1101 return -EROFS;
1102
1103 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1104 return -EINVAL;
1105
1106 if (copy_from_user(&flags, arg, sizeof(flags)))
1107 return -EFAULT;
1108
Li Zefanb4dc2b82011-02-16 06:06:34 +00001109 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001110 return -EINVAL;
1111
1112 if (flags & ~BTRFS_SUBVOL_RDONLY)
1113 return -EOPNOTSUPP;
1114
Li Zefanb4dc2b82011-02-16 06:06:34 +00001115 if (!is_owner_or_cap(inode))
1116 return -EACCES;
1117
Li Zefan0caa1022010-12-20 16:30:25 +08001118 down_write(&root->fs_info->subvol_sem);
1119
1120 /* nothing to do */
1121 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1122 goto out;
1123
1124 root_flags = btrfs_root_flags(&root->root_item);
1125 if (flags & BTRFS_SUBVOL_RDONLY)
1126 btrfs_set_root_flags(&root->root_item,
1127 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1128 else
1129 btrfs_set_root_flags(&root->root_item,
1130 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1131
1132 trans = btrfs_start_transaction(root, 1);
1133 if (IS_ERR(trans)) {
1134 ret = PTR_ERR(trans);
1135 goto out_reset;
1136 }
1137
Li Zefanb4dc2b82011-02-16 06:06:34 +00001138 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001139 &root->root_key, &root->root_item);
1140
1141 btrfs_commit_transaction(trans, root);
1142out_reset:
1143 if (ret)
1144 btrfs_set_root_flags(&root->root_item, root_flags);
1145out:
1146 up_write(&root->fs_info->subvol_sem);
1147 return ret;
1148}
1149
Yan, Zheng76dda932009-09-21 16:00:26 -04001150/*
1151 * helper to check if the subvolume references other subvolumes
1152 */
1153static noinline int may_destroy_subvol(struct btrfs_root *root)
1154{
1155 struct btrfs_path *path;
1156 struct btrfs_key key;
1157 int ret;
1158
1159 path = btrfs_alloc_path();
1160 if (!path)
1161 return -ENOMEM;
1162
1163 key.objectid = root->root_key.objectid;
1164 key.type = BTRFS_ROOT_REF_KEY;
1165 key.offset = (u64)-1;
1166
1167 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1168 &key, path, 0, 0);
1169 if (ret < 0)
1170 goto out;
1171 BUG_ON(ret == 0);
1172
1173 ret = 0;
1174 if (path->slots[0] > 0) {
1175 path->slots[0]--;
1176 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1177 if (key.objectid == root->root_key.objectid &&
1178 key.type == BTRFS_ROOT_REF_KEY)
1179 ret = -ENOTEMPTY;
1180 }
1181out:
1182 btrfs_free_path(path);
1183 return ret;
1184}
1185
Chris Masonac8e9812010-02-28 15:39:26 -05001186static noinline int key_in_sk(struct btrfs_key *key,
1187 struct btrfs_ioctl_search_key *sk)
1188{
Chris Masonabc6e132010-03-18 12:10:08 -04001189 struct btrfs_key test;
1190 int ret;
1191
1192 test.objectid = sk->min_objectid;
1193 test.type = sk->min_type;
1194 test.offset = sk->min_offset;
1195
1196 ret = btrfs_comp_cpu_keys(key, &test);
1197 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001198 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001199
1200 test.objectid = sk->max_objectid;
1201 test.type = sk->max_type;
1202 test.offset = sk->max_offset;
1203
1204 ret = btrfs_comp_cpu_keys(key, &test);
1205 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001206 return 0;
1207 return 1;
1208}
1209
1210static noinline int copy_to_sk(struct btrfs_root *root,
1211 struct btrfs_path *path,
1212 struct btrfs_key *key,
1213 struct btrfs_ioctl_search_key *sk,
1214 char *buf,
1215 unsigned long *sk_offset,
1216 int *num_found)
1217{
1218 u64 found_transid;
1219 struct extent_buffer *leaf;
1220 struct btrfs_ioctl_search_header sh;
1221 unsigned long item_off;
1222 unsigned long item_len;
1223 int nritems;
1224 int i;
1225 int slot;
1226 int found = 0;
1227 int ret = 0;
1228
1229 leaf = path->nodes[0];
1230 slot = path->slots[0];
1231 nritems = btrfs_header_nritems(leaf);
1232
1233 if (btrfs_header_generation(leaf) > sk->max_transid) {
1234 i = nritems;
1235 goto advance_key;
1236 }
1237 found_transid = btrfs_header_generation(leaf);
1238
1239 for (i = slot; i < nritems; i++) {
1240 item_off = btrfs_item_ptr_offset(leaf, i);
1241 item_len = btrfs_item_size_nr(leaf, i);
1242
1243 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1244 item_len = 0;
1245
1246 if (sizeof(sh) + item_len + *sk_offset >
1247 BTRFS_SEARCH_ARGS_BUFSIZE) {
1248 ret = 1;
1249 goto overflow;
1250 }
1251
1252 btrfs_item_key_to_cpu(leaf, key, i);
1253 if (!key_in_sk(key, sk))
1254 continue;
1255
1256 sh.objectid = key->objectid;
1257 sh.offset = key->offset;
1258 sh.type = key->type;
1259 sh.len = item_len;
1260 sh.transid = found_transid;
1261
1262 /* copy search result header */
1263 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1264 *sk_offset += sizeof(sh);
1265
1266 if (item_len) {
1267 char *p = buf + *sk_offset;
1268 /* copy the item */
1269 read_extent_buffer(leaf, p,
1270 item_off, item_len);
1271 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001272 }
Chris Mason90fdde12010-03-18 12:14:54 -04001273 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001274
1275 if (*num_found >= sk->nr_items)
1276 break;
1277 }
1278advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001279 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001280 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1281 key->offset++;
1282 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1283 key->offset = 0;
1284 key->type++;
1285 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1286 key->offset = 0;
1287 key->type = 0;
1288 key->objectid++;
1289 } else
1290 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001291overflow:
1292 *num_found += found;
1293 return ret;
1294}
1295
1296static noinline int search_ioctl(struct inode *inode,
1297 struct btrfs_ioctl_search_args *args)
1298{
1299 struct btrfs_root *root;
1300 struct btrfs_key key;
1301 struct btrfs_key max_key;
1302 struct btrfs_path *path;
1303 struct btrfs_ioctl_search_key *sk = &args->key;
1304 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1305 int ret;
1306 int num_found = 0;
1307 unsigned long sk_offset = 0;
1308
1309 path = btrfs_alloc_path();
1310 if (!path)
1311 return -ENOMEM;
1312
1313 if (sk->tree_id == 0) {
1314 /* search the root of the inode that was passed */
1315 root = BTRFS_I(inode)->root;
1316 } else {
1317 key.objectid = sk->tree_id;
1318 key.type = BTRFS_ROOT_ITEM_KEY;
1319 key.offset = (u64)-1;
1320 root = btrfs_read_fs_root_no_name(info, &key);
1321 if (IS_ERR(root)) {
1322 printk(KERN_ERR "could not find root %llu\n",
1323 sk->tree_id);
1324 btrfs_free_path(path);
1325 return -ENOENT;
1326 }
1327 }
1328
1329 key.objectid = sk->min_objectid;
1330 key.type = sk->min_type;
1331 key.offset = sk->min_offset;
1332
1333 max_key.objectid = sk->max_objectid;
1334 max_key.type = sk->max_type;
1335 max_key.offset = sk->max_offset;
1336
1337 path->keep_locks = 1;
1338
1339 while(1) {
1340 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1341 sk->min_transid);
1342 if (ret != 0) {
1343 if (ret > 0)
1344 ret = 0;
1345 goto err;
1346 }
1347 ret = copy_to_sk(root, path, &key, sk, args->buf,
1348 &sk_offset, &num_found);
1349 btrfs_release_path(root, path);
1350 if (ret || num_found >= sk->nr_items)
1351 break;
1352
1353 }
1354 ret = 0;
1355err:
1356 sk->nr_items = num_found;
1357 btrfs_free_path(path);
1358 return ret;
1359}
1360
1361static noinline int btrfs_ioctl_tree_search(struct file *file,
1362 void __user *argp)
1363{
1364 struct btrfs_ioctl_search_args *args;
1365 struct inode *inode;
1366 int ret;
1367
1368 if (!capable(CAP_SYS_ADMIN))
1369 return -EPERM;
1370
Julia Lawall2354d082010-10-29 15:14:18 -04001371 args = memdup_user(argp, sizeof(*args));
1372 if (IS_ERR(args))
1373 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001374
Chris Masonac8e9812010-02-28 15:39:26 -05001375 inode = fdentry(file)->d_inode;
1376 ret = search_ioctl(inode, args);
1377 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1378 ret = -EFAULT;
1379 kfree(args);
1380 return ret;
1381}
1382
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001383/*
Chris Masonac8e9812010-02-28 15:39:26 -05001384 * Search INODE_REFs to identify path name of 'dirid' directory
1385 * in a 'tree_id' tree. and sets path name to 'name'.
1386 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001387static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1388 u64 tree_id, u64 dirid, char *name)
1389{
1390 struct btrfs_root *root;
1391 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001392 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001393 int ret = -1;
1394 int slot;
1395 int len;
1396 int total_len = 0;
1397 struct btrfs_inode_ref *iref;
1398 struct extent_buffer *l;
1399 struct btrfs_path *path;
1400
1401 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1402 name[0]='\0';
1403 return 0;
1404 }
1405
1406 path = btrfs_alloc_path();
1407 if (!path)
1408 return -ENOMEM;
1409
Chris Masonac8e9812010-02-28 15:39:26 -05001410 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001411
1412 key.objectid = tree_id;
1413 key.type = BTRFS_ROOT_ITEM_KEY;
1414 key.offset = (u64)-1;
1415 root = btrfs_read_fs_root_no_name(info, &key);
1416 if (IS_ERR(root)) {
1417 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001418 ret = -ENOENT;
1419 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001420 }
1421
1422 key.objectid = dirid;
1423 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001424 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001425
1426 while(1) {
1427 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1428 if (ret < 0)
1429 goto out;
1430
1431 l = path->nodes[0];
1432 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001433 if (ret > 0 && slot > 0)
1434 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001435 btrfs_item_key_to_cpu(l, &key, slot);
1436
1437 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001438 key.type != BTRFS_INODE_REF_KEY)) {
1439 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001440 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001441 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001442
1443 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1444 len = btrfs_inode_ref_name_len(l, iref);
1445 ptr -= len + 1;
1446 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001447 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001448 goto out;
1449
1450 *(ptr + len) = '/';
1451 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1452
1453 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1454 break;
1455
1456 btrfs_release_path(root, path);
1457 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001458 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001459 dirid = key.objectid;
1460
1461 }
Chris Masonac8e9812010-02-28 15:39:26 -05001462 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001463 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001464 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001465 name[total_len]='\0';
1466 ret = 0;
1467out:
1468 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001469 return ret;
1470}
1471
1472static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1473 void __user *argp)
1474{
1475 struct btrfs_ioctl_ino_lookup_args *args;
1476 struct inode *inode;
1477 int ret;
1478
1479 if (!capable(CAP_SYS_ADMIN))
1480 return -EPERM;
1481
Julia Lawall2354d082010-10-29 15:14:18 -04001482 args = memdup_user(argp, sizeof(*args));
1483 if (IS_ERR(args))
1484 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001485
Chris Masonac8e9812010-02-28 15:39:26 -05001486 inode = fdentry(file)->d_inode;
1487
Chris Mason1b53ac42010-03-18 12:17:05 -04001488 if (args->treeid == 0)
1489 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1490
Chris Masonac8e9812010-02-28 15:39:26 -05001491 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1492 args->treeid, args->objectid,
1493 args->name);
1494
1495 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1496 ret = -EFAULT;
1497
1498 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001499 return ret;
1500}
1501
Yan, Zheng76dda932009-09-21 16:00:26 -04001502static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1503 void __user *arg)
1504{
1505 struct dentry *parent = fdentry(file);
1506 struct dentry *dentry;
1507 struct inode *dir = parent->d_inode;
1508 struct inode *inode;
1509 struct btrfs_root *root = BTRFS_I(dir)->root;
1510 struct btrfs_root *dest = NULL;
1511 struct btrfs_ioctl_vol_args *vol_args;
1512 struct btrfs_trans_handle *trans;
1513 int namelen;
1514 int ret;
1515 int err = 0;
1516
Yan, Zheng76dda932009-09-21 16:00:26 -04001517 vol_args = memdup_user(arg, sizeof(*vol_args));
1518 if (IS_ERR(vol_args))
1519 return PTR_ERR(vol_args);
1520
1521 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1522 namelen = strlen(vol_args->name);
1523 if (strchr(vol_args->name, '/') ||
1524 strncmp(vol_args->name, "..", namelen) == 0) {
1525 err = -EINVAL;
1526 goto out;
1527 }
1528
1529 err = mnt_want_write(file->f_path.mnt);
1530 if (err)
1531 goto out;
1532
1533 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1534 dentry = lookup_one_len(vol_args->name, parent, namelen);
1535 if (IS_ERR(dentry)) {
1536 err = PTR_ERR(dentry);
1537 goto out_unlock_dir;
1538 }
1539
1540 if (!dentry->d_inode) {
1541 err = -ENOENT;
1542 goto out_dput;
1543 }
1544
1545 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001546 dest = BTRFS_I(inode)->root;
1547 if (!capable(CAP_SYS_ADMIN)){
1548 /*
1549 * Regular user. Only allow this with a special mount
1550 * option, when the user has write+exec access to the
1551 * subvol root, and when rmdir(2) would have been
1552 * allowed.
1553 *
1554 * Note that this is _not_ check that the subvol is
1555 * empty or doesn't contain data that we wouldn't
1556 * otherwise be able to delete.
1557 *
1558 * Users who want to delete empty subvols should try
1559 * rmdir(2).
1560 */
1561 err = -EPERM;
1562 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1563 goto out_dput;
1564
1565 /*
1566 * Do not allow deletion if the parent dir is the same
1567 * as the dir to be deleted. That means the ioctl
1568 * must be called on the dentry referencing the root
1569 * of the subvol, not a random directory contained
1570 * within it.
1571 */
1572 err = -EINVAL;
1573 if (root == dest)
1574 goto out_dput;
1575
1576 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1577 if (err)
1578 goto out_dput;
1579
1580 /* check if subvolume may be deleted by a non-root user */
1581 err = btrfs_may_delete(dir, dentry, 1);
1582 if (err)
1583 goto out_dput;
1584 }
1585
Yan, Zheng76dda932009-09-21 16:00:26 -04001586 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1587 err = -EINVAL;
1588 goto out_dput;
1589 }
1590
Yan, Zheng76dda932009-09-21 16:00:26 -04001591 mutex_lock(&inode->i_mutex);
1592 err = d_invalidate(dentry);
1593 if (err)
1594 goto out_unlock;
1595
1596 down_write(&root->fs_info->subvol_sem);
1597
1598 err = may_destroy_subvol(dest);
1599 if (err)
1600 goto out_up_write;
1601
Yan, Zhenga22285a2010-05-16 10:48:46 -04001602 trans = btrfs_start_transaction(root, 0);
1603 if (IS_ERR(trans)) {
1604 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001605 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001606 }
1607 trans->block_rsv = &root->fs_info->global_block_rsv;
1608
Yan, Zheng76dda932009-09-21 16:00:26 -04001609 ret = btrfs_unlink_subvol(trans, root, dir,
1610 dest->root_key.objectid,
1611 dentry->d_name.name,
1612 dentry->d_name.len);
1613 BUG_ON(ret);
1614
1615 btrfs_record_root_in_trans(trans, dest);
1616
1617 memset(&dest->root_item.drop_progress, 0,
1618 sizeof(dest->root_item.drop_progress));
1619 dest->root_item.drop_level = 0;
1620 btrfs_set_root_refs(&dest->root_item, 0);
1621
Yan, Zhengd68fc572010-05-16 10:49:58 -04001622 if (!xchg(&dest->orphan_item_inserted, 1)) {
1623 ret = btrfs_insert_orphan_item(trans,
1624 root->fs_info->tree_root,
1625 dest->root_key.objectid);
1626 BUG_ON(ret);
1627 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001628
Sage Weil531cb132010-10-29 15:41:32 -04001629 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001630 BUG_ON(ret);
1631 inode->i_flags |= S_DEAD;
1632out_up_write:
1633 up_write(&root->fs_info->subvol_sem);
1634out_unlock:
1635 mutex_unlock(&inode->i_mutex);
1636 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001637 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001638 btrfs_invalidate_inodes(dest);
1639 d_delete(dentry);
1640 }
1641out_dput:
1642 dput(dentry);
1643out_unlock_dir:
1644 mutex_unlock(&dir->i_mutex);
1645 mnt_drop_write(file->f_path.mnt);
1646out:
1647 kfree(vol_args);
1648 return err;
1649}
1650
Chris Mason1e701a32010-03-11 09:42:04 -05001651static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001652{
1653 struct inode *inode = fdentry(file)->d_inode;
1654 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001655 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001656 int ret;
1657
Li Zefanb83cc962010-12-20 16:04:08 +08001658 if (btrfs_root_readonly(root))
1659 return -EROFS;
1660
Yan Zhengc146afa2008-11-12 14:34:12 -05001661 ret = mnt_want_write(file->f_path.mnt);
1662 if (ret)
1663 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001664
1665 switch (inode->i_mode & S_IFMT) {
1666 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001667 if (!capable(CAP_SYS_ADMIN)) {
1668 ret = -EPERM;
1669 goto out;
1670 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001671 ret = btrfs_defrag_root(root, 0);
1672 if (ret)
1673 goto out;
1674 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001675 break;
1676 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001677 if (!(file->f_mode & FMODE_WRITE)) {
1678 ret = -EINVAL;
1679 goto out;
1680 }
Chris Mason1e701a32010-03-11 09:42:04 -05001681
1682 range = kzalloc(sizeof(*range), GFP_KERNEL);
1683 if (!range) {
1684 ret = -ENOMEM;
1685 goto out;
1686 }
1687
1688 if (argp) {
1689 if (copy_from_user(range, argp,
1690 sizeof(*range))) {
1691 ret = -EFAULT;
1692 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001693 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001694 }
1695 /* compression requires us to start the IO */
1696 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1697 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1698 range->extent_thresh = (u32)-1;
1699 }
1700 } else {
1701 /* the rest are all set to zero by kzalloc */
1702 range->len = (u64)-1;
1703 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001704 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001705 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001707 default:
1708 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001709 }
Chris Masone441d542009-01-05 16:57:23 -05001710out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001711 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001712 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001713}
1714
Christoph Hellwigb2950862008-12-02 09:54:17 -05001715static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001716{
1717 struct btrfs_ioctl_vol_args *vol_args;
1718 int ret;
1719
Chris Masone441d542009-01-05 16:57:23 -05001720 if (!capable(CAP_SYS_ADMIN))
1721 return -EPERM;
1722
Li Zefandae7b662009-04-08 15:06:54 +08001723 vol_args = memdup_user(arg, sizeof(*vol_args));
1724 if (IS_ERR(vol_args))
1725 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001726
Mark Fasheh5516e592008-07-24 12:20:14 -04001727 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001728 ret = btrfs_init_new_device(root, vol_args->name);
1729
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 kfree(vol_args);
1731 return ret;
1732}
1733
Christoph Hellwigb2950862008-12-02 09:54:17 -05001734static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001735{
1736 struct btrfs_ioctl_vol_args *vol_args;
1737 int ret;
1738
Chris Masone441d542009-01-05 16:57:23 -05001739 if (!capable(CAP_SYS_ADMIN))
1740 return -EPERM;
1741
Yan Zhengc146afa2008-11-12 14:34:12 -05001742 if (root->fs_info->sb->s_flags & MS_RDONLY)
1743 return -EROFS;
1744
Li Zefandae7b662009-04-08 15:06:54 +08001745 vol_args = memdup_user(arg, sizeof(*vol_args));
1746 if (IS_ERR(vol_args))
1747 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001748
Mark Fasheh5516e592008-07-24 12:20:14 -04001749 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 ret = btrfs_rm_device(root, vol_args->name);
1751
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001752 kfree(vol_args);
1753 return ret;
1754}
1755
Yan, Zheng76dda932009-09-21 16:00:26 -04001756static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1757 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001758{
1759 struct inode *inode = fdentry(file)->d_inode;
1760 struct btrfs_root *root = BTRFS_I(inode)->root;
1761 struct file *src_file;
1762 struct inode *src;
1763 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001765 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001766 char *buf;
1767 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001768 u32 nritems;
1769 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001770 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001771 u64 len = olen;
1772 u64 bs = root->fs_info->sb->s_blocksize;
1773 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001774
Sage Weilc5c9cd42008-11-12 14:32:25 -05001775 /*
1776 * TODO:
1777 * - split compressed inline extents. annoying: we need to
1778 * decompress into destination's address_space (the file offset
1779 * may change, so source mapping won't do), then recompress (or
1780 * otherwise reinsert) a subrange.
1781 * - allow ranges within the same file to be cloned (provided
1782 * they don't overlap)?
1783 */
1784
Chris Masone441d542009-01-05 16:57:23 -05001785 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001786 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001787 return -EINVAL;
1788
Li Zefanb83cc962010-12-20 16:04:08 +08001789 if (btrfs_root_readonly(root))
1790 return -EROFS;
1791
Yan Zhengc146afa2008-11-12 14:34:12 -05001792 ret = mnt_want_write(file->f_path.mnt);
1793 if (ret)
1794 return ret;
1795
Sage Weilc5c9cd42008-11-12 14:32:25 -05001796 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001797 if (!src_file) {
1798 ret = -EBADF;
1799 goto out_drop_write;
1800 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001801
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001802 src = src_file->f_dentry->d_inode;
1803
Sage Weilc5c9cd42008-11-12 14:32:25 -05001804 ret = -EINVAL;
1805 if (src == inode)
1806 goto out_fput;
1807
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001808 /* the src must be open for reading */
1809 if (!(src_file->f_mode & FMODE_READ))
1810 goto out_fput;
1811
Yan Zhengae01a0a2008-08-04 23:23:47 -04001812 ret = -EISDIR;
1813 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001814 goto out_fput;
1815
Yan Zhengae01a0a2008-08-04 23:23:47 -04001816 ret = -EXDEV;
1817 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1818 goto out_fput;
1819
1820 ret = -ENOMEM;
1821 buf = vmalloc(btrfs_level_size(root, 0));
1822 if (!buf)
1823 goto out_fput;
1824
1825 path = btrfs_alloc_path();
1826 if (!path) {
1827 vfree(buf);
1828 goto out_fput;
1829 }
1830 path->reada = 2;
1831
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001832 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001833 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1834 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001835 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001836 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1837 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001838 }
1839
Sage Weilc5c9cd42008-11-12 14:32:25 -05001840 /* determine range to clone */
1841 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001842 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001843 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001844 if (len == 0)
1845 olen = len = src->i_size - off;
1846 /* if we extend to eof, continue to block boundary */
1847 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001848 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001849
1850 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001851 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1852 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001853 goto out_unlock;
1854
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001855 /* do any pending delalloc/csum calc on src, one way or
1856 another, and lock file content */
1857 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001858 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001859 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001860 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1861 if (!ordered &&
1862 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1863 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001864 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001865 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001866 if (ordered)
1867 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001868 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001869 }
1870
Sage Weilc5c9cd42008-11-12 14:32:25 -05001871 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001872 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001873 key.type = BTRFS_EXTENT_DATA_KEY;
1874 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001875
1876 while (1) {
1877 /*
1878 * note the key will change type as we walk through the
1879 * tree.
1880 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001881 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001882 if (ret < 0)
1883 goto out;
1884
Yan Zhengae01a0a2008-08-04 23:23:47 -04001885 nritems = btrfs_header_nritems(path->nodes[0]);
1886 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001887 ret = btrfs_next_leaf(root, path);
1888 if (ret < 0)
1889 goto out;
1890 if (ret > 0)
1891 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001892 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001893 }
1894 leaf = path->nodes[0];
1895 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001896
Yan Zhengae01a0a2008-08-04 23:23:47 -04001897 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001898 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001899 key.objectid != src->i_ino)
1900 break;
1901
Sage Weilc5c9cd42008-11-12 14:32:25 -05001902 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1903 struct btrfs_file_extent_item *extent;
1904 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001905 u32 size;
1906 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001907 u64 disko = 0, diskl = 0;
1908 u64 datao = 0, datal = 0;
1909 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001910 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001911
1912 size = btrfs_item_size_nr(leaf, slot);
1913 read_extent_buffer(leaf, buf,
1914 btrfs_item_ptr_offset(leaf, slot),
1915 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001916
1917 extent = btrfs_item_ptr(leaf, slot,
1918 struct btrfs_file_extent_item);
1919 comp = btrfs_file_extent_compression(leaf, extent);
1920 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001921 if (type == BTRFS_FILE_EXTENT_REG ||
1922 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001923 disko = btrfs_file_extent_disk_bytenr(leaf,
1924 extent);
1925 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1926 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001927 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001928 datal = btrfs_file_extent_num_bytes(leaf,
1929 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001930 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1931 /* take upper bound, may be compressed */
1932 datal = btrfs_file_extent_ram_bytes(leaf,
1933 extent);
1934 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001935 btrfs_release_path(root, path);
1936
Sage Weil050006a2010-10-29 15:37:33 -04001937 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001938 key.offset >= off+len)
1939 goto next;
1940
Zheng Yan31840ae2008-09-23 13:14:14 -04001941 memcpy(&new_key, &key, sizeof(new_key));
1942 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001943 if (off <= key.offset)
1944 new_key.offset = key.offset + destoff - off;
1945 else
1946 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001947
Yan, Zhenga22285a2010-05-16 10:48:46 -04001948 trans = btrfs_start_transaction(root, 1);
1949 if (IS_ERR(trans)) {
1950 ret = PTR_ERR(trans);
1951 goto out;
1952 }
1953
Chris Masonc8a894d2009-06-27 21:07:03 -04001954 if (type == BTRFS_FILE_EXTENT_REG ||
1955 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001956 if (off > key.offset) {
1957 datao += off - key.offset;
1958 datal -= off - key.offset;
1959 }
1960
1961 if (key.offset + datal > off + len)
1962 datal = off + len - key.offset;
1963
1964 ret = btrfs_drop_extents(trans, inode,
1965 new_key.offset,
1966 new_key.offset + datal,
1967 &hint_byte, 1);
1968 BUG_ON(ret);
1969
Sage Weilc5c9cd42008-11-12 14:32:25 -05001970 ret = btrfs_insert_empty_item(trans, root, path,
1971 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04001972 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001973
1974 leaf = path->nodes[0];
1975 slot = path->slots[0];
1976 write_extent_buffer(leaf, buf,
1977 btrfs_item_ptr_offset(leaf, slot),
1978 size);
1979
1980 extent = btrfs_item_ptr(leaf, slot,
1981 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001982
Sage Weilc5c9cd42008-11-12 14:32:25 -05001983 /* disko == 0 means it's a hole */
1984 if (!disko)
1985 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001986
1987 btrfs_set_file_extent_offset(leaf, extent,
1988 datao);
1989 btrfs_set_file_extent_num_bytes(leaf, extent,
1990 datal);
1991 if (disko) {
1992 inode_add_bytes(inode, datal);
1993 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001994 disko, diskl, 0,
1995 root->root_key.objectid,
1996 inode->i_ino,
1997 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001998 BUG_ON(ret);
1999 }
2000 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2001 u64 skip = 0;
2002 u64 trim = 0;
2003 if (off > key.offset) {
2004 skip = off - key.offset;
2005 new_key.offset += skip;
2006 }
Chris Masond3977122009-01-05 21:25:51 -05002007
Sage Weilc5c9cd42008-11-12 14:32:25 -05002008 if (key.offset + datal > off+len)
2009 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002010
Sage Weilc5c9cd42008-11-12 14:32:25 -05002011 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002012 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002013 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002014 goto out;
2015 }
2016 size -= skip + trim;
2017 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002018
2019 ret = btrfs_drop_extents(trans, inode,
2020 new_key.offset,
2021 new_key.offset + datal,
2022 &hint_byte, 1);
2023 BUG_ON(ret);
2024
Sage Weilc5c9cd42008-11-12 14:32:25 -05002025 ret = btrfs_insert_empty_item(trans, root, path,
2026 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002027 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002028
2029 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002030 u32 start =
2031 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002032 memmove(buf+start, buf+start+skip,
2033 datal);
2034 }
2035
2036 leaf = path->nodes[0];
2037 slot = path->slots[0];
2038 write_extent_buffer(leaf, buf,
2039 btrfs_item_ptr_offset(leaf, slot),
2040 size);
2041 inode_add_bytes(inode, datal);
2042 }
2043
2044 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002045 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002046
Yan, Zhenga22285a2010-05-16 10:48:46 -04002047 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002048
2049 /*
2050 * we round up to the block size at eof when
2051 * determining which extents to clone above,
2052 * but shouldn't round up the file size
2053 */
2054 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002055 if (endoff > destoff+olen)
2056 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002057 if (endoff > inode->i_size)
2058 btrfs_i_size_write(inode, endoff);
2059
Yan, Zhenga22285a2010-05-16 10:48:46 -04002060 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2061 ret = btrfs_update_inode(trans, root, inode);
2062 BUG_ON(ret);
2063 btrfs_end_transaction(trans, root);
2064 }
Chris Masond3977122009-01-05 21:25:51 -05002065next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002066 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002067 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002068 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002069 ret = 0;
2070out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002071 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002072 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002073out_unlock:
2074 mutex_unlock(&src->i_mutex);
2075 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002076 vfree(buf);
2077 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002078out_fput:
2079 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002080out_drop_write:
2081 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002082 return ret;
2083}
2084
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002085static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002086{
2087 struct btrfs_ioctl_clone_range_args args;
2088
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002089 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002090 return -EFAULT;
2091 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2092 args.src_length, args.dest_offset);
2093}
2094
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002095/*
2096 * there are many ways the trans_start and trans_end ioctls can lead
2097 * to deadlocks. They should only be used by applications that
2098 * basically own the machine, and have a very in depth understanding
2099 * of all the possible deadlocks and enospc problems.
2100 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002101static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002102{
2103 struct inode *inode = fdentry(file)->d_inode;
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002106 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002107
Sage Weil1ab86ae2009-09-29 18:38:44 -04002108 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002109 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002110 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002111
2112 ret = -EINPROGRESS;
2113 if (file->private_data)
2114 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002115
Li Zefanb83cc962010-12-20 16:04:08 +08002116 ret = -EROFS;
2117 if (btrfs_root_readonly(root))
2118 goto out;
2119
Yan Zhengc146afa2008-11-12 14:34:12 -05002120 ret = mnt_want_write(file->f_path.mnt);
2121 if (ret)
2122 goto out;
2123
Sage Weil9ca9ee02008-08-04 10:41:27 -04002124 mutex_lock(&root->fs_info->trans_mutex);
2125 root->fs_info->open_ioctl_trans++;
2126 mutex_unlock(&root->fs_info->trans_mutex);
2127
Sage Weil1ab86ae2009-09-29 18:38:44 -04002128 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002129 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002130 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002131 goto out_drop;
2132
2133 file->private_data = trans;
2134 return 0;
2135
2136out_drop:
2137 mutex_lock(&root->fs_info->trans_mutex);
2138 root->fs_info->open_ioctl_trans--;
2139 mutex_unlock(&root->fs_info->trans_mutex);
2140 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002141out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002142 return ret;
2143}
2144
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002145static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2146{
2147 struct inode *inode = fdentry(file)->d_inode;
2148 struct btrfs_root *root = BTRFS_I(inode)->root;
2149 struct btrfs_root *new_root;
2150 struct btrfs_dir_item *di;
2151 struct btrfs_trans_handle *trans;
2152 struct btrfs_path *path;
2153 struct btrfs_key location;
2154 struct btrfs_disk_key disk_key;
2155 struct btrfs_super_block *disk_super;
2156 u64 features;
2157 u64 objectid = 0;
2158 u64 dir_id;
2159
2160 if (!capable(CAP_SYS_ADMIN))
2161 return -EPERM;
2162
2163 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2164 return -EFAULT;
2165
2166 if (!objectid)
2167 objectid = root->root_key.objectid;
2168
2169 location.objectid = objectid;
2170 location.type = BTRFS_ROOT_ITEM_KEY;
2171 location.offset = (u64)-1;
2172
2173 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2174 if (IS_ERR(new_root))
2175 return PTR_ERR(new_root);
2176
2177 if (btrfs_root_refs(&new_root->root_item) == 0)
2178 return -ENOENT;
2179
2180 path = btrfs_alloc_path();
2181 if (!path)
2182 return -ENOMEM;
2183 path->leave_spinning = 1;
2184
2185 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002186 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002187 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002188 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002189 }
2190
2191 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2192 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2193 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002194 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002195 btrfs_free_path(path);
2196 btrfs_end_transaction(trans, root);
2197 printk(KERN_ERR "Umm, you don't have the default dir item, "
2198 "this isn't going to work\n");
2199 return -ENOENT;
2200 }
2201
2202 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2203 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2204 btrfs_mark_buffer_dirty(path->nodes[0]);
2205 btrfs_free_path(path);
2206
2207 disk_super = &root->fs_info->super_copy;
2208 features = btrfs_super_incompat_flags(disk_super);
2209 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2210 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2211 btrfs_set_super_incompat_flags(disk_super, features);
2212 }
2213 btrfs_end_transaction(trans, root);
2214
2215 return 0;
2216}
2217
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002218static void get_block_group_info(struct list_head *groups_list,
2219 struct btrfs_ioctl_space_info *space)
2220{
2221 struct btrfs_block_group_cache *block_group;
2222
2223 space->total_bytes = 0;
2224 space->used_bytes = 0;
2225 space->flags = 0;
2226 list_for_each_entry(block_group, groups_list, list) {
2227 space->flags = block_group->flags;
2228 space->total_bytes += block_group->key.offset;
2229 space->used_bytes +=
2230 btrfs_block_group_used(&block_group->item);
2231 }
2232}
2233
Josef Bacik1406e432010-01-13 18:19:06 +00002234long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2235{
2236 struct btrfs_ioctl_space_args space_args;
2237 struct btrfs_ioctl_space_info space;
2238 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002239 struct btrfs_ioctl_space_info *dest_orig;
2240 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002241 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002242 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2243 BTRFS_BLOCK_GROUP_SYSTEM,
2244 BTRFS_BLOCK_GROUP_METADATA,
2245 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2246 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002247 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002248 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002249 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002250 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002251
2252 if (copy_from_user(&space_args,
2253 (struct btrfs_ioctl_space_args __user *)arg,
2254 sizeof(space_args)))
2255 return -EFAULT;
2256
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002257 for (i = 0; i < num_types; i++) {
2258 struct btrfs_space_info *tmp;
2259
2260 info = NULL;
2261 rcu_read_lock();
2262 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2263 list) {
2264 if (tmp->flags == types[i]) {
2265 info = tmp;
2266 break;
2267 }
2268 }
2269 rcu_read_unlock();
2270
2271 if (!info)
2272 continue;
2273
2274 down_read(&info->groups_sem);
2275 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2276 if (!list_empty(&info->block_groups[c]))
2277 slot_count++;
2278 }
2279 up_read(&info->groups_sem);
2280 }
Josef Bacik1406e432010-01-13 18:19:06 +00002281
Chris Mason7fde62b2010-03-16 15:40:10 -04002282 /* space_slots == 0 means they are asking for a count */
2283 if (space_args.space_slots == 0) {
2284 space_args.total_spaces = slot_count;
2285 goto out;
2286 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002287
Dan Rosenberg51788b12011-02-14 16:04:23 -05002288 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002289
Chris Mason7fde62b2010-03-16 15:40:10 -04002290 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002291
Chris Mason7fde62b2010-03-16 15:40:10 -04002292 /* we generally have at most 6 or so space infos, one for each raid
2293 * level. So, a whole page should be more than enough for everyone
2294 */
2295 if (alloc_size > PAGE_CACHE_SIZE)
2296 return -ENOMEM;
2297
2298 space_args.total_spaces = 0;
2299 dest = kmalloc(alloc_size, GFP_NOFS);
2300 if (!dest)
2301 return -ENOMEM;
2302 dest_orig = dest;
2303
2304 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002305 for (i = 0; i < num_types; i++) {
2306 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002307
Dan Rosenberg51788b12011-02-14 16:04:23 -05002308 if (!slot_count)
2309 break;
2310
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002311 info = NULL;
2312 rcu_read_lock();
2313 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2314 list) {
2315 if (tmp->flags == types[i]) {
2316 info = tmp;
2317 break;
2318 }
2319 }
2320 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002321
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002322 if (!info)
2323 continue;
2324 down_read(&info->groups_sem);
2325 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2326 if (!list_empty(&info->block_groups[c])) {
2327 get_block_group_info(&info->block_groups[c],
2328 &space);
2329 memcpy(dest, &space, sizeof(space));
2330 dest++;
2331 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002332 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002333 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002334 if (!slot_count)
2335 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002336 }
2337 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002338 }
Josef Bacik1406e432010-01-13 18:19:06 +00002339
Chris Mason7fde62b2010-03-16 15:40:10 -04002340 user_dest = (struct btrfs_ioctl_space_info *)
2341 (arg + sizeof(struct btrfs_ioctl_space_args));
2342
2343 if (copy_to_user(user_dest, dest_orig, alloc_size))
2344 ret = -EFAULT;
2345
2346 kfree(dest_orig);
2347out:
2348 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002349 ret = -EFAULT;
2350
2351 return ret;
2352}
2353
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002354/*
2355 * there are many ways the trans_start and trans_end ioctls can lead
2356 * to deadlocks. They should only be used by applications that
2357 * basically own the machine, and have a very in depth understanding
2358 * of all the possible deadlocks and enospc problems.
2359 */
2360long btrfs_ioctl_trans_end(struct file *file)
2361{
2362 struct inode *inode = fdentry(file)->d_inode;
2363 struct btrfs_root *root = BTRFS_I(inode)->root;
2364 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002365
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002366 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002367 if (!trans)
2368 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002369 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002370
Sage Weil1ab86ae2009-09-29 18:38:44 -04002371 btrfs_end_transaction(trans, root);
2372
Sage Weil9ca9ee02008-08-04 10:41:27 -04002373 mutex_lock(&root->fs_info->trans_mutex);
2374 root->fs_info->open_ioctl_trans--;
2375 mutex_unlock(&root->fs_info->trans_mutex);
2376
Sage Weilcfc8ea82008-12-11 16:30:06 -05002377 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002378 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002379}
2380
Sage Weil46204592010-10-29 15:41:32 -04002381static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2382{
2383 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2384 struct btrfs_trans_handle *trans;
2385 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002386 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002387
2388 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002389 if (IS_ERR(trans))
2390 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002391 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002392 ret = btrfs_commit_transaction_async(trans, root, 0);
2393 if (ret)
2394 return ret;
Sage Weil46204592010-10-29 15:41:32 -04002395
2396 if (argp)
2397 if (copy_to_user(argp, &transid, sizeof(transid)))
2398 return -EFAULT;
2399 return 0;
2400}
2401
2402static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2403{
2404 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2405 u64 transid;
2406
2407 if (argp) {
2408 if (copy_from_user(&transid, argp, sizeof(transid)))
2409 return -EFAULT;
2410 } else {
2411 transid = 0; /* current trans */
2412 }
2413 return btrfs_wait_for_commit(root, transid);
2414}
2415
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002416long btrfs_ioctl(struct file *file, unsigned int
2417 cmd, unsigned long arg)
2418{
2419 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002420 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002421
2422 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002423 case FS_IOC_GETFLAGS:
2424 return btrfs_ioctl_getflags(file, argp);
2425 case FS_IOC_SETFLAGS:
2426 return btrfs_ioctl_setflags(file, argp);
2427 case FS_IOC_GETVERSION:
2428 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002429 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002430 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002431 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002432 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002433 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002434 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002435 case BTRFS_IOC_SNAP_DESTROY:
2436 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002437 case BTRFS_IOC_SUBVOL_GETFLAGS:
2438 return btrfs_ioctl_subvol_getflags(file, argp);
2439 case BTRFS_IOC_SUBVOL_SETFLAGS:
2440 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002441 case BTRFS_IOC_DEFAULT_SUBVOL:
2442 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002443 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002444 return btrfs_ioctl_defrag(file, NULL);
2445 case BTRFS_IOC_DEFRAG_RANGE:
2446 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002447 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002448 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002449 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002450 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002451 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002452 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002453 case BTRFS_IOC_BALANCE:
2454 return btrfs_balance(root->fs_info->dev_root);
2455 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002456 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2457 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002458 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002459 case BTRFS_IOC_TRANS_START:
2460 return btrfs_ioctl_trans_start(file);
2461 case BTRFS_IOC_TRANS_END:
2462 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002463 case BTRFS_IOC_TREE_SEARCH:
2464 return btrfs_ioctl_tree_search(file, argp);
2465 case BTRFS_IOC_INO_LOOKUP:
2466 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002467 case BTRFS_IOC_SPACE_INFO:
2468 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002469 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002470 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2471 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002472 case BTRFS_IOC_START_SYNC:
2473 return btrfs_ioctl_start_sync(file, argp);
2474 case BTRFS_IOC_WAIT_SYNC:
2475 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002476 }
2477
2478 return -ENOTTY;
2479}