blob: 1ae45bd9d27d51d15a511b85ad5a520e54c1dd65 [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>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000045#include <linux/btrfs.h>
Mark Fasheh416161d2013-08-06 11:42:51 -070046#include <linux/uaccess.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040051#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"
Alexander Block31db9f72012-07-25 23:19:24 +020057#include "send.h"
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +010058#include "dev-replace.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000059#include "props.h"
Jeff Mahoney3b02a682013-11-01 13:07:02 -040060#include "sysfs.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040061
Hugo Millsabccd002014-01-30 20:17:00 +000062#ifdef CONFIG_64BIT
63/* If we have a 32-bit userspace and 64-bit kernel, then the UAPI
64 * structures are incorrect, as the timespec structure from userspace
65 * is 4 bytes too small. We define these alternatives here to teach
66 * the kernel about the 32-bit struct packing.
67 */
68struct btrfs_ioctl_timespec_32 {
69 __u64 sec;
70 __u32 nsec;
71} __attribute__ ((__packed__));
72
73struct btrfs_ioctl_received_subvol_args_32 {
74 char uuid[BTRFS_UUID_SIZE]; /* in */
75 __u64 stransid; /* in */
76 __u64 rtransid; /* out */
77 struct btrfs_ioctl_timespec_32 stime; /* in */
78 struct btrfs_ioctl_timespec_32 rtime; /* out */
79 __u64 flags; /* in */
80 __u64 reserved[16]; /* in */
81} __attribute__ ((__packed__));
82
83#define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \
84 struct btrfs_ioctl_received_subvol_args_32)
85#endif
86
87
Mark Fasheh416161d2013-08-06 11:42:51 -070088static int btrfs_clone(struct inode *src, struct inode *inode,
89 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
90
Christoph Hellwig6cbff002009-04-17 10:37:41 +020091/* Mask out flags that are inappropriate for the given type of inode. */
92static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
93{
94 if (S_ISDIR(mode))
95 return flags;
96 else if (S_ISREG(mode))
97 return flags & ~FS_DIRSYNC_FL;
98 else
99 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
100}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400101
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200102/*
103 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
104 */
105static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
106{
107 unsigned int iflags = 0;
108
109 if (flags & BTRFS_INODE_SYNC)
110 iflags |= FS_SYNC_FL;
111 if (flags & BTRFS_INODE_IMMUTABLE)
112 iflags |= FS_IMMUTABLE_FL;
113 if (flags & BTRFS_INODE_APPEND)
114 iflags |= FS_APPEND_FL;
115 if (flags & BTRFS_INODE_NODUMP)
116 iflags |= FS_NODUMP_FL;
117 if (flags & BTRFS_INODE_NOATIME)
118 iflags |= FS_NOATIME_FL;
119 if (flags & BTRFS_INODE_DIRSYNC)
120 iflags |= FS_DIRSYNC_FL;
Li Zefand0092bd2011-04-15 03:03:06 +0000121 if (flags & BTRFS_INODE_NODATACOW)
122 iflags |= FS_NOCOW_FL;
123
124 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
125 iflags |= FS_COMPR_FL;
126 else if (flags & BTRFS_INODE_NOCOMPRESS)
127 iflags |= FS_NOCOMP_FL;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200128
129 return iflags;
130}
131
132/*
133 * Update inode->i_flags based on the btrfs internal flags.
134 */
135void btrfs_update_iflags(struct inode *inode)
136{
137 struct btrfs_inode *ip = BTRFS_I(inode);
138
139 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
140
141 if (ip->flags & BTRFS_INODE_SYNC)
142 inode->i_flags |= S_SYNC;
143 if (ip->flags & BTRFS_INODE_IMMUTABLE)
144 inode->i_flags |= S_IMMUTABLE;
145 if (ip->flags & BTRFS_INODE_APPEND)
146 inode->i_flags |= S_APPEND;
147 if (ip->flags & BTRFS_INODE_NOATIME)
148 inode->i_flags |= S_NOATIME;
149 if (ip->flags & BTRFS_INODE_DIRSYNC)
150 inode->i_flags |= S_DIRSYNC;
151}
152
153/*
154 * Inherit flags from the parent inode.
155 *
Josef Bacike27425d2011-09-27 11:01:30 -0400156 * Currently only the compression flags and the cow flags are inherited.
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200157 */
158void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
159{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400160 unsigned int flags;
161
162 if (!dir)
163 return;
164
165 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200166
Josef Bacike27425d2011-09-27 11:01:30 -0400167 if (flags & BTRFS_INODE_NOCOMPRESS) {
168 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
169 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
170 } else if (flags & BTRFS_INODE_COMPRESS) {
171 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
172 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
173 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200174
Liu Bo213490b2012-09-11 08:33:50 -0600175 if (flags & BTRFS_INODE_NODATACOW) {
Josef Bacike27425d2011-09-27 11:01:30 -0400176 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
Liu Bo213490b2012-09-11 08:33:50 -0600177 if (S_ISREG(inode->i_mode))
178 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
179 }
Josef Bacike27425d2011-09-27 11:01:30 -0400180
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200181 btrfs_update_iflags(inode);
182}
183
184static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
185{
Al Viro496ad9a2013-01-23 17:07:38 -0500186 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200187 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
188
189 if (copy_to_user(arg, &flags, sizeof(flags)))
190 return -EFAULT;
191 return 0;
192}
193
Liu Bo75e7cb72011-03-22 10:12:20 +0000194static int check_flags(unsigned int flags)
195{
196 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
197 FS_NOATIME_FL | FS_NODUMP_FL | \
198 FS_SYNC_FL | FS_DIRSYNC_FL | \
Li Zefane1e8fb62011-04-15 03:02:49 +0000199 FS_NOCOMP_FL | FS_COMPR_FL |
200 FS_NOCOW_FL))
Liu Bo75e7cb72011-03-22 10:12:20 +0000201 return -EOPNOTSUPP;
202
203 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
204 return -EINVAL;
205
Liu Bo75e7cb72011-03-22 10:12:20 +0000206 return 0;
207}
208
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200209static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
210{
Al Viro496ad9a2013-01-23 17:07:38 -0500211 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200212 struct btrfs_inode *ip = BTRFS_I(inode);
213 struct btrfs_root *root = ip->root;
214 struct btrfs_trans_handle *trans;
215 unsigned int flags, oldflags;
216 int ret;
Li Zefanf062abf2011-12-29 13:36:45 +0800217 u64 ip_oldflags;
218 unsigned int i_oldflags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600219 umode_t mode;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200220
David Sterbabd60ea02014-01-16 15:50:22 +0100221 if (!inode_owner_or_capable(inode))
222 return -EPERM;
223
Li Zefanb83cc962010-12-20 16:04:08 +0800224 if (btrfs_root_readonly(root))
225 return -EROFS;
226
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200227 if (copy_from_user(&flags, arg, sizeof(flags)))
228 return -EFAULT;
229
Liu Bo75e7cb72011-03-22 10:12:20 +0000230 ret = check_flags(flags);
231 if (ret)
232 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200233
Jan Karae7848682012-06-12 16:20:32 +0200234 ret = mnt_want_write_file(file);
235 if (ret)
236 return ret;
237
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200238 mutex_lock(&inode->i_mutex);
239
Li Zefanf062abf2011-12-29 13:36:45 +0800240 ip_oldflags = ip->flags;
241 i_oldflags = inode->i_flags;
David Sterba7e97b8d2012-09-07 05:56:55 -0600242 mode = inode->i_mode;
Li Zefanf062abf2011-12-29 13:36:45 +0800243
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200244 flags = btrfs_mask_flags(inode->i_mode, flags);
245 oldflags = btrfs_flags_to_ioctl(ip->flags);
246 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
247 if (!capable(CAP_LINUX_IMMUTABLE)) {
248 ret = -EPERM;
249 goto out_unlock;
250 }
251 }
252
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200253 if (flags & FS_SYNC_FL)
254 ip->flags |= BTRFS_INODE_SYNC;
255 else
256 ip->flags &= ~BTRFS_INODE_SYNC;
257 if (flags & FS_IMMUTABLE_FL)
258 ip->flags |= BTRFS_INODE_IMMUTABLE;
259 else
260 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
261 if (flags & FS_APPEND_FL)
262 ip->flags |= BTRFS_INODE_APPEND;
263 else
264 ip->flags &= ~BTRFS_INODE_APPEND;
265 if (flags & FS_NODUMP_FL)
266 ip->flags |= BTRFS_INODE_NODUMP;
267 else
268 ip->flags &= ~BTRFS_INODE_NODUMP;
269 if (flags & FS_NOATIME_FL)
270 ip->flags |= BTRFS_INODE_NOATIME;
271 else
272 ip->flags &= ~BTRFS_INODE_NOATIME;
273 if (flags & FS_DIRSYNC_FL)
274 ip->flags |= BTRFS_INODE_DIRSYNC;
275 else
276 ip->flags &= ~BTRFS_INODE_DIRSYNC;
David Sterba7e97b8d2012-09-07 05:56:55 -0600277 if (flags & FS_NOCOW_FL) {
278 if (S_ISREG(mode)) {
279 /*
280 * It's safe to turn csums off here, no extents exist.
281 * Otherwise we want the flag to reflect the real COW
282 * status of the file and will not set it.
283 */
284 if (inode->i_size == 0)
285 ip->flags |= BTRFS_INODE_NODATACOW
286 | BTRFS_INODE_NODATASUM;
287 } else {
288 ip->flags |= BTRFS_INODE_NODATACOW;
289 }
290 } else {
291 /*
292 * Revert back under same assuptions as above
293 */
294 if (S_ISREG(mode)) {
295 if (inode->i_size == 0)
296 ip->flags &= ~(BTRFS_INODE_NODATACOW
297 | BTRFS_INODE_NODATASUM);
298 } else {
299 ip->flags &= ~BTRFS_INODE_NODATACOW;
300 }
301 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200302
Liu Bo75e7cb72011-03-22 10:12:20 +0000303 /*
304 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
305 * flag may be changed automatically if compression code won't make
306 * things smaller.
307 */
308 if (flags & FS_NOCOMP_FL) {
309 ip->flags &= ~BTRFS_INODE_COMPRESS;
310 ip->flags |= BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000311
312 ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
313 if (ret && ret != -ENODATA)
314 goto out_drop;
Liu Bo75e7cb72011-03-22 10:12:20 +0000315 } else if (flags & FS_COMPR_FL) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000316 const char *comp;
317
Liu Bo75e7cb72011-03-22 10:12:20 +0000318 ip->flags |= BTRFS_INODE_COMPRESS;
319 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000320
321 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
322 comp = "lzo";
323 else
324 comp = "zlib";
325 ret = btrfs_set_prop(inode, "btrfs.compression",
326 comp, strlen(comp), 0);
327 if (ret)
328 goto out_drop;
329
Li Zefanebcb9042011-04-15 03:03:17 +0000330 } else {
331 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
Liu Bo75e7cb72011-03-22 10:12:20 +0000332 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200333
Li Zefan4da6f1a2011-12-29 13:39:50 +0800334 trans = btrfs_start_transaction(root, 1);
Li Zefanf062abf2011-12-29 13:36:45 +0800335 if (IS_ERR(trans)) {
336 ret = PTR_ERR(trans);
337 goto out_drop;
338 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200339
Li Zefan306424cc2011-12-14 20:12:02 -0500340 btrfs_update_iflags(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -0400341 inode_inc_iversion(inode);
Li Zefan306424cc2011-12-14 20:12:02 -0500342 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200343 ret = btrfs_update_inode(trans, root, inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200344
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200345 btrfs_end_transaction(trans, root);
Li Zefanf062abf2011-12-29 13:36:45 +0800346 out_drop:
347 if (ret) {
348 ip->flags = ip_oldflags;
349 inode->i_flags = i_oldflags;
350 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200351
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200352 out_unlock:
353 mutex_unlock(&inode->i_mutex);
Jan Karae7848682012-06-12 16:20:32 +0200354 mnt_drop_write_file(file);
liubo2d4e6f62011-02-24 09:38:16 +0000355 return ret;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200356}
357
358static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
359{
Al Viro496ad9a2013-01-23 17:07:38 -0500360 struct inode *inode = file_inode(file);
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200361
362 return put_user(inode->i_generation, arg);
363}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400364
Li Dongyangf7039b12011-03-24 10:24:28 +0000365static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
366{
Al Viro54563d42013-09-01 15:57:51 -0400367 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
Li Dongyangf7039b12011-03-24 10:24:28 +0000368 struct btrfs_device *device;
369 struct request_queue *q;
370 struct fstrim_range range;
371 u64 minlen = ULLONG_MAX;
372 u64 num_devices = 0;
Al Viro815745c2011-11-17 15:40:49 -0500373 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +0000374 int ret;
375
376 if (!capable(CAP_SYS_ADMIN))
377 return -EPERM;
378
Xiao Guangrong1f781602011-04-20 10:09:16 +0000379 rcu_read_lock();
380 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
381 dev_list) {
Li Dongyangf7039b12011-03-24 10:24:28 +0000382 if (!device->bdev)
383 continue;
384 q = bdev_get_queue(device->bdev);
385 if (blk_queue_discard(q)) {
386 num_devices++;
387 minlen = min((u64)q->limits.discard_granularity,
388 minlen);
389 }
390 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000391 rcu_read_unlock();
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200392
Li Dongyangf7039b12011-03-24 10:24:28 +0000393 if (!num_devices)
394 return -EOPNOTSUPP;
Li Dongyangf7039b12011-03-24 10:24:28 +0000395 if (copy_from_user(&range, arg, sizeof(range)))
396 return -EFAULT;
Lukas Czernere515c182012-10-16 09:34:36 +0000397 if (range.start > total_bytes ||
398 range.len < fs_info->sb->s_blocksize)
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200399 return -EINVAL;
Li Dongyangf7039b12011-03-24 10:24:28 +0000400
Lukas Czernerf4c697e2011-09-05 16:34:54 +0200401 range.len = min(range.len, total_bytes - range.start);
Li Dongyangf7039b12011-03-24 10:24:28 +0000402 range.minlen = max(range.minlen, minlen);
Al Viro815745c2011-11-17 15:40:49 -0500403 ret = btrfs_trim_fs(fs_info->tree_root, &range);
Li Dongyangf7039b12011-03-24 10:24:28 +0000404 if (ret < 0)
405 return ret;
406
407 if (copy_to_user(arg, &range, sizeof(range)))
408 return -EFAULT;
409
410 return 0;
411}
412
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200413int btrfs_is_empty_uuid(u8 *uuid)
414{
Chris Mason46e0f662013-11-15 12:14:55 +0100415 int i;
416
417 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
418 if (uuid[i])
419 return 0;
420 }
421 return 1;
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200422}
423
Miao Xied5c12072013-02-28 10:04:33 +0000424static noinline int create_subvol(struct inode *dir,
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400425 struct dentry *dentry,
Sage Weil72fd0322010-10-29 15:41:32 -0400426 char *name, int namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200427 u64 *async_transid,
Miao Xie8696c532013-02-07 06:02:44 +0000428 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400429{
430 struct btrfs_trans_handle *trans;
431 struct btrfs_key key;
432 struct btrfs_root_item root_item;
433 struct btrfs_inode_item *inode_item;
434 struct extent_buffer *leaf;
Miao Xied5c12072013-02-28 10:04:33 +0000435 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -0400436 struct btrfs_root *new_root;
Miao Xied5c12072013-02-28 10:04:33 +0000437 struct btrfs_block_rsv block_rsv;
Alexander Block8ea05e32012-07-25 17:35:53 +0200438 struct timespec cur_time = CURRENT_TIME;
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900439 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400440 int ret;
441 int err;
442 u64 objectid;
443 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500444 u64 index = 0;
Miao Xied5c12072013-02-28 10:04:33 +0000445 u64 qgroup_reserved;
Alexander Block8ea05e32012-07-25 17:35:53 +0200446 uuid_le new_uuid;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400447
Li Zefan581bb052011-04-20 10:06:11 +0800448 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
Al Viro2fbe8c82011-07-16 21:38:06 -0400449 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400450 return ret;
Josef Bacik6a912212010-11-20 09:48:00 +0000451
Miao Xied5c12072013-02-28 10:04:33 +0000452 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400453 /*
Miao Xied5c12072013-02-28 10:04:33 +0000454 * The same as the snapshot creation, please see the comment
455 * of create_snapshot().
Josef Bacik9ed74f22009-09-11 16:12:44 -0400456 */
Miao Xied5c12072013-02-28 10:04:33 +0000457 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200458 8, &qgroup_reserved, false);
Miao Xied5c12072013-02-28 10:04:33 +0000459 if (ret)
460 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400461
Miao Xied5c12072013-02-28 10:04:33 +0000462 trans = btrfs_start_transaction(root, 0);
463 if (IS_ERR(trans)) {
464 ret = PTR_ERR(trans);
Liu Bode6e8202014-01-09 14:57:06 +0800465 btrfs_subvolume_release_metadata(root, &block_rsv,
466 qgroup_reserved);
467 return ret;
Miao Xied5c12072013-02-28 10:04:33 +0000468 }
469 trans->block_rsv = &block_rsv;
470 trans->bytes_reserved = block_rsv.size;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400471
Miao Xie8696c532013-02-07 06:02:44 +0000472 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200473 if (ret)
474 goto fail;
475
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400476 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
Jan Schmidt5581a512012-05-16 17:04:52 +0200477 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400478 if (IS_ERR(leaf)) {
479 ret = PTR_ERR(leaf);
480 goto fail;
481 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400482
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400483 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484 btrfs_set_header_bytenr(leaf, leaf->start);
485 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400486 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400487 btrfs_set_header_owner(leaf, objectid);
488
Ross Kirk0a4e5582013-09-24 10:12:38 +0100489 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400490 BTRFS_FSID_SIZE);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400491 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +0200492 btrfs_header_chunk_tree_uuid(leaf),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400493 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400494 btrfs_mark_buffer_dirty(leaf);
495
Alexander Block8ea05e32012-07-25 17:35:53 +0200496 memset(&root_item, 0, sizeof(root_item));
497
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400498 inode_item = &root_item.inode;
Qu Wenruo3cae2102013-07-16 11:19:18 +0800499 btrfs_set_stack_inode_generation(inode_item, 1);
500 btrfs_set_stack_inode_size(inode_item, 3);
501 btrfs_set_stack_inode_nlink(inode_item, 1);
502 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
503 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400504
Qu Wenruo3cae2102013-07-16 11:19:18 +0800505 btrfs_set_root_flags(&root_item, 0);
506 btrfs_set_root_limit(&root_item, 0);
507 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
Li Zefan08fe4db2011-03-28 02:01:25 +0000508
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400509 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400510 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400511 btrfs_set_root_level(&root_item, 0);
512 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000513 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400514 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400515
Alexander Block8ea05e32012-07-25 17:35:53 +0200516 btrfs_set_root_generation_v2(&root_item,
517 btrfs_root_generation(&root_item));
518 uuid_le_gen(&new_uuid);
519 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
Qu Wenruo3cae2102013-07-16 11:19:18 +0800520 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
521 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +0200522 root_item.ctime = root_item.otime;
523 btrfs_set_root_ctransid(&root_item, trans->transid);
524 btrfs_set_root_otransid(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400525
Chris Mason925baed2008-06-25 16:01:30 -0400526 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400527 free_extent_buffer(leaf);
528 leaf = NULL;
529
530 btrfs_set_root_dirid(&root_item, new_dirid);
531
532 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400533 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400534 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
535 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
536 &root_item);
537 if (ret)
538 goto fail;
539
Yan, Zheng76dda932009-09-21 16:00:26 -0400540 key.offset = (u64)-1;
541 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100542 if (IS_ERR(new_root)) {
543 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
544 ret = PTR_ERR(new_root);
545 goto fail;
546 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400547
548 btrfs_record_root_in_trans(trans, new_root);
549
Filipe David Borba Manana63541922014-01-07 11:47:46 +0000550 ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid);
Mark Fashehce598972011-07-26 11:32:23 -0700551 if (ret) {
552 /* We potentially lose an unused inode item here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100553 btrfs_abort_transaction(trans, root, ret);
Mark Fashehce598972011-07-26 11:32:23 -0700554 goto fail;
555 }
556
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557 /*
558 * insert the directory item
559 */
Chris Mason3de45862008-11-17 21:02:50 -0500560 ret = btrfs_set_inode_index(dir, &index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100561 if (ret) {
562 btrfs_abort_transaction(trans, root, ret);
563 goto fail;
564 }
Chris Mason3de45862008-11-17 21:02:50 -0500565
566 ret = btrfs_insert_dir_item(trans, root,
Miao Xie16cdcec2011-04-22 18:12:22 +0800567 name, namelen, dir, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500568 BTRFS_FT_DIR, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100569 if (ret) {
570 btrfs_abort_transaction(trans, root, ret);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400571 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100572 }
Chris Mason0660b5a2008-11-17 20:37:39 -0500573
Yan Zheng52c26172009-01-05 15:43:43 -0500574 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
575 ret = btrfs_update_inode(trans, root, dir);
576 BUG_ON(ret);
577
Chris Mason0660b5a2008-11-17 20:37:39 -0500578 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400579 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +0800580 btrfs_ino(dir), index, name, namelen);
Chris Mason0660b5a2008-11-17 20:37:39 -0500581 BUG_ON(ret);
582
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200583 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
584 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
585 objectid);
586 if (ret)
587 btrfs_abort_transaction(trans, root, ret);
588
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400589fail:
Miao Xied5c12072013-02-28 10:04:33 +0000590 trans->block_rsv = NULL;
591 trans->bytes_reserved = 0;
Liu Bode6e8202014-01-09 14:57:06 +0800592 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
593
Sage Weil72fd0322010-10-29 15:41:32 -0400594 if (async_transid) {
595 *async_transid = trans->transid;
596 err = btrfs_commit_transaction_async(trans, root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000597 if (err)
598 err = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400599 } else {
600 err = btrfs_commit_transaction(trans, root);
601 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400602 if (err && !ret)
603 ret = err;
Chris Mason1a65e242013-02-06 12:06:02 -0500604
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900605 if (!ret) {
606 inode = btrfs_lookup_dentry(dir, dentry);
Liu Bode6e8202014-01-09 14:57:06 +0800607 if (IS_ERR(inode))
608 return PTR_ERR(inode);
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900609 d_instantiate(dentry, inode);
610 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400611 return ret;
612}
613
Miao Xiee9662f72013-02-28 10:01:15 +0000614static int create_snapshot(struct btrfs_root *root, struct inode *dir,
615 struct dentry *dentry, char *name, int namelen,
616 u64 *async_transid, bool readonly,
617 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400618{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000619 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400620 struct btrfs_pending_snapshot *pending_snapshot;
621 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000622 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400623
624 if (!root->ref_cows)
625 return -EINVAL;
626
Miao Xie6a038432013-05-15 07:48:24 +0000627 ret = btrfs_start_delalloc_inodes(root, 0);
628 if (ret)
629 return ret;
630
Miao Xieb0244192013-11-04 23:13:25 +0800631 btrfs_wait_ordered_extents(root, -1);
Miao Xie6a038432013-05-15 07:48:24 +0000632
Yan, Zhenga22285a2010-05-16 10:48:46 -0400633 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
634 if (!pending_snapshot)
635 return -ENOMEM;
636
Miao Xie66d8f3d2012-09-06 04:02:28 -0600637 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
638 BTRFS_BLOCK_RSV_TEMP);
Miao Xied5c12072013-02-28 10:04:33 +0000639 /*
640 * 1 - parent dir inode
641 * 2 - dir entries
642 * 1 - root item
643 * 2 - root ref/backref
644 * 1 - root of snapshot
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200645 * 1 - UUID item
Miao Xied5c12072013-02-28 10:04:33 +0000646 */
647 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
Stefan Behrensdd5f9612013-08-15 17:11:20 +0200648 &pending_snapshot->block_rsv, 8,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -0400649 &pending_snapshot->qgroup_reserved,
650 false);
Miao Xied5c12072013-02-28 10:04:33 +0000651 if (ret)
652 goto out;
653
Yan, Zhenga22285a2010-05-16 10:48:46 -0400654 pending_snapshot->dentry = dentry;
655 pending_snapshot->root = root;
Li Zefanb83cc962010-12-20 16:04:08 +0800656 pending_snapshot->readonly = readonly;
Miao Xiee9662f72013-02-28 10:01:15 +0000657 pending_snapshot->dir = dir;
Miao Xie8696c532013-02-07 06:02:44 +0000658 pending_snapshot->inherit = inherit;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400659
Miao Xied5c12072013-02-28 10:04:33 +0000660 trans = btrfs_start_transaction(root, 0);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400661 if (IS_ERR(trans)) {
662 ret = PTR_ERR(trans);
663 goto fail;
664 }
665
Josef Bacik83515832011-06-14 15:16:14 -0400666 spin_lock(&root->fs_info->trans_lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400667 list_add(&pending_snapshot->list,
668 &trans->transaction->pending_snapshots);
Josef Bacik83515832011-06-14 15:16:14 -0400669 spin_unlock(&root->fs_info->trans_lock);
Sage Weil72fd0322010-10-29 15:41:32 -0400670 if (async_transid) {
671 *async_transid = trans->transid;
672 ret = btrfs_commit_transaction_async(trans,
673 root->fs_info->extent_root, 1);
Miao Xie00d71c92013-03-04 09:45:06 +0000674 if (ret)
675 ret = btrfs_commit_transaction(trans, root);
Sage Weil72fd0322010-10-29 15:41:32 -0400676 } else {
677 ret = btrfs_commit_transaction(trans,
678 root->fs_info->extent_root);
679 }
Miao Xieaec80302013-03-04 09:44:29 +0000680 if (ret)
Josef Bacikc37b2b62012-10-22 15:51:44 -0400681 goto fail;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400682
683 ret = pending_snapshot->error;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400684 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000685 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400686
Josef Bacik66b4ffd2011-01-31 16:22:42 -0500687 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
688 if (ret)
689 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400690
Al Viro2fbe8c82011-07-16 21:38:06 -0400691 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000692 if (IS_ERR(inode)) {
693 ret = PTR_ERR(inode);
694 goto fail;
695 }
Tsutomu Itoh5662344b32013-12-13 09:51:42 +0900696
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000697 d_instantiate(dentry, inode);
698 ret = 0;
699fail:
Miao Xied5c12072013-02-28 10:04:33 +0000700 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
701 &pending_snapshot->block_rsv,
702 pending_snapshot->qgroup_reserved);
703out:
Yan, Zhenga22285a2010-05-16 10:48:46 -0400704 kfree(pending_snapshot);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400705 return ret;
706}
707
Sage Weil4260f7c2010-10-29 15:46:43 -0400708/* copy of check_sticky in fs/namei.c()
709* It's inline, so penalty for filesystems that don't use sticky bit is
710* minimal.
711*/
712static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
713{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800714 kuid_t fsuid = current_fsuid();
Sage Weil4260f7c2010-10-29 15:46:43 -0400715
716 if (!(dir->i_mode & S_ISVTX))
717 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800718 if (uid_eq(inode->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400719 return 0;
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -0800720 if (uid_eq(dir->i_uid, fsuid))
Sage Weil4260f7c2010-10-29 15:46:43 -0400721 return 0;
722 return !capable(CAP_FOWNER);
723}
724
725/* copy of may_delete in fs/namei.c()
726 * Check whether we can remove a link victim from directory dir, check
727 * whether the type of victim is right.
728 * 1. We can't do it if dir is read-only (done in permission())
729 * 2. We should have write and exec permissions on dir
730 * 3. We can't remove anything from append-only dir
731 * 4. We can't do anything with immutable dir (done in permission())
732 * 5. If the sticky bit on dir is set we should either
733 * a. be owner of dir, or
734 * b. be owner of victim, or
735 * c. have CAP_FOWNER capability
736 * 6. If the victim is append-only or immutable we can't do antyhing with
737 * links pointing to it.
738 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
739 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
740 * 9. We can't remove a root or mountpoint.
741 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
742 * nfs_async_unlink().
743 */
744
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530745static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
Sage Weil4260f7c2010-10-29 15:46:43 -0400746{
747 int error;
748
749 if (!victim->d_inode)
750 return -ENOENT;
751
752 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400753 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Sage Weil4260f7c2010-10-29 15:46:43 -0400754
755 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
756 if (error)
757 return error;
758 if (IS_APPEND(dir))
759 return -EPERM;
760 if (btrfs_check_sticky(dir, victim->d_inode)||
761 IS_APPEND(victim->d_inode)||
762 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
763 return -EPERM;
764 if (isdir) {
765 if (!S_ISDIR(victim->d_inode->i_mode))
766 return -ENOTDIR;
767 if (IS_ROOT(victim))
768 return -EBUSY;
769 } else if (S_ISDIR(victim->d_inode->i_mode))
770 return -EISDIR;
771 if (IS_DEADDIR(dir))
772 return -ENOENT;
773 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
774 return -EBUSY;
775 return 0;
776}
777
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400778/* copy of may_create in fs/namei.c() */
779static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
780{
781 if (child->d_inode)
782 return -EEXIST;
783 if (IS_DEADDIR(dir))
784 return -ENOENT;
785 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
786}
787
788/*
789 * Create a new subvolume below @parent. This is largely modeled after
790 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
791 * inside this filesystem so it's quite a bit simpler.
792 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400793static noinline int btrfs_mksubvol(struct path *parent,
794 char *name, int namelen,
Sage Weil72fd0322010-10-29 15:41:32 -0400795 struct btrfs_root *snap_src,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200796 u64 *async_transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +0000797 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400798{
Yan, Zheng76dda932009-09-21 16:00:26 -0400799 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400800 struct dentry *dentry;
801 int error;
802
David Sterba5c50c9b2013-03-22 18:12:51 +0000803 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
804 if (error == -EINTR)
805 return error;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400806
807 dentry = lookup_one_len(name, parent->dentry, namelen);
808 error = PTR_ERR(dentry);
809 if (IS_ERR(dentry))
810 goto out_unlock;
811
812 error = -EEXIST;
813 if (dentry->d_inode)
814 goto out_dput;
815
Yan, Zheng76dda932009-09-21 16:00:26 -0400816 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400817 if (error)
Liu Boa874a632012-06-29 03:58:46 -0600818 goto out_dput;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400819
Chris Mason9c520572012-12-17 14:26:57 -0500820 /*
821 * even if this name doesn't exist, we may get hash collisions.
822 * check for them now when we can safely fail
823 */
824 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
825 dir->i_ino, name,
826 namelen);
827 if (error)
828 goto out_dput;
829
Yan, Zheng76dda932009-09-21 16:00:26 -0400830 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
831
832 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
833 goto out_up_read;
834
Chris Mason3de45862008-11-17 21:02:50 -0500835 if (snap_src) {
Miao Xiee9662f72013-02-28 10:01:15 +0000836 error = create_snapshot(snap_src, dir, dentry, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +0200837 async_transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500838 } else {
Miao Xied5c12072013-02-28 10:04:33 +0000839 error = create_subvol(dir, dentry, name, namelen,
840 async_transid, inherit);
Chris Mason3de45862008-11-17 21:02:50 -0500841 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400842 if (!error)
843 fsnotify_mkdir(dir, dentry);
844out_up_read:
845 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400846out_dput:
847 dput(dentry);
848out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400849 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400850 return error;
851}
852
Chris Mason4cb53002011-05-24 15:35:30 -0400853/*
854 * When we're defragging a range, we don't want to kick it off again
855 * if it is really just waiting for delalloc to send it down.
856 * If we find a nice big extent or delalloc range for the bytes in the
857 * file you want to defrag, we return 0 to let you know to skip this
858 * part of the file
859 */
860static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
861{
862 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
863 struct extent_map *em = NULL;
864 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
865 u64 end;
866
867 read_lock(&em_tree->lock);
868 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
869 read_unlock(&em_tree->lock);
870
871 if (em) {
872 end = extent_map_end(em);
873 free_extent_map(em);
874 if (end - offset > thresh)
875 return 0;
876 }
877 /* if we already have a nice delalloc here, just stop */
878 thresh /= 2;
879 end = count_range_bits(io_tree, &offset, offset + thresh,
880 thresh, EXTENT_DELALLOC, 1);
881 if (end >= thresh)
882 return 0;
883 return 1;
884}
885
886/*
887 * helper function to walk through a file and find extents
888 * newer than a specific transid, and smaller than thresh.
889 *
890 * This is used by the defragging code to find new and small
891 * extents
892 */
893static int find_new_extents(struct btrfs_root *root,
894 struct inode *inode, u64 newer_than,
895 u64 *off, int thresh)
896{
897 struct btrfs_path *path;
898 struct btrfs_key min_key;
Chris Mason4cb53002011-05-24 15:35:30 -0400899 struct extent_buffer *leaf;
900 struct btrfs_file_extent_item *extent;
901 int type;
902 int ret;
David Sterbaa4689d22011-05-31 17:08:14 +0000903 u64 ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400904
905 path = btrfs_alloc_path();
906 if (!path)
907 return -ENOMEM;
908
David Sterbaa4689d22011-05-31 17:08:14 +0000909 min_key.objectid = ino;
Chris Mason4cb53002011-05-24 15:35:30 -0400910 min_key.type = BTRFS_EXTENT_DATA_KEY;
911 min_key.offset = *off;
912
Chris Mason4cb53002011-05-24 15:35:30 -0400913 path->keep_locks = 1;
914
Dulshani Gunawardhana67871252013-10-31 10:33:04 +0530915 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +0100916 ret = btrfs_search_forward(root, &min_key, path, newer_than);
Chris Mason4cb53002011-05-24 15:35:30 -0400917 if (ret != 0)
918 goto none;
David Sterbaa4689d22011-05-31 17:08:14 +0000919 if (min_key.objectid != ino)
Chris Mason4cb53002011-05-24 15:35:30 -0400920 goto none;
921 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
922 goto none;
923
924 leaf = path->nodes[0];
925 extent = btrfs_item_ptr(leaf, path->slots[0],
926 struct btrfs_file_extent_item);
927
928 type = btrfs_file_extent_type(leaf, extent);
929 if (type == BTRFS_FILE_EXTENT_REG &&
930 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
931 check_defrag_in_cache(inode, min_key.offset, thresh)) {
932 *off = min_key.offset;
933 btrfs_free_path(path);
934 return 0;
935 }
936
937 if (min_key.offset == (u64)-1)
938 goto none;
939
940 min_key.offset++;
941 btrfs_release_path(path);
942 }
943none:
944 btrfs_free_path(path);
945 return -ENOENT;
946}
947
Li Zefan6c282eb2012-06-11 16:03:35 +0800948static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
Liu Bo17ce6ef2012-03-29 09:57:45 -0400949{
950 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason940100a2010-03-10 10:52:59 -0500951 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Li Zefan6c282eb2012-06-11 16:03:35 +0800952 struct extent_map *em;
953 u64 len = PAGE_CACHE_SIZE;
Chris Mason940100a2010-03-10 10:52:59 -0500954
955 /*
956 * hopefully we have this extent in the tree already, try without
957 * the full extent lock
958 */
959 read_lock(&em_tree->lock);
960 em = lookup_extent_mapping(em_tree, start, len);
961 read_unlock(&em_tree->lock);
962
963 if (!em) {
964 /* get the big lock and read metadata off disk */
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100965 lock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500966 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100967 unlock_extent(io_tree, start, start + len - 1);
Chris Mason940100a2010-03-10 10:52:59 -0500968
Dan Carpenter6cf8bfb2010-03-20 11:22:10 +0000969 if (IS_ERR(em))
Li Zefan6c282eb2012-06-11 16:03:35 +0800970 return NULL;
Chris Mason940100a2010-03-10 10:52:59 -0500971 }
972
Li Zefan6c282eb2012-06-11 16:03:35 +0800973 return em;
974}
975
976static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
977{
978 struct extent_map *next;
979 bool ret = true;
980
981 /* this is the last extent */
982 if (em->start + em->len >= i_size_read(inode))
983 return false;
984
985 next = defrag_lookup_extent(inode, em->start + em->len);
Filipe Mananae2127cf2014-03-01 10:57:03 +0000986 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE ||
987 (em->block_start + em->block_len == next->block_start))
Li Zefan6c282eb2012-06-11 16:03:35 +0800988 ret = false;
989
990 free_extent_map(next);
991 return ret;
992}
993
994static int should_defrag_range(struct inode *inode, u64 start, int thresh,
Andrew Mahonea43a2112012-06-19 21:08:32 -0400995 u64 *last_len, u64 *skip, u64 *defrag_end,
996 int compress)
Li Zefan6c282eb2012-06-11 16:03:35 +0800997{
998 struct extent_map *em;
999 int ret = 1;
1000 bool next_mergeable = true;
1001
1002 /*
1003 * make sure that once we start defragging an extent, we keep on
1004 * defragging it
1005 */
1006 if (start < *defrag_end)
1007 return 1;
1008
1009 *skip = 0;
1010
1011 em = defrag_lookup_extent(inode, start);
1012 if (!em)
1013 return 0;
1014
Chris Mason940100a2010-03-10 10:52:59 -05001015 /* this will cover holes, and inline extents */
Liu Bo17ce6ef2012-03-29 09:57:45 -04001016 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
Chris Mason940100a2010-03-10 10:52:59 -05001017 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001018 goto out;
1019 }
1020
Li Zefan6c282eb2012-06-11 16:03:35 +08001021 next_mergeable = defrag_check_next_extent(inode, em);
Chris Mason940100a2010-03-10 10:52:59 -05001022
1023 /*
Li Zefan6c282eb2012-06-11 16:03:35 +08001024 * we hit a real extent, if it is big or the next extent is not a
1025 * real extent, don't bother defragging it
Chris Mason940100a2010-03-10 10:52:59 -05001026 */
Andrew Mahonea43a2112012-06-19 21:08:32 -04001027 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
Li Zefan6c282eb2012-06-11 16:03:35 +08001028 (em->len >= thresh || !next_mergeable))
Chris Mason940100a2010-03-10 10:52:59 -05001029 ret = 0;
Liu Bo17ce6ef2012-03-29 09:57:45 -04001030out:
Chris Mason940100a2010-03-10 10:52:59 -05001031 /*
1032 * last_len ends up being a counter of how many bytes we've defragged.
1033 * every time we choose not to defrag an extent, we reset *last_len
1034 * so that the next tiny extent will force a defrag.
1035 *
1036 * The end result of this is that tiny extents before a single big
1037 * extent will force at least part of that big extent to be defragged.
1038 */
1039 if (ret) {
Chris Mason940100a2010-03-10 10:52:59 -05001040 *defrag_end = extent_map_end(em);
1041 } else {
1042 *last_len = 0;
1043 *skip = extent_map_end(em);
1044 *defrag_end = 0;
1045 }
1046
1047 free_extent_map(em);
1048 return ret;
1049}
1050
Chris Mason4cb53002011-05-24 15:35:30 -04001051/*
1052 * it doesn't do much good to defrag one or two pages
1053 * at a time. This pulls in a nice chunk of pages
1054 * to COW and defrag.
1055 *
1056 * It also makes sure the delalloc code has enough
1057 * dirty data to avoid making new small extents as part
1058 * of the defrag
1059 *
1060 * It's a good idea to start RA on this range
1061 * before calling this.
1062 */
1063static int cluster_pages_for_defrag(struct inode *inode,
1064 struct page **pages,
1065 unsigned long start_index,
Justin Maggardc41570c2014-01-21 11:18:29 -08001066 unsigned long num_pages)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001067{
Chris Mason4cb53002011-05-24 15:35:30 -04001068 unsigned long file_end;
1069 u64 isize = i_size_read(inode);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001070 u64 page_start;
1071 u64 page_end;
Liu Bo1f12bd02012-03-29 09:57:44 -04001072 u64 page_cnt;
Chris Mason4cb53002011-05-24 15:35:30 -04001073 int ret;
1074 int i;
1075 int i_done;
1076 struct btrfs_ordered_extent *ordered;
1077 struct extent_state *cached_state = NULL;
Miao Xie600a45e2012-02-16 15:01:24 +08001078 struct extent_io_tree *tree;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001079 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason4cb53002011-05-24 15:35:30 -04001080
Chris Mason4cb53002011-05-24 15:35:30 -04001081 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
Liu Bo1f12bd02012-03-29 09:57:44 -04001082 if (!isize || start_index > file_end)
1083 return 0;
1084
1085 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
Chris Mason4cb53002011-05-24 15:35:30 -04001086
1087 ret = btrfs_delalloc_reserve_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001088 page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001089 if (ret)
1090 return ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001091 i_done = 0;
Miao Xie600a45e2012-02-16 15:01:24 +08001092 tree = &BTRFS_I(inode)->io_tree;
Chris Mason4cb53002011-05-24 15:35:30 -04001093
1094 /* step one, lock all the pages */
Liu Bo1f12bd02012-03-29 09:57:44 -04001095 for (i = 0; i < page_cnt; i++) {
Chris Mason4cb53002011-05-24 15:35:30 -04001096 struct page *page;
Miao Xie600a45e2012-02-16 15:01:24 +08001097again:
Josef Bacika94733d2011-07-11 10:47:06 -04001098 page = find_or_create_page(inode->i_mapping,
Miao Xie600a45e2012-02-16 15:01:24 +08001099 start_index + i, mask);
Chris Mason4cb53002011-05-24 15:35:30 -04001100 if (!page)
1101 break;
1102
Miao Xie600a45e2012-02-16 15:01:24 +08001103 page_start = page_offset(page);
1104 page_end = page_start + PAGE_CACHE_SIZE - 1;
1105 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001106 lock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001107 ordered = btrfs_lookup_ordered_extent(inode,
1108 page_start);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001109 unlock_extent(tree, page_start, page_end);
Miao Xie600a45e2012-02-16 15:01:24 +08001110 if (!ordered)
1111 break;
1112
1113 unlock_page(page);
1114 btrfs_start_ordered_extent(inode, ordered, 1);
1115 btrfs_put_ordered_extent(ordered);
1116 lock_page(page);
Liu Bo1f12bd02012-03-29 09:57:44 -04001117 /*
1118 * we unlocked the page above, so we need check if
1119 * it was released or not.
1120 */
1121 if (page->mapping != inode->i_mapping) {
1122 unlock_page(page);
1123 page_cache_release(page);
1124 goto again;
1125 }
Miao Xie600a45e2012-02-16 15:01:24 +08001126 }
1127
Chris Mason4cb53002011-05-24 15:35:30 -04001128 if (!PageUptodate(page)) {
1129 btrfs_readpage(NULL, page);
1130 lock_page(page);
1131 if (!PageUptodate(page)) {
1132 unlock_page(page);
1133 page_cache_release(page);
1134 ret = -EIO;
1135 break;
1136 }
1137 }
Miao Xie600a45e2012-02-16 15:01:24 +08001138
Miao Xie600a45e2012-02-16 15:01:24 +08001139 if (page->mapping != inode->i_mapping) {
1140 unlock_page(page);
1141 page_cache_release(page);
1142 goto again;
1143 }
1144
Chris Mason4cb53002011-05-24 15:35:30 -04001145 pages[i] = page;
1146 i_done++;
1147 }
1148 if (!i_done || ret)
1149 goto out;
1150
1151 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1152 goto out;
1153
1154 /*
1155 * so now we have a nice long stream of locked
1156 * and up to date pages, lets wait on them
1157 */
1158 for (i = 0; i < i_done; i++)
1159 wait_on_page_writeback(pages[i]);
1160
1161 page_start = page_offset(pages[0]);
1162 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1163
1164 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001165 page_start, page_end - 1, 0, &cached_state);
Chris Mason4cb53002011-05-24 15:35:30 -04001166 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1167 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001168 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1169 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001170
Liu Bo1f12bd02012-03-29 09:57:44 -04001171 if (i_done != page_cnt) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001172 spin_lock(&BTRFS_I(inode)->lock);
1173 BTRFS_I(inode)->outstanding_extents++;
1174 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason4cb53002011-05-24 15:35:30 -04001175 btrfs_delalloc_release_space(inode,
Liu Bo1f12bd02012-03-29 09:57:44 -04001176 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001177 }
1178
1179
Liu Bo9e8a4a82012-09-05 19:10:51 -06001180 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1181 &cached_state, GFP_NOFS);
Chris Mason4cb53002011-05-24 15:35:30 -04001182
1183 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1184 page_start, page_end - 1, &cached_state,
1185 GFP_NOFS);
1186
1187 for (i = 0; i < i_done; i++) {
1188 clear_page_dirty_for_io(pages[i]);
1189 ClearPageChecked(pages[i]);
1190 set_page_extent_mapped(pages[i]);
1191 set_page_dirty(pages[i]);
1192 unlock_page(pages[i]);
1193 page_cache_release(pages[i]);
1194 }
1195 return i_done;
1196out:
1197 for (i = 0; i < i_done; i++) {
1198 unlock_page(pages[i]);
1199 page_cache_release(pages[i]);
1200 }
Liu Bo1f12bd02012-03-29 09:57:44 -04001201 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
Chris Mason4cb53002011-05-24 15:35:30 -04001202 return ret;
1203
1204}
1205
1206int btrfs_defrag_file(struct inode *inode, struct file *file,
1207 struct btrfs_ioctl_defrag_range_args *range,
1208 u64 newer_than, unsigned long max_to_defrag)
1209{
1210 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4cb53002011-05-24 15:35:30 -04001211 struct file_ra_state *ra = NULL;
1212 unsigned long last_index;
Li Zefan151a31b2011-09-02 15:56:39 +08001213 u64 isize = i_size_read(inode);
Chris Mason940100a2010-03-10 10:52:59 -05001214 u64 last_len = 0;
1215 u64 skip = 0;
1216 u64 defrag_end = 0;
Chris Mason4cb53002011-05-24 15:35:30 -04001217 u64 newer_off = range->start;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001218 unsigned long i;
Li Zefan008873e2011-09-02 15:57:07 +08001219 unsigned long ra_index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001220 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -04001221 int defrag_count = 0;
Li Zefan1a419d82010-10-25 15:12:50 +08001222 int compress_type = BTRFS_COMPRESS_ZLIB;
Chris Mason4cb53002011-05-24 15:35:30 -04001223 int extent_thresh = range->extent_thresh;
Justin Maggardc41570c2014-01-21 11:18:29 -08001224 unsigned long max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1225 unsigned long cluster = max_cluster;
Chris Mason4cb53002011-05-24 15:35:30 -04001226 u64 new_align = ~((u64)128 * 1024 - 1);
1227 struct page **pages = NULL;
1228
Liu Bo0abd5b12013-04-16 09:20:28 +00001229 if (isize == 0)
1230 return 0;
1231
1232 if (range->start >= isize)
1233 return -EINVAL;
Li Zefan1a419d82010-10-25 15:12:50 +08001234
1235 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1236 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1237 return -EINVAL;
1238 if (range->compress_type)
1239 compress_type = range->compress_type;
1240 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001241
Liu Bo0abd5b12013-04-16 09:20:28 +00001242 if (extent_thresh == 0)
1243 extent_thresh = 256 * 1024;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001244
Chris Mason4cb53002011-05-24 15:35:30 -04001245 /*
1246 * if we were not given a file, allocate a readahead
1247 * context
1248 */
1249 if (!file) {
1250 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1251 if (!ra)
1252 return -ENOMEM;
1253 file_ra_state_init(ra, inode->i_mapping);
1254 } else {
1255 ra = &file->f_ra;
1256 }
1257
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +05301258 pages = kmalloc_array(max_cluster, sizeof(struct page *),
Chris Mason4cb53002011-05-24 15:35:30 -04001259 GFP_NOFS);
1260 if (!pages) {
1261 ret = -ENOMEM;
1262 goto out_ra;
1263 }
1264
1265 /* find the last page to defrag */
Chris Mason1e701a32010-03-11 09:42:04 -05001266 if (range->start + range->len > range->start) {
Li Zefan151a31b2011-09-02 15:56:39 +08001267 last_index = min_t(u64, isize - 1,
Chris Mason1e701a32010-03-11 09:42:04 -05001268 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1269 } else {
Li Zefan151a31b2011-09-02 15:56:39 +08001270 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
Chris Mason1e701a32010-03-11 09:42:04 -05001271 }
1272
Chris Mason4cb53002011-05-24 15:35:30 -04001273 if (newer_than) {
1274 ret = find_new_extents(root, inode, newer_than,
1275 &newer_off, 64 * 1024);
1276 if (!ret) {
1277 range->start = newer_off;
1278 /*
1279 * we always align our defrag to help keep
1280 * the extents in the file evenly spaced
1281 */
1282 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001283 } else
1284 goto out_ra;
1285 } else {
1286 i = range->start >> PAGE_CACHE_SHIFT;
1287 }
1288 if (!max_to_defrag)
Liu Bo7ec31b52012-01-26 15:01:12 -05001289 max_to_defrag = last_index + 1;
Chris Mason4cb53002011-05-24 15:35:30 -04001290
Li Zefan2a0f7f52011-10-10 15:43:34 -04001291 /*
1292 * make writeback starts from i, so the defrag range can be
1293 * written sequentially.
1294 */
1295 if (i < inode->i_mapping->writeback_index)
1296 inode->i_mapping->writeback_index = i;
1297
Chris Masonf7f43cc2011-10-11 11:41:40 -04001298 while (i <= last_index && defrag_count < max_to_defrag &&
1299 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1300 PAGE_CACHE_SHIFT)) {
Chris Mason4cb53002011-05-24 15:35:30 -04001301 /*
1302 * make sure we stop running if someone unmounts
1303 * the FS
1304 */
1305 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1306 break;
1307
David Sterba210549e2013-02-09 23:38:06 +00001308 if (btrfs_defrag_cancelled(root->fs_info)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001309 printk(KERN_DEBUG "BTRFS: defrag_file cancelled\n");
David Sterba210549e2013-02-09 23:38:06 +00001310 ret = -EAGAIN;
1311 break;
1312 }
1313
Liu Bo66c26892012-03-29 09:57:45 -04001314 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
Li Zefan6c282eb2012-06-11 16:03:35 +08001315 extent_thresh, &last_len, &skip,
Andrew Mahonea43a2112012-06-19 21:08:32 -04001316 &defrag_end, range->flags &
1317 BTRFS_DEFRAG_RANGE_COMPRESS)) {
Chris Mason940100a2010-03-10 10:52:59 -05001318 unsigned long next;
1319 /*
1320 * the should_defrag function tells us how much to skip
1321 * bump our counter by the suggested amount
1322 */
1323 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1324 i = max(i + 1, next);
1325 continue;
1326 }
Li Zefan008873e2011-09-02 15:57:07 +08001327
1328 if (!newer_than) {
1329 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1330 PAGE_CACHE_SHIFT) - i;
1331 cluster = min(cluster, max_cluster);
1332 } else {
1333 cluster = max_cluster;
1334 }
1335
Li Zefan008873e2011-09-02 15:57:07 +08001336 if (i + cluster > ra_index) {
1337 ra_index = max(i, ra_index);
1338 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1339 cluster);
1340 ra_index += max_cluster;
1341 }
Chris Mason940100a2010-03-10 10:52:59 -05001342
Liu Boecb8bea2012-03-29 09:57:44 -04001343 mutex_lock(&inode->i_mutex);
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001344 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1345 BTRFS_I(inode)->force_compress = compress_type;
Li Zefan008873e2011-09-02 15:57:07 +08001346 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
Liu Boecb8bea2012-03-29 09:57:44 -04001347 if (ret < 0) {
1348 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001349 goto out_ra;
Liu Boecb8bea2012-03-29 09:57:44 -04001350 }
Chris Mason940100a2010-03-10 10:52:59 -05001351
Chris Mason4cb53002011-05-24 15:35:30 -04001352 defrag_count += ret;
Namjae Jeond0e1d662012-12-11 16:00:21 -08001353 balance_dirty_pages_ratelimited(inode->i_mapping);
Liu Boecb8bea2012-03-29 09:57:44 -04001354 mutex_unlock(&inode->i_mutex);
Chris Mason4cb53002011-05-24 15:35:30 -04001355
1356 if (newer_than) {
1357 if (newer_off == (u64)-1)
1358 break;
1359
Liu Boe1f041e2012-03-29 09:57:45 -04001360 if (ret > 0)
1361 i += ret;
1362
Chris Mason4cb53002011-05-24 15:35:30 -04001363 newer_off = max(newer_off + 1,
1364 (u64)i << PAGE_CACHE_SHIFT);
1365
1366 ret = find_new_extents(root, inode,
1367 newer_than, &newer_off,
1368 64 * 1024);
1369 if (!ret) {
1370 range->start = newer_off;
1371 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
Chris Mason4cb53002011-05-24 15:35:30 -04001372 } else {
1373 break;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001374 }
Chris Mason4cb53002011-05-24 15:35:30 -04001375 } else {
Li Zefan008873e2011-09-02 15:57:07 +08001376 if (ret > 0) {
Li Zefancbcc8322011-09-02 15:56:25 +08001377 i += ret;
Li Zefan008873e2011-09-02 15:57:07 +08001378 last_len += ret << PAGE_CACHE_SHIFT;
1379 } else {
Li Zefancbcc8322011-09-02 15:56:25 +08001380 i++;
Li Zefan008873e2011-09-02 15:57:07 +08001381 last_len = 0;
1382 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001383 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001384 }
1385
Filipe Mananadec8ef92014-03-01 10:55:54 +00001386 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) {
Chris Mason1e701a32010-03-11 09:42:04 -05001387 filemap_flush(inode->i_mapping);
Filipe Mananadec8ef92014-03-01 10:55:54 +00001388 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1389 &BTRFS_I(inode)->runtime_flags))
1390 filemap_flush(inode->i_mapping);
1391 }
Chris Mason1e701a32010-03-11 09:42:04 -05001392
1393 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1394 /* the filemap_flush will queue IO into the worker threads, but
1395 * we have to make sure the IO is actually started and that
1396 * ordered extents get created before we return
1397 */
1398 atomic_inc(&root->fs_info->async_submit_draining);
1399 while (atomic_read(&root->fs_info->nr_async_submits) ||
1400 atomic_read(&root->fs_info->async_delalloc_pages)) {
1401 wait_event(root->fs_info->async_submit_wait,
1402 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1403 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1404 }
1405 atomic_dec(&root->fs_info->async_submit_draining);
Chris Mason1e701a32010-03-11 09:42:04 -05001406 }
1407
Li Zefan1a419d82010-10-25 15:12:50 +08001408 if (range->compress_type == BTRFS_COMPRESS_LZO) {
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06001409 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
Li Zefan1a419d82010-10-25 15:12:50 +08001410 }
1411
Diego Calleja60ccf822011-09-01 16:33:57 +02001412 ret = defrag_count;
Chris Mason940100a2010-03-10 10:52:59 -05001413
Chris Mason4cb53002011-05-24 15:35:30 -04001414out_ra:
Filipe David Borba Manana633085c2013-08-16 15:23:33 +01001415 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1416 mutex_lock(&inode->i_mutex);
1417 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1418 mutex_unlock(&inode->i_mutex);
1419 }
Chris Mason4cb53002011-05-24 15:35:30 -04001420 if (!file)
1421 kfree(ra);
1422 kfree(pages);
Chris Mason940100a2010-03-10 10:52:59 -05001423 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001424}
1425
Miao Xie198605a2012-11-26 08:43:45 +00001426static noinline int btrfs_ioctl_resize(struct file *file,
Yan, Zheng76dda932009-09-21 16:00:26 -04001427 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001428{
1429 u64 new_size;
1430 u64 old_size;
1431 u64 devid = 1;
Al Viro496ad9a2013-01-23 17:07:38 -05001432 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001433 struct btrfs_ioctl_vol_args *vol_args;
1434 struct btrfs_trans_handle *trans;
1435 struct btrfs_device *device = NULL;
1436 char *sizestr;
1437 char *devstr = NULL;
1438 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001439 int mod = 0;
1440
Chris Masone441d542009-01-05 16:57:23 -05001441 if (!capable(CAP_SYS_ADMIN))
1442 return -EPERM;
1443
Miao Xie198605a2012-11-26 08:43:45 +00001444 ret = mnt_want_write_file(file);
1445 if (ret)
1446 return ret;
1447
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001448 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1449 1)) {
Miao Xie97547672012-12-21 10:38:50 +00001450 mnt_drop_write_file(file);
Anand Jaine57138b2013-08-21 11:44:48 +08001451 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001452 }
1453
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001454 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08001455 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001456 if (IS_ERR(vol_args)) {
1457 ret = PTR_ERR(vol_args);
1458 goto out;
1459 }
Mark Fasheh5516e592008-07-24 12:20:14 -04001460
1461 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001462
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001463 sizestr = vol_args->name;
1464 devstr = strchr(sizestr, ':');
1465 if (devstr) {
1466 char *end;
1467 sizestr = devstr + 1;
1468 *devstr = '\0';
1469 devstr = vol_args->name;
1470 devid = simple_strtoull(devstr, &end, 10);
Miao Xiedfd79822012-12-21 09:21:30 +00001471 if (!devid) {
1472 ret = -EINVAL;
1473 goto out_free;
1474 }
Frank Holtonefe120a2013-12-20 11:37:06 -05001475 btrfs_info(root->fs_info, "resizing devid %llu", devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001476 }
Miao Xiedba60f32012-12-21 09:19:51 +00001477
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001478 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001479 if (!device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001480 btrfs_info(root->fs_info, "resizer unable to find device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001481 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001482 ret = -ENODEV;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001483 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001484 }
Miao Xiedba60f32012-12-21 09:19:51 +00001485
1486 if (!device->writeable) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001487 btrfs_info(root->fs_info,
1488 "resizer unable to apply on readonly device %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001489 devid);
Miao Xiedfd79822012-12-21 09:21:30 +00001490 ret = -EPERM;
Liu Bo4e42ae12012-06-14 02:23:19 -06001491 goto out_free;
1492 }
1493
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001494 if (!strcmp(sizestr, "max"))
1495 new_size = device->bdev->bd_inode->i_size;
1496 else {
1497 if (sizestr[0] == '-') {
1498 mod = -1;
1499 sizestr++;
1500 } else if (sizestr[0] == '+') {
1501 mod = 1;
1502 sizestr++;
1503 }
Akinobu Mita91748462010-02-28 10:59:11 +00001504 new_size = memparse(sizestr, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001505 if (new_size == 0) {
1506 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001507 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001508 }
1509 }
1510
Stefan Behrens63a212a2012-11-05 18:29:28 +01001511 if (device->is_tgtdev_for_dev_replace) {
Miao Xiedfd79822012-12-21 09:21:30 +00001512 ret = -EPERM;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001513 goto out_free;
1514 }
1515
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001516 old_size = device->total_bytes;
1517
1518 if (mod < 0) {
1519 if (new_size > old_size) {
1520 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001521 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001522 }
1523 new_size = old_size - new_size;
1524 } else if (mod > 0) {
Wenliang Faneb8052e2013-12-20 15:28:56 +08001525 if (new_size > ULLONG_MAX - old_size) {
1526 ret = -EINVAL;
1527 goto out_free;
1528 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001529 new_size = old_size + new_size;
1530 }
1531
1532 if (new_size < 256 * 1024 * 1024) {
1533 ret = -EINVAL;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001534 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001535 }
1536 if (new_size > device->bdev->bd_inode->i_size) {
1537 ret = -EFBIG;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001538 goto out_free;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001539 }
1540
1541 do_div(new_size, root->sectorsize);
1542 new_size *= root->sectorsize;
1543
Frank Holtonefe120a2013-12-20 11:37:06 -05001544 printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001545 rcu_str_deref(device->name), new_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001546
1547 if (new_size > old_size) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001548 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001549 if (IS_ERR(trans)) {
1550 ret = PTR_ERR(trans);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001551 goto out_free;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001552 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001553 ret = btrfs_grow_device(trans, device, new_size);
1554 btrfs_commit_transaction(trans, root);
Mike Fleetwoodece7d202011-11-18 18:55:01 +00001555 } else if (new_size < old_size) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001556 ret = btrfs_shrink_device(device, new_size);
jeff.liu0253f402012-10-27 12:06:39 +00001557 } /* equal, nothing need to do */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001558
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001559out_free:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001560 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02001561out:
1562 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01001563 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov18f39c42013-01-20 15:57:57 +02001564 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001565 return ret;
1566}
1567
Sage Weil72fd0322010-10-29 15:41:32 -04001568static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001569 char *name, unsigned long fd, int subvol,
1570 u64 *transid, bool readonly,
Miao Xie8696c532013-02-07 06:02:44 +00001571 struct btrfs_qgroup_inherit *inherit)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001572{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001573 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -05001574 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001575
Liu Boa874a632012-06-29 03:58:46 -06001576 ret = mnt_want_write_file(file);
1577 if (ret)
1578 goto out;
1579
Sage Weil72fd0322010-10-29 15:41:32 -04001580 namelen = strlen(name);
1581 if (strchr(name, '/')) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001582 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001583 goto out_drop_write;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001584 }
1585
Chris Mason16780ca2012-02-20 22:14:55 -05001586 if (name[0] == '.' &&
1587 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1588 ret = -EEXIST;
Liu Boa874a632012-06-29 03:58:46 -06001589 goto out_drop_write;
Chris Mason16780ca2012-02-20 22:14:55 -05001590 }
1591
Chris Mason3de45862008-11-17 21:02:50 -05001592 if (subvol) {
Sage Weil72fd0322010-10-29 15:41:32 -04001593 ret = btrfs_mksubvol(&file->f_path, name, namelen,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001594 NULL, transid, readonly, inherit);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001595 } else {
Al Viro2903ff02012-08-28 12:52:22 -04001596 struct fd src = fdget(fd);
Chris Mason3de45862008-11-17 21:02:50 -05001597 struct inode *src_inode;
Al Viro2903ff02012-08-28 12:52:22 -04001598 if (!src.file) {
Chris Mason3de45862008-11-17 21:02:50 -05001599 ret = -EINVAL;
Liu Boa874a632012-06-29 03:58:46 -06001600 goto out_drop_write;
Chris Mason3de45862008-11-17 21:02:50 -05001601 }
1602
Al Viro496ad9a2013-01-23 17:07:38 -05001603 src_inode = file_inode(src.file);
1604 if (src_inode->i_sb != file_inode(file)->i_sb) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001605 btrfs_info(BTRFS_I(src_inode)->root->fs_info,
1606 "Snapshot src from another FS");
Kusanagi Kouichi23ad5b12014-01-30 16:32:02 +09001607 ret = -EXDEV;
David Sterbad0242062014-01-15 18:15:52 +01001608 } else if (!inode_owner_or_capable(src_inode)) {
1609 /*
1610 * Subvolume creation is not restricted, but snapshots
1611 * are limited to own subvolumes only
1612 */
1613 ret = -EPERM;
Al Viroecd18812012-08-26 21:20:24 -04001614 } else {
1615 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1616 BTRFS_I(src_inode)->root,
1617 transid, readonly, inherit);
Chris Mason3de45862008-11-17 21:02:50 -05001618 }
Al Viro2903ff02012-08-28 12:52:22 -04001619 fdput(src);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04001620 }
Liu Boa874a632012-06-29 03:58:46 -06001621out_drop_write:
1622 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001623out:
Sage Weil72fd0322010-10-29 15:41:32 -04001624 return ret;
1625}
1626
1627static noinline int btrfs_ioctl_snap_create(struct file *file,
Li Zefanfa0d2b92010-12-20 15:53:28 +08001628 void __user *arg, int subvol)
Sage Weil72fd0322010-10-29 15:41:32 -04001629{
Li Zefanfa0d2b92010-12-20 15:53:28 +08001630 struct btrfs_ioctl_vol_args *vol_args;
Sage Weil72fd0322010-10-29 15:41:32 -04001631 int ret;
1632
Li Zefanfa0d2b92010-12-20 15:53:28 +08001633 vol_args = memdup_user(arg, sizeof(*vol_args));
1634 if (IS_ERR(vol_args))
1635 return PTR_ERR(vol_args);
1636 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Sage Weil72fd0322010-10-29 15:41:32 -04001637
Li Zefanfa0d2b92010-12-20 15:53:28 +08001638 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Li Zefanb83cc962010-12-20 16:04:08 +08001639 vol_args->fd, subvol,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001640 NULL, false, NULL);
Li Zefanfdfb1e42010-12-10 06:41:56 +00001641
Li Zefanfa0d2b92010-12-20 15:53:28 +08001642 kfree(vol_args);
1643 return ret;
1644}
Li Zefanfdfb1e42010-12-10 06:41:56 +00001645
Li Zefanfa0d2b92010-12-20 15:53:28 +08001646static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1647 void __user *arg, int subvol)
1648{
1649 struct btrfs_ioctl_vol_args_v2 *vol_args;
1650 int ret;
1651 u64 transid = 0;
1652 u64 *ptr = NULL;
Li Zefanb83cc962010-12-20 16:04:08 +08001653 bool readonly = false;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001654 struct btrfs_qgroup_inherit *inherit = NULL;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001655
Li Zefanfa0d2b92010-12-20 15:53:28 +08001656 vol_args = memdup_user(arg, sizeof(*vol_args));
1657 if (IS_ERR(vol_args))
1658 return PTR_ERR(vol_args);
1659 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
Sage Weil75eaa0e2010-12-10 00:36:28 +00001660
Li Zefanb83cc962010-12-20 16:04:08 +08001661 if (vol_args->flags &
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001662 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1663 BTRFS_SUBVOL_QGROUP_INHERIT)) {
Li Zefanb83cc962010-12-20 16:04:08 +08001664 ret = -EOPNOTSUPP;
Li Zefanfa0d2b92010-12-20 15:53:28 +08001665 goto out;
Sage Weil72fd0322010-10-29 15:41:32 -04001666 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001667
1668 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1669 ptr = &transid;
Li Zefanb83cc962010-12-20 16:04:08 +08001670 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1671 readonly = true;
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001672 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1673 if (vol_args->size > PAGE_CACHE_SIZE) {
1674 ret = -EINVAL;
1675 goto out;
1676 }
1677 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1678 if (IS_ERR(inherit)) {
1679 ret = PTR_ERR(inherit);
1680 goto out;
1681 }
1682 }
Li Zefanfa0d2b92010-12-20 15:53:28 +08001683
1684 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001685 vol_args->fd, subvol, ptr,
Miao Xie8696c532013-02-07 06:02:44 +00001686 readonly, inherit);
Li Zefanfa0d2b92010-12-20 15:53:28 +08001687
1688 if (ret == 0 && ptr &&
1689 copy_to_user(arg +
1690 offsetof(struct btrfs_ioctl_vol_args_v2,
1691 transid), ptr, sizeof(*ptr)))
1692 ret = -EFAULT;
Li Zefanfdfb1e42010-12-10 06:41:56 +00001693out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001694 kfree(vol_args);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02001695 kfree(inherit);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001696 return ret;
1697}
1698
Li Zefan0caa1022010-12-20 16:30:25 +08001699static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1700 void __user *arg)
1701{
Al Viro496ad9a2013-01-23 17:07:38 -05001702 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001703 struct btrfs_root *root = BTRFS_I(inode)->root;
1704 int ret = 0;
1705 u64 flags = 0;
1706
Li Zefan33345d012011-04-20 10:31:50 +08001707 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
Li Zefan0caa1022010-12-20 16:30:25 +08001708 return -EINVAL;
1709
1710 down_read(&root->fs_info->subvol_sem);
1711 if (btrfs_root_readonly(root))
1712 flags |= BTRFS_SUBVOL_RDONLY;
1713 up_read(&root->fs_info->subvol_sem);
1714
1715 if (copy_to_user(arg, &flags, sizeof(flags)))
1716 ret = -EFAULT;
1717
1718 return ret;
1719}
1720
1721static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1722 void __user *arg)
1723{
Al Viro496ad9a2013-01-23 17:07:38 -05001724 struct inode *inode = file_inode(file);
Li Zefan0caa1022010-12-20 16:30:25 +08001725 struct btrfs_root *root = BTRFS_I(inode)->root;
1726 struct btrfs_trans_handle *trans;
1727 u64 root_flags;
1728 u64 flags;
1729 int ret = 0;
1730
David Sterbabd60ea02014-01-16 15:50:22 +01001731 if (!inode_owner_or_capable(inode))
1732 return -EPERM;
1733
Liu Bob9ca0662012-06-29 03:58:49 -06001734 ret = mnt_want_write_file(file);
1735 if (ret)
1736 goto out;
Li Zefan0caa1022010-12-20 16:30:25 +08001737
Liu Bob9ca0662012-06-29 03:58:49 -06001738 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1739 ret = -EINVAL;
1740 goto out_drop_write;
1741 }
Li Zefan0caa1022010-12-20 16:30:25 +08001742
Liu Bob9ca0662012-06-29 03:58:49 -06001743 if (copy_from_user(&flags, arg, sizeof(flags))) {
1744 ret = -EFAULT;
1745 goto out_drop_write;
1746 }
Li Zefan0caa1022010-12-20 16:30:25 +08001747
Liu Bob9ca0662012-06-29 03:58:49 -06001748 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1749 ret = -EINVAL;
1750 goto out_drop_write;
1751 }
Li Zefan0caa1022010-12-20 16:30:25 +08001752
Liu Bob9ca0662012-06-29 03:58:49 -06001753 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1754 ret = -EOPNOTSUPP;
1755 goto out_drop_write;
1756 }
Li Zefan0caa1022010-12-20 16:30:25 +08001757
1758 down_write(&root->fs_info->subvol_sem);
1759
1760 /* nothing to do */
1761 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
Liu Bob9ca0662012-06-29 03:58:49 -06001762 goto out_drop_sem;
Li Zefan0caa1022010-12-20 16:30:25 +08001763
1764 root_flags = btrfs_root_flags(&root->root_item);
David Sterba2c686532013-12-16 17:34:17 +01001765 if (flags & BTRFS_SUBVOL_RDONLY) {
Li Zefan0caa1022010-12-20 16:30:25 +08001766 btrfs_set_root_flags(&root->root_item,
1767 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001768 } else {
1769 /*
1770 * Block RO -> RW transition if this subvolume is involved in
1771 * send
1772 */
1773 spin_lock(&root->root_item_lock);
1774 if (root->send_in_progress == 0) {
1775 btrfs_set_root_flags(&root->root_item,
Li Zefan0caa1022010-12-20 16:30:25 +08001776 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
David Sterba2c686532013-12-16 17:34:17 +01001777 spin_unlock(&root->root_item_lock);
1778 } else {
1779 spin_unlock(&root->root_item_lock);
1780 btrfs_warn(root->fs_info,
1781 "Attempt to set subvolume %llu read-write during send",
1782 root->root_key.objectid);
1783 ret = -EPERM;
1784 goto out_drop_sem;
1785 }
1786 }
Li Zefan0caa1022010-12-20 16:30:25 +08001787
1788 trans = btrfs_start_transaction(root, 1);
1789 if (IS_ERR(trans)) {
1790 ret = PTR_ERR(trans);
1791 goto out_reset;
1792 }
1793
Li Zefanb4dc2b82011-02-16 06:06:34 +00001794 ret = btrfs_update_root(trans, root->fs_info->tree_root,
Li Zefan0caa1022010-12-20 16:30:25 +08001795 &root->root_key, &root->root_item);
1796
1797 btrfs_commit_transaction(trans, root);
1798out_reset:
1799 if (ret)
1800 btrfs_set_root_flags(&root->root_item, root_flags);
Liu Bob9ca0662012-06-29 03:58:49 -06001801out_drop_sem:
Li Zefan0caa1022010-12-20 16:30:25 +08001802 up_write(&root->fs_info->subvol_sem);
Liu Bob9ca0662012-06-29 03:58:49 -06001803out_drop_write:
1804 mnt_drop_write_file(file);
1805out:
Li Zefan0caa1022010-12-20 16:30:25 +08001806 return ret;
1807}
1808
Yan, Zheng76dda932009-09-21 16:00:26 -04001809/*
1810 * helper to check if the subvolume references other subvolumes
1811 */
1812static noinline int may_destroy_subvol(struct btrfs_root *root)
1813{
1814 struct btrfs_path *path;
Josef Bacik175a2b82013-08-12 15:36:44 -04001815 struct btrfs_dir_item *di;
Yan, Zheng76dda932009-09-21 16:00:26 -04001816 struct btrfs_key key;
Josef Bacik175a2b82013-08-12 15:36:44 -04001817 u64 dir_id;
Yan, Zheng76dda932009-09-21 16:00:26 -04001818 int ret;
1819
1820 path = btrfs_alloc_path();
1821 if (!path)
1822 return -ENOMEM;
1823
Josef Bacik175a2b82013-08-12 15:36:44 -04001824 /* Make sure this root isn't set as the default subvol */
1825 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1826 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1827 dir_id, "default", 7, 0);
1828 if (di && !IS_ERR(di)) {
1829 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1830 if (key.objectid == root->root_key.objectid) {
1831 ret = -ENOTEMPTY;
1832 goto out;
1833 }
1834 btrfs_release_path(path);
1835 }
1836
Yan, Zheng76dda932009-09-21 16:00:26 -04001837 key.objectid = root->root_key.objectid;
1838 key.type = BTRFS_ROOT_REF_KEY;
1839 key.offset = (u64)-1;
1840
1841 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1842 &key, path, 0, 0);
1843 if (ret < 0)
1844 goto out;
1845 BUG_ON(ret == 0);
1846
1847 ret = 0;
1848 if (path->slots[0] > 0) {
1849 path->slots[0]--;
1850 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1851 if (key.objectid == root->root_key.objectid &&
1852 key.type == BTRFS_ROOT_REF_KEY)
1853 ret = -ENOTEMPTY;
1854 }
1855out:
1856 btrfs_free_path(path);
1857 return ret;
1858}
1859
Chris Masonac8e9812010-02-28 15:39:26 -05001860static noinline int key_in_sk(struct btrfs_key *key,
1861 struct btrfs_ioctl_search_key *sk)
1862{
Chris Masonabc6e132010-03-18 12:10:08 -04001863 struct btrfs_key test;
1864 int ret;
1865
1866 test.objectid = sk->min_objectid;
1867 test.type = sk->min_type;
1868 test.offset = sk->min_offset;
1869
1870 ret = btrfs_comp_cpu_keys(key, &test);
1871 if (ret < 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001872 return 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001873
1874 test.objectid = sk->max_objectid;
1875 test.type = sk->max_type;
1876 test.offset = sk->max_offset;
1877
1878 ret = btrfs_comp_cpu_keys(key, &test);
1879 if (ret > 0)
Chris Masonac8e9812010-02-28 15:39:26 -05001880 return 0;
1881 return 1;
1882}
1883
1884static noinline int copy_to_sk(struct btrfs_root *root,
1885 struct btrfs_path *path,
1886 struct btrfs_key *key,
1887 struct btrfs_ioctl_search_key *sk,
1888 char *buf,
1889 unsigned long *sk_offset,
1890 int *num_found)
1891{
1892 u64 found_transid;
1893 struct extent_buffer *leaf;
1894 struct btrfs_ioctl_search_header sh;
1895 unsigned long item_off;
1896 unsigned long item_len;
1897 int nritems;
1898 int i;
1899 int slot;
Chris Masonac8e9812010-02-28 15:39:26 -05001900 int ret = 0;
1901
1902 leaf = path->nodes[0];
1903 slot = path->slots[0];
1904 nritems = btrfs_header_nritems(leaf);
1905
1906 if (btrfs_header_generation(leaf) > sk->max_transid) {
1907 i = nritems;
1908 goto advance_key;
1909 }
1910 found_transid = btrfs_header_generation(leaf);
1911
1912 for (i = slot; i < nritems; i++) {
1913 item_off = btrfs_item_ptr_offset(leaf, i);
1914 item_len = btrfs_item_size_nr(leaf, i);
1915
Gabriel de Perthuis03b71c62013-05-06 17:40:18 +00001916 btrfs_item_key_to_cpu(leaf, key, i);
1917 if (!key_in_sk(key, sk))
1918 continue;
1919
1920 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
Chris Masonac8e9812010-02-28 15:39:26 -05001921 item_len = 0;
1922
1923 if (sizeof(sh) + item_len + *sk_offset >
1924 BTRFS_SEARCH_ARGS_BUFSIZE) {
1925 ret = 1;
1926 goto overflow;
1927 }
1928
Chris Masonac8e9812010-02-28 15:39:26 -05001929 sh.objectid = key->objectid;
1930 sh.offset = key->offset;
1931 sh.type = key->type;
1932 sh.len = item_len;
1933 sh.transid = found_transid;
1934
1935 /* copy search result header */
1936 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1937 *sk_offset += sizeof(sh);
1938
1939 if (item_len) {
1940 char *p = buf + *sk_offset;
1941 /* copy the item */
1942 read_extent_buffer(leaf, p,
1943 item_off, item_len);
1944 *sk_offset += item_len;
Chris Masonac8e9812010-02-28 15:39:26 -05001945 }
Hugo Millse2156862011-05-14 17:43:41 +00001946 (*num_found)++;
Chris Masonac8e9812010-02-28 15:39:26 -05001947
1948 if (*num_found >= sk->nr_items)
1949 break;
1950 }
1951advance_key:
Chris Masonac8e9812010-02-28 15:39:26 -05001952 ret = 0;
Chris Masonabc6e132010-03-18 12:10:08 -04001953 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1954 key->offset++;
1955 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1956 key->offset = 0;
1957 key->type++;
1958 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1959 key->offset = 0;
1960 key->type = 0;
1961 key->objectid++;
1962 } else
1963 ret = 1;
Chris Masonac8e9812010-02-28 15:39:26 -05001964overflow:
Chris Masonac8e9812010-02-28 15:39:26 -05001965 return ret;
1966}
1967
1968static noinline int search_ioctl(struct inode *inode,
1969 struct btrfs_ioctl_search_args *args)
1970{
1971 struct btrfs_root *root;
1972 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05001973 struct btrfs_path *path;
1974 struct btrfs_ioctl_search_key *sk = &args->key;
1975 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1976 int ret;
1977 int num_found = 0;
1978 unsigned long sk_offset = 0;
1979
1980 path = btrfs_alloc_path();
1981 if (!path)
1982 return -ENOMEM;
1983
1984 if (sk->tree_id == 0) {
1985 /* search the root of the inode that was passed */
1986 root = BTRFS_I(inode)->root;
1987 } else {
1988 key.objectid = sk->tree_id;
1989 key.type = BTRFS_ROOT_ITEM_KEY;
1990 key.offset = (u64)-1;
1991 root = btrfs_read_fs_root_no_name(info, &key);
1992 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001993 printk(KERN_ERR "BTRFS: could not find root %llu\n",
Chris Masonac8e9812010-02-28 15:39:26 -05001994 sk->tree_id);
1995 btrfs_free_path(path);
1996 return -ENOENT;
1997 }
1998 }
1999
2000 key.objectid = sk->min_objectid;
2001 key.type = sk->min_type;
2002 key.offset = sk->min_offset;
2003
Chris Masonac8e9812010-02-28 15:39:26 -05002004 path->keep_locks = 1;
2005
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302006 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01002007 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
Chris Masonac8e9812010-02-28 15:39:26 -05002008 if (ret != 0) {
2009 if (ret > 0)
2010 ret = 0;
2011 goto err;
2012 }
2013 ret = copy_to_sk(root, path, &key, sk, args->buf,
2014 &sk_offset, &num_found);
David Sterbab3b4aa72011-04-21 01:20:15 +02002015 btrfs_release_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002016 if (ret || num_found >= sk->nr_items)
2017 break;
2018
2019 }
2020 ret = 0;
2021err:
2022 sk->nr_items = num_found;
2023 btrfs_free_path(path);
2024 return ret;
2025}
2026
2027static noinline int btrfs_ioctl_tree_search(struct file *file,
2028 void __user *argp)
2029{
2030 struct btrfs_ioctl_search_args *args;
2031 struct inode *inode;
2032 int ret;
2033
2034 if (!capable(CAP_SYS_ADMIN))
2035 return -EPERM;
2036
Julia Lawall2354d082010-10-29 15:14:18 -04002037 args = memdup_user(argp, sizeof(*args));
2038 if (IS_ERR(args))
2039 return PTR_ERR(args);
Chris Masonac8e9812010-02-28 15:39:26 -05002040
Al Viro496ad9a2013-01-23 17:07:38 -05002041 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002042 ret = search_ioctl(inode, args);
2043 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2044 ret = -EFAULT;
2045 kfree(args);
2046 return ret;
2047}
2048
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002049/*
Chris Masonac8e9812010-02-28 15:39:26 -05002050 * Search INODE_REFs to identify path name of 'dirid' directory
2051 * in a 'tree_id' tree. and sets path name to 'name'.
2052 */
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002053static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
2054 u64 tree_id, u64 dirid, char *name)
2055{
2056 struct btrfs_root *root;
2057 struct btrfs_key key;
Chris Masonac8e9812010-02-28 15:39:26 -05002058 char *ptr;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002059 int ret = -1;
2060 int slot;
2061 int len;
2062 int total_len = 0;
2063 struct btrfs_inode_ref *iref;
2064 struct extent_buffer *l;
2065 struct btrfs_path *path;
2066
2067 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
2068 name[0]='\0';
2069 return 0;
2070 }
2071
2072 path = btrfs_alloc_path();
2073 if (!path)
2074 return -ENOMEM;
2075
Chris Masonac8e9812010-02-28 15:39:26 -05002076 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002077
2078 key.objectid = tree_id;
2079 key.type = BTRFS_ROOT_ITEM_KEY;
2080 key.offset = (u64)-1;
2081 root = btrfs_read_fs_root_no_name(info, &key);
2082 if (IS_ERR(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002083 printk(KERN_ERR "BTRFS: could not find root %llu\n", tree_id);
Chris Mason8ad6fca2010-03-18 12:23:10 -04002084 ret = -ENOENT;
2085 goto out;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002086 }
2087
2088 key.objectid = dirid;
2089 key.type = BTRFS_INODE_REF_KEY;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002090 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002091
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302092 while (1) {
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002093 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2094 if (ret < 0)
2095 goto out;
Filipe David Borba Manana18674c62013-08-14 03:00:21 +01002096 else if (ret > 0) {
2097 ret = btrfs_previous_item(root, path, dirid,
2098 BTRFS_INODE_REF_KEY);
2099 if (ret < 0)
2100 goto out;
2101 else if (ret > 0) {
2102 ret = -ENOENT;
2103 goto out;
2104 }
2105 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002106
2107 l = path->nodes[0];
2108 slot = path->slots[0];
2109 btrfs_item_key_to_cpu(l, &key, slot);
2110
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002111 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2112 len = btrfs_inode_ref_name_len(l, iref);
2113 ptr -= len + 1;
2114 total_len += len + 1;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002115 if (ptr < name) {
2116 ret = -ENAMETOOLONG;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002117 goto out;
Filipe David Borba Mananaa696cf32013-08-14 03:00:20 +01002118 }
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002119
2120 *(ptr + len) = '/';
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302121 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002122
2123 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2124 break;
2125
David Sterbab3b4aa72011-04-21 01:20:15 +02002126 btrfs_release_path(path);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002127 key.objectid = key.offset;
Chris Mason8ad6fca2010-03-18 12:23:10 -04002128 key.offset = (u64)-1;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002129 dirid = key.objectid;
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002130 }
Li Zefan77906a502011-07-14 03:16:00 +00002131 memmove(name, ptr, total_len);
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302132 name[total_len] = '\0';
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002133 ret = 0;
2134out:
2135 btrfs_free_path(path);
Chris Masonac8e9812010-02-28 15:39:26 -05002136 return ret;
2137}
2138
2139static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2140 void __user *argp)
2141{
2142 struct btrfs_ioctl_ino_lookup_args *args;
2143 struct inode *inode;
2144 int ret;
2145
2146 if (!capable(CAP_SYS_ADMIN))
2147 return -EPERM;
2148
Julia Lawall2354d082010-10-29 15:14:18 -04002149 args = memdup_user(argp, sizeof(*args));
2150 if (IS_ERR(args))
2151 return PTR_ERR(args);
Dan Carpenterc2b96922010-03-20 11:24:15 +00002152
Al Viro496ad9a2013-01-23 17:07:38 -05002153 inode = file_inode(file);
Chris Masonac8e9812010-02-28 15:39:26 -05002154
Chris Mason1b53ac42010-03-18 12:17:05 -04002155 if (args->treeid == 0)
2156 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2157
Chris Masonac8e9812010-02-28 15:39:26 -05002158 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2159 args->treeid, args->objectid,
2160 args->name);
2161
2162 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2163 ret = -EFAULT;
2164
2165 kfree(args);
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +00002166 return ret;
2167}
2168
Yan, Zheng76dda932009-09-21 16:00:26 -04002169static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2170 void __user *arg)
2171{
Al Viro54563d42013-09-01 15:57:51 -04002172 struct dentry *parent = file->f_path.dentry;
Yan, Zheng76dda932009-09-21 16:00:26 -04002173 struct dentry *dentry;
2174 struct inode *dir = parent->d_inode;
2175 struct inode *inode;
2176 struct btrfs_root *root = BTRFS_I(dir)->root;
2177 struct btrfs_root *dest = NULL;
2178 struct btrfs_ioctl_vol_args *vol_args;
2179 struct btrfs_trans_handle *trans;
Miao Xiec58aaad2013-02-28 10:05:36 +00002180 struct btrfs_block_rsv block_rsv;
2181 u64 qgroup_reserved;
Yan, Zheng76dda932009-09-21 16:00:26 -04002182 int namelen;
2183 int ret;
2184 int err = 0;
2185
Yan, Zheng76dda932009-09-21 16:00:26 -04002186 vol_args = memdup_user(arg, sizeof(*vol_args));
2187 if (IS_ERR(vol_args))
2188 return PTR_ERR(vol_args);
2189
2190 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2191 namelen = strlen(vol_args->name);
2192 if (strchr(vol_args->name, '/') ||
2193 strncmp(vol_args->name, "..", namelen) == 0) {
2194 err = -EINVAL;
2195 goto out;
2196 }
2197
Al Viroa561be72011-11-23 11:57:51 -05002198 err = mnt_want_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002199 if (err)
2200 goto out;
2201
David Sterba5c50c9b2013-03-22 18:12:51 +00002202 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2203 if (err == -EINTR)
David Sterbae43f9982013-12-06 17:51:32 +01002204 goto out_drop_write;
Yan, Zheng76dda932009-09-21 16:00:26 -04002205 dentry = lookup_one_len(vol_args->name, parent, namelen);
2206 if (IS_ERR(dentry)) {
2207 err = PTR_ERR(dentry);
2208 goto out_unlock_dir;
2209 }
2210
2211 if (!dentry->d_inode) {
2212 err = -ENOENT;
2213 goto out_dput;
2214 }
2215
2216 inode = dentry->d_inode;
Sage Weil4260f7c2010-10-29 15:46:43 -04002217 dest = BTRFS_I(inode)->root;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05302218 if (!capable(CAP_SYS_ADMIN)) {
Sage Weil4260f7c2010-10-29 15:46:43 -04002219 /*
2220 * Regular user. Only allow this with a special mount
2221 * option, when the user has write+exec access to the
2222 * subvol root, and when rmdir(2) would have been
2223 * allowed.
2224 *
2225 * Note that this is _not_ check that the subvol is
2226 * empty or doesn't contain data that we wouldn't
2227 * otherwise be able to delete.
2228 *
2229 * Users who want to delete empty subvols should try
2230 * rmdir(2).
2231 */
2232 err = -EPERM;
2233 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2234 goto out_dput;
2235
2236 /*
2237 * Do not allow deletion if the parent dir is the same
2238 * as the dir to be deleted. That means the ioctl
2239 * must be called on the dentry referencing the root
2240 * of the subvol, not a random directory contained
2241 * within it.
2242 */
2243 err = -EINVAL;
2244 if (root == dest)
2245 goto out_dput;
2246
2247 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2248 if (err)
2249 goto out_dput;
Sage Weil4260f7c2010-10-29 15:46:43 -04002250 }
2251
Miao Xie5c39da52012-10-22 11:39:53 +00002252 /* check if subvolume may be deleted by a user */
2253 err = btrfs_may_delete(dir, dentry, 1);
2254 if (err)
2255 goto out_dput;
2256
Li Zefan33345d012011-04-20 10:31:50 +08002257 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
Yan, Zheng76dda932009-09-21 16:00:26 -04002258 err = -EINVAL;
2259 goto out_dput;
2260 }
2261
Yan, Zheng76dda932009-09-21 16:00:26 -04002262 mutex_lock(&inode->i_mutex);
2263 err = d_invalidate(dentry);
2264 if (err)
2265 goto out_unlock;
2266
2267 down_write(&root->fs_info->subvol_sem);
2268
2269 err = may_destroy_subvol(dest);
2270 if (err)
2271 goto out_up_write;
2272
Miao Xiec58aaad2013-02-28 10:05:36 +00002273 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2274 /*
2275 * One for dir inode, two for dir entries, two for root
2276 * ref/backref.
2277 */
2278 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04002279 5, &qgroup_reserved, true);
Miao Xiec58aaad2013-02-28 10:05:36 +00002280 if (err)
2281 goto out_up_write;
2282
Yan, Zhenga22285a2010-05-16 10:48:46 -04002283 trans = btrfs_start_transaction(root, 0);
2284 if (IS_ERR(trans)) {
2285 err = PTR_ERR(trans);
Miao Xiec58aaad2013-02-28 10:05:36 +00002286 goto out_release;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002287 }
Miao Xiec58aaad2013-02-28 10:05:36 +00002288 trans->block_rsv = &block_rsv;
2289 trans->bytes_reserved = block_rsv.size;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002290
Yan, Zheng76dda932009-09-21 16:00:26 -04002291 ret = btrfs_unlink_subvol(trans, root, dir,
2292 dest->root_key.objectid,
2293 dentry->d_name.name,
2294 dentry->d_name.len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002295 if (ret) {
2296 err = ret;
2297 btrfs_abort_transaction(trans, root, ret);
2298 goto out_end_trans;
2299 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002300
2301 btrfs_record_root_in_trans(trans, dest);
2302
2303 memset(&dest->root_item.drop_progress, 0,
2304 sizeof(dest->root_item.drop_progress));
2305 dest->root_item.drop_level = 0;
2306 btrfs_set_root_refs(&dest->root_item, 0);
2307
Yan, Zhengd68fc572010-05-16 10:49:58 -04002308 if (!xchg(&dest->orphan_item_inserted, 1)) {
2309 ret = btrfs_insert_orphan_item(trans,
2310 root->fs_info->tree_root,
2311 dest->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002312 if (ret) {
2313 btrfs_abort_transaction(trans, root, ret);
2314 err = ret;
2315 goto out_end_trans;
2316 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002317 }
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002318
2319 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2320 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2321 dest->root_key.objectid);
2322 if (ret && ret != -ENOENT) {
2323 btrfs_abort_transaction(trans, root, ret);
2324 err = ret;
2325 goto out_end_trans;
2326 }
2327 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2328 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2329 dest->root_item.received_uuid,
2330 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2331 dest->root_key.objectid);
2332 if (ret && ret != -ENOENT) {
2333 btrfs_abort_transaction(trans, root, ret);
2334 err = ret;
2335 goto out_end_trans;
2336 }
2337 }
2338
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002339out_end_trans:
Miao Xiec58aaad2013-02-28 10:05:36 +00002340 trans->block_rsv = NULL;
2341 trans->bytes_reserved = 0;
Sage Weil531cb132010-10-29 15:41:32 -04002342 ret = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002343 if (ret && !err)
2344 err = ret;
Yan, Zheng76dda932009-09-21 16:00:26 -04002345 inode->i_flags |= S_DEAD;
Miao Xiec58aaad2013-02-28 10:05:36 +00002346out_release:
2347 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
Yan, Zheng76dda932009-09-21 16:00:26 -04002348out_up_write:
2349 up_write(&root->fs_info->subvol_sem);
2350out_unlock:
2351 mutex_unlock(&inode->i_mutex);
2352 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -04002353 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -04002354 btrfs_invalidate_inodes(dest);
2355 d_delete(dentry);
Liu Bofa6ac872013-02-20 14:10:23 +00002356
2357 /* the last ref */
2358 if (dest->cache_inode) {
2359 iput(dest->cache_inode);
2360 dest->cache_inode = NULL;
2361 }
Yan, Zheng76dda932009-09-21 16:00:26 -04002362 }
2363out_dput:
2364 dput(dentry);
2365out_unlock_dir:
2366 mutex_unlock(&dir->i_mutex);
David Sterbae43f9982013-12-06 17:51:32 +01002367out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05002368 mnt_drop_write_file(file);
Yan, Zheng76dda932009-09-21 16:00:26 -04002369out:
2370 kfree(vol_args);
2371 return err;
2372}
2373
Chris Mason1e701a32010-03-11 09:42:04 -05002374static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002375{
Al Viro496ad9a2013-01-23 17:07:38 -05002376 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002377 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason1e701a32010-03-11 09:42:04 -05002378 struct btrfs_ioctl_defrag_range_args *range;
Yan Zhengc146afa2008-11-12 14:34:12 -05002379 int ret;
2380
Ilya Dryomov25122d12013-01-20 15:57:57 +02002381 ret = mnt_want_write_file(file);
2382 if (ret)
2383 return ret;
Li Zefanb83cc962010-12-20 16:04:08 +08002384
Ilya Dryomov25122d12013-01-20 15:57:57 +02002385 if (btrfs_root_readonly(root)) {
2386 ret = -EROFS;
2387 goto out;
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002388 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002389
2390 switch (inode->i_mode & S_IFMT) {
2391 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -05002392 if (!capable(CAP_SYS_ADMIN)) {
2393 ret = -EPERM;
2394 goto out;
2395 }
Eric Sandeende78b512013-01-31 18:21:12 +00002396 ret = btrfs_defrag_root(root);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002397 if (ret)
2398 goto out;
Eric Sandeende78b512013-01-31 18:21:12 +00002399 ret = btrfs_defrag_root(root->fs_info->extent_root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002400 break;
2401 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -05002402 if (!(file->f_mode & FMODE_WRITE)) {
2403 ret = -EINVAL;
2404 goto out;
2405 }
Chris Mason1e701a32010-03-11 09:42:04 -05002406
2407 range = kzalloc(sizeof(*range), GFP_KERNEL);
2408 if (!range) {
2409 ret = -ENOMEM;
2410 goto out;
2411 }
2412
2413 if (argp) {
2414 if (copy_from_user(range, argp,
2415 sizeof(*range))) {
2416 ret = -EFAULT;
2417 kfree(range);
Dan Carpenter683be162010-03-20 11:24:48 +00002418 goto out;
Chris Mason1e701a32010-03-11 09:42:04 -05002419 }
2420 /* compression requires us to start the IO */
2421 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2422 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2423 range->extent_thresh = (u32)-1;
2424 }
2425 } else {
2426 /* the rest are all set to zero by kzalloc */
2427 range->len = (u64)-1;
2428 }
Al Viro496ad9a2013-01-23 17:07:38 -05002429 ret = btrfs_defrag_file(file_inode(file), file,
Chris Mason4cb53002011-05-24 15:35:30 -04002430 range, 0, 0);
2431 if (ret > 0)
2432 ret = 0;
Chris Mason1e701a32010-03-11 09:42:04 -05002433 kfree(range);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002434 break;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04002435 default:
2436 ret = -EINVAL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002437 }
Chris Masone441d542009-01-05 16:57:23 -05002438out:
Ilya Dryomov25122d12013-01-20 15:57:57 +02002439 mnt_drop_write_file(file);
Chris Masone441d542009-01-05 16:57:23 -05002440 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002441}
2442
Christoph Hellwigb2950862008-12-02 09:54:17 -05002443static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002444{
2445 struct btrfs_ioctl_vol_args *vol_args;
2446 int ret;
2447
Chris Masone441d542009-01-05 16:57:23 -05002448 if (!capable(CAP_SYS_ADMIN))
2449 return -EPERM;
2450
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002451 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2452 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08002453 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002454 }
2455
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002456 mutex_lock(&root->fs_info->volume_mutex);
Li Zefandae7b662009-04-08 15:06:54 +08002457 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002458 if (IS_ERR(vol_args)) {
2459 ret = PTR_ERR(vol_args);
2460 goto out;
2461 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002462
Mark Fasheh5516e592008-07-24 12:20:14 -04002463 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002464 ret = btrfs_init_new_device(root, vol_args->name);
2465
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002466 kfree(vol_args);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002467out:
2468 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002469 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002470 return ret;
2471}
2472
Miao Xieda249272012-11-26 08:44:50 +00002473static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002474{
Al Viro496ad9a2013-01-23 17:07:38 -05002475 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002476 struct btrfs_ioctl_vol_args *vol_args;
2477 int ret;
2478
Chris Masone441d542009-01-05 16:57:23 -05002479 if (!capable(CAP_SYS_ADMIN))
2480 return -EPERM;
2481
Miao Xieda249272012-11-26 08:44:50 +00002482 ret = mnt_want_write_file(file);
2483 if (ret)
2484 return ret;
Yan Zhengc146afa2008-11-12 14:34:12 -05002485
Li Zefandae7b662009-04-08 15:06:54 +08002486 vol_args = memdup_user(arg, sizeof(*vol_args));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002487 if (IS_ERR(vol_args)) {
2488 ret = PTR_ERR(vol_args);
2489 goto out;
2490 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002491
Mark Fasheh5516e592008-07-24 12:20:14 -04002492 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002493
Anand Jain183860f2013-05-17 10:52:45 +00002494 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2495 1)) {
2496 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2497 goto out;
2498 }
2499
2500 mutex_lock(&root->fs_info->volume_mutex);
2501 ret = btrfs_rm_device(root, vol_args->name);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002502 mutex_unlock(&root->fs_info->volume_mutex);
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01002503 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
Anand Jain183860f2013-05-17 10:52:45 +00002504
2505out:
2506 kfree(vol_args);
Ilya Dryomov4ac20c72013-01-20 15:57:57 +02002507 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002508 return ret;
2509}
2510
Jan Schmidt475f6382011-03-11 15:41:01 +01002511static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2512{
Li Zefan027ed2f2011-06-08 08:27:56 +00002513 struct btrfs_ioctl_fs_info_args *fi_args;
Jan Schmidt475f6382011-03-11 15:41:01 +01002514 struct btrfs_device *device;
2515 struct btrfs_device *next;
2516 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Li Zefan027ed2f2011-06-08 08:27:56 +00002517 int ret = 0;
Jan Schmidt475f6382011-03-11 15:41:01 +01002518
2519 if (!capable(CAP_SYS_ADMIN))
2520 return -EPERM;
2521
Li Zefan027ed2f2011-06-08 08:27:56 +00002522 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2523 if (!fi_args)
2524 return -ENOMEM;
2525
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002526 mutex_lock(&fs_devices->device_list_mutex);
Li Zefan027ed2f2011-06-08 08:27:56 +00002527 fi_args->num_devices = fs_devices->num_devices;
2528 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
Jan Schmidt475f6382011-03-11 15:41:01 +01002529
Jan Schmidt475f6382011-03-11 15:41:01 +01002530 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Li Zefan027ed2f2011-06-08 08:27:56 +00002531 if (device->devid > fi_args->max_id)
2532 fi_args->max_id = device->devid;
Jan Schmidt475f6382011-03-11 15:41:01 +01002533 }
2534 mutex_unlock(&fs_devices->device_list_mutex);
2535
Li Zefan027ed2f2011-06-08 08:27:56 +00002536 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2537 ret = -EFAULT;
Jan Schmidt475f6382011-03-11 15:41:01 +01002538
Li Zefan027ed2f2011-06-08 08:27:56 +00002539 kfree(fi_args);
2540 return ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01002541}
2542
2543static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2544{
2545 struct btrfs_ioctl_dev_info_args *di_args;
2546 struct btrfs_device *dev;
2547 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2548 int ret = 0;
2549 char *s_uuid = NULL;
Jan Schmidt475f6382011-03-11 15:41:01 +01002550
2551 if (!capable(CAP_SYS_ADMIN))
2552 return -EPERM;
2553
2554 di_args = memdup_user(arg, sizeof(*di_args));
2555 if (IS_ERR(di_args))
2556 return PTR_ERR(di_args);
2557
Stefan Behrensdd5f9612013-08-15 17:11:20 +02002558 if (!btrfs_is_empty_uuid(di_args->uuid))
Jan Schmidt475f6382011-03-11 15:41:01 +01002559 s_uuid = di_args->uuid;
2560
2561 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002562 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
Jan Schmidt475f6382011-03-11 15:41:01 +01002563
2564 if (!dev) {
2565 ret = -ENODEV;
2566 goto out;
2567 }
2568
2569 di_args->devid = dev->devid;
2570 di_args->bytes_used = dev->bytes_used;
2571 di_args->total_bytes = dev->total_bytes;
2572 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
Jim Meyeringa27202f2012-04-26 18:36:56 +02002573 if (dev->name) {
Josef Bacik606686e2012-06-04 14:03:51 -04002574 struct rcu_string *name;
2575
2576 rcu_read_lock();
2577 name = rcu_dereference(dev->name);
2578 strncpy(di_args->path, name->str, sizeof(di_args->path));
2579 rcu_read_unlock();
Jim Meyeringa27202f2012-04-26 18:36:56 +02002580 di_args->path[sizeof(di_args->path) - 1] = 0;
2581 } else {
Stefan Behrens99ba55a2012-03-19 16:17:22 +01002582 di_args->path[0] = '\0';
Jim Meyeringa27202f2012-04-26 18:36:56 +02002583 }
Jan Schmidt475f6382011-03-11 15:41:01 +01002584
2585out:
David Sterba55793c02013-04-26 15:20:23 +00002586 mutex_unlock(&fs_devices->device_list_mutex);
Jan Schmidt475f6382011-03-11 15:41:01 +01002587 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2588 ret = -EFAULT;
2589
2590 kfree(di_args);
2591 return ret;
2592}
2593
Mark Fasheh416161d2013-08-06 11:42:51 -07002594static struct page *extent_same_get_page(struct inode *inode, u64 off)
2595{
2596 struct page *page;
2597 pgoff_t index;
2598 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2599
2600 index = off >> PAGE_CACHE_SHIFT;
2601
2602 page = grab_cache_page(inode->i_mapping, index);
2603 if (!page)
2604 return NULL;
2605
2606 if (!PageUptodate(page)) {
2607 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2608 0))
2609 return NULL;
2610 lock_page(page);
2611 if (!PageUptodate(page)) {
2612 unlock_page(page);
2613 page_cache_release(page);
2614 return NULL;
2615 }
2616 }
2617 unlock_page(page);
2618
2619 return page;
2620}
2621
Mark Fasheh77fe20dc2013-08-06 11:42:48 -07002622static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2623{
2624 /* do any pending delalloc/csum calc on src, one way or
2625 another, and lock file content */
2626 while (1) {
2627 struct btrfs_ordered_extent *ordered;
2628 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2629 ordered = btrfs_lookup_first_ordered_extent(inode,
2630 off + len - 1);
2631 if (!ordered &&
2632 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2633 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2634 break;
2635 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2636 if (ordered)
2637 btrfs_put_ordered_extent(ordered);
2638 btrfs_wait_ordered_range(inode, off, len);
2639 }
2640}
2641
Mark Fasheh416161d2013-08-06 11:42:51 -07002642static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2643 struct inode *inode2, u64 loff2, u64 len)
2644{
2645 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2646 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2647
2648 mutex_unlock(&inode1->i_mutex);
2649 mutex_unlock(&inode2->i_mutex);
2650}
2651
2652static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2653 struct inode *inode2, u64 loff2, u64 len)
2654{
2655 if (inode1 < inode2) {
2656 swap(inode1, inode2);
2657 swap(loff1, loff2);
2658 }
2659
2660 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2661 lock_extent_range(inode1, loff1, len);
2662 if (inode1 != inode2) {
2663 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2664 lock_extent_range(inode2, loff2, len);
2665 }
2666}
2667
2668static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2669 u64 dst_loff, u64 len)
2670{
2671 int ret = 0;
2672 struct page *src_page, *dst_page;
2673 unsigned int cmp_len = PAGE_CACHE_SIZE;
2674 void *addr, *dst_addr;
2675
2676 while (len) {
2677 if (len < PAGE_CACHE_SIZE)
2678 cmp_len = len;
2679
2680 src_page = extent_same_get_page(src, loff);
2681 if (!src_page)
2682 return -EINVAL;
2683 dst_page = extent_same_get_page(dst, dst_loff);
2684 if (!dst_page) {
2685 page_cache_release(src_page);
2686 return -EINVAL;
2687 }
2688 addr = kmap_atomic(src_page);
2689 dst_addr = kmap_atomic(dst_page);
2690
2691 flush_dcache_page(src_page);
2692 flush_dcache_page(dst_page);
2693
2694 if (memcmp(addr, dst_addr, cmp_len))
2695 ret = BTRFS_SAME_DATA_DIFFERS;
2696
2697 kunmap_atomic(addr);
2698 kunmap_atomic(dst_addr);
2699 page_cache_release(src_page);
2700 page_cache_release(dst_page);
2701
2702 if (ret)
2703 break;
2704
2705 loff += cmp_len;
2706 dst_loff += cmp_len;
2707 len -= cmp_len;
2708 }
2709
2710 return ret;
2711}
2712
2713static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2714{
2715 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2716
2717 if (off + len > inode->i_size || off + len < off)
2718 return -EINVAL;
2719 /* Check that we are block aligned - btrfs_clone() requires this */
2720 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2721 return -EINVAL;
2722
2723 return 0;
2724}
2725
2726static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2727 struct inode *dst, u64 dst_loff)
2728{
2729 int ret;
2730
2731 /*
2732 * btrfs_clone() can't handle extents in the same file
2733 * yet. Once that works, we can drop this check and replace it
2734 * with a check for the same inode, but overlapping extents.
2735 */
2736 if (src == dst)
2737 return -EINVAL;
2738
2739 btrfs_double_lock(src, loff, dst, dst_loff, len);
2740
2741 ret = extent_same_check_offsets(src, loff, len);
2742 if (ret)
2743 goto out_unlock;
2744
2745 ret = extent_same_check_offsets(dst, dst_loff, len);
2746 if (ret)
2747 goto out_unlock;
2748
2749 /* don't make the dst file partly checksummed */
2750 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2751 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2752 ret = -EINVAL;
2753 goto out_unlock;
2754 }
2755
2756 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2757 if (ret == 0)
2758 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2759
2760out_unlock:
2761 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2762
2763 return ret;
2764}
2765
2766#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2767
2768static long btrfs_ioctl_file_extent_same(struct file *file,
2769 void __user *argp)
2770{
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002771 struct btrfs_ioctl_same_args tmp;
2772 struct btrfs_ioctl_same_args *same;
2773 struct btrfs_ioctl_same_extent_info *info;
Mark Fasheh416161d2013-08-06 11:42:51 -07002774 struct inode *src = file->f_dentry->d_inode;
2775 struct file *dst_file = NULL;
2776 struct inode *dst;
2777 u64 off;
2778 u64 len;
2779 int i;
2780 int ret;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002781 unsigned long size;
Mark Fasheh416161d2013-08-06 11:42:51 -07002782 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2783 bool is_admin = capable(CAP_SYS_ADMIN);
2784
2785 if (!(file->f_mode & FMODE_READ))
2786 return -EINVAL;
2787
2788 ret = mnt_want_write_file(file);
2789 if (ret)
2790 return ret;
2791
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002792 if (copy_from_user(&tmp,
Mark Fasheh416161d2013-08-06 11:42:51 -07002793 (struct btrfs_ioctl_same_args __user *)argp,
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002794 sizeof(tmp))) {
Mark Fasheh416161d2013-08-06 11:42:51 -07002795 ret = -EFAULT;
2796 goto out;
2797 }
2798
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002799 size = sizeof(tmp) +
2800 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2801
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002802 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002803
Geyslan G. Bem229eed42013-10-14 12:18:25 -03002804 if (IS_ERR(same)) {
2805 ret = PTR_ERR(same);
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002806 goto out;
2807 }
2808
2809 off = same->logical_offset;
2810 len = same->length;
Mark Fasheh416161d2013-08-06 11:42:51 -07002811
2812 /*
2813 * Limit the total length we will dedupe for each operation.
2814 * This is intended to bound the total time spent in this
2815 * ioctl to something sane.
2816 */
2817 if (len > BTRFS_MAX_DEDUPE_LEN)
2818 len = BTRFS_MAX_DEDUPE_LEN;
2819
2820 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2821 /*
2822 * Btrfs does not support blocksize < page_size. As a
2823 * result, btrfs_cmp_data() won't correctly handle
2824 * this situation without an update.
2825 */
2826 ret = -EINVAL;
2827 goto out;
2828 }
2829
2830 ret = -EISDIR;
2831 if (S_ISDIR(src->i_mode))
2832 goto out;
2833
2834 ret = -EACCES;
2835 if (!S_ISREG(src->i_mode))
2836 goto out;
2837
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002838 /* pre-format output fields to sane values */
2839 for (i = 0; i < same->dest_count; i++) {
2840 same->info[i].bytes_deduped = 0ULL;
2841 same->info[i].status = 0;
2842 }
2843
Mark Fasheh416161d2013-08-06 11:42:51 -07002844 ret = 0;
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002845 for (i = 0; i < same->dest_count; i++) {
2846 info = &same->info[i];
Mark Fasheh416161d2013-08-06 11:42:51 -07002847
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002848 dst_file = fget(info->fd);
Mark Fasheh416161d2013-08-06 11:42:51 -07002849 if (!dst_file) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002850 info->status = -EBADF;
Mark Fasheh416161d2013-08-06 11:42:51 -07002851 goto next;
2852 }
2853
2854 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002855 info->status = -EINVAL;
Mark Fasheh416161d2013-08-06 11:42:51 -07002856 goto next;
2857 }
2858
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002859 info->status = -EXDEV;
Mark Fasheh416161d2013-08-06 11:42:51 -07002860 if (file->f_path.mnt != dst_file->f_path.mnt)
2861 goto next;
2862
2863 dst = dst_file->f_dentry->d_inode;
2864 if (src->i_sb != dst->i_sb)
2865 goto next;
2866
2867 if (S_ISDIR(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002868 info->status = -EISDIR;
Mark Fasheh416161d2013-08-06 11:42:51 -07002869 goto next;
2870 }
2871
2872 if (!S_ISREG(dst->i_mode)) {
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002873 info->status = -EACCES;
Mark Fasheh416161d2013-08-06 11:42:51 -07002874 goto next;
2875 }
2876
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002877 info->status = btrfs_extent_same(src, off, len, dst,
2878 info->logical_offset);
2879 if (info->status == 0)
2880 info->bytes_deduped += len;
Mark Fasheh416161d2013-08-06 11:42:51 -07002881
2882next:
2883 if (dst_file)
2884 fput(dst_file);
Mark Fasheh416161d2013-08-06 11:42:51 -07002885 }
2886
Mark Fashehcbf8b8c2013-09-17 15:43:54 -07002887 ret = copy_to_user(argp, same, size);
2888 if (ret)
2889 ret = -EFAULT;
2890
Mark Fasheh416161d2013-08-06 11:42:51 -07002891out:
2892 mnt_drop_write_file(file);
2893 return ret;
2894}
2895
Mark Fasheh32b7c682013-08-06 11:42:49 -07002896/**
2897 * btrfs_clone() - clone a range from inode file to another
2898 *
2899 * @src: Inode to clone from
2900 * @inode: Inode to clone to
2901 * @off: Offset within source to start clone from
2902 * @olen: Original length, passed by user, of range to clone
2903 * @olen_aligned: Block-aligned value of olen, extent_same uses
2904 * identical values here
2905 * @destoff: Offset within @inode to start clone
2906 */
2907static int btrfs_clone(struct inode *src, struct inode *inode,
2908 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002909{
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002910 struct btrfs_root *root = BTRFS_I(inode)->root;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002911 struct btrfs_path *path = NULL;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002912 struct extent_buffer *leaf;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002913 struct btrfs_trans_handle *trans;
2914 char *buf = NULL;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002915 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002916 u32 nritems;
2917 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002918 int ret;
Mark Fasheh32b7c682013-08-06 11:42:49 -07002919 u64 len = olen_aligned;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002920
2921 ret = -ENOMEM;
2922 buf = vmalloc(btrfs_level_size(root, 0));
2923 if (!buf)
Mark Fasheh32b7c682013-08-06 11:42:49 -07002924 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002925
2926 path = btrfs_alloc_path();
2927 if (!path) {
2928 vfree(buf);
Mark Fasheh32b7c682013-08-06 11:42:49 -07002929 return ret;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002930 }
Mark Fasheh32b7c682013-08-06 11:42:49 -07002931
Yan Zhengae01a0a2008-08-04 23:23:47 -04002932 path->reada = 2;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002933 /* clone data */
Li Zefan33345d012011-04-20 10:31:50 +08002934 key.objectid = btrfs_ino(src);
Yan Zhengae01a0a2008-08-04 23:23:47 -04002935 key.type = BTRFS_EXTENT_DATA_KEY;
2936 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002937
2938 while (1) {
2939 /*
2940 * note the key will change type as we walk through the
2941 * tree.
2942 */
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00002943 path->leave_spinning = 1;
David Sterba362a20c2011-08-01 18:11:57 +02002944 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2945 0, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002946 if (ret < 0)
2947 goto out;
2948
Yan Zhengae01a0a2008-08-04 23:23:47 -04002949 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00002950process_slot:
Yan Zhengae01a0a2008-08-04 23:23:47 -04002951 if (path->slots[0] >= nritems) {
David Sterba362a20c2011-08-01 18:11:57 +02002952 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002953 if (ret < 0)
2954 goto out;
2955 if (ret > 0)
2956 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04002957 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002958 }
2959 leaf = path->nodes[0];
2960 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002961
Yan Zhengae01a0a2008-08-04 23:23:47 -04002962 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05002963 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Li Zefan33345d012011-04-20 10:31:50 +08002964 key.objectid != btrfs_ino(src))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002965 break;
2966
Sage Weilc5c9cd42008-11-12 14:32:25 -05002967 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2968 struct btrfs_file_extent_item *extent;
2969 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04002970 u32 size;
2971 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05002972 u64 disko = 0, diskl = 0;
2973 u64 datao = 0, datal = 0;
2974 u8 comp;
Sage Weilb5384d42010-06-12 22:31:14 +00002975 u64 endoff;
Zheng Yan31840ae2008-09-23 13:14:14 -04002976
Sage Weilc5c9cd42008-11-12 14:32:25 -05002977 extent = btrfs_item_ptr(leaf, slot,
2978 struct btrfs_file_extent_item);
2979 comp = btrfs_file_extent_compression(leaf, extent);
2980 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04002981 if (type == BTRFS_FILE_EXTENT_REG ||
2982 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05002983 disko = btrfs_file_extent_disk_bytenr(leaf,
2984 extent);
2985 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2986 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002987 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05002988 datal = btrfs_file_extent_num_bytes(leaf,
2989 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05002990 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2991 /* take upper bound, may be compressed */
2992 datal = btrfs_file_extent_ram_bytes(leaf,
2993 extent);
2994 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002995
Sage Weil050006a2010-10-29 15:37:33 -04002996 if (key.offset + datal <= off ||
Filipe David Borba Mananae4355f32014-01-13 19:35:01 +00002997 key.offset >= off + len - 1) {
2998 path->slots[0]++;
2999 goto process_slot;
3000 }
3001
3002 size = btrfs_item_size_nr(leaf, slot);
3003 read_extent_buffer(leaf, buf,
3004 btrfs_item_ptr_offset(leaf, slot),
3005 size);
3006
3007 btrfs_release_path(path);
3008 path->leave_spinning = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003009
Zheng Yan31840ae2008-09-23 13:14:14 -04003010 memcpy(&new_key, &key, sizeof(new_key));
Li Zefan33345d012011-04-20 10:31:50 +08003011 new_key.objectid = btrfs_ino(inode);
Li Zefan4d728ec2011-01-26 14:10:43 +08003012 if (off <= key.offset)
3013 new_key.offset = key.offset + destoff - off;
3014 else
3015 new_key.offset = destoff;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003016
Sage Weilb6f34092011-09-20 14:48:51 -04003017 /*
3018 * 1 - adjusting old extent (we may have to split it)
3019 * 1 - add new extent
3020 * 1 - inode update
3021 */
3022 trans = btrfs_start_transaction(root, 3);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003023 if (IS_ERR(trans)) {
3024 ret = PTR_ERR(trans);
3025 goto out;
3026 }
3027
Chris Masonc8a894d2009-06-27 21:07:03 -04003028 if (type == BTRFS_FILE_EXTENT_REG ||
3029 type == BTRFS_FILE_EXTENT_PREALLOC) {
Li Zefand72c0842011-09-11 10:52:25 -04003030 /*
3031 * a | --- range to clone ---| b
3032 * | ------------- extent ------------- |
3033 */
3034
3035 /* substract range b */
3036 if (key.offset + datal > off + len)
3037 datal = off + len - key.offset;
3038
3039 /* substract range a */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003040 if (off > key.offset) {
3041 datao += off - key.offset;
3042 datal -= off - key.offset;
3043 }
3044
Josef Bacik5dc562c2012-08-17 13:14:17 -04003045 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003046 new_key.offset,
3047 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003048 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003049 if (ret) {
3050 btrfs_abort_transaction(trans, root,
3051 ret);
3052 btrfs_end_transaction(trans, root);
3053 goto out;
3054 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003055
Sage Weilc5c9cd42008-11-12 14:32:25 -05003056 ret = btrfs_insert_empty_item(trans, root, path,
3057 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 if (ret) {
3059 btrfs_abort_transaction(trans, root,
3060 ret);
3061 btrfs_end_transaction(trans, root);
3062 goto out;
3063 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003064
3065 leaf = path->nodes[0];
3066 slot = path->slots[0];
3067 write_extent_buffer(leaf, buf,
3068 btrfs_item_ptr_offset(leaf, slot),
3069 size);
3070
3071 extent = btrfs_item_ptr(leaf, slot,
3072 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003073
Sage Weilc5c9cd42008-11-12 14:32:25 -05003074 /* disko == 0 means it's a hole */
3075 if (!disko)
3076 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05003077
3078 btrfs_set_file_extent_offset(leaf, extent,
3079 datao);
3080 btrfs_set_file_extent_num_bytes(leaf, extent,
3081 datal);
3082 if (disko) {
3083 inode_add_bytes(inode, datal);
3084 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003085 disko, diskl, 0,
3086 root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08003087 btrfs_ino(inode),
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003088 new_key.offset - datao,
3089 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003090 if (ret) {
3091 btrfs_abort_transaction(trans,
3092 root,
3093 ret);
3094 btrfs_end_transaction(trans,
3095 root);
3096 goto out;
3097
3098 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003099 }
3100 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3101 u64 skip = 0;
3102 u64 trim = 0;
3103 if (off > key.offset) {
3104 skip = off - key.offset;
3105 new_key.offset += skip;
3106 }
Chris Masond3977122009-01-05 21:25:51 -05003107
Liu Boaa42ffd2012-09-18 03:52:23 -06003108 if (key.offset + datal > off + len)
3109 trim = key.offset + datal - (off + len);
Chris Masond3977122009-01-05 21:25:51 -05003110
Sage Weilc5c9cd42008-11-12 14:32:25 -05003111 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05003112 ret = -EINVAL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003113 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003114 goto out;
3115 }
3116 size -= skip + trim;
3117 datal -= skip + trim;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003118
Josef Bacik5dc562c2012-08-17 13:14:17 -04003119 ret = btrfs_drop_extents(trans, root, inode,
Yan, Zhenga22285a2010-05-16 10:48:46 -04003120 new_key.offset,
3121 new_key.offset + datal,
Josef Bacik26714852012-08-29 12:24:27 -04003122 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003123 if (ret) {
3124 btrfs_abort_transaction(trans, root,
3125 ret);
3126 btrfs_end_transaction(trans, root);
3127 goto out;
3128 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04003129
Sage Weilc5c9cd42008-11-12 14:32:25 -05003130 ret = btrfs_insert_empty_item(trans, root, path,
3131 &new_key, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003132 if (ret) {
3133 btrfs_abort_transaction(trans, root,
3134 ret);
3135 btrfs_end_transaction(trans, root);
3136 goto out;
3137 }
Sage Weilc5c9cd42008-11-12 14:32:25 -05003138
3139 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05003140 u32 start =
3141 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003142 memmove(buf+start, buf+start+skip,
3143 datal);
3144 }
3145
3146 leaf = path->nodes[0];
3147 slot = path->slots[0];
3148 write_extent_buffer(leaf, buf,
3149 btrfs_item_ptr_offset(leaf, slot),
3150 size);
3151 inode_add_bytes(inode, datal);
3152 }
3153
3154 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003155 btrfs_release_path(path);
Sage Weilc5c9cd42008-11-12 14:32:25 -05003156
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003157 inode_inc_iversion(inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003158 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilb5384d42010-06-12 22:31:14 +00003159
3160 /*
3161 * we round up to the block size at eof when
3162 * determining which extents to clone above,
3163 * but shouldn't round up the file size
3164 */
3165 endoff = new_key.offset + datal;
Li Zefan5f3888f2010-11-19 01:36:34 +00003166 if (endoff > destoff+olen)
3167 endoff = destoff+olen;
Sage Weilb5384d42010-06-12 22:31:14 +00003168 if (endoff > inode->i_size)
3169 btrfs_i_size_write(inode, endoff);
3170
Yan, Zhenga22285a2010-05-16 10:48:46 -04003171 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003172 if (ret) {
3173 btrfs_abort_transaction(trans, root, ret);
3174 btrfs_end_transaction(trans, root);
3175 goto out;
3176 }
3177 ret = btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003178 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003179 btrfs_release_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003180 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003181 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003182 ret = 0;
Mark Fasheh32b7c682013-08-06 11:42:49 -07003183
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003184out:
David Sterbab3b4aa72011-04-21 01:20:15 +02003185 btrfs_release_path(path);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003186 btrfs_free_path(path);
3187 vfree(buf);
3188 return ret;
3189}
3190
3191static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3192 u64 off, u64 olen, u64 destoff)
3193{
Al Viro54563d42013-09-01 15:57:51 -04003194 struct inode *inode = file_inode(file);
Mark Fasheh32b7c682013-08-06 11:42:49 -07003195 struct btrfs_root *root = BTRFS_I(inode)->root;
3196 struct fd src_file;
3197 struct inode *src;
3198 int ret;
3199 u64 len = olen;
3200 u64 bs = root->fs_info->sb->s_blocksize;
3201 int same_inode = 0;
3202
3203 /*
3204 * TODO:
3205 * - split compressed inline extents. annoying: we need to
3206 * decompress into destination's address_space (the file offset
3207 * may change, so source mapping won't do), then recompress (or
3208 * otherwise reinsert) a subrange.
3209 * - allow ranges within the same file to be cloned (provided
3210 * they don't overlap)?
3211 */
3212
3213 /* the destination must be opened for writing */
3214 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3215 return -EINVAL;
3216
3217 if (btrfs_root_readonly(root))
3218 return -EROFS;
3219
3220 ret = mnt_want_write_file(file);
3221 if (ret)
3222 return ret;
3223
3224 src_file = fdget(srcfd);
3225 if (!src_file.file) {
3226 ret = -EBADF;
3227 goto out_drop_write;
3228 }
3229
3230 ret = -EXDEV;
3231 if (src_file.file->f_path.mnt != file->f_path.mnt)
3232 goto out_fput;
3233
3234 src = file_inode(src_file.file);
3235
3236 ret = -EINVAL;
3237 if (src == inode)
3238 same_inode = 1;
3239
3240 /* the src must be open for reading */
3241 if (!(src_file.file->f_mode & FMODE_READ))
3242 goto out_fput;
3243
3244 /* don't make the dst file partly checksummed */
3245 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3246 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3247 goto out_fput;
3248
3249 ret = -EISDIR;
3250 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3251 goto out_fput;
3252
3253 ret = -EXDEV;
3254 if (src->i_sb != inode->i_sb)
3255 goto out_fput;
3256
3257 if (!same_inode) {
3258 if (inode < src) {
3259 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3260 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3261 } else {
3262 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3263 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3264 }
3265 } else {
3266 mutex_lock(&src->i_mutex);
3267 }
3268
3269 /* determine range to clone */
3270 ret = -EINVAL;
3271 if (off + len > src->i_size || off + len < off)
3272 goto out_unlock;
3273 if (len == 0)
3274 olen = len = src->i_size - off;
3275 /* if we extend to eof, continue to block boundary */
3276 if (off + len == src->i_size)
3277 len = ALIGN(src->i_size, bs) - off;
3278
3279 /* verify the end result is block aligned */
3280 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3281 !IS_ALIGNED(destoff, bs))
3282 goto out_unlock;
3283
3284 /* verify if ranges are overlapped within the same file */
3285 if (same_inode) {
3286 if (destoff + len > off && destoff < off + len)
3287 goto out_unlock;
3288 }
3289
3290 if (destoff > inode->i_size) {
3291 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3292 if (ret)
3293 goto out_unlock;
3294 }
3295
3296 /* truncate page cache pages from target inode range */
3297 truncate_inode_pages_range(&inode->i_data, destoff,
3298 PAGE_CACHE_ALIGN(destoff + len) - 1);
3299
3300 lock_extent_range(src, off, len);
3301
3302 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3303
Liu Boaa42ffd2012-09-18 03:52:23 -06003304 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003305out_unlock:
Filipe David Borba Mananac57c2b32014-01-11 21:31:25 +00003306 if (!same_inode) {
3307 if (inode < src) {
3308 mutex_unlock(&src->i_mutex);
3309 mutex_unlock(&inode->i_mutex);
3310 } else {
3311 mutex_unlock(&inode->i_mutex);
3312 mutex_unlock(&src->i_mutex);
3313 }
3314 } else {
3315 mutex_unlock(&src->i_mutex);
3316 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003317out_fput:
Al Viro2903ff02012-08-28 12:52:22 -04003318 fdput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05003319out_drop_write:
Al Viro2a79f172011-12-09 08:06:57 -05003320 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003321 return ret;
3322}
3323
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003324static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05003325{
3326 struct btrfs_ioctl_clone_range_args args;
3327
Christoph Hellwig7a865e82008-12-02 09:52:24 -05003328 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05003329 return -EFAULT;
3330 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3331 args.src_length, args.dest_offset);
3332}
3333
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003334/*
3335 * there are many ways the trans_start and trans_end ioctls can lead
3336 * to deadlocks. They should only be used by applications that
3337 * basically own the machine, and have a very in depth understanding
3338 * of all the possible deadlocks and enospc problems.
3339 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003340static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003341{
Al Viro496ad9a2013-01-23 17:07:38 -05003342 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003343 struct btrfs_root *root = BTRFS_I(inode)->root;
3344 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003345 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003346
Sage Weil1ab86ae2009-09-29 18:38:44 -04003347 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04003348 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003349 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003350
3351 ret = -EINPROGRESS;
3352 if (file->private_data)
3353 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003354
Li Zefanb83cc962010-12-20 16:04:08 +08003355 ret = -EROFS;
3356 if (btrfs_root_readonly(root))
3357 goto out;
3358
Al Viroa561be72011-11-23 11:57:51 -05003359 ret = mnt_want_write_file(file);
Yan Zhengc146afa2008-11-12 14:34:12 -05003360 if (ret)
3361 goto out;
3362
Josef Bacika4abeea2011-04-11 17:25:13 -04003363 atomic_inc(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003364
Sage Weil1ab86ae2009-09-29 18:38:44 -04003365 ret = -ENOMEM;
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003366 trans = btrfs_start_ioctl_transaction(root);
Tsutomu Itohabd30bb2011-01-24 00:57:10 +00003367 if (IS_ERR(trans))
Sage Weil1ab86ae2009-09-29 18:38:44 -04003368 goto out_drop;
3369
3370 file->private_data = trans;
3371 return 0;
3372
3373out_drop:
Josef Bacika4abeea2011-04-11 17:25:13 -04003374 atomic_dec(&root->fs_info->open_ioctl_trans);
Al Viro2a79f172011-12-09 08:06:57 -05003375 mnt_drop_write_file(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003376out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003377 return ret;
3378}
3379
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003380static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3381{
Al Viro496ad9a2013-01-23 17:07:38 -05003382 struct inode *inode = file_inode(file);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003383 struct btrfs_root *root = BTRFS_I(inode)->root;
3384 struct btrfs_root *new_root;
3385 struct btrfs_dir_item *di;
3386 struct btrfs_trans_handle *trans;
3387 struct btrfs_path *path;
3388 struct btrfs_key location;
3389 struct btrfs_disk_key disk_key;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003390 u64 objectid = 0;
3391 u64 dir_id;
Miao Xie3c04ce02012-11-26 08:43:07 +00003392 int ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003393
3394 if (!capable(CAP_SYS_ADMIN))
3395 return -EPERM;
3396
Miao Xie3c04ce02012-11-26 08:43:07 +00003397 ret = mnt_want_write_file(file);
3398 if (ret)
3399 return ret;
3400
3401 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3402 ret = -EFAULT;
3403 goto out;
3404 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003405
3406 if (!objectid)
chandan1cecf572013-09-13 19:34:10 +05303407 objectid = BTRFS_FS_TREE_OBJECTID;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003408
3409 location.objectid = objectid;
3410 location.type = BTRFS_ROOT_ITEM_KEY;
3411 location.offset = (u64)-1;
3412
3413 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
Miao Xie3c04ce02012-11-26 08:43:07 +00003414 if (IS_ERR(new_root)) {
3415 ret = PTR_ERR(new_root);
3416 goto out;
3417 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003418
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003419 path = btrfs_alloc_path();
Miao Xie3c04ce02012-11-26 08:43:07 +00003420 if (!path) {
3421 ret = -ENOMEM;
3422 goto out;
3423 }
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003424 path->leave_spinning = 1;
3425
3426 trans = btrfs_start_transaction(root, 1);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003427 if (IS_ERR(trans)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003428 btrfs_free_path(path);
Miao Xie3c04ce02012-11-26 08:43:07 +00003429 ret = PTR_ERR(trans);
3430 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003431 }
3432
David Sterba6c417612011-04-13 15:41:04 +02003433 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003434 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3435 dir_id, "default", 7, 1);
Dan Carpentercf1e99a2010-05-29 09:47:24 +00003436 if (IS_ERR_OR_NULL(di)) {
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003437 btrfs_free_path(path);
3438 btrfs_end_transaction(trans, root);
Frank Holtonefe120a2013-12-20 11:37:06 -05003439 btrfs_err(new_root->fs_info, "Umm, you don't have the default dir"
3440 "item, this isn't going to work");
Miao Xie3c04ce02012-11-26 08:43:07 +00003441 ret = -ENOENT;
3442 goto out;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003443 }
3444
3445 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3446 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3447 btrfs_mark_buffer_dirty(path->nodes[0]);
3448 btrfs_free_path(path);
3449
Mitch Harder2b0ce2c2012-07-24 11:58:43 -06003450 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003451 btrfs_end_transaction(trans, root);
Miao Xie3c04ce02012-11-26 08:43:07 +00003452out:
3453 mnt_drop_write_file(file);
3454 return ret;
Josef Bacik6ef5ed02009-12-11 21:11:29 +00003455}
3456
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003457void btrfs_get_block_group_info(struct list_head *groups_list,
3458 struct btrfs_ioctl_space_info *space)
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003459{
3460 struct btrfs_block_group_cache *block_group;
3461
3462 space->total_bytes = 0;
3463 space->used_bytes = 0;
3464 space->flags = 0;
3465 list_for_each_entry(block_group, groups_list, list) {
3466 space->flags = block_group->flags;
3467 space->total_bytes += block_group->key.offset;
3468 space->used_bytes +=
3469 btrfs_block_group_used(&block_group->item);
3470 }
3471}
3472
Eric Sandeen48a3b632013-04-25 20:41:01 +00003473static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
Josef Bacik1406e432010-01-13 18:19:06 +00003474{
3475 struct btrfs_ioctl_space_args space_args;
3476 struct btrfs_ioctl_space_info space;
3477 struct btrfs_ioctl_space_info *dest;
Chris Mason7fde62b2010-03-16 15:40:10 -04003478 struct btrfs_ioctl_space_info *dest_orig;
Daniel J Blueman13f26962011-04-11 15:56:31 +00003479 struct btrfs_ioctl_space_info __user *user_dest;
Josef Bacik1406e432010-01-13 18:19:06 +00003480 struct btrfs_space_info *info;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003481 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3482 BTRFS_BLOCK_GROUP_SYSTEM,
3483 BTRFS_BLOCK_GROUP_METADATA,
3484 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3485 int num_types = 4;
Chris Mason7fde62b2010-03-16 15:40:10 -04003486 int alloc_size;
Josef Bacik1406e432010-01-13 18:19:06 +00003487 int ret = 0;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003488 u64 slot_count = 0;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003489 int i, c;
Josef Bacik1406e432010-01-13 18:19:06 +00003490
3491 if (copy_from_user(&space_args,
3492 (struct btrfs_ioctl_space_args __user *)arg,
3493 sizeof(space_args)))
3494 return -EFAULT;
3495
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003496 for (i = 0; i < num_types; i++) {
3497 struct btrfs_space_info *tmp;
3498
3499 info = NULL;
3500 rcu_read_lock();
3501 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3502 list) {
3503 if (tmp->flags == types[i]) {
3504 info = tmp;
3505 break;
3506 }
3507 }
3508 rcu_read_unlock();
3509
3510 if (!info)
3511 continue;
3512
3513 down_read(&info->groups_sem);
3514 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3515 if (!list_empty(&info->block_groups[c]))
3516 slot_count++;
3517 }
3518 up_read(&info->groups_sem);
3519 }
Josef Bacik1406e432010-01-13 18:19:06 +00003520
Chris Mason7fde62b2010-03-16 15:40:10 -04003521 /* space_slots == 0 means they are asking for a count */
3522 if (space_args.space_slots == 0) {
3523 space_args.total_spaces = slot_count;
3524 goto out;
3525 }
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003526
Dan Rosenberg51788b12011-02-14 16:04:23 -05003527 slot_count = min_t(u64, space_args.space_slots, slot_count);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003528
Chris Mason7fde62b2010-03-16 15:40:10 -04003529 alloc_size = sizeof(*dest) * slot_count;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003530
Chris Mason7fde62b2010-03-16 15:40:10 -04003531 /* we generally have at most 6 or so space infos, one for each raid
3532 * level. So, a whole page should be more than enough for everyone
3533 */
3534 if (alloc_size > PAGE_CACHE_SIZE)
3535 return -ENOMEM;
3536
3537 space_args.total_spaces = 0;
3538 dest = kmalloc(alloc_size, GFP_NOFS);
3539 if (!dest)
3540 return -ENOMEM;
3541 dest_orig = dest;
3542
3543 /* now we have a buffer to copy into */
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003544 for (i = 0; i < num_types; i++) {
3545 struct btrfs_space_info *tmp;
Chris Mason7fde62b2010-03-16 15:40:10 -04003546
Dan Rosenberg51788b12011-02-14 16:04:23 -05003547 if (!slot_count)
3548 break;
3549
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003550 info = NULL;
3551 rcu_read_lock();
3552 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3553 list) {
3554 if (tmp->flags == types[i]) {
3555 info = tmp;
3556 break;
3557 }
3558 }
3559 rcu_read_unlock();
Chris Mason7fde62b2010-03-16 15:40:10 -04003560
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003561 if (!info)
3562 continue;
3563 down_read(&info->groups_sem);
3564 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3565 if (!list_empty(&info->block_groups[c])) {
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003566 btrfs_get_block_group_info(
3567 &info->block_groups[c], &space);
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003568 memcpy(dest, &space, sizeof(space));
3569 dest++;
3570 space_args.total_spaces++;
Dan Rosenberg51788b12011-02-14 16:04:23 -05003571 slot_count--;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003572 }
Dan Rosenberg51788b12011-02-14 16:04:23 -05003573 if (!slot_count)
3574 break;
Josef Bacikbf5fc0932010-09-29 11:22:36 -04003575 }
3576 up_read(&info->groups_sem);
Josef Bacik1406e432010-01-13 18:19:06 +00003577 }
Josef Bacik1406e432010-01-13 18:19:06 +00003578
Daniel J Blueman2eec6c82012-04-26 00:37:14 +08003579 user_dest = (struct btrfs_ioctl_space_info __user *)
Chris Mason7fde62b2010-03-16 15:40:10 -04003580 (arg + sizeof(struct btrfs_ioctl_space_args));
3581
3582 if (copy_to_user(user_dest, dest_orig, alloc_size))
3583 ret = -EFAULT;
3584
3585 kfree(dest_orig);
3586out:
3587 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
Josef Bacik1406e432010-01-13 18:19:06 +00003588 ret = -EFAULT;
3589
3590 return ret;
3591}
3592
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003593/*
3594 * there are many ways the trans_start and trans_end ioctls can lead
3595 * to deadlocks. They should only be used by applications that
3596 * basically own the machine, and have a very in depth understanding
3597 * of all the possible deadlocks and enospc problems.
3598 */
3599long btrfs_ioctl_trans_end(struct file *file)
3600{
Al Viro496ad9a2013-01-23 17:07:38 -05003601 struct inode *inode = file_inode(file);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003602 struct btrfs_root *root = BTRFS_I(inode)->root;
3603 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003604
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003605 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04003606 if (!trans)
3607 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04003608 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04003609
Sage Weil1ab86ae2009-09-29 18:38:44 -04003610 btrfs_end_transaction(trans, root);
3611
Josef Bacika4abeea2011-04-11 17:25:13 -04003612 atomic_dec(&root->fs_info->open_ioctl_trans);
Sage Weil9ca9ee02008-08-04 10:41:27 -04003613
Al Viro2a79f172011-12-09 08:06:57 -05003614 mnt_drop_write_file(file);
Sage Weil1ab86ae2009-09-29 18:38:44 -04003615 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003616}
3617
Miao Xie9a8c28b2012-11-26 08:40:43 +00003618static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3619 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003620{
Sage Weil46204592010-10-29 15:41:32 -04003621 struct btrfs_trans_handle *trans;
3622 u64 transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003623 int ret;
Sage Weil46204592010-10-29 15:41:32 -04003624
Miao Xied4edf392013-02-20 09:17:06 +00003625 trans = btrfs_attach_transaction_barrier(root);
Miao Xieff7c1d32012-11-26 08:41:29 +00003626 if (IS_ERR(trans)) {
3627 if (PTR_ERR(trans) != -ENOENT)
3628 return PTR_ERR(trans);
3629
3630 /* No running transaction, don't bother */
3631 transid = root->fs_info->last_trans_committed;
3632 goto out;
3633 }
Sage Weil46204592010-10-29 15:41:32 -04003634 transid = trans->transid;
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003635 ret = btrfs_commit_transaction_async(trans, root, 0);
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003636 if (ret) {
3637 btrfs_end_transaction(trans, root);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003638 return ret;
Tsutomu Itoh8b2b2d32011-04-04 01:52:13 +00003639 }
Miao Xieff7c1d32012-11-26 08:41:29 +00003640out:
Sage Weil46204592010-10-29 15:41:32 -04003641 if (argp)
3642 if (copy_to_user(argp, &transid, sizeof(transid)))
3643 return -EFAULT;
3644 return 0;
3645}
3646
Miao Xie9a8c28b2012-11-26 08:40:43 +00003647static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3648 void __user *argp)
Sage Weil46204592010-10-29 15:41:32 -04003649{
Sage Weil46204592010-10-29 15:41:32 -04003650 u64 transid;
3651
3652 if (argp) {
3653 if (copy_from_user(&transid, argp, sizeof(transid)))
3654 return -EFAULT;
3655 } else {
3656 transid = 0; /* current trans */
3657 }
3658 return btrfs_wait_for_commit(root, transid);
3659}
3660
Miao Xieb8e95482012-11-26 08:48:01 +00003661static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
Jan Schmidt475f6382011-03-11 15:41:01 +01003662{
Al Viro496ad9a2013-01-23 17:07:38 -05003663 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt475f6382011-03-11 15:41:01 +01003664 struct btrfs_ioctl_scrub_args *sa;
Miao Xieb8e95482012-11-26 08:48:01 +00003665 int ret;
Jan Schmidt475f6382011-03-11 15:41:01 +01003666
3667 if (!capable(CAP_SYS_ADMIN))
3668 return -EPERM;
3669
3670 sa = memdup_user(arg, sizeof(*sa));
3671 if (IS_ERR(sa))
3672 return PTR_ERR(sa);
3673
Miao Xieb8e95482012-11-26 08:48:01 +00003674 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3675 ret = mnt_want_write_file(file);
3676 if (ret)
3677 goto out;
3678 }
3679
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003680 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
Stefan Behrens63a212a2012-11-05 18:29:28 +01003681 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3682 0);
Jan Schmidt475f6382011-03-11 15:41:01 +01003683
3684 if (copy_to_user(arg, sa, sizeof(*sa)))
3685 ret = -EFAULT;
3686
Miao Xieb8e95482012-11-26 08:48:01 +00003687 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3688 mnt_drop_write_file(file);
3689out:
Jan Schmidt475f6382011-03-11 15:41:01 +01003690 kfree(sa);
3691 return ret;
3692}
3693
3694static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3695{
3696 if (!capable(CAP_SYS_ADMIN))
3697 return -EPERM;
3698
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003699 return btrfs_scrub_cancel(root->fs_info);
Jan Schmidt475f6382011-03-11 15:41:01 +01003700}
3701
3702static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3703 void __user *arg)
3704{
3705 struct btrfs_ioctl_scrub_args *sa;
3706 int ret;
3707
3708 if (!capable(CAP_SYS_ADMIN))
3709 return -EPERM;
3710
3711 sa = memdup_user(arg, sizeof(*sa));
3712 if (IS_ERR(sa))
3713 return PTR_ERR(sa);
3714
3715 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3716
3717 if (copy_to_user(arg, sa, sizeof(*sa)))
3718 ret = -EFAULT;
3719
3720 kfree(sa);
3721 return ret;
3722}
3723
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003724static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06003725 void __user *arg)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003726{
3727 struct btrfs_ioctl_get_dev_stats *sa;
3728 int ret;
3729
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003730 sa = memdup_user(arg, sizeof(*sa));
3731 if (IS_ERR(sa))
3732 return PTR_ERR(sa);
3733
David Sterbab27f7c02012-06-22 06:30:39 -06003734 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3735 kfree(sa);
3736 return -EPERM;
3737 }
3738
3739 ret = btrfs_get_dev_stats(root, sa);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02003740
3741 if (copy_to_user(arg, sa, sizeof(*sa)))
3742 ret = -EFAULT;
3743
3744 kfree(sa);
3745 return ret;
3746}
3747
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003748static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3749{
3750 struct btrfs_ioctl_dev_replace_args *p;
3751 int ret;
3752
3753 if (!capable(CAP_SYS_ADMIN))
3754 return -EPERM;
3755
3756 p = memdup_user(arg, sizeof(*p));
3757 if (IS_ERR(p))
3758 return PTR_ERR(p);
3759
3760 switch (p->cmd) {
3761 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003762 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3763 ret = -EROFS;
3764 goto out;
3765 }
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003766 if (atomic_xchg(
3767 &root->fs_info->mutually_exclusive_operation_running,
3768 1)) {
Anand Jaine57138b2013-08-21 11:44:48 +08003769 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003770 } else {
3771 ret = btrfs_dev_replace_start(root, p);
3772 atomic_set(
3773 &root->fs_info->mutually_exclusive_operation_running,
3774 0);
3775 }
3776 break;
3777 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3778 btrfs_dev_replace_status(root->fs_info, p);
3779 ret = 0;
3780 break;
3781 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3782 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3783 break;
3784 default:
3785 ret = -EINVAL;
3786 break;
3787 }
3788
3789 if (copy_to_user(arg, p, sizeof(*p)))
3790 ret = -EFAULT;
Ilya Dryomovadfa97c2013-10-10 20:39:28 +03003791out:
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01003792 kfree(p);
3793 return ret;
3794}
3795
Jan Schmidtd7728c92011-07-07 16:48:38 +02003796static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3797{
3798 int ret = 0;
3799 int i;
Chris Mason740c3d22011-11-02 15:48:34 -04003800 u64 rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003801 int size;
Chris Mason806468f2011-11-06 03:07:10 -05003802 struct btrfs_ioctl_ino_path_args *ipa = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003803 struct inode_fs_paths *ipath = NULL;
3804 struct btrfs_path *path;
3805
Kusanagi Kouichi82b22ac2013-01-28 11:33:31 +00003806 if (!capable(CAP_DAC_READ_SEARCH))
Jan Schmidtd7728c92011-07-07 16:48:38 +02003807 return -EPERM;
3808
3809 path = btrfs_alloc_path();
3810 if (!path) {
3811 ret = -ENOMEM;
3812 goto out;
3813 }
3814
3815 ipa = memdup_user(arg, sizeof(*ipa));
3816 if (IS_ERR(ipa)) {
3817 ret = PTR_ERR(ipa);
3818 ipa = NULL;
3819 goto out;
3820 }
3821
3822 size = min_t(u32, ipa->size, 4096);
3823 ipath = init_ipath(size, root, path);
3824 if (IS_ERR(ipath)) {
3825 ret = PTR_ERR(ipath);
3826 ipath = NULL;
3827 goto out;
3828 }
3829
3830 ret = paths_from_inode(ipa->inum, ipath);
3831 if (ret < 0)
3832 goto out;
3833
3834 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003835 rel_ptr = ipath->fspath->val[i] -
3836 (u64)(unsigned long)ipath->fspath->val;
Chris Mason740c3d22011-11-02 15:48:34 -04003837 ipath->fspath->val[i] = rel_ptr;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003838 }
3839
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003840 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3841 (void *)(unsigned long)ipath->fspath, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003842 if (ret) {
3843 ret = -EFAULT;
3844 goto out;
3845 }
3846
3847out:
3848 btrfs_free_path(path);
3849 free_ipath(ipath);
3850 kfree(ipa);
3851
3852 return ret;
3853}
3854
3855static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3856{
3857 struct btrfs_data_container *inodes = ctx;
3858 const size_t c = 3 * sizeof(u64);
3859
3860 if (inodes->bytes_left >= c) {
3861 inodes->bytes_left -= c;
3862 inodes->val[inodes->elem_cnt] = inum;
3863 inodes->val[inodes->elem_cnt + 1] = offset;
3864 inodes->val[inodes->elem_cnt + 2] = root;
3865 inodes->elem_cnt += 3;
3866 } else {
3867 inodes->bytes_missing += c - inodes->bytes_left;
3868 inodes->bytes_left = 0;
3869 inodes->elem_missed += 3;
3870 }
3871
3872 return 0;
3873}
3874
3875static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3876 void __user *arg)
3877{
3878 int ret = 0;
3879 int size;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003880 struct btrfs_ioctl_logical_ino_args *loi;
3881 struct btrfs_data_container *inodes = NULL;
3882 struct btrfs_path *path = NULL;
Jan Schmidtd7728c92011-07-07 16:48:38 +02003883
3884 if (!capable(CAP_SYS_ADMIN))
3885 return -EPERM;
3886
3887 loi = memdup_user(arg, sizeof(*loi));
3888 if (IS_ERR(loi)) {
3889 ret = PTR_ERR(loi);
3890 loi = NULL;
3891 goto out;
3892 }
3893
3894 path = btrfs_alloc_path();
3895 if (!path) {
3896 ret = -ENOMEM;
3897 goto out;
3898 }
3899
Liu Bo425d17a2012-09-07 20:01:30 -06003900 size = min_t(u32, loi->size, 64 * 1024);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003901 inodes = init_data_container(size);
3902 if (IS_ERR(inodes)) {
3903 ret = PTR_ERR(inodes);
3904 inodes = NULL;
3905 goto out;
3906 }
3907
Liu Bodf031f02012-09-07 20:01:29 -06003908 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3909 build_ino_list, inodes);
3910 if (ret == -EINVAL)
Jan Schmidtd7728c92011-07-07 16:48:38 +02003911 ret = -ENOENT;
3912 if (ret < 0)
3913 goto out;
3914
Jeff Mahoney745c4d82011-11-20 07:31:57 -05003915 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3916 (void *)(unsigned long)inodes, size);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003917 if (ret)
3918 ret = -EFAULT;
3919
3920out:
3921 btrfs_free_path(path);
Liu Bo425d17a2012-09-07 20:01:30 -06003922 vfree(inodes);
Jan Schmidtd7728c92011-07-07 16:48:38 +02003923 kfree(loi);
3924
3925 return ret;
3926}
3927
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003928void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003929 struct btrfs_ioctl_balance_args *bargs)
3930{
3931 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3932
3933 bargs->flags = bctl->flags;
3934
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003935 if (atomic_read(&fs_info->balance_running))
3936 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3937 if (atomic_read(&fs_info->balance_pause_req))
3938 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003939 if (atomic_read(&fs_info->balance_cancel_req))
3940 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003941
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003942 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3943 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3944 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003945
3946 if (lock) {
3947 spin_lock(&fs_info->balance_lock);
3948 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3949 spin_unlock(&fs_info->balance_lock);
3950 } else {
3951 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3952 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003953}
3954
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003955static long btrfs_ioctl_balance(struct file *file, void __user *arg)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003956{
Al Viro496ad9a2013-01-23 17:07:38 -05003957 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003958 struct btrfs_fs_info *fs_info = root->fs_info;
3959 struct btrfs_ioctl_balance_args *bargs;
3960 struct btrfs_balance_control *bctl;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003961 bool need_unlock; /* for mut. excl. ops lock */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003962 int ret;
3963
3964 if (!capable(CAP_SYS_ADMIN))
3965 return -EPERM;
3966
Liu Boe54bfa32012-06-29 03:58:48 -06003967 ret = mnt_want_write_file(file);
Liu Bo9ba1f6e2012-05-11 18:11:26 +08003968 if (ret)
3969 return ret;
3970
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003971again:
3972 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3973 mutex_lock(&fs_info->volume_mutex);
3974 mutex_lock(&fs_info->balance_mutex);
3975 need_unlock = true;
3976 goto locked;
3977 }
3978
3979 /*
3980 * mut. excl. ops lock is locked. Three possibilites:
3981 * (1) some other op is running
3982 * (2) balance is running
3983 * (3) balance is paused -- special case (think resume)
3984 */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003985 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003986 if (fs_info->balance_ctl) {
3987 /* this is either (2) or (3) */
3988 if (!atomic_read(&fs_info->balance_running)) {
3989 mutex_unlock(&fs_info->balance_mutex);
3990 if (!mutex_trylock(&fs_info->volume_mutex))
3991 goto again;
3992 mutex_lock(&fs_info->balance_mutex);
3993
3994 if (fs_info->balance_ctl &&
3995 !atomic_read(&fs_info->balance_running)) {
3996 /* this is (3) */
3997 need_unlock = false;
3998 goto locked;
3999 }
4000
4001 mutex_unlock(&fs_info->balance_mutex);
4002 mutex_unlock(&fs_info->volume_mutex);
4003 goto again;
4004 } else {
4005 /* this is (2) */
4006 mutex_unlock(&fs_info->balance_mutex);
4007 ret = -EINPROGRESS;
4008 goto out;
4009 }
4010 } else {
4011 /* this is (1) */
4012 mutex_unlock(&fs_info->balance_mutex);
Anand Jaine57138b2013-08-21 11:44:48 +08004013 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004014 goto out;
4015 }
4016
4017locked:
4018 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004019
4020 if (arg) {
4021 bargs = memdup_user(arg, sizeof(*bargs));
4022 if (IS_ERR(bargs)) {
4023 ret = PTR_ERR(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004024 goto out_unlock;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004025 }
Ilya Dryomovde322262012-01-16 22:04:49 +02004026
4027 if (bargs->flags & BTRFS_BALANCE_RESUME) {
4028 if (!fs_info->balance_ctl) {
4029 ret = -ENOTCONN;
4030 goto out_bargs;
4031 }
4032
4033 bctl = fs_info->balance_ctl;
4034 spin_lock(&fs_info->balance_lock);
4035 bctl->flags |= BTRFS_BALANCE_RESUME;
4036 spin_unlock(&fs_info->balance_lock);
4037
4038 goto do_balance;
4039 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004040 } else {
4041 bargs = NULL;
4042 }
4043
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004044 if (fs_info->balance_ctl) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004045 ret = -EINPROGRESS;
4046 goto out_bargs;
4047 }
4048
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004049 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4050 if (!bctl) {
4051 ret = -ENOMEM;
4052 goto out_bargs;
4053 }
4054
4055 bctl->fs_info = fs_info;
4056 if (arg) {
4057 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
4058 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
4059 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
4060
4061 bctl->flags = bargs->flags;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004062 } else {
4063 /* balance everything - no filters */
4064 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004065 }
4066
Ilya Dryomovde322262012-01-16 22:04:49 +02004067do_balance:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004068 /*
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004069 * Ownership of bctl and mutually_exclusive_operation_running
4070 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
4071 * or, if restriper was paused all the way until unmount, in
4072 * free_fs_info. mutually_exclusive_operation_running is
4073 * cleared in __cancel_balance.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004074 */
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004075 need_unlock = false;
4076
4077 ret = btrfs_balance(bctl, bargs);
4078
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004079 if (arg) {
4080 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4081 ret = -EFAULT;
4082 }
4083
4084out_bargs:
4085 kfree(bargs);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004086out_unlock:
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004087 mutex_unlock(&fs_info->balance_mutex);
4088 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004089 if (need_unlock)
4090 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
4091out:
Liu Boe54bfa32012-06-29 03:58:48 -06004092 mnt_drop_write_file(file);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004093 return ret;
4094}
4095
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004096static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4097{
4098 if (!capable(CAP_SYS_ADMIN))
4099 return -EPERM;
4100
4101 switch (cmd) {
4102 case BTRFS_BALANCE_CTL_PAUSE:
4103 return btrfs_pause_balance(root->fs_info);
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004104 case BTRFS_BALANCE_CTL_CANCEL:
4105 return btrfs_cancel_balance(root->fs_info);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004106 }
4107
4108 return -EINVAL;
4109}
4110
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004111static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4112 void __user *arg)
4113{
4114 struct btrfs_fs_info *fs_info = root->fs_info;
4115 struct btrfs_ioctl_balance_args *bargs;
4116 int ret = 0;
4117
4118 if (!capable(CAP_SYS_ADMIN))
4119 return -EPERM;
4120
4121 mutex_lock(&fs_info->balance_mutex);
4122 if (!fs_info->balance_ctl) {
4123 ret = -ENOTCONN;
4124 goto out;
4125 }
4126
4127 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4128 if (!bargs) {
4129 ret = -ENOMEM;
4130 goto out;
4131 }
4132
4133 update_ioctl_balance_args(fs_info, 1, bargs);
4134
4135 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4136 ret = -EFAULT;
4137
4138 kfree(bargs);
4139out:
4140 mutex_unlock(&fs_info->balance_mutex);
4141 return ret;
4142}
4143
Miao Xie905b0dd2012-11-26 08:50:11 +00004144static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004145{
Al Viro496ad9a2013-01-23 17:07:38 -05004146 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004147 struct btrfs_ioctl_quota_ctl_args *sa;
4148 struct btrfs_trans_handle *trans = NULL;
4149 int ret;
4150 int err;
4151
4152 if (!capable(CAP_SYS_ADMIN))
4153 return -EPERM;
4154
Miao Xie905b0dd2012-11-26 08:50:11 +00004155 ret = mnt_want_write_file(file);
4156 if (ret)
4157 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004158
4159 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004160 if (IS_ERR(sa)) {
4161 ret = PTR_ERR(sa);
4162 goto drop_write;
4163 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004164
Wang Shilong7708f022013-04-07 10:24:57 +00004165 down_write(&root->fs_info->subvol_sem);
Jan Schmidt2f232032013-04-25 16:04:51 +00004166 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4167 if (IS_ERR(trans)) {
4168 ret = PTR_ERR(trans);
4169 goto out;
Arne Jansen5d13a372011-09-14 15:53:51 +02004170 }
4171
4172 switch (sa->cmd) {
4173 case BTRFS_QUOTA_CTL_ENABLE:
4174 ret = btrfs_quota_enable(trans, root->fs_info);
4175 break;
4176 case BTRFS_QUOTA_CTL_DISABLE:
4177 ret = btrfs_quota_disable(trans, root->fs_info);
4178 break;
Arne Jansen5d13a372011-09-14 15:53:51 +02004179 default:
4180 ret = -EINVAL;
4181 break;
4182 }
4183
Jan Schmidt2f232032013-04-25 16:04:51 +00004184 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4185 if (err && !ret)
4186 ret = err;
Arne Jansen5d13a372011-09-14 15:53:51 +02004187out:
4188 kfree(sa);
Wang Shilong7708f022013-04-07 10:24:57 +00004189 up_write(&root->fs_info->subvol_sem);
Miao Xie905b0dd2012-11-26 08:50:11 +00004190drop_write:
4191 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004192 return ret;
4193}
4194
Miao Xie905b0dd2012-11-26 08:50:11 +00004195static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004196{
Al Viro496ad9a2013-01-23 17:07:38 -05004197 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004198 struct btrfs_ioctl_qgroup_assign_args *sa;
4199 struct btrfs_trans_handle *trans;
4200 int ret;
4201 int err;
4202
4203 if (!capable(CAP_SYS_ADMIN))
4204 return -EPERM;
4205
Miao Xie905b0dd2012-11-26 08:50:11 +00004206 ret = mnt_want_write_file(file);
4207 if (ret)
4208 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004209
4210 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004211 if (IS_ERR(sa)) {
4212 ret = PTR_ERR(sa);
4213 goto drop_write;
4214 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004215
4216 trans = btrfs_join_transaction(root);
4217 if (IS_ERR(trans)) {
4218 ret = PTR_ERR(trans);
4219 goto out;
4220 }
4221
4222 /* FIXME: check if the IDs really exist */
4223 if (sa->assign) {
4224 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4225 sa->src, sa->dst);
4226 } else {
4227 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4228 sa->src, sa->dst);
4229 }
4230
4231 err = btrfs_end_transaction(trans, root);
4232 if (err && !ret)
4233 ret = err;
4234
4235out:
4236 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004237drop_write:
4238 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004239 return ret;
4240}
4241
Miao Xie905b0dd2012-11-26 08:50:11 +00004242static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004243{
Al Viro496ad9a2013-01-23 17:07:38 -05004244 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004245 struct btrfs_ioctl_qgroup_create_args *sa;
4246 struct btrfs_trans_handle *trans;
4247 int ret;
4248 int err;
4249
4250 if (!capable(CAP_SYS_ADMIN))
4251 return -EPERM;
4252
Miao Xie905b0dd2012-11-26 08:50:11 +00004253 ret = mnt_want_write_file(file);
4254 if (ret)
4255 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004256
4257 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004258 if (IS_ERR(sa)) {
4259 ret = PTR_ERR(sa);
4260 goto drop_write;
4261 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004262
Miao Xied86e56c2012-11-15 11:35:41 +00004263 if (!sa->qgroupid) {
4264 ret = -EINVAL;
4265 goto out;
4266 }
4267
Arne Jansen5d13a372011-09-14 15:53:51 +02004268 trans = btrfs_join_transaction(root);
4269 if (IS_ERR(trans)) {
4270 ret = PTR_ERR(trans);
4271 goto out;
4272 }
4273
4274 /* FIXME: check if the IDs really exist */
4275 if (sa->create) {
4276 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4277 NULL);
4278 } else {
4279 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4280 }
4281
4282 err = btrfs_end_transaction(trans, root);
4283 if (err && !ret)
4284 ret = err;
4285
4286out:
4287 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004288drop_write:
4289 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004290 return ret;
4291}
4292
Miao Xie905b0dd2012-11-26 08:50:11 +00004293static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
Arne Jansen5d13a372011-09-14 15:53:51 +02004294{
Al Viro496ad9a2013-01-23 17:07:38 -05004295 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Arne Jansen5d13a372011-09-14 15:53:51 +02004296 struct btrfs_ioctl_qgroup_limit_args *sa;
4297 struct btrfs_trans_handle *trans;
4298 int ret;
4299 int err;
4300 u64 qgroupid;
4301
4302 if (!capable(CAP_SYS_ADMIN))
4303 return -EPERM;
4304
Miao Xie905b0dd2012-11-26 08:50:11 +00004305 ret = mnt_want_write_file(file);
4306 if (ret)
4307 return ret;
Arne Jansen5d13a372011-09-14 15:53:51 +02004308
4309 sa = memdup_user(arg, sizeof(*sa));
Miao Xie905b0dd2012-11-26 08:50:11 +00004310 if (IS_ERR(sa)) {
4311 ret = PTR_ERR(sa);
4312 goto drop_write;
4313 }
Arne Jansen5d13a372011-09-14 15:53:51 +02004314
4315 trans = btrfs_join_transaction(root);
4316 if (IS_ERR(trans)) {
4317 ret = PTR_ERR(trans);
4318 goto out;
4319 }
4320
4321 qgroupid = sa->qgroupid;
4322 if (!qgroupid) {
4323 /* take the current subvol as qgroup */
4324 qgroupid = root->root_key.objectid;
4325 }
4326
4327 /* FIXME: check if the IDs really exist */
4328 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4329
4330 err = btrfs_end_transaction(trans, root);
4331 if (err && !ret)
4332 ret = err;
4333
4334out:
4335 kfree(sa);
Miao Xie905b0dd2012-11-26 08:50:11 +00004336drop_write:
4337 mnt_drop_write_file(file);
Arne Jansen5d13a372011-09-14 15:53:51 +02004338 return ret;
4339}
4340
Jan Schmidt2f232032013-04-25 16:04:51 +00004341static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4342{
Al Viro6d0379e2013-06-16 19:32:35 +04004343 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004344 struct btrfs_ioctl_quota_rescan_args *qsa;
4345 int ret;
4346
4347 if (!capable(CAP_SYS_ADMIN))
4348 return -EPERM;
4349
4350 ret = mnt_want_write_file(file);
4351 if (ret)
4352 return ret;
4353
4354 qsa = memdup_user(arg, sizeof(*qsa));
4355 if (IS_ERR(qsa)) {
4356 ret = PTR_ERR(qsa);
4357 goto drop_write;
4358 }
4359
4360 if (qsa->flags) {
4361 ret = -EINVAL;
4362 goto out;
4363 }
4364
4365 ret = btrfs_qgroup_rescan(root->fs_info);
4366
4367out:
4368 kfree(qsa);
4369drop_write:
4370 mnt_drop_write_file(file);
4371 return ret;
4372}
4373
4374static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4375{
Al Viro6d0379e2013-06-16 19:32:35 +04004376 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt2f232032013-04-25 16:04:51 +00004377 struct btrfs_ioctl_quota_rescan_args *qsa;
4378 int ret = 0;
4379
4380 if (!capable(CAP_SYS_ADMIN))
4381 return -EPERM;
4382
4383 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4384 if (!qsa)
4385 return -ENOMEM;
4386
4387 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4388 qsa->flags = 1;
4389 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4390 }
4391
4392 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4393 ret = -EFAULT;
4394
4395 kfree(qsa);
4396 return ret;
4397}
4398
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004399static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4400{
Al Viro54563d42013-09-01 15:57:51 -04004401 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004402
4403 if (!capable(CAP_SYS_ADMIN))
4404 return -EPERM;
4405
4406 return btrfs_qgroup_wait_for_completion(root->fs_info);
4407}
4408
Hugo Millsabccd002014-01-30 20:17:00 +00004409static long _btrfs_ioctl_set_received_subvol(struct file *file,
4410 struct btrfs_ioctl_received_subvol_args *sa)
Alexander Block8ea05e32012-07-25 17:35:53 +02004411{
Al Viro496ad9a2013-01-23 17:07:38 -05004412 struct inode *inode = file_inode(file);
Alexander Block8ea05e32012-07-25 17:35:53 +02004413 struct btrfs_root *root = BTRFS_I(inode)->root;
4414 struct btrfs_root_item *root_item = &root->root_item;
4415 struct btrfs_trans_handle *trans;
4416 struct timespec ct = CURRENT_TIME;
4417 int ret = 0;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004418 int received_uuid_changed;
Alexander Block8ea05e32012-07-25 17:35:53 +02004419
David Sterbabd60ea02014-01-16 15:50:22 +01004420 if (!inode_owner_or_capable(inode))
4421 return -EPERM;
4422
Alexander Block8ea05e32012-07-25 17:35:53 +02004423 ret = mnt_want_write_file(file);
4424 if (ret < 0)
4425 return ret;
4426
4427 down_write(&root->fs_info->subvol_sem);
4428
4429 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4430 ret = -EINVAL;
4431 goto out;
4432 }
4433
4434 if (btrfs_root_readonly(root)) {
4435 ret = -EROFS;
4436 goto out;
4437 }
4438
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004439 /*
4440 * 1 - root item
4441 * 2 - uuid items (received uuid + subvol uuid)
4442 */
4443 trans = btrfs_start_transaction(root, 3);
Alexander Block8ea05e32012-07-25 17:35:53 +02004444 if (IS_ERR(trans)) {
4445 ret = PTR_ERR(trans);
4446 trans = NULL;
4447 goto out;
4448 }
4449
4450 sa->rtransid = trans->transid;
4451 sa->rtime.sec = ct.tv_sec;
4452 sa->rtime.nsec = ct.tv_nsec;
4453
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004454 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4455 BTRFS_UUID_SIZE);
4456 if (received_uuid_changed &&
4457 !btrfs_is_empty_uuid(root_item->received_uuid))
4458 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4459 root_item->received_uuid,
4460 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4461 root->root_key.objectid);
Alexander Block8ea05e32012-07-25 17:35:53 +02004462 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4463 btrfs_set_root_stransid(root_item, sa->stransid);
4464 btrfs_set_root_rtransid(root_item, sa->rtransid);
Qu Wenruo3cae2102013-07-16 11:19:18 +08004465 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4466 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4467 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4468 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
Alexander Block8ea05e32012-07-25 17:35:53 +02004469
4470 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4471 &root->root_key, &root->root_item);
4472 if (ret < 0) {
4473 btrfs_end_transaction(trans, root);
Alexander Block8ea05e32012-07-25 17:35:53 +02004474 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004475 }
4476 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4477 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4478 sa->uuid,
4479 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4480 root->root_key.objectid);
4481 if (ret < 0 && ret != -EEXIST) {
4482 btrfs_abort_transaction(trans, root, ret);
Alexander Block8ea05e32012-07-25 17:35:53 +02004483 goto out;
Stefan Behrensdd5f9612013-08-15 17:11:20 +02004484 }
4485 }
4486 ret = btrfs_commit_transaction(trans, root);
4487 if (ret < 0) {
4488 btrfs_abort_transaction(trans, root, ret);
4489 goto out;
Alexander Block8ea05e32012-07-25 17:35:53 +02004490 }
4491
Hugo Millsabccd002014-01-30 20:17:00 +00004492out:
4493 up_write(&root->fs_info->subvol_sem);
4494 mnt_drop_write_file(file);
4495 return ret;
4496}
4497
4498#ifdef CONFIG_64BIT
4499static long btrfs_ioctl_set_received_subvol_32(struct file *file,
4500 void __user *arg)
4501{
4502 struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL;
4503 struct btrfs_ioctl_received_subvol_args *args64 = NULL;
4504 int ret = 0;
4505
4506 args32 = memdup_user(arg, sizeof(*args32));
4507 if (IS_ERR(args32)) {
4508 ret = PTR_ERR(args32);
4509 args32 = NULL;
4510 goto out;
4511 }
4512
4513 args64 = kmalloc(sizeof(*args64), GFP_NOFS);
4514 if (IS_ERR(args64)) {
4515 ret = PTR_ERR(args64);
4516 args64 = NULL;
4517 goto out;
4518 }
4519
4520 memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE);
4521 args64->stransid = args32->stransid;
4522 args64->rtransid = args32->rtransid;
4523 args64->stime.sec = args32->stime.sec;
4524 args64->stime.nsec = args32->stime.nsec;
4525 args64->rtime.sec = args32->rtime.sec;
4526 args64->rtime.nsec = args32->rtime.nsec;
4527 args64->flags = args32->flags;
4528
4529 ret = _btrfs_ioctl_set_received_subvol(file, args64);
4530 if (ret)
4531 goto out;
4532
4533 memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE);
4534 args32->stransid = args64->stransid;
4535 args32->rtransid = args64->rtransid;
4536 args32->stime.sec = args64->stime.sec;
4537 args32->stime.nsec = args64->stime.nsec;
4538 args32->rtime.sec = args64->rtime.sec;
4539 args32->rtime.nsec = args64->rtime.nsec;
4540 args32->flags = args64->flags;
4541
4542 ret = copy_to_user(arg, args32, sizeof(*args32));
4543 if (ret)
4544 ret = -EFAULT;
4545
4546out:
4547 kfree(args32);
4548 kfree(args64);
4549 return ret;
4550}
4551#endif
4552
4553static long btrfs_ioctl_set_received_subvol(struct file *file,
4554 void __user *arg)
4555{
4556 struct btrfs_ioctl_received_subvol_args *sa = NULL;
4557 int ret = 0;
4558
4559 sa = memdup_user(arg, sizeof(*sa));
4560 if (IS_ERR(sa)) {
4561 ret = PTR_ERR(sa);
4562 sa = NULL;
4563 goto out;
4564 }
4565
4566 ret = _btrfs_ioctl_set_received_subvol(file, sa);
4567
4568 if (ret)
4569 goto out;
4570
Alexander Block8ea05e32012-07-25 17:35:53 +02004571 ret = copy_to_user(arg, sa, sizeof(*sa));
4572 if (ret)
4573 ret = -EFAULT;
4574
4575out:
4576 kfree(sa);
Alexander Block8ea05e32012-07-25 17:35:53 +02004577 return ret;
4578}
4579
jeff.liu867ab662013-01-05 02:48:01 +00004580static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4581{
Al Viro6d0379e2013-06-16 19:32:35 +04004582 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004583 size_t len;
jeff.liu867ab662013-01-05 02:48:01 +00004584 int ret;
Anand Jaina1b83ac2013-07-19 17:39:32 +08004585 char label[BTRFS_LABEL_SIZE];
4586
4587 spin_lock(&root->fs_info->super_lock);
4588 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4589 spin_unlock(&root->fs_info->super_lock);
4590
4591 len = strnlen(label, BTRFS_LABEL_SIZE);
jeff.liu867ab662013-01-05 02:48:01 +00004592
4593 if (len == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004594 btrfs_warn(root->fs_info,
4595 "label is too long, return the first %zu bytes", --len);
jeff.liu867ab662013-01-05 02:48:01 +00004596 }
4597
jeff.liu867ab662013-01-05 02:48:01 +00004598 ret = copy_to_user(arg, label, len);
jeff.liu867ab662013-01-05 02:48:01 +00004599
4600 return ret ? -EFAULT : 0;
4601}
4602
jeff.liua8bfd4a2013-01-05 02:48:08 +00004603static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4604{
Al Viro6d0379e2013-06-16 19:32:35 +04004605 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
jeff.liua8bfd4a2013-01-05 02:48:08 +00004606 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4607 struct btrfs_trans_handle *trans;
4608 char label[BTRFS_LABEL_SIZE];
4609 int ret;
4610
4611 if (!capable(CAP_SYS_ADMIN))
4612 return -EPERM;
4613
4614 if (copy_from_user(label, arg, sizeof(label)))
4615 return -EFAULT;
4616
4617 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004618 btrfs_err(root->fs_info, "unable to set label with more than %d bytes",
jeff.liua8bfd4a2013-01-05 02:48:08 +00004619 BTRFS_LABEL_SIZE - 1);
4620 return -EINVAL;
4621 }
4622
4623 ret = mnt_want_write_file(file);
4624 if (ret)
4625 return ret;
4626
jeff.liua8bfd4a2013-01-05 02:48:08 +00004627 trans = btrfs_start_transaction(root, 0);
4628 if (IS_ERR(trans)) {
4629 ret = PTR_ERR(trans);
4630 goto out_unlock;
4631 }
4632
Anand Jaina1b83ac2013-07-19 17:39:32 +08004633 spin_lock(&root->fs_info->super_lock);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004634 strcpy(super_block->label, label);
Anand Jaina1b83ac2013-07-19 17:39:32 +08004635 spin_unlock(&root->fs_info->super_lock);
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01004636 ret = btrfs_commit_transaction(trans, root);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004637
4638out_unlock:
jeff.liua8bfd4a2013-01-05 02:48:08 +00004639 mnt_drop_write_file(file);
4640 return ret;
4641}
4642
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004643#define INIT_FEATURE_FLAGS(suffix) \
4644 { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \
4645 .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \
4646 .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix }
4647
4648static int btrfs_ioctl_get_supported_features(struct file *file,
4649 void __user *arg)
4650{
4651 static struct btrfs_ioctl_feature_flags features[3] = {
4652 INIT_FEATURE_FLAGS(SUPP),
4653 INIT_FEATURE_FLAGS(SAFE_SET),
4654 INIT_FEATURE_FLAGS(SAFE_CLEAR)
4655 };
4656
4657 if (copy_to_user(arg, &features, sizeof(features)))
4658 return -EFAULT;
4659
4660 return 0;
4661}
4662
4663static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
4664{
4665 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4666 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4667 struct btrfs_ioctl_feature_flags features;
4668
4669 features.compat_flags = btrfs_super_compat_flags(super_block);
4670 features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block);
4671 features.incompat_flags = btrfs_super_incompat_flags(super_block);
4672
4673 if (copy_to_user(arg, &features, sizeof(features)))
4674 return -EFAULT;
4675
4676 return 0;
4677}
4678
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004679static int check_feature_bits(struct btrfs_root *root,
4680 enum btrfs_feature_set set,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004681 u64 change_mask, u64 flags, u64 supported_flags,
4682 u64 safe_set, u64 safe_clear)
4683{
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004684 const char *type = btrfs_feature_set_names[set];
4685 char *names;
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004686 u64 disallowed, unsupported;
4687 u64 set_mask = flags & change_mask;
4688 u64 clear_mask = ~flags & change_mask;
4689
4690 unsupported = set_mask & ~supported_flags;
4691 if (unsupported) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004692 names = btrfs_printable_features(set, unsupported);
4693 if (names) {
4694 btrfs_warn(root->fs_info,
4695 "this kernel does not support the %s feature bit%s",
4696 names, strchr(names, ',') ? "s" : "");
4697 kfree(names);
4698 } else
4699 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004700 "this kernel does not support %s bits 0x%llx",
4701 type, unsupported);
4702 return -EOPNOTSUPP;
4703 }
4704
4705 disallowed = set_mask & ~safe_set;
4706 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004707 names = btrfs_printable_features(set, disallowed);
4708 if (names) {
4709 btrfs_warn(root->fs_info,
4710 "can't set the %s feature bit%s while mounted",
4711 names, strchr(names, ',') ? "s" : "");
4712 kfree(names);
4713 } else
4714 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004715 "can't set %s bits 0x%llx while mounted",
4716 type, disallowed);
4717 return -EPERM;
4718 }
4719
4720 disallowed = clear_mask & ~safe_clear;
4721 if (disallowed) {
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004722 names = btrfs_printable_features(set, disallowed);
4723 if (names) {
4724 btrfs_warn(root->fs_info,
4725 "can't clear the %s feature bit%s while mounted",
4726 names, strchr(names, ',') ? "s" : "");
4727 kfree(names);
4728 } else
4729 btrfs_warn(root->fs_info,
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004730 "can't clear %s bits 0x%llx while mounted",
4731 type, disallowed);
4732 return -EPERM;
4733 }
4734
4735 return 0;
4736}
4737
4738#define check_feature(root, change_mask, flags, mask_base) \
Jeff Mahoney3b02a682013-11-01 13:07:02 -04004739check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004740 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
4741 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
4742 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
4743
4744static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
4745{
4746 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4747 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4748 struct btrfs_ioctl_feature_flags flags[2];
4749 struct btrfs_trans_handle *trans;
4750 u64 newflags;
4751 int ret;
4752
4753 if (!capable(CAP_SYS_ADMIN))
4754 return -EPERM;
4755
4756 if (copy_from_user(flags, arg, sizeof(flags)))
4757 return -EFAULT;
4758
4759 /* Nothing to do */
4760 if (!flags[0].compat_flags && !flags[0].compat_ro_flags &&
4761 !flags[0].incompat_flags)
4762 return 0;
4763
4764 ret = check_feature(root, flags[0].compat_flags,
4765 flags[1].compat_flags, COMPAT);
4766 if (ret)
4767 return ret;
4768
4769 ret = check_feature(root, flags[0].compat_ro_flags,
4770 flags[1].compat_ro_flags, COMPAT_RO);
4771 if (ret)
4772 return ret;
4773
4774 ret = check_feature(root, flags[0].incompat_flags,
4775 flags[1].incompat_flags, INCOMPAT);
4776 if (ret)
4777 return ret;
4778
David Sterba8051aa12014-02-07 14:34:04 +01004779 trans = btrfs_start_transaction(root, 0);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004780 if (IS_ERR(trans))
4781 return PTR_ERR(trans);
4782
4783 spin_lock(&root->fs_info->super_lock);
4784 newflags = btrfs_super_compat_flags(super_block);
4785 newflags |= flags[0].compat_flags & flags[1].compat_flags;
4786 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
4787 btrfs_set_super_compat_flags(super_block, newflags);
4788
4789 newflags = btrfs_super_compat_ro_flags(super_block);
4790 newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags;
4791 newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags);
4792 btrfs_set_super_compat_ro_flags(super_block, newflags);
4793
4794 newflags = btrfs_super_incompat_flags(super_block);
4795 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
4796 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
4797 btrfs_set_super_incompat_flags(super_block, newflags);
4798 spin_unlock(&root->fs_info->super_lock);
4799
Jeff Mahoneyd0270ac2014-02-07 14:33:57 +01004800 return btrfs_commit_transaction(trans, root);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004801}
4802
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004803long btrfs_ioctl(struct file *file, unsigned int
4804 cmd, unsigned long arg)
4805{
Al Viro496ad9a2013-01-23 17:07:38 -05004806 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004807 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004808
4809 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004810 case FS_IOC_GETFLAGS:
4811 return btrfs_ioctl_getflags(file, argp);
4812 case FS_IOC_SETFLAGS:
4813 return btrfs_ioctl_setflags(file, argp);
4814 case FS_IOC_GETVERSION:
4815 return btrfs_ioctl_getversion(file, argp);
Li Dongyangf7039b12011-03-24 10:24:28 +00004816 case FITRIM:
4817 return btrfs_ioctl_fitrim(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004818 case BTRFS_IOC_SNAP_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004819 return btrfs_ioctl_snap_create(file, argp, 0);
Li Zefanfdfb1e42010-12-10 06:41:56 +00004820 case BTRFS_IOC_SNAP_CREATE_V2:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004821 return btrfs_ioctl_snap_create_v2(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05004822 case BTRFS_IOC_SUBVOL_CREATE:
Li Zefanfa0d2b92010-12-20 15:53:28 +08004823 return btrfs_ioctl_snap_create(file, argp, 1);
Arne Jansen6f72c7e2011-09-14 15:58:21 +02004824 case BTRFS_IOC_SUBVOL_CREATE_V2:
4825 return btrfs_ioctl_snap_create_v2(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04004826 case BTRFS_IOC_SNAP_DESTROY:
4827 return btrfs_ioctl_snap_destroy(file, argp);
Li Zefan0caa1022010-12-20 16:30:25 +08004828 case BTRFS_IOC_SUBVOL_GETFLAGS:
4829 return btrfs_ioctl_subvol_getflags(file, argp);
4830 case BTRFS_IOC_SUBVOL_SETFLAGS:
4831 return btrfs_ioctl_subvol_setflags(file, argp);
Josef Bacik6ef5ed02009-12-11 21:11:29 +00004832 case BTRFS_IOC_DEFAULT_SUBVOL:
4833 return btrfs_ioctl_default_subvol(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004834 case BTRFS_IOC_DEFRAG:
Chris Mason1e701a32010-03-11 09:42:04 -05004835 return btrfs_ioctl_defrag(file, NULL);
4836 case BTRFS_IOC_DEFRAG_RANGE:
4837 return btrfs_ioctl_defrag(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004838 case BTRFS_IOC_RESIZE:
Miao Xie198605a2012-11-26 08:43:45 +00004839 return btrfs_ioctl_resize(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004840 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05004841 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004842 case BTRFS_IOC_RM_DEV:
Miao Xieda249272012-11-26 08:44:50 +00004843 return btrfs_ioctl_rm_dev(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004844 case BTRFS_IOC_FS_INFO:
4845 return btrfs_ioctl_fs_info(root, argp);
4846 case BTRFS_IOC_DEV_INFO:
4847 return btrfs_ioctl_dev_info(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004848 case BTRFS_IOC_BALANCE:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004849 return btrfs_ioctl_balance(file, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004850 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05004851 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4852 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05004853 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004854 case BTRFS_IOC_TRANS_START:
4855 return btrfs_ioctl_trans_start(file);
4856 case BTRFS_IOC_TRANS_END:
4857 return btrfs_ioctl_trans_end(file);
Chris Masonac8e9812010-02-28 15:39:26 -05004858 case BTRFS_IOC_TREE_SEARCH:
4859 return btrfs_ioctl_tree_search(file, argp);
4860 case BTRFS_IOC_INO_LOOKUP:
4861 return btrfs_ioctl_ino_lookup(file, argp);
Jan Schmidtd7728c92011-07-07 16:48:38 +02004862 case BTRFS_IOC_INO_PATHS:
4863 return btrfs_ioctl_ino_to_path(root, argp);
4864 case BTRFS_IOC_LOGICAL_INO:
4865 return btrfs_ioctl_logical_to_ino(root, argp);
Josef Bacik1406e432010-01-13 18:19:06 +00004866 case BTRFS_IOC_SPACE_INFO:
4867 return btrfs_ioctl_space_info(root, argp);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004868 case BTRFS_IOC_SYNC: {
4869 int ret;
4870
Miao Xie91aef862013-11-04 23:13:26 +08004871 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
Filipe David Borba Manana9b199852013-09-23 11:35:11 +01004872 if (ret)
4873 return ret;
4874 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4875 return ret;
4876 }
Sage Weil46204592010-10-29 15:41:32 -04004877 case BTRFS_IOC_START_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004878 return btrfs_ioctl_start_sync(root, argp);
Sage Weil46204592010-10-29 15:41:32 -04004879 case BTRFS_IOC_WAIT_SYNC:
Miao Xie9a8c28b2012-11-26 08:40:43 +00004880 return btrfs_ioctl_wait_sync(root, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004881 case BTRFS_IOC_SCRUB:
Miao Xieb8e95482012-11-26 08:48:01 +00004882 return btrfs_ioctl_scrub(file, argp);
Jan Schmidt475f6382011-03-11 15:41:01 +01004883 case BTRFS_IOC_SCRUB_CANCEL:
4884 return btrfs_ioctl_scrub_cancel(root, argp);
4885 case BTRFS_IOC_SCRUB_PROGRESS:
4886 return btrfs_ioctl_scrub_progress(root, argp);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004887 case BTRFS_IOC_BALANCE_V2:
Liu Bo9ba1f6e2012-05-11 18:11:26 +08004888 return btrfs_ioctl_balance(file, argp);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004889 case BTRFS_IOC_BALANCE_CTL:
4890 return btrfs_ioctl_balance_ctl(root, arg);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02004891 case BTRFS_IOC_BALANCE_PROGRESS:
4892 return btrfs_ioctl_balance_progress(root, argp);
Alexander Block8ea05e32012-07-25 17:35:53 +02004893 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4894 return btrfs_ioctl_set_received_subvol(file, argp);
Hugo Millsabccd002014-01-30 20:17:00 +00004895#ifdef CONFIG_64BIT
4896 case BTRFS_IOC_SET_RECEIVED_SUBVOL_32:
4897 return btrfs_ioctl_set_received_subvol_32(file, argp);
4898#endif
Alexander Block31db9f72012-07-25 23:19:24 +02004899 case BTRFS_IOC_SEND:
4900 return btrfs_ioctl_send(file, argp);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02004901 case BTRFS_IOC_GET_DEV_STATS:
David Sterbab27f7c02012-06-22 06:30:39 -06004902 return btrfs_ioctl_get_dev_stats(root, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004903 case BTRFS_IOC_QUOTA_CTL:
Miao Xie905b0dd2012-11-26 08:50:11 +00004904 return btrfs_ioctl_quota_ctl(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004905 case BTRFS_IOC_QGROUP_ASSIGN:
Miao Xie905b0dd2012-11-26 08:50:11 +00004906 return btrfs_ioctl_qgroup_assign(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004907 case BTRFS_IOC_QGROUP_CREATE:
Miao Xie905b0dd2012-11-26 08:50:11 +00004908 return btrfs_ioctl_qgroup_create(file, argp);
Arne Jansen5d13a372011-09-14 15:53:51 +02004909 case BTRFS_IOC_QGROUP_LIMIT:
Miao Xie905b0dd2012-11-26 08:50:11 +00004910 return btrfs_ioctl_qgroup_limit(file, argp);
Jan Schmidt2f232032013-04-25 16:04:51 +00004911 case BTRFS_IOC_QUOTA_RESCAN:
4912 return btrfs_ioctl_quota_rescan(file, argp);
4913 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4914 return btrfs_ioctl_quota_rescan_status(file, argp);
Jan Schmidt57254b6e2013-05-06 19:14:17 +00004915 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4916 return btrfs_ioctl_quota_rescan_wait(file, argp);
Stefan Behrens3f6bcfb2012-11-06 15:08:53 +01004917 case BTRFS_IOC_DEV_REPLACE:
4918 return btrfs_ioctl_dev_replace(root, argp);
jeff.liu867ab662013-01-05 02:48:01 +00004919 case BTRFS_IOC_GET_FSLABEL:
4920 return btrfs_ioctl_get_fslabel(file, argp);
jeff.liua8bfd4a2013-01-05 02:48:08 +00004921 case BTRFS_IOC_SET_FSLABEL:
4922 return btrfs_ioctl_set_fslabel(file, argp);
Mark Fasheh416161d2013-08-06 11:42:51 -07004923 case BTRFS_IOC_FILE_EXTENT_SAME:
4924 return btrfs_ioctl_file_extent_same(file, argp);
Jeff Mahoney2eaa0552013-11-15 15:33:55 -05004925 case BTRFS_IOC_GET_SUPPORTED_FEATURES:
4926 return btrfs_ioctl_get_supported_features(file, argp);
4927 case BTRFS_IOC_GET_FEATURES:
4928 return btrfs_ioctl_get_features(file, argp);
4929 case BTRFS_IOC_SET_FEATURES:
4930 return btrfs_ioctl_set_features(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004931 }
4932
4933 return -ENOTTY;
4934}