blob: 3240dd90da42899675d4378a6bd22e9589917811 [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 | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000147 FS_NOCOMP_FL | FS_COMPR_FL |
148 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000149 return -EOPNOTSUPP;
150
151 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
152 return -EINVAL;
153
Liu Bo75e7cb72011-03-22 10:12:20 +0000154 return 0;
155}
156
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
158{
159 struct inode *inode = file->f_path.dentry->d_inode;
160 struct btrfs_inode *ip = BTRFS_I(inode);
161 struct btrfs_root *root = ip->root;
162 struct btrfs_trans_handle *trans;
163 unsigned int flags, oldflags;
164 int ret;
165
Li Zefanb83cc962010-12-20 16:04:08 +0800166 if (btrfs_root_readonly(root))
167 return -EROFS;
168
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200169 if (copy_from_user(&flags, arg, sizeof(flags)))
170 return -EFAULT;
171
Liu Bo75e7cb72011-03-22 10:12:20 +0000172 ret = check_flags(flags);
173 if (ret)
174 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200175
176 if (!is_owner_or_cap(inode))
177 return -EACCES;
178
179 mutex_lock(&inode->i_mutex);
180
181 flags = btrfs_mask_flags(inode->i_mode, flags);
182 oldflags = btrfs_flags_to_ioctl(ip->flags);
183 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
184 if (!capable(CAP_LINUX_IMMUTABLE)) {
185 ret = -EPERM;
186 goto out_unlock;
187 }
188 }
189
190 ret = mnt_want_write(file->f_path.mnt);
191 if (ret)
192 goto out_unlock;
193
194 if (flags & FS_SYNC_FL)
195 ip->flags |= BTRFS_INODE_SYNC;
196 else
197 ip->flags &= ~BTRFS_INODE_SYNC;
198 if (flags & FS_IMMUTABLE_FL)
199 ip->flags |= BTRFS_INODE_IMMUTABLE;
200 else
201 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
202 if (flags & FS_APPEND_FL)
203 ip->flags |= BTRFS_INODE_APPEND;
204 else
205 ip->flags &= ~BTRFS_INODE_APPEND;
206 if (flags & FS_NODUMP_FL)
207 ip->flags |= BTRFS_INODE_NODUMP;
208 else
209 ip->flags &= ~BTRFS_INODE_NODUMP;
210 if (flags & FS_NOATIME_FL)
211 ip->flags |= BTRFS_INODE_NOATIME;
212 else
213 ip->flags &= ~BTRFS_INODE_NOATIME;
214 if (flags & FS_DIRSYNC_FL)
215 ip->flags |= BTRFS_INODE_DIRSYNC;
216 else
217 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000218 if (flags & FS_NOCOW_FL)
219 ip->flags |= BTRFS_INODE_NODATACOW;
220 else
221 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200222
Liu Bo75e7cb72011-03-22 10:12:20 +0000223 /*
224 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
225 * flag may be changed automatically if compression code won't make
226 * things smaller.
227 */
228 if (flags & FS_NOCOMP_FL) {
229 ip->flags &= ~BTRFS_INODE_COMPRESS;
230 ip->flags |= BTRFS_INODE_NOCOMPRESS;
231 } else if (flags & FS_COMPR_FL) {
232 ip->flags |= BTRFS_INODE_COMPRESS;
233 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
234 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200235
236 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000237 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238
239 ret = btrfs_update_inode(trans, root, inode);
240 BUG_ON(ret);
241
242 btrfs_update_iflags(inode);
243 inode->i_ctime = CURRENT_TIME;
244 btrfs_end_transaction(trans, root);
245
246 mnt_drop_write(file->f_path.mnt);
liubo2d4e6f6a2011-02-24 09:38:16 +0000247
248 ret = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200249 out_unlock:
250 mutex_unlock(&inode->i_mutex);
liubo2d4e6f6a2011-02-24 09:38:16 +0000251 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200252}
253
254static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
255{
256 struct inode *inode = file->f_path.dentry->d_inode;
257
258 return put_user(inode->i_generation, arg);
259}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400260
Li Dongyangf7039b12011-03-24 10:24:28 +0000261static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
262{
263 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
264 struct btrfs_fs_info *fs_info = root->fs_info;
265 struct btrfs_device *device;
266 struct request_queue *q;
267 struct fstrim_range range;
268 u64 minlen = ULLONG_MAX;
269 u64 num_devices = 0;
270 int ret;
271
272 if (!capable(CAP_SYS_ADMIN))
273 return -EPERM;
274
275 mutex_lock(&fs_info->fs_devices->device_list_mutex);
276 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
277 if (!device->bdev)
278 continue;
279 q = bdev_get_queue(device->bdev);
280 if (blk_queue_discard(q)) {
281 num_devices++;
282 minlen = min((u64)q->limits.discard_granularity,
283 minlen);
284 }
285 }
286 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
287 if (!num_devices)
288 return -EOPNOTSUPP;
289
290 if (copy_from_user(&range, arg, sizeof(range)))
291 return -EFAULT;
292
293 range.minlen = max(range.minlen, minlen);
294 ret = btrfs_trim_fs(root, &range);
295 if (ret < 0)
296 return ret;
297
298 if (copy_to_user(arg, &range, sizeof(range)))
299 return -EFAULT;
300
301 return 0;
302}
303
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400304static noinline int create_subvol(struct btrfs_root *root,
305 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400306 char *name, int namelen,
307 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400308{
309 struct btrfs_trans_handle *trans;
310 struct btrfs_key key;
311 struct btrfs_root_item root_item;
312 struct btrfs_inode_item *inode_item;
313 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400314 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000315 struct dentry *parent = dget_parent(dentry);
316 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400317 int ret;
318 int err;
319 u64 objectid;
320 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500321 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400322
Yan, Zhenga22285a2010-05-16 10:48:46 -0400323 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
324 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000325 if (ret) {
326 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400327 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000328 }
329
330 dir = parent->d_inode;
331
Josef Bacik9ed74f22009-09-11 16:12:44 -0400332 /*
333 * 1 - inode item
334 * 2 - refs
335 * 1 - root item
336 * 2 - dir items
337 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400338 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000339 if (IS_ERR(trans)) {
340 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400341 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000342 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400343
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400344 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
345 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400346 if (IS_ERR(leaf)) {
347 ret = PTR_ERR(leaf);
348 goto fail;
349 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400350
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400351 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400352 btrfs_set_header_bytenr(leaf, leaf->start);
353 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400355 btrfs_set_header_owner(leaf, objectid);
356
357 write_extent_buffer(leaf, root->fs_info->fsid,
358 (unsigned long)btrfs_header_fsid(leaf),
359 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
361 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
362 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400363 btrfs_mark_buffer_dirty(leaf);
364
365 inode_item = &root_item.inode;
366 memset(inode_item, 0, sizeof(*inode_item));
367 inode_item->generation = cpu_to_le64(1);
368 inode_item->size = cpu_to_le64(3);
369 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400370 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
372
Li Zefan08fe4db2011-03-28 02:01:25 +0000373 root_item.flags = 0;
374 root_item.byte_limit = 0;
375 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
376
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400377 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400378 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379 btrfs_set_root_level(&root_item, 0);
380 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000381 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400382 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400383
384 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
385 root_item.drop_level = 0;
386
Chris Mason925baed2008-06-25 16:01:30 -0400387 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400388 free_extent_buffer(leaf);
389 leaf = NULL;
390
391 btrfs_set_root_dirid(&root_item, new_dirid);
392
393 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400394 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400395 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
396 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
397 &root_item);
398 if (ret)
399 goto fail;
400
Yan, Zheng76dda932009-09-21 16:00:26 -0400401 key.offset = (u64)-1;
402 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
403 BUG_ON(IS_ERR(new_root));
404
405 btrfs_record_root_in_trans(trans, new_root);
406
407 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
408 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409 /*
410 * insert the directory item
411 */
Chris Mason3de45862008-11-17 21:02:50 -0500412 ret = btrfs_set_inode_index(dir, &index);
413 BUG_ON(ret);
414
415 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400416 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500417 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400418 if (ret)
419 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500420
Yan Zheng52c26172009-01-05 15:43:43 -0500421 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
422 ret = btrfs_update_inode(trans, root, dir);
423 BUG_ON(ret);
424
Chris Mason0660b5a2008-11-17 20:37:39 -0500425 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400426 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500427 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400428
Chris Mason0660b5a2008-11-17 20:37:39 -0500429 BUG_ON(ret);
430
Yan, Zheng76dda932009-09-21 16:00:26 -0400431 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400432fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000433 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400434 if (async_transid) {
435 *async_transid = trans->transid;
436 err = btrfs_commit_transaction_async(trans, root, 1);
437 } else {
438 err = btrfs_commit_transaction(trans, root);
439 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 if (err && !ret)
441 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400442 return ret;
443}
444
Sage Weil72fd0322010-10-29 15:41:32 -0400445static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800446 char *name, int namelen, u64 *async_transid,
447 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400448{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000449 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000450 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451 struct btrfs_pending_snapshot *pending_snapshot;
452 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000453 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400454
455 if (!root->ref_cows)
456 return -EINVAL;
457
Yan, Zhenga22285a2010-05-16 10:48:46 -0400458 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
459 if (!pending_snapshot)
460 return -ENOMEM;
461
462 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
463 pending_snapshot->dentry = dentry;
464 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800465 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400466
467 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
468 if (IS_ERR(trans)) {
469 ret = PTR_ERR(trans);
470 goto fail;
471 }
472
473 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
474 BUG_ON(ret);
475
476 list_add(&pending_snapshot->list,
477 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400478 if (async_transid) {
479 *async_transid = trans->transid;
480 ret = btrfs_commit_transaction_async(trans,
481 root->fs_info->extent_root, 1);
482 } else {
483 ret = btrfs_commit_transaction(trans,
484 root->fs_info->extent_root);
485 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400486 BUG_ON(ret);
487
488 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400489 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000490 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500492 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
493 if (ret)
494 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400495
Josef Bacik6a912212010-11-20 09:48:00 +0000496 parent = dget_parent(dentry);
497 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
498 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000499 if (IS_ERR(inode)) {
500 ret = PTR_ERR(inode);
501 goto fail;
502 }
503 BUG_ON(!inode);
504 d_instantiate(dentry, inode);
505 ret = 0;
506fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400507 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400508 return ret;
509}
510
Sage Weil4260f7c2010-10-29 15:46:43 -0400511/* copy of check_sticky in fs/namei.c()
512* It's inline, so penalty for filesystems that don't use sticky bit is
513* minimal.
514*/
515static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
516{
517 uid_t fsuid = current_fsuid();
518
519 if (!(dir->i_mode & S_ISVTX))
520 return 0;
521 if (inode->i_uid == fsuid)
522 return 0;
523 if (dir->i_uid == fsuid)
524 return 0;
525 return !capable(CAP_FOWNER);
526}
527
528/* copy of may_delete in fs/namei.c()
529 * Check whether we can remove a link victim from directory dir, check
530 * whether the type of victim is right.
531 * 1. We can't do it if dir is read-only (done in permission())
532 * 2. We should have write and exec permissions on dir
533 * 3. We can't remove anything from append-only dir
534 * 4. We can't do anything with immutable dir (done in permission())
535 * 5. If the sticky bit on dir is set we should either
536 * a. be owner of dir, or
537 * b. be owner of victim, or
538 * c. have CAP_FOWNER capability
539 * 6. If the victim is append-only or immutable we can't do antyhing with
540 * links pointing to it.
541 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
542 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
543 * 9. We can't remove a root or mountpoint.
544 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
545 * nfs_async_unlink().
546 */
547
548static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
549{
550 int error;
551
552 if (!victim->d_inode)
553 return -ENOENT;
554
555 BUG_ON(victim->d_parent->d_inode != dir);
556 audit_inode_child(victim, dir);
557
558 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
559 if (error)
560 return error;
561 if (IS_APPEND(dir))
562 return -EPERM;
563 if (btrfs_check_sticky(dir, victim->d_inode)||
564 IS_APPEND(victim->d_inode)||
565 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
566 return -EPERM;
567 if (isdir) {
568 if (!S_ISDIR(victim->d_inode->i_mode))
569 return -ENOTDIR;
570 if (IS_ROOT(victim))
571 return -EBUSY;
572 } else if (S_ISDIR(victim->d_inode->i_mode))
573 return -EISDIR;
574 if (IS_DEADDIR(dir))
575 return -ENOENT;
576 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
577 return -EBUSY;
578 return 0;
579}
580
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400581/* copy of may_create in fs/namei.c() */
582static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
583{
584 if (child->d_inode)
585 return -EEXIST;
586 if (IS_DEADDIR(dir))
587 return -ENOENT;
588 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
589}
590
591/*
592 * Create a new subvolume below @parent. This is largely modeled after
593 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
594 * inside this filesystem so it's quite a bit simpler.
595 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400596static noinline int btrfs_mksubvol(struct path *parent,
597 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400598 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800599 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400600{
Yan, Zheng76dda932009-09-21 16:00:26 -0400601 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400602 struct dentry *dentry;
603 int error;
604
Yan, Zheng76dda932009-09-21 16:00:26 -0400605 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400606
607 dentry = lookup_one_len(name, parent->dentry, namelen);
608 error = PTR_ERR(dentry);
609 if (IS_ERR(dentry))
610 goto out_unlock;
611
612 error = -EEXIST;
613 if (dentry->d_inode)
614 goto out_dput;
615
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400616 error = mnt_want_write(parent->mnt);
617 if (error)
618 goto out_dput;
619
Yan, Zheng76dda932009-09-21 16:00:26 -0400620 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400621 if (error)
622 goto out_drop_write;
623
Yan, Zheng76dda932009-09-21 16:00:26 -0400624 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
625
626 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
627 goto out_up_read;
628
Chris Mason3de45862008-11-17 21:02:50 -0500629 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400630 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800631 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500632 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400633 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400634 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500635 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400636 if (!error)
637 fsnotify_mkdir(dir, dentry);
638out_up_read:
639 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400640out_drop_write:
641 mnt_drop_write(parent->mnt);
642out_dput:
643 dput(dentry);
644out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400645 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400646 return error;
647}
648
Chris Mason940100a2010-03-10 10:52:59 -0500649static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500650 int thresh, u64 *last_len, u64 *skip,
651 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500652{
653 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
654 struct extent_map *em = NULL;
655 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
656 int ret = 1;
657
Chris Mason1e701a32010-03-11 09:42:04 -0500658
659 if (thresh == 0)
660 thresh = 256 * 1024;
661
Chris Mason940100a2010-03-10 10:52:59 -0500662 /*
663 * make sure that once we start defragging and extent, we keep on
664 * defragging it
665 */
666 if (start < *defrag_end)
667 return 1;
668
669 *skip = 0;
670
671 /*
672 * hopefully we have this extent in the tree already, try without
673 * the full extent lock
674 */
675 read_lock(&em_tree->lock);
676 em = lookup_extent_mapping(em_tree, start, len);
677 read_unlock(&em_tree->lock);
678
679 if (!em) {
680 /* get the big lock and read metadata off disk */
681 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
682 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
683 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
684
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000685 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500686 return 0;
687 }
688
689 /* this will cover holes, and inline extents */
690 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
691 ret = 0;
692
693 /*
694 * we hit a real extent, if it is big don't bother defragging it again
695 */
Chris Mason1e701a32010-03-11 09:42:04 -0500696 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500697 ret = 0;
698
699 /*
700 * last_len ends up being a counter of how many bytes we've defragged.
701 * every time we choose not to defrag an extent, we reset *last_len
702 * so that the next tiny extent will force a defrag.
703 *
704 * The end result of this is that tiny extents before a single big
705 * extent will force at least part of that big extent to be defragged.
706 */
707 if (ret) {
708 *last_len += len;
709 *defrag_end = extent_map_end(em);
710 } else {
711 *last_len = 0;
712 *skip = extent_map_end(em);
713 *defrag_end = 0;
714 }
715
716 free_extent_map(em);
717 return ret;
718}
719
Chris Mason1e701a32010-03-11 09:42:04 -0500720static int btrfs_defrag_file(struct file *file,
721 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400722{
723 struct inode *inode = fdentry(file)->d_inode;
724 struct btrfs_root *root = BTRFS_I(inode)->root;
725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400726 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400727 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800728 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400729 unsigned long last_index;
730 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
731 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800732 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400733 u64 page_start;
734 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500735 u64 last_len = 0;
736 u64 skip = 0;
737 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400738 unsigned long i;
739 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800740 int compress_type = BTRFS_COMPRESS_ZLIB;
741
742 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
743 if (range->compress_type > BTRFS_COMPRESS_TYPES)
744 return -EINVAL;
745 if (range->compress_type)
746 compress_type = range->compress_type;
747 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400748
Chris Mason940100a2010-03-10 10:52:59 -0500749 if (inode->i_size == 0)
750 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400751
Chris Mason1e701a32010-03-11 09:42:04 -0500752 if (range->start + range->len > range->start) {
753 last_index = min_t(u64, inode->i_size - 1,
754 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
755 } else {
756 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
757 }
758
759 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500760 while (i <= last_index) {
761 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500762 PAGE_CACHE_SIZE,
763 range->extent_thresh,
764 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500765 &defrag_end)) {
766 unsigned long next;
767 /*
768 * the should_defrag function tells us how much to skip
769 * bump our counter by the suggested amount
770 */
771 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
772 i = max(i + 1, next);
773 continue;
774 }
775
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400776 if (total_read % ra_pages == 0) {
777 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
778 min(last_index, i + ra_pages - 1));
779 }
780 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500781 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500782 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800783 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500784
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400785 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
786 if (ret)
787 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400788again:
Chris Mason940100a2010-03-10 10:52:59 -0500789 if (inode->i_size == 0 ||
790 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
791 ret = 0;
792 goto err_reservations;
793 }
794
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400795 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400796 if (!page) {
797 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500798 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400799 }
Chris Mason940100a2010-03-10 10:52:59 -0500800
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400801 if (!PageUptodate(page)) {
802 btrfs_readpage(NULL, page);
803 lock_page(page);
804 if (!PageUptodate(page)) {
805 unlock_page(page);
806 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400807 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500808 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400809 }
810 }
811
Chris Mason940100a2010-03-10 10:52:59 -0500812 if (page->mapping != inode->i_mapping) {
813 unlock_page(page);
814 page_cache_release(page);
815 goto again;
816 }
817
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400818 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400819
Chris Mason940100a2010-03-10 10:52:59 -0500820 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400821 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500822 goto loop_unlock;
823 }
824
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400825 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
826 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400827 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400828
829 ordered = btrfs_lookup_ordered_extent(inode, page_start);
830 if (ordered) {
831 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
832 unlock_page(page);
833 page_cache_release(page);
834 btrfs_start_ordered_extent(inode, ordered, 1);
835 btrfs_put_ordered_extent(ordered);
836 goto again;
837 }
838 set_page_extent_mapped(page);
839
Chris Masonf87f0572008-08-01 11:27:23 -0400840 /*
841 * this makes sure page_mkwrite is called on the
842 * page if it is dirtied again later
843 */
844 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500845 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
846 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
847 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400848
Josef Bacik2ac55d42010-02-03 19:33:23 +0000849 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500850 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400851 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400852 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500853
854loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400855 unlock_page(page);
856 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500857 mutex_unlock(&inode->i_mutex);
858
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400859 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500860 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400861 }
862
Chris Mason1e701a32010-03-11 09:42:04 -0500863 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
864 filemap_flush(inode->i_mapping);
865
866 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
867 /* the filemap_flush will queue IO into the worker threads, but
868 * we have to make sure the IO is actually started and that
869 * ordered extents get created before we return
870 */
871 atomic_inc(&root->fs_info->async_submit_draining);
872 while (atomic_read(&root->fs_info->nr_async_submits) ||
873 atomic_read(&root->fs_info->async_delalloc_pages)) {
874 wait_event(root->fs_info->async_submit_wait,
875 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
876 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
877 }
878 atomic_dec(&root->fs_info->async_submit_draining);
879
880 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800881 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500882 mutex_unlock(&inode->i_mutex);
883 }
884
Li Zefan1a419d82010-10-25 15:12:50 +0800885 disk_super = &root->fs_info->super_copy;
886 features = btrfs_super_incompat_flags(disk_super);
887 if (range->compress_type == BTRFS_COMPRESS_LZO) {
888 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
889 btrfs_set_super_incompat_flags(disk_super, features);
890 }
891
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400892 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500893
894err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400895 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
896err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500897 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500898 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400899}
900
Yan, Zheng76dda932009-09-21 16:00:26 -0400901static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
902 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400903{
904 u64 new_size;
905 u64 old_size;
906 u64 devid = 1;
907 struct btrfs_ioctl_vol_args *vol_args;
908 struct btrfs_trans_handle *trans;
909 struct btrfs_device *device = NULL;
910 char *sizestr;
911 char *devstr = NULL;
912 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400913 int mod = 0;
914
Yan Zhengc146afa2008-11-12 14:34:12 -0500915 if (root->fs_info->sb->s_flags & MS_RDONLY)
916 return -EROFS;
917
Chris Masone441d542009-01-05 16:57:23 -0500918 if (!capable(CAP_SYS_ADMIN))
919 return -EPERM;
920
Li Zefandae7b662009-04-08 15:06:54 +0800921 vol_args = memdup_user(arg, sizeof(*vol_args));
922 if (IS_ERR(vol_args))
923 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400924
925 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400926
Chris Mason7d9eb122008-07-08 14:19:17 -0400927 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400928 sizestr = vol_args->name;
929 devstr = strchr(sizestr, ':');
930 if (devstr) {
931 char *end;
932 sizestr = devstr + 1;
933 *devstr = '\0';
934 devstr = vol_args->name;
935 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700936 printk(KERN_INFO "resizing devid %llu\n",
937 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400938 }
Yan Zheng2b820322008-11-17 21:11:30 -0500939 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400940 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700941 printk(KERN_INFO "resizer unable to find device %llu\n",
942 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400943 ret = -EINVAL;
944 goto out_unlock;
945 }
946 if (!strcmp(sizestr, "max"))
947 new_size = device->bdev->bd_inode->i_size;
948 else {
949 if (sizestr[0] == '-') {
950 mod = -1;
951 sizestr++;
952 } else if (sizestr[0] == '+') {
953 mod = 1;
954 sizestr++;
955 }
Akinobu Mita91748462010-02-28 10:59:11 +0000956 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400957 if (new_size == 0) {
958 ret = -EINVAL;
959 goto out_unlock;
960 }
961 }
962
963 old_size = device->total_bytes;
964
965 if (mod < 0) {
966 if (new_size > old_size) {
967 ret = -EINVAL;
968 goto out_unlock;
969 }
970 new_size = old_size - new_size;
971 } else if (mod > 0) {
972 new_size = old_size + new_size;
973 }
974
975 if (new_size < 256 * 1024 * 1024) {
976 ret = -EINVAL;
977 goto out_unlock;
978 }
979 if (new_size > device->bdev->bd_inode->i_size) {
980 ret = -EFBIG;
981 goto out_unlock;
982 }
983
984 do_div(new_size, root->sectorsize);
985 new_size *= root->sectorsize;
986
987 printk(KERN_INFO "new size for %s is %llu\n",
988 device->name, (unsigned long long)new_size);
989
990 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400991 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +0000992 if (IS_ERR(trans)) {
993 ret = PTR_ERR(trans);
994 goto out_unlock;
995 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400996 ret = btrfs_grow_device(trans, device, new_size);
997 btrfs_commit_transaction(trans, root);
998 } else {
999 ret = btrfs_shrink_device(device, new_size);
1000 }
1001
1002out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001003 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001004 kfree(vol_args);
1005 return ret;
1006}
1007
Sage Weil72fd0322010-10-29 15:41:32 -04001008static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1009 char *name,
1010 unsigned long fd,
1011 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001012 u64 *transid,
1013 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001014{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001015 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001016 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001017 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001018 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001019
Yan Zhengc146afa2008-11-12 14:34:12 -05001020 if (root->fs_info->sb->s_flags & MS_RDONLY)
1021 return -EROFS;
1022
Sage Weil72fd0322010-10-29 15:41:32 -04001023 namelen = strlen(name);
1024 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001025 ret = -EINVAL;
1026 goto out;
1027 }
1028
Chris Mason3de45862008-11-17 21:02:50 -05001029 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001030 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001031 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001032 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001033 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001034 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001035 if (!src_file) {
1036 ret = -EINVAL;
1037 goto out;
1038 }
1039
1040 src_inode = src_file->f_path.dentry->d_inode;
1041 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001042 printk(KERN_INFO "btrfs: Snapshot src from "
1043 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001044 ret = -EINVAL;
1045 fput(src_file);
1046 goto out;
1047 }
Sage Weil72fd0322010-10-29 15:41:32 -04001048 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1049 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001050 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001051 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001052 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001053out:
Sage Weil72fd0322010-10-29 15:41:32 -04001054 return ret;
1055}
1056
1057static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001058 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001059{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001060 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001061 int ret;
1062
Li Zefanfa0d2b92010-12-20 15:53:28 +08001063 vol_args = memdup_user(arg, sizeof(*vol_args));
1064 if (IS_ERR(vol_args))
1065 return PTR_ERR(vol_args);
1066 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001067
Li Zefanfa0d2b92010-12-20 15:53:28 +08001068 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001069 vol_args->fd, subvol,
1070 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001071
Li Zefanfa0d2b92010-12-20 15:53:28 +08001072 kfree(vol_args);
1073 return ret;
1074}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001075
Li Zefanfa0d2b92010-12-20 15:53:28 +08001076static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1077 void __user *arg, int subvol)
1078{
1079 struct btrfs_ioctl_vol_args_v2 *vol_args;
1080 int ret;
1081 u64 transid = 0;
1082 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001083 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001084
Li Zefanfa0d2b92010-12-20 15:53:28 +08001085 vol_args = memdup_user(arg, sizeof(*vol_args));
1086 if (IS_ERR(vol_args))
1087 return PTR_ERR(vol_args);
1088 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001089
Li Zefanb83cc962010-12-20 16:04:08 +08001090 if (vol_args->flags &
1091 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1092 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001093 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001094 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001095
1096 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1097 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001098 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1099 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001100
1101 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001102 vol_args->fd, subvol,
1103 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001104
1105 if (ret == 0 && ptr &&
1106 copy_to_user(arg +
1107 offsetof(struct btrfs_ioctl_vol_args_v2,
1108 transid), ptr, sizeof(*ptr)))
1109 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001110out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001111 kfree(vol_args);
1112 return ret;
1113}
1114
Li Zefan0caa1022010-12-20 16:30:25 +08001115static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1116 void __user *arg)
1117{
1118 struct inode *inode = fdentry(file)->d_inode;
1119 struct btrfs_root *root = BTRFS_I(inode)->root;
1120 int ret = 0;
1121 u64 flags = 0;
1122
1123 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1124 return -EINVAL;
1125
1126 down_read(&root->fs_info->subvol_sem);
1127 if (btrfs_root_readonly(root))
1128 flags |= BTRFS_SUBVOL_RDONLY;
1129 up_read(&root->fs_info->subvol_sem);
1130
1131 if (copy_to_user(arg, &flags, sizeof(flags)))
1132 ret = -EFAULT;
1133
1134 return ret;
1135}
1136
1137static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1138 void __user *arg)
1139{
1140 struct inode *inode = fdentry(file)->d_inode;
1141 struct btrfs_root *root = BTRFS_I(inode)->root;
1142 struct btrfs_trans_handle *trans;
1143 u64 root_flags;
1144 u64 flags;
1145 int ret = 0;
1146
1147 if (root->fs_info->sb->s_flags & MS_RDONLY)
1148 return -EROFS;
1149
1150 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1151 return -EINVAL;
1152
1153 if (copy_from_user(&flags, arg, sizeof(flags)))
1154 return -EFAULT;
1155
Li Zefanb4dc2b82011-02-16 06:06:34 +00001156 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001157 return -EINVAL;
1158
1159 if (flags & ~BTRFS_SUBVOL_RDONLY)
1160 return -EOPNOTSUPP;
1161
Li Zefanb4dc2b82011-02-16 06:06:34 +00001162 if (!is_owner_or_cap(inode))
1163 return -EACCES;
1164
Li Zefan0caa1022010-12-20 16:30:25 +08001165 down_write(&root->fs_info->subvol_sem);
1166
1167 /* nothing to do */
1168 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1169 goto out;
1170
1171 root_flags = btrfs_root_flags(&root->root_item);
1172 if (flags & BTRFS_SUBVOL_RDONLY)
1173 btrfs_set_root_flags(&root->root_item,
1174 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1175 else
1176 btrfs_set_root_flags(&root->root_item,
1177 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1178
1179 trans = btrfs_start_transaction(root, 1);
1180 if (IS_ERR(trans)) {
1181 ret = PTR_ERR(trans);
1182 goto out_reset;
1183 }
1184
Li Zefanb4dc2b82011-02-16 06:06:34 +00001185 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001186 &root->root_key, &root->root_item);
1187
1188 btrfs_commit_transaction(trans, root);
1189out_reset:
1190 if (ret)
1191 btrfs_set_root_flags(&root->root_item, root_flags);
1192out:
1193 up_write(&root->fs_info->subvol_sem);
1194 return ret;
1195}
1196
Yan, Zheng76dda932009-09-21 16:00:26 -04001197/*
1198 * helper to check if the subvolume references other subvolumes
1199 */
1200static noinline int may_destroy_subvol(struct btrfs_root *root)
1201{
1202 struct btrfs_path *path;
1203 struct btrfs_key key;
1204 int ret;
1205
1206 path = btrfs_alloc_path();
1207 if (!path)
1208 return -ENOMEM;
1209
1210 key.objectid = root->root_key.objectid;
1211 key.type = BTRFS_ROOT_REF_KEY;
1212 key.offset = (u64)-1;
1213
1214 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1215 &key, path, 0, 0);
1216 if (ret < 0)
1217 goto out;
1218 BUG_ON(ret == 0);
1219
1220 ret = 0;
1221 if (path->slots[0] > 0) {
1222 path->slots[0]--;
1223 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1224 if (key.objectid == root->root_key.objectid &&
1225 key.type == BTRFS_ROOT_REF_KEY)
1226 ret = -ENOTEMPTY;
1227 }
1228out:
1229 btrfs_free_path(path);
1230 return ret;
1231}
1232
Chris Masonac8e9812010-02-28 15:39:26 -05001233static noinline int key_in_sk(struct btrfs_key *key,
1234 struct btrfs_ioctl_search_key *sk)
1235{
Chris Masonabc6e132010-03-18 12:10:08 -04001236 struct btrfs_key test;
1237 int ret;
1238
1239 test.objectid = sk->min_objectid;
1240 test.type = sk->min_type;
1241 test.offset = sk->min_offset;
1242
1243 ret = btrfs_comp_cpu_keys(key, &test);
1244 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001245 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001246
1247 test.objectid = sk->max_objectid;
1248 test.type = sk->max_type;
1249 test.offset = sk->max_offset;
1250
1251 ret = btrfs_comp_cpu_keys(key, &test);
1252 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001253 return 0;
1254 return 1;
1255}
1256
1257static noinline int copy_to_sk(struct btrfs_root *root,
1258 struct btrfs_path *path,
1259 struct btrfs_key *key,
1260 struct btrfs_ioctl_search_key *sk,
1261 char *buf,
1262 unsigned long *sk_offset,
1263 int *num_found)
1264{
1265 u64 found_transid;
1266 struct extent_buffer *leaf;
1267 struct btrfs_ioctl_search_header sh;
1268 unsigned long item_off;
1269 unsigned long item_len;
1270 int nritems;
1271 int i;
1272 int slot;
1273 int found = 0;
1274 int ret = 0;
1275
1276 leaf = path->nodes[0];
1277 slot = path->slots[0];
1278 nritems = btrfs_header_nritems(leaf);
1279
1280 if (btrfs_header_generation(leaf) > sk->max_transid) {
1281 i = nritems;
1282 goto advance_key;
1283 }
1284 found_transid = btrfs_header_generation(leaf);
1285
1286 for (i = slot; i < nritems; i++) {
1287 item_off = btrfs_item_ptr_offset(leaf, i);
1288 item_len = btrfs_item_size_nr(leaf, i);
1289
1290 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1291 item_len = 0;
1292
1293 if (sizeof(sh) + item_len + *sk_offset >
1294 BTRFS_SEARCH_ARGS_BUFSIZE) {
1295 ret = 1;
1296 goto overflow;
1297 }
1298
1299 btrfs_item_key_to_cpu(leaf, key, i);
1300 if (!key_in_sk(key, sk))
1301 continue;
1302
1303 sh.objectid = key->objectid;
1304 sh.offset = key->offset;
1305 sh.type = key->type;
1306 sh.len = item_len;
1307 sh.transid = found_transid;
1308
1309 /* copy search result header */
1310 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1311 *sk_offset += sizeof(sh);
1312
1313 if (item_len) {
1314 char *p = buf + *sk_offset;
1315 /* copy the item */
1316 read_extent_buffer(leaf, p,
1317 item_off, item_len);
1318 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001319 }
Chris Mason90fdde12010-03-18 12:14:54 -04001320 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001321
1322 if (*num_found >= sk->nr_items)
1323 break;
1324 }
1325advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001326 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001327 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1328 key->offset++;
1329 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1330 key->offset = 0;
1331 key->type++;
1332 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1333 key->offset = 0;
1334 key->type = 0;
1335 key->objectid++;
1336 } else
1337 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001338overflow:
1339 *num_found += found;
1340 return ret;
1341}
1342
1343static noinline int search_ioctl(struct inode *inode,
1344 struct btrfs_ioctl_search_args *args)
1345{
1346 struct btrfs_root *root;
1347 struct btrfs_key key;
1348 struct btrfs_key max_key;
1349 struct btrfs_path *path;
1350 struct btrfs_ioctl_search_key *sk = &args->key;
1351 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1352 int ret;
1353 int num_found = 0;
1354 unsigned long sk_offset = 0;
1355
1356 path = btrfs_alloc_path();
1357 if (!path)
1358 return -ENOMEM;
1359
1360 if (sk->tree_id == 0) {
1361 /* search the root of the inode that was passed */
1362 root = BTRFS_I(inode)->root;
1363 } else {
1364 key.objectid = sk->tree_id;
1365 key.type = BTRFS_ROOT_ITEM_KEY;
1366 key.offset = (u64)-1;
1367 root = btrfs_read_fs_root_no_name(info, &key);
1368 if (IS_ERR(root)) {
1369 printk(KERN_ERR "could not find root %llu\n",
1370 sk->tree_id);
1371 btrfs_free_path(path);
1372 return -ENOENT;
1373 }
1374 }
1375
1376 key.objectid = sk->min_objectid;
1377 key.type = sk->min_type;
1378 key.offset = sk->min_offset;
1379
1380 max_key.objectid = sk->max_objectid;
1381 max_key.type = sk->max_type;
1382 max_key.offset = sk->max_offset;
1383
1384 path->keep_locks = 1;
1385
1386 while(1) {
1387 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1388 sk->min_transid);
1389 if (ret != 0) {
1390 if (ret > 0)
1391 ret = 0;
1392 goto err;
1393 }
1394 ret = copy_to_sk(root, path, &key, sk, args->buf,
1395 &sk_offset, &num_found);
1396 btrfs_release_path(root, path);
1397 if (ret || num_found >= sk->nr_items)
1398 break;
1399
1400 }
1401 ret = 0;
1402err:
1403 sk->nr_items = num_found;
1404 btrfs_free_path(path);
1405 return ret;
1406}
1407
1408static noinline int btrfs_ioctl_tree_search(struct file *file,
1409 void __user *argp)
1410{
1411 struct btrfs_ioctl_search_args *args;
1412 struct inode *inode;
1413 int ret;
1414
1415 if (!capable(CAP_SYS_ADMIN))
1416 return -EPERM;
1417
Julia Lawall2354d082010-10-29 15:14:18 -04001418 args = memdup_user(argp, sizeof(*args));
1419 if (IS_ERR(args))
1420 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001421
Chris Masonac8e9812010-02-28 15:39:26 -05001422 inode = fdentry(file)->d_inode;
1423 ret = search_ioctl(inode, args);
1424 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1425 ret = -EFAULT;
1426 kfree(args);
1427 return ret;
1428}
1429
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001430/*
Chris Masonac8e9812010-02-28 15:39:26 -05001431 * Search INODE_REFs to identify path name of 'dirid' directory
1432 * in a 'tree_id' tree. and sets path name to 'name'.
1433 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001434static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1435 u64 tree_id, u64 dirid, char *name)
1436{
1437 struct btrfs_root *root;
1438 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001439 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001440 int ret = -1;
1441 int slot;
1442 int len;
1443 int total_len = 0;
1444 struct btrfs_inode_ref *iref;
1445 struct extent_buffer *l;
1446 struct btrfs_path *path;
1447
1448 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1449 name[0]='\0';
1450 return 0;
1451 }
1452
1453 path = btrfs_alloc_path();
1454 if (!path)
1455 return -ENOMEM;
1456
Chris Masonac8e9812010-02-28 15:39:26 -05001457 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001458
1459 key.objectid = tree_id;
1460 key.type = BTRFS_ROOT_ITEM_KEY;
1461 key.offset = (u64)-1;
1462 root = btrfs_read_fs_root_no_name(info, &key);
1463 if (IS_ERR(root)) {
1464 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001465 ret = -ENOENT;
1466 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001467 }
1468
1469 key.objectid = dirid;
1470 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001471 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001472
1473 while(1) {
1474 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1475 if (ret < 0)
1476 goto out;
1477
1478 l = path->nodes[0];
1479 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001480 if (ret > 0 && slot > 0)
1481 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001482 btrfs_item_key_to_cpu(l, &key, slot);
1483
1484 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001485 key.type != BTRFS_INODE_REF_KEY)) {
1486 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001487 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001488 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001489
1490 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1491 len = btrfs_inode_ref_name_len(l, iref);
1492 ptr -= len + 1;
1493 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001494 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001495 goto out;
1496
1497 *(ptr + len) = '/';
1498 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1499
1500 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1501 break;
1502
1503 btrfs_release_path(root, path);
1504 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001505 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001506 dirid = key.objectid;
1507
1508 }
Chris Masonac8e9812010-02-28 15:39:26 -05001509 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001510 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001511 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001512 name[total_len]='\0';
1513 ret = 0;
1514out:
1515 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001516 return ret;
1517}
1518
1519static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1520 void __user *argp)
1521{
1522 struct btrfs_ioctl_ino_lookup_args *args;
1523 struct inode *inode;
1524 int ret;
1525
1526 if (!capable(CAP_SYS_ADMIN))
1527 return -EPERM;
1528
Julia Lawall2354d082010-10-29 15:14:18 -04001529 args = memdup_user(argp, sizeof(*args));
1530 if (IS_ERR(args))
1531 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001532
Chris Masonac8e9812010-02-28 15:39:26 -05001533 inode = fdentry(file)->d_inode;
1534
Chris Mason1b53ac42010-03-18 12:17:05 -04001535 if (args->treeid == 0)
1536 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1537
Chris Masonac8e9812010-02-28 15:39:26 -05001538 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1539 args->treeid, args->objectid,
1540 args->name);
1541
1542 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1543 ret = -EFAULT;
1544
1545 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001546 return ret;
1547}
1548
Yan, Zheng76dda932009-09-21 16:00:26 -04001549static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1550 void __user *arg)
1551{
1552 struct dentry *parent = fdentry(file);
1553 struct dentry *dentry;
1554 struct inode *dir = parent->d_inode;
1555 struct inode *inode;
1556 struct btrfs_root *root = BTRFS_I(dir)->root;
1557 struct btrfs_root *dest = NULL;
1558 struct btrfs_ioctl_vol_args *vol_args;
1559 struct btrfs_trans_handle *trans;
1560 int namelen;
1561 int ret;
1562 int err = 0;
1563
Yan, Zheng76dda932009-09-21 16:00:26 -04001564 vol_args = memdup_user(arg, sizeof(*vol_args));
1565 if (IS_ERR(vol_args))
1566 return PTR_ERR(vol_args);
1567
1568 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1569 namelen = strlen(vol_args->name);
1570 if (strchr(vol_args->name, '/') ||
1571 strncmp(vol_args->name, "..", namelen) == 0) {
1572 err = -EINVAL;
1573 goto out;
1574 }
1575
1576 err = mnt_want_write(file->f_path.mnt);
1577 if (err)
1578 goto out;
1579
1580 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1581 dentry = lookup_one_len(vol_args->name, parent, namelen);
1582 if (IS_ERR(dentry)) {
1583 err = PTR_ERR(dentry);
1584 goto out_unlock_dir;
1585 }
1586
1587 if (!dentry->d_inode) {
1588 err = -ENOENT;
1589 goto out_dput;
1590 }
1591
1592 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001593 dest = BTRFS_I(inode)->root;
1594 if (!capable(CAP_SYS_ADMIN)){
1595 /*
1596 * Regular user. Only allow this with a special mount
1597 * option, when the user has write+exec access to the
1598 * subvol root, and when rmdir(2) would have been
1599 * allowed.
1600 *
1601 * Note that this is _not_ check that the subvol is
1602 * empty or doesn't contain data that we wouldn't
1603 * otherwise be able to delete.
1604 *
1605 * Users who want to delete empty subvols should try
1606 * rmdir(2).
1607 */
1608 err = -EPERM;
1609 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1610 goto out_dput;
1611
1612 /*
1613 * Do not allow deletion if the parent dir is the same
1614 * as the dir to be deleted. That means the ioctl
1615 * must be called on the dentry referencing the root
1616 * of the subvol, not a random directory contained
1617 * within it.
1618 */
1619 err = -EINVAL;
1620 if (root == dest)
1621 goto out_dput;
1622
1623 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1624 if (err)
1625 goto out_dput;
1626
1627 /* check if subvolume may be deleted by a non-root user */
1628 err = btrfs_may_delete(dir, dentry, 1);
1629 if (err)
1630 goto out_dput;
1631 }
1632
Yan, Zheng76dda932009-09-21 16:00:26 -04001633 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1634 err = -EINVAL;
1635 goto out_dput;
1636 }
1637
Yan, Zheng76dda932009-09-21 16:00:26 -04001638 mutex_lock(&inode->i_mutex);
1639 err = d_invalidate(dentry);
1640 if (err)
1641 goto out_unlock;
1642
1643 down_write(&root->fs_info->subvol_sem);
1644
1645 err = may_destroy_subvol(dest);
1646 if (err)
1647 goto out_up_write;
1648
Yan, Zhenga22285a2010-05-16 10:48:46 -04001649 trans = btrfs_start_transaction(root, 0);
1650 if (IS_ERR(trans)) {
1651 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001652 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001653 }
1654 trans->block_rsv = &root->fs_info->global_block_rsv;
1655
Yan, Zheng76dda932009-09-21 16:00:26 -04001656 ret = btrfs_unlink_subvol(trans, root, dir,
1657 dest->root_key.objectid,
1658 dentry->d_name.name,
1659 dentry->d_name.len);
1660 BUG_ON(ret);
1661
1662 btrfs_record_root_in_trans(trans, dest);
1663
1664 memset(&dest->root_item.drop_progress, 0,
1665 sizeof(dest->root_item.drop_progress));
1666 dest->root_item.drop_level = 0;
1667 btrfs_set_root_refs(&dest->root_item, 0);
1668
Yan, Zhengd68fc572010-05-16 10:49:58 -04001669 if (!xchg(&dest->orphan_item_inserted, 1)) {
1670 ret = btrfs_insert_orphan_item(trans,
1671 root->fs_info->tree_root,
1672 dest->root_key.objectid);
1673 BUG_ON(ret);
1674 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001675
Sage Weil531cb132010-10-29 15:41:32 -04001676 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001677 BUG_ON(ret);
1678 inode->i_flags |= S_DEAD;
1679out_up_write:
1680 up_write(&root->fs_info->subvol_sem);
1681out_unlock:
1682 mutex_unlock(&inode->i_mutex);
1683 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001684 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001685 btrfs_invalidate_inodes(dest);
1686 d_delete(dentry);
1687 }
1688out_dput:
1689 dput(dentry);
1690out_unlock_dir:
1691 mutex_unlock(&dir->i_mutex);
1692 mnt_drop_write(file->f_path.mnt);
1693out:
1694 kfree(vol_args);
1695 return err;
1696}
1697
Chris Mason1e701a32010-03-11 09:42:04 -05001698static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001699{
1700 struct inode *inode = fdentry(file)->d_inode;
1701 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001702 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001703 int ret;
1704
Li Zefanb83cc962010-12-20 16:04:08 +08001705 if (btrfs_root_readonly(root))
1706 return -EROFS;
1707
Yan Zhengc146afa2008-11-12 14:34:12 -05001708 ret = mnt_want_write(file->f_path.mnt);
1709 if (ret)
1710 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001711
1712 switch (inode->i_mode & S_IFMT) {
1713 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001714 if (!capable(CAP_SYS_ADMIN)) {
1715 ret = -EPERM;
1716 goto out;
1717 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001718 ret = btrfs_defrag_root(root, 0);
1719 if (ret)
1720 goto out;
1721 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001722 break;
1723 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001724 if (!(file->f_mode & FMODE_WRITE)) {
1725 ret = -EINVAL;
1726 goto out;
1727 }
Chris Mason1e701a32010-03-11 09:42:04 -05001728
1729 range = kzalloc(sizeof(*range), GFP_KERNEL);
1730 if (!range) {
1731 ret = -ENOMEM;
1732 goto out;
1733 }
1734
1735 if (argp) {
1736 if (copy_from_user(range, argp,
1737 sizeof(*range))) {
1738 ret = -EFAULT;
1739 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001740 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001741 }
1742 /* compression requires us to start the IO */
1743 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1744 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1745 range->extent_thresh = (u32)-1;
1746 }
1747 } else {
1748 /* the rest are all set to zero by kzalloc */
1749 range->len = (u64)-1;
1750 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001751 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001752 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001753 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001754 default:
1755 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001756 }
Chris Masone441d542009-01-05 16:57:23 -05001757out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001758 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001759 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001760}
1761
Christoph Hellwigb2950862008-12-02 09:54:17 -05001762static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001763{
1764 struct btrfs_ioctl_vol_args *vol_args;
1765 int ret;
1766
Chris Masone441d542009-01-05 16:57:23 -05001767 if (!capable(CAP_SYS_ADMIN))
1768 return -EPERM;
1769
Li Zefandae7b662009-04-08 15:06:54 +08001770 vol_args = memdup_user(arg, sizeof(*vol_args));
1771 if (IS_ERR(vol_args))
1772 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001773
Mark Fasheh5516e592008-07-24 12:20:14 -04001774 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001775 ret = btrfs_init_new_device(root, vol_args->name);
1776
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001777 kfree(vol_args);
1778 return ret;
1779}
1780
Christoph Hellwigb2950862008-12-02 09:54:17 -05001781static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001782{
1783 struct btrfs_ioctl_vol_args *vol_args;
1784 int ret;
1785
Chris Masone441d542009-01-05 16:57:23 -05001786 if (!capable(CAP_SYS_ADMIN))
1787 return -EPERM;
1788
Yan Zhengc146afa2008-11-12 14:34:12 -05001789 if (root->fs_info->sb->s_flags & MS_RDONLY)
1790 return -EROFS;
1791
Li Zefandae7b662009-04-08 15:06:54 +08001792 vol_args = memdup_user(arg, sizeof(*vol_args));
1793 if (IS_ERR(vol_args))
1794 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001795
Mark Fasheh5516e592008-07-24 12:20:14 -04001796 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001797 ret = btrfs_rm_device(root, vol_args->name);
1798
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001799 kfree(vol_args);
1800 return ret;
1801}
1802
Yan, Zheng76dda932009-09-21 16:00:26 -04001803static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1804 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001805{
1806 struct inode *inode = fdentry(file)->d_inode;
1807 struct btrfs_root *root = BTRFS_I(inode)->root;
1808 struct file *src_file;
1809 struct inode *src;
1810 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001811 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001812 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001813 char *buf;
1814 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001815 u32 nritems;
1816 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001817 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001818 u64 len = olen;
1819 u64 bs = root->fs_info->sb->s_blocksize;
1820 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001821
Sage Weilc5c9cd42008-11-12 14:32:25 -05001822 /*
1823 * TODO:
1824 * - split compressed inline extents. annoying: we need to
1825 * decompress into destination's address_space (the file offset
1826 * may change, so source mapping won't do), then recompress (or
1827 * otherwise reinsert) a subrange.
1828 * - allow ranges within the same file to be cloned (provided
1829 * they don't overlap)?
1830 */
1831
Chris Masone441d542009-01-05 16:57:23 -05001832 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001833 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001834 return -EINVAL;
1835
Li Zefanb83cc962010-12-20 16:04:08 +08001836 if (btrfs_root_readonly(root))
1837 return -EROFS;
1838
Yan Zhengc146afa2008-11-12 14:34:12 -05001839 ret = mnt_want_write(file->f_path.mnt);
1840 if (ret)
1841 return ret;
1842
Sage Weilc5c9cd42008-11-12 14:32:25 -05001843 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001844 if (!src_file) {
1845 ret = -EBADF;
1846 goto out_drop_write;
1847 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001848
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001849 src = src_file->f_dentry->d_inode;
1850
Sage Weilc5c9cd42008-11-12 14:32:25 -05001851 ret = -EINVAL;
1852 if (src == inode)
1853 goto out_fput;
1854
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001855 /* the src must be open for reading */
1856 if (!(src_file->f_mode & FMODE_READ))
1857 goto out_fput;
1858
Yan Zhengae01a0a2008-08-04 23:23:47 -04001859 ret = -EISDIR;
1860 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001861 goto out_fput;
1862
Yan Zhengae01a0a2008-08-04 23:23:47 -04001863 ret = -EXDEV;
1864 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1865 goto out_fput;
1866
1867 ret = -ENOMEM;
1868 buf = vmalloc(btrfs_level_size(root, 0));
1869 if (!buf)
1870 goto out_fput;
1871
1872 path = btrfs_alloc_path();
1873 if (!path) {
1874 vfree(buf);
1875 goto out_fput;
1876 }
1877 path->reada = 2;
1878
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001879 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001880 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1881 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001882 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001883 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1884 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001885 }
1886
Sage Weilc5c9cd42008-11-12 14:32:25 -05001887 /* determine range to clone */
1888 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001889 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001890 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001891 if (len == 0)
1892 olen = len = src->i_size - off;
1893 /* if we extend to eof, continue to block boundary */
1894 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001895 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001896
1897 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001898 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1899 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001900 goto out_unlock;
1901
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001902 /* do any pending delalloc/csum calc on src, one way or
1903 another, and lock file content */
1904 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001905 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001906 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001907 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1908 if (!ordered &&
1909 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1910 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001911 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001912 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001913 if (ordered)
1914 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001915 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001916 }
1917
Sage Weilc5c9cd42008-11-12 14:32:25 -05001918 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001919 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001920 key.type = BTRFS_EXTENT_DATA_KEY;
1921 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001922
1923 while (1) {
1924 /*
1925 * note the key will change type as we walk through the
1926 * tree.
1927 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001929 if (ret < 0)
1930 goto out;
1931
Yan Zhengae01a0a2008-08-04 23:23:47 -04001932 nritems = btrfs_header_nritems(path->nodes[0]);
1933 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001934 ret = btrfs_next_leaf(root, path);
1935 if (ret < 0)
1936 goto out;
1937 if (ret > 0)
1938 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001939 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001940 }
1941 leaf = path->nodes[0];
1942 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001943
Yan Zhengae01a0a2008-08-04 23:23:47 -04001944 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001945 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001946 key.objectid != src->i_ino)
1947 break;
1948
Sage Weilc5c9cd42008-11-12 14:32:25 -05001949 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1950 struct btrfs_file_extent_item *extent;
1951 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001952 u32 size;
1953 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001954 u64 disko = 0, diskl = 0;
1955 u64 datao = 0, datal = 0;
1956 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001957 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001958
1959 size = btrfs_item_size_nr(leaf, slot);
1960 read_extent_buffer(leaf, buf,
1961 btrfs_item_ptr_offset(leaf, slot),
1962 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001963
1964 extent = btrfs_item_ptr(leaf, slot,
1965 struct btrfs_file_extent_item);
1966 comp = btrfs_file_extent_compression(leaf, extent);
1967 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001968 if (type == BTRFS_FILE_EXTENT_REG ||
1969 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001970 disko = btrfs_file_extent_disk_bytenr(leaf,
1971 extent);
1972 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1973 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001974 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001975 datal = btrfs_file_extent_num_bytes(leaf,
1976 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001977 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1978 /* take upper bound, may be compressed */
1979 datal = btrfs_file_extent_ram_bytes(leaf,
1980 extent);
1981 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001982 btrfs_release_path(root, path);
1983
Sage Weil050006a2010-10-29 15:37:33 -04001984 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001985 key.offset >= off+len)
1986 goto next;
1987
Zheng Yan31840ae2008-09-23 13:14:14 -04001988 memcpy(&new_key, &key, sizeof(new_key));
1989 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001990 if (off <= key.offset)
1991 new_key.offset = key.offset + destoff - off;
1992 else
1993 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001994
Yan, Zhenga22285a2010-05-16 10:48:46 -04001995 trans = btrfs_start_transaction(root, 1);
1996 if (IS_ERR(trans)) {
1997 ret = PTR_ERR(trans);
1998 goto out;
1999 }
2000
Chris Masonc8a894d2009-06-27 21:07:03 -04002001 if (type == BTRFS_FILE_EXTENT_REG ||
2002 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002003 if (off > key.offset) {
2004 datao += off - key.offset;
2005 datal -= off - key.offset;
2006 }
2007
2008 if (key.offset + datal > off + len)
2009 datal = off + len - key.offset;
2010
2011 ret = btrfs_drop_extents(trans, inode,
2012 new_key.offset,
2013 new_key.offset + datal,
2014 &hint_byte, 1);
2015 BUG_ON(ret);
2016
Sage Weilc5c9cd42008-11-12 14:32:25 -05002017 ret = btrfs_insert_empty_item(trans, root, path,
2018 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002019 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002020
2021 leaf = path->nodes[0];
2022 slot = path->slots[0];
2023 write_extent_buffer(leaf, buf,
2024 btrfs_item_ptr_offset(leaf, slot),
2025 size);
2026
2027 extent = btrfs_item_ptr(leaf, slot,
2028 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002029
Sage Weilc5c9cd42008-11-12 14:32:25 -05002030 /* disko == 0 means it's a hole */
2031 if (!disko)
2032 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002033
2034 btrfs_set_file_extent_offset(leaf, extent,
2035 datao);
2036 btrfs_set_file_extent_num_bytes(leaf, extent,
2037 datal);
2038 if (disko) {
2039 inode_add_bytes(inode, datal);
2040 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002041 disko, diskl, 0,
2042 root->root_key.objectid,
2043 inode->i_ino,
2044 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002045 BUG_ON(ret);
2046 }
2047 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2048 u64 skip = 0;
2049 u64 trim = 0;
2050 if (off > key.offset) {
2051 skip = off - key.offset;
2052 new_key.offset += skip;
2053 }
Chris Masond3977122009-01-05 21:25:51 -05002054
Sage Weilc5c9cd42008-11-12 14:32:25 -05002055 if (key.offset + datal > off+len)
2056 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002057
Sage Weilc5c9cd42008-11-12 14:32:25 -05002058 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002059 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002060 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002061 goto out;
2062 }
2063 size -= skip + trim;
2064 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002065
2066 ret = btrfs_drop_extents(trans, inode,
2067 new_key.offset,
2068 new_key.offset + datal,
2069 &hint_byte, 1);
2070 BUG_ON(ret);
2071
Sage Weilc5c9cd42008-11-12 14:32:25 -05002072 ret = btrfs_insert_empty_item(trans, root, path,
2073 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002074 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002075
2076 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002077 u32 start =
2078 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002079 memmove(buf+start, buf+start+skip,
2080 datal);
2081 }
2082
2083 leaf = path->nodes[0];
2084 slot = path->slots[0];
2085 write_extent_buffer(leaf, buf,
2086 btrfs_item_ptr_offset(leaf, slot),
2087 size);
2088 inode_add_bytes(inode, datal);
2089 }
2090
2091 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002092 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002093
Yan, Zhenga22285a2010-05-16 10:48:46 -04002094 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002095
2096 /*
2097 * we round up to the block size at eof when
2098 * determining which extents to clone above,
2099 * but shouldn't round up the file size
2100 */
2101 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002102 if (endoff > destoff+olen)
2103 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002104 if (endoff > inode->i_size)
2105 btrfs_i_size_write(inode, endoff);
2106
Yan, Zhenga22285a2010-05-16 10:48:46 -04002107 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2108 ret = btrfs_update_inode(trans, root, inode);
2109 BUG_ON(ret);
2110 btrfs_end_transaction(trans, root);
2111 }
Chris Masond3977122009-01-05 21:25:51 -05002112next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002113 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002114 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002115 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002116 ret = 0;
2117out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002118 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002119 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002120out_unlock:
2121 mutex_unlock(&src->i_mutex);
2122 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002123 vfree(buf);
2124 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002125out_fput:
2126 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002127out_drop_write:
2128 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002129 return ret;
2130}
2131
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002132static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002133{
2134 struct btrfs_ioctl_clone_range_args args;
2135
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002136 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002137 return -EFAULT;
2138 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2139 args.src_length, args.dest_offset);
2140}
2141
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002142/*
2143 * there are many ways the trans_start and trans_end ioctls can lead
2144 * to deadlocks. They should only be used by applications that
2145 * basically own the machine, and have a very in depth understanding
2146 * of all the possible deadlocks and enospc problems.
2147 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002148static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002149{
2150 struct inode *inode = fdentry(file)->d_inode;
2151 struct btrfs_root *root = BTRFS_I(inode)->root;
2152 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002153 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002154
Sage Weil1ab86ae2009-09-29 18:38:44 -04002155 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002156 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002157 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002158
2159 ret = -EINPROGRESS;
2160 if (file->private_data)
2161 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002162
Li Zefanb83cc962010-12-20 16:04:08 +08002163 ret = -EROFS;
2164 if (btrfs_root_readonly(root))
2165 goto out;
2166
Yan Zhengc146afa2008-11-12 14:34:12 -05002167 ret = mnt_want_write(file->f_path.mnt);
2168 if (ret)
2169 goto out;
2170
Sage Weil9ca9ee02008-08-04 10:41:27 -04002171 mutex_lock(&root->fs_info->trans_mutex);
2172 root->fs_info->open_ioctl_trans++;
2173 mutex_unlock(&root->fs_info->trans_mutex);
2174
Sage Weil1ab86ae2009-09-29 18:38:44 -04002175 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002176 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002177 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002178 goto out_drop;
2179
2180 file->private_data = trans;
2181 return 0;
2182
2183out_drop:
2184 mutex_lock(&root->fs_info->trans_mutex);
2185 root->fs_info->open_ioctl_trans--;
2186 mutex_unlock(&root->fs_info->trans_mutex);
2187 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002188out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002189 return ret;
2190}
2191
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002192static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2193{
2194 struct inode *inode = fdentry(file)->d_inode;
2195 struct btrfs_root *root = BTRFS_I(inode)->root;
2196 struct btrfs_root *new_root;
2197 struct btrfs_dir_item *di;
2198 struct btrfs_trans_handle *trans;
2199 struct btrfs_path *path;
2200 struct btrfs_key location;
2201 struct btrfs_disk_key disk_key;
2202 struct btrfs_super_block *disk_super;
2203 u64 features;
2204 u64 objectid = 0;
2205 u64 dir_id;
2206
2207 if (!capable(CAP_SYS_ADMIN))
2208 return -EPERM;
2209
2210 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2211 return -EFAULT;
2212
2213 if (!objectid)
2214 objectid = root->root_key.objectid;
2215
2216 location.objectid = objectid;
2217 location.type = BTRFS_ROOT_ITEM_KEY;
2218 location.offset = (u64)-1;
2219
2220 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2221 if (IS_ERR(new_root))
2222 return PTR_ERR(new_root);
2223
2224 if (btrfs_root_refs(&new_root->root_item) == 0)
2225 return -ENOENT;
2226
2227 path = btrfs_alloc_path();
2228 if (!path)
2229 return -ENOMEM;
2230 path->leave_spinning = 1;
2231
2232 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002233 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002234 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002235 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002236 }
2237
2238 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2239 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2240 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002241 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002242 btrfs_free_path(path);
2243 btrfs_end_transaction(trans, root);
2244 printk(KERN_ERR "Umm, you don't have the default dir item, "
2245 "this isn't going to work\n");
2246 return -ENOENT;
2247 }
2248
2249 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2250 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2251 btrfs_mark_buffer_dirty(path->nodes[0]);
2252 btrfs_free_path(path);
2253
2254 disk_super = &root->fs_info->super_copy;
2255 features = btrfs_super_incompat_flags(disk_super);
2256 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2257 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2258 btrfs_set_super_incompat_flags(disk_super, features);
2259 }
2260 btrfs_end_transaction(trans, root);
2261
2262 return 0;
2263}
2264
Josef Bacikbf5fc092010-09-29 11:22:36 -04002265static void get_block_group_info(struct list_head *groups_list,
2266 struct btrfs_ioctl_space_info *space)
2267{
2268 struct btrfs_block_group_cache *block_group;
2269
2270 space->total_bytes = 0;
2271 space->used_bytes = 0;
2272 space->flags = 0;
2273 list_for_each_entry(block_group, groups_list, list) {
2274 space->flags = block_group->flags;
2275 space->total_bytes += block_group->key.offset;
2276 space->used_bytes +=
2277 btrfs_block_group_used(&block_group->item);
2278 }
2279}
2280
Josef Bacik1406e432010-01-13 18:19:06 +00002281long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2282{
2283 struct btrfs_ioctl_space_args space_args;
2284 struct btrfs_ioctl_space_info space;
2285 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002286 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002287 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002288 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002289 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2290 BTRFS_BLOCK_GROUP_SYSTEM,
2291 BTRFS_BLOCK_GROUP_METADATA,
2292 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2293 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002294 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002295 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002296 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002297 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002298
2299 if (copy_from_user(&space_args,
2300 (struct btrfs_ioctl_space_args __user *)arg,
2301 sizeof(space_args)))
2302 return -EFAULT;
2303
Josef Bacikbf5fc092010-09-29 11:22:36 -04002304 for (i = 0; i < num_types; i++) {
2305 struct btrfs_space_info *tmp;
2306
2307 info = NULL;
2308 rcu_read_lock();
2309 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2310 list) {
2311 if (tmp->flags == types[i]) {
2312 info = tmp;
2313 break;
2314 }
2315 }
2316 rcu_read_unlock();
2317
2318 if (!info)
2319 continue;
2320
2321 down_read(&info->groups_sem);
2322 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2323 if (!list_empty(&info->block_groups[c]))
2324 slot_count++;
2325 }
2326 up_read(&info->groups_sem);
2327 }
Josef Bacik1406e432010-01-13 18:19:06 +00002328
Chris Mason7fde62b2010-03-16 15:40:10 -04002329 /* space_slots == 0 means they are asking for a count */
2330 if (space_args.space_slots == 0) {
2331 space_args.total_spaces = slot_count;
2332 goto out;
2333 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002334
Dan Rosenberg51788b12011-02-14 16:04:23 -05002335 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002336
Chris Mason7fde62b2010-03-16 15:40:10 -04002337 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002338
Chris Mason7fde62b2010-03-16 15:40:10 -04002339 /* we generally have at most 6 or so space infos, one for each raid
2340 * level. So, a whole page should be more than enough for everyone
2341 */
2342 if (alloc_size > PAGE_CACHE_SIZE)
2343 return -ENOMEM;
2344
2345 space_args.total_spaces = 0;
2346 dest = kmalloc(alloc_size, GFP_NOFS);
2347 if (!dest)
2348 return -ENOMEM;
2349 dest_orig = dest;
2350
2351 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002352 for (i = 0; i < num_types; i++) {
2353 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002354
Dan Rosenberg51788b12011-02-14 16:04:23 -05002355 if (!slot_count)
2356 break;
2357
Josef Bacikbf5fc092010-09-29 11:22:36 -04002358 info = NULL;
2359 rcu_read_lock();
2360 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2361 list) {
2362 if (tmp->flags == types[i]) {
2363 info = tmp;
2364 break;
2365 }
2366 }
2367 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002368
Josef Bacikbf5fc092010-09-29 11:22:36 -04002369 if (!info)
2370 continue;
2371 down_read(&info->groups_sem);
2372 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2373 if (!list_empty(&info->block_groups[c])) {
2374 get_block_group_info(&info->block_groups[c],
2375 &space);
2376 memcpy(dest, &space, sizeof(space));
2377 dest++;
2378 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002379 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002380 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002381 if (!slot_count)
2382 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002383 }
2384 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002385 }
Josef Bacik1406e432010-01-13 18:19:06 +00002386
Chris Mason7fde62b2010-03-16 15:40:10 -04002387 user_dest = (struct btrfs_ioctl_space_info *)
2388 (arg + sizeof(struct btrfs_ioctl_space_args));
2389
2390 if (copy_to_user(user_dest, dest_orig, alloc_size))
2391 ret = -EFAULT;
2392
2393 kfree(dest_orig);
2394out:
2395 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002396 ret = -EFAULT;
2397
2398 return ret;
2399}
2400
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002401/*
2402 * there are many ways the trans_start and trans_end ioctls can lead
2403 * to deadlocks. They should only be used by applications that
2404 * basically own the machine, and have a very in depth understanding
2405 * of all the possible deadlocks and enospc problems.
2406 */
2407long btrfs_ioctl_trans_end(struct file *file)
2408{
2409 struct inode *inode = fdentry(file)->d_inode;
2410 struct btrfs_root *root = BTRFS_I(inode)->root;
2411 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002412
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002413 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002414 if (!trans)
2415 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002416 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002417
Sage Weil1ab86ae2009-09-29 18:38:44 -04002418 btrfs_end_transaction(trans, root);
2419
Sage Weil9ca9ee02008-08-04 10:41:27 -04002420 mutex_lock(&root->fs_info->trans_mutex);
2421 root->fs_info->open_ioctl_trans--;
2422 mutex_unlock(&root->fs_info->trans_mutex);
2423
Sage Weilcfc8ea82008-12-11 16:30:06 -05002424 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002425 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002426}
2427
Sage Weil46204592010-10-29 15:41:32 -04002428static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2429{
2430 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2431 struct btrfs_trans_handle *trans;
2432 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002433 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002434
2435 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002436 if (IS_ERR(trans))
2437 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002438 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002439 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002440 if (ret) {
2441 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002442 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002443 }
Sage Weil46204592010-10-29 15:41:32 -04002444
2445 if (argp)
2446 if (copy_to_user(argp, &transid, sizeof(transid)))
2447 return -EFAULT;
2448 return 0;
2449}
2450
2451static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2452{
2453 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2454 u64 transid;
2455
2456 if (argp) {
2457 if (copy_from_user(&transid, argp, sizeof(transid)))
2458 return -EFAULT;
2459 } else {
2460 transid = 0; /* current trans */
2461 }
2462 return btrfs_wait_for_commit(root, transid);
2463}
2464
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002465long btrfs_ioctl(struct file *file, unsigned int
2466 cmd, unsigned long arg)
2467{
2468 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002469 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002470
2471 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002472 case FS_IOC_GETFLAGS:
2473 return btrfs_ioctl_getflags(file, argp);
2474 case FS_IOC_SETFLAGS:
2475 return btrfs_ioctl_setflags(file, argp);
2476 case FS_IOC_GETVERSION:
2477 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00002478 case FITRIM:
2479 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002480 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002481 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002482 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002483 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002484 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002485 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002486 case BTRFS_IOC_SNAP_DESTROY:
2487 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002488 case BTRFS_IOC_SUBVOL_GETFLAGS:
2489 return btrfs_ioctl_subvol_getflags(file, argp);
2490 case BTRFS_IOC_SUBVOL_SETFLAGS:
2491 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002492 case BTRFS_IOC_DEFAULT_SUBVOL:
2493 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002494 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002495 return btrfs_ioctl_defrag(file, NULL);
2496 case BTRFS_IOC_DEFRAG_RANGE:
2497 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002498 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002499 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002500 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002501 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002502 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002503 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002504 case BTRFS_IOC_BALANCE:
2505 return btrfs_balance(root->fs_info->dev_root);
2506 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002507 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2508 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002509 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002510 case BTRFS_IOC_TRANS_START:
2511 return btrfs_ioctl_trans_start(file);
2512 case BTRFS_IOC_TRANS_END:
2513 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002514 case BTRFS_IOC_TREE_SEARCH:
2515 return btrfs_ioctl_tree_search(file, argp);
2516 case BTRFS_IOC_INO_LOOKUP:
2517 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002518 case BTRFS_IOC_SPACE_INFO:
2519 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002520 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002521 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2522 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002523 case BTRFS_IOC_START_SYNC:
2524 return btrfs_ioctl_start_sync(file, argp);
2525 case BTRFS_IOC_WAIT_SYNC:
2526 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002527 }
2528
2529 return -ENOTTY;
2530}