2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.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>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
50 #include "print-tree.h"
53 #include "inode-map.h"
55 /* Mask out flags that are inappropriate for the given type of inode. */
56 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
60 else if (S_ISREG(mode
))
61 return flags
& ~FS_DIRSYNC_FL
;
63 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
67 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
69 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
71 unsigned int iflags
= 0;
73 if (flags
& BTRFS_INODE_SYNC
)
75 if (flags
& BTRFS_INODE_IMMUTABLE
)
76 iflags
|= FS_IMMUTABLE_FL
;
77 if (flags
& BTRFS_INODE_APPEND
)
78 iflags
|= FS_APPEND_FL
;
79 if (flags
& BTRFS_INODE_NODUMP
)
80 iflags
|= FS_NODUMP_FL
;
81 if (flags
& BTRFS_INODE_NOATIME
)
82 iflags
|= FS_NOATIME_FL
;
83 if (flags
& BTRFS_INODE_DIRSYNC
)
84 iflags
|= FS_DIRSYNC_FL
;
85 if (flags
& BTRFS_INODE_NODATACOW
)
86 iflags
|= FS_NOCOW_FL
;
88 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
89 iflags
|= FS_COMPR_FL
;
90 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
91 iflags
|= FS_NOCOMP_FL
;
97 * Update inode->i_flags based on the btrfs internal flags.
99 void btrfs_update_iflags(struct inode
*inode
)
101 struct btrfs_inode
*ip
= BTRFS_I(inode
);
103 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
105 if (ip
->flags
& BTRFS_INODE_SYNC
)
106 inode
->i_flags
|= S_SYNC
;
107 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
108 inode
->i_flags
|= S_IMMUTABLE
;
109 if (ip
->flags
& BTRFS_INODE_APPEND
)
110 inode
->i_flags
|= S_APPEND
;
111 if (ip
->flags
& BTRFS_INODE_NOATIME
)
112 inode
->i_flags
|= S_NOATIME
;
113 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
114 inode
->i_flags
|= S_DIRSYNC
;
118 * Inherit flags from the parent inode.
120 * Unlike extN we don't have any flags we don't want to inherit currently.
122 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
129 flags
= BTRFS_I(dir
)->flags
;
131 if (S_ISREG(inode
->i_mode
))
132 flags
&= ~BTRFS_INODE_DIRSYNC
;
133 else if (!S_ISDIR(inode
->i_mode
))
134 flags
&= (BTRFS_INODE_NODUMP
| BTRFS_INODE_NOATIME
);
136 BTRFS_I(inode
)->flags
= flags
;
137 btrfs_update_iflags(inode
);
140 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
142 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
143 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
145 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
150 static int check_flags(unsigned int flags
)
152 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
153 FS_NOATIME_FL
| FS_NODUMP_FL
| \
154 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
155 FS_NOCOMP_FL
| FS_COMPR_FL
|
159 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
165 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
167 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
168 struct btrfs_inode
*ip
= BTRFS_I(inode
);
169 struct btrfs_root
*root
= ip
->root
;
170 struct btrfs_trans_handle
*trans
;
171 unsigned int flags
, oldflags
;
174 if (btrfs_root_readonly(root
))
177 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
180 ret
= check_flags(flags
);
184 if (!inode_owner_or_capable(inode
))
187 mutex_lock(&inode
->i_mutex
);
189 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
190 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
191 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
192 if (!capable(CAP_LINUX_IMMUTABLE
)) {
198 ret
= mnt_want_write(file
->f_path
.mnt
);
202 if (flags
& FS_SYNC_FL
)
203 ip
->flags
|= BTRFS_INODE_SYNC
;
205 ip
->flags
&= ~BTRFS_INODE_SYNC
;
206 if (flags
& FS_IMMUTABLE_FL
)
207 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
209 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
210 if (flags
& FS_APPEND_FL
)
211 ip
->flags
|= BTRFS_INODE_APPEND
;
213 ip
->flags
&= ~BTRFS_INODE_APPEND
;
214 if (flags
& FS_NODUMP_FL
)
215 ip
->flags
|= BTRFS_INODE_NODUMP
;
217 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
218 if (flags
& FS_NOATIME_FL
)
219 ip
->flags
|= BTRFS_INODE_NOATIME
;
221 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
222 if (flags
& FS_DIRSYNC_FL
)
223 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
225 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
226 if (flags
& FS_NOCOW_FL
)
227 ip
->flags
|= BTRFS_INODE_NODATACOW
;
229 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
232 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
233 * flag may be changed automatically if compression code won't make
236 if (flags
& FS_NOCOMP_FL
) {
237 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
238 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
239 } else if (flags
& FS_COMPR_FL
) {
240 ip
->flags
|= BTRFS_INODE_COMPRESS
;
241 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
243 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
246 trans
= btrfs_join_transaction(root
, 1);
247 BUG_ON(IS_ERR(trans
));
249 ret
= btrfs_update_inode(trans
, root
, inode
);
252 btrfs_update_iflags(inode
);
253 inode
->i_ctime
= CURRENT_TIME
;
254 btrfs_end_transaction(trans
, root
);
256 mnt_drop_write(file
->f_path
.mnt
);
260 mutex_unlock(&inode
->i_mutex
);
264 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
266 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
268 return put_user(inode
->i_generation
, arg
);
271 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
273 struct btrfs_root
*root
= fdentry(file
)->d_sb
->s_fs_info
;
274 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
275 struct btrfs_device
*device
;
276 struct request_queue
*q
;
277 struct fstrim_range range
;
278 u64 minlen
= ULLONG_MAX
;
282 if (!capable(CAP_SYS_ADMIN
))
286 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
290 q
= bdev_get_queue(device
->bdev
);
291 if (blk_queue_discard(q
)) {
293 minlen
= min((u64
)q
->limits
.discard_granularity
,
301 if (copy_from_user(&range
, arg
, sizeof(range
)))
304 range
.minlen
= max(range
.minlen
, minlen
);
305 ret
= btrfs_trim_fs(root
, &range
);
309 if (copy_to_user(arg
, &range
, sizeof(range
)))
315 static noinline
int create_subvol(struct btrfs_root
*root
,
316 struct dentry
*dentry
,
317 char *name
, int namelen
,
320 struct btrfs_trans_handle
*trans
;
321 struct btrfs_key key
;
322 struct btrfs_root_item root_item
;
323 struct btrfs_inode_item
*inode_item
;
324 struct extent_buffer
*leaf
;
325 struct btrfs_root
*new_root
;
326 struct dentry
*parent
= dget_parent(dentry
);
331 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
334 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
340 dir
= parent
->d_inode
;
348 trans
= btrfs_start_transaction(root
, 6);
351 return PTR_ERR(trans
);
354 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
355 0, objectid
, NULL
, 0, 0, 0);
361 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
362 btrfs_set_header_bytenr(leaf
, leaf
->start
);
363 btrfs_set_header_generation(leaf
, trans
->transid
);
364 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
365 btrfs_set_header_owner(leaf
, objectid
);
367 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
368 (unsigned long)btrfs_header_fsid(leaf
),
370 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
371 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
373 btrfs_mark_buffer_dirty(leaf
);
375 inode_item
= &root_item
.inode
;
376 memset(inode_item
, 0, sizeof(*inode_item
));
377 inode_item
->generation
= cpu_to_le64(1);
378 inode_item
->size
= cpu_to_le64(3);
379 inode_item
->nlink
= cpu_to_le32(1);
380 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
381 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
384 root_item
.byte_limit
= 0;
385 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
387 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
388 btrfs_set_root_generation(&root_item
, trans
->transid
);
389 btrfs_set_root_level(&root_item
, 0);
390 btrfs_set_root_refs(&root_item
, 1);
391 btrfs_set_root_used(&root_item
, leaf
->len
);
392 btrfs_set_root_last_snapshot(&root_item
, 0);
394 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
395 root_item
.drop_level
= 0;
397 btrfs_tree_unlock(leaf
);
398 free_extent_buffer(leaf
);
401 btrfs_set_root_dirid(&root_item
, new_dirid
);
403 key
.objectid
= objectid
;
405 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
406 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
411 key
.offset
= (u64
)-1;
412 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
413 BUG_ON(IS_ERR(new_root
));
415 btrfs_record_root_in_trans(trans
, new_root
);
417 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
418 BTRFS_I(dir
)->block_group
);
420 * insert the directory item
422 ret
= btrfs_set_inode_index(dir
, &index
);
425 ret
= btrfs_insert_dir_item(trans
, root
,
426 name
, namelen
, dir
, &key
,
427 BTRFS_FT_DIR
, index
);
431 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
432 ret
= btrfs_update_inode(trans
, root
, dir
);
435 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
436 objectid
, root
->root_key
.objectid
,
437 btrfs_ino(dir
), index
, name
, namelen
);
441 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
445 *async_transid
= trans
->transid
;
446 err
= btrfs_commit_transaction_async(trans
, root
, 1);
448 err
= btrfs_commit_transaction(trans
, root
);
455 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
456 char *name
, int namelen
, u64
*async_transid
,
460 struct dentry
*parent
;
461 struct btrfs_pending_snapshot
*pending_snapshot
;
462 struct btrfs_trans_handle
*trans
;
468 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
469 if (!pending_snapshot
)
472 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
473 pending_snapshot
->dentry
= dentry
;
474 pending_snapshot
->root
= root
;
475 pending_snapshot
->readonly
= readonly
;
477 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
479 ret
= PTR_ERR(trans
);
483 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
486 list_add(&pending_snapshot
->list
,
487 &trans
->transaction
->pending_snapshots
);
489 *async_transid
= trans
->transid
;
490 ret
= btrfs_commit_transaction_async(trans
,
491 root
->fs_info
->extent_root
, 1);
493 ret
= btrfs_commit_transaction(trans
,
494 root
->fs_info
->extent_root
);
498 ret
= pending_snapshot
->error
;
502 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
506 parent
= dget_parent(dentry
);
507 inode
= btrfs_lookup_dentry(parent
->d_inode
, dentry
);
510 ret
= PTR_ERR(inode
);
514 d_instantiate(dentry
, inode
);
517 kfree(pending_snapshot
);
521 /* copy of check_sticky in fs/namei.c()
522 * It's inline, so penalty for filesystems that don't use sticky bit is
525 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
527 uid_t fsuid
= current_fsuid();
529 if (!(dir
->i_mode
& S_ISVTX
))
531 if (inode
->i_uid
== fsuid
)
533 if (dir
->i_uid
== fsuid
)
535 return !capable(CAP_FOWNER
);
538 /* copy of may_delete in fs/namei.c()
539 * Check whether we can remove a link victim from directory dir, check
540 * whether the type of victim is right.
541 * 1. We can't do it if dir is read-only (done in permission())
542 * 2. We should have write and exec permissions on dir
543 * 3. We can't remove anything from append-only dir
544 * 4. We can't do anything with immutable dir (done in permission())
545 * 5. If the sticky bit on dir is set we should either
546 * a. be owner of dir, or
547 * b. be owner of victim, or
548 * c. have CAP_FOWNER capability
549 * 6. If the victim is append-only or immutable we can't do antyhing with
550 * links pointing to it.
551 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
552 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
553 * 9. We can't remove a root or mountpoint.
554 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
555 * nfs_async_unlink().
558 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
562 if (!victim
->d_inode
)
565 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
566 audit_inode_child(victim
, dir
);
568 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
573 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
574 IS_APPEND(victim
->d_inode
)||
575 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
578 if (!S_ISDIR(victim
->d_inode
->i_mode
))
582 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
586 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
591 /* copy of may_create in fs/namei.c() */
592 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
598 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
602 * Create a new subvolume below @parent. This is largely modeled after
603 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
604 * inside this filesystem so it's quite a bit simpler.
606 static noinline
int btrfs_mksubvol(struct path
*parent
,
607 char *name
, int namelen
,
608 struct btrfs_root
*snap_src
,
609 u64
*async_transid
, bool readonly
)
611 struct inode
*dir
= parent
->dentry
->d_inode
;
612 struct dentry
*dentry
;
615 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
617 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
618 error
= PTR_ERR(dentry
);
626 error
= mnt_want_write(parent
->mnt
);
630 error
= btrfs_may_create(dir
, dentry
);
634 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
636 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
640 error
= create_snapshot(snap_src
, dentry
,
641 name
, namelen
, async_transid
, readonly
);
643 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
644 name
, namelen
, async_transid
);
647 fsnotify_mkdir(dir
, dentry
);
649 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
651 mnt_drop_write(parent
->mnt
);
655 mutex_unlock(&dir
->i_mutex
);
659 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
660 int thresh
, u64
*last_len
, u64
*skip
,
663 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
664 struct extent_map
*em
= NULL
;
665 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
673 * make sure that once we start defragging and extent, we keep on
676 if (start
< *defrag_end
)
682 * hopefully we have this extent in the tree already, try without
683 * the full extent lock
685 read_lock(&em_tree
->lock
);
686 em
= lookup_extent_mapping(em_tree
, start
, len
);
687 read_unlock(&em_tree
->lock
);
690 /* get the big lock and read metadata off disk */
691 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
692 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
693 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
699 /* this will cover holes, and inline extents */
700 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
704 * we hit a real extent, if it is big don't bother defragging it again
706 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
710 * last_len ends up being a counter of how many bytes we've defragged.
711 * every time we choose not to defrag an extent, we reset *last_len
712 * so that the next tiny extent will force a defrag.
714 * The end result of this is that tiny extents before a single big
715 * extent will force at least part of that big extent to be defragged.
719 *defrag_end
= extent_map_end(em
);
722 *skip
= extent_map_end(em
);
730 static int btrfs_defrag_file(struct file
*file
,
731 struct btrfs_ioctl_defrag_range_args
*range
)
733 struct inode
*inode
= fdentry(file
)->d_inode
;
734 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
735 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
736 struct btrfs_ordered_extent
*ordered
;
738 struct btrfs_super_block
*disk_super
;
739 unsigned long last_index
;
740 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
741 unsigned long total_read
= 0;
750 int compress_type
= BTRFS_COMPRESS_ZLIB
;
752 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
753 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
755 if (range
->compress_type
)
756 compress_type
= range
->compress_type
;
759 if (inode
->i_size
== 0)
762 if (range
->start
+ range
->len
> range
->start
) {
763 last_index
= min_t(u64
, inode
->i_size
- 1,
764 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
766 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
769 i
= range
->start
>> PAGE_CACHE_SHIFT
;
770 while (i
<= last_index
) {
771 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
773 range
->extent_thresh
,
778 * the should_defrag function tells us how much to skip
779 * bump our counter by the suggested amount
781 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
782 i
= max(i
+ 1, next
);
786 if (total_read
% ra_pages
== 0) {
787 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
788 min(last_index
, i
+ ra_pages
- 1));
791 mutex_lock(&inode
->i_mutex
);
792 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
793 BTRFS_I(inode
)->force_compress
= compress_type
;
795 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
799 if (inode
->i_size
== 0 ||
800 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
802 goto err_reservations
;
805 page
= grab_cache_page(inode
->i_mapping
, i
);
808 goto err_reservations
;
811 if (!PageUptodate(page
)) {
812 btrfs_readpage(NULL
, page
);
814 if (!PageUptodate(page
)) {
816 page_cache_release(page
);
818 goto err_reservations
;
822 if (page
->mapping
!= inode
->i_mapping
) {
824 page_cache_release(page
);
828 wait_on_page_writeback(page
);
830 if (PageDirty(page
)) {
831 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
835 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
836 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
837 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
839 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
841 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
843 page_cache_release(page
);
844 btrfs_start_ordered_extent(inode
, ordered
, 1);
845 btrfs_put_ordered_extent(ordered
);
848 set_page_extent_mapped(page
);
851 * this makes sure page_mkwrite is called on the
852 * page if it is dirtied again later
854 clear_page_dirty_for_io(page
);
855 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
856 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
857 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
859 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
860 ClearPageChecked(page
);
861 set_page_dirty(page
);
862 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
866 page_cache_release(page
);
867 mutex_unlock(&inode
->i_mutex
);
869 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
873 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
874 filemap_flush(inode
->i_mapping
);
876 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
877 /* the filemap_flush will queue IO into the worker threads, but
878 * we have to make sure the IO is actually started and that
879 * ordered extents get created before we return
881 atomic_inc(&root
->fs_info
->async_submit_draining
);
882 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
883 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
884 wait_event(root
->fs_info
->async_submit_wait
,
885 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
886 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
888 atomic_dec(&root
->fs_info
->async_submit_draining
);
890 mutex_lock(&inode
->i_mutex
);
891 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
892 mutex_unlock(&inode
->i_mutex
);
895 disk_super
= &root
->fs_info
->super_copy
;
896 features
= btrfs_super_incompat_flags(disk_super
);
897 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
898 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
899 btrfs_set_super_incompat_flags(disk_super
, features
);
905 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
907 mutex_unlock(&inode
->i_mutex
);
911 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
917 struct btrfs_ioctl_vol_args
*vol_args
;
918 struct btrfs_trans_handle
*trans
;
919 struct btrfs_device
*device
= NULL
;
925 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
928 if (!capable(CAP_SYS_ADMIN
))
931 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
932 if (IS_ERR(vol_args
))
933 return PTR_ERR(vol_args
);
935 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
937 mutex_lock(&root
->fs_info
->volume_mutex
);
938 sizestr
= vol_args
->name
;
939 devstr
= strchr(sizestr
, ':');
942 sizestr
= devstr
+ 1;
944 devstr
= vol_args
->name
;
945 devid
= simple_strtoull(devstr
, &end
, 10);
946 printk(KERN_INFO
"resizing devid %llu\n",
947 (unsigned long long)devid
);
949 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
951 printk(KERN_INFO
"resizer unable to find device %llu\n",
952 (unsigned long long)devid
);
956 if (!strcmp(sizestr
, "max"))
957 new_size
= device
->bdev
->bd_inode
->i_size
;
959 if (sizestr
[0] == '-') {
962 } else if (sizestr
[0] == '+') {
966 new_size
= memparse(sizestr
, NULL
);
973 old_size
= device
->total_bytes
;
976 if (new_size
> old_size
) {
980 new_size
= old_size
- new_size
;
981 } else if (mod
> 0) {
982 new_size
= old_size
+ new_size
;
985 if (new_size
< 256 * 1024 * 1024) {
989 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
994 do_div(new_size
, root
->sectorsize
);
995 new_size
*= root
->sectorsize
;
997 printk(KERN_INFO
"new size for %s is %llu\n",
998 device
->name
, (unsigned long long)new_size
);
1000 if (new_size
> old_size
) {
1001 trans
= btrfs_start_transaction(root
, 0);
1002 if (IS_ERR(trans
)) {
1003 ret
= PTR_ERR(trans
);
1006 ret
= btrfs_grow_device(trans
, device
, new_size
);
1007 btrfs_commit_transaction(trans
, root
);
1009 ret
= btrfs_shrink_device(device
, new_size
);
1013 mutex_unlock(&root
->fs_info
->volume_mutex
);
1018 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1025 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1026 struct file
*src_file
;
1030 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1033 namelen
= strlen(name
);
1034 if (strchr(name
, '/')) {
1040 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1041 NULL
, transid
, readonly
);
1043 struct inode
*src_inode
;
1044 src_file
= fget(fd
);
1050 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1051 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1052 printk(KERN_INFO
"btrfs: Snapshot src from "
1058 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1059 BTRFS_I(src_inode
)->root
,
1067 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1068 void __user
*arg
, int subvol
)
1070 struct btrfs_ioctl_vol_args
*vol_args
;
1073 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1074 if (IS_ERR(vol_args
))
1075 return PTR_ERR(vol_args
);
1076 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1078 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1079 vol_args
->fd
, subvol
,
1086 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1087 void __user
*arg
, int subvol
)
1089 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1093 bool readonly
= false;
1095 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1096 if (IS_ERR(vol_args
))
1097 return PTR_ERR(vol_args
);
1098 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1100 if (vol_args
->flags
&
1101 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1106 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1108 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1111 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1112 vol_args
->fd
, subvol
,
1115 if (ret
== 0 && ptr
&&
1117 offsetof(struct btrfs_ioctl_vol_args_v2
,
1118 transid
), ptr
, sizeof(*ptr
)))
1125 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1128 struct inode
*inode
= fdentry(file
)->d_inode
;
1129 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1133 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1136 down_read(&root
->fs_info
->subvol_sem
);
1137 if (btrfs_root_readonly(root
))
1138 flags
|= BTRFS_SUBVOL_RDONLY
;
1139 up_read(&root
->fs_info
->subvol_sem
);
1141 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1147 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1150 struct inode
*inode
= fdentry(file
)->d_inode
;
1151 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1152 struct btrfs_trans_handle
*trans
;
1157 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1160 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1163 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1166 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1169 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1172 if (!inode_owner_or_capable(inode
))
1175 down_write(&root
->fs_info
->subvol_sem
);
1178 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1181 root_flags
= btrfs_root_flags(&root
->root_item
);
1182 if (flags
& BTRFS_SUBVOL_RDONLY
)
1183 btrfs_set_root_flags(&root
->root_item
,
1184 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1186 btrfs_set_root_flags(&root
->root_item
,
1187 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1189 trans
= btrfs_start_transaction(root
, 1);
1190 if (IS_ERR(trans
)) {
1191 ret
= PTR_ERR(trans
);
1195 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1196 &root
->root_key
, &root
->root_item
);
1198 btrfs_commit_transaction(trans
, root
);
1201 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1203 up_write(&root
->fs_info
->subvol_sem
);
1208 * helper to check if the subvolume references other subvolumes
1210 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1212 struct btrfs_path
*path
;
1213 struct btrfs_key key
;
1216 path
= btrfs_alloc_path();
1220 key
.objectid
= root
->root_key
.objectid
;
1221 key
.type
= BTRFS_ROOT_REF_KEY
;
1222 key
.offset
= (u64
)-1;
1224 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1231 if (path
->slots
[0] > 0) {
1233 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1234 if (key
.objectid
== root
->root_key
.objectid
&&
1235 key
.type
== BTRFS_ROOT_REF_KEY
)
1239 btrfs_free_path(path
);
1243 static noinline
int key_in_sk(struct btrfs_key
*key
,
1244 struct btrfs_ioctl_search_key
*sk
)
1246 struct btrfs_key test
;
1249 test
.objectid
= sk
->min_objectid
;
1250 test
.type
= sk
->min_type
;
1251 test
.offset
= sk
->min_offset
;
1253 ret
= btrfs_comp_cpu_keys(key
, &test
);
1257 test
.objectid
= sk
->max_objectid
;
1258 test
.type
= sk
->max_type
;
1259 test
.offset
= sk
->max_offset
;
1261 ret
= btrfs_comp_cpu_keys(key
, &test
);
1267 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1268 struct btrfs_path
*path
,
1269 struct btrfs_key
*key
,
1270 struct btrfs_ioctl_search_key
*sk
,
1272 unsigned long *sk_offset
,
1276 struct extent_buffer
*leaf
;
1277 struct btrfs_ioctl_search_header sh
;
1278 unsigned long item_off
;
1279 unsigned long item_len
;
1285 leaf
= path
->nodes
[0];
1286 slot
= path
->slots
[0];
1287 nritems
= btrfs_header_nritems(leaf
);
1289 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1293 found_transid
= btrfs_header_generation(leaf
);
1295 for (i
= slot
; i
< nritems
; i
++) {
1296 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1297 item_len
= btrfs_item_size_nr(leaf
, i
);
1299 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1302 if (sizeof(sh
) + item_len
+ *sk_offset
>
1303 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1308 btrfs_item_key_to_cpu(leaf
, key
, i
);
1309 if (!key_in_sk(key
, sk
))
1312 sh
.objectid
= key
->objectid
;
1313 sh
.offset
= key
->offset
;
1314 sh
.type
= key
->type
;
1316 sh
.transid
= found_transid
;
1318 /* copy search result header */
1319 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1320 *sk_offset
+= sizeof(sh
);
1323 char *p
= buf
+ *sk_offset
;
1325 read_extent_buffer(leaf
, p
,
1326 item_off
, item_len
);
1327 *sk_offset
+= item_len
;
1331 if (*num_found
>= sk
->nr_items
)
1336 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1338 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1341 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1351 static noinline
int search_ioctl(struct inode
*inode
,
1352 struct btrfs_ioctl_search_args
*args
)
1354 struct btrfs_root
*root
;
1355 struct btrfs_key key
;
1356 struct btrfs_key max_key
;
1357 struct btrfs_path
*path
;
1358 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1359 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1362 unsigned long sk_offset
= 0;
1364 path
= btrfs_alloc_path();
1368 if (sk
->tree_id
== 0) {
1369 /* search the root of the inode that was passed */
1370 root
= BTRFS_I(inode
)->root
;
1372 key
.objectid
= sk
->tree_id
;
1373 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1374 key
.offset
= (u64
)-1;
1375 root
= btrfs_read_fs_root_no_name(info
, &key
);
1377 printk(KERN_ERR
"could not find root %llu\n",
1379 btrfs_free_path(path
);
1384 key
.objectid
= sk
->min_objectid
;
1385 key
.type
= sk
->min_type
;
1386 key
.offset
= sk
->min_offset
;
1388 max_key
.objectid
= sk
->max_objectid
;
1389 max_key
.type
= sk
->max_type
;
1390 max_key
.offset
= sk
->max_offset
;
1392 path
->keep_locks
= 1;
1395 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1402 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1403 &sk_offset
, &num_found
);
1404 btrfs_release_path(path
);
1405 if (ret
|| num_found
>= sk
->nr_items
)
1411 sk
->nr_items
= num_found
;
1412 btrfs_free_path(path
);
1416 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1419 struct btrfs_ioctl_search_args
*args
;
1420 struct inode
*inode
;
1423 if (!capable(CAP_SYS_ADMIN
))
1426 args
= memdup_user(argp
, sizeof(*args
));
1428 return PTR_ERR(args
);
1430 inode
= fdentry(file
)->d_inode
;
1431 ret
= search_ioctl(inode
, args
);
1432 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1439 * Search INODE_REFs to identify path name of 'dirid' directory
1440 * in a 'tree_id' tree. and sets path name to 'name'.
1442 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1443 u64 tree_id
, u64 dirid
, char *name
)
1445 struct btrfs_root
*root
;
1446 struct btrfs_key key
;
1452 struct btrfs_inode_ref
*iref
;
1453 struct extent_buffer
*l
;
1454 struct btrfs_path
*path
;
1456 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1461 path
= btrfs_alloc_path();
1465 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1467 key
.objectid
= tree_id
;
1468 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1469 key
.offset
= (u64
)-1;
1470 root
= btrfs_read_fs_root_no_name(info
, &key
);
1472 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1477 key
.objectid
= dirid
;
1478 key
.type
= BTRFS_INODE_REF_KEY
;
1479 key
.offset
= (u64
)-1;
1482 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1487 slot
= path
->slots
[0];
1488 if (ret
> 0 && slot
> 0)
1490 btrfs_item_key_to_cpu(l
, &key
, slot
);
1492 if (ret
> 0 && (key
.objectid
!= dirid
||
1493 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1498 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1499 len
= btrfs_inode_ref_name_len(l
, iref
);
1501 total_len
+= len
+ 1;
1506 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1508 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1511 btrfs_release_path(path
);
1512 key
.objectid
= key
.offset
;
1513 key
.offset
= (u64
)-1;
1514 dirid
= key
.objectid
;
1519 memcpy(name
, ptr
, total_len
);
1520 name
[total_len
]='\0';
1523 btrfs_free_path(path
);
1527 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1530 struct btrfs_ioctl_ino_lookup_args
*args
;
1531 struct inode
*inode
;
1534 if (!capable(CAP_SYS_ADMIN
))
1537 args
= memdup_user(argp
, sizeof(*args
));
1539 return PTR_ERR(args
);
1541 inode
= fdentry(file
)->d_inode
;
1543 if (args
->treeid
== 0)
1544 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1546 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1547 args
->treeid
, args
->objectid
,
1550 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1557 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1560 struct dentry
*parent
= fdentry(file
);
1561 struct dentry
*dentry
;
1562 struct inode
*dir
= parent
->d_inode
;
1563 struct inode
*inode
;
1564 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1565 struct btrfs_root
*dest
= NULL
;
1566 struct btrfs_ioctl_vol_args
*vol_args
;
1567 struct btrfs_trans_handle
*trans
;
1572 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1573 if (IS_ERR(vol_args
))
1574 return PTR_ERR(vol_args
);
1576 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1577 namelen
= strlen(vol_args
->name
);
1578 if (strchr(vol_args
->name
, '/') ||
1579 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1584 err
= mnt_want_write(file
->f_path
.mnt
);
1588 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1589 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1590 if (IS_ERR(dentry
)) {
1591 err
= PTR_ERR(dentry
);
1592 goto out_unlock_dir
;
1595 if (!dentry
->d_inode
) {
1600 inode
= dentry
->d_inode
;
1601 dest
= BTRFS_I(inode
)->root
;
1602 if (!capable(CAP_SYS_ADMIN
)){
1604 * Regular user. Only allow this with a special mount
1605 * option, when the user has write+exec access to the
1606 * subvol root, and when rmdir(2) would have been
1609 * Note that this is _not_ check that the subvol is
1610 * empty or doesn't contain data that we wouldn't
1611 * otherwise be able to delete.
1613 * Users who want to delete empty subvols should try
1617 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1621 * Do not allow deletion if the parent dir is the same
1622 * as the dir to be deleted. That means the ioctl
1623 * must be called on the dentry referencing the root
1624 * of the subvol, not a random directory contained
1631 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1635 /* check if subvolume may be deleted by a non-root user */
1636 err
= btrfs_may_delete(dir
, dentry
, 1);
1641 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1646 mutex_lock(&inode
->i_mutex
);
1647 err
= d_invalidate(dentry
);
1651 down_write(&root
->fs_info
->subvol_sem
);
1653 err
= may_destroy_subvol(dest
);
1657 trans
= btrfs_start_transaction(root
, 0);
1658 if (IS_ERR(trans
)) {
1659 err
= PTR_ERR(trans
);
1662 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1664 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1665 dest
->root_key
.objectid
,
1666 dentry
->d_name
.name
,
1667 dentry
->d_name
.len
);
1670 btrfs_record_root_in_trans(trans
, dest
);
1672 memset(&dest
->root_item
.drop_progress
, 0,
1673 sizeof(dest
->root_item
.drop_progress
));
1674 dest
->root_item
.drop_level
= 0;
1675 btrfs_set_root_refs(&dest
->root_item
, 0);
1677 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1678 ret
= btrfs_insert_orphan_item(trans
,
1679 root
->fs_info
->tree_root
,
1680 dest
->root_key
.objectid
);
1684 ret
= btrfs_end_transaction(trans
, root
);
1686 inode
->i_flags
|= S_DEAD
;
1688 up_write(&root
->fs_info
->subvol_sem
);
1690 mutex_unlock(&inode
->i_mutex
);
1692 shrink_dcache_sb(root
->fs_info
->sb
);
1693 btrfs_invalidate_inodes(dest
);
1699 mutex_unlock(&dir
->i_mutex
);
1700 mnt_drop_write(file
->f_path
.mnt
);
1706 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1708 struct inode
*inode
= fdentry(file
)->d_inode
;
1709 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1710 struct btrfs_ioctl_defrag_range_args
*range
;
1713 if (btrfs_root_readonly(root
))
1716 ret
= mnt_want_write(file
->f_path
.mnt
);
1720 switch (inode
->i_mode
& S_IFMT
) {
1722 if (!capable(CAP_SYS_ADMIN
)) {
1726 ret
= btrfs_defrag_root(root
, 0);
1729 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1732 if (!(file
->f_mode
& FMODE_WRITE
)) {
1737 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1744 if (copy_from_user(range
, argp
,
1750 /* compression requires us to start the IO */
1751 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1752 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1753 range
->extent_thresh
= (u32
)-1;
1756 /* the rest are all set to zero by kzalloc */
1757 range
->len
= (u64
)-1;
1759 ret
= btrfs_defrag_file(file
, range
);
1766 mnt_drop_write(file
->f_path
.mnt
);
1770 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1772 struct btrfs_ioctl_vol_args
*vol_args
;
1775 if (!capable(CAP_SYS_ADMIN
))
1778 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1779 if (IS_ERR(vol_args
))
1780 return PTR_ERR(vol_args
);
1782 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1783 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1789 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1791 struct btrfs_ioctl_vol_args
*vol_args
;
1794 if (!capable(CAP_SYS_ADMIN
))
1797 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1800 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1801 if (IS_ERR(vol_args
))
1802 return PTR_ERR(vol_args
);
1804 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1805 ret
= btrfs_rm_device(root
, vol_args
->name
);
1811 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
1813 struct btrfs_ioctl_fs_info_args fi_args
;
1814 struct btrfs_device
*device
;
1815 struct btrfs_device
*next
;
1816 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1818 if (!capable(CAP_SYS_ADMIN
))
1821 fi_args
.num_devices
= fs_devices
->num_devices
;
1823 memcpy(&fi_args
.fsid
, root
->fs_info
->fsid
, sizeof(fi_args
.fsid
));
1825 mutex_lock(&fs_devices
->device_list_mutex
);
1826 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
1827 if (device
->devid
> fi_args
.max_id
)
1828 fi_args
.max_id
= device
->devid
;
1830 mutex_unlock(&fs_devices
->device_list_mutex
);
1832 if (copy_to_user(arg
, &fi_args
, sizeof(fi_args
)))
1838 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
1840 struct btrfs_ioctl_dev_info_args
*di_args
;
1841 struct btrfs_device
*dev
;
1842 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
1844 char *s_uuid
= NULL
;
1845 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
1847 if (!capable(CAP_SYS_ADMIN
))
1850 di_args
= memdup_user(arg
, sizeof(*di_args
));
1851 if (IS_ERR(di_args
))
1852 return PTR_ERR(di_args
);
1854 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
1855 s_uuid
= di_args
->uuid
;
1857 mutex_lock(&fs_devices
->device_list_mutex
);
1858 dev
= btrfs_find_device(root
, di_args
->devid
, s_uuid
, NULL
);
1859 mutex_unlock(&fs_devices
->device_list_mutex
);
1866 di_args
->devid
= dev
->devid
;
1867 di_args
->bytes_used
= dev
->bytes_used
;
1868 di_args
->total_bytes
= dev
->total_bytes
;
1869 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
1870 strncpy(di_args
->path
, dev
->name
, sizeof(di_args
->path
));
1873 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
1880 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1881 u64 off
, u64 olen
, u64 destoff
)
1883 struct inode
*inode
= fdentry(file
)->d_inode
;
1884 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1885 struct file
*src_file
;
1887 struct btrfs_trans_handle
*trans
;
1888 struct btrfs_path
*path
;
1889 struct extent_buffer
*leaf
;
1891 struct btrfs_key key
;
1896 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1901 * - split compressed inline extents. annoying: we need to
1902 * decompress into destination's address_space (the file offset
1903 * may change, so source mapping won't do), then recompress (or
1904 * otherwise reinsert) a subrange.
1905 * - allow ranges within the same file to be cloned (provided
1906 * they don't overlap)?
1909 /* the destination must be opened for writing */
1910 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1913 if (btrfs_root_readonly(root
))
1916 ret
= mnt_want_write(file
->f_path
.mnt
);
1920 src_file
= fget(srcfd
);
1923 goto out_drop_write
;
1926 src
= src_file
->f_dentry
->d_inode
;
1932 /* the src must be open for reading */
1933 if (!(src_file
->f_mode
& FMODE_READ
))
1937 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1941 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1945 buf
= vmalloc(btrfs_level_size(root
, 0));
1949 path
= btrfs_alloc_path();
1957 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
1958 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
1960 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
1961 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1964 /* determine range to clone */
1966 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1969 olen
= len
= src
->i_size
- off
;
1970 /* if we extend to eof, continue to block boundary */
1971 if (off
+ len
== src
->i_size
)
1972 len
= ALIGN(src
->i_size
, bs
) - off
;
1974 /* verify the end result is block aligned */
1975 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
1976 !IS_ALIGNED(destoff
, bs
))
1979 /* do any pending delalloc/csum calc on src, one way or
1980 another, and lock file content */
1982 struct btrfs_ordered_extent
*ordered
;
1983 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1984 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
1986 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
1987 EXTENT_DELALLOC
, 0, NULL
))
1989 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1991 btrfs_put_ordered_extent(ordered
);
1992 btrfs_wait_ordered_range(src
, off
, len
);
1996 key
.objectid
= btrfs_ino(src
);
1997 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2002 * note the key will change type as we walk through the
2005 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2009 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2010 if (path
->slots
[0] >= nritems
) {
2011 ret
= btrfs_next_leaf(root
, path
);
2016 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2018 leaf
= path
->nodes
[0];
2019 slot
= path
->slots
[0];
2021 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2022 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2023 key
.objectid
!= btrfs_ino(src
))
2026 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2027 struct btrfs_file_extent_item
*extent
;
2030 struct btrfs_key new_key
;
2031 u64 disko
= 0, diskl
= 0;
2032 u64 datao
= 0, datal
= 0;
2036 size
= btrfs_item_size_nr(leaf
, slot
);
2037 read_extent_buffer(leaf
, buf
,
2038 btrfs_item_ptr_offset(leaf
, slot
),
2041 extent
= btrfs_item_ptr(leaf
, slot
,
2042 struct btrfs_file_extent_item
);
2043 comp
= btrfs_file_extent_compression(leaf
, extent
);
2044 type
= btrfs_file_extent_type(leaf
, extent
);
2045 if (type
== BTRFS_FILE_EXTENT_REG
||
2046 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2047 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2049 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2051 datao
= btrfs_file_extent_offset(leaf
, extent
);
2052 datal
= btrfs_file_extent_num_bytes(leaf
,
2054 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2055 /* take upper bound, may be compressed */
2056 datal
= btrfs_file_extent_ram_bytes(leaf
,
2059 btrfs_release_path(path
);
2061 if (key
.offset
+ datal
<= off
||
2062 key
.offset
>= off
+len
)
2065 memcpy(&new_key
, &key
, sizeof(new_key
));
2066 new_key
.objectid
= btrfs_ino(inode
);
2067 if (off
<= key
.offset
)
2068 new_key
.offset
= key
.offset
+ destoff
- off
;
2070 new_key
.offset
= destoff
;
2072 trans
= btrfs_start_transaction(root
, 1);
2073 if (IS_ERR(trans
)) {
2074 ret
= PTR_ERR(trans
);
2078 if (type
== BTRFS_FILE_EXTENT_REG
||
2079 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2080 if (off
> key
.offset
) {
2081 datao
+= off
- key
.offset
;
2082 datal
-= off
- key
.offset
;
2085 if (key
.offset
+ datal
> off
+ len
)
2086 datal
= off
+ len
- key
.offset
;
2088 ret
= btrfs_drop_extents(trans
, inode
,
2090 new_key
.offset
+ datal
,
2094 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2098 leaf
= path
->nodes
[0];
2099 slot
= path
->slots
[0];
2100 write_extent_buffer(leaf
, buf
,
2101 btrfs_item_ptr_offset(leaf
, slot
),
2104 extent
= btrfs_item_ptr(leaf
, slot
,
2105 struct btrfs_file_extent_item
);
2107 /* disko == 0 means it's a hole */
2111 btrfs_set_file_extent_offset(leaf
, extent
,
2113 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2116 inode_add_bytes(inode
, datal
);
2117 ret
= btrfs_inc_extent_ref(trans
, root
,
2119 root
->root_key
.objectid
,
2121 new_key
.offset
- datao
);
2124 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2127 if (off
> key
.offset
) {
2128 skip
= off
- key
.offset
;
2129 new_key
.offset
+= skip
;
2132 if (key
.offset
+ datal
> off
+len
)
2133 trim
= key
.offset
+ datal
- (off
+len
);
2135 if (comp
&& (skip
|| trim
)) {
2137 btrfs_end_transaction(trans
, root
);
2140 size
-= skip
+ trim
;
2141 datal
-= skip
+ trim
;
2143 ret
= btrfs_drop_extents(trans
, inode
,
2145 new_key
.offset
+ datal
,
2149 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2155 btrfs_file_extent_calc_inline_size(0);
2156 memmove(buf
+start
, buf
+start
+skip
,
2160 leaf
= path
->nodes
[0];
2161 slot
= path
->slots
[0];
2162 write_extent_buffer(leaf
, buf
,
2163 btrfs_item_ptr_offset(leaf
, slot
),
2165 inode_add_bytes(inode
, datal
);
2168 btrfs_mark_buffer_dirty(leaf
);
2169 btrfs_release_path(path
);
2171 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2174 * we round up to the block size at eof when
2175 * determining which extents to clone above,
2176 * but shouldn't round up the file size
2178 endoff
= new_key
.offset
+ datal
;
2179 if (endoff
> destoff
+olen
)
2180 endoff
= destoff
+olen
;
2181 if (endoff
> inode
->i_size
)
2182 btrfs_i_size_write(inode
, endoff
);
2184 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
2185 ret
= btrfs_update_inode(trans
, root
, inode
);
2187 btrfs_end_transaction(trans
, root
);
2190 btrfs_release_path(path
);
2195 btrfs_release_path(path
);
2196 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2198 mutex_unlock(&src
->i_mutex
);
2199 mutex_unlock(&inode
->i_mutex
);
2201 btrfs_free_path(path
);
2205 mnt_drop_write(file
->f_path
.mnt
);
2209 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2211 struct btrfs_ioctl_clone_range_args args
;
2213 if (copy_from_user(&args
, argp
, sizeof(args
)))
2215 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2216 args
.src_length
, args
.dest_offset
);
2220 * there are many ways the trans_start and trans_end ioctls can lead
2221 * to deadlocks. They should only be used by applications that
2222 * basically own the machine, and have a very in depth understanding
2223 * of all the possible deadlocks and enospc problems.
2225 static long btrfs_ioctl_trans_start(struct file
*file
)
2227 struct inode
*inode
= fdentry(file
)->d_inode
;
2228 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2229 struct btrfs_trans_handle
*trans
;
2233 if (!capable(CAP_SYS_ADMIN
))
2237 if (file
->private_data
)
2241 if (btrfs_root_readonly(root
))
2244 ret
= mnt_want_write(file
->f_path
.mnt
);
2248 mutex_lock(&root
->fs_info
->trans_mutex
);
2249 root
->fs_info
->open_ioctl_trans
++;
2250 mutex_unlock(&root
->fs_info
->trans_mutex
);
2253 trans
= btrfs_start_ioctl_transaction(root
, 0);
2257 file
->private_data
= trans
;
2261 mutex_lock(&root
->fs_info
->trans_mutex
);
2262 root
->fs_info
->open_ioctl_trans
--;
2263 mutex_unlock(&root
->fs_info
->trans_mutex
);
2264 mnt_drop_write(file
->f_path
.mnt
);
2269 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2271 struct inode
*inode
= fdentry(file
)->d_inode
;
2272 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2273 struct btrfs_root
*new_root
;
2274 struct btrfs_dir_item
*di
;
2275 struct btrfs_trans_handle
*trans
;
2276 struct btrfs_path
*path
;
2277 struct btrfs_key location
;
2278 struct btrfs_disk_key disk_key
;
2279 struct btrfs_super_block
*disk_super
;
2284 if (!capable(CAP_SYS_ADMIN
))
2287 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2291 objectid
= root
->root_key
.objectid
;
2293 location
.objectid
= objectid
;
2294 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2295 location
.offset
= (u64
)-1;
2297 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2298 if (IS_ERR(new_root
))
2299 return PTR_ERR(new_root
);
2301 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2304 path
= btrfs_alloc_path();
2307 path
->leave_spinning
= 1;
2309 trans
= btrfs_start_transaction(root
, 1);
2310 if (IS_ERR(trans
)) {
2311 btrfs_free_path(path
);
2312 return PTR_ERR(trans
);
2315 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2316 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2317 dir_id
, "default", 7, 1);
2318 if (IS_ERR_OR_NULL(di
)) {
2319 btrfs_free_path(path
);
2320 btrfs_end_transaction(trans
, root
);
2321 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2322 "this isn't going to work\n");
2326 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2327 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2328 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2329 btrfs_free_path(path
);
2331 disk_super
= &root
->fs_info
->super_copy
;
2332 features
= btrfs_super_incompat_flags(disk_super
);
2333 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2334 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2335 btrfs_set_super_incompat_flags(disk_super
, features
);
2337 btrfs_end_transaction(trans
, root
);
2342 static void get_block_group_info(struct list_head
*groups_list
,
2343 struct btrfs_ioctl_space_info
*space
)
2345 struct btrfs_block_group_cache
*block_group
;
2347 space
->total_bytes
= 0;
2348 space
->used_bytes
= 0;
2350 list_for_each_entry(block_group
, groups_list
, list
) {
2351 space
->flags
= block_group
->flags
;
2352 space
->total_bytes
+= block_group
->key
.offset
;
2353 space
->used_bytes
+=
2354 btrfs_block_group_used(&block_group
->item
);
2358 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2360 struct btrfs_ioctl_space_args space_args
;
2361 struct btrfs_ioctl_space_info space
;
2362 struct btrfs_ioctl_space_info
*dest
;
2363 struct btrfs_ioctl_space_info
*dest_orig
;
2364 struct btrfs_ioctl_space_info __user
*user_dest
;
2365 struct btrfs_space_info
*info
;
2366 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2367 BTRFS_BLOCK_GROUP_SYSTEM
,
2368 BTRFS_BLOCK_GROUP_METADATA
,
2369 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2376 if (copy_from_user(&space_args
,
2377 (struct btrfs_ioctl_space_args __user
*)arg
,
2378 sizeof(space_args
)))
2381 for (i
= 0; i
< num_types
; i
++) {
2382 struct btrfs_space_info
*tmp
;
2386 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2388 if (tmp
->flags
== types
[i
]) {
2398 down_read(&info
->groups_sem
);
2399 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2400 if (!list_empty(&info
->block_groups
[c
]))
2403 up_read(&info
->groups_sem
);
2406 /* space_slots == 0 means they are asking for a count */
2407 if (space_args
.space_slots
== 0) {
2408 space_args
.total_spaces
= slot_count
;
2412 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2414 alloc_size
= sizeof(*dest
) * slot_count
;
2416 /* we generally have at most 6 or so space infos, one for each raid
2417 * level. So, a whole page should be more than enough for everyone
2419 if (alloc_size
> PAGE_CACHE_SIZE
)
2422 space_args
.total_spaces
= 0;
2423 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2428 /* now we have a buffer to copy into */
2429 for (i
= 0; i
< num_types
; i
++) {
2430 struct btrfs_space_info
*tmp
;
2437 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2439 if (tmp
->flags
== types
[i
]) {
2448 down_read(&info
->groups_sem
);
2449 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2450 if (!list_empty(&info
->block_groups
[c
])) {
2451 get_block_group_info(&info
->block_groups
[c
],
2453 memcpy(dest
, &space
, sizeof(space
));
2455 space_args
.total_spaces
++;
2461 up_read(&info
->groups_sem
);
2464 user_dest
= (struct btrfs_ioctl_space_info
*)
2465 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2467 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2472 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2479 * there are many ways the trans_start and trans_end ioctls can lead
2480 * to deadlocks. They should only be used by applications that
2481 * basically own the machine, and have a very in depth understanding
2482 * of all the possible deadlocks and enospc problems.
2484 long btrfs_ioctl_trans_end(struct file
*file
)
2486 struct inode
*inode
= fdentry(file
)->d_inode
;
2487 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2488 struct btrfs_trans_handle
*trans
;
2490 trans
= file
->private_data
;
2493 file
->private_data
= NULL
;
2495 btrfs_end_transaction(trans
, root
);
2497 mutex_lock(&root
->fs_info
->trans_mutex
);
2498 root
->fs_info
->open_ioctl_trans
--;
2499 mutex_unlock(&root
->fs_info
->trans_mutex
);
2501 mnt_drop_write(file
->f_path
.mnt
);
2505 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2507 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2508 struct btrfs_trans_handle
*trans
;
2512 trans
= btrfs_start_transaction(root
, 0);
2514 return PTR_ERR(trans
);
2515 transid
= trans
->transid
;
2516 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2518 btrfs_end_transaction(trans
, root
);
2523 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2528 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2530 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2534 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2537 transid
= 0; /* current trans */
2539 return btrfs_wait_for_commit(root
, transid
);
2542 static long btrfs_ioctl_scrub(struct btrfs_root
*root
, void __user
*arg
)
2545 struct btrfs_ioctl_scrub_args
*sa
;
2547 if (!capable(CAP_SYS_ADMIN
))
2550 sa
= memdup_user(arg
, sizeof(*sa
));
2554 ret
= btrfs_scrub_dev(root
, sa
->devid
, sa
->start
, sa
->end
,
2555 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
);
2557 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2564 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
2566 if (!capable(CAP_SYS_ADMIN
))
2569 return btrfs_scrub_cancel(root
);
2572 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
2575 struct btrfs_ioctl_scrub_args
*sa
;
2578 if (!capable(CAP_SYS_ADMIN
))
2581 sa
= memdup_user(arg
, sizeof(*sa
));
2585 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
2587 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2594 long btrfs_ioctl(struct file
*file
, unsigned int
2595 cmd
, unsigned long arg
)
2597 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2598 void __user
*argp
= (void __user
*)arg
;
2601 case FS_IOC_GETFLAGS
:
2602 return btrfs_ioctl_getflags(file
, argp
);
2603 case FS_IOC_SETFLAGS
:
2604 return btrfs_ioctl_setflags(file
, argp
);
2605 case FS_IOC_GETVERSION
:
2606 return btrfs_ioctl_getversion(file
, argp
);
2608 return btrfs_ioctl_fitrim(file
, argp
);
2609 case BTRFS_IOC_SNAP_CREATE
:
2610 return btrfs_ioctl_snap_create(file
, argp
, 0);
2611 case BTRFS_IOC_SNAP_CREATE_V2
:
2612 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
2613 case BTRFS_IOC_SUBVOL_CREATE
:
2614 return btrfs_ioctl_snap_create(file
, argp
, 1);
2615 case BTRFS_IOC_SNAP_DESTROY
:
2616 return btrfs_ioctl_snap_destroy(file
, argp
);
2617 case BTRFS_IOC_SUBVOL_GETFLAGS
:
2618 return btrfs_ioctl_subvol_getflags(file
, argp
);
2619 case BTRFS_IOC_SUBVOL_SETFLAGS
:
2620 return btrfs_ioctl_subvol_setflags(file
, argp
);
2621 case BTRFS_IOC_DEFAULT_SUBVOL
:
2622 return btrfs_ioctl_default_subvol(file
, argp
);
2623 case BTRFS_IOC_DEFRAG
:
2624 return btrfs_ioctl_defrag(file
, NULL
);
2625 case BTRFS_IOC_DEFRAG_RANGE
:
2626 return btrfs_ioctl_defrag(file
, argp
);
2627 case BTRFS_IOC_RESIZE
:
2628 return btrfs_ioctl_resize(root
, argp
);
2629 case BTRFS_IOC_ADD_DEV
:
2630 return btrfs_ioctl_add_dev(root
, argp
);
2631 case BTRFS_IOC_RM_DEV
:
2632 return btrfs_ioctl_rm_dev(root
, argp
);
2633 case BTRFS_IOC_FS_INFO
:
2634 return btrfs_ioctl_fs_info(root
, argp
);
2635 case BTRFS_IOC_DEV_INFO
:
2636 return btrfs_ioctl_dev_info(root
, argp
);
2637 case BTRFS_IOC_BALANCE
:
2638 return btrfs_balance(root
->fs_info
->dev_root
);
2639 case BTRFS_IOC_CLONE
:
2640 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2641 case BTRFS_IOC_CLONE_RANGE
:
2642 return btrfs_ioctl_clone_range(file
, argp
);
2643 case BTRFS_IOC_TRANS_START
:
2644 return btrfs_ioctl_trans_start(file
);
2645 case BTRFS_IOC_TRANS_END
:
2646 return btrfs_ioctl_trans_end(file
);
2647 case BTRFS_IOC_TREE_SEARCH
:
2648 return btrfs_ioctl_tree_search(file
, argp
);
2649 case BTRFS_IOC_INO_LOOKUP
:
2650 return btrfs_ioctl_ino_lookup(file
, argp
);
2651 case BTRFS_IOC_SPACE_INFO
:
2652 return btrfs_ioctl_space_info(root
, argp
);
2653 case BTRFS_IOC_SYNC
:
2654 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
2656 case BTRFS_IOC_START_SYNC
:
2657 return btrfs_ioctl_start_sync(file
, argp
);
2658 case BTRFS_IOC_WAIT_SYNC
:
2659 return btrfs_ioctl_wait_sync(file
, argp
);
2660 case BTRFS_IOC_SCRUB
:
2661 return btrfs_ioctl_scrub(root
, argp
);
2662 case BTRFS_IOC_SCRUB_CANCEL
:
2663 return btrfs_ioctl_scrub_cancel(root
, argp
);
2664 case BTRFS_IOC_SCRUB_PROGRESS
:
2665 return btrfs_ioctl_scrub_progress(root
, argp
);