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
->f_path
.mnt
);
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
->f_path
.mnt
);
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_root
*root
= fdentry(file
)->d_sb
->s_fs_info
;
290 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
291 struct btrfs_device
*device
;
292 struct request_queue
*q
;
293 struct fstrim_range range
;
294 u64 minlen
= ULLONG_MAX
;
296 u64 total_bytes
= btrfs_super_total_bytes(root
->fs_info
->super_copy
);
299 if (!capable(CAP_SYS_ADMIN
))
303 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
307 q
= bdev_get_queue(device
->bdev
);
308 if (blk_queue_discard(q
)) {
310 minlen
= min((u64
)q
->limits
.discard_granularity
,
318 if (copy_from_user(&range
, arg
, sizeof(range
)))
320 if (range
.start
> total_bytes
)
323 range
.len
= min(range
.len
, total_bytes
- range
.start
);
324 range
.minlen
= max(range
.minlen
, minlen
);
325 ret
= btrfs_trim_fs(root
, &range
);
329 if (copy_to_user(arg
, &range
, sizeof(range
)))
335 static noinline
int create_subvol(struct btrfs_root
*root
,
336 struct dentry
*dentry
,
337 char *name
, int namelen
,
340 struct btrfs_trans_handle
*trans
;
341 struct btrfs_key key
;
342 struct btrfs_root_item root_item
;
343 struct btrfs_inode_item
*inode_item
;
344 struct extent_buffer
*leaf
;
345 struct btrfs_root
*new_root
;
346 struct dentry
*parent
= dentry
->d_parent
;
351 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
354 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
358 dir
= parent
->d_inode
;
366 trans
= btrfs_start_transaction(root
, 6);
368 return PTR_ERR(trans
);
370 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
371 0, objectid
, NULL
, 0, 0, 0, 0);
377 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
378 btrfs_set_header_bytenr(leaf
, leaf
->start
);
379 btrfs_set_header_generation(leaf
, trans
->transid
);
380 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
381 btrfs_set_header_owner(leaf
, objectid
);
383 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
384 (unsigned long)btrfs_header_fsid(leaf
),
386 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
387 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
389 btrfs_mark_buffer_dirty(leaf
);
391 inode_item
= &root_item
.inode
;
392 memset(inode_item
, 0, sizeof(*inode_item
));
393 inode_item
->generation
= cpu_to_le64(1);
394 inode_item
->size
= cpu_to_le64(3);
395 inode_item
->nlink
= cpu_to_le32(1);
396 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
397 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
400 root_item
.byte_limit
= 0;
401 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
403 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
404 btrfs_set_root_generation(&root_item
, trans
->transid
);
405 btrfs_set_root_level(&root_item
, 0);
406 btrfs_set_root_refs(&root_item
, 1);
407 btrfs_set_root_used(&root_item
, leaf
->len
);
408 btrfs_set_root_last_snapshot(&root_item
, 0);
410 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
411 root_item
.drop_level
= 0;
413 btrfs_tree_unlock(leaf
);
414 free_extent_buffer(leaf
);
417 btrfs_set_root_dirid(&root_item
, new_dirid
);
419 key
.objectid
= objectid
;
421 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
422 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
427 key
.offset
= (u64
)-1;
428 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
429 BUG_ON(IS_ERR(new_root
));
431 btrfs_record_root_in_trans(trans
, new_root
);
433 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
435 * insert the directory item
437 ret
= btrfs_set_inode_index(dir
, &index
);
440 ret
= btrfs_insert_dir_item(trans
, root
,
441 name
, namelen
, dir
, &key
,
442 BTRFS_FT_DIR
, index
);
446 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
447 ret
= btrfs_update_inode(trans
, root
, dir
);
450 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
451 objectid
, root
->root_key
.objectid
,
452 btrfs_ino(dir
), index
, name
, namelen
);
456 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
459 *async_transid
= trans
->transid
;
460 err
= btrfs_commit_transaction_async(trans
, root
, 1);
462 err
= btrfs_commit_transaction(trans
, root
);
469 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
470 char *name
, int namelen
, u64
*async_transid
,
474 struct btrfs_pending_snapshot
*pending_snapshot
;
475 struct btrfs_trans_handle
*trans
;
481 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
482 if (!pending_snapshot
)
485 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
486 pending_snapshot
->dentry
= dentry
;
487 pending_snapshot
->root
= root
;
488 pending_snapshot
->readonly
= readonly
;
490 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
492 ret
= PTR_ERR(trans
);
496 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
499 spin_lock(&root
->fs_info
->trans_lock
);
500 list_add(&pending_snapshot
->list
,
501 &trans
->transaction
->pending_snapshots
);
502 spin_unlock(&root
->fs_info
->trans_lock
);
504 *async_transid
= trans
->transid
;
505 ret
= btrfs_commit_transaction_async(trans
,
506 root
->fs_info
->extent_root
, 1);
508 ret
= btrfs_commit_transaction(trans
,
509 root
->fs_info
->extent_root
);
513 ret
= pending_snapshot
->error
;
517 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
521 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
523 ret
= PTR_ERR(inode
);
527 d_instantiate(dentry
, inode
);
530 kfree(pending_snapshot
);
534 /* copy of check_sticky in fs/namei.c()
535 * It's inline, so penalty for filesystems that don't use sticky bit is
538 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
540 uid_t fsuid
= current_fsuid();
542 if (!(dir
->i_mode
& S_ISVTX
))
544 if (inode
->i_uid
== fsuid
)
546 if (dir
->i_uid
== fsuid
)
548 return !capable(CAP_FOWNER
);
551 /* copy of may_delete in fs/namei.c()
552 * Check whether we can remove a link victim from directory dir, check
553 * whether the type of victim is right.
554 * 1. We can't do it if dir is read-only (done in permission())
555 * 2. We should have write and exec permissions on dir
556 * 3. We can't remove anything from append-only dir
557 * 4. We can't do anything with immutable dir (done in permission())
558 * 5. If the sticky bit on dir is set we should either
559 * a. be owner of dir, or
560 * b. be owner of victim, or
561 * c. have CAP_FOWNER capability
562 * 6. If the victim is append-only or immutable we can't do antyhing with
563 * links pointing to it.
564 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
565 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
566 * 9. We can't remove a root or mountpoint.
567 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
568 * nfs_async_unlink().
571 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
575 if (!victim
->d_inode
)
578 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
579 audit_inode_child(victim
, dir
);
581 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
586 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
587 IS_APPEND(victim
->d_inode
)||
588 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
591 if (!S_ISDIR(victim
->d_inode
->i_mode
))
595 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
599 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
604 /* copy of may_create in fs/namei.c() */
605 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
611 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
615 * Create a new subvolume below @parent. This is largely modeled after
616 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
617 * inside this filesystem so it's quite a bit simpler.
619 static noinline
int btrfs_mksubvol(struct path
*parent
,
620 char *name
, int namelen
,
621 struct btrfs_root
*snap_src
,
622 u64
*async_transid
, bool readonly
)
624 struct inode
*dir
= parent
->dentry
->d_inode
;
625 struct dentry
*dentry
;
628 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
630 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
631 error
= PTR_ERR(dentry
);
639 error
= mnt_want_write(parent
->mnt
);
643 error
= btrfs_may_create(dir
, dentry
);
647 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
649 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
653 error
= create_snapshot(snap_src
, dentry
,
654 name
, namelen
, async_transid
, readonly
);
656 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
657 name
, namelen
, async_transid
);
660 fsnotify_mkdir(dir
, dentry
);
662 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
664 mnt_drop_write(parent
->mnt
);
668 mutex_unlock(&dir
->i_mutex
);
673 * When we're defragging a range, we don't want to kick it off again
674 * if it is really just waiting for delalloc to send it down.
675 * If we find a nice big extent or delalloc range for the bytes in the
676 * file you want to defrag, we return 0 to let you know to skip this
679 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
681 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
682 struct extent_map
*em
= NULL
;
683 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
686 read_lock(&em_tree
->lock
);
687 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
688 read_unlock(&em_tree
->lock
);
691 end
= extent_map_end(em
);
693 if (end
- offset
> thresh
)
696 /* if we already have a nice delalloc here, just stop */
698 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
699 thresh
, EXTENT_DELALLOC
, 1);
706 * helper function to walk through a file and find extents
707 * newer than a specific transid, and smaller than thresh.
709 * This is used by the defragging code to find new and small
712 static int find_new_extents(struct btrfs_root
*root
,
713 struct inode
*inode
, u64 newer_than
,
714 u64
*off
, int thresh
)
716 struct btrfs_path
*path
;
717 struct btrfs_key min_key
;
718 struct btrfs_key max_key
;
719 struct extent_buffer
*leaf
;
720 struct btrfs_file_extent_item
*extent
;
723 u64 ino
= btrfs_ino(inode
);
725 path
= btrfs_alloc_path();
729 min_key
.objectid
= ino
;
730 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
731 min_key
.offset
= *off
;
733 max_key
.objectid
= ino
;
734 max_key
.type
= (u8
)-1;
735 max_key
.offset
= (u64
)-1;
737 path
->keep_locks
= 1;
740 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
741 path
, 0, newer_than
);
744 if (min_key
.objectid
!= ino
)
746 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
749 leaf
= path
->nodes
[0];
750 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
751 struct btrfs_file_extent_item
);
753 type
= btrfs_file_extent_type(leaf
, extent
);
754 if (type
== BTRFS_FILE_EXTENT_REG
&&
755 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
756 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
757 *off
= min_key
.offset
;
758 btrfs_free_path(path
);
762 if (min_key
.offset
== (u64
)-1)
766 btrfs_release_path(path
);
769 btrfs_free_path(path
);
773 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
774 int thresh
, u64
*last_len
, u64
*skip
,
777 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
778 struct extent_map
*em
= NULL
;
779 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
783 * make sure that once we start defragging an extent, we keep on
786 if (start
< *defrag_end
)
792 * hopefully we have this extent in the tree already, try without
793 * the full extent lock
795 read_lock(&em_tree
->lock
);
796 em
= lookup_extent_mapping(em_tree
, start
, len
);
797 read_unlock(&em_tree
->lock
);
800 /* get the big lock and read metadata off disk */
801 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
802 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
803 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
809 /* this will cover holes, and inline extents */
810 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
814 * we hit a real extent, if it is big don't bother defragging it again
816 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
820 * last_len ends up being a counter of how many bytes we've defragged.
821 * every time we choose not to defrag an extent, we reset *last_len
822 * so that the next tiny extent will force a defrag.
824 * The end result of this is that tiny extents before a single big
825 * extent will force at least part of that big extent to be defragged.
828 *defrag_end
= extent_map_end(em
);
831 *skip
= extent_map_end(em
);
840 * it doesn't do much good to defrag one or two pages
841 * at a time. This pulls in a nice chunk of pages
844 * It also makes sure the delalloc code has enough
845 * dirty data to avoid making new small extents as part
848 * It's a good idea to start RA on this range
849 * before calling this.
851 static int cluster_pages_for_defrag(struct inode
*inode
,
853 unsigned long start_index
,
856 unsigned long file_end
;
857 u64 isize
= i_size_read(inode
);
863 struct btrfs_ordered_extent
*ordered
;
864 struct extent_state
*cached_state
= NULL
;
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
);
879 /* step one, lock all the pages */
880 for (i
= 0; i
< num_pages
; i
++) {
882 page
= find_or_create_page(inode
->i_mapping
,
883 start_index
+ i
, mask
);
887 if (!PageUptodate(page
)) {
888 btrfs_readpage(NULL
, page
);
890 if (!PageUptodate(page
)) {
892 page_cache_release(page
);
897 isize
= i_size_read(inode
);
898 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
899 if (!isize
|| page
->index
> file_end
||
900 page
->mapping
!= inode
->i_mapping
) {
901 /* whoops, we blew past eof, skip this page */
903 page_cache_release(page
);
912 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
916 * so now we have a nice long stream of locked
917 * and up to date pages, lets wait on them
919 for (i
= 0; i
< i_done
; i
++)
920 wait_on_page_writeback(pages
[i
]);
922 page_start
= page_offset(pages
[0]);
923 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
925 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
926 page_start
, page_end
- 1, 0, &cached_state
,
928 ordered
= btrfs_lookup_first_ordered_extent(inode
, page_end
- 1);
930 ordered
->file_offset
+ ordered
->len
> page_start
&&
931 ordered
->file_offset
< page_end
) {
932 btrfs_put_ordered_extent(ordered
);
933 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
934 page_start
, page_end
- 1,
935 &cached_state
, GFP_NOFS
);
936 for (i
= 0; i
< i_done
; i
++) {
937 unlock_page(pages
[i
]);
938 page_cache_release(pages
[i
]);
940 btrfs_wait_ordered_range(inode
, page_start
,
941 page_end
- page_start
);
945 btrfs_put_ordered_extent(ordered
);
947 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
948 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
949 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
952 if (i_done
!= num_pages
) {
953 spin_lock(&BTRFS_I(inode
)->lock
);
954 BTRFS_I(inode
)->outstanding_extents
++;
955 spin_unlock(&BTRFS_I(inode
)->lock
);
956 btrfs_delalloc_release_space(inode
,
957 (num_pages
- i_done
) << PAGE_CACHE_SHIFT
);
961 btrfs_set_extent_delalloc(inode
, page_start
, page_end
- 1,
964 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
965 page_start
, page_end
- 1, &cached_state
,
968 for (i
= 0; i
< i_done
; i
++) {
969 clear_page_dirty_for_io(pages
[i
]);
970 ClearPageChecked(pages
[i
]);
971 set_page_extent_mapped(pages
[i
]);
972 set_page_dirty(pages
[i
]);
973 unlock_page(pages
[i
]);
974 page_cache_release(pages
[i
]);
978 for (i
= 0; i
< i_done
; i
++) {
979 unlock_page(pages
[i
]);
980 page_cache_release(pages
[i
]);
982 btrfs_delalloc_release_space(inode
, num_pages
<< PAGE_CACHE_SHIFT
);
987 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
988 struct btrfs_ioctl_defrag_range_args
*range
,
989 u64 newer_than
, unsigned long max_to_defrag
)
991 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
992 struct btrfs_super_block
*disk_super
;
993 struct file_ra_state
*ra
= NULL
;
994 unsigned long last_index
;
995 u64 isize
= i_size_read(inode
);
1000 u64 newer_off
= range
->start
;
1002 unsigned long ra_index
= 0;
1004 int defrag_count
= 0;
1005 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1006 int extent_thresh
= range
->extent_thresh
;
1007 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1008 int cluster
= max_cluster
;
1009 u64 new_align
= ~((u64
)128 * 1024 - 1);
1010 struct page
**pages
= NULL
;
1012 if (extent_thresh
== 0)
1013 extent_thresh
= 256 * 1024;
1015 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1016 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1018 if (range
->compress_type
)
1019 compress_type
= range
->compress_type
;
1026 * if we were not given a file, allocate a readahead
1030 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1033 file_ra_state_init(ra
, inode
->i_mapping
);
1038 pages
= kmalloc(sizeof(struct page
*) * max_cluster
,
1045 /* find the last page to defrag */
1046 if (range
->start
+ range
->len
> range
->start
) {
1047 last_index
= min_t(u64
, isize
- 1,
1048 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1050 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1054 ret
= find_new_extents(root
, inode
, newer_than
,
1055 &newer_off
, 64 * 1024);
1057 range
->start
= newer_off
;
1059 * we always align our defrag to help keep
1060 * the extents in the file evenly spaced
1062 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1066 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1069 max_to_defrag
= last_index
+ 1;
1072 * make writeback starts from i, so the defrag range can be
1073 * written sequentially.
1075 if (i
< inode
->i_mapping
->writeback_index
)
1076 inode
->i_mapping
->writeback_index
= i
;
1078 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1079 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1080 PAGE_CACHE_SHIFT
)) {
1082 * make sure we stop running if someone unmounts
1085 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1089 !should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1096 * the should_defrag function tells us how much to skip
1097 * bump our counter by the suggested amount
1099 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1100 i
= max(i
+ 1, next
);
1105 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1106 PAGE_CACHE_SHIFT
) - i
;
1107 cluster
= min(cluster
, max_cluster
);
1109 cluster
= max_cluster
;
1112 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1113 BTRFS_I(inode
)->force_compress
= compress_type
;
1115 if (i
+ cluster
> ra_index
) {
1116 ra_index
= max(i
, ra_index
);
1117 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1119 ra_index
+= max_cluster
;
1122 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1126 defrag_count
+= ret
;
1127 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, ret
);
1130 if (newer_off
== (u64
)-1)
1133 newer_off
= max(newer_off
+ 1,
1134 (u64
)i
<< PAGE_CACHE_SHIFT
);
1136 ret
= find_new_extents(root
, inode
,
1137 newer_than
, &newer_off
,
1140 range
->start
= newer_off
;
1141 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1148 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1156 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1157 filemap_flush(inode
->i_mapping
);
1159 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1160 /* the filemap_flush will queue IO into the worker threads, but
1161 * we have to make sure the IO is actually started and that
1162 * ordered extents get created before we return
1164 atomic_inc(&root
->fs_info
->async_submit_draining
);
1165 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1166 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1167 wait_event(root
->fs_info
->async_submit_wait
,
1168 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1169 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1171 atomic_dec(&root
->fs_info
->async_submit_draining
);
1173 mutex_lock(&inode
->i_mutex
);
1174 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1175 mutex_unlock(&inode
->i_mutex
);
1178 disk_super
= root
->fs_info
->super_copy
;
1179 features
= btrfs_super_incompat_flags(disk_super
);
1180 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1181 features
|= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO
;
1182 btrfs_set_super_incompat_flags(disk_super
, features
);
1194 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
1200 struct btrfs_ioctl_vol_args
*vol_args
;
1201 struct btrfs_trans_handle
*trans
;
1202 struct btrfs_device
*device
= NULL
;
1204 char *devstr
= NULL
;
1208 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1211 if (!capable(CAP_SYS_ADMIN
))
1214 mutex_lock(&root
->fs_info
->volume_mutex
);
1215 if (root
->fs_info
->balance_ctl
) {
1216 printk(KERN_INFO
"btrfs: balance in progress\n");
1221 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1222 if (IS_ERR(vol_args
)) {
1223 ret
= PTR_ERR(vol_args
);
1227 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1229 sizestr
= vol_args
->name
;
1230 devstr
= strchr(sizestr
, ':');
1233 sizestr
= devstr
+ 1;
1235 devstr
= vol_args
->name
;
1236 devid
= simple_strtoull(devstr
, &end
, 10);
1237 printk(KERN_INFO
"btrfs: resizing devid %llu\n",
1238 (unsigned long long)devid
);
1240 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1242 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1243 (unsigned long long)devid
);
1247 if (!strcmp(sizestr
, "max"))
1248 new_size
= device
->bdev
->bd_inode
->i_size
;
1250 if (sizestr
[0] == '-') {
1253 } else if (sizestr
[0] == '+') {
1257 new_size
= memparse(sizestr
, NULL
);
1258 if (new_size
== 0) {
1264 old_size
= device
->total_bytes
;
1267 if (new_size
> old_size
) {
1271 new_size
= old_size
- new_size
;
1272 } else if (mod
> 0) {
1273 new_size
= old_size
+ new_size
;
1276 if (new_size
< 256 * 1024 * 1024) {
1280 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1285 do_div(new_size
, root
->sectorsize
);
1286 new_size
*= root
->sectorsize
;
1288 printk(KERN_INFO
"btrfs: new size for %s is %llu\n",
1289 device
->name
, (unsigned long long)new_size
);
1291 if (new_size
> old_size
) {
1292 trans
= btrfs_start_transaction(root
, 0);
1293 if (IS_ERR(trans
)) {
1294 ret
= PTR_ERR(trans
);
1297 ret
= btrfs_grow_device(trans
, device
, new_size
);
1298 btrfs_commit_transaction(trans
, root
);
1299 } else if (new_size
< old_size
) {
1300 ret
= btrfs_shrink_device(device
, new_size
);
1306 mutex_unlock(&root
->fs_info
->volume_mutex
);
1310 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1317 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1318 struct file
*src_file
;
1322 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1325 namelen
= strlen(name
);
1326 if (strchr(name
, '/')) {
1332 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1333 NULL
, transid
, readonly
);
1335 struct inode
*src_inode
;
1336 src_file
= fget(fd
);
1342 src_inode
= src_file
->f_path
.dentry
->d_inode
;
1343 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1344 printk(KERN_INFO
"btrfs: Snapshot src from "
1350 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1351 BTRFS_I(src_inode
)->root
,
1359 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1360 void __user
*arg
, int subvol
)
1362 struct btrfs_ioctl_vol_args
*vol_args
;
1365 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1366 if (IS_ERR(vol_args
))
1367 return PTR_ERR(vol_args
);
1368 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1370 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1371 vol_args
->fd
, subvol
,
1378 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1379 void __user
*arg
, int subvol
)
1381 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1385 bool readonly
= false;
1387 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1388 if (IS_ERR(vol_args
))
1389 return PTR_ERR(vol_args
);
1390 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1392 if (vol_args
->flags
&
1393 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
)) {
1398 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1400 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1403 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1404 vol_args
->fd
, subvol
,
1407 if (ret
== 0 && ptr
&&
1409 offsetof(struct btrfs_ioctl_vol_args_v2
,
1410 transid
), ptr
, sizeof(*ptr
)))
1417 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1420 struct inode
*inode
= fdentry(file
)->d_inode
;
1421 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1425 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1428 down_read(&root
->fs_info
->subvol_sem
);
1429 if (btrfs_root_readonly(root
))
1430 flags
|= BTRFS_SUBVOL_RDONLY
;
1431 up_read(&root
->fs_info
->subvol_sem
);
1433 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1439 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1442 struct inode
*inode
= fdentry(file
)->d_inode
;
1443 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1444 struct btrfs_trans_handle
*trans
;
1449 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1452 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1455 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
1458 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1461 if (flags
& ~BTRFS_SUBVOL_RDONLY
)
1464 if (!inode_owner_or_capable(inode
))
1467 down_write(&root
->fs_info
->subvol_sem
);
1470 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1473 root_flags
= btrfs_root_flags(&root
->root_item
);
1474 if (flags
& BTRFS_SUBVOL_RDONLY
)
1475 btrfs_set_root_flags(&root
->root_item
,
1476 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1478 btrfs_set_root_flags(&root
->root_item
,
1479 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1481 trans
= btrfs_start_transaction(root
, 1);
1482 if (IS_ERR(trans
)) {
1483 ret
= PTR_ERR(trans
);
1487 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1488 &root
->root_key
, &root
->root_item
);
1490 btrfs_commit_transaction(trans
, root
);
1493 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1495 up_write(&root
->fs_info
->subvol_sem
);
1500 * helper to check if the subvolume references other subvolumes
1502 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1504 struct btrfs_path
*path
;
1505 struct btrfs_key key
;
1508 path
= btrfs_alloc_path();
1512 key
.objectid
= root
->root_key
.objectid
;
1513 key
.type
= BTRFS_ROOT_REF_KEY
;
1514 key
.offset
= (u64
)-1;
1516 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1523 if (path
->slots
[0] > 0) {
1525 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1526 if (key
.objectid
== root
->root_key
.objectid
&&
1527 key
.type
== BTRFS_ROOT_REF_KEY
)
1531 btrfs_free_path(path
);
1535 static noinline
int key_in_sk(struct btrfs_key
*key
,
1536 struct btrfs_ioctl_search_key
*sk
)
1538 struct btrfs_key test
;
1541 test
.objectid
= sk
->min_objectid
;
1542 test
.type
= sk
->min_type
;
1543 test
.offset
= sk
->min_offset
;
1545 ret
= btrfs_comp_cpu_keys(key
, &test
);
1549 test
.objectid
= sk
->max_objectid
;
1550 test
.type
= sk
->max_type
;
1551 test
.offset
= sk
->max_offset
;
1553 ret
= btrfs_comp_cpu_keys(key
, &test
);
1559 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1560 struct btrfs_path
*path
,
1561 struct btrfs_key
*key
,
1562 struct btrfs_ioctl_search_key
*sk
,
1564 unsigned long *sk_offset
,
1568 struct extent_buffer
*leaf
;
1569 struct btrfs_ioctl_search_header sh
;
1570 unsigned long item_off
;
1571 unsigned long item_len
;
1577 leaf
= path
->nodes
[0];
1578 slot
= path
->slots
[0];
1579 nritems
= btrfs_header_nritems(leaf
);
1581 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1585 found_transid
= btrfs_header_generation(leaf
);
1587 for (i
= slot
; i
< nritems
; i
++) {
1588 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1589 item_len
= btrfs_item_size_nr(leaf
, i
);
1591 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1594 if (sizeof(sh
) + item_len
+ *sk_offset
>
1595 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1600 btrfs_item_key_to_cpu(leaf
, key
, i
);
1601 if (!key_in_sk(key
, sk
))
1604 sh
.objectid
= key
->objectid
;
1605 sh
.offset
= key
->offset
;
1606 sh
.type
= key
->type
;
1608 sh
.transid
= found_transid
;
1610 /* copy search result header */
1611 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1612 *sk_offset
+= sizeof(sh
);
1615 char *p
= buf
+ *sk_offset
;
1617 read_extent_buffer(leaf
, p
,
1618 item_off
, item_len
);
1619 *sk_offset
+= item_len
;
1623 if (*num_found
>= sk
->nr_items
)
1628 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1630 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1633 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1643 static noinline
int search_ioctl(struct inode
*inode
,
1644 struct btrfs_ioctl_search_args
*args
)
1646 struct btrfs_root
*root
;
1647 struct btrfs_key key
;
1648 struct btrfs_key max_key
;
1649 struct btrfs_path
*path
;
1650 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1651 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1654 unsigned long sk_offset
= 0;
1656 path
= btrfs_alloc_path();
1660 if (sk
->tree_id
== 0) {
1661 /* search the root of the inode that was passed */
1662 root
= BTRFS_I(inode
)->root
;
1664 key
.objectid
= sk
->tree_id
;
1665 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1666 key
.offset
= (u64
)-1;
1667 root
= btrfs_read_fs_root_no_name(info
, &key
);
1669 printk(KERN_ERR
"could not find root %llu\n",
1671 btrfs_free_path(path
);
1676 key
.objectid
= sk
->min_objectid
;
1677 key
.type
= sk
->min_type
;
1678 key
.offset
= sk
->min_offset
;
1680 max_key
.objectid
= sk
->max_objectid
;
1681 max_key
.type
= sk
->max_type
;
1682 max_key
.offset
= sk
->max_offset
;
1684 path
->keep_locks
= 1;
1687 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1694 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1695 &sk_offset
, &num_found
);
1696 btrfs_release_path(path
);
1697 if (ret
|| num_found
>= sk
->nr_items
)
1703 sk
->nr_items
= num_found
;
1704 btrfs_free_path(path
);
1708 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1711 struct btrfs_ioctl_search_args
*args
;
1712 struct inode
*inode
;
1715 if (!capable(CAP_SYS_ADMIN
))
1718 args
= memdup_user(argp
, sizeof(*args
));
1720 return PTR_ERR(args
);
1722 inode
= fdentry(file
)->d_inode
;
1723 ret
= search_ioctl(inode
, args
);
1724 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1731 * Search INODE_REFs to identify path name of 'dirid' directory
1732 * in a 'tree_id' tree. and sets path name to 'name'.
1734 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1735 u64 tree_id
, u64 dirid
, char *name
)
1737 struct btrfs_root
*root
;
1738 struct btrfs_key key
;
1744 struct btrfs_inode_ref
*iref
;
1745 struct extent_buffer
*l
;
1746 struct btrfs_path
*path
;
1748 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1753 path
= btrfs_alloc_path();
1757 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1759 key
.objectid
= tree_id
;
1760 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1761 key
.offset
= (u64
)-1;
1762 root
= btrfs_read_fs_root_no_name(info
, &key
);
1764 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1769 key
.objectid
= dirid
;
1770 key
.type
= BTRFS_INODE_REF_KEY
;
1771 key
.offset
= (u64
)-1;
1774 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1779 slot
= path
->slots
[0];
1780 if (ret
> 0 && slot
> 0)
1782 btrfs_item_key_to_cpu(l
, &key
, slot
);
1784 if (ret
> 0 && (key
.objectid
!= dirid
||
1785 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1790 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1791 len
= btrfs_inode_ref_name_len(l
, iref
);
1793 total_len
+= len
+ 1;
1798 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1800 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1803 btrfs_release_path(path
);
1804 key
.objectid
= key
.offset
;
1805 key
.offset
= (u64
)-1;
1806 dirid
= key
.objectid
;
1810 memmove(name
, ptr
, total_len
);
1811 name
[total_len
]='\0';
1814 btrfs_free_path(path
);
1818 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1821 struct btrfs_ioctl_ino_lookup_args
*args
;
1822 struct inode
*inode
;
1825 if (!capable(CAP_SYS_ADMIN
))
1828 args
= memdup_user(argp
, sizeof(*args
));
1830 return PTR_ERR(args
);
1832 inode
= fdentry(file
)->d_inode
;
1834 if (args
->treeid
== 0)
1835 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1837 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1838 args
->treeid
, args
->objectid
,
1841 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1848 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1851 struct dentry
*parent
= fdentry(file
);
1852 struct dentry
*dentry
;
1853 struct inode
*dir
= parent
->d_inode
;
1854 struct inode
*inode
;
1855 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1856 struct btrfs_root
*dest
= NULL
;
1857 struct btrfs_ioctl_vol_args
*vol_args
;
1858 struct btrfs_trans_handle
*trans
;
1863 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1864 if (IS_ERR(vol_args
))
1865 return PTR_ERR(vol_args
);
1867 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1868 namelen
= strlen(vol_args
->name
);
1869 if (strchr(vol_args
->name
, '/') ||
1870 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1875 err
= mnt_want_write(file
->f_path
.mnt
);
1879 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1880 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1881 if (IS_ERR(dentry
)) {
1882 err
= PTR_ERR(dentry
);
1883 goto out_unlock_dir
;
1886 if (!dentry
->d_inode
) {
1891 inode
= dentry
->d_inode
;
1892 dest
= BTRFS_I(inode
)->root
;
1893 if (!capable(CAP_SYS_ADMIN
)){
1895 * Regular user. Only allow this with a special mount
1896 * option, when the user has write+exec access to the
1897 * subvol root, and when rmdir(2) would have been
1900 * Note that this is _not_ check that the subvol is
1901 * empty or doesn't contain data that we wouldn't
1902 * otherwise be able to delete.
1904 * Users who want to delete empty subvols should try
1908 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1912 * Do not allow deletion if the parent dir is the same
1913 * as the dir to be deleted. That means the ioctl
1914 * must be called on the dentry referencing the root
1915 * of the subvol, not a random directory contained
1922 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1926 /* check if subvolume may be deleted by a non-root user */
1927 err
= btrfs_may_delete(dir
, dentry
, 1);
1932 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1937 mutex_lock(&inode
->i_mutex
);
1938 err
= d_invalidate(dentry
);
1942 down_write(&root
->fs_info
->subvol_sem
);
1944 err
= may_destroy_subvol(dest
);
1948 trans
= btrfs_start_transaction(root
, 0);
1949 if (IS_ERR(trans
)) {
1950 err
= PTR_ERR(trans
);
1953 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1955 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1956 dest
->root_key
.objectid
,
1957 dentry
->d_name
.name
,
1958 dentry
->d_name
.len
);
1961 btrfs_record_root_in_trans(trans
, dest
);
1963 memset(&dest
->root_item
.drop_progress
, 0,
1964 sizeof(dest
->root_item
.drop_progress
));
1965 dest
->root_item
.drop_level
= 0;
1966 btrfs_set_root_refs(&dest
->root_item
, 0);
1968 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1969 ret
= btrfs_insert_orphan_item(trans
,
1970 root
->fs_info
->tree_root
,
1971 dest
->root_key
.objectid
);
1975 ret
= btrfs_end_transaction(trans
, root
);
1977 inode
->i_flags
|= S_DEAD
;
1979 up_write(&root
->fs_info
->subvol_sem
);
1981 mutex_unlock(&inode
->i_mutex
);
1983 shrink_dcache_sb(root
->fs_info
->sb
);
1984 btrfs_invalidate_inodes(dest
);
1990 mutex_unlock(&dir
->i_mutex
);
1991 mnt_drop_write(file
->f_path
.mnt
);
1997 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1999 struct inode
*inode
= fdentry(file
)->d_inode
;
2000 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2001 struct btrfs_ioctl_defrag_range_args
*range
;
2004 if (btrfs_root_readonly(root
))
2007 ret
= mnt_want_write(file
->f_path
.mnt
);
2011 switch (inode
->i_mode
& S_IFMT
) {
2013 if (!capable(CAP_SYS_ADMIN
)) {
2017 ret
= btrfs_defrag_root(root
, 0);
2020 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2023 if (!(file
->f_mode
& FMODE_WRITE
)) {
2028 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2035 if (copy_from_user(range
, argp
,
2041 /* compression requires us to start the IO */
2042 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2043 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2044 range
->extent_thresh
= (u32
)-1;
2047 /* the rest are all set to zero by kzalloc */
2048 range
->len
= (u64
)-1;
2050 ret
= btrfs_defrag_file(fdentry(file
)->d_inode
, file
,
2060 mnt_drop_write(file
->f_path
.mnt
);
2064 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2066 struct btrfs_ioctl_vol_args
*vol_args
;
2069 if (!capable(CAP_SYS_ADMIN
))
2072 mutex_lock(&root
->fs_info
->volume_mutex
);
2073 if (root
->fs_info
->balance_ctl
) {
2074 printk(KERN_INFO
"btrfs: balance in progress\n");
2079 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2080 if (IS_ERR(vol_args
)) {
2081 ret
= PTR_ERR(vol_args
);
2085 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2086 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2090 mutex_unlock(&root
->fs_info
->volume_mutex
);
2094 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
2096 struct btrfs_ioctl_vol_args
*vol_args
;
2099 if (!capable(CAP_SYS_ADMIN
))
2102 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2105 mutex_lock(&root
->fs_info
->volume_mutex
);
2106 if (root
->fs_info
->balance_ctl
) {
2107 printk(KERN_INFO
"btrfs: balance in progress\n");
2112 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2113 if (IS_ERR(vol_args
)) {
2114 ret
= PTR_ERR(vol_args
);
2118 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2119 ret
= btrfs_rm_device(root
, vol_args
->name
);
2123 mutex_unlock(&root
->fs_info
->volume_mutex
);
2127 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2129 struct btrfs_ioctl_fs_info_args
*fi_args
;
2130 struct btrfs_device
*device
;
2131 struct btrfs_device
*next
;
2132 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2135 if (!capable(CAP_SYS_ADMIN
))
2138 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2142 fi_args
->num_devices
= fs_devices
->num_devices
;
2143 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2145 mutex_lock(&fs_devices
->device_list_mutex
);
2146 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2147 if (device
->devid
> fi_args
->max_id
)
2148 fi_args
->max_id
= device
->devid
;
2150 mutex_unlock(&fs_devices
->device_list_mutex
);
2152 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2159 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2161 struct btrfs_ioctl_dev_info_args
*di_args
;
2162 struct btrfs_device
*dev
;
2163 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2165 char *s_uuid
= NULL
;
2166 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
2168 if (!capable(CAP_SYS_ADMIN
))
2171 di_args
= memdup_user(arg
, sizeof(*di_args
));
2172 if (IS_ERR(di_args
))
2173 return PTR_ERR(di_args
);
2175 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
2176 s_uuid
= di_args
->uuid
;
2178 mutex_lock(&fs_devices
->device_list_mutex
);
2179 dev
= btrfs_find_device(root
, di_args
->devid
, s_uuid
, NULL
);
2180 mutex_unlock(&fs_devices
->device_list_mutex
);
2187 di_args
->devid
= dev
->devid
;
2188 di_args
->bytes_used
= dev
->bytes_used
;
2189 di_args
->total_bytes
= dev
->total_bytes
;
2190 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2191 strncpy(di_args
->path
, dev
->name
, sizeof(di_args
->path
));
2194 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2201 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
2202 u64 off
, u64 olen
, u64 destoff
)
2204 struct inode
*inode
= fdentry(file
)->d_inode
;
2205 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2206 struct file
*src_file
;
2208 struct btrfs_trans_handle
*trans
;
2209 struct btrfs_path
*path
;
2210 struct extent_buffer
*leaf
;
2212 struct btrfs_key key
;
2217 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
2222 * - split compressed inline extents. annoying: we need to
2223 * decompress into destination's address_space (the file offset
2224 * may change, so source mapping won't do), then recompress (or
2225 * otherwise reinsert) a subrange.
2226 * - allow ranges within the same file to be cloned (provided
2227 * they don't overlap)?
2230 /* the destination must be opened for writing */
2231 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
2234 if (btrfs_root_readonly(root
))
2237 ret
= mnt_want_write(file
->f_path
.mnt
);
2241 src_file
= fget(srcfd
);
2244 goto out_drop_write
;
2247 src
= src_file
->f_dentry
->d_inode
;
2253 /* the src must be open for reading */
2254 if (!(src_file
->f_mode
& FMODE_READ
))
2257 /* don't make the dst file partly checksummed */
2258 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2259 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
2263 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
2267 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
2271 buf
= vmalloc(btrfs_level_size(root
, 0));
2275 path
= btrfs_alloc_path();
2283 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
2284 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
2286 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
2287 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2290 /* determine range to clone */
2292 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
2295 olen
= len
= src
->i_size
- off
;
2296 /* if we extend to eof, continue to block boundary */
2297 if (off
+ len
== src
->i_size
)
2298 len
= ALIGN(src
->i_size
, bs
) - off
;
2300 /* verify the end result is block aligned */
2301 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
2302 !IS_ALIGNED(destoff
, bs
))
2305 if (destoff
> inode
->i_size
) {
2306 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
2311 /* truncate page cache pages from target inode range */
2312 truncate_inode_pages_range(&inode
->i_data
, destoff
,
2313 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
2315 /* do any pending delalloc/csum calc on src, one way or
2316 another, and lock file content */
2318 struct btrfs_ordered_extent
*ordered
;
2319 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2320 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
2322 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
2323 EXTENT_DELALLOC
, 0, NULL
))
2325 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2327 btrfs_put_ordered_extent(ordered
);
2328 btrfs_wait_ordered_range(src
, off
, len
);
2332 key
.objectid
= btrfs_ino(src
);
2333 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2338 * note the key will change type as we walk through the
2341 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2345 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2346 if (path
->slots
[0] >= nritems
) {
2347 ret
= btrfs_next_leaf(root
, path
);
2352 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2354 leaf
= path
->nodes
[0];
2355 slot
= path
->slots
[0];
2357 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2358 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2359 key
.objectid
!= btrfs_ino(src
))
2362 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2363 struct btrfs_file_extent_item
*extent
;
2366 struct btrfs_key new_key
;
2367 u64 disko
= 0, diskl
= 0;
2368 u64 datao
= 0, datal
= 0;
2372 size
= btrfs_item_size_nr(leaf
, slot
);
2373 read_extent_buffer(leaf
, buf
,
2374 btrfs_item_ptr_offset(leaf
, slot
),
2377 extent
= btrfs_item_ptr(leaf
, slot
,
2378 struct btrfs_file_extent_item
);
2379 comp
= btrfs_file_extent_compression(leaf
, extent
);
2380 type
= btrfs_file_extent_type(leaf
, extent
);
2381 if (type
== BTRFS_FILE_EXTENT_REG
||
2382 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2383 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2385 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2387 datao
= btrfs_file_extent_offset(leaf
, extent
);
2388 datal
= btrfs_file_extent_num_bytes(leaf
,
2390 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2391 /* take upper bound, may be compressed */
2392 datal
= btrfs_file_extent_ram_bytes(leaf
,
2395 btrfs_release_path(path
);
2397 if (key
.offset
+ datal
<= off
||
2398 key
.offset
>= off
+len
)
2401 memcpy(&new_key
, &key
, sizeof(new_key
));
2402 new_key
.objectid
= btrfs_ino(inode
);
2403 if (off
<= key
.offset
)
2404 new_key
.offset
= key
.offset
+ destoff
- off
;
2406 new_key
.offset
= destoff
;
2409 * 1 - adjusting old extent (we may have to split it)
2410 * 1 - add new extent
2413 trans
= btrfs_start_transaction(root
, 3);
2414 if (IS_ERR(trans
)) {
2415 ret
= PTR_ERR(trans
);
2419 if (type
== BTRFS_FILE_EXTENT_REG
||
2420 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2422 * a | --- range to clone ---| b
2423 * | ------------- extent ------------- |
2426 /* substract range b */
2427 if (key
.offset
+ datal
> off
+ len
)
2428 datal
= off
+ len
- key
.offset
;
2430 /* substract range a */
2431 if (off
> key
.offset
) {
2432 datao
+= off
- key
.offset
;
2433 datal
-= off
- key
.offset
;
2436 ret
= btrfs_drop_extents(trans
, inode
,
2438 new_key
.offset
+ datal
,
2442 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2446 leaf
= path
->nodes
[0];
2447 slot
= path
->slots
[0];
2448 write_extent_buffer(leaf
, buf
,
2449 btrfs_item_ptr_offset(leaf
, slot
),
2452 extent
= btrfs_item_ptr(leaf
, slot
,
2453 struct btrfs_file_extent_item
);
2455 /* disko == 0 means it's a hole */
2459 btrfs_set_file_extent_offset(leaf
, extent
,
2461 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2464 inode_add_bytes(inode
, datal
);
2465 ret
= btrfs_inc_extent_ref(trans
, root
,
2467 root
->root_key
.objectid
,
2469 new_key
.offset
- datao
,
2473 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2476 if (off
> key
.offset
) {
2477 skip
= off
- key
.offset
;
2478 new_key
.offset
+= skip
;
2481 if (key
.offset
+ datal
> off
+len
)
2482 trim
= key
.offset
+ datal
- (off
+len
);
2484 if (comp
&& (skip
|| trim
)) {
2486 btrfs_end_transaction(trans
, root
);
2489 size
-= skip
+ trim
;
2490 datal
-= skip
+ trim
;
2492 ret
= btrfs_drop_extents(trans
, inode
,
2494 new_key
.offset
+ datal
,
2498 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2504 btrfs_file_extent_calc_inline_size(0);
2505 memmove(buf
+start
, buf
+start
+skip
,
2509 leaf
= path
->nodes
[0];
2510 slot
= path
->slots
[0];
2511 write_extent_buffer(leaf
, buf
,
2512 btrfs_item_ptr_offset(leaf
, slot
),
2514 inode_add_bytes(inode
, datal
);
2517 btrfs_mark_buffer_dirty(leaf
);
2518 btrfs_release_path(path
);
2520 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2523 * we round up to the block size at eof when
2524 * determining which extents to clone above,
2525 * but shouldn't round up the file size
2527 endoff
= new_key
.offset
+ datal
;
2528 if (endoff
> destoff
+olen
)
2529 endoff
= destoff
+olen
;
2530 if (endoff
> inode
->i_size
)
2531 btrfs_i_size_write(inode
, endoff
);
2533 ret
= btrfs_update_inode(trans
, root
, inode
);
2535 btrfs_end_transaction(trans
, root
);
2538 btrfs_release_path(path
);
2543 btrfs_release_path(path
);
2544 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
2546 mutex_unlock(&src
->i_mutex
);
2547 mutex_unlock(&inode
->i_mutex
);
2549 btrfs_free_path(path
);
2553 mnt_drop_write(file
->f_path
.mnt
);
2557 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2559 struct btrfs_ioctl_clone_range_args args
;
2561 if (copy_from_user(&args
, argp
, sizeof(args
)))
2563 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2564 args
.src_length
, args
.dest_offset
);
2568 * there are many ways the trans_start and trans_end ioctls can lead
2569 * to deadlocks. They should only be used by applications that
2570 * basically own the machine, and have a very in depth understanding
2571 * of all the possible deadlocks and enospc problems.
2573 static long btrfs_ioctl_trans_start(struct file
*file
)
2575 struct inode
*inode
= fdentry(file
)->d_inode
;
2576 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2577 struct btrfs_trans_handle
*trans
;
2581 if (!capable(CAP_SYS_ADMIN
))
2585 if (file
->private_data
)
2589 if (btrfs_root_readonly(root
))
2592 ret
= mnt_want_write(file
->f_path
.mnt
);
2596 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
2599 trans
= btrfs_start_ioctl_transaction(root
);
2603 file
->private_data
= trans
;
2607 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2608 mnt_drop_write(file
->f_path
.mnt
);
2613 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2615 struct inode
*inode
= fdentry(file
)->d_inode
;
2616 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2617 struct btrfs_root
*new_root
;
2618 struct btrfs_dir_item
*di
;
2619 struct btrfs_trans_handle
*trans
;
2620 struct btrfs_path
*path
;
2621 struct btrfs_key location
;
2622 struct btrfs_disk_key disk_key
;
2623 struct btrfs_super_block
*disk_super
;
2628 if (!capable(CAP_SYS_ADMIN
))
2631 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2635 objectid
= root
->root_key
.objectid
;
2637 location
.objectid
= objectid
;
2638 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2639 location
.offset
= (u64
)-1;
2641 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2642 if (IS_ERR(new_root
))
2643 return PTR_ERR(new_root
);
2645 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2648 path
= btrfs_alloc_path();
2651 path
->leave_spinning
= 1;
2653 trans
= btrfs_start_transaction(root
, 1);
2654 if (IS_ERR(trans
)) {
2655 btrfs_free_path(path
);
2656 return PTR_ERR(trans
);
2659 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
2660 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2661 dir_id
, "default", 7, 1);
2662 if (IS_ERR_OR_NULL(di
)) {
2663 btrfs_free_path(path
);
2664 btrfs_end_transaction(trans
, root
);
2665 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2666 "this isn't going to work\n");
2670 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2671 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2672 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2673 btrfs_free_path(path
);
2675 disk_super
= root
->fs_info
->super_copy
;
2676 features
= btrfs_super_incompat_flags(disk_super
);
2677 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2678 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2679 btrfs_set_super_incompat_flags(disk_super
, features
);
2681 btrfs_end_transaction(trans
, root
);
2686 static void get_block_group_info(struct list_head
*groups_list
,
2687 struct btrfs_ioctl_space_info
*space
)
2689 struct btrfs_block_group_cache
*block_group
;
2691 space
->total_bytes
= 0;
2692 space
->used_bytes
= 0;
2694 list_for_each_entry(block_group
, groups_list
, list
) {
2695 space
->flags
= block_group
->flags
;
2696 space
->total_bytes
+= block_group
->key
.offset
;
2697 space
->used_bytes
+=
2698 btrfs_block_group_used(&block_group
->item
);
2702 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2704 struct btrfs_ioctl_space_args space_args
;
2705 struct btrfs_ioctl_space_info space
;
2706 struct btrfs_ioctl_space_info
*dest
;
2707 struct btrfs_ioctl_space_info
*dest_orig
;
2708 struct btrfs_ioctl_space_info __user
*user_dest
;
2709 struct btrfs_space_info
*info
;
2710 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2711 BTRFS_BLOCK_GROUP_SYSTEM
,
2712 BTRFS_BLOCK_GROUP_METADATA
,
2713 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2720 if (copy_from_user(&space_args
,
2721 (struct btrfs_ioctl_space_args __user
*)arg
,
2722 sizeof(space_args
)))
2725 for (i
= 0; i
< num_types
; i
++) {
2726 struct btrfs_space_info
*tmp
;
2730 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2732 if (tmp
->flags
== types
[i
]) {
2742 down_read(&info
->groups_sem
);
2743 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2744 if (!list_empty(&info
->block_groups
[c
]))
2747 up_read(&info
->groups_sem
);
2750 /* space_slots == 0 means they are asking for a count */
2751 if (space_args
.space_slots
== 0) {
2752 space_args
.total_spaces
= slot_count
;
2756 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2758 alloc_size
= sizeof(*dest
) * slot_count
;
2760 /* we generally have at most 6 or so space infos, one for each raid
2761 * level. So, a whole page should be more than enough for everyone
2763 if (alloc_size
> PAGE_CACHE_SIZE
)
2766 space_args
.total_spaces
= 0;
2767 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2772 /* now we have a buffer to copy into */
2773 for (i
= 0; i
< num_types
; i
++) {
2774 struct btrfs_space_info
*tmp
;
2781 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2783 if (tmp
->flags
== types
[i
]) {
2792 down_read(&info
->groups_sem
);
2793 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2794 if (!list_empty(&info
->block_groups
[c
])) {
2795 get_block_group_info(&info
->block_groups
[c
],
2797 memcpy(dest
, &space
, sizeof(space
));
2799 space_args
.total_spaces
++;
2805 up_read(&info
->groups_sem
);
2808 user_dest
= (struct btrfs_ioctl_space_info
*)
2809 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2811 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2816 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2823 * there are many ways the trans_start and trans_end ioctls can lead
2824 * to deadlocks. They should only be used by applications that
2825 * basically own the machine, and have a very in depth understanding
2826 * of all the possible deadlocks and enospc problems.
2828 long btrfs_ioctl_trans_end(struct file
*file
)
2830 struct inode
*inode
= fdentry(file
)->d_inode
;
2831 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2832 struct btrfs_trans_handle
*trans
;
2834 trans
= file
->private_data
;
2837 file
->private_data
= NULL
;
2839 btrfs_end_transaction(trans
, root
);
2841 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2843 mnt_drop_write(file
->f_path
.mnt
);
2847 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2849 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2850 struct btrfs_trans_handle
*trans
;
2854 trans
= btrfs_start_transaction(root
, 0);
2856 return PTR_ERR(trans
);
2857 transid
= trans
->transid
;
2858 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
2860 btrfs_end_transaction(trans
, root
);
2865 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2870 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2872 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2876 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2879 transid
= 0; /* current trans */
2881 return btrfs_wait_for_commit(root
, transid
);
2884 static long btrfs_ioctl_scrub(struct btrfs_root
*root
, void __user
*arg
)
2887 struct btrfs_ioctl_scrub_args
*sa
;
2889 if (!capable(CAP_SYS_ADMIN
))
2892 sa
= memdup_user(arg
, sizeof(*sa
));
2896 ret
= btrfs_scrub_dev(root
, sa
->devid
, sa
->start
, sa
->end
,
2897 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
);
2899 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2906 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
2908 if (!capable(CAP_SYS_ADMIN
))
2911 return btrfs_scrub_cancel(root
);
2914 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
2917 struct btrfs_ioctl_scrub_args
*sa
;
2920 if (!capable(CAP_SYS_ADMIN
))
2923 sa
= memdup_user(arg
, sizeof(*sa
));
2927 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
2929 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
2936 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
2942 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
2943 struct inode_fs_paths
*ipath
= NULL
;
2944 struct btrfs_path
*path
;
2946 if (!capable(CAP_SYS_ADMIN
))
2949 path
= btrfs_alloc_path();
2955 ipa
= memdup_user(arg
, sizeof(*ipa
));
2962 size
= min_t(u32
, ipa
->size
, 4096);
2963 ipath
= init_ipath(size
, root
, path
);
2964 if (IS_ERR(ipath
)) {
2965 ret
= PTR_ERR(ipath
);
2970 ret
= paths_from_inode(ipa
->inum
, ipath
);
2974 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
2975 rel_ptr
= ipath
->fspath
->val
[i
] -
2976 (u64
)(unsigned long)ipath
->fspath
->val
;
2977 ipath
->fspath
->val
[i
] = rel_ptr
;
2980 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
2981 (void *)(unsigned long)ipath
->fspath
, size
);
2988 btrfs_free_path(path
);
2995 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
2997 struct btrfs_data_container
*inodes
= ctx
;
2998 const size_t c
= 3 * sizeof(u64
);
3000 if (inodes
->bytes_left
>= c
) {
3001 inodes
->bytes_left
-= c
;
3002 inodes
->val
[inodes
->elem_cnt
] = inum
;
3003 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3004 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3005 inodes
->elem_cnt
+= 3;
3007 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3008 inodes
->bytes_left
= 0;
3009 inodes
->elem_missed
+= 3;
3015 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3020 u64 extent_item_pos
;
3021 struct btrfs_ioctl_logical_ino_args
*loi
;
3022 struct btrfs_data_container
*inodes
= NULL
;
3023 struct btrfs_path
*path
= NULL
;
3024 struct btrfs_key key
;
3026 if (!capable(CAP_SYS_ADMIN
))
3029 loi
= memdup_user(arg
, sizeof(*loi
));
3036 path
= btrfs_alloc_path();
3042 size
= min_t(u32
, loi
->size
, 4096);
3043 inodes
= init_data_container(size
);
3044 if (IS_ERR(inodes
)) {
3045 ret
= PTR_ERR(inodes
);
3050 ret
= extent_from_logical(root
->fs_info
, loi
->logical
, path
, &key
);
3051 btrfs_release_path(path
);
3053 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
3058 extent_item_pos
= loi
->logical
- key
.objectid
;
3059 ret
= iterate_extent_inodes(root
->fs_info
, path
, key
.objectid
,
3060 extent_item_pos
, build_ino_list
,
3066 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3067 (void *)(unsigned long)inodes
, size
);
3072 btrfs_free_path(path
);
3079 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3080 struct btrfs_ioctl_balance_args
*bargs
)
3082 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3084 bargs
->flags
= bctl
->flags
;
3086 if (atomic_read(&fs_info
->balance_running
))
3087 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3088 if (atomic_read(&fs_info
->balance_pause_req
))
3089 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3090 if (atomic_read(&fs_info
->balance_cancel_req
))
3091 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3093 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3094 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3095 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3098 spin_lock(&fs_info
->balance_lock
);
3099 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3100 spin_unlock(&fs_info
->balance_lock
);
3102 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3106 static long btrfs_ioctl_balance(struct btrfs_root
*root
, void __user
*arg
)
3108 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3109 struct btrfs_ioctl_balance_args
*bargs
;
3110 struct btrfs_balance_control
*bctl
;
3113 if (!capable(CAP_SYS_ADMIN
))
3116 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
3119 mutex_lock(&fs_info
->volume_mutex
);
3120 mutex_lock(&fs_info
->balance_mutex
);
3123 bargs
= memdup_user(arg
, sizeof(*bargs
));
3124 if (IS_ERR(bargs
)) {
3125 ret
= PTR_ERR(bargs
);
3129 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3130 if (!fs_info
->balance_ctl
) {
3135 bctl
= fs_info
->balance_ctl
;
3136 spin_lock(&fs_info
->balance_lock
);
3137 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3138 spin_unlock(&fs_info
->balance_lock
);
3146 if (fs_info
->balance_ctl
) {
3151 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3157 bctl
->fs_info
= fs_info
;
3159 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
3160 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
3161 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
3163 bctl
->flags
= bargs
->flags
;
3165 /* balance everything - no filters */
3166 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
3170 ret
= btrfs_balance(bctl
, bargs
);
3172 * bctl is freed in __cancel_balance or in free_fs_info if
3173 * restriper was paused all the way until unmount
3176 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3183 mutex_unlock(&fs_info
->balance_mutex
);
3184 mutex_unlock(&fs_info
->volume_mutex
);
3188 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
3190 if (!capable(CAP_SYS_ADMIN
))
3194 case BTRFS_BALANCE_CTL_PAUSE
:
3195 return btrfs_pause_balance(root
->fs_info
);
3196 case BTRFS_BALANCE_CTL_CANCEL
:
3197 return btrfs_cancel_balance(root
->fs_info
);
3203 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
3206 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3207 struct btrfs_ioctl_balance_args
*bargs
;
3210 if (!capable(CAP_SYS_ADMIN
))
3213 mutex_lock(&fs_info
->balance_mutex
);
3214 if (!fs_info
->balance_ctl
) {
3219 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
3225 update_ioctl_balance_args(fs_info
, 1, bargs
);
3227 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3232 mutex_unlock(&fs_info
->balance_mutex
);
3236 long btrfs_ioctl(struct file
*file
, unsigned int
3237 cmd
, unsigned long arg
)
3239 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3240 void __user
*argp
= (void __user
*)arg
;
3243 case FS_IOC_GETFLAGS
:
3244 return btrfs_ioctl_getflags(file
, argp
);
3245 case FS_IOC_SETFLAGS
:
3246 return btrfs_ioctl_setflags(file
, argp
);
3247 case FS_IOC_GETVERSION
:
3248 return btrfs_ioctl_getversion(file
, argp
);
3250 return btrfs_ioctl_fitrim(file
, argp
);
3251 case BTRFS_IOC_SNAP_CREATE
:
3252 return btrfs_ioctl_snap_create(file
, argp
, 0);
3253 case BTRFS_IOC_SNAP_CREATE_V2
:
3254 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
3255 case BTRFS_IOC_SUBVOL_CREATE
:
3256 return btrfs_ioctl_snap_create(file
, argp
, 1);
3257 case BTRFS_IOC_SNAP_DESTROY
:
3258 return btrfs_ioctl_snap_destroy(file
, argp
);
3259 case BTRFS_IOC_SUBVOL_GETFLAGS
:
3260 return btrfs_ioctl_subvol_getflags(file
, argp
);
3261 case BTRFS_IOC_SUBVOL_SETFLAGS
:
3262 return btrfs_ioctl_subvol_setflags(file
, argp
);
3263 case BTRFS_IOC_DEFAULT_SUBVOL
:
3264 return btrfs_ioctl_default_subvol(file
, argp
);
3265 case BTRFS_IOC_DEFRAG
:
3266 return btrfs_ioctl_defrag(file
, NULL
);
3267 case BTRFS_IOC_DEFRAG_RANGE
:
3268 return btrfs_ioctl_defrag(file
, argp
);
3269 case BTRFS_IOC_RESIZE
:
3270 return btrfs_ioctl_resize(root
, argp
);
3271 case BTRFS_IOC_ADD_DEV
:
3272 return btrfs_ioctl_add_dev(root
, argp
);
3273 case BTRFS_IOC_RM_DEV
:
3274 return btrfs_ioctl_rm_dev(root
, argp
);
3275 case BTRFS_IOC_FS_INFO
:
3276 return btrfs_ioctl_fs_info(root
, argp
);
3277 case BTRFS_IOC_DEV_INFO
:
3278 return btrfs_ioctl_dev_info(root
, argp
);
3279 case BTRFS_IOC_BALANCE
:
3280 return btrfs_ioctl_balance(root
, NULL
);
3281 case BTRFS_IOC_CLONE
:
3282 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
3283 case BTRFS_IOC_CLONE_RANGE
:
3284 return btrfs_ioctl_clone_range(file
, argp
);
3285 case BTRFS_IOC_TRANS_START
:
3286 return btrfs_ioctl_trans_start(file
);
3287 case BTRFS_IOC_TRANS_END
:
3288 return btrfs_ioctl_trans_end(file
);
3289 case BTRFS_IOC_TREE_SEARCH
:
3290 return btrfs_ioctl_tree_search(file
, argp
);
3291 case BTRFS_IOC_INO_LOOKUP
:
3292 return btrfs_ioctl_ino_lookup(file
, argp
);
3293 case BTRFS_IOC_INO_PATHS
:
3294 return btrfs_ioctl_ino_to_path(root
, argp
);
3295 case BTRFS_IOC_LOGICAL_INO
:
3296 return btrfs_ioctl_logical_to_ino(root
, argp
);
3297 case BTRFS_IOC_SPACE_INFO
:
3298 return btrfs_ioctl_space_info(root
, argp
);
3299 case BTRFS_IOC_SYNC
:
3300 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
3302 case BTRFS_IOC_START_SYNC
:
3303 return btrfs_ioctl_start_sync(file
, argp
);
3304 case BTRFS_IOC_WAIT_SYNC
:
3305 return btrfs_ioctl_wait_sync(file
, argp
);
3306 case BTRFS_IOC_SCRUB
:
3307 return btrfs_ioctl_scrub(root
, argp
);
3308 case BTRFS_IOC_SCRUB_CANCEL
:
3309 return btrfs_ioctl_scrub_cancel(root
, argp
);
3310 case BTRFS_IOC_SCRUB_PROGRESS
:
3311 return btrfs_ioctl_scrub_progress(root
, argp
);
3312 case BTRFS_IOC_BALANCE_V2
:
3313 return btrfs_ioctl_balance(root
, argp
);
3314 case BTRFS_IOC_BALANCE_CTL
:
3315 return btrfs_ioctl_balance_ctl(root
, arg
);
3316 case BTRFS_IOC_BALANCE_PROGRESS
:
3317 return btrfs_ioctl_balance_progress(root
, argp
);