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