blob: f580a3a5d2fc86ab22eabee482985140553b6c6a [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#include <linux/swap.h>
35#include <linux/writeback.h>
36#include <linux/statfs.h>
37#include <linux/compat.h>
38#include <linux/bit_spinlock.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Li Dongyangf7039b12011-03-24 10:24:28 +000043#include <linux/blkdev.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050044#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
49#include "ioctl.h"
50#include "print-tree.h"
51#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040052#include "locking.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040053
Christoph Hellwig6cbff002009-04-17 10:37:41 +020054/* Mask out flags that are inappropriate for the given type of inode. */
55static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
56{
57 if (S_ISDIR(mode))
58 return flags;
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
61 else
62 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
63}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040064
Christoph Hellwig6cbff002009-04-17 10:37:41 +020065/*
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 */
68static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69{
70 unsigned int iflags = 0;
71
72 if (flags & BTRFS_INODE_SYNC)
73 iflags |= FS_SYNC_FL;
74 if (flags & BTRFS_INODE_IMMUTABLE)
75 iflags |= FS_IMMUTABLE_FL;
76 if (flags & BTRFS_INODE_APPEND)
77 iflags |= FS_APPEND_FL;
78 if (flags & BTRFS_INODE_NODUMP)
79 iflags |= FS_NODUMP_FL;
80 if (flags & BTRFS_INODE_NOATIME)
81 iflags |= FS_NOATIME_FL;
82 if (flags & BTRFS_INODE_DIRSYNC)
83 iflags |= FS_DIRSYNC_FL;
84
85 return iflags;
86}
87
88/*
89 * Update inode->i_flags based on the btrfs internal flags.
90 */
91void btrfs_update_iflags(struct inode *inode)
92{
93 struct btrfs_inode *ip = BTRFS_I(inode);
94
95 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96
97 if (ip->flags & BTRFS_INODE_SYNC)
98 inode->i_flags |= S_SYNC;
99 if (ip->flags & BTRFS_INODE_IMMUTABLE)
100 inode->i_flags |= S_IMMUTABLE;
101 if (ip->flags & BTRFS_INODE_APPEND)
102 inode->i_flags |= S_APPEND;
103 if (ip->flags & BTRFS_INODE_NOATIME)
104 inode->i_flags |= S_NOATIME;
105 if (ip->flags & BTRFS_INODE_DIRSYNC)
106 inode->i_flags |= S_DIRSYNC;
107}
108
109/*
110 * Inherit flags from the parent inode.
111 *
112 * Unlike extN we don't have any flags we don't want to inherit currently.
113 */
114void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400116 unsigned int flags;
117
118 if (!dir)
119 return;
120
121 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122
123 if (S_ISREG(inode->i_mode))
124 flags &= ~BTRFS_INODE_DIRSYNC;
125 else if (!S_ISDIR(inode->i_mode))
126 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127
128 BTRFS_I(inode)->flags = flags;
129 btrfs_update_iflags(inode);
130}
131
132static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133{
134 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
135 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136
137 if (copy_to_user(arg, &flags, sizeof(flags)))
138 return -EFAULT;
139 return 0;
140}
141
Liu Bo75e7cb72011-03-22 10:12:20 +0000142static int check_flags(unsigned int flags)
143{
144 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
145 FS_NOATIME_FL | FS_NODUMP_FL | \
146 FS_SYNC_FL | FS_DIRSYNC_FL | \
147 FS_NOCOMP_FL | FS_COMPR_FL | \
148 FS_NOCOW_FL | FS_COW_FL))
149 return -EOPNOTSUPP;
150
151 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
152 return -EINVAL;
153
154 if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL))
155 return -EINVAL;
156
157 return 0;
158}
159
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200160static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
161{
162 struct inode *inode = file->f_path.dentry->d_inode;
163 struct btrfs_inode *ip = BTRFS_I(inode);
164 struct btrfs_root *root = ip->root;
165 struct btrfs_trans_handle *trans;
166 unsigned int flags, oldflags;
167 int ret;
168
Li Zefanb83cc962010-12-20 16:04:08 +0800169 if (btrfs_root_readonly(root))
170 return -EROFS;
171
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200172 if (copy_from_user(&flags, arg, sizeof(flags)))
173 return -EFAULT;
174
Liu Bo75e7cb72011-03-22 10:12:20 +0000175 ret = check_flags(flags);
176 if (ret)
177 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200178
179 if (!is_owner_or_cap(inode))
180 return -EACCES;
181
182 mutex_lock(&inode->i_mutex);
183
184 flags = btrfs_mask_flags(inode->i_mode, flags);
185 oldflags = btrfs_flags_to_ioctl(ip->flags);
186 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
187 if (!capable(CAP_LINUX_IMMUTABLE)) {
188 ret = -EPERM;
189 goto out_unlock;
190 }
191 }
192
193 ret = mnt_want_write(file->f_path.mnt);
194 if (ret)
195 goto out_unlock;
196
197 if (flags & FS_SYNC_FL)
198 ip->flags |= BTRFS_INODE_SYNC;
199 else
200 ip->flags &= ~BTRFS_INODE_SYNC;
201 if (flags & FS_IMMUTABLE_FL)
202 ip->flags |= BTRFS_INODE_IMMUTABLE;
203 else
204 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
205 if (flags & FS_APPEND_FL)
206 ip->flags |= BTRFS_INODE_APPEND;
207 else
208 ip->flags &= ~BTRFS_INODE_APPEND;
209 if (flags & FS_NODUMP_FL)
210 ip->flags |= BTRFS_INODE_NODUMP;
211 else
212 ip->flags &= ~BTRFS_INODE_NODUMP;
213 if (flags & FS_NOATIME_FL)
214 ip->flags |= BTRFS_INODE_NOATIME;
215 else
216 ip->flags &= ~BTRFS_INODE_NOATIME;
217 if (flags & FS_DIRSYNC_FL)
218 ip->flags |= BTRFS_INODE_DIRSYNC;
219 else
220 ip->flags &= ~BTRFS_INODE_DIRSYNC;
221
Liu Bo75e7cb72011-03-22 10:12:20 +0000222 /*
223 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
224 * flag may be changed automatically if compression code won't make
225 * things smaller.
226 */
227 if (flags & FS_NOCOMP_FL) {
228 ip->flags &= ~BTRFS_INODE_COMPRESS;
229 ip->flags |= BTRFS_INODE_NOCOMPRESS;
230 } else if (flags & FS_COMPR_FL) {
231 ip->flags |= BTRFS_INODE_COMPRESS;
232 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
233 }
234 if (flags & FS_NOCOW_FL)
235 ip->flags |= BTRFS_INODE_NODATACOW;
236 else if (flags & FS_COW_FL)
237 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238
239 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000240 BUG_ON(IS_ERR(trans));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241
242 ret = btrfs_update_inode(trans, root, inode);
243 BUG_ON(ret);
244
245 btrfs_update_iflags(inode);
246 inode->i_ctime = CURRENT_TIME;
247 btrfs_end_transaction(trans, root);
248
249 mnt_drop_write(file->f_path.mnt);
liubo2d4e6f62011-02-24 09:38:16 +0000250
251 ret = 0;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200252 out_unlock:
253 mutex_unlock(&inode->i_mutex);
liubo2d4e6f62011-02-24 09:38:16 +0000254 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200255}
256
257static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
258{
259 struct inode *inode = file->f_path.dentry->d_inode;
260
261 return put_user(inode->i_generation, arg);
262}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400263
Li Dongyangf7039b12011-03-24 10:24:28 +0000264static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
265{
266 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
267 struct btrfs_fs_info *fs_info = root->fs_info;
268 struct btrfs_device *device;
269 struct request_queue *q;
270 struct fstrim_range range;
271 u64 minlen = ULLONG_MAX;
272 u64 num_devices = 0;
273 int ret;
274
275 if (!capable(CAP_SYS_ADMIN))
276 return -EPERM;
277
278 mutex_lock(&fs_info->fs_devices->device_list_mutex);
279 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
280 if (!device->bdev)
281 continue;
282 q = bdev_get_queue(device->bdev);
283 if (blk_queue_discard(q)) {
284 num_devices++;
285 minlen = min((u64)q->limits.discard_granularity,
286 minlen);
287 }
288 }
289 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
290 if (!num_devices)
291 return -EOPNOTSUPP;
292
293 if (copy_from_user(&range, arg, sizeof(range)))
294 return -EFAULT;
295
296 range.minlen = max(range.minlen, minlen);
297 ret = btrfs_trim_fs(root, &range);
298 if (ret < 0)
299 return ret;
300
301 if (copy_to_user(arg, &range, sizeof(range)))
302 return -EFAULT;
303
304 return 0;
305}
306
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400307static noinline int create_subvol(struct btrfs_root *root,
308 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400309 char *name, int namelen,
310 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400311{
312 struct btrfs_trans_handle *trans;
313 struct btrfs_key key;
314 struct btrfs_root_item root_item;
315 struct btrfs_inode_item *inode_item;
316 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400317 struct btrfs_root *new_root;
Josef Bacik6a912212010-11-20 09:48:00 +0000318 struct dentry *parent = dget_parent(dentry);
319 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400320 int ret;
321 int err;
322 u64 objectid;
323 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500324 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400325
Yan, Zhenga22285a2010-05-16 10:48:46 -0400326 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
327 0, &objectid);
Josef Bacik6a912212010-11-20 09:48:00 +0000328 if (ret) {
329 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400330 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000331 }
332
333 dir = parent->d_inode;
334
Josef Bacik9ed74f22009-09-11 16:12:44 -0400335 /*
336 * 1 - inode item
337 * 2 - refs
338 * 1 - root item
339 * 2 - dir items
340 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400341 trans = btrfs_start_transaction(root, 6);
Josef Bacik6a912212010-11-20 09:48:00 +0000342 if (IS_ERR(trans)) {
343 dput(parent);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400344 return PTR_ERR(trans);
Josef Bacik6a912212010-11-20 09:48:00 +0000345 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400346
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400347 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
348 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400349 if (IS_ERR(leaf)) {
350 ret = PTR_ERR(leaf);
351 goto fail;
352 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400354 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400355 btrfs_set_header_bytenr(leaf, leaf->start);
356 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400357 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400358 btrfs_set_header_owner(leaf, objectid);
359
360 write_extent_buffer(leaf, root->fs_info->fsid,
361 (unsigned long)btrfs_header_fsid(leaf),
362 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400363 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
364 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
365 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400366 btrfs_mark_buffer_dirty(leaf);
367
368 inode_item = &root_item.inode;
369 memset(inode_item, 0, sizeof(*inode_item));
370 inode_item->generation = cpu_to_le64(1);
371 inode_item->size = cpu_to_le64(3);
372 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400373 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400374 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
375
Li Zefan08fe4db2011-03-28 02:01:25 +0000376 root_item.flags = 0;
377 root_item.byte_limit = 0;
378 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
379
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400381 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382 btrfs_set_root_level(&root_item, 0);
383 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000384 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400385 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400386
387 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
388 root_item.drop_level = 0;
389
Chris Mason925baed2008-06-25 16:01:30 -0400390 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400391 free_extent_buffer(leaf);
392 leaf = NULL;
393
394 btrfs_set_root_dirid(&root_item, new_dirid);
395
396 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400397 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
399 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
400 &root_item);
401 if (ret)
402 goto fail;
403
Yan, Zheng76dda932009-09-21 16:00:26 -0400404 key.offset = (u64)-1;
405 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
406 BUG_ON(IS_ERR(new_root));
407
408 btrfs_record_root_in_trans(trans, new_root);
409
410 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
411 BTRFS_I(dir)->block_group);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400412 /*
413 * insert the directory item
414 */
Chris Mason3de45862008-11-17 21:02:50 -0500415 ret = btrfs_set_inode_index(dir, &index);
416 BUG_ON(ret);
417
418 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400419 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500420 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 if (ret)
422 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500423
Yan Zheng52c26172009-01-05 15:43:43 -0500424 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
425 ret = btrfs_update_inode(trans, root, dir);
426 BUG_ON(ret);
427
Chris Mason0660b5a2008-11-17 20:37:39 -0500428 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400429 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500430 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400431
Chris Mason0660b5a2008-11-17 20:37:39 -0500432 BUG_ON(ret);
433
Yan, Zheng76dda932009-09-21 16:00:26 -0400434 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400435fail:
Josef Bacik6a912212010-11-20 09:48:00 +0000436 dput(parent);
Sage Weil72fd0322010-10-29 15:41:32 -0400437 if (async_transid) {
438 *async_transid = trans->transid;
439 err = btrfs_commit_transaction_async(trans, root, 1);
440 } else {
441 err = btrfs_commit_transaction(trans, root);
442 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 if (err && !ret)
444 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400445 return ret;
446}
447
Sage Weil72fd0322010-10-29 15:41:32 -0400448static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800449 char *name, int namelen, u64 *async_transid,
450 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400451{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000452 struct inode *inode;
Josef Bacik6a912212010-11-20 09:48:00 +0000453 struct dentry *parent;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400454 struct btrfs_pending_snapshot *pending_snapshot;
455 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000456 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457
458 if (!root->ref_cows)
459 return -EINVAL;
460
Yan, Zhenga22285a2010-05-16 10:48:46 -0400461 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
462 if (!pending_snapshot)
463 return -ENOMEM;
464
465 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
466 pending_snapshot->dentry = dentry;
467 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800468 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400469
470 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
471 if (IS_ERR(trans)) {
472 ret = PTR_ERR(trans);
473 goto fail;
474 }
475
476 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
477 BUG_ON(ret);
478
479 list_add(&pending_snapshot->list,
480 &trans->transaction->pending_snapshots);
Sage Weil72fd0322010-10-29 15:41:32 -0400481 if (async_transid) {
482 *async_transid = trans->transid;
483 ret = btrfs_commit_transaction_async(trans,
484 root->fs_info->extent_root, 1);
485 } else {
486 ret = btrfs_commit_transaction(trans,
487 root->fs_info->extent_root);
488 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400489 BUG_ON(ret);
490
491 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400492 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000493 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400494
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500495 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
496 if (ret)
497 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400498
Josef Bacik6a912212010-11-20 09:48:00 +0000499 parent = dget_parent(dentry);
500 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
501 dput(parent);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000502 if (IS_ERR(inode)) {
503 ret = PTR_ERR(inode);
504 goto fail;
505 }
506 BUG_ON(!inode);
507 d_instantiate(dentry, inode);
508 ret = 0;
509fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400510 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400511 return ret;
512}
513
Sage Weil4260f7c2010-10-29 15:46:43 -0400514/* copy of check_sticky in fs/namei.c()
515* It's inline, so penalty for filesystems that don't use sticky bit is
516* minimal.
517*/
518static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
519{
520 uid_t fsuid = current_fsuid();
521
522 if (!(dir->i_mode & S_ISVTX))
523 return 0;
524 if (inode->i_uid == fsuid)
525 return 0;
526 if (dir->i_uid == fsuid)
527 return 0;
528 return !capable(CAP_FOWNER);
529}
530
531/* copy of may_delete in fs/namei.c()
532 * Check whether we can remove a link victim from directory dir, check
533 * whether the type of victim is right.
534 * 1. We can't do it if dir is read-only (done in permission())
535 * 2. We should have write and exec permissions on dir
536 * 3. We can't remove anything from append-only dir
537 * 4. We can't do anything with immutable dir (done in permission())
538 * 5. If the sticky bit on dir is set we should either
539 * a. be owner of dir, or
540 * b. be owner of victim, or
541 * c. have CAP_FOWNER capability
542 * 6. If the victim is append-only or immutable we can't do antyhing with
543 * links pointing to it.
544 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
545 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
546 * 9. We can't remove a root or mountpoint.
547 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
548 * nfs_async_unlink().
549 */
550
551static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
552{
553 int error;
554
555 if (!victim->d_inode)
556 return -ENOENT;
557
558 BUG_ON(victim->d_parent->d_inode != dir);
559 audit_inode_child(victim, dir);
560
561 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
562 if (error)
563 return error;
564 if (IS_APPEND(dir))
565 return -EPERM;
566 if (btrfs_check_sticky(dir, victim->d_inode)||
567 IS_APPEND(victim->d_inode)||
568 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
569 return -EPERM;
570 if (isdir) {
571 if (!S_ISDIR(victim->d_inode->i_mode))
572 return -ENOTDIR;
573 if (IS_ROOT(victim))
574 return -EBUSY;
575 } else if (S_ISDIR(victim->d_inode->i_mode))
576 return -EISDIR;
577 if (IS_DEADDIR(dir))
578 return -ENOENT;
579 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
580 return -EBUSY;
581 return 0;
582}
583
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400584/* copy of may_create in fs/namei.c() */
585static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
586{
587 if (child->d_inode)
588 return -EEXIST;
589 if (IS_DEADDIR(dir))
590 return -ENOENT;
591 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
592}
593
594/*
595 * Create a new subvolume below @parent. This is largely modeled after
596 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
597 * inside this filesystem so it's quite a bit simpler.
598 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400599static noinline int btrfs_mksubvol(struct path *parent,
600 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400601 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800602 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400603{
Yan, Zheng76dda932009-09-21 16:00:26 -0400604 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400605 struct dentry *dentry;
606 int error;
607
Yan, Zheng76dda932009-09-21 16:00:26 -0400608 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400609
610 dentry = lookup_one_len(name, parent->dentry, namelen);
611 error = PTR_ERR(dentry);
612 if (IS_ERR(dentry))
613 goto out_unlock;
614
615 error = -EEXIST;
616 if (dentry->d_inode)
617 goto out_dput;
618
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400619 error = mnt_want_write(parent->mnt);
620 if (error)
621 goto out_dput;
622
Yan, Zheng76dda932009-09-21 16:00:26 -0400623 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400624 if (error)
625 goto out_drop_write;
626
Yan, Zheng76dda932009-09-21 16:00:26 -0400627 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
628
629 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
630 goto out_up_read;
631
Chris Mason3de45862008-11-17 21:02:50 -0500632 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400633 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800634 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500635 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400636 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400637 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500638 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400639 if (!error)
640 fsnotify_mkdir(dir, dentry);
641out_up_read:
642 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400643out_drop_write:
644 mnt_drop_write(parent->mnt);
645out_dput:
646 dput(dentry);
647out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400648 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400649 return error;
650}
651
Chris Mason940100a2010-03-10 10:52:59 -0500652static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500653 int thresh, u64 *last_len, u64 *skip,
654 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500655{
656 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
657 struct extent_map *em = NULL;
658 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
659 int ret = 1;
660
Chris Mason1e701a32010-03-11 09:42:04 -0500661
662 if (thresh == 0)
663 thresh = 256 * 1024;
664
Chris Mason940100a2010-03-10 10:52:59 -0500665 /*
666 * make sure that once we start defragging and extent, we keep on
667 * defragging it
668 */
669 if (start < *defrag_end)
670 return 1;
671
672 *skip = 0;
673
674 /*
675 * hopefully we have this extent in the tree already, try without
676 * the full extent lock
677 */
678 read_lock(&em_tree->lock);
679 em = lookup_extent_mapping(em_tree, start, len);
680 read_unlock(&em_tree->lock);
681
682 if (!em) {
683 /* get the big lock and read metadata off disk */
684 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
685 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
686 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
687
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000688 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500689 return 0;
690 }
691
692 /* this will cover holes, and inline extents */
693 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
694 ret = 0;
695
696 /*
697 * we hit a real extent, if it is big don't bother defragging it again
698 */
Chris Mason1e701a32010-03-11 09:42:04 -0500699 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500700 ret = 0;
701
702 /*
703 * last_len ends up being a counter of how many bytes we've defragged.
704 * every time we choose not to defrag an extent, we reset *last_len
705 * so that the next tiny extent will force a defrag.
706 *
707 * The end result of this is that tiny extents before a single big
708 * extent will force at least part of that big extent to be defragged.
709 */
710 if (ret) {
711 *last_len += len;
712 *defrag_end = extent_map_end(em);
713 } else {
714 *last_len = 0;
715 *skip = extent_map_end(em);
716 *defrag_end = 0;
717 }
718
719 free_extent_map(em);
720 return ret;
721}
722
Chris Mason1e701a32010-03-11 09:42:04 -0500723static int btrfs_defrag_file(struct file *file,
724 struct btrfs_ioctl_defrag_range_args *range)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400725{
726 struct inode *inode = fdentry(file)->d_inode;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
728 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400729 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400730 struct page *page;
Li Zefan1a419d82010-10-25 15:12:50 +0800731 struct btrfs_super_block *disk_super;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400732 unsigned long last_index;
733 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
734 unsigned long total_read = 0;
Li Zefan1a419d82010-10-25 15:12:50 +0800735 u64 features;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400736 u64 page_start;
737 u64 page_end;
Chris Mason940100a2010-03-10 10:52:59 -0500738 u64 last_len = 0;
739 u64 skip = 0;
740 u64 defrag_end = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400741 unsigned long i;
742 int ret;
Li Zefan1a419d82010-10-25 15:12:50 +0800743 int compress_type = BTRFS_COMPRESS_ZLIB;
744
745 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
746 if (range->compress_type > BTRFS_COMPRESS_TYPES)
747 return -EINVAL;
748 if (range->compress_type)
749 compress_type = range->compress_type;
750 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400751
Chris Mason940100a2010-03-10 10:52:59 -0500752 if (inode->i_size == 0)
753 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400754
Chris Mason1e701a32010-03-11 09:42:04 -0500755 if (range->start + range->len > range->start) {
756 last_index = min_t(u64, inode->i_size - 1,
757 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
758 } else {
759 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
760 }
761
762 i = range->start >> PAGE_CACHE_SHIFT;
Chris Mason940100a2010-03-10 10:52:59 -0500763 while (i <= last_index) {
764 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -0500765 PAGE_CACHE_SIZE,
766 range->extent_thresh,
767 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -0500768 &defrag_end)) {
769 unsigned long next;
770 /*
771 * the should_defrag function tells us how much to skip
772 * bump our counter by the suggested amount
773 */
774 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
775 i = max(i + 1, next);
776 continue;
777 }
778
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400779 if (total_read % ra_pages == 0) {
780 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
781 min(last_index, i + ra_pages - 1));
782 }
783 total_read++;
Chris Mason940100a2010-03-10 10:52:59 -0500784 mutex_lock(&inode->i_mutex);
Chris Mason1e701a32010-03-11 09:42:04 -0500785 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +0800786 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -0500787
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400788 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
789 if (ret)
790 goto err_unlock;
Chris Mason3eaa2882008-07-24 11:57:52 -0400791again:
Chris Mason940100a2010-03-10 10:52:59 -0500792 if (inode->i_size == 0 ||
793 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
794 ret = 0;
795 goto err_reservations;
796 }
797
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400798 page = grab_cache_page(inode->i_mapping, i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400799 if (!page) {
800 ret = -ENOMEM;
Chris Mason940100a2010-03-10 10:52:59 -0500801 goto err_reservations;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400802 }
Chris Mason940100a2010-03-10 10:52:59 -0500803
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400804 if (!PageUptodate(page)) {
805 btrfs_readpage(NULL, page);
806 lock_page(page);
807 if (!PageUptodate(page)) {
808 unlock_page(page);
809 page_cache_release(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400810 ret = -EIO;
Chris Mason940100a2010-03-10 10:52:59 -0500811 goto err_reservations;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400812 }
813 }
814
Chris Mason940100a2010-03-10 10:52:59 -0500815 if (page->mapping != inode->i_mapping) {
816 unlock_page(page);
817 page_cache_release(page);
818 goto again;
819 }
820
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400821 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400822
Chris Mason940100a2010-03-10 10:52:59 -0500823 if (PageDirty(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400824 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason940100a2010-03-10 10:52:59 -0500825 goto loop_unlock;
826 }
827
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400828 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
829 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400830 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400831
832 ordered = btrfs_lookup_ordered_extent(inode, page_start);
833 if (ordered) {
834 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
835 unlock_page(page);
836 page_cache_release(page);
837 btrfs_start_ordered_extent(inode, ordered, 1);
838 btrfs_put_ordered_extent(ordered);
839 goto again;
840 }
841 set_page_extent_mapped(page);
842
Chris Masonf87f0572008-08-01 11:27:23 -0400843 /*
844 * this makes sure page_mkwrite is called on the
845 * page if it is dirtied again later
846 */
847 clear_page_dirty_for_io(page);
Chris Mason940100a2010-03-10 10:52:59 -0500848 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
849 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
850 EXTENT_DO_ACCOUNTING, GFP_NOFS);
Chris Masonf87f0572008-08-01 11:27:23 -0400851
Josef Bacik2ac55d42010-02-03 19:33:23 +0000852 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
Chris Mason940100a2010-03-10 10:52:59 -0500853 ClearPageChecked(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400854 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400855 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason940100a2010-03-10 10:52:59 -0500856
857loop_unlock:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 unlock_page(page);
859 page_cache_release(page);
Chris Mason940100a2010-03-10 10:52:59 -0500860 mutex_unlock(&inode->i_mutex);
861
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400862 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Mason940100a2010-03-10 10:52:59 -0500863 i++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400864 }
865
Chris Mason1e701a32010-03-11 09:42:04 -0500866 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
867 filemap_flush(inode->i_mapping);
868
869 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
870 /* the filemap_flush will queue IO into the worker threads, but
871 * we have to make sure the IO is actually started and that
872 * ordered extents get created before we return
873 */
874 atomic_inc(&root->fs_info->async_submit_draining);
875 while (atomic_read(&root->fs_info->nr_async_submits) ||
876 atomic_read(&root->fs_info->async_delalloc_pages)) {
877 wait_event(root->fs_info->async_submit_wait,
878 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
879 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
880 }
881 atomic_dec(&root->fs_info->async_submit_draining);
882
883 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +0800884 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -0500885 mutex_unlock(&inode->i_mutex);
886 }
887
Li Zefan1a419d82010-10-25 15:12:50 +0800888 disk_super = &root->fs_info->super_copy;
889 features = btrfs_super_incompat_flags(disk_super);
890 if (range->compress_type == BTRFS_COMPRESS_LZO) {
891 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
892 btrfs_set_super_incompat_flags(disk_super, features);
893 }
894
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400895 return 0;
Chris Mason940100a2010-03-10 10:52:59 -0500896
897err_reservations:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400898 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
899err_unlock:
Chris Mason940100a2010-03-10 10:52:59 -0500900 mutex_unlock(&inode->i_mutex);
Chris Mason940100a2010-03-10 10:52:59 -0500901 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400902}
903
Yan, Zheng76dda932009-09-21 16:00:26 -0400904static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
905 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400906{
907 u64 new_size;
908 u64 old_size;
909 u64 devid = 1;
910 struct btrfs_ioctl_vol_args *vol_args;
911 struct btrfs_trans_handle *trans;
912 struct btrfs_device *device = NULL;
913 char *sizestr;
914 char *devstr = NULL;
915 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400916 int mod = 0;
917
Yan Zhengc146afa2008-11-12 14:34:12 -0500918 if (root->fs_info->sb->s_flags & MS_RDONLY)
919 return -EROFS;
920
Chris Masone441d542009-01-05 16:57:23 -0500921 if (!capable(CAP_SYS_ADMIN))
922 return -EPERM;
923
Li Zefandae7b662009-04-08 15:06:54 +0800924 vol_args = memdup_user(arg, sizeof(*vol_args));
925 if (IS_ERR(vol_args))
926 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400927
928 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400929
Chris Mason7d9eb122008-07-08 14:19:17 -0400930 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400931 sizestr = vol_args->name;
932 devstr = strchr(sizestr, ':');
933 if (devstr) {
934 char *end;
935 sizestr = devstr + 1;
936 *devstr = '\0';
937 devstr = vol_args->name;
938 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700939 printk(KERN_INFO "resizing devid %llu\n",
940 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400941 }
Yan Zheng2b820322008-11-17 21:11:30 -0500942 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400943 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700944 printk(KERN_INFO "resizer unable to find device %llu\n",
945 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400946 ret = -EINVAL;
947 goto out_unlock;
948 }
949 if (!strcmp(sizestr, "max"))
950 new_size = device->bdev->bd_inode->i_size;
951 else {
952 if (sizestr[0] == '-') {
953 mod = -1;
954 sizestr++;
955 } else if (sizestr[0] == '+') {
956 mod = 1;
957 sizestr++;
958 }
Akinobu Mita91748462010-02-28 10:59:11 +0000959 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400960 if (new_size == 0) {
961 ret = -EINVAL;
962 goto out_unlock;
963 }
964 }
965
966 old_size = device->total_bytes;
967
968 if (mod < 0) {
969 if (new_size > old_size) {
970 ret = -EINVAL;
971 goto out_unlock;
972 }
973 new_size = old_size - new_size;
974 } else if (mod > 0) {
975 new_size = old_size + new_size;
976 }
977
978 if (new_size < 256 * 1024 * 1024) {
979 ret = -EINVAL;
980 goto out_unlock;
981 }
982 if (new_size > device->bdev->bd_inode->i_size) {
983 ret = -EFBIG;
984 goto out_unlock;
985 }
986
987 do_div(new_size, root->sectorsize);
988 new_size *= root->sectorsize;
989
990 printk(KERN_INFO "new size for %s is %llu\n",
991 device->name, (unsigned long long)new_size);
992
993 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400994 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +0000995 if (IS_ERR(trans)) {
996 ret = PTR_ERR(trans);
997 goto out_unlock;
998 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400999 ret = btrfs_grow_device(trans, device, new_size);
1000 btrfs_commit_transaction(trans, root);
1001 } else {
1002 ret = btrfs_shrink_device(device, new_size);
1003 }
1004
1005out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001006 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001007 kfree(vol_args);
1008 return ret;
1009}
1010
Sage Weil72fd0322010-10-29 15:41:32 -04001011static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1012 char *name,
1013 unsigned long fd,
1014 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001015 u64 *transid,
1016 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001017{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001018 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001019 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001020 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001021 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001022
Yan Zhengc146afa2008-11-12 14:34:12 -05001023 if (root->fs_info->sb->s_flags & MS_RDONLY)
1024 return -EROFS;
1025
Sage Weil72fd0322010-10-29 15:41:32 -04001026 namelen = strlen(name);
1027 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001028 ret = -EINVAL;
1029 goto out;
1030 }
1031
Chris Mason3de45862008-11-17 21:02:50 -05001032 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001033 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001034 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001035 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001036 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001037 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001038 if (!src_file) {
1039 ret = -EINVAL;
1040 goto out;
1041 }
1042
1043 src_inode = src_file->f_path.dentry->d_inode;
1044 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001045 printk(KERN_INFO "btrfs: Snapshot src from "
1046 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001047 ret = -EINVAL;
1048 fput(src_file);
1049 goto out;
1050 }
Sage Weil72fd0322010-10-29 15:41:32 -04001051 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1052 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001053 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001054 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001055 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001056out:
Sage Weil72fd0322010-10-29 15:41:32 -04001057 return ret;
1058}
1059
1060static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001061 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001062{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001063 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001064 int ret;
1065
Li Zefanfa0d2b92010-12-20 15:53:28 +08001066 vol_args = memdup_user(arg, sizeof(*vol_args));
1067 if (IS_ERR(vol_args))
1068 return PTR_ERR(vol_args);
1069 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001070
Li Zefanfa0d2b92010-12-20 15:53:28 +08001071 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001072 vol_args->fd, subvol,
1073 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001074
Li Zefanfa0d2b92010-12-20 15:53:28 +08001075 kfree(vol_args);
1076 return ret;
1077}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001078
Li Zefanfa0d2b92010-12-20 15:53:28 +08001079static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1080 void __user *arg, int subvol)
1081{
1082 struct btrfs_ioctl_vol_args_v2 *vol_args;
1083 int ret;
1084 u64 transid = 0;
1085 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001086 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001087
Li Zefanfa0d2b92010-12-20 15:53:28 +08001088 vol_args = memdup_user(arg, sizeof(*vol_args));
1089 if (IS_ERR(vol_args))
1090 return PTR_ERR(vol_args);
1091 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001092
Li Zefanb83cc962010-12-20 16:04:08 +08001093 if (vol_args->flags &
1094 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1095 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001096 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001097 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001098
1099 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1100 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001101 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1102 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001103
1104 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001105 vol_args->fd, subvol,
1106 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001107
1108 if (ret == 0 && ptr &&
1109 copy_to_user(arg +
1110 offsetof(struct btrfs_ioctl_vol_args_v2,
1111 transid), ptr, sizeof(*ptr)))
1112 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001113out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001114 kfree(vol_args);
1115 return ret;
1116}
1117
Li Zefan0caa1022010-12-20 16:30:25 +08001118static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1119 void __user *arg)
1120{
1121 struct inode *inode = fdentry(file)->d_inode;
1122 struct btrfs_root *root = BTRFS_I(inode)->root;
1123 int ret = 0;
1124 u64 flags = 0;
1125
1126 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1127 return -EINVAL;
1128
1129 down_read(&root->fs_info->subvol_sem);
1130 if (btrfs_root_readonly(root))
1131 flags |= BTRFS_SUBVOL_RDONLY;
1132 up_read(&root->fs_info->subvol_sem);
1133
1134 if (copy_to_user(arg, &flags, sizeof(flags)))
1135 ret = -EFAULT;
1136
1137 return ret;
1138}
1139
1140static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1141 void __user *arg)
1142{
1143 struct inode *inode = fdentry(file)->d_inode;
1144 struct btrfs_root *root = BTRFS_I(inode)->root;
1145 struct btrfs_trans_handle *trans;
1146 u64 root_flags;
1147 u64 flags;
1148 int ret = 0;
1149
1150 if (root->fs_info->sb->s_flags & MS_RDONLY)
1151 return -EROFS;
1152
1153 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1154 return -EINVAL;
1155
1156 if (copy_from_user(&flags, arg, sizeof(flags)))
1157 return -EFAULT;
1158
Li Zefanb4dc2b82011-02-16 06:06:34 +00001159 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001160 return -EINVAL;
1161
1162 if (flags & ~BTRFS_SUBVOL_RDONLY)
1163 return -EOPNOTSUPP;
1164
Li Zefanb4dc2b82011-02-16 06:06:34 +00001165 if (!is_owner_or_cap(inode))
1166 return -EACCES;
1167
Li Zefan0caa1022010-12-20 16:30:25 +08001168 down_write(&root->fs_info->subvol_sem);
1169
1170 /* nothing to do */
1171 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1172 goto out;
1173
1174 root_flags = btrfs_root_flags(&root->root_item);
1175 if (flags & BTRFS_SUBVOL_RDONLY)
1176 btrfs_set_root_flags(&root->root_item,
1177 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1178 else
1179 btrfs_set_root_flags(&root->root_item,
1180 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1181
1182 trans = btrfs_start_transaction(root, 1);
1183 if (IS_ERR(trans)) {
1184 ret = PTR_ERR(trans);
1185 goto out_reset;
1186 }
1187
Li Zefanb4dc2b82011-02-16 06:06:34 +00001188 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001189 &root->root_key, &root->root_item);
1190
1191 btrfs_commit_transaction(trans, root);
1192out_reset:
1193 if (ret)
1194 btrfs_set_root_flags(&root->root_item, root_flags);
1195out:
1196 up_write(&root->fs_info->subvol_sem);
1197 return ret;
1198}
1199
Yan, Zheng76dda932009-09-21 16:00:26 -04001200/*
1201 * helper to check if the subvolume references other subvolumes
1202 */
1203static noinline int may_destroy_subvol(struct btrfs_root *root)
1204{
1205 struct btrfs_path *path;
1206 struct btrfs_key key;
1207 int ret;
1208
1209 path = btrfs_alloc_path();
1210 if (!path)
1211 return -ENOMEM;
1212
1213 key.objectid = root->root_key.objectid;
1214 key.type = BTRFS_ROOT_REF_KEY;
1215 key.offset = (u64)-1;
1216
1217 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1218 &key, path, 0, 0);
1219 if (ret < 0)
1220 goto out;
1221 BUG_ON(ret == 0);
1222
1223 ret = 0;
1224 if (path->slots[0] > 0) {
1225 path->slots[0]--;
1226 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1227 if (key.objectid == root->root_key.objectid &&
1228 key.type == BTRFS_ROOT_REF_KEY)
1229 ret = -ENOTEMPTY;
1230 }
1231out:
1232 btrfs_free_path(path);
1233 return ret;
1234}
1235
Chris Masonac8e9812010-02-28 15:39:26 -05001236static noinline int key_in_sk(struct btrfs_key *key,
1237 struct btrfs_ioctl_search_key *sk)
1238{
Chris Masonabc6e132010-03-18 12:10:08 -04001239 struct btrfs_key test;
1240 int ret;
1241
1242 test.objectid = sk->min_objectid;
1243 test.type = sk->min_type;
1244 test.offset = sk->min_offset;
1245
1246 ret = btrfs_comp_cpu_keys(key, &test);
1247 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001248 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001249
1250 test.objectid = sk->max_objectid;
1251 test.type = sk->max_type;
1252 test.offset = sk->max_offset;
1253
1254 ret = btrfs_comp_cpu_keys(key, &test);
1255 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001256 return 0;
1257 return 1;
1258}
1259
1260static noinline int copy_to_sk(struct btrfs_root *root,
1261 struct btrfs_path *path,
1262 struct btrfs_key *key,
1263 struct btrfs_ioctl_search_key *sk,
1264 char *buf,
1265 unsigned long *sk_offset,
1266 int *num_found)
1267{
1268 u64 found_transid;
1269 struct extent_buffer *leaf;
1270 struct btrfs_ioctl_search_header sh;
1271 unsigned long item_off;
1272 unsigned long item_len;
1273 int nritems;
1274 int i;
1275 int slot;
1276 int found = 0;
1277 int ret = 0;
1278
1279 leaf = path->nodes[0];
1280 slot = path->slots[0];
1281 nritems = btrfs_header_nritems(leaf);
1282
1283 if (btrfs_header_generation(leaf) > sk->max_transid) {
1284 i = nritems;
1285 goto advance_key;
1286 }
1287 found_transid = btrfs_header_generation(leaf);
1288
1289 for (i = slot; i < nritems; i++) {
1290 item_off = btrfs_item_ptr_offset(leaf, i);
1291 item_len = btrfs_item_size_nr(leaf, i);
1292
1293 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1294 item_len = 0;
1295
1296 if (sizeof(sh) + item_len + *sk_offset >
1297 BTRFS_SEARCH_ARGS_BUFSIZE) {
1298 ret = 1;
1299 goto overflow;
1300 }
1301
1302 btrfs_item_key_to_cpu(leaf, key, i);
1303 if (!key_in_sk(key, sk))
1304 continue;
1305
1306 sh.objectid = key->objectid;
1307 sh.offset = key->offset;
1308 sh.type = key->type;
1309 sh.len = item_len;
1310 sh.transid = found_transid;
1311
1312 /* copy search result header */
1313 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1314 *sk_offset += sizeof(sh);
1315
1316 if (item_len) {
1317 char *p = buf + *sk_offset;
1318 /* copy the item */
1319 read_extent_buffer(leaf, p,
1320 item_off, item_len);
1321 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001322 }
Chris Mason90fdde12010-03-18 12:14:54 -04001323 found++;
Chris Masonac8e9812010-02-28 15:39:26 -05001324
1325 if (*num_found >= sk->nr_items)
1326 break;
1327 }
1328advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001329 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001330 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1331 key->offset++;
1332 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1333 key->offset = 0;
1334 key->type++;
1335 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1336 key->offset = 0;
1337 key->type = 0;
1338 key->objectid++;
1339 } else
1340 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001341overflow:
1342 *num_found += found;
1343 return ret;
1344}
1345
1346static noinline int search_ioctl(struct inode *inode,
1347 struct btrfs_ioctl_search_args *args)
1348{
1349 struct btrfs_root *root;
1350 struct btrfs_key key;
1351 struct btrfs_key max_key;
1352 struct btrfs_path *path;
1353 struct btrfs_ioctl_search_key *sk = &args->key;
1354 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1355 int ret;
1356 int num_found = 0;
1357 unsigned long sk_offset = 0;
1358
1359 path = btrfs_alloc_path();
1360 if (!path)
1361 return -ENOMEM;
1362
1363 if (sk->tree_id == 0) {
1364 /* search the root of the inode that was passed */
1365 root = BTRFS_I(inode)->root;
1366 } else {
1367 key.objectid = sk->tree_id;
1368 key.type = BTRFS_ROOT_ITEM_KEY;
1369 key.offset = (u64)-1;
1370 root = btrfs_read_fs_root_no_name(info, &key);
1371 if (IS_ERR(root)) {
1372 printk(KERN_ERR "could not find root %llu\n",
1373 sk->tree_id);
1374 btrfs_free_path(path);
1375 return -ENOENT;
1376 }
1377 }
1378
1379 key.objectid = sk->min_objectid;
1380 key.type = sk->min_type;
1381 key.offset = sk->min_offset;
1382
1383 max_key.objectid = sk->max_objectid;
1384 max_key.type = sk->max_type;
1385 max_key.offset = sk->max_offset;
1386
1387 path->keep_locks = 1;
1388
1389 while(1) {
1390 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1391 sk->min_transid);
1392 if (ret != 0) {
1393 if (ret > 0)
1394 ret = 0;
1395 goto err;
1396 }
1397 ret = copy_to_sk(root, path, &key, sk, args->buf,
1398 &sk_offset, &num_found);
1399 btrfs_release_path(root, path);
1400 if (ret || num_found >= sk->nr_items)
1401 break;
1402
1403 }
1404 ret = 0;
1405err:
1406 sk->nr_items = num_found;
1407 btrfs_free_path(path);
1408 return ret;
1409}
1410
1411static noinline int btrfs_ioctl_tree_search(struct file *file,
1412 void __user *argp)
1413{
1414 struct btrfs_ioctl_search_args *args;
1415 struct inode *inode;
1416 int ret;
1417
1418 if (!capable(CAP_SYS_ADMIN))
1419 return -EPERM;
1420
Julia Lawall2354d082010-10-29 15:14:18 -04001421 args = memdup_user(argp, sizeof(*args));
1422 if (IS_ERR(args))
1423 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001424
Chris Masonac8e9812010-02-28 15:39:26 -05001425 inode = fdentry(file)->d_inode;
1426 ret = search_ioctl(inode, args);
1427 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1428 ret = -EFAULT;
1429 kfree(args);
1430 return ret;
1431}
1432
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001433/*
Chris Masonac8e9812010-02-28 15:39:26 -05001434 * Search INODE_REFs to identify path name of 'dirid' directory
1435 * in a 'tree_id' tree. and sets path name to 'name'.
1436 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001437static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1438 u64 tree_id, u64 dirid, char *name)
1439{
1440 struct btrfs_root *root;
1441 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001442 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001443 int ret = -1;
1444 int slot;
1445 int len;
1446 int total_len = 0;
1447 struct btrfs_inode_ref *iref;
1448 struct extent_buffer *l;
1449 struct btrfs_path *path;
1450
1451 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1452 name[0]='\0';
1453 return 0;
1454 }
1455
1456 path = btrfs_alloc_path();
1457 if (!path)
1458 return -ENOMEM;
1459
Chris Masonac8e9812010-02-28 15:39:26 -05001460 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001461
1462 key.objectid = tree_id;
1463 key.type = BTRFS_ROOT_ITEM_KEY;
1464 key.offset = (u64)-1;
1465 root = btrfs_read_fs_root_no_name(info, &key);
1466 if (IS_ERR(root)) {
1467 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001468 ret = -ENOENT;
1469 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001470 }
1471
1472 key.objectid = dirid;
1473 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001474 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001475
1476 while(1) {
1477 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1478 if (ret < 0)
1479 goto out;
1480
1481 l = path->nodes[0];
1482 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001483 if (ret > 0 && slot > 0)
1484 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001485 btrfs_item_key_to_cpu(l, &key, slot);
1486
1487 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001488 key.type != BTRFS_INODE_REF_KEY)) {
1489 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001490 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001491 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001492
1493 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1494 len = btrfs_inode_ref_name_len(l, iref);
1495 ptr -= len + 1;
1496 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001497 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001498 goto out;
1499
1500 *(ptr + len) = '/';
1501 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1502
1503 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1504 break;
1505
1506 btrfs_release_path(root, path);
1507 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001508 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001509 dirid = key.objectid;
1510
1511 }
Chris Masonac8e9812010-02-28 15:39:26 -05001512 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001513 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001514 memcpy(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001515 name[total_len]='\0';
1516 ret = 0;
1517out:
1518 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001519 return ret;
1520}
1521
1522static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1523 void __user *argp)
1524{
1525 struct btrfs_ioctl_ino_lookup_args *args;
1526 struct inode *inode;
1527 int ret;
1528
1529 if (!capable(CAP_SYS_ADMIN))
1530 return -EPERM;
1531
Julia Lawall2354d082010-10-29 15:14:18 -04001532 args = memdup_user(argp, sizeof(*args));
1533 if (IS_ERR(args))
1534 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001535
Chris Masonac8e9812010-02-28 15:39:26 -05001536 inode = fdentry(file)->d_inode;
1537
Chris Mason1b53ac42010-03-18 12:17:05 -04001538 if (args->treeid == 0)
1539 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1540
Chris Masonac8e9812010-02-28 15:39:26 -05001541 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1542 args->treeid, args->objectid,
1543 args->name);
1544
1545 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1546 ret = -EFAULT;
1547
1548 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001549 return ret;
1550}
1551
Yan, Zheng76dda932009-09-21 16:00:26 -04001552static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1553 void __user *arg)
1554{
1555 struct dentry *parent = fdentry(file);
1556 struct dentry *dentry;
1557 struct inode *dir = parent->d_inode;
1558 struct inode *inode;
1559 struct btrfs_root *root = BTRFS_I(dir)->root;
1560 struct btrfs_root *dest = NULL;
1561 struct btrfs_ioctl_vol_args *vol_args;
1562 struct btrfs_trans_handle *trans;
1563 int namelen;
1564 int ret;
1565 int err = 0;
1566
Yan, Zheng76dda932009-09-21 16:00:26 -04001567 vol_args = memdup_user(arg, sizeof(*vol_args));
1568 if (IS_ERR(vol_args))
1569 return PTR_ERR(vol_args);
1570
1571 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1572 namelen = strlen(vol_args->name);
1573 if (strchr(vol_args->name, '/') ||
1574 strncmp(vol_args->name, "..", namelen) == 0) {
1575 err = -EINVAL;
1576 goto out;
1577 }
1578
1579 err = mnt_want_write(file->f_path.mnt);
1580 if (err)
1581 goto out;
1582
1583 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1584 dentry = lookup_one_len(vol_args->name, parent, namelen);
1585 if (IS_ERR(dentry)) {
1586 err = PTR_ERR(dentry);
1587 goto out_unlock_dir;
1588 }
1589
1590 if (!dentry->d_inode) {
1591 err = -ENOENT;
1592 goto out_dput;
1593 }
1594
1595 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001596 dest = BTRFS_I(inode)->root;
1597 if (!capable(CAP_SYS_ADMIN)){
1598 /*
1599 * Regular user. Only allow this with a special mount
1600 * option, when the user has write+exec access to the
1601 * subvol root, and when rmdir(2) would have been
1602 * allowed.
1603 *
1604 * Note that this is _not_ check that the subvol is
1605 * empty or doesn't contain data that we wouldn't
1606 * otherwise be able to delete.
1607 *
1608 * Users who want to delete empty subvols should try
1609 * rmdir(2).
1610 */
1611 err = -EPERM;
1612 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1613 goto out_dput;
1614
1615 /*
1616 * Do not allow deletion if the parent dir is the same
1617 * as the dir to be deleted. That means the ioctl
1618 * must be called on the dentry referencing the root
1619 * of the subvol, not a random directory contained
1620 * within it.
1621 */
1622 err = -EINVAL;
1623 if (root == dest)
1624 goto out_dput;
1625
1626 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1627 if (err)
1628 goto out_dput;
1629
1630 /* check if subvolume may be deleted by a non-root user */
1631 err = btrfs_may_delete(dir, dentry, 1);
1632 if (err)
1633 goto out_dput;
1634 }
1635
Yan, Zheng76dda932009-09-21 16:00:26 -04001636 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1637 err = -EINVAL;
1638 goto out_dput;
1639 }
1640
Yan, Zheng76dda932009-09-21 16:00:26 -04001641 mutex_lock(&inode->i_mutex);
1642 err = d_invalidate(dentry);
1643 if (err)
1644 goto out_unlock;
1645
1646 down_write(&root->fs_info->subvol_sem);
1647
1648 err = may_destroy_subvol(dest);
1649 if (err)
1650 goto out_up_write;
1651
Yan, Zhenga22285a2010-05-16 10:48:46 -04001652 trans = btrfs_start_transaction(root, 0);
1653 if (IS_ERR(trans)) {
1654 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001655 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001656 }
1657 trans->block_rsv = &root->fs_info->global_block_rsv;
1658
Yan, Zheng76dda932009-09-21 16:00:26 -04001659 ret = btrfs_unlink_subvol(trans, root, dir,
1660 dest->root_key.objectid,
1661 dentry->d_name.name,
1662 dentry->d_name.len);
1663 BUG_ON(ret);
1664
1665 btrfs_record_root_in_trans(trans, dest);
1666
1667 memset(&dest->root_item.drop_progress, 0,
1668 sizeof(dest->root_item.drop_progress));
1669 dest->root_item.drop_level = 0;
1670 btrfs_set_root_refs(&dest->root_item, 0);
1671
Yan, Zhengd68fc572010-05-16 10:49:58 -04001672 if (!xchg(&dest->orphan_item_inserted, 1)) {
1673 ret = btrfs_insert_orphan_item(trans,
1674 root->fs_info->tree_root,
1675 dest->root_key.objectid);
1676 BUG_ON(ret);
1677 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001678
Sage Weil531cb132010-10-29 15:41:32 -04001679 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001680 BUG_ON(ret);
1681 inode->i_flags |= S_DEAD;
1682out_up_write:
1683 up_write(&root->fs_info->subvol_sem);
1684out_unlock:
1685 mutex_unlock(&inode->i_mutex);
1686 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001687 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001688 btrfs_invalidate_inodes(dest);
1689 d_delete(dentry);
1690 }
1691out_dput:
1692 dput(dentry);
1693out_unlock_dir:
1694 mutex_unlock(&dir->i_mutex);
1695 mnt_drop_write(file->f_path.mnt);
1696out:
1697 kfree(vol_args);
1698 return err;
1699}
1700
Chris Mason1e701a32010-03-11 09:42:04 -05001701static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001702{
1703 struct inode *inode = fdentry(file)->d_inode;
1704 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001705 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001706 int ret;
1707
Li Zefanb83cc962010-12-20 16:04:08 +08001708 if (btrfs_root_readonly(root))
1709 return -EROFS;
1710
Yan Zhengc146afa2008-11-12 14:34:12 -05001711 ret = mnt_want_write(file->f_path.mnt);
1712 if (ret)
1713 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001714
1715 switch (inode->i_mode & S_IFMT) {
1716 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05001717 if (!capable(CAP_SYS_ADMIN)) {
1718 ret = -EPERM;
1719 goto out;
1720 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001721 ret = btrfs_defrag_root(root, 0);
1722 if (ret)
1723 goto out;
1724 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001725 break;
1726 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05001727 if (!(file->f_mode & FMODE_WRITE)) {
1728 ret = -EINVAL;
1729 goto out;
1730 }
Chris Mason1e701a32010-03-11 09:42:04 -05001731
1732 range = kzalloc(sizeof(*range), GFP_KERNEL);
1733 if (!range) {
1734 ret = -ENOMEM;
1735 goto out;
1736 }
1737
1738 if (argp) {
1739 if (copy_from_user(range, argp,
1740 sizeof(*range))) {
1741 ret = -EFAULT;
1742 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00001743 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05001744 }
1745 /* compression requires us to start the IO */
1746 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1747 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1748 range->extent_thresh = (u32)-1;
1749 }
1750 } else {
1751 /* the rest are all set to zero by kzalloc */
1752 range->len = (u64)-1;
1753 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001754 ret = btrfs_defrag_file(file, range);
Chris Mason1e701a32010-03-11 09:42:04 -05001755 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001756 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04001757 default:
1758 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001759 }
Chris Masone441d542009-01-05 16:57:23 -05001760out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05001761 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05001762 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001763}
1764
Christoph Hellwigb2950862008-12-02 09:54:17 -05001765static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001766{
1767 struct btrfs_ioctl_vol_args *vol_args;
1768 int ret;
1769
Chris Masone441d542009-01-05 16:57:23 -05001770 if (!capable(CAP_SYS_ADMIN))
1771 return -EPERM;
1772
Li Zefandae7b662009-04-08 15:06:54 +08001773 vol_args = memdup_user(arg, sizeof(*vol_args));
1774 if (IS_ERR(vol_args))
1775 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001776
Mark Fasheh5516e592008-07-24 12:20:14 -04001777 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001778 ret = btrfs_init_new_device(root, vol_args->name);
1779
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001780 kfree(vol_args);
1781 return ret;
1782}
1783
Christoph Hellwigb2950862008-12-02 09:54:17 -05001784static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001785{
1786 struct btrfs_ioctl_vol_args *vol_args;
1787 int ret;
1788
Chris Masone441d542009-01-05 16:57:23 -05001789 if (!capable(CAP_SYS_ADMIN))
1790 return -EPERM;
1791
Yan Zhengc146afa2008-11-12 14:34:12 -05001792 if (root->fs_info->sb->s_flags & MS_RDONLY)
1793 return -EROFS;
1794
Li Zefandae7b662009-04-08 15:06:54 +08001795 vol_args = memdup_user(arg, sizeof(*vol_args));
1796 if (IS_ERR(vol_args))
1797 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001798
Mark Fasheh5516e592008-07-24 12:20:14 -04001799 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001800 ret = btrfs_rm_device(root, vol_args->name);
1801
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001802 kfree(vol_args);
1803 return ret;
1804}
1805
Yan, Zheng76dda932009-09-21 16:00:26 -04001806static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1807 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001808{
1809 struct inode *inode = fdentry(file)->d_inode;
1810 struct btrfs_root *root = BTRFS_I(inode)->root;
1811 struct file *src_file;
1812 struct inode *src;
1813 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001814 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001815 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001816 char *buf;
1817 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001818 u32 nritems;
1819 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001820 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001821 u64 len = olen;
1822 u64 bs = root->fs_info->sb->s_blocksize;
1823 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001824
Sage Weilc5c9cd42008-11-12 14:32:25 -05001825 /*
1826 * TODO:
1827 * - split compressed inline extents. annoying: we need to
1828 * decompress into destination's address_space (the file offset
1829 * may change, so source mapping won't do), then recompress (or
1830 * otherwise reinsert) a subrange.
1831 * - allow ranges within the same file to be cloned (provided
1832 * they don't overlap)?
1833 */
1834
Chris Masone441d542009-01-05 16:57:23 -05001835 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001836 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05001837 return -EINVAL;
1838
Li Zefanb83cc962010-12-20 16:04:08 +08001839 if (btrfs_root_readonly(root))
1840 return -EROFS;
1841
Yan Zhengc146afa2008-11-12 14:34:12 -05001842 ret = mnt_want_write(file->f_path.mnt);
1843 if (ret)
1844 return ret;
1845
Sage Weilc5c9cd42008-11-12 14:32:25 -05001846 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001847 if (!src_file) {
1848 ret = -EBADF;
1849 goto out_drop_write;
1850 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001851
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001852 src = src_file->f_dentry->d_inode;
1853
Sage Weilc5c9cd42008-11-12 14:32:25 -05001854 ret = -EINVAL;
1855 if (src == inode)
1856 goto out_fput;
1857
Dan Rosenberg5dc64162010-05-15 11:27:37 -04001858 /* the src must be open for reading */
1859 if (!(src_file->f_mode & FMODE_READ))
1860 goto out_fput;
1861
Yan Zhengae01a0a2008-08-04 23:23:47 -04001862 ret = -EISDIR;
1863 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001864 goto out_fput;
1865
Yan Zhengae01a0a2008-08-04 23:23:47 -04001866 ret = -EXDEV;
1867 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1868 goto out_fput;
1869
1870 ret = -ENOMEM;
1871 buf = vmalloc(btrfs_level_size(root, 0));
1872 if (!buf)
1873 goto out_fput;
1874
1875 path = btrfs_alloc_path();
1876 if (!path) {
1877 vfree(buf);
1878 goto out_fput;
1879 }
1880 path->reada = 2;
1881
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001882 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04001883 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1884 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001885 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04001886 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1887 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001888 }
1889
Sage Weilc5c9cd42008-11-12 14:32:25 -05001890 /* determine range to clone */
1891 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04001892 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001893 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001894 if (len == 0)
1895 olen = len = src->i_size - off;
1896 /* if we extend to eof, continue to block boundary */
1897 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00001898 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001899
1900 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00001901 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1902 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001903 goto out_unlock;
1904
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001905 /* do any pending delalloc/csum calc on src, one way or
1906 another, and lock file content */
1907 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001908 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001909 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04001910 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1911 if (!ordered &&
1912 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1913 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001914 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001915 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001916 if (ordered)
1917 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04001918 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001919 }
1920
Sage Weilc5c9cd42008-11-12 14:32:25 -05001921 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001922 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001923 key.type = BTRFS_EXTENT_DATA_KEY;
1924 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001925
1926 while (1) {
1927 /*
1928 * note the key will change type as we walk through the
1929 * tree.
1930 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04001931 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001932 if (ret < 0)
1933 goto out;
1934
Yan Zhengae01a0a2008-08-04 23:23:47 -04001935 nritems = btrfs_header_nritems(path->nodes[0]);
1936 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001937 ret = btrfs_next_leaf(root, path);
1938 if (ret < 0)
1939 goto out;
1940 if (ret > 0)
1941 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001942 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001943 }
1944 leaf = path->nodes[0];
1945 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001946
Yan Zhengae01a0a2008-08-04 23:23:47 -04001947 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001948 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001949 key.objectid != src->i_ino)
1950 break;
1951
Sage Weilc5c9cd42008-11-12 14:32:25 -05001952 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1953 struct btrfs_file_extent_item *extent;
1954 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001955 u32 size;
1956 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001957 u64 disko = 0, diskl = 0;
1958 u64 datao = 0, datal = 0;
1959 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00001960 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04001961
1962 size = btrfs_item_size_nr(leaf, slot);
1963 read_extent_buffer(leaf, buf,
1964 btrfs_item_ptr_offset(leaf, slot),
1965 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001966
1967 extent = btrfs_item_ptr(leaf, slot,
1968 struct btrfs_file_extent_item);
1969 comp = btrfs_file_extent_compression(leaf, extent);
1970 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001971 if (type == BTRFS_FILE_EXTENT_REG ||
1972 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001973 disko = btrfs_file_extent_disk_bytenr(leaf,
1974 extent);
1975 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1976 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001977 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001978 datal = btrfs_file_extent_num_bytes(leaf,
1979 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001980 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1981 /* take upper bound, may be compressed */
1982 datal = btrfs_file_extent_ram_bytes(leaf,
1983 extent);
1984 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001985 btrfs_release_path(root, path);
1986
Sage Weil050006a2010-10-29 15:37:33 -04001987 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05001988 key.offset >= off+len)
1989 goto next;
1990
Zheng Yan31840ae2008-09-23 13:14:14 -04001991 memcpy(&new_key, &key, sizeof(new_key));
1992 new_key.objectid = inode->i_ino;
Li Zefan4d728ec2011-01-26 14:10:43 +08001993 if (off <= key.offset)
1994 new_key.offset = key.offset + destoff - off;
1995 else
1996 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001997
Yan, Zhenga22285a2010-05-16 10:48:46 -04001998 trans = btrfs_start_transaction(root, 1);
1999 if (IS_ERR(trans)) {
2000 ret = PTR_ERR(trans);
2001 goto out;
2002 }
2003
Chris Masonc8a894d2009-06-27 21:07:03 -04002004 if (type == BTRFS_FILE_EXTENT_REG ||
2005 type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002006 if (off > key.offset) {
2007 datao += off - key.offset;
2008 datal -= off - key.offset;
2009 }
2010
2011 if (key.offset + datal > off + len)
2012 datal = off + len - key.offset;
2013
2014 ret = btrfs_drop_extents(trans, inode,
2015 new_key.offset,
2016 new_key.offset + datal,
2017 &hint_byte, 1);
2018 BUG_ON(ret);
2019
Sage Weilc5c9cd42008-11-12 14:32:25 -05002020 ret = btrfs_insert_empty_item(trans, root, path,
2021 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002022 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002023
2024 leaf = path->nodes[0];
2025 slot = path->slots[0];
2026 write_extent_buffer(leaf, buf,
2027 btrfs_item_ptr_offset(leaf, slot),
2028 size);
2029
2030 extent = btrfs_item_ptr(leaf, slot,
2031 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002032
Sage Weilc5c9cd42008-11-12 14:32:25 -05002033 /* disko == 0 means it's a hole */
2034 if (!disko)
2035 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002036
2037 btrfs_set_file_extent_offset(leaf, extent,
2038 datao);
2039 btrfs_set_file_extent_num_bytes(leaf, extent,
2040 datal);
2041 if (disko) {
2042 inode_add_bytes(inode, datal);
2043 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002044 disko, diskl, 0,
2045 root->root_key.objectid,
2046 inode->i_ino,
2047 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002048 BUG_ON(ret);
2049 }
2050 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2051 u64 skip = 0;
2052 u64 trim = 0;
2053 if (off > key.offset) {
2054 skip = off - key.offset;
2055 new_key.offset += skip;
2056 }
Chris Masond3977122009-01-05 21:25:51 -05002057
Sage Weilc5c9cd42008-11-12 14:32:25 -05002058 if (key.offset + datal > off+len)
2059 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002060
Sage Weilc5c9cd42008-11-12 14:32:25 -05002061 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002062 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002063 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002064 goto out;
2065 }
2066 size -= skip + trim;
2067 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002068
2069 ret = btrfs_drop_extents(trans, inode,
2070 new_key.offset,
2071 new_key.offset + datal,
2072 &hint_byte, 1);
2073 BUG_ON(ret);
2074
Sage Weilc5c9cd42008-11-12 14:32:25 -05002075 ret = btrfs_insert_empty_item(trans, root, path,
2076 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002077 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002078
2079 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002080 u32 start =
2081 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002082 memmove(buf+start, buf+start+skip,
2083 datal);
2084 }
2085
2086 leaf = path->nodes[0];
2087 slot = path->slots[0];
2088 write_extent_buffer(leaf, buf,
2089 btrfs_item_ptr_offset(leaf, slot),
2090 size);
2091 inode_add_bytes(inode, datal);
2092 }
2093
2094 btrfs_mark_buffer_dirty(leaf);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002095 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002096
Yan, Zhenga22285a2010-05-16 10:48:46 -04002097 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002098
2099 /*
2100 * we round up to the block size at eof when
2101 * determining which extents to clone above,
2102 * but shouldn't round up the file size
2103 */
2104 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002105 if (endoff > destoff+olen)
2106 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002107 if (endoff > inode->i_size)
2108 btrfs_i_size_write(inode, endoff);
2109
Yan, Zhenga22285a2010-05-16 10:48:46 -04002110 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2111 ret = btrfs_update_inode(trans, root, inode);
2112 BUG_ON(ret);
2113 btrfs_end_transaction(trans, root);
2114 }
Chris Masond3977122009-01-05 21:25:51 -05002115next:
Zheng Yan31840ae2008-09-23 13:14:14 -04002116 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002117 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002118 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002119 ret = 0;
2120out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002121 btrfs_release_path(root, path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002122 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002123out_unlock:
2124 mutex_unlock(&src->i_mutex);
2125 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002126 vfree(buf);
2127 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002128out_fput:
2129 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002130out_drop_write:
2131 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002132 return ret;
2133}
2134
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002135static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002136{
2137 struct btrfs_ioctl_clone_range_args args;
2138
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002139 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002140 return -EFAULT;
2141 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2142 args.src_length, args.dest_offset);
2143}
2144
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002145/*
2146 * there are many ways the trans_start and trans_end ioctls can lead
2147 * to deadlocks. They should only be used by applications that
2148 * basically own the machine, and have a very in depth understanding
2149 * of all the possible deadlocks and enospc problems.
2150 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002151static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002152{
2153 struct inode *inode = fdentry(file)->d_inode;
2154 struct btrfs_root *root = BTRFS_I(inode)->root;
2155 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002156 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002157
Sage Weil1ab86ae2009-09-29 18:38:44 -04002158 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002159 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002160 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002161
2162 ret = -EINPROGRESS;
2163 if (file->private_data)
2164 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002165
Li Zefanb83cc962010-12-20 16:04:08 +08002166 ret = -EROFS;
2167 if (btrfs_root_readonly(root))
2168 goto out;
2169
Yan Zhengc146afa2008-11-12 14:34:12 -05002170 ret = mnt_want_write(file->f_path.mnt);
2171 if (ret)
2172 goto out;
2173
Sage Weil9ca9ee02008-08-04 10:41:27 -04002174 mutex_lock(&root->fs_info->trans_mutex);
2175 root->fs_info->open_ioctl_trans++;
2176 mutex_unlock(&root->fs_info->trans_mutex);
2177
Sage Weil1ab86ae2009-09-29 18:38:44 -04002178 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002179 trans = btrfs_start_ioctl_transaction(root, 0);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002180 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002181 goto out_drop;
2182
2183 file->private_data = trans;
2184 return 0;
2185
2186out_drop:
2187 mutex_lock(&root->fs_info->trans_mutex);
2188 root->fs_info->open_ioctl_trans--;
2189 mutex_unlock(&root->fs_info->trans_mutex);
2190 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002191out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002192 return ret;
2193}
2194
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002195static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2196{
2197 struct inode *inode = fdentry(file)->d_inode;
2198 struct btrfs_root *root = BTRFS_I(inode)->root;
2199 struct btrfs_root *new_root;
2200 struct btrfs_dir_item *di;
2201 struct btrfs_trans_handle *trans;
2202 struct btrfs_path *path;
2203 struct btrfs_key location;
2204 struct btrfs_disk_key disk_key;
2205 struct btrfs_super_block *disk_super;
2206 u64 features;
2207 u64 objectid = 0;
2208 u64 dir_id;
2209
2210 if (!capable(CAP_SYS_ADMIN))
2211 return -EPERM;
2212
2213 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2214 return -EFAULT;
2215
2216 if (!objectid)
2217 objectid = root->root_key.objectid;
2218
2219 location.objectid = objectid;
2220 location.type = BTRFS_ROOT_ITEM_KEY;
2221 location.offset = (u64)-1;
2222
2223 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2224 if (IS_ERR(new_root))
2225 return PTR_ERR(new_root);
2226
2227 if (btrfs_root_refs(&new_root->root_item) == 0)
2228 return -ENOENT;
2229
2230 path = btrfs_alloc_path();
2231 if (!path)
2232 return -ENOMEM;
2233 path->leave_spinning = 1;
2234
2235 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002236 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002237 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002238 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002239 }
2240
2241 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2242 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2243 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002244 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002245 btrfs_free_path(path);
2246 btrfs_end_transaction(trans, root);
2247 printk(KERN_ERR "Umm, you don't have the default dir item, "
2248 "this isn't going to work\n");
2249 return -ENOENT;
2250 }
2251
2252 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2253 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2254 btrfs_mark_buffer_dirty(path->nodes[0]);
2255 btrfs_free_path(path);
2256
2257 disk_super = &root->fs_info->super_copy;
2258 features = btrfs_super_incompat_flags(disk_super);
2259 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2260 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2261 btrfs_set_super_incompat_flags(disk_super, features);
2262 }
2263 btrfs_end_transaction(trans, root);
2264
2265 return 0;
2266}
2267
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002268static void get_block_group_info(struct list_head *groups_list,
2269 struct btrfs_ioctl_space_info *space)
2270{
2271 struct btrfs_block_group_cache *block_group;
2272
2273 space->total_bytes = 0;
2274 space->used_bytes = 0;
2275 space->flags = 0;
2276 list_for_each_entry(block_group, groups_list, list) {
2277 space->flags = block_group->flags;
2278 space->total_bytes += block_group->key.offset;
2279 space->used_bytes +=
2280 btrfs_block_group_used(&block_group->item);
2281 }
2282}
2283
Josef Bacik1406e432010-01-13 18:19:06 +00002284long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2285{
2286 struct btrfs_ioctl_space_args space_args;
2287 struct btrfs_ioctl_space_info space;
2288 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002289 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002290 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002291 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002292 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2293 BTRFS_BLOCK_GROUP_SYSTEM,
2294 BTRFS_BLOCK_GROUP_METADATA,
2295 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2296 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002297 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002298 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002299 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002300 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002301
2302 if (copy_from_user(&space_args,
2303 (struct btrfs_ioctl_space_args __user *)arg,
2304 sizeof(space_args)))
2305 return -EFAULT;
2306
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002307 for (i = 0; i < num_types; i++) {
2308 struct btrfs_space_info *tmp;
2309
2310 info = NULL;
2311 rcu_read_lock();
2312 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2313 list) {
2314 if (tmp->flags == types[i]) {
2315 info = tmp;
2316 break;
2317 }
2318 }
2319 rcu_read_unlock();
2320
2321 if (!info)
2322 continue;
2323
2324 down_read(&info->groups_sem);
2325 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2326 if (!list_empty(&info->block_groups[c]))
2327 slot_count++;
2328 }
2329 up_read(&info->groups_sem);
2330 }
Josef Bacik1406e432010-01-13 18:19:06 +00002331
Chris Mason7fde62b2010-03-16 15:40:10 -04002332 /* space_slots == 0 means they are asking for a count */
2333 if (space_args.space_slots == 0) {
2334 space_args.total_spaces = slot_count;
2335 goto out;
2336 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002337
Dan Rosenberg51788b12011-02-14 16:04:23 -05002338 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002339
Chris Mason7fde62b2010-03-16 15:40:10 -04002340 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002341
Chris Mason7fde62b2010-03-16 15:40:10 -04002342 /* we generally have at most 6 or so space infos, one for each raid
2343 * level. So, a whole page should be more than enough for everyone
2344 */
2345 if (alloc_size > PAGE_CACHE_SIZE)
2346 return -ENOMEM;
2347
2348 space_args.total_spaces = 0;
2349 dest = kmalloc(alloc_size, GFP_NOFS);
2350 if (!dest)
2351 return -ENOMEM;
2352 dest_orig = dest;
2353
2354 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002355 for (i = 0; i < num_types; i++) {
2356 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002357
Dan Rosenberg51788b12011-02-14 16:04:23 -05002358 if (!slot_count)
2359 break;
2360
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002361 info = NULL;
2362 rcu_read_lock();
2363 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2364 list) {
2365 if (tmp->flags == types[i]) {
2366 info = tmp;
2367 break;
2368 }
2369 }
2370 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002371
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002372 if (!info)
2373 continue;
2374 down_read(&info->groups_sem);
2375 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2376 if (!list_empty(&info->block_groups[c])) {
2377 get_block_group_info(&info->block_groups[c],
2378 &space);
2379 memcpy(dest, &space, sizeof(space));
2380 dest++;
2381 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002382 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002383 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002384 if (!slot_count)
2385 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002386 }
2387 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002388 }
Josef Bacik1406e432010-01-13 18:19:06 +00002389
Chris Mason7fde62b2010-03-16 15:40:10 -04002390 user_dest = (struct btrfs_ioctl_space_info *)
2391 (arg + sizeof(struct btrfs_ioctl_space_args));
2392
2393 if (copy_to_user(user_dest, dest_orig, alloc_size))
2394 ret = -EFAULT;
2395
2396 kfree(dest_orig);
2397out:
2398 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002399 ret = -EFAULT;
2400
2401 return ret;
2402}
2403
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002404/*
2405 * there are many ways the trans_start and trans_end ioctls can lead
2406 * to deadlocks. They should only be used by applications that
2407 * basically own the machine, and have a very in depth understanding
2408 * of all the possible deadlocks and enospc problems.
2409 */
2410long btrfs_ioctl_trans_end(struct file *file)
2411{
2412 struct inode *inode = fdentry(file)->d_inode;
2413 struct btrfs_root *root = BTRFS_I(inode)->root;
2414 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002415
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002416 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002417 if (!trans)
2418 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002419 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002420
Sage Weil1ab86ae2009-09-29 18:38:44 -04002421 btrfs_end_transaction(trans, root);
2422
Sage Weil9ca9ee02008-08-04 10:41:27 -04002423 mutex_lock(&root->fs_info->trans_mutex);
2424 root->fs_info->open_ioctl_trans--;
2425 mutex_unlock(&root->fs_info->trans_mutex);
2426
Sage Weilcfc8ea82008-12-11 16:30:06 -05002427 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002428 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002429}
2430
Sage Weil46204592010-10-29 15:41:32 -04002431static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2432{
2433 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2434 struct btrfs_trans_handle *trans;
2435 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002436 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002437
2438 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002439 if (IS_ERR(trans))
2440 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002441 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002442 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002443 if (ret) {
2444 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002445 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002446 }
Sage Weil46204592010-10-29 15:41:32 -04002447
2448 if (argp)
2449 if (copy_to_user(argp, &transid, sizeof(transid)))
2450 return -EFAULT;
2451 return 0;
2452}
2453
2454static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2455{
2456 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2457 u64 transid;
2458
2459 if (argp) {
2460 if (copy_from_user(&transid, argp, sizeof(transid)))
2461 return -EFAULT;
2462 } else {
2463 transid = 0; /* current trans */
2464 }
2465 return btrfs_wait_for_commit(root, transid);
2466}
2467
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002468long btrfs_ioctl(struct file *file, unsigned int
2469 cmd, unsigned long arg)
2470{
2471 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002472 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002473
2474 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002475 case FS_IOC_GETFLAGS:
2476 return btrfs_ioctl_getflags(file, argp);
2477 case FS_IOC_SETFLAGS:
2478 return btrfs_ioctl_setflags(file, argp);
2479 case FS_IOC_GETVERSION:
2480 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00002481 case FITRIM:
2482 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002483 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002484 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00002485 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002486 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05002487 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08002488 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04002489 case BTRFS_IOC_SNAP_DESTROY:
2490 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08002491 case BTRFS_IOC_SUBVOL_GETFLAGS:
2492 return btrfs_ioctl_subvol_getflags(file, argp);
2493 case BTRFS_IOC_SUBVOL_SETFLAGS:
2494 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002495 case BTRFS_IOC_DEFAULT_SUBVOL:
2496 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002497 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05002498 return btrfs_ioctl_defrag(file, NULL);
2499 case BTRFS_IOC_DEFRAG_RANGE:
2500 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002501 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002502 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002503 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002504 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002505 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05002506 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002507 case BTRFS_IOC_BALANCE:
2508 return btrfs_balance(root->fs_info->dev_root);
2509 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05002510 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2511 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002512 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002513 case BTRFS_IOC_TRANS_START:
2514 return btrfs_ioctl_trans_start(file);
2515 case BTRFS_IOC_TRANS_END:
2516 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002517 case BTRFS_IOC_TREE_SEARCH:
2518 return btrfs_ioctl_tree_search(file, argp);
2519 case BTRFS_IOC_INO_LOOKUP:
2520 return btrfs_ioctl_ino_lookup(file, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00002521 case BTRFS_IOC_SPACE_INFO:
2522 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002523 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002524 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2525 return 0;
Sage Weil46204592010-10-29 15:41:32 -04002526 case BTRFS_IOC_START_SYNC:
2527 return btrfs_ioctl_start_sync(file, argp);
2528 case BTRFS_IOC_WAIT_SYNC:
2529 return btrfs_ioctl_wait_sync(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002530 }
2531
2532 return -ENOTTY;
2533}