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 unsigned int i_oldflags
;
182 if (btrfs_root_readonly(root
))
185 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
188 ret
= check_flags(flags
);
192 if (!inode_owner_or_capable(inode
))
195 mutex_lock(&inode
->i_mutex
);
197 ip_oldflags
= ip
->flags
;
198 i_oldflags
= inode
->i_flags
;
200 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
201 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
202 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
203 if (!capable(CAP_LINUX_IMMUTABLE
)) {
209 ret
= mnt_want_write_file(file
);
213 if (flags
& FS_SYNC_FL
)
214 ip
->flags
|= BTRFS_INODE_SYNC
;
216 ip
->flags
&= ~BTRFS_INODE_SYNC
;
217 if (flags
& FS_IMMUTABLE_FL
)
218 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
220 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
221 if (flags
& FS_APPEND_FL
)
222 ip
->flags
|= BTRFS_INODE_APPEND
;
224 ip
->flags
&= ~BTRFS_INODE_APPEND
;
225 if (flags
& FS_NODUMP_FL
)
226 ip
->flags
|= BTRFS_INODE_NODUMP
;
228 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
229 if (flags
& FS_NOATIME_FL
)
230 ip
->flags
|= BTRFS_INODE_NOATIME
;
232 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
233 if (flags
& FS_DIRSYNC_FL
)
234 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
236 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
237 if (flags
& FS_NOCOW_FL
)
238 ip
->flags
|= BTRFS_INODE_NODATACOW
;
240 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
243 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
244 * flag may be changed automatically if compression code won't make
247 if (flags
& FS_NOCOMP_FL
) {
248 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
249 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
250 } else if (flags
& FS_COMPR_FL
) {
251 ip
->flags
|= BTRFS_INODE_COMPRESS
;
252 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
254 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
257 trans
= btrfs_start_transaction(root
, 1);
259 ret
= PTR_ERR(trans
);
263 btrfs_update_iflags(inode
);
264 inode
->i_ctime
= CURRENT_TIME
;
265 ret
= btrfs_update_inode(trans
, root
, inode
);
267 btrfs_end_transaction(trans
, root
);
270 ip
->flags
= ip_oldflags
;
271 inode
->i_flags
= i_oldflags
;
274 mnt_drop_write_file(file
);
276 mutex_unlock(&inode
->i_mutex
);
280 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
282 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
284 return put_user(inode
->i_generation
, arg
);
287 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
289 struct btrfs_fs_info
*fs_info
= btrfs_sb(fdentry(file
)->d_sb
);
290 struct btrfs_device
*device
;
291 struct request_queue
*q
;
292 struct fstrim_range range
;
293 u64 minlen
= ULLONG_MAX
;
295 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
298 if (!capable(CAP_SYS_ADMIN
))
302 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
306 q
= bdev_get_queue(device
->bdev
);
307 if (blk_queue_discard(q
)) {
309 minlen
= min((u64
)q
->limits
.discard_granularity
,
317 if (copy_from_user(&range
, arg
, sizeof(range
)))
319 if (range
.start
> total_bytes
)
322 range
.len
= min(range
.len
, total_bytes
- range
.start
);
323 range
.minlen
= max(range
.minlen
, minlen
);
324 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
328 if (copy_to_user(arg
, &range
, sizeof(range
)))
334 static noinline
int create_subvol(struct btrfs_root
*root
,
335 struct dentry
*dentry
,
336 char *name
, int namelen
,
339 struct btrfs_trans_handle
*trans
;
340 struct btrfs_key key
;
341 struct btrfs_root_item root_item
;
342 struct btrfs_inode_item
*inode_item
;
343 struct extent_buffer
*leaf
;
344 struct btrfs_root
*new_root
;
345 struct dentry
*parent
= dentry
->d_parent
;
350 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
353 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
357 dir
= parent
->d_inode
;
365 trans
= btrfs_start_transaction(root
, 6);
367 return PTR_ERR(trans
);
369 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
370 0, objectid
, NULL
, 0, 0, 0, 0);
376 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
377 btrfs_set_header_bytenr(leaf
, leaf
->start
);
378 btrfs_set_header_generation(leaf
, trans
->transid
);
379 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
380 btrfs_set_header_owner(leaf
, objectid
);
382 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
383 (unsigned long)btrfs_header_fsid(leaf
),
385 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
386 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
388 btrfs_mark_buffer_dirty(leaf
);
390 inode_item
= &root_item
.inode
;
391 memset(inode_item
, 0, sizeof(*inode_item
));
392 inode_item
->generation
= cpu_to_le64(1);
393 inode_item
->size
= cpu_to_le64(3);
394 inode_item
->nlink
= cpu_to_le32(1);
395 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
396 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
399 root_item
.byte_limit
= 0;
400 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
402 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
403 btrfs_set_root_generation(&root_item
, trans
->transid
);
404 btrfs_set_root_level(&root_item
, 0);
405 btrfs_set_root_refs(&root_item
, 1);
406 btrfs_set_root_used(&root_item
, leaf
->len
);
407 btrfs_set_root_last_snapshot(&root_item
, 0);
409 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
410 root_item
.drop_level
= 0;
412 btrfs_tree_unlock(leaf
);
413 free_extent_buffer(leaf
);
416 btrfs_set_root_dirid(&root_item
, new_dirid
);
418 key
.objectid
= objectid
;
420 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
421 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
426 key
.offset
= (u64
)-1;
427 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
428 BUG_ON(IS_ERR(new_root
));
430 btrfs_record_root_in_trans(trans
, new_root
);
432 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
434 * insert the directory item
436 ret
= btrfs_set_inode_index(dir
, &index
);
439 ret
= btrfs_insert_dir_item(trans
, root
,
440 name
, namelen
, dir
, &key
,
441 BTRFS_FT_DIR
, index
);
445 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
446 ret
= btrfs_update_inode(trans
, root
, dir
);
449 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
450 objectid
, root
->root_key
.objectid
,
451 btrfs_ino(dir
), index
, name
, namelen
);
455 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
458 *async_transid
= trans
->transid
;
459 err
= btrfs_commit_transaction_async(trans
, root
, 1);
461 err
= btrfs_commit_transaction(trans
, root
);
468 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
469 char *name
, int namelen
, u64
*async_transid
,
473 struct btrfs_pending_snapshot
*pending_snapshot
;
474 struct btrfs_trans_handle
*trans
;
480 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
481 if (!pending_snapshot
)
484 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
485 pending_snapshot
->dentry
= dentry
;
486 pending_snapshot
->root
= root
;
487 pending_snapshot
->readonly
= readonly
;
489 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
491 ret
= PTR_ERR(trans
);
495 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
498 spin_lock(&root
->fs_info
->trans_lock
);
499 list_add(&pending_snapshot
->list
,
500 &trans
->transaction
->pending_snapshots
);
501 spin_unlock(&root
->fs_info
->trans_lock
);
503 *async_transid
= trans
->transid
;
504 ret
= btrfs_commit_transaction_async(trans
,
505 root
->fs_info
->extent_root
, 1);
507 ret
= btrfs_commit_transaction(trans
,
508 root
->fs_info
->extent_root
);
512 ret
= pending_snapshot
->error
;
516 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
520 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
522 ret
= PTR_ERR(inode
);
526 d_instantiate(dentry
, inode
);
529 kfree(pending_snapshot
);
533 /* copy of check_sticky in fs/namei.c()
534 * It's inline, so penalty for filesystems that don't use sticky bit is
537 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
539 uid_t fsuid
= current_fsuid();
541 if (!(dir
->i_mode
& S_ISVTX
))
543 if (inode
->i_uid
== fsuid
)
545 if (dir
->i_uid
== fsuid
)
547 return !capable(CAP_FOWNER
);
550 /* copy of may_delete in fs/namei.c()
551 * Check whether we can remove a link victim from directory dir, check
552 * whether the type of victim is right.
553 * 1. We can't do it if dir is read-only (done in permission())
554 * 2. We should have write and exec permissions on dir
555 * 3. We can't remove anything from append-only dir
556 * 4. We can't do anything with immutable dir (done in permission())
557 * 5. If the sticky bit on dir is set we should either
558 * a. be owner of dir, or
559 * b. be owner of victim, or
560 * c. have CAP_FOWNER capability
561 * 6. If the victim is append-only or immutable we can't do antyhing with
562 * links pointing to it.
563 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
564 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
565 * 9. We can't remove a root or mountpoint.
566 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
567 * nfs_async_unlink().
570 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
574 if (!victim
->d_inode
)
577 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
578 audit_inode_child(victim
, dir
);
580 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
585 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
586 IS_APPEND(victim
->d_inode
)||
587 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
590 if (!S_ISDIR(victim
->d_inode
->i_mode
))
594 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
598 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
603 /* copy of may_create in fs/namei.c() */
604 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
610 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
614 * Create a new subvolume below @parent. This is largely modeled after
615 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
616 * inside this filesystem so it's quite a bit simpler.
618 static noinline
int btrfs_mksubvol(struct path
*parent
,
619 char *name
, int namelen
,
620 struct btrfs_root
*snap_src
,
621 u64
*async_transid
, bool readonly
)
623 struct inode
*dir
= parent
->dentry
->d_inode
;
624 struct dentry
*dentry
;
627 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
629 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
630 error
= PTR_ERR(dentry
);
638 error
= mnt_want_write(parent
->mnt
);
642 error
= btrfs_may_create(dir
, dentry
);
646 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
648 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
652 error
= create_snapshot(snap_src
, dentry
,
653 name
, namelen
, async_transid
, readonly
);
655 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
656 name
, namelen
, async_transid
);
659 fsnotify_mkdir(dir
, dentry
);
661 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
663 mnt_drop_write(parent
->mnt
);
667 mutex_unlock(&dir
->i_mutex
);
672 * When we're defragging a range, we don't want to kick it off again
673 * if it is really just waiting for delalloc to send it down.
674 * If we find a nice big extent or delalloc range for the bytes in the
675 * file you want to defrag, we return 0 to let you know to skip this
678 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
680 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
681 struct extent_map
*em
= NULL
;
682 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
685 read_lock(&em_tree
->lock
);
686 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
687 read_unlock(&em_tree
->lock
);
690 end
= extent_map_end(em
);
692 if (end
- offset
> thresh
)
695 /* if we already have a nice delalloc here, just stop */
697 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
698 thresh
, EXTENT_DELALLOC
, 1);
705 * helper function to walk through a file and find extents
706 * newer than a specific transid, and smaller than thresh.
708 * This is used by the defragging code to find new and small
711 static int find_new_extents(struct btrfs_root
*root
,
712 struct inode
*inode
, u64 newer_than
,
713 u64
*off
, int thresh
)
715 struct btrfs_path
*path
;
716 struct btrfs_key min_key
;
717 struct btrfs_key max_key
;
718 struct extent_buffer
*leaf
;
719 struct btrfs_file_extent_item
*extent
;
722 u64 ino
= btrfs_ino(inode
);
724 path
= btrfs_alloc_path();
728 min_key
.objectid
= ino
;
729 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
730 min_key
.offset
= *off
;
732 max_key
.objectid
= ino
;
733 max_key
.type
= (u8
)-1;
734 max_key
.offset
= (u64
)-1;
736 path
->keep_locks
= 1;
739 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
740 path
, 0, newer_than
);
743 if (min_key
.objectid
!= ino
)
745 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
748 leaf
= path
->nodes
[0];
749 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
750 struct btrfs_file_extent_item
);
752 type
= btrfs_file_extent_type(leaf
, extent
);
753 if (type
== BTRFS_FILE_EXTENT_REG
&&
754 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
755 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
756 *off
= min_key
.offset
;
757 btrfs_free_path(path
);
761 if (min_key
.offset
== (u64
)-1)
765 btrfs_release_path(path
);
768 btrfs_free_path(path
);
772 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
773 int thresh
, u64
*last_len
, u64
*skip
,
776 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
777 struct extent_map
*em
= NULL
;
778 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
782 * make sure that once we start defragging an extent, we keep on
785 if (start
< *defrag_end
)
791 * hopefully we have this extent in the tree already, try without
792 * the full extent lock
794 read_lock(&em_tree
->lock
);
795 em
= lookup_extent_mapping(em_tree
, start
, len
);
796 read_unlock(&em_tree
->lock
);
799 /* get the big lock and read metadata off disk */
800 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
801 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
802 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
808 /* this will cover holes, and inline extents */
809 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
813 * we hit a real extent, if it is big don't bother defragging it again
815 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
819 * last_len ends up being a counter of how many bytes we've defragged.
820 * every time we choose not to defrag an extent, we reset *last_len
821 * so that the next tiny extent will force a defrag.
823 * The end result of this is that tiny extents before a single big
824 * extent will force at least part of that big extent to be defragged.
827 *defrag_end
= extent_map_end(em
);
830 *skip
= extent_map_end(em
);
839 * it doesn't do much good to defrag one or two pages
840 * at a time. This pulls in a nice chunk of pages
843 * It also makes sure the delalloc code has enough
844 * dirty data to avoid making new small extents as part
847 * It's a good idea to start RA on this range
848 * before calling this.
850 static int cluster_pages_for_defrag(struct inode
*inode
,
852 unsigned long start_index
,
855 unsigned long file_end
;
856 u64 isize
= i_size_read(inode
);
862 struct btrfs_ordered_extent
*ordered
;
863 struct extent_state
*cached_state
= NULL
;
864 struct extent_io_tree
*tree
;
865 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
869 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
871 ret
= btrfs_delalloc_reserve_space(inode
,
872 num_pages
<< PAGE_CACHE_SHIFT
);
876 tree
= &BTRFS_I(inode
)->io_tree
;
878 /* step one, lock all the pages */
879 for (i
= 0; i
< num_pages
; i
++) {
882 page
= find_or_create_page(inode
->i_mapping
,
883 start_index
+ i
, mask
);
887 page_start
= page_offset(page
);
888 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
890 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
891 ordered
= btrfs_lookup_ordered_extent(inode
,
893 unlock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
898 btrfs_start_ordered_extent(inode
, ordered
, 1);
899 btrfs_put_ordered_extent(ordered
);
903 if (!PageUptodate(page
)) {
904 btrfs_readpage(NULL
, page
);
906 if (!PageUptodate(page
)) {
908 page_cache_release(page
);
914 isize
= i_size_read(inode
);
915 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
916 if (!isize
|| page
->index
> file_end
) {
917 /* whoops, we blew past eof, skip this page */
919 page_cache_release(page
);
923 if (page
->mapping
!= inode
->i_mapping
) {
925 page_cache_release(page
);
935 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
939 * so now we have a nice long stream of locked
940 * and up to date pages, lets wait on them
942 for (i
= 0; i
< i_done
; i
++)
943 wait_on_page_writeback(pages
[i
]);
945 page_start
= page_offset(pages
[0]);
946 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
948 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
949 page_start
, page_end
- 1, 0, &cached_state
,
951 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
952 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
953 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
956 if (i_done
!= num_pages
) {
957 spin_lock(&BTRFS_I(inode
)->lock
);
958 BTRFS_I(inode
)->outstanding_extents
++;
959 spin_unlock(&BTRFS_I(inode
)->lock
);
960 btrfs_delalloc_release_space(inode
,
961 (num_pages
- i_done
) << PAGE_CACHE_SHIFT
);
965 btrfs_set_extent_delalloc(inode
, page_start
, page_end
- 1,
968 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
969 page_start
, page_end
- 1, &cached_state
,
972 for (i
= 0; i
< i_done
; i
++) {
973 clear_page_dirty_for_io(pages
[i
]);
974 ClearPageChecked(pages
[i
]);
975 set_page_extent_mapped(pages
[i
]);
976 set_page_dirty(pages
[i
]);
977 unlock_page(pages
[i
]);
978 page_cache_release(pages
[i
]);
982 for (i
= 0; i
< i_done
; i
++) {
983 unlock_page(pages
[i
]);
984 page_cache_release(pages
[i
]);
986 btrfs_delalloc_release_space(inode
, num_pages
<< PAGE_CACHE_SHIFT
);
991 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
992 struct btrfs_ioctl_defrag_range_args
*range
,
993 u64 newer_than
, unsigned long max_to_defrag
)
995 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
996 struct btrfs_super_block
*disk_super
;
997 struct file_ra_state
*ra
= NULL
;
998 unsigned long last_index
;
999 u64 isize
= i_size_read(inode
);
1004 u64 newer_off
= range
->start
;
1006 unsigned long ra_index
= 0;
1008 int defrag_count
= 0;
1009 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1010 int extent_thresh
= range
->extent_thresh
;
1011 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1012 int cluster
= max_cluster
;
1013 u64 new_align
= ~((u64
)128 * 1024 - 1);
1014 struct page
**pages
= NULL
;
1016 if (extent_thresh
== 0)
1017 extent_thresh
= 256 * 1024;
1019 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1020 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1022 if (range
->compress_type
)
1023 compress_type
= range
->compress_type
;
1030 * if we were not given a file, allocate a readahead
1034 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1037 file_ra_state_init(ra
, inode
->i_mapping
);
1042 pages
= kmalloc(sizeof(struct page
*) * max_cluster
,
1049 /* find the last page to defrag */
1050 if (range
->start
+ range
->len
> range
->start
) {
1051 last_index
= min_t(u64
, isize
- 1,
1052 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1054 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1058 ret
= find_new_extents(root
, inode
, newer_than
,
1059 &newer_off
, 64 * 1024);
1061 range
->start
= newer_off
;
1063 * we always align our defrag to help keep
1064 * the extents in the file evenly spaced
1066 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1070 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1073 max_to_defrag
= last_index
+ 1;
1076 * make writeback starts from i, so the defrag range can be
1077 * written sequentially.
1079 if (i
< inode
->i_mapping
->writeback_index
)
1080 inode
->i_mapping
->writeback_index
= i
;
1082 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1083 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1084 PAGE_CACHE_SHIFT
)) {
1086 * make sure we stop running if someone unmounts
1089 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1093 !should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1100 * the should_defrag function tells us how much to skip
1101 * bump our counter by the suggested amount
1103 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1104 i
= max(i
+ 1, next
);
1109 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1110 PAGE_CACHE_SHIFT
) - i
;
1111 cluster
= min(cluster
, max_cluster
);
1113 cluster
= max_cluster
;
1116 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1117 BTRFS_I(inode
)->force_compress
= compress_type
;
1119 if (i
+ cluster
> ra_index
) {
1120 ra_index
= max(i
, ra_index
);
1121 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1123 ra_index
+= max_cluster
;
1126 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1130 defrag_count
+= ret
;
1131 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, ret
);
1134 if (newer_off
== (u64
)-1)
1137 newer_off
= max(newer_off
+ 1,
1138 (u64
)i
<< PAGE_CACHE_SHIFT
);
1140 ret
= find_new_extents(root
, inode
,
1141 newer_than
, &newer_off
,
1144 range
->start
= newer_off
;
1145 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1152 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1160 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1161 filemap_flush(inode
->i_mapping
);
1163 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1164 /* the filemap_flush will queue IO into the worker threads, but
1165 * we have to make sure the IO is actually started and that
1166 * ordered extents get created before we return
1168 atomic_inc(&root
->fs_info
->async_submit_draining
);
1169 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1170 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1171 wait_event(root
->fs_info
->async_submit_wait
,
1172 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1173 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1175 atomic_dec(&root
->fs_info
->async_submit_draining
);
1177 mutex_lock(&inode
->i_mutex
);
1178 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1179 mutex_unlock(&inode
->i_mutex
);
1182 disk_super
= root
->fs_info
->super_copy
;
1183 features
= btrfs_super_incompat_flags(disk_super
);
1184 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1185 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
1186 btrfs_set_super_incompat_flags(disk_super
, features
);
1198 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
1204 struct btrfs_ioctl_vol_args
*vol_args
;
1205 struct btrfs_trans_handle
*trans
;
1206 struct btrfs_device
*device
= NULL
;
1208 char *devstr
= NULL
;
1212 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1215 if (!capable(CAP_SYS_ADMIN
))
1218 mutex_lock(&root
->fs_info
->volume_mutex
);
1219 if (root
->fs_info
->balance_ctl
) {
1220 printk(KERN_INFO
"btrfs: balance in progress\n");
1225 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1226 if (IS_ERR(vol_args
)) {
1227 ret
= PTR_ERR(vol_args
);
1231 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1233 sizestr
= vol_args
->name
;
1234 devstr
= strchr(sizestr
, ':');
1237 sizestr
= devstr
+ 1;
1239 devstr
= vol_args
->name
;
1240 devid
= simple_strtoull(devstr
, &end
, 10);
1241 printk(KERN_INFO
"btrfs: resizing devid %llu\n",
1242 (unsigned long long)devid
);
1244 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1246 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1247 (unsigned long long)devid
);
1251 if (!strcmp(sizestr
, "max"))
1252 new_size
= device
->bdev
->bd_inode
->i_size
;
1254 if (sizestr
[0] == '-') {
1257 } else if (sizestr
[0] == '+') {
1261 new_size
= memparse(sizestr
, NULL
);
1262 if (new_size
== 0) {
1268 old_size
= device
->total_bytes
;
1271 if (new_size
> old_size
) {
1275 new_size
= old_size
- new_size
;
1276 } else if (mod
> 0) {
1277 new_size
= old_size
+ new_size
;
1280 if (new_size
< 256 * 1024 * 1024) {
1284 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1289 do_div(new_size
, root
->sectorsize
);
1290 new_size
*= root
->sectorsize
;
1292 printk(KERN_INFO
"btrfs: new size for %s is %llu\n",
1293 device
->name
, (unsigned long long)new_size
);
1295 if (new_size
> old_size
) {
1296 trans
= btrfs_start_transaction(root
, 0);
1297 if (IS_ERR(trans
)) {
1298 ret
= PTR_ERR(trans
);
1301 ret
= btrfs_grow_device(trans
, device
, new_size
);
1302 btrfs_commit_transaction(trans
, root
);
1303 } else if (new_size
< old_size
) {
1304 ret
= btrfs_shrink_device(device
, new_size
);
1310 mutex_unlock(&root
->fs_info
->volume_mutex
);
1314 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1321 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1322 struct file
*src_file
;
1326 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1329 namelen
= strlen(name
);
1330 if (strchr(name
, '/')) {
1335 if (name
[0] == '.' &&
1336 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1342 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1343 NULL
, transid
, readonly
);
1345 struct inode
*src_inode
;
1346 src_file
= fget(fd
);
1352 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1353 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1354 printk(KERN_INFO
"btrfs: Snapshot src from "
1360 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1361 BTRFS_I(src_inode
)->root
,
1369 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1370 void __user
*arg
, int subvol
)
1372 struct btrfs_ioctl_vol_args
*vol_args
;
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_PATH_NAME_MAX
] = '\0';
1380 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1381 vol_args
->fd
, subvol
,
1388 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1389 void __user
*arg
, int subvol
)
1391 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1395 bool readonly
= false;
1397 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1398 if (IS_ERR(vol_args
))
1399 return PTR_ERR(vol_args
);
1400 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1402 if (vol_args
->flags
&
1403 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1408 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1410 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1413 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1414 vol_args
->fd
, subvol
,
1417 if (ret
== 0 && ptr
&&
1419 offsetof(struct btrfs_ioctl_vol_args_v2
,
1420 transid
), ptr
, sizeof(*ptr
)))
1427 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1430 struct inode
*inode
= fdentry(file
)->d_inode
;
1431 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1435 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1438 down_read(&root
->fs_info
->subvol_sem
);
1439 if (btrfs_root_readonly(root
))
1440 flags
|= BTRFS_SUBVOL_RDONLY
;
1441 up_read(&root
->fs_info
->subvol_sem
);
1443 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1449 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1452 struct inode
*inode
= fdentry(file
)->d_inode
;
1453 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1454 struct btrfs_trans_handle
*trans
;
1459 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1462 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1465 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1468 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1471 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1474 if (!inode_owner_or_capable(inode
))
1477 down_write(&root
->fs_info
->subvol_sem
);
1480 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1483 root_flags
= btrfs_root_flags(&root
->root_item
);
1484 if (flags
& BTRFS_SUBVOL_RDONLY
)
1485 btrfs_set_root_flags(&root
->root_item
,
1486 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1488 btrfs_set_root_flags(&root
->root_item
,
1489 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1491 trans
= btrfs_start_transaction(root
, 1);
1492 if (IS_ERR(trans
)) {
1493 ret
= PTR_ERR(trans
);
1497 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1498 &root
->root_key
, &root
->root_item
);
1500 btrfs_commit_transaction(trans
, root
);
1503 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1505 up_write(&root
->fs_info
->subvol_sem
);
1510 * helper to check if the subvolume references other subvolumes
1512 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1514 struct btrfs_path
*path
;
1515 struct btrfs_key key
;
1518 path
= btrfs_alloc_path();
1522 key
.objectid
= root
->root_key
.objectid
;
1523 key
.type
= BTRFS_ROOT_REF_KEY
;
1524 key
.offset
= (u64
)-1;
1526 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1533 if (path
->slots
[0] > 0) {
1535 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1536 if (key
.objectid
== root
->root_key
.objectid
&&
1537 key
.type
== BTRFS_ROOT_REF_KEY
)
1541 btrfs_free_path(path
);
1545 static noinline
int key_in_sk(struct btrfs_key
*key
,
1546 struct btrfs_ioctl_search_key
*sk
)
1548 struct btrfs_key test
;
1551 test
.objectid
= sk
->min_objectid
;
1552 test
.type
= sk
->min_type
;
1553 test
.offset
= sk
->min_offset
;
1555 ret
= btrfs_comp_cpu_keys(key
, &test
);
1559 test
.objectid
= sk
->max_objectid
;
1560 test
.type
= sk
->max_type
;
1561 test
.offset
= sk
->max_offset
;
1563 ret
= btrfs_comp_cpu_keys(key
, &test
);
1569 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1570 struct btrfs_path
*path
,
1571 struct btrfs_key
*key
,
1572 struct btrfs_ioctl_search_key
*sk
,
1574 unsigned long *sk_offset
,
1578 struct extent_buffer
*leaf
;
1579 struct btrfs_ioctl_search_header sh
;
1580 unsigned long item_off
;
1581 unsigned long item_len
;
1587 leaf
= path
->nodes
[0];
1588 slot
= path
->slots
[0];
1589 nritems
= btrfs_header_nritems(leaf
);
1591 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1595 found_transid
= btrfs_header_generation(leaf
);
1597 for (i
= slot
; i
< nritems
; i
++) {
1598 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1599 item_len
= btrfs_item_size_nr(leaf
, i
);
1601 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1604 if (sizeof(sh
) + item_len
+ *sk_offset
>
1605 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1610 btrfs_item_key_to_cpu(leaf
, key
, i
);
1611 if (!key_in_sk(key
, sk
))
1614 sh
.objectid
= key
->objectid
;
1615 sh
.offset
= key
->offset
;
1616 sh
.type
= key
->type
;
1618 sh
.transid
= found_transid
;
1620 /* copy search result header */
1621 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1622 *sk_offset
+= sizeof(sh
);
1625 char *p
= buf
+ *sk_offset
;
1627 read_extent_buffer(leaf
, p
,
1628 item_off
, item_len
);
1629 *sk_offset
+= item_len
;
1633 if (*num_found
>= sk
->nr_items
)
1638 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1640 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1643 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1653 static noinline
int search_ioctl(struct inode
*inode
,
1654 struct btrfs_ioctl_search_args
*args
)
1656 struct btrfs_root
*root
;
1657 struct btrfs_key key
;
1658 struct btrfs_key max_key
;
1659 struct btrfs_path
*path
;
1660 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1661 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1664 unsigned long sk_offset
= 0;
1666 path
= btrfs_alloc_path();
1670 if (sk
->tree_id
== 0) {
1671 /* search the root of the inode that was passed */
1672 root
= BTRFS_I(inode
)->root
;
1674 key
.objectid
= sk
->tree_id
;
1675 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1676 key
.offset
= (u64
)-1;
1677 root
= btrfs_read_fs_root_no_name(info
, &key
);
1679 printk(KERN_ERR
"could not find root %llu\n",
1681 btrfs_free_path(path
);
1686 key
.objectid
= sk
->min_objectid
;
1687 key
.type
= sk
->min_type
;
1688 key
.offset
= sk
->min_offset
;
1690 max_key
.objectid
= sk
->max_objectid
;
1691 max_key
.type
= sk
->max_type
;
1692 max_key
.offset
= sk
->max_offset
;
1694 path
->keep_locks
= 1;
1697 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1704 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1705 &sk_offset
, &num_found
);
1706 btrfs_release_path(path
);
1707 if (ret
|| num_found
>= sk
->nr_items
)
1713 sk
->nr_items
= num_found
;
1714 btrfs_free_path(path
);
1718 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1721 struct btrfs_ioctl_search_args
*args
;
1722 struct inode
*inode
;
1725 if (!capable(CAP_SYS_ADMIN
))
1728 args
= memdup_user(argp
, sizeof(*args
));
1730 return PTR_ERR(args
);
1732 inode
= fdentry(file
)->d_inode
;
1733 ret
= search_ioctl(inode
, args
);
1734 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1741 * Search INODE_REFs to identify path name of 'dirid' directory
1742 * in a 'tree_id' tree. and sets path name to 'name'.
1744 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1745 u64 tree_id
, u64 dirid
, char *name
)
1747 struct btrfs_root
*root
;
1748 struct btrfs_key key
;
1754 struct btrfs_inode_ref
*iref
;
1755 struct extent_buffer
*l
;
1756 struct btrfs_path
*path
;
1758 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1763 path
= btrfs_alloc_path();
1767 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1769 key
.objectid
= tree_id
;
1770 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1771 key
.offset
= (u64
)-1;
1772 root
= btrfs_read_fs_root_no_name(info
, &key
);
1774 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1779 key
.objectid
= dirid
;
1780 key
.type
= BTRFS_INODE_REF_KEY
;
1781 key
.offset
= (u64
)-1;
1784 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1789 slot
= path
->slots
[0];
1790 if (ret
> 0 && slot
> 0)
1792 btrfs_item_key_to_cpu(l
, &key
, slot
);
1794 if (ret
> 0 && (key
.objectid
!= dirid
||
1795 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1800 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1801 len
= btrfs_inode_ref_name_len(l
, iref
);
1803 total_len
+= len
+ 1;
1808 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1810 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1813 btrfs_release_path(path
);
1814 key
.objectid
= key
.offset
;
1815 key
.offset
= (u64
)-1;
1816 dirid
= key
.objectid
;
1820 memmove(name
, ptr
, total_len
);
1821 name
[total_len
]='\0';
1824 btrfs_free_path(path
);
1828 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1831 struct btrfs_ioctl_ino_lookup_args
*args
;
1832 struct inode
*inode
;
1835 if (!capable(CAP_SYS_ADMIN
))
1838 args
= memdup_user(argp
, sizeof(*args
));
1840 return PTR_ERR(args
);
1842 inode
= fdentry(file
)->d_inode
;
1844 if (args
->treeid
== 0)
1845 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1847 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1848 args
->treeid
, args
->objectid
,
1851 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1858 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1861 struct dentry
*parent
= fdentry(file
);
1862 struct dentry
*dentry
;
1863 struct inode
*dir
= parent
->d_inode
;
1864 struct inode
*inode
;
1865 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1866 struct btrfs_root
*dest
= NULL
;
1867 struct btrfs_ioctl_vol_args
*vol_args
;
1868 struct btrfs_trans_handle
*trans
;
1873 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1874 if (IS_ERR(vol_args
))
1875 return PTR_ERR(vol_args
);
1877 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1878 namelen
= strlen(vol_args
->name
);
1879 if (strchr(vol_args
->name
, '/') ||
1880 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1885 err
= mnt_want_write_file(file
);
1889 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1890 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1891 if (IS_ERR(dentry
)) {
1892 err
= PTR_ERR(dentry
);
1893 goto out_unlock_dir
;
1896 if (!dentry
->d_inode
) {
1901 inode
= dentry
->d_inode
;
1902 dest
= BTRFS_I(inode
)->root
;
1903 if (!capable(CAP_SYS_ADMIN
)){
1905 * Regular user. Only allow this with a special mount
1906 * option, when the user has write+exec access to the
1907 * subvol root, and when rmdir(2) would have been
1910 * Note that this is _not_ check that the subvol is
1911 * empty or doesn't contain data that we wouldn't
1912 * otherwise be able to delete.
1914 * Users who want to delete empty subvols should try
1918 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1922 * Do not allow deletion if the parent dir is the same
1923 * as the dir to be deleted. That means the ioctl
1924 * must be called on the dentry referencing the root
1925 * of the subvol, not a random directory contained
1932 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1936 /* check if subvolume may be deleted by a non-root user */
1937 err
= btrfs_may_delete(dir
, dentry
, 1);
1942 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1947 mutex_lock(&inode
->i_mutex
);
1948 err
= d_invalidate(dentry
);
1952 down_write(&root
->fs_info
->subvol_sem
);
1954 err
= may_destroy_subvol(dest
);
1958 trans
= btrfs_start_transaction(root
, 0);
1959 if (IS_ERR(trans
)) {
1960 err
= PTR_ERR(trans
);
1963 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1965 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1966 dest
->root_key
.objectid
,
1967 dentry
->d_name
.name
,
1968 dentry
->d_name
.len
);
1971 btrfs_record_root_in_trans(trans
, dest
);
1973 memset(&dest
->root_item
.drop_progress
, 0,
1974 sizeof(dest
->root_item
.drop_progress
));
1975 dest
->root_item
.drop_level
= 0;
1976 btrfs_set_root_refs(&dest
->root_item
, 0);
1978 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1979 ret
= btrfs_insert_orphan_item(trans
,
1980 root
->fs_info
->tree_root
,
1981 dest
->root_key
.objectid
);
1985 ret
= btrfs_end_transaction(trans
, root
);
1987 inode
->i_flags
|= S_DEAD
;
1989 up_write(&root
->fs_info
->subvol_sem
);
1991 mutex_unlock(&inode
->i_mutex
);
1993 shrink_dcache_sb(root
->fs_info
->sb
);
1994 btrfs_invalidate_inodes(dest
);
2000 mutex_unlock(&dir
->i_mutex
);
2001 mnt_drop_write_file(file
);
2007 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2009 struct inode
*inode
= fdentry(file
)->d_inode
;
2010 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2011 struct btrfs_ioctl_defrag_range_args
*range
;
2014 if (btrfs_root_readonly(root
))
2017 ret
= mnt_want_write_file(file
);
2021 switch (inode
->i_mode
& S_IFMT
) {
2023 if (!capable(CAP_SYS_ADMIN
)) {
2027 ret
= btrfs_defrag_root(root
, 0);
2030 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2033 if (!(file
->f_mode
& FMODE_WRITE
)) {
2038 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2045 if (copy_from_user(range
, argp
,
2051 /* compression requires us to start the IO */
2052 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2053 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2054 range
->extent_thresh
= (u32
)-1;
2057 /* the rest are all set to zero by kzalloc */
2058 range
->len
= (u64
)-1;
2060 ret
= btrfs_defrag_file(fdentry(file
)->d_inode
, file
,
2070 mnt_drop_write_file(file
);
2074 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2076 struct btrfs_ioctl_vol_args
*vol_args
;
2079 if (!capable(CAP_SYS_ADMIN
))
2082 mutex_lock(&root
->fs_info
->volume_mutex
);
2083 if (root
->fs_info
->balance_ctl
) {
2084 printk(KERN_INFO
"btrfs: balance in progress\n");
2089 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2090 if (IS_ERR(vol_args
)) {
2091 ret
= PTR_ERR(vol_args
);
2095 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2096 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2100 mutex_unlock(&root
->fs_info
->volume_mutex
);
2104 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
2106 struct btrfs_ioctl_vol_args
*vol_args
;
2109 if (!capable(CAP_SYS_ADMIN
))
2112 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2115 mutex_lock(&root
->fs_info
->volume_mutex
);
2116 if (root
->fs_info
->balance_ctl
) {
2117 printk(KERN_INFO
"btrfs: balance in progress\n");
2122 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2123 if (IS_ERR(vol_args
)) {
2124 ret
= PTR_ERR(vol_args
);
2128 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2129 ret
= btrfs_rm_device(root
, vol_args
->name
);
2133 mutex_unlock(&root
->fs_info
->volume_mutex
);
2137 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2139 struct btrfs_ioctl_fs_info_args
*fi_args
;
2140 struct btrfs_device
*device
;
2141 struct btrfs_device
*next
;
2142 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2145 if (!capable(CAP_SYS_ADMIN
))
2148 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2152 fi_args
->num_devices
= fs_devices
->num_devices
;
2153 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2155 mutex_lock(&fs_devices
->device_list_mutex
);
2156 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2157 if (device
->devid
> fi_args
->max_id
)
2158 fi_args
->max_id
= device
->devid
;
2160 mutex_unlock(&fs_devices
->device_list_mutex
);
2162 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2169 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2171 struct btrfs_ioctl_dev_info_args
*di_args
;
2172 struct btrfs_device
*dev
;
2173 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2175 char *s_uuid
= NULL
;
2176 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
2178 if (!capable(CAP_SYS_ADMIN
))
2181 di_args
= memdup_user(arg
, sizeof(*di_args
));
2182 if (IS_ERR(di_args
))
2183 return PTR_ERR(di_args
);
2185 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
2186 s_uuid
= di_args
->uuid
;
2188 mutex_lock(&fs_devices
->device_list_mutex
);
2189 dev
= btrfs_find_device(root
, di_args
->devid
, s_uuid
, NULL
);
2190 mutex_unlock(&fs_devices
->device_list_mutex
);
2197 di_args
->devid
= dev
->devid
;
2198 di_args
->bytes_used
= dev
->bytes_used
;
2199 di_args
->total_bytes
= dev
->total_bytes
;
2200 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2201 strncpy(di_args
->path
, dev
->name
, sizeof(di_args
->path
));
2204 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2211 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
2212 u64 off
, u64 olen
, u64 destoff
)
2214 struct inode
*inode
= fdentry(file
)->d_inode
;
2215 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2216 struct file
*src_file
;
2218 struct btrfs_trans_handle
*trans
;
2219 struct btrfs_path
*path
;
2220 struct extent_buffer
*leaf
;
2222 struct btrfs_key key
;
2227 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
2232 * - split compressed inline extents. annoying: we need to
2233 * decompress into destination's address_space (the file offset
2234 * may change, so source mapping won't do), then recompress (or
2235 * otherwise reinsert) a subrange.
2236 * - allow ranges within the same file to be cloned (provided
2237 * they don't overlap)?
2240 /* the destination must be opened for writing */
2241 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
2244 if (btrfs_root_readonly(root
))
2247 ret
= mnt_want_write_file(file
);
2251 src_file
= fget(srcfd
);
2254 goto out_drop_write
;
2257 src
= src_file
->f_dentry
->d_inode
;
2263 /* the src must be open for reading */
2264 if (!(src_file
->f_mode
& FMODE_READ
))
2267 /* don't make the dst file partly checksummed */
2268 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2269 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
2273 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
2277 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
2281 buf
= vmalloc(btrfs_level_size(root
, 0));
2285 path
= btrfs_alloc_path();
2293 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
2294 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
2296 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
2297 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2300 /* determine range to clone */
2302 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
2305 olen
= len
= src
->i_size
- off
;
2306 /* if we extend to eof, continue to block boundary */
2307 if (off
+ len
== src
->i_size
)
2308 len
= ALIGN(src
->i_size
, bs
) - off
;
2310 /* verify the end result is block aligned */
2311 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
2312 !IS_ALIGNED(destoff
, bs
))
2315 if (destoff
> inode
->i_size
) {
2316 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
2321 /* truncate page cache pages from target inode range */
2322 truncate_inode_pages_range(&inode
->i_data
, destoff
,
2323 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
2325 /* do any pending delalloc/csum calc on src, one way or
2326 another, and lock file content */
2328 struct btrfs_ordered_extent
*ordered
;
2329 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2330 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
2332 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
2333 EXTENT_DELALLOC
, 0, NULL
))
2335 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2337 btrfs_put_ordered_extent(ordered
);
2338 btrfs_wait_ordered_range(src
, off
, len
);
2342 key
.objectid
= btrfs_ino(src
);
2343 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2348 * note the key will change type as we walk through the
2351 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2355 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2356 if (path
->slots
[0] >= nritems
) {
2357 ret
= btrfs_next_leaf(root
, path
);
2362 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2364 leaf
= path
->nodes
[0];
2365 slot
= path
->slots
[0];
2367 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2368 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2369 key
.objectid
!= btrfs_ino(src
))
2372 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2373 struct btrfs_file_extent_item
*extent
;
2376 struct btrfs_key new_key
;
2377 u64 disko
= 0, diskl
= 0;
2378 u64 datao
= 0, datal
= 0;
2382 size
= btrfs_item_size_nr(leaf
, slot
);
2383 read_extent_buffer(leaf
, buf
,
2384 btrfs_item_ptr_offset(leaf
, slot
),
2387 extent
= btrfs_item_ptr(leaf
, slot
,
2388 struct btrfs_file_extent_item
);
2389 comp
= btrfs_file_extent_compression(leaf
, extent
);
2390 type
= btrfs_file_extent_type(leaf
, extent
);
2391 if (type
== BTRFS_FILE_EXTENT_REG
||
2392 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2393 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2395 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2397 datao
= btrfs_file_extent_offset(leaf
, extent
);
2398 datal
= btrfs_file_extent_num_bytes(leaf
,
2400 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2401 /* take upper bound, may be compressed */
2402 datal
= btrfs_file_extent_ram_bytes(leaf
,
2405 btrfs_release_path(path
);
2407 if (key
.offset
+ datal
<= off
||
2408 key
.offset
>= off
+len
)
2411 memcpy(&new_key
, &key
, sizeof(new_key
));
2412 new_key
.objectid
= btrfs_ino(inode
);
2413 if (off
<= key
.offset
)
2414 new_key
.offset
= key
.offset
+ destoff
- off
;
2416 new_key
.offset
= destoff
;
2419 * 1 - adjusting old extent (we may have to split it)
2420 * 1 - add new extent
2423 trans
= btrfs_start_transaction(root
, 3);
2424 if (IS_ERR(trans
)) {
2425 ret
= PTR_ERR(trans
);
2429 if (type
== BTRFS_FILE_EXTENT_REG
||
2430 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2432 * a | --- range to clone ---| b
2433 * | ------------- extent ------------- |
2436 /* substract range b */
2437 if (key
.offset
+ datal
> off
+ len
)
2438 datal
= off
+ len
- key
.offset
;
2440 /* substract range a */
2441 if (off
> key
.offset
) {
2442 datao
+= off
- key
.offset
;
2443 datal
-= off
- key
.offset
;
2446 ret
= btrfs_drop_extents(trans
, inode
,
2448 new_key
.offset
+ datal
,
2452 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2456 leaf
= path
->nodes
[0];
2457 slot
= path
->slots
[0];
2458 write_extent_buffer(leaf
, buf
,
2459 btrfs_item_ptr_offset(leaf
, slot
),
2462 extent
= btrfs_item_ptr(leaf
, slot
,
2463 struct btrfs_file_extent_item
);
2465 /* disko == 0 means it's a hole */
2469 btrfs_set_file_extent_offset(leaf
, extent
,
2471 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2474 inode_add_bytes(inode
, datal
);
2475 ret
= btrfs_inc_extent_ref(trans
, root
,
2477 root
->root_key
.objectid
,
2479 new_key
.offset
- datao
,
2483 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2486 if (off
> key
.offset
) {
2487 skip
= off
- key
.offset
;
2488 new_key
.offset
+= skip
;
2491 if (key
.offset
+ datal
> off
+len
)
2492 trim
= key
.offset
+ datal
- (off
+len
);
2494 if (comp
&& (skip
|| trim
)) {
2496 btrfs_end_transaction(trans
, root
);
2499 size
-= skip
+ trim
;
2500 datal
-= skip
+ trim
;
2502 ret
= btrfs_drop_extents(trans
, inode
,
2504 new_key
.offset
+ datal
,
2508 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2514 btrfs_file_extent_calc_inline_size(0);
2515 memmove(buf
+start
, buf
+start
+skip
,
2519 leaf
= path
->nodes
[0];
2520 slot
= path
->slots
[0];
2521 write_extent_buffer(leaf
, buf
,
2522 btrfs_item_ptr_offset(leaf
, slot
),
2524 inode_add_bytes(inode
, datal
);
2527 btrfs_mark_buffer_dirty(leaf
);
2528 btrfs_release_path(path
);
2530 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2533 * we round up to the block size at eof when
2534 * determining which extents to clone above,
2535 * but shouldn't round up the file size
2537 endoff
= new_key
.offset
+ datal
;
2538 if (endoff
> destoff
+olen
)
2539 endoff
= destoff
+olen
;
2540 if (endoff
> inode
->i_size
)
2541 btrfs_i_size_write(inode
, endoff
);
2543 ret
= btrfs_update_inode(trans
, root
, inode
);
2545 btrfs_end_transaction(trans
, root
);
2548 btrfs_release_path(path
);
2553 btrfs_release_path(path
);
2554 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2556 mutex_unlock(&src
->i_mutex
);
2557 mutex_unlock(&inode
->i_mutex
);
2559 btrfs_free_path(path
);
2563 mnt_drop_write_file(file
);
2567 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2569 struct btrfs_ioctl_clone_range_args args
;
2571 if (copy_from_user(&args
, argp
, sizeof(args
)))
2573 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2574 args
.src_length
, args
.dest_offset
);
2578 * there are many ways the trans_start and trans_end ioctls can lead
2579 * to deadlocks. They should only be used by applications that
2580 * basically own the machine, and have a very in depth understanding
2581 * of all the possible deadlocks and enospc problems.
2583 static long btrfs_ioctl_trans_start(struct file
*file
)
2585 struct inode
*inode
= fdentry(file
)->d_inode
;
2586 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2587 struct btrfs_trans_handle
*trans
;
2591 if (!capable(CAP_SYS_ADMIN
))
2595 if (file
->private_data
)
2599 if (btrfs_root_readonly(root
))
2602 ret
= mnt_want_write_file(file
);
2606 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
2609 trans
= btrfs_start_ioctl_transaction(root
);
2613 file
->private_data
= trans
;
2617 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2618 mnt_drop_write_file(file
);
2623 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2625 struct inode
*inode
= fdentry(file
)->d_inode
;
2626 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2627 struct btrfs_root
*new_root
;
2628 struct btrfs_dir_item
*di
;
2629 struct btrfs_trans_handle
*trans
;
2630 struct btrfs_path
*path
;
2631 struct btrfs_key location
;
2632 struct btrfs_disk_key disk_key
;
2633 struct btrfs_super_block
*disk_super
;
2638 if (!capable(CAP_SYS_ADMIN
))
2641 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2645 objectid
= root
->root_key
.objectid
;
2647 location
.objectid
= objectid
;
2648 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2649 location
.offset
= (u64
)-1;
2651 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2652 if (IS_ERR(new_root
))
2653 return PTR_ERR(new_root
);
2655 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2658 path
= btrfs_alloc_path();
2661 path
->leave_spinning
= 1;
2663 trans
= btrfs_start_transaction(root
, 1);
2664 if (IS_ERR(trans
)) {
2665 btrfs_free_path(path
);
2666 return PTR_ERR(trans
);
2669 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
2670 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2671 dir_id
, "default", 7, 1);
2672 if (IS_ERR_OR_NULL(di
)) {
2673 btrfs_free_path(path
);
2674 btrfs_end_transaction(trans
, root
);
2675 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2676 "this isn't going to work\n");
2680 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2681 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2682 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2683 btrfs_free_path(path
);
2685 disk_super
= root
->fs_info
->super_copy
;
2686 features
= btrfs_super_incompat_flags(disk_super
);
2687 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2688 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2689 btrfs_set_super_incompat_flags(disk_super
, features
);
2691 btrfs_end_transaction(trans
, root
);
2696 static void get_block_group_info(struct list_head
*groups_list
,
2697 struct btrfs_ioctl_space_info
*space
)
2699 struct btrfs_block_group_cache
*block_group
;
2701 space
->total_bytes
= 0;
2702 space
->used_bytes
= 0;
2704 list_for_each_entry(block_group
, groups_list
, list
) {
2705 space
->flags
= block_group
->flags
;
2706 space
->total_bytes
+= block_group
->key
.offset
;
2707 space
->used_bytes
+=
2708 btrfs_block_group_used(&block_group
->item
);
2712 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2714 struct btrfs_ioctl_space_args space_args
;
2715 struct btrfs_ioctl_space_info space
;
2716 struct btrfs_ioctl_space_info
*dest
;
2717 struct btrfs_ioctl_space_info
*dest_orig
;
2718 struct btrfs_ioctl_space_info __user
*user_dest
;
2719 struct btrfs_space_info
*info
;
2720 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2721 BTRFS_BLOCK_GROUP_SYSTEM
,
2722 BTRFS_BLOCK_GROUP_METADATA
,
2723 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2730 if (copy_from_user(&space_args
,
2731 (struct btrfs_ioctl_space_args __user
*)arg
,
2732 sizeof(space_args
)))
2735 for (i
= 0; i
< num_types
; i
++) {
2736 struct btrfs_space_info
*tmp
;
2740 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2742 if (tmp
->flags
== types
[i
]) {
2752 down_read(&info
->groups_sem
);
2753 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2754 if (!list_empty(&info
->block_groups
[c
]))
2757 up_read(&info
->groups_sem
);
2760 /* space_slots == 0 means they are asking for a count */
2761 if (space_args
.space_slots
== 0) {
2762 space_args
.total_spaces
= slot_count
;
2766 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2768 alloc_size
= sizeof(*dest
) * slot_count
;
2770 /* we generally have at most 6 or so space infos, one for each raid
2771 * level. So, a whole page should be more than enough for everyone
2773 if (alloc_size
> PAGE_CACHE_SIZE
)
2776 space_args
.total_spaces
= 0;
2777 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2782 /* now we have a buffer to copy into */
2783 for (i
= 0; i
< num_types
; i
++) {
2784 struct btrfs_space_info
*tmp
;
2791 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2793 if (tmp
->flags
== types
[i
]) {
2802 down_read(&info
->groups_sem
);
2803 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2804 if (!list_empty(&info
->block_groups
[c
])) {
2805 get_block_group_info(&info
->block_groups
[c
],
2807 memcpy(dest
, &space
, sizeof(space
));
2809 space_args
.total_spaces
++;
2815 up_read(&info
->groups_sem
);
2818 user_dest
= (struct btrfs_ioctl_space_info
*)
2819 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2821 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2826 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2833 * there are many ways the trans_start and trans_end ioctls can lead
2834 * to deadlocks. They should only be used by applications that
2835 * basically own the machine, and have a very in depth understanding
2836 * of all the possible deadlocks and enospc problems.
2838 long btrfs_ioctl_trans_end(struct file
*file
)
2840 struct inode
*inode
= fdentry(file
)->d_inode
;
2841 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2842 struct btrfs_trans_handle
*trans
;
2844 trans
= file
->private_data
;
2847 file
->private_data
= NULL
;
2849 btrfs_end_transaction(trans
, root
);
2851 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2853 mnt_drop_write_file(file
);
2857 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2859 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2860 struct btrfs_trans_handle
*trans
;
2864 trans
= btrfs_start_transaction(root
, 0);
2866 return PTR_ERR(trans
);
2867 transid
= trans
->transid
;
2868 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2870 btrfs_end_transaction(trans
, root
);
2875 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2880 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2882 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2886 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2889 transid
= 0; /* current trans */
2891 return btrfs_wait_for_commit(root
, transid
);
2894 static long btrfs_ioctl_scrub(struct btrfs_root
*root
, void __user
*arg
)
2897 struct btrfs_ioctl_scrub_args
*sa
;
2899 if (!capable(CAP_SYS_ADMIN
))
2902 sa
= memdup_user(arg
, sizeof(*sa
));
2906 ret
= btrfs_scrub_dev(root
, sa
->devid
, sa
->start
, sa
->end
,
2907 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
);
2909 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2916 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
2918 if (!capable(CAP_SYS_ADMIN
))
2921 return btrfs_scrub_cancel(root
);
2924 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
2927 struct btrfs_ioctl_scrub_args
*sa
;
2930 if (!capable(CAP_SYS_ADMIN
))
2933 sa
= memdup_user(arg
, sizeof(*sa
));
2937 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
2939 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2946 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
2952 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
2953 struct inode_fs_paths
*ipath
= NULL
;
2954 struct btrfs_path
*path
;
2956 if (!capable(CAP_SYS_ADMIN
))
2959 path
= btrfs_alloc_path();
2965 ipa
= memdup_user(arg
, sizeof(*ipa
));
2972 size
= min_t(u32
, ipa
->size
, 4096);
2973 ipath
= init_ipath(size
, root
, path
);
2974 if (IS_ERR(ipath
)) {
2975 ret
= PTR_ERR(ipath
);
2980 ret
= paths_from_inode(ipa
->inum
, ipath
);
2984 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
2985 rel_ptr
= ipath
->fspath
->val
[i
] -
2986 (u64
)(unsigned long)ipath
->fspath
->val
;
2987 ipath
->fspath
->val
[i
] = rel_ptr
;
2990 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
2991 (void *)(unsigned long)ipath
->fspath
, size
);
2998 btrfs_free_path(path
);
3005 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3007 struct btrfs_data_container
*inodes
= ctx
;
3008 const size_t c
= 3 * sizeof(u64
);
3010 if (inodes
->bytes_left
>= c
) {
3011 inodes
->bytes_left
-= c
;
3012 inodes
->val
[inodes
->elem_cnt
] = inum
;
3013 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3014 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3015 inodes
->elem_cnt
+= 3;
3017 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3018 inodes
->bytes_left
= 0;
3019 inodes
->elem_missed
+= 3;
3025 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3030 u64 extent_item_pos
;
3031 struct btrfs_ioctl_logical_ino_args
*loi
;
3032 struct btrfs_data_container
*inodes
= NULL
;
3033 struct btrfs_path
*path
= NULL
;
3034 struct btrfs_key key
;
3036 if (!capable(CAP_SYS_ADMIN
))
3039 loi
= memdup_user(arg
, sizeof(*loi
));
3046 path
= btrfs_alloc_path();
3052 size
= min_t(u32
, loi
->size
, 4096);
3053 inodes
= init_data_container(size
);
3054 if (IS_ERR(inodes
)) {
3055 ret
= PTR_ERR(inodes
);
3060 ret
= extent_from_logical(root
->fs_info
, loi
->logical
, path
, &key
);
3061 btrfs_release_path(path
);
3063 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
3068 extent_item_pos
= loi
->logical
- key
.objectid
;
3069 ret
= iterate_extent_inodes(root
->fs_info
, key
.objectid
,
3070 extent_item_pos
, 0, build_ino_list
,
3076 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3077 (void *)(unsigned long)inodes
, size
);
3082 btrfs_free_path(path
);
3089 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3090 struct btrfs_ioctl_balance_args
*bargs
)
3092 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3094 bargs
->flags
= bctl
->flags
;
3096 if (atomic_read(&fs_info
->balance_running
))
3097 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3098 if (atomic_read(&fs_info
->balance_pause_req
))
3099 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3100 if (atomic_read(&fs_info
->balance_cancel_req
))
3101 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3103 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3104 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3105 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3108 spin_lock(&fs_info
->balance_lock
);
3109 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3110 spin_unlock(&fs_info
->balance_lock
);
3112 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3116 static long btrfs_ioctl_balance(struct btrfs_root
*root
, void __user
*arg
)
3118 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3119 struct btrfs_ioctl_balance_args
*bargs
;
3120 struct btrfs_balance_control
*bctl
;
3123 if (!capable(CAP_SYS_ADMIN
))
3126 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3129 mutex_lock(&fs_info
->volume_mutex
);
3130 mutex_lock(&fs_info
->balance_mutex
);
3133 bargs
= memdup_user(arg
, sizeof(*bargs
));
3134 if (IS_ERR(bargs
)) {
3135 ret
= PTR_ERR(bargs
);
3139 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3140 if (!fs_info
->balance_ctl
) {
3145 bctl
= fs_info
->balance_ctl
;
3146 spin_lock(&fs_info
->balance_lock
);
3147 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3148 spin_unlock(&fs_info
->balance_lock
);
3156 if (fs_info
->balance_ctl
) {
3161 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3167 bctl
->fs_info
= fs_info
;
3169 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
3170 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
3171 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
3173 bctl
->flags
= bargs
->flags
;
3175 /* balance everything - no filters */
3176 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
3180 ret
= btrfs_balance(bctl
, bargs
);
3182 * bctl is freed in __cancel_balance or in free_fs_info if
3183 * restriper was paused all the way until unmount
3186 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3193 mutex_unlock(&fs_info
->balance_mutex
);
3194 mutex_unlock(&fs_info
->volume_mutex
);
3198 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
3200 if (!capable(CAP_SYS_ADMIN
))
3204 case BTRFS_BALANCE_CTL_PAUSE
:
3205 return btrfs_pause_balance(root
->fs_info
);
3206 case BTRFS_BALANCE_CTL_CANCEL
:
3207 return btrfs_cancel_balance(root
->fs_info
);
3213 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
3216 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3217 struct btrfs_ioctl_balance_args
*bargs
;
3220 if (!capable(CAP_SYS_ADMIN
))
3223 mutex_lock(&fs_info
->balance_mutex
);
3224 if (!fs_info
->balance_ctl
) {
3229 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
3235 update_ioctl_balance_args(fs_info
, 1, bargs
);
3237 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3242 mutex_unlock(&fs_info
->balance_mutex
);
3246 long btrfs_ioctl(struct file
*file
, unsigned int
3247 cmd
, unsigned long arg
)
3249 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3250 void __user
*argp
= (void __user
*)arg
;
3253 case FS_IOC_GETFLAGS
:
3254 return btrfs_ioctl_getflags(file
, argp
);
3255 case FS_IOC_SETFLAGS
:
3256 return btrfs_ioctl_setflags(file
, argp
);
3257 case FS_IOC_GETVERSION
:
3258 return btrfs_ioctl_getversion(file
, argp
);
3260 return btrfs_ioctl_fitrim(file
, argp
);
3261 case BTRFS_IOC_SNAP_CREATE
:
3262 return btrfs_ioctl_snap_create(file
, argp
, 0);
3263 case BTRFS_IOC_SNAP_CREATE_V2
:
3264 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
3265 case BTRFS_IOC_SUBVOL_CREATE
:
3266 return btrfs_ioctl_snap_create(file
, argp
, 1);
3267 case BTRFS_IOC_SNAP_DESTROY
:
3268 return btrfs_ioctl_snap_destroy(file
, argp
);
3269 case BTRFS_IOC_SUBVOL_GETFLAGS
:
3270 return btrfs_ioctl_subvol_getflags(file
, argp
);
3271 case BTRFS_IOC_SUBVOL_SETFLAGS
:
3272 return btrfs_ioctl_subvol_setflags(file
, argp
);
3273 case BTRFS_IOC_DEFAULT_SUBVOL
:
3274 return btrfs_ioctl_default_subvol(file
, argp
);
3275 case BTRFS_IOC_DEFRAG
:
3276 return btrfs_ioctl_defrag(file
, NULL
);
3277 case BTRFS_IOC_DEFRAG_RANGE
:
3278 return btrfs_ioctl_defrag(file
, argp
);
3279 case BTRFS_IOC_RESIZE
:
3280 return btrfs_ioctl_resize(root
, argp
);
3281 case BTRFS_IOC_ADD_DEV
:
3282 return btrfs_ioctl_add_dev(root
, argp
);
3283 case BTRFS_IOC_RM_DEV
:
3284 return btrfs_ioctl_rm_dev(root
, argp
);
3285 case BTRFS_IOC_FS_INFO
:
3286 return btrfs_ioctl_fs_info(root
, argp
);
3287 case BTRFS_IOC_DEV_INFO
:
3288 return btrfs_ioctl_dev_info(root
, argp
);
3289 case BTRFS_IOC_BALANCE
:
3290 return btrfs_ioctl_balance(root
, NULL
);
3291 case BTRFS_IOC_CLONE
:
3292 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
3293 case BTRFS_IOC_CLONE_RANGE
:
3294 return btrfs_ioctl_clone_range(file
, argp
);
3295 case BTRFS_IOC_TRANS_START
:
3296 return btrfs_ioctl_trans_start(file
);
3297 case BTRFS_IOC_TRANS_END
:
3298 return btrfs_ioctl_trans_end(file
);
3299 case BTRFS_IOC_TREE_SEARCH
:
3300 return btrfs_ioctl_tree_search(file
, argp
);
3301 case BTRFS_IOC_INO_LOOKUP
:
3302 return btrfs_ioctl_ino_lookup(file
, argp
);
3303 case BTRFS_IOC_INO_PATHS
:
3304 return btrfs_ioctl_ino_to_path(root
, argp
);
3305 case BTRFS_IOC_LOGICAL_INO
:
3306 return btrfs_ioctl_logical_to_ino(root
, argp
);
3307 case BTRFS_IOC_SPACE_INFO
:
3308 return btrfs_ioctl_space_info(root
, argp
);
3309 case BTRFS_IOC_SYNC
:
3310 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
3312 case BTRFS_IOC_START_SYNC
:
3313 return btrfs_ioctl_start_sync(file
, argp
);
3314 case BTRFS_IOC_WAIT_SYNC
:
3315 return btrfs_ioctl_wait_sync(file
, argp
);
3316 case BTRFS_IOC_SCRUB
:
3317 return btrfs_ioctl_scrub(root
, argp
);
3318 case BTRFS_IOC_SCRUB_CANCEL
:
3319 return btrfs_ioctl_scrub_cancel(root
, argp
);
3320 case BTRFS_IOC_SCRUB_PROGRESS
:
3321 return btrfs_ioctl_scrub_progress(root
, argp
);
3322 case BTRFS_IOC_BALANCE_V2
:
3323 return btrfs_ioctl_balance(root
, argp
);
3324 case BTRFS_IOC_BALANCE_CTL
:
3325 return btrfs_ioctl_balance_ctl(root
, arg
);
3326 case BTRFS_IOC_BALANCE_PROGRESS
:
3327 return btrfs_ioctl_balance_progress(root
, argp
);