blob: 649f47d2afb41302850904a9f3b19fd8633de4aa [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;
84
85 return iflags;
86}
87
88/*
89 * Update inode->i_flags based on the btrfs internal flags.
90 */
91void btrfs_update_iflags(struct inode *inode)
92{
93 struct btrfs_inode *ip = BTRFS_I(inode);
94
95 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96
97 if (ip->flags & BTRFS_INODE_SYNC)
98 inode->i_flags |= S_SYNC;
99 if (ip->flags & BTRFS_INODE_IMMUTABLE)
100 inode->i_flags |= S_IMMUTABLE;
101 if (ip->flags & BTRFS_INODE_APPEND)
102 inode->i_flags |= S_APPEND;
103 if (ip->flags & BTRFS_INODE_NOATIME)
104 inode->i_flags |= S_NOATIME;
105 if (ip->flags & BTRFS_INODE_DIRSYNC)
106 inode->i_flags |= S_DIRSYNC;
107}
108
109/*
110 * Inherit flags from the parent inode.
111 *
112 * Unlike extN we don't have any flags we don't want to inherit currently.
113 */
114void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400116 unsigned int flags;
117
118 if (!dir)
119 return;
120
121 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122
123 if (S_ISREG(inode->i_mode))
124 flags &= ~BTRFS_INODE_DIRSYNC;
125 else if (!S_ISDIR(inode->i_mode))
126 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127
128 BTRFS_I(inode)->flags = flags;
129 btrfs_update_iflags(inode);
130}
131
132static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133{
134 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
135 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136
137 if (copy_to_user(arg, &flags, sizeof(flags)))
138 return -EFAULT;
139 return 0;
140}
141
Liu Bo75e7cb72011-03-22 10:12:20 +0000142static int check_flags(unsigned int flags)
143{
144 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
145 FS_NOATIME_FL | FS_NODUMP_FL | \
146 FS_SYNC_FL | FS_DIRSYNC_FL | \
147 FS_NOCOMP_FL | FS_COMPR_FL | \
148 FS_NOCOW_FL | FS_COW_FL))
149 return -EOPNOTSUPP;
150
151 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
152 return -EINVAL;
153
154 if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL))
155 return -EINVAL;
156
157 return 0;
158}
159
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
161{
162 struct inode *inode = file->f_path.dentry->d_inode;
163 struct btrfs_inode *ip = BTRFS_I(inode);
164 struct btrfs_root *root = ip->root;
165 struct btrfs_trans_handle *trans;
166 unsigned int flags, oldflags;
167 int ret;
168
Li Zefanb83cc962010-12-20 16:04:08 +0800169 if (btrfs_root_readonly(root))
170 return -EROFS;
171
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200172 if (copy_from_user(&flags, arg, sizeof(flags)))
173 return -EFAULT;
174
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 ret = check_flags(flags);
176 if (ret)
177 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200178
179 if (!is_owner_or_cap(inode))
180 return -EACCES;
181
182 mutex_lock(&inode->i_mutex);
183
184 flags = btrfs_mask_flags(inode->i_mode, flags);
185 oldflags = btrfs_flags_to_ioctl(ip->flags);
186 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
187 if (!capable(CAP_LINUX_IMMUTABLE)) {
188 ret = -EPERM;
189 goto out_unlock;
190 }
191 }
192
193 ret = mnt_want_write(file->f_path.mnt);
194 if (ret)
195 goto out_unlock;
196
197 if (flags & FS_SYNC_FL)
198 ip->flags |= BTRFS_INODE_SYNC;
199 else
200 ip->flags &= ~BTRFS_INODE_SYNC;
201 if (flags & FS_IMMUTABLE_FL)
202 ip->flags |= BTRFS_INODE_IMMUTABLE;
203 else
204 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
205 if (flags & FS_APPEND_FL)
206 ip->flags |= BTRFS_INODE_APPEND;
207 else
208 ip->flags &= ~BTRFS_INODE_APPEND;
209 if (flags & FS_NODUMP_FL)
210 ip->flags |= BTRFS_INODE_NODUMP;
211 else
212 ip->flags &= ~BTRFS_INODE_NODUMP;
213 if (flags & FS_NOATIME_FL)
214 ip->flags |= BTRFS_INODE_NOATIME;
215 else
216 ip->flags &= ~BTRFS_INODE_NOATIME;
217 if (flags & FS_DIRSYNC_FL)
218 ip->flags |= BTRFS_INODE_DIRSYNC;
219 else
220 ip->flags &= ~BTRFS_INODE_DIRSYNC;
221
Liu Bo75e7cb72011-03-22 10:12:20 +0000222 /*
223 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
224 * flag may be changed automatically if compression code won't make
225 * things smaller.
226 */
227 if (flags & FS_NOCOMP_FL) {
228 ip->flags &= ~BTRFS_INODE_COMPRESS;
229 ip->flags |= BTRFS_INODE_NOCOMPRESS;
230 } else if (flags & FS_COMPR_FL) {
231 ip->flags |= BTRFS_INODE_COMPRESS;
232 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
233 }
234 if (flags & FS_NOCOW_FL)
235 ip->flags |= BTRFS_INODE_NODATACOW;
236 else if (flags & FS_COW_FL)
237 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238
239 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000240 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241
242 ret = btrfs_update_inode(trans, root, inode);
243 BUG_ON(ret);
244
245 btrfs_update_iflags(inode);
246 inode->i_ctime = CURRENT_TIME;
247 btrfs_end_transaction(trans, root);
248
249 mnt_drop_write(file->f_path.mnt);
250 out_unlock:
251 mutex_unlock(&inode->i_mutex);
252 return 0;
253}
254
255static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
256{
257 struct inode *inode = file->f_path.dentry->d_inode;
258
259 return put_user(inode->i_generation, arg);
260}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400261
Li Dongyangf7039b12011-03-24 10:24:28 +0000262static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
263{
264 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
265 struct btrfs_fs_info *fs_info = root->fs_info;
266 struct btrfs_device *device;
267 struct request_queue *q;
268 struct fstrim_range range;
269 u64 minlen = ULLONG_MAX;
270 u64 num_devices = 0;
271 int ret;
272
273 if (!capable(CAP_SYS_ADMIN))
274 return -EPERM;
275
276 mutex_lock(&fs_info->fs_devices->device_list_mutex);
277 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
278 if (!device->bdev)
279 continue;
280 q = bdev_get_queue(device->bdev);
281 if (blk_queue_discard(q)) {
282 num_devices++;
283 minlen = min((u64)q->limits.discard_granularity,
284 minlen);
285 }
286 }
287 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
288 if (!num_devices)
289 return -EOPNOTSUPP;
290
291 if (copy_from_user(&range, arg, sizeof(range)))
292 return -EFAULT;
293
294 range.minlen = max(range.minlen, minlen);
295 ret = btrfs_trim_fs(root, &range);
296 if (ret < 0)
297 return ret;
298
299 if (copy_to_user(arg, &range, sizeof(range)))
300 return -EFAULT;
301
302 return 0;
303}
304
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400305static noinline int create_subvol(struct btrfs_root *root,
306 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400307 char *name, int namelen,
308 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400309{
310 struct btrfs_trans_handle *trans;
311 struct btrfs_key key;
312 struct btrfs_root_item root_item;
313 struct btrfs_inode_item *inode_item;
314 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400315 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000316 struct dentry *parent = dget_parent(dentry);
317 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400318 int ret;
319 int err;
320 u64 objectid;
321 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500322 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400323
Yan, Zhenga22285a2010-05-16 10:48:46 -0400324 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
325 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000326 if (ret) {
327 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400328 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000329 }
330
331 dir = parent->d_inode;
332
Josef Bacik9ed74f22009-09-11 16:12:44 -0400333 /*
334 * 1 - inode item
335 * 2 - refs
336 * 1 - root item
337 * 2 - dir items
338 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400339 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000340 if (IS_ERR(trans)) {
341 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400342 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000343 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400344
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400345 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
346 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400347 if (IS_ERR(leaf)) {
348 ret = PTR_ERR(leaf);
349 goto fail;
350 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400351
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400352 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353 btrfs_set_header_bytenr(leaf, leaf->start);
354 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400355 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400356 btrfs_set_header_owner(leaf, objectid);
357
358 write_extent_buffer(leaf, root->fs_info->fsid,
359 (unsigned long)btrfs_header_fsid(leaf),
360 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
362 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
363 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400364 btrfs_mark_buffer_dirty(leaf);
365
366 inode_item = &root_item.inode;
367 memset(inode_item, 0, sizeof(*inode_item));
368 inode_item->generation = cpu_to_le64(1);
369 inode_item->size = cpu_to_le64(3);
370 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400371 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400372 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
373
374 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400375 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376 btrfs_set_root_level(&root_item, 0);
377 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000378 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400379 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380
381 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
382 root_item.drop_level = 0;
383
Chris Mason925baed2008-06-25 16:01:30 -0400384 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400385 free_extent_buffer(leaf);
386 leaf = NULL;
387
388 btrfs_set_root_dirid(&root_item, new_dirid);
389
390 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400391 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400392 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
393 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
394 &root_item);
395 if (ret)
396 goto fail;
397
Yan, Zheng76dda932009-09-21 16:00:26 -0400398 key.offset = (u64)-1;
399 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
400 BUG_ON(IS_ERR(new_root));
401
402 btrfs_record_root_in_trans(trans, new_root);
403
404 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
405 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400406 /*
407 * insert the directory item
408 */
Chris Mason3de45862008-11-17 21:02:50 -0500409 ret = btrfs_set_inode_index(dir, &index);
410 BUG_ON(ret);
411
412 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400413 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500414 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400415 if (ret)
416 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500417
Yan Zheng52c26172009-01-05 15:43:43 -0500418 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
419 ret = btrfs_update_inode(trans, root, dir);
420 BUG_ON(ret);
421
Chris Mason0660b5a2008-11-17 20:37:39 -0500422 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400423 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500424 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400425
Chris Mason0660b5a2008-11-17 20:37:39 -0500426 BUG_ON(ret);
427
Yan, Zheng76dda932009-09-21 16:00:26 -0400428 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400429fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000430 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400431 if (async_transid) {
432 *async_transid = trans->transid;
433 err = btrfs_commit_transaction_async(trans, root, 1);
434 } else {
435 err = btrfs_commit_transaction(trans, root);
436 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400437 if (err && !ret)
438 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400439 return ret;
440}
441
Sage Weil72fd0322010-10-29 15:41:32 -0400442static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800443 char *name, int namelen, u64 *async_transid,
444 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000446 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000447 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448 struct btrfs_pending_snapshot *pending_snapshot;
449 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000450 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451
452 if (!root->ref_cows)
453 return -EINVAL;
454
Yan, Zhenga22285a2010-05-16 10:48:46 -0400455 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
456 if (!pending_snapshot)
457 return -ENOMEM;
458
459 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
460 pending_snapshot->dentry = dentry;
461 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800462 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400463
464 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 goto fail;
468 }
469
470 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
471 BUG_ON(ret);
472
473 list_add(&pending_snapshot->list,
474 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400475 if (async_transid) {
476 *async_transid = trans->transid;
477 ret = btrfs_commit_transaction_async(trans,
478 root->fs_info->extent_root, 1);
479 } else {
480 ret = btrfs_commit_transaction(trans,
481 root->fs_info->extent_root);
482 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400483 BUG_ON(ret);
484
485 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400486 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000487 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400488
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500489 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
490 if (ret)
491 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400492
Josef Bacik6a912212010-11-20 09:48:00 +0000493 parent = dget_parent(dentry);
494 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
495 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000496 if (IS_ERR(inode)) {
497 ret = PTR_ERR(inode);
498 goto fail;
499 }
500 BUG_ON(!inode);
501 d_instantiate(dentry, inode);
502 ret = 0;
503fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400504 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400505 return ret;
506}
507
Sage Weil4260f7c2010-10-29 15:46:43 -0400508/* copy of check_sticky in fs/namei.c()
509* It's inline, so penalty for filesystems that don't use sticky bit is
510* minimal.
511*/
512static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
513{
514 uid_t fsuid = current_fsuid();
515
516 if (!(dir->i_mode & S_ISVTX))
517 return 0;
518 if (inode->i_uid == fsuid)
519 return 0;
520 if (dir->i_uid == fsuid)
521 return 0;
522 return !capable(CAP_FOWNER);
523}
524
525/* copy of may_delete in fs/namei.c()
526 * Check whether we can remove a link victim from directory dir, check
527 * whether the type of victim is right.
528 * 1. We can't do it if dir is read-only (done in permission())
529 * 2. We should have write and exec permissions on dir
530 * 3. We can't remove anything from append-only dir
531 * 4. We can't do anything with immutable dir (done in permission())
532 * 5. If the sticky bit on dir is set we should either
533 * a. be owner of dir, or
534 * b. be owner of victim, or
535 * c. have CAP_FOWNER capability
536 * 6. If the victim is append-only or immutable we can't do antyhing with
537 * links pointing to it.
538 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
539 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
540 * 9. We can't remove a root or mountpoint.
541 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
542 * nfs_async_unlink().
543 */
544
545static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
546{
547 int error;
548
549 if (!victim->d_inode)
550 return -ENOENT;
551
552 BUG_ON(victim->d_parent->d_inode != dir);
553 audit_inode_child(victim, dir);
554
555 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
556 if (error)
557 return error;
558 if (IS_APPEND(dir))
559 return -EPERM;
560 if (btrfs_check_sticky(dir, victim->d_inode)||
561 IS_APPEND(victim->d_inode)||
562 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
563 return -EPERM;
564 if (isdir) {
565 if (!S_ISDIR(victim->d_inode->i_mode))
566 return -ENOTDIR;
567 if (IS_ROOT(victim))
568 return -EBUSY;
569 } else if (S_ISDIR(victim->d_inode->i_mode))
570 return -EISDIR;
571 if (IS_DEADDIR(dir))
572 return -ENOENT;
573 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
574 return -EBUSY;
575 return 0;
576}
577
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400578/* copy of may_create in fs/namei.c() */
579static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
580{
581 if (child->d_inode)
582 return -EEXIST;
583 if (IS_DEADDIR(dir))
584 return -ENOENT;
585 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
586}
587
588/*
589 * Create a new subvolume below @parent. This is largely modeled after
590 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
591 * inside this filesystem so it's quite a bit simpler.
592 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400593static noinline int btrfs_mksubvol(struct path *parent,
594 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400595 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800596 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400597{
Yan, Zheng76dda932009-09-21 16:00:26 -0400598 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400599 struct dentry *dentry;
600 int error;
601
Yan, Zheng76dda932009-09-21 16:00:26 -0400602 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400603
604 dentry = lookup_one_len(name, parent->dentry, namelen);
605 error = PTR_ERR(dentry);
606 if (IS_ERR(dentry))
607 goto out_unlock;
608
609 error = -EEXIST;
610 if (dentry->d_inode)
611 goto out_dput;
612
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400613 error = mnt_want_write(parent->mnt);
614 if (error)
615 goto out_dput;
616
Yan, Zheng76dda932009-09-21 16:00:26 -0400617 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400618 if (error)
619 goto out_drop_write;
620
Yan, Zheng76dda932009-09-21 16:00:26 -0400621 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
622
623 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
624 goto out_up_read;
625
Chris Mason3de45862008-11-17 21:02:50 -0500626 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400627 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800628 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500629 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400630 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400631 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500632 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400633 if (!error)
634 fsnotify_mkdir(dir, dentry);
635out_up_read:
636 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400637out_drop_write:
638 mnt_drop_write(parent->mnt);
639out_dput:
640 dput(dentry);
641out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400642 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400643 return error;
644}
645
Chris Mason940100a2010-03-10 10:52:59 -0500646static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500647 int thresh, u64 *last_len, u64 *skip,
648 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500649{
650 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
651 struct extent_map *em = NULL;
652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
653 int ret = 1;
654
Chris Mason1e701a32010-03-11 09:42:04 -0500655
656 if (thresh == 0)
657 thresh = 256 * 1024;
658
Chris Mason940100a2010-03-10 10:52:59 -0500659 /*
660 * make sure that once we start defragging and extent, we keep on
661 * defragging it
662 */
663 if (start < *defrag_end)
664 return 1;
665
666 *skip = 0;
667
668 /*
669 * hopefully we have this extent in the tree already, try without
670 * the full extent lock
671 */
672 read_lock(&em_tree->lock);
673 em = lookup_extent_mapping(em_tree, start, len);
674 read_unlock(&em_tree->lock);
675
676 if (!em) {
677 /* get the big lock and read metadata off disk */
678 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
679 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
680 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
681
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000682 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500683 return 0;
684 }
685
686 /* this will cover holes, and inline extents */
687 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
688 ret = 0;
689
690 /*
691 * we hit a real extent, if it is big don't bother defragging it again
692 */
Chris Mason1e701a32010-03-11 09:42:04 -0500693 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500694 ret = 0;
695
696 /*
697 * last_len ends up being a counter of how many bytes we've defragged.
698 * every time we choose not to defrag an extent, we reset *last_len
699 * so that the next tiny extent will force a defrag.
700 *
701 * The end result of this is that tiny extents before a single big
702 * extent will force at least part of that big extent to be defragged.
703 */
704 if (ret) {
705 *last_len += len;
706 *defrag_end = extent_map_end(em);
707 } else {
708 *last_len = 0;
709 *skip = extent_map_end(em);
710 *defrag_end = 0;
711 }
712
713 free_extent_map(em);
714 return ret;
715}
716
Chris Mason1e701a32010-03-11 09:42:04 -0500717static int btrfs_defrag_file(struct file *file,
718 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400719{
720 struct inode *inode = fdentry(file)->d_inode;
721 struct btrfs_root *root = BTRFS_I(inode)->root;
722 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400723 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400724 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800725 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400726 unsigned long last_index;
727 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
728 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800729 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400730 u64 page_start;
731 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500732 u64 last_len = 0;
733 u64 skip = 0;
734 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400735 unsigned long i;
736 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800737 int compress_type = BTRFS_COMPRESS_ZLIB;
738
739 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
740 if (range->compress_type > BTRFS_COMPRESS_TYPES)
741 return -EINVAL;
742 if (range->compress_type)
743 compress_type = range->compress_type;
744 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400745
Chris Mason940100a2010-03-10 10:52:59 -0500746 if (inode->i_size == 0)
747 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748
Chris Mason1e701a32010-03-11 09:42:04 -0500749 if (range->start + range->len > range->start) {
750 last_index = min_t(u64, inode->i_size - 1,
751 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
752 } else {
753 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
754 }
755
756 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500757 while (i <= last_index) {
758 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500759 PAGE_CACHE_SIZE,
760 range->extent_thresh,
761 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500762 &defrag_end)) {
763 unsigned long next;
764 /*
765 * the should_defrag function tells us how much to skip
766 * bump our counter by the suggested amount
767 */
768 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
769 i = max(i + 1, next);
770 continue;
771 }
772
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400773 if (total_read % ra_pages == 0) {
774 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
775 min(last_index, i + ra_pages - 1));
776 }
777 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500778 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500779 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800780 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500781
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400782 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
783 if (ret)
784 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400785again:
Chris Mason940100a2010-03-10 10:52:59 -0500786 if (inode->i_size == 0 ||
787 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
788 ret = 0;
789 goto err_reservations;
790 }
791
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400792 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400793 if (!page) {
794 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500795 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400796 }
Chris Mason940100a2010-03-10 10:52:59 -0500797
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400798 if (!PageUptodate(page)) {
799 btrfs_readpage(NULL, page);
800 lock_page(page);
801 if (!PageUptodate(page)) {
802 unlock_page(page);
803 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400804 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500805 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400806 }
807 }
808
Chris Mason940100a2010-03-10 10:52:59 -0500809 if (page->mapping != inode->i_mapping) {
810 unlock_page(page);
811 page_cache_release(page);
812 goto again;
813 }
814
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400815 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400816
Chris Mason940100a2010-03-10 10:52:59 -0500817 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400818 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500819 goto loop_unlock;
820 }
821
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400822 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
823 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400824 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400825
826 ordered = btrfs_lookup_ordered_extent(inode, page_start);
827 if (ordered) {
828 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
829 unlock_page(page);
830 page_cache_release(page);
831 btrfs_start_ordered_extent(inode, ordered, 1);
832 btrfs_put_ordered_extent(ordered);
833 goto again;
834 }
835 set_page_extent_mapped(page);
836
Chris Masonf87f0572008-08-01 11:27:23 -0400837 /*
838 * this makes sure page_mkwrite is called on the
839 * page if it is dirtied again later
840 */
841 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500842 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
843 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
844 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400845
Josef Bacik2ac55d42010-02-03 19:33:23 +0000846 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500847 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400848 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400849 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500850
851loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400852 unlock_page(page);
853 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500854 mutex_unlock(&inode->i_mutex);
855
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400856 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500857 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 }
859
Chris Mason1e701a32010-03-11 09:42:04 -0500860 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
861 filemap_flush(inode->i_mapping);
862
863 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
864 /* the filemap_flush will queue IO into the worker threads, but
865 * we have to make sure the IO is actually started and that
866 * ordered extents get created before we return
867 */
868 atomic_inc(&root->fs_info->async_submit_draining);
869 while (atomic_read(&root->fs_info->nr_async_submits) ||
870 atomic_read(&root->fs_info->async_delalloc_pages)) {
871 wait_event(root->fs_info->async_submit_wait,
872 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
873 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
874 }
875 atomic_dec(&root->fs_info->async_submit_draining);
876
877 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800878 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500879 mutex_unlock(&inode->i_mutex);
880 }
881
Li Zefan1a419d82010-10-25 15:12:50 +0800882 disk_super = &root->fs_info->super_copy;
883 features = btrfs_super_incompat_flags(disk_super);
884 if (range->compress_type == BTRFS_COMPRESS_LZO) {
885 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
886 btrfs_set_super_incompat_flags(disk_super, features);
887 }
888
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400889 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500890
891err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400892 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
893err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500894 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500895 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400896}
897
Yan, Zheng76dda932009-09-21 16:00:26 -0400898static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
899 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400900{
901 u64 new_size;
902 u64 old_size;
903 u64 devid = 1;
904 struct btrfs_ioctl_vol_args *vol_args;
905 struct btrfs_trans_handle *trans;
906 struct btrfs_device *device = NULL;
907 char *sizestr;
908 char *devstr = NULL;
909 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400910 int mod = 0;
911
Yan Zhengc146afa2008-11-12 14:34:12 -0500912 if (root->fs_info->sb->s_flags & MS_RDONLY)
913 return -EROFS;
914
Chris Masone441d542009-01-05 16:57:23 -0500915 if (!capable(CAP_SYS_ADMIN))
916 return -EPERM;
917
Li Zefandae7b662009-04-08 15:06:54 +0800918 vol_args = memdup_user(arg, sizeof(*vol_args));
919 if (IS_ERR(vol_args))
920 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400921
922 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400923
Chris Mason7d9eb122008-07-08 14:19:17 -0400924 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400925 sizestr = vol_args->name;
926 devstr = strchr(sizestr, ':');
927 if (devstr) {
928 char *end;
929 sizestr = devstr + 1;
930 *devstr = '\0';
931 devstr = vol_args->name;
932 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700933 printk(KERN_INFO "resizing devid %llu\n",
934 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400935 }
Yan Zheng2b820322008-11-17 21:11:30 -0500936 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400937 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700938 printk(KERN_INFO "resizer unable to find device %llu\n",
939 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400940 ret = -EINVAL;
941 goto out_unlock;
942 }
943 if (!strcmp(sizestr, "max"))
944 new_size = device->bdev->bd_inode->i_size;
945 else {
946 if (sizestr[0] == '-') {
947 mod = -1;
948 sizestr++;
949 } else if (sizestr[0] == '+') {
950 mod = 1;
951 sizestr++;
952 }
Akinobu Mita91748462010-02-28 10:59:11 +0000953 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400954 if (new_size == 0) {
955 ret = -EINVAL;
956 goto out_unlock;
957 }
958 }
959
960 old_size = device->total_bytes;
961
962 if (mod < 0) {
963 if (new_size > old_size) {
964 ret = -EINVAL;
965 goto out_unlock;
966 }
967 new_size = old_size - new_size;
968 } else if (mod > 0) {
969 new_size = old_size + new_size;
970 }
971
972 if (new_size < 256 * 1024 * 1024) {
973 ret = -EINVAL;
974 goto out_unlock;
975 }
976 if (new_size > device->bdev->bd_inode->i_size) {
977 ret = -EFBIG;
978 goto out_unlock;
979 }
980
981 do_div(new_size, root->sectorsize);
982 new_size *= root->sectorsize;
983
984 printk(KERN_INFO "new size for %s is %llu\n",
985 device->name, (unsigned long long)new_size);
986
987 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400988 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +0000989 if (IS_ERR(trans)) {
990 ret = PTR_ERR(trans);
991 goto out_unlock;
992 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400993 ret = btrfs_grow_device(trans, device, new_size);
994 btrfs_commit_transaction(trans, root);
995 } else {
996 ret = btrfs_shrink_device(device, new_size);
997 }
998
999out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001000 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001001 kfree(vol_args);
1002 return ret;
1003}
1004
Sage Weil72fd0322010-10-29 15:41:32 -04001005static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1006 char *name,
1007 unsigned long fd,
1008 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001009 u64 *transid,
1010 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001011{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001012 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001013 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001014 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001015 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001016
Yan Zhengc146afa2008-11-12 14:34:12 -05001017 if (root->fs_info->sb->s_flags & MS_RDONLY)
1018 return -EROFS;
1019
Sage Weil72fd0322010-10-29 15:41:32 -04001020 namelen = strlen(name);
1021 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001022 ret = -EINVAL;
1023 goto out;
1024 }
1025
Chris Mason3de45862008-11-17 21:02:50 -05001026 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001027 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001028 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001029 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001030 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001031 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001032 if (!src_file) {
1033 ret = -EINVAL;
1034 goto out;
1035 }
1036
1037 src_inode = src_file->f_path.dentry->d_inode;
1038 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001039 printk(KERN_INFO "btrfs: Snapshot src from "
1040 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001041 ret = -EINVAL;
1042 fput(src_file);
1043 goto out;
1044 }
Sage Weil72fd0322010-10-29 15:41:32 -04001045 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1046 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001047 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001048 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001049 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001050out:
Sage Weil72fd0322010-10-29 15:41:32 -04001051 return ret;
1052}
1053
1054static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001055 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001056{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001057 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001058 int ret;
1059
Li Zefanfa0d2b92010-12-20 15:53:28 +08001060 vol_args = memdup_user(arg, sizeof(*vol_args));
1061 if (IS_ERR(vol_args))
1062 return PTR_ERR(vol_args);
1063 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001064
Li Zefanfa0d2b92010-12-20 15:53:28 +08001065 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001066 vol_args->fd, subvol,
1067 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001068
Li Zefanfa0d2b92010-12-20 15:53:28 +08001069 kfree(vol_args);
1070 return ret;
1071}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001072
Li Zefanfa0d2b92010-12-20 15:53:28 +08001073static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1074 void __user *arg, int subvol)
1075{
1076 struct btrfs_ioctl_vol_args_v2 *vol_args;
1077 int ret;
1078 u64 transid = 0;
1079 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001080 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001081
Li Zefanfa0d2b92010-12-20 15:53:28 +08001082 vol_args = memdup_user(arg, sizeof(*vol_args));
1083 if (IS_ERR(vol_args))
1084 return PTR_ERR(vol_args);
1085 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001086
Li Zefanb83cc962010-12-20 16:04:08 +08001087 if (vol_args->flags &
1088 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1089 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001090 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001091 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001092
1093 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1094 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001095 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1096 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001097
1098 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001099 vol_args->fd, subvol,
1100 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001101
1102 if (ret == 0 && ptr &&
1103 copy_to_user(arg +
1104 offsetof(struct btrfs_ioctl_vol_args_v2,
1105 transid), ptr, sizeof(*ptr)))
1106 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001107out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001108 kfree(vol_args);
1109 return ret;
1110}
1111
Li Zefan0caa1022010-12-20 16:30:25 +08001112static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1113 void __user *arg)
1114{
1115 struct inode *inode = fdentry(file)->d_inode;
1116 struct btrfs_root *root = BTRFS_I(inode)->root;
1117 int ret = 0;
1118 u64 flags = 0;
1119
1120 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1121 return -EINVAL;
1122
1123 down_read(&root->fs_info->subvol_sem);
1124 if (btrfs_root_readonly(root))
1125 flags |= BTRFS_SUBVOL_RDONLY;
1126 up_read(&root->fs_info->subvol_sem);
1127
1128 if (copy_to_user(arg, &flags, sizeof(flags)))
1129 ret = -EFAULT;
1130
1131 return ret;
1132}
1133
1134static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1135 void __user *arg)
1136{
1137 struct inode *inode = fdentry(file)->d_inode;
1138 struct btrfs_root *root = BTRFS_I(inode)->root;
1139 struct btrfs_trans_handle *trans;
1140 u64 root_flags;
1141 u64 flags;
1142 int ret = 0;
1143
1144 if (root->fs_info->sb->s_flags & MS_RDONLY)
1145 return -EROFS;
1146
1147 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1148 return -EINVAL;
1149
1150 if (copy_from_user(&flags, arg, sizeof(flags)))
1151 return -EFAULT;
1152
Li Zefanb4dc2b82011-02-16 06:06:34 +00001153 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001154 return -EINVAL;
1155
1156 if (flags & ~BTRFS_SUBVOL_RDONLY)
1157 return -EOPNOTSUPP;
1158
Li Zefanb4dc2b82011-02-16 06:06:34 +00001159 if (!is_owner_or_cap(inode))
1160 return -EACCES;
1161
Li Zefan0caa1022010-12-20 16:30:25 +08001162 down_write(&root->fs_info->subvol_sem);
1163
1164 /* nothing to do */
1165 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1166 goto out;
1167
1168 root_flags = btrfs_root_flags(&root->root_item);
1169 if (flags & BTRFS_SUBVOL_RDONLY)
1170 btrfs_set_root_flags(&root->root_item,
1171 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1172 else
1173 btrfs_set_root_flags(&root->root_item,
1174 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1175
1176 trans = btrfs_start_transaction(root, 1);
1177 if (IS_ERR(trans)) {
1178 ret = PTR_ERR(trans);
1179 goto out_reset;
1180 }
1181
Li Zefanb4dc2b82011-02-16 06:06:34 +00001182 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001183 &root->root_key, &root->root_item);
1184
1185 btrfs_commit_transaction(trans, root);
1186out_reset:
1187 if (ret)
1188 btrfs_set_root_flags(&root->root_item, root_flags);
1189out:
1190 up_write(&root->fs_info->subvol_sem);
1191 return ret;
1192}
1193
Yan, Zheng76dda932009-09-21 16:00:26 -04001194/*
1195 * helper to check if the subvolume references other subvolumes
1196 */
1197static noinline int may_destroy_subvol(struct btrfs_root *root)
1198{
1199 struct btrfs_path *path;
1200 struct btrfs_key key;
1201 int ret;
1202
1203 path = btrfs_alloc_path();
1204 if (!path)
1205 return -ENOMEM;
1206
1207 key.objectid = root->root_key.objectid;
1208 key.type = BTRFS_ROOT_REF_KEY;
1209 key.offset = (u64)-1;
1210
1211 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1212 &key, path, 0, 0);
1213 if (ret < 0)
1214 goto out;
1215 BUG_ON(ret == 0);
1216
1217 ret = 0;
1218 if (path->slots[0] > 0) {
1219 path->slots[0]--;
1220 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1221 if (key.objectid == root->root_key.objectid &&
1222 key.type == BTRFS_ROOT_REF_KEY)
1223 ret = -ENOTEMPTY;
1224 }
1225out:
1226 btrfs_free_path(path);
1227 return ret;
1228}
1229
Chris Masonac8e9812010-02-28 15:39:26 -05001230static noinline int key_in_sk(struct btrfs_key *key,
1231 struct btrfs_ioctl_search_key *sk)
1232{
Chris Masonabc6e132010-03-18 12:10:08 -04001233 struct btrfs_key test;
1234 int ret;
1235
1236 test.objectid = sk->min_objectid;
1237 test.type = sk->min_type;
1238 test.offset = sk->min_offset;
1239
1240 ret = btrfs_comp_cpu_keys(key, &test);
1241 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001242 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001243
1244 test.objectid = sk->max_objectid;
1245 test.type = sk->max_type;
1246 test.offset = sk->max_offset;
1247
1248 ret = btrfs_comp_cpu_keys(key, &test);
1249 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001250 return 0;
1251 return 1;
1252}
1253
1254static noinline int copy_to_sk(struct btrfs_root *root,
1255 struct btrfs_path *path,
1256 struct btrfs_key *key,
1257 struct btrfs_ioctl_search_key *sk,
1258 char *buf,
1259 unsigned long *sk_offset,
1260 int *num_found)
1261{
1262 u64 found_transid;
1263 struct extent_buffer *leaf;
1264 struct btrfs_ioctl_search_header sh;
1265 unsigned long item_off;
1266 unsigned long item_len;
1267 int nritems;
1268 int i;
1269 int slot;
1270 int found = 0;
1271 int ret = 0;
1272
1273 leaf = path->nodes[0];
1274 slot = path->slots[0];
1275 nritems = btrfs_header_nritems(leaf);
1276
1277 if (btrfs_header_generation(leaf) > sk->max_transid) {
1278 i = nritems;
1279 goto advance_key;
1280 }
1281 found_transid = btrfs_header_generation(leaf);
1282
1283 for (i = slot; i < nritems; i++) {
1284 item_off = btrfs_item_ptr_offset(leaf, i);
1285 item_len = btrfs_item_size_nr(leaf, i);
1286
1287 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1288 item_len = 0;
1289
1290 if (sizeof(sh) + item_len + *sk_offset >
1291 BTRFS_SEARCH_ARGS_BUFSIZE) {
1292 ret = 1;
1293 goto overflow;
1294 }
1295
1296 btrfs_item_key_to_cpu(leaf, key, i);
1297 if (!key_in_sk(key, sk))
1298 continue;
1299
1300 sh.objectid = key->objectid;
1301 sh.offset = key->offset;
1302 sh.type = key->type;
1303 sh.len = item_len;
1304 sh.transid = found_transid;
1305
1306 /* copy search result header */
1307 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1308 *sk_offset += sizeof(sh);
1309
1310 if (item_len) {
1311 char *p = buf + *sk_offset;
1312 /* copy the item */
1313 read_extent_buffer(leaf, p,
1314 item_off, item_len);
1315 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001316 }
Chris Mason90fdde12010-03-18 12:14:54 -04001317 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001318
1319 if (*num_found >= sk->nr_items)
1320 break;
1321 }
1322advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001323 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001324 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1325 key->offset++;
1326 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1327 key->offset = 0;
1328 key->type++;
1329 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1330 key->offset = 0;
1331 key->type = 0;
1332 key->objectid++;
1333 } else
1334 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001335overflow:
1336 *num_found += found;
1337 return ret;
1338}
1339
1340static noinline int search_ioctl(struct inode *inode,
1341 struct btrfs_ioctl_search_args *args)
1342{
1343 struct btrfs_root *root;
1344 struct btrfs_key key;
1345 struct btrfs_key max_key;
1346 struct btrfs_path *path;
1347 struct btrfs_ioctl_search_key *sk = &args->key;
1348 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1349 int ret;
1350 int num_found = 0;
1351 unsigned long sk_offset = 0;
1352
1353 path = btrfs_alloc_path();
1354 if (!path)
1355 return -ENOMEM;
1356
1357 if (sk->tree_id == 0) {
1358 /* search the root of the inode that was passed */
1359 root = BTRFS_I(inode)->root;
1360 } else {
1361 key.objectid = sk->tree_id;
1362 key.type = BTRFS_ROOT_ITEM_KEY;
1363 key.offset = (u64)-1;
1364 root = btrfs_read_fs_root_no_name(info, &key);
1365 if (IS_ERR(root)) {
1366 printk(KERN_ERR "could not find root %llu\n",
1367 sk->tree_id);
1368 btrfs_free_path(path);
1369 return -ENOENT;
1370 }
1371 }
1372
1373 key.objectid = sk->min_objectid;
1374 key.type = sk->min_type;
1375 key.offset = sk->min_offset;
1376
1377 max_key.objectid = sk->max_objectid;
1378 max_key.type = sk->max_type;
1379 max_key.offset = sk->max_offset;
1380
1381 path->keep_locks = 1;
1382
1383 while(1) {
1384 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1385 sk->min_transid);
1386 if (ret != 0) {
1387 if (ret > 0)
1388 ret = 0;
1389 goto err;
1390 }
1391 ret = copy_to_sk(root, path, &key, sk, args->buf,
1392 &sk_offset, &num_found);
1393 btrfs_release_path(root, path);
1394 if (ret || num_found >= sk->nr_items)
1395 break;
1396
1397 }
1398 ret = 0;
1399err:
1400 sk->nr_items = num_found;
1401 btrfs_free_path(path);
1402 return ret;
1403}
1404
1405static noinline int btrfs_ioctl_tree_search(struct file *file,
1406 void __user *argp)
1407{
1408 struct btrfs_ioctl_search_args *args;
1409 struct inode *inode;
1410 int ret;
1411
1412 if (!capable(CAP_SYS_ADMIN))
1413 return -EPERM;
1414
Julia Lawall2354d082010-10-29 15:14:18 -04001415 args = memdup_user(argp, sizeof(*args));
1416 if (IS_ERR(args))
1417 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001418
Chris Masonac8e9812010-02-28 15:39:26 -05001419 inode = fdentry(file)->d_inode;
1420 ret = search_ioctl(inode, args);
1421 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1422 ret = -EFAULT;
1423 kfree(args);
1424 return ret;
1425}
1426
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001427/*
Chris Masonac8e9812010-02-28 15:39:26 -05001428 * Search INODE_REFs to identify path name of 'dirid' directory
1429 * in a 'tree_id' tree. and sets path name to 'name'.
1430 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001431static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1432 u64 tree_id, u64 dirid, char *name)
1433{
1434 struct btrfs_root *root;
1435 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001436 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001437 int ret = -1;
1438 int slot;
1439 int len;
1440 int total_len = 0;
1441 struct btrfs_inode_ref *iref;
1442 struct extent_buffer *l;
1443 struct btrfs_path *path;
1444
1445 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1446 name[0]='\0';
1447 return 0;
1448 }
1449
1450 path = btrfs_alloc_path();
1451 if (!path)
1452 return -ENOMEM;
1453
Chris Masonac8e9812010-02-28 15:39:26 -05001454 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001455
1456 key.objectid = tree_id;
1457 key.type = BTRFS_ROOT_ITEM_KEY;
1458 key.offset = (u64)-1;
1459 root = btrfs_read_fs_root_no_name(info, &key);
1460 if (IS_ERR(root)) {
1461 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001462 ret = -ENOENT;
1463 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001464 }
1465
1466 key.objectid = dirid;
1467 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001468 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001469
1470 while(1) {
1471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1472 if (ret < 0)
1473 goto out;
1474
1475 l = path->nodes[0];
1476 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001477 if (ret > 0 && slot > 0)
1478 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001479 btrfs_item_key_to_cpu(l, &key, slot);
1480
1481 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001482 key.type != BTRFS_INODE_REF_KEY)) {
1483 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001484 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001485 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001486
1487 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1488 len = btrfs_inode_ref_name_len(l, iref);
1489 ptr -= len + 1;
1490 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001491 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001492 goto out;
1493
1494 *(ptr + len) = '/';
1495 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1496
1497 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1498 break;
1499
1500 btrfs_release_path(root, path);
1501 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001502 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001503 dirid = key.objectid;
1504
1505 }
Chris Masonac8e9812010-02-28 15:39:26 -05001506 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001507 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001508 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001509 name[total_len]='\0';
1510 ret = 0;
1511out:
1512 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001513 return ret;
1514}
1515
1516static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1517 void __user *argp)
1518{
1519 struct btrfs_ioctl_ino_lookup_args *args;
1520 struct inode *inode;
1521 int ret;
1522
1523 if (!capable(CAP_SYS_ADMIN))
1524 return -EPERM;
1525
Julia Lawall2354d082010-10-29 15:14:18 -04001526 args = memdup_user(argp, sizeof(*args));
1527 if (IS_ERR(args))
1528 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001529
Chris Masonac8e9812010-02-28 15:39:26 -05001530 inode = fdentry(file)->d_inode;
1531
Chris Mason1b53ac42010-03-18 12:17:05 -04001532 if (args->treeid == 0)
1533 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1534
Chris Masonac8e9812010-02-28 15:39:26 -05001535 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1536 args->treeid, args->objectid,
1537 args->name);
1538
1539 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1540 ret = -EFAULT;
1541
1542 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001543 return ret;
1544}
1545
Yan, Zheng76dda932009-09-21 16:00:26 -04001546static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1547 void __user *arg)
1548{
1549 struct dentry *parent = fdentry(file);
1550 struct dentry *dentry;
1551 struct inode *dir = parent->d_inode;
1552 struct inode *inode;
1553 struct btrfs_root *root = BTRFS_I(dir)->root;
1554 struct btrfs_root *dest = NULL;
1555 struct btrfs_ioctl_vol_args *vol_args;
1556 struct btrfs_trans_handle *trans;
1557 int namelen;
1558 int ret;
1559 int err = 0;
1560
Yan, Zheng76dda932009-09-21 16:00:26 -04001561 vol_args = memdup_user(arg, sizeof(*vol_args));
1562 if (IS_ERR(vol_args))
1563 return PTR_ERR(vol_args);
1564
1565 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1566 namelen = strlen(vol_args->name);
1567 if (strchr(vol_args->name, '/') ||
1568 strncmp(vol_args->name, "..", namelen) == 0) {
1569 err = -EINVAL;
1570 goto out;
1571 }
1572
1573 err = mnt_want_write(file->f_path.mnt);
1574 if (err)
1575 goto out;
1576
1577 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1578 dentry = lookup_one_len(vol_args->name, parent, namelen);
1579 if (IS_ERR(dentry)) {
1580 err = PTR_ERR(dentry);
1581 goto out_unlock_dir;
1582 }
1583
1584 if (!dentry->d_inode) {
1585 err = -ENOENT;
1586 goto out_dput;
1587 }
1588
1589 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001590 dest = BTRFS_I(inode)->root;
1591 if (!capable(CAP_SYS_ADMIN)){
1592 /*
1593 * Regular user. Only allow this with a special mount
1594 * option, when the user has write+exec access to the
1595 * subvol root, and when rmdir(2) would have been
1596 * allowed.
1597 *
1598 * Note that this is _not_ check that the subvol is
1599 * empty or doesn't contain data that we wouldn't
1600 * otherwise be able to delete.
1601 *
1602 * Users who want to delete empty subvols should try
1603 * rmdir(2).
1604 */
1605 err = -EPERM;
1606 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1607 goto out_dput;
1608
1609 /*
1610 * Do not allow deletion if the parent dir is the same
1611 * as the dir to be deleted. That means the ioctl
1612 * must be called on the dentry referencing the root
1613 * of the subvol, not a random directory contained
1614 * within it.
1615 */
1616 err = -EINVAL;
1617 if (root == dest)
1618 goto out_dput;
1619
1620 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1621 if (err)
1622 goto out_dput;
1623
1624 /* check if subvolume may be deleted by a non-root user */
1625 err = btrfs_may_delete(dir, dentry, 1);
1626 if (err)
1627 goto out_dput;
1628 }
1629
Yan, Zheng76dda932009-09-21 16:00:26 -04001630 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1631 err = -EINVAL;
1632 goto out_dput;
1633 }
1634
Yan, Zheng76dda932009-09-21 16:00:26 -04001635 mutex_lock(&inode->i_mutex);
1636 err = d_invalidate(dentry);
1637 if (err)
1638 goto out_unlock;
1639
1640 down_write(&root->fs_info->subvol_sem);
1641
1642 err = may_destroy_subvol(dest);
1643 if (err)
1644 goto out_up_write;
1645
Yan, Zhenga22285a2010-05-16 10:48:46 -04001646 trans = btrfs_start_transaction(root, 0);
1647 if (IS_ERR(trans)) {
1648 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001649 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001650 }
1651 trans->block_rsv = &root->fs_info->global_block_rsv;
1652
Yan, Zheng76dda932009-09-21 16:00:26 -04001653 ret = btrfs_unlink_subvol(trans, root, dir,
1654 dest->root_key.objectid,
1655 dentry->d_name.name,
1656 dentry->d_name.len);
1657 BUG_ON(ret);
1658
1659 btrfs_record_root_in_trans(trans, dest);
1660
1661 memset(&dest->root_item.drop_progress, 0,
1662 sizeof(dest->root_item.drop_progress));
1663 dest->root_item.drop_level = 0;
1664 btrfs_set_root_refs(&dest->root_item, 0);
1665
Yan, Zhengd68fc572010-05-16 10:49:58 -04001666 if (!xchg(&dest->orphan_item_inserted, 1)) {
1667 ret = btrfs_insert_orphan_item(trans,
1668 root->fs_info->tree_root,
1669 dest->root_key.objectid);
1670 BUG_ON(ret);
1671 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001672
Sage Weil531cb132010-10-29 15:41:32 -04001673 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001674 BUG_ON(ret);
1675 inode->i_flags |= S_DEAD;
1676out_up_write:
1677 up_write(&root->fs_info->subvol_sem);
1678out_unlock:
1679 mutex_unlock(&inode->i_mutex);
1680 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001681 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001682 btrfs_invalidate_inodes(dest);
1683 d_delete(dentry);
1684 }
1685out_dput:
1686 dput(dentry);
1687out_unlock_dir:
1688 mutex_unlock(&dir->i_mutex);
1689 mnt_drop_write(file->f_path.mnt);
1690out:
1691 kfree(vol_args);
1692 return err;
1693}
1694
Chris Mason1e701a32010-03-11 09:42:04 -05001695static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696{
1697 struct inode *inode = fdentry(file)->d_inode;
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001699 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001700 int ret;
1701
Li Zefanb83cc962010-12-20 16:04:08 +08001702 if (btrfs_root_readonly(root))
1703 return -EROFS;
1704
Yan Zhengc146afa2008-11-12 14:34:12 -05001705 ret = mnt_want_write(file->f_path.mnt);
1706 if (ret)
1707 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001708
1709 switch (inode->i_mode & S_IFMT) {
1710 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001711 if (!capable(CAP_SYS_ADMIN)) {
1712 ret = -EPERM;
1713 goto out;
1714 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001715 ret = btrfs_defrag_root(root, 0);
1716 if (ret)
1717 goto out;
1718 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001719 break;
1720 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001721 if (!(file->f_mode & FMODE_WRITE)) {
1722 ret = -EINVAL;
1723 goto out;
1724 }
Chris Mason1e701a32010-03-11 09:42:04 -05001725
1726 range = kzalloc(sizeof(*range), GFP_KERNEL);
1727 if (!range) {
1728 ret = -ENOMEM;
1729 goto out;
1730 }
1731
1732 if (argp) {
1733 if (copy_from_user(range, argp,
1734 sizeof(*range))) {
1735 ret = -EFAULT;
1736 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001737 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001738 }
1739 /* compression requires us to start the IO */
1740 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1741 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1742 range->extent_thresh = (u32)-1;
1743 }
1744 } else {
1745 /* the rest are all set to zero by kzalloc */
1746 range->len = (u64)-1;
1747 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001748 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001749 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001750 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001751 default:
1752 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001753 }
Chris Masone441d542009-01-05 16:57:23 -05001754out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001755 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001756 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001757}
1758
Christoph Hellwigb2950862008-12-02 09:54:17 -05001759static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001760{
1761 struct btrfs_ioctl_vol_args *vol_args;
1762 int ret;
1763
Chris Masone441d542009-01-05 16:57:23 -05001764 if (!capable(CAP_SYS_ADMIN))
1765 return -EPERM;
1766
Li Zefandae7b662009-04-08 15:06:54 +08001767 vol_args = memdup_user(arg, sizeof(*vol_args));
1768 if (IS_ERR(vol_args))
1769 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001770
Mark Fasheh5516e592008-07-24 12:20:14 -04001771 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001772 ret = btrfs_init_new_device(root, vol_args->name);
1773
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001774 kfree(vol_args);
1775 return ret;
1776}
1777
Christoph Hellwigb2950862008-12-02 09:54:17 -05001778static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001779{
1780 struct btrfs_ioctl_vol_args *vol_args;
1781 int ret;
1782
Chris Masone441d542009-01-05 16:57:23 -05001783 if (!capable(CAP_SYS_ADMIN))
1784 return -EPERM;
1785
Yan Zhengc146afa2008-11-12 14:34:12 -05001786 if (root->fs_info->sb->s_flags & MS_RDONLY)
1787 return -EROFS;
1788
Li Zefandae7b662009-04-08 15:06:54 +08001789 vol_args = memdup_user(arg, sizeof(*vol_args));
1790 if (IS_ERR(vol_args))
1791 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001792
Mark Fasheh5516e592008-07-24 12:20:14 -04001793 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001794 ret = btrfs_rm_device(root, vol_args->name);
1795
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001796 kfree(vol_args);
1797 return ret;
1798}
1799
Yan, Zheng76dda932009-09-21 16:00:26 -04001800static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1801 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001802{
1803 struct inode *inode = fdentry(file)->d_inode;
1804 struct btrfs_root *root = BTRFS_I(inode)->root;
1805 struct file *src_file;
1806 struct inode *src;
1807 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001808 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001809 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001810 char *buf;
1811 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001812 u32 nritems;
1813 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001814 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001815 u64 len = olen;
1816 u64 bs = root->fs_info->sb->s_blocksize;
1817 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001818
Sage Weilc5c9cd42008-11-12 14:32:25 -05001819 /*
1820 * TODO:
1821 * - split compressed inline extents. annoying: we need to
1822 * decompress into destination's address_space (the file offset
1823 * may change, so source mapping won't do), then recompress (or
1824 * otherwise reinsert) a subrange.
1825 * - allow ranges within the same file to be cloned (provided
1826 * they don't overlap)?
1827 */
1828
Chris Masone441d542009-01-05 16:57:23 -05001829 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001830 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001831 return -EINVAL;
1832
Li Zefanb83cc962010-12-20 16:04:08 +08001833 if (btrfs_root_readonly(root))
1834 return -EROFS;
1835
Yan Zhengc146afa2008-11-12 14:34:12 -05001836 ret = mnt_want_write(file->f_path.mnt);
1837 if (ret)
1838 return ret;
1839
Sage Weilc5c9cd42008-11-12 14:32:25 -05001840 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001841 if (!src_file) {
1842 ret = -EBADF;
1843 goto out_drop_write;
1844 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001845
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001846 src = src_file->f_dentry->d_inode;
1847
Sage Weilc5c9cd42008-11-12 14:32:25 -05001848 ret = -EINVAL;
1849 if (src == inode)
1850 goto out_fput;
1851
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001852 /* the src must be open for reading */
1853 if (!(src_file->f_mode & FMODE_READ))
1854 goto out_fput;
1855
Yan Zhengae01a0a2008-08-04 23:23:47 -04001856 ret = -EISDIR;
1857 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001858 goto out_fput;
1859
Yan Zhengae01a0a2008-08-04 23:23:47 -04001860 ret = -EXDEV;
1861 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1862 goto out_fput;
1863
1864 ret = -ENOMEM;
1865 buf = vmalloc(btrfs_level_size(root, 0));
1866 if (!buf)
1867 goto out_fput;
1868
1869 path = btrfs_alloc_path();
1870 if (!path) {
1871 vfree(buf);
1872 goto out_fput;
1873 }
1874 path->reada = 2;
1875
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001876 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001877 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1878 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001879 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001880 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1881 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001882 }
1883
Sage Weilc5c9cd42008-11-12 14:32:25 -05001884 /* determine range to clone */
1885 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001886 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001887 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001888 if (len == 0)
1889 olen = len = src->i_size - off;
1890 /* if we extend to eof, continue to block boundary */
1891 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001892 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001893
1894 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001895 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1896 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001897 goto out_unlock;
1898
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001899 /* do any pending delalloc/csum calc on src, one way or
1900 another, and lock file content */
1901 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001902 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001903 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001904 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1905 if (!ordered &&
1906 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1907 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001908 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001909 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001910 if (ordered)
1911 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001912 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001913 }
1914
Sage Weilc5c9cd42008-11-12 14:32:25 -05001915 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001916 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001917 key.type = BTRFS_EXTENT_DATA_KEY;
1918 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001919
1920 while (1) {
1921 /*
1922 * note the key will change type as we walk through the
1923 * tree.
1924 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001926 if (ret < 0)
1927 goto out;
1928
Yan Zhengae01a0a2008-08-04 23:23:47 -04001929 nritems = btrfs_header_nritems(path->nodes[0]);
1930 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001931 ret = btrfs_next_leaf(root, path);
1932 if (ret < 0)
1933 goto out;
1934 if (ret > 0)
1935 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001936 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001937 }
1938 leaf = path->nodes[0];
1939 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001940
Yan Zhengae01a0a2008-08-04 23:23:47 -04001941 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001942 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001943 key.objectid != src->i_ino)
1944 break;
1945
Sage Weilc5c9cd42008-11-12 14:32:25 -05001946 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1947 struct btrfs_file_extent_item *extent;
1948 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001949 u32 size;
1950 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001951 u64 disko = 0, diskl = 0;
1952 u64 datao = 0, datal = 0;
1953 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001954 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001955
1956 size = btrfs_item_size_nr(leaf, slot);
1957 read_extent_buffer(leaf, buf,
1958 btrfs_item_ptr_offset(leaf, slot),
1959 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001960
1961 extent = btrfs_item_ptr(leaf, slot,
1962 struct btrfs_file_extent_item);
1963 comp = btrfs_file_extent_compression(leaf, extent);
1964 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001965 if (type == BTRFS_FILE_EXTENT_REG ||
1966 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001967 disko = btrfs_file_extent_disk_bytenr(leaf,
1968 extent);
1969 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1970 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001971 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001972 datal = btrfs_file_extent_num_bytes(leaf,
1973 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001974 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1975 /* take upper bound, may be compressed */
1976 datal = btrfs_file_extent_ram_bytes(leaf,
1977 extent);
1978 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001979 btrfs_release_path(root, path);
1980
Sage Weil050006a2010-10-29 15:37:33 -04001981 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001982 key.offset >= off+len)
1983 goto next;
1984
Zheng Yan31840ae2008-09-23 13:14:14 -04001985 memcpy(&new_key, &key, sizeof(new_key));
1986 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001987 if (off <= key.offset)
1988 new_key.offset = key.offset + destoff - off;
1989 else
1990 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001991
Yan, Zhenga22285a2010-05-16 10:48:46 -04001992 trans = btrfs_start_transaction(root, 1);
1993 if (IS_ERR(trans)) {
1994 ret = PTR_ERR(trans);
1995 goto out;
1996 }
1997
Chris Masonc8a894d2009-06-27 21:07:03 -04001998 if (type == BTRFS_FILE_EXTENT_REG ||
1999 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002000 if (off > key.offset) {
2001 datao += off - key.offset;
2002 datal -= off - key.offset;
2003 }
2004
2005 if (key.offset + datal > off + len)
2006 datal = off + len - key.offset;
2007
2008 ret = btrfs_drop_extents(trans, inode,
2009 new_key.offset,
2010 new_key.offset + datal,
2011 &hint_byte, 1);
2012 BUG_ON(ret);
2013
Sage Weilc5c9cd42008-11-12 14:32:25 -05002014 ret = btrfs_insert_empty_item(trans, root, path,
2015 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002016 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002017
2018 leaf = path->nodes[0];
2019 slot = path->slots[0];
2020 write_extent_buffer(leaf, buf,
2021 btrfs_item_ptr_offset(leaf, slot),
2022 size);
2023
2024 extent = btrfs_item_ptr(leaf, slot,
2025 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002026
Sage Weilc5c9cd42008-11-12 14:32:25 -05002027 /* disko == 0 means it's a hole */
2028 if (!disko)
2029 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002030
2031 btrfs_set_file_extent_offset(leaf, extent,
2032 datao);
2033 btrfs_set_file_extent_num_bytes(leaf, extent,
2034 datal);
2035 if (disko) {
2036 inode_add_bytes(inode, datal);
2037 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038 disko, diskl, 0,
2039 root->root_key.objectid,
2040 inode->i_ino,
2041 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002042 BUG_ON(ret);
2043 }
2044 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2045 u64 skip = 0;
2046 u64 trim = 0;
2047 if (off > key.offset) {
2048 skip = off - key.offset;
2049 new_key.offset += skip;
2050 }
Chris Masond3977122009-01-05 21:25:51 -05002051
Sage Weilc5c9cd42008-11-12 14:32:25 -05002052 if (key.offset + datal > off+len)
2053 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002054
Sage Weilc5c9cd42008-11-12 14:32:25 -05002055 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002056 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002057 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002058 goto out;
2059 }
2060 size -= skip + trim;
2061 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002062
2063 ret = btrfs_drop_extents(trans, inode,
2064 new_key.offset,
2065 new_key.offset + datal,
2066 &hint_byte, 1);
2067 BUG_ON(ret);
2068
Sage Weilc5c9cd42008-11-12 14:32:25 -05002069 ret = btrfs_insert_empty_item(trans, root, path,
2070 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002071 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002072
2073 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002074 u32 start =
2075 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002076 memmove(buf+start, buf+start+skip,
2077 datal);
2078 }
2079
2080 leaf = path->nodes[0];
2081 slot = path->slots[0];
2082 write_extent_buffer(leaf, buf,
2083 btrfs_item_ptr_offset(leaf, slot),
2084 size);
2085 inode_add_bytes(inode, datal);
2086 }
2087
2088 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002089 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002090
Yan, Zhenga22285a2010-05-16 10:48:46 -04002091 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002092
2093 /*
2094 * we round up to the block size at eof when
2095 * determining which extents to clone above,
2096 * but shouldn't round up the file size
2097 */
2098 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002099 if (endoff > destoff+olen)
2100 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002101 if (endoff > inode->i_size)
2102 btrfs_i_size_write(inode, endoff);
2103
Yan, Zhenga22285a2010-05-16 10:48:46 -04002104 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2105 ret = btrfs_update_inode(trans, root, inode);
2106 BUG_ON(ret);
2107 btrfs_end_transaction(trans, root);
2108 }
Chris Masond3977122009-01-05 21:25:51 -05002109next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002110 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002111 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002112 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002113 ret = 0;
2114out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002115 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002116 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002117out_unlock:
2118 mutex_unlock(&src->i_mutex);
2119 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002120 vfree(buf);
2121 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002122out_fput:
2123 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002124out_drop_write:
2125 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002126 return ret;
2127}
2128
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002129static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002130{
2131 struct btrfs_ioctl_clone_range_args args;
2132
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002133 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002134 return -EFAULT;
2135 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2136 args.src_length, args.dest_offset);
2137}
2138
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002139/*
2140 * there are many ways the trans_start and trans_end ioctls can lead
2141 * to deadlocks. They should only be used by applications that
2142 * basically own the machine, and have a very in depth understanding
2143 * of all the possible deadlocks and enospc problems.
2144 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002145static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002146{
2147 struct inode *inode = fdentry(file)->d_inode;
2148 struct btrfs_root *root = BTRFS_I(inode)->root;
2149 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002150 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002151
Sage Weil1ab86ae2009-09-29 18:38:44 -04002152 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002153 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002154 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002155
2156 ret = -EINPROGRESS;
2157 if (file->private_data)
2158 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002159
Li Zefanb83cc962010-12-20 16:04:08 +08002160 ret = -EROFS;
2161 if (btrfs_root_readonly(root))
2162 goto out;
2163
Yan Zhengc146afa2008-11-12 14:34:12 -05002164 ret = mnt_want_write(file->f_path.mnt);
2165 if (ret)
2166 goto out;
2167
Sage Weil9ca9ee02008-08-04 10:41:27 -04002168 mutex_lock(&root->fs_info->trans_mutex);
2169 root->fs_info->open_ioctl_trans++;
2170 mutex_unlock(&root->fs_info->trans_mutex);
2171
Sage Weil1ab86ae2009-09-29 18:38:44 -04002172 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002173 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002174 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002175 goto out_drop;
2176
2177 file->private_data = trans;
2178 return 0;
2179
2180out_drop:
2181 mutex_lock(&root->fs_info->trans_mutex);
2182 root->fs_info->open_ioctl_trans--;
2183 mutex_unlock(&root->fs_info->trans_mutex);
2184 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002185out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002186 return ret;
2187}
2188
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002189static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2190{
2191 struct inode *inode = fdentry(file)->d_inode;
2192 struct btrfs_root *root = BTRFS_I(inode)->root;
2193 struct btrfs_root *new_root;
2194 struct btrfs_dir_item *di;
2195 struct btrfs_trans_handle *trans;
2196 struct btrfs_path *path;
2197 struct btrfs_key location;
2198 struct btrfs_disk_key disk_key;
2199 struct btrfs_super_block *disk_super;
2200 u64 features;
2201 u64 objectid = 0;
2202 u64 dir_id;
2203
2204 if (!capable(CAP_SYS_ADMIN))
2205 return -EPERM;
2206
2207 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2208 return -EFAULT;
2209
2210 if (!objectid)
2211 objectid = root->root_key.objectid;
2212
2213 location.objectid = objectid;
2214 location.type = BTRFS_ROOT_ITEM_KEY;
2215 location.offset = (u64)-1;
2216
2217 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2218 if (IS_ERR(new_root))
2219 return PTR_ERR(new_root);
2220
2221 if (btrfs_root_refs(&new_root->root_item) == 0)
2222 return -ENOENT;
2223
2224 path = btrfs_alloc_path();
2225 if (!path)
2226 return -ENOMEM;
2227 path->leave_spinning = 1;
2228
2229 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002230 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002231 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002232 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002233 }
2234
2235 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2236 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2237 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002238 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002239 btrfs_free_path(path);
2240 btrfs_end_transaction(trans, root);
2241 printk(KERN_ERR "Umm, you don't have the default dir item, "
2242 "this isn't going to work\n");
2243 return -ENOENT;
2244 }
2245
2246 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2247 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2248 btrfs_mark_buffer_dirty(path->nodes[0]);
2249 btrfs_free_path(path);
2250
2251 disk_super = &root->fs_info->super_copy;
2252 features = btrfs_super_incompat_flags(disk_super);
2253 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2254 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2255 btrfs_set_super_incompat_flags(disk_super, features);
2256 }
2257 btrfs_end_transaction(trans, root);
2258
2259 return 0;
2260}
2261
Josef Bacikbf5fc092010-09-29 11:22:36 -04002262static void get_block_group_info(struct list_head *groups_list,
2263 struct btrfs_ioctl_space_info *space)
2264{
2265 struct btrfs_block_group_cache *block_group;
2266
2267 space->total_bytes = 0;
2268 space->used_bytes = 0;
2269 space->flags = 0;
2270 list_for_each_entry(block_group, groups_list, list) {
2271 space->flags = block_group->flags;
2272 space->total_bytes += block_group->key.offset;
2273 space->used_bytes +=
2274 btrfs_block_group_used(&block_group->item);
2275 }
2276}
2277
Josef Bacik1406e432010-01-13 18:19:06 +00002278long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2279{
2280 struct btrfs_ioctl_space_args space_args;
2281 struct btrfs_ioctl_space_info space;
2282 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002283 struct btrfs_ioctl_space_info *dest_orig;
2284 struct btrfs_ioctl_space_info *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002285 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002286 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2287 BTRFS_BLOCK_GROUP_SYSTEM,
2288 BTRFS_BLOCK_GROUP_METADATA,
2289 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2290 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002291 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002292 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002293 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002294 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002295
2296 if (copy_from_user(&space_args,
2297 (struct btrfs_ioctl_space_args __user *)arg,
2298 sizeof(space_args)))
2299 return -EFAULT;
2300
Josef Bacikbf5fc092010-09-29 11:22:36 -04002301 for (i = 0; i < num_types; i++) {
2302 struct btrfs_space_info *tmp;
2303
2304 info = NULL;
2305 rcu_read_lock();
2306 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2307 list) {
2308 if (tmp->flags == types[i]) {
2309 info = tmp;
2310 break;
2311 }
2312 }
2313 rcu_read_unlock();
2314
2315 if (!info)
2316 continue;
2317
2318 down_read(&info->groups_sem);
2319 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2320 if (!list_empty(&info->block_groups[c]))
2321 slot_count++;
2322 }
2323 up_read(&info->groups_sem);
2324 }
Josef Bacik1406e432010-01-13 18:19:06 +00002325
Chris Mason7fde62b2010-03-16 15:40:10 -04002326 /* space_slots == 0 means they are asking for a count */
2327 if (space_args.space_slots == 0) {
2328 space_args.total_spaces = slot_count;
2329 goto out;
2330 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002331
Dan Rosenberg51788b12011-02-14 16:04:23 -05002332 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002333
Chris Mason7fde62b2010-03-16 15:40:10 -04002334 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002335
Chris Mason7fde62b2010-03-16 15:40:10 -04002336 /* we generally have at most 6 or so space infos, one for each raid
2337 * level. So, a whole page should be more than enough for everyone
2338 */
2339 if (alloc_size > PAGE_CACHE_SIZE)
2340 return -ENOMEM;
2341
2342 space_args.total_spaces = 0;
2343 dest = kmalloc(alloc_size, GFP_NOFS);
2344 if (!dest)
2345 return -ENOMEM;
2346 dest_orig = dest;
2347
2348 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002349 for (i = 0; i < num_types; i++) {
2350 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002351
Dan Rosenberg51788b12011-02-14 16:04:23 -05002352 if (!slot_count)
2353 break;
2354
Josef Bacikbf5fc092010-09-29 11:22:36 -04002355 info = NULL;
2356 rcu_read_lock();
2357 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2358 list) {
2359 if (tmp->flags == types[i]) {
2360 info = tmp;
2361 break;
2362 }
2363 }
2364 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002365
Josef Bacikbf5fc092010-09-29 11:22:36 -04002366 if (!info)
2367 continue;
2368 down_read(&info->groups_sem);
2369 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2370 if (!list_empty(&info->block_groups[c])) {
2371 get_block_group_info(&info->block_groups[c],
2372 &space);
2373 memcpy(dest, &space, sizeof(space));
2374 dest++;
2375 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002376 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002377 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002378 if (!slot_count)
2379 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002380 }
2381 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002382 }
Josef Bacik1406e432010-01-13 18:19:06 +00002383
Chris Mason7fde62b2010-03-16 15:40:10 -04002384 user_dest = (struct btrfs_ioctl_space_info *)
2385 (arg + sizeof(struct btrfs_ioctl_space_args));
2386
2387 if (copy_to_user(user_dest, dest_orig, alloc_size))
2388 ret = -EFAULT;
2389
2390 kfree(dest_orig);
2391out:
2392 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002393 ret = -EFAULT;
2394
2395 return ret;
2396}
2397
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002398/*
2399 * there are many ways the trans_start and trans_end ioctls can lead
2400 * to deadlocks. They should only be used by applications that
2401 * basically own the machine, and have a very in depth understanding
2402 * of all the possible deadlocks and enospc problems.
2403 */
2404long btrfs_ioctl_trans_end(struct file *file)
2405{
2406 struct inode *inode = fdentry(file)->d_inode;
2407 struct btrfs_root *root = BTRFS_I(inode)->root;
2408 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002409
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002410 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002411 if (!trans)
2412 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002413 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002414
Sage Weil1ab86ae2009-09-29 18:38:44 -04002415 btrfs_end_transaction(trans, root);
2416
Sage Weil9ca9ee02008-08-04 10:41:27 -04002417 mutex_lock(&root->fs_info->trans_mutex);
2418 root->fs_info->open_ioctl_trans--;
2419 mutex_unlock(&root->fs_info->trans_mutex);
2420
Sage Weilcfc8ea82008-12-11 16:30:06 -05002421 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002422 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002423}
2424
Sage Weil46204592010-10-29 15:41:32 -04002425static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2426{
2427 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2428 struct btrfs_trans_handle *trans;
2429 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002430 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002431
2432 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002433 if (IS_ERR(trans))
2434 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002435 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002436 ret = btrfs_commit_transaction_async(trans, root, 0);
2437 if (ret)
2438 return ret;
Sage Weil46204592010-10-29 15:41:32 -04002439
2440 if (argp)
2441 if (copy_to_user(argp, &transid, sizeof(transid)))
2442 return -EFAULT;
2443 return 0;
2444}
2445
2446static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2447{
2448 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2449 u64 transid;
2450
2451 if (argp) {
2452 if (copy_from_user(&transid, argp, sizeof(transid)))
2453 return -EFAULT;
2454 } else {
2455 transid = 0; /* current trans */
2456 }
2457 return btrfs_wait_for_commit(root, transid);
2458}
2459
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002460long btrfs_ioctl(struct file *file, unsigned int
2461 cmd, unsigned long arg)
2462{
2463 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002464 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002465
2466 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002467 case FS_IOC_GETFLAGS:
2468 return btrfs_ioctl_getflags(file, argp);
2469 case FS_IOC_SETFLAGS:
2470 return btrfs_ioctl_setflags(file, argp);
2471 case FS_IOC_GETVERSION:
2472 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00002473 case FITRIM:
2474 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002475 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002476 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002477 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002478 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002479 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002480 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002481 case BTRFS_IOC_SNAP_DESTROY:
2482 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002483 case BTRFS_IOC_SUBVOL_GETFLAGS:
2484 return btrfs_ioctl_subvol_getflags(file, argp);
2485 case BTRFS_IOC_SUBVOL_SETFLAGS:
2486 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002487 case BTRFS_IOC_DEFAULT_SUBVOL:
2488 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002489 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002490 return btrfs_ioctl_defrag(file, NULL);
2491 case BTRFS_IOC_DEFRAG_RANGE:
2492 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002493 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002494 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002495 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002496 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002497 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002498 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002499 case BTRFS_IOC_BALANCE:
2500 return btrfs_balance(root->fs_info->dev_root);
2501 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002502 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2503 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002504 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002505 case BTRFS_IOC_TRANS_START:
2506 return btrfs_ioctl_trans_start(file);
2507 case BTRFS_IOC_TRANS_END:
2508 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002509 case BTRFS_IOC_TREE_SEARCH:
2510 return btrfs_ioctl_tree_search(file, argp);
2511 case BTRFS_IOC_INO_LOOKUP:
2512 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002513 case BTRFS_IOC_SPACE_INFO:
2514 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002515 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002516 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2517 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002518 case BTRFS_IOC_START_SYNC:
2519 return btrfs_ioctl_start_sync(file, argp);
2520 case BTRFS_IOC_WAIT_SYNC:
2521 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002522 }
2523
2524 return -ENOTTY;
2525}