blob: ce773fb736a119a4738080157ff60140a2c6908a [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>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040053
Christoph Hellwig6cbff002009-04-17 10:37:41 +020054/* Mask out flags that are inappropriate for the given type of inode. */
55static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
56{
57 if (S_ISDIR(mode))
58 return flags;
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
61 else
62 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
63}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040064
Christoph Hellwig6cbff002009-04-17 10:37:41 +020065/*
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 */
68static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69{
70 unsigned int iflags = 0;
71
72 if (flags & BTRFS_INODE_SYNC)
73 iflags |= FS_SYNC_FL;
74 if (flags & BTRFS_INODE_IMMUTABLE)
75 iflags |= FS_IMMUTABLE_FL;
76 if (flags & BTRFS_INODE_APPEND)
77 iflags |= FS_APPEND_FL;
78 if (flags & BTRFS_INODE_NODUMP)
79 iflags |= FS_NODUMP_FL;
80 if (flags & BTRFS_INODE_NOATIME)
81 iflags |= FS_NOATIME_FL;
82 if (flags & BTRFS_INODE_DIRSYNC)
83 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000084 if (flags & BTRFS_INODE_NODATACOW)
85 iflags |= FS_NOCOW_FL;
86
87 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
88 iflags |= FS_COMPR_FL;
89 else if (flags & BTRFS_INODE_NOCOMPRESS)
90 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020091
92 return iflags;
93}
94
95/*
96 * Update inode->i_flags based on the btrfs internal flags.
97 */
98void btrfs_update_iflags(struct inode *inode)
99{
100 struct btrfs_inode *ip = BTRFS_I(inode);
101
102 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
103
104 if (ip->flags & BTRFS_INODE_SYNC)
105 inode->i_flags |= S_SYNC;
106 if (ip->flags & BTRFS_INODE_IMMUTABLE)
107 inode->i_flags |= S_IMMUTABLE;
108 if (ip->flags & BTRFS_INODE_APPEND)
109 inode->i_flags |= S_APPEND;
110 if (ip->flags & BTRFS_INODE_NOATIME)
111 inode->i_flags |= S_NOATIME;
112 if (ip->flags & BTRFS_INODE_DIRSYNC)
113 inode->i_flags |= S_DIRSYNC;
114}
115
116/*
117 * Inherit flags from the parent inode.
118 *
119 * Unlike extN we don't have any flags we don't want to inherit currently.
120 */
121void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
122{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400123 unsigned int flags;
124
125 if (!dir)
126 return;
127
128 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200129
130 if (S_ISREG(inode->i_mode))
131 flags &= ~BTRFS_INODE_DIRSYNC;
132 else if (!S_ISDIR(inode->i_mode))
133 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
134
135 BTRFS_I(inode)->flags = flags;
136 btrfs_update_iflags(inode);
137}
138
139static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
140{
141 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
142 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
143
144 if (copy_to_user(arg, &flags, sizeof(flags)))
145 return -EFAULT;
146 return 0;
147}
148
Liu Bo75e7cb72011-03-22 10:12:20 +0000149static int check_flags(unsigned int flags)
150{
151 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
152 FS_NOATIME_FL | FS_NODUMP_FL | \
153 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000154 FS_NOCOMP_FL | FS_COMPR_FL |
155 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000156 return -EOPNOTSUPP;
157
158 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
159 return -EINVAL;
160
Liu Bo75e7cb72011-03-22 10:12:20 +0000161 return 0;
162}
163
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200164static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
165{
166 struct inode *inode = file->f_path.dentry->d_inode;
167 struct btrfs_inode *ip = BTRFS_I(inode);
168 struct btrfs_root *root = ip->root;
169 struct btrfs_trans_handle *trans;
170 unsigned int flags, oldflags;
171 int ret;
172
Li Zefanb83cc962010-12-20 16:04:08 +0800173 if (btrfs_root_readonly(root))
174 return -EROFS;
175
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200176 if (copy_from_user(&flags, arg, sizeof(flags)))
177 return -EFAULT;
178
Liu Bo75e7cb72011-03-22 10:12:20 +0000179 ret = check_flags(flags);
180 if (ret)
181 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200182
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700183 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200184 return -EACCES;
185
186 mutex_lock(&inode->i_mutex);
187
188 flags = btrfs_mask_flags(inode->i_mode, flags);
189 oldflags = btrfs_flags_to_ioctl(ip->flags);
190 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
191 if (!capable(CAP_LINUX_IMMUTABLE)) {
192 ret = -EPERM;
193 goto out_unlock;
194 }
195 }
196
197 ret = mnt_want_write(file->f_path.mnt);
198 if (ret)
199 goto out_unlock;
200
201 if (flags & FS_SYNC_FL)
202 ip->flags |= BTRFS_INODE_SYNC;
203 else
204 ip->flags &= ~BTRFS_INODE_SYNC;
205 if (flags & FS_IMMUTABLE_FL)
206 ip->flags |= BTRFS_INODE_IMMUTABLE;
207 else
208 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
209 if (flags & FS_APPEND_FL)
210 ip->flags |= BTRFS_INODE_APPEND;
211 else
212 ip->flags &= ~BTRFS_INODE_APPEND;
213 if (flags & FS_NODUMP_FL)
214 ip->flags |= BTRFS_INODE_NODUMP;
215 else
216 ip->flags &= ~BTRFS_INODE_NODUMP;
217 if (flags & FS_NOATIME_FL)
218 ip->flags |= BTRFS_INODE_NOATIME;
219 else
220 ip->flags &= ~BTRFS_INODE_NOATIME;
221 if (flags & FS_DIRSYNC_FL)
222 ip->flags |= BTRFS_INODE_DIRSYNC;
223 else
224 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000225 if (flags & FS_NOCOW_FL)
226 ip->flags |= BTRFS_INODE_NODATACOW;
227 else
228 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200229
Liu Bo75e7cb72011-03-22 10:12:20 +0000230 /*
231 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
232 * flag may be changed automatically if compression code won't make
233 * things smaller.
234 */
235 if (flags & FS_NOCOMP_FL) {
236 ip->flags &= ~BTRFS_INODE_COMPRESS;
237 ip->flags |= BTRFS_INODE_NOCOMPRESS;
238 } else if (flags & FS_COMPR_FL) {
239 ip->flags |= BTRFS_INODE_COMPRESS;
240 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000241 } else {
242 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000243 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200244
245 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000246 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200247
248 ret = btrfs_update_inode(trans, root, inode);
249 BUG_ON(ret);
250
251 btrfs_update_iflags(inode);
252 inode->i_ctime = CURRENT_TIME;
253 btrfs_end_transaction(trans, root);
254
255 mnt_drop_write(file->f_path.mnt);
liubo2d4e6f62011-02-24 09:38:16 +0000256
257 ret = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200258 out_unlock:
259 mutex_unlock(&inode->i_mutex);
liubo2d4e6f62011-02-24 09:38:16 +0000260 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200261}
262
263static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
264{
265 struct inode *inode = file->f_path.dentry->d_inode;
266
267 return put_user(inode->i_generation, arg);
268}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400269
Li Dongyangf7039b12011-03-24 10:24:28 +0000270static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
271{
272 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
273 struct btrfs_fs_info *fs_info = root->fs_info;
274 struct btrfs_device *device;
275 struct request_queue *q;
276 struct fstrim_range range;
277 u64 minlen = ULLONG_MAX;
278 u64 num_devices = 0;
279 int ret;
280
281 if (!capable(CAP_SYS_ADMIN))
282 return -EPERM;
283
284 mutex_lock(&fs_info->fs_devices->device_list_mutex);
285 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
286 if (!device->bdev)
287 continue;
288 q = bdev_get_queue(device->bdev);
289 if (blk_queue_discard(q)) {
290 num_devices++;
291 minlen = min((u64)q->limits.discard_granularity,
292 minlen);
293 }
294 }
295 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
296 if (!num_devices)
297 return -EOPNOTSUPP;
298
299 if (copy_from_user(&range, arg, sizeof(range)))
300 return -EFAULT;
301
302 range.minlen = max(range.minlen, minlen);
303 ret = btrfs_trim_fs(root, &range);
304 if (ret < 0)
305 return ret;
306
307 if (copy_to_user(arg, &range, sizeof(range)))
308 return -EFAULT;
309
310 return 0;
311}
312
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400313static noinline int create_subvol(struct btrfs_root *root,
314 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400315 char *name, int namelen,
316 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400317{
318 struct btrfs_trans_handle *trans;
319 struct btrfs_key key;
320 struct btrfs_root_item root_item;
321 struct btrfs_inode_item *inode_item;
322 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400323 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000324 struct dentry *parent = dget_parent(dentry);
325 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400326 int ret;
327 int err;
328 u64 objectid;
329 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500330 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400331
Yan, Zhenga22285a2010-05-16 10:48:46 -0400332 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
333 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000334 if (ret) {
335 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400336 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000337 }
338
339 dir = parent->d_inode;
340
Josef Bacik9ed74f22009-09-11 16:12:44 -0400341 /*
342 * 1 - inode item
343 * 2 - refs
344 * 1 - root item
345 * 2 - dir items
346 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400347 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000348 if (IS_ERR(trans)) {
349 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400350 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000351 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400352
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400353 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
354 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400355 if (IS_ERR(leaf)) {
356 ret = PTR_ERR(leaf);
357 goto fail;
358 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400359
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400361 btrfs_set_header_bytenr(leaf, leaf->start);
362 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400363 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400364 btrfs_set_header_owner(leaf, objectid);
365
366 write_extent_buffer(leaf, root->fs_info->fsid,
367 (unsigned long)btrfs_header_fsid(leaf),
368 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400369 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
370 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
371 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400372 btrfs_mark_buffer_dirty(leaf);
373
374 inode_item = &root_item.inode;
375 memset(inode_item, 0, sizeof(*inode_item));
376 inode_item->generation = cpu_to_le64(1);
377 inode_item->size = cpu_to_le64(3);
378 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400379 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
381
Li Zefan08fe4db2011-03-28 02:01:25 +0000382 root_item.flags = 0;
383 root_item.byte_limit = 0;
384 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
385
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400386 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400387 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400388 btrfs_set_root_level(&root_item, 0);
389 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000390 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400391 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392
393 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
394 root_item.drop_level = 0;
395
Chris Mason925baed2008-06-25 16:01:30 -0400396 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 free_extent_buffer(leaf);
398 leaf = NULL;
399
400 btrfs_set_root_dirid(&root_item, new_dirid);
401
402 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400403 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400404 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
405 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
406 &root_item);
407 if (ret)
408 goto fail;
409
Yan, Zheng76dda932009-09-21 16:00:26 -0400410 key.offset = (u64)-1;
411 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
412 BUG_ON(IS_ERR(new_root));
413
414 btrfs_record_root_in_trans(trans, new_root);
415
416 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
417 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400418 /*
419 * insert the directory item
420 */
Chris Mason3de45862008-11-17 21:02:50 -0500421 ret = btrfs_set_inode_index(dir, &index);
422 BUG_ON(ret);
423
424 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400425 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500426 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400427 if (ret)
428 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500429
Yan Zheng52c26172009-01-05 15:43:43 -0500430 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
431 ret = btrfs_update_inode(trans, root, dir);
432 BUG_ON(ret);
433
Chris Mason0660b5a2008-11-17 20:37:39 -0500434 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400435 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500436 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400437
Chris Mason0660b5a2008-11-17 20:37:39 -0500438 BUG_ON(ret);
439
Yan, Zheng76dda932009-09-21 16:00:26 -0400440 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400441fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000442 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400443 if (async_transid) {
444 *async_transid = trans->transid;
445 err = btrfs_commit_transaction_async(trans, root, 1);
446 } else {
447 err = btrfs_commit_transaction(trans, root);
448 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400449 if (err && !ret)
450 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451 return ret;
452}
453
Sage Weil72fd0322010-10-29 15:41:32 -0400454static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800455 char *name, int namelen, u64 *async_transid,
456 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000458 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000459 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400460 struct btrfs_pending_snapshot *pending_snapshot;
461 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000462 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400463
464 if (!root->ref_cows)
465 return -EINVAL;
466
Yan, Zhenga22285a2010-05-16 10:48:46 -0400467 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
468 if (!pending_snapshot)
469 return -ENOMEM;
470
471 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
472 pending_snapshot->dentry = dentry;
473 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800474 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400475
476 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
477 if (IS_ERR(trans)) {
478 ret = PTR_ERR(trans);
479 goto fail;
480 }
481
482 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
483 BUG_ON(ret);
484
485 list_add(&pending_snapshot->list,
486 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400487 if (async_transid) {
488 *async_transid = trans->transid;
489 ret = btrfs_commit_transaction_async(trans,
490 root->fs_info->extent_root, 1);
491 } else {
492 ret = btrfs_commit_transaction(trans,
493 root->fs_info->extent_root);
494 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400495 BUG_ON(ret);
496
497 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400498 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000499 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400500
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500501 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
502 if (ret)
503 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400504
Josef Bacik6a912212010-11-20 09:48:00 +0000505 parent = dget_parent(dentry);
506 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
507 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000508 if (IS_ERR(inode)) {
509 ret = PTR_ERR(inode);
510 goto fail;
511 }
512 BUG_ON(!inode);
513 d_instantiate(dentry, inode);
514 ret = 0;
515fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400516 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400517 return ret;
518}
519
Sage Weil4260f7c2010-10-29 15:46:43 -0400520/* copy of check_sticky in fs/namei.c()
521* It's inline, so penalty for filesystems that don't use sticky bit is
522* minimal.
523*/
524static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
525{
526 uid_t fsuid = current_fsuid();
527
528 if (!(dir->i_mode & S_ISVTX))
529 return 0;
530 if (inode->i_uid == fsuid)
531 return 0;
532 if (dir->i_uid == fsuid)
533 return 0;
534 return !capable(CAP_FOWNER);
535}
536
537/* copy of may_delete in fs/namei.c()
538 * Check whether we can remove a link victim from directory dir, check
539 * whether the type of victim is right.
540 * 1. We can't do it if dir is read-only (done in permission())
541 * 2. We should have write and exec permissions on dir
542 * 3. We can't remove anything from append-only dir
543 * 4. We can't do anything with immutable dir (done in permission())
544 * 5. If the sticky bit on dir is set we should either
545 * a. be owner of dir, or
546 * b. be owner of victim, or
547 * c. have CAP_FOWNER capability
548 * 6. If the victim is append-only or immutable we can't do antyhing with
549 * links pointing to it.
550 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
551 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
552 * 9. We can't remove a root or mountpoint.
553 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
554 * nfs_async_unlink().
555 */
556
557static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
558{
559 int error;
560
561 if (!victim->d_inode)
562 return -ENOENT;
563
564 BUG_ON(victim->d_parent->d_inode != dir);
565 audit_inode_child(victim, dir);
566
567 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
568 if (error)
569 return error;
570 if (IS_APPEND(dir))
571 return -EPERM;
572 if (btrfs_check_sticky(dir, victim->d_inode)||
573 IS_APPEND(victim->d_inode)||
574 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
575 return -EPERM;
576 if (isdir) {
577 if (!S_ISDIR(victim->d_inode->i_mode))
578 return -ENOTDIR;
579 if (IS_ROOT(victim))
580 return -EBUSY;
581 } else if (S_ISDIR(victim->d_inode->i_mode))
582 return -EISDIR;
583 if (IS_DEADDIR(dir))
584 return -ENOENT;
585 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
586 return -EBUSY;
587 return 0;
588}
589
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400590/* copy of may_create in fs/namei.c() */
591static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
592{
593 if (child->d_inode)
594 return -EEXIST;
595 if (IS_DEADDIR(dir))
596 return -ENOENT;
597 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
598}
599
600/*
601 * Create a new subvolume below @parent. This is largely modeled after
602 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
603 * inside this filesystem so it's quite a bit simpler.
604 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400605static noinline int btrfs_mksubvol(struct path *parent,
606 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400607 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800608 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400609{
Yan, Zheng76dda932009-09-21 16:00:26 -0400610 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400611 struct dentry *dentry;
612 int error;
613
Yan, Zheng76dda932009-09-21 16:00:26 -0400614 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400615
616 dentry = lookup_one_len(name, parent->dentry, namelen);
617 error = PTR_ERR(dentry);
618 if (IS_ERR(dentry))
619 goto out_unlock;
620
621 error = -EEXIST;
622 if (dentry->d_inode)
623 goto out_dput;
624
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400625 error = mnt_want_write(parent->mnt);
626 if (error)
627 goto out_dput;
628
Yan, Zheng76dda932009-09-21 16:00:26 -0400629 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400630 if (error)
631 goto out_drop_write;
632
Yan, Zheng76dda932009-09-21 16:00:26 -0400633 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
634
635 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
636 goto out_up_read;
637
Chris Mason3de45862008-11-17 21:02:50 -0500638 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400639 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800640 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500641 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400642 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400643 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500644 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400645 if (!error)
646 fsnotify_mkdir(dir, dentry);
647out_up_read:
648 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400649out_drop_write:
650 mnt_drop_write(parent->mnt);
651out_dput:
652 dput(dentry);
653out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400654 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400655 return error;
656}
657
Chris Mason940100a2010-03-10 10:52:59 -0500658static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500659 int thresh, u64 *last_len, u64 *skip,
660 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500661{
662 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
663 struct extent_map *em = NULL;
664 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
665 int ret = 1;
666
Chris Mason1e701a32010-03-11 09:42:04 -0500667
668 if (thresh == 0)
669 thresh = 256 * 1024;
670
Chris Mason940100a2010-03-10 10:52:59 -0500671 /*
672 * make sure that once we start defragging and extent, we keep on
673 * defragging it
674 */
675 if (start < *defrag_end)
676 return 1;
677
678 *skip = 0;
679
680 /*
681 * hopefully we have this extent in the tree already, try without
682 * the full extent lock
683 */
684 read_lock(&em_tree->lock);
685 em = lookup_extent_mapping(em_tree, start, len);
686 read_unlock(&em_tree->lock);
687
688 if (!em) {
689 /* get the big lock and read metadata off disk */
690 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
691 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
692 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
693
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000694 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500695 return 0;
696 }
697
698 /* this will cover holes, and inline extents */
699 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
700 ret = 0;
701
702 /*
703 * we hit a real extent, if it is big don't bother defragging it again
704 */
Chris Mason1e701a32010-03-11 09:42:04 -0500705 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500706 ret = 0;
707
708 /*
709 * last_len ends up being a counter of how many bytes we've defragged.
710 * every time we choose not to defrag an extent, we reset *last_len
711 * so that the next tiny extent will force a defrag.
712 *
713 * The end result of this is that tiny extents before a single big
714 * extent will force at least part of that big extent to be defragged.
715 */
716 if (ret) {
717 *last_len += len;
718 *defrag_end = extent_map_end(em);
719 } else {
720 *last_len = 0;
721 *skip = extent_map_end(em);
722 *defrag_end = 0;
723 }
724
725 free_extent_map(em);
726 return ret;
727}
728
Chris Mason1e701a32010-03-11 09:42:04 -0500729static int btrfs_defrag_file(struct file *file,
730 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400731{
732 struct inode *inode = fdentry(file)->d_inode;
733 struct btrfs_root *root = BTRFS_I(inode)->root;
734 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400735 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400736 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800737 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 unsigned long last_index;
739 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
740 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800741 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400742 u64 page_start;
743 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500744 u64 last_len = 0;
745 u64 skip = 0;
746 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400747 unsigned long i;
748 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800749 int compress_type = BTRFS_COMPRESS_ZLIB;
750
751 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
752 if (range->compress_type > BTRFS_COMPRESS_TYPES)
753 return -EINVAL;
754 if (range->compress_type)
755 compress_type = range->compress_type;
756 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400757
Chris Mason940100a2010-03-10 10:52:59 -0500758 if (inode->i_size == 0)
759 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400760
Chris Mason1e701a32010-03-11 09:42:04 -0500761 if (range->start + range->len > range->start) {
762 last_index = min_t(u64, inode->i_size - 1,
763 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
764 } else {
765 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
766 }
767
768 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500769 while (i <= last_index) {
770 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500771 PAGE_CACHE_SIZE,
772 range->extent_thresh,
773 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500774 &defrag_end)) {
775 unsigned long next;
776 /*
777 * the should_defrag function tells us how much to skip
778 * bump our counter by the suggested amount
779 */
780 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
781 i = max(i + 1, next);
782 continue;
783 }
784
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400785 if (total_read % ra_pages == 0) {
786 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
787 min(last_index, i + ra_pages - 1));
788 }
789 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500790 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500791 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800792 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500793
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400794 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
795 if (ret)
796 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400797again:
Chris Mason940100a2010-03-10 10:52:59 -0500798 if (inode->i_size == 0 ||
799 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
800 ret = 0;
801 goto err_reservations;
802 }
803
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400804 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400805 if (!page) {
806 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500807 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400808 }
Chris Mason940100a2010-03-10 10:52:59 -0500809
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400810 if (!PageUptodate(page)) {
811 btrfs_readpage(NULL, page);
812 lock_page(page);
813 if (!PageUptodate(page)) {
814 unlock_page(page);
815 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400816 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500817 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400818 }
819 }
820
Chris Mason940100a2010-03-10 10:52:59 -0500821 if (page->mapping != inode->i_mapping) {
822 unlock_page(page);
823 page_cache_release(page);
824 goto again;
825 }
826
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400827 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400828
Chris Mason940100a2010-03-10 10:52:59 -0500829 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400830 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500831 goto loop_unlock;
832 }
833
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400834 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
835 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400836 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400837
838 ordered = btrfs_lookup_ordered_extent(inode, page_start);
839 if (ordered) {
840 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
841 unlock_page(page);
842 page_cache_release(page);
843 btrfs_start_ordered_extent(inode, ordered, 1);
844 btrfs_put_ordered_extent(ordered);
845 goto again;
846 }
847 set_page_extent_mapped(page);
848
Chris Masonf87f0572008-08-01 11:27:23 -0400849 /*
850 * this makes sure page_mkwrite is called on the
851 * page if it is dirtied again later
852 */
853 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500854 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
855 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
856 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400857
Josef Bacik2ac55d42010-02-03 19:33:23 +0000858 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500859 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400860 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400861 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500862
863loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400864 unlock_page(page);
865 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500866 mutex_unlock(&inode->i_mutex);
867
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400868 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500869 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400870 }
871
Chris Mason1e701a32010-03-11 09:42:04 -0500872 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
873 filemap_flush(inode->i_mapping);
874
875 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
876 /* the filemap_flush will queue IO into the worker threads, but
877 * we have to make sure the IO is actually started and that
878 * ordered extents get created before we return
879 */
880 atomic_inc(&root->fs_info->async_submit_draining);
881 while (atomic_read(&root->fs_info->nr_async_submits) ||
882 atomic_read(&root->fs_info->async_delalloc_pages)) {
883 wait_event(root->fs_info->async_submit_wait,
884 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
885 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
886 }
887 atomic_dec(&root->fs_info->async_submit_draining);
888
889 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800890 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500891 mutex_unlock(&inode->i_mutex);
892 }
893
Li Zefan1a419d82010-10-25 15:12:50 +0800894 disk_super = &root->fs_info->super_copy;
895 features = btrfs_super_incompat_flags(disk_super);
896 if (range->compress_type == BTRFS_COMPRESS_LZO) {
897 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
898 btrfs_set_super_incompat_flags(disk_super, features);
899 }
900
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400901 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500902
903err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400904 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
905err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500906 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500907 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400908}
909
Yan, Zheng76dda932009-09-21 16:00:26 -0400910static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
911 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400912{
913 u64 new_size;
914 u64 old_size;
915 u64 devid = 1;
916 struct btrfs_ioctl_vol_args *vol_args;
917 struct btrfs_trans_handle *trans;
918 struct btrfs_device *device = NULL;
919 char *sizestr;
920 char *devstr = NULL;
921 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400922 int mod = 0;
923
Yan Zhengc146afa2008-11-12 14:34:12 -0500924 if (root->fs_info->sb->s_flags & MS_RDONLY)
925 return -EROFS;
926
Chris Masone441d542009-01-05 16:57:23 -0500927 if (!capable(CAP_SYS_ADMIN))
928 return -EPERM;
929
Li Zefandae7b662009-04-08 15:06:54 +0800930 vol_args = memdup_user(arg, sizeof(*vol_args));
931 if (IS_ERR(vol_args))
932 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400933
934 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400935
Chris Mason7d9eb122008-07-08 14:19:17 -0400936 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400937 sizestr = vol_args->name;
938 devstr = strchr(sizestr, ':');
939 if (devstr) {
940 char *end;
941 sizestr = devstr + 1;
942 *devstr = '\0';
943 devstr = vol_args->name;
944 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700945 printk(KERN_INFO "resizing devid %llu\n",
946 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400947 }
Yan Zheng2b820322008-11-17 21:11:30 -0500948 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400949 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700950 printk(KERN_INFO "resizer unable to find device %llu\n",
951 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400952 ret = -EINVAL;
953 goto out_unlock;
954 }
955 if (!strcmp(sizestr, "max"))
956 new_size = device->bdev->bd_inode->i_size;
957 else {
958 if (sizestr[0] == '-') {
959 mod = -1;
960 sizestr++;
961 } else if (sizestr[0] == '+') {
962 mod = 1;
963 sizestr++;
964 }
Akinobu Mita91748462010-02-28 10:59:11 +0000965 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400966 if (new_size == 0) {
967 ret = -EINVAL;
968 goto out_unlock;
969 }
970 }
971
972 old_size = device->total_bytes;
973
974 if (mod < 0) {
975 if (new_size > old_size) {
976 ret = -EINVAL;
977 goto out_unlock;
978 }
979 new_size = old_size - new_size;
980 } else if (mod > 0) {
981 new_size = old_size + new_size;
982 }
983
984 if (new_size < 256 * 1024 * 1024) {
985 ret = -EINVAL;
986 goto out_unlock;
987 }
988 if (new_size > device->bdev->bd_inode->i_size) {
989 ret = -EFBIG;
990 goto out_unlock;
991 }
992
993 do_div(new_size, root->sectorsize);
994 new_size *= root->sectorsize;
995
996 printk(KERN_INFO "new size for %s is %llu\n",
997 device->name, (unsigned long long)new_size);
998
999 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001000 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001001 if (IS_ERR(trans)) {
1002 ret = PTR_ERR(trans);
1003 goto out_unlock;
1004 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001005 ret = btrfs_grow_device(trans, device, new_size);
1006 btrfs_commit_transaction(trans, root);
1007 } else {
1008 ret = btrfs_shrink_device(device, new_size);
1009 }
1010
1011out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001012 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001013 kfree(vol_args);
1014 return ret;
1015}
1016
Sage Weil72fd0322010-10-29 15:41:32 -04001017static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1018 char *name,
1019 unsigned long fd,
1020 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001021 u64 *transid,
1022 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001023{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001024 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001025 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001026 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001027 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001028
Yan Zhengc146afa2008-11-12 14:34:12 -05001029 if (root->fs_info->sb->s_flags & MS_RDONLY)
1030 return -EROFS;
1031
Sage Weil72fd0322010-10-29 15:41:32 -04001032 namelen = strlen(name);
1033 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001034 ret = -EINVAL;
1035 goto out;
1036 }
1037
Chris Mason3de45862008-11-17 21:02:50 -05001038 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001039 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001040 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001041 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001042 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001043 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001044 if (!src_file) {
1045 ret = -EINVAL;
1046 goto out;
1047 }
1048
1049 src_inode = src_file->f_path.dentry->d_inode;
1050 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001051 printk(KERN_INFO "btrfs: Snapshot src from "
1052 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001053 ret = -EINVAL;
1054 fput(src_file);
1055 goto out;
1056 }
Sage Weil72fd0322010-10-29 15:41:32 -04001057 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1058 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001059 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001060 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001061 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001062out:
Sage Weil72fd0322010-10-29 15:41:32 -04001063 return ret;
1064}
1065
1066static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001067 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001068{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001069 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001070 int ret;
1071
Li Zefanfa0d2b92010-12-20 15:53:28 +08001072 vol_args = memdup_user(arg, sizeof(*vol_args));
1073 if (IS_ERR(vol_args))
1074 return PTR_ERR(vol_args);
1075 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001076
Li Zefanfa0d2b92010-12-20 15:53:28 +08001077 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001078 vol_args->fd, subvol,
1079 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001080
Li Zefanfa0d2b92010-12-20 15:53:28 +08001081 kfree(vol_args);
1082 return ret;
1083}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001084
Li Zefanfa0d2b92010-12-20 15:53:28 +08001085static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1086 void __user *arg, int subvol)
1087{
1088 struct btrfs_ioctl_vol_args_v2 *vol_args;
1089 int ret;
1090 u64 transid = 0;
1091 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001092 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001093
Li Zefanfa0d2b92010-12-20 15:53:28 +08001094 vol_args = memdup_user(arg, sizeof(*vol_args));
1095 if (IS_ERR(vol_args))
1096 return PTR_ERR(vol_args);
1097 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001098
Li Zefanb83cc962010-12-20 16:04:08 +08001099 if (vol_args->flags &
1100 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1101 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001102 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001103 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001104
1105 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1106 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001107 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1108 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001109
1110 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001111 vol_args->fd, subvol,
1112 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001113
1114 if (ret == 0 && ptr &&
1115 copy_to_user(arg +
1116 offsetof(struct btrfs_ioctl_vol_args_v2,
1117 transid), ptr, sizeof(*ptr)))
1118 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001119out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001120 kfree(vol_args);
1121 return ret;
1122}
1123
Li Zefan0caa1022010-12-20 16:30:25 +08001124static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1125 void __user *arg)
1126{
1127 struct inode *inode = fdentry(file)->d_inode;
1128 struct btrfs_root *root = BTRFS_I(inode)->root;
1129 int ret = 0;
1130 u64 flags = 0;
1131
1132 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1133 return -EINVAL;
1134
1135 down_read(&root->fs_info->subvol_sem);
1136 if (btrfs_root_readonly(root))
1137 flags |= BTRFS_SUBVOL_RDONLY;
1138 up_read(&root->fs_info->subvol_sem);
1139
1140 if (copy_to_user(arg, &flags, sizeof(flags)))
1141 ret = -EFAULT;
1142
1143 return ret;
1144}
1145
1146static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1147 void __user *arg)
1148{
1149 struct inode *inode = fdentry(file)->d_inode;
1150 struct btrfs_root *root = BTRFS_I(inode)->root;
1151 struct btrfs_trans_handle *trans;
1152 u64 root_flags;
1153 u64 flags;
1154 int ret = 0;
1155
1156 if (root->fs_info->sb->s_flags & MS_RDONLY)
1157 return -EROFS;
1158
1159 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1160 return -EINVAL;
1161
1162 if (copy_from_user(&flags, arg, sizeof(flags)))
1163 return -EFAULT;
1164
Li Zefanb4dc2b82011-02-16 06:06:34 +00001165 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001166 return -EINVAL;
1167
1168 if (flags & ~BTRFS_SUBVOL_RDONLY)
1169 return -EOPNOTSUPP;
1170
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001171 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001172 return -EACCES;
1173
Li Zefan0caa1022010-12-20 16:30:25 +08001174 down_write(&root->fs_info->subvol_sem);
1175
1176 /* nothing to do */
1177 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1178 goto out;
1179
1180 root_flags = btrfs_root_flags(&root->root_item);
1181 if (flags & BTRFS_SUBVOL_RDONLY)
1182 btrfs_set_root_flags(&root->root_item,
1183 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1184 else
1185 btrfs_set_root_flags(&root->root_item,
1186 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1187
1188 trans = btrfs_start_transaction(root, 1);
1189 if (IS_ERR(trans)) {
1190 ret = PTR_ERR(trans);
1191 goto out_reset;
1192 }
1193
Li Zefanb4dc2b82011-02-16 06:06:34 +00001194 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001195 &root->root_key, &root->root_item);
1196
1197 btrfs_commit_transaction(trans, root);
1198out_reset:
1199 if (ret)
1200 btrfs_set_root_flags(&root->root_item, root_flags);
1201out:
1202 up_write(&root->fs_info->subvol_sem);
1203 return ret;
1204}
1205
Yan, Zheng76dda932009-09-21 16:00:26 -04001206/*
1207 * helper to check if the subvolume references other subvolumes
1208 */
1209static noinline int may_destroy_subvol(struct btrfs_root *root)
1210{
1211 struct btrfs_path *path;
1212 struct btrfs_key key;
1213 int ret;
1214
1215 path = btrfs_alloc_path();
1216 if (!path)
1217 return -ENOMEM;
1218
1219 key.objectid = root->root_key.objectid;
1220 key.type = BTRFS_ROOT_REF_KEY;
1221 key.offset = (u64)-1;
1222
1223 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1224 &key, path, 0, 0);
1225 if (ret < 0)
1226 goto out;
1227 BUG_ON(ret == 0);
1228
1229 ret = 0;
1230 if (path->slots[0] > 0) {
1231 path->slots[0]--;
1232 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1233 if (key.objectid == root->root_key.objectid &&
1234 key.type == BTRFS_ROOT_REF_KEY)
1235 ret = -ENOTEMPTY;
1236 }
1237out:
1238 btrfs_free_path(path);
1239 return ret;
1240}
1241
Chris Masonac8e9812010-02-28 15:39:26 -05001242static noinline int key_in_sk(struct btrfs_key *key,
1243 struct btrfs_ioctl_search_key *sk)
1244{
Chris Masonabc6e132010-03-18 12:10:08 -04001245 struct btrfs_key test;
1246 int ret;
1247
1248 test.objectid = sk->min_objectid;
1249 test.type = sk->min_type;
1250 test.offset = sk->min_offset;
1251
1252 ret = btrfs_comp_cpu_keys(key, &test);
1253 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001254 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001255
1256 test.objectid = sk->max_objectid;
1257 test.type = sk->max_type;
1258 test.offset = sk->max_offset;
1259
1260 ret = btrfs_comp_cpu_keys(key, &test);
1261 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001262 return 0;
1263 return 1;
1264}
1265
1266static noinline int copy_to_sk(struct btrfs_root *root,
1267 struct btrfs_path *path,
1268 struct btrfs_key *key,
1269 struct btrfs_ioctl_search_key *sk,
1270 char *buf,
1271 unsigned long *sk_offset,
1272 int *num_found)
1273{
1274 u64 found_transid;
1275 struct extent_buffer *leaf;
1276 struct btrfs_ioctl_search_header sh;
1277 unsigned long item_off;
1278 unsigned long item_len;
1279 int nritems;
1280 int i;
1281 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001282 int ret = 0;
1283
1284 leaf = path->nodes[0];
1285 slot = path->slots[0];
1286 nritems = btrfs_header_nritems(leaf);
1287
1288 if (btrfs_header_generation(leaf) > sk->max_transid) {
1289 i = nritems;
1290 goto advance_key;
1291 }
1292 found_transid = btrfs_header_generation(leaf);
1293
1294 for (i = slot; i < nritems; i++) {
1295 item_off = btrfs_item_ptr_offset(leaf, i);
1296 item_len = btrfs_item_size_nr(leaf, i);
1297
1298 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1299 item_len = 0;
1300
1301 if (sizeof(sh) + item_len + *sk_offset >
1302 BTRFS_SEARCH_ARGS_BUFSIZE) {
1303 ret = 1;
1304 goto overflow;
1305 }
1306
1307 btrfs_item_key_to_cpu(leaf, key, i);
1308 if (!key_in_sk(key, sk))
1309 continue;
1310
1311 sh.objectid = key->objectid;
1312 sh.offset = key->offset;
1313 sh.type = key->type;
1314 sh.len = item_len;
1315 sh.transid = found_transid;
1316
1317 /* copy search result header */
1318 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1319 *sk_offset += sizeof(sh);
1320
1321 if (item_len) {
1322 char *p = buf + *sk_offset;
1323 /* copy the item */
1324 read_extent_buffer(leaf, p,
1325 item_off, item_len);
1326 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001327 }
Hugo Millse2156862011-05-14 17:43:41 +00001328 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001329
1330 if (*num_found >= sk->nr_items)
1331 break;
1332 }
1333advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001334 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001335 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1336 key->offset++;
1337 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1338 key->offset = 0;
1339 key->type++;
1340 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1341 key->offset = 0;
1342 key->type = 0;
1343 key->objectid++;
1344 } else
1345 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001346overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001347 return ret;
1348}
1349
1350static noinline int search_ioctl(struct inode *inode,
1351 struct btrfs_ioctl_search_args *args)
1352{
1353 struct btrfs_root *root;
1354 struct btrfs_key key;
1355 struct btrfs_key max_key;
1356 struct btrfs_path *path;
1357 struct btrfs_ioctl_search_key *sk = &args->key;
1358 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1359 int ret;
1360 int num_found = 0;
1361 unsigned long sk_offset = 0;
1362
1363 path = btrfs_alloc_path();
1364 if (!path)
1365 return -ENOMEM;
1366
1367 if (sk->tree_id == 0) {
1368 /* search the root of the inode that was passed */
1369 root = BTRFS_I(inode)->root;
1370 } else {
1371 key.objectid = sk->tree_id;
1372 key.type = BTRFS_ROOT_ITEM_KEY;
1373 key.offset = (u64)-1;
1374 root = btrfs_read_fs_root_no_name(info, &key);
1375 if (IS_ERR(root)) {
1376 printk(KERN_ERR "could not find root %llu\n",
1377 sk->tree_id);
1378 btrfs_free_path(path);
1379 return -ENOENT;
1380 }
1381 }
1382
1383 key.objectid = sk->min_objectid;
1384 key.type = sk->min_type;
1385 key.offset = sk->min_offset;
1386
1387 max_key.objectid = sk->max_objectid;
1388 max_key.type = sk->max_type;
1389 max_key.offset = sk->max_offset;
1390
1391 path->keep_locks = 1;
1392
1393 while(1) {
1394 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1395 sk->min_transid);
1396 if (ret != 0) {
1397 if (ret > 0)
1398 ret = 0;
1399 goto err;
1400 }
1401 ret = copy_to_sk(root, path, &key, sk, args->buf,
1402 &sk_offset, &num_found);
1403 btrfs_release_path(root, path);
1404 if (ret || num_found >= sk->nr_items)
1405 break;
1406
1407 }
1408 ret = 0;
1409err:
1410 sk->nr_items = num_found;
1411 btrfs_free_path(path);
1412 return ret;
1413}
1414
1415static noinline int btrfs_ioctl_tree_search(struct file *file,
1416 void __user *argp)
1417{
1418 struct btrfs_ioctl_search_args *args;
1419 struct inode *inode;
1420 int ret;
1421
1422 if (!capable(CAP_SYS_ADMIN))
1423 return -EPERM;
1424
Julia Lawall2354d082010-10-29 15:14:18 -04001425 args = memdup_user(argp, sizeof(*args));
1426 if (IS_ERR(args))
1427 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001428
Chris Masonac8e9812010-02-28 15:39:26 -05001429 inode = fdentry(file)->d_inode;
1430 ret = search_ioctl(inode, args);
1431 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1432 ret = -EFAULT;
1433 kfree(args);
1434 return ret;
1435}
1436
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001437/*
Chris Masonac8e9812010-02-28 15:39:26 -05001438 * Search INODE_REFs to identify path name of 'dirid' directory
1439 * in a 'tree_id' tree. and sets path name to 'name'.
1440 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001441static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1442 u64 tree_id, u64 dirid, char *name)
1443{
1444 struct btrfs_root *root;
1445 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001446 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001447 int ret = -1;
1448 int slot;
1449 int len;
1450 int total_len = 0;
1451 struct btrfs_inode_ref *iref;
1452 struct extent_buffer *l;
1453 struct btrfs_path *path;
1454
1455 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1456 name[0]='\0';
1457 return 0;
1458 }
1459
1460 path = btrfs_alloc_path();
1461 if (!path)
1462 return -ENOMEM;
1463
Chris Masonac8e9812010-02-28 15:39:26 -05001464 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001465
1466 key.objectid = tree_id;
1467 key.type = BTRFS_ROOT_ITEM_KEY;
1468 key.offset = (u64)-1;
1469 root = btrfs_read_fs_root_no_name(info, &key);
1470 if (IS_ERR(root)) {
1471 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001472 ret = -ENOENT;
1473 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001474 }
1475
1476 key.objectid = dirid;
1477 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001478 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001479
1480 while(1) {
1481 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1482 if (ret < 0)
1483 goto out;
1484
1485 l = path->nodes[0];
1486 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001487 if (ret > 0 && slot > 0)
1488 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001489 btrfs_item_key_to_cpu(l, &key, slot);
1490
1491 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001492 key.type != BTRFS_INODE_REF_KEY)) {
1493 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001494 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001495 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001496
1497 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1498 len = btrfs_inode_ref_name_len(l, iref);
1499 ptr -= len + 1;
1500 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001501 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001502 goto out;
1503
1504 *(ptr + len) = '/';
1505 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1506
1507 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1508 break;
1509
1510 btrfs_release_path(root, path);
1511 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001512 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001513 dirid = key.objectid;
1514
1515 }
Chris Masonac8e9812010-02-28 15:39:26 -05001516 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001517 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001518 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001519 name[total_len]='\0';
1520 ret = 0;
1521out:
1522 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001523 return ret;
1524}
1525
1526static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1527 void __user *argp)
1528{
1529 struct btrfs_ioctl_ino_lookup_args *args;
1530 struct inode *inode;
1531 int ret;
1532
1533 if (!capable(CAP_SYS_ADMIN))
1534 return -EPERM;
1535
Julia Lawall2354d082010-10-29 15:14:18 -04001536 args = memdup_user(argp, sizeof(*args));
1537 if (IS_ERR(args))
1538 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001539
Chris Masonac8e9812010-02-28 15:39:26 -05001540 inode = fdentry(file)->d_inode;
1541
Chris Mason1b53ac42010-03-18 12:17:05 -04001542 if (args->treeid == 0)
1543 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1544
Chris Masonac8e9812010-02-28 15:39:26 -05001545 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1546 args->treeid, args->objectid,
1547 args->name);
1548
1549 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1550 ret = -EFAULT;
1551
1552 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001553 return ret;
1554}
1555
Yan, Zheng76dda932009-09-21 16:00:26 -04001556static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1557 void __user *arg)
1558{
1559 struct dentry *parent = fdentry(file);
1560 struct dentry *dentry;
1561 struct inode *dir = parent->d_inode;
1562 struct inode *inode;
1563 struct btrfs_root *root = BTRFS_I(dir)->root;
1564 struct btrfs_root *dest = NULL;
1565 struct btrfs_ioctl_vol_args *vol_args;
1566 struct btrfs_trans_handle *trans;
1567 int namelen;
1568 int ret;
1569 int err = 0;
1570
Yan, Zheng76dda932009-09-21 16:00:26 -04001571 vol_args = memdup_user(arg, sizeof(*vol_args));
1572 if (IS_ERR(vol_args))
1573 return PTR_ERR(vol_args);
1574
1575 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1576 namelen = strlen(vol_args->name);
1577 if (strchr(vol_args->name, '/') ||
1578 strncmp(vol_args->name, "..", namelen) == 0) {
1579 err = -EINVAL;
1580 goto out;
1581 }
1582
1583 err = mnt_want_write(file->f_path.mnt);
1584 if (err)
1585 goto out;
1586
1587 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1588 dentry = lookup_one_len(vol_args->name, parent, namelen);
1589 if (IS_ERR(dentry)) {
1590 err = PTR_ERR(dentry);
1591 goto out_unlock_dir;
1592 }
1593
1594 if (!dentry->d_inode) {
1595 err = -ENOENT;
1596 goto out_dput;
1597 }
1598
1599 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001600 dest = BTRFS_I(inode)->root;
1601 if (!capable(CAP_SYS_ADMIN)){
1602 /*
1603 * Regular user. Only allow this with a special mount
1604 * option, when the user has write+exec access to the
1605 * subvol root, and when rmdir(2) would have been
1606 * allowed.
1607 *
1608 * Note that this is _not_ check that the subvol is
1609 * empty or doesn't contain data that we wouldn't
1610 * otherwise be able to delete.
1611 *
1612 * Users who want to delete empty subvols should try
1613 * rmdir(2).
1614 */
1615 err = -EPERM;
1616 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1617 goto out_dput;
1618
1619 /*
1620 * Do not allow deletion if the parent dir is the same
1621 * as the dir to be deleted. That means the ioctl
1622 * must be called on the dentry referencing the root
1623 * of the subvol, not a random directory contained
1624 * within it.
1625 */
1626 err = -EINVAL;
1627 if (root == dest)
1628 goto out_dput;
1629
1630 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1631 if (err)
1632 goto out_dput;
1633
1634 /* check if subvolume may be deleted by a non-root user */
1635 err = btrfs_may_delete(dir, dentry, 1);
1636 if (err)
1637 goto out_dput;
1638 }
1639
Yan, Zheng76dda932009-09-21 16:00:26 -04001640 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1641 err = -EINVAL;
1642 goto out_dput;
1643 }
1644
Yan, Zheng76dda932009-09-21 16:00:26 -04001645 mutex_lock(&inode->i_mutex);
1646 err = d_invalidate(dentry);
1647 if (err)
1648 goto out_unlock;
1649
1650 down_write(&root->fs_info->subvol_sem);
1651
1652 err = may_destroy_subvol(dest);
1653 if (err)
1654 goto out_up_write;
1655
Yan, Zhenga22285a2010-05-16 10:48:46 -04001656 trans = btrfs_start_transaction(root, 0);
1657 if (IS_ERR(trans)) {
1658 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001659 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001660 }
1661 trans->block_rsv = &root->fs_info->global_block_rsv;
1662
Yan, Zheng76dda932009-09-21 16:00:26 -04001663 ret = btrfs_unlink_subvol(trans, root, dir,
1664 dest->root_key.objectid,
1665 dentry->d_name.name,
1666 dentry->d_name.len);
1667 BUG_ON(ret);
1668
1669 btrfs_record_root_in_trans(trans, dest);
1670
1671 memset(&dest->root_item.drop_progress, 0,
1672 sizeof(dest->root_item.drop_progress));
1673 dest->root_item.drop_level = 0;
1674 btrfs_set_root_refs(&dest->root_item, 0);
1675
Yan, Zhengd68fc572010-05-16 10:49:58 -04001676 if (!xchg(&dest->orphan_item_inserted, 1)) {
1677 ret = btrfs_insert_orphan_item(trans,
1678 root->fs_info->tree_root,
1679 dest->root_key.objectid);
1680 BUG_ON(ret);
1681 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001682
Sage Weil531cb132010-10-29 15:41:32 -04001683 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001684 BUG_ON(ret);
1685 inode->i_flags |= S_DEAD;
1686out_up_write:
1687 up_write(&root->fs_info->subvol_sem);
1688out_unlock:
1689 mutex_unlock(&inode->i_mutex);
1690 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001691 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001692 btrfs_invalidate_inodes(dest);
1693 d_delete(dentry);
1694 }
1695out_dput:
1696 dput(dentry);
1697out_unlock_dir:
1698 mutex_unlock(&dir->i_mutex);
1699 mnt_drop_write(file->f_path.mnt);
1700out:
1701 kfree(vol_args);
1702 return err;
1703}
1704
Chris Mason1e701a32010-03-11 09:42:04 -05001705static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001706{
1707 struct inode *inode = fdentry(file)->d_inode;
1708 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001709 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001710 int ret;
1711
Li Zefanb83cc962010-12-20 16:04:08 +08001712 if (btrfs_root_readonly(root))
1713 return -EROFS;
1714
Yan Zhengc146afa2008-11-12 14:34:12 -05001715 ret = mnt_want_write(file->f_path.mnt);
1716 if (ret)
1717 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001718
1719 switch (inode->i_mode & S_IFMT) {
1720 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001721 if (!capable(CAP_SYS_ADMIN)) {
1722 ret = -EPERM;
1723 goto out;
1724 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001725 ret = btrfs_defrag_root(root, 0);
1726 if (ret)
1727 goto out;
1728 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001729 break;
1730 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001731 if (!(file->f_mode & FMODE_WRITE)) {
1732 ret = -EINVAL;
1733 goto out;
1734 }
Chris Mason1e701a32010-03-11 09:42:04 -05001735
1736 range = kzalloc(sizeof(*range), GFP_KERNEL);
1737 if (!range) {
1738 ret = -ENOMEM;
1739 goto out;
1740 }
1741
1742 if (argp) {
1743 if (copy_from_user(range, argp,
1744 sizeof(*range))) {
1745 ret = -EFAULT;
1746 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001747 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001748 }
1749 /* compression requires us to start the IO */
1750 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1751 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1752 range->extent_thresh = (u32)-1;
1753 }
1754 } else {
1755 /* the rest are all set to zero by kzalloc */
1756 range->len = (u64)-1;
1757 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001758 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001759 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001760 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001761 default:
1762 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001763 }
Chris Masone441d542009-01-05 16:57:23 -05001764out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001765 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001766 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001767}
1768
Christoph Hellwigb2950862008-12-02 09:54:17 -05001769static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001770{
1771 struct btrfs_ioctl_vol_args *vol_args;
1772 int ret;
1773
Chris Masone441d542009-01-05 16:57:23 -05001774 if (!capable(CAP_SYS_ADMIN))
1775 return -EPERM;
1776
Li Zefandae7b662009-04-08 15:06:54 +08001777 vol_args = memdup_user(arg, sizeof(*vol_args));
1778 if (IS_ERR(vol_args))
1779 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001780
Mark Fasheh5516e592008-07-24 12:20:14 -04001781 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001782 ret = btrfs_init_new_device(root, vol_args->name);
1783
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001784 kfree(vol_args);
1785 return ret;
1786}
1787
Christoph Hellwigb2950862008-12-02 09:54:17 -05001788static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001789{
1790 struct btrfs_ioctl_vol_args *vol_args;
1791 int ret;
1792
Chris Masone441d542009-01-05 16:57:23 -05001793 if (!capable(CAP_SYS_ADMIN))
1794 return -EPERM;
1795
Yan Zhengc146afa2008-11-12 14:34:12 -05001796 if (root->fs_info->sb->s_flags & MS_RDONLY)
1797 return -EROFS;
1798
Li Zefandae7b662009-04-08 15:06:54 +08001799 vol_args = memdup_user(arg, sizeof(*vol_args));
1800 if (IS_ERR(vol_args))
1801 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001802
Mark Fasheh5516e592008-07-24 12:20:14 -04001803 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001804 ret = btrfs_rm_device(root, vol_args->name);
1805
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001806 kfree(vol_args);
1807 return ret;
1808}
1809
Yan, Zheng76dda932009-09-21 16:00:26 -04001810static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1811 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001812{
1813 struct inode *inode = fdentry(file)->d_inode;
1814 struct btrfs_root *root = BTRFS_I(inode)->root;
1815 struct file *src_file;
1816 struct inode *src;
1817 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001818 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001819 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001820 char *buf;
1821 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001822 u32 nritems;
1823 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001824 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001825 u64 len = olen;
1826 u64 bs = root->fs_info->sb->s_blocksize;
1827 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001828
Sage Weilc5c9cd42008-11-12 14:32:25 -05001829 /*
1830 * TODO:
1831 * - split compressed inline extents. annoying: we need to
1832 * decompress into destination's address_space (the file offset
1833 * may change, so source mapping won't do), then recompress (or
1834 * otherwise reinsert) a subrange.
1835 * - allow ranges within the same file to be cloned (provided
1836 * they don't overlap)?
1837 */
1838
Chris Masone441d542009-01-05 16:57:23 -05001839 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001840 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001841 return -EINVAL;
1842
Li Zefanb83cc962010-12-20 16:04:08 +08001843 if (btrfs_root_readonly(root))
1844 return -EROFS;
1845
Yan Zhengc146afa2008-11-12 14:34:12 -05001846 ret = mnt_want_write(file->f_path.mnt);
1847 if (ret)
1848 return ret;
1849
Sage Weilc5c9cd42008-11-12 14:32:25 -05001850 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001851 if (!src_file) {
1852 ret = -EBADF;
1853 goto out_drop_write;
1854 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001855
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001856 src = src_file->f_dentry->d_inode;
1857
Sage Weilc5c9cd42008-11-12 14:32:25 -05001858 ret = -EINVAL;
1859 if (src == inode)
1860 goto out_fput;
1861
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001862 /* the src must be open for reading */
1863 if (!(src_file->f_mode & FMODE_READ))
1864 goto out_fput;
1865
Yan Zhengae01a0a2008-08-04 23:23:47 -04001866 ret = -EISDIR;
1867 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001868 goto out_fput;
1869
Yan Zhengae01a0a2008-08-04 23:23:47 -04001870 ret = -EXDEV;
1871 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1872 goto out_fput;
1873
1874 ret = -ENOMEM;
1875 buf = vmalloc(btrfs_level_size(root, 0));
1876 if (!buf)
1877 goto out_fput;
1878
1879 path = btrfs_alloc_path();
1880 if (!path) {
1881 vfree(buf);
1882 goto out_fput;
1883 }
1884 path->reada = 2;
1885
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001886 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001887 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1888 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001889 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001890 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1891 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001892 }
1893
Sage Weilc5c9cd42008-11-12 14:32:25 -05001894 /* determine range to clone */
1895 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001896 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001897 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001898 if (len == 0)
1899 olen = len = src->i_size - off;
1900 /* if we extend to eof, continue to block boundary */
1901 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001902 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001903
1904 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001905 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1906 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001907 goto out_unlock;
1908
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001909 /* do any pending delalloc/csum calc on src, one way or
1910 another, and lock file content */
1911 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001912 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001913 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001914 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1915 if (!ordered &&
1916 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1917 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001918 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001919 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001920 if (ordered)
1921 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001922 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001923 }
1924
Sage Weilc5c9cd42008-11-12 14:32:25 -05001925 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001926 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001927 key.type = BTRFS_EXTENT_DATA_KEY;
1928 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001929
1930 while (1) {
1931 /*
1932 * note the key will change type as we walk through the
1933 * tree.
1934 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001935 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001936 if (ret < 0)
1937 goto out;
1938
Yan Zhengae01a0a2008-08-04 23:23:47 -04001939 nritems = btrfs_header_nritems(path->nodes[0]);
1940 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001941 ret = btrfs_next_leaf(root, path);
1942 if (ret < 0)
1943 goto out;
1944 if (ret > 0)
1945 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001946 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001947 }
1948 leaf = path->nodes[0];
1949 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001950
Yan Zhengae01a0a2008-08-04 23:23:47 -04001951 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001952 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001953 key.objectid != src->i_ino)
1954 break;
1955
Sage Weilc5c9cd42008-11-12 14:32:25 -05001956 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1957 struct btrfs_file_extent_item *extent;
1958 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001959 u32 size;
1960 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001961 u64 disko = 0, diskl = 0;
1962 u64 datao = 0, datal = 0;
1963 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001964 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001965
1966 size = btrfs_item_size_nr(leaf, slot);
1967 read_extent_buffer(leaf, buf,
1968 btrfs_item_ptr_offset(leaf, slot),
1969 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001970
1971 extent = btrfs_item_ptr(leaf, slot,
1972 struct btrfs_file_extent_item);
1973 comp = btrfs_file_extent_compression(leaf, extent);
1974 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001975 if (type == BTRFS_FILE_EXTENT_REG ||
1976 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001977 disko = btrfs_file_extent_disk_bytenr(leaf,
1978 extent);
1979 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1980 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001981 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001982 datal = btrfs_file_extent_num_bytes(leaf,
1983 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001984 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1985 /* take upper bound, may be compressed */
1986 datal = btrfs_file_extent_ram_bytes(leaf,
1987 extent);
1988 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001989 btrfs_release_path(root, path);
1990
Sage Weil050006a2010-10-29 15:37:33 -04001991 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001992 key.offset >= off+len)
1993 goto next;
1994
Zheng Yan31840ae2008-09-23 13:14:14 -04001995 memcpy(&new_key, &key, sizeof(new_key));
1996 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001997 if (off <= key.offset)
1998 new_key.offset = key.offset + destoff - off;
1999 else
2000 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002001
Yan, Zhenga22285a2010-05-16 10:48:46 -04002002 trans = btrfs_start_transaction(root, 1);
2003 if (IS_ERR(trans)) {
2004 ret = PTR_ERR(trans);
2005 goto out;
2006 }
2007
Chris Masonc8a894d2009-06-27 21:07:03 -04002008 if (type == BTRFS_FILE_EXTENT_REG ||
2009 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002010 if (off > key.offset) {
2011 datao += off - key.offset;
2012 datal -= off - key.offset;
2013 }
2014
2015 if (key.offset + datal > off + len)
2016 datal = off + len - key.offset;
2017
2018 ret = btrfs_drop_extents(trans, inode,
2019 new_key.offset,
2020 new_key.offset + datal,
2021 &hint_byte, 1);
2022 BUG_ON(ret);
2023
Sage Weilc5c9cd42008-11-12 14:32:25 -05002024 ret = btrfs_insert_empty_item(trans, root, path,
2025 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002026 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002027
2028 leaf = path->nodes[0];
2029 slot = path->slots[0];
2030 write_extent_buffer(leaf, buf,
2031 btrfs_item_ptr_offset(leaf, slot),
2032 size);
2033
2034 extent = btrfs_item_ptr(leaf, slot,
2035 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002036
Sage Weilc5c9cd42008-11-12 14:32:25 -05002037 /* disko == 0 means it's a hole */
2038 if (!disko)
2039 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002040
2041 btrfs_set_file_extent_offset(leaf, extent,
2042 datao);
2043 btrfs_set_file_extent_num_bytes(leaf, extent,
2044 datal);
2045 if (disko) {
2046 inode_add_bytes(inode, datal);
2047 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002048 disko, diskl, 0,
2049 root->root_key.objectid,
2050 inode->i_ino,
2051 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002052 BUG_ON(ret);
2053 }
2054 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2055 u64 skip = 0;
2056 u64 trim = 0;
2057 if (off > key.offset) {
2058 skip = off - key.offset;
2059 new_key.offset += skip;
2060 }
Chris Masond3977122009-01-05 21:25:51 -05002061
Sage Weilc5c9cd42008-11-12 14:32:25 -05002062 if (key.offset + datal > off+len)
2063 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002064
Sage Weilc5c9cd42008-11-12 14:32:25 -05002065 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002066 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002067 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002068 goto out;
2069 }
2070 size -= skip + trim;
2071 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002072
2073 ret = btrfs_drop_extents(trans, inode,
2074 new_key.offset,
2075 new_key.offset + datal,
2076 &hint_byte, 1);
2077 BUG_ON(ret);
2078
Sage Weilc5c9cd42008-11-12 14:32:25 -05002079 ret = btrfs_insert_empty_item(trans, root, path,
2080 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002081 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002082
2083 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002084 u32 start =
2085 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002086 memmove(buf+start, buf+start+skip,
2087 datal);
2088 }
2089
2090 leaf = path->nodes[0];
2091 slot = path->slots[0];
2092 write_extent_buffer(leaf, buf,
2093 btrfs_item_ptr_offset(leaf, slot),
2094 size);
2095 inode_add_bytes(inode, datal);
2096 }
2097
2098 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002099 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002100
Yan, Zhenga22285a2010-05-16 10:48:46 -04002101 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002102
2103 /*
2104 * we round up to the block size at eof when
2105 * determining which extents to clone above,
2106 * but shouldn't round up the file size
2107 */
2108 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002109 if (endoff > destoff+olen)
2110 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002111 if (endoff > inode->i_size)
2112 btrfs_i_size_write(inode, endoff);
2113
Yan, Zhenga22285a2010-05-16 10:48:46 -04002114 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2115 ret = btrfs_update_inode(trans, root, inode);
2116 BUG_ON(ret);
2117 btrfs_end_transaction(trans, root);
2118 }
Chris Masond3977122009-01-05 21:25:51 -05002119next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002120 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002121 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002122 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002123 ret = 0;
2124out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002125 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002126 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002127out_unlock:
2128 mutex_unlock(&src->i_mutex);
2129 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002130 vfree(buf);
2131 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002132out_fput:
2133 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002134out_drop_write:
2135 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002136 return ret;
2137}
2138
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002139static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002140{
2141 struct btrfs_ioctl_clone_range_args args;
2142
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002143 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002144 return -EFAULT;
2145 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2146 args.src_length, args.dest_offset);
2147}
2148
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002149/*
2150 * there are many ways the trans_start and trans_end ioctls can lead
2151 * to deadlocks. They should only be used by applications that
2152 * basically own the machine, and have a very in depth understanding
2153 * of all the possible deadlocks and enospc problems.
2154 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002155static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002156{
2157 struct inode *inode = fdentry(file)->d_inode;
2158 struct btrfs_root *root = BTRFS_I(inode)->root;
2159 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002160 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002161
Sage Weil1ab86ae2009-09-29 18:38:44 -04002162 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002163 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002164 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002165
2166 ret = -EINPROGRESS;
2167 if (file->private_data)
2168 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002169
Li Zefanb83cc962010-12-20 16:04:08 +08002170 ret = -EROFS;
2171 if (btrfs_root_readonly(root))
2172 goto out;
2173
Yan Zhengc146afa2008-11-12 14:34:12 -05002174 ret = mnt_want_write(file->f_path.mnt);
2175 if (ret)
2176 goto out;
2177
Sage Weil9ca9ee02008-08-04 10:41:27 -04002178 mutex_lock(&root->fs_info->trans_mutex);
2179 root->fs_info->open_ioctl_trans++;
2180 mutex_unlock(&root->fs_info->trans_mutex);
2181
Sage Weil1ab86ae2009-09-29 18:38:44 -04002182 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002183 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002184 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002185 goto out_drop;
2186
2187 file->private_data = trans;
2188 return 0;
2189
2190out_drop:
2191 mutex_lock(&root->fs_info->trans_mutex);
2192 root->fs_info->open_ioctl_trans--;
2193 mutex_unlock(&root->fs_info->trans_mutex);
2194 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002195out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002196 return ret;
2197}
2198
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002199static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2200{
2201 struct inode *inode = fdentry(file)->d_inode;
2202 struct btrfs_root *root = BTRFS_I(inode)->root;
2203 struct btrfs_root *new_root;
2204 struct btrfs_dir_item *di;
2205 struct btrfs_trans_handle *trans;
2206 struct btrfs_path *path;
2207 struct btrfs_key location;
2208 struct btrfs_disk_key disk_key;
2209 struct btrfs_super_block *disk_super;
2210 u64 features;
2211 u64 objectid = 0;
2212 u64 dir_id;
2213
2214 if (!capable(CAP_SYS_ADMIN))
2215 return -EPERM;
2216
2217 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2218 return -EFAULT;
2219
2220 if (!objectid)
2221 objectid = root->root_key.objectid;
2222
2223 location.objectid = objectid;
2224 location.type = BTRFS_ROOT_ITEM_KEY;
2225 location.offset = (u64)-1;
2226
2227 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2228 if (IS_ERR(new_root))
2229 return PTR_ERR(new_root);
2230
2231 if (btrfs_root_refs(&new_root->root_item) == 0)
2232 return -ENOENT;
2233
2234 path = btrfs_alloc_path();
2235 if (!path)
2236 return -ENOMEM;
2237 path->leave_spinning = 1;
2238
2239 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002240 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002241 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002242 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002243 }
2244
2245 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2246 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2247 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002248 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002249 btrfs_free_path(path);
2250 btrfs_end_transaction(trans, root);
2251 printk(KERN_ERR "Umm, you don't have the default dir item, "
2252 "this isn't going to work\n");
2253 return -ENOENT;
2254 }
2255
2256 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2257 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2258 btrfs_mark_buffer_dirty(path->nodes[0]);
2259 btrfs_free_path(path);
2260
2261 disk_super = &root->fs_info->super_copy;
2262 features = btrfs_super_incompat_flags(disk_super);
2263 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2264 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2265 btrfs_set_super_incompat_flags(disk_super, features);
2266 }
2267 btrfs_end_transaction(trans, root);
2268
2269 return 0;
2270}
2271
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002272static void get_block_group_info(struct list_head *groups_list,
2273 struct btrfs_ioctl_space_info *space)
2274{
2275 struct btrfs_block_group_cache *block_group;
2276
2277 space->total_bytes = 0;
2278 space->used_bytes = 0;
2279 space->flags = 0;
2280 list_for_each_entry(block_group, groups_list, list) {
2281 space->flags = block_group->flags;
2282 space->total_bytes += block_group->key.offset;
2283 space->used_bytes +=
2284 btrfs_block_group_used(&block_group->item);
2285 }
2286}
2287
Josef Bacik1406e432010-01-13 18:19:06 +00002288long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2289{
2290 struct btrfs_ioctl_space_args space_args;
2291 struct btrfs_ioctl_space_info space;
2292 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002293 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002294 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002295 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002296 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2297 BTRFS_BLOCK_GROUP_SYSTEM,
2298 BTRFS_BLOCK_GROUP_METADATA,
2299 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2300 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002301 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002302 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002303 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002304 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002305
2306 if (copy_from_user(&space_args,
2307 (struct btrfs_ioctl_space_args __user *)arg,
2308 sizeof(space_args)))
2309 return -EFAULT;
2310
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002311 for (i = 0; i < num_types; i++) {
2312 struct btrfs_space_info *tmp;
2313
2314 info = NULL;
2315 rcu_read_lock();
2316 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2317 list) {
2318 if (tmp->flags == types[i]) {
2319 info = tmp;
2320 break;
2321 }
2322 }
2323 rcu_read_unlock();
2324
2325 if (!info)
2326 continue;
2327
2328 down_read(&info->groups_sem);
2329 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2330 if (!list_empty(&info->block_groups[c]))
2331 slot_count++;
2332 }
2333 up_read(&info->groups_sem);
2334 }
Josef Bacik1406e432010-01-13 18:19:06 +00002335
Chris Mason7fde62b2010-03-16 15:40:10 -04002336 /* space_slots == 0 means they are asking for a count */
2337 if (space_args.space_slots == 0) {
2338 space_args.total_spaces = slot_count;
2339 goto out;
2340 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002341
Dan Rosenberg51788b12011-02-14 16:04:23 -05002342 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002343
Chris Mason7fde62b2010-03-16 15:40:10 -04002344 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002345
Chris Mason7fde62b2010-03-16 15:40:10 -04002346 /* we generally have at most 6 or so space infos, one for each raid
2347 * level. So, a whole page should be more than enough for everyone
2348 */
2349 if (alloc_size > PAGE_CACHE_SIZE)
2350 return -ENOMEM;
2351
2352 space_args.total_spaces = 0;
2353 dest = kmalloc(alloc_size, GFP_NOFS);
2354 if (!dest)
2355 return -ENOMEM;
2356 dest_orig = dest;
2357
2358 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002359 for (i = 0; i < num_types; i++) {
2360 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002361
Dan Rosenberg51788b12011-02-14 16:04:23 -05002362 if (!slot_count)
2363 break;
2364
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002365 info = NULL;
2366 rcu_read_lock();
2367 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2368 list) {
2369 if (tmp->flags == types[i]) {
2370 info = tmp;
2371 break;
2372 }
2373 }
2374 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002375
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002376 if (!info)
2377 continue;
2378 down_read(&info->groups_sem);
2379 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2380 if (!list_empty(&info->block_groups[c])) {
2381 get_block_group_info(&info->block_groups[c],
2382 &space);
2383 memcpy(dest, &space, sizeof(space));
2384 dest++;
2385 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002386 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002387 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002388 if (!slot_count)
2389 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002390 }
2391 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002392 }
Josef Bacik1406e432010-01-13 18:19:06 +00002393
Chris Mason7fde62b2010-03-16 15:40:10 -04002394 user_dest = (struct btrfs_ioctl_space_info *)
2395 (arg + sizeof(struct btrfs_ioctl_space_args));
2396
2397 if (copy_to_user(user_dest, dest_orig, alloc_size))
2398 ret = -EFAULT;
2399
2400 kfree(dest_orig);
2401out:
2402 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002403 ret = -EFAULT;
2404
2405 return ret;
2406}
2407
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002408/*
2409 * there are many ways the trans_start and trans_end ioctls can lead
2410 * to deadlocks. They should only be used by applications that
2411 * basically own the machine, and have a very in depth understanding
2412 * of all the possible deadlocks and enospc problems.
2413 */
2414long btrfs_ioctl_trans_end(struct file *file)
2415{
2416 struct inode *inode = fdentry(file)->d_inode;
2417 struct btrfs_root *root = BTRFS_I(inode)->root;
2418 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002419
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002420 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002421 if (!trans)
2422 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002423 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002424
Sage Weil1ab86ae2009-09-29 18:38:44 -04002425 btrfs_end_transaction(trans, root);
2426
Sage Weil9ca9ee02008-08-04 10:41:27 -04002427 mutex_lock(&root->fs_info->trans_mutex);
2428 root->fs_info->open_ioctl_trans--;
2429 mutex_unlock(&root->fs_info->trans_mutex);
2430
Sage Weilcfc8ea82008-12-11 16:30:06 -05002431 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002432 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002433}
2434
Sage Weil46204592010-10-29 15:41:32 -04002435static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2436{
2437 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2438 struct btrfs_trans_handle *trans;
2439 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002440 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002441
2442 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002443 if (IS_ERR(trans))
2444 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002445 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002446 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002447 if (ret) {
2448 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002449 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002450 }
Sage Weil46204592010-10-29 15:41:32 -04002451
2452 if (argp)
2453 if (copy_to_user(argp, &transid, sizeof(transid)))
2454 return -EFAULT;
2455 return 0;
2456}
2457
2458static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2459{
2460 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2461 u64 transid;
2462
2463 if (argp) {
2464 if (copy_from_user(&transid, argp, sizeof(transid)))
2465 return -EFAULT;
2466 } else {
2467 transid = 0; /* current trans */
2468 }
2469 return btrfs_wait_for_commit(root, transid);
2470}
2471
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002472long btrfs_ioctl(struct file *file, unsigned int
2473 cmd, unsigned long arg)
2474{
2475 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002476 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002477
2478 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002479 case FS_IOC_GETFLAGS:
2480 return btrfs_ioctl_getflags(file, argp);
2481 case FS_IOC_SETFLAGS:
2482 return btrfs_ioctl_setflags(file, argp);
2483 case FS_IOC_GETVERSION:
2484 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00002485 case FITRIM:
2486 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002487 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002488 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002489 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002490 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002491 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002492 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002493 case BTRFS_IOC_SNAP_DESTROY:
2494 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002495 case BTRFS_IOC_SUBVOL_GETFLAGS:
2496 return btrfs_ioctl_subvol_getflags(file, argp);
2497 case BTRFS_IOC_SUBVOL_SETFLAGS:
2498 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002499 case BTRFS_IOC_DEFAULT_SUBVOL:
2500 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002502 return btrfs_ioctl_defrag(file, NULL);
2503 case BTRFS_IOC_DEFRAG_RANGE:
2504 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002505 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002506 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002507 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002508 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002509 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002510 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002511 case BTRFS_IOC_BALANCE:
2512 return btrfs_balance(root->fs_info->dev_root);
2513 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002514 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2515 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002516 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002517 case BTRFS_IOC_TRANS_START:
2518 return btrfs_ioctl_trans_start(file);
2519 case BTRFS_IOC_TRANS_END:
2520 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002521 case BTRFS_IOC_TREE_SEARCH:
2522 return btrfs_ioctl_tree_search(file, argp);
2523 case BTRFS_IOC_INO_LOOKUP:
2524 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002525 case BTRFS_IOC_SPACE_INFO:
2526 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002527 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002528 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2529 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002530 case BTRFS_IOC_START_SYNC:
2531 return btrfs_ioctl_start_sync(file, argp);
2532 case BTRFS_IOC_WAIT_SYNC:
2533 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002534 }
2535
2536 return -ENOTTY;
2537}