blob: 0de71feb8e1c4a03ddadfe13eeb7b6fe56e2fb2a [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);
liubo2d4e6f6a2011-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);
liubo2d4e6f6a2011-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
Xiao Guangrong1f781602011-04-20 10:09:16 +0000284 rcu_read_lock();
285 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
286 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000287 if (!device->bdev)
288 continue;
289 q = bdev_get_queue(device->bdev);
290 if (blk_queue_discard(q)) {
291 num_devices++;
292 minlen = min((u64)q->limits.discard_granularity,
293 minlen);
294 }
295 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000296 rcu_read_unlock();
Li Dongyangf7039b12011-03-24 10:24:28 +0000297 if (!num_devices)
298 return -EOPNOTSUPP;
299
300 if (copy_from_user(&range, arg, sizeof(range)))
301 return -EFAULT;
302
303 range.minlen = max(range.minlen, minlen);
304 ret = btrfs_trim_fs(root, &range);
305 if (ret < 0)
306 return ret;
307
308 if (copy_to_user(arg, &range, sizeof(range)))
309 return -EFAULT;
310
311 return 0;
312}
313
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400314static noinline int create_subvol(struct btrfs_root *root,
315 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400316 char *name, int namelen,
317 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400318{
319 struct btrfs_trans_handle *trans;
320 struct btrfs_key key;
321 struct btrfs_root_item root_item;
322 struct btrfs_inode_item *inode_item;
323 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400324 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000325 struct dentry *parent = dget_parent(dentry);
326 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400327 int ret;
328 int err;
329 u64 objectid;
330 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500331 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400332
Yan, Zhenga22285a2010-05-16 10:48:46 -0400333 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
334 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000335 if (ret) {
336 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400337 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000338 }
339
340 dir = parent->d_inode;
341
Josef Bacik9ed74f22009-09-11 16:12:44 -0400342 /*
343 * 1 - inode item
344 * 2 - refs
345 * 1 - root item
346 * 2 - dir items
347 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400348 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000349 if (IS_ERR(trans)) {
350 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400351 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000352 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
355 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400356 if (IS_ERR(leaf)) {
357 ret = PTR_ERR(leaf);
358 goto fail;
359 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400360
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400361 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400362 btrfs_set_header_bytenr(leaf, leaf->start);
363 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400364 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400365 btrfs_set_header_owner(leaf, objectid);
366
367 write_extent_buffer(leaf, root->fs_info->fsid,
368 (unsigned long)btrfs_header_fsid(leaf),
369 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
371 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
372 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400373 btrfs_mark_buffer_dirty(leaf);
374
375 inode_item = &root_item.inode;
376 memset(inode_item, 0, sizeof(*inode_item));
377 inode_item->generation = cpu_to_le64(1);
378 inode_item->size = cpu_to_le64(3);
379 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400380 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400381 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
382
Li Zefan08fe4db2011-03-28 02:01:25 +0000383 root_item.flags = 0;
384 root_item.byte_limit = 0;
385 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
386
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400387 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400388 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 btrfs_set_root_level(&root_item, 0);
390 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000391 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400392 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400393
394 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
395 root_item.drop_level = 0;
396
Chris Mason925baed2008-06-25 16:01:30 -0400397 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398 free_extent_buffer(leaf);
399 leaf = NULL;
400
401 btrfs_set_root_dirid(&root_item, new_dirid);
402
403 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400404 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400405 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
406 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
407 &root_item);
408 if (ret)
409 goto fail;
410
Yan, Zheng76dda932009-09-21 16:00:26 -0400411 key.offset = (u64)-1;
412 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
413 BUG_ON(IS_ERR(new_root));
414
415 btrfs_record_root_in_trans(trans, new_root);
416
417 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
418 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400419 /*
420 * insert the directory item
421 */
Chris Mason3de45862008-11-17 21:02:50 -0500422 ret = btrfs_set_inode_index(dir, &index);
423 BUG_ON(ret);
424
425 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400426 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500427 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400428 if (ret)
429 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500430
Yan Zheng52c26172009-01-05 15:43:43 -0500431 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
432 ret = btrfs_update_inode(trans, root, dir);
433 BUG_ON(ret);
434
Chris Mason0660b5a2008-11-17 20:37:39 -0500435 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400436 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500437 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400438
Chris Mason0660b5a2008-11-17 20:37:39 -0500439 BUG_ON(ret);
440
Yan, Zheng76dda932009-09-21 16:00:26 -0400441 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000443 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400444 if (async_transid) {
445 *async_transid = trans->transid;
446 err = btrfs_commit_transaction_async(trans, root, 1);
447 } else {
448 err = btrfs_commit_transaction(trans, root);
449 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400450 if (err && !ret)
451 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400452 return ret;
453}
454
Sage Weil72fd0322010-10-29 15:41:32 -0400455static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800456 char *name, int namelen, u64 *async_transid,
457 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400458{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000459 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000460 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461 struct btrfs_pending_snapshot *pending_snapshot;
462 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000463 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464
465 if (!root->ref_cows)
466 return -EINVAL;
467
Yan, Zhenga22285a2010-05-16 10:48:46 -0400468 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
469 if (!pending_snapshot)
470 return -ENOMEM;
471
472 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
473 pending_snapshot->dentry = dentry;
474 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800475 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400476
477 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
478 if (IS_ERR(trans)) {
479 ret = PTR_ERR(trans);
480 goto fail;
481 }
482
483 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
484 BUG_ON(ret);
485
486 list_add(&pending_snapshot->list,
487 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400488 if (async_transid) {
489 *async_transid = trans->transid;
490 ret = btrfs_commit_transaction_async(trans,
491 root->fs_info->extent_root, 1);
492 } else {
493 ret = btrfs_commit_transaction(trans,
494 root->fs_info->extent_root);
495 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400496 BUG_ON(ret);
497
498 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400499 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000500 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400501
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500502 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
503 if (ret)
504 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400505
Josef Bacik6a912212010-11-20 09:48:00 +0000506 parent = dget_parent(dentry);
507 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
508 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000509 if (IS_ERR(inode)) {
510 ret = PTR_ERR(inode);
511 goto fail;
512 }
513 BUG_ON(!inode);
514 d_instantiate(dentry, inode);
515 ret = 0;
516fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400517 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400518 return ret;
519}
520
Sage Weil4260f7c2010-10-29 15:46:43 -0400521/* copy of check_sticky in fs/namei.c()
522* It's inline, so penalty for filesystems that don't use sticky bit is
523* minimal.
524*/
525static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
526{
527 uid_t fsuid = current_fsuid();
528
529 if (!(dir->i_mode & S_ISVTX))
530 return 0;
531 if (inode->i_uid == fsuid)
532 return 0;
533 if (dir->i_uid == fsuid)
534 return 0;
535 return !capable(CAP_FOWNER);
536}
537
538/* copy of may_delete in fs/namei.c()
539 * Check whether we can remove a link victim from directory dir, check
540 * whether the type of victim is right.
541 * 1. We can't do it if dir is read-only (done in permission())
542 * 2. We should have write and exec permissions on dir
543 * 3. We can't remove anything from append-only dir
544 * 4. We can't do anything with immutable dir (done in permission())
545 * 5. If the sticky bit on dir is set we should either
546 * a. be owner of dir, or
547 * b. be owner of victim, or
548 * c. have CAP_FOWNER capability
549 * 6. If the victim is append-only or immutable we can't do antyhing with
550 * links pointing to it.
551 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
552 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
553 * 9. We can't remove a root or mountpoint.
554 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
555 * nfs_async_unlink().
556 */
557
558static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
559{
560 int error;
561
562 if (!victim->d_inode)
563 return -ENOENT;
564
565 BUG_ON(victim->d_parent->d_inode != dir);
566 audit_inode_child(victim, dir);
567
568 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
569 if (error)
570 return error;
571 if (IS_APPEND(dir))
572 return -EPERM;
573 if (btrfs_check_sticky(dir, victim->d_inode)||
574 IS_APPEND(victim->d_inode)||
575 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
576 return -EPERM;
577 if (isdir) {
578 if (!S_ISDIR(victim->d_inode->i_mode))
579 return -ENOTDIR;
580 if (IS_ROOT(victim))
581 return -EBUSY;
582 } else if (S_ISDIR(victim->d_inode->i_mode))
583 return -EISDIR;
584 if (IS_DEADDIR(dir))
585 return -ENOENT;
586 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
587 return -EBUSY;
588 return 0;
589}
590
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400591/* copy of may_create in fs/namei.c() */
592static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
593{
594 if (child->d_inode)
595 return -EEXIST;
596 if (IS_DEADDIR(dir))
597 return -ENOENT;
598 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
599}
600
601/*
602 * Create a new subvolume below @parent. This is largely modeled after
603 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
604 * inside this filesystem so it's quite a bit simpler.
605 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400606static noinline int btrfs_mksubvol(struct path *parent,
607 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400608 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800609 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400610{
Yan, Zheng76dda932009-09-21 16:00:26 -0400611 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400612 struct dentry *dentry;
613 int error;
614
Yan, Zheng76dda932009-09-21 16:00:26 -0400615 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400616
617 dentry = lookup_one_len(name, parent->dentry, namelen);
618 error = PTR_ERR(dentry);
619 if (IS_ERR(dentry))
620 goto out_unlock;
621
622 error = -EEXIST;
623 if (dentry->d_inode)
624 goto out_dput;
625
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400626 error = mnt_want_write(parent->mnt);
627 if (error)
628 goto out_dput;
629
Yan, Zheng76dda932009-09-21 16:00:26 -0400630 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400631 if (error)
632 goto out_drop_write;
633
Yan, Zheng76dda932009-09-21 16:00:26 -0400634 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
635
636 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
637 goto out_up_read;
638
Chris Mason3de45862008-11-17 21:02:50 -0500639 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400640 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800641 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500642 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400643 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400644 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500645 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400646 if (!error)
647 fsnotify_mkdir(dir, dentry);
648out_up_read:
649 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400650out_drop_write:
651 mnt_drop_write(parent->mnt);
652out_dput:
653 dput(dentry);
654out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400655 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400656 return error;
657}
658
Chris Mason940100a2010-03-10 10:52:59 -0500659static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500660 int thresh, u64 *last_len, u64 *skip,
661 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500662{
663 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
664 struct extent_map *em = NULL;
665 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
666 int ret = 1;
667
Chris Mason1e701a32010-03-11 09:42:04 -0500668
669 if (thresh == 0)
670 thresh = 256 * 1024;
671
Chris Mason940100a2010-03-10 10:52:59 -0500672 /*
673 * make sure that once we start defragging and extent, we keep on
674 * defragging it
675 */
676 if (start < *defrag_end)
677 return 1;
678
679 *skip = 0;
680
681 /*
682 * hopefully we have this extent in the tree already, try without
683 * the full extent lock
684 */
685 read_lock(&em_tree->lock);
686 em = lookup_extent_mapping(em_tree, start, len);
687 read_unlock(&em_tree->lock);
688
689 if (!em) {
690 /* get the big lock and read metadata off disk */
691 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
692 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
693 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
694
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000695 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500696 return 0;
697 }
698
699 /* this will cover holes, and inline extents */
700 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
701 ret = 0;
702
703 /*
704 * we hit a real extent, if it is big don't bother defragging it again
705 */
Chris Mason1e701a32010-03-11 09:42:04 -0500706 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500707 ret = 0;
708
709 /*
710 * last_len ends up being a counter of how many bytes we've defragged.
711 * every time we choose not to defrag an extent, we reset *last_len
712 * so that the next tiny extent will force a defrag.
713 *
714 * The end result of this is that tiny extents before a single big
715 * extent will force at least part of that big extent to be defragged.
716 */
717 if (ret) {
718 *last_len += len;
719 *defrag_end = extent_map_end(em);
720 } else {
721 *last_len = 0;
722 *skip = extent_map_end(em);
723 *defrag_end = 0;
724 }
725
726 free_extent_map(em);
727 return ret;
728}
729
Chris Mason1e701a32010-03-11 09:42:04 -0500730static int btrfs_defrag_file(struct file *file,
731 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732{
733 struct inode *inode = fdentry(file)->d_inode;
734 struct btrfs_root *root = BTRFS_I(inode)->root;
735 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400736 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400737 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800738 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400739 unsigned long last_index;
740 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
741 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800742 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400743 u64 page_start;
744 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500745 u64 last_len = 0;
746 u64 skip = 0;
747 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748 unsigned long i;
749 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800750 int compress_type = BTRFS_COMPRESS_ZLIB;
751
752 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
753 if (range->compress_type > BTRFS_COMPRESS_TYPES)
754 return -EINVAL;
755 if (range->compress_type)
756 compress_type = range->compress_type;
757 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400758
Chris Mason940100a2010-03-10 10:52:59 -0500759 if (inode->i_size == 0)
760 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400761
Chris Mason1e701a32010-03-11 09:42:04 -0500762 if (range->start + range->len > range->start) {
763 last_index = min_t(u64, inode->i_size - 1,
764 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
765 } else {
766 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
767 }
768
769 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500770 while (i <= last_index) {
771 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500772 PAGE_CACHE_SIZE,
773 range->extent_thresh,
774 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500775 &defrag_end)) {
776 unsigned long next;
777 /*
778 * the should_defrag function tells us how much to skip
779 * bump our counter by the suggested amount
780 */
781 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
782 i = max(i + 1, next);
783 continue;
784 }
785
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400786 if (total_read % ra_pages == 0) {
787 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
788 min(last_index, i + ra_pages - 1));
789 }
790 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500791 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500792 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800793 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500794
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400795 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
796 if (ret)
797 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400798again:
Chris Mason940100a2010-03-10 10:52:59 -0500799 if (inode->i_size == 0 ||
800 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
801 ret = 0;
802 goto err_reservations;
803 }
804
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400805 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400806 if (!page) {
807 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500808 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400809 }
Chris Mason940100a2010-03-10 10:52:59 -0500810
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400811 if (!PageUptodate(page)) {
812 btrfs_readpage(NULL, page);
813 lock_page(page);
814 if (!PageUptodate(page)) {
815 unlock_page(page);
816 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400817 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500818 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400819 }
820 }
821
Chris Mason940100a2010-03-10 10:52:59 -0500822 if (page->mapping != inode->i_mapping) {
823 unlock_page(page);
824 page_cache_release(page);
825 goto again;
826 }
827
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400828 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400829
Chris Mason940100a2010-03-10 10:52:59 -0500830 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400831 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500832 goto loop_unlock;
833 }
834
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400835 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
836 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400837 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400838
839 ordered = btrfs_lookup_ordered_extent(inode, page_start);
840 if (ordered) {
841 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
842 unlock_page(page);
843 page_cache_release(page);
844 btrfs_start_ordered_extent(inode, ordered, 1);
845 btrfs_put_ordered_extent(ordered);
846 goto again;
847 }
848 set_page_extent_mapped(page);
849
Chris Masonf87f0572008-08-01 11:27:23 -0400850 /*
851 * this makes sure page_mkwrite is called on the
852 * page if it is dirtied again later
853 */
854 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500855 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
856 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
857 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400858
Josef Bacik2ac55d42010-02-03 19:33:23 +0000859 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500860 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400862 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500863
864loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400865 unlock_page(page);
866 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500867 mutex_unlock(&inode->i_mutex);
868
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400869 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500870 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400871 }
872
Chris Mason1e701a32010-03-11 09:42:04 -0500873 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
874 filemap_flush(inode->i_mapping);
875
876 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
877 /* the filemap_flush will queue IO into the worker threads, but
878 * we have to make sure the IO is actually started and that
879 * ordered extents get created before we return
880 */
881 atomic_inc(&root->fs_info->async_submit_draining);
882 while (atomic_read(&root->fs_info->nr_async_submits) ||
883 atomic_read(&root->fs_info->async_delalloc_pages)) {
884 wait_event(root->fs_info->async_submit_wait,
885 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
886 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
887 }
888 atomic_dec(&root->fs_info->async_submit_draining);
889
890 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800891 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500892 mutex_unlock(&inode->i_mutex);
893 }
894
Li Zefan1a419d82010-10-25 15:12:50 +0800895 disk_super = &root->fs_info->super_copy;
896 features = btrfs_super_incompat_flags(disk_super);
897 if (range->compress_type == BTRFS_COMPRESS_LZO) {
898 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
899 btrfs_set_super_incompat_flags(disk_super, features);
900 }
901
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400902 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500903
904err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400905 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
906err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500907 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500908 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400909}
910
Yan, Zheng76dda932009-09-21 16:00:26 -0400911static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
912 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400913{
914 u64 new_size;
915 u64 old_size;
916 u64 devid = 1;
917 struct btrfs_ioctl_vol_args *vol_args;
918 struct btrfs_trans_handle *trans;
919 struct btrfs_device *device = NULL;
920 char *sizestr;
921 char *devstr = NULL;
922 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400923 int mod = 0;
924
Yan Zhengc146afa2008-11-12 14:34:12 -0500925 if (root->fs_info->sb->s_flags & MS_RDONLY)
926 return -EROFS;
927
Chris Masone441d542009-01-05 16:57:23 -0500928 if (!capable(CAP_SYS_ADMIN))
929 return -EPERM;
930
Li Zefandae7b662009-04-08 15:06:54 +0800931 vol_args = memdup_user(arg, sizeof(*vol_args));
932 if (IS_ERR(vol_args))
933 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400934
935 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400936
Chris Mason7d9eb122008-07-08 14:19:17 -0400937 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400938 sizestr = vol_args->name;
939 devstr = strchr(sizestr, ':');
940 if (devstr) {
941 char *end;
942 sizestr = devstr + 1;
943 *devstr = '\0';
944 devstr = vol_args->name;
945 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700946 printk(KERN_INFO "resizing devid %llu\n",
947 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400948 }
Yan Zheng2b820322008-11-17 21:11:30 -0500949 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400950 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700951 printk(KERN_INFO "resizer unable to find device %llu\n",
952 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400953 ret = -EINVAL;
954 goto out_unlock;
955 }
956 if (!strcmp(sizestr, "max"))
957 new_size = device->bdev->bd_inode->i_size;
958 else {
959 if (sizestr[0] == '-') {
960 mod = -1;
961 sizestr++;
962 } else if (sizestr[0] == '+') {
963 mod = 1;
964 sizestr++;
965 }
Akinobu Mita91748462010-02-28 10:59:11 +0000966 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400967 if (new_size == 0) {
968 ret = -EINVAL;
969 goto out_unlock;
970 }
971 }
972
973 old_size = device->total_bytes;
974
975 if (mod < 0) {
976 if (new_size > old_size) {
977 ret = -EINVAL;
978 goto out_unlock;
979 }
980 new_size = old_size - new_size;
981 } else if (mod > 0) {
982 new_size = old_size + new_size;
983 }
984
985 if (new_size < 256 * 1024 * 1024) {
986 ret = -EINVAL;
987 goto out_unlock;
988 }
989 if (new_size > device->bdev->bd_inode->i_size) {
990 ret = -EFBIG;
991 goto out_unlock;
992 }
993
994 do_div(new_size, root->sectorsize);
995 new_size *= root->sectorsize;
996
997 printk(KERN_INFO "new size for %s is %llu\n",
998 device->name, (unsigned long long)new_size);
999
1000 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001001 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001002 if (IS_ERR(trans)) {
1003 ret = PTR_ERR(trans);
1004 goto out_unlock;
1005 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001006 ret = btrfs_grow_device(trans, device, new_size);
1007 btrfs_commit_transaction(trans, root);
1008 } else {
1009 ret = btrfs_shrink_device(device, new_size);
1010 }
1011
1012out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001013 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001014 kfree(vol_args);
1015 return ret;
1016}
1017
Sage Weil72fd0322010-10-29 15:41:32 -04001018static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1019 char *name,
1020 unsigned long fd,
1021 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001022 u64 *transid,
1023 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001024{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001025 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001026 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001027 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001028 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001029
Yan Zhengc146afa2008-11-12 14:34:12 -05001030 if (root->fs_info->sb->s_flags & MS_RDONLY)
1031 return -EROFS;
1032
Sage Weil72fd0322010-10-29 15:41:32 -04001033 namelen = strlen(name);
1034 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001035 ret = -EINVAL;
1036 goto out;
1037 }
1038
Chris Mason3de45862008-11-17 21:02:50 -05001039 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001040 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001041 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001042 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001043 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001044 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001045 if (!src_file) {
1046 ret = -EINVAL;
1047 goto out;
1048 }
1049
1050 src_inode = src_file->f_path.dentry->d_inode;
1051 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001052 printk(KERN_INFO "btrfs: Snapshot src from "
1053 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001054 ret = -EINVAL;
1055 fput(src_file);
1056 goto out;
1057 }
Sage Weil72fd0322010-10-29 15:41:32 -04001058 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1059 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001060 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001061 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001062 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001063out:
Sage Weil72fd0322010-10-29 15:41:32 -04001064 return ret;
1065}
1066
1067static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001068 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001069{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001070 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001071 int ret;
1072
Li Zefanfa0d2b92010-12-20 15:53:28 +08001073 vol_args = memdup_user(arg, sizeof(*vol_args));
1074 if (IS_ERR(vol_args))
1075 return PTR_ERR(vol_args);
1076 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001077
Li Zefanfa0d2b92010-12-20 15:53:28 +08001078 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001079 vol_args->fd, subvol,
1080 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001081
Li Zefanfa0d2b92010-12-20 15:53:28 +08001082 kfree(vol_args);
1083 return ret;
1084}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001085
Li Zefanfa0d2b92010-12-20 15:53:28 +08001086static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1087 void __user *arg, int subvol)
1088{
1089 struct btrfs_ioctl_vol_args_v2 *vol_args;
1090 int ret;
1091 u64 transid = 0;
1092 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001093 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001094
Li Zefanfa0d2b92010-12-20 15:53:28 +08001095 vol_args = memdup_user(arg, sizeof(*vol_args));
1096 if (IS_ERR(vol_args))
1097 return PTR_ERR(vol_args);
1098 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001099
Li Zefanb83cc962010-12-20 16:04:08 +08001100 if (vol_args->flags &
1101 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1102 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001103 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001104 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001105
1106 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1107 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001108 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1109 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001110
1111 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001112 vol_args->fd, subvol,
1113 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001114
1115 if (ret == 0 && ptr &&
1116 copy_to_user(arg +
1117 offsetof(struct btrfs_ioctl_vol_args_v2,
1118 transid), ptr, sizeof(*ptr)))
1119 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001120out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001121 kfree(vol_args);
1122 return ret;
1123}
1124
Li Zefan0caa1022010-12-20 16:30:25 +08001125static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1126 void __user *arg)
1127{
1128 struct inode *inode = fdentry(file)->d_inode;
1129 struct btrfs_root *root = BTRFS_I(inode)->root;
1130 int ret = 0;
1131 u64 flags = 0;
1132
1133 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1134 return -EINVAL;
1135
1136 down_read(&root->fs_info->subvol_sem);
1137 if (btrfs_root_readonly(root))
1138 flags |= BTRFS_SUBVOL_RDONLY;
1139 up_read(&root->fs_info->subvol_sem);
1140
1141 if (copy_to_user(arg, &flags, sizeof(flags)))
1142 ret = -EFAULT;
1143
1144 return ret;
1145}
1146
1147static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1148 void __user *arg)
1149{
1150 struct inode *inode = fdentry(file)->d_inode;
1151 struct btrfs_root *root = BTRFS_I(inode)->root;
1152 struct btrfs_trans_handle *trans;
1153 u64 root_flags;
1154 u64 flags;
1155 int ret = 0;
1156
1157 if (root->fs_info->sb->s_flags & MS_RDONLY)
1158 return -EROFS;
1159
1160 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1161 return -EINVAL;
1162
1163 if (copy_from_user(&flags, arg, sizeof(flags)))
1164 return -EFAULT;
1165
Li Zefanb4dc2b82011-02-16 06:06:34 +00001166 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001167 return -EINVAL;
1168
1169 if (flags & ~BTRFS_SUBVOL_RDONLY)
1170 return -EOPNOTSUPP;
1171
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001172 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001173 return -EACCES;
1174
Li Zefan0caa1022010-12-20 16:30:25 +08001175 down_write(&root->fs_info->subvol_sem);
1176
1177 /* nothing to do */
1178 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1179 goto out;
1180
1181 root_flags = btrfs_root_flags(&root->root_item);
1182 if (flags & BTRFS_SUBVOL_RDONLY)
1183 btrfs_set_root_flags(&root->root_item,
1184 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1185 else
1186 btrfs_set_root_flags(&root->root_item,
1187 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1188
1189 trans = btrfs_start_transaction(root, 1);
1190 if (IS_ERR(trans)) {
1191 ret = PTR_ERR(trans);
1192 goto out_reset;
1193 }
1194
Li Zefanb4dc2b82011-02-16 06:06:34 +00001195 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001196 &root->root_key, &root->root_item);
1197
1198 btrfs_commit_transaction(trans, root);
1199out_reset:
1200 if (ret)
1201 btrfs_set_root_flags(&root->root_item, root_flags);
1202out:
1203 up_write(&root->fs_info->subvol_sem);
1204 return ret;
1205}
1206
Yan, Zheng76dda932009-09-21 16:00:26 -04001207/*
1208 * helper to check if the subvolume references other subvolumes
1209 */
1210static noinline int may_destroy_subvol(struct btrfs_root *root)
1211{
1212 struct btrfs_path *path;
1213 struct btrfs_key key;
1214 int ret;
1215
1216 path = btrfs_alloc_path();
1217 if (!path)
1218 return -ENOMEM;
1219
1220 key.objectid = root->root_key.objectid;
1221 key.type = BTRFS_ROOT_REF_KEY;
1222 key.offset = (u64)-1;
1223
1224 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1225 &key, path, 0, 0);
1226 if (ret < 0)
1227 goto out;
1228 BUG_ON(ret == 0);
1229
1230 ret = 0;
1231 if (path->slots[0] > 0) {
1232 path->slots[0]--;
1233 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1234 if (key.objectid == root->root_key.objectid &&
1235 key.type == BTRFS_ROOT_REF_KEY)
1236 ret = -ENOTEMPTY;
1237 }
1238out:
1239 btrfs_free_path(path);
1240 return ret;
1241}
1242
Chris Masonac8e9812010-02-28 15:39:26 -05001243static noinline int key_in_sk(struct btrfs_key *key,
1244 struct btrfs_ioctl_search_key *sk)
1245{
Chris Masonabc6e132010-03-18 12:10:08 -04001246 struct btrfs_key test;
1247 int ret;
1248
1249 test.objectid = sk->min_objectid;
1250 test.type = sk->min_type;
1251 test.offset = sk->min_offset;
1252
1253 ret = btrfs_comp_cpu_keys(key, &test);
1254 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001255 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001256
1257 test.objectid = sk->max_objectid;
1258 test.type = sk->max_type;
1259 test.offset = sk->max_offset;
1260
1261 ret = btrfs_comp_cpu_keys(key, &test);
1262 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001263 return 0;
1264 return 1;
1265}
1266
1267static noinline int copy_to_sk(struct btrfs_root *root,
1268 struct btrfs_path *path,
1269 struct btrfs_key *key,
1270 struct btrfs_ioctl_search_key *sk,
1271 char *buf,
1272 unsigned long *sk_offset,
1273 int *num_found)
1274{
1275 u64 found_transid;
1276 struct extent_buffer *leaf;
1277 struct btrfs_ioctl_search_header sh;
1278 unsigned long item_off;
1279 unsigned long item_len;
1280 int nritems;
1281 int i;
1282 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001283 int ret = 0;
1284
1285 leaf = path->nodes[0];
1286 slot = path->slots[0];
1287 nritems = btrfs_header_nritems(leaf);
1288
1289 if (btrfs_header_generation(leaf) > sk->max_transid) {
1290 i = nritems;
1291 goto advance_key;
1292 }
1293 found_transid = btrfs_header_generation(leaf);
1294
1295 for (i = slot; i < nritems; i++) {
1296 item_off = btrfs_item_ptr_offset(leaf, i);
1297 item_len = btrfs_item_size_nr(leaf, i);
1298
1299 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1300 item_len = 0;
1301
1302 if (sizeof(sh) + item_len + *sk_offset >
1303 BTRFS_SEARCH_ARGS_BUFSIZE) {
1304 ret = 1;
1305 goto overflow;
1306 }
1307
1308 btrfs_item_key_to_cpu(leaf, key, i);
1309 if (!key_in_sk(key, sk))
1310 continue;
1311
1312 sh.objectid = key->objectid;
1313 sh.offset = key->offset;
1314 sh.type = key->type;
1315 sh.len = item_len;
1316 sh.transid = found_transid;
1317
1318 /* copy search result header */
1319 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1320 *sk_offset += sizeof(sh);
1321
1322 if (item_len) {
1323 char *p = buf + *sk_offset;
1324 /* copy the item */
1325 read_extent_buffer(leaf, p,
1326 item_off, item_len);
1327 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001328 }
Hugo Millse2156862011-05-14 17:43:41 +00001329 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001330
1331 if (*num_found >= sk->nr_items)
1332 break;
1333 }
1334advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001335 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001336 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1337 key->offset++;
1338 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1339 key->offset = 0;
1340 key->type++;
1341 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1342 key->offset = 0;
1343 key->type = 0;
1344 key->objectid++;
1345 } else
1346 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001347overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001348 return ret;
1349}
1350
1351static noinline int search_ioctl(struct inode *inode,
1352 struct btrfs_ioctl_search_args *args)
1353{
1354 struct btrfs_root *root;
1355 struct btrfs_key key;
1356 struct btrfs_key max_key;
1357 struct btrfs_path *path;
1358 struct btrfs_ioctl_search_key *sk = &args->key;
1359 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1360 int ret;
1361 int num_found = 0;
1362 unsigned long sk_offset = 0;
1363
1364 path = btrfs_alloc_path();
1365 if (!path)
1366 return -ENOMEM;
1367
1368 if (sk->tree_id == 0) {
1369 /* search the root of the inode that was passed */
1370 root = BTRFS_I(inode)->root;
1371 } else {
1372 key.objectid = sk->tree_id;
1373 key.type = BTRFS_ROOT_ITEM_KEY;
1374 key.offset = (u64)-1;
1375 root = btrfs_read_fs_root_no_name(info, &key);
1376 if (IS_ERR(root)) {
1377 printk(KERN_ERR "could not find root %llu\n",
1378 sk->tree_id);
1379 btrfs_free_path(path);
1380 return -ENOENT;
1381 }
1382 }
1383
1384 key.objectid = sk->min_objectid;
1385 key.type = sk->min_type;
1386 key.offset = sk->min_offset;
1387
1388 max_key.objectid = sk->max_objectid;
1389 max_key.type = sk->max_type;
1390 max_key.offset = sk->max_offset;
1391
1392 path->keep_locks = 1;
1393
1394 while(1) {
1395 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1396 sk->min_transid);
1397 if (ret != 0) {
1398 if (ret > 0)
1399 ret = 0;
1400 goto err;
1401 }
1402 ret = copy_to_sk(root, path, &key, sk, args->buf,
1403 &sk_offset, &num_found);
1404 btrfs_release_path(root, path);
1405 if (ret || num_found >= sk->nr_items)
1406 break;
1407
1408 }
1409 ret = 0;
1410err:
1411 sk->nr_items = num_found;
1412 btrfs_free_path(path);
1413 return ret;
1414}
1415
1416static noinline int btrfs_ioctl_tree_search(struct file *file,
1417 void __user *argp)
1418{
1419 struct btrfs_ioctl_search_args *args;
1420 struct inode *inode;
1421 int ret;
1422
1423 if (!capable(CAP_SYS_ADMIN))
1424 return -EPERM;
1425
Julia Lawall2354d082010-10-29 15:14:18 -04001426 args = memdup_user(argp, sizeof(*args));
1427 if (IS_ERR(args))
1428 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001429
Chris Masonac8e9812010-02-28 15:39:26 -05001430 inode = fdentry(file)->d_inode;
1431 ret = search_ioctl(inode, args);
1432 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1433 ret = -EFAULT;
1434 kfree(args);
1435 return ret;
1436}
1437
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001438/*
Chris Masonac8e9812010-02-28 15:39:26 -05001439 * Search INODE_REFs to identify path name of 'dirid' directory
1440 * in a 'tree_id' tree. and sets path name to 'name'.
1441 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001442static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1443 u64 tree_id, u64 dirid, char *name)
1444{
1445 struct btrfs_root *root;
1446 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001447 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001448 int ret = -1;
1449 int slot;
1450 int len;
1451 int total_len = 0;
1452 struct btrfs_inode_ref *iref;
1453 struct extent_buffer *l;
1454 struct btrfs_path *path;
1455
1456 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1457 name[0]='\0';
1458 return 0;
1459 }
1460
1461 path = btrfs_alloc_path();
1462 if (!path)
1463 return -ENOMEM;
1464
Chris Masonac8e9812010-02-28 15:39:26 -05001465 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001466
1467 key.objectid = tree_id;
1468 key.type = BTRFS_ROOT_ITEM_KEY;
1469 key.offset = (u64)-1;
1470 root = btrfs_read_fs_root_no_name(info, &key);
1471 if (IS_ERR(root)) {
1472 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001473 ret = -ENOENT;
1474 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001475 }
1476
1477 key.objectid = dirid;
1478 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001479 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001480
1481 while(1) {
1482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1483 if (ret < 0)
1484 goto out;
1485
1486 l = path->nodes[0];
1487 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001488 if (ret > 0 && slot > 0)
1489 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001490 btrfs_item_key_to_cpu(l, &key, slot);
1491
1492 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001493 key.type != BTRFS_INODE_REF_KEY)) {
1494 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001495 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001496 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001497
1498 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1499 len = btrfs_inode_ref_name_len(l, iref);
1500 ptr -= len + 1;
1501 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001502 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001503 goto out;
1504
1505 *(ptr + len) = '/';
1506 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1507
1508 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1509 break;
1510
1511 btrfs_release_path(root, path);
1512 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001513 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001514 dirid = key.objectid;
1515
1516 }
Chris Masonac8e9812010-02-28 15:39:26 -05001517 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001518 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001519 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001520 name[total_len]='\0';
1521 ret = 0;
1522out:
1523 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001524 return ret;
1525}
1526
1527static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1528 void __user *argp)
1529{
1530 struct btrfs_ioctl_ino_lookup_args *args;
1531 struct inode *inode;
1532 int ret;
1533
1534 if (!capable(CAP_SYS_ADMIN))
1535 return -EPERM;
1536
Julia Lawall2354d082010-10-29 15:14:18 -04001537 args = memdup_user(argp, sizeof(*args));
1538 if (IS_ERR(args))
1539 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001540
Chris Masonac8e9812010-02-28 15:39:26 -05001541 inode = fdentry(file)->d_inode;
1542
Chris Mason1b53ac42010-03-18 12:17:05 -04001543 if (args->treeid == 0)
1544 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1545
Chris Masonac8e9812010-02-28 15:39:26 -05001546 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1547 args->treeid, args->objectid,
1548 args->name);
1549
1550 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1551 ret = -EFAULT;
1552
1553 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001554 return ret;
1555}
1556
Yan, Zheng76dda932009-09-21 16:00:26 -04001557static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1558 void __user *arg)
1559{
1560 struct dentry *parent = fdentry(file);
1561 struct dentry *dentry;
1562 struct inode *dir = parent->d_inode;
1563 struct inode *inode;
1564 struct btrfs_root *root = BTRFS_I(dir)->root;
1565 struct btrfs_root *dest = NULL;
1566 struct btrfs_ioctl_vol_args *vol_args;
1567 struct btrfs_trans_handle *trans;
1568 int namelen;
1569 int ret;
1570 int err = 0;
1571
Yan, Zheng76dda932009-09-21 16:00:26 -04001572 vol_args = memdup_user(arg, sizeof(*vol_args));
1573 if (IS_ERR(vol_args))
1574 return PTR_ERR(vol_args);
1575
1576 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1577 namelen = strlen(vol_args->name);
1578 if (strchr(vol_args->name, '/') ||
1579 strncmp(vol_args->name, "..", namelen) == 0) {
1580 err = -EINVAL;
1581 goto out;
1582 }
1583
1584 err = mnt_want_write(file->f_path.mnt);
1585 if (err)
1586 goto out;
1587
1588 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1589 dentry = lookup_one_len(vol_args->name, parent, namelen);
1590 if (IS_ERR(dentry)) {
1591 err = PTR_ERR(dentry);
1592 goto out_unlock_dir;
1593 }
1594
1595 if (!dentry->d_inode) {
1596 err = -ENOENT;
1597 goto out_dput;
1598 }
1599
1600 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001601 dest = BTRFS_I(inode)->root;
1602 if (!capable(CAP_SYS_ADMIN)){
1603 /*
1604 * Regular user. Only allow this with a special mount
1605 * option, when the user has write+exec access to the
1606 * subvol root, and when rmdir(2) would have been
1607 * allowed.
1608 *
1609 * Note that this is _not_ check that the subvol is
1610 * empty or doesn't contain data that we wouldn't
1611 * otherwise be able to delete.
1612 *
1613 * Users who want to delete empty subvols should try
1614 * rmdir(2).
1615 */
1616 err = -EPERM;
1617 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1618 goto out_dput;
1619
1620 /*
1621 * Do not allow deletion if the parent dir is the same
1622 * as the dir to be deleted. That means the ioctl
1623 * must be called on the dentry referencing the root
1624 * of the subvol, not a random directory contained
1625 * within it.
1626 */
1627 err = -EINVAL;
1628 if (root == dest)
1629 goto out_dput;
1630
1631 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1632 if (err)
1633 goto out_dput;
1634
1635 /* check if subvolume may be deleted by a non-root user */
1636 err = btrfs_may_delete(dir, dentry, 1);
1637 if (err)
1638 goto out_dput;
1639 }
1640
Yan, Zheng76dda932009-09-21 16:00:26 -04001641 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1642 err = -EINVAL;
1643 goto out_dput;
1644 }
1645
Yan, Zheng76dda932009-09-21 16:00:26 -04001646 mutex_lock(&inode->i_mutex);
1647 err = d_invalidate(dentry);
1648 if (err)
1649 goto out_unlock;
1650
1651 down_write(&root->fs_info->subvol_sem);
1652
1653 err = may_destroy_subvol(dest);
1654 if (err)
1655 goto out_up_write;
1656
Yan, Zhenga22285a2010-05-16 10:48:46 -04001657 trans = btrfs_start_transaction(root, 0);
1658 if (IS_ERR(trans)) {
1659 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001660 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001661 }
1662 trans->block_rsv = &root->fs_info->global_block_rsv;
1663
Yan, Zheng76dda932009-09-21 16:00:26 -04001664 ret = btrfs_unlink_subvol(trans, root, dir,
1665 dest->root_key.objectid,
1666 dentry->d_name.name,
1667 dentry->d_name.len);
1668 BUG_ON(ret);
1669
1670 btrfs_record_root_in_trans(trans, dest);
1671
1672 memset(&dest->root_item.drop_progress, 0,
1673 sizeof(dest->root_item.drop_progress));
1674 dest->root_item.drop_level = 0;
1675 btrfs_set_root_refs(&dest->root_item, 0);
1676
Yan, Zhengd68fc572010-05-16 10:49:58 -04001677 if (!xchg(&dest->orphan_item_inserted, 1)) {
1678 ret = btrfs_insert_orphan_item(trans,
1679 root->fs_info->tree_root,
1680 dest->root_key.objectid);
1681 BUG_ON(ret);
1682 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001683
Sage Weil531cb132010-10-29 15:41:32 -04001684 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001685 BUG_ON(ret);
1686 inode->i_flags |= S_DEAD;
1687out_up_write:
1688 up_write(&root->fs_info->subvol_sem);
1689out_unlock:
1690 mutex_unlock(&inode->i_mutex);
1691 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001692 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001693 btrfs_invalidate_inodes(dest);
1694 d_delete(dentry);
1695 }
1696out_dput:
1697 dput(dentry);
1698out_unlock_dir:
1699 mutex_unlock(&dir->i_mutex);
1700 mnt_drop_write(file->f_path.mnt);
1701out:
1702 kfree(vol_args);
1703 return err;
1704}
1705
Chris Mason1e701a32010-03-11 09:42:04 -05001706static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001707{
1708 struct inode *inode = fdentry(file)->d_inode;
1709 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001710 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001711 int ret;
1712
Li Zefanb83cc962010-12-20 16:04:08 +08001713 if (btrfs_root_readonly(root))
1714 return -EROFS;
1715
Yan Zhengc146afa2008-11-12 14:34:12 -05001716 ret = mnt_want_write(file->f_path.mnt);
1717 if (ret)
1718 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719
1720 switch (inode->i_mode & S_IFMT) {
1721 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001722 if (!capable(CAP_SYS_ADMIN)) {
1723 ret = -EPERM;
1724 goto out;
1725 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001726 ret = btrfs_defrag_root(root, 0);
1727 if (ret)
1728 goto out;
1729 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001730 break;
1731 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001732 if (!(file->f_mode & FMODE_WRITE)) {
1733 ret = -EINVAL;
1734 goto out;
1735 }
Chris Mason1e701a32010-03-11 09:42:04 -05001736
1737 range = kzalloc(sizeof(*range), GFP_KERNEL);
1738 if (!range) {
1739 ret = -ENOMEM;
1740 goto out;
1741 }
1742
1743 if (argp) {
1744 if (copy_from_user(range, argp,
1745 sizeof(*range))) {
1746 ret = -EFAULT;
1747 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001748 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001749 }
1750 /* compression requires us to start the IO */
1751 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1752 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1753 range->extent_thresh = (u32)-1;
1754 }
1755 } else {
1756 /* the rest are all set to zero by kzalloc */
1757 range->len = (u64)-1;
1758 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001759 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001760 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001761 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001762 default:
1763 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001764 }
Chris Masone441d542009-01-05 16:57:23 -05001765out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001766 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001767 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001768}
1769
Christoph Hellwigb2950862008-12-02 09:54:17 -05001770static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001771{
1772 struct btrfs_ioctl_vol_args *vol_args;
1773 int ret;
1774
Chris Masone441d542009-01-05 16:57:23 -05001775 if (!capable(CAP_SYS_ADMIN))
1776 return -EPERM;
1777
Li Zefandae7b662009-04-08 15:06:54 +08001778 vol_args = memdup_user(arg, sizeof(*vol_args));
1779 if (IS_ERR(vol_args))
1780 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001781
Mark Fasheh5516e592008-07-24 12:20:14 -04001782 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001783 ret = btrfs_init_new_device(root, vol_args->name);
1784
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001785 kfree(vol_args);
1786 return ret;
1787}
1788
Christoph Hellwigb2950862008-12-02 09:54:17 -05001789static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001790{
1791 struct btrfs_ioctl_vol_args *vol_args;
1792 int ret;
1793
Chris Masone441d542009-01-05 16:57:23 -05001794 if (!capable(CAP_SYS_ADMIN))
1795 return -EPERM;
1796
Yan Zhengc146afa2008-11-12 14:34:12 -05001797 if (root->fs_info->sb->s_flags & MS_RDONLY)
1798 return -EROFS;
1799
Li Zefandae7b662009-04-08 15:06:54 +08001800 vol_args = memdup_user(arg, sizeof(*vol_args));
1801 if (IS_ERR(vol_args))
1802 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001803
Mark Fasheh5516e592008-07-24 12:20:14 -04001804 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001805 ret = btrfs_rm_device(root, vol_args->name);
1806
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001807 kfree(vol_args);
1808 return ret;
1809}
1810
Yan, Zheng76dda932009-09-21 16:00:26 -04001811static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1812 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001813{
1814 struct inode *inode = fdentry(file)->d_inode;
1815 struct btrfs_root *root = BTRFS_I(inode)->root;
1816 struct file *src_file;
1817 struct inode *src;
1818 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001819 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001820 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001821 char *buf;
1822 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001823 u32 nritems;
1824 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001825 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001826 u64 len = olen;
1827 u64 bs = root->fs_info->sb->s_blocksize;
1828 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001829
Sage Weilc5c9cd42008-11-12 14:32:25 -05001830 /*
1831 * TODO:
1832 * - split compressed inline extents. annoying: we need to
1833 * decompress into destination's address_space (the file offset
1834 * may change, so source mapping won't do), then recompress (or
1835 * otherwise reinsert) a subrange.
1836 * - allow ranges within the same file to be cloned (provided
1837 * they don't overlap)?
1838 */
1839
Chris Masone441d542009-01-05 16:57:23 -05001840 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001841 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001842 return -EINVAL;
1843
Li Zefanb83cc962010-12-20 16:04:08 +08001844 if (btrfs_root_readonly(root))
1845 return -EROFS;
1846
Yan Zhengc146afa2008-11-12 14:34:12 -05001847 ret = mnt_want_write(file->f_path.mnt);
1848 if (ret)
1849 return ret;
1850
Sage Weilc5c9cd42008-11-12 14:32:25 -05001851 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001852 if (!src_file) {
1853 ret = -EBADF;
1854 goto out_drop_write;
1855 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001856
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001857 src = src_file->f_dentry->d_inode;
1858
Sage Weilc5c9cd42008-11-12 14:32:25 -05001859 ret = -EINVAL;
1860 if (src == inode)
1861 goto out_fput;
1862
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001863 /* the src must be open for reading */
1864 if (!(src_file->f_mode & FMODE_READ))
1865 goto out_fput;
1866
Yan Zhengae01a0a2008-08-04 23:23:47 -04001867 ret = -EISDIR;
1868 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001869 goto out_fput;
1870
Yan Zhengae01a0a2008-08-04 23:23:47 -04001871 ret = -EXDEV;
1872 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1873 goto out_fput;
1874
1875 ret = -ENOMEM;
1876 buf = vmalloc(btrfs_level_size(root, 0));
1877 if (!buf)
1878 goto out_fput;
1879
1880 path = btrfs_alloc_path();
1881 if (!path) {
1882 vfree(buf);
1883 goto out_fput;
1884 }
1885 path->reada = 2;
1886
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001887 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001888 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1889 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001890 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001891 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1892 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001893 }
1894
Sage Weilc5c9cd42008-11-12 14:32:25 -05001895 /* determine range to clone */
1896 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001897 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001898 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001899 if (len == 0)
1900 olen = len = src->i_size - off;
1901 /* if we extend to eof, continue to block boundary */
1902 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001903 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001904
1905 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001906 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1907 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001908 goto out_unlock;
1909
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001910 /* do any pending delalloc/csum calc on src, one way or
1911 another, and lock file content */
1912 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001913 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001914 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001915 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1916 if (!ordered &&
1917 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1918 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001919 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001920 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001921 if (ordered)
1922 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001923 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001924 }
1925
Sage Weilc5c9cd42008-11-12 14:32:25 -05001926 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001927 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001928 key.type = BTRFS_EXTENT_DATA_KEY;
1929 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001930
1931 while (1) {
1932 /*
1933 * note the key will change type as we walk through the
1934 * tree.
1935 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001936 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001937 if (ret < 0)
1938 goto out;
1939
Yan Zhengae01a0a2008-08-04 23:23:47 -04001940 nritems = btrfs_header_nritems(path->nodes[0]);
1941 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001942 ret = btrfs_next_leaf(root, path);
1943 if (ret < 0)
1944 goto out;
1945 if (ret > 0)
1946 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001947 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001948 }
1949 leaf = path->nodes[0];
1950 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001951
Yan Zhengae01a0a2008-08-04 23:23:47 -04001952 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001953 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001954 key.objectid != src->i_ino)
1955 break;
1956
Sage Weilc5c9cd42008-11-12 14:32:25 -05001957 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1958 struct btrfs_file_extent_item *extent;
1959 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001960 u32 size;
1961 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001962 u64 disko = 0, diskl = 0;
1963 u64 datao = 0, datal = 0;
1964 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001965 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001966
1967 size = btrfs_item_size_nr(leaf, slot);
1968 read_extent_buffer(leaf, buf,
1969 btrfs_item_ptr_offset(leaf, slot),
1970 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001971
1972 extent = btrfs_item_ptr(leaf, slot,
1973 struct btrfs_file_extent_item);
1974 comp = btrfs_file_extent_compression(leaf, extent);
1975 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001976 if (type == BTRFS_FILE_EXTENT_REG ||
1977 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001978 disko = btrfs_file_extent_disk_bytenr(leaf,
1979 extent);
1980 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1981 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001982 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001983 datal = btrfs_file_extent_num_bytes(leaf,
1984 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001985 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1986 /* take upper bound, may be compressed */
1987 datal = btrfs_file_extent_ram_bytes(leaf,
1988 extent);
1989 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001990 btrfs_release_path(root, path);
1991
Sage Weil050006a2010-10-29 15:37:33 -04001992 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001993 key.offset >= off+len)
1994 goto next;
1995
Zheng Yan31840ae2008-09-23 13:14:14 -04001996 memcpy(&new_key, &key, sizeof(new_key));
1997 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001998 if (off <= key.offset)
1999 new_key.offset = key.offset + destoff - off;
2000 else
2001 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002002
Yan, Zhenga22285a2010-05-16 10:48:46 -04002003 trans = btrfs_start_transaction(root, 1);
2004 if (IS_ERR(trans)) {
2005 ret = PTR_ERR(trans);
2006 goto out;
2007 }
2008
Chris Masonc8a894d2009-06-27 21:07:03 -04002009 if (type == BTRFS_FILE_EXTENT_REG ||
2010 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002011 if (off > key.offset) {
2012 datao += off - key.offset;
2013 datal -= off - key.offset;
2014 }
2015
2016 if (key.offset + datal > off + len)
2017 datal = off + len - key.offset;
2018
2019 ret = btrfs_drop_extents(trans, inode,
2020 new_key.offset,
2021 new_key.offset + datal,
2022 &hint_byte, 1);
2023 BUG_ON(ret);
2024
Sage Weilc5c9cd42008-11-12 14:32:25 -05002025 ret = btrfs_insert_empty_item(trans, root, path,
2026 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002027 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002028
2029 leaf = path->nodes[0];
2030 slot = path->slots[0];
2031 write_extent_buffer(leaf, buf,
2032 btrfs_item_ptr_offset(leaf, slot),
2033 size);
2034
2035 extent = btrfs_item_ptr(leaf, slot,
2036 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002037
Sage Weilc5c9cd42008-11-12 14:32:25 -05002038 /* disko == 0 means it's a hole */
2039 if (!disko)
2040 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002041
2042 btrfs_set_file_extent_offset(leaf, extent,
2043 datao);
2044 btrfs_set_file_extent_num_bytes(leaf, extent,
2045 datal);
2046 if (disko) {
2047 inode_add_bytes(inode, datal);
2048 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002049 disko, diskl, 0,
2050 root->root_key.objectid,
2051 inode->i_ino,
2052 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002053 BUG_ON(ret);
2054 }
2055 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2056 u64 skip = 0;
2057 u64 trim = 0;
2058 if (off > key.offset) {
2059 skip = off - key.offset;
2060 new_key.offset += skip;
2061 }
Chris Masond3977122009-01-05 21:25:51 -05002062
Sage Weilc5c9cd42008-11-12 14:32:25 -05002063 if (key.offset + datal > off+len)
2064 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002065
Sage Weilc5c9cd42008-11-12 14:32:25 -05002066 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002067 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002068 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002069 goto out;
2070 }
2071 size -= skip + trim;
2072 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002073
2074 ret = btrfs_drop_extents(trans, inode,
2075 new_key.offset,
2076 new_key.offset + datal,
2077 &hint_byte, 1);
2078 BUG_ON(ret);
2079
Sage Weilc5c9cd42008-11-12 14:32:25 -05002080 ret = btrfs_insert_empty_item(trans, root, path,
2081 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002082 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002083
2084 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002085 u32 start =
2086 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002087 memmove(buf+start, buf+start+skip,
2088 datal);
2089 }
2090
2091 leaf = path->nodes[0];
2092 slot = path->slots[0];
2093 write_extent_buffer(leaf, buf,
2094 btrfs_item_ptr_offset(leaf, slot),
2095 size);
2096 inode_add_bytes(inode, datal);
2097 }
2098
2099 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002100 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002101
Yan, Zhenga22285a2010-05-16 10:48:46 -04002102 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002103
2104 /*
2105 * we round up to the block size at eof when
2106 * determining which extents to clone above,
2107 * but shouldn't round up the file size
2108 */
2109 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002110 if (endoff > destoff+olen)
2111 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002112 if (endoff > inode->i_size)
2113 btrfs_i_size_write(inode, endoff);
2114
Yan, Zhenga22285a2010-05-16 10:48:46 -04002115 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2116 ret = btrfs_update_inode(trans, root, inode);
2117 BUG_ON(ret);
2118 btrfs_end_transaction(trans, root);
2119 }
Chris Masond3977122009-01-05 21:25:51 -05002120next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002121 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002122 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002123 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002124 ret = 0;
2125out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002126 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002127 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002128out_unlock:
2129 mutex_unlock(&src->i_mutex);
2130 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002131 vfree(buf);
2132 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002133out_fput:
2134 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002135out_drop_write:
2136 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002137 return ret;
2138}
2139
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002140static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002141{
2142 struct btrfs_ioctl_clone_range_args args;
2143
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002144 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002145 return -EFAULT;
2146 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2147 args.src_length, args.dest_offset);
2148}
2149
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002150/*
2151 * there are many ways the trans_start and trans_end ioctls can lead
2152 * to deadlocks. They should only be used by applications that
2153 * basically own the machine, and have a very in depth understanding
2154 * of all the possible deadlocks and enospc problems.
2155 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002156static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002157{
2158 struct inode *inode = fdentry(file)->d_inode;
2159 struct btrfs_root *root = BTRFS_I(inode)->root;
2160 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002161 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002162
Sage Weil1ab86ae2009-09-29 18:38:44 -04002163 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002164 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002165 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002166
2167 ret = -EINPROGRESS;
2168 if (file->private_data)
2169 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002170
Li Zefanb83cc962010-12-20 16:04:08 +08002171 ret = -EROFS;
2172 if (btrfs_root_readonly(root))
2173 goto out;
2174
Yan Zhengc146afa2008-11-12 14:34:12 -05002175 ret = mnt_want_write(file->f_path.mnt);
2176 if (ret)
2177 goto out;
2178
Sage Weil9ca9ee02008-08-04 10:41:27 -04002179 mutex_lock(&root->fs_info->trans_mutex);
2180 root->fs_info->open_ioctl_trans++;
2181 mutex_unlock(&root->fs_info->trans_mutex);
2182
Sage Weil1ab86ae2009-09-29 18:38:44 -04002183 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002184 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002185 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002186 goto out_drop;
2187
2188 file->private_data = trans;
2189 return 0;
2190
2191out_drop:
2192 mutex_lock(&root->fs_info->trans_mutex);
2193 root->fs_info->open_ioctl_trans--;
2194 mutex_unlock(&root->fs_info->trans_mutex);
2195 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002196out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002197 return ret;
2198}
2199
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002200static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2201{
2202 struct inode *inode = fdentry(file)->d_inode;
2203 struct btrfs_root *root = BTRFS_I(inode)->root;
2204 struct btrfs_root *new_root;
2205 struct btrfs_dir_item *di;
2206 struct btrfs_trans_handle *trans;
2207 struct btrfs_path *path;
2208 struct btrfs_key location;
2209 struct btrfs_disk_key disk_key;
2210 struct btrfs_super_block *disk_super;
2211 u64 features;
2212 u64 objectid = 0;
2213 u64 dir_id;
2214
2215 if (!capable(CAP_SYS_ADMIN))
2216 return -EPERM;
2217
2218 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2219 return -EFAULT;
2220
2221 if (!objectid)
2222 objectid = root->root_key.objectid;
2223
2224 location.objectid = objectid;
2225 location.type = BTRFS_ROOT_ITEM_KEY;
2226 location.offset = (u64)-1;
2227
2228 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2229 if (IS_ERR(new_root))
2230 return PTR_ERR(new_root);
2231
2232 if (btrfs_root_refs(&new_root->root_item) == 0)
2233 return -ENOENT;
2234
2235 path = btrfs_alloc_path();
2236 if (!path)
2237 return -ENOMEM;
2238 path->leave_spinning = 1;
2239
2240 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002241 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002242 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002243 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002244 }
2245
2246 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2247 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2248 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002249 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002250 btrfs_free_path(path);
2251 btrfs_end_transaction(trans, root);
2252 printk(KERN_ERR "Umm, you don't have the default dir item, "
2253 "this isn't going to work\n");
2254 return -ENOENT;
2255 }
2256
2257 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2258 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2259 btrfs_mark_buffer_dirty(path->nodes[0]);
2260 btrfs_free_path(path);
2261
2262 disk_super = &root->fs_info->super_copy;
2263 features = btrfs_super_incompat_flags(disk_super);
2264 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2265 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2266 btrfs_set_super_incompat_flags(disk_super, features);
2267 }
2268 btrfs_end_transaction(trans, root);
2269
2270 return 0;
2271}
2272
Josef Bacikbf5fc092010-09-29 11:22:36 -04002273static void get_block_group_info(struct list_head *groups_list,
2274 struct btrfs_ioctl_space_info *space)
2275{
2276 struct btrfs_block_group_cache *block_group;
2277
2278 space->total_bytes = 0;
2279 space->used_bytes = 0;
2280 space->flags = 0;
2281 list_for_each_entry(block_group, groups_list, list) {
2282 space->flags = block_group->flags;
2283 space->total_bytes += block_group->key.offset;
2284 space->used_bytes +=
2285 btrfs_block_group_used(&block_group->item);
2286 }
2287}
2288
Josef Bacik1406e432010-01-13 18:19:06 +00002289long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2290{
2291 struct btrfs_ioctl_space_args space_args;
2292 struct btrfs_ioctl_space_info space;
2293 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002294 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002295 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002296 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002297 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2298 BTRFS_BLOCK_GROUP_SYSTEM,
2299 BTRFS_BLOCK_GROUP_METADATA,
2300 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2301 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002302 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002303 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002304 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002305 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002306
2307 if (copy_from_user(&space_args,
2308 (struct btrfs_ioctl_space_args __user *)arg,
2309 sizeof(space_args)))
2310 return -EFAULT;
2311
Josef Bacikbf5fc092010-09-29 11:22:36 -04002312 for (i = 0; i < num_types; i++) {
2313 struct btrfs_space_info *tmp;
2314
2315 info = NULL;
2316 rcu_read_lock();
2317 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2318 list) {
2319 if (tmp->flags == types[i]) {
2320 info = tmp;
2321 break;
2322 }
2323 }
2324 rcu_read_unlock();
2325
2326 if (!info)
2327 continue;
2328
2329 down_read(&info->groups_sem);
2330 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2331 if (!list_empty(&info->block_groups[c]))
2332 slot_count++;
2333 }
2334 up_read(&info->groups_sem);
2335 }
Josef Bacik1406e432010-01-13 18:19:06 +00002336
Chris Mason7fde62b2010-03-16 15:40:10 -04002337 /* space_slots == 0 means they are asking for a count */
2338 if (space_args.space_slots == 0) {
2339 space_args.total_spaces = slot_count;
2340 goto out;
2341 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002342
Dan Rosenberg51788b12011-02-14 16:04:23 -05002343 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002344
Chris Mason7fde62b2010-03-16 15:40:10 -04002345 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002346
Chris Mason7fde62b2010-03-16 15:40:10 -04002347 /* we generally have at most 6 or so space infos, one for each raid
2348 * level. So, a whole page should be more than enough for everyone
2349 */
2350 if (alloc_size > PAGE_CACHE_SIZE)
2351 return -ENOMEM;
2352
2353 space_args.total_spaces = 0;
2354 dest = kmalloc(alloc_size, GFP_NOFS);
2355 if (!dest)
2356 return -ENOMEM;
2357 dest_orig = dest;
2358
2359 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002360 for (i = 0; i < num_types; i++) {
2361 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002362
Dan Rosenberg51788b12011-02-14 16:04:23 -05002363 if (!slot_count)
2364 break;
2365
Josef Bacikbf5fc092010-09-29 11:22:36 -04002366 info = NULL;
2367 rcu_read_lock();
2368 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2369 list) {
2370 if (tmp->flags == types[i]) {
2371 info = tmp;
2372 break;
2373 }
2374 }
2375 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002376
Josef Bacikbf5fc092010-09-29 11:22:36 -04002377 if (!info)
2378 continue;
2379 down_read(&info->groups_sem);
2380 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2381 if (!list_empty(&info->block_groups[c])) {
2382 get_block_group_info(&info->block_groups[c],
2383 &space);
2384 memcpy(dest, &space, sizeof(space));
2385 dest++;
2386 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002387 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002388 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002389 if (!slot_count)
2390 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002391 }
2392 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002393 }
Josef Bacik1406e432010-01-13 18:19:06 +00002394
Chris Mason7fde62b2010-03-16 15:40:10 -04002395 user_dest = (struct btrfs_ioctl_space_info *)
2396 (arg + sizeof(struct btrfs_ioctl_space_args));
2397
2398 if (copy_to_user(user_dest, dest_orig, alloc_size))
2399 ret = -EFAULT;
2400
2401 kfree(dest_orig);
2402out:
2403 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002404 ret = -EFAULT;
2405
2406 return ret;
2407}
2408
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002409/*
2410 * there are many ways the trans_start and trans_end ioctls can lead
2411 * to deadlocks. They should only be used by applications that
2412 * basically own the machine, and have a very in depth understanding
2413 * of all the possible deadlocks and enospc problems.
2414 */
2415long btrfs_ioctl_trans_end(struct file *file)
2416{
2417 struct inode *inode = fdentry(file)->d_inode;
2418 struct btrfs_root *root = BTRFS_I(inode)->root;
2419 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002420
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002421 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002422 if (!trans)
2423 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002424 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002425
Sage Weil1ab86ae2009-09-29 18:38:44 -04002426 btrfs_end_transaction(trans, root);
2427
Sage Weil9ca9ee02008-08-04 10:41:27 -04002428 mutex_lock(&root->fs_info->trans_mutex);
2429 root->fs_info->open_ioctl_trans--;
2430 mutex_unlock(&root->fs_info->trans_mutex);
2431
Sage Weilcfc8ea82008-12-11 16:30:06 -05002432 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002433 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002434}
2435
Sage Weil46204592010-10-29 15:41:32 -04002436static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2437{
2438 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2439 struct btrfs_trans_handle *trans;
2440 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002441 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002442
2443 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002444 if (IS_ERR(trans))
2445 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002446 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002447 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002448 if (ret) {
2449 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002450 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002451 }
Sage Weil46204592010-10-29 15:41:32 -04002452
2453 if (argp)
2454 if (copy_to_user(argp, &transid, sizeof(transid)))
2455 return -EFAULT;
2456 return 0;
2457}
2458
2459static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2460{
2461 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2462 u64 transid;
2463
2464 if (argp) {
2465 if (copy_from_user(&transid, argp, sizeof(transid)))
2466 return -EFAULT;
2467 } else {
2468 transid = 0; /* current trans */
2469 }
2470 return btrfs_wait_for_commit(root, transid);
2471}
2472
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002473long btrfs_ioctl(struct file *file, unsigned int
2474 cmd, unsigned long arg)
2475{
2476 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002477 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002478
2479 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002480 case FS_IOC_GETFLAGS:
2481 return btrfs_ioctl_getflags(file, argp);
2482 case FS_IOC_SETFLAGS:
2483 return btrfs_ioctl_setflags(file, argp);
2484 case FS_IOC_GETVERSION:
2485 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00002486 case FITRIM:
2487 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002488 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002489 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002490 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002491 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002492 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002493 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002494 case BTRFS_IOC_SNAP_DESTROY:
2495 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002496 case BTRFS_IOC_SUBVOL_GETFLAGS:
2497 return btrfs_ioctl_subvol_getflags(file, argp);
2498 case BTRFS_IOC_SUBVOL_SETFLAGS:
2499 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002500 case BTRFS_IOC_DEFAULT_SUBVOL:
2501 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002502 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002503 return btrfs_ioctl_defrag(file, NULL);
2504 case BTRFS_IOC_DEFRAG_RANGE:
2505 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002506 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002507 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002508 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002509 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002510 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002511 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002512 case BTRFS_IOC_BALANCE:
2513 return btrfs_balance(root->fs_info->dev_root);
2514 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002515 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2516 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002517 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002518 case BTRFS_IOC_TRANS_START:
2519 return btrfs_ioctl_trans_start(file);
2520 case BTRFS_IOC_TRANS_END:
2521 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002522 case BTRFS_IOC_TREE_SEARCH:
2523 return btrfs_ioctl_tree_search(file, argp);
2524 case BTRFS_IOC_INO_LOOKUP:
2525 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002526 case BTRFS_IOC_SPACE_INFO:
2527 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002528 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002529 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2530 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002531 case BTRFS_IOC_START_SYNC:
2532 return btrfs_ioctl_start_sync(file, argp);
2533 case BTRFS_IOC_WAIT_SYNC:
2534 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002535 }
2536
2537 return -ENOTTY;
2538}