blob: fe8a60c865ebe7107b79db2bef11cbf561f2c04b [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"
Li Zefan581bb052011-04-20 10:06:11 +080053#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020054#include "backref.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040055
Christoph Hellwig6cbff002009-04-17 10:37:41 +020056/* Mask out flags that are inappropriate for the given type of inode. */
57static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
58{
59 if (S_ISDIR(mode))
60 return flags;
61 else if (S_ISREG(mode))
62 return flags & ~FS_DIRSYNC_FL;
63 else
64 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
65}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040066
Christoph Hellwig6cbff002009-04-17 10:37:41 +020067/*
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69 */
70static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
71{
72 unsigned int iflags = 0;
73
74 if (flags & BTRFS_INODE_SYNC)
75 iflags |= FS_SYNC_FL;
76 if (flags & BTRFS_INODE_IMMUTABLE)
77 iflags |= FS_IMMUTABLE_FL;
78 if (flags & BTRFS_INODE_APPEND)
79 iflags |= FS_APPEND_FL;
80 if (flags & BTRFS_INODE_NODUMP)
81 iflags |= FS_NODUMP_FL;
82 if (flags & BTRFS_INODE_NOATIME)
83 iflags |= FS_NOATIME_FL;
84 if (flags & BTRFS_INODE_DIRSYNC)
85 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000086 if (flags & BTRFS_INODE_NODATACOW)
87 iflags |= FS_NOCOW_FL;
88
89 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
90 iflags |= FS_COMPR_FL;
91 else if (flags & BTRFS_INODE_NOCOMPRESS)
92 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020093
94 return iflags;
95}
96
97/*
98 * Update inode->i_flags based on the btrfs internal flags.
99 */
100void btrfs_update_iflags(struct inode *inode)
101{
102 struct btrfs_inode *ip = BTRFS_I(inode);
103
104 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
105
106 if (ip->flags & BTRFS_INODE_SYNC)
107 inode->i_flags |= S_SYNC;
108 if (ip->flags & BTRFS_INODE_IMMUTABLE)
109 inode->i_flags |= S_IMMUTABLE;
110 if (ip->flags & BTRFS_INODE_APPEND)
111 inode->i_flags |= S_APPEND;
112 if (ip->flags & BTRFS_INODE_NOATIME)
113 inode->i_flags |= S_NOATIME;
114 if (ip->flags & BTRFS_INODE_DIRSYNC)
115 inode->i_flags |= S_DIRSYNC;
116}
117
118/*
119 * Inherit flags from the parent inode.
120 *
Josef Bacike27425d2011-09-27 11:01:30 -0400121 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200122 */
123void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
124{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400125 unsigned int flags;
126
127 if (!dir)
128 return;
129
130 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200131
Josef Bacike27425d2011-09-27 11:01:30 -0400132 if (flags & BTRFS_INODE_NOCOMPRESS) {
133 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
134 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
135 } else if (flags & BTRFS_INODE_COMPRESS) {
136 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
137 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
138 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200139
Josef Bacike27425d2011-09-27 11:01:30 -0400140 if (flags & BTRFS_INODE_NODATACOW)
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
142
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200143 btrfs_update_iflags(inode);
144}
145
146static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
147{
148 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
149 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
150
151 if (copy_to_user(arg, &flags, sizeof(flags)))
152 return -EFAULT;
153 return 0;
154}
155
Liu Bo75e7cb72011-03-22 10:12:20 +0000156static int check_flags(unsigned int flags)
157{
158 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
159 FS_NOATIME_FL | FS_NODUMP_FL | \
160 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000161 FS_NOCOMP_FL | FS_COMPR_FL |
162 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000163 return -EOPNOTSUPP;
164
165 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
166 return -EINVAL;
167
Liu Bo75e7cb72011-03-22 10:12:20 +0000168 return 0;
169}
170
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200171static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
172{
173 struct inode *inode = file->f_path.dentry->d_inode;
174 struct btrfs_inode *ip = BTRFS_I(inode);
175 struct btrfs_root *root = ip->root;
176 struct btrfs_trans_handle *trans;
177 unsigned int flags, oldflags;
178 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800179 u64 ip_oldflags;
180 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181
Li Zefanb83cc962010-12-20 16:04:08 +0800182 if (btrfs_root_readonly(root))
183 return -EROFS;
184
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200185 if (copy_from_user(&flags, arg, sizeof(flags)))
186 return -EFAULT;
187
Liu Bo75e7cb72011-03-22 10:12:20 +0000188 ret = check_flags(flags);
189 if (ret)
190 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200191
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700192 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193 return -EACCES;
194
195 mutex_lock(&inode->i_mutex);
196
Li Zefanf062abf2011-12-29 13:36:45 +0800197 ip_oldflags = ip->flags;
198 i_oldflags = inode->i_flags;
199
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200200 flags = btrfs_mask_flags(inode->i_mode, flags);
201 oldflags = btrfs_flags_to_ioctl(ip->flags);
202 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
203 if (!capable(CAP_LINUX_IMMUTABLE)) {
204 ret = -EPERM;
205 goto out_unlock;
206 }
207 }
208
209 ret = mnt_want_write(file->f_path.mnt);
210 if (ret)
211 goto out_unlock;
212
213 if (flags & FS_SYNC_FL)
214 ip->flags |= BTRFS_INODE_SYNC;
215 else
216 ip->flags &= ~BTRFS_INODE_SYNC;
217 if (flags & FS_IMMUTABLE_FL)
218 ip->flags |= BTRFS_INODE_IMMUTABLE;
219 else
220 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
221 if (flags & FS_APPEND_FL)
222 ip->flags |= BTRFS_INODE_APPEND;
223 else
224 ip->flags &= ~BTRFS_INODE_APPEND;
225 if (flags & FS_NODUMP_FL)
226 ip->flags |= BTRFS_INODE_NODUMP;
227 else
228 ip->flags &= ~BTRFS_INODE_NODUMP;
229 if (flags & FS_NOATIME_FL)
230 ip->flags |= BTRFS_INODE_NOATIME;
231 else
232 ip->flags &= ~BTRFS_INODE_NOATIME;
233 if (flags & FS_DIRSYNC_FL)
234 ip->flags |= BTRFS_INODE_DIRSYNC;
235 else
236 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000237 if (flags & FS_NOCOW_FL)
238 ip->flags |= BTRFS_INODE_NODATACOW;
239 else
240 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200241
Liu Bo75e7cb72011-03-22 10:12:20 +0000242 /*
243 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 * flag may be changed automatically if compression code won't make
245 * things smaller.
246 */
247 if (flags & FS_NOCOMP_FL) {
248 ip->flags &= ~BTRFS_INODE_COMPRESS;
249 ip->flags |= BTRFS_INODE_NOCOMPRESS;
250 } else if (flags & FS_COMPR_FL) {
251 ip->flags |= BTRFS_INODE_COMPRESS;
252 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000253 } else {
254 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000255 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200256
Li Zefan4da6f1a2011-12-29 13:39:50 +0800257 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800258 if (IS_ERR(trans)) {
259 ret = PTR_ERR(trans);
260 goto out_drop;
261 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200262
Li Zefan306424c2011-12-14 20:12:02 -0500263 btrfs_update_iflags(inode);
264 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200265 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200266
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200267 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800268 out_drop:
269 if (ret) {
270 ip->flags = ip_oldflags;
271 inode->i_flags = i_oldflags;
272 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200273
274 mnt_drop_write(file->f_path.mnt);
275 out_unlock:
276 mutex_unlock(&inode->i_mutex);
liubo2d4e6f6a2011-02-24 09:38:16 +0000277 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278}
279
280static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
281{
282 struct inode *inode = file->f_path.dentry->d_inode;
283
284 return put_user(inode->i_generation, arg);
285}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400286
Li Dongyangf7039b12011-03-24 10:24:28 +0000287static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
288{
289 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
290 struct btrfs_fs_info *fs_info = root->fs_info;
291 struct btrfs_device *device;
292 struct request_queue *q;
293 struct fstrim_range range;
294 u64 minlen = ULLONG_MAX;
295 u64 num_devices = 0;
David Sterba6c417612011-04-13 15:41:04 +0200296 u64 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000297 int ret;
298
299 if (!capable(CAP_SYS_ADMIN))
300 return -EPERM;
301
Xiao Guangrong1f781602011-04-20 10:09:16 +0000302 rcu_read_lock();
303 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
304 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000305 if (!device->bdev)
306 continue;
307 q = bdev_get_queue(device->bdev);
308 if (blk_queue_discard(q)) {
309 num_devices++;
310 minlen = min((u64)q->limits.discard_granularity,
311 minlen);
312 }
313 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000314 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200315
Li Dongyangf7039b12011-03-24 10:24:28 +0000316 if (!num_devices)
317 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000318 if (copy_from_user(&range, arg, sizeof(range)))
319 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200320 if (range.start > total_bytes)
321 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000322
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200323 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000324 range.minlen = max(range.minlen, minlen);
325 ret = btrfs_trim_fs(root, &range);
326 if (ret < 0)
327 return ret;
328
329 if (copy_to_user(arg, &range, sizeof(range)))
330 return -EFAULT;
331
332 return 0;
333}
334
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400335static noinline int create_subvol(struct btrfs_root *root,
336 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400337 char *name, int namelen,
338 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400339{
340 struct btrfs_trans_handle *trans;
341 struct btrfs_key key;
342 struct btrfs_root_item root_item;
343 struct btrfs_inode_item *inode_item;
344 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400345 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400346 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000347 struct inode *dir;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400348 int ret;
349 int err;
350 u64 objectid;
351 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500352 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400353
Li Zefan581bb052011-04-20 10:06:11 +0800354 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400355 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400356 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000357
358 dir = parent->d_inode;
359
Josef Bacik9ed74f22009-09-11 16:12:44 -0400360 /*
361 * 1 - inode item
362 * 2 - refs
363 * 1 - root item
364 * 2 - dir items
365 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400366 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400367 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400368 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400369
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400370 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
371 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400372 if (IS_ERR(leaf)) {
373 ret = PTR_ERR(leaf);
374 goto fail;
375 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400376
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400377 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400378 btrfs_set_header_bytenr(leaf, leaf->start);
379 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400380 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400381 btrfs_set_header_owner(leaf, objectid);
382
383 write_extent_buffer(leaf, root->fs_info->fsid,
384 (unsigned long)btrfs_header_fsid(leaf),
385 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400386 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
387 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
388 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400389 btrfs_mark_buffer_dirty(leaf);
390
391 inode_item = &root_item.inode;
392 memset(inode_item, 0, sizeof(*inode_item));
393 inode_item->generation = cpu_to_le64(1);
394 inode_item->size = cpu_to_le64(3);
395 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400396 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
398
Li Zefan08fe4db2011-03-28 02:01:25 +0000399 root_item.flags = 0;
400 root_item.byte_limit = 0;
401 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
402
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400403 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400404 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400405 btrfs_set_root_level(&root_item, 0);
406 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000407 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400408 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400409
410 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
411 root_item.drop_level = 0;
412
Chris Mason925baed2008-06-25 16:01:30 -0400413 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414 free_extent_buffer(leaf);
415 leaf = NULL;
416
417 btrfs_set_root_dirid(&root_item, new_dirid);
418
419 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400420 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400421 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
422 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
423 &root_item);
424 if (ret)
425 goto fail;
426
Yan, Zheng76dda932009-09-21 16:00:26 -0400427 key.offset = (u64)-1;
428 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
429 BUG_ON(IS_ERR(new_root));
430
431 btrfs_record_root_in_trans(trans, new_root);
432
Josef Bacikd82a6f12011-05-11 15:26:06 -0400433 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400434 /*
435 * insert the directory item
436 */
Chris Mason3de45862008-11-17 21:02:50 -0500437 ret = btrfs_set_inode_index(dir, &index);
438 BUG_ON(ret);
439
440 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800441 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500442 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400443 if (ret)
444 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500445
Yan Zheng52c26172009-01-05 15:43:43 -0500446 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
447 ret = btrfs_update_inode(trans, root, dir);
448 BUG_ON(ret);
449
Chris Mason0660b5a2008-11-17 20:37:39 -0500450 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400451 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800452 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400453
Chris Mason0660b5a2008-11-17 20:37:39 -0500454 BUG_ON(ret);
455
Yan, Zheng76dda932009-09-21 16:00:26 -0400456 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400457fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400458 if (async_transid) {
459 *async_transid = trans->transid;
460 err = btrfs_commit_transaction_async(trans, root, 1);
461 } else {
462 err = btrfs_commit_transaction(trans, root);
463 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400464 if (err && !ret)
465 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400466 return ret;
467}
468
Sage Weil72fd0322010-10-29 15:41:32 -0400469static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800470 char *name, int namelen, u64 *async_transid,
471 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400472{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000473 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400474 struct btrfs_pending_snapshot *pending_snapshot;
475 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000476 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400477
478 if (!root->ref_cows)
479 return -EINVAL;
480
Yan, Zhenga22285a2010-05-16 10:48:46 -0400481 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
482 if (!pending_snapshot)
483 return -ENOMEM;
484
485 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
486 pending_snapshot->dentry = dentry;
487 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800488 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400489
490 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
491 if (IS_ERR(trans)) {
492 ret = PTR_ERR(trans);
493 goto fail;
494 }
495
496 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
497 BUG_ON(ret);
498
Josef Bacik83515832011-06-14 15:16:14 -0400499 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400500 list_add(&pending_snapshot->list,
501 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400502 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400503 if (async_transid) {
504 *async_transid = trans->transid;
505 ret = btrfs_commit_transaction_async(trans,
506 root->fs_info->extent_root, 1);
507 } else {
508 ret = btrfs_commit_transaction(trans,
509 root->fs_info->extent_root);
510 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400511 BUG_ON(ret);
512
513 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400514 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000515 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400516
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500517 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
518 if (ret)
519 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400520
Al Viro2fbe8c82011-07-16 21:38:06 -0400521 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000522 if (IS_ERR(inode)) {
523 ret = PTR_ERR(inode);
524 goto fail;
525 }
526 BUG_ON(!inode);
527 d_instantiate(dentry, inode);
528 ret = 0;
529fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400530 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400531 return ret;
532}
533
Sage Weil4260f7c2010-10-29 15:46:43 -0400534/* copy of check_sticky in fs/namei.c()
535* It's inline, so penalty for filesystems that don't use sticky bit is
536* minimal.
537*/
538static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
539{
540 uid_t fsuid = current_fsuid();
541
542 if (!(dir->i_mode & S_ISVTX))
543 return 0;
544 if (inode->i_uid == fsuid)
545 return 0;
546 if (dir->i_uid == fsuid)
547 return 0;
548 return !capable(CAP_FOWNER);
549}
550
551/* copy of may_delete in fs/namei.c()
552 * Check whether we can remove a link victim from directory dir, check
553 * whether the type of victim is right.
554 * 1. We can't do it if dir is read-only (done in permission())
555 * 2. We should have write and exec permissions on dir
556 * 3. We can't remove anything from append-only dir
557 * 4. We can't do anything with immutable dir (done in permission())
558 * 5. If the sticky bit on dir is set we should either
559 * a. be owner of dir, or
560 * b. be owner of victim, or
561 * c. have CAP_FOWNER capability
562 * 6. If the victim is append-only or immutable we can't do antyhing with
563 * links pointing to it.
564 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
565 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
566 * 9. We can't remove a root or mountpoint.
567 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
568 * nfs_async_unlink().
569 */
570
571static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
572{
573 int error;
574
575 if (!victim->d_inode)
576 return -ENOENT;
577
578 BUG_ON(victim->d_parent->d_inode != dir);
579 audit_inode_child(victim, dir);
580
581 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
582 if (error)
583 return error;
584 if (IS_APPEND(dir))
585 return -EPERM;
586 if (btrfs_check_sticky(dir, victim->d_inode)||
587 IS_APPEND(victim->d_inode)||
588 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
589 return -EPERM;
590 if (isdir) {
591 if (!S_ISDIR(victim->d_inode->i_mode))
592 return -ENOTDIR;
593 if (IS_ROOT(victim))
594 return -EBUSY;
595 } else if (S_ISDIR(victim->d_inode->i_mode))
596 return -EISDIR;
597 if (IS_DEADDIR(dir))
598 return -ENOENT;
599 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
600 return -EBUSY;
601 return 0;
602}
603
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400604/* copy of may_create in fs/namei.c() */
605static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
606{
607 if (child->d_inode)
608 return -EEXIST;
609 if (IS_DEADDIR(dir))
610 return -ENOENT;
611 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
612}
613
614/*
615 * Create a new subvolume below @parent. This is largely modeled after
616 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
617 * inside this filesystem so it's quite a bit simpler.
618 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400619static noinline int btrfs_mksubvol(struct path *parent,
620 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400621 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800622 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400623{
Yan, Zheng76dda932009-09-21 16:00:26 -0400624 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400625 struct dentry *dentry;
626 int error;
627
Yan, Zheng76dda932009-09-21 16:00:26 -0400628 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400629
630 dentry = lookup_one_len(name, parent->dentry, namelen);
631 error = PTR_ERR(dentry);
632 if (IS_ERR(dentry))
633 goto out_unlock;
634
635 error = -EEXIST;
636 if (dentry->d_inode)
637 goto out_dput;
638
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400639 error = mnt_want_write(parent->mnt);
640 if (error)
641 goto out_dput;
642
Yan, Zheng76dda932009-09-21 16:00:26 -0400643 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400644 if (error)
645 goto out_drop_write;
646
Yan, Zheng76dda932009-09-21 16:00:26 -0400647 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
648
649 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
650 goto out_up_read;
651
Chris Mason3de45862008-11-17 21:02:50 -0500652 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400653 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800654 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500655 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400656 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400657 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500658 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400659 if (!error)
660 fsnotify_mkdir(dir, dentry);
661out_up_read:
662 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400663out_drop_write:
664 mnt_drop_write(parent->mnt);
665out_dput:
666 dput(dentry);
667out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400668 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400669 return error;
670}
671
Chris Mason4cb53002011-05-24 15:35:30 -0400672/*
673 * When we're defragging a range, we don't want to kick it off again
674 * if it is really just waiting for delalloc to send it down.
675 * If we find a nice big extent or delalloc range for the bytes in the
676 * file you want to defrag, we return 0 to let you know to skip this
677 * part of the file
678 */
679static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
680{
681 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
682 struct extent_map *em = NULL;
683 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
684 u64 end;
685
686 read_lock(&em_tree->lock);
687 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
688 read_unlock(&em_tree->lock);
689
690 if (em) {
691 end = extent_map_end(em);
692 free_extent_map(em);
693 if (end - offset > thresh)
694 return 0;
695 }
696 /* if we already have a nice delalloc here, just stop */
697 thresh /= 2;
698 end = count_range_bits(io_tree, &offset, offset + thresh,
699 thresh, EXTENT_DELALLOC, 1);
700 if (end >= thresh)
701 return 0;
702 return 1;
703}
704
705/*
706 * helper function to walk through a file and find extents
707 * newer than a specific transid, and smaller than thresh.
708 *
709 * This is used by the defragging code to find new and small
710 * extents
711 */
712static int find_new_extents(struct btrfs_root *root,
713 struct inode *inode, u64 newer_than,
714 u64 *off, int thresh)
715{
716 struct btrfs_path *path;
717 struct btrfs_key min_key;
718 struct btrfs_key max_key;
719 struct extent_buffer *leaf;
720 struct btrfs_file_extent_item *extent;
721 int type;
722 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000723 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400724
725 path = btrfs_alloc_path();
726 if (!path)
727 return -ENOMEM;
728
David Sterbaa4689d22011-05-31 17:08:14 +0000729 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400730 min_key.type = BTRFS_EXTENT_DATA_KEY;
731 min_key.offset = *off;
732
David Sterbaa4689d22011-05-31 17:08:14 +0000733 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400734 max_key.type = (u8)-1;
735 max_key.offset = (u64)-1;
736
737 path->keep_locks = 1;
738
739 while(1) {
740 ret = btrfs_search_forward(root, &min_key, &max_key,
741 path, 0, newer_than);
742 if (ret != 0)
743 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000744 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400745 goto none;
746 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
747 goto none;
748
749 leaf = path->nodes[0];
750 extent = btrfs_item_ptr(leaf, path->slots[0],
751 struct btrfs_file_extent_item);
752
753 type = btrfs_file_extent_type(leaf, extent);
754 if (type == BTRFS_FILE_EXTENT_REG &&
755 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
756 check_defrag_in_cache(inode, min_key.offset, thresh)) {
757 *off = min_key.offset;
758 btrfs_free_path(path);
759 return 0;
760 }
761
762 if (min_key.offset == (u64)-1)
763 goto none;
764
765 min_key.offset++;
766 btrfs_release_path(path);
767 }
768none:
769 btrfs_free_path(path);
770 return -ENOENT;
771}
772
Chris Mason940100a2010-03-10 10:52:59 -0500773static int should_defrag_range(struct inode *inode, u64 start, u64 len,
Chris Mason1e701a32010-03-11 09:42:04 -0500774 int thresh, u64 *last_len, u64 *skip,
775 u64 *defrag_end)
Chris Mason940100a2010-03-10 10:52:59 -0500776{
777 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
778 struct extent_map *em = NULL;
779 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
780 int ret = 1;
781
782 /*
Li Zefan008873e2011-09-02 15:57:07 +0800783 * make sure that once we start defragging an extent, we keep on
Chris Mason940100a2010-03-10 10:52:59 -0500784 * defragging it
785 */
786 if (start < *defrag_end)
787 return 1;
788
789 *skip = 0;
790
791 /*
792 * hopefully we have this extent in the tree already, try without
793 * the full extent lock
794 */
795 read_lock(&em_tree->lock);
796 em = lookup_extent_mapping(em_tree, start, len);
797 read_unlock(&em_tree->lock);
798
799 if (!em) {
800 /* get the big lock and read metadata off disk */
801 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
802 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
803 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
804
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000805 if (IS_ERR(em))
Chris Mason940100a2010-03-10 10:52:59 -0500806 return 0;
807 }
808
809 /* this will cover holes, and inline extents */
810 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
811 ret = 0;
812
813 /*
814 * we hit a real extent, if it is big don't bother defragging it again
815 */
Chris Mason1e701a32010-03-11 09:42:04 -0500816 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
Chris Mason940100a2010-03-10 10:52:59 -0500817 ret = 0;
818
819 /*
820 * last_len ends up being a counter of how many bytes we've defragged.
821 * every time we choose not to defrag an extent, we reset *last_len
822 * so that the next tiny extent will force a defrag.
823 *
824 * The end result of this is that tiny extents before a single big
825 * extent will force at least part of that big extent to be defragged.
826 */
827 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500828 *defrag_end = extent_map_end(em);
829 } else {
830 *last_len = 0;
831 *skip = extent_map_end(em);
832 *defrag_end = 0;
833 }
834
835 free_extent_map(em);
836 return ret;
837}
838
Chris Mason4cb53002011-05-24 15:35:30 -0400839/*
840 * it doesn't do much good to defrag one or two pages
841 * at a time. This pulls in a nice chunk of pages
842 * to COW and defrag.
843 *
844 * It also makes sure the delalloc code has enough
845 * dirty data to avoid making new small extents as part
846 * of the defrag
847 *
848 * It's a good idea to start RA on this range
849 * before calling this.
850 */
851static int cluster_pages_for_defrag(struct inode *inode,
852 struct page **pages,
853 unsigned long start_index,
854 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400855{
Chris Mason4cb53002011-05-24 15:35:30 -0400856 unsigned long file_end;
857 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400858 u64 page_start;
859 u64 page_end;
Chris Mason4cb53002011-05-24 15:35:30 -0400860 int ret;
861 int i;
862 int i_done;
863 struct btrfs_ordered_extent *ordered;
864 struct extent_state *cached_state = NULL;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400865 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400866
867 if (isize == 0)
868 return 0;
869 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
870
Josef Bacik660d3f62011-12-09 11:18:51 -0500871 mutex_lock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -0400872 ret = btrfs_delalloc_reserve_space(inode,
873 num_pages << PAGE_CACHE_SHIFT);
Josef Bacik660d3f62011-12-09 11:18:51 -0500874 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -0400875 if (ret)
876 return ret;
877again:
878 ret = 0;
879 i_done = 0;
880
881 /* step one, lock all the pages */
882 for (i = 0; i < num_pages; i++) {
883 struct page *page;
Josef Bacika94733d2011-07-11 10:47:06 -0400884 page = find_or_create_page(inode->i_mapping,
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400885 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400886 if (!page)
887 break;
888
889 if (!PageUptodate(page)) {
890 btrfs_readpage(NULL, page);
891 lock_page(page);
892 if (!PageUptodate(page)) {
893 unlock_page(page);
894 page_cache_release(page);
895 ret = -EIO;
896 break;
897 }
898 }
899 isize = i_size_read(inode);
900 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
901 if (!isize || page->index > file_end ||
902 page->mapping != inode->i_mapping) {
903 /* whoops, we blew past eof, skip this page */
904 unlock_page(page);
905 page_cache_release(page);
906 break;
907 }
908 pages[i] = page;
909 i_done++;
910 }
911 if (!i_done || ret)
912 goto out;
913
914 if (!(inode->i_sb->s_flags & MS_ACTIVE))
915 goto out;
916
917 /*
918 * so now we have a nice long stream of locked
919 * and up to date pages, lets wait on them
920 */
921 for (i = 0; i < i_done; i++)
922 wait_on_page_writeback(pages[i]);
923
924 page_start = page_offset(pages[0]);
925 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
926
927 lock_extent_bits(&BTRFS_I(inode)->io_tree,
928 page_start, page_end - 1, 0, &cached_state,
929 GFP_NOFS);
930 ordered = btrfs_lookup_first_ordered_extent(inode, page_end - 1);
931 if (ordered &&
932 ordered->file_offset + ordered->len > page_start &&
933 ordered->file_offset < page_end) {
934 btrfs_put_ordered_extent(ordered);
935 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
936 page_start, page_end - 1,
937 &cached_state, GFP_NOFS);
938 for (i = 0; i < i_done; i++) {
939 unlock_page(pages[i]);
940 page_cache_release(pages[i]);
941 }
942 btrfs_wait_ordered_range(inode, page_start,
943 page_end - page_start);
944 goto again;
945 }
946 if (ordered)
947 btrfs_put_ordered_extent(ordered);
948
949 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
950 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
951 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
952 GFP_NOFS);
953
954 if (i_done != num_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +0000955 spin_lock(&BTRFS_I(inode)->lock);
956 BTRFS_I(inode)->outstanding_extents++;
957 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -0400958 btrfs_delalloc_release_space(inode,
959 (num_pages - i_done) << PAGE_CACHE_SHIFT);
960 }
961
962
963 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
964 &cached_state);
965
966 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
967 page_start, page_end - 1, &cached_state,
968 GFP_NOFS);
969
970 for (i = 0; i < i_done; i++) {
971 clear_page_dirty_for_io(pages[i]);
972 ClearPageChecked(pages[i]);
973 set_page_extent_mapped(pages[i]);
974 set_page_dirty(pages[i]);
975 unlock_page(pages[i]);
976 page_cache_release(pages[i]);
977 }
978 return i_done;
979out:
980 for (i = 0; i < i_done; i++) {
981 unlock_page(pages[i]);
982 page_cache_release(pages[i]);
983 }
984 btrfs_delalloc_release_space(inode, num_pages << PAGE_CACHE_SHIFT);
985 return ret;
986
987}
988
989int btrfs_defrag_file(struct inode *inode, struct file *file,
990 struct btrfs_ioctl_defrag_range_args *range,
991 u64 newer_than, unsigned long max_to_defrag)
992{
993 struct btrfs_root *root = BTRFS_I(inode)->root;
994 struct btrfs_super_block *disk_super;
995 struct file_ra_state *ra = NULL;
996 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +0800997 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400998 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -0500999 u64 last_len = 0;
1000 u64 skip = 0;
1001 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001002 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001003 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001004 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001005 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001006 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001007 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001008 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001009 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1010 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001011 u64 new_align = ~((u64)128 * 1024 - 1);
1012 struct page **pages = NULL;
1013
1014 if (extent_thresh == 0)
1015 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001016
1017 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1018 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1019 return -EINVAL;
1020 if (range->compress_type)
1021 compress_type = range->compress_type;
1022 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001023
Li Zefan151a31b2011-09-02 15:56:39 +08001024 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001025 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001026
Chris Mason4cb53002011-05-24 15:35:30 -04001027 /*
1028 * if we were not given a file, allocate a readahead
1029 * context
1030 */
1031 if (!file) {
1032 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1033 if (!ra)
1034 return -ENOMEM;
1035 file_ra_state_init(ra, inode->i_mapping);
1036 } else {
1037 ra = &file->f_ra;
1038 }
1039
Li Zefan008873e2011-09-02 15:57:07 +08001040 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001041 GFP_NOFS);
1042 if (!pages) {
1043 ret = -ENOMEM;
1044 goto out_ra;
1045 }
1046
1047 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001048 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001049 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001050 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1051 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001052 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001053 }
1054
Chris Mason4cb53002011-05-24 15:35:30 -04001055 if (newer_than) {
1056 ret = find_new_extents(root, inode, newer_than,
1057 &newer_off, 64 * 1024);
1058 if (!ret) {
1059 range->start = newer_off;
1060 /*
1061 * we always align our defrag to help keep
1062 * the extents in the file evenly spaced
1063 */
1064 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001065 } else
1066 goto out_ra;
1067 } else {
1068 i = range->start >> PAGE_CACHE_SHIFT;
1069 }
1070 if (!max_to_defrag)
Li Zefan5ca49662011-09-02 15:56:55 +08001071 max_to_defrag = last_index;
Chris Mason4cb53002011-05-24 15:35:30 -04001072
Li Zefan2a0f7f52011-10-10 15:43:34 -04001073 /*
1074 * make writeback starts from i, so the defrag range can be
1075 * written sequentially.
1076 */
1077 if (i < inode->i_mapping->writeback_index)
1078 inode->i_mapping->writeback_index = i;
1079
Chris Masonf7f43cc2011-10-11 11:41:40 -04001080 while (i <= last_index && defrag_count < max_to_defrag &&
1081 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1082 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001083 /*
1084 * make sure we stop running if someone unmounts
1085 * the FS
1086 */
1087 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1088 break;
1089
1090 if (!newer_than &&
1091 !should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Chris Mason1e701a32010-03-11 09:42:04 -05001092 PAGE_CACHE_SIZE,
Chris Mason4cb53002011-05-24 15:35:30 -04001093 extent_thresh,
Chris Mason1e701a32010-03-11 09:42:04 -05001094 &last_len, &skip,
Chris Mason940100a2010-03-10 10:52:59 -05001095 &defrag_end)) {
1096 unsigned long next;
1097 /*
1098 * the should_defrag function tells us how much to skip
1099 * bump our counter by the suggested amount
1100 */
1101 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1102 i = max(i + 1, next);
1103 continue;
1104 }
Li Zefan008873e2011-09-02 15:57:07 +08001105
1106 if (!newer_than) {
1107 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1108 PAGE_CACHE_SHIFT) - i;
1109 cluster = min(cluster, max_cluster);
1110 } else {
1111 cluster = max_cluster;
1112 }
1113
Chris Mason1e701a32010-03-11 09:42:04 -05001114 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001115 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001116
Li Zefan008873e2011-09-02 15:57:07 +08001117 if (i + cluster > ra_index) {
1118 ra_index = max(i, ra_index);
1119 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1120 cluster);
1121 ra_index += max_cluster;
1122 }
Chris Mason940100a2010-03-10 10:52:59 -05001123
Li Zefan008873e2011-09-02 15:57:07 +08001124 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Chris Mason4cb53002011-05-24 15:35:30 -04001125 if (ret < 0)
1126 goto out_ra;
Chris Mason940100a2010-03-10 10:52:59 -05001127
Chris Mason4cb53002011-05-24 15:35:30 -04001128 defrag_count += ret;
1129 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Chris Mason4cb53002011-05-24 15:35:30 -04001130
1131 if (newer_than) {
1132 if (newer_off == (u64)-1)
1133 break;
1134
1135 newer_off = max(newer_off + 1,
1136 (u64)i << PAGE_CACHE_SHIFT);
1137
1138 ret = find_new_extents(root, inode,
1139 newer_than, &newer_off,
1140 64 * 1024);
1141 if (!ret) {
1142 range->start = newer_off;
1143 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001144 } else {
1145 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001146 }
Chris Mason4cb53002011-05-24 15:35:30 -04001147 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001148 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001149 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001150 last_len += ret << PAGE_CACHE_SHIFT;
1151 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001152 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001153 last_len = 0;
1154 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001155 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001156 }
1157
Chris Mason1e701a32010-03-11 09:42:04 -05001158 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1159 filemap_flush(inode->i_mapping);
1160
1161 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1162 /* the filemap_flush will queue IO into the worker threads, but
1163 * we have to make sure the IO is actually started and that
1164 * ordered extents get created before we return
1165 */
1166 atomic_inc(&root->fs_info->async_submit_draining);
1167 while (atomic_read(&root->fs_info->nr_async_submits) ||
1168 atomic_read(&root->fs_info->async_delalloc_pages)) {
1169 wait_event(root->fs_info->async_submit_wait,
1170 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1171 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1172 }
1173 atomic_dec(&root->fs_info->async_submit_draining);
1174
1175 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001176 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001177 mutex_unlock(&inode->i_mutex);
1178 }
1179
David Sterba6c417612011-04-13 15:41:04 +02001180 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001181 features = btrfs_super_incompat_flags(disk_super);
1182 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1183 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1184 btrfs_set_super_incompat_flags(disk_super, features);
1185 }
1186
Diego Calleja60ccf822011-09-01 16:33:57 +02001187 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001188
Chris Mason4cb53002011-05-24 15:35:30 -04001189out_ra:
1190 if (!file)
1191 kfree(ra);
1192 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001193 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001194}
1195
Yan, Zheng76dda932009-09-21 16:00:26 -04001196static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1197 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001198{
1199 u64 new_size;
1200 u64 old_size;
1201 u64 devid = 1;
1202 struct btrfs_ioctl_vol_args *vol_args;
1203 struct btrfs_trans_handle *trans;
1204 struct btrfs_device *device = NULL;
1205 char *sizestr;
1206 char *devstr = NULL;
1207 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001208 int mod = 0;
1209
Yan Zhengc146afa2008-11-12 14:34:12 -05001210 if (root->fs_info->sb->s_flags & MS_RDONLY)
1211 return -EROFS;
1212
Chris Masone441d542009-01-05 16:57:23 -05001213 if (!capable(CAP_SYS_ADMIN))
1214 return -EPERM;
1215
Li Zefandae7b662009-04-08 15:06:54 +08001216 vol_args = memdup_user(arg, sizeof(*vol_args));
1217 if (IS_ERR(vol_args))
1218 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -04001219
1220 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001221
Chris Mason7d9eb122008-07-08 14:19:17 -04001222 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001223 sizestr = vol_args->name;
1224 devstr = strchr(sizestr, ':');
1225 if (devstr) {
1226 char *end;
1227 sizestr = devstr + 1;
1228 *devstr = '\0';
1229 devstr = vol_args->name;
1230 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001231 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001232 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001233 }
Yan Zheng2b820322008-11-17 21:11:30 -05001234 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001235 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001236 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001237 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001238 ret = -EINVAL;
1239 goto out_unlock;
1240 }
1241 if (!strcmp(sizestr, "max"))
1242 new_size = device->bdev->bd_inode->i_size;
1243 else {
1244 if (sizestr[0] == '-') {
1245 mod = -1;
1246 sizestr++;
1247 } else if (sizestr[0] == '+') {
1248 mod = 1;
1249 sizestr++;
1250 }
Akinobu Mita91748462010-02-28 10:59:11 +00001251 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001252 if (new_size == 0) {
1253 ret = -EINVAL;
1254 goto out_unlock;
1255 }
1256 }
1257
1258 old_size = device->total_bytes;
1259
1260 if (mod < 0) {
1261 if (new_size > old_size) {
1262 ret = -EINVAL;
1263 goto out_unlock;
1264 }
1265 new_size = old_size - new_size;
1266 } else if (mod > 0) {
1267 new_size = old_size + new_size;
1268 }
1269
1270 if (new_size < 256 * 1024 * 1024) {
1271 ret = -EINVAL;
1272 goto out_unlock;
1273 }
1274 if (new_size > device->bdev->bd_inode->i_size) {
1275 ret = -EFBIG;
1276 goto out_unlock;
1277 }
1278
1279 do_div(new_size, root->sectorsize);
1280 new_size *= root->sectorsize;
1281
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001282 printk(KERN_INFO "btrfs: new size for %s is %llu\n",
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001283 device->name, (unsigned long long)new_size);
1284
1285 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001286 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001287 if (IS_ERR(trans)) {
1288 ret = PTR_ERR(trans);
1289 goto out_unlock;
1290 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001291 ret = btrfs_grow_device(trans, device, new_size);
1292 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001293 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001294 ret = btrfs_shrink_device(device, new_size);
1295 }
1296
1297out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -04001298 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001299 kfree(vol_args);
1300 return ret;
1301}
1302
Sage Weil72fd0322010-10-29 15:41:32 -04001303static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1304 char *name,
1305 unsigned long fd,
1306 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001307 u64 *transid,
1308 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001309{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001310 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001311 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001312 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001313 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001314
Yan Zhengc146afa2008-11-12 14:34:12 -05001315 if (root->fs_info->sb->s_flags & MS_RDONLY)
1316 return -EROFS;
1317
Sage Weil72fd0322010-10-29 15:41:32 -04001318 namelen = strlen(name);
1319 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001320 ret = -EINVAL;
1321 goto out;
1322 }
1323
Chris Mason3de45862008-11-17 21:02:50 -05001324 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001325 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001326 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001327 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001328 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001329 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001330 if (!src_file) {
1331 ret = -EINVAL;
1332 goto out;
1333 }
1334
1335 src_inode = src_file->f_path.dentry->d_inode;
1336 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001337 printk(KERN_INFO "btrfs: Snapshot src from "
1338 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001339 ret = -EINVAL;
1340 fput(src_file);
1341 goto out;
1342 }
Sage Weil72fd0322010-10-29 15:41:32 -04001343 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1344 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001345 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001346 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001347 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001348out:
Sage Weil72fd0322010-10-29 15:41:32 -04001349 return ret;
1350}
1351
1352static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001353 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001354{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001355 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001356 int ret;
1357
Li Zefanfa0d2b92010-12-20 15:53:28 +08001358 vol_args = memdup_user(arg, sizeof(*vol_args));
1359 if (IS_ERR(vol_args))
1360 return PTR_ERR(vol_args);
1361 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001362
Li Zefanfa0d2b92010-12-20 15:53:28 +08001363 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001364 vol_args->fd, subvol,
1365 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001366
Li Zefanfa0d2b92010-12-20 15:53:28 +08001367 kfree(vol_args);
1368 return ret;
1369}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001370
Li Zefanfa0d2b92010-12-20 15:53:28 +08001371static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1372 void __user *arg, int subvol)
1373{
1374 struct btrfs_ioctl_vol_args_v2 *vol_args;
1375 int ret;
1376 u64 transid = 0;
1377 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001378 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001379
Li Zefanfa0d2b92010-12-20 15:53:28 +08001380 vol_args = memdup_user(arg, sizeof(*vol_args));
1381 if (IS_ERR(vol_args))
1382 return PTR_ERR(vol_args);
1383 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001384
Li Zefanb83cc962010-12-20 16:04:08 +08001385 if (vol_args->flags &
1386 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1387 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001388 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001389 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001390
1391 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1392 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001393 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1394 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001395
1396 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001397 vol_args->fd, subvol,
1398 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001399
1400 if (ret == 0 && ptr &&
1401 copy_to_user(arg +
1402 offsetof(struct btrfs_ioctl_vol_args_v2,
1403 transid), ptr, sizeof(*ptr)))
1404 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001405out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001406 kfree(vol_args);
1407 return ret;
1408}
1409
Li Zefan0caa1022010-12-20 16:30:25 +08001410static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1411 void __user *arg)
1412{
1413 struct inode *inode = fdentry(file)->d_inode;
1414 struct btrfs_root *root = BTRFS_I(inode)->root;
1415 int ret = 0;
1416 u64 flags = 0;
1417
Li Zefan33345d012011-04-20 10:31:50 +08001418 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001419 return -EINVAL;
1420
1421 down_read(&root->fs_info->subvol_sem);
1422 if (btrfs_root_readonly(root))
1423 flags |= BTRFS_SUBVOL_RDONLY;
1424 up_read(&root->fs_info->subvol_sem);
1425
1426 if (copy_to_user(arg, &flags, sizeof(flags)))
1427 ret = -EFAULT;
1428
1429 return ret;
1430}
1431
1432static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1433 void __user *arg)
1434{
1435 struct inode *inode = fdentry(file)->d_inode;
1436 struct btrfs_root *root = BTRFS_I(inode)->root;
1437 struct btrfs_trans_handle *trans;
1438 u64 root_flags;
1439 u64 flags;
1440 int ret = 0;
1441
1442 if (root->fs_info->sb->s_flags & MS_RDONLY)
1443 return -EROFS;
1444
Li Zefan33345d012011-04-20 10:31:50 +08001445 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001446 return -EINVAL;
1447
1448 if (copy_from_user(&flags, arg, sizeof(flags)))
1449 return -EFAULT;
1450
Li Zefanb4dc2b82011-02-16 06:06:34 +00001451 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001452 return -EINVAL;
1453
1454 if (flags & ~BTRFS_SUBVOL_RDONLY)
1455 return -EOPNOTSUPP;
1456
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001457 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001458 return -EACCES;
1459
Li Zefan0caa1022010-12-20 16:30:25 +08001460 down_write(&root->fs_info->subvol_sem);
1461
1462 /* nothing to do */
1463 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1464 goto out;
1465
1466 root_flags = btrfs_root_flags(&root->root_item);
1467 if (flags & BTRFS_SUBVOL_RDONLY)
1468 btrfs_set_root_flags(&root->root_item,
1469 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1470 else
1471 btrfs_set_root_flags(&root->root_item,
1472 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1473
1474 trans = btrfs_start_transaction(root, 1);
1475 if (IS_ERR(trans)) {
1476 ret = PTR_ERR(trans);
1477 goto out_reset;
1478 }
1479
Li Zefanb4dc2b82011-02-16 06:06:34 +00001480 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001481 &root->root_key, &root->root_item);
1482
1483 btrfs_commit_transaction(trans, root);
1484out_reset:
1485 if (ret)
1486 btrfs_set_root_flags(&root->root_item, root_flags);
1487out:
1488 up_write(&root->fs_info->subvol_sem);
1489 return ret;
1490}
1491
Yan, Zheng76dda932009-09-21 16:00:26 -04001492/*
1493 * helper to check if the subvolume references other subvolumes
1494 */
1495static noinline int may_destroy_subvol(struct btrfs_root *root)
1496{
1497 struct btrfs_path *path;
1498 struct btrfs_key key;
1499 int ret;
1500
1501 path = btrfs_alloc_path();
1502 if (!path)
1503 return -ENOMEM;
1504
1505 key.objectid = root->root_key.objectid;
1506 key.type = BTRFS_ROOT_REF_KEY;
1507 key.offset = (u64)-1;
1508
1509 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1510 &key, path, 0, 0);
1511 if (ret < 0)
1512 goto out;
1513 BUG_ON(ret == 0);
1514
1515 ret = 0;
1516 if (path->slots[0] > 0) {
1517 path->slots[0]--;
1518 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1519 if (key.objectid == root->root_key.objectid &&
1520 key.type == BTRFS_ROOT_REF_KEY)
1521 ret = -ENOTEMPTY;
1522 }
1523out:
1524 btrfs_free_path(path);
1525 return ret;
1526}
1527
Chris Masonac8e9812010-02-28 15:39:26 -05001528static noinline int key_in_sk(struct btrfs_key *key,
1529 struct btrfs_ioctl_search_key *sk)
1530{
Chris Masonabc6e132010-03-18 12:10:08 -04001531 struct btrfs_key test;
1532 int ret;
1533
1534 test.objectid = sk->min_objectid;
1535 test.type = sk->min_type;
1536 test.offset = sk->min_offset;
1537
1538 ret = btrfs_comp_cpu_keys(key, &test);
1539 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001540 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001541
1542 test.objectid = sk->max_objectid;
1543 test.type = sk->max_type;
1544 test.offset = sk->max_offset;
1545
1546 ret = btrfs_comp_cpu_keys(key, &test);
1547 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001548 return 0;
1549 return 1;
1550}
1551
1552static noinline int copy_to_sk(struct btrfs_root *root,
1553 struct btrfs_path *path,
1554 struct btrfs_key *key,
1555 struct btrfs_ioctl_search_key *sk,
1556 char *buf,
1557 unsigned long *sk_offset,
1558 int *num_found)
1559{
1560 u64 found_transid;
1561 struct extent_buffer *leaf;
1562 struct btrfs_ioctl_search_header sh;
1563 unsigned long item_off;
1564 unsigned long item_len;
1565 int nritems;
1566 int i;
1567 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001568 int ret = 0;
1569
1570 leaf = path->nodes[0];
1571 slot = path->slots[0];
1572 nritems = btrfs_header_nritems(leaf);
1573
1574 if (btrfs_header_generation(leaf) > sk->max_transid) {
1575 i = nritems;
1576 goto advance_key;
1577 }
1578 found_transid = btrfs_header_generation(leaf);
1579
1580 for (i = slot; i < nritems; i++) {
1581 item_off = btrfs_item_ptr_offset(leaf, i);
1582 item_len = btrfs_item_size_nr(leaf, i);
1583
1584 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1585 item_len = 0;
1586
1587 if (sizeof(sh) + item_len + *sk_offset >
1588 BTRFS_SEARCH_ARGS_BUFSIZE) {
1589 ret = 1;
1590 goto overflow;
1591 }
1592
1593 btrfs_item_key_to_cpu(leaf, key, i);
1594 if (!key_in_sk(key, sk))
1595 continue;
1596
1597 sh.objectid = key->objectid;
1598 sh.offset = key->offset;
1599 sh.type = key->type;
1600 sh.len = item_len;
1601 sh.transid = found_transid;
1602
1603 /* copy search result header */
1604 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1605 *sk_offset += sizeof(sh);
1606
1607 if (item_len) {
1608 char *p = buf + *sk_offset;
1609 /* copy the item */
1610 read_extent_buffer(leaf, p,
1611 item_off, item_len);
1612 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001613 }
Hugo Millse2156862011-05-14 17:43:41 +00001614 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001615
1616 if (*num_found >= sk->nr_items)
1617 break;
1618 }
1619advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001620 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001621 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1622 key->offset++;
1623 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1624 key->offset = 0;
1625 key->type++;
1626 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1627 key->offset = 0;
1628 key->type = 0;
1629 key->objectid++;
1630 } else
1631 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001632overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001633 return ret;
1634}
1635
1636static noinline int search_ioctl(struct inode *inode,
1637 struct btrfs_ioctl_search_args *args)
1638{
1639 struct btrfs_root *root;
1640 struct btrfs_key key;
1641 struct btrfs_key max_key;
1642 struct btrfs_path *path;
1643 struct btrfs_ioctl_search_key *sk = &args->key;
1644 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1645 int ret;
1646 int num_found = 0;
1647 unsigned long sk_offset = 0;
1648
1649 path = btrfs_alloc_path();
1650 if (!path)
1651 return -ENOMEM;
1652
1653 if (sk->tree_id == 0) {
1654 /* search the root of the inode that was passed */
1655 root = BTRFS_I(inode)->root;
1656 } else {
1657 key.objectid = sk->tree_id;
1658 key.type = BTRFS_ROOT_ITEM_KEY;
1659 key.offset = (u64)-1;
1660 root = btrfs_read_fs_root_no_name(info, &key);
1661 if (IS_ERR(root)) {
1662 printk(KERN_ERR "could not find root %llu\n",
1663 sk->tree_id);
1664 btrfs_free_path(path);
1665 return -ENOENT;
1666 }
1667 }
1668
1669 key.objectid = sk->min_objectid;
1670 key.type = sk->min_type;
1671 key.offset = sk->min_offset;
1672
1673 max_key.objectid = sk->max_objectid;
1674 max_key.type = sk->max_type;
1675 max_key.offset = sk->max_offset;
1676
1677 path->keep_locks = 1;
1678
1679 while(1) {
1680 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1681 sk->min_transid);
1682 if (ret != 0) {
1683 if (ret > 0)
1684 ret = 0;
1685 goto err;
1686 }
1687 ret = copy_to_sk(root, path, &key, sk, args->buf,
1688 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001689 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001690 if (ret || num_found >= sk->nr_items)
1691 break;
1692
1693 }
1694 ret = 0;
1695err:
1696 sk->nr_items = num_found;
1697 btrfs_free_path(path);
1698 return ret;
1699}
1700
1701static noinline int btrfs_ioctl_tree_search(struct file *file,
1702 void __user *argp)
1703{
1704 struct btrfs_ioctl_search_args *args;
1705 struct inode *inode;
1706 int ret;
1707
1708 if (!capable(CAP_SYS_ADMIN))
1709 return -EPERM;
1710
Julia Lawall2354d082010-10-29 15:14:18 -04001711 args = memdup_user(argp, sizeof(*args));
1712 if (IS_ERR(args))
1713 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001714
Chris Masonac8e9812010-02-28 15:39:26 -05001715 inode = fdentry(file)->d_inode;
1716 ret = search_ioctl(inode, args);
1717 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1718 ret = -EFAULT;
1719 kfree(args);
1720 return ret;
1721}
1722
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001723/*
Chris Masonac8e9812010-02-28 15:39:26 -05001724 * Search INODE_REFs to identify path name of 'dirid' directory
1725 * in a 'tree_id' tree. and sets path name to 'name'.
1726 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001727static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1728 u64 tree_id, u64 dirid, char *name)
1729{
1730 struct btrfs_root *root;
1731 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001732 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001733 int ret = -1;
1734 int slot;
1735 int len;
1736 int total_len = 0;
1737 struct btrfs_inode_ref *iref;
1738 struct extent_buffer *l;
1739 struct btrfs_path *path;
1740
1741 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1742 name[0]='\0';
1743 return 0;
1744 }
1745
1746 path = btrfs_alloc_path();
1747 if (!path)
1748 return -ENOMEM;
1749
Chris Masonac8e9812010-02-28 15:39:26 -05001750 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001751
1752 key.objectid = tree_id;
1753 key.type = BTRFS_ROOT_ITEM_KEY;
1754 key.offset = (u64)-1;
1755 root = btrfs_read_fs_root_no_name(info, &key);
1756 if (IS_ERR(root)) {
1757 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001758 ret = -ENOENT;
1759 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001760 }
1761
1762 key.objectid = dirid;
1763 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001764 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001765
1766 while(1) {
1767 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1768 if (ret < 0)
1769 goto out;
1770
1771 l = path->nodes[0];
1772 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001773 if (ret > 0 && slot > 0)
1774 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001775 btrfs_item_key_to_cpu(l, &key, slot);
1776
1777 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001778 key.type != BTRFS_INODE_REF_KEY)) {
1779 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001780 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001781 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001782
1783 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1784 len = btrfs_inode_ref_name_len(l, iref);
1785 ptr -= len + 1;
1786 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001787 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001788 goto out;
1789
1790 *(ptr + len) = '/';
1791 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1792
1793 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1794 break;
1795
David Sterbab3b4aa72011-04-21 01:20:15 +02001796 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001797 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001798 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001799 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001800 }
Chris Masonac8e9812010-02-28 15:39:26 -05001801 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001802 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001803 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001804 name[total_len]='\0';
1805 ret = 0;
1806out:
1807 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001808 return ret;
1809}
1810
1811static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1812 void __user *argp)
1813{
1814 struct btrfs_ioctl_ino_lookup_args *args;
1815 struct inode *inode;
1816 int ret;
1817
1818 if (!capable(CAP_SYS_ADMIN))
1819 return -EPERM;
1820
Julia Lawall2354d082010-10-29 15:14:18 -04001821 args = memdup_user(argp, sizeof(*args));
1822 if (IS_ERR(args))
1823 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001824
Chris Masonac8e9812010-02-28 15:39:26 -05001825 inode = fdentry(file)->d_inode;
1826
Chris Mason1b53ac42010-03-18 12:17:05 -04001827 if (args->treeid == 0)
1828 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1829
Chris Masonac8e9812010-02-28 15:39:26 -05001830 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1831 args->treeid, args->objectid,
1832 args->name);
1833
1834 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1835 ret = -EFAULT;
1836
1837 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001838 return ret;
1839}
1840
Yan, Zheng76dda932009-09-21 16:00:26 -04001841static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1842 void __user *arg)
1843{
1844 struct dentry *parent = fdentry(file);
1845 struct dentry *dentry;
1846 struct inode *dir = parent->d_inode;
1847 struct inode *inode;
1848 struct btrfs_root *root = BTRFS_I(dir)->root;
1849 struct btrfs_root *dest = NULL;
1850 struct btrfs_ioctl_vol_args *vol_args;
1851 struct btrfs_trans_handle *trans;
1852 int namelen;
1853 int ret;
1854 int err = 0;
1855
Yan, Zheng76dda932009-09-21 16:00:26 -04001856 vol_args = memdup_user(arg, sizeof(*vol_args));
1857 if (IS_ERR(vol_args))
1858 return PTR_ERR(vol_args);
1859
1860 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1861 namelen = strlen(vol_args->name);
1862 if (strchr(vol_args->name, '/') ||
1863 strncmp(vol_args->name, "..", namelen) == 0) {
1864 err = -EINVAL;
1865 goto out;
1866 }
1867
1868 err = mnt_want_write(file->f_path.mnt);
1869 if (err)
1870 goto out;
1871
1872 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1873 dentry = lookup_one_len(vol_args->name, parent, namelen);
1874 if (IS_ERR(dentry)) {
1875 err = PTR_ERR(dentry);
1876 goto out_unlock_dir;
1877 }
1878
1879 if (!dentry->d_inode) {
1880 err = -ENOENT;
1881 goto out_dput;
1882 }
1883
1884 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001885 dest = BTRFS_I(inode)->root;
1886 if (!capable(CAP_SYS_ADMIN)){
1887 /*
1888 * Regular user. Only allow this with a special mount
1889 * option, when the user has write+exec access to the
1890 * subvol root, and when rmdir(2) would have been
1891 * allowed.
1892 *
1893 * Note that this is _not_ check that the subvol is
1894 * empty or doesn't contain data that we wouldn't
1895 * otherwise be able to delete.
1896 *
1897 * Users who want to delete empty subvols should try
1898 * rmdir(2).
1899 */
1900 err = -EPERM;
1901 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1902 goto out_dput;
1903
1904 /*
1905 * Do not allow deletion if the parent dir is the same
1906 * as the dir to be deleted. That means the ioctl
1907 * must be called on the dentry referencing the root
1908 * of the subvol, not a random directory contained
1909 * within it.
1910 */
1911 err = -EINVAL;
1912 if (root == dest)
1913 goto out_dput;
1914
1915 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1916 if (err)
1917 goto out_dput;
1918
1919 /* check if subvolume may be deleted by a non-root user */
1920 err = btrfs_may_delete(dir, dentry, 1);
1921 if (err)
1922 goto out_dput;
1923 }
1924
Li Zefan33345d012011-04-20 10:31:50 +08001925 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04001926 err = -EINVAL;
1927 goto out_dput;
1928 }
1929
Yan, Zheng76dda932009-09-21 16:00:26 -04001930 mutex_lock(&inode->i_mutex);
1931 err = d_invalidate(dentry);
1932 if (err)
1933 goto out_unlock;
1934
1935 down_write(&root->fs_info->subvol_sem);
1936
1937 err = may_destroy_subvol(dest);
1938 if (err)
1939 goto out_up_write;
1940
Yan, Zhenga22285a2010-05-16 10:48:46 -04001941 trans = btrfs_start_transaction(root, 0);
1942 if (IS_ERR(trans)) {
1943 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00001944 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04001945 }
1946 trans->block_rsv = &root->fs_info->global_block_rsv;
1947
Yan, Zheng76dda932009-09-21 16:00:26 -04001948 ret = btrfs_unlink_subvol(trans, root, dir,
1949 dest->root_key.objectid,
1950 dentry->d_name.name,
1951 dentry->d_name.len);
1952 BUG_ON(ret);
1953
1954 btrfs_record_root_in_trans(trans, dest);
1955
1956 memset(&dest->root_item.drop_progress, 0,
1957 sizeof(dest->root_item.drop_progress));
1958 dest->root_item.drop_level = 0;
1959 btrfs_set_root_refs(&dest->root_item, 0);
1960
Yan, Zhengd68fc572010-05-16 10:49:58 -04001961 if (!xchg(&dest->orphan_item_inserted, 1)) {
1962 ret = btrfs_insert_orphan_item(trans,
1963 root->fs_info->tree_root,
1964 dest->root_key.objectid);
1965 BUG_ON(ret);
1966 }
Yan, Zheng76dda932009-09-21 16:00:26 -04001967
Sage Weil531cb132010-10-29 15:41:32 -04001968 ret = btrfs_end_transaction(trans, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04001969 BUG_ON(ret);
1970 inode->i_flags |= S_DEAD;
1971out_up_write:
1972 up_write(&root->fs_info->subvol_sem);
1973out_unlock:
1974 mutex_unlock(&inode->i_mutex);
1975 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04001976 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04001977 btrfs_invalidate_inodes(dest);
1978 d_delete(dentry);
1979 }
1980out_dput:
1981 dput(dentry);
1982out_unlock_dir:
1983 mutex_unlock(&dir->i_mutex);
1984 mnt_drop_write(file->f_path.mnt);
1985out:
1986 kfree(vol_args);
1987 return err;
1988}
1989
Chris Mason1e701a32010-03-11 09:42:04 -05001990static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001991{
1992 struct inode *inode = fdentry(file)->d_inode;
1993 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05001994 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05001995 int ret;
1996
Li Zefanb83cc962010-12-20 16:04:08 +08001997 if (btrfs_root_readonly(root))
1998 return -EROFS;
1999
Yan Zhengc146afa2008-11-12 14:34:12 -05002000 ret = mnt_want_write(file->f_path.mnt);
2001 if (ret)
2002 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002003
2004 switch (inode->i_mode & S_IFMT) {
2005 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002006 if (!capable(CAP_SYS_ADMIN)) {
2007 ret = -EPERM;
2008 goto out;
2009 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002010 ret = btrfs_defrag_root(root, 0);
2011 if (ret)
2012 goto out;
2013 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002014 break;
2015 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002016 if (!(file->f_mode & FMODE_WRITE)) {
2017 ret = -EINVAL;
2018 goto out;
2019 }
Chris Mason1e701a32010-03-11 09:42:04 -05002020
2021 range = kzalloc(sizeof(*range), GFP_KERNEL);
2022 if (!range) {
2023 ret = -ENOMEM;
2024 goto out;
2025 }
2026
2027 if (argp) {
2028 if (copy_from_user(range, argp,
2029 sizeof(*range))) {
2030 ret = -EFAULT;
2031 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002032 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002033 }
2034 /* compression requires us to start the IO */
2035 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2036 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2037 range->extent_thresh = (u32)-1;
2038 }
2039 } else {
2040 /* the rest are all set to zero by kzalloc */
2041 range->len = (u64)-1;
2042 }
Chris Mason4cb53002011-05-24 15:35:30 -04002043 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2044 range, 0, 0);
2045 if (ret > 0)
2046 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002047 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002048 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002049 default:
2050 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002051 }
Chris Masone441d542009-01-05 16:57:23 -05002052out:
Yan Zhengab67b7c2008-12-19 10:58:39 -05002053 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -05002054 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002055}
2056
Christoph Hellwigb2950862008-12-02 09:54:17 -05002057static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002058{
2059 struct btrfs_ioctl_vol_args *vol_args;
2060 int ret;
2061
Chris Masone441d542009-01-05 16:57:23 -05002062 if (!capable(CAP_SYS_ADMIN))
2063 return -EPERM;
2064
Li Zefandae7b662009-04-08 15:06:54 +08002065 vol_args = memdup_user(arg, sizeof(*vol_args));
2066 if (IS_ERR(vol_args))
2067 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002068
Mark Fasheh5516e592008-07-24 12:20:14 -04002069 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002070 ret = btrfs_init_new_device(root, vol_args->name);
2071
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002072 kfree(vol_args);
2073 return ret;
2074}
2075
Christoph Hellwigb2950862008-12-02 09:54:17 -05002076static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002077{
2078 struct btrfs_ioctl_vol_args *vol_args;
2079 int ret;
2080
Chris Masone441d542009-01-05 16:57:23 -05002081 if (!capable(CAP_SYS_ADMIN))
2082 return -EPERM;
2083
Yan Zhengc146afa2008-11-12 14:34:12 -05002084 if (root->fs_info->sb->s_flags & MS_RDONLY)
2085 return -EROFS;
2086
Li Zefandae7b662009-04-08 15:06:54 +08002087 vol_args = memdup_user(arg, sizeof(*vol_args));
2088 if (IS_ERR(vol_args))
2089 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002090
Mark Fasheh5516e592008-07-24 12:20:14 -04002091 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002092 ret = btrfs_rm_device(root, vol_args->name);
2093
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002094 kfree(vol_args);
2095 return ret;
2096}
2097
Jan Schmidt475f6382011-03-11 15:41:01 +01002098static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2099{
Li Zefan027ed2f2011-06-08 08:27:56 +00002100 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002101 struct btrfs_device *device;
2102 struct btrfs_device *next;
2103 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002104 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002105
2106 if (!capable(CAP_SYS_ADMIN))
2107 return -EPERM;
2108
Li Zefan027ed2f2011-06-08 08:27:56 +00002109 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2110 if (!fi_args)
2111 return -ENOMEM;
2112
2113 fi_args->num_devices = fs_devices->num_devices;
2114 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002115
2116 mutex_lock(&fs_devices->device_list_mutex);
2117 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002118 if (device->devid > fi_args->max_id)
2119 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002120 }
2121 mutex_unlock(&fs_devices->device_list_mutex);
2122
Li Zefan027ed2f2011-06-08 08:27:56 +00002123 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2124 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002125
Li Zefan027ed2f2011-06-08 08:27:56 +00002126 kfree(fi_args);
2127 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002128}
2129
2130static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2131{
2132 struct btrfs_ioctl_dev_info_args *di_args;
2133 struct btrfs_device *dev;
2134 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2135 int ret = 0;
2136 char *s_uuid = NULL;
2137 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2138
2139 if (!capable(CAP_SYS_ADMIN))
2140 return -EPERM;
2141
2142 di_args = memdup_user(arg, sizeof(*di_args));
2143 if (IS_ERR(di_args))
2144 return PTR_ERR(di_args);
2145
2146 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2147 s_uuid = di_args->uuid;
2148
2149 mutex_lock(&fs_devices->device_list_mutex);
2150 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2151 mutex_unlock(&fs_devices->device_list_mutex);
2152
2153 if (!dev) {
2154 ret = -ENODEV;
2155 goto out;
2156 }
2157
2158 di_args->devid = dev->devid;
2159 di_args->bytes_used = dev->bytes_used;
2160 di_args->total_bytes = dev->total_bytes;
2161 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
2162 strncpy(di_args->path, dev->name, sizeof(di_args->path));
2163
2164out:
2165 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2166 ret = -EFAULT;
2167
2168 kfree(di_args);
2169 return ret;
2170}
2171
Yan, Zheng76dda932009-09-21 16:00:26 -04002172static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2173 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002174{
2175 struct inode *inode = fdentry(file)->d_inode;
2176 struct btrfs_root *root = BTRFS_I(inode)->root;
2177 struct file *src_file;
2178 struct inode *src;
2179 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002180 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002181 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002182 char *buf;
2183 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002184 u32 nritems;
2185 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002186 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002187 u64 len = olen;
2188 u64 bs = root->fs_info->sb->s_blocksize;
2189 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002190
Sage Weilc5c9cd42008-11-12 14:32:25 -05002191 /*
2192 * TODO:
2193 * - split compressed inline extents. annoying: we need to
2194 * decompress into destination's address_space (the file offset
2195 * may change, so source mapping won't do), then recompress (or
2196 * otherwise reinsert) a subrange.
2197 * - allow ranges within the same file to be cloned (provided
2198 * they don't overlap)?
2199 */
2200
Chris Masone441d542009-01-05 16:57:23 -05002201 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002202 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002203 return -EINVAL;
2204
Li Zefanb83cc962010-12-20 16:04:08 +08002205 if (btrfs_root_readonly(root))
2206 return -EROFS;
2207
Yan Zhengc146afa2008-11-12 14:34:12 -05002208 ret = mnt_want_write(file->f_path.mnt);
2209 if (ret)
2210 return ret;
2211
Sage Weilc5c9cd42008-11-12 14:32:25 -05002212 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002213 if (!src_file) {
2214 ret = -EBADF;
2215 goto out_drop_write;
2216 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002217
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002218 src = src_file->f_dentry->d_inode;
2219
Sage Weilc5c9cd42008-11-12 14:32:25 -05002220 ret = -EINVAL;
2221 if (src == inode)
2222 goto out_fput;
2223
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002224 /* the src must be open for reading */
2225 if (!(src_file->f_mode & FMODE_READ))
2226 goto out_fput;
2227
Li Zefan0e7b8242011-09-18 10:20:46 -04002228 /* don't make the dst file partly checksummed */
2229 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2230 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2231 goto out_fput;
2232
Yan Zhengae01a0a2008-08-04 23:23:47 -04002233 ret = -EISDIR;
2234 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002235 goto out_fput;
2236
Yan Zhengae01a0a2008-08-04 23:23:47 -04002237 ret = -EXDEV;
2238 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
2239 goto out_fput;
2240
2241 ret = -ENOMEM;
2242 buf = vmalloc(btrfs_level_size(root, 0));
2243 if (!buf)
2244 goto out_fput;
2245
2246 path = btrfs_alloc_path();
2247 if (!path) {
2248 vfree(buf);
2249 goto out_fput;
2250 }
2251 path->reada = 2;
2252
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002253 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002254 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2255 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002256 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002257 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2258 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002259 }
2260
Sage Weilc5c9cd42008-11-12 14:32:25 -05002261 /* determine range to clone */
2262 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002263 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002264 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002265 if (len == 0)
2266 olen = len = src->i_size - off;
2267 /* if we extend to eof, continue to block boundary */
2268 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002269 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002270
2271 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002272 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2273 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002274 goto out_unlock;
2275
Li Zefand525e8a2011-09-11 10:52:25 -04002276 if (destoff > inode->i_size) {
2277 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2278 if (ret)
2279 goto out_unlock;
2280 }
2281
Li Zefan71ef0782011-09-18 10:20:46 -04002282 /* truncate page cache pages from target inode range */
2283 truncate_inode_pages_range(&inode->i_data, destoff,
2284 PAGE_CACHE_ALIGN(destoff + len) - 1);
2285
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002286 /* do any pending delalloc/csum calc on src, one way or
2287 another, and lock file content */
2288 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002289 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002290 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Sage Weil9a019192010-10-29 15:37:33 -04002291 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2292 if (!ordered &&
2293 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2294 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002295 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002296 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002297 if (ordered)
2298 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002299 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002300 }
2301
Sage Weilc5c9cd42008-11-12 14:32:25 -05002302 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002303 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002304 key.type = BTRFS_EXTENT_DATA_KEY;
2305 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002306
2307 while (1) {
2308 /*
2309 * note the key will change type as we walk through the
2310 * tree.
2311 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002312 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002313 if (ret < 0)
2314 goto out;
2315
Yan Zhengae01a0a2008-08-04 23:23:47 -04002316 nritems = btrfs_header_nritems(path->nodes[0]);
2317 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002318 ret = btrfs_next_leaf(root, path);
2319 if (ret < 0)
2320 goto out;
2321 if (ret > 0)
2322 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002323 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002324 }
2325 leaf = path->nodes[0];
2326 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002327
Yan Zhengae01a0a2008-08-04 23:23:47 -04002328 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002329 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002330 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002331 break;
2332
Sage Weilc5c9cd42008-11-12 14:32:25 -05002333 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2334 struct btrfs_file_extent_item *extent;
2335 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002336 u32 size;
2337 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002338 u64 disko = 0, diskl = 0;
2339 u64 datao = 0, datal = 0;
2340 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002341 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002342
2343 size = btrfs_item_size_nr(leaf, slot);
2344 read_extent_buffer(leaf, buf,
2345 btrfs_item_ptr_offset(leaf, slot),
2346 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002347
2348 extent = btrfs_item_ptr(leaf, slot,
2349 struct btrfs_file_extent_item);
2350 comp = btrfs_file_extent_compression(leaf, extent);
2351 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002352 if (type == BTRFS_FILE_EXTENT_REG ||
2353 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002354 disko = btrfs_file_extent_disk_bytenr(leaf,
2355 extent);
2356 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2357 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002358 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002359 datal = btrfs_file_extent_num_bytes(leaf,
2360 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002361 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2362 /* take upper bound, may be compressed */
2363 datal = btrfs_file_extent_ram_bytes(leaf,
2364 extent);
2365 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002366 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002367
Sage Weil050006a2010-10-29 15:37:33 -04002368 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002369 key.offset >= off+len)
2370 goto next;
2371
Zheng Yan31840ae2008-09-23 13:14:14 -04002372 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002373 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002374 if (off <= key.offset)
2375 new_key.offset = key.offset + destoff - off;
2376 else
2377 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002378
Sage Weilb6f34092011-09-20 14:48:51 -04002379 /*
2380 * 1 - adjusting old extent (we may have to split it)
2381 * 1 - add new extent
2382 * 1 - inode update
2383 */
2384 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002385 if (IS_ERR(trans)) {
2386 ret = PTR_ERR(trans);
2387 goto out;
2388 }
2389
Chris Masonc8a894d2009-06-27 21:07:03 -04002390 if (type == BTRFS_FILE_EXTENT_REG ||
2391 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002392 /*
2393 * a | --- range to clone ---| b
2394 * | ------------- extent ------------- |
2395 */
2396
2397 /* substract range b */
2398 if (key.offset + datal > off + len)
2399 datal = off + len - key.offset;
2400
2401 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002402 if (off > key.offset) {
2403 datao += off - key.offset;
2404 datal -= off - key.offset;
2405 }
2406
Yan, Zhenga22285a2010-05-16 10:48:46 -04002407 ret = btrfs_drop_extents(trans, inode,
2408 new_key.offset,
2409 new_key.offset + datal,
2410 &hint_byte, 1);
2411 BUG_ON(ret);
2412
Sage Weilc5c9cd42008-11-12 14:32:25 -05002413 ret = btrfs_insert_empty_item(trans, root, path,
2414 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002415 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002416
2417 leaf = path->nodes[0];
2418 slot = path->slots[0];
2419 write_extent_buffer(leaf, buf,
2420 btrfs_item_ptr_offset(leaf, slot),
2421 size);
2422
2423 extent = btrfs_item_ptr(leaf, slot,
2424 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002425
Sage Weilc5c9cd42008-11-12 14:32:25 -05002426 /* disko == 0 means it's a hole */
2427 if (!disko)
2428 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002429
2430 btrfs_set_file_extent_offset(leaf, extent,
2431 datao);
2432 btrfs_set_file_extent_num_bytes(leaf, extent,
2433 datal);
2434 if (disko) {
2435 inode_add_bytes(inode, datal);
2436 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002437 disko, diskl, 0,
2438 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002439 btrfs_ino(inode),
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002441 BUG_ON(ret);
2442 }
2443 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2444 u64 skip = 0;
2445 u64 trim = 0;
2446 if (off > key.offset) {
2447 skip = off - key.offset;
2448 new_key.offset += skip;
2449 }
Chris Masond3977122009-01-05 21:25:51 -05002450
Sage Weilc5c9cd42008-11-12 14:32:25 -05002451 if (key.offset + datal > off+len)
2452 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002453
Sage Weilc5c9cd42008-11-12 14:32:25 -05002454 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002455 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002456 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002457 goto out;
2458 }
2459 size -= skip + trim;
2460 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002461
2462 ret = btrfs_drop_extents(trans, inode,
2463 new_key.offset,
2464 new_key.offset + datal,
2465 &hint_byte, 1);
2466 BUG_ON(ret);
2467
Sage Weilc5c9cd42008-11-12 14:32:25 -05002468 ret = btrfs_insert_empty_item(trans, root, path,
2469 &new_key, size);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002470 BUG_ON(ret);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002471
2472 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002473 u32 start =
2474 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002475 memmove(buf+start, buf+start+skip,
2476 datal);
2477 }
2478
2479 leaf = path->nodes[0];
2480 slot = path->slots[0];
2481 write_extent_buffer(leaf, buf,
2482 btrfs_item_ptr_offset(leaf, slot),
2483 size);
2484 inode_add_bytes(inode, datal);
2485 }
2486
2487 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002488 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002489
Yan, Zhenga22285a2010-05-16 10:48:46 -04002490 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002491
2492 /*
2493 * we round up to the block size at eof when
2494 * determining which extents to clone above,
2495 * but shouldn't round up the file size
2496 */
2497 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002498 if (endoff > destoff+olen)
2499 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002500 if (endoff > inode->i_size)
2501 btrfs_i_size_write(inode, endoff);
2502
Yan, Zhenga22285a2010-05-16 10:48:46 -04002503 ret = btrfs_update_inode(trans, root, inode);
2504 BUG_ON(ret);
2505 btrfs_end_transaction(trans, root);
2506 }
Chris Masond3977122009-01-05 21:25:51 -05002507next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002508 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002509 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002510 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002511 ret = 0;
2512out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002513 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002514 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002515out_unlock:
2516 mutex_unlock(&src->i_mutex);
2517 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002518 vfree(buf);
2519 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002520out_fput:
2521 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002522out_drop_write:
2523 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002524 return ret;
2525}
2526
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002527static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002528{
2529 struct btrfs_ioctl_clone_range_args args;
2530
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002531 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002532 return -EFAULT;
2533 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2534 args.src_length, args.dest_offset);
2535}
2536
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002537/*
2538 * there are many ways the trans_start and trans_end ioctls can lead
2539 * to deadlocks. They should only be used by applications that
2540 * basically own the machine, and have a very in depth understanding
2541 * of all the possible deadlocks and enospc problems.
2542 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002543static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002544{
2545 struct inode *inode = fdentry(file)->d_inode;
2546 struct btrfs_root *root = BTRFS_I(inode)->root;
2547 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002548 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002549
Sage Weil1ab86ae2009-09-29 18:38:44 -04002550 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002551 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002552 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002553
2554 ret = -EINPROGRESS;
2555 if (file->private_data)
2556 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002557
Li Zefanb83cc962010-12-20 16:04:08 +08002558 ret = -EROFS;
2559 if (btrfs_root_readonly(root))
2560 goto out;
2561
Yan Zhengc146afa2008-11-12 14:34:12 -05002562 ret = mnt_want_write(file->f_path.mnt);
2563 if (ret)
2564 goto out;
2565
Josef Bacika4abeea2011-04-11 17:25:13 -04002566 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002567
Sage Weil1ab86ae2009-09-29 18:38:44 -04002568 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002569 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002570 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002571 goto out_drop;
2572
2573 file->private_data = trans;
2574 return 0;
2575
2576out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002577 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002578 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002579out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002580 return ret;
2581}
2582
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002583static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2584{
2585 struct inode *inode = fdentry(file)->d_inode;
2586 struct btrfs_root *root = BTRFS_I(inode)->root;
2587 struct btrfs_root *new_root;
2588 struct btrfs_dir_item *di;
2589 struct btrfs_trans_handle *trans;
2590 struct btrfs_path *path;
2591 struct btrfs_key location;
2592 struct btrfs_disk_key disk_key;
2593 struct btrfs_super_block *disk_super;
2594 u64 features;
2595 u64 objectid = 0;
2596 u64 dir_id;
2597
2598 if (!capable(CAP_SYS_ADMIN))
2599 return -EPERM;
2600
2601 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2602 return -EFAULT;
2603
2604 if (!objectid)
2605 objectid = root->root_key.objectid;
2606
2607 location.objectid = objectid;
2608 location.type = BTRFS_ROOT_ITEM_KEY;
2609 location.offset = (u64)-1;
2610
2611 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2612 if (IS_ERR(new_root))
2613 return PTR_ERR(new_root);
2614
2615 if (btrfs_root_refs(&new_root->root_item) == 0)
2616 return -ENOENT;
2617
2618 path = btrfs_alloc_path();
2619 if (!path)
2620 return -ENOMEM;
2621 path->leave_spinning = 1;
2622
2623 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002624 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002625 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002626 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002627 }
2628
David Sterba6c417612011-04-13 15:41:04 +02002629 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002630 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2631 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002632 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002633 btrfs_free_path(path);
2634 btrfs_end_transaction(trans, root);
2635 printk(KERN_ERR "Umm, you don't have the default dir item, "
2636 "this isn't going to work\n");
2637 return -ENOENT;
2638 }
2639
2640 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2641 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2642 btrfs_mark_buffer_dirty(path->nodes[0]);
2643 btrfs_free_path(path);
2644
David Sterba6c417612011-04-13 15:41:04 +02002645 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002646 features = btrfs_super_incompat_flags(disk_super);
2647 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2648 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2649 btrfs_set_super_incompat_flags(disk_super, features);
2650 }
2651 btrfs_end_transaction(trans, root);
2652
2653 return 0;
2654}
2655
Josef Bacikbf5fc092010-09-29 11:22:36 -04002656static void get_block_group_info(struct list_head *groups_list,
2657 struct btrfs_ioctl_space_info *space)
2658{
2659 struct btrfs_block_group_cache *block_group;
2660
2661 space->total_bytes = 0;
2662 space->used_bytes = 0;
2663 space->flags = 0;
2664 list_for_each_entry(block_group, groups_list, list) {
2665 space->flags = block_group->flags;
2666 space->total_bytes += block_group->key.offset;
2667 space->used_bytes +=
2668 btrfs_block_group_used(&block_group->item);
2669 }
2670}
2671
Josef Bacik1406e432010-01-13 18:19:06 +00002672long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2673{
2674 struct btrfs_ioctl_space_args space_args;
2675 struct btrfs_ioctl_space_info space;
2676 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002677 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002678 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002679 struct btrfs_space_info *info;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002680 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2681 BTRFS_BLOCK_GROUP_SYSTEM,
2682 BTRFS_BLOCK_GROUP_METADATA,
2683 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2684 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002685 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002686 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002687 u64 slot_count = 0;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002688 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002689
2690 if (copy_from_user(&space_args,
2691 (struct btrfs_ioctl_space_args __user *)arg,
2692 sizeof(space_args)))
2693 return -EFAULT;
2694
Josef Bacikbf5fc092010-09-29 11:22:36 -04002695 for (i = 0; i < num_types; i++) {
2696 struct btrfs_space_info *tmp;
2697
2698 info = NULL;
2699 rcu_read_lock();
2700 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2701 list) {
2702 if (tmp->flags == types[i]) {
2703 info = tmp;
2704 break;
2705 }
2706 }
2707 rcu_read_unlock();
2708
2709 if (!info)
2710 continue;
2711
2712 down_read(&info->groups_sem);
2713 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2714 if (!list_empty(&info->block_groups[c]))
2715 slot_count++;
2716 }
2717 up_read(&info->groups_sem);
2718 }
Josef Bacik1406e432010-01-13 18:19:06 +00002719
Chris Mason7fde62b2010-03-16 15:40:10 -04002720 /* space_slots == 0 means they are asking for a count */
2721 if (space_args.space_slots == 0) {
2722 space_args.total_spaces = slot_count;
2723 goto out;
2724 }
Josef Bacikbf5fc092010-09-29 11:22:36 -04002725
Dan Rosenberg51788b12011-02-14 16:04:23 -05002726 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc092010-09-29 11:22:36 -04002727
Chris Mason7fde62b2010-03-16 15:40:10 -04002728 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002729
Chris Mason7fde62b2010-03-16 15:40:10 -04002730 /* we generally have at most 6 or so space infos, one for each raid
2731 * level. So, a whole page should be more than enough for everyone
2732 */
2733 if (alloc_size > PAGE_CACHE_SIZE)
2734 return -ENOMEM;
2735
2736 space_args.total_spaces = 0;
2737 dest = kmalloc(alloc_size, GFP_NOFS);
2738 if (!dest)
2739 return -ENOMEM;
2740 dest_orig = dest;
2741
2742 /* now we have a buffer to copy into */
Josef Bacikbf5fc092010-09-29 11:22:36 -04002743 for (i = 0; i < num_types; i++) {
2744 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002745
Dan Rosenberg51788b12011-02-14 16:04:23 -05002746 if (!slot_count)
2747 break;
2748
Josef Bacikbf5fc092010-09-29 11:22:36 -04002749 info = NULL;
2750 rcu_read_lock();
2751 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2752 list) {
2753 if (tmp->flags == types[i]) {
2754 info = tmp;
2755 break;
2756 }
2757 }
2758 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002759
Josef Bacikbf5fc092010-09-29 11:22:36 -04002760 if (!info)
2761 continue;
2762 down_read(&info->groups_sem);
2763 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2764 if (!list_empty(&info->block_groups[c])) {
2765 get_block_group_info(&info->block_groups[c],
2766 &space);
2767 memcpy(dest, &space, sizeof(space));
2768 dest++;
2769 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002770 slot_count--;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002771 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002772 if (!slot_count)
2773 break;
Josef Bacikbf5fc092010-09-29 11:22:36 -04002774 }
2775 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002776 }
Josef Bacik1406e432010-01-13 18:19:06 +00002777
Chris Mason7fde62b2010-03-16 15:40:10 -04002778 user_dest = (struct btrfs_ioctl_space_info *)
2779 (arg + sizeof(struct btrfs_ioctl_space_args));
2780
2781 if (copy_to_user(user_dest, dest_orig, alloc_size))
2782 ret = -EFAULT;
2783
2784 kfree(dest_orig);
2785out:
2786 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002787 ret = -EFAULT;
2788
2789 return ret;
2790}
2791
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002792/*
2793 * there are many ways the trans_start and trans_end ioctls can lead
2794 * to deadlocks. They should only be used by applications that
2795 * basically own the machine, and have a very in depth understanding
2796 * of all the possible deadlocks and enospc problems.
2797 */
2798long btrfs_ioctl_trans_end(struct file *file)
2799{
2800 struct inode *inode = fdentry(file)->d_inode;
2801 struct btrfs_root *root = BTRFS_I(inode)->root;
2802 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002803
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002804 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002805 if (!trans)
2806 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002807 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002808
Sage Weil1ab86ae2009-09-29 18:38:44 -04002809 btrfs_end_transaction(trans, root);
2810
Josef Bacika4abeea2011-04-11 17:25:13 -04002811 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002812
Sage Weilcfc8ea82008-12-11 16:30:06 -05002813 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002814 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002815}
2816
Sage Weil46204592010-10-29 15:41:32 -04002817static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2818{
2819 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2820 struct btrfs_trans_handle *trans;
2821 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002822 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002823
2824 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002825 if (IS_ERR(trans))
2826 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04002827 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002828 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002829 if (ret) {
2830 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002831 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00002832 }
Sage Weil46204592010-10-29 15:41:32 -04002833
2834 if (argp)
2835 if (copy_to_user(argp, &transid, sizeof(transid)))
2836 return -EFAULT;
2837 return 0;
2838}
2839
2840static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2841{
2842 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2843 u64 transid;
2844
2845 if (argp) {
2846 if (copy_from_user(&transid, argp, sizeof(transid)))
2847 return -EFAULT;
2848 } else {
2849 transid = 0; /* current trans */
2850 }
2851 return btrfs_wait_for_commit(root, transid);
2852}
2853
Jan Schmidt475f6382011-03-11 15:41:01 +01002854static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
2855{
2856 int ret;
2857 struct btrfs_ioctl_scrub_args *sa;
2858
2859 if (!capable(CAP_SYS_ADMIN))
2860 return -EPERM;
2861
2862 sa = memdup_user(arg, sizeof(*sa));
2863 if (IS_ERR(sa))
2864 return PTR_ERR(sa);
2865
2866 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01002867 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01002868
2869 if (copy_to_user(arg, sa, sizeof(*sa)))
2870 ret = -EFAULT;
2871
2872 kfree(sa);
2873 return ret;
2874}
2875
2876static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
2877{
2878 if (!capable(CAP_SYS_ADMIN))
2879 return -EPERM;
2880
2881 return btrfs_scrub_cancel(root);
2882}
2883
2884static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
2885 void __user *arg)
2886{
2887 struct btrfs_ioctl_scrub_args *sa;
2888 int ret;
2889
2890 if (!capable(CAP_SYS_ADMIN))
2891 return -EPERM;
2892
2893 sa = memdup_user(arg, sizeof(*sa));
2894 if (IS_ERR(sa))
2895 return PTR_ERR(sa);
2896
2897 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
2898
2899 if (copy_to_user(arg, sa, sizeof(*sa)))
2900 ret = -EFAULT;
2901
2902 kfree(sa);
2903 return ret;
2904}
2905
Jan Schmidtd7728c92011-07-07 16:48:38 +02002906static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
2907{
2908 int ret = 0;
2909 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04002910 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002911 int size;
Chris Mason806468f2011-11-06 03:07:10 -05002912 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002913 struct inode_fs_paths *ipath = NULL;
2914 struct btrfs_path *path;
2915
2916 if (!capable(CAP_SYS_ADMIN))
2917 return -EPERM;
2918
2919 path = btrfs_alloc_path();
2920 if (!path) {
2921 ret = -ENOMEM;
2922 goto out;
2923 }
2924
2925 ipa = memdup_user(arg, sizeof(*ipa));
2926 if (IS_ERR(ipa)) {
2927 ret = PTR_ERR(ipa);
2928 ipa = NULL;
2929 goto out;
2930 }
2931
2932 size = min_t(u32, ipa->size, 4096);
2933 ipath = init_ipath(size, root, path);
2934 if (IS_ERR(ipath)) {
2935 ret = PTR_ERR(ipath);
2936 ipath = NULL;
2937 goto out;
2938 }
2939
2940 ret = paths_from_inode(ipa->inum, ipath);
2941 if (ret < 0)
2942 goto out;
2943
2944 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002945 rel_ptr = ipath->fspath->val[i] -
2946 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04002947 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02002948 }
2949
Jeff Mahoney745c4d82011-11-20 07:31:57 -05002950 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
2951 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02002952 if (ret) {
2953 ret = -EFAULT;
2954 goto out;
2955 }
2956
2957out:
2958 btrfs_free_path(path);
2959 free_ipath(ipath);
2960 kfree(ipa);
2961
2962 return ret;
2963}
2964
2965static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
2966{
2967 struct btrfs_data_container *inodes = ctx;
2968 const size_t c = 3 * sizeof(u64);
2969
2970 if (inodes->bytes_left >= c) {
2971 inodes->bytes_left -= c;
2972 inodes->val[inodes->elem_cnt] = inum;
2973 inodes->val[inodes->elem_cnt + 1] = offset;
2974 inodes->val[inodes->elem_cnt + 2] = root;
2975 inodes->elem_cnt += 3;
2976 } else {
2977 inodes->bytes_missing += c - inodes->bytes_left;
2978 inodes->bytes_left = 0;
2979 inodes->elem_missed += 3;
2980 }
2981
2982 return 0;
2983}
2984
2985static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
2986 void __user *arg)
2987{
2988 int ret = 0;
2989 int size;
2990 u64 extent_offset;
2991 struct btrfs_ioctl_logical_ino_args *loi;
2992 struct btrfs_data_container *inodes = NULL;
2993 struct btrfs_path *path = NULL;
2994 struct btrfs_key key;
2995
2996 if (!capable(CAP_SYS_ADMIN))
2997 return -EPERM;
2998
2999 loi = memdup_user(arg, sizeof(*loi));
3000 if (IS_ERR(loi)) {
3001 ret = PTR_ERR(loi);
3002 loi = NULL;
3003 goto out;
3004 }
3005
3006 path = btrfs_alloc_path();
3007 if (!path) {
3008 ret = -ENOMEM;
3009 goto out;
3010 }
3011
3012 size = min_t(u32, loi->size, 4096);
3013 inodes = init_data_container(size);
3014 if (IS_ERR(inodes)) {
3015 ret = PTR_ERR(inodes);
3016 inodes = NULL;
3017 goto out;
3018 }
3019
3020 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
3021
3022 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3023 ret = -ENOENT;
3024 if (ret < 0)
3025 goto out;
3026
3027 extent_offset = loi->logical - key.objectid;
3028 ret = iterate_extent_inodes(root->fs_info, path, key.objectid,
3029 extent_offset, build_ino_list, inodes);
3030
3031 if (ret < 0)
3032 goto out;
3033
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003034 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3035 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003036 if (ret)
3037 ret = -EFAULT;
3038
3039out:
3040 btrfs_free_path(path);
3041 kfree(inodes);
3042 kfree(loi);
3043
3044 return ret;
3045}
3046
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003047long btrfs_ioctl(struct file *file, unsigned int
3048 cmd, unsigned long arg)
3049{
3050 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003051 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003052
3053 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003054 case FS_IOC_GETFLAGS:
3055 return btrfs_ioctl_getflags(file, argp);
3056 case FS_IOC_SETFLAGS:
3057 return btrfs_ioctl_setflags(file, argp);
3058 case FS_IOC_GETVERSION:
3059 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003060 case FITRIM:
3061 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003062 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003063 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003064 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003065 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003066 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003067 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003068 case BTRFS_IOC_SNAP_DESTROY:
3069 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003070 case BTRFS_IOC_SUBVOL_GETFLAGS:
3071 return btrfs_ioctl_subvol_getflags(file, argp);
3072 case BTRFS_IOC_SUBVOL_SETFLAGS:
3073 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003074 case BTRFS_IOC_DEFAULT_SUBVOL:
3075 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003076 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003077 return btrfs_ioctl_defrag(file, NULL);
3078 case BTRFS_IOC_DEFRAG_RANGE:
3079 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003080 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003081 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003082 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003083 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003084 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003085 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003086 case BTRFS_IOC_FS_INFO:
3087 return btrfs_ioctl_fs_info(root, argp);
3088 case BTRFS_IOC_DEV_INFO:
3089 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003090 case BTRFS_IOC_BALANCE:
3091 return btrfs_balance(root->fs_info->dev_root);
3092 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003093 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3094 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003095 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003096 case BTRFS_IOC_TRANS_START:
3097 return btrfs_ioctl_trans_start(file);
3098 case BTRFS_IOC_TRANS_END:
3099 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003100 case BTRFS_IOC_TREE_SEARCH:
3101 return btrfs_ioctl_tree_search(file, argp);
3102 case BTRFS_IOC_INO_LOOKUP:
3103 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003104 case BTRFS_IOC_INO_PATHS:
3105 return btrfs_ioctl_ino_to_path(root, argp);
3106 case BTRFS_IOC_LOGICAL_INO:
3107 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003108 case BTRFS_IOC_SPACE_INFO:
3109 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003110 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003111 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3112 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003113 case BTRFS_IOC_START_SYNC:
3114 return btrfs_ioctl_start_sync(file, argp);
3115 case BTRFS_IOC_WAIT_SYNC:
3116 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003117 case BTRFS_IOC_SCRUB:
3118 return btrfs_ioctl_scrub(root, argp);
3119 case BTRFS_IOC_SCRUB_CANCEL:
3120 return btrfs_ioctl_scrub_cancel(root, argp);
3121 case BTRFS_IOC_SCRUB_PROGRESS:
3122 return btrfs_ioctl_scrub_progress(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003123 }
3124
3125 return -ENOTTY;
3126}