Merge remote-tracking branches 'regmap/fix/doc' and 'regmap/fix/mmio' into regmap...
[deliverable/linux.git] / fs / btrfs / ioctl.c
CommitLineData
f46b5a66
CH
1/*
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>
cb8e7090 24#include <linux/fsnotify.h>
f46b5a66
CH
25#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
f46b5a66 30#include <linux/backing-dev.h>
cb8e7090 31#include <linux/mount.h>
f46b5a66 32#include <linux/mpage.h>
cb8e7090 33#include <linux/namei.h>
f46b5a66
CH
34#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>
cb8e7090 39#include <linux/security.h>
f46b5a66 40#include <linux/xattr.h>
7ea394f1 41#include <linux/vmalloc.h>
5a0e3ad6 42#include <linux/slab.h>
f7039b1d 43#include <linux/blkdev.h>
8ea05e3a 44#include <linux/uuid.h>
55e301fd 45#include <linux/btrfs.h>
416161db 46#include <linux/uaccess.h>
f46b5a66
CH
47#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
f46b5a66
CH
51#include "print-tree.h"
52#include "volumes.h"
925baedd 53#include "locking.h"
581bb050 54#include "inode-map.h"
d7728c96 55#include "backref.h"
606686ee 56#include "rcu-string.h"
31db9f7c 57#include "send.h"
3f6bcfbd 58#include "dev-replace.h"
f46b5a66 59
416161db
MF
60static int btrfs_clone(struct inode *src, struct inode *inode,
61 u64 off, u64 olen, u64 olen_aligned, u64 destoff);
62
6cbff00f
CH
63/* Mask out flags that are inappropriate for the given type of inode. */
64static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
65{
66 if (S_ISDIR(mode))
67 return flags;
68 else if (S_ISREG(mode))
69 return flags & ~FS_DIRSYNC_FL;
70 else
71 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
72}
73
74/*
75 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
76 */
77static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
78{
79 unsigned int iflags = 0;
80
81 if (flags & BTRFS_INODE_SYNC)
82 iflags |= FS_SYNC_FL;
83 if (flags & BTRFS_INODE_IMMUTABLE)
84 iflags |= FS_IMMUTABLE_FL;
85 if (flags & BTRFS_INODE_APPEND)
86 iflags |= FS_APPEND_FL;
87 if (flags & BTRFS_INODE_NODUMP)
88 iflags |= FS_NODUMP_FL;
89 if (flags & BTRFS_INODE_NOATIME)
90 iflags |= FS_NOATIME_FL;
91 if (flags & BTRFS_INODE_DIRSYNC)
92 iflags |= FS_DIRSYNC_FL;
d0092bdd
LZ
93 if (flags & BTRFS_INODE_NODATACOW)
94 iflags |= FS_NOCOW_FL;
95
96 if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS))
97 iflags |= FS_COMPR_FL;
98 else if (flags & BTRFS_INODE_NOCOMPRESS)
99 iflags |= FS_NOCOMP_FL;
6cbff00f
CH
100
101 return iflags;
102}
103
104/*
105 * Update inode->i_flags based on the btrfs internal flags.
106 */
107void btrfs_update_iflags(struct inode *inode)
108{
109 struct btrfs_inode *ip = BTRFS_I(inode);
110
111 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
112
113 if (ip->flags & BTRFS_INODE_SYNC)
114 inode->i_flags |= S_SYNC;
115 if (ip->flags & BTRFS_INODE_IMMUTABLE)
116 inode->i_flags |= S_IMMUTABLE;
117 if (ip->flags & BTRFS_INODE_APPEND)
118 inode->i_flags |= S_APPEND;
119 if (ip->flags & BTRFS_INODE_NOATIME)
120 inode->i_flags |= S_NOATIME;
121 if (ip->flags & BTRFS_INODE_DIRSYNC)
122 inode->i_flags |= S_DIRSYNC;
123}
124
125/*
126 * Inherit flags from the parent inode.
127 *
e27425d6 128 * Currently only the compression flags and the cow flags are inherited.
6cbff00f
CH
129 */
130void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
131{
0b4dcea5
CM
132 unsigned int flags;
133
134 if (!dir)
135 return;
136
137 flags = BTRFS_I(dir)->flags;
6cbff00f 138
e27425d6
JB
139 if (flags & BTRFS_INODE_NOCOMPRESS) {
140 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
141 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
142 } else if (flags & BTRFS_INODE_COMPRESS) {
143 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
144 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
145 }
146
213490b3 147 if (flags & BTRFS_INODE_NODATACOW) {
e27425d6 148 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
213490b3
LB
149 if (S_ISREG(inode->i_mode))
150 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
151 }
6cbff00f 152
6cbff00f
CH
153 btrfs_update_iflags(inode);
154}
155
156static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
157{
496ad9aa 158 struct btrfs_inode *ip = BTRFS_I(file_inode(file));
6cbff00f
CH
159 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
160
161 if (copy_to_user(arg, &flags, sizeof(flags)))
162 return -EFAULT;
163 return 0;
164}
165
75e7cb7f
LB
166static int check_flags(unsigned int flags)
167{
168 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
169 FS_NOATIME_FL | FS_NODUMP_FL | \
170 FS_SYNC_FL | FS_DIRSYNC_FL | \
e1e8fb6a
LZ
171 FS_NOCOMP_FL | FS_COMPR_FL |
172 FS_NOCOW_FL))
75e7cb7f
LB
173 return -EOPNOTSUPP;
174
175 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
176 return -EINVAL;
177
75e7cb7f
LB
178 return 0;
179}
180
6cbff00f
CH
181static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
182{
496ad9aa 183 struct inode *inode = file_inode(file);
6cbff00f
CH
184 struct btrfs_inode *ip = BTRFS_I(inode);
185 struct btrfs_root *root = ip->root;
186 struct btrfs_trans_handle *trans;
187 unsigned int flags, oldflags;
188 int ret;
f062abf0
LZ
189 u64 ip_oldflags;
190 unsigned int i_oldflags;
7e97b8da 191 umode_t mode;
6cbff00f 192
b83cc969
LZ
193 if (btrfs_root_readonly(root))
194 return -EROFS;
195
6cbff00f
CH
196 if (copy_from_user(&flags, arg, sizeof(flags)))
197 return -EFAULT;
198
75e7cb7f
LB
199 ret = check_flags(flags);
200 if (ret)
201 return ret;
f46b5a66 202
2e149670 203 if (!inode_owner_or_capable(inode))
6cbff00f
CH
204 return -EACCES;
205
e7848683
JK
206 ret = mnt_want_write_file(file);
207 if (ret)
208 return ret;
209
6cbff00f
CH
210 mutex_lock(&inode->i_mutex);
211
f062abf0
LZ
212 ip_oldflags = ip->flags;
213 i_oldflags = inode->i_flags;
7e97b8da 214 mode = inode->i_mode;
f062abf0 215
6cbff00f
CH
216 flags = btrfs_mask_flags(inode->i_mode, flags);
217 oldflags = btrfs_flags_to_ioctl(ip->flags);
218 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
219 if (!capable(CAP_LINUX_IMMUTABLE)) {
220 ret = -EPERM;
221 goto out_unlock;
222 }
223 }
224
6cbff00f
CH
225 if (flags & FS_SYNC_FL)
226 ip->flags |= BTRFS_INODE_SYNC;
227 else
228 ip->flags &= ~BTRFS_INODE_SYNC;
229 if (flags & FS_IMMUTABLE_FL)
230 ip->flags |= BTRFS_INODE_IMMUTABLE;
231 else
232 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
233 if (flags & FS_APPEND_FL)
234 ip->flags |= BTRFS_INODE_APPEND;
235 else
236 ip->flags &= ~BTRFS_INODE_APPEND;
237 if (flags & FS_NODUMP_FL)
238 ip->flags |= BTRFS_INODE_NODUMP;
239 else
240 ip->flags &= ~BTRFS_INODE_NODUMP;
241 if (flags & FS_NOATIME_FL)
242 ip->flags |= BTRFS_INODE_NOATIME;
243 else
244 ip->flags &= ~BTRFS_INODE_NOATIME;
245 if (flags & FS_DIRSYNC_FL)
246 ip->flags |= BTRFS_INODE_DIRSYNC;
247 else
248 ip->flags &= ~BTRFS_INODE_DIRSYNC;
7e97b8da
DS
249 if (flags & FS_NOCOW_FL) {
250 if (S_ISREG(mode)) {
251 /*
252 * It's safe to turn csums off here, no extents exist.
253 * Otherwise we want the flag to reflect the real COW
254 * status of the file and will not set it.
255 */
256 if (inode->i_size == 0)
257 ip->flags |= BTRFS_INODE_NODATACOW
258 | BTRFS_INODE_NODATASUM;
259 } else {
260 ip->flags |= BTRFS_INODE_NODATACOW;
261 }
262 } else {
263 /*
264 * Revert back under same assuptions as above
265 */
266 if (S_ISREG(mode)) {
267 if (inode->i_size == 0)
268 ip->flags &= ~(BTRFS_INODE_NODATACOW
269 | BTRFS_INODE_NODATASUM);
270 } else {
271 ip->flags &= ~BTRFS_INODE_NODATACOW;
272 }
273 }
6cbff00f 274
75e7cb7f
LB
275 /*
276 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
277 * flag may be changed automatically if compression code won't make
278 * things smaller.
279 */
280 if (flags & FS_NOCOMP_FL) {
281 ip->flags &= ~BTRFS_INODE_COMPRESS;
282 ip->flags |= BTRFS_INODE_NOCOMPRESS;
283 } else if (flags & FS_COMPR_FL) {
284 ip->flags |= BTRFS_INODE_COMPRESS;
285 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
ebcb904d
LZ
286 } else {
287 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS);
75e7cb7f 288 }
6cbff00f 289
4da6f1a3 290 trans = btrfs_start_transaction(root, 1);
f062abf0
LZ
291 if (IS_ERR(trans)) {
292 ret = PTR_ERR(trans);
293 goto out_drop;
294 }
6cbff00f 295
306424cc 296 btrfs_update_iflags(inode);
0c4d2d95 297 inode_inc_iversion(inode);
306424cc 298 inode->i_ctime = CURRENT_TIME;
6cbff00f 299 ret = btrfs_update_inode(trans, root, inode);
6cbff00f 300
6cbff00f 301 btrfs_end_transaction(trans, root);
f062abf0
LZ
302 out_drop:
303 if (ret) {
304 ip->flags = ip_oldflags;
305 inode->i_flags = i_oldflags;
306 }
6cbff00f 307
6cbff00f
CH
308 out_unlock:
309 mutex_unlock(&inode->i_mutex);
e7848683 310 mnt_drop_write_file(file);
2d4e6f6a 311 return ret;
6cbff00f
CH
312}
313
314static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
315{
496ad9aa 316 struct inode *inode = file_inode(file);
6cbff00f
CH
317
318 return put_user(inode->i_generation, arg);
319}
f46b5a66 320
f7039b1d
LD
321static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
322{
54563d41 323 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
f7039b1d
LD
324 struct btrfs_device *device;
325 struct request_queue *q;
326 struct fstrim_range range;
327 u64 minlen = ULLONG_MAX;
328 u64 num_devices = 0;
815745cf 329 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
f7039b1d
LD
330 int ret;
331
332 if (!capable(CAP_SYS_ADMIN))
333 return -EPERM;
334
1f78160c
XG
335 rcu_read_lock();
336 list_for_each_entry_rcu(device, &fs_info->fs_devices->devices,
337 dev_list) {
f7039b1d
LD
338 if (!device->bdev)
339 continue;
340 q = bdev_get_queue(device->bdev);
341 if (blk_queue_discard(q)) {
342 num_devices++;
343 minlen = min((u64)q->limits.discard_granularity,
344 minlen);
345 }
346 }
1f78160c 347 rcu_read_unlock();
f4c697e6 348
f7039b1d
LD
349 if (!num_devices)
350 return -EOPNOTSUPP;
f7039b1d
LD
351 if (copy_from_user(&range, arg, sizeof(range)))
352 return -EFAULT;
e515c18b
LC
353 if (range.start > total_bytes ||
354 range.len < fs_info->sb->s_blocksize)
f4c697e6 355 return -EINVAL;
f7039b1d 356
f4c697e6 357 range.len = min(range.len, total_bytes - range.start);
f7039b1d 358 range.minlen = max(range.minlen, minlen);
815745cf 359 ret = btrfs_trim_fs(fs_info->tree_root, &range);
f7039b1d
LD
360 if (ret < 0)
361 return ret;
362
363 if (copy_to_user(arg, &range, sizeof(range)))
364 return -EFAULT;
365
366 return 0;
367}
368
dd5f9615
SB
369int btrfs_is_empty_uuid(u8 *uuid)
370{
46e0f66a
CM
371 int i;
372
373 for (i = 0; i < BTRFS_UUID_SIZE; i++) {
374 if (uuid[i])
375 return 0;
376 }
377 return 1;
dd5f9615
SB
378}
379
d5c12070 380static noinline int create_subvol(struct inode *dir,
cb8e7090 381 struct dentry *dentry,
72fd032e 382 char *name, int namelen,
6f72c7e2 383 u64 *async_transid,
8696c533 384 struct btrfs_qgroup_inherit *inherit)
f46b5a66
CH
385{
386 struct btrfs_trans_handle *trans;
387 struct btrfs_key key;
388 struct btrfs_root_item root_item;
389 struct btrfs_inode_item *inode_item;
390 struct extent_buffer *leaf;
d5c12070 391 struct btrfs_root *root = BTRFS_I(dir)->root;
76dda93c 392 struct btrfs_root *new_root;
d5c12070 393 struct btrfs_block_rsv block_rsv;
8ea05e3a 394 struct timespec cur_time = CURRENT_TIME;
f46b5a66
CH
395 int ret;
396 int err;
397 u64 objectid;
398 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
3de4586c 399 u64 index = 0;
d5c12070 400 u64 qgroup_reserved;
8ea05e3a 401 uuid_le new_uuid;
f46b5a66 402
581bb050 403 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid);
2fbe8c8a 404 if (ret)
a22285a6 405 return ret;
6a912213 406
d5c12070 407 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
9ed74f2d 408 /*
d5c12070
MX
409 * The same as the snapshot creation, please see the comment
410 * of create_snapshot().
9ed74f2d 411 */
d5c12070 412 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
dd5f9615 413 8, &qgroup_reserved, false);
d5c12070
MX
414 if (ret)
415 return ret;
416
417 trans = btrfs_start_transaction(root, 0);
418 if (IS_ERR(trans)) {
419 ret = PTR_ERR(trans);
420 goto out;
421 }
422 trans->block_rsv = &block_rsv;
423 trans->bytes_reserved = block_rsv.size;
f46b5a66 424
8696c533 425 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit);
6f72c7e2
AJ
426 if (ret)
427 goto fail;
428
5d4f98a2 429 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
5581a51a 430 0, objectid, NULL, 0, 0, 0);
8e8a1e31
JB
431 if (IS_ERR(leaf)) {
432 ret = PTR_ERR(leaf);
433 goto fail;
434 }
f46b5a66 435
5d4f98a2 436 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
f46b5a66
CH
437 btrfs_set_header_bytenr(leaf, leaf->start);
438 btrfs_set_header_generation(leaf, trans->transid);
5d4f98a2 439 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
f46b5a66
CH
440 btrfs_set_header_owner(leaf, objectid);
441
0a4e5586 442 write_extent_buffer(leaf, root->fs_info->fsid, btrfs_header_fsid(),
f46b5a66 443 BTRFS_FSID_SIZE);
5d4f98a2 444 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
b308bc2f 445 btrfs_header_chunk_tree_uuid(leaf),
5d4f98a2 446 BTRFS_UUID_SIZE);
f46b5a66
CH
447 btrfs_mark_buffer_dirty(leaf);
448
8ea05e3a
AB
449 memset(&root_item, 0, sizeof(root_item));
450
f46b5a66 451 inode_item = &root_item.inode;
3cae210f
QW
452 btrfs_set_stack_inode_generation(inode_item, 1);
453 btrfs_set_stack_inode_size(inode_item, 3);
454 btrfs_set_stack_inode_nlink(inode_item, 1);
455 btrfs_set_stack_inode_nbytes(inode_item, root->leafsize);
456 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
f46b5a66 457
3cae210f
QW
458 btrfs_set_root_flags(&root_item, 0);
459 btrfs_set_root_limit(&root_item, 0);
460 btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT);
08fe4db1 461
f46b5a66 462 btrfs_set_root_bytenr(&root_item, leaf->start);
84234f3a 463 btrfs_set_root_generation(&root_item, trans->transid);
f46b5a66
CH
464 btrfs_set_root_level(&root_item, 0);
465 btrfs_set_root_refs(&root_item, 1);
86b9f2ec 466 btrfs_set_root_used(&root_item, leaf->len);
80ff3856 467 btrfs_set_root_last_snapshot(&root_item, 0);
f46b5a66 468
8ea05e3a
AB
469 btrfs_set_root_generation_v2(&root_item,
470 btrfs_root_generation(&root_item));
471 uuid_le_gen(&new_uuid);
472 memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE);
3cae210f
QW
473 btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec);
474 btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec);
8ea05e3a
AB
475 root_item.ctime = root_item.otime;
476 btrfs_set_root_ctransid(&root_item, trans->transid);
477 btrfs_set_root_otransid(&root_item, trans->transid);
f46b5a66 478
925baedd 479 btrfs_tree_unlock(leaf);
f46b5a66
CH
480 free_extent_buffer(leaf);
481 leaf = NULL;
482
483 btrfs_set_root_dirid(&root_item, new_dirid);
484
485 key.objectid = objectid;
5d4f98a2 486 key.offset = 0;
f46b5a66
CH
487 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
488 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
489 &root_item);
490 if (ret)
491 goto fail;
492
76dda93c
YZ
493 key.offset = (u64)-1;
494 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
79787eaa
JM
495 if (IS_ERR(new_root)) {
496 btrfs_abort_transaction(trans, root, PTR_ERR(new_root));
497 ret = PTR_ERR(new_root);
498 goto fail;
499 }
76dda93c
YZ
500
501 btrfs_record_root_in_trans(trans, new_root);
502
d82a6f1d 503 ret = btrfs_create_subvol_root(trans, new_root, new_dirid);
ce598979
MF
504 if (ret) {
505 /* We potentially lose an unused inode item here */
79787eaa 506 btrfs_abort_transaction(trans, root, ret);
ce598979
MF
507 goto fail;
508 }
509
f46b5a66
CH
510 /*
511 * insert the directory item
512 */
3de4586c 513 ret = btrfs_set_inode_index(dir, &index);
79787eaa
JM
514 if (ret) {
515 btrfs_abort_transaction(trans, root, ret);
516 goto fail;
517 }
3de4586c
CM
518
519 ret = btrfs_insert_dir_item(trans, root,
16cdcec7 520 name, namelen, dir, &key,
3de4586c 521 BTRFS_FT_DIR, index);
79787eaa
JM
522 if (ret) {
523 btrfs_abort_transaction(trans, root, ret);
f46b5a66 524 goto fail;
79787eaa 525 }
0660b5af 526
52c26179
YZ
527 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
528 ret = btrfs_update_inode(trans, root, dir);
529 BUG_ON(ret);
530
0660b5af 531 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4df27c4d 532 objectid, root->root_key.objectid,
33345d01 533 btrfs_ino(dir), index, name, namelen);
76dda93c 534 BUG_ON(ret);
f46b5a66 535
dd5f9615
SB
536 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
537 root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
538 objectid);
539 if (ret)
540 btrfs_abort_transaction(trans, root, ret);
541
f46b5a66 542fail:
d5c12070
MX
543 trans->block_rsv = NULL;
544 trans->bytes_reserved = 0;
72fd032e
SW
545 if (async_transid) {
546 *async_transid = trans->transid;
547 err = btrfs_commit_transaction_async(trans, root, 1);
00d71c9c
MX
548 if (err)
549 err = btrfs_commit_transaction(trans, root);
72fd032e
SW
550 } else {
551 err = btrfs_commit_transaction(trans, root);
552 }
f46b5a66
CH
553 if (err && !ret)
554 ret = err;
1a65e24b
CM
555
556 if (!ret)
557 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
d5c12070
MX
558out:
559 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
f46b5a66
CH
560 return ret;
561}
562
e9662f70
MX
563static int create_snapshot(struct btrfs_root *root, struct inode *dir,
564 struct dentry *dentry, char *name, int namelen,
565 u64 *async_transid, bool readonly,
566 struct btrfs_qgroup_inherit *inherit)
f46b5a66 567{
2e4bfab9 568 struct inode *inode;
f46b5a66
CH
569 struct btrfs_pending_snapshot *pending_snapshot;
570 struct btrfs_trans_handle *trans;
2e4bfab9 571 int ret;
f46b5a66
CH
572
573 if (!root->ref_cows)
574 return -EINVAL;
575
6a03843d
MX
576 ret = btrfs_start_delalloc_inodes(root, 0);
577 if (ret)
578 return ret;
579
b0244199 580 btrfs_wait_ordered_extents(root, -1);
6a03843d 581
3de4586c 582 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
a22285a6
YZ
583 if (!pending_snapshot)
584 return -ENOMEM;
585
66d8f3dd
MX
586 btrfs_init_block_rsv(&pending_snapshot->block_rsv,
587 BTRFS_BLOCK_RSV_TEMP);
d5c12070
MX
588 /*
589 * 1 - parent dir inode
590 * 2 - dir entries
591 * 1 - root item
592 * 2 - root ref/backref
593 * 1 - root of snapshot
dd5f9615 594 * 1 - UUID item
d5c12070
MX
595 */
596 ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root,
dd5f9615 597 &pending_snapshot->block_rsv, 8,
ee3441b4
JM
598 &pending_snapshot->qgroup_reserved,
599 false);
d5c12070
MX
600 if (ret)
601 goto out;
602
3de4586c 603 pending_snapshot->dentry = dentry;
f46b5a66 604 pending_snapshot->root = root;
b83cc969 605 pending_snapshot->readonly = readonly;
e9662f70 606 pending_snapshot->dir = dir;
8696c533 607 pending_snapshot->inherit = inherit;
a22285a6 608
d5c12070 609 trans = btrfs_start_transaction(root, 0);
a22285a6
YZ
610 if (IS_ERR(trans)) {
611 ret = PTR_ERR(trans);
612 goto fail;
613 }
614
8351583e 615 spin_lock(&root->fs_info->trans_lock);
f46b5a66
CH
616 list_add(&pending_snapshot->list,
617 &trans->transaction->pending_snapshots);
8351583e 618 spin_unlock(&root->fs_info->trans_lock);
72fd032e
SW
619 if (async_transid) {
620 *async_transid = trans->transid;
621 ret = btrfs_commit_transaction_async(trans,
622 root->fs_info->extent_root, 1);
00d71c9c
MX
623 if (ret)
624 ret = btrfs_commit_transaction(trans, root);
72fd032e
SW
625 } else {
626 ret = btrfs_commit_transaction(trans,
627 root->fs_info->extent_root);
628 }
aec8030a 629 if (ret)
c37b2b62 630 goto fail;
a22285a6
YZ
631
632 ret = pending_snapshot->error;
633 if (ret)
634 goto fail;
635
66b4ffd1
JB
636 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
637 if (ret)
638 goto fail;
f46b5a66 639
2fbe8c8a 640 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
2e4bfab9
YZ
641 if (IS_ERR(inode)) {
642 ret = PTR_ERR(inode);
643 goto fail;
644 }
645 BUG_ON(!inode);
646 d_instantiate(dentry, inode);
647 ret = 0;
648fail:
d5c12070
MX
649 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root,
650 &pending_snapshot->block_rsv,
651 pending_snapshot->qgroup_reserved);
652out:
a22285a6 653 kfree(pending_snapshot);
f46b5a66
CH
654 return ret;
655}
656
4260f7c7
SW
657/* copy of check_sticky in fs/namei.c()
658* It's inline, so penalty for filesystems that don't use sticky bit is
659* minimal.
660*/
661static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
662{
2f2f43d3 663 kuid_t fsuid = current_fsuid();
4260f7c7
SW
664
665 if (!(dir->i_mode & S_ISVTX))
666 return 0;
2f2f43d3 667 if (uid_eq(inode->i_uid, fsuid))
4260f7c7 668 return 0;
2f2f43d3 669 if (uid_eq(dir->i_uid, fsuid))
4260f7c7
SW
670 return 0;
671 return !capable(CAP_FOWNER);
672}
673
674/* copy of may_delete in fs/namei.c()
675 * Check whether we can remove a link victim from directory dir, check
676 * whether the type of victim is right.
677 * 1. We can't do it if dir is read-only (done in permission())
678 * 2. We should have write and exec permissions on dir
679 * 3. We can't remove anything from append-only dir
680 * 4. We can't do anything with immutable dir (done in permission())
681 * 5. If the sticky bit on dir is set we should either
682 * a. be owner of dir, or
683 * b. be owner of victim, or
684 * c. have CAP_FOWNER capability
685 * 6. If the victim is append-only or immutable we can't do antyhing with
686 * links pointing to it.
687 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
688 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
689 * 9. We can't remove a root or mountpoint.
690 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
691 * nfs_async_unlink().
692 */
693
67871254 694static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir)
4260f7c7
SW
695{
696 int error;
697
698 if (!victim->d_inode)
699 return -ENOENT;
700
701 BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ec 702 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
4260f7c7
SW
703
704 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
705 if (error)
706 return error;
707 if (IS_APPEND(dir))
708 return -EPERM;
709 if (btrfs_check_sticky(dir, victim->d_inode)||
710 IS_APPEND(victim->d_inode)||
711 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
712 return -EPERM;
713 if (isdir) {
714 if (!S_ISDIR(victim->d_inode->i_mode))
715 return -ENOTDIR;
716 if (IS_ROOT(victim))
717 return -EBUSY;
718 } else if (S_ISDIR(victim->d_inode->i_mode))
719 return -EISDIR;
720 if (IS_DEADDIR(dir))
721 return -ENOENT;
722 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
723 return -EBUSY;
724 return 0;
725}
726
cb8e7090
CH
727/* copy of may_create in fs/namei.c() */
728static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
729{
730 if (child->d_inode)
731 return -EEXIST;
732 if (IS_DEADDIR(dir))
733 return -ENOENT;
734 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
735}
736
737/*
738 * Create a new subvolume below @parent. This is largely modeled after
739 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
740 * inside this filesystem so it's quite a bit simpler.
741 */
76dda93c
YZ
742static noinline int btrfs_mksubvol(struct path *parent,
743 char *name, int namelen,
72fd032e 744 struct btrfs_root *snap_src,
6f72c7e2 745 u64 *async_transid, bool readonly,
8696c533 746 struct btrfs_qgroup_inherit *inherit)
cb8e7090 747{
76dda93c 748 struct inode *dir = parent->dentry->d_inode;
cb8e7090
CH
749 struct dentry *dentry;
750 int error;
751
5c50c9b8
DS
752 error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
753 if (error == -EINTR)
754 return error;
cb8e7090
CH
755
756 dentry = lookup_one_len(name, parent->dentry, namelen);
757 error = PTR_ERR(dentry);
758 if (IS_ERR(dentry))
759 goto out_unlock;
760
761 error = -EEXIST;
762 if (dentry->d_inode)
763 goto out_dput;
764
76dda93c 765 error = btrfs_may_create(dir, dentry);
cb8e7090 766 if (error)
a874a63e 767 goto out_dput;
cb8e7090 768
9c52057c
CM
769 /*
770 * even if this name doesn't exist, we may get hash collisions.
771 * check for them now when we can safely fail
772 */
773 error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root,
774 dir->i_ino, name,
775 namelen);
776 if (error)
777 goto out_dput;
778
76dda93c
YZ
779 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
780
781 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
782 goto out_up_read;
783
3de4586c 784 if (snap_src) {
e9662f70 785 error = create_snapshot(snap_src, dir, dentry, name, namelen,
6f72c7e2 786 async_transid, readonly, inherit);
3de4586c 787 } else {
d5c12070
MX
788 error = create_subvol(dir, dentry, name, namelen,
789 async_transid, inherit);
3de4586c 790 }
76dda93c
YZ
791 if (!error)
792 fsnotify_mkdir(dir, dentry);
793out_up_read:
794 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
cb8e7090
CH
795out_dput:
796 dput(dentry);
797out_unlock:
76dda93c 798 mutex_unlock(&dir->i_mutex);
cb8e7090
CH
799 return error;
800}
801
4cb5300b
CM
802/*
803 * When we're defragging a range, we don't want to kick it off again
804 * if it is really just waiting for delalloc to send it down.
805 * If we find a nice big extent or delalloc range for the bytes in the
806 * file you want to defrag, we return 0 to let you know to skip this
807 * part of the file
808 */
809static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh)
810{
811 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
812 struct extent_map *em = NULL;
813 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
814 u64 end;
815
816 read_lock(&em_tree->lock);
817 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
818 read_unlock(&em_tree->lock);
819
820 if (em) {
821 end = extent_map_end(em);
822 free_extent_map(em);
823 if (end - offset > thresh)
824 return 0;
825 }
826 /* if we already have a nice delalloc here, just stop */
827 thresh /= 2;
828 end = count_range_bits(io_tree, &offset, offset + thresh,
829 thresh, EXTENT_DELALLOC, 1);
830 if (end >= thresh)
831 return 0;
832 return 1;
833}
834
835/*
836 * helper function to walk through a file and find extents
837 * newer than a specific transid, and smaller than thresh.
838 *
839 * This is used by the defragging code to find new and small
840 * extents
841 */
842static int find_new_extents(struct btrfs_root *root,
843 struct inode *inode, u64 newer_than,
844 u64 *off, int thresh)
845{
846 struct btrfs_path *path;
847 struct btrfs_key min_key;
4cb5300b
CM
848 struct extent_buffer *leaf;
849 struct btrfs_file_extent_item *extent;
850 int type;
851 int ret;
a4689d2b 852 u64 ino = btrfs_ino(inode);
4cb5300b
CM
853
854 path = btrfs_alloc_path();
855 if (!path)
856 return -ENOMEM;
857
a4689d2b 858 min_key.objectid = ino;
4cb5300b
CM
859 min_key.type = BTRFS_EXTENT_DATA_KEY;
860 min_key.offset = *off;
861
4cb5300b
CM
862 path->keep_locks = 1;
863
67871254 864 while (1) {
6174d3cb 865 ret = btrfs_search_forward(root, &min_key, path, newer_than);
4cb5300b
CM
866 if (ret != 0)
867 goto none;
a4689d2b 868 if (min_key.objectid != ino)
4cb5300b
CM
869 goto none;
870 if (min_key.type != BTRFS_EXTENT_DATA_KEY)
871 goto none;
872
873 leaf = path->nodes[0];
874 extent = btrfs_item_ptr(leaf, path->slots[0],
875 struct btrfs_file_extent_item);
876
877 type = btrfs_file_extent_type(leaf, extent);
878 if (type == BTRFS_FILE_EXTENT_REG &&
879 btrfs_file_extent_num_bytes(leaf, extent) < thresh &&
880 check_defrag_in_cache(inode, min_key.offset, thresh)) {
881 *off = min_key.offset;
882 btrfs_free_path(path);
883 return 0;
884 }
885
886 if (min_key.offset == (u64)-1)
887 goto none;
888
889 min_key.offset++;
890 btrfs_release_path(path);
891 }
892none:
893 btrfs_free_path(path);
894 return -ENOENT;
895}
896
6c282eb4 897static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)
17ce6ef8
LB
898{
899 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6c282eb4
LZ
900 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
901 struct extent_map *em;
902 u64 len = PAGE_CACHE_SIZE;
17ce6ef8 903
6c282eb4
LZ
904 /*
905 * hopefully we have this extent in the tree already, try without
906 * the full extent lock
907 */
17ce6ef8 908 read_lock(&em_tree->lock);
6c282eb4 909 em = lookup_extent_mapping(em_tree, start, len);
17ce6ef8
LB
910 read_unlock(&em_tree->lock);
911
6c282eb4
LZ
912 if (!em) {
913 /* get the big lock and read metadata off disk */
914 lock_extent(io_tree, start, start + len - 1);
915 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
916 unlock_extent(io_tree, start, start + len - 1);
917
918 if (IS_ERR(em))
919 return NULL;
920 }
921
922 return em;
923}
17ce6ef8 924
6c282eb4
LZ
925static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em)
926{
927 struct extent_map *next;
928 bool ret = true;
929
930 /* this is the last extent */
931 if (em->start + em->len >= i_size_read(inode))
932 return false;
933
934 next = defrag_lookup_extent(inode, em->start + em->len);
935 if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
936 ret = false;
937
938 free_extent_map(next);
17ce6ef8
LB
939 return ret;
940}
941
6c282eb4 942static int should_defrag_range(struct inode *inode, u64 start, int thresh,
a43a2111
AM
943 u64 *last_len, u64 *skip, u64 *defrag_end,
944 int compress)
940100a4 945{
6c282eb4 946 struct extent_map *em;
940100a4 947 int ret = 1;
6c282eb4 948 bool next_mergeable = true;
940100a4
CM
949
950 /*
008873ea 951 * make sure that once we start defragging an extent, we keep on
940100a4
CM
952 * defragging it
953 */
954 if (start < *defrag_end)
955 return 1;
956
957 *skip = 0;
958
6c282eb4
LZ
959 em = defrag_lookup_extent(inode, start);
960 if (!em)
961 return 0;
940100a4
CM
962
963 /* this will cover holes, and inline extents */
17ce6ef8 964 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
940100a4 965 ret = 0;
17ce6ef8
LB
966 goto out;
967 }
968
6c282eb4 969 next_mergeable = defrag_check_next_extent(inode, em);
940100a4
CM
970
971 /*
6c282eb4
LZ
972 * we hit a real extent, if it is big or the next extent is not a
973 * real extent, don't bother defragging it
940100a4 974 */
a43a2111 975 if (!compress && (*last_len == 0 || *last_len >= thresh) &&
6c282eb4 976 (em->len >= thresh || !next_mergeable))
940100a4 977 ret = 0;
17ce6ef8 978out:
940100a4
CM
979 /*
980 * last_len ends up being a counter of how many bytes we've defragged.
981 * every time we choose not to defrag an extent, we reset *last_len
982 * so that the next tiny extent will force a defrag.
983 *
984 * The end result of this is that tiny extents before a single big
985 * extent will force at least part of that big extent to be defragged.
986 */
987 if (ret) {
940100a4
CM
988 *defrag_end = extent_map_end(em);
989 } else {
990 *last_len = 0;
991 *skip = extent_map_end(em);
992 *defrag_end = 0;
993 }
994
995 free_extent_map(em);
996 return ret;
997}
998
4cb5300b
CM
999/*
1000 * it doesn't do much good to defrag one or two pages
1001 * at a time. This pulls in a nice chunk of pages
1002 * to COW and defrag.
1003 *
1004 * It also makes sure the delalloc code has enough
1005 * dirty data to avoid making new small extents as part
1006 * of the defrag
1007 *
1008 * It's a good idea to start RA on this range
1009 * before calling this.
1010 */
1011static int cluster_pages_for_defrag(struct inode *inode,
1012 struct page **pages,
1013 unsigned long start_index,
1014 int num_pages)
f46b5a66 1015{
4cb5300b
CM
1016 unsigned long file_end;
1017 u64 isize = i_size_read(inode);
1018 u64 page_start;
1019 u64 page_end;
1f12bd06 1020 u64 page_cnt;
4cb5300b
CM
1021 int ret;
1022 int i;
1023 int i_done;
3eaa2885 1024 struct btrfs_ordered_extent *ordered;
4cb5300b 1025 struct extent_state *cached_state = NULL;
600a45e1 1026 struct extent_io_tree *tree;
3b16a4e3 1027 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
4cb5300b 1028
4cb5300b 1029 file_end = (isize - 1) >> PAGE_CACHE_SHIFT;
1f12bd06
LB
1030 if (!isize || start_index > file_end)
1031 return 0;
1032
1033 page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1);
4cb5300b
CM
1034
1035 ret = btrfs_delalloc_reserve_space(inode,
1f12bd06 1036 page_cnt << PAGE_CACHE_SHIFT);
4cb5300b
CM
1037 if (ret)
1038 return ret;
4cb5300b 1039 i_done = 0;
600a45e1 1040 tree = &BTRFS_I(inode)->io_tree;
4cb5300b
CM
1041
1042 /* step one, lock all the pages */
1f12bd06 1043 for (i = 0; i < page_cnt; i++) {
4cb5300b 1044 struct page *page;
600a45e1 1045again:
a94733d0 1046 page = find_or_create_page(inode->i_mapping,
600a45e1 1047 start_index + i, mask);
4cb5300b
CM
1048 if (!page)
1049 break;
1050
600a45e1
MX
1051 page_start = page_offset(page);
1052 page_end = page_start + PAGE_CACHE_SIZE - 1;
1053 while (1) {
d0082371 1054 lock_extent(tree, page_start, page_end);
600a45e1
MX
1055 ordered = btrfs_lookup_ordered_extent(inode,
1056 page_start);
d0082371 1057 unlock_extent(tree, page_start, page_end);
600a45e1
MX
1058 if (!ordered)
1059 break;
1060
1061 unlock_page(page);
1062 btrfs_start_ordered_extent(inode, ordered, 1);
1063 btrfs_put_ordered_extent(ordered);
1064 lock_page(page);
1f12bd06
LB
1065 /*
1066 * we unlocked the page above, so we need check if
1067 * it was released or not.
1068 */
1069 if (page->mapping != inode->i_mapping) {
1070 unlock_page(page);
1071 page_cache_release(page);
1072 goto again;
1073 }
600a45e1
MX
1074 }
1075
4cb5300b
CM
1076 if (!PageUptodate(page)) {
1077 btrfs_readpage(NULL, page);
1078 lock_page(page);
1079 if (!PageUptodate(page)) {
1080 unlock_page(page);
1081 page_cache_release(page);
1082 ret = -EIO;
1083 break;
1084 }
1085 }
600a45e1 1086
600a45e1
MX
1087 if (page->mapping != inode->i_mapping) {
1088 unlock_page(page);
1089 page_cache_release(page);
1090 goto again;
1091 }
1092
4cb5300b
CM
1093 pages[i] = page;
1094 i_done++;
1095 }
1096 if (!i_done || ret)
1097 goto out;
1098
1099 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1100 goto out;
1101
1102 /*
1103 * so now we have a nice long stream of locked
1104 * and up to date pages, lets wait on them
1105 */
1106 for (i = 0; i < i_done; i++)
1107 wait_on_page_writeback(pages[i]);
1108
1109 page_start = page_offset(pages[0]);
1110 page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE;
1111
1112 lock_extent_bits(&BTRFS_I(inode)->io_tree,
d0082371 1113 page_start, page_end - 1, 0, &cached_state);
4cb5300b
CM
1114 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start,
1115 page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
9e8a4a8b
LB
1116 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0,
1117 &cached_state, GFP_NOFS);
4cb5300b 1118
1f12bd06 1119 if (i_done != page_cnt) {
9e0baf60
JB
1120 spin_lock(&BTRFS_I(inode)->lock);
1121 BTRFS_I(inode)->outstanding_extents++;
1122 spin_unlock(&BTRFS_I(inode)->lock);
4cb5300b 1123 btrfs_delalloc_release_space(inode,
1f12bd06 1124 (page_cnt - i_done) << PAGE_CACHE_SHIFT);
4cb5300b
CM
1125 }
1126
1127
9e8a4a8b
LB
1128 set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1,
1129 &cached_state, GFP_NOFS);
4cb5300b
CM
1130
1131 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1132 page_start, page_end - 1, &cached_state,
1133 GFP_NOFS);
1134
1135 for (i = 0; i < i_done; i++) {
1136 clear_page_dirty_for_io(pages[i]);
1137 ClearPageChecked(pages[i]);
1138 set_page_extent_mapped(pages[i]);
1139 set_page_dirty(pages[i]);
1140 unlock_page(pages[i]);
1141 page_cache_release(pages[i]);
1142 }
1143 return i_done;
1144out:
1145 for (i = 0; i < i_done; i++) {
1146 unlock_page(pages[i]);
1147 page_cache_release(pages[i]);
1148 }
1f12bd06 1149 btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT);
4cb5300b
CM
1150 return ret;
1151
1152}
1153
1154int btrfs_defrag_file(struct inode *inode, struct file *file,
1155 struct btrfs_ioctl_defrag_range_args *range,
1156 u64 newer_than, unsigned long max_to_defrag)
1157{
1158 struct btrfs_root *root = BTRFS_I(inode)->root;
4cb5300b 1159 struct file_ra_state *ra = NULL;
f46b5a66 1160 unsigned long last_index;
151a31b2 1161 u64 isize = i_size_read(inode);
940100a4
CM
1162 u64 last_len = 0;
1163 u64 skip = 0;
1164 u64 defrag_end = 0;
4cb5300b 1165 u64 newer_off = range->start;
f46b5a66 1166 unsigned long i;
008873ea 1167 unsigned long ra_index = 0;
f46b5a66 1168 int ret;
4cb5300b 1169 int defrag_count = 0;
1a419d85 1170 int compress_type = BTRFS_COMPRESS_ZLIB;
4cb5300b 1171 int extent_thresh = range->extent_thresh;
008873ea
LZ
1172 int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
1173 int cluster = max_cluster;
4cb5300b
CM
1174 u64 new_align = ~((u64)128 * 1024 - 1);
1175 struct page **pages = NULL;
1176
0abd5b17
LB
1177 if (isize == 0)
1178 return 0;
1179
1180 if (range->start >= isize)
1181 return -EINVAL;
1a419d85
LZ
1182
1183 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1184 if (range->compress_type > BTRFS_COMPRESS_TYPES)
1185 return -EINVAL;
1186 if (range->compress_type)
1187 compress_type = range->compress_type;
1188 }
f46b5a66 1189
0abd5b17
LB
1190 if (extent_thresh == 0)
1191 extent_thresh = 256 * 1024;
940100a4 1192
4cb5300b
CM
1193 /*
1194 * if we were not given a file, allocate a readahead
1195 * context
1196 */
1197 if (!file) {
1198 ra = kzalloc(sizeof(*ra), GFP_NOFS);
1199 if (!ra)
1200 return -ENOMEM;
1201 file_ra_state_init(ra, inode->i_mapping);
1202 } else {
1203 ra = &file->f_ra;
1204 }
1205
d9b0d9ba 1206 pages = kmalloc_array(max_cluster, sizeof(struct page *),
4cb5300b
CM
1207 GFP_NOFS);
1208 if (!pages) {
1209 ret = -ENOMEM;
1210 goto out_ra;
1211 }
1212
1213 /* find the last page to defrag */
1e701a32 1214 if (range->start + range->len > range->start) {
151a31b2 1215 last_index = min_t(u64, isize - 1,
1e701a32
CM
1216 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
1217 } else {
151a31b2 1218 last_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1e701a32
CM
1219 }
1220
4cb5300b
CM
1221 if (newer_than) {
1222 ret = find_new_extents(root, inode, newer_than,
1223 &newer_off, 64 * 1024);
1224 if (!ret) {
1225 range->start = newer_off;
1226 /*
1227 * we always align our defrag to help keep
1228 * the extents in the file evenly spaced
1229 */
1230 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1231 } else
1232 goto out_ra;
1233 } else {
1234 i = range->start >> PAGE_CACHE_SHIFT;
1235 }
1236 if (!max_to_defrag)
7ec31b54 1237 max_to_defrag = last_index + 1;
4cb5300b 1238
2a0f7f57
LZ
1239 /*
1240 * make writeback starts from i, so the defrag range can be
1241 * written sequentially.
1242 */
1243 if (i < inode->i_mapping->writeback_index)
1244 inode->i_mapping->writeback_index = i;
1245
f7f43cc8
CM
1246 while (i <= last_index && defrag_count < max_to_defrag &&
1247 (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1248 PAGE_CACHE_SHIFT)) {
4cb5300b
CM
1249 /*
1250 * make sure we stop running if someone unmounts
1251 * the FS
1252 */
1253 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1254 break;
1255
210549eb
DS
1256 if (btrfs_defrag_cancelled(root->fs_info)) {
1257 printk(KERN_DEBUG "btrfs: defrag_file cancelled\n");
1258 ret = -EAGAIN;
1259 break;
1260 }
1261
66c26892 1262 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
6c282eb4 1263 extent_thresh, &last_len, &skip,
a43a2111
AM
1264 &defrag_end, range->flags &
1265 BTRFS_DEFRAG_RANGE_COMPRESS)) {
940100a4
CM
1266 unsigned long next;
1267 /*
1268 * the should_defrag function tells us how much to skip
1269 * bump our counter by the suggested amount
1270 */
1271 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1272 i = max(i + 1, next);
1273 continue;
1274 }
008873ea
LZ
1275
1276 if (!newer_than) {
1277 cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1278 PAGE_CACHE_SHIFT) - i;
1279 cluster = min(cluster, max_cluster);
1280 } else {
1281 cluster = max_cluster;
1282 }
1283
008873ea
LZ
1284 if (i + cluster > ra_index) {
1285 ra_index = max(i, ra_index);
1286 btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1287 cluster);
1288 ra_index += max_cluster;
1289 }
940100a4 1290
ecb8bea8 1291 mutex_lock(&inode->i_mutex);
633085c7
FDBM
1292 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
1293 BTRFS_I(inode)->force_compress = compress_type;
008873ea 1294 ret = cluster_pages_for_defrag(inode, pages, i, cluster);
ecb8bea8
LB
1295 if (ret < 0) {
1296 mutex_unlock(&inode->i_mutex);
4cb5300b 1297 goto out_ra;
ecb8bea8 1298 }
4cb5300b
CM
1299
1300 defrag_count += ret;
d0e1d66b 1301 balance_dirty_pages_ratelimited(inode->i_mapping);
ecb8bea8 1302 mutex_unlock(&inode->i_mutex);
4cb5300b
CM
1303
1304 if (newer_than) {
1305 if (newer_off == (u64)-1)
1306 break;
1307
e1f041e1
LB
1308 if (ret > 0)
1309 i += ret;
1310
4cb5300b
CM
1311 newer_off = max(newer_off + 1,
1312 (u64)i << PAGE_CACHE_SHIFT);
1313
1314 ret = find_new_extents(root, inode,
1315 newer_than, &newer_off,
1316 64 * 1024);
1317 if (!ret) {
1318 range->start = newer_off;
1319 i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
4cb5300b
CM
1320 } else {
1321 break;
f46b5a66 1322 }
4cb5300b 1323 } else {
008873ea 1324 if (ret > 0) {
cbcc8326 1325 i += ret;
008873ea
LZ
1326 last_len += ret << PAGE_CACHE_SHIFT;
1327 } else {
cbcc8326 1328 i++;
008873ea
LZ
1329 last_len = 0;
1330 }
f46b5a66 1331 }
f46b5a66
CH
1332 }
1333
1e701a32
CM
1334 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
1335 filemap_flush(inode->i_mapping);
1336
1337 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1338 /* the filemap_flush will queue IO into the worker threads, but
1339 * we have to make sure the IO is actually started and that
1340 * ordered extents get created before we return
1341 */
1342 atomic_inc(&root->fs_info->async_submit_draining);
1343 while (atomic_read(&root->fs_info->nr_async_submits) ||
1344 atomic_read(&root->fs_info->async_delalloc_pages)) {
1345 wait_event(root->fs_info->async_submit_wait,
1346 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
1347 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
1348 }
1349 atomic_dec(&root->fs_info->async_submit_draining);
1e701a32
CM
1350 }
1351
1a419d85 1352 if (range->compress_type == BTRFS_COMPRESS_LZO) {
2b0ce2c2 1353 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO);
1a419d85
LZ
1354 }
1355
60ccf82f 1356 ret = defrag_count;
940100a4 1357
4cb5300b 1358out_ra:
633085c7
FDBM
1359 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
1360 mutex_lock(&inode->i_mutex);
1361 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
1362 mutex_unlock(&inode->i_mutex);
1363 }
4cb5300b
CM
1364 if (!file)
1365 kfree(ra);
1366 kfree(pages);
940100a4 1367 return ret;
f46b5a66
CH
1368}
1369
198605a8 1370static noinline int btrfs_ioctl_resize(struct file *file,
76dda93c 1371 void __user *arg)
f46b5a66
CH
1372{
1373 u64 new_size;
1374 u64 old_size;
1375 u64 devid = 1;
496ad9aa 1376 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
f46b5a66
CH
1377 struct btrfs_ioctl_vol_args *vol_args;
1378 struct btrfs_trans_handle *trans;
1379 struct btrfs_device *device = NULL;
1380 char *sizestr;
1381 char *devstr = NULL;
1382 int ret = 0;
f46b5a66
CH
1383 int mod = 0;
1384
e441d54d
CM
1385 if (!capable(CAP_SYS_ADMIN))
1386 return -EPERM;
1387
198605a8
MX
1388 ret = mnt_want_write_file(file);
1389 if (ret)
1390 return ret;
1391
5ac00add
SB
1392 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
1393 1)) {
97547676 1394 mnt_drop_write_file(file);
e57138b3 1395 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
c9e9f97b
ID
1396 }
1397
5ac00add 1398 mutex_lock(&root->fs_info->volume_mutex);
dae7b665 1399 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
1400 if (IS_ERR(vol_args)) {
1401 ret = PTR_ERR(vol_args);
1402 goto out;
1403 }
5516e595
MF
1404
1405 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 1406
f46b5a66
CH
1407 sizestr = vol_args->name;
1408 devstr = strchr(sizestr, ':');
1409 if (devstr) {
1410 char *end;
1411 sizestr = devstr + 1;
1412 *devstr = '\0';
1413 devstr = vol_args->name;
1414 devid = simple_strtoull(devstr, &end, 10);
dfd79829
MX
1415 if (!devid) {
1416 ret = -EINVAL;
1417 goto out_free;
1418 }
c1c9ff7c 1419 printk(KERN_INFO "btrfs: resizing devid %llu\n", devid);
f46b5a66 1420 }
dba60f3f 1421
aa1b8cd4 1422 device = btrfs_find_device(root->fs_info, devid, NULL, NULL);
f46b5a66 1423 if (!device) {
5bb14682 1424 printk(KERN_INFO "btrfs: resizer unable to find device %llu\n",
c1c9ff7c 1425 devid);
dfd79829 1426 ret = -ENODEV;
c9e9f97b 1427 goto out_free;
f46b5a66 1428 }
dba60f3f
MX
1429
1430 if (!device->writeable) {
4e42ae1b 1431 printk(KERN_INFO "btrfs: resizer unable to apply on "
dba60f3f 1432 "readonly device %llu\n",
c1c9ff7c 1433 devid);
dfd79829 1434 ret = -EPERM;
4e42ae1b
LB
1435 goto out_free;
1436 }
1437
f46b5a66
CH
1438 if (!strcmp(sizestr, "max"))
1439 new_size = device->bdev->bd_inode->i_size;
1440 else {
1441 if (sizestr[0] == '-') {
1442 mod = -1;
1443 sizestr++;
1444 } else if (sizestr[0] == '+') {
1445 mod = 1;
1446 sizestr++;
1447 }
91748467 1448 new_size = memparse(sizestr, NULL);
f46b5a66
CH
1449 if (new_size == 0) {
1450 ret = -EINVAL;
c9e9f97b 1451 goto out_free;
f46b5a66
CH
1452 }
1453 }
1454
63a212ab 1455 if (device->is_tgtdev_for_dev_replace) {
dfd79829 1456 ret = -EPERM;
63a212ab
SB
1457 goto out_free;
1458 }
1459
f46b5a66
CH
1460 old_size = device->total_bytes;
1461
1462 if (mod < 0) {
1463 if (new_size > old_size) {
1464 ret = -EINVAL;
c9e9f97b 1465 goto out_free;
f46b5a66
CH
1466 }
1467 new_size = old_size - new_size;
1468 } else if (mod > 0) {
1469 new_size = old_size + new_size;
1470 }
1471
1472 if (new_size < 256 * 1024 * 1024) {
1473 ret = -EINVAL;
c9e9f97b 1474 goto out_free;
f46b5a66
CH
1475 }
1476 if (new_size > device->bdev->bd_inode->i_size) {
1477 ret = -EFBIG;
c9e9f97b 1478 goto out_free;
f46b5a66
CH
1479 }
1480
1481 do_div(new_size, root->sectorsize);
1482 new_size *= root->sectorsize;
1483
606686ee 1484 printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n",
c1c9ff7c 1485 rcu_str_deref(device->name), new_size);
f46b5a66
CH
1486
1487 if (new_size > old_size) {
a22285a6 1488 trans = btrfs_start_transaction(root, 0);
98d5dc13
TI
1489 if (IS_ERR(trans)) {
1490 ret = PTR_ERR(trans);
c9e9f97b 1491 goto out_free;
98d5dc13 1492 }
f46b5a66
CH
1493 ret = btrfs_grow_device(trans, device, new_size);
1494 btrfs_commit_transaction(trans, root);
ece7d20e 1495 } else if (new_size < old_size) {
f46b5a66 1496 ret = btrfs_shrink_device(device, new_size);
0253f40e 1497 } /* equal, nothing need to do */
f46b5a66 1498
c9e9f97b 1499out_free:
f46b5a66 1500 kfree(vol_args);
c9e9f97b
ID
1501out:
1502 mutex_unlock(&root->fs_info->volume_mutex);
5ac00add 1503 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
18f39c41 1504 mnt_drop_write_file(file);
f46b5a66
CH
1505 return ret;
1506}
1507
72fd032e 1508static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
6f72c7e2
AJ
1509 char *name, unsigned long fd, int subvol,
1510 u64 *transid, bool readonly,
8696c533 1511 struct btrfs_qgroup_inherit *inherit)
f46b5a66 1512{
f46b5a66 1513 int namelen;
3de4586c 1514 int ret = 0;
f46b5a66 1515
a874a63e
LB
1516 ret = mnt_want_write_file(file);
1517 if (ret)
1518 goto out;
1519
72fd032e
SW
1520 namelen = strlen(name);
1521 if (strchr(name, '/')) {
f46b5a66 1522 ret = -EINVAL;
a874a63e 1523 goto out_drop_write;
f46b5a66
CH
1524 }
1525
16780cab
CM
1526 if (name[0] == '.' &&
1527 (namelen == 1 || (name[1] == '.' && namelen == 2))) {
1528 ret = -EEXIST;
a874a63e 1529 goto out_drop_write;
16780cab
CM
1530 }
1531
3de4586c 1532 if (subvol) {
72fd032e 1533 ret = btrfs_mksubvol(&file->f_path, name, namelen,
6f72c7e2 1534 NULL, transid, readonly, inherit);
cb8e7090 1535 } else {
2903ff01 1536 struct fd src = fdget(fd);
3de4586c 1537 struct inode *src_inode;
2903ff01 1538 if (!src.file) {
3de4586c 1539 ret = -EINVAL;
a874a63e 1540 goto out_drop_write;
3de4586c
CM
1541 }
1542
496ad9aa
AV
1543 src_inode = file_inode(src.file);
1544 if (src_inode->i_sb != file_inode(file)->i_sb) {
d397712b
CM
1545 printk(KERN_INFO "btrfs: Snapshot src from "
1546 "another FS\n");
3de4586c 1547 ret = -EINVAL;
ecd18815
AV
1548 } else {
1549 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1550 BTRFS_I(src_inode)->root,
1551 transid, readonly, inherit);
3de4586c 1552 }
2903ff01 1553 fdput(src);
cb8e7090 1554 }
a874a63e
LB
1555out_drop_write:
1556 mnt_drop_write_file(file);
f46b5a66 1557out:
72fd032e
SW
1558 return ret;
1559}
1560
1561static noinline int btrfs_ioctl_snap_create(struct file *file,
fa0d2b9b 1562 void __user *arg, int subvol)
72fd032e 1563{
fa0d2b9b 1564 struct btrfs_ioctl_vol_args *vol_args;
72fd032e
SW
1565 int ret;
1566
fa0d2b9b
LZ
1567 vol_args = memdup_user(arg, sizeof(*vol_args));
1568 if (IS_ERR(vol_args))
1569 return PTR_ERR(vol_args);
1570 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
72fd032e 1571
fa0d2b9b 1572 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
b83cc969 1573 vol_args->fd, subvol,
6f72c7e2 1574 NULL, false, NULL);
fdfb1e4f 1575
fa0d2b9b
LZ
1576 kfree(vol_args);
1577 return ret;
1578}
fdfb1e4f 1579
fa0d2b9b
LZ
1580static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1581 void __user *arg, int subvol)
1582{
1583 struct btrfs_ioctl_vol_args_v2 *vol_args;
1584 int ret;
1585 u64 transid = 0;
1586 u64 *ptr = NULL;
b83cc969 1587 bool readonly = false;
6f72c7e2 1588 struct btrfs_qgroup_inherit *inherit = NULL;
75eaa0e2 1589
fa0d2b9b
LZ
1590 vol_args = memdup_user(arg, sizeof(*vol_args));
1591 if (IS_ERR(vol_args))
1592 return PTR_ERR(vol_args);
1593 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
75eaa0e2 1594
b83cc969 1595 if (vol_args->flags &
6f72c7e2
AJ
1596 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY |
1597 BTRFS_SUBVOL_QGROUP_INHERIT)) {
b83cc969 1598 ret = -EOPNOTSUPP;
fa0d2b9b 1599 goto out;
72fd032e 1600 }
fa0d2b9b
LZ
1601
1602 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1603 ptr = &transid;
b83cc969
LZ
1604 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1605 readonly = true;
6f72c7e2
AJ
1606 if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) {
1607 if (vol_args->size > PAGE_CACHE_SIZE) {
1608 ret = -EINVAL;
1609 goto out;
1610 }
1611 inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size);
1612 if (IS_ERR(inherit)) {
1613 ret = PTR_ERR(inherit);
1614 goto out;
1615 }
1616 }
fa0d2b9b
LZ
1617
1618 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
6f72c7e2 1619 vol_args->fd, subvol, ptr,
8696c533 1620 readonly, inherit);
fa0d2b9b
LZ
1621
1622 if (ret == 0 && ptr &&
1623 copy_to_user(arg +
1624 offsetof(struct btrfs_ioctl_vol_args_v2,
1625 transid), ptr, sizeof(*ptr)))
1626 ret = -EFAULT;
fdfb1e4f 1627out:
f46b5a66 1628 kfree(vol_args);
6f72c7e2 1629 kfree(inherit);
f46b5a66
CH
1630 return ret;
1631}
1632
0caa102d
LZ
1633static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1634 void __user *arg)
1635{
496ad9aa 1636 struct inode *inode = file_inode(file);
0caa102d
LZ
1637 struct btrfs_root *root = BTRFS_I(inode)->root;
1638 int ret = 0;
1639 u64 flags = 0;
1640
33345d01 1641 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
0caa102d
LZ
1642 return -EINVAL;
1643
1644 down_read(&root->fs_info->subvol_sem);
1645 if (btrfs_root_readonly(root))
1646 flags |= BTRFS_SUBVOL_RDONLY;
1647 up_read(&root->fs_info->subvol_sem);
1648
1649 if (copy_to_user(arg, &flags, sizeof(flags)))
1650 ret = -EFAULT;
1651
1652 return ret;
1653}
1654
1655static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1656 void __user *arg)
1657{
496ad9aa 1658 struct inode *inode = file_inode(file);
0caa102d
LZ
1659 struct btrfs_root *root = BTRFS_I(inode)->root;
1660 struct btrfs_trans_handle *trans;
1661 u64 root_flags;
1662 u64 flags;
1663 int ret = 0;
1664
b9ca0664
LB
1665 ret = mnt_want_write_file(file);
1666 if (ret)
1667 goto out;
0caa102d 1668
b9ca0664
LB
1669 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
1670 ret = -EINVAL;
1671 goto out_drop_write;
1672 }
0caa102d 1673
b9ca0664
LB
1674 if (copy_from_user(&flags, arg, sizeof(flags))) {
1675 ret = -EFAULT;
1676 goto out_drop_write;
1677 }
0caa102d 1678
b9ca0664
LB
1679 if (flags & BTRFS_SUBVOL_CREATE_ASYNC) {
1680 ret = -EINVAL;
1681 goto out_drop_write;
1682 }
0caa102d 1683
b9ca0664
LB
1684 if (flags & ~BTRFS_SUBVOL_RDONLY) {
1685 ret = -EOPNOTSUPP;
1686 goto out_drop_write;
1687 }
0caa102d 1688
b9ca0664
LB
1689 if (!inode_owner_or_capable(inode)) {
1690 ret = -EACCES;
1691 goto out_drop_write;
1692 }
b4dc2b8c 1693
0caa102d
LZ
1694 down_write(&root->fs_info->subvol_sem);
1695
1696 /* nothing to do */
1697 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
b9ca0664 1698 goto out_drop_sem;
0caa102d
LZ
1699
1700 root_flags = btrfs_root_flags(&root->root_item);
1701 if (flags & BTRFS_SUBVOL_RDONLY)
1702 btrfs_set_root_flags(&root->root_item,
1703 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1704 else
1705 btrfs_set_root_flags(&root->root_item,
1706 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1707
1708 trans = btrfs_start_transaction(root, 1);
1709 if (IS_ERR(trans)) {
1710 ret = PTR_ERR(trans);
1711 goto out_reset;
1712 }
1713
b4dc2b8c 1714 ret = btrfs_update_root(trans, root->fs_info->tree_root,
0caa102d
LZ
1715 &root->root_key, &root->root_item);
1716
1717 btrfs_commit_transaction(trans, root);
1718out_reset:
1719 if (ret)
1720 btrfs_set_root_flags(&root->root_item, root_flags);
b9ca0664 1721out_drop_sem:
0caa102d 1722 up_write(&root->fs_info->subvol_sem);
b9ca0664
LB
1723out_drop_write:
1724 mnt_drop_write_file(file);
1725out:
0caa102d
LZ
1726 return ret;
1727}
1728
76dda93c
YZ
1729/*
1730 * helper to check if the subvolume references other subvolumes
1731 */
1732static noinline int may_destroy_subvol(struct btrfs_root *root)
1733{
1734 struct btrfs_path *path;
175a2b87 1735 struct btrfs_dir_item *di;
76dda93c 1736 struct btrfs_key key;
175a2b87 1737 u64 dir_id;
76dda93c
YZ
1738 int ret;
1739
1740 path = btrfs_alloc_path();
1741 if (!path)
1742 return -ENOMEM;
1743
175a2b87
JB
1744 /* Make sure this root isn't set as the default subvol */
1745 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
1746 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path,
1747 dir_id, "default", 7, 0);
1748 if (di && !IS_ERR(di)) {
1749 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1750 if (key.objectid == root->root_key.objectid) {
1751 ret = -ENOTEMPTY;
1752 goto out;
1753 }
1754 btrfs_release_path(path);
1755 }
1756
76dda93c
YZ
1757 key.objectid = root->root_key.objectid;
1758 key.type = BTRFS_ROOT_REF_KEY;
1759 key.offset = (u64)-1;
1760
1761 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1762 &key, path, 0, 0);
1763 if (ret < 0)
1764 goto out;
1765 BUG_ON(ret == 0);
1766
1767 ret = 0;
1768 if (path->slots[0] > 0) {
1769 path->slots[0]--;
1770 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1771 if (key.objectid == root->root_key.objectid &&
1772 key.type == BTRFS_ROOT_REF_KEY)
1773 ret = -ENOTEMPTY;
1774 }
1775out:
1776 btrfs_free_path(path);
1777 return ret;
1778}
1779
ac8e9819
CM
1780static noinline int key_in_sk(struct btrfs_key *key,
1781 struct btrfs_ioctl_search_key *sk)
1782{
abc6e134
CM
1783 struct btrfs_key test;
1784 int ret;
1785
1786 test.objectid = sk->min_objectid;
1787 test.type = sk->min_type;
1788 test.offset = sk->min_offset;
1789
1790 ret = btrfs_comp_cpu_keys(key, &test);
1791 if (ret < 0)
ac8e9819 1792 return 0;
abc6e134
CM
1793
1794 test.objectid = sk->max_objectid;
1795 test.type = sk->max_type;
1796 test.offset = sk->max_offset;
1797
1798 ret = btrfs_comp_cpu_keys(key, &test);
1799 if (ret > 0)
ac8e9819
CM
1800 return 0;
1801 return 1;
1802}
1803
1804static noinline int copy_to_sk(struct btrfs_root *root,
1805 struct btrfs_path *path,
1806 struct btrfs_key *key,
1807 struct btrfs_ioctl_search_key *sk,
1808 char *buf,
1809 unsigned long *sk_offset,
1810 int *num_found)
1811{
1812 u64 found_transid;
1813 struct extent_buffer *leaf;
1814 struct btrfs_ioctl_search_header sh;
1815 unsigned long item_off;
1816 unsigned long item_len;
1817 int nritems;
1818 int i;
1819 int slot;
ac8e9819
CM
1820 int ret = 0;
1821
1822 leaf = path->nodes[0];
1823 slot = path->slots[0];
1824 nritems = btrfs_header_nritems(leaf);
1825
1826 if (btrfs_header_generation(leaf) > sk->max_transid) {
1827 i = nritems;
1828 goto advance_key;
1829 }
1830 found_transid = btrfs_header_generation(leaf);
1831
1832 for (i = slot; i < nritems; i++) {
1833 item_off = btrfs_item_ptr_offset(leaf, i);
1834 item_len = btrfs_item_size_nr(leaf, i);
1835
03b71c6c
GP
1836 btrfs_item_key_to_cpu(leaf, key, i);
1837 if (!key_in_sk(key, sk))
1838 continue;
1839
1840 if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
ac8e9819
CM
1841 item_len = 0;
1842
1843 if (sizeof(sh) + item_len + *sk_offset >
1844 BTRFS_SEARCH_ARGS_BUFSIZE) {
1845 ret = 1;
1846 goto overflow;
1847 }
1848
ac8e9819
CM
1849 sh.objectid = key->objectid;
1850 sh.offset = key->offset;
1851 sh.type = key->type;
1852 sh.len = item_len;
1853 sh.transid = found_transid;
1854
1855 /* copy search result header */
1856 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1857 *sk_offset += sizeof(sh);
1858
1859 if (item_len) {
1860 char *p = buf + *sk_offset;
1861 /* copy the item */
1862 read_extent_buffer(leaf, p,
1863 item_off, item_len);
1864 *sk_offset += item_len;
ac8e9819 1865 }
e2156867 1866 (*num_found)++;
ac8e9819
CM
1867
1868 if (*num_found >= sk->nr_items)
1869 break;
1870 }
1871advance_key:
abc6e134
CM
1872 ret = 0;
1873 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
ac8e9819 1874 key->offset++;
abc6e134
CM
1875 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1876 key->offset = 0;
ac8e9819 1877 key->type++;
abc6e134
CM
1878 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1879 key->offset = 0;
1880 key->type = 0;
ac8e9819 1881 key->objectid++;
abc6e134
CM
1882 } else
1883 ret = 1;
ac8e9819 1884overflow:
ac8e9819
CM
1885 return ret;
1886}
1887
1888static noinline int search_ioctl(struct inode *inode,
1889 struct btrfs_ioctl_search_args *args)
1890{
1891 struct btrfs_root *root;
1892 struct btrfs_key key;
ac8e9819
CM
1893 struct btrfs_path *path;
1894 struct btrfs_ioctl_search_key *sk = &args->key;
1895 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1896 int ret;
1897 int num_found = 0;
1898 unsigned long sk_offset = 0;
1899
1900 path = btrfs_alloc_path();
1901 if (!path)
1902 return -ENOMEM;
1903
1904 if (sk->tree_id == 0) {
1905 /* search the root of the inode that was passed */
1906 root = BTRFS_I(inode)->root;
1907 } else {
1908 key.objectid = sk->tree_id;
1909 key.type = BTRFS_ROOT_ITEM_KEY;
1910 key.offset = (u64)-1;
1911 root = btrfs_read_fs_root_no_name(info, &key);
1912 if (IS_ERR(root)) {
1913 printk(KERN_ERR "could not find root %llu\n",
1914 sk->tree_id);
1915 btrfs_free_path(path);
1916 return -ENOENT;
1917 }
1918 }
1919
1920 key.objectid = sk->min_objectid;
1921 key.type = sk->min_type;
1922 key.offset = sk->min_offset;
1923
ac8e9819
CM
1924 path->keep_locks = 1;
1925
67871254 1926 while (1) {
6174d3cb 1927 ret = btrfs_search_forward(root, &key, path, sk->min_transid);
ac8e9819
CM
1928 if (ret != 0) {
1929 if (ret > 0)
1930 ret = 0;
1931 goto err;
1932 }
1933 ret = copy_to_sk(root, path, &key, sk, args->buf,
1934 &sk_offset, &num_found);
b3b4aa74 1935 btrfs_release_path(path);
ac8e9819
CM
1936 if (ret || num_found >= sk->nr_items)
1937 break;
1938
1939 }
1940 ret = 0;
1941err:
1942 sk->nr_items = num_found;
1943 btrfs_free_path(path);
1944 return ret;
1945}
1946
1947static noinline int btrfs_ioctl_tree_search(struct file *file,
1948 void __user *argp)
1949{
1950 struct btrfs_ioctl_search_args *args;
1951 struct inode *inode;
1952 int ret;
1953
1954 if (!capable(CAP_SYS_ADMIN))
1955 return -EPERM;
1956
2354d08f
JL
1957 args = memdup_user(argp, sizeof(*args));
1958 if (IS_ERR(args))
1959 return PTR_ERR(args);
ac8e9819 1960
496ad9aa 1961 inode = file_inode(file);
ac8e9819
CM
1962 ret = search_ioctl(inode, args);
1963 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1964 ret = -EFAULT;
1965 kfree(args);
1966 return ret;
1967}
1968
98d377a0 1969/*
ac8e9819
CM
1970 * Search INODE_REFs to identify path name of 'dirid' directory
1971 * in a 'tree_id' tree. and sets path name to 'name'.
1972 */
98d377a0
TH
1973static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1974 u64 tree_id, u64 dirid, char *name)
1975{
1976 struct btrfs_root *root;
1977 struct btrfs_key key;
ac8e9819 1978 char *ptr;
98d377a0
TH
1979 int ret = -1;
1980 int slot;
1981 int len;
1982 int total_len = 0;
1983 struct btrfs_inode_ref *iref;
1984 struct extent_buffer *l;
1985 struct btrfs_path *path;
1986
1987 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1988 name[0]='\0';
1989 return 0;
1990 }
1991
1992 path = btrfs_alloc_path();
1993 if (!path)
1994 return -ENOMEM;
1995
ac8e9819 1996 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
98d377a0
TH
1997
1998 key.objectid = tree_id;
1999 key.type = BTRFS_ROOT_ITEM_KEY;
2000 key.offset = (u64)-1;
2001 root = btrfs_read_fs_root_no_name(info, &key);
2002 if (IS_ERR(root)) {
2003 printk(KERN_ERR "could not find root %llu\n", tree_id);
8ad6fcab
CM
2004 ret = -ENOENT;
2005 goto out;
98d377a0
TH
2006 }
2007
2008 key.objectid = dirid;
2009 key.type = BTRFS_INODE_REF_KEY;
8ad6fcab 2010 key.offset = (u64)-1;
98d377a0 2011
67871254 2012 while (1) {
98d377a0
TH
2013 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2014 if (ret < 0)
2015 goto out;
18674c6c
FDBM
2016 else if (ret > 0) {
2017 ret = btrfs_previous_item(root, path, dirid,
2018 BTRFS_INODE_REF_KEY);
2019 if (ret < 0)
2020 goto out;
2021 else if (ret > 0) {
2022 ret = -ENOENT;
2023 goto out;
2024 }
2025 }
98d377a0
TH
2026
2027 l = path->nodes[0];
2028 slot = path->slots[0];
2029 btrfs_item_key_to_cpu(l, &key, slot);
2030
98d377a0
TH
2031 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
2032 len = btrfs_inode_ref_name_len(l, iref);
2033 ptr -= len + 1;
2034 total_len += len + 1;
a696cf35
FDBM
2035 if (ptr < name) {
2036 ret = -ENAMETOOLONG;
98d377a0 2037 goto out;
a696cf35 2038 }
98d377a0
TH
2039
2040 *(ptr + len) = '/';
67871254 2041 read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len);
98d377a0
TH
2042
2043 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
2044 break;
2045
b3b4aa74 2046 btrfs_release_path(path);
98d377a0 2047 key.objectid = key.offset;
8ad6fcab 2048 key.offset = (u64)-1;
98d377a0 2049 dirid = key.objectid;
98d377a0 2050 }
77906a50 2051 memmove(name, ptr, total_len);
67871254 2052 name[total_len] = '\0';
98d377a0
TH
2053 ret = 0;
2054out:
2055 btrfs_free_path(path);
ac8e9819
CM
2056 return ret;
2057}
2058
2059static noinline int btrfs_ioctl_ino_lookup(struct file *file,
2060 void __user *argp)
2061{
2062 struct btrfs_ioctl_ino_lookup_args *args;
2063 struct inode *inode;
2064 int ret;
2065
2066 if (!capable(CAP_SYS_ADMIN))
2067 return -EPERM;
2068
2354d08f
JL
2069 args = memdup_user(argp, sizeof(*args));
2070 if (IS_ERR(args))
2071 return PTR_ERR(args);
c2b96929 2072
496ad9aa 2073 inode = file_inode(file);
ac8e9819 2074
1b53ac4d
CM
2075 if (args->treeid == 0)
2076 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
2077
ac8e9819
CM
2078 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
2079 args->treeid, args->objectid,
2080 args->name);
2081
2082 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
2083 ret = -EFAULT;
2084
2085 kfree(args);
98d377a0
TH
2086 return ret;
2087}
2088
76dda93c
YZ
2089static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2090 void __user *arg)
2091{
54563d41 2092 struct dentry *parent = file->f_path.dentry;
76dda93c
YZ
2093 struct dentry *dentry;
2094 struct inode *dir = parent->d_inode;
2095 struct inode *inode;
2096 struct btrfs_root *root = BTRFS_I(dir)->root;
2097 struct btrfs_root *dest = NULL;
2098 struct btrfs_ioctl_vol_args *vol_args;
2099 struct btrfs_trans_handle *trans;
c58aaad2
MX
2100 struct btrfs_block_rsv block_rsv;
2101 u64 qgroup_reserved;
76dda93c
YZ
2102 int namelen;
2103 int ret;
2104 int err = 0;
2105
76dda93c
YZ
2106 vol_args = memdup_user(arg, sizeof(*vol_args));
2107 if (IS_ERR(vol_args))
2108 return PTR_ERR(vol_args);
2109
2110 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2111 namelen = strlen(vol_args->name);
2112 if (strchr(vol_args->name, '/') ||
2113 strncmp(vol_args->name, "..", namelen) == 0) {
2114 err = -EINVAL;
2115 goto out;
2116 }
2117
a561be71 2118 err = mnt_want_write_file(file);
76dda93c
YZ
2119 if (err)
2120 goto out;
2121
5c50c9b8
DS
2122 err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT);
2123 if (err == -EINTR)
2124 goto out;
76dda93c
YZ
2125 dentry = lookup_one_len(vol_args->name, parent, namelen);
2126 if (IS_ERR(dentry)) {
2127 err = PTR_ERR(dentry);
2128 goto out_unlock_dir;
2129 }
2130
2131 if (!dentry->d_inode) {
2132 err = -ENOENT;
2133 goto out_dput;
2134 }
2135
2136 inode = dentry->d_inode;
4260f7c7 2137 dest = BTRFS_I(inode)->root;
67871254 2138 if (!capable(CAP_SYS_ADMIN)) {
4260f7c7
SW
2139 /*
2140 * Regular user. Only allow this with a special mount
2141 * option, when the user has write+exec access to the
2142 * subvol root, and when rmdir(2) would have been
2143 * allowed.
2144 *
2145 * Note that this is _not_ check that the subvol is
2146 * empty or doesn't contain data that we wouldn't
2147 * otherwise be able to delete.
2148 *
2149 * Users who want to delete empty subvols should try
2150 * rmdir(2).
2151 */
2152 err = -EPERM;
2153 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
2154 goto out_dput;
2155
2156 /*
2157 * Do not allow deletion if the parent dir is the same
2158 * as the dir to be deleted. That means the ioctl
2159 * must be called on the dentry referencing the root
2160 * of the subvol, not a random directory contained
2161 * within it.
2162 */
2163 err = -EINVAL;
2164 if (root == dest)
2165 goto out_dput;
2166
2167 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
2168 if (err)
2169 goto out_dput;
4260f7c7
SW
2170 }
2171
5c39da5b
MX
2172 /* check if subvolume may be deleted by a user */
2173 err = btrfs_may_delete(dir, dentry, 1);
2174 if (err)
2175 goto out_dput;
2176
33345d01 2177 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
2178 err = -EINVAL;
2179 goto out_dput;
2180 }
2181
76dda93c
YZ
2182 mutex_lock(&inode->i_mutex);
2183 err = d_invalidate(dentry);
2184 if (err)
2185 goto out_unlock;
2186
2187 down_write(&root->fs_info->subvol_sem);
2188
2189 err = may_destroy_subvol(dest);
2190 if (err)
2191 goto out_up_write;
2192
c58aaad2
MX
2193 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
2194 /*
2195 * One for dir inode, two for dir entries, two for root
2196 * ref/backref.
2197 */
2198 err = btrfs_subvolume_reserve_metadata(root, &block_rsv,
ee3441b4 2199 5, &qgroup_reserved, true);
c58aaad2
MX
2200 if (err)
2201 goto out_up_write;
2202
a22285a6
YZ
2203 trans = btrfs_start_transaction(root, 0);
2204 if (IS_ERR(trans)) {
2205 err = PTR_ERR(trans);
c58aaad2 2206 goto out_release;
a22285a6 2207 }
c58aaad2
MX
2208 trans->block_rsv = &block_rsv;
2209 trans->bytes_reserved = block_rsv.size;
a22285a6 2210
76dda93c
YZ
2211 ret = btrfs_unlink_subvol(trans, root, dir,
2212 dest->root_key.objectid,
2213 dentry->d_name.name,
2214 dentry->d_name.len);
79787eaa
JM
2215 if (ret) {
2216 err = ret;
2217 btrfs_abort_transaction(trans, root, ret);
2218 goto out_end_trans;
2219 }
76dda93c
YZ
2220
2221 btrfs_record_root_in_trans(trans, dest);
2222
2223 memset(&dest->root_item.drop_progress, 0,
2224 sizeof(dest->root_item.drop_progress));
2225 dest->root_item.drop_level = 0;
2226 btrfs_set_root_refs(&dest->root_item, 0);
2227
d68fc57b
YZ
2228 if (!xchg(&dest->orphan_item_inserted, 1)) {
2229 ret = btrfs_insert_orphan_item(trans,
2230 root->fs_info->tree_root,
2231 dest->root_key.objectid);
79787eaa
JM
2232 if (ret) {
2233 btrfs_abort_transaction(trans, root, ret);
2234 err = ret;
2235 goto out_end_trans;
2236 }
d68fc57b 2237 }
dd5f9615
SB
2238
2239 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2240 dest->root_item.uuid, BTRFS_UUID_KEY_SUBVOL,
2241 dest->root_key.objectid);
2242 if (ret && ret != -ENOENT) {
2243 btrfs_abort_transaction(trans, root, ret);
2244 err = ret;
2245 goto out_end_trans;
2246 }
2247 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2248 ret = btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
2249 dest->root_item.received_uuid,
2250 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2251 dest->root_key.objectid);
2252 if (ret && ret != -ENOENT) {
2253 btrfs_abort_transaction(trans, root, ret);
2254 err = ret;
2255 goto out_end_trans;
2256 }
2257 }
2258
79787eaa 2259out_end_trans:
c58aaad2
MX
2260 trans->block_rsv = NULL;
2261 trans->bytes_reserved = 0;
531cb13f 2262 ret = btrfs_end_transaction(trans, root);
79787eaa
JM
2263 if (ret && !err)
2264 err = ret;
76dda93c 2265 inode->i_flags |= S_DEAD;
c58aaad2
MX
2266out_release:
2267 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
76dda93c
YZ
2268out_up_write:
2269 up_write(&root->fs_info->subvol_sem);
2270out_unlock:
2271 mutex_unlock(&inode->i_mutex);
2272 if (!err) {
efefb143 2273 shrink_dcache_sb(root->fs_info->sb);
76dda93c
YZ
2274 btrfs_invalidate_inodes(dest);
2275 d_delete(dentry);
fa6ac876
LB
2276
2277 /* the last ref */
2278 if (dest->cache_inode) {
2279 iput(dest->cache_inode);
2280 dest->cache_inode = NULL;
2281 }
76dda93c
YZ
2282 }
2283out_dput:
2284 dput(dentry);
2285out_unlock_dir:
2286 mutex_unlock(&dir->i_mutex);
2a79f17e 2287 mnt_drop_write_file(file);
76dda93c
YZ
2288out:
2289 kfree(vol_args);
2290 return err;
2291}
2292
1e701a32 2293static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
f46b5a66 2294{
496ad9aa 2295 struct inode *inode = file_inode(file);
f46b5a66 2296 struct btrfs_root *root = BTRFS_I(inode)->root;
1e701a32 2297 struct btrfs_ioctl_defrag_range_args *range;
c146afad
YZ
2298 int ret;
2299
25122d15
ID
2300 ret = mnt_want_write_file(file);
2301 if (ret)
2302 return ret;
b83cc969 2303
25122d15
ID
2304 if (btrfs_root_readonly(root)) {
2305 ret = -EROFS;
2306 goto out;
5ac00add 2307 }
f46b5a66
CH
2308
2309 switch (inode->i_mode & S_IFMT) {
2310 case S_IFDIR:
e441d54d
CM
2311 if (!capable(CAP_SYS_ADMIN)) {
2312 ret = -EPERM;
2313 goto out;
2314 }
de78b51a 2315 ret = btrfs_defrag_root(root);
8929ecfa
YZ
2316 if (ret)
2317 goto out;
de78b51a 2318 ret = btrfs_defrag_root(root->fs_info->extent_root);
f46b5a66
CH
2319 break;
2320 case S_IFREG:
e441d54d
CM
2321 if (!(file->f_mode & FMODE_WRITE)) {
2322 ret = -EINVAL;
2323 goto out;
2324 }
1e701a32
CM
2325
2326 range = kzalloc(sizeof(*range), GFP_KERNEL);
2327 if (!range) {
2328 ret = -ENOMEM;
2329 goto out;
2330 }
2331
2332 if (argp) {
2333 if (copy_from_user(range, argp,
2334 sizeof(*range))) {
2335 ret = -EFAULT;
2336 kfree(range);
683be16e 2337 goto out;
1e701a32
CM
2338 }
2339 /* compression requires us to start the IO */
2340 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
2341 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
2342 range->extent_thresh = (u32)-1;
2343 }
2344 } else {
2345 /* the rest are all set to zero by kzalloc */
2346 range->len = (u64)-1;
2347 }
496ad9aa 2348 ret = btrfs_defrag_file(file_inode(file), file,
4cb5300b
CM
2349 range, 0, 0);
2350 if (ret > 0)
2351 ret = 0;
1e701a32 2352 kfree(range);
f46b5a66 2353 break;
8929ecfa
YZ
2354 default:
2355 ret = -EINVAL;
f46b5a66 2356 }
e441d54d 2357out:
25122d15 2358 mnt_drop_write_file(file);
e441d54d 2359 return ret;
f46b5a66
CH
2360}
2361
b2950863 2362static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
f46b5a66
CH
2363{
2364 struct btrfs_ioctl_vol_args *vol_args;
2365 int ret;
2366
e441d54d
CM
2367 if (!capable(CAP_SYS_ADMIN))
2368 return -EPERM;
2369
5ac00add
SB
2370 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2371 1)) {
e57138b3 2372 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
c9e9f97b
ID
2373 }
2374
5ac00add 2375 mutex_lock(&root->fs_info->volume_mutex);
dae7b665 2376 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2377 if (IS_ERR(vol_args)) {
2378 ret = PTR_ERR(vol_args);
2379 goto out;
2380 }
f46b5a66 2381
5516e595 2382 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66
CH
2383 ret = btrfs_init_new_device(root, vol_args->name);
2384
f46b5a66 2385 kfree(vol_args);
c9e9f97b
ID
2386out:
2387 mutex_unlock(&root->fs_info->volume_mutex);
5ac00add 2388 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
f46b5a66
CH
2389 return ret;
2390}
2391
da24927b 2392static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
f46b5a66 2393{
496ad9aa 2394 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
f46b5a66
CH
2395 struct btrfs_ioctl_vol_args *vol_args;
2396 int ret;
2397
e441d54d
CM
2398 if (!capable(CAP_SYS_ADMIN))
2399 return -EPERM;
2400
da24927b
MX
2401 ret = mnt_want_write_file(file);
2402 if (ret)
2403 return ret;
c146afad 2404
dae7b665 2405 vol_args = memdup_user(arg, sizeof(*vol_args));
c9e9f97b
ID
2406 if (IS_ERR(vol_args)) {
2407 ret = PTR_ERR(vol_args);
2408 goto out;
2409 }
f46b5a66 2410
5516e595 2411 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
f46b5a66 2412
183860f6
AJ
2413 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running,
2414 1)) {
2415 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2416 goto out;
2417 }
2418
2419 mutex_lock(&root->fs_info->volume_mutex);
2420 ret = btrfs_rm_device(root, vol_args->name);
c9e9f97b 2421 mutex_unlock(&root->fs_info->volume_mutex);
5ac00add 2422 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0);
183860f6
AJ
2423
2424out:
2425 kfree(vol_args);
4ac20c70 2426 mnt_drop_write_file(file);
f46b5a66
CH
2427 return ret;
2428}
2429
475f6387
JS
2430static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2431{
027ed2f0 2432 struct btrfs_ioctl_fs_info_args *fi_args;
475f6387
JS
2433 struct btrfs_device *device;
2434 struct btrfs_device *next;
2435 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
027ed2f0 2436 int ret = 0;
475f6387
JS
2437
2438 if (!capable(CAP_SYS_ADMIN))
2439 return -EPERM;
2440
027ed2f0
LZ
2441 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
2442 if (!fi_args)
2443 return -ENOMEM;
2444
f7171750 2445 mutex_lock(&fs_devices->device_list_mutex);
027ed2f0
LZ
2446 fi_args->num_devices = fs_devices->num_devices;
2447 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid));
475f6387 2448
475f6387 2449 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
027ed2f0
LZ
2450 if (device->devid > fi_args->max_id)
2451 fi_args->max_id = device->devid;
475f6387
JS
2452 }
2453 mutex_unlock(&fs_devices->device_list_mutex);
2454
027ed2f0
LZ
2455 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2456 ret = -EFAULT;
475f6387 2457
027ed2f0
LZ
2458 kfree(fi_args);
2459 return ret;
475f6387
JS
2460}
2461
2462static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2463{
2464 struct btrfs_ioctl_dev_info_args *di_args;
2465 struct btrfs_device *dev;
2466 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2467 int ret = 0;
2468 char *s_uuid = NULL;
475f6387
JS
2469
2470 if (!capable(CAP_SYS_ADMIN))
2471 return -EPERM;
2472
2473 di_args = memdup_user(arg, sizeof(*di_args));
2474 if (IS_ERR(di_args))
2475 return PTR_ERR(di_args);
2476
dd5f9615 2477 if (!btrfs_is_empty_uuid(di_args->uuid))
475f6387
JS
2478 s_uuid = di_args->uuid;
2479
2480 mutex_lock(&fs_devices->device_list_mutex);
aa1b8cd4 2481 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL);
475f6387
JS
2482
2483 if (!dev) {
2484 ret = -ENODEV;
2485 goto out;
2486 }
2487
2488 di_args->devid = dev->devid;
2489 di_args->bytes_used = dev->bytes_used;
2490 di_args->total_bytes = dev->total_bytes;
2491 memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid));
a27202fb 2492 if (dev->name) {
606686ee
JB
2493 struct rcu_string *name;
2494
2495 rcu_read_lock();
2496 name = rcu_dereference(dev->name);
2497 strncpy(di_args->path, name->str, sizeof(di_args->path));
2498 rcu_read_unlock();
a27202fb
JM
2499 di_args->path[sizeof(di_args->path) - 1] = 0;
2500 } else {
99ba55ad 2501 di_args->path[0] = '\0';
a27202fb 2502 }
475f6387
JS
2503
2504out:
55793c0d 2505 mutex_unlock(&fs_devices->device_list_mutex);
475f6387
JS
2506 if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args)))
2507 ret = -EFAULT;
2508
2509 kfree(di_args);
2510 return ret;
2511}
2512
416161db
MF
2513static struct page *extent_same_get_page(struct inode *inode, u64 off)
2514{
2515 struct page *page;
2516 pgoff_t index;
2517 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2518
2519 index = off >> PAGE_CACHE_SHIFT;
2520
2521 page = grab_cache_page(inode->i_mapping, index);
2522 if (!page)
2523 return NULL;
2524
2525 if (!PageUptodate(page)) {
2526 if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2527 0))
2528 return NULL;
2529 lock_page(page);
2530 if (!PageUptodate(page)) {
2531 unlock_page(page);
2532 page_cache_release(page);
2533 return NULL;
2534 }
2535 }
2536 unlock_page(page);
2537
2538 return page;
2539}
2540
77fe20dc
MF
2541static inline void lock_extent_range(struct inode *inode, u64 off, u64 len)
2542{
2543 /* do any pending delalloc/csum calc on src, one way or
2544 another, and lock file content */
2545 while (1) {
2546 struct btrfs_ordered_extent *ordered;
2547 lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2548 ordered = btrfs_lookup_first_ordered_extent(inode,
2549 off + len - 1);
2550 if (!ordered &&
2551 !test_range_bit(&BTRFS_I(inode)->io_tree, off,
2552 off + len - 1, EXTENT_DELALLOC, 0, NULL))
2553 break;
2554 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
2555 if (ordered)
2556 btrfs_put_ordered_extent(ordered);
2557 btrfs_wait_ordered_range(inode, off, len);
2558 }
2559}
2560
416161db
MF
2561static void btrfs_double_unlock(struct inode *inode1, u64 loff1,
2562 struct inode *inode2, u64 loff2, u64 len)
2563{
2564 unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1);
2565 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
2566
2567 mutex_unlock(&inode1->i_mutex);
2568 mutex_unlock(&inode2->i_mutex);
2569}
2570
2571static void btrfs_double_lock(struct inode *inode1, u64 loff1,
2572 struct inode *inode2, u64 loff2, u64 len)
2573{
2574 if (inode1 < inode2) {
2575 swap(inode1, inode2);
2576 swap(loff1, loff2);
2577 }
2578
2579 mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT);
2580 lock_extent_range(inode1, loff1, len);
2581 if (inode1 != inode2) {
2582 mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD);
2583 lock_extent_range(inode2, loff2, len);
2584 }
2585}
2586
2587static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst,
2588 u64 dst_loff, u64 len)
2589{
2590 int ret = 0;
2591 struct page *src_page, *dst_page;
2592 unsigned int cmp_len = PAGE_CACHE_SIZE;
2593 void *addr, *dst_addr;
2594
2595 while (len) {
2596 if (len < PAGE_CACHE_SIZE)
2597 cmp_len = len;
2598
2599 src_page = extent_same_get_page(src, loff);
2600 if (!src_page)
2601 return -EINVAL;
2602 dst_page = extent_same_get_page(dst, dst_loff);
2603 if (!dst_page) {
2604 page_cache_release(src_page);
2605 return -EINVAL;
2606 }
2607 addr = kmap_atomic(src_page);
2608 dst_addr = kmap_atomic(dst_page);
2609
2610 flush_dcache_page(src_page);
2611 flush_dcache_page(dst_page);
2612
2613 if (memcmp(addr, dst_addr, cmp_len))
2614 ret = BTRFS_SAME_DATA_DIFFERS;
2615
2616 kunmap_atomic(addr);
2617 kunmap_atomic(dst_addr);
2618 page_cache_release(src_page);
2619 page_cache_release(dst_page);
2620
2621 if (ret)
2622 break;
2623
2624 loff += cmp_len;
2625 dst_loff += cmp_len;
2626 len -= cmp_len;
2627 }
2628
2629 return ret;
2630}
2631
2632static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len)
2633{
2634 u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize;
2635
2636 if (off + len > inode->i_size || off + len < off)
2637 return -EINVAL;
2638 /* Check that we are block aligned - btrfs_clone() requires this */
2639 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs))
2640 return -EINVAL;
2641
2642 return 0;
2643}
2644
2645static int btrfs_extent_same(struct inode *src, u64 loff, u64 len,
2646 struct inode *dst, u64 dst_loff)
2647{
2648 int ret;
2649
2650 /*
2651 * btrfs_clone() can't handle extents in the same file
2652 * yet. Once that works, we can drop this check and replace it
2653 * with a check for the same inode, but overlapping extents.
2654 */
2655 if (src == dst)
2656 return -EINVAL;
2657
2658 btrfs_double_lock(src, loff, dst, dst_loff, len);
2659
2660 ret = extent_same_check_offsets(src, loff, len);
2661 if (ret)
2662 goto out_unlock;
2663
2664 ret = extent_same_check_offsets(dst, dst_loff, len);
2665 if (ret)
2666 goto out_unlock;
2667
2668 /* don't make the dst file partly checksummed */
2669 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
2670 (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) {
2671 ret = -EINVAL;
2672 goto out_unlock;
2673 }
2674
2675 ret = btrfs_cmp_data(src, loff, dst, dst_loff, len);
2676 if (ret == 0)
2677 ret = btrfs_clone(src, dst, loff, len, len, dst_loff);
2678
2679out_unlock:
2680 btrfs_double_unlock(src, loff, dst, dst_loff, len);
2681
2682 return ret;
2683}
2684
2685#define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024)
2686
2687static long btrfs_ioctl_file_extent_same(struct file *file,
2688 void __user *argp)
2689{
cbf8b8ca
MF
2690 struct btrfs_ioctl_same_args tmp;
2691 struct btrfs_ioctl_same_args *same;
2692 struct btrfs_ioctl_same_extent_info *info;
416161db
MF
2693 struct inode *src = file->f_dentry->d_inode;
2694 struct file *dst_file = NULL;
2695 struct inode *dst;
2696 u64 off;
2697 u64 len;
2698 int i;
2699 int ret;
cbf8b8ca 2700 unsigned long size;
416161db
MF
2701 u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize;
2702 bool is_admin = capable(CAP_SYS_ADMIN);
2703
2704 if (!(file->f_mode & FMODE_READ))
2705 return -EINVAL;
2706
2707 ret = mnt_want_write_file(file);
2708 if (ret)
2709 return ret;
2710
cbf8b8ca 2711 if (copy_from_user(&tmp,
416161db 2712 (struct btrfs_ioctl_same_args __user *)argp,
cbf8b8ca 2713 sizeof(tmp))) {
416161db
MF
2714 ret = -EFAULT;
2715 goto out;
2716 }
2717
cbf8b8ca
MF
2718 size = sizeof(tmp) +
2719 tmp.dest_count * sizeof(struct btrfs_ioctl_same_extent_info);
2720
229eed43 2721 same = memdup_user((struct btrfs_ioctl_same_args __user *)argp, size);
cbf8b8ca 2722
229eed43
GB
2723 if (IS_ERR(same)) {
2724 ret = PTR_ERR(same);
cbf8b8ca
MF
2725 goto out;
2726 }
2727
2728 off = same->logical_offset;
2729 len = same->length;
416161db
MF
2730
2731 /*
2732 * Limit the total length we will dedupe for each operation.
2733 * This is intended to bound the total time spent in this
2734 * ioctl to something sane.
2735 */
2736 if (len > BTRFS_MAX_DEDUPE_LEN)
2737 len = BTRFS_MAX_DEDUPE_LEN;
2738
2739 if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) {
2740 /*
2741 * Btrfs does not support blocksize < page_size. As a
2742 * result, btrfs_cmp_data() won't correctly handle
2743 * this situation without an update.
2744 */
2745 ret = -EINVAL;
2746 goto out;
2747 }
2748
2749 ret = -EISDIR;
2750 if (S_ISDIR(src->i_mode))
2751 goto out;
2752
2753 ret = -EACCES;
2754 if (!S_ISREG(src->i_mode))
2755 goto out;
2756
cbf8b8ca
MF
2757 /* pre-format output fields to sane values */
2758 for (i = 0; i < same->dest_count; i++) {
2759 same->info[i].bytes_deduped = 0ULL;
2760 same->info[i].status = 0;
2761 }
416161db 2762
cbf8b8ca
MF
2763 ret = 0;
2764 for (i = 0; i < same->dest_count; i++) {
2765 info = &same->info[i];
416161db 2766
cbf8b8ca 2767 dst_file = fget(info->fd);
416161db 2768 if (!dst_file) {
cbf8b8ca 2769 info->status = -EBADF;
416161db
MF
2770 goto next;
2771 }
2772
2773 if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
cbf8b8ca 2774 info->status = -EINVAL;
416161db
MF
2775 goto next;
2776 }
2777
cbf8b8ca 2778 info->status = -EXDEV;
416161db
MF
2779 if (file->f_path.mnt != dst_file->f_path.mnt)
2780 goto next;
2781
2782 dst = dst_file->f_dentry->d_inode;
2783 if (src->i_sb != dst->i_sb)
2784 goto next;
2785
2786 if (S_ISDIR(dst->i_mode)) {
cbf8b8ca 2787 info->status = -EISDIR;
416161db
MF
2788 goto next;
2789 }
2790
2791 if (!S_ISREG(dst->i_mode)) {
cbf8b8ca 2792 info->status = -EACCES;
416161db
MF
2793 goto next;
2794 }
2795
cbf8b8ca
MF
2796 info->status = btrfs_extent_same(src, off, len, dst,
2797 info->logical_offset);
2798 if (info->status == 0)
2799 info->bytes_deduped += len;
416161db
MF
2800
2801next:
2802 if (dst_file)
2803 fput(dst_file);
416161db
MF
2804 }
2805
cbf8b8ca
MF
2806 ret = copy_to_user(argp, same, size);
2807 if (ret)
2808 ret = -EFAULT;
2809
416161db
MF
2810out:
2811 mnt_drop_write_file(file);
2812 return ret;
2813}
2814
32b7c687
MF
2815/**
2816 * btrfs_clone() - clone a range from inode file to another
2817 *
2818 * @src: Inode to clone from
2819 * @inode: Inode to clone to
2820 * @off: Offset within source to start clone from
2821 * @olen: Original length, passed by user, of range to clone
2822 * @olen_aligned: Block-aligned value of olen, extent_same uses
2823 * identical values here
2824 * @destoff: Offset within @inode to start clone
2825 */
2826static int btrfs_clone(struct inode *src, struct inode *inode,
2827 u64 off, u64 olen, u64 olen_aligned, u64 destoff)
f46b5a66 2828{
f46b5a66 2829 struct btrfs_root *root = BTRFS_I(inode)->root;
32b7c687 2830 struct btrfs_path *path = NULL;
f46b5a66 2831 struct extent_buffer *leaf;
32b7c687
MF
2832 struct btrfs_trans_handle *trans;
2833 char *buf = NULL;
ae01a0ab 2834 struct btrfs_key key;
f46b5a66
CH
2835 u32 nritems;
2836 int slot;
ae01a0ab 2837 int ret;
32b7c687 2838 u64 len = olen_aligned;
ae01a0ab
YZ
2839
2840 ret = -ENOMEM;
2841 buf = vmalloc(btrfs_level_size(root, 0));
2842 if (!buf)
32b7c687 2843 return ret;
ae01a0ab
YZ
2844
2845 path = btrfs_alloc_path();
2846 if (!path) {
2847 vfree(buf);
32b7c687 2848 return ret;
d525e8ab
LZ
2849 }
2850
32b7c687 2851 path->reada = 2;
c5c9cd4d 2852 /* clone data */
33345d01 2853 key.objectid = btrfs_ino(src);
ae01a0ab
YZ
2854 key.type = BTRFS_EXTENT_DATA_KEY;
2855 key.offset = 0;
f46b5a66
CH
2856
2857 while (1) {
2858 /*
2859 * note the key will change type as we walk through the
2860 * tree.
2861 */
362a20c5
DS
2862 ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path,
2863 0, 0);
f46b5a66
CH
2864 if (ret < 0)
2865 goto out;
2866
ae01a0ab
YZ
2867 nritems = btrfs_header_nritems(path->nodes[0]);
2868 if (path->slots[0] >= nritems) {
362a20c5 2869 ret = btrfs_next_leaf(BTRFS_I(src)->root, path);
f46b5a66
CH
2870 if (ret < 0)
2871 goto out;
2872 if (ret > 0)
2873 break;
ae01a0ab 2874 nritems = btrfs_header_nritems(path->nodes[0]);
f46b5a66
CH
2875 }
2876 leaf = path->nodes[0];
2877 slot = path->slots[0];
f46b5a66 2878
ae01a0ab 2879 btrfs_item_key_to_cpu(leaf, &key, slot);
d20f7043 2880 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
33345d01 2881 key.objectid != btrfs_ino(src))
f46b5a66
CH
2882 break;
2883
c5c9cd4d
SW
2884 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
2885 struct btrfs_file_extent_item *extent;
2886 int type;
31840ae1
ZY
2887 u32 size;
2888 struct btrfs_key new_key;
c5c9cd4d
SW
2889 u64 disko = 0, diskl = 0;
2890 u64 datao = 0, datal = 0;
2891 u8 comp;
b5384d48 2892 u64 endoff;
31840ae1
ZY
2893
2894 size = btrfs_item_size_nr(leaf, slot);
2895 read_extent_buffer(leaf, buf,
2896 btrfs_item_ptr_offset(leaf, slot),
2897 size);
c5c9cd4d
SW
2898
2899 extent = btrfs_item_ptr(leaf, slot,
2900 struct btrfs_file_extent_item);
2901 comp = btrfs_file_extent_compression(leaf, extent);
2902 type = btrfs_file_extent_type(leaf, extent);
c8a894d7
CM
2903 if (type == BTRFS_FILE_EXTENT_REG ||
2904 type == BTRFS_FILE_EXTENT_PREALLOC) {
d397712b
CM
2905 disko = btrfs_file_extent_disk_bytenr(leaf,
2906 extent);
2907 diskl = btrfs_file_extent_disk_num_bytes(leaf,
2908 extent);
c5c9cd4d 2909 datao = btrfs_file_extent_offset(leaf, extent);
d397712b
CM
2910 datal = btrfs_file_extent_num_bytes(leaf,
2911 extent);
c5c9cd4d
SW
2912 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2913 /* take upper bound, may be compressed */
2914 datal = btrfs_file_extent_ram_bytes(leaf,
2915 extent);
2916 }
b3b4aa74 2917 btrfs_release_path(path);
31840ae1 2918
050006a7 2919 if (key.offset + datal <= off ||
aa42ffd9 2920 key.offset >= off + len - 1)
c5c9cd4d
SW
2921 goto next;
2922
31840ae1 2923 memcpy(&new_key, &key, sizeof(new_key));
33345d01 2924 new_key.objectid = btrfs_ino(inode);
4d728ec7
LZ
2925 if (off <= key.offset)
2926 new_key.offset = key.offset + destoff - off;
2927 else
2928 new_key.offset = destoff;
31840ae1 2929
b6f3409b
SW
2930 /*
2931 * 1 - adjusting old extent (we may have to split it)
2932 * 1 - add new extent
2933 * 1 - inode update
2934 */
2935 trans = btrfs_start_transaction(root, 3);
a22285a6
YZ
2936 if (IS_ERR(trans)) {
2937 ret = PTR_ERR(trans);
2938 goto out;
2939 }
2940
c8a894d7
CM
2941 if (type == BTRFS_FILE_EXTENT_REG ||
2942 type == BTRFS_FILE_EXTENT_PREALLOC) {
d72c0842
LZ
2943 /*
2944 * a | --- range to clone ---| b
2945 * | ------------- extent ------------- |
2946 */
2947
2948 /* substract range b */
2949 if (key.offset + datal > off + len)
2950 datal = off + len - key.offset;
2951
2952 /* substract range a */
a22285a6
YZ
2953 if (off > key.offset) {
2954 datao += off - key.offset;
2955 datal -= off - key.offset;
2956 }
2957
5dc562c5 2958 ret = btrfs_drop_extents(trans, root, inode,
a22285a6
YZ
2959 new_key.offset,
2960 new_key.offset + datal,
2671485d 2961 1);
79787eaa
JM
2962 if (ret) {
2963 btrfs_abort_transaction(trans, root,
2964 ret);
2965 btrfs_end_transaction(trans, root);
2966 goto out;
2967 }
a22285a6 2968
c5c9cd4d
SW
2969 ret = btrfs_insert_empty_item(trans, root, path,
2970 &new_key, size);
79787eaa
JM
2971 if (ret) {
2972 btrfs_abort_transaction(trans, root,
2973 ret);
2974 btrfs_end_transaction(trans, root);
2975 goto out;
2976 }
c5c9cd4d
SW
2977
2978 leaf = path->nodes[0];
2979 slot = path->slots[0];
2980 write_extent_buffer(leaf, buf,
31840ae1
ZY
2981 btrfs_item_ptr_offset(leaf, slot),
2982 size);
ae01a0ab 2983
c5c9cd4d 2984 extent = btrfs_item_ptr(leaf, slot,
f46b5a66 2985 struct btrfs_file_extent_item);
c5c9cd4d 2986
c5c9cd4d
SW
2987 /* disko == 0 means it's a hole */
2988 if (!disko)
2989 datao = 0;
c5c9cd4d
SW
2990
2991 btrfs_set_file_extent_offset(leaf, extent,
2992 datao);
2993 btrfs_set_file_extent_num_bytes(leaf, extent,
2994 datal);
2995 if (disko) {
2996 inode_add_bytes(inode, datal);
ae01a0ab 2997 ret = btrfs_inc_extent_ref(trans, root,
5d4f98a2
YZ
2998 disko, diskl, 0,
2999 root->root_key.objectid,
33345d01 3000 btrfs_ino(inode),
66d7e7f0
AJ
3001 new_key.offset - datao,
3002 0);
79787eaa
JM
3003 if (ret) {
3004 btrfs_abort_transaction(trans,
3005 root,
3006 ret);
3007 btrfs_end_transaction(trans,
3008 root);
3009 goto out;
3010
3011 }
f46b5a66 3012 }
c5c9cd4d
SW
3013 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
3014 u64 skip = 0;
3015 u64 trim = 0;
3016 if (off > key.offset) {
3017 skip = off - key.offset;
3018 new_key.offset += skip;
3019 }
d397712b 3020
aa42ffd9
LB
3021 if (key.offset + datal > off + len)
3022 trim = key.offset + datal - (off + len);
d397712b 3023
c5c9cd4d 3024 if (comp && (skip || trim)) {
c5c9cd4d 3025 ret = -EINVAL;
a22285a6 3026 btrfs_end_transaction(trans, root);
c5c9cd4d
SW
3027 goto out;
3028 }
3029 size -= skip + trim;
3030 datal -= skip + trim;
a22285a6 3031
5dc562c5 3032 ret = btrfs_drop_extents(trans, root, inode,
a22285a6
YZ
3033 new_key.offset,
3034 new_key.offset + datal,
2671485d 3035 1);
79787eaa
JM
3036 if (ret) {
3037 btrfs_abort_transaction(trans, root,
3038 ret);
3039 btrfs_end_transaction(trans, root);
3040 goto out;
3041 }
a22285a6 3042
c5c9cd4d
SW
3043 ret = btrfs_insert_empty_item(trans, root, path,
3044 &new_key, size);
79787eaa
JM
3045 if (ret) {
3046 btrfs_abort_transaction(trans, root,
3047 ret);
3048 btrfs_end_transaction(trans, root);
3049 goto out;
3050 }
c5c9cd4d
SW
3051
3052 if (skip) {
d397712b
CM
3053 u32 start =
3054 btrfs_file_extent_calc_inline_size(0);
c5c9cd4d
SW
3055 memmove(buf+start, buf+start+skip,
3056 datal);
3057 }
3058
3059 leaf = path->nodes[0];
3060 slot = path->slots[0];
3061 write_extent_buffer(leaf, buf,
3062 btrfs_item_ptr_offset(leaf, slot),
3063 size);
3064 inode_add_bytes(inode, datal);
f46b5a66 3065 }
c5c9cd4d
SW
3066
3067 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 3068 btrfs_release_path(path);
c5c9cd4d 3069
0c4d2d95 3070 inode_inc_iversion(inode);
a22285a6 3071 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
b5384d48
SW
3072
3073 /*
3074 * we round up to the block size at eof when
3075 * determining which extents to clone above,
3076 * but shouldn't round up the file size
3077 */
3078 endoff = new_key.offset + datal;
5f3888ff
LZ
3079 if (endoff > destoff+olen)
3080 endoff = destoff+olen;
b5384d48
SW
3081 if (endoff > inode->i_size)
3082 btrfs_i_size_write(inode, endoff);
3083
a22285a6 3084 ret = btrfs_update_inode(trans, root, inode);
79787eaa
JM
3085 if (ret) {
3086 btrfs_abort_transaction(trans, root, ret);
3087 btrfs_end_transaction(trans, root);
3088 goto out;
3089 }
3090 ret = btrfs_end_transaction(trans, root);
a22285a6 3091 }
d397712b 3092next:
b3b4aa74 3093 btrfs_release_path(path);
f46b5a66 3094 key.offset++;
f46b5a66 3095 }
f46b5a66 3096 ret = 0;
32b7c687 3097
f46b5a66 3098out:
b3b4aa74 3099 btrfs_release_path(path);
32b7c687
MF
3100 btrfs_free_path(path);
3101 vfree(buf);
3102 return ret;
3103}
3104
3105static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
3106 u64 off, u64 olen, u64 destoff)
3107{
54563d41 3108 struct inode *inode = file_inode(file);
32b7c687
MF
3109 struct btrfs_root *root = BTRFS_I(inode)->root;
3110 struct fd src_file;
3111 struct inode *src;
3112 int ret;
3113 u64 len = olen;
3114 u64 bs = root->fs_info->sb->s_blocksize;
3115 int same_inode = 0;
3116
3117 /*
3118 * TODO:
3119 * - split compressed inline extents. annoying: we need to
3120 * decompress into destination's address_space (the file offset
3121 * may change, so source mapping won't do), then recompress (or
3122 * otherwise reinsert) a subrange.
3123 * - allow ranges within the same file to be cloned (provided
3124 * they don't overlap)?
3125 */
3126
3127 /* the destination must be opened for writing */
3128 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
3129 return -EINVAL;
3130
3131 if (btrfs_root_readonly(root))
3132 return -EROFS;
3133
3134 ret = mnt_want_write_file(file);
3135 if (ret)
3136 return ret;
3137
3138 src_file = fdget(srcfd);
3139 if (!src_file.file) {
3140 ret = -EBADF;
3141 goto out_drop_write;
3142 }
3143
3144 ret = -EXDEV;
3145 if (src_file.file->f_path.mnt != file->f_path.mnt)
3146 goto out_fput;
3147
3148 src = file_inode(src_file.file);
3149
3150 ret = -EINVAL;
3151 if (src == inode)
3152 same_inode = 1;
3153
3154 /* the src must be open for reading */
3155 if (!(src_file.file->f_mode & FMODE_READ))
3156 goto out_fput;
3157
3158 /* don't make the dst file partly checksummed */
3159 if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) !=
3160 (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
3161 goto out_fput;
3162
3163 ret = -EISDIR;
3164 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
3165 goto out_fput;
3166
3167 ret = -EXDEV;
3168 if (src->i_sb != inode->i_sb)
3169 goto out_fput;
3170
3171 if (!same_inode) {
3172 if (inode < src) {
3173 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
3174 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
3175 } else {
3176 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
3177 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
3178 }
3179 } else {
3180 mutex_lock(&src->i_mutex);
3181 }
3182
3183 /* determine range to clone */
3184 ret = -EINVAL;
3185 if (off + len > src->i_size || off + len < off)
3186 goto out_unlock;
3187 if (len == 0)
3188 olen = len = src->i_size - off;
3189 /* if we extend to eof, continue to block boundary */
3190 if (off + len == src->i_size)
3191 len = ALIGN(src->i_size, bs) - off;
3192
3193 /* verify the end result is block aligned */
3194 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
3195 !IS_ALIGNED(destoff, bs))
3196 goto out_unlock;
3197
3198 /* verify if ranges are overlapped within the same file */
3199 if (same_inode) {
3200 if (destoff + len > off && destoff < off + len)
3201 goto out_unlock;
3202 }
3203
3204 if (destoff > inode->i_size) {
3205 ret = btrfs_cont_expand(inode, inode->i_size, destoff);
3206 if (ret)
3207 goto out_unlock;
3208 }
3209
3210 /* truncate page cache pages from target inode range */
3211 truncate_inode_pages_range(&inode->i_data, destoff,
3212 PAGE_CACHE_ALIGN(destoff + len) - 1);
3213
3214 lock_extent_range(src, off, len);
3215
3216 ret = btrfs_clone(src, inode, off, olen, len, destoff);
3217
aa42ffd9 3218 unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1);
f46b5a66
CH
3219out_unlock:
3220 mutex_unlock(&src->i_mutex);
a96fbc72
LB
3221 if (!same_inode)
3222 mutex_unlock(&inode->i_mutex);
f46b5a66 3223out_fput:
2903ff01 3224 fdput(src_file);
ab67b7c1 3225out_drop_write:
2a79f17e 3226 mnt_drop_write_file(file);
f46b5a66
CH
3227 return ret;
3228}
3229
7a865e8a 3230static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
c5c9cd4d
SW
3231{
3232 struct btrfs_ioctl_clone_range_args args;
3233
7a865e8a 3234 if (copy_from_user(&args, argp, sizeof(args)))
c5c9cd4d
SW
3235 return -EFAULT;
3236 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
3237 args.src_length, args.dest_offset);
3238}
3239
f46b5a66
CH
3240/*
3241 * there are many ways the trans_start and trans_end ioctls can lead
3242 * to deadlocks. They should only be used by applications that
3243 * basically own the machine, and have a very in depth understanding
3244 * of all the possible deadlocks and enospc problems.
3245 */
b2950863 3246static long btrfs_ioctl_trans_start(struct file *file)
f46b5a66 3247{
496ad9aa 3248 struct inode *inode = file_inode(file);
f46b5a66
CH
3249 struct btrfs_root *root = BTRFS_I(inode)->root;
3250 struct btrfs_trans_handle *trans;
1ab86aed 3251 int ret;
f46b5a66 3252
1ab86aed 3253 ret = -EPERM;
df5b5520 3254 if (!capable(CAP_SYS_ADMIN))
1ab86aed 3255 goto out;
df5b5520 3256
1ab86aed
SW
3257 ret = -EINPROGRESS;
3258 if (file->private_data)
f46b5a66 3259 goto out;
9ca9ee09 3260
b83cc969
LZ
3261 ret = -EROFS;
3262 if (btrfs_root_readonly(root))
3263 goto out;
3264
a561be71 3265 ret = mnt_want_write_file(file);
c146afad
YZ
3266 if (ret)
3267 goto out;
3268
a4abeea4 3269 atomic_inc(&root->fs_info->open_ioctl_trans);
9ca9ee09 3270
1ab86aed 3271 ret = -ENOMEM;
7a7eaa40 3272 trans = btrfs_start_ioctl_transaction(root);
abd30bb0 3273 if (IS_ERR(trans))
1ab86aed
SW
3274 goto out_drop;
3275
3276 file->private_data = trans;
3277 return 0;
3278
3279out_drop:
a4abeea4 3280 atomic_dec(&root->fs_info->open_ioctl_trans);
2a79f17e 3281 mnt_drop_write_file(file);
f46b5a66 3282out:
f46b5a66
CH
3283 return ret;
3284}
3285
6ef5ed0d
JB
3286static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
3287{
496ad9aa 3288 struct inode *inode = file_inode(file);
6ef5ed0d
JB
3289 struct btrfs_root *root = BTRFS_I(inode)->root;
3290 struct btrfs_root *new_root;
3291 struct btrfs_dir_item *di;
3292 struct btrfs_trans_handle *trans;
3293 struct btrfs_path *path;
3294 struct btrfs_key location;
3295 struct btrfs_disk_key disk_key;
6ef5ed0d
JB
3296 u64 objectid = 0;
3297 u64 dir_id;
3c04ce01 3298 int ret;
6ef5ed0d
JB
3299
3300 if (!capable(CAP_SYS_ADMIN))
3301 return -EPERM;
3302
3c04ce01
MX
3303 ret = mnt_want_write_file(file);
3304 if (ret)
3305 return ret;
3306
3307 if (copy_from_user(&objectid, argp, sizeof(objectid))) {
3308 ret = -EFAULT;
3309 goto out;
3310 }
6ef5ed0d
JB
3311
3312 if (!objectid)
1cecf579 3313 objectid = BTRFS_FS_TREE_OBJECTID;
6ef5ed0d
JB
3314
3315 location.objectid = objectid;
3316 location.type = BTRFS_ROOT_ITEM_KEY;
3317 location.offset = (u64)-1;
3318
3319 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
3c04ce01
MX
3320 if (IS_ERR(new_root)) {
3321 ret = PTR_ERR(new_root);
3322 goto out;
3323 }
6ef5ed0d 3324
6ef5ed0d 3325 path = btrfs_alloc_path();
3c04ce01
MX
3326 if (!path) {
3327 ret = -ENOMEM;
3328 goto out;
3329 }
6ef5ed0d
JB
3330 path->leave_spinning = 1;
3331
3332 trans = btrfs_start_transaction(root, 1);
98d5dc13 3333 if (IS_ERR(trans)) {
6ef5ed0d 3334 btrfs_free_path(path);
3c04ce01
MX
3335 ret = PTR_ERR(trans);
3336 goto out;
6ef5ed0d
JB
3337 }
3338
6c41761f 3339 dir_id = btrfs_super_root_dir(root->fs_info->super_copy);
6ef5ed0d
JB
3340 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
3341 dir_id, "default", 7, 1);
cf1e99a4 3342 if (IS_ERR_OR_NULL(di)) {
6ef5ed0d
JB
3343 btrfs_free_path(path);
3344 btrfs_end_transaction(trans, root);
3345 printk(KERN_ERR "Umm, you don't have the default dir item, "
3346 "this isn't going to work\n");
3c04ce01
MX
3347 ret = -ENOENT;
3348 goto out;
6ef5ed0d
JB
3349 }
3350
3351 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
3352 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
3353 btrfs_mark_buffer_dirty(path->nodes[0]);
3354 btrfs_free_path(path);
3355
2b0ce2c2 3356 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL);
6ef5ed0d 3357 btrfs_end_transaction(trans, root);
3c04ce01
MX
3358out:
3359 mnt_drop_write_file(file);
3360 return ret;
6ef5ed0d
JB
3361}
3362
5af3e8cc
SB
3363void btrfs_get_block_group_info(struct list_head *groups_list,
3364 struct btrfs_ioctl_space_info *space)
bf5fc093
JB
3365{
3366 struct btrfs_block_group_cache *block_group;
3367
3368 space->total_bytes = 0;
3369 space->used_bytes = 0;
3370 space->flags = 0;
3371 list_for_each_entry(block_group, groups_list, list) {
3372 space->flags = block_group->flags;
3373 space->total_bytes += block_group->key.offset;
3374 space->used_bytes +=
3375 btrfs_block_group_used(&block_group->item);
3376 }
3377}
3378
48a3b636 3379static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
1406e432
JB
3380{
3381 struct btrfs_ioctl_space_args space_args;
3382 struct btrfs_ioctl_space_info space;
3383 struct btrfs_ioctl_space_info *dest;
7fde62bf 3384 struct btrfs_ioctl_space_info *dest_orig;
13f2696f 3385 struct btrfs_ioctl_space_info __user *user_dest;
1406e432 3386 struct btrfs_space_info *info;
bf5fc093
JB
3387 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
3388 BTRFS_BLOCK_GROUP_SYSTEM,
3389 BTRFS_BLOCK_GROUP_METADATA,
3390 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
3391 int num_types = 4;
7fde62bf 3392 int alloc_size;
1406e432 3393 int ret = 0;
51788b1b 3394 u64 slot_count = 0;
bf5fc093 3395 int i, c;
1406e432
JB
3396
3397 if (copy_from_user(&space_args,
3398 (struct btrfs_ioctl_space_args __user *)arg,
3399 sizeof(space_args)))
3400 return -EFAULT;
3401
bf5fc093
JB
3402 for (i = 0; i < num_types; i++) {
3403 struct btrfs_space_info *tmp;
3404
3405 info = NULL;
3406 rcu_read_lock();
3407 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3408 list) {
3409 if (tmp->flags == types[i]) {
3410 info = tmp;
3411 break;
3412 }
3413 }
3414 rcu_read_unlock();
3415
3416 if (!info)
3417 continue;
3418
3419 down_read(&info->groups_sem);
3420 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3421 if (!list_empty(&info->block_groups[c]))
3422 slot_count++;
3423 }
3424 up_read(&info->groups_sem);
3425 }
7fde62bf
CM
3426
3427 /* space_slots == 0 means they are asking for a count */
3428 if (space_args.space_slots == 0) {
3429 space_args.total_spaces = slot_count;
3430 goto out;
3431 }
bf5fc093 3432
51788b1b 3433 slot_count = min_t(u64, space_args.space_slots, slot_count);
bf5fc093 3434
7fde62bf 3435 alloc_size = sizeof(*dest) * slot_count;
bf5fc093 3436
7fde62bf
CM
3437 /* we generally have at most 6 or so space infos, one for each raid
3438 * level. So, a whole page should be more than enough for everyone
3439 */
3440 if (alloc_size > PAGE_CACHE_SIZE)
3441 return -ENOMEM;
3442
1406e432 3443 space_args.total_spaces = 0;
7fde62bf
CM
3444 dest = kmalloc(alloc_size, GFP_NOFS);
3445 if (!dest)
3446 return -ENOMEM;
3447 dest_orig = dest;
1406e432 3448
7fde62bf 3449 /* now we have a buffer to copy into */
bf5fc093
JB
3450 for (i = 0; i < num_types; i++) {
3451 struct btrfs_space_info *tmp;
3452
51788b1b
DR
3453 if (!slot_count)
3454 break;
3455
bf5fc093
JB
3456 info = NULL;
3457 rcu_read_lock();
3458 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
3459 list) {
3460 if (tmp->flags == types[i]) {
3461 info = tmp;
3462 break;
3463 }
3464 }
3465 rcu_read_unlock();
7fde62bf 3466
bf5fc093
JB
3467 if (!info)
3468 continue;
3469 down_read(&info->groups_sem);
3470 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
3471 if (!list_empty(&info->block_groups[c])) {
5af3e8cc
SB
3472 btrfs_get_block_group_info(
3473 &info->block_groups[c], &space);
bf5fc093
JB
3474 memcpy(dest, &space, sizeof(space));
3475 dest++;
3476 space_args.total_spaces++;
51788b1b 3477 slot_count--;
bf5fc093 3478 }
51788b1b
DR
3479 if (!slot_count)
3480 break;
bf5fc093
JB
3481 }
3482 up_read(&info->groups_sem);
1406e432 3483 }
1406e432 3484
2eec6c81 3485 user_dest = (struct btrfs_ioctl_space_info __user *)
7fde62bf
CM
3486 (arg + sizeof(struct btrfs_ioctl_space_args));
3487
3488 if (copy_to_user(user_dest, dest_orig, alloc_size))
3489 ret = -EFAULT;
3490
3491 kfree(dest_orig);
3492out:
3493 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
1406e432
JB
3494 ret = -EFAULT;
3495
3496 return ret;
3497}
3498
f46b5a66
CH
3499/*
3500 * there are many ways the trans_start and trans_end ioctls can lead
3501 * to deadlocks. They should only be used by applications that
3502 * basically own the machine, and have a very in depth understanding
3503 * of all the possible deadlocks and enospc problems.
3504 */
3505long btrfs_ioctl_trans_end(struct file *file)
3506{
496ad9aa 3507 struct inode *inode = file_inode(file);
f46b5a66
CH
3508 struct btrfs_root *root = BTRFS_I(inode)->root;
3509 struct btrfs_trans_handle *trans;
f46b5a66 3510
f46b5a66 3511 trans = file->private_data;
1ab86aed
SW
3512 if (!trans)
3513 return -EINVAL;
b214107e 3514 file->private_data = NULL;
9ca9ee09 3515
1ab86aed
SW
3516 btrfs_end_transaction(trans, root);
3517
a4abeea4 3518 atomic_dec(&root->fs_info->open_ioctl_trans);
9ca9ee09 3519
2a79f17e 3520 mnt_drop_write_file(file);
1ab86aed 3521 return 0;
f46b5a66
CH
3522}
3523
9a8c28be
MX
3524static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root,
3525 void __user *argp)
46204592 3526{
46204592
SW
3527 struct btrfs_trans_handle *trans;
3528 u64 transid;
db5b493a 3529 int ret;
46204592 3530
d4edf39b 3531 trans = btrfs_attach_transaction_barrier(root);
ff7c1d33
MX
3532 if (IS_ERR(trans)) {
3533 if (PTR_ERR(trans) != -ENOENT)
3534 return PTR_ERR(trans);
3535
3536 /* No running transaction, don't bother */
3537 transid = root->fs_info->last_trans_committed;
3538 goto out;
3539 }
46204592 3540 transid = trans->transid;
db5b493a 3541 ret = btrfs_commit_transaction_async(trans, root, 0);
8b2b2d3c
TI
3542 if (ret) {
3543 btrfs_end_transaction(trans, root);
db5b493a 3544 return ret;
8b2b2d3c 3545 }
ff7c1d33 3546out:
46204592
SW
3547 if (argp)
3548 if (copy_to_user(argp, &transid, sizeof(transid)))
3549 return -EFAULT;
3550 return 0;
3551}
3552
9a8c28be
MX
3553static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
3554 void __user *argp)
46204592 3555{
46204592
SW
3556 u64 transid;
3557
3558 if (argp) {
3559 if (copy_from_user(&transid, argp, sizeof(transid)))
3560 return -EFAULT;
3561 } else {
3562 transid = 0; /* current trans */
3563 }
3564 return btrfs_wait_for_commit(root, transid);
3565}
3566
b8e95489 3567static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
475f6387 3568{
496ad9aa 3569 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
475f6387 3570 struct btrfs_ioctl_scrub_args *sa;
b8e95489 3571 int ret;
475f6387
JS
3572
3573 if (!capable(CAP_SYS_ADMIN))
3574 return -EPERM;
3575
3576 sa = memdup_user(arg, sizeof(*sa));
3577 if (IS_ERR(sa))
3578 return PTR_ERR(sa);
3579
b8e95489
MX
3580 if (!(sa->flags & BTRFS_SCRUB_READONLY)) {
3581 ret = mnt_want_write_file(file);
3582 if (ret)
3583 goto out;
3584 }
3585
aa1b8cd4 3586 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end,
63a212ab
SB
3587 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
3588 0);
475f6387
JS
3589
3590 if (copy_to_user(arg, sa, sizeof(*sa)))
3591 ret = -EFAULT;
3592
b8e95489
MX
3593 if (!(sa->flags & BTRFS_SCRUB_READONLY))
3594 mnt_drop_write_file(file);
3595out:
475f6387
JS
3596 kfree(sa);
3597 return ret;
3598}
3599
3600static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg)
3601{
3602 if (!capable(CAP_SYS_ADMIN))
3603 return -EPERM;
3604
aa1b8cd4 3605 return btrfs_scrub_cancel(root->fs_info);
475f6387
JS
3606}
3607
3608static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
3609 void __user *arg)
3610{
3611 struct btrfs_ioctl_scrub_args *sa;
3612 int ret;
3613
3614 if (!capable(CAP_SYS_ADMIN))
3615 return -EPERM;
3616
3617 sa = memdup_user(arg, sizeof(*sa));
3618 if (IS_ERR(sa))
3619 return PTR_ERR(sa);
3620
3621 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress);
3622
3623 if (copy_to_user(arg, sa, sizeof(*sa)))
3624 ret = -EFAULT;
3625
3626 kfree(sa);
3627 return ret;
3628}
3629
c11d2c23 3630static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
b27f7c0c 3631 void __user *arg)
c11d2c23
SB
3632{
3633 struct btrfs_ioctl_get_dev_stats *sa;
3634 int ret;
3635
c11d2c23
SB
3636 sa = memdup_user(arg, sizeof(*sa));
3637 if (IS_ERR(sa))
3638 return PTR_ERR(sa);
3639
b27f7c0c
DS
3640 if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) {
3641 kfree(sa);
3642 return -EPERM;
3643 }
3644
3645 ret = btrfs_get_dev_stats(root, sa);
c11d2c23
SB
3646
3647 if (copy_to_user(arg, sa, sizeof(*sa)))
3648 ret = -EFAULT;
3649
3650 kfree(sa);
3651 return ret;
3652}
3653
3f6bcfbd
SB
3654static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
3655{
3656 struct btrfs_ioctl_dev_replace_args *p;
3657 int ret;
3658
3659 if (!capable(CAP_SYS_ADMIN))
3660 return -EPERM;
3661
3662 p = memdup_user(arg, sizeof(*p));
3663 if (IS_ERR(p))
3664 return PTR_ERR(p);
3665
3666 switch (p->cmd) {
3667 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
adfa97cb
ID
3668 if (root->fs_info->sb->s_flags & MS_RDONLY) {
3669 ret = -EROFS;
3670 goto out;
3671 }
3f6bcfbd
SB
3672 if (atomic_xchg(
3673 &root->fs_info->mutually_exclusive_operation_running,
3674 1)) {
e57138b3 3675 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
3f6bcfbd
SB
3676 } else {
3677 ret = btrfs_dev_replace_start(root, p);
3678 atomic_set(
3679 &root->fs_info->mutually_exclusive_operation_running,
3680 0);
3681 }
3682 break;
3683 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
3684 btrfs_dev_replace_status(root->fs_info, p);
3685 ret = 0;
3686 break;
3687 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
3688 ret = btrfs_dev_replace_cancel(root->fs_info, p);
3689 break;
3690 default:
3691 ret = -EINVAL;
3692 break;
3693 }
3694
3695 if (copy_to_user(arg, p, sizeof(*p)))
3696 ret = -EFAULT;
adfa97cb 3697out:
3f6bcfbd
SB
3698 kfree(p);
3699 return ret;
3700}
3701
d7728c96
JS
3702static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
3703{
3704 int ret = 0;
3705 int i;
740c3d22 3706 u64 rel_ptr;
d7728c96 3707 int size;
806468f8 3708 struct btrfs_ioctl_ino_path_args *ipa = NULL;
d7728c96
JS
3709 struct inode_fs_paths *ipath = NULL;
3710 struct btrfs_path *path;
3711
82b22ac8 3712 if (!capable(CAP_DAC_READ_SEARCH))
d7728c96
JS
3713 return -EPERM;
3714
3715 path = btrfs_alloc_path();
3716 if (!path) {
3717 ret = -ENOMEM;
3718 goto out;
3719 }
3720
3721 ipa = memdup_user(arg, sizeof(*ipa));
3722 if (IS_ERR(ipa)) {
3723 ret = PTR_ERR(ipa);
3724 ipa = NULL;
3725 goto out;
3726 }
3727
3728 size = min_t(u32, ipa->size, 4096);
3729 ipath = init_ipath(size, root, path);
3730 if (IS_ERR(ipath)) {
3731 ret = PTR_ERR(ipath);
3732 ipath = NULL;
3733 goto out;
3734 }
3735
3736 ret = paths_from_inode(ipa->inum, ipath);
3737 if (ret < 0)
3738 goto out;
3739
3740 for (i = 0; i < ipath->fspath->elem_cnt; ++i) {
745c4d8e
JM
3741 rel_ptr = ipath->fspath->val[i] -
3742 (u64)(unsigned long)ipath->fspath->val;
740c3d22 3743 ipath->fspath->val[i] = rel_ptr;
d7728c96
JS
3744 }
3745
745c4d8e
JM
3746 ret = copy_to_user((void *)(unsigned long)ipa->fspath,
3747 (void *)(unsigned long)ipath->fspath, size);
d7728c96
JS
3748 if (ret) {
3749 ret = -EFAULT;
3750 goto out;
3751 }
3752
3753out:
3754 btrfs_free_path(path);
3755 free_ipath(ipath);
3756 kfree(ipa);
3757
3758 return ret;
3759}
3760
3761static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
3762{
3763 struct btrfs_data_container *inodes = ctx;
3764 const size_t c = 3 * sizeof(u64);
3765
3766 if (inodes->bytes_left >= c) {
3767 inodes->bytes_left -= c;
3768 inodes->val[inodes->elem_cnt] = inum;
3769 inodes->val[inodes->elem_cnt + 1] = offset;
3770 inodes->val[inodes->elem_cnt + 2] = root;
3771 inodes->elem_cnt += 3;
3772 } else {
3773 inodes->bytes_missing += c - inodes->bytes_left;
3774 inodes->bytes_left = 0;
3775 inodes->elem_missed += 3;
3776 }
3777
3778 return 0;
3779}
3780
3781static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
3782 void __user *arg)
3783{
3784 int ret = 0;
3785 int size;
d7728c96
JS
3786 struct btrfs_ioctl_logical_ino_args *loi;
3787 struct btrfs_data_container *inodes = NULL;
3788 struct btrfs_path *path = NULL;
d7728c96
JS
3789
3790 if (!capable(CAP_SYS_ADMIN))
3791 return -EPERM;
3792
3793 loi = memdup_user(arg, sizeof(*loi));
3794 if (IS_ERR(loi)) {
3795 ret = PTR_ERR(loi);
3796 loi = NULL;
3797 goto out;
3798 }
3799
3800 path = btrfs_alloc_path();
3801 if (!path) {
3802 ret = -ENOMEM;
3803 goto out;
3804 }
3805
425d17a2 3806 size = min_t(u32, loi->size, 64 * 1024);
d7728c96
JS
3807 inodes = init_data_container(size);
3808 if (IS_ERR(inodes)) {
3809 ret = PTR_ERR(inodes);
3810 inodes = NULL;
3811 goto out;
3812 }
3813
df031f07
LB
3814 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path,
3815 build_ino_list, inodes);
3816 if (ret == -EINVAL)
d7728c96
JS
3817 ret = -ENOENT;
3818 if (ret < 0)
3819 goto out;
3820
745c4d8e
JM
3821 ret = copy_to_user((void *)(unsigned long)loi->inodes,
3822 (void *)(unsigned long)inodes, size);
d7728c96
JS
3823 if (ret)
3824 ret = -EFAULT;
3825
3826out:
3827 btrfs_free_path(path);
425d17a2 3828 vfree(inodes);
d7728c96
JS
3829 kfree(loi);
3830
3831 return ret;
3832}
3833
19a39dce 3834void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
c9e9f97b
ID
3835 struct btrfs_ioctl_balance_args *bargs)
3836{
3837 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3838
3839 bargs->flags = bctl->flags;
3840
837d5b6e
ID
3841 if (atomic_read(&fs_info->balance_running))
3842 bargs->state |= BTRFS_BALANCE_STATE_RUNNING;
3843 if (atomic_read(&fs_info->balance_pause_req))
3844 bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ;
a7e99c69
ID
3845 if (atomic_read(&fs_info->balance_cancel_req))
3846 bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ;
837d5b6e 3847
c9e9f97b
ID
3848 memcpy(&bargs->data, &bctl->data, sizeof(bargs->data));
3849 memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta));
3850 memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys));
19a39dce
ID
3851
3852 if (lock) {
3853 spin_lock(&fs_info->balance_lock);
3854 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3855 spin_unlock(&fs_info->balance_lock);
3856 } else {
3857 memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat));
3858 }
c9e9f97b
ID
3859}
3860
9ba1f6e4 3861static long btrfs_ioctl_balance(struct file *file, void __user *arg)
c9e9f97b 3862{
496ad9aa 3863 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
c9e9f97b
ID
3864 struct btrfs_fs_info *fs_info = root->fs_info;
3865 struct btrfs_ioctl_balance_args *bargs;
3866 struct btrfs_balance_control *bctl;
ed0fb78f 3867 bool need_unlock; /* for mut. excl. ops lock */
c9e9f97b
ID
3868 int ret;
3869
3870 if (!capable(CAP_SYS_ADMIN))
3871 return -EPERM;
3872
e54bfa31 3873 ret = mnt_want_write_file(file);
9ba1f6e4
LB
3874 if (ret)
3875 return ret;
3876
ed0fb78f
ID
3877again:
3878 if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
3879 mutex_lock(&fs_info->volume_mutex);
3880 mutex_lock(&fs_info->balance_mutex);
3881 need_unlock = true;
3882 goto locked;
3883 }
3884
3885 /*
3886 * mut. excl. ops lock is locked. Three possibilites:
3887 * (1) some other op is running
3888 * (2) balance is running
3889 * (3) balance is paused -- special case (think resume)
3890 */
c9e9f97b 3891 mutex_lock(&fs_info->balance_mutex);
ed0fb78f
ID
3892 if (fs_info->balance_ctl) {
3893 /* this is either (2) or (3) */
3894 if (!atomic_read(&fs_info->balance_running)) {
3895 mutex_unlock(&fs_info->balance_mutex);
3896 if (!mutex_trylock(&fs_info->volume_mutex))
3897 goto again;
3898 mutex_lock(&fs_info->balance_mutex);
3899
3900 if (fs_info->balance_ctl &&
3901 !atomic_read(&fs_info->balance_running)) {
3902 /* this is (3) */
3903 need_unlock = false;
3904 goto locked;
3905 }
3906
3907 mutex_unlock(&fs_info->balance_mutex);
3908 mutex_unlock(&fs_info->volume_mutex);
3909 goto again;
3910 } else {
3911 /* this is (2) */
3912 mutex_unlock(&fs_info->balance_mutex);
3913 ret = -EINPROGRESS;
3914 goto out;
3915 }
3916 } else {
3917 /* this is (1) */
3918 mutex_unlock(&fs_info->balance_mutex);
e57138b3 3919 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
ed0fb78f
ID
3920 goto out;
3921 }
3922
3923locked:
3924 BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running));
c9e9f97b
ID
3925
3926 if (arg) {
3927 bargs = memdup_user(arg, sizeof(*bargs));
3928 if (IS_ERR(bargs)) {
3929 ret = PTR_ERR(bargs);
ed0fb78f 3930 goto out_unlock;
c9e9f97b 3931 }
de322263
ID
3932
3933 if (bargs->flags & BTRFS_BALANCE_RESUME) {
3934 if (!fs_info->balance_ctl) {
3935 ret = -ENOTCONN;
3936 goto out_bargs;
3937 }
3938
3939 bctl = fs_info->balance_ctl;
3940 spin_lock(&fs_info->balance_lock);
3941 bctl->flags |= BTRFS_BALANCE_RESUME;
3942 spin_unlock(&fs_info->balance_lock);
3943
3944 goto do_balance;
3945 }
c9e9f97b
ID
3946 } else {
3947 bargs = NULL;
3948 }
3949
ed0fb78f 3950 if (fs_info->balance_ctl) {
837d5b6e
ID
3951 ret = -EINPROGRESS;
3952 goto out_bargs;
3953 }
3954
c9e9f97b
ID
3955 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3956 if (!bctl) {
3957 ret = -ENOMEM;
3958 goto out_bargs;
3959 }
3960
3961 bctl->fs_info = fs_info;
3962 if (arg) {
3963 memcpy(&bctl->data, &bargs->data, sizeof(bctl->data));
3964 memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta));
3965 memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys));
3966
3967 bctl->flags = bargs->flags;
f43ffb60
ID
3968 } else {
3969 /* balance everything - no filters */
3970 bctl->flags |= BTRFS_BALANCE_TYPE_MASK;
c9e9f97b
ID
3971 }
3972
de322263 3973do_balance:
c9e9f97b 3974 /*
ed0fb78f
ID
3975 * Ownership of bctl and mutually_exclusive_operation_running
3976 * goes to to btrfs_balance. bctl is freed in __cancel_balance,
3977 * or, if restriper was paused all the way until unmount, in
3978 * free_fs_info. mutually_exclusive_operation_running is
3979 * cleared in __cancel_balance.
c9e9f97b 3980 */
ed0fb78f
ID
3981 need_unlock = false;
3982
3983 ret = btrfs_balance(bctl, bargs);
3984
c9e9f97b
ID
3985 if (arg) {
3986 if (copy_to_user(arg, bargs, sizeof(*bargs)))
3987 ret = -EFAULT;
3988 }
3989
3990out_bargs:
3991 kfree(bargs);
ed0fb78f 3992out_unlock:
c9e9f97b
ID
3993 mutex_unlock(&fs_info->balance_mutex);
3994 mutex_unlock(&fs_info->volume_mutex);
ed0fb78f
ID
3995 if (need_unlock)
3996 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3997out:
e54bfa31 3998 mnt_drop_write_file(file);
c9e9f97b
ID
3999 return ret;
4000}
4001
837d5b6e
ID
4002static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4003{
4004 if (!capable(CAP_SYS_ADMIN))
4005 return -EPERM;
4006
4007 switch (cmd) {
4008 case BTRFS_BALANCE_CTL_PAUSE:
4009 return btrfs_pause_balance(root->fs_info);
a7e99c69
ID
4010 case BTRFS_BALANCE_CTL_CANCEL:
4011 return btrfs_cancel_balance(root->fs_info);
837d5b6e
ID
4012 }
4013
4014 return -EINVAL;
4015}
4016
19a39dce
ID
4017static long btrfs_ioctl_balance_progress(struct btrfs_root *root,
4018 void __user *arg)
4019{
4020 struct btrfs_fs_info *fs_info = root->fs_info;
4021 struct btrfs_ioctl_balance_args *bargs;
4022 int ret = 0;
4023
4024 if (!capable(CAP_SYS_ADMIN))
4025 return -EPERM;
4026
4027 mutex_lock(&fs_info->balance_mutex);
4028 if (!fs_info->balance_ctl) {
4029 ret = -ENOTCONN;
4030 goto out;
4031 }
4032
4033 bargs = kzalloc(sizeof(*bargs), GFP_NOFS);
4034 if (!bargs) {
4035 ret = -ENOMEM;
4036 goto out;
4037 }
4038
4039 update_ioctl_balance_args(fs_info, 1, bargs);
4040
4041 if (copy_to_user(arg, bargs, sizeof(*bargs)))
4042 ret = -EFAULT;
4043
4044 kfree(bargs);
4045out:
4046 mutex_unlock(&fs_info->balance_mutex);
4047 return ret;
4048}
4049
905b0dda 4050static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
5d13a37b 4051{
496ad9aa 4052 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4053 struct btrfs_ioctl_quota_ctl_args *sa;
4054 struct btrfs_trans_handle *trans = NULL;
4055 int ret;
4056 int err;
4057
4058 if (!capable(CAP_SYS_ADMIN))
4059 return -EPERM;
4060
905b0dda
MX
4061 ret = mnt_want_write_file(file);
4062 if (ret)
4063 return ret;
5d13a37b
AJ
4064
4065 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4066 if (IS_ERR(sa)) {
4067 ret = PTR_ERR(sa);
4068 goto drop_write;
4069 }
5d13a37b 4070
7708f029 4071 down_write(&root->fs_info->subvol_sem);
2f232036
JS
4072 trans = btrfs_start_transaction(root->fs_info->tree_root, 2);
4073 if (IS_ERR(trans)) {
4074 ret = PTR_ERR(trans);
4075 goto out;
5d13a37b
AJ
4076 }
4077
4078 switch (sa->cmd) {
4079 case BTRFS_QUOTA_CTL_ENABLE:
4080 ret = btrfs_quota_enable(trans, root->fs_info);
4081 break;
4082 case BTRFS_QUOTA_CTL_DISABLE:
4083 ret = btrfs_quota_disable(trans, root->fs_info);
4084 break;
5d13a37b
AJ
4085 default:
4086 ret = -EINVAL;
4087 break;
4088 }
4089
2f232036
JS
4090 err = btrfs_commit_transaction(trans, root->fs_info->tree_root);
4091 if (err && !ret)
4092 ret = err;
5d13a37b
AJ
4093out:
4094 kfree(sa);
7708f029 4095 up_write(&root->fs_info->subvol_sem);
905b0dda
MX
4096drop_write:
4097 mnt_drop_write_file(file);
5d13a37b
AJ
4098 return ret;
4099}
4100
905b0dda 4101static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
5d13a37b 4102{
496ad9aa 4103 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4104 struct btrfs_ioctl_qgroup_assign_args *sa;
4105 struct btrfs_trans_handle *trans;
4106 int ret;
4107 int err;
4108
4109 if (!capable(CAP_SYS_ADMIN))
4110 return -EPERM;
4111
905b0dda
MX
4112 ret = mnt_want_write_file(file);
4113 if (ret)
4114 return ret;
5d13a37b
AJ
4115
4116 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4117 if (IS_ERR(sa)) {
4118 ret = PTR_ERR(sa);
4119 goto drop_write;
4120 }
5d13a37b
AJ
4121
4122 trans = btrfs_join_transaction(root);
4123 if (IS_ERR(trans)) {
4124 ret = PTR_ERR(trans);
4125 goto out;
4126 }
4127
4128 /* FIXME: check if the IDs really exist */
4129 if (sa->assign) {
4130 ret = btrfs_add_qgroup_relation(trans, root->fs_info,
4131 sa->src, sa->dst);
4132 } else {
4133 ret = btrfs_del_qgroup_relation(trans, root->fs_info,
4134 sa->src, sa->dst);
4135 }
4136
4137 err = btrfs_end_transaction(trans, root);
4138 if (err && !ret)
4139 ret = err;
4140
4141out:
4142 kfree(sa);
905b0dda
MX
4143drop_write:
4144 mnt_drop_write_file(file);
5d13a37b
AJ
4145 return ret;
4146}
4147
905b0dda 4148static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
5d13a37b 4149{
496ad9aa 4150 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4151 struct btrfs_ioctl_qgroup_create_args *sa;
4152 struct btrfs_trans_handle *trans;
4153 int ret;
4154 int err;
4155
4156 if (!capable(CAP_SYS_ADMIN))
4157 return -EPERM;
4158
905b0dda
MX
4159 ret = mnt_want_write_file(file);
4160 if (ret)
4161 return ret;
5d13a37b
AJ
4162
4163 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4164 if (IS_ERR(sa)) {
4165 ret = PTR_ERR(sa);
4166 goto drop_write;
4167 }
5d13a37b 4168
d86e56cf
MX
4169 if (!sa->qgroupid) {
4170 ret = -EINVAL;
4171 goto out;
4172 }
4173
5d13a37b
AJ
4174 trans = btrfs_join_transaction(root);
4175 if (IS_ERR(trans)) {
4176 ret = PTR_ERR(trans);
4177 goto out;
4178 }
4179
4180 /* FIXME: check if the IDs really exist */
4181 if (sa->create) {
4182 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid,
4183 NULL);
4184 } else {
4185 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid);
4186 }
4187
4188 err = btrfs_end_transaction(trans, root);
4189 if (err && !ret)
4190 ret = err;
4191
4192out:
4193 kfree(sa);
905b0dda
MX
4194drop_write:
4195 mnt_drop_write_file(file);
5d13a37b
AJ
4196 return ret;
4197}
4198
905b0dda 4199static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
5d13a37b 4200{
496ad9aa 4201 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
5d13a37b
AJ
4202 struct btrfs_ioctl_qgroup_limit_args *sa;
4203 struct btrfs_trans_handle *trans;
4204 int ret;
4205 int err;
4206 u64 qgroupid;
4207
4208 if (!capable(CAP_SYS_ADMIN))
4209 return -EPERM;
4210
905b0dda
MX
4211 ret = mnt_want_write_file(file);
4212 if (ret)
4213 return ret;
5d13a37b
AJ
4214
4215 sa = memdup_user(arg, sizeof(*sa));
905b0dda
MX
4216 if (IS_ERR(sa)) {
4217 ret = PTR_ERR(sa);
4218 goto drop_write;
4219 }
5d13a37b
AJ
4220
4221 trans = btrfs_join_transaction(root);
4222 if (IS_ERR(trans)) {
4223 ret = PTR_ERR(trans);
4224 goto out;
4225 }
4226
4227 qgroupid = sa->qgroupid;
4228 if (!qgroupid) {
4229 /* take the current subvol as qgroup */
4230 qgroupid = root->root_key.objectid;
4231 }
4232
4233 /* FIXME: check if the IDs really exist */
4234 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim);
4235
4236 err = btrfs_end_transaction(trans, root);
4237 if (err && !ret)
4238 ret = err;
4239
4240out:
4241 kfree(sa);
905b0dda
MX
4242drop_write:
4243 mnt_drop_write_file(file);
5d13a37b
AJ
4244 return ret;
4245}
4246
2f232036
JS
4247static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
4248{
6d0379ec 4249 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2f232036
JS
4250 struct btrfs_ioctl_quota_rescan_args *qsa;
4251 int ret;
4252
4253 if (!capable(CAP_SYS_ADMIN))
4254 return -EPERM;
4255
4256 ret = mnt_want_write_file(file);
4257 if (ret)
4258 return ret;
4259
4260 qsa = memdup_user(arg, sizeof(*qsa));
4261 if (IS_ERR(qsa)) {
4262 ret = PTR_ERR(qsa);
4263 goto drop_write;
4264 }
4265
4266 if (qsa->flags) {
4267 ret = -EINVAL;
4268 goto out;
4269 }
4270
4271 ret = btrfs_qgroup_rescan(root->fs_info);
4272
4273out:
4274 kfree(qsa);
4275drop_write:
4276 mnt_drop_write_file(file);
4277 return ret;
4278}
4279
4280static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
4281{
6d0379ec 4282 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
2f232036
JS
4283 struct btrfs_ioctl_quota_rescan_args *qsa;
4284 int ret = 0;
4285
4286 if (!capable(CAP_SYS_ADMIN))
4287 return -EPERM;
4288
4289 qsa = kzalloc(sizeof(*qsa), GFP_NOFS);
4290 if (!qsa)
4291 return -ENOMEM;
4292
4293 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
4294 qsa->flags = 1;
4295 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid;
4296 }
4297
4298 if (copy_to_user(arg, qsa, sizeof(*qsa)))
4299 ret = -EFAULT;
4300
4301 kfree(qsa);
4302 return ret;
4303}
4304
57254b6e
JS
4305static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
4306{
54563d41 4307 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
57254b6e
JS
4308
4309 if (!capable(CAP_SYS_ADMIN))
4310 return -EPERM;
4311
4312 return btrfs_qgroup_wait_for_completion(root->fs_info);
4313}
4314
8ea05e3a
AB
4315static long btrfs_ioctl_set_received_subvol(struct file *file,
4316 void __user *arg)
4317{
4318 struct btrfs_ioctl_received_subvol_args *sa = NULL;
496ad9aa 4319 struct inode *inode = file_inode(file);
8ea05e3a
AB
4320 struct btrfs_root *root = BTRFS_I(inode)->root;
4321 struct btrfs_root_item *root_item = &root->root_item;
4322 struct btrfs_trans_handle *trans;
4323 struct timespec ct = CURRENT_TIME;
4324 int ret = 0;
dd5f9615 4325 int received_uuid_changed;
8ea05e3a
AB
4326
4327 ret = mnt_want_write_file(file);
4328 if (ret < 0)
4329 return ret;
4330
4331 down_write(&root->fs_info->subvol_sem);
4332
4333 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
4334 ret = -EINVAL;
4335 goto out;
4336 }
4337
4338 if (btrfs_root_readonly(root)) {
4339 ret = -EROFS;
4340 goto out;
4341 }
4342
4343 if (!inode_owner_or_capable(inode)) {
4344 ret = -EACCES;
4345 goto out;
4346 }
4347
4348 sa = memdup_user(arg, sizeof(*sa));
4349 if (IS_ERR(sa)) {
4350 ret = PTR_ERR(sa);
4351 sa = NULL;
4352 goto out;
4353 }
4354
dd5f9615
SB
4355 /*
4356 * 1 - root item
4357 * 2 - uuid items (received uuid + subvol uuid)
4358 */
4359 trans = btrfs_start_transaction(root, 3);
8ea05e3a
AB
4360 if (IS_ERR(trans)) {
4361 ret = PTR_ERR(trans);
4362 trans = NULL;
4363 goto out;
4364 }
4365
4366 sa->rtransid = trans->transid;
4367 sa->rtime.sec = ct.tv_sec;
4368 sa->rtime.nsec = ct.tv_nsec;
4369
dd5f9615
SB
4370 received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid,
4371 BTRFS_UUID_SIZE);
4372 if (received_uuid_changed &&
4373 !btrfs_is_empty_uuid(root_item->received_uuid))
4374 btrfs_uuid_tree_rem(trans, root->fs_info->uuid_root,
4375 root_item->received_uuid,
4376 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4377 root->root_key.objectid);
8ea05e3a
AB
4378 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
4379 btrfs_set_root_stransid(root_item, sa->stransid);
4380 btrfs_set_root_rtransid(root_item, sa->rtransid);
3cae210f
QW
4381 btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec);
4382 btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec);
4383 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
4384 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
8ea05e3a
AB
4385
4386 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4387 &root->root_key, &root->root_item);
4388 if (ret < 0) {
4389 btrfs_end_transaction(trans, root);
8ea05e3a 4390 goto out;
dd5f9615
SB
4391 }
4392 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
4393 ret = btrfs_uuid_tree_add(trans, root->fs_info->uuid_root,
4394 sa->uuid,
4395 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4396 root->root_key.objectid);
4397 if (ret < 0 && ret != -EEXIST) {
4398 btrfs_abort_transaction(trans, root, ret);
8ea05e3a 4399 goto out;
dd5f9615
SB
4400 }
4401 }
4402 ret = btrfs_commit_transaction(trans, root);
4403 if (ret < 0) {
4404 btrfs_abort_transaction(trans, root, ret);
4405 goto out;
8ea05e3a
AB
4406 }
4407
4408 ret = copy_to_user(arg, sa, sizeof(*sa));
4409 if (ret)
4410 ret = -EFAULT;
4411
4412out:
4413 kfree(sa);
4414 up_write(&root->fs_info->subvol_sem);
4415 mnt_drop_write_file(file);
4416 return ret;
4417}
4418
867ab667 4419static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
4420{
6d0379ec 4421 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
a1b83ac5 4422 size_t len;
867ab667 4423 int ret;
a1b83ac5
AJ
4424 char label[BTRFS_LABEL_SIZE];
4425
4426 spin_lock(&root->fs_info->super_lock);
4427 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
4428 spin_unlock(&root->fs_info->super_lock);
4429
4430 len = strnlen(label, BTRFS_LABEL_SIZE);
867ab667 4431
4432 if (len == BTRFS_LABEL_SIZE) {
4433 pr_warn("btrfs: label is too long, return the first %zu bytes\n",
4434 --len);
4435 }
4436
867ab667 4437 ret = copy_to_user(arg, label, len);
867ab667 4438
4439 return ret ? -EFAULT : 0;
4440}
4441
a8bfd4ab 4442static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
4443{
6d0379ec 4444 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
a8bfd4ab 4445 struct btrfs_super_block *super_block = root->fs_info->super_copy;
4446 struct btrfs_trans_handle *trans;
4447 char label[BTRFS_LABEL_SIZE];
4448 int ret;
4449
4450 if (!capable(CAP_SYS_ADMIN))
4451 return -EPERM;
4452
4453 if (copy_from_user(label, arg, sizeof(label)))
4454 return -EFAULT;
4455
4456 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
4457 pr_err("btrfs: unable to set label with more than %d bytes\n",
4458 BTRFS_LABEL_SIZE - 1);
4459 return -EINVAL;
4460 }
4461
4462 ret = mnt_want_write_file(file);
4463 if (ret)
4464 return ret;
4465
a8bfd4ab 4466 trans = btrfs_start_transaction(root, 0);
4467 if (IS_ERR(trans)) {
4468 ret = PTR_ERR(trans);
4469 goto out_unlock;
4470 }
4471
a1b83ac5 4472 spin_lock(&root->fs_info->super_lock);
a8bfd4ab 4473 strcpy(super_block->label, label);
a1b83ac5 4474 spin_unlock(&root->fs_info->super_lock);
a8bfd4ab 4475 ret = btrfs_end_transaction(trans, root);
4476
4477out_unlock:
a8bfd4ab 4478 mnt_drop_write_file(file);
4479 return ret;
4480}
4481
f46b5a66
CH
4482long btrfs_ioctl(struct file *file, unsigned int
4483 cmd, unsigned long arg)
4484{
496ad9aa 4485 struct btrfs_root *root = BTRFS_I(file_inode(file))->root;
4bcabaa3 4486 void __user *argp = (void __user *)arg;
f46b5a66
CH
4487
4488 switch (cmd) {
6cbff00f
CH
4489 case FS_IOC_GETFLAGS:
4490 return btrfs_ioctl_getflags(file, argp);
4491 case FS_IOC_SETFLAGS:
4492 return btrfs_ioctl_setflags(file, argp);
4493 case FS_IOC_GETVERSION:
4494 return btrfs_ioctl_getversion(file, argp);
f7039b1d
LD
4495 case FITRIM:
4496 return btrfs_ioctl_fitrim(file, argp);
f46b5a66 4497 case BTRFS_IOC_SNAP_CREATE:
fa0d2b9b 4498 return btrfs_ioctl_snap_create(file, argp, 0);
fdfb1e4f 4499 case BTRFS_IOC_SNAP_CREATE_V2:
fa0d2b9b 4500 return btrfs_ioctl_snap_create_v2(file, argp, 0);
3de4586c 4501 case BTRFS_IOC_SUBVOL_CREATE:
fa0d2b9b 4502 return btrfs_ioctl_snap_create(file, argp, 1);
6f72c7e2
AJ
4503 case BTRFS_IOC_SUBVOL_CREATE_V2:
4504 return btrfs_ioctl_snap_create_v2(file, argp, 1);
76dda93c
YZ
4505 case BTRFS_IOC_SNAP_DESTROY:
4506 return btrfs_ioctl_snap_destroy(file, argp);
0caa102d
LZ
4507 case BTRFS_IOC_SUBVOL_GETFLAGS:
4508 return btrfs_ioctl_subvol_getflags(file, argp);
4509 case BTRFS_IOC_SUBVOL_SETFLAGS:
4510 return btrfs_ioctl_subvol_setflags(file, argp);
6ef5ed0d
JB
4511 case BTRFS_IOC_DEFAULT_SUBVOL:
4512 return btrfs_ioctl_default_subvol(file, argp);
f46b5a66 4513 case BTRFS_IOC_DEFRAG:
1e701a32
CM
4514 return btrfs_ioctl_defrag(file, NULL);
4515 case BTRFS_IOC_DEFRAG_RANGE:
4516 return btrfs_ioctl_defrag(file, argp);
f46b5a66 4517 case BTRFS_IOC_RESIZE:
198605a8 4518 return btrfs_ioctl_resize(file, argp);
f46b5a66 4519 case BTRFS_IOC_ADD_DEV:
4bcabaa3 4520 return btrfs_ioctl_add_dev(root, argp);
f46b5a66 4521 case BTRFS_IOC_RM_DEV:
da24927b 4522 return btrfs_ioctl_rm_dev(file, argp);
475f6387
JS
4523 case BTRFS_IOC_FS_INFO:
4524 return btrfs_ioctl_fs_info(root, argp);
4525 case BTRFS_IOC_DEV_INFO:
4526 return btrfs_ioctl_dev_info(root, argp);
f46b5a66 4527 case BTRFS_IOC_BALANCE:
9ba1f6e4 4528 return btrfs_ioctl_balance(file, NULL);
f46b5a66 4529 case BTRFS_IOC_CLONE:
c5c9cd4d
SW
4530 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
4531 case BTRFS_IOC_CLONE_RANGE:
7a865e8a 4532 return btrfs_ioctl_clone_range(file, argp);
f46b5a66
CH
4533 case BTRFS_IOC_TRANS_START:
4534 return btrfs_ioctl_trans_start(file);
4535 case BTRFS_IOC_TRANS_END:
4536 return btrfs_ioctl_trans_end(file);
ac8e9819
CM
4537 case BTRFS_IOC_TREE_SEARCH:
4538 return btrfs_ioctl_tree_search(file, argp);
4539 case BTRFS_IOC_INO_LOOKUP:
4540 return btrfs_ioctl_ino_lookup(file, argp);
d7728c96
JS
4541 case BTRFS_IOC_INO_PATHS:
4542 return btrfs_ioctl_ino_to_path(root, argp);
4543 case BTRFS_IOC_LOGICAL_INO:
4544 return btrfs_ioctl_logical_to_ino(root, argp);
1406e432
JB
4545 case BTRFS_IOC_SPACE_INFO:
4546 return btrfs_ioctl_space_info(root, argp);
9b199859
FDBM
4547 case BTRFS_IOC_SYNC: {
4548 int ret;
4549
91aef86f 4550 ret = btrfs_start_delalloc_roots(root->fs_info, 0);
9b199859
FDBM
4551 if (ret)
4552 return ret;
4553 ret = btrfs_sync_fs(file->f_dentry->d_sb, 1);
4554 return ret;
4555 }
46204592 4556 case BTRFS_IOC_START_SYNC:
9a8c28be 4557 return btrfs_ioctl_start_sync(root, argp);
46204592 4558 case BTRFS_IOC_WAIT_SYNC:
9a8c28be 4559 return btrfs_ioctl_wait_sync(root, argp);
475f6387 4560 case BTRFS_IOC_SCRUB:
b8e95489 4561 return btrfs_ioctl_scrub(file, argp);
475f6387
JS
4562 case BTRFS_IOC_SCRUB_CANCEL:
4563 return btrfs_ioctl_scrub_cancel(root, argp);
4564 case BTRFS_IOC_SCRUB_PROGRESS:
4565 return btrfs_ioctl_scrub_progress(root, argp);
c9e9f97b 4566 case BTRFS_IOC_BALANCE_V2:
9ba1f6e4 4567 return btrfs_ioctl_balance(file, argp);
837d5b6e
ID
4568 case BTRFS_IOC_BALANCE_CTL:
4569 return btrfs_ioctl_balance_ctl(root, arg);
19a39dce
ID
4570 case BTRFS_IOC_BALANCE_PROGRESS:
4571 return btrfs_ioctl_balance_progress(root, argp);
8ea05e3a
AB
4572 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
4573 return btrfs_ioctl_set_received_subvol(file, argp);
31db9f7c
AB
4574 case BTRFS_IOC_SEND:
4575 return btrfs_ioctl_send(file, argp);
c11d2c23 4576 case BTRFS_IOC_GET_DEV_STATS:
b27f7c0c 4577 return btrfs_ioctl_get_dev_stats(root, argp);
5d13a37b 4578 case BTRFS_IOC_QUOTA_CTL:
905b0dda 4579 return btrfs_ioctl_quota_ctl(file, argp);
5d13a37b 4580 case BTRFS_IOC_QGROUP_ASSIGN:
905b0dda 4581 return btrfs_ioctl_qgroup_assign(file, argp);
5d13a37b 4582 case BTRFS_IOC_QGROUP_CREATE:
905b0dda 4583 return btrfs_ioctl_qgroup_create(file, argp);
5d13a37b 4584 case BTRFS_IOC_QGROUP_LIMIT:
905b0dda 4585 return btrfs_ioctl_qgroup_limit(file, argp);
2f232036
JS
4586 case BTRFS_IOC_QUOTA_RESCAN:
4587 return btrfs_ioctl_quota_rescan(file, argp);
4588 case BTRFS_IOC_QUOTA_RESCAN_STATUS:
4589 return btrfs_ioctl_quota_rescan_status(file, argp);
57254b6e
JS
4590 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
4591 return btrfs_ioctl_quota_rescan_wait(file, argp);
3f6bcfbd
SB
4592 case BTRFS_IOC_DEV_REPLACE:
4593 return btrfs_ioctl_dev_replace(root, argp);
867ab667 4594 case BTRFS_IOC_GET_FSLABEL:
4595 return btrfs_ioctl_get_fslabel(file, argp);
a8bfd4ab 4596 case BTRFS_IOC_SET_FSLABEL:
4597 return btrfs_ioctl_set_fslabel(file, argp);
416161db
MF
4598 case BTRFS_IOC_FILE_EXTENT_SAME:
4599 return btrfs_ioctl_file_extent_same(file, argp);
f46b5a66
CH
4600 }
4601
4602 return -ENOTTY;
4603}
This page took 0.540972 seconds and 5 git commands to generate.