blob: 99fe2ce7f7216c7e56c651a5128a807f877218f8 [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>
Alexander Block8ea05e32012-07-25 17:35:53 +020044#include <linux/uuid.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050045#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
50#include "ioctl.h"
51#include "print-tree.h"
52#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040053#include "locking.h"
Li Zefan581bb052011-04-20 10:06:11 +080054#include "inode-map.h"
Jan Schmidtd7728c92011-07-07 16:48:38 +020055#include "backref.h"
Josef Bacik606686e2012-06-04 14:03:51 -040056#include "rcu-string.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040057
Christoph Hellwig6cbff002009-04-17 10:37:41 +020058/* Mask out flags that are inappropriate for the given type of inode. */
59static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
60{
61 if (S_ISDIR(mode))
62 return flags;
63 else if (S_ISREG(mode))
64 return flags & ~FS_DIRSYNC_FL;
65 else
66 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
67}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040068
Christoph Hellwig6cbff002009-04-17 10:37:41 +020069/*
70 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
71 */
72static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
73{
74 unsigned int iflags = 0;
75
76 if (flags & BTRFS_INODE_SYNC)
77 iflags |= FS_SYNC_FL;
78 if (flags & BTRFS_INODE_IMMUTABLE)
79 iflags |= FS_IMMUTABLE_FL;
80 if (flags & BTRFS_INODE_APPEND)
81 iflags |= FS_APPEND_FL;
82 if (flags & BTRFS_INODE_NODUMP)
83 iflags |= FS_NODUMP_FL;
84 if (flags & BTRFS_INODE_NOATIME)
85 iflags |= FS_NOATIME_FL;
86 if (flags & BTRFS_INODE_DIRSYNC)
87 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +000088 if (flags & BTRFS_INODE_NODATACOW)
89 iflags |= FS_NOCOW_FL;
90
91 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
92 iflags |= FS_COMPR_FL;
93 else if (flags & BTRFS_INODE_NOCOMPRESS)
94 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +020095
96 return iflags;
97}
98
99/*
100 * Update inode->i_flags based on the btrfs internal flags.
101 */
102void btrfs_update_iflags(struct inode *inode)
103{
104 struct btrfs_inode *ip = BTRFS_I(inode);
105
106 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
107
108 if (ip->flags & BTRFS_INODE_SYNC)
109 inode->i_flags |= S_SYNC;
110 if (ip->flags & BTRFS_INODE_IMMUTABLE)
111 inode->i_flags |= S_IMMUTABLE;
112 if (ip->flags & BTRFS_INODE_APPEND)
113 inode->i_flags |= S_APPEND;
114 if (ip->flags & BTRFS_INODE_NOATIME)
115 inode->i_flags |= S_NOATIME;
116 if (ip->flags & BTRFS_INODE_DIRSYNC)
117 inode->i_flags |= S_DIRSYNC;
118}
119
120/*
121 * Inherit flags from the parent inode.
122 *
Josef Bacike27425d2011-09-27 11:01:30 -0400123 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200124 */
125void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
126{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400127 unsigned int flags;
128
129 if (!dir)
130 return;
131
132 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200133
Josef Bacike27425d2011-09-27 11:01:30 -0400134 if (flags & BTRFS_INODE_NOCOMPRESS) {
135 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
136 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
137 } else if (flags & BTRFS_INODE_COMPRESS) {
138 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
139 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
140 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200141
Josef Bacike27425d2011-09-27 11:01:30 -0400142 if (flags & BTRFS_INODE_NODATACOW)
143 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
144
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200145 btrfs_update_iflags(inode);
146}
147
148static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
149{
150 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
151 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
152
153 if (copy_to_user(arg, &flags, sizeof(flags)))
154 return -EFAULT;
155 return 0;
156}
157
Liu Bo75e7cb72011-03-22 10:12:20 +0000158static int check_flags(unsigned int flags)
159{
160 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
161 FS_NOATIME_FL | FS_NODUMP_FL | \
162 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000163 FS_NOCOMP_FL | FS_COMPR_FL |
164 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000165 return -EOPNOTSUPP;
166
167 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
168 return -EINVAL;
169
Liu Bo75e7cb72011-03-22 10:12:20 +0000170 return 0;
171}
172
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200173static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
174{
175 struct inode *inode = file->f_path.dentry->d_inode;
176 struct btrfs_inode *ip = BTRFS_I(inode);
177 struct btrfs_root *root = ip->root;
178 struct btrfs_trans_handle *trans;
179 unsigned int flags, oldflags;
180 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800181 u64 ip_oldflags;
182 unsigned int i_oldflags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200183
Li Zefanb83cc962010-12-20 16:04:08 +0800184 if (btrfs_root_readonly(root))
185 return -EROFS;
186
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200187 if (copy_from_user(&flags, arg, sizeof(flags)))
188 return -EFAULT;
189
Liu Bo75e7cb72011-03-22 10:12:20 +0000190 ret = check_flags(flags);
191 if (ret)
192 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200193
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700194 if (!inode_owner_or_capable(inode))
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200195 return -EACCES;
196
197 mutex_lock(&inode->i_mutex);
198
Li Zefanf062abf2011-12-29 13:36:45 +0800199 ip_oldflags = ip->flags;
200 i_oldflags = inode->i_flags;
201
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200202 flags = btrfs_mask_flags(inode->i_mode, flags);
203 oldflags = btrfs_flags_to_ioctl(ip->flags);
204 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
205 if (!capable(CAP_LINUX_IMMUTABLE)) {
206 ret = -EPERM;
207 goto out_unlock;
208 }
209 }
210
Al Viroa561be72011-11-23 11:57:51 -0500211 ret = mnt_want_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200212 if (ret)
213 goto out_unlock;
214
215 if (flags & FS_SYNC_FL)
216 ip->flags |= BTRFS_INODE_SYNC;
217 else
218 ip->flags &= ~BTRFS_INODE_SYNC;
219 if (flags & FS_IMMUTABLE_FL)
220 ip->flags |= BTRFS_INODE_IMMUTABLE;
221 else
222 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
223 if (flags & FS_APPEND_FL)
224 ip->flags |= BTRFS_INODE_APPEND;
225 else
226 ip->flags &= ~BTRFS_INODE_APPEND;
227 if (flags & FS_NODUMP_FL)
228 ip->flags |= BTRFS_INODE_NODUMP;
229 else
230 ip->flags &= ~BTRFS_INODE_NODUMP;
231 if (flags & FS_NOATIME_FL)
232 ip->flags |= BTRFS_INODE_NOATIME;
233 else
234 ip->flags &= ~BTRFS_INODE_NOATIME;
235 if (flags & FS_DIRSYNC_FL)
236 ip->flags |= BTRFS_INODE_DIRSYNC;
237 else
238 ip->flags &= ~BTRFS_INODE_DIRSYNC;
Li Zefane1e8fb62011-04-15 03:02:49 +0000239 if (flags & FS_NOCOW_FL)
240 ip->flags |= BTRFS_INODE_NODATACOW;
241 else
242 ip->flags &= ~BTRFS_INODE_NODATACOW;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200243
Liu Bo75e7cb72011-03-22 10:12:20 +0000244 /*
245 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
246 * flag may be changed automatically if compression code won't make
247 * things smaller.
248 */
249 if (flags & FS_NOCOMP_FL) {
250 ip->flags &= ~BTRFS_INODE_COMPRESS;
251 ip->flags |= BTRFS_INODE_NOCOMPRESS;
252 } else if (flags & FS_COMPR_FL) {
253 ip->flags |= BTRFS_INODE_COMPRESS;
254 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Li Zefanebcb9042011-04-15 03:03:17 +0000255 } else {
256 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000257 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200258
Li Zefan4da6f1a2011-12-29 13:39:50 +0800259 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800260 if (IS_ERR(trans)) {
261 ret = PTR_ERR(trans);
262 goto out_drop;
263 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200264
Li Zefan306424cc2011-12-14 20:12:02 -0500265 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400266 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500267 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200268 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200269
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200270 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800271 out_drop:
272 if (ret) {
273 ip->flags = ip_oldflags;
274 inode->i_flags = i_oldflags;
275 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200276
Al Viro2a79f172011-12-09 08:06:57 -0500277 mnt_drop_write_file(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200278 out_unlock:
279 mutex_unlock(&inode->i_mutex);
liubo2d4e6f62011-02-24 09:38:16 +0000280 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200281}
282
283static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
284{
285 struct inode *inode = file->f_path.dentry->d_inode;
286
287 return put_user(inode->i_generation, arg);
288}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400289
Li Dongyangf7039b12011-03-24 10:24:28 +0000290static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
291{
Al Viro815745c2011-11-17 15:40:49 -0500292 struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000293 struct btrfs_device *device;
294 struct request_queue *q;
295 struct fstrim_range range;
296 u64 minlen = ULLONG_MAX;
297 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500298 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000299 int ret;
300
301 if (!capable(CAP_SYS_ADMIN))
302 return -EPERM;
303
Xiao Guangrong1f781602011-04-20 10:09:16 +0000304 rcu_read_lock();
305 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
306 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000307 if (!device->bdev)
308 continue;
309 q = bdev_get_queue(device->bdev);
310 if (blk_queue_discard(q)) {
311 num_devices++;
312 minlen = min((u64)q->limits.discard_granularity,
313 minlen);
314 }
315 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000316 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200317
Li Dongyangf7039b12011-03-24 10:24:28 +0000318 if (!num_devices)
319 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000320 if (copy_from_user(&range, arg, sizeof(range)))
321 return -EFAULT;
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200322 if (range.start > total_bytes)
323 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000324
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200325 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000326 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500327 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000328 if (ret < 0)
329 return ret;
330
331 if (copy_to_user(arg, &range, sizeof(range)))
332 return -EFAULT;
333
334 return 0;
335}
336
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400337static noinline int create_subvol(struct btrfs_root *root,
338 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400339 char *name, int namelen,
340 u64 *async_transid)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400341{
342 struct btrfs_trans_handle *trans;
343 struct btrfs_key key;
344 struct btrfs_root_item root_item;
345 struct btrfs_inode_item *inode_item;
346 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400347 struct btrfs_root *new_root;
Al Viro2fbe8c82011-07-16 21:38:06 -0400348 struct dentry *parent = dentry->d_parent;
Josef Bacik6a912212010-11-20 09:48:00 +0000349 struct inode *dir;
Alexander Block8ea05e32012-07-25 17:35:53 +0200350 struct timespec cur_time = CURRENT_TIME;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400351 int ret;
352 int err;
353 u64 objectid;
354 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500355 u64 index = 0;
Alexander Block8ea05e32012-07-25 17:35:53 +0200356 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400357
Li Zefan581bb052011-04-20 10:06:11 +0800358 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400359 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400360 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000361
362 dir = parent->d_inode;
363
Josef Bacik9ed74f22009-09-11 16:12:44 -0400364 /*
365 * 1 - inode item
366 * 2 - refs
367 * 1 - root item
368 * 2 - dir items
369 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400370 trans = btrfs_start_transaction(root, 6);
Al Viro2fbe8c82011-07-16 21:38:06 -0400371 if (IS_ERR(trans))
Yan, Zhenga22285a2010-05-16 10:48:46 -0400372 return PTR_ERR(trans);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400373
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400374 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200375 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400376 if (IS_ERR(leaf)) {
377 ret = PTR_ERR(leaf);
378 goto fail;
379 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400380
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400381 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400382 btrfs_set_header_bytenr(leaf, leaf->start);
383 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400384 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400385 btrfs_set_header_owner(leaf, objectid);
386
387 write_extent_buffer(leaf, root->fs_info->fsid,
388 (unsigned long)btrfs_header_fsid(leaf),
389 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400390 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
391 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
392 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400393 btrfs_mark_buffer_dirty(leaf);
394
Alexander Block8ea05e32012-07-25 17:35:53 +0200395 memset(&root_item, 0, sizeof(root_item));
396
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400397 inode_item = &root_item.inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398 inode_item->generation = cpu_to_le64(1);
399 inode_item->size = cpu_to_le64(3);
400 inode_item->nlink = cpu_to_le32(1);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400401 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400402 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
403
Li Zefan08fe4db2011-03-28 02:01:25 +0000404 root_item.flags = 0;
405 root_item.byte_limit = 0;
406 inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT);
407
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400408 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400409 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400410 btrfs_set_root_level(&root_item, 0);
411 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000412 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400413 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400414
Alexander Block8ea05e32012-07-25 17:35:53 +0200415 btrfs_set_root_generation_v2(&root_item,
416 btrfs_root_generation(&root_item));
417 uuid_le_gen(&new_uuid);
418 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
419 root_item.otime.sec = cpu_to_le64(cur_time.tv_sec);
420 root_item.otime.nsec = cpu_to_le64(cur_time.tv_nsec);
421 root_item.ctime = root_item.otime;
422 btrfs_set_root_ctransid(&root_item, trans->transid);
423 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400424
Chris Mason925baed2008-06-25 16:01:30 -0400425 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400426 free_extent_buffer(leaf);
427 leaf = NULL;
428
429 btrfs_set_root_dirid(&root_item, new_dirid);
430
431 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400432 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400433 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
434 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
435 &root_item);
436 if (ret)
437 goto fail;
438
Yan, Zheng76dda932009-09-21 16:00:26 -0400439 key.offset = (u64)-1;
440 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100441 if (IS_ERR(new_root)) {
442 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
443 ret = PTR_ERR(new_root);
444 goto fail;
445 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400446
447 btrfs_record_root_in_trans(trans, new_root);
448
Josef Bacikd82a6f12011-05-11 15:26:06 -0400449 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700450 if (ret) {
451 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100452 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700453 goto fail;
454 }
455
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400456 /*
457 * insert the directory item
458 */
Chris Mason3de45862008-11-17 21:02:50 -0500459 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100460 if (ret) {
461 btrfs_abort_transaction(trans, root, ret);
462 goto fail;
463 }
Chris Mason3de45862008-11-17 21:02:50 -0500464
465 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800466 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500467 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100468 if (ret) {
469 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400470 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100471 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500472
Yan Zheng52c26172009-01-05 15:43:43 -0500473 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
474 ret = btrfs_update_inode(trans, root, dir);
475 BUG_ON(ret);
476
Chris Mason0660b5a2008-11-17 20:37:39 -0500477 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400478 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800479 btrfs_ino(dir), index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400480
Chris Mason0660b5a2008-11-17 20:37:39 -0500481 BUG_ON(ret);
482
Yan, Zheng76dda932009-09-21 16:00:26 -0400483 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484fail:
Sage Weil72fd0322010-10-29 15:41:32 -0400485 if (async_transid) {
486 *async_transid = trans->transid;
487 err = btrfs_commit_transaction_async(trans, root, 1);
488 } else {
489 err = btrfs_commit_transaction(trans, root);
490 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400491 if (err && !ret)
492 ret = err;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400493 return ret;
494}
495
Sage Weil72fd0322010-10-29 15:41:32 -0400496static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800497 char *name, int namelen, u64 *async_transid,
498 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400499{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000500 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400501 struct btrfs_pending_snapshot *pending_snapshot;
502 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000503 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400504
505 if (!root->ref_cows)
506 return -EINVAL;
507
Yan, Zhenga22285a2010-05-16 10:48:46 -0400508 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
509 if (!pending_snapshot)
510 return -ENOMEM;
511
512 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
513 pending_snapshot->dentry = dentry;
514 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800515 pending_snapshot->readonly = readonly;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400516
517 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
518 if (IS_ERR(trans)) {
519 ret = PTR_ERR(trans);
520 goto fail;
521 }
522
523 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
524 BUG_ON(ret);
525
Josef Bacik83515832011-06-14 15:16:14 -0400526 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400527 list_add(&pending_snapshot->list,
528 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400529 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400530 if (async_transid) {
531 *async_transid = trans->transid;
532 ret = btrfs_commit_transaction_async(trans,
533 root->fs_info->extent_root, 1);
534 } else {
535 ret = btrfs_commit_transaction(trans,
536 root->fs_info->extent_root);
537 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400538 BUG_ON(ret);
539
540 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400541 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000542 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400543
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500544 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
545 if (ret)
546 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400547
Al Viro2fbe8c82011-07-16 21:38:06 -0400548 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000549 if (IS_ERR(inode)) {
550 ret = PTR_ERR(inode);
551 goto fail;
552 }
553 BUG_ON(!inode);
554 d_instantiate(dentry, inode);
555 ret = 0;
556fail:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400557 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400558 return ret;
559}
560
Sage Weil4260f7c2010-10-29 15:46:43 -0400561/* copy of check_sticky in fs/namei.c()
562* It's inline, so penalty for filesystems that don't use sticky bit is
563* minimal.
564*/
565static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
566{
567 uid_t fsuid = current_fsuid();
568
569 if (!(dir->i_mode & S_ISVTX))
570 return 0;
571 if (inode->i_uid == fsuid)
572 return 0;
573 if (dir->i_uid == fsuid)
574 return 0;
575 return !capable(CAP_FOWNER);
576}
577
578/* copy of may_delete in fs/namei.c()
579 * Check whether we can remove a link victim from directory dir, check
580 * whether the type of victim is right.
581 * 1. We can't do it if dir is read-only (done in permission())
582 * 2. We should have write and exec permissions on dir
583 * 3. We can't remove anything from append-only dir
584 * 4. We can't do anything with immutable dir (done in permission())
585 * 5. If the sticky bit on dir is set we should either
586 * a. be owner of dir, or
587 * b. be owner of victim, or
588 * c. have CAP_FOWNER capability
589 * 6. If the victim is append-only or immutable we can't do antyhing with
590 * links pointing to it.
591 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
592 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
593 * 9. We can't remove a root or mountpoint.
594 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
595 * nfs_async_unlink().
596 */
597
598static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
599{
600 int error;
601
602 if (!victim->d_inode)
603 return -ENOENT;
604
605 BUG_ON(victim->d_parent->d_inode != dir);
606 audit_inode_child(victim, dir);
607
608 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
609 if (error)
610 return error;
611 if (IS_APPEND(dir))
612 return -EPERM;
613 if (btrfs_check_sticky(dir, victim->d_inode)||
614 IS_APPEND(victim->d_inode)||
615 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
616 return -EPERM;
617 if (isdir) {
618 if (!S_ISDIR(victim->d_inode->i_mode))
619 return -ENOTDIR;
620 if (IS_ROOT(victim))
621 return -EBUSY;
622 } else if (S_ISDIR(victim->d_inode->i_mode))
623 return -EISDIR;
624 if (IS_DEADDIR(dir))
625 return -ENOENT;
626 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
627 return -EBUSY;
628 return 0;
629}
630
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400631/* copy of may_create in fs/namei.c() */
632static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
633{
634 if (child->d_inode)
635 return -EEXIST;
636 if (IS_DEADDIR(dir))
637 return -ENOENT;
638 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
639}
640
641/*
642 * Create a new subvolume below @parent. This is largely modeled after
643 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
644 * inside this filesystem so it's quite a bit simpler.
645 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400646static noinline int btrfs_mksubvol(struct path *parent,
647 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400648 struct btrfs_root *snap_src,
Li Zefanb83cc962010-12-20 16:04:08 +0800649 u64 *async_transid, bool readonly)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400650{
Yan, Zheng76dda932009-09-21 16:00:26 -0400651 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400652 struct dentry *dentry;
653 int error;
654
Yan, Zheng76dda932009-09-21 16:00:26 -0400655 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400656
657 dentry = lookup_one_len(name, parent->dentry, namelen);
658 error = PTR_ERR(dentry);
659 if (IS_ERR(dentry))
660 goto out_unlock;
661
662 error = -EEXIST;
663 if (dentry->d_inode)
664 goto out_dput;
665
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400666 error = mnt_want_write(parent->mnt);
667 if (error)
668 goto out_dput;
669
Yan, Zheng76dda932009-09-21 16:00:26 -0400670 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400671 if (error)
672 goto out_drop_write;
673
Yan, Zheng76dda932009-09-21 16:00:26 -0400674 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
675
676 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
677 goto out_up_read;
678
Chris Mason3de45862008-11-17 21:02:50 -0500679 if (snap_src) {
Sage Weil72fd0322010-10-29 15:41:32 -0400680 error = create_snapshot(snap_src, dentry,
Li Zefanb83cc962010-12-20 16:04:08 +0800681 name, namelen, async_transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -0500682 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400683 error = create_subvol(BTRFS_I(dir)->root, dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400684 name, namelen, async_transid);
Chris Mason3de45862008-11-17 21:02:50 -0500685 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400686 if (!error)
687 fsnotify_mkdir(dir, dentry);
688out_up_read:
689 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400690out_drop_write:
691 mnt_drop_write(parent->mnt);
692out_dput:
693 dput(dentry);
694out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400695 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400696 return error;
697}
698
Chris Mason4cb53002011-05-24 15:35:30 -0400699/*
700 * When we're defragging a range, we don't want to kick it off again
701 * if it is really just waiting for delalloc to send it down.
702 * If we find a nice big extent or delalloc range for the bytes in the
703 * file you want to defrag, we return 0 to let you know to skip this
704 * part of the file
705 */
706static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
707{
708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
709 struct extent_map *em = NULL;
710 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
711 u64 end;
712
713 read_lock(&em_tree->lock);
714 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
715 read_unlock(&em_tree->lock);
716
717 if (em) {
718 end = extent_map_end(em);
719 free_extent_map(em);
720 if (end - offset > thresh)
721 return 0;
722 }
723 /* if we already have a nice delalloc here, just stop */
724 thresh /= 2;
725 end = count_range_bits(io_tree, &offset, offset + thresh,
726 thresh, EXTENT_DELALLOC, 1);
727 if (end >= thresh)
728 return 0;
729 return 1;
730}
731
732/*
733 * helper function to walk through a file and find extents
734 * newer than a specific transid, and smaller than thresh.
735 *
736 * This is used by the defragging code to find new and small
737 * extents
738 */
739static int find_new_extents(struct btrfs_root *root,
740 struct inode *inode, u64 newer_than,
741 u64 *off, int thresh)
742{
743 struct btrfs_path *path;
744 struct btrfs_key min_key;
745 struct btrfs_key max_key;
746 struct extent_buffer *leaf;
747 struct btrfs_file_extent_item *extent;
748 int type;
749 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000750 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400751
752 path = btrfs_alloc_path();
753 if (!path)
754 return -ENOMEM;
755
David Sterbaa4689d22011-05-31 17:08:14 +0000756 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400757 min_key.type = BTRFS_EXTENT_DATA_KEY;
758 min_key.offset = *off;
759
David Sterbaa4689d22011-05-31 17:08:14 +0000760 max_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400761 max_key.type = (u8)-1;
762 max_key.offset = (u64)-1;
763
764 path->keep_locks = 1;
765
766 while(1) {
767 ret = btrfs_search_forward(root, &min_key, &max_key,
768 path, 0, newer_than);
769 if (ret != 0)
770 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000771 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400772 goto none;
773 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
774 goto none;
775
776 leaf = path->nodes[0];
777 extent = btrfs_item_ptr(leaf, path->slots[0],
778 struct btrfs_file_extent_item);
779
780 type = btrfs_file_extent_type(leaf, extent);
781 if (type == BTRFS_FILE_EXTENT_REG &&
782 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
783 check_defrag_in_cache(inode, min_key.offset, thresh)) {
784 *off = min_key.offset;
785 btrfs_free_path(path);
786 return 0;
787 }
788
789 if (min_key.offset == (u64)-1)
790 goto none;
791
792 min_key.offset++;
793 btrfs_release_path(path);
794 }
795none:
796 btrfs_free_path(path);
797 return -ENOENT;
798}
799
Li Zefan6c282eb2012-06-11 16:03:35 +0800800static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400801{
802 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500803 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800804 struct extent_map *em;
805 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500806
807 /*
808 * hopefully we have this extent in the tree already, try without
809 * the full extent lock
810 */
811 read_lock(&em_tree->lock);
812 em = lookup_extent_mapping(em_tree, start, len);
813 read_unlock(&em_tree->lock);
814
815 if (!em) {
816 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100817 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500818 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100819 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500820
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000821 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800822 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500823 }
824
Li Zefan6c282eb2012-06-11 16:03:35 +0800825 return em;
826}
827
828static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
829{
830 struct extent_map *next;
831 bool ret = true;
832
833 /* this is the last extent */
834 if (em->start + em->len >= i_size_read(inode))
835 return false;
836
837 next = defrag_lookup_extent(inode, em->start + em->len);
838 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
839 ret = false;
840
841 free_extent_map(next);
842 return ret;
843}
844
845static int should_defrag_range(struct inode *inode, u64 start, int thresh,
846 u64 *last_len, u64 *skip, u64 *defrag_end)
847{
848 struct extent_map *em;
849 int ret = 1;
850 bool next_mergeable = true;
851
852 /*
853 * make sure that once we start defragging an extent, we keep on
854 * defragging it
855 */
856 if (start < *defrag_end)
857 return 1;
858
859 *skip = 0;
860
861 em = defrag_lookup_extent(inode, start);
862 if (!em)
863 return 0;
864
Chris Mason940100a2010-03-10 10:52:59 -0500865 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -0400866 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -0500867 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400868 goto out;
869 }
870
Li Zefan6c282eb2012-06-11 16:03:35 +0800871 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -0500872
873 /*
Li Zefan6c282eb2012-06-11 16:03:35 +0800874 * we hit a real extent, if it is big or the next extent is not a
875 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -0500876 */
Li Zefan6c282eb2012-06-11 16:03:35 +0800877 if ((*last_len == 0 || *last_len >= thresh) &&
878 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -0500879 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -0400880out:
Chris Mason940100a2010-03-10 10:52:59 -0500881 /*
882 * last_len ends up being a counter of how many bytes we've defragged.
883 * every time we choose not to defrag an extent, we reset *last_len
884 * so that the next tiny extent will force a defrag.
885 *
886 * The end result of this is that tiny extents before a single big
887 * extent will force at least part of that big extent to be defragged.
888 */
889 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -0500890 *defrag_end = extent_map_end(em);
891 } else {
892 *last_len = 0;
893 *skip = extent_map_end(em);
894 *defrag_end = 0;
895 }
896
897 free_extent_map(em);
898 return ret;
899}
900
Chris Mason4cb53002011-05-24 15:35:30 -0400901/*
902 * it doesn't do much good to defrag one or two pages
903 * at a time. This pulls in a nice chunk of pages
904 * to COW and defrag.
905 *
906 * It also makes sure the delalloc code has enough
907 * dirty data to avoid making new small extents as part
908 * of the defrag
909 *
910 * It's a good idea to start RA on this range
911 * before calling this.
912 */
913static int cluster_pages_for_defrag(struct inode *inode,
914 struct page **pages,
915 unsigned long start_index,
916 int num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400917{
Chris Mason4cb53002011-05-24 15:35:30 -0400918 unsigned long file_end;
919 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400920 u64 page_start;
921 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -0400922 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -0400923 int ret;
924 int i;
925 int i_done;
926 struct btrfs_ordered_extent *ordered;
927 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +0800928 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -0400929 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -0400930
Chris Mason4cb53002011-05-24 15:35:30 -0400931 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -0400932 if (!isize || start_index > file_end)
933 return 0;
934
935 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -0400936
937 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -0400938 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -0400939 if (ret)
940 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400941 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +0800942 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -0400943
944 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -0400945 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -0400946 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +0800947again:
Josef Bacika94733d2011-07-11 10:47:06 -0400948 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +0800949 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -0400950 if (!page)
951 break;
952
Miao Xie600a45e2012-02-16 15:01:24 +0800953 page_start = page_offset(page);
954 page_end = page_start + PAGE_CACHE_SIZE - 1;
955 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100956 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800957 ordered = btrfs_lookup_ordered_extent(inode,
958 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100959 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +0800960 if (!ordered)
961 break;
962
963 unlock_page(page);
964 btrfs_start_ordered_extent(inode, ordered, 1);
965 btrfs_put_ordered_extent(ordered);
966 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -0400967 /*
968 * we unlocked the page above, so we need check if
969 * it was released or not.
970 */
971 if (page->mapping != inode->i_mapping) {
972 unlock_page(page);
973 page_cache_release(page);
974 goto again;
975 }
Miao Xie600a45e2012-02-16 15:01:24 +0800976 }
977
Chris Mason4cb53002011-05-24 15:35:30 -0400978 if (!PageUptodate(page)) {
979 btrfs_readpage(NULL, page);
980 lock_page(page);
981 if (!PageUptodate(page)) {
982 unlock_page(page);
983 page_cache_release(page);
984 ret = -EIO;
985 break;
986 }
987 }
Miao Xie600a45e2012-02-16 15:01:24 +0800988
Miao Xie600a45e2012-02-16 15:01:24 +0800989 if (page->mapping != inode->i_mapping) {
990 unlock_page(page);
991 page_cache_release(page);
992 goto again;
993 }
994
Chris Mason4cb53002011-05-24 15:35:30 -0400995 pages[i] = page;
996 i_done++;
997 }
998 if (!i_done || ret)
999 goto out;
1000
1001 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1002 goto out;
1003
1004 /*
1005 * so now we have a nice long stream of locked
1006 * and up to date pages, lets wait on them
1007 */
1008 for (i = 0; i < i_done; i++)
1009 wait_on_page_writeback(pages[i]);
1010
1011 page_start = page_offset(pages[0]);
1012 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1013
1014 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001015 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001016 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1017 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
1018 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
1019 GFP_NOFS);
1020
Liu Bo1f12bd02012-03-29 09:57:44 -04001021 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001022 spin_lock(&BTRFS_I(inode)->lock);
1023 BTRFS_I(inode)->outstanding_extents++;
1024 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001025 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001026 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001027 }
1028
1029
1030 btrfs_set_extent_delalloc(inode, page_start, page_end - 1,
1031 &cached_state);
1032
1033 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1034 page_start, page_end - 1, &cached_state,
1035 GFP_NOFS);
1036
1037 for (i = 0; i < i_done; i++) {
1038 clear_page_dirty_for_io(pages[i]);
1039 ClearPageChecked(pages[i]);
1040 set_page_extent_mapped(pages[i]);
1041 set_page_dirty(pages[i]);
1042 unlock_page(pages[i]);
1043 page_cache_release(pages[i]);
1044 }
1045 return i_done;
1046out:
1047 for (i = 0; i < i_done; i++) {
1048 unlock_page(pages[i]);
1049 page_cache_release(pages[i]);
1050 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001051 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001052 return ret;
1053
1054}
1055
1056int btrfs_defrag_file(struct inode *inode, struct file *file,
1057 struct btrfs_ioctl_defrag_range_args *range,
1058 u64 newer_than, unsigned long max_to_defrag)
1059{
1060 struct btrfs_root *root = BTRFS_I(inode)->root;
1061 struct btrfs_super_block *disk_super;
1062 struct file_ra_state *ra = NULL;
1063 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001064 u64 isize = i_size_read(inode);
Chris Mason4cb53002011-05-24 15:35:30 -04001065 u64 features;
Chris Mason940100a2010-03-10 10:52:59 -05001066 u64 last_len = 0;
1067 u64 skip = 0;
1068 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001069 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001070 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001071 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001072 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001073 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001074 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001075 int extent_thresh = range->extent_thresh;
Li Zefan008873e2011-09-02 15:57:07 +08001076 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1077 int cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001078 u64 new_align = ~((u64)128 * 1024 - 1);
1079 struct page **pages = NULL;
1080
1081 if (extent_thresh == 0)
1082 extent_thresh = 256 * 1024;
Li Zefan1a419d82010-10-25 15:12:50 +08001083
1084 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1085 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1086 return -EINVAL;
1087 if (range->compress_type)
1088 compress_type = range->compress_type;
1089 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001090
Li Zefan151a31b2011-09-02 15:56:39 +08001091 if (isize == 0)
Chris Mason940100a2010-03-10 10:52:59 -05001092 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001093
Chris Mason4cb53002011-05-24 15:35:30 -04001094 /*
1095 * if we were not given a file, allocate a readahead
1096 * context
1097 */
1098 if (!file) {
1099 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1100 if (!ra)
1101 return -ENOMEM;
1102 file_ra_state_init(ra, inode->i_mapping);
1103 } else {
1104 ra = &file->f_ra;
1105 }
1106
Li Zefan008873e2011-09-02 15:57:07 +08001107 pages = kmalloc(sizeof(struct page *) * max_cluster,
Chris Mason4cb53002011-05-24 15:35:30 -04001108 GFP_NOFS);
1109 if (!pages) {
1110 ret = -ENOMEM;
1111 goto out_ra;
1112 }
1113
1114 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001115 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001116 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001117 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1118 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001119 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001120 }
1121
Chris Mason4cb53002011-05-24 15:35:30 -04001122 if (newer_than) {
1123 ret = find_new_extents(root, inode, newer_than,
1124 &newer_off, 64 * 1024);
1125 if (!ret) {
1126 range->start = newer_off;
1127 /*
1128 * we always align our defrag to help keep
1129 * the extents in the file evenly spaced
1130 */
1131 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001132 } else
1133 goto out_ra;
1134 } else {
1135 i = range->start >> PAGE_CACHE_SHIFT;
1136 }
1137 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001138 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001139
Li Zefan2a0f7f52011-10-10 15:43:34 -04001140 /*
1141 * make writeback starts from i, so the defrag range can be
1142 * written sequentially.
1143 */
1144 if (i < inode->i_mapping->writeback_index)
1145 inode->i_mapping->writeback_index = i;
1146
Chris Masonf7f43cc2011-10-11 11:41:40 -04001147 while (i <= last_index && defrag_count < max_to_defrag &&
1148 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1149 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001150 /*
1151 * make sure we stop running if someone unmounts
1152 * the FS
1153 */
1154 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1155 break;
1156
Liu Bo66c26892012-03-29 09:57:45 -04001157 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001158 extent_thresh, &last_len, &skip,
1159 &defrag_end)) {
Chris Mason940100a2010-03-10 10:52:59 -05001160 unsigned long next;
1161 /*
1162 * the should_defrag function tells us how much to skip
1163 * bump our counter by the suggested amount
1164 */
1165 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1166 i = max(i + 1, next);
1167 continue;
1168 }
Li Zefan008873e2011-09-02 15:57:07 +08001169
1170 if (!newer_than) {
1171 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1172 PAGE_CACHE_SHIFT) - i;
1173 cluster = min(cluster, max_cluster);
1174 } else {
1175 cluster = max_cluster;
1176 }
1177
Chris Mason1e701a32010-03-11 09:42:04 -05001178 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
Li Zefan1a419d82010-10-25 15:12:50 +08001179 BTRFS_I(inode)->force_compress = compress_type;
Chris Mason940100a2010-03-10 10:52:59 -05001180
Li Zefan008873e2011-09-02 15:57:07 +08001181 if (i + cluster > ra_index) {
1182 ra_index = max(i, ra_index);
1183 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1184 cluster);
1185 ra_index += max_cluster;
1186 }
Chris Mason940100a2010-03-10 10:52:59 -05001187
Liu Boecb8bea2012-03-29 09:57:44 -04001188 mutex_lock(&inode->i_mutex);
Li Zefan008873e2011-09-02 15:57:07 +08001189 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001190 if (ret < 0) {
1191 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001192 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001193 }
Chris Mason940100a2010-03-10 10:52:59 -05001194
Chris Mason4cb53002011-05-24 15:35:30 -04001195 defrag_count += ret;
1196 balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret);
Liu Boecb8bea2012-03-29 09:57:44 -04001197 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001198
1199 if (newer_than) {
1200 if (newer_off == (u64)-1)
1201 break;
1202
Liu Boe1f041e2012-03-29 09:57:45 -04001203 if (ret > 0)
1204 i += ret;
1205
Chris Mason4cb53002011-05-24 15:35:30 -04001206 newer_off = max(newer_off + 1,
1207 (u64)i << PAGE_CACHE_SHIFT);
1208
1209 ret = find_new_extents(root, inode,
1210 newer_than, &newer_off,
1211 64 * 1024);
1212 if (!ret) {
1213 range->start = newer_off;
1214 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001215 } else {
1216 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001217 }
Chris Mason4cb53002011-05-24 15:35:30 -04001218 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001219 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001220 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001221 last_len += ret << PAGE_CACHE_SHIFT;
1222 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001223 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001224 last_len = 0;
1225 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001226 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001227 }
1228
Chris Mason1e701a32010-03-11 09:42:04 -05001229 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1230 filemap_flush(inode->i_mapping);
1231
1232 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1233 /* the filemap_flush will queue IO into the worker threads, but
1234 * we have to make sure the IO is actually started and that
1235 * ordered extents get created before we return
1236 */
1237 atomic_inc(&root->fs_info->async_submit_draining);
1238 while (atomic_read(&root->fs_info->nr_async_submits) ||
1239 atomic_read(&root->fs_info->async_delalloc_pages)) {
1240 wait_event(root->fs_info->async_submit_wait,
1241 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1242 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1243 }
1244 atomic_dec(&root->fs_info->async_submit_draining);
1245
1246 mutex_lock(&inode->i_mutex);
Li Zefan261507a02010-12-17 14:21:50 +08001247 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
Chris Mason1e701a32010-03-11 09:42:04 -05001248 mutex_unlock(&inode->i_mutex);
1249 }
1250
David Sterba6c417612011-04-13 15:41:04 +02001251 disk_super = root->fs_info->super_copy;
Li Zefan1a419d82010-10-25 15:12:50 +08001252 features = btrfs_super_incompat_flags(disk_super);
1253 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1254 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
1255 btrfs_set_super_incompat_flags(disk_super, features);
1256 }
1257
Diego Calleja60ccf822011-09-01 16:33:57 +02001258 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001259
Chris Mason4cb53002011-05-24 15:35:30 -04001260out_ra:
1261 if (!file)
1262 kfree(ra);
1263 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001264 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001265}
1266
Yan, Zheng76dda932009-09-21 16:00:26 -04001267static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
1268 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001269{
1270 u64 new_size;
1271 u64 old_size;
1272 u64 devid = 1;
1273 struct btrfs_ioctl_vol_args *vol_args;
1274 struct btrfs_trans_handle *trans;
1275 struct btrfs_device *device = NULL;
1276 char *sizestr;
1277 char *devstr = NULL;
1278 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001279 int mod = 0;
1280
Yan Zhengc146afa2008-11-12 14:34:12 -05001281 if (root->fs_info->sb->s_flags & MS_RDONLY)
1282 return -EROFS;
1283
Chris Masone441d542009-01-05 16:57:23 -05001284 if (!capable(CAP_SYS_ADMIN))
1285 return -EPERM;
1286
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001287 mutex_lock(&root->fs_info->volume_mutex);
1288 if (root->fs_info->balance_ctl) {
1289 printk(KERN_INFO "btrfs: balance in progress\n");
1290 ret = -EINVAL;
1291 goto out;
1292 }
1293
Li Zefandae7b662009-04-08 15:06:54 +08001294 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001295 if (IS_ERR(vol_args)) {
1296 ret = PTR_ERR(vol_args);
1297 goto out;
1298 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001299
1300 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001301
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001302 sizestr = vol_args->name;
1303 devstr = strchr(sizestr, ':');
1304 if (devstr) {
1305 char *end;
1306 sizestr = devstr + 1;
1307 *devstr = '\0';
1308 devstr = vol_args->name;
1309 devid = simple_strtoull(devstr, &end, 10);
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001310 printk(KERN_INFO "btrfs: resizing devid %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001311 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001312 }
Yan Zheng2b820322008-11-17 21:11:30 -05001313 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001314 if (!device) {
Arnd Hannemann5bb14682011-11-20 07:33:38 -05001315 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
Joel Becker21380932009-04-21 12:38:29 -07001316 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001317 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001318 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001319 }
Liu Bo4e42ae12012-06-14 02:23:19 -06001320 if (device->fs_devices && device->fs_devices->seeding) {
1321 printk(KERN_INFO "btrfs: resizer unable to apply on "
Chris Masona8c4a332012-06-15 20:07:17 -04001322 "seeding device %llu\n",
1323 (unsigned long long)devid);
Liu Bo4e42ae12012-06-14 02:23:19 -06001324 ret = -EINVAL;
1325 goto out_free;
1326 }
1327
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001328 if (!strcmp(sizestr, "max"))
1329 new_size = device->bdev->bd_inode->i_size;
1330 else {
1331 if (sizestr[0] == '-') {
1332 mod = -1;
1333 sizestr++;
1334 } else if (sizestr[0] == '+') {
1335 mod = 1;
1336 sizestr++;
1337 }
Akinobu Mita91748462010-02-28 10:59:11 +00001338 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001339 if (new_size == 0) {
1340 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001341 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001342 }
1343 }
1344
1345 old_size = device->total_bytes;
1346
1347 if (mod < 0) {
1348 if (new_size > old_size) {
1349 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001350 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001351 }
1352 new_size = old_size - new_size;
1353 } else if (mod > 0) {
1354 new_size = old_size + new_size;
1355 }
1356
1357 if (new_size < 256 * 1024 * 1024) {
1358 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001359 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001360 }
1361 if (new_size > device->bdev->bd_inode->i_size) {
1362 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001363 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001364 }
1365
1366 do_div(new_size, root->sectorsize);
1367 new_size *= root->sectorsize;
1368
Josef Bacik606686e2012-06-04 14:03:51 -04001369 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
1370 rcu_str_deref(device->name),
1371 (unsigned long long)new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001372
1373 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001374 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001375 if (IS_ERR(trans)) {
1376 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001377 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001378 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001379 ret = btrfs_grow_device(trans, device, new_size);
1380 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001381 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001382 ret = btrfs_shrink_device(device, new_size);
1383 }
1384
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001385out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001386 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001387out:
1388 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001389 return ret;
1390}
1391
Sage Weil72fd0322010-10-29 15:41:32 -04001392static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1393 char *name,
1394 unsigned long fd,
1395 int subvol,
Li Zefanb83cc962010-12-20 16:04:08 +08001396 u64 *transid,
1397 bool readonly)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001398{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001399 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason3de45862008-11-17 21:02:50 -05001400 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001402 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001403
Yan Zhengc146afa2008-11-12 14:34:12 -05001404 if (root->fs_info->sb->s_flags & MS_RDONLY)
1405 return -EROFS;
1406
Sage Weil72fd0322010-10-29 15:41:32 -04001407 namelen = strlen(name);
1408 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409 ret = -EINVAL;
1410 goto out;
1411 }
1412
Chris Mason16780ca2012-02-20 22:14:55 -05001413 if (name[0] == '.' &&
1414 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1415 ret = -EEXIST;
1416 goto out;
1417 }
1418
Chris Mason3de45862008-11-17 21:02:50 -05001419 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001420 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Li Zefanb83cc962010-12-20 16:04:08 +08001421 NULL, transid, readonly);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001422 } else {
Chris Mason3de45862008-11-17 21:02:50 -05001423 struct inode *src_inode;
Sage Weil72fd0322010-10-29 15:41:32 -04001424 src_file = fget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001425 if (!src_file) {
1426 ret = -EINVAL;
1427 goto out;
1428 }
1429
1430 src_inode = src_file->f_path.dentry->d_inode;
1431 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -05001432 printk(KERN_INFO "btrfs: Snapshot src from "
1433 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -05001434 ret = -EINVAL;
1435 fput(src_file);
1436 goto out;
1437 }
Sage Weil72fd0322010-10-29 15:41:32 -04001438 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1439 BTRFS_I(src_inode)->root,
Li Zefanb83cc962010-12-20 16:04:08 +08001440 transid, readonly);
Chris Mason3de45862008-11-17 21:02:50 -05001441 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001442 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001443out:
Sage Weil72fd0322010-10-29 15:41:32 -04001444 return ret;
1445}
1446
1447static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001448 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001449{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001450 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001451 int ret;
1452
Li Zefanfa0d2b92010-12-20 15:53:28 +08001453 vol_args = memdup_user(arg, sizeof(*vol_args));
1454 if (IS_ERR(vol_args))
1455 return PTR_ERR(vol_args);
1456 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001457
Li Zefanfa0d2b92010-12-20 15:53:28 +08001458 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001459 vol_args->fd, subvol,
1460 NULL, false);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001461
Li Zefanfa0d2b92010-12-20 15:53:28 +08001462 kfree(vol_args);
1463 return ret;
1464}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001465
Li Zefanfa0d2b92010-12-20 15:53:28 +08001466static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1467 void __user *arg, int subvol)
1468{
1469 struct btrfs_ioctl_vol_args_v2 *vol_args;
1470 int ret;
1471 u64 transid = 0;
1472 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001473 bool readonly = false;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001474
Li Zefanfa0d2b92010-12-20 15:53:28 +08001475 vol_args = memdup_user(arg, sizeof(*vol_args));
1476 if (IS_ERR(vol_args))
1477 return PTR_ERR(vol_args);
1478 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001479
Li Zefanb83cc962010-12-20 16:04:08 +08001480 if (vol_args->flags &
1481 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1482 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001483 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001484 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001485
1486 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1487 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001488 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1489 readonly = true;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001490
1491 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001492 vol_args->fd, subvol,
1493 ptr, readonly);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001494
1495 if (ret == 0 && ptr &&
1496 copy_to_user(arg +
1497 offsetof(struct btrfs_ioctl_vol_args_v2,
1498 transid), ptr, sizeof(*ptr)))
1499 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001500out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001501 kfree(vol_args);
1502 return ret;
1503}
1504
Li Zefan0caa1022010-12-20 16:30:25 +08001505static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1506 void __user *arg)
1507{
1508 struct inode *inode = fdentry(file)->d_inode;
1509 struct btrfs_root *root = BTRFS_I(inode)->root;
1510 int ret = 0;
1511 u64 flags = 0;
1512
Li Zefan33345d012011-04-20 10:31:50 +08001513 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001514 return -EINVAL;
1515
1516 down_read(&root->fs_info->subvol_sem);
1517 if (btrfs_root_readonly(root))
1518 flags |= BTRFS_SUBVOL_RDONLY;
1519 up_read(&root->fs_info->subvol_sem);
1520
1521 if (copy_to_user(arg, &flags, sizeof(flags)))
1522 ret = -EFAULT;
1523
1524 return ret;
1525}
1526
1527static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1528 void __user *arg)
1529{
1530 struct inode *inode = fdentry(file)->d_inode;
1531 struct btrfs_root *root = BTRFS_I(inode)->root;
1532 struct btrfs_trans_handle *trans;
1533 u64 root_flags;
1534 u64 flags;
1535 int ret = 0;
1536
1537 if (root->fs_info->sb->s_flags & MS_RDONLY)
1538 return -EROFS;
1539
Li Zefan33345d012011-04-20 10:31:50 +08001540 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001541 return -EINVAL;
1542
1543 if (copy_from_user(&flags, arg, sizeof(flags)))
1544 return -EFAULT;
1545
Li Zefanb4dc2b82011-02-16 06:06:34 +00001546 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
Li Zefan0caa1022010-12-20 16:30:25 +08001547 return -EINVAL;
1548
1549 if (flags & ~BTRFS_SUBVOL_RDONLY)
1550 return -EOPNOTSUPP;
1551
Serge E. Hallyn2e149672011-03-23 16:43:26 -07001552 if (!inode_owner_or_capable(inode))
Li Zefanb4dc2b82011-02-16 06:06:34 +00001553 return -EACCES;
1554
Li Zefan0caa1022010-12-20 16:30:25 +08001555 down_write(&root->fs_info->subvol_sem);
1556
1557 /* nothing to do */
1558 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1559 goto out;
1560
1561 root_flags = btrfs_root_flags(&root->root_item);
1562 if (flags & BTRFS_SUBVOL_RDONLY)
1563 btrfs_set_root_flags(&root->root_item,
1564 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1565 else
1566 btrfs_set_root_flags(&root->root_item,
1567 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1568
1569 trans = btrfs_start_transaction(root, 1);
1570 if (IS_ERR(trans)) {
1571 ret = PTR_ERR(trans);
1572 goto out_reset;
1573 }
1574
Li Zefanb4dc2b82011-02-16 06:06:34 +00001575 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001576 &root->root_key, &root->root_item);
1577
1578 btrfs_commit_transaction(trans, root);
1579out_reset:
1580 if (ret)
1581 btrfs_set_root_flags(&root->root_item, root_flags);
1582out:
1583 up_write(&root->fs_info->subvol_sem);
1584 return ret;
1585}
1586
Yan, Zheng76dda932009-09-21 16:00:26 -04001587/*
1588 * helper to check if the subvolume references other subvolumes
1589 */
1590static noinline int may_destroy_subvol(struct btrfs_root *root)
1591{
1592 struct btrfs_path *path;
1593 struct btrfs_key key;
1594 int ret;
1595
1596 path = btrfs_alloc_path();
1597 if (!path)
1598 return -ENOMEM;
1599
1600 key.objectid = root->root_key.objectid;
1601 key.type = BTRFS_ROOT_REF_KEY;
1602 key.offset = (u64)-1;
1603
1604 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1605 &key, path, 0, 0);
1606 if (ret < 0)
1607 goto out;
1608 BUG_ON(ret == 0);
1609
1610 ret = 0;
1611 if (path->slots[0] > 0) {
1612 path->slots[0]--;
1613 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1614 if (key.objectid == root->root_key.objectid &&
1615 key.type == BTRFS_ROOT_REF_KEY)
1616 ret = -ENOTEMPTY;
1617 }
1618out:
1619 btrfs_free_path(path);
1620 return ret;
1621}
1622
Chris Masonac8e9812010-02-28 15:39:26 -05001623static noinline int key_in_sk(struct btrfs_key *key,
1624 struct btrfs_ioctl_search_key *sk)
1625{
Chris Masonabc6e132010-03-18 12:10:08 -04001626 struct btrfs_key test;
1627 int ret;
1628
1629 test.objectid = sk->min_objectid;
1630 test.type = sk->min_type;
1631 test.offset = sk->min_offset;
1632
1633 ret = btrfs_comp_cpu_keys(key, &test);
1634 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001635 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001636
1637 test.objectid = sk->max_objectid;
1638 test.type = sk->max_type;
1639 test.offset = sk->max_offset;
1640
1641 ret = btrfs_comp_cpu_keys(key, &test);
1642 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001643 return 0;
1644 return 1;
1645}
1646
1647static noinline int copy_to_sk(struct btrfs_root *root,
1648 struct btrfs_path *path,
1649 struct btrfs_key *key,
1650 struct btrfs_ioctl_search_key *sk,
1651 char *buf,
1652 unsigned long *sk_offset,
1653 int *num_found)
1654{
1655 u64 found_transid;
1656 struct extent_buffer *leaf;
1657 struct btrfs_ioctl_search_header sh;
1658 unsigned long item_off;
1659 unsigned long item_len;
1660 int nritems;
1661 int i;
1662 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001663 int ret = 0;
1664
1665 leaf = path->nodes[0];
1666 slot = path->slots[0];
1667 nritems = btrfs_header_nritems(leaf);
1668
1669 if (btrfs_header_generation(leaf) > sk->max_transid) {
1670 i = nritems;
1671 goto advance_key;
1672 }
1673 found_transid = btrfs_header_generation(leaf);
1674
1675 for (i = slot; i < nritems; i++) {
1676 item_off = btrfs_item_ptr_offset(leaf, i);
1677 item_len = btrfs_item_size_nr(leaf, i);
1678
1679 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1680 item_len = 0;
1681
1682 if (sizeof(sh) + item_len + *sk_offset >
1683 BTRFS_SEARCH_ARGS_BUFSIZE) {
1684 ret = 1;
1685 goto overflow;
1686 }
1687
1688 btrfs_item_key_to_cpu(leaf, key, i);
1689 if (!key_in_sk(key, sk))
1690 continue;
1691
1692 sh.objectid = key->objectid;
1693 sh.offset = key->offset;
1694 sh.type = key->type;
1695 sh.len = item_len;
1696 sh.transid = found_transid;
1697
1698 /* copy search result header */
1699 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1700 *sk_offset += sizeof(sh);
1701
1702 if (item_len) {
1703 char *p = buf + *sk_offset;
1704 /* copy the item */
1705 read_extent_buffer(leaf, p,
1706 item_off, item_len);
1707 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001708 }
Hugo Millse2156862011-05-14 17:43:41 +00001709 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001710
1711 if (*num_found >= sk->nr_items)
1712 break;
1713 }
1714advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001715 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001716 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1717 key->offset++;
1718 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1719 key->offset = 0;
1720 key->type++;
1721 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1722 key->offset = 0;
1723 key->type = 0;
1724 key->objectid++;
1725 } else
1726 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001727overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001728 return ret;
1729}
1730
1731static noinline int search_ioctl(struct inode *inode,
1732 struct btrfs_ioctl_search_args *args)
1733{
1734 struct btrfs_root *root;
1735 struct btrfs_key key;
1736 struct btrfs_key max_key;
1737 struct btrfs_path *path;
1738 struct btrfs_ioctl_search_key *sk = &args->key;
1739 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1740 int ret;
1741 int num_found = 0;
1742 unsigned long sk_offset = 0;
1743
1744 path = btrfs_alloc_path();
1745 if (!path)
1746 return -ENOMEM;
1747
1748 if (sk->tree_id == 0) {
1749 /* search the root of the inode that was passed */
1750 root = BTRFS_I(inode)->root;
1751 } else {
1752 key.objectid = sk->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",
1758 sk->tree_id);
1759 btrfs_free_path(path);
1760 return -ENOENT;
1761 }
1762 }
1763
1764 key.objectid = sk->min_objectid;
1765 key.type = sk->min_type;
1766 key.offset = sk->min_offset;
1767
1768 max_key.objectid = sk->max_objectid;
1769 max_key.type = sk->max_type;
1770 max_key.offset = sk->max_offset;
1771
1772 path->keep_locks = 1;
1773
1774 while(1) {
1775 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1776 sk->min_transid);
1777 if (ret != 0) {
1778 if (ret > 0)
1779 ret = 0;
1780 goto err;
1781 }
1782 ret = copy_to_sk(root, path, &key, sk, args->buf,
1783 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02001784 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001785 if (ret || num_found >= sk->nr_items)
1786 break;
1787
1788 }
1789 ret = 0;
1790err:
1791 sk->nr_items = num_found;
1792 btrfs_free_path(path);
1793 return ret;
1794}
1795
1796static noinline int btrfs_ioctl_tree_search(struct file *file,
1797 void __user *argp)
1798{
1799 struct btrfs_ioctl_search_args *args;
1800 struct inode *inode;
1801 int ret;
1802
1803 if (!capable(CAP_SYS_ADMIN))
1804 return -EPERM;
1805
Julia Lawall2354d082010-10-29 15:14:18 -04001806 args = memdup_user(argp, sizeof(*args));
1807 if (IS_ERR(args))
1808 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05001809
Chris Masonac8e9812010-02-28 15:39:26 -05001810 inode = fdentry(file)->d_inode;
1811 ret = search_ioctl(inode, args);
1812 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1813 ret = -EFAULT;
1814 kfree(args);
1815 return ret;
1816}
1817
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001818/*
Chris Masonac8e9812010-02-28 15:39:26 -05001819 * Search INODE_REFs to identify path name of 'dirid' directory
1820 * in a 'tree_id' tree. and sets path name to 'name'.
1821 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001822static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1823 u64 tree_id, u64 dirid, char *name)
1824{
1825 struct btrfs_root *root;
1826 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001827 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001828 int ret = -1;
1829 int slot;
1830 int len;
1831 int total_len = 0;
1832 struct btrfs_inode_ref *iref;
1833 struct extent_buffer *l;
1834 struct btrfs_path *path;
1835
1836 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1837 name[0]='\0';
1838 return 0;
1839 }
1840
1841 path = btrfs_alloc_path();
1842 if (!path)
1843 return -ENOMEM;
1844
Chris Masonac8e9812010-02-28 15:39:26 -05001845 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001846
1847 key.objectid = tree_id;
1848 key.type = BTRFS_ROOT_ITEM_KEY;
1849 key.offset = (u64)-1;
1850 root = btrfs_read_fs_root_no_name(info, &key);
1851 if (IS_ERR(root)) {
1852 printk(KERN_ERR "could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04001853 ret = -ENOENT;
1854 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001855 }
1856
1857 key.objectid = dirid;
1858 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001859 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001860
1861 while(1) {
1862 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1863 if (ret < 0)
1864 goto out;
1865
1866 l = path->nodes[0];
1867 slot = path->slots[0];
Chris Mason8ad6fca2010-03-18 12:23:10 -04001868 if (ret > 0 && slot > 0)
1869 slot--;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001870 btrfs_item_key_to_cpu(l, &key, slot);
1871
1872 if (ret > 0 && (key.objectid != dirid ||
Chris Masonac8e9812010-02-28 15:39:26 -05001873 key.type != BTRFS_INODE_REF_KEY)) {
1874 ret = -ENOENT;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001875 goto out;
Chris Masonac8e9812010-02-28 15:39:26 -05001876 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001877
1878 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1879 len = btrfs_inode_ref_name_len(l, iref);
1880 ptr -= len + 1;
1881 total_len += len + 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001882 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001883 goto out;
1884
1885 *(ptr + len) = '/';
1886 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1887
1888 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1889 break;
1890
David Sterbab3b4aa72011-04-21 01:20:15 +02001891 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001892 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04001893 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001894 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001895 }
Chris Masonac8e9812010-02-28 15:39:26 -05001896 if (ptr < name)
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001897 goto out;
Li Zefan77906a502011-07-14 03:16:00 +00001898 memmove(name, ptr, total_len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001899 name[total_len]='\0';
1900 ret = 0;
1901out:
1902 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05001903 return ret;
1904}
1905
1906static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1907 void __user *argp)
1908{
1909 struct btrfs_ioctl_ino_lookup_args *args;
1910 struct inode *inode;
1911 int ret;
1912
1913 if (!capable(CAP_SYS_ADMIN))
1914 return -EPERM;
1915
Julia Lawall2354d082010-10-29 15:14:18 -04001916 args = memdup_user(argp, sizeof(*args));
1917 if (IS_ERR(args))
1918 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00001919
Chris Masonac8e9812010-02-28 15:39:26 -05001920 inode = fdentry(file)->d_inode;
1921
Chris Mason1b53ac42010-03-18 12:17:05 -04001922 if (args->treeid == 0)
1923 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1924
Chris Masonac8e9812010-02-28 15:39:26 -05001925 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1926 args->treeid, args->objectid,
1927 args->name);
1928
1929 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1930 ret = -EFAULT;
1931
1932 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00001933 return ret;
1934}
1935
Yan, Zheng76dda932009-09-21 16:00:26 -04001936static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1937 void __user *arg)
1938{
1939 struct dentry *parent = fdentry(file);
1940 struct dentry *dentry;
1941 struct inode *dir = parent->d_inode;
1942 struct inode *inode;
1943 struct btrfs_root *root = BTRFS_I(dir)->root;
1944 struct btrfs_root *dest = NULL;
1945 struct btrfs_ioctl_vol_args *vol_args;
1946 struct btrfs_trans_handle *trans;
1947 int namelen;
1948 int ret;
1949 int err = 0;
1950
Yan, Zheng76dda932009-09-21 16:00:26 -04001951 vol_args = memdup_user(arg, sizeof(*vol_args));
1952 if (IS_ERR(vol_args))
1953 return PTR_ERR(vol_args);
1954
1955 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1956 namelen = strlen(vol_args->name);
1957 if (strchr(vol_args->name, '/') ||
1958 strncmp(vol_args->name, "..", namelen) == 0) {
1959 err = -EINVAL;
1960 goto out;
1961 }
1962
Al Viroa561be72011-11-23 11:57:51 -05001963 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04001964 if (err)
1965 goto out;
1966
1967 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1968 dentry = lookup_one_len(vol_args->name, parent, namelen);
1969 if (IS_ERR(dentry)) {
1970 err = PTR_ERR(dentry);
1971 goto out_unlock_dir;
1972 }
1973
1974 if (!dentry->d_inode) {
1975 err = -ENOENT;
1976 goto out_dput;
1977 }
1978
1979 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04001980 dest = BTRFS_I(inode)->root;
1981 if (!capable(CAP_SYS_ADMIN)){
1982 /*
1983 * Regular user. Only allow this with a special mount
1984 * option, when the user has write+exec access to the
1985 * subvol root, and when rmdir(2) would have been
1986 * allowed.
1987 *
1988 * Note that this is _not_ check that the subvol is
1989 * empty or doesn't contain data that we wouldn't
1990 * otherwise be able to delete.
1991 *
1992 * Users who want to delete empty subvols should try
1993 * rmdir(2).
1994 */
1995 err = -EPERM;
1996 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1997 goto out_dput;
1998
1999 /*
2000 * Do not allow deletion if the parent dir is the same
2001 * as the dir to be deleted. That means the ioctl
2002 * must be called on the dentry referencing the root
2003 * of the subvol, not a random directory contained
2004 * within it.
2005 */
2006 err = -EINVAL;
2007 if (root == dest)
2008 goto out_dput;
2009
2010 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2011 if (err)
2012 goto out_dput;
2013
2014 /* check if subvolume may be deleted by a non-root user */
2015 err = btrfs_may_delete(dir, dentry, 1);
2016 if (err)
2017 goto out_dput;
2018 }
2019
Li Zefan33345d012011-04-20 10:31:50 +08002020 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002021 err = -EINVAL;
2022 goto out_dput;
2023 }
2024
Yan, Zheng76dda932009-09-21 16:00:26 -04002025 mutex_lock(&inode->i_mutex);
2026 err = d_invalidate(dentry);
2027 if (err)
2028 goto out_unlock;
2029
2030 down_write(&root->fs_info->subvol_sem);
2031
2032 err = may_destroy_subvol(dest);
2033 if (err)
2034 goto out_up_write;
2035
Yan, Zhenga22285a2010-05-16 10:48:46 -04002036 trans = btrfs_start_transaction(root, 0);
2037 if (IS_ERR(trans)) {
2038 err = PTR_ERR(trans);
Dan Carpenterd3270992010-05-29 09:46:47 +00002039 goto out_up_write;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002040 }
2041 trans->block_rsv = &root->fs_info->global_block_rsv;
2042
Yan, Zheng76dda932009-09-21 16:00:26 -04002043 ret = btrfs_unlink_subvol(trans, root, dir,
2044 dest->root_key.objectid,
2045 dentry->d_name.name,
2046 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002047 if (ret) {
2048 err = ret;
2049 btrfs_abort_transaction(trans, root, ret);
2050 goto out_end_trans;
2051 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002052
2053 btrfs_record_root_in_trans(trans, dest);
2054
2055 memset(&dest->root_item.drop_progress, 0,
2056 sizeof(dest->root_item.drop_progress));
2057 dest->root_item.drop_level = 0;
2058 btrfs_set_root_refs(&dest->root_item, 0);
2059
Yan, Zhengd68fc572010-05-16 10:49:58 -04002060 if (!xchg(&dest->orphan_item_inserted, 1)) {
2061 ret = btrfs_insert_orphan_item(trans,
2062 root->fs_info->tree_root,
2063 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002064 if (ret) {
2065 btrfs_abort_transaction(trans, root, ret);
2066 err = ret;
2067 goto out_end_trans;
2068 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002069 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002070out_end_trans:
Sage Weil531cb132010-10-29 15:41:32 -04002071 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002072 if (ret && !err)
2073 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002074 inode->i_flags |= S_DEAD;
2075out_up_write:
2076 up_write(&root->fs_info->subvol_sem);
2077out_unlock:
2078 mutex_unlock(&inode->i_mutex);
2079 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002080 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002081 btrfs_invalidate_inodes(dest);
2082 d_delete(dentry);
2083 }
2084out_dput:
2085 dput(dentry);
2086out_unlock_dir:
2087 mutex_unlock(&dir->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -05002088 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002089out:
2090 kfree(vol_args);
2091 return err;
2092}
2093
Chris Mason1e701a32010-03-11 09:42:04 -05002094static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002095{
2096 struct inode *inode = fdentry(file)->d_inode;
2097 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002098 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002099 int ret;
2100
Li Zefanb83cc962010-12-20 16:04:08 +08002101 if (btrfs_root_readonly(root))
2102 return -EROFS;
2103
Al Viroa561be72011-11-23 11:57:51 -05002104 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002105 if (ret)
2106 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002107
2108 switch (inode->i_mode & S_IFMT) {
2109 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002110 if (!capable(CAP_SYS_ADMIN)) {
2111 ret = -EPERM;
2112 goto out;
2113 }
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002114 ret = btrfs_defrag_root(root, 0);
2115 if (ret)
2116 goto out;
2117 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002118 break;
2119 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002120 if (!(file->f_mode & FMODE_WRITE)) {
2121 ret = -EINVAL;
2122 goto out;
2123 }
Chris Mason1e701a32010-03-11 09:42:04 -05002124
2125 range = kzalloc(sizeof(*range), GFP_KERNEL);
2126 if (!range) {
2127 ret = -ENOMEM;
2128 goto out;
2129 }
2130
2131 if (argp) {
2132 if (copy_from_user(range, argp,
2133 sizeof(*range))) {
2134 ret = -EFAULT;
2135 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002136 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002137 }
2138 /* compression requires us to start the IO */
2139 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2140 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2141 range->extent_thresh = (u32)-1;
2142 }
2143 } else {
2144 /* the rest are all set to zero by kzalloc */
2145 range->len = (u64)-1;
2146 }
Chris Mason4cb53002011-05-24 15:35:30 -04002147 ret = btrfs_defrag_file(fdentry(file)->d_inode, file,
2148 range, 0, 0);
2149 if (ret > 0)
2150 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002151 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002152 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002153 default:
2154 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002155 }
Chris Masone441d542009-01-05 16:57:23 -05002156out:
Al Viro2a79f172011-12-09 08:06:57 -05002157 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002158 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002159}
2160
Christoph Hellwigb2950862008-12-02 09:54:17 -05002161static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002162{
2163 struct btrfs_ioctl_vol_args *vol_args;
2164 int ret;
2165
Chris Masone441d542009-01-05 16:57:23 -05002166 if (!capable(CAP_SYS_ADMIN))
2167 return -EPERM;
2168
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002169 mutex_lock(&root->fs_info->volume_mutex);
2170 if (root->fs_info->balance_ctl) {
2171 printk(KERN_INFO "btrfs: balance in progress\n");
2172 ret = -EINVAL;
2173 goto out;
2174 }
2175
Li Zefandae7b662009-04-08 15:06:54 +08002176 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002177 if (IS_ERR(vol_args)) {
2178 ret = PTR_ERR(vol_args);
2179 goto out;
2180 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002181
Mark Fasheh5516e592008-07-24 12:20:14 -04002182 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002183 ret = btrfs_init_new_device(root, vol_args->name);
2184
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002185 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002186out:
2187 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002188 return ret;
2189}
2190
Christoph Hellwigb2950862008-12-02 09:54:17 -05002191static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002192{
2193 struct btrfs_ioctl_vol_args *vol_args;
2194 int ret;
2195
Chris Masone441d542009-01-05 16:57:23 -05002196 if (!capable(CAP_SYS_ADMIN))
2197 return -EPERM;
2198
Yan Zhengc146afa2008-11-12 14:34:12 -05002199 if (root->fs_info->sb->s_flags & MS_RDONLY)
2200 return -EROFS;
2201
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002202 mutex_lock(&root->fs_info->volume_mutex);
2203 if (root->fs_info->balance_ctl) {
2204 printk(KERN_INFO "btrfs: balance in progress\n");
2205 ret = -EINVAL;
2206 goto out;
2207 }
2208
Li Zefandae7b662009-04-08 15:06:54 +08002209 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002210 if (IS_ERR(vol_args)) {
2211 ret = PTR_ERR(vol_args);
2212 goto out;
2213 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002214
Mark Fasheh5516e592008-07-24 12:20:14 -04002215 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002216 ret = btrfs_rm_device(root, vol_args->name);
2217
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002218 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002219out:
2220 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002221 return ret;
2222}
2223
Jan Schmidt475f6382011-03-11 15:41:01 +01002224static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2225{
Li Zefan027ed2f2011-06-08 08:27:56 +00002226 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002227 struct btrfs_device *device;
2228 struct btrfs_device *next;
2229 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002230 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002231
2232 if (!capable(CAP_SYS_ADMIN))
2233 return -EPERM;
2234
Li Zefan027ed2f2011-06-08 08:27:56 +00002235 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2236 if (!fi_args)
2237 return -ENOMEM;
2238
2239 fi_args->num_devices = fs_devices->num_devices;
2240 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002241
2242 mutex_lock(&fs_devices->device_list_mutex);
2243 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002244 if (device->devid > fi_args->max_id)
2245 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002246 }
2247 mutex_unlock(&fs_devices->device_list_mutex);
2248
Li Zefan027ed2f2011-06-08 08:27:56 +00002249 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2250 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002251
Li Zefan027ed2f2011-06-08 08:27:56 +00002252 kfree(fi_args);
2253 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002254}
2255
2256static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2257{
2258 struct btrfs_ioctl_dev_info_args *di_args;
2259 struct btrfs_device *dev;
2260 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2261 int ret = 0;
2262 char *s_uuid = NULL;
2263 char empty_uuid[BTRFS_UUID_SIZE] = {0};
2264
2265 if (!capable(CAP_SYS_ADMIN))
2266 return -EPERM;
2267
2268 di_args = memdup_user(arg, sizeof(*di_args));
2269 if (IS_ERR(di_args))
2270 return PTR_ERR(di_args);
2271
2272 if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0)
2273 s_uuid = di_args->uuid;
2274
2275 mutex_lock(&fs_devices->device_list_mutex);
2276 dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL);
2277 mutex_unlock(&fs_devices->device_list_mutex);
2278
2279 if (!dev) {
2280 ret = -ENODEV;
2281 goto out;
2282 }
2283
2284 di_args->devid = dev->devid;
2285 di_args->bytes_used = dev->bytes_used;
2286 di_args->total_bytes = dev->total_bytes;
2287 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002288 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002289 struct rcu_string *name;
2290
2291 rcu_read_lock();
2292 name = rcu_dereference(dev->name);
2293 strncpy(di_args->path, name->str, sizeof(di_args->path));
2294 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002295 di_args->path[sizeof(di_args->path) - 1] = 0;
2296 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002297 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002298 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002299
2300out:
2301 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2302 ret = -EFAULT;
2303
2304 kfree(di_args);
2305 return ret;
2306}
2307
Yan, Zheng76dda932009-09-21 16:00:26 -04002308static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
2309 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002310{
2311 struct inode *inode = fdentry(file)->d_inode;
2312 struct btrfs_root *root = BTRFS_I(inode)->root;
2313 struct file *src_file;
2314 struct inode *src;
2315 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002316 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002317 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002318 char *buf;
2319 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002320 u32 nritems;
2321 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002322 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002323 u64 len = olen;
2324 u64 bs = root->fs_info->sb->s_blocksize;
2325 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05002326
Sage Weilc5c9cd42008-11-12 14:32:25 -05002327 /*
2328 * TODO:
2329 * - split compressed inline extents. annoying: we need to
2330 * decompress into destination's address_space (the file offset
2331 * may change, so source mapping won't do), then recompress (or
2332 * otherwise reinsert) a subrange.
2333 * - allow ranges within the same file to be cloned (provided
2334 * they don't overlap)?
2335 */
2336
Chris Masone441d542009-01-05 16:57:23 -05002337 /* the destination must be opened for writing */
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002338 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
Chris Masone441d542009-01-05 16:57:23 -05002339 return -EINVAL;
2340
Li Zefanb83cc962010-12-20 16:04:08 +08002341 if (btrfs_root_readonly(root))
2342 return -EROFS;
2343
Al Viroa561be72011-11-23 11:57:51 -05002344 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002345 if (ret)
2346 return ret;
2347
Sage Weilc5c9cd42008-11-12 14:32:25 -05002348 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002349 if (!src_file) {
2350 ret = -EBADF;
2351 goto out_drop_write;
2352 }
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002353
David Sterba362a20c2011-08-01 18:11:57 +02002354 ret = -EXDEV;
2355 if (src_file->f_path.mnt != file->f_path.mnt)
2356 goto out_fput;
2357
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002358 src = src_file->f_dentry->d_inode;
2359
Sage Weilc5c9cd42008-11-12 14:32:25 -05002360 ret = -EINVAL;
2361 if (src == inode)
2362 goto out_fput;
2363
Dan Rosenberg5dc64162010-05-15 11:27:37 -04002364 /* the src must be open for reading */
2365 if (!(src_file->f_mode & FMODE_READ))
2366 goto out_fput;
2367
Li Zefan0e7b8242011-09-18 10:20:46 -04002368 /* don't make the dst file partly checksummed */
2369 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2370 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
2371 goto out_fput;
2372
Yan Zhengae01a0a2008-08-04 23:23:47 -04002373 ret = -EISDIR;
2374 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002375 goto out_fput;
2376
Yan Zhengae01a0a2008-08-04 23:23:47 -04002377 ret = -EXDEV;
David Sterba362a20c2011-08-01 18:11:57 +02002378 if (src->i_sb != inode->i_sb)
Yan Zhengae01a0a2008-08-04 23:23:47 -04002379 goto out_fput;
2380
2381 ret = -ENOMEM;
2382 buf = vmalloc(btrfs_level_size(root, 0));
2383 if (!buf)
2384 goto out_fput;
2385
2386 path = btrfs_alloc_path();
2387 if (!path) {
2388 vfree(buf);
2389 goto out_fput;
2390 }
2391 path->reada = 2;
2392
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002393 if (inode < src) {
Sage Weilfccdae42010-10-29 15:37:33 -04002394 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
2395 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002396 } else {
Sage Weilfccdae42010-10-29 15:37:33 -04002397 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
2398 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002399 }
2400
Sage Weilc5c9cd42008-11-12 14:32:25 -05002401 /* determine range to clone */
2402 ret = -EINVAL;
Dan Rosenberg2ebc3462010-07-19 16:58:20 -04002403 if (off + len > src->i_size || off + len < off)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002404 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002405 if (len == 0)
2406 olen = len = src->i_size - off;
2407 /* if we extend to eof, continue to block boundary */
2408 if (off + len == src->i_size)
Li Zefan2a6b8da2010-11-19 01:36:10 +00002409 len = ALIGN(src->i_size, bs) - off;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002410
2411 /* verify the end result is block aligned */
Li Zefan2a6b8da2010-11-19 01:36:10 +00002412 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
2413 !IS_ALIGNED(destoff, bs))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002414 goto out_unlock;
2415
Li Zefand525e8a2011-09-11 10:52:25 -04002416 if (destoff > inode->i_size) {
2417 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
2418 if (ret)
2419 goto out_unlock;
2420 }
2421
Li Zefan71ef0782011-09-18 10:20:46 -04002422 /* truncate page cache pages from target inode range */
2423 truncate_inode_pages_range(&inode->i_data, destoff,
2424 PAGE_CACHE_ALIGN(destoff + len) - 1);
2425
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002426 /* do any pending delalloc/csum calc on src, one way or
2427 another, and lock file content */
2428 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002429 struct btrfs_ordered_extent *ordered;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002430 lock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Sage Weil9a019192010-10-29 15:37:33 -04002431 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
2432 if (!ordered &&
2433 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
2434 EXTENT_DELALLOC, 0, NULL))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002435 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002436 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002437 if (ordered)
2438 btrfs_put_ordered_extent(ordered);
Sage Weil9a019192010-10-29 15:37:33 -04002439 btrfs_wait_ordered_range(src, off, len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002440 }
2441
Sage Weilc5c9cd42008-11-12 14:32:25 -05002442 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002443 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002444 key.type = BTRFS_EXTENT_DATA_KEY;
2445 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002446
2447 while (1) {
2448 /*
2449 * note the key will change type as we walk through the
2450 * tree.
2451 */
David Sterba362a20c2011-08-01 18:11:57 +02002452 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2453 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002454 if (ret < 0)
2455 goto out;
2456
Yan Zhengae01a0a2008-08-04 23:23:47 -04002457 nritems = btrfs_header_nritems(path->nodes[0]);
2458 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002459 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002460 if (ret < 0)
2461 goto out;
2462 if (ret > 0)
2463 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002464 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002465 }
2466 leaf = path->nodes[0];
2467 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002468
Yan Zhengae01a0a2008-08-04 23:23:47 -04002469 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002470 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002471 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002472 break;
2473
Sage Weilc5c9cd42008-11-12 14:32:25 -05002474 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2475 struct btrfs_file_extent_item *extent;
2476 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002477 u32 size;
2478 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002479 u64 disko = 0, diskl = 0;
2480 u64 datao = 0, datal = 0;
2481 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002482 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002483
2484 size = btrfs_item_size_nr(leaf, slot);
2485 read_extent_buffer(leaf, buf,
2486 btrfs_item_ptr_offset(leaf, slot),
2487 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002488
2489 extent = btrfs_item_ptr(leaf, slot,
2490 struct btrfs_file_extent_item);
2491 comp = btrfs_file_extent_compression(leaf, extent);
2492 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002493 if (type == BTRFS_FILE_EXTENT_REG ||
2494 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002495 disko = btrfs_file_extent_disk_bytenr(leaf,
2496 extent);
2497 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2498 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002499 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002500 datal = btrfs_file_extent_num_bytes(leaf,
2501 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002502 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2503 /* take upper bound, may be compressed */
2504 datal = btrfs_file_extent_ram_bytes(leaf,
2505 extent);
2506 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002507 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002508
Sage Weil050006a2010-10-29 15:37:33 -04002509 if (key.offset + datal <= off ||
Sage Weilc5c9cd42008-11-12 14:32:25 -05002510 key.offset >= off+len)
2511 goto next;
2512
Zheng Yan31840ae2008-09-23 13:14:14 -04002513 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08002514 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08002515 if (off <= key.offset)
2516 new_key.offset = key.offset + destoff - off;
2517 else
2518 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002519
Sage Weilb6f34092011-09-20 14:48:51 -04002520 /*
2521 * 1 - adjusting old extent (we may have to split it)
2522 * 1 - add new extent
2523 * 1 - inode update
2524 */
2525 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002526 if (IS_ERR(trans)) {
2527 ret = PTR_ERR(trans);
2528 goto out;
2529 }
2530
Chris Masonc8a894d2009-06-27 21:07:03 -04002531 if (type == BTRFS_FILE_EXTENT_REG ||
2532 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04002533 /*
2534 * a | --- range to clone ---| b
2535 * | ------------- extent ------------- |
2536 */
2537
2538 /* substract range b */
2539 if (key.offset + datal > off + len)
2540 datal = off + len - key.offset;
2541
2542 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002543 if (off > key.offset) {
2544 datao += off - key.offset;
2545 datal -= off - key.offset;
2546 }
2547
Yan, Zhenga22285a2010-05-16 10:48:46 -04002548 ret = btrfs_drop_extents(trans, inode,
2549 new_key.offset,
2550 new_key.offset + datal,
2551 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002552 if (ret) {
2553 btrfs_abort_transaction(trans, root,
2554 ret);
2555 btrfs_end_transaction(trans, root);
2556 goto out;
2557 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002558
Sage Weilc5c9cd42008-11-12 14:32:25 -05002559 ret = btrfs_insert_empty_item(trans, root, path,
2560 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002561 if (ret) {
2562 btrfs_abort_transaction(trans, root,
2563 ret);
2564 btrfs_end_transaction(trans, root);
2565 goto out;
2566 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002567
2568 leaf = path->nodes[0];
2569 slot = path->slots[0];
2570 write_extent_buffer(leaf, buf,
2571 btrfs_item_ptr_offset(leaf, slot),
2572 size);
2573
2574 extent = btrfs_item_ptr(leaf, slot,
2575 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002576
Sage Weilc5c9cd42008-11-12 14:32:25 -05002577 /* disko == 0 means it's a hole */
2578 if (!disko)
2579 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002580
2581 btrfs_set_file_extent_offset(leaf, extent,
2582 datao);
2583 btrfs_set_file_extent_num_bytes(leaf, extent,
2584 datal);
2585 if (disko) {
2586 inode_add_bytes(inode, datal);
2587 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002588 disko, diskl, 0,
2589 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08002590 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002591 new_key.offset - datao,
2592 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002593 if (ret) {
2594 btrfs_abort_transaction(trans,
2595 root,
2596 ret);
2597 btrfs_end_transaction(trans,
2598 root);
2599 goto out;
2600
2601 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002602 }
2603 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2604 u64 skip = 0;
2605 u64 trim = 0;
2606 if (off > key.offset) {
2607 skip = off - key.offset;
2608 new_key.offset += skip;
2609 }
Chris Masond3977122009-01-05 21:25:51 -05002610
Sage Weilc5c9cd42008-11-12 14:32:25 -05002611 if (key.offset + datal > off+len)
2612 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05002613
Sage Weilc5c9cd42008-11-12 14:32:25 -05002614 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05002615 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002616 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002617 goto out;
2618 }
2619 size -= skip + trim;
2620 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002621
2622 ret = btrfs_drop_extents(trans, inode,
2623 new_key.offset,
2624 new_key.offset + datal,
2625 &hint_byte, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002626 if (ret) {
2627 btrfs_abort_transaction(trans, root,
2628 ret);
2629 btrfs_end_transaction(trans, root);
2630 goto out;
2631 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04002632
Sage Weilc5c9cd42008-11-12 14:32:25 -05002633 ret = btrfs_insert_empty_item(trans, root, path,
2634 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002635 if (ret) {
2636 btrfs_abort_transaction(trans, root,
2637 ret);
2638 btrfs_end_transaction(trans, root);
2639 goto out;
2640 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05002641
2642 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05002643 u32 start =
2644 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002645 memmove(buf+start, buf+start+skip,
2646 datal);
2647 }
2648
2649 leaf = path->nodes[0];
2650 slot = path->slots[0];
2651 write_extent_buffer(leaf, buf,
2652 btrfs_item_ptr_offset(leaf, slot),
2653 size);
2654 inode_add_bytes(inode, datal);
2655 }
2656
2657 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002658 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002659
Josef Bacik0c4d2d92012-04-05 15:03:02 -04002660 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002661 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00002662
2663 /*
2664 * we round up to the block size at eof when
2665 * determining which extents to clone above,
2666 * but shouldn't round up the file size
2667 */
2668 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00002669 if (endoff > destoff+olen)
2670 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00002671 if (endoff > inode->i_size)
2672 btrfs_i_size_write(inode, endoff);
2673
Yan, Zhenga22285a2010-05-16 10:48:46 -04002674 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002675 if (ret) {
2676 btrfs_abort_transaction(trans, root, ret);
2677 btrfs_end_transaction(trans, root);
2678 goto out;
2679 }
2680 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002681 }
Chris Masond3977122009-01-05 21:25:51 -05002682next:
David Sterbab3b4aa72011-04-21 01:20:15 +02002683 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002684 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002685 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002686 ret = 0;
2687out:
David Sterbab3b4aa72011-04-21 01:20:15 +02002688 btrfs_release_path(path);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002689 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002690out_unlock:
2691 mutex_unlock(&src->i_mutex);
2692 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002693 vfree(buf);
2694 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002695out_fput:
2696 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05002697out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002698 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002699 return ret;
2700}
2701
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002702static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05002703{
2704 struct btrfs_ioctl_clone_range_args args;
2705
Christoph Hellwig7a865e82008-12-02 09:52:24 -05002706 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05002707 return -EFAULT;
2708 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2709 args.src_length, args.dest_offset);
2710}
2711
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002712/*
2713 * there are many ways the trans_start and trans_end ioctls can lead
2714 * to deadlocks. They should only be used by applications that
2715 * basically own the machine, and have a very in depth understanding
2716 * of all the possible deadlocks and enospc problems.
2717 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002718static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002719{
2720 struct inode *inode = fdentry(file)->d_inode;
2721 struct btrfs_root *root = BTRFS_I(inode)->root;
2722 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002723 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002724
Sage Weil1ab86ae2009-09-29 18:38:44 -04002725 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04002726 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002727 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002728
2729 ret = -EINPROGRESS;
2730 if (file->private_data)
2731 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002732
Li Zefanb83cc962010-12-20 16:04:08 +08002733 ret = -EROFS;
2734 if (btrfs_root_readonly(root))
2735 goto out;
2736
Al Viroa561be72011-11-23 11:57:51 -05002737 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05002738 if (ret)
2739 goto out;
2740
Josef Bacika4abeea2011-04-11 17:25:13 -04002741 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002742
Sage Weil1ab86ae2009-09-29 18:38:44 -04002743 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002744 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00002745 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04002746 goto out_drop;
2747
2748 file->private_data = trans;
2749 return 0;
2750
2751out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04002752 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05002753 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002754out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002755 return ret;
2756}
2757
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002758static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2759{
2760 struct inode *inode = fdentry(file)->d_inode;
2761 struct btrfs_root *root = BTRFS_I(inode)->root;
2762 struct btrfs_root *new_root;
2763 struct btrfs_dir_item *di;
2764 struct btrfs_trans_handle *trans;
2765 struct btrfs_path *path;
2766 struct btrfs_key location;
2767 struct btrfs_disk_key disk_key;
2768 struct btrfs_super_block *disk_super;
2769 u64 features;
2770 u64 objectid = 0;
2771 u64 dir_id;
2772
2773 if (!capable(CAP_SYS_ADMIN))
2774 return -EPERM;
2775
2776 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2777 return -EFAULT;
2778
2779 if (!objectid)
2780 objectid = root->root_key.objectid;
2781
2782 location.objectid = objectid;
2783 location.type = BTRFS_ROOT_ITEM_KEY;
2784 location.offset = (u64)-1;
2785
2786 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2787 if (IS_ERR(new_root))
2788 return PTR_ERR(new_root);
2789
2790 if (btrfs_root_refs(&new_root->root_item) == 0)
2791 return -ENOENT;
2792
2793 path = btrfs_alloc_path();
2794 if (!path)
2795 return -ENOMEM;
2796 path->leave_spinning = 1;
2797
2798 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002799 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002800 btrfs_free_path(path);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002801 return PTR_ERR(trans);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002802 }
2803
David Sterba6c417612011-04-13 15:41:04 +02002804 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002805 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2806 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00002807 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002808 btrfs_free_path(path);
2809 btrfs_end_transaction(trans, root);
2810 printk(KERN_ERR "Umm, you don't have the default dir item, "
2811 "this isn't going to work\n");
2812 return -ENOENT;
2813 }
2814
2815 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2816 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2817 btrfs_mark_buffer_dirty(path->nodes[0]);
2818 btrfs_free_path(path);
2819
David Sterba6c417612011-04-13 15:41:04 +02002820 disk_super = root->fs_info->super_copy;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00002821 features = btrfs_super_incompat_flags(disk_super);
2822 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2823 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2824 btrfs_set_super_incompat_flags(disk_super, features);
2825 }
2826 btrfs_end_transaction(trans, root);
2827
2828 return 0;
2829}
2830
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002831static void get_block_group_info(struct list_head *groups_list,
2832 struct btrfs_ioctl_space_info *space)
2833{
2834 struct btrfs_block_group_cache *block_group;
2835
2836 space->total_bytes = 0;
2837 space->used_bytes = 0;
2838 space->flags = 0;
2839 list_for_each_entry(block_group, groups_list, list) {
2840 space->flags = block_group->flags;
2841 space->total_bytes += block_group->key.offset;
2842 space->used_bytes +=
2843 btrfs_block_group_used(&block_group->item);
2844 }
2845}
2846
Josef Bacik1406e432010-01-13 18:19:06 +00002847long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2848{
2849 struct btrfs_ioctl_space_args space_args;
2850 struct btrfs_ioctl_space_info space;
2851 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04002852 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00002853 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00002854 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002855 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2856 BTRFS_BLOCK_GROUP_SYSTEM,
2857 BTRFS_BLOCK_GROUP_METADATA,
2858 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2859 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04002860 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00002861 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002862 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002863 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00002864
2865 if (copy_from_user(&space_args,
2866 (struct btrfs_ioctl_space_args __user *)arg,
2867 sizeof(space_args)))
2868 return -EFAULT;
2869
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002870 for (i = 0; i < num_types; i++) {
2871 struct btrfs_space_info *tmp;
2872
2873 info = NULL;
2874 rcu_read_lock();
2875 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2876 list) {
2877 if (tmp->flags == types[i]) {
2878 info = tmp;
2879 break;
2880 }
2881 }
2882 rcu_read_unlock();
2883
2884 if (!info)
2885 continue;
2886
2887 down_read(&info->groups_sem);
2888 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2889 if (!list_empty(&info->block_groups[c]))
2890 slot_count++;
2891 }
2892 up_read(&info->groups_sem);
2893 }
Josef Bacik1406e432010-01-13 18:19:06 +00002894
Chris Mason7fde62b2010-03-16 15:40:10 -04002895 /* space_slots == 0 means they are asking for a count */
2896 if (space_args.space_slots == 0) {
2897 space_args.total_spaces = slot_count;
2898 goto out;
2899 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002900
Dan Rosenberg51788b12011-02-14 16:04:23 -05002901 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002902
Chris Mason7fde62b2010-03-16 15:40:10 -04002903 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002904
Chris Mason7fde62b2010-03-16 15:40:10 -04002905 /* we generally have at most 6 or so space infos, one for each raid
2906 * level. So, a whole page should be more than enough for everyone
2907 */
2908 if (alloc_size > PAGE_CACHE_SIZE)
2909 return -ENOMEM;
2910
2911 space_args.total_spaces = 0;
2912 dest = kmalloc(alloc_size, GFP_NOFS);
2913 if (!dest)
2914 return -ENOMEM;
2915 dest_orig = dest;
2916
2917 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002918 for (i = 0; i < num_types; i++) {
2919 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04002920
Dan Rosenberg51788b12011-02-14 16:04:23 -05002921 if (!slot_count)
2922 break;
2923
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002924 info = NULL;
2925 rcu_read_lock();
2926 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2927 list) {
2928 if (tmp->flags == types[i]) {
2929 info = tmp;
2930 break;
2931 }
2932 }
2933 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04002934
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002935 if (!info)
2936 continue;
2937 down_read(&info->groups_sem);
2938 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2939 if (!list_empty(&info->block_groups[c])) {
2940 get_block_group_info(&info->block_groups[c],
2941 &space);
2942 memcpy(dest, &space, sizeof(space));
2943 dest++;
2944 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05002945 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002946 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05002947 if (!slot_count)
2948 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04002949 }
2950 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00002951 }
Josef Bacik1406e432010-01-13 18:19:06 +00002952
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08002953 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04002954 (arg + sizeof(struct btrfs_ioctl_space_args));
2955
2956 if (copy_to_user(user_dest, dest_orig, alloc_size))
2957 ret = -EFAULT;
2958
2959 kfree(dest_orig);
2960out:
2961 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00002962 ret = -EFAULT;
2963
2964 return ret;
2965}
2966
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002967/*
2968 * there are many ways the trans_start and trans_end ioctls can lead
2969 * to deadlocks. They should only be used by applications that
2970 * basically own the machine, and have a very in depth understanding
2971 * of all the possible deadlocks and enospc problems.
2972 */
2973long btrfs_ioctl_trans_end(struct file *file)
2974{
2975 struct inode *inode = fdentry(file)->d_inode;
2976 struct btrfs_root *root = BTRFS_I(inode)->root;
2977 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002978
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002979 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04002980 if (!trans)
2981 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04002982 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04002983
Sage Weil1ab86ae2009-09-29 18:38:44 -04002984 btrfs_end_transaction(trans, root);
2985
Josef Bacika4abeea2011-04-11 17:25:13 -04002986 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04002987
Al Viro2a79f172011-12-09 08:06:57 -05002988 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04002989 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002990}
2991
Sage Weil46204592010-10-29 15:41:32 -04002992static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2993{
2994 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2995 struct btrfs_trans_handle *trans;
2996 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00002997 int ret;
Sage Weil46204592010-10-29 15:41:32 -04002998
2999 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003000 if (IS_ERR(trans))
3001 return PTR_ERR(trans);
Sage Weil46204592010-10-29 15:41:32 -04003002 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003003 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003004 if (ret) {
3005 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003006 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003007 }
Sage Weil46204592010-10-29 15:41:32 -04003008
3009 if (argp)
3010 if (copy_to_user(argp, &transid, sizeof(transid)))
3011 return -EFAULT;
3012 return 0;
3013}
3014
3015static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
3016{
3017 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
3018 u64 transid;
3019
3020 if (argp) {
3021 if (copy_from_user(&transid, argp, sizeof(transid)))
3022 return -EFAULT;
3023 } else {
3024 transid = 0; /* current trans */
3025 }
3026 return btrfs_wait_for_commit(root, transid);
3027}
3028
Jan Schmidt475f6382011-03-11 15:41:01 +01003029static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg)
3030{
3031 int ret;
3032 struct btrfs_ioctl_scrub_args *sa;
3033
3034 if (!capable(CAP_SYS_ADMIN))
3035 return -EPERM;
3036
3037 sa = memdup_user(arg, sizeof(*sa));
3038 if (IS_ERR(sa))
3039 return PTR_ERR(sa);
3040
3041 ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end,
Arne Jansen86287642011-03-23 16:34:19 +01003042 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY);
Jan Schmidt475f6382011-03-11 15:41:01 +01003043
3044 if (copy_to_user(arg, sa, sizeof(*sa)))
3045 ret = -EFAULT;
3046
3047 kfree(sa);
3048 return ret;
3049}
3050
3051static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3052{
3053 if (!capable(CAP_SYS_ADMIN))
3054 return -EPERM;
3055
3056 return btrfs_scrub_cancel(root);
3057}
3058
3059static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3060 void __user *arg)
3061{
3062 struct btrfs_ioctl_scrub_args *sa;
3063 int ret;
3064
3065 if (!capable(CAP_SYS_ADMIN))
3066 return -EPERM;
3067
3068 sa = memdup_user(arg, sizeof(*sa));
3069 if (IS_ERR(sa))
3070 return PTR_ERR(sa);
3071
3072 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3073
3074 if (copy_to_user(arg, sa, sizeof(*sa)))
3075 ret = -EFAULT;
3076
3077 kfree(sa);
3078 return ret;
3079}
3080
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003081static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
3082 void __user *arg, int reset_after_read)
3083{
3084 struct btrfs_ioctl_get_dev_stats *sa;
3085 int ret;
3086
3087 if (reset_after_read && !capable(CAP_SYS_ADMIN))
3088 return -EPERM;
3089
3090 sa = memdup_user(arg, sizeof(*sa));
3091 if (IS_ERR(sa))
3092 return PTR_ERR(sa);
3093
3094 ret = btrfs_get_dev_stats(root, sa, reset_after_read);
3095
3096 if (copy_to_user(arg, sa, sizeof(*sa)))
3097 ret = -EFAULT;
3098
3099 kfree(sa);
3100 return ret;
3101}
3102
Jan Schmidtd7728c92011-07-07 16:48:38 +02003103static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3104{
3105 int ret = 0;
3106 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003107 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003108 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003109 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003110 struct inode_fs_paths *ipath = NULL;
3111 struct btrfs_path *path;
3112
3113 if (!capable(CAP_SYS_ADMIN))
3114 return -EPERM;
3115
3116 path = btrfs_alloc_path();
3117 if (!path) {
3118 ret = -ENOMEM;
3119 goto out;
3120 }
3121
3122 ipa = memdup_user(arg, sizeof(*ipa));
3123 if (IS_ERR(ipa)) {
3124 ret = PTR_ERR(ipa);
3125 ipa = NULL;
3126 goto out;
3127 }
3128
3129 size = min_t(u32, ipa->size, 4096);
3130 ipath = init_ipath(size, root, path);
3131 if (IS_ERR(ipath)) {
3132 ret = PTR_ERR(ipath);
3133 ipath = NULL;
3134 goto out;
3135 }
3136
3137 ret = paths_from_inode(ipa->inum, ipath);
3138 if (ret < 0)
3139 goto out;
3140
3141 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003142 rel_ptr = ipath->fspath->val[i] -
3143 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003144 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003145 }
3146
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003147 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3148 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003149 if (ret) {
3150 ret = -EFAULT;
3151 goto out;
3152 }
3153
3154out:
3155 btrfs_free_path(path);
3156 free_ipath(ipath);
3157 kfree(ipa);
3158
3159 return ret;
3160}
3161
3162static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3163{
3164 struct btrfs_data_container *inodes = ctx;
3165 const size_t c = 3 * sizeof(u64);
3166
3167 if (inodes->bytes_left >= c) {
3168 inodes->bytes_left -= c;
3169 inodes->val[inodes->elem_cnt] = inum;
3170 inodes->val[inodes->elem_cnt + 1] = offset;
3171 inodes->val[inodes->elem_cnt + 2] = root;
3172 inodes->elem_cnt += 3;
3173 } else {
3174 inodes->bytes_missing += c - inodes->bytes_left;
3175 inodes->bytes_left = 0;
3176 inodes->elem_missed += 3;
3177 }
3178
3179 return 0;
3180}
3181
3182static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3183 void __user *arg)
3184{
3185 int ret = 0;
3186 int size;
Jan Schmidt4692cf52011-12-02 14:56:41 +01003187 u64 extent_item_pos;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003188 struct btrfs_ioctl_logical_ino_args *loi;
3189 struct btrfs_data_container *inodes = NULL;
3190 struct btrfs_path *path = NULL;
3191 struct btrfs_key key;
3192
3193 if (!capable(CAP_SYS_ADMIN))
3194 return -EPERM;
3195
3196 loi = memdup_user(arg, sizeof(*loi));
3197 if (IS_ERR(loi)) {
3198 ret = PTR_ERR(loi);
3199 loi = NULL;
3200 goto out;
3201 }
3202
3203 path = btrfs_alloc_path();
3204 if (!path) {
3205 ret = -ENOMEM;
3206 goto out;
3207 }
3208
3209 size = min_t(u32, loi->size, 4096);
3210 inodes = init_data_container(size);
3211 if (IS_ERR(inodes)) {
3212 ret = PTR_ERR(inodes);
3213 inodes = NULL;
3214 goto out;
3215 }
3216
3217 ret = extent_from_logical(root->fs_info, loi->logical, path, &key);
Jan Schmidt4692cf52011-12-02 14:56:41 +01003218 btrfs_release_path(path);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003219
3220 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK)
3221 ret = -ENOENT;
3222 if (ret < 0)
3223 goto out;
3224
Jan Schmidt4692cf52011-12-02 14:56:41 +01003225 extent_item_pos = loi->logical - key.objectid;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +01003226 ret = iterate_extent_inodes(root->fs_info, key.objectid,
3227 extent_item_pos, 0, build_ino_list,
Jan Schmidt4692cf52011-12-02 14:56:41 +01003228 inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003229
3230 if (ret < 0)
3231 goto out;
3232
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003233 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3234 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003235 if (ret)
3236 ret = -EFAULT;
3237
3238out:
3239 btrfs_free_path(path);
3240 kfree(inodes);
3241 kfree(loi);
3242
3243 return ret;
3244}
3245
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003246void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003247 struct btrfs_ioctl_balance_args *bargs)
3248{
3249 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3250
3251 bargs->flags = bctl->flags;
3252
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003253 if (atomic_read(&fs_info->balance_running))
3254 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3255 if (atomic_read(&fs_info->balance_pause_req))
3256 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003257 if (atomic_read(&fs_info->balance_cancel_req))
3258 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003259
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003260 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3261 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3262 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003263
3264 if (lock) {
3265 spin_lock(&fs_info->balance_lock);
3266 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3267 spin_unlock(&fs_info->balance_lock);
3268 } else {
3269 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3270 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003271}
3272
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003273static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003274{
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003275 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276 struct btrfs_fs_info *fs_info = root->fs_info;
3277 struct btrfs_ioctl_balance_args *bargs;
3278 struct btrfs_balance_control *bctl;
3279 int ret;
3280
3281 if (!capable(CAP_SYS_ADMIN))
3282 return -EPERM;
3283
3284 if (fs_info->sb->s_flags & MS_RDONLY)
3285 return -EROFS;
3286
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003287 ret = mnt_want_write(file->f_path.mnt);
3288 if (ret)
3289 return ret;
3290
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003291 mutex_lock(&fs_info->volume_mutex);
3292 mutex_lock(&fs_info->balance_mutex);
3293
3294 if (arg) {
3295 bargs = memdup_user(arg, sizeof(*bargs));
3296 if (IS_ERR(bargs)) {
3297 ret = PTR_ERR(bargs);
3298 goto out;
3299 }
Ilya Dryomovde322262012-01-16 22:04:49 +02003300
3301 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3302 if (!fs_info->balance_ctl) {
3303 ret = -ENOTCONN;
3304 goto out_bargs;
3305 }
3306
3307 bctl = fs_info->balance_ctl;
3308 spin_lock(&fs_info->balance_lock);
3309 bctl->flags |= BTRFS_BALANCE_RESUME;
3310 spin_unlock(&fs_info->balance_lock);
3311
3312 goto do_balance;
3313 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003314 } else {
3315 bargs = NULL;
3316 }
3317
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003318 if (fs_info->balance_ctl) {
3319 ret = -EINPROGRESS;
3320 goto out_bargs;
3321 }
3322
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003323 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3324 if (!bctl) {
3325 ret = -ENOMEM;
3326 goto out_bargs;
3327 }
3328
3329 bctl->fs_info = fs_info;
3330 if (arg) {
3331 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3332 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3333 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3334
3335 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003336 } else {
3337 /* balance everything - no filters */
3338 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003339 }
3340
Ilya Dryomovde322262012-01-16 22:04:49 +02003341do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003342 ret = btrfs_balance(bctl, bargs);
3343 /*
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003344 * bctl is freed in __cancel_balance or in free_fs_info if
3345 * restriper was paused all the way until unmount
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003346 */
3347 if (arg) {
3348 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3349 ret = -EFAULT;
3350 }
3351
3352out_bargs:
3353 kfree(bargs);
3354out:
3355 mutex_unlock(&fs_info->balance_mutex);
3356 mutex_unlock(&fs_info->volume_mutex);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003357 mnt_drop_write(file->f_path.mnt);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003358 return ret;
3359}
3360
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003361static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
3362{
3363 if (!capable(CAP_SYS_ADMIN))
3364 return -EPERM;
3365
3366 switch (cmd) {
3367 case BTRFS_BALANCE_CTL_PAUSE:
3368 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003369 case BTRFS_BALANCE_CTL_CANCEL:
3370 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003371 }
3372
3373 return -EINVAL;
3374}
3375
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003376static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
3377 void __user *arg)
3378{
3379 struct btrfs_fs_info *fs_info = root->fs_info;
3380 struct btrfs_ioctl_balance_args *bargs;
3381 int ret = 0;
3382
3383 if (!capable(CAP_SYS_ADMIN))
3384 return -EPERM;
3385
3386 mutex_lock(&fs_info->balance_mutex);
3387 if (!fs_info->balance_ctl) {
3388 ret = -ENOTCONN;
3389 goto out;
3390 }
3391
3392 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
3393 if (!bargs) {
3394 ret = -ENOMEM;
3395 goto out;
3396 }
3397
3398 update_ioctl_balance_args(fs_info, 1, bargs);
3399
3400 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3401 ret = -EFAULT;
3402
3403 kfree(bargs);
3404out:
3405 mutex_unlock(&fs_info->balance_mutex);
3406 return ret;
3407}
3408
Alexander Block8ea05e32012-07-25 17:35:53 +02003409static long btrfs_ioctl_set_received_subvol(struct file *file,
3410 void __user *arg)
3411{
3412 struct btrfs_ioctl_received_subvol_args *sa = NULL;
3413 struct inode *inode = fdentry(file)->d_inode;
3414 struct btrfs_root *root = BTRFS_I(inode)->root;
3415 struct btrfs_root_item *root_item = &root->root_item;
3416 struct btrfs_trans_handle *trans;
3417 struct timespec ct = CURRENT_TIME;
3418 int ret = 0;
3419
3420 ret = mnt_want_write_file(file);
3421 if (ret < 0)
3422 return ret;
3423
3424 down_write(&root->fs_info->subvol_sem);
3425
3426 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
3427 ret = -EINVAL;
3428 goto out;
3429 }
3430
3431 if (btrfs_root_readonly(root)) {
3432 ret = -EROFS;
3433 goto out;
3434 }
3435
3436 if (!inode_owner_or_capable(inode)) {
3437 ret = -EACCES;
3438 goto out;
3439 }
3440
3441 sa = memdup_user(arg, sizeof(*sa));
3442 if (IS_ERR(sa)) {
3443 ret = PTR_ERR(sa);
3444 sa = NULL;
3445 goto out;
3446 }
3447
3448 trans = btrfs_start_transaction(root, 1);
3449 if (IS_ERR(trans)) {
3450 ret = PTR_ERR(trans);
3451 trans = NULL;
3452 goto out;
3453 }
3454
3455 sa->rtransid = trans->transid;
3456 sa->rtime.sec = ct.tv_sec;
3457 sa->rtime.nsec = ct.tv_nsec;
3458
3459 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
3460 btrfs_set_root_stransid(root_item, sa->stransid);
3461 btrfs_set_root_rtransid(root_item, sa->rtransid);
3462 root_item->stime.sec = cpu_to_le64(sa->stime.sec);
3463 root_item->stime.nsec = cpu_to_le32(sa->stime.nsec);
3464 root_item->rtime.sec = cpu_to_le64(sa->rtime.sec);
3465 root_item->rtime.nsec = cpu_to_le32(sa->rtime.nsec);
3466
3467 ret = btrfs_update_root(trans, root->fs_info->tree_root,
3468 &root->root_key, &root->root_item);
3469 if (ret < 0) {
3470 btrfs_end_transaction(trans, root);
3471 trans = NULL;
3472 goto out;
3473 } else {
3474 ret = btrfs_commit_transaction(trans, root);
3475 if (ret < 0)
3476 goto out;
3477 }
3478
3479 ret = copy_to_user(arg, sa, sizeof(*sa));
3480 if (ret)
3481 ret = -EFAULT;
3482
3483out:
3484 kfree(sa);
3485 up_write(&root->fs_info->subvol_sem);
3486 mnt_drop_write_file(file);
3487 return ret;
3488}
3489
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003490long btrfs_ioctl(struct file *file, unsigned int
3491 cmd, unsigned long arg)
3492{
3493 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003494 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003495
3496 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003497 case FS_IOC_GETFLAGS:
3498 return btrfs_ioctl_getflags(file, argp);
3499 case FS_IOC_SETFLAGS:
3500 return btrfs_ioctl_setflags(file, argp);
3501 case FS_IOC_GETVERSION:
3502 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00003503 case FITRIM:
3504 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003505 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003506 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00003507 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003508 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05003509 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08003510 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04003511 case BTRFS_IOC_SNAP_DESTROY:
3512 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08003513 case BTRFS_IOC_SUBVOL_GETFLAGS:
3514 return btrfs_ioctl_subvol_getflags(file, argp);
3515 case BTRFS_IOC_SUBVOL_SETFLAGS:
3516 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003517 case BTRFS_IOC_DEFAULT_SUBVOL:
3518 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003519 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05003520 return btrfs_ioctl_defrag(file, NULL);
3521 case BTRFS_IOC_DEFRAG_RANGE:
3522 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003523 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003524 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003525 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003526 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003527 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05003528 return btrfs_ioctl_rm_dev(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003529 case BTRFS_IOC_FS_INFO:
3530 return btrfs_ioctl_fs_info(root, argp);
3531 case BTRFS_IOC_DEV_INFO:
3532 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003533 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003534 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003535 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05003536 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
3537 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003538 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003539 case BTRFS_IOC_TRANS_START:
3540 return btrfs_ioctl_trans_start(file);
3541 case BTRFS_IOC_TRANS_END:
3542 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05003543 case BTRFS_IOC_TREE_SEARCH:
3544 return btrfs_ioctl_tree_search(file, argp);
3545 case BTRFS_IOC_INO_LOOKUP:
3546 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003547 case BTRFS_IOC_INO_PATHS:
3548 return btrfs_ioctl_ino_to_path(root, argp);
3549 case BTRFS_IOC_LOGICAL_INO:
3550 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00003551 case BTRFS_IOC_SPACE_INFO:
3552 return btrfs_ioctl_space_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003553 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003554 btrfs_sync_fs(file->f_dentry->d_sb, 1);
3555 return 0;
Sage Weil46204592010-10-29 15:41:32 -04003556 case BTRFS_IOC_START_SYNC:
3557 return btrfs_ioctl_start_sync(file, argp);
3558 case BTRFS_IOC_WAIT_SYNC:
3559 return btrfs_ioctl_wait_sync(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01003560 case BTRFS_IOC_SCRUB:
3561 return btrfs_ioctl_scrub(root, argp);
3562 case BTRFS_IOC_SCRUB_CANCEL:
3563 return btrfs_ioctl_scrub_cancel(root, argp);
3564 case BTRFS_IOC_SCRUB_PROGRESS:
3565 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003566 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003567 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003568 case BTRFS_IOC_BALANCE_CTL:
3569 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003570 case BTRFS_IOC_BALANCE_PROGRESS:
3571 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02003572 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
3573 return btrfs_ioctl_set_received_subvol(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003574 case BTRFS_IOC_GET_DEV_STATS:
3575 return btrfs_ioctl_get_dev_stats(root, argp, 0);
3576 case BTRFS_IOC_GET_AND_RESET_DEV_STATS:
3577 return btrfs_ioctl_get_dev_stats(root, argp, 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003578 }
3579
3580 return -ENOTTY;
3581}