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"
56 /* Mask out flags that are inappropriate for the given type of inode. */
57 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
61 else if (S_ISREG(mode
))
62 return flags
& ~FS_DIRSYNC_FL
;
64 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
68 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
70 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
72 unsigned int iflags
= 0;
74 if (flags
& BTRFS_INODE_SYNC
)
76 if (flags
& BTRFS_INODE_IMMUTABLE
)
77 iflags
|= FS_IMMUTABLE_FL
;
78 if (flags
& BTRFS_INODE_APPEND
)
79 iflags
|= FS_APPEND_FL
;
80 if (flags
& BTRFS_INODE_NODUMP
)
81 iflags
|= FS_NODUMP_FL
;
82 if (flags
& BTRFS_INODE_NOATIME
)
83 iflags
|= FS_NOATIME_FL
;
84 if (flags
& BTRFS_INODE_DIRSYNC
)
85 iflags
|= FS_DIRSYNC_FL
;
86 if (flags
& BTRFS_INODE_NODATACOW
)
87 iflags
|= FS_NOCOW_FL
;
89 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
90 iflags
|= FS_COMPR_FL
;
91 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
92 iflags
|= FS_NOCOMP_FL
;
98 * Update inode->i_flags based on the btrfs internal flags.
100 void btrfs_update_iflags(struct inode
*inode
)
102 struct btrfs_inode
*ip
= BTRFS_I(inode
);
104 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
106 if (ip
->flags
& BTRFS_INODE_SYNC
)
107 inode
->i_flags
|= S_SYNC
;
108 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
109 inode
->i_flags
|= S_IMMUTABLE
;
110 if (ip
->flags
& BTRFS_INODE_APPEND
)
111 inode
->i_flags
|= S_APPEND
;
112 if (ip
->flags
& BTRFS_INODE_NOATIME
)
113 inode
->i_flags
|= S_NOATIME
;
114 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
115 inode
->i_flags
|= S_DIRSYNC
;
119 * Inherit flags from the parent inode.
121 * Currently only the compression flags and the cow flags are inherited.
123 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
130 flags
= BTRFS_I(dir
)->flags
;
132 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
133 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
134 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
135 } else if (flags
& BTRFS_INODE_COMPRESS
) {
136 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
137 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
140 if (flags
& BTRFS_INODE_NODATACOW
)
141 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
143 btrfs_update_iflags(inode
);
146 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
148 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
149 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
151 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
156 static int check_flags(unsigned int flags
)
158 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
159 FS_NOATIME_FL
| FS_NODUMP_FL
| \
160 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
161 FS_NOCOMP_FL
| FS_COMPR_FL
|
165 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
171 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
173 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
174 struct btrfs_inode
*ip
= BTRFS_I(inode
);
175 struct btrfs_root
*root
= ip
->root
;
176 struct btrfs_trans_handle
*trans
;
177 unsigned int flags
, oldflags
;
180 if (btrfs_root_readonly(root
))
183 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
186 ret
= check_flags(flags
);
190 if (!inode_owner_or_capable(inode
))
193 mutex_lock(&inode
->i_mutex
);
195 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
196 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
197 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
198 if (!capable(CAP_LINUX_IMMUTABLE
)) {
204 ret
= mnt_want_write(file
->f_path
.mnt
);
208 if (flags
& FS_SYNC_FL
)
209 ip
->flags
|= BTRFS_INODE_SYNC
;
211 ip
->flags
&= ~BTRFS_INODE_SYNC
;
212 if (flags
& FS_IMMUTABLE_FL
)
213 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
215 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
216 if (flags
& FS_APPEND_FL
)
217 ip
->flags
|= BTRFS_INODE_APPEND
;
219 ip
->flags
&= ~BTRFS_INODE_APPEND
;
220 if (flags
& FS_NODUMP_FL
)
221 ip
->flags
|= BTRFS_INODE_NODUMP
;
223 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
224 if (flags
& FS_NOATIME_FL
)
225 ip
->flags
|= BTRFS_INODE_NOATIME
;
227 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
228 if (flags
& FS_DIRSYNC_FL
)
229 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
231 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
232 if (flags
& FS_NOCOW_FL
)
233 ip
->flags
|= BTRFS_INODE_NODATACOW
;
235 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
238 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
239 * flag may be changed automatically if compression code won't make
242 if (flags
& FS_NOCOMP_FL
) {
243 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
244 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
245 } else if (flags
& FS_COMPR_FL
) {
246 ip
->flags
|= BTRFS_INODE_COMPRESS
;
247 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
249 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
252 trans
= btrfs_join_transaction(root
);
253 BUG_ON(IS_ERR(trans
));
255 btrfs_update_iflags(inode
);
256 inode
->i_ctime
= CURRENT_TIME
;
257 ret
= btrfs_update_inode(trans
, root
, inode
);
260 btrfs_end_transaction(trans
, root
);
262 mnt_drop_write(file
->f_path
.mnt
);
266 mutex_unlock(&inode
->i_mutex
);
270 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
272 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
274 return put_user(inode
->i_generation
, arg
);
277 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
279 struct btrfs_root
*root
= fdentry(file
)->d_sb
->s_fs_info
;
280 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
281 struct btrfs_device
*device
;
282 struct request_queue
*q
;
283 struct fstrim_range range
;
284 u64 minlen
= ULLONG_MAX
;
286 u64 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
289 if (!capable(CAP_SYS_ADMIN
))
293 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
297 q
= bdev_get_queue(device
->bdev
);
298 if (blk_queue_discard(q
)) {
300 minlen
= min((u64
)q
->limits
.discard_granularity
,
308 if (copy_from_user(&range
, arg
, sizeof(range
)))
310 if (range
.start
> total_bytes
)
313 range
.len
= min(range
.len
, total_bytes
- range
.start
);
314 range
.minlen
= max(range
.minlen
, minlen
);
315 ret
= btrfs_trim_fs(root
, &range
);
319 if (copy_to_user(arg
, &range
, sizeof(range
)))
325 static noinline
int create_subvol(struct btrfs_root
*root
,
326 struct dentry
*dentry
,
327 char *name
, int namelen
,
330 struct btrfs_trans_handle
*trans
;
331 struct btrfs_key key
;
332 struct btrfs_root_item root_item
;
333 struct btrfs_inode_item
*inode_item
;
334 struct extent_buffer
*leaf
;
335 struct btrfs_root
*new_root
;
336 struct dentry
*parent
= dentry
->d_parent
;
341 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
344 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
348 dir
= parent
->d_inode
;
356 trans
= btrfs_start_transaction(root
, 6);
358 return PTR_ERR(trans
);
360 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
361 0, objectid
, NULL
, 0, 0, 0);
367 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
368 btrfs_set_header_bytenr(leaf
, leaf
->start
);
369 btrfs_set_header_generation(leaf
, trans
->transid
);
370 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
371 btrfs_set_header_owner(leaf
, objectid
);
373 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
374 (unsigned long)btrfs_header_fsid(leaf
),
376 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
377 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
379 btrfs_mark_buffer_dirty(leaf
);
381 inode_item
= &root_item
.inode
;
382 memset(inode_item
, 0, sizeof(*inode_item
));
383 inode_item
->generation
= cpu_to_le64(1);
384 inode_item
->size
= cpu_to_le64(3);
385 inode_item
->nlink
= cpu_to_le32(1);
386 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
387 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
390 root_item
.byte_limit
= 0;
391 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
393 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
394 btrfs_set_root_generation(&root_item
, trans
->transid
);
395 btrfs_set_root_level(&root_item
, 0);
396 btrfs_set_root_refs(&root_item
, 1);
397 btrfs_set_root_used(&root_item
, leaf
->len
);
398 btrfs_set_root_last_snapshot(&root_item
, 0);
400 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
401 root_item
.drop_level
= 0;
403 btrfs_tree_unlock(leaf
);
404 free_extent_buffer(leaf
);
407 btrfs_set_root_dirid(&root_item
, new_dirid
);
409 key
.objectid
= objectid
;
411 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
412 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
417 key
.offset
= (u64
)-1;
418 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
419 BUG_ON(IS_ERR(new_root
));
421 btrfs_record_root_in_trans(trans
, new_root
);
423 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
425 * insert the directory item
427 ret
= btrfs_set_inode_index(dir
, &index
);
430 ret
= btrfs_insert_dir_item(trans
, root
,
431 name
, namelen
, dir
, &key
,
432 BTRFS_FT_DIR
, index
);
436 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
437 ret
= btrfs_update_inode(trans
, root
, dir
);
440 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
441 objectid
, root
->root_key
.objectid
,
442 btrfs_ino(dir
), index
, name
, namelen
);
446 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
449 *async_transid
= trans
->transid
;
450 err
= btrfs_commit_transaction_async(trans
, root
, 1);
452 err
= btrfs_commit_transaction(trans
, root
);
459 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
460 char *name
, int namelen
, u64
*async_transid
,
464 struct btrfs_pending_snapshot
*pending_snapshot
;
465 struct btrfs_trans_handle
*trans
;
471 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
472 if (!pending_snapshot
)
475 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
476 pending_snapshot
->dentry
= dentry
;
477 pending_snapshot
->root
= root
;
478 pending_snapshot
->readonly
= readonly
;
480 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
482 ret
= PTR_ERR(trans
);
486 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
489 spin_lock(&root
->fs_info
->trans_lock
);
490 list_add(&pending_snapshot
->list
,
491 &trans
->transaction
->pending_snapshots
);
492 spin_unlock(&root
->fs_info
->trans_lock
);
494 *async_transid
= trans
->transid
;
495 ret
= btrfs_commit_transaction_async(trans
,
496 root
->fs_info
->extent_root
, 1);
498 ret
= btrfs_commit_transaction(trans
,
499 root
->fs_info
->extent_root
);
503 ret
= pending_snapshot
->error
;
507 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
511 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
513 ret
= PTR_ERR(inode
);
517 d_instantiate(dentry
, inode
);
520 kfree(pending_snapshot
);
524 /* copy of check_sticky in fs/namei.c()
525 * It's inline, so penalty for filesystems that don't use sticky bit is
528 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
530 uid_t fsuid
= current_fsuid();
532 if (!(dir
->i_mode
& S_ISVTX
))
534 if (inode
->i_uid
== fsuid
)
536 if (dir
->i_uid
== fsuid
)
538 return !capable(CAP_FOWNER
);
541 /* copy of may_delete in fs/namei.c()
542 * Check whether we can remove a link victim from directory dir, check
543 * whether the type of victim is right.
544 * 1. We can't do it if dir is read-only (done in permission())
545 * 2. We should have write and exec permissions on dir
546 * 3. We can't remove anything from append-only dir
547 * 4. We can't do anything with immutable dir (done in permission())
548 * 5. If the sticky bit on dir is set we should either
549 * a. be owner of dir, or
550 * b. be owner of victim, or
551 * c. have CAP_FOWNER capability
552 * 6. If the victim is append-only or immutable we can't do antyhing with
553 * links pointing to it.
554 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
555 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
556 * 9. We can't remove a root or mountpoint.
557 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
558 * nfs_async_unlink().
561 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
565 if (!victim
->d_inode
)
568 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
569 audit_inode_child(victim
, dir
);
571 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
576 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
577 IS_APPEND(victim
->d_inode
)||
578 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
581 if (!S_ISDIR(victim
->d_inode
->i_mode
))
585 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
589 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
594 /* copy of may_create in fs/namei.c() */
595 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
601 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
605 * Create a new subvolume below @parent. This is largely modeled after
606 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
607 * inside this filesystem so it's quite a bit simpler.
609 static noinline
int btrfs_mksubvol(struct path
*parent
,
610 char *name
, int namelen
,
611 struct btrfs_root
*snap_src
,
612 u64
*async_transid
, bool readonly
)
614 struct inode
*dir
= parent
->dentry
->d_inode
;
615 struct dentry
*dentry
;
618 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
620 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
621 error
= PTR_ERR(dentry
);
629 error
= mnt_want_write(parent
->mnt
);
633 error
= btrfs_may_create(dir
, dentry
);
637 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
639 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
643 error
= create_snapshot(snap_src
, dentry
,
644 name
, namelen
, async_transid
, readonly
);
646 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
647 name
, namelen
, async_transid
);
650 fsnotify_mkdir(dir
, dentry
);
652 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
654 mnt_drop_write(parent
->mnt
);
658 mutex_unlock(&dir
->i_mutex
);
663 * When we're defragging a range, we don't want to kick it off again
664 * if it is really just waiting for delalloc to send it down.
665 * If we find a nice big extent or delalloc range for the bytes in the
666 * file you want to defrag, we return 0 to let you know to skip this
669 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
671 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
672 struct extent_map
*em
= NULL
;
673 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
676 read_lock(&em_tree
->lock
);
677 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
678 read_unlock(&em_tree
->lock
);
681 end
= extent_map_end(em
);
683 if (end
- offset
> thresh
)
686 /* if we already have a nice delalloc here, just stop */
688 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
689 thresh
, EXTENT_DELALLOC
, 1);
696 * helper function to walk through a file and find extents
697 * newer than a specific transid, and smaller than thresh.
699 * This is used by the defragging code to find new and small
702 static int find_new_extents(struct btrfs_root
*root
,
703 struct inode
*inode
, u64 newer_than
,
704 u64
*off
, int thresh
)
706 struct btrfs_path
*path
;
707 struct btrfs_key min_key
;
708 struct btrfs_key max_key
;
709 struct extent_buffer
*leaf
;
710 struct btrfs_file_extent_item
*extent
;
713 u64 ino
= btrfs_ino(inode
);
715 path
= btrfs_alloc_path();
719 min_key
.objectid
= ino
;
720 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
721 min_key
.offset
= *off
;
723 max_key
.objectid
= ino
;
724 max_key
.type
= (u8
)-1;
725 max_key
.offset
= (u64
)-1;
727 path
->keep_locks
= 1;
730 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
731 path
, 0, newer_than
);
734 if (min_key
.objectid
!= ino
)
736 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
739 leaf
= path
->nodes
[0];
740 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
741 struct btrfs_file_extent_item
);
743 type
= btrfs_file_extent_type(leaf
, extent
);
744 if (type
== BTRFS_FILE_EXTENT_REG
&&
745 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
746 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
747 *off
= min_key
.offset
;
748 btrfs_free_path(path
);
752 if (min_key
.offset
== (u64
)-1)
756 btrfs_release_path(path
);
759 btrfs_free_path(path
);
763 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
764 int thresh
, u64
*last_len
, u64
*skip
,
767 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
768 struct extent_map
*em
= NULL
;
769 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
773 * make sure that once we start defragging an extent, we keep on
776 if (start
< *defrag_end
)
782 * hopefully we have this extent in the tree already, try without
783 * the full extent lock
785 read_lock(&em_tree
->lock
);
786 em
= lookup_extent_mapping(em_tree
, start
, len
);
787 read_unlock(&em_tree
->lock
);
790 /* get the big lock and read metadata off disk */
791 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
792 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
793 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
799 /* this will cover holes, and inline extents */
800 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
804 * we hit a real extent, if it is big don't bother defragging it again
806 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
810 * last_len ends up being a counter of how many bytes we've defragged.
811 * every time we choose not to defrag an extent, we reset *last_len
812 * so that the next tiny extent will force a defrag.
814 * The end result of this is that tiny extents before a single big
815 * extent will force at least part of that big extent to be defragged.
818 *defrag_end
= extent_map_end(em
);
821 *skip
= extent_map_end(em
);
830 * it doesn't do much good to defrag one or two pages
831 * at a time. This pulls in a nice chunk of pages
834 * It also makes sure the delalloc code has enough
835 * dirty data to avoid making new small extents as part
838 * It's a good idea to start RA on this range
839 * before calling this.
841 static int cluster_pages_for_defrag(struct inode
*inode
,
843 unsigned long start_index
,
846 unsigned long file_end
;
847 u64 isize
= i_size_read(inode
);
853 struct btrfs_ordered_extent
*ordered
;
854 struct extent_state
*cached_state
= NULL
;
855 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
859 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
861 mutex_lock(&inode
->i_mutex
);
862 ret
= btrfs_delalloc_reserve_space(inode
,
863 num_pages
<< PAGE_CACHE_SHIFT
);
864 mutex_unlock(&inode
->i_mutex
);
871 /* step one, lock all the pages */
872 for (i
= 0; i
< num_pages
; i
++) {
874 page
= find_or_create_page(inode
->i_mapping
,
875 start_index
+ i
, mask
);
879 if (!PageUptodate(page
)) {
880 btrfs_readpage(NULL
, page
);
882 if (!PageUptodate(page
)) {
884 page_cache_release(page
);
889 isize
= i_size_read(inode
);
890 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
891 if (!isize
|| page
->index
> file_end
||
892 page
->mapping
!= inode
->i_mapping
) {
893 /* whoops, we blew past eof, skip this page */
895 page_cache_release(page
);
904 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
908 * so now we have a nice long stream of locked
909 * and up to date pages, lets wait on them
911 for (i
= 0; i
< i_done
; i
++)
912 wait_on_page_writeback(pages
[i
]);
914 page_start
= page_offset(pages
[0]);
915 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
917 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
918 page_start
, page_end
- 1, 0, &cached_state
,
920 ordered
= btrfs_lookup_first_ordered_extent(inode
, page_end
- 1);
922 ordered
->file_offset
+ ordered
->len
> page_start
&&
923 ordered
->file_offset
< page_end
) {
924 btrfs_put_ordered_extent(ordered
);
925 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
926 page_start
, page_end
- 1,
927 &cached_state
, GFP_NOFS
);
928 for (i
= 0; i
< i_done
; i
++) {
929 unlock_page(pages
[i
]);
930 page_cache_release(pages
[i
]);
932 btrfs_wait_ordered_range(inode
, page_start
,
933 page_end
- page_start
);
937 btrfs_put_ordered_extent(ordered
);
939 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
940 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
941 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
944 if (i_done
!= num_pages
) {
945 spin_lock(&BTRFS_I(inode
)->lock
);
946 BTRFS_I(inode
)->outstanding_extents
++;
947 spin_unlock(&BTRFS_I(inode
)->lock
);
948 btrfs_delalloc_release_space(inode
,
949 (num_pages
- i_done
) << PAGE_CACHE_SHIFT
);
953 btrfs_set_extent_delalloc(inode
, page_start
, page_end
- 1,
956 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
957 page_start
, page_end
- 1, &cached_state
,
960 for (i
= 0; i
< i_done
; i
++) {
961 clear_page_dirty_for_io(pages
[i
]);
962 ClearPageChecked(pages
[i
]);
963 set_page_extent_mapped(pages
[i
]);
964 set_page_dirty(pages
[i
]);
965 unlock_page(pages
[i
]);
966 page_cache_release(pages
[i
]);
970 for (i
= 0; i
< i_done
; i
++) {
971 unlock_page(pages
[i
]);
972 page_cache_release(pages
[i
]);
974 btrfs_delalloc_release_space(inode
, num_pages
<< PAGE_CACHE_SHIFT
);
979 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
980 struct btrfs_ioctl_defrag_range_args
*range
,
981 u64 newer_than
, unsigned long max_to_defrag
)
983 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
984 struct btrfs_super_block
*disk_super
;
985 struct file_ra_state
*ra
= NULL
;
986 unsigned long last_index
;
987 u64 isize
= i_size_read(inode
);
992 u64 newer_off
= range
->start
;
994 unsigned long ra_index
= 0;
996 int defrag_count
= 0;
997 int compress_type
= BTRFS_COMPRESS_ZLIB
;
998 int extent_thresh
= range
->extent_thresh
;
999 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1000 int cluster
= max_cluster
;
1001 u64 new_align
= ~((u64
)128 * 1024 - 1);
1002 struct page
**pages
= NULL
;
1004 if (extent_thresh
== 0)
1005 extent_thresh
= 256 * 1024;
1007 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1008 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1010 if (range
->compress_type
)
1011 compress_type
= range
->compress_type
;
1018 * if we were not given a file, allocate a readahead
1022 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1025 file_ra_state_init(ra
, inode
->i_mapping
);
1030 pages
= kmalloc(sizeof(struct page
*) * max_cluster
,
1037 /* find the last page to defrag */
1038 if (range
->start
+ range
->len
> range
->start
) {
1039 last_index
= min_t(u64
, isize
- 1,
1040 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1042 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1046 ret
= find_new_extents(root
, inode
, newer_than
,
1047 &newer_off
, 64 * 1024);
1049 range
->start
= newer_off
;
1051 * we always align our defrag to help keep
1052 * the extents in the file evenly spaced
1054 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1058 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1061 max_to_defrag
= last_index
;
1064 * make writeback starts from i, so the defrag range can be
1065 * written sequentially.
1067 if (i
< inode
->i_mapping
->writeback_index
)
1068 inode
->i_mapping
->writeback_index
= i
;
1070 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1071 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1072 PAGE_CACHE_SHIFT
)) {
1074 * make sure we stop running if someone unmounts
1077 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1081 !should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1088 * the should_defrag function tells us how much to skip
1089 * bump our counter by the suggested amount
1091 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1092 i
= max(i
+ 1, next
);
1097 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1098 PAGE_CACHE_SHIFT
) - i
;
1099 cluster
= min(cluster
, max_cluster
);
1101 cluster
= max_cluster
;
1104 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1105 BTRFS_I(inode
)->force_compress
= compress_type
;
1107 if (i
+ cluster
> ra_index
) {
1108 ra_index
= max(i
, ra_index
);
1109 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1111 ra_index
+= max_cluster
;
1114 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1118 defrag_count
+= ret
;
1119 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, ret
);
1122 if (newer_off
== (u64
)-1)
1125 newer_off
= max(newer_off
+ 1,
1126 (u64
)i
<< PAGE_CACHE_SHIFT
);
1128 ret
= find_new_extents(root
, inode
,
1129 newer_than
, &newer_off
,
1132 range
->start
= newer_off
;
1133 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1140 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1148 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1149 filemap_flush(inode
->i_mapping
);
1151 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1152 /* the filemap_flush will queue IO into the worker threads, but
1153 * we have to make sure the IO is actually started and that
1154 * ordered extents get created before we return
1156 atomic_inc(&root
->fs_info
->async_submit_draining
);
1157 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1158 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1159 wait_event(root
->fs_info
->async_submit_wait
,
1160 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1161 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1163 atomic_dec(&root
->fs_info
->async_submit_draining
);
1165 mutex_lock(&inode
->i_mutex
);
1166 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1167 mutex_unlock(&inode
->i_mutex
);
1170 disk_super
= root
->fs_info
->super_copy
;
1171 features
= btrfs_super_incompat_flags(disk_super
);
1172 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1173 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
1174 btrfs_set_super_incompat_flags(disk_super
, features
);
1186 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
1192 struct btrfs_ioctl_vol_args
*vol_args
;
1193 struct btrfs_trans_handle
*trans
;
1194 struct btrfs_device
*device
= NULL
;
1196 char *devstr
= NULL
;
1200 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1203 if (!capable(CAP_SYS_ADMIN
))
1206 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1207 if (IS_ERR(vol_args
))
1208 return PTR_ERR(vol_args
);
1210 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1212 mutex_lock(&root
->fs_info
->volume_mutex
);
1213 sizestr
= vol_args
->name
;
1214 devstr
= strchr(sizestr
, ':');
1217 sizestr
= devstr
+ 1;
1219 devstr
= vol_args
->name
;
1220 devid
= simple_strtoull(devstr
, &end
, 10);
1221 printk(KERN_INFO
"btrfs: resizing devid %llu\n",
1222 (unsigned long long)devid
);
1224 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1226 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1227 (unsigned long long)devid
);
1231 if (!strcmp(sizestr
, "max"))
1232 new_size
= device
->bdev
->bd_inode
->i_size
;
1234 if (sizestr
[0] == '-') {
1237 } else if (sizestr
[0] == '+') {
1241 new_size
= memparse(sizestr
, NULL
);
1242 if (new_size
== 0) {
1248 old_size
= device
->total_bytes
;
1251 if (new_size
> old_size
) {
1255 new_size
= old_size
- new_size
;
1256 } else if (mod
> 0) {
1257 new_size
= old_size
+ new_size
;
1260 if (new_size
< 256 * 1024 * 1024) {
1264 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1269 do_div(new_size
, root
->sectorsize
);
1270 new_size
*= root
->sectorsize
;
1272 printk(KERN_INFO
"btrfs: new size for %s is %llu\n",
1273 device
->name
, (unsigned long long)new_size
);
1275 if (new_size
> old_size
) {
1276 trans
= btrfs_start_transaction(root
, 0);
1277 if (IS_ERR(trans
)) {
1278 ret
= PTR_ERR(trans
);
1281 ret
= btrfs_grow_device(trans
, device
, new_size
);
1282 btrfs_commit_transaction(trans
, root
);
1283 } else if (new_size
< old_size
) {
1284 ret
= btrfs_shrink_device(device
, new_size
);
1288 mutex_unlock(&root
->fs_info
->volume_mutex
);
1293 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1300 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1301 struct file
*src_file
;
1305 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1308 namelen
= strlen(name
);
1309 if (strchr(name
, '/')) {
1315 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1316 NULL
, transid
, readonly
);
1318 struct inode
*src_inode
;
1319 src_file
= fget(fd
);
1325 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1326 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1327 printk(KERN_INFO
"btrfs: Snapshot src from "
1330 } else if (!inode_owner_or_capable(src_inode
)) {
1332 * Subvolume creation is not restricted, but snapshots
1333 * are limited to own subvolumes only
1337 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1338 BTRFS_I(src_inode
)->root
,
1347 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1348 void __user
*arg
, int subvol
)
1350 struct btrfs_ioctl_vol_args
*vol_args
;
1353 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1354 if (IS_ERR(vol_args
))
1355 return PTR_ERR(vol_args
);
1356 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1358 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1359 vol_args
->fd
, subvol
,
1366 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1367 void __user
*arg
, int subvol
)
1369 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1373 bool readonly
= false;
1375 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1376 if (IS_ERR(vol_args
))
1377 return PTR_ERR(vol_args
);
1378 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1380 if (vol_args
->flags
&
1381 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1386 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1388 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1391 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1392 vol_args
->fd
, subvol
,
1395 if (ret
== 0 && ptr
&&
1397 offsetof(struct btrfs_ioctl_vol_args_v2
,
1398 transid
), ptr
, sizeof(*ptr
)))
1405 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1408 struct inode
*inode
= fdentry(file
)->d_inode
;
1409 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1413 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1416 down_read(&root
->fs_info
->subvol_sem
);
1417 if (btrfs_root_readonly(root
))
1418 flags
|= BTRFS_SUBVOL_RDONLY
;
1419 up_read(&root
->fs_info
->subvol_sem
);
1421 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1427 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1430 struct inode
*inode
= fdentry(file
)->d_inode
;
1431 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1432 struct btrfs_trans_handle
*trans
;
1437 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1440 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1443 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1446 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1449 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1452 if (!inode_owner_or_capable(inode
))
1455 down_write(&root
->fs_info
->subvol_sem
);
1458 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1461 root_flags
= btrfs_root_flags(&root
->root_item
);
1462 if (flags
& BTRFS_SUBVOL_RDONLY
)
1463 btrfs_set_root_flags(&root
->root_item
,
1464 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1466 btrfs_set_root_flags(&root
->root_item
,
1467 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1469 trans
= btrfs_start_transaction(root
, 1);
1470 if (IS_ERR(trans
)) {
1471 ret
= PTR_ERR(trans
);
1475 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1476 &root
->root_key
, &root
->root_item
);
1478 btrfs_commit_transaction(trans
, root
);
1481 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1483 up_write(&root
->fs_info
->subvol_sem
);
1488 * helper to check if the subvolume references other subvolumes
1490 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1492 struct btrfs_path
*path
;
1493 struct btrfs_key key
;
1496 path
= btrfs_alloc_path();
1500 key
.objectid
= root
->root_key
.objectid
;
1501 key
.type
= BTRFS_ROOT_REF_KEY
;
1502 key
.offset
= (u64
)-1;
1504 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1511 if (path
->slots
[0] > 0) {
1513 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1514 if (key
.objectid
== root
->root_key
.objectid
&&
1515 key
.type
== BTRFS_ROOT_REF_KEY
)
1519 btrfs_free_path(path
);
1523 static noinline
int key_in_sk(struct btrfs_key
*key
,
1524 struct btrfs_ioctl_search_key
*sk
)
1526 struct btrfs_key test
;
1529 test
.objectid
= sk
->min_objectid
;
1530 test
.type
= sk
->min_type
;
1531 test
.offset
= sk
->min_offset
;
1533 ret
= btrfs_comp_cpu_keys(key
, &test
);
1537 test
.objectid
= sk
->max_objectid
;
1538 test
.type
= sk
->max_type
;
1539 test
.offset
= sk
->max_offset
;
1541 ret
= btrfs_comp_cpu_keys(key
, &test
);
1547 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1548 struct btrfs_path
*path
,
1549 struct btrfs_key
*key
,
1550 struct btrfs_ioctl_search_key
*sk
,
1552 unsigned long *sk_offset
,
1556 struct extent_buffer
*leaf
;
1557 struct btrfs_ioctl_search_header sh
;
1558 unsigned long item_off
;
1559 unsigned long item_len
;
1565 leaf
= path
->nodes
[0];
1566 slot
= path
->slots
[0];
1567 nritems
= btrfs_header_nritems(leaf
);
1569 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1573 found_transid
= btrfs_header_generation(leaf
);
1575 for (i
= slot
; i
< nritems
; i
++) {
1576 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1577 item_len
= btrfs_item_size_nr(leaf
, i
);
1579 btrfs_item_key_to_cpu(leaf
, key
, i
);
1580 if (!key_in_sk(key
, sk
))
1583 if (sizeof(sh
) + item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1586 if (sizeof(sh
) + item_len
+ *sk_offset
>
1587 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1592 sh
.objectid
= key
->objectid
;
1593 sh
.offset
= key
->offset
;
1594 sh
.type
= key
->type
;
1596 sh
.transid
= found_transid
;
1598 /* copy search result header */
1599 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1600 *sk_offset
+= sizeof(sh
);
1603 char *p
= buf
+ *sk_offset
;
1605 read_extent_buffer(leaf
, p
,
1606 item_off
, item_len
);
1607 *sk_offset
+= item_len
;
1611 if (*num_found
>= sk
->nr_items
)
1616 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1618 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1621 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1631 static noinline
int search_ioctl(struct inode
*inode
,
1632 struct btrfs_ioctl_search_args
*args
)
1634 struct btrfs_root
*root
;
1635 struct btrfs_key key
;
1636 struct btrfs_key max_key
;
1637 struct btrfs_path
*path
;
1638 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1639 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1642 unsigned long sk_offset
= 0;
1644 path
= btrfs_alloc_path();
1648 if (sk
->tree_id
== 0) {
1649 /* search the root of the inode that was passed */
1650 root
= BTRFS_I(inode
)->root
;
1652 key
.objectid
= sk
->tree_id
;
1653 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1654 key
.offset
= (u64
)-1;
1655 root
= btrfs_read_fs_root_no_name(info
, &key
);
1657 printk(KERN_ERR
"could not find root %llu\n",
1659 btrfs_free_path(path
);
1664 key
.objectid
= sk
->min_objectid
;
1665 key
.type
= sk
->min_type
;
1666 key
.offset
= sk
->min_offset
;
1668 max_key
.objectid
= sk
->max_objectid
;
1669 max_key
.type
= sk
->max_type
;
1670 max_key
.offset
= sk
->max_offset
;
1672 path
->keep_locks
= 1;
1675 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1682 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1683 &sk_offset
, &num_found
);
1684 btrfs_release_path(path
);
1685 if (ret
|| num_found
>= sk
->nr_items
)
1691 sk
->nr_items
= num_found
;
1692 btrfs_free_path(path
);
1696 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1699 struct btrfs_ioctl_search_args
*args
;
1700 struct inode
*inode
;
1703 if (!capable(CAP_SYS_ADMIN
))
1706 args
= memdup_user(argp
, sizeof(*args
));
1708 return PTR_ERR(args
);
1710 inode
= fdentry(file
)->d_inode
;
1711 ret
= search_ioctl(inode
, args
);
1712 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1719 * Search INODE_REFs to identify path name of 'dirid' directory
1720 * in a 'tree_id' tree. and sets path name to 'name'.
1722 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1723 u64 tree_id
, u64 dirid
, char *name
)
1725 struct btrfs_root
*root
;
1726 struct btrfs_key key
;
1732 struct btrfs_inode_ref
*iref
;
1733 struct extent_buffer
*l
;
1734 struct btrfs_path
*path
;
1736 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1741 path
= btrfs_alloc_path();
1745 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1747 key
.objectid
= tree_id
;
1748 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1749 key
.offset
= (u64
)-1;
1750 root
= btrfs_read_fs_root_no_name(info
, &key
);
1752 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1757 key
.objectid
= dirid
;
1758 key
.type
= BTRFS_INODE_REF_KEY
;
1759 key
.offset
= (u64
)-1;
1762 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1767 slot
= path
->slots
[0];
1768 if (ret
> 0 && slot
> 0)
1770 btrfs_item_key_to_cpu(l
, &key
, slot
);
1772 if (ret
> 0 && (key
.objectid
!= dirid
||
1773 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1778 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1779 len
= btrfs_inode_ref_name_len(l
, iref
);
1781 total_len
+= len
+ 1;
1786 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1788 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1791 btrfs_release_path(path
);
1792 key
.objectid
= key
.offset
;
1793 key
.offset
= (u64
)-1;
1794 dirid
= key
.objectid
;
1798 memmove(name
, ptr
, total_len
);
1799 name
[total_len
]='\0';
1802 btrfs_free_path(path
);
1806 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1809 struct btrfs_ioctl_ino_lookup_args
*args
;
1810 struct inode
*inode
;
1813 if (!capable(CAP_SYS_ADMIN
))
1816 args
= memdup_user(argp
, sizeof(*args
));
1818 return PTR_ERR(args
);
1820 inode
= fdentry(file
)->d_inode
;
1822 if (args
->treeid
== 0)
1823 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1825 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1826 args
->treeid
, args
->objectid
,
1829 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1836 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1839 struct dentry
*parent
= fdentry(file
);
1840 struct dentry
*dentry
;
1841 struct inode
*dir
= parent
->d_inode
;
1842 struct inode
*inode
;
1843 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1844 struct btrfs_root
*dest
= NULL
;
1845 struct btrfs_ioctl_vol_args
*vol_args
;
1846 struct btrfs_trans_handle
*trans
;
1851 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1852 if (IS_ERR(vol_args
))
1853 return PTR_ERR(vol_args
);
1855 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1856 namelen
= strlen(vol_args
->name
);
1857 if (strchr(vol_args
->name
, '/') ||
1858 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1863 err
= mnt_want_write(file
->f_path
.mnt
);
1867 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1868 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1869 if (IS_ERR(dentry
)) {
1870 err
= PTR_ERR(dentry
);
1871 goto out_unlock_dir
;
1874 if (!dentry
->d_inode
) {
1879 inode
= dentry
->d_inode
;
1880 dest
= BTRFS_I(inode
)->root
;
1881 if (!capable(CAP_SYS_ADMIN
)){
1883 * Regular user. Only allow this with a special mount
1884 * option, when the user has write+exec access to the
1885 * subvol root, and when rmdir(2) would have been
1888 * Note that this is _not_ check that the subvol is
1889 * empty or doesn't contain data that we wouldn't
1890 * otherwise be able to delete.
1892 * Users who want to delete empty subvols should try
1896 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1900 * Do not allow deletion if the parent dir is the same
1901 * as the dir to be deleted. That means the ioctl
1902 * must be called on the dentry referencing the root
1903 * of the subvol, not a random directory contained
1910 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1914 /* check if subvolume may be deleted by a non-root user */
1915 err
= btrfs_may_delete(dir
, dentry
, 1);
1920 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1925 mutex_lock(&inode
->i_mutex
);
1926 err
= d_invalidate(dentry
);
1930 down_write(&root
->fs_info
->subvol_sem
);
1932 err
= may_destroy_subvol(dest
);
1936 trans
= btrfs_start_transaction(root
, 0);
1937 if (IS_ERR(trans
)) {
1938 err
= PTR_ERR(trans
);
1941 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1943 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1944 dest
->root_key
.objectid
,
1945 dentry
->d_name
.name
,
1946 dentry
->d_name
.len
);
1949 btrfs_record_root_in_trans(trans
, dest
);
1951 memset(&dest
->root_item
.drop_progress
, 0,
1952 sizeof(dest
->root_item
.drop_progress
));
1953 dest
->root_item
.drop_level
= 0;
1954 btrfs_set_root_refs(&dest
->root_item
, 0);
1956 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1957 ret
= btrfs_insert_orphan_item(trans
,
1958 root
->fs_info
->tree_root
,
1959 dest
->root_key
.objectid
);
1963 ret
= btrfs_end_transaction(trans
, root
);
1965 inode
->i_flags
|= S_DEAD
;
1967 up_write(&root
->fs_info
->subvol_sem
);
1969 mutex_unlock(&inode
->i_mutex
);
1971 shrink_dcache_sb(root
->fs_info
->sb
);
1972 btrfs_invalidate_inodes(dest
);
1978 mutex_unlock(&dir
->i_mutex
);
1979 mnt_drop_write(file
->f_path
.mnt
);
1985 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1987 struct inode
*inode
= fdentry(file
)->d_inode
;
1988 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1989 struct btrfs_ioctl_defrag_range_args
*range
;
1992 if (btrfs_root_readonly(root
))
1995 ret
= mnt_want_write(file
->f_path
.mnt
);
1999 switch (inode
->i_mode
& S_IFMT
) {
2001 if (!capable(CAP_SYS_ADMIN
)) {
2005 ret
= btrfs_defrag_root(root
, 0);
2008 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2011 if (!(file
->f_mode
& FMODE_WRITE
)) {
2016 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2023 if (copy_from_user(range
, argp
,
2029 /* compression requires us to start the IO */
2030 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2031 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2032 range
->extent_thresh
= (u32
)-1;
2035 /* the rest are all set to zero by kzalloc */
2036 range
->len
= (u64
)-1;
2038 ret
= btrfs_defrag_file(fdentry(file
)->d_inode
, file
,
2048 mnt_drop_write(file
->f_path
.mnt
);
2052 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2054 struct btrfs_ioctl_vol_args
*vol_args
;
2057 if (!capable(CAP_SYS_ADMIN
))
2060 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2061 if (IS_ERR(vol_args
))
2062 return PTR_ERR(vol_args
);
2064 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2065 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2071 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
2073 struct btrfs_ioctl_vol_args
*vol_args
;
2076 if (!capable(CAP_SYS_ADMIN
))
2079 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2082 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2083 if (IS_ERR(vol_args
))
2084 return PTR_ERR(vol_args
);
2086 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2087 ret
= btrfs_rm_device(root
, vol_args
->name
);
2093 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2095 struct btrfs_ioctl_fs_info_args
*fi_args
;
2096 struct btrfs_device
*device
;
2097 struct btrfs_device
*next
;
2098 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2101 if (!capable(CAP_SYS_ADMIN
))
2104 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2108 fi_args
->num_devices
= fs_devices
->num_devices
;
2109 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2111 mutex_lock(&fs_devices
->device_list_mutex
);
2112 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2113 if (device
->devid
> fi_args
->max_id
)
2114 fi_args
->max_id
= device
->devid
;
2116 mutex_unlock(&fs_devices
->device_list_mutex
);
2118 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2125 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2127 struct btrfs_ioctl_dev_info_args
*di_args
;
2128 struct btrfs_device
*dev
;
2129 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2131 char *s_uuid
= NULL
;
2132 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
2134 if (!capable(CAP_SYS_ADMIN
))
2137 di_args
= memdup_user(arg
, sizeof(*di_args
));
2138 if (IS_ERR(di_args
))
2139 return PTR_ERR(di_args
);
2141 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
2142 s_uuid
= di_args
->uuid
;
2144 mutex_lock(&fs_devices
->device_list_mutex
);
2145 dev
= btrfs_find_device(root
, di_args
->devid
, s_uuid
, NULL
);
2146 mutex_unlock(&fs_devices
->device_list_mutex
);
2153 di_args
->devid
= dev
->devid
;
2154 di_args
->bytes_used
= dev
->bytes_used
;
2155 di_args
->total_bytes
= dev
->total_bytes
;
2156 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2157 strncpy(di_args
->path
, dev
->name
, sizeof(di_args
->path
));
2160 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2167 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
2168 u64 off
, u64 olen
, u64 destoff
)
2170 struct inode
*inode
= fdentry(file
)->d_inode
;
2171 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2172 struct file
*src_file
;
2174 struct btrfs_trans_handle
*trans
;
2175 struct btrfs_path
*path
;
2176 struct extent_buffer
*leaf
;
2178 struct btrfs_key key
;
2183 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
2188 * - split compressed inline extents. annoying: we need to
2189 * decompress into destination's address_space (the file offset
2190 * may change, so source mapping won't do), then recompress (or
2191 * otherwise reinsert) a subrange.
2192 * - allow ranges within the same file to be cloned (provided
2193 * they don't overlap)?
2196 /* the destination must be opened for writing */
2197 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
2200 if (btrfs_root_readonly(root
))
2203 ret
= mnt_want_write(file
->f_path
.mnt
);
2207 src_file
= fget(srcfd
);
2210 goto out_drop_write
;
2213 src
= src_file
->f_dentry
->d_inode
;
2219 /* the src must be open for reading */
2220 if (!(src_file
->f_mode
& FMODE_READ
))
2223 /* don't make the dst file partly checksummed */
2224 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2225 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
2229 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
2233 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
2237 buf
= vmalloc(btrfs_level_size(root
, 0));
2241 path
= btrfs_alloc_path();
2249 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
2250 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
2252 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
2253 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2256 /* determine range to clone */
2258 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
2261 olen
= len
= src
->i_size
- off
;
2262 /* if we extend to eof, continue to block boundary */
2263 if (off
+ len
== src
->i_size
)
2264 len
= ALIGN(src
->i_size
, bs
) - off
;
2266 /* verify the end result is block aligned */
2267 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
2268 !IS_ALIGNED(destoff
, bs
))
2271 if (destoff
> inode
->i_size
) {
2272 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
2277 /* truncate page cache pages from target inode range */
2278 truncate_inode_pages_range(&inode
->i_data
, destoff
,
2279 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
2281 /* do any pending delalloc/csum calc on src, one way or
2282 another, and lock file content */
2284 struct btrfs_ordered_extent
*ordered
;
2285 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2286 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
2288 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
2289 EXTENT_DELALLOC
, 0, NULL
))
2291 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2293 btrfs_put_ordered_extent(ordered
);
2294 btrfs_wait_ordered_range(src
, off
, len
);
2298 key
.objectid
= btrfs_ino(src
);
2299 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2304 * note the key will change type as we walk through the
2307 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2311 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2312 if (path
->slots
[0] >= nritems
) {
2313 ret
= btrfs_next_leaf(root
, path
);
2318 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2320 leaf
= path
->nodes
[0];
2321 slot
= path
->slots
[0];
2323 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2324 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2325 key
.objectid
!= btrfs_ino(src
))
2328 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2329 struct btrfs_file_extent_item
*extent
;
2332 struct btrfs_key new_key
;
2333 u64 disko
= 0, diskl
= 0;
2334 u64 datao
= 0, datal
= 0;
2338 size
= btrfs_item_size_nr(leaf
, slot
);
2339 read_extent_buffer(leaf
, buf
,
2340 btrfs_item_ptr_offset(leaf
, slot
),
2343 extent
= btrfs_item_ptr(leaf
, slot
,
2344 struct btrfs_file_extent_item
);
2345 comp
= btrfs_file_extent_compression(leaf
, extent
);
2346 type
= btrfs_file_extent_type(leaf
, extent
);
2347 if (type
== BTRFS_FILE_EXTENT_REG
||
2348 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2349 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2351 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2353 datao
= btrfs_file_extent_offset(leaf
, extent
);
2354 datal
= btrfs_file_extent_num_bytes(leaf
,
2356 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2357 /* take upper bound, may be compressed */
2358 datal
= btrfs_file_extent_ram_bytes(leaf
,
2361 btrfs_release_path(path
);
2363 if (key
.offset
+ datal
<= off
||
2364 key
.offset
>= off
+len
)
2367 memcpy(&new_key
, &key
, sizeof(new_key
));
2368 new_key
.objectid
= btrfs_ino(inode
);
2369 if (off
<= key
.offset
)
2370 new_key
.offset
= key
.offset
+ destoff
- off
;
2372 new_key
.offset
= destoff
;
2375 * 1 - adjusting old extent (we may have to split it)
2376 * 1 - add new extent
2379 trans
= btrfs_start_transaction(root
, 3);
2380 if (IS_ERR(trans
)) {
2381 ret
= PTR_ERR(trans
);
2385 if (type
== BTRFS_FILE_EXTENT_REG
||
2386 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2388 * a | --- range to clone ---| b
2389 * | ------------- extent ------------- |
2392 /* substract range b */
2393 if (key
.offset
+ datal
> off
+ len
)
2394 datal
= off
+ len
- key
.offset
;
2396 /* substract range a */
2397 if (off
> key
.offset
) {
2398 datao
+= off
- key
.offset
;
2399 datal
-= off
- key
.offset
;
2402 ret
= btrfs_drop_extents(trans
, inode
,
2404 new_key
.offset
+ datal
,
2408 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2412 leaf
= path
->nodes
[0];
2413 slot
= path
->slots
[0];
2414 write_extent_buffer(leaf
, buf
,
2415 btrfs_item_ptr_offset(leaf
, slot
),
2418 extent
= btrfs_item_ptr(leaf
, slot
,
2419 struct btrfs_file_extent_item
);
2421 /* disko == 0 means it's a hole */
2425 btrfs_set_file_extent_offset(leaf
, extent
,
2427 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2430 inode_add_bytes(inode
, datal
);
2431 ret
= btrfs_inc_extent_ref(trans
, root
,
2433 root
->root_key
.objectid
,
2435 new_key
.offset
- datao
);
2438 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2441 if (off
> key
.offset
) {
2442 skip
= off
- key
.offset
;
2443 new_key
.offset
+= skip
;
2446 if (key
.offset
+ datal
> off
+len
)
2447 trim
= key
.offset
+ datal
- (off
+len
);
2449 if (comp
&& (skip
|| trim
)) {
2451 btrfs_end_transaction(trans
, root
);
2454 size
-= skip
+ trim
;
2455 datal
-= skip
+ trim
;
2457 ret
= btrfs_drop_extents(trans
, inode
,
2459 new_key
.offset
+ datal
,
2463 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2469 btrfs_file_extent_calc_inline_size(0);
2470 memmove(buf
+start
, buf
+start
+skip
,
2474 leaf
= path
->nodes
[0];
2475 slot
= path
->slots
[0];
2476 write_extent_buffer(leaf
, buf
,
2477 btrfs_item_ptr_offset(leaf
, slot
),
2479 inode_add_bytes(inode
, datal
);
2482 btrfs_mark_buffer_dirty(leaf
);
2483 btrfs_release_path(path
);
2485 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2488 * we round up to the block size at eof when
2489 * determining which extents to clone above,
2490 * but shouldn't round up the file size
2492 endoff
= new_key
.offset
+ datal
;
2493 if (endoff
> destoff
+olen
)
2494 endoff
= destoff
+olen
;
2495 if (endoff
> inode
->i_size
)
2496 btrfs_i_size_write(inode
, endoff
);
2498 ret
= btrfs_update_inode(trans
, root
, inode
);
2500 btrfs_end_transaction(trans
, root
);
2503 btrfs_release_path(path
);
2508 btrfs_release_path(path
);
2509 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2511 mutex_unlock(&src
->i_mutex
);
2512 mutex_unlock(&inode
->i_mutex
);
2514 btrfs_free_path(path
);
2518 mnt_drop_write(file
->f_path
.mnt
);
2522 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2524 struct btrfs_ioctl_clone_range_args args
;
2526 if (copy_from_user(&args
, argp
, sizeof(args
)))
2528 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2529 args
.src_length
, args
.dest_offset
);
2533 * there are many ways the trans_start and trans_end ioctls can lead
2534 * to deadlocks. They should only be used by applications that
2535 * basically own the machine, and have a very in depth understanding
2536 * of all the possible deadlocks and enospc problems.
2538 static long btrfs_ioctl_trans_start(struct file
*file
)
2540 struct inode
*inode
= fdentry(file
)->d_inode
;
2541 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2542 struct btrfs_trans_handle
*trans
;
2546 if (!capable(CAP_SYS_ADMIN
))
2550 if (file
->private_data
)
2554 if (btrfs_root_readonly(root
))
2557 ret
= mnt_want_write(file
->f_path
.mnt
);
2561 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
2564 trans
= btrfs_start_ioctl_transaction(root
);
2568 file
->private_data
= trans
;
2572 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2573 mnt_drop_write(file
->f_path
.mnt
);
2578 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2580 struct inode
*inode
= fdentry(file
)->d_inode
;
2581 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2582 struct btrfs_root
*new_root
;
2583 struct btrfs_dir_item
*di
;
2584 struct btrfs_trans_handle
*trans
;
2585 struct btrfs_path
*path
;
2586 struct btrfs_key location
;
2587 struct btrfs_disk_key disk_key
;
2588 struct btrfs_super_block
*disk_super
;
2593 if (!capable(CAP_SYS_ADMIN
))
2596 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2600 objectid
= root
->root_key
.objectid
;
2602 location
.objectid
= objectid
;
2603 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2604 location
.offset
= (u64
)-1;
2606 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2607 if (IS_ERR(new_root
))
2608 return PTR_ERR(new_root
);
2610 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2613 path
= btrfs_alloc_path();
2616 path
->leave_spinning
= 1;
2618 trans
= btrfs_start_transaction(root
, 1);
2619 if (IS_ERR(trans
)) {
2620 btrfs_free_path(path
);
2621 return PTR_ERR(trans
);
2624 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
2625 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2626 dir_id
, "default", 7, 1);
2627 if (IS_ERR_OR_NULL(di
)) {
2628 btrfs_free_path(path
);
2629 btrfs_end_transaction(trans
, root
);
2630 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2631 "this isn't going to work\n");
2635 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2636 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2637 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2638 btrfs_free_path(path
);
2640 disk_super
= root
->fs_info
->super_copy
;
2641 features
= btrfs_super_incompat_flags(disk_super
);
2642 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2643 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2644 btrfs_set_super_incompat_flags(disk_super
, features
);
2646 btrfs_end_transaction(trans
, root
);
2651 static void get_block_group_info(struct list_head
*groups_list
,
2652 struct btrfs_ioctl_space_info
*space
)
2654 struct btrfs_block_group_cache
*block_group
;
2656 space
->total_bytes
= 0;
2657 space
->used_bytes
= 0;
2659 list_for_each_entry(block_group
, groups_list
, list
) {
2660 space
->flags
= block_group
->flags
;
2661 space
->total_bytes
+= block_group
->key
.offset
;
2662 space
->used_bytes
+=
2663 btrfs_block_group_used(&block_group
->item
);
2667 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2669 struct btrfs_ioctl_space_args space_args
;
2670 struct btrfs_ioctl_space_info space
;
2671 struct btrfs_ioctl_space_info
*dest
;
2672 struct btrfs_ioctl_space_info
*dest_orig
;
2673 struct btrfs_ioctl_space_info __user
*user_dest
;
2674 struct btrfs_space_info
*info
;
2675 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2676 BTRFS_BLOCK_GROUP_SYSTEM
,
2677 BTRFS_BLOCK_GROUP_METADATA
,
2678 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2685 if (copy_from_user(&space_args
,
2686 (struct btrfs_ioctl_space_args __user
*)arg
,
2687 sizeof(space_args
)))
2690 for (i
= 0; i
< num_types
; i
++) {
2691 struct btrfs_space_info
*tmp
;
2695 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2697 if (tmp
->flags
== types
[i
]) {
2707 down_read(&info
->groups_sem
);
2708 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2709 if (!list_empty(&info
->block_groups
[c
]))
2712 up_read(&info
->groups_sem
);
2715 /* space_slots == 0 means they are asking for a count */
2716 if (space_args
.space_slots
== 0) {
2717 space_args
.total_spaces
= slot_count
;
2721 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2723 alloc_size
= sizeof(*dest
) * slot_count
;
2725 /* we generally have at most 6 or so space infos, one for each raid
2726 * level. So, a whole page should be more than enough for everyone
2728 if (alloc_size
> PAGE_CACHE_SIZE
)
2731 space_args
.total_spaces
= 0;
2732 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2737 /* now we have a buffer to copy into */
2738 for (i
= 0; i
< num_types
; i
++) {
2739 struct btrfs_space_info
*tmp
;
2746 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2748 if (tmp
->flags
== types
[i
]) {
2757 down_read(&info
->groups_sem
);
2758 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2759 if (!list_empty(&info
->block_groups
[c
])) {
2760 get_block_group_info(&info
->block_groups
[c
],
2762 memcpy(dest
, &space
, sizeof(space
));
2764 space_args
.total_spaces
++;
2770 up_read(&info
->groups_sem
);
2773 user_dest
= (struct btrfs_ioctl_space_info
*)
2774 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2776 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2781 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2788 * there are many ways the trans_start and trans_end ioctls can lead
2789 * to deadlocks. They should only be used by applications that
2790 * basically own the machine, and have a very in depth understanding
2791 * of all the possible deadlocks and enospc problems.
2793 long btrfs_ioctl_trans_end(struct file
*file
)
2795 struct inode
*inode
= fdentry(file
)->d_inode
;
2796 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2797 struct btrfs_trans_handle
*trans
;
2799 trans
= file
->private_data
;
2802 file
->private_data
= NULL
;
2804 btrfs_end_transaction(trans
, root
);
2806 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2808 mnt_drop_write(file
->f_path
.mnt
);
2812 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2814 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2815 struct btrfs_trans_handle
*trans
;
2819 trans
= btrfs_start_transaction(root
, 0);
2821 return PTR_ERR(trans
);
2822 transid
= trans
->transid
;
2823 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2825 btrfs_end_transaction(trans
, root
);
2830 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2835 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2837 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2841 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2844 transid
= 0; /* current trans */
2846 return btrfs_wait_for_commit(root
, transid
);
2849 static long btrfs_ioctl_scrub(struct btrfs_root
*root
, void __user
*arg
)
2852 struct btrfs_ioctl_scrub_args
*sa
;
2854 if (!capable(CAP_SYS_ADMIN
))
2857 sa
= memdup_user(arg
, sizeof(*sa
));
2861 ret
= btrfs_scrub_dev(root
, sa
->devid
, sa
->start
, sa
->end
,
2862 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
);
2864 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2871 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
2873 if (!capable(CAP_SYS_ADMIN
))
2876 return btrfs_scrub_cancel(root
);
2879 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
2882 struct btrfs_ioctl_scrub_args
*sa
;
2885 if (!capable(CAP_SYS_ADMIN
))
2888 sa
= memdup_user(arg
, sizeof(*sa
));
2892 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
2894 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2901 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
2907 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
2908 struct inode_fs_paths
*ipath
= NULL
;
2909 struct btrfs_path
*path
;
2911 if (!capable(CAP_SYS_ADMIN
))
2914 path
= btrfs_alloc_path();
2920 ipa
= memdup_user(arg
, sizeof(*ipa
));
2927 size
= min_t(u32
, ipa
->size
, 4096);
2928 ipath
= init_ipath(size
, root
, path
);
2929 if (IS_ERR(ipath
)) {
2930 ret
= PTR_ERR(ipath
);
2935 ret
= paths_from_inode(ipa
->inum
, ipath
);
2939 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
2940 rel_ptr
= ipath
->fspath
->val
[i
] -
2941 (u64
)(unsigned long)ipath
->fspath
->val
;
2942 ipath
->fspath
->val
[i
] = rel_ptr
;
2945 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
2946 (void *)(unsigned long)ipath
->fspath
, size
);
2953 btrfs_free_path(path
);
2960 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
2962 struct btrfs_data_container
*inodes
= ctx
;
2963 const size_t c
= 3 * sizeof(u64
);
2965 if (inodes
->bytes_left
>= c
) {
2966 inodes
->bytes_left
-= c
;
2967 inodes
->val
[inodes
->elem_cnt
] = inum
;
2968 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
2969 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
2970 inodes
->elem_cnt
+= 3;
2972 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
2973 inodes
->bytes_left
= 0;
2974 inodes
->elem_missed
+= 3;
2980 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
2986 struct btrfs_ioctl_logical_ino_args
*loi
;
2987 struct btrfs_data_container
*inodes
= NULL
;
2988 struct btrfs_path
*path
= NULL
;
2989 struct btrfs_key key
;
2991 if (!capable(CAP_SYS_ADMIN
))
2994 loi
= memdup_user(arg
, sizeof(*loi
));
3001 path
= btrfs_alloc_path();
3007 size
= min_t(u32
, loi
->size
, 4096);
3008 inodes
= init_data_container(size
);
3009 if (IS_ERR(inodes
)) {
3010 ret
= PTR_ERR(inodes
);
3015 ret
= extent_from_logical(root
->fs_info
, loi
->logical
, path
, &key
);
3017 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
3022 extent_offset
= loi
->logical
- key
.objectid
;
3023 ret
= iterate_extent_inodes(root
->fs_info
, path
, key
.objectid
,
3024 extent_offset
, build_ino_list
, inodes
);
3029 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3030 (void *)(unsigned long)inodes
, size
);
3035 btrfs_free_path(path
);
3042 long btrfs_ioctl(struct file
*file
, unsigned int
3043 cmd
, unsigned long arg
)
3045 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3046 void __user
*argp
= (void __user
*)arg
;
3049 case FS_IOC_GETFLAGS
:
3050 return btrfs_ioctl_getflags(file
, argp
);
3051 case FS_IOC_SETFLAGS
:
3052 return btrfs_ioctl_setflags(file
, argp
);
3053 case FS_IOC_GETVERSION
:
3054 return btrfs_ioctl_getversion(file
, argp
);
3056 return btrfs_ioctl_fitrim(file
, argp
);
3057 case BTRFS_IOC_SNAP_CREATE
:
3058 return btrfs_ioctl_snap_create(file
, argp
, 0);
3059 case BTRFS_IOC_SNAP_CREATE_V2
:
3060 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
3061 case BTRFS_IOC_SUBVOL_CREATE
:
3062 return btrfs_ioctl_snap_create(file
, argp
, 1);
3063 case BTRFS_IOC_SNAP_DESTROY
:
3064 return btrfs_ioctl_snap_destroy(file
, argp
);
3065 case BTRFS_IOC_SUBVOL_GETFLAGS
:
3066 return btrfs_ioctl_subvol_getflags(file
, argp
);
3067 case BTRFS_IOC_SUBVOL_SETFLAGS
:
3068 return btrfs_ioctl_subvol_setflags(file
, argp
);
3069 case BTRFS_IOC_DEFAULT_SUBVOL
:
3070 return btrfs_ioctl_default_subvol(file
, argp
);
3071 case BTRFS_IOC_DEFRAG
:
3072 return btrfs_ioctl_defrag(file
, NULL
);
3073 case BTRFS_IOC_DEFRAG_RANGE
:
3074 return btrfs_ioctl_defrag(file
, argp
);
3075 case BTRFS_IOC_RESIZE
:
3076 return btrfs_ioctl_resize(root
, argp
);
3077 case BTRFS_IOC_ADD_DEV
:
3078 return btrfs_ioctl_add_dev(root
, argp
);
3079 case BTRFS_IOC_RM_DEV
:
3080 return btrfs_ioctl_rm_dev(root
, argp
);
3081 case BTRFS_IOC_FS_INFO
:
3082 return btrfs_ioctl_fs_info(root
, argp
);
3083 case BTRFS_IOC_DEV_INFO
:
3084 return btrfs_ioctl_dev_info(root
, argp
);
3085 case BTRFS_IOC_BALANCE
:
3086 return btrfs_balance(root
->fs_info
->dev_root
);
3087 case BTRFS_IOC_CLONE
:
3088 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
3089 case BTRFS_IOC_CLONE_RANGE
:
3090 return btrfs_ioctl_clone_range(file
, argp
);
3091 case BTRFS_IOC_TRANS_START
:
3092 return btrfs_ioctl_trans_start(file
);
3093 case BTRFS_IOC_TRANS_END
:
3094 return btrfs_ioctl_trans_end(file
);
3095 case BTRFS_IOC_TREE_SEARCH
:
3096 return btrfs_ioctl_tree_search(file
, argp
);
3097 case BTRFS_IOC_INO_LOOKUP
:
3098 return btrfs_ioctl_ino_lookup(file
, argp
);
3099 case BTRFS_IOC_INO_PATHS
:
3100 return btrfs_ioctl_ino_to_path(root
, argp
);
3101 case BTRFS_IOC_LOGICAL_INO
:
3102 return btrfs_ioctl_logical_to_ino(root
, argp
);
3103 case BTRFS_IOC_SPACE_INFO
:
3104 return btrfs_ioctl_space_info(root
, argp
);
3105 case BTRFS_IOC_SYNC
:
3106 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
3108 case BTRFS_IOC_START_SYNC
:
3109 return btrfs_ioctl_start_sync(file
, argp
);
3110 case BTRFS_IOC_WAIT_SYNC
:
3111 return btrfs_ioctl_wait_sync(file
, argp
);
3112 case BTRFS_IOC_SCRUB
:
3113 return btrfs_ioctl_scrub(root
, argp
);
3114 case BTRFS_IOC_SCRUB_CANCEL
:
3115 return btrfs_ioctl_scrub_cancel(root
, argp
);
3116 case BTRFS_IOC_SCRUB_PROGRESS
:
3117 return btrfs_ioctl_scrub_progress(root
, argp
);