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>
44 #include <linux/uuid.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
51 #include "print-tree.h"
54 #include "inode-map.h"
56 #include "rcu-string.h"
59 /* Mask out flags that are inappropriate for the given type of inode. */
60 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
64 else if (S_ISREG(mode
))
65 return flags
& ~FS_DIRSYNC_FL
;
67 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
71 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
73 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
75 unsigned int iflags
= 0;
77 if (flags
& BTRFS_INODE_SYNC
)
79 if (flags
& BTRFS_INODE_IMMUTABLE
)
80 iflags
|= FS_IMMUTABLE_FL
;
81 if (flags
& BTRFS_INODE_APPEND
)
82 iflags
|= FS_APPEND_FL
;
83 if (flags
& BTRFS_INODE_NODUMP
)
84 iflags
|= FS_NODUMP_FL
;
85 if (flags
& BTRFS_INODE_NOATIME
)
86 iflags
|= FS_NOATIME_FL
;
87 if (flags
& BTRFS_INODE_DIRSYNC
)
88 iflags
|= FS_DIRSYNC_FL
;
89 if (flags
& BTRFS_INODE_NODATACOW
)
90 iflags
|= FS_NOCOW_FL
;
92 if ((flags
& BTRFS_INODE_COMPRESS
) && !(flags
& BTRFS_INODE_NOCOMPRESS
))
93 iflags
|= FS_COMPR_FL
;
94 else if (flags
& BTRFS_INODE_NOCOMPRESS
)
95 iflags
|= FS_NOCOMP_FL
;
101 * Update inode->i_flags based on the btrfs internal flags.
103 void btrfs_update_iflags(struct inode
*inode
)
105 struct btrfs_inode
*ip
= BTRFS_I(inode
);
107 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
109 if (ip
->flags
& BTRFS_INODE_SYNC
)
110 inode
->i_flags
|= S_SYNC
;
111 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
112 inode
->i_flags
|= S_IMMUTABLE
;
113 if (ip
->flags
& BTRFS_INODE_APPEND
)
114 inode
->i_flags
|= S_APPEND
;
115 if (ip
->flags
& BTRFS_INODE_NOATIME
)
116 inode
->i_flags
|= S_NOATIME
;
117 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
118 inode
->i_flags
|= S_DIRSYNC
;
122 * Inherit flags from the parent inode.
124 * Currently only the compression flags and the cow flags are inherited.
126 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
133 flags
= BTRFS_I(dir
)->flags
;
135 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
136 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
137 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
138 } else if (flags
& BTRFS_INODE_COMPRESS
) {
139 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
140 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
143 if (flags
& BTRFS_INODE_NODATACOW
)
144 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
146 btrfs_update_iflags(inode
);
149 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
151 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
152 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
154 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
159 static int check_flags(unsigned int flags
)
161 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
162 FS_NOATIME_FL
| FS_NODUMP_FL
| \
163 FS_SYNC_FL
| FS_DIRSYNC_FL
| \
164 FS_NOCOMP_FL
| FS_COMPR_FL
|
168 if ((flags
& FS_NOCOMP_FL
) && (flags
& FS_COMPR_FL
))
174 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
176 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
177 struct btrfs_inode
*ip
= BTRFS_I(inode
);
178 struct btrfs_root
*root
= ip
->root
;
179 struct btrfs_trans_handle
*trans
;
180 unsigned int flags
, oldflags
;
183 unsigned int i_oldflags
;
186 if (btrfs_root_readonly(root
))
189 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
192 ret
= check_flags(flags
);
196 if (!inode_owner_or_capable(inode
))
199 ret
= mnt_want_write_file(file
);
203 mutex_lock(&inode
->i_mutex
);
205 ip_oldflags
= ip
->flags
;
206 i_oldflags
= inode
->i_flags
;
207 mode
= inode
->i_mode
;
209 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
210 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
211 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
212 if (!capable(CAP_LINUX_IMMUTABLE
)) {
218 if (flags
& FS_SYNC_FL
)
219 ip
->flags
|= BTRFS_INODE_SYNC
;
221 ip
->flags
&= ~BTRFS_INODE_SYNC
;
222 if (flags
& FS_IMMUTABLE_FL
)
223 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
225 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
226 if (flags
& FS_APPEND_FL
)
227 ip
->flags
|= BTRFS_INODE_APPEND
;
229 ip
->flags
&= ~BTRFS_INODE_APPEND
;
230 if (flags
& FS_NODUMP_FL
)
231 ip
->flags
|= BTRFS_INODE_NODUMP
;
233 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
234 if (flags
& FS_NOATIME_FL
)
235 ip
->flags
|= BTRFS_INODE_NOATIME
;
237 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
238 if (flags
& FS_DIRSYNC_FL
)
239 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
241 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
242 if (flags
& FS_NOCOW_FL
) {
245 * It's safe to turn csums off here, no extents exist.
246 * Otherwise we want the flag to reflect the real COW
247 * status of the file and will not set it.
249 if (inode
->i_size
== 0)
250 ip
->flags
|= BTRFS_INODE_NODATACOW
251 | BTRFS_INODE_NODATASUM
;
253 ip
->flags
|= BTRFS_INODE_NODATACOW
;
257 * Revert back under same assuptions as above
260 if (inode
->i_size
== 0)
261 ip
->flags
&= ~(BTRFS_INODE_NODATACOW
262 | BTRFS_INODE_NODATASUM
);
264 ip
->flags
&= ~BTRFS_INODE_NODATACOW
;
269 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
270 * flag may be changed automatically if compression code won't make
273 if (flags
& FS_NOCOMP_FL
) {
274 ip
->flags
&= ~BTRFS_INODE_COMPRESS
;
275 ip
->flags
|= BTRFS_INODE_NOCOMPRESS
;
276 } else if (flags
& FS_COMPR_FL
) {
277 ip
->flags
|= BTRFS_INODE_COMPRESS
;
278 ip
->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
280 ip
->flags
&= ~(BTRFS_INODE_COMPRESS
| BTRFS_INODE_NOCOMPRESS
);
283 trans
= btrfs_start_transaction(root
, 1);
285 ret
= PTR_ERR(trans
);
289 btrfs_update_iflags(inode
);
290 inode_inc_iversion(inode
);
291 inode
->i_ctime
= CURRENT_TIME
;
292 ret
= btrfs_update_inode(trans
, root
, inode
);
294 btrfs_end_transaction(trans
, root
);
297 ip
->flags
= ip_oldflags
;
298 inode
->i_flags
= i_oldflags
;
302 mutex_unlock(&inode
->i_mutex
);
303 mnt_drop_write_file(file
);
307 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
309 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
311 return put_user(inode
->i_generation
, arg
);
314 static noinline
int btrfs_ioctl_fitrim(struct file
*file
, void __user
*arg
)
316 struct btrfs_fs_info
*fs_info
= btrfs_sb(fdentry(file
)->d_sb
);
317 struct btrfs_device
*device
;
318 struct request_queue
*q
;
319 struct fstrim_range range
;
320 u64 minlen
= ULLONG_MAX
;
322 u64 total_bytes
= btrfs_super_total_bytes(fs_info
->super_copy
);
325 if (!capable(CAP_SYS_ADMIN
))
329 list_for_each_entry_rcu(device
, &fs_info
->fs_devices
->devices
,
333 q
= bdev_get_queue(device
->bdev
);
334 if (blk_queue_discard(q
)) {
336 minlen
= min((u64
)q
->limits
.discard_granularity
,
344 if (copy_from_user(&range
, arg
, sizeof(range
)))
346 if (range
.start
> total_bytes
||
347 range
.len
< fs_info
->sb
->s_blocksize
)
350 range
.len
= min(range
.len
, total_bytes
- range
.start
);
351 range
.minlen
= max(range
.minlen
, minlen
);
352 ret
= btrfs_trim_fs(fs_info
->tree_root
, &range
);
356 if (copy_to_user(arg
, &range
, sizeof(range
)))
362 static noinline
int create_subvol(struct btrfs_root
*root
,
363 struct dentry
*dentry
,
364 char *name
, int namelen
,
366 struct btrfs_qgroup_inherit
**inherit
)
368 struct btrfs_trans_handle
*trans
;
369 struct btrfs_key key
;
370 struct btrfs_root_item root_item
;
371 struct btrfs_inode_item
*inode_item
;
372 struct extent_buffer
*leaf
;
373 struct btrfs_root
*new_root
;
374 struct dentry
*parent
= dentry
->d_parent
;
376 struct timespec cur_time
= CURRENT_TIME
;
380 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
384 ret
= btrfs_find_free_objectid(root
->fs_info
->tree_root
, &objectid
);
388 dir
= parent
->d_inode
;
396 trans
= btrfs_start_transaction(root
, 6);
398 return PTR_ERR(trans
);
400 ret
= btrfs_qgroup_inherit(trans
, root
->fs_info
, 0, objectid
,
401 inherit
? *inherit
: NULL
);
405 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
406 0, objectid
, NULL
, 0, 0, 0);
412 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
413 btrfs_set_header_bytenr(leaf
, leaf
->start
);
414 btrfs_set_header_generation(leaf
, trans
->transid
);
415 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
416 btrfs_set_header_owner(leaf
, objectid
);
418 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
419 (unsigned long)btrfs_header_fsid(leaf
),
421 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
422 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
424 btrfs_mark_buffer_dirty(leaf
);
426 memset(&root_item
, 0, sizeof(root_item
));
428 inode_item
= &root_item
.inode
;
429 inode_item
->generation
= cpu_to_le64(1);
430 inode_item
->size
= cpu_to_le64(3);
431 inode_item
->nlink
= cpu_to_le32(1);
432 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
433 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
436 root_item
.byte_limit
= 0;
437 inode_item
->flags
= cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT
);
439 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
440 btrfs_set_root_generation(&root_item
, trans
->transid
);
441 btrfs_set_root_level(&root_item
, 0);
442 btrfs_set_root_refs(&root_item
, 1);
443 btrfs_set_root_used(&root_item
, leaf
->len
);
444 btrfs_set_root_last_snapshot(&root_item
, 0);
446 btrfs_set_root_generation_v2(&root_item
,
447 btrfs_root_generation(&root_item
));
448 uuid_le_gen(&new_uuid
);
449 memcpy(root_item
.uuid
, new_uuid
.b
, BTRFS_UUID_SIZE
);
450 root_item
.otime
.sec
= cpu_to_le64(cur_time
.tv_sec
);
451 root_item
.otime
.nsec
= cpu_to_le32(cur_time
.tv_nsec
);
452 root_item
.ctime
= root_item
.otime
;
453 btrfs_set_root_ctransid(&root_item
, trans
->transid
);
454 btrfs_set_root_otransid(&root_item
, trans
->transid
);
456 btrfs_tree_unlock(leaf
);
457 free_extent_buffer(leaf
);
460 btrfs_set_root_dirid(&root_item
, new_dirid
);
462 key
.objectid
= objectid
;
464 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
465 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
470 key
.offset
= (u64
)-1;
471 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
472 if (IS_ERR(new_root
)) {
473 btrfs_abort_transaction(trans
, root
, PTR_ERR(new_root
));
474 ret
= PTR_ERR(new_root
);
478 btrfs_record_root_in_trans(trans
, new_root
);
480 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
);
482 /* We potentially lose an unused inode item here */
483 btrfs_abort_transaction(trans
, root
, ret
);
488 * insert the directory item
490 ret
= btrfs_set_inode_index(dir
, &index
);
492 btrfs_abort_transaction(trans
, root
, ret
);
496 ret
= btrfs_insert_dir_item(trans
, root
,
497 name
, namelen
, dir
, &key
,
498 BTRFS_FT_DIR
, index
);
500 btrfs_abort_transaction(trans
, root
, ret
);
504 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
505 ret
= btrfs_update_inode(trans
, root
, dir
);
508 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
509 objectid
, root
->root_key
.objectid
,
510 btrfs_ino(dir
), index
, name
, namelen
);
514 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
517 *async_transid
= trans
->transid
;
518 err
= btrfs_commit_transaction_async(trans
, root
, 1);
520 err
= btrfs_commit_transaction(trans
, root
);
527 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
528 char *name
, int namelen
, u64
*async_transid
,
529 bool readonly
, struct btrfs_qgroup_inherit
**inherit
)
532 struct btrfs_pending_snapshot
*pending_snapshot
;
533 struct btrfs_trans_handle
*trans
;
539 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
540 if (!pending_snapshot
)
543 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
,
544 BTRFS_BLOCK_RSV_TEMP
);
545 pending_snapshot
->dentry
= dentry
;
546 pending_snapshot
->root
= root
;
547 pending_snapshot
->readonly
= readonly
;
549 pending_snapshot
->inherit
= *inherit
;
550 *inherit
= NULL
; /* take responsibility to free it */
553 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 6);
555 ret
= PTR_ERR(trans
);
559 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
562 spin_lock(&root
->fs_info
->trans_lock
);
563 list_add(&pending_snapshot
->list
,
564 &trans
->transaction
->pending_snapshots
);
565 spin_unlock(&root
->fs_info
->trans_lock
);
567 *async_transid
= trans
->transid
;
568 ret
= btrfs_commit_transaction_async(trans
,
569 root
->fs_info
->extent_root
, 1);
571 ret
= btrfs_commit_transaction(trans
,
572 root
->fs_info
->extent_root
);
575 /* cleanup_transaction has freed this for us */
577 pending_snapshot
= NULL
;
581 ret
= pending_snapshot
->error
;
585 ret
= btrfs_orphan_cleanup(pending_snapshot
->snap
);
589 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
591 ret
= PTR_ERR(inode
);
595 d_instantiate(dentry
, inode
);
598 kfree(pending_snapshot
);
602 /* copy of check_sticky in fs/namei.c()
603 * It's inline, so penalty for filesystems that don't use sticky bit is
606 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
608 kuid_t fsuid
= current_fsuid();
610 if (!(dir
->i_mode
& S_ISVTX
))
612 if (uid_eq(inode
->i_uid
, fsuid
))
614 if (uid_eq(dir
->i_uid
, fsuid
))
616 return !capable(CAP_FOWNER
);
619 /* copy of may_delete in fs/namei.c()
620 * Check whether we can remove a link victim from directory dir, check
621 * whether the type of victim is right.
622 * 1. We can't do it if dir is read-only (done in permission())
623 * 2. We should have write and exec permissions on dir
624 * 3. We can't remove anything from append-only dir
625 * 4. We can't do anything with immutable dir (done in permission())
626 * 5. If the sticky bit on dir is set we should either
627 * a. be owner of dir, or
628 * b. be owner of victim, or
629 * c. have CAP_FOWNER capability
630 * 6. If the victim is append-only or immutable we can't do antyhing with
631 * links pointing to it.
632 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
633 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
634 * 9. We can't remove a root or mountpoint.
635 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
636 * nfs_async_unlink().
639 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
643 if (!victim
->d_inode
)
646 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
647 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
649 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
654 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
655 IS_APPEND(victim
->d_inode
)||
656 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
659 if (!S_ISDIR(victim
->d_inode
->i_mode
))
663 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
667 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
672 /* copy of may_create in fs/namei.c() */
673 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
679 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
683 * Create a new subvolume below @parent. This is largely modeled after
684 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
685 * inside this filesystem so it's quite a bit simpler.
687 static noinline
int btrfs_mksubvol(struct path
*parent
,
688 char *name
, int namelen
,
689 struct btrfs_root
*snap_src
,
690 u64
*async_transid
, bool readonly
,
691 struct btrfs_qgroup_inherit
**inherit
)
693 struct inode
*dir
= parent
->dentry
->d_inode
;
694 struct dentry
*dentry
;
697 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
699 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
700 error
= PTR_ERR(dentry
);
708 error
= btrfs_may_create(dir
, dentry
);
712 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
714 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
718 error
= create_snapshot(snap_src
, dentry
, name
, namelen
,
719 async_transid
, readonly
, inherit
);
721 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
722 name
, namelen
, async_transid
, inherit
);
725 fsnotify_mkdir(dir
, dentry
);
727 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
731 mutex_unlock(&dir
->i_mutex
);
736 * When we're defragging a range, we don't want to kick it off again
737 * if it is really just waiting for delalloc to send it down.
738 * If we find a nice big extent or delalloc range for the bytes in the
739 * file you want to defrag, we return 0 to let you know to skip this
742 static int check_defrag_in_cache(struct inode
*inode
, u64 offset
, int thresh
)
744 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
745 struct extent_map
*em
= NULL
;
746 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
749 read_lock(&em_tree
->lock
);
750 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
751 read_unlock(&em_tree
->lock
);
754 end
= extent_map_end(em
);
756 if (end
- offset
> thresh
)
759 /* if we already have a nice delalloc here, just stop */
761 end
= count_range_bits(io_tree
, &offset
, offset
+ thresh
,
762 thresh
, EXTENT_DELALLOC
, 1);
769 * helper function to walk through a file and find extents
770 * newer than a specific transid, and smaller than thresh.
772 * This is used by the defragging code to find new and small
775 static int find_new_extents(struct btrfs_root
*root
,
776 struct inode
*inode
, u64 newer_than
,
777 u64
*off
, int thresh
)
779 struct btrfs_path
*path
;
780 struct btrfs_key min_key
;
781 struct btrfs_key max_key
;
782 struct extent_buffer
*leaf
;
783 struct btrfs_file_extent_item
*extent
;
786 u64 ino
= btrfs_ino(inode
);
788 path
= btrfs_alloc_path();
792 min_key
.objectid
= ino
;
793 min_key
.type
= BTRFS_EXTENT_DATA_KEY
;
794 min_key
.offset
= *off
;
796 max_key
.objectid
= ino
;
797 max_key
.type
= (u8
)-1;
798 max_key
.offset
= (u64
)-1;
800 path
->keep_locks
= 1;
803 ret
= btrfs_search_forward(root
, &min_key
, &max_key
,
804 path
, 0, newer_than
);
807 if (min_key
.objectid
!= ino
)
809 if (min_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
812 leaf
= path
->nodes
[0];
813 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
814 struct btrfs_file_extent_item
);
816 type
= btrfs_file_extent_type(leaf
, extent
);
817 if (type
== BTRFS_FILE_EXTENT_REG
&&
818 btrfs_file_extent_num_bytes(leaf
, extent
) < thresh
&&
819 check_defrag_in_cache(inode
, min_key
.offset
, thresh
)) {
820 *off
= min_key
.offset
;
821 btrfs_free_path(path
);
825 if (min_key
.offset
== (u64
)-1)
829 btrfs_release_path(path
);
832 btrfs_free_path(path
);
836 static struct extent_map
*defrag_lookup_extent(struct inode
*inode
, u64 start
)
838 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
839 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
840 struct extent_map
*em
;
841 u64 len
= PAGE_CACHE_SIZE
;
844 * hopefully we have this extent in the tree already, try without
845 * the full extent lock
847 read_lock(&em_tree
->lock
);
848 em
= lookup_extent_mapping(em_tree
, start
, len
);
849 read_unlock(&em_tree
->lock
);
852 /* get the big lock and read metadata off disk */
853 lock_extent(io_tree
, start
, start
+ len
- 1);
854 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
855 unlock_extent(io_tree
, start
, start
+ len
- 1);
864 static bool defrag_check_next_extent(struct inode
*inode
, struct extent_map
*em
)
866 struct extent_map
*next
;
869 /* this is the last extent */
870 if (em
->start
+ em
->len
>= i_size_read(inode
))
873 next
= defrag_lookup_extent(inode
, em
->start
+ em
->len
);
874 if (!next
|| next
->block_start
>= EXTENT_MAP_LAST_BYTE
)
877 free_extent_map(next
);
881 static int should_defrag_range(struct inode
*inode
, u64 start
, int thresh
,
882 u64
*last_len
, u64
*skip
, u64
*defrag_end
,
885 struct extent_map
*em
;
887 bool next_mergeable
= true;
890 * make sure that once we start defragging an extent, we keep on
893 if (start
< *defrag_end
)
898 em
= defrag_lookup_extent(inode
, start
);
902 /* this will cover holes, and inline extents */
903 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
908 next_mergeable
= defrag_check_next_extent(inode
, em
);
911 * we hit a real extent, if it is big or the next extent is not a
912 * real extent, don't bother defragging it
914 if (!compress
&& (*last_len
== 0 || *last_len
>= thresh
) &&
915 (em
->len
>= thresh
|| !next_mergeable
))
919 * last_len ends up being a counter of how many bytes we've defragged.
920 * every time we choose not to defrag an extent, we reset *last_len
921 * so that the next tiny extent will force a defrag.
923 * The end result of this is that tiny extents before a single big
924 * extent will force at least part of that big extent to be defragged.
927 *defrag_end
= extent_map_end(em
);
930 *skip
= extent_map_end(em
);
939 * it doesn't do much good to defrag one or two pages
940 * at a time. This pulls in a nice chunk of pages
943 * It also makes sure the delalloc code has enough
944 * dirty data to avoid making new small extents as part
947 * It's a good idea to start RA on this range
948 * before calling this.
950 static int cluster_pages_for_defrag(struct inode
*inode
,
952 unsigned long start_index
,
955 unsigned long file_end
;
956 u64 isize
= i_size_read(inode
);
963 struct btrfs_ordered_extent
*ordered
;
964 struct extent_state
*cached_state
= NULL
;
965 struct extent_io_tree
*tree
;
966 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
968 file_end
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
969 if (!isize
|| start_index
> file_end
)
972 page_cnt
= min_t(u64
, (u64
)num_pages
, (u64
)file_end
- start_index
+ 1);
974 ret
= btrfs_delalloc_reserve_space(inode
,
975 page_cnt
<< PAGE_CACHE_SHIFT
);
979 tree
= &BTRFS_I(inode
)->io_tree
;
981 /* step one, lock all the pages */
982 for (i
= 0; i
< page_cnt
; i
++) {
985 page
= find_or_create_page(inode
->i_mapping
,
986 start_index
+ i
, mask
);
990 page_start
= page_offset(page
);
991 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
993 lock_extent(tree
, page_start
, page_end
);
994 ordered
= btrfs_lookup_ordered_extent(inode
,
996 unlock_extent(tree
, page_start
, page_end
);
1001 btrfs_start_ordered_extent(inode
, ordered
, 1);
1002 btrfs_put_ordered_extent(ordered
);
1005 * we unlocked the page above, so we need check if
1006 * it was released or not.
1008 if (page
->mapping
!= inode
->i_mapping
) {
1010 page_cache_release(page
);
1015 if (!PageUptodate(page
)) {
1016 btrfs_readpage(NULL
, page
);
1018 if (!PageUptodate(page
)) {
1020 page_cache_release(page
);
1026 if (page
->mapping
!= inode
->i_mapping
) {
1028 page_cache_release(page
);
1038 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1042 * so now we have a nice long stream of locked
1043 * and up to date pages, lets wait on them
1045 for (i
= 0; i
< i_done
; i
++)
1046 wait_on_page_writeback(pages
[i
]);
1048 page_start
= page_offset(pages
[0]);
1049 page_end
= page_offset(pages
[i_done
- 1]) + PAGE_CACHE_SIZE
;
1051 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
1052 page_start
, page_end
- 1, 0, &cached_state
);
1053 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
,
1054 page_end
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
1055 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 0, 0,
1056 &cached_state
, GFP_NOFS
);
1058 if (i_done
!= page_cnt
) {
1059 spin_lock(&BTRFS_I(inode
)->lock
);
1060 BTRFS_I(inode
)->outstanding_extents
++;
1061 spin_unlock(&BTRFS_I(inode
)->lock
);
1062 btrfs_delalloc_release_space(inode
,
1063 (page_cnt
- i_done
) << PAGE_CACHE_SHIFT
);
1067 set_extent_defrag(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
- 1,
1068 &cached_state
, GFP_NOFS
);
1070 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1071 page_start
, page_end
- 1, &cached_state
,
1074 for (i
= 0; i
< i_done
; i
++) {
1075 clear_page_dirty_for_io(pages
[i
]);
1076 ClearPageChecked(pages
[i
]);
1077 set_page_extent_mapped(pages
[i
]);
1078 set_page_dirty(pages
[i
]);
1079 unlock_page(pages
[i
]);
1080 page_cache_release(pages
[i
]);
1084 for (i
= 0; i
< i_done
; i
++) {
1085 unlock_page(pages
[i
]);
1086 page_cache_release(pages
[i
]);
1088 btrfs_delalloc_release_space(inode
, page_cnt
<< PAGE_CACHE_SHIFT
);
1093 int btrfs_defrag_file(struct inode
*inode
, struct file
*file
,
1094 struct btrfs_ioctl_defrag_range_args
*range
,
1095 u64 newer_than
, unsigned long max_to_defrag
)
1097 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1098 struct file_ra_state
*ra
= NULL
;
1099 unsigned long last_index
;
1100 u64 isize
= i_size_read(inode
);
1104 u64 newer_off
= range
->start
;
1106 unsigned long ra_index
= 0;
1108 int defrag_count
= 0;
1109 int compress_type
= BTRFS_COMPRESS_ZLIB
;
1110 int extent_thresh
= range
->extent_thresh
;
1111 int max_cluster
= (256 * 1024) >> PAGE_CACHE_SHIFT
;
1112 int cluster
= max_cluster
;
1113 u64 new_align
= ~((u64
)128 * 1024 - 1);
1114 struct page
**pages
= NULL
;
1116 if (extent_thresh
== 0)
1117 extent_thresh
= 256 * 1024;
1119 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
) {
1120 if (range
->compress_type
> BTRFS_COMPRESS_TYPES
)
1122 if (range
->compress_type
)
1123 compress_type
= range
->compress_type
;
1130 * if we were not given a file, allocate a readahead
1134 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
1137 file_ra_state_init(ra
, inode
->i_mapping
);
1142 pages
= kmalloc(sizeof(struct page
*) * max_cluster
,
1149 /* find the last page to defrag */
1150 if (range
->start
+ range
->len
> range
->start
) {
1151 last_index
= min_t(u64
, isize
- 1,
1152 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
1154 last_index
= (isize
- 1) >> PAGE_CACHE_SHIFT
;
1158 ret
= find_new_extents(root
, inode
, newer_than
,
1159 &newer_off
, 64 * 1024);
1161 range
->start
= newer_off
;
1163 * we always align our defrag to help keep
1164 * the extents in the file evenly spaced
1166 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1170 i
= range
->start
>> PAGE_CACHE_SHIFT
;
1173 max_to_defrag
= last_index
+ 1;
1176 * make writeback starts from i, so the defrag range can be
1177 * written sequentially.
1179 if (i
< inode
->i_mapping
->writeback_index
)
1180 inode
->i_mapping
->writeback_index
= i
;
1182 while (i
<= last_index
&& defrag_count
< max_to_defrag
&&
1183 (i
< (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
1184 PAGE_CACHE_SHIFT
)) {
1186 * make sure we stop running if someone unmounts
1189 if (!(inode
->i_sb
->s_flags
& MS_ACTIVE
))
1192 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
1193 extent_thresh
, &last_len
, &skip
,
1194 &defrag_end
, range
->flags
&
1195 BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1198 * the should_defrag function tells us how much to skip
1199 * bump our counter by the suggested amount
1201 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1202 i
= max(i
+ 1, next
);
1207 cluster
= (PAGE_CACHE_ALIGN(defrag_end
) >>
1208 PAGE_CACHE_SHIFT
) - i
;
1209 cluster
= min(cluster
, max_cluster
);
1211 cluster
= max_cluster
;
1214 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
1215 BTRFS_I(inode
)->force_compress
= compress_type
;
1217 if (i
+ cluster
> ra_index
) {
1218 ra_index
= max(i
, ra_index
);
1219 btrfs_force_ra(inode
->i_mapping
, ra
, file
, ra_index
,
1221 ra_index
+= max_cluster
;
1224 mutex_lock(&inode
->i_mutex
);
1225 ret
= cluster_pages_for_defrag(inode
, pages
, i
, cluster
);
1227 mutex_unlock(&inode
->i_mutex
);
1231 defrag_count
+= ret
;
1232 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, ret
);
1233 mutex_unlock(&inode
->i_mutex
);
1236 if (newer_off
== (u64
)-1)
1242 newer_off
= max(newer_off
+ 1,
1243 (u64
)i
<< PAGE_CACHE_SHIFT
);
1245 ret
= find_new_extents(root
, inode
,
1246 newer_than
, &newer_off
,
1249 range
->start
= newer_off
;
1250 i
= (newer_off
& new_align
) >> PAGE_CACHE_SHIFT
;
1257 last_len
+= ret
<< PAGE_CACHE_SHIFT
;
1265 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
1266 filemap_flush(inode
->i_mapping
);
1268 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1269 /* the filemap_flush will queue IO into the worker threads, but
1270 * we have to make sure the IO is actually started and that
1271 * ordered extents get created before we return
1273 atomic_inc(&root
->fs_info
->async_submit_draining
);
1274 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
1275 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1276 wait_event(root
->fs_info
->async_submit_wait
,
1277 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
1278 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
1280 atomic_dec(&root
->fs_info
->async_submit_draining
);
1282 mutex_lock(&inode
->i_mutex
);
1283 BTRFS_I(inode
)->force_compress
= BTRFS_COMPRESS_NONE
;
1284 mutex_unlock(&inode
->i_mutex
);
1287 if (range
->compress_type
== BTRFS_COMPRESS_LZO
) {
1288 btrfs_set_fs_incompat(root
->fs_info
, COMPRESS_LZO
);
1300 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
1306 struct btrfs_ioctl_vol_args
*vol_args
;
1307 struct btrfs_trans_handle
*trans
;
1308 struct btrfs_device
*device
= NULL
;
1310 char *devstr
= NULL
;
1314 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1317 if (!capable(CAP_SYS_ADMIN
))
1320 mutex_lock(&root
->fs_info
->volume_mutex
);
1321 if (root
->fs_info
->balance_ctl
) {
1322 printk(KERN_INFO
"btrfs: balance in progress\n");
1327 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1328 if (IS_ERR(vol_args
)) {
1329 ret
= PTR_ERR(vol_args
);
1333 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1335 sizestr
= vol_args
->name
;
1336 devstr
= strchr(sizestr
, ':');
1339 sizestr
= devstr
+ 1;
1341 devstr
= vol_args
->name
;
1342 devid
= simple_strtoull(devstr
, &end
, 10);
1343 printk(KERN_INFO
"btrfs: resizing devid %llu\n",
1344 (unsigned long long)devid
);
1346 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
1348 printk(KERN_INFO
"btrfs: resizer unable to find device %llu\n",
1349 (unsigned long long)devid
);
1353 if (device
->fs_devices
&& device
->fs_devices
->seeding
) {
1354 printk(KERN_INFO
"btrfs: resizer unable to apply on "
1355 "seeding device %llu\n",
1356 (unsigned long long)devid
);
1361 if (!strcmp(sizestr
, "max"))
1362 new_size
= device
->bdev
->bd_inode
->i_size
;
1364 if (sizestr
[0] == '-') {
1367 } else if (sizestr
[0] == '+') {
1371 new_size
= memparse(sizestr
, NULL
);
1372 if (new_size
== 0) {
1378 old_size
= device
->total_bytes
;
1381 if (new_size
> old_size
) {
1385 new_size
= old_size
- new_size
;
1386 } else if (mod
> 0) {
1387 new_size
= old_size
+ new_size
;
1390 if (new_size
< 256 * 1024 * 1024) {
1394 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
1399 do_div(new_size
, root
->sectorsize
);
1400 new_size
*= root
->sectorsize
;
1402 printk_in_rcu(KERN_INFO
"btrfs: new size for %s is %llu\n",
1403 rcu_str_deref(device
->name
),
1404 (unsigned long long)new_size
);
1406 if (new_size
> old_size
) {
1407 trans
= btrfs_start_transaction(root
, 0);
1408 if (IS_ERR(trans
)) {
1409 ret
= PTR_ERR(trans
);
1412 ret
= btrfs_grow_device(trans
, device
, new_size
);
1413 btrfs_commit_transaction(trans
, root
);
1414 } else if (new_size
< old_size
) {
1415 ret
= btrfs_shrink_device(device
, new_size
);
1416 } /* equal, nothing need to do */
1421 mutex_unlock(&root
->fs_info
->volume_mutex
);
1425 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
1426 char *name
, unsigned long fd
, int subvol
,
1427 u64
*transid
, bool readonly
,
1428 struct btrfs_qgroup_inherit
**inherit
)
1433 ret
= mnt_want_write_file(file
);
1437 namelen
= strlen(name
);
1438 if (strchr(name
, '/')) {
1440 goto out_drop_write
;
1443 if (name
[0] == '.' &&
1444 (namelen
== 1 || (name
[1] == '.' && namelen
== 2))) {
1446 goto out_drop_write
;
1450 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1451 NULL
, transid
, readonly
, inherit
);
1453 struct fd src
= fdget(fd
);
1454 struct inode
*src_inode
;
1457 goto out_drop_write
;
1460 src_inode
= src
.file
->f_path
.dentry
->d_inode
;
1461 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
1462 printk(KERN_INFO
"btrfs: Snapshot src from "
1466 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
1467 BTRFS_I(src_inode
)->root
,
1468 transid
, readonly
, inherit
);
1473 mnt_drop_write_file(file
);
1478 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
1479 void __user
*arg
, int subvol
)
1481 struct btrfs_ioctl_vol_args
*vol_args
;
1484 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1485 if (IS_ERR(vol_args
))
1486 return PTR_ERR(vol_args
);
1487 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1489 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1490 vol_args
->fd
, subvol
,
1497 static noinline
int btrfs_ioctl_snap_create_v2(struct file
*file
,
1498 void __user
*arg
, int subvol
)
1500 struct btrfs_ioctl_vol_args_v2
*vol_args
;
1504 bool readonly
= false;
1505 struct btrfs_qgroup_inherit
*inherit
= NULL
;
1507 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1508 if (IS_ERR(vol_args
))
1509 return PTR_ERR(vol_args
);
1510 vol_args
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
1512 if (vol_args
->flags
&
1513 ~(BTRFS_SUBVOL_CREATE_ASYNC
| BTRFS_SUBVOL_RDONLY
|
1514 BTRFS_SUBVOL_QGROUP_INHERIT
)) {
1519 if (vol_args
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
1521 if (vol_args
->flags
& BTRFS_SUBVOL_RDONLY
)
1523 if (vol_args
->flags
& BTRFS_SUBVOL_QGROUP_INHERIT
) {
1524 if (vol_args
->size
> PAGE_CACHE_SIZE
) {
1528 inherit
= memdup_user(vol_args
->qgroup_inherit
, vol_args
->size
);
1529 if (IS_ERR(inherit
)) {
1530 ret
= PTR_ERR(inherit
);
1535 ret
= btrfs_ioctl_snap_create_transid(file
, vol_args
->name
,
1536 vol_args
->fd
, subvol
, ptr
,
1537 readonly
, &inherit
);
1539 if (ret
== 0 && ptr
&&
1541 offsetof(struct btrfs_ioctl_vol_args_v2
,
1542 transid
), ptr
, sizeof(*ptr
)))
1550 static noinline
int btrfs_ioctl_subvol_getflags(struct file
*file
,
1553 struct inode
*inode
= fdentry(file
)->d_inode
;
1554 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1558 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
)
1561 down_read(&root
->fs_info
->subvol_sem
);
1562 if (btrfs_root_readonly(root
))
1563 flags
|= BTRFS_SUBVOL_RDONLY
;
1564 up_read(&root
->fs_info
->subvol_sem
);
1566 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
1572 static noinline
int btrfs_ioctl_subvol_setflags(struct file
*file
,
1575 struct inode
*inode
= fdentry(file
)->d_inode
;
1576 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1577 struct btrfs_trans_handle
*trans
;
1582 ret
= mnt_want_write_file(file
);
1586 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
1588 goto out_drop_write
;
1591 if (copy_from_user(&flags
, arg
, sizeof(flags
))) {
1593 goto out_drop_write
;
1596 if (flags
& BTRFS_SUBVOL_CREATE_ASYNC
) {
1598 goto out_drop_write
;
1601 if (flags
& ~BTRFS_SUBVOL_RDONLY
) {
1603 goto out_drop_write
;
1606 if (!inode_owner_or_capable(inode
)) {
1608 goto out_drop_write
;
1611 down_write(&root
->fs_info
->subvol_sem
);
1614 if (!!(flags
& BTRFS_SUBVOL_RDONLY
) == btrfs_root_readonly(root
))
1617 root_flags
= btrfs_root_flags(&root
->root_item
);
1618 if (flags
& BTRFS_SUBVOL_RDONLY
)
1619 btrfs_set_root_flags(&root
->root_item
,
1620 root_flags
| BTRFS_ROOT_SUBVOL_RDONLY
);
1622 btrfs_set_root_flags(&root
->root_item
,
1623 root_flags
& ~BTRFS_ROOT_SUBVOL_RDONLY
);
1625 trans
= btrfs_start_transaction(root
, 1);
1626 if (IS_ERR(trans
)) {
1627 ret
= PTR_ERR(trans
);
1631 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
1632 &root
->root_key
, &root
->root_item
);
1634 btrfs_commit_transaction(trans
, root
);
1637 btrfs_set_root_flags(&root
->root_item
, root_flags
);
1639 up_write(&root
->fs_info
->subvol_sem
);
1641 mnt_drop_write_file(file
);
1647 * helper to check if the subvolume references other subvolumes
1649 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1651 struct btrfs_path
*path
;
1652 struct btrfs_key key
;
1655 path
= btrfs_alloc_path();
1659 key
.objectid
= root
->root_key
.objectid
;
1660 key
.type
= BTRFS_ROOT_REF_KEY
;
1661 key
.offset
= (u64
)-1;
1663 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1670 if (path
->slots
[0] > 0) {
1672 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1673 if (key
.objectid
== root
->root_key
.objectid
&&
1674 key
.type
== BTRFS_ROOT_REF_KEY
)
1678 btrfs_free_path(path
);
1682 static noinline
int key_in_sk(struct btrfs_key
*key
,
1683 struct btrfs_ioctl_search_key
*sk
)
1685 struct btrfs_key test
;
1688 test
.objectid
= sk
->min_objectid
;
1689 test
.type
= sk
->min_type
;
1690 test
.offset
= sk
->min_offset
;
1692 ret
= btrfs_comp_cpu_keys(key
, &test
);
1696 test
.objectid
= sk
->max_objectid
;
1697 test
.type
= sk
->max_type
;
1698 test
.offset
= sk
->max_offset
;
1700 ret
= btrfs_comp_cpu_keys(key
, &test
);
1706 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1707 struct btrfs_path
*path
,
1708 struct btrfs_key
*key
,
1709 struct btrfs_ioctl_search_key
*sk
,
1711 unsigned long *sk_offset
,
1715 struct extent_buffer
*leaf
;
1716 struct btrfs_ioctl_search_header sh
;
1717 unsigned long item_off
;
1718 unsigned long item_len
;
1724 leaf
= path
->nodes
[0];
1725 slot
= path
->slots
[0];
1726 nritems
= btrfs_header_nritems(leaf
);
1728 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1732 found_transid
= btrfs_header_generation(leaf
);
1734 for (i
= slot
; i
< nritems
; i
++) {
1735 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1736 item_len
= btrfs_item_size_nr(leaf
, i
);
1738 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1741 if (sizeof(sh
) + item_len
+ *sk_offset
>
1742 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1747 btrfs_item_key_to_cpu(leaf
, key
, i
);
1748 if (!key_in_sk(key
, sk
))
1751 sh
.objectid
= key
->objectid
;
1752 sh
.offset
= key
->offset
;
1753 sh
.type
= key
->type
;
1755 sh
.transid
= found_transid
;
1757 /* copy search result header */
1758 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1759 *sk_offset
+= sizeof(sh
);
1762 char *p
= buf
+ *sk_offset
;
1764 read_extent_buffer(leaf
, p
,
1765 item_off
, item_len
);
1766 *sk_offset
+= item_len
;
1770 if (*num_found
>= sk
->nr_items
)
1775 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1777 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1780 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1790 static noinline
int search_ioctl(struct inode
*inode
,
1791 struct btrfs_ioctl_search_args
*args
)
1793 struct btrfs_root
*root
;
1794 struct btrfs_key key
;
1795 struct btrfs_key max_key
;
1796 struct btrfs_path
*path
;
1797 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1798 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1801 unsigned long sk_offset
= 0;
1803 path
= btrfs_alloc_path();
1807 if (sk
->tree_id
== 0) {
1808 /* search the root of the inode that was passed */
1809 root
= BTRFS_I(inode
)->root
;
1811 key
.objectid
= sk
->tree_id
;
1812 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1813 key
.offset
= (u64
)-1;
1814 root
= btrfs_read_fs_root_no_name(info
, &key
);
1816 printk(KERN_ERR
"could not find root %llu\n",
1818 btrfs_free_path(path
);
1823 key
.objectid
= sk
->min_objectid
;
1824 key
.type
= sk
->min_type
;
1825 key
.offset
= sk
->min_offset
;
1827 max_key
.objectid
= sk
->max_objectid
;
1828 max_key
.type
= sk
->max_type
;
1829 max_key
.offset
= sk
->max_offset
;
1831 path
->keep_locks
= 1;
1834 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1841 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1842 &sk_offset
, &num_found
);
1843 btrfs_release_path(path
);
1844 if (ret
|| num_found
>= sk
->nr_items
)
1850 sk
->nr_items
= num_found
;
1851 btrfs_free_path(path
);
1855 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1858 struct btrfs_ioctl_search_args
*args
;
1859 struct inode
*inode
;
1862 if (!capable(CAP_SYS_ADMIN
))
1865 args
= memdup_user(argp
, sizeof(*args
));
1867 return PTR_ERR(args
);
1869 inode
= fdentry(file
)->d_inode
;
1870 ret
= search_ioctl(inode
, args
);
1871 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1878 * Search INODE_REFs to identify path name of 'dirid' directory
1879 * in a 'tree_id' tree. and sets path name to 'name'.
1881 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1882 u64 tree_id
, u64 dirid
, char *name
)
1884 struct btrfs_root
*root
;
1885 struct btrfs_key key
;
1891 struct btrfs_inode_ref
*iref
;
1892 struct extent_buffer
*l
;
1893 struct btrfs_path
*path
;
1895 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1900 path
= btrfs_alloc_path();
1904 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1906 key
.objectid
= tree_id
;
1907 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1908 key
.offset
= (u64
)-1;
1909 root
= btrfs_read_fs_root_no_name(info
, &key
);
1911 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1916 key
.objectid
= dirid
;
1917 key
.type
= BTRFS_INODE_REF_KEY
;
1918 key
.offset
= (u64
)-1;
1921 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1926 slot
= path
->slots
[0];
1927 if (ret
> 0 && slot
> 0)
1929 btrfs_item_key_to_cpu(l
, &key
, slot
);
1931 if (ret
> 0 && (key
.objectid
!= dirid
||
1932 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1937 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1938 len
= btrfs_inode_ref_name_len(l
, iref
);
1940 total_len
+= len
+ 1;
1945 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1947 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1950 btrfs_release_path(path
);
1951 key
.objectid
= key
.offset
;
1952 key
.offset
= (u64
)-1;
1953 dirid
= key
.objectid
;
1957 memmove(name
, ptr
, total_len
);
1958 name
[total_len
]='\0';
1961 btrfs_free_path(path
);
1965 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1968 struct btrfs_ioctl_ino_lookup_args
*args
;
1969 struct inode
*inode
;
1972 if (!capable(CAP_SYS_ADMIN
))
1975 args
= memdup_user(argp
, sizeof(*args
));
1977 return PTR_ERR(args
);
1979 inode
= fdentry(file
)->d_inode
;
1981 if (args
->treeid
== 0)
1982 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1984 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1985 args
->treeid
, args
->objectid
,
1988 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1995 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1998 struct dentry
*parent
= fdentry(file
);
1999 struct dentry
*dentry
;
2000 struct inode
*dir
= parent
->d_inode
;
2001 struct inode
*inode
;
2002 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
2003 struct btrfs_root
*dest
= NULL
;
2004 struct btrfs_ioctl_vol_args
*vol_args
;
2005 struct btrfs_trans_handle
*trans
;
2010 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2011 if (IS_ERR(vol_args
))
2012 return PTR_ERR(vol_args
);
2014 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2015 namelen
= strlen(vol_args
->name
);
2016 if (strchr(vol_args
->name
, '/') ||
2017 strncmp(vol_args
->name
, "..", namelen
) == 0) {
2022 err
= mnt_want_write_file(file
);
2026 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
2027 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
2028 if (IS_ERR(dentry
)) {
2029 err
= PTR_ERR(dentry
);
2030 goto out_unlock_dir
;
2033 if (!dentry
->d_inode
) {
2038 inode
= dentry
->d_inode
;
2039 dest
= BTRFS_I(inode
)->root
;
2040 if (!capable(CAP_SYS_ADMIN
)){
2042 * Regular user. Only allow this with a special mount
2043 * option, when the user has write+exec access to the
2044 * subvol root, and when rmdir(2) would have been
2047 * Note that this is _not_ check that the subvol is
2048 * empty or doesn't contain data that we wouldn't
2049 * otherwise be able to delete.
2051 * Users who want to delete empty subvols should try
2055 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
2059 * Do not allow deletion if the parent dir is the same
2060 * as the dir to be deleted. That means the ioctl
2061 * must be called on the dentry referencing the root
2062 * of the subvol, not a random directory contained
2069 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
2073 /* check if subvolume may be deleted by a non-root user */
2074 err
= btrfs_may_delete(dir
, dentry
, 1);
2079 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
2084 mutex_lock(&inode
->i_mutex
);
2085 err
= d_invalidate(dentry
);
2089 down_write(&root
->fs_info
->subvol_sem
);
2091 err
= may_destroy_subvol(dest
);
2095 trans
= btrfs_start_transaction(root
, 0);
2096 if (IS_ERR(trans
)) {
2097 err
= PTR_ERR(trans
);
2100 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
2102 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
2103 dest
->root_key
.objectid
,
2104 dentry
->d_name
.name
,
2105 dentry
->d_name
.len
);
2108 btrfs_abort_transaction(trans
, root
, ret
);
2112 btrfs_record_root_in_trans(trans
, dest
);
2114 memset(&dest
->root_item
.drop_progress
, 0,
2115 sizeof(dest
->root_item
.drop_progress
));
2116 dest
->root_item
.drop_level
= 0;
2117 btrfs_set_root_refs(&dest
->root_item
, 0);
2119 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
2120 ret
= btrfs_insert_orphan_item(trans
,
2121 root
->fs_info
->tree_root
,
2122 dest
->root_key
.objectid
);
2124 btrfs_abort_transaction(trans
, root
, ret
);
2130 ret
= btrfs_end_transaction(trans
, root
);
2133 inode
->i_flags
|= S_DEAD
;
2135 up_write(&root
->fs_info
->subvol_sem
);
2137 mutex_unlock(&inode
->i_mutex
);
2139 shrink_dcache_sb(root
->fs_info
->sb
);
2140 btrfs_invalidate_inodes(dest
);
2146 mutex_unlock(&dir
->i_mutex
);
2147 mnt_drop_write_file(file
);
2153 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
2155 struct inode
*inode
= fdentry(file
)->d_inode
;
2156 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2157 struct btrfs_ioctl_defrag_range_args
*range
;
2160 if (btrfs_root_readonly(root
))
2163 ret
= mnt_want_write_file(file
);
2167 switch (inode
->i_mode
& S_IFMT
) {
2169 if (!capable(CAP_SYS_ADMIN
)) {
2173 ret
= btrfs_defrag_root(root
, 0);
2176 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
2179 if (!(file
->f_mode
& FMODE_WRITE
)) {
2184 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
2191 if (copy_from_user(range
, argp
,
2197 /* compression requires us to start the IO */
2198 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
2199 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
2200 range
->extent_thresh
= (u32
)-1;
2203 /* the rest are all set to zero by kzalloc */
2204 range
->len
= (u64
)-1;
2206 ret
= btrfs_defrag_file(fdentry(file
)->d_inode
, file
,
2216 mnt_drop_write_file(file
);
2220 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
2222 struct btrfs_ioctl_vol_args
*vol_args
;
2225 if (!capable(CAP_SYS_ADMIN
))
2228 mutex_lock(&root
->fs_info
->volume_mutex
);
2229 if (root
->fs_info
->balance_ctl
) {
2230 printk(KERN_INFO
"btrfs: balance in progress\n");
2235 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2236 if (IS_ERR(vol_args
)) {
2237 ret
= PTR_ERR(vol_args
);
2241 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2242 ret
= btrfs_init_new_device(root
, vol_args
->name
);
2246 mutex_unlock(&root
->fs_info
->volume_mutex
);
2250 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
2252 struct btrfs_ioctl_vol_args
*vol_args
;
2255 if (!capable(CAP_SYS_ADMIN
))
2258 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
2261 mutex_lock(&root
->fs_info
->volume_mutex
);
2262 if (root
->fs_info
->balance_ctl
) {
2263 printk(KERN_INFO
"btrfs: balance in progress\n");
2268 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
2269 if (IS_ERR(vol_args
)) {
2270 ret
= PTR_ERR(vol_args
);
2274 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
2275 ret
= btrfs_rm_device(root
, vol_args
->name
);
2279 mutex_unlock(&root
->fs_info
->volume_mutex
);
2283 static long btrfs_ioctl_fs_info(struct btrfs_root
*root
, void __user
*arg
)
2285 struct btrfs_ioctl_fs_info_args
*fi_args
;
2286 struct btrfs_device
*device
;
2287 struct btrfs_device
*next
;
2288 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2291 if (!capable(CAP_SYS_ADMIN
))
2294 fi_args
= kzalloc(sizeof(*fi_args
), GFP_KERNEL
);
2298 fi_args
->num_devices
= fs_devices
->num_devices
;
2299 memcpy(&fi_args
->fsid
, root
->fs_info
->fsid
, sizeof(fi_args
->fsid
));
2301 mutex_lock(&fs_devices
->device_list_mutex
);
2302 list_for_each_entry_safe(device
, next
, &fs_devices
->devices
, dev_list
) {
2303 if (device
->devid
> fi_args
->max_id
)
2304 fi_args
->max_id
= device
->devid
;
2306 mutex_unlock(&fs_devices
->device_list_mutex
);
2308 if (copy_to_user(arg
, fi_args
, sizeof(*fi_args
)))
2315 static long btrfs_ioctl_dev_info(struct btrfs_root
*root
, void __user
*arg
)
2317 struct btrfs_ioctl_dev_info_args
*di_args
;
2318 struct btrfs_device
*dev
;
2319 struct btrfs_fs_devices
*fs_devices
= root
->fs_info
->fs_devices
;
2321 char *s_uuid
= NULL
;
2322 char empty_uuid
[BTRFS_UUID_SIZE
] = {0};
2324 if (!capable(CAP_SYS_ADMIN
))
2327 di_args
= memdup_user(arg
, sizeof(*di_args
));
2328 if (IS_ERR(di_args
))
2329 return PTR_ERR(di_args
);
2331 if (memcmp(empty_uuid
, di_args
->uuid
, BTRFS_UUID_SIZE
) != 0)
2332 s_uuid
= di_args
->uuid
;
2334 mutex_lock(&fs_devices
->device_list_mutex
);
2335 dev
= btrfs_find_device(root
, di_args
->devid
, s_uuid
, NULL
);
2336 mutex_unlock(&fs_devices
->device_list_mutex
);
2343 di_args
->devid
= dev
->devid
;
2344 di_args
->bytes_used
= dev
->bytes_used
;
2345 di_args
->total_bytes
= dev
->total_bytes
;
2346 memcpy(di_args
->uuid
, dev
->uuid
, sizeof(di_args
->uuid
));
2348 struct rcu_string
*name
;
2351 name
= rcu_dereference(dev
->name
);
2352 strncpy(di_args
->path
, name
->str
, sizeof(di_args
->path
));
2354 di_args
->path
[sizeof(di_args
->path
) - 1] = 0;
2356 di_args
->path
[0] = '\0';
2360 if (ret
== 0 && copy_to_user(arg
, di_args
, sizeof(*di_args
)))
2367 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
2368 u64 off
, u64 olen
, u64 destoff
)
2370 struct inode
*inode
= fdentry(file
)->d_inode
;
2371 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2374 struct btrfs_trans_handle
*trans
;
2375 struct btrfs_path
*path
;
2376 struct extent_buffer
*leaf
;
2378 struct btrfs_key key
;
2383 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
2387 * - split compressed inline extents. annoying: we need to
2388 * decompress into destination's address_space (the file offset
2389 * may change, so source mapping won't do), then recompress (or
2390 * otherwise reinsert) a subrange.
2391 * - allow ranges within the same file to be cloned (provided
2392 * they don't overlap)?
2395 /* the destination must be opened for writing */
2396 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
2399 if (btrfs_root_readonly(root
))
2402 ret
= mnt_want_write_file(file
);
2406 src_file
= fdget(srcfd
);
2407 if (!src_file
.file
) {
2409 goto out_drop_write
;
2413 if (src_file
.file
->f_path
.mnt
!= file
->f_path
.mnt
)
2416 src
= src_file
.file
->f_dentry
->d_inode
;
2422 /* the src must be open for reading */
2423 if (!(src_file
.file
->f_mode
& FMODE_READ
))
2426 /* don't make the dst file partly checksummed */
2427 if ((BTRFS_I(src
)->flags
& BTRFS_INODE_NODATASUM
) !=
2428 (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
2432 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
2436 if (src
->i_sb
!= inode
->i_sb
)
2440 buf
= vmalloc(btrfs_level_size(root
, 0));
2444 path
= btrfs_alloc_path();
2452 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
2453 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
2455 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
2456 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2459 /* determine range to clone */
2461 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
2464 olen
= len
= src
->i_size
- off
;
2465 /* if we extend to eof, continue to block boundary */
2466 if (off
+ len
== src
->i_size
)
2467 len
= ALIGN(src
->i_size
, bs
) - off
;
2469 /* verify the end result is block aligned */
2470 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
2471 !IS_ALIGNED(destoff
, bs
))
2474 if (destoff
> inode
->i_size
) {
2475 ret
= btrfs_cont_expand(inode
, inode
->i_size
, destoff
);
2480 /* truncate page cache pages from target inode range */
2481 truncate_inode_pages_range(&inode
->i_data
, destoff
,
2482 PAGE_CACHE_ALIGN(destoff
+ len
) - 1);
2484 /* do any pending delalloc/csum calc on src, one way or
2485 another, and lock file content */
2487 struct btrfs_ordered_extent
*ordered
;
2488 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1);
2489 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+ len
- 1);
2491 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1,
2492 EXTENT_DELALLOC
, 0, NULL
))
2494 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1);
2496 btrfs_put_ordered_extent(ordered
);
2497 btrfs_wait_ordered_range(src
, off
, len
);
2501 key
.objectid
= btrfs_ino(src
);
2502 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2507 * note the key will change type as we walk through the
2510 ret
= btrfs_search_slot(NULL
, BTRFS_I(src
)->root
, &key
, path
,
2515 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2516 if (path
->slots
[0] >= nritems
) {
2517 ret
= btrfs_next_leaf(BTRFS_I(src
)->root
, path
);
2522 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2524 leaf
= path
->nodes
[0];
2525 slot
= path
->slots
[0];
2527 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2528 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
2529 key
.objectid
!= btrfs_ino(src
))
2532 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
2533 struct btrfs_file_extent_item
*extent
;
2536 struct btrfs_key new_key
;
2537 u64 disko
= 0, diskl
= 0;
2538 u64 datao
= 0, datal
= 0;
2542 size
= btrfs_item_size_nr(leaf
, slot
);
2543 read_extent_buffer(leaf
, buf
,
2544 btrfs_item_ptr_offset(leaf
, slot
),
2547 extent
= btrfs_item_ptr(leaf
, slot
,
2548 struct btrfs_file_extent_item
);
2549 comp
= btrfs_file_extent_compression(leaf
, extent
);
2550 type
= btrfs_file_extent_type(leaf
, extent
);
2551 if (type
== BTRFS_FILE_EXTENT_REG
||
2552 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2553 disko
= btrfs_file_extent_disk_bytenr(leaf
,
2555 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
2557 datao
= btrfs_file_extent_offset(leaf
, extent
);
2558 datal
= btrfs_file_extent_num_bytes(leaf
,
2560 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2561 /* take upper bound, may be compressed */
2562 datal
= btrfs_file_extent_ram_bytes(leaf
,
2565 btrfs_release_path(path
);
2567 if (key
.offset
+ datal
<= off
||
2568 key
.offset
>= off
+ len
- 1)
2571 memcpy(&new_key
, &key
, sizeof(new_key
));
2572 new_key
.objectid
= btrfs_ino(inode
);
2573 if (off
<= key
.offset
)
2574 new_key
.offset
= key
.offset
+ destoff
- off
;
2576 new_key
.offset
= destoff
;
2579 * 1 - adjusting old extent (we may have to split it)
2580 * 1 - add new extent
2583 trans
= btrfs_start_transaction(root
, 3);
2584 if (IS_ERR(trans
)) {
2585 ret
= PTR_ERR(trans
);
2589 if (type
== BTRFS_FILE_EXTENT_REG
||
2590 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
2592 * a | --- range to clone ---| b
2593 * | ------------- extent ------------- |
2596 /* substract range b */
2597 if (key
.offset
+ datal
> off
+ len
)
2598 datal
= off
+ len
- key
.offset
;
2600 /* substract range a */
2601 if (off
> key
.offset
) {
2602 datao
+= off
- key
.offset
;
2603 datal
-= off
- key
.offset
;
2606 ret
= btrfs_drop_extents(trans
, root
, inode
,
2608 new_key
.offset
+ datal
,
2611 btrfs_abort_transaction(trans
, root
,
2613 btrfs_end_transaction(trans
, root
);
2617 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2620 btrfs_abort_transaction(trans
, root
,
2622 btrfs_end_transaction(trans
, root
);
2626 leaf
= path
->nodes
[0];
2627 slot
= path
->slots
[0];
2628 write_extent_buffer(leaf
, buf
,
2629 btrfs_item_ptr_offset(leaf
, slot
),
2632 extent
= btrfs_item_ptr(leaf
, slot
,
2633 struct btrfs_file_extent_item
);
2635 /* disko == 0 means it's a hole */
2639 btrfs_set_file_extent_offset(leaf
, extent
,
2641 btrfs_set_file_extent_num_bytes(leaf
, extent
,
2644 inode_add_bytes(inode
, datal
);
2645 ret
= btrfs_inc_extent_ref(trans
, root
,
2647 root
->root_key
.objectid
,
2649 new_key
.offset
- datao
,
2652 btrfs_abort_transaction(trans
,
2655 btrfs_end_transaction(trans
,
2661 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
2664 if (off
> key
.offset
) {
2665 skip
= off
- key
.offset
;
2666 new_key
.offset
+= skip
;
2669 if (key
.offset
+ datal
> off
+ len
)
2670 trim
= key
.offset
+ datal
- (off
+ len
);
2672 if (comp
&& (skip
|| trim
)) {
2674 btrfs_end_transaction(trans
, root
);
2677 size
-= skip
+ trim
;
2678 datal
-= skip
+ trim
;
2680 ret
= btrfs_drop_extents(trans
, root
, inode
,
2682 new_key
.offset
+ datal
,
2685 btrfs_abort_transaction(trans
, root
,
2687 btrfs_end_transaction(trans
, root
);
2691 ret
= btrfs_insert_empty_item(trans
, root
, path
,
2694 btrfs_abort_transaction(trans
, root
,
2696 btrfs_end_transaction(trans
, root
);
2702 btrfs_file_extent_calc_inline_size(0);
2703 memmove(buf
+start
, buf
+start
+skip
,
2707 leaf
= path
->nodes
[0];
2708 slot
= path
->slots
[0];
2709 write_extent_buffer(leaf
, buf
,
2710 btrfs_item_ptr_offset(leaf
, slot
),
2712 inode_add_bytes(inode
, datal
);
2715 btrfs_mark_buffer_dirty(leaf
);
2716 btrfs_release_path(path
);
2718 inode_inc_iversion(inode
);
2719 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
2722 * we round up to the block size at eof when
2723 * determining which extents to clone above,
2724 * but shouldn't round up the file size
2726 endoff
= new_key
.offset
+ datal
;
2727 if (endoff
> destoff
+olen
)
2728 endoff
= destoff
+olen
;
2729 if (endoff
> inode
->i_size
)
2730 btrfs_i_size_write(inode
, endoff
);
2732 ret
= btrfs_update_inode(trans
, root
, inode
);
2734 btrfs_abort_transaction(trans
, root
, ret
);
2735 btrfs_end_transaction(trans
, root
);
2738 ret
= btrfs_end_transaction(trans
, root
);
2741 btrfs_release_path(path
);
2746 btrfs_release_path(path
);
2747 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+ len
- 1);
2749 mutex_unlock(&src
->i_mutex
);
2750 mutex_unlock(&inode
->i_mutex
);
2752 btrfs_free_path(path
);
2756 mnt_drop_write_file(file
);
2760 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
2762 struct btrfs_ioctl_clone_range_args args
;
2764 if (copy_from_user(&args
, argp
, sizeof(args
)))
2766 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
2767 args
.src_length
, args
.dest_offset
);
2771 * there are many ways the trans_start and trans_end ioctls can lead
2772 * to deadlocks. They should only be used by applications that
2773 * basically own the machine, and have a very in depth understanding
2774 * of all the possible deadlocks and enospc problems.
2776 static long btrfs_ioctl_trans_start(struct file
*file
)
2778 struct inode
*inode
= fdentry(file
)->d_inode
;
2779 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2780 struct btrfs_trans_handle
*trans
;
2784 if (!capable(CAP_SYS_ADMIN
))
2788 if (file
->private_data
)
2792 if (btrfs_root_readonly(root
))
2795 ret
= mnt_want_write_file(file
);
2799 atomic_inc(&root
->fs_info
->open_ioctl_trans
);
2802 trans
= btrfs_start_ioctl_transaction(root
);
2806 file
->private_data
= trans
;
2810 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
2811 mnt_drop_write_file(file
);
2816 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
2818 struct inode
*inode
= fdentry(file
)->d_inode
;
2819 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2820 struct btrfs_root
*new_root
;
2821 struct btrfs_dir_item
*di
;
2822 struct btrfs_trans_handle
*trans
;
2823 struct btrfs_path
*path
;
2824 struct btrfs_key location
;
2825 struct btrfs_disk_key disk_key
;
2829 if (!capable(CAP_SYS_ADMIN
))
2832 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2836 objectid
= root
->root_key
.objectid
;
2838 location
.objectid
= objectid
;
2839 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2840 location
.offset
= (u64
)-1;
2842 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2843 if (IS_ERR(new_root
))
2844 return PTR_ERR(new_root
);
2846 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2849 path
= btrfs_alloc_path();
2852 path
->leave_spinning
= 1;
2854 trans
= btrfs_start_transaction(root
, 1);
2855 if (IS_ERR(trans
)) {
2856 btrfs_free_path(path
);
2857 return PTR_ERR(trans
);
2860 dir_id
= btrfs_super_root_dir(root
->fs_info
->super_copy
);
2861 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2862 dir_id
, "default", 7, 1);
2863 if (IS_ERR_OR_NULL(di
)) {
2864 btrfs_free_path(path
);
2865 btrfs_end_transaction(trans
, root
);
2866 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2867 "this isn't going to work\n");
2871 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2872 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2873 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2874 btrfs_free_path(path
);
2876 btrfs_set_fs_incompat(root
->fs_info
, DEFAULT_SUBVOL
);
2877 btrfs_end_transaction(trans
, root
);
2882 void btrfs_get_block_group_info(struct list_head
*groups_list
,
2883 struct btrfs_ioctl_space_info
*space
)
2885 struct btrfs_block_group_cache
*block_group
;
2887 space
->total_bytes
= 0;
2888 space
->used_bytes
= 0;
2890 list_for_each_entry(block_group
, groups_list
, list
) {
2891 space
->flags
= block_group
->flags
;
2892 space
->total_bytes
+= block_group
->key
.offset
;
2893 space
->used_bytes
+=
2894 btrfs_block_group_used(&block_group
->item
);
2898 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2900 struct btrfs_ioctl_space_args space_args
;
2901 struct btrfs_ioctl_space_info space
;
2902 struct btrfs_ioctl_space_info
*dest
;
2903 struct btrfs_ioctl_space_info
*dest_orig
;
2904 struct btrfs_ioctl_space_info __user
*user_dest
;
2905 struct btrfs_space_info
*info
;
2906 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2907 BTRFS_BLOCK_GROUP_SYSTEM
,
2908 BTRFS_BLOCK_GROUP_METADATA
,
2909 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2916 if (copy_from_user(&space_args
,
2917 (struct btrfs_ioctl_space_args __user
*)arg
,
2918 sizeof(space_args
)))
2921 for (i
= 0; i
< num_types
; i
++) {
2922 struct btrfs_space_info
*tmp
;
2926 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2928 if (tmp
->flags
== types
[i
]) {
2938 down_read(&info
->groups_sem
);
2939 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2940 if (!list_empty(&info
->block_groups
[c
]))
2943 up_read(&info
->groups_sem
);
2946 /* space_slots == 0 means they are asking for a count */
2947 if (space_args
.space_slots
== 0) {
2948 space_args
.total_spaces
= slot_count
;
2952 slot_count
= min_t(u64
, space_args
.space_slots
, slot_count
);
2954 alloc_size
= sizeof(*dest
) * slot_count
;
2956 /* we generally have at most 6 or so space infos, one for each raid
2957 * level. So, a whole page should be more than enough for everyone
2959 if (alloc_size
> PAGE_CACHE_SIZE
)
2962 space_args
.total_spaces
= 0;
2963 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2968 /* now we have a buffer to copy into */
2969 for (i
= 0; i
< num_types
; i
++) {
2970 struct btrfs_space_info
*tmp
;
2977 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2979 if (tmp
->flags
== types
[i
]) {
2988 down_read(&info
->groups_sem
);
2989 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2990 if (!list_empty(&info
->block_groups
[c
])) {
2991 btrfs_get_block_group_info(
2992 &info
->block_groups
[c
], &space
);
2993 memcpy(dest
, &space
, sizeof(space
));
2995 space_args
.total_spaces
++;
3001 up_read(&info
->groups_sem
);
3004 user_dest
= (struct btrfs_ioctl_space_info __user
*)
3005 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
3007 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
3012 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
3019 * there are many ways the trans_start and trans_end ioctls can lead
3020 * to deadlocks. They should only be used by applications that
3021 * basically own the machine, and have a very in depth understanding
3022 * of all the possible deadlocks and enospc problems.
3024 long btrfs_ioctl_trans_end(struct file
*file
)
3026 struct inode
*inode
= fdentry(file
)->d_inode
;
3027 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3028 struct btrfs_trans_handle
*trans
;
3030 trans
= file
->private_data
;
3033 file
->private_data
= NULL
;
3035 btrfs_end_transaction(trans
, root
);
3037 atomic_dec(&root
->fs_info
->open_ioctl_trans
);
3039 mnt_drop_write_file(file
);
3043 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
3045 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
3046 struct btrfs_trans_handle
*trans
;
3050 trans
= btrfs_start_transaction(root
, 0);
3052 return PTR_ERR(trans
);
3053 transid
= trans
->transid
;
3054 ret
= btrfs_commit_transaction_async(trans
, root
, 0);
3056 btrfs_end_transaction(trans
, root
);
3061 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
3066 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
3068 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
3072 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
3075 transid
= 0; /* current trans */
3077 return btrfs_wait_for_commit(root
, transid
);
3080 static long btrfs_ioctl_scrub(struct btrfs_root
*root
, void __user
*arg
)
3083 struct btrfs_ioctl_scrub_args
*sa
;
3085 if (!capable(CAP_SYS_ADMIN
))
3088 sa
= memdup_user(arg
, sizeof(*sa
));
3092 ret
= btrfs_scrub_dev(root
, sa
->devid
, sa
->start
, sa
->end
,
3093 &sa
->progress
, sa
->flags
& BTRFS_SCRUB_READONLY
);
3095 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3102 static long btrfs_ioctl_scrub_cancel(struct btrfs_root
*root
, void __user
*arg
)
3104 if (!capable(CAP_SYS_ADMIN
))
3107 return btrfs_scrub_cancel(root
);
3110 static long btrfs_ioctl_scrub_progress(struct btrfs_root
*root
,
3113 struct btrfs_ioctl_scrub_args
*sa
;
3116 if (!capable(CAP_SYS_ADMIN
))
3119 sa
= memdup_user(arg
, sizeof(*sa
));
3123 ret
= btrfs_scrub_progress(root
, sa
->devid
, &sa
->progress
);
3125 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3132 static long btrfs_ioctl_get_dev_stats(struct btrfs_root
*root
,
3135 struct btrfs_ioctl_get_dev_stats
*sa
;
3138 sa
= memdup_user(arg
, sizeof(*sa
));
3142 if ((sa
->flags
& BTRFS_DEV_STATS_RESET
) && !capable(CAP_SYS_ADMIN
)) {
3147 ret
= btrfs_get_dev_stats(root
, sa
);
3149 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3156 static long btrfs_ioctl_ino_to_path(struct btrfs_root
*root
, void __user
*arg
)
3162 struct btrfs_ioctl_ino_path_args
*ipa
= NULL
;
3163 struct inode_fs_paths
*ipath
= NULL
;
3164 struct btrfs_path
*path
;
3166 if (!capable(CAP_SYS_ADMIN
))
3169 path
= btrfs_alloc_path();
3175 ipa
= memdup_user(arg
, sizeof(*ipa
));
3182 size
= min_t(u32
, ipa
->size
, 4096);
3183 ipath
= init_ipath(size
, root
, path
);
3184 if (IS_ERR(ipath
)) {
3185 ret
= PTR_ERR(ipath
);
3190 ret
= paths_from_inode(ipa
->inum
, ipath
);
3194 for (i
= 0; i
< ipath
->fspath
->elem_cnt
; ++i
) {
3195 rel_ptr
= ipath
->fspath
->val
[i
] -
3196 (u64
)(unsigned long)ipath
->fspath
->val
;
3197 ipath
->fspath
->val
[i
] = rel_ptr
;
3200 ret
= copy_to_user((void *)(unsigned long)ipa
->fspath
,
3201 (void *)(unsigned long)ipath
->fspath
, size
);
3208 btrfs_free_path(path
);
3215 static int build_ino_list(u64 inum
, u64 offset
, u64 root
, void *ctx
)
3217 struct btrfs_data_container
*inodes
= ctx
;
3218 const size_t c
= 3 * sizeof(u64
);
3220 if (inodes
->bytes_left
>= c
) {
3221 inodes
->bytes_left
-= c
;
3222 inodes
->val
[inodes
->elem_cnt
] = inum
;
3223 inodes
->val
[inodes
->elem_cnt
+ 1] = offset
;
3224 inodes
->val
[inodes
->elem_cnt
+ 2] = root
;
3225 inodes
->elem_cnt
+= 3;
3227 inodes
->bytes_missing
+= c
- inodes
->bytes_left
;
3228 inodes
->bytes_left
= 0;
3229 inodes
->elem_missed
+= 3;
3235 static long btrfs_ioctl_logical_to_ino(struct btrfs_root
*root
,
3240 struct btrfs_ioctl_logical_ino_args
*loi
;
3241 struct btrfs_data_container
*inodes
= NULL
;
3242 struct btrfs_path
*path
= NULL
;
3244 if (!capable(CAP_SYS_ADMIN
))
3247 loi
= memdup_user(arg
, sizeof(*loi
));
3254 path
= btrfs_alloc_path();
3260 size
= min_t(u32
, loi
->size
, 64 * 1024);
3261 inodes
= init_data_container(size
);
3262 if (IS_ERR(inodes
)) {
3263 ret
= PTR_ERR(inodes
);
3268 ret
= iterate_inodes_from_logical(loi
->logical
, root
->fs_info
, path
,
3269 build_ino_list
, inodes
);
3275 ret
= copy_to_user((void *)(unsigned long)loi
->inodes
,
3276 (void *)(unsigned long)inodes
, size
);
3281 btrfs_free_path(path
);
3288 void update_ioctl_balance_args(struct btrfs_fs_info
*fs_info
, int lock
,
3289 struct btrfs_ioctl_balance_args
*bargs
)
3291 struct btrfs_balance_control
*bctl
= fs_info
->balance_ctl
;
3293 bargs
->flags
= bctl
->flags
;
3295 if (atomic_read(&fs_info
->balance_running
))
3296 bargs
->state
|= BTRFS_BALANCE_STATE_RUNNING
;
3297 if (atomic_read(&fs_info
->balance_pause_req
))
3298 bargs
->state
|= BTRFS_BALANCE_STATE_PAUSE_REQ
;
3299 if (atomic_read(&fs_info
->balance_cancel_req
))
3300 bargs
->state
|= BTRFS_BALANCE_STATE_CANCEL_REQ
;
3302 memcpy(&bargs
->data
, &bctl
->data
, sizeof(bargs
->data
));
3303 memcpy(&bargs
->meta
, &bctl
->meta
, sizeof(bargs
->meta
));
3304 memcpy(&bargs
->sys
, &bctl
->sys
, sizeof(bargs
->sys
));
3307 spin_lock(&fs_info
->balance_lock
);
3308 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3309 spin_unlock(&fs_info
->balance_lock
);
3311 memcpy(&bargs
->stat
, &bctl
->stat
, sizeof(bargs
->stat
));
3315 static long btrfs_ioctl_balance(struct file
*file
, void __user
*arg
)
3317 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3318 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3319 struct btrfs_ioctl_balance_args
*bargs
;
3320 struct btrfs_balance_control
*bctl
;
3323 if (!capable(CAP_SYS_ADMIN
))
3326 ret
= mnt_want_write_file(file
);
3330 mutex_lock(&fs_info
->volume_mutex
);
3331 mutex_lock(&fs_info
->balance_mutex
);
3334 bargs
= memdup_user(arg
, sizeof(*bargs
));
3335 if (IS_ERR(bargs
)) {
3336 ret
= PTR_ERR(bargs
);
3340 if (bargs
->flags
& BTRFS_BALANCE_RESUME
) {
3341 if (!fs_info
->balance_ctl
) {
3346 bctl
= fs_info
->balance_ctl
;
3347 spin_lock(&fs_info
->balance_lock
);
3348 bctl
->flags
|= BTRFS_BALANCE_RESUME
;
3349 spin_unlock(&fs_info
->balance_lock
);
3357 if (fs_info
->balance_ctl
) {
3362 bctl
= kzalloc(sizeof(*bctl
), GFP_NOFS
);
3368 bctl
->fs_info
= fs_info
;
3370 memcpy(&bctl
->data
, &bargs
->data
, sizeof(bctl
->data
));
3371 memcpy(&bctl
->meta
, &bargs
->meta
, sizeof(bctl
->meta
));
3372 memcpy(&bctl
->sys
, &bargs
->sys
, sizeof(bctl
->sys
));
3374 bctl
->flags
= bargs
->flags
;
3376 /* balance everything - no filters */
3377 bctl
->flags
|= BTRFS_BALANCE_TYPE_MASK
;
3381 ret
= btrfs_balance(bctl
, bargs
);
3383 * bctl is freed in __cancel_balance or in free_fs_info if
3384 * restriper was paused all the way until unmount
3387 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3394 mutex_unlock(&fs_info
->balance_mutex
);
3395 mutex_unlock(&fs_info
->volume_mutex
);
3396 mnt_drop_write_file(file
);
3400 static long btrfs_ioctl_balance_ctl(struct btrfs_root
*root
, int cmd
)
3402 if (!capable(CAP_SYS_ADMIN
))
3406 case BTRFS_BALANCE_CTL_PAUSE
:
3407 return btrfs_pause_balance(root
->fs_info
);
3408 case BTRFS_BALANCE_CTL_CANCEL
:
3409 return btrfs_cancel_balance(root
->fs_info
);
3415 static long btrfs_ioctl_balance_progress(struct btrfs_root
*root
,
3418 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3419 struct btrfs_ioctl_balance_args
*bargs
;
3422 if (!capable(CAP_SYS_ADMIN
))
3425 mutex_lock(&fs_info
->balance_mutex
);
3426 if (!fs_info
->balance_ctl
) {
3431 bargs
= kzalloc(sizeof(*bargs
), GFP_NOFS
);
3437 update_ioctl_balance_args(fs_info
, 1, bargs
);
3439 if (copy_to_user(arg
, bargs
, sizeof(*bargs
)))
3444 mutex_unlock(&fs_info
->balance_mutex
);
3448 static long btrfs_ioctl_quota_ctl(struct btrfs_root
*root
, void __user
*arg
)
3450 struct btrfs_ioctl_quota_ctl_args
*sa
;
3451 struct btrfs_trans_handle
*trans
= NULL
;
3455 if (!capable(CAP_SYS_ADMIN
))
3458 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
3461 sa
= memdup_user(arg
, sizeof(*sa
));
3465 if (sa
->cmd
!= BTRFS_QUOTA_CTL_RESCAN
) {
3466 trans
= btrfs_start_transaction(root
, 2);
3467 if (IS_ERR(trans
)) {
3468 ret
= PTR_ERR(trans
);
3474 case BTRFS_QUOTA_CTL_ENABLE
:
3475 ret
= btrfs_quota_enable(trans
, root
->fs_info
);
3477 case BTRFS_QUOTA_CTL_DISABLE
:
3478 ret
= btrfs_quota_disable(trans
, root
->fs_info
);
3480 case BTRFS_QUOTA_CTL_RESCAN
:
3481 ret
= btrfs_quota_rescan(root
->fs_info
);
3488 if (copy_to_user(arg
, sa
, sizeof(*sa
)))
3492 err
= btrfs_commit_transaction(trans
, root
);
3502 static long btrfs_ioctl_qgroup_assign(struct btrfs_root
*root
, void __user
*arg
)
3504 struct btrfs_ioctl_qgroup_assign_args
*sa
;
3505 struct btrfs_trans_handle
*trans
;
3509 if (!capable(CAP_SYS_ADMIN
))
3512 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
3515 sa
= memdup_user(arg
, sizeof(*sa
));
3519 trans
= btrfs_join_transaction(root
);
3520 if (IS_ERR(trans
)) {
3521 ret
= PTR_ERR(trans
);
3525 /* FIXME: check if the IDs really exist */
3527 ret
= btrfs_add_qgroup_relation(trans
, root
->fs_info
,
3530 ret
= btrfs_del_qgroup_relation(trans
, root
->fs_info
,
3534 err
= btrfs_end_transaction(trans
, root
);
3543 static long btrfs_ioctl_qgroup_create(struct btrfs_root
*root
, void __user
*arg
)
3545 struct btrfs_ioctl_qgroup_create_args
*sa
;
3546 struct btrfs_trans_handle
*trans
;
3550 if (!capable(CAP_SYS_ADMIN
))
3553 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
3556 sa
= memdup_user(arg
, sizeof(*sa
));
3560 trans
= btrfs_join_transaction(root
);
3561 if (IS_ERR(trans
)) {
3562 ret
= PTR_ERR(trans
);
3566 /* FIXME: check if the IDs really exist */
3568 ret
= btrfs_create_qgroup(trans
, root
->fs_info
, sa
->qgroupid
,
3571 ret
= btrfs_remove_qgroup(trans
, root
->fs_info
, sa
->qgroupid
);
3574 err
= btrfs_end_transaction(trans
, root
);
3583 static long btrfs_ioctl_qgroup_limit(struct btrfs_root
*root
, void __user
*arg
)
3585 struct btrfs_ioctl_qgroup_limit_args
*sa
;
3586 struct btrfs_trans_handle
*trans
;
3591 if (!capable(CAP_SYS_ADMIN
))
3594 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
3597 sa
= memdup_user(arg
, sizeof(*sa
));
3601 trans
= btrfs_join_transaction(root
);
3602 if (IS_ERR(trans
)) {
3603 ret
= PTR_ERR(trans
);
3607 qgroupid
= sa
->qgroupid
;
3609 /* take the current subvol as qgroup */
3610 qgroupid
= root
->root_key
.objectid
;
3613 /* FIXME: check if the IDs really exist */
3614 ret
= btrfs_limit_qgroup(trans
, root
->fs_info
, qgroupid
, &sa
->lim
);
3616 err
= btrfs_end_transaction(trans
, root
);
3625 static long btrfs_ioctl_set_received_subvol(struct file
*file
,
3628 struct btrfs_ioctl_received_subvol_args
*sa
= NULL
;
3629 struct inode
*inode
= fdentry(file
)->d_inode
;
3630 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3631 struct btrfs_root_item
*root_item
= &root
->root_item
;
3632 struct btrfs_trans_handle
*trans
;
3633 struct timespec ct
= CURRENT_TIME
;
3636 ret
= mnt_want_write_file(file
);
3640 down_write(&root
->fs_info
->subvol_sem
);
3642 if (btrfs_ino(inode
) != BTRFS_FIRST_FREE_OBJECTID
) {
3647 if (btrfs_root_readonly(root
)) {
3652 if (!inode_owner_or_capable(inode
)) {
3657 sa
= memdup_user(arg
, sizeof(*sa
));
3664 trans
= btrfs_start_transaction(root
, 1);
3665 if (IS_ERR(trans
)) {
3666 ret
= PTR_ERR(trans
);
3671 sa
->rtransid
= trans
->transid
;
3672 sa
->rtime
.sec
= ct
.tv_sec
;
3673 sa
->rtime
.nsec
= ct
.tv_nsec
;
3675 memcpy(root_item
->received_uuid
, sa
->uuid
, BTRFS_UUID_SIZE
);
3676 btrfs_set_root_stransid(root_item
, sa
->stransid
);
3677 btrfs_set_root_rtransid(root_item
, sa
->rtransid
);
3678 root_item
->stime
.sec
= cpu_to_le64(sa
->stime
.sec
);
3679 root_item
->stime
.nsec
= cpu_to_le32(sa
->stime
.nsec
);
3680 root_item
->rtime
.sec
= cpu_to_le64(sa
->rtime
.sec
);
3681 root_item
->rtime
.nsec
= cpu_to_le32(sa
->rtime
.nsec
);
3683 ret
= btrfs_update_root(trans
, root
->fs_info
->tree_root
,
3684 &root
->root_key
, &root
->root_item
);
3686 btrfs_end_transaction(trans
, root
);
3690 ret
= btrfs_commit_transaction(trans
, root
);
3695 ret
= copy_to_user(arg
, sa
, sizeof(*sa
));
3701 up_write(&root
->fs_info
->subvol_sem
);
3702 mnt_drop_write_file(file
);
3706 long btrfs_ioctl(struct file
*file
, unsigned int
3707 cmd
, unsigned long arg
)
3709 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
3710 void __user
*argp
= (void __user
*)arg
;
3713 case FS_IOC_GETFLAGS
:
3714 return btrfs_ioctl_getflags(file
, argp
);
3715 case FS_IOC_SETFLAGS
:
3716 return btrfs_ioctl_setflags(file
, argp
);
3717 case FS_IOC_GETVERSION
:
3718 return btrfs_ioctl_getversion(file
, argp
);
3720 return btrfs_ioctl_fitrim(file
, argp
);
3721 case BTRFS_IOC_SNAP_CREATE
:
3722 return btrfs_ioctl_snap_create(file
, argp
, 0);
3723 case BTRFS_IOC_SNAP_CREATE_V2
:
3724 return btrfs_ioctl_snap_create_v2(file
, argp
, 0);
3725 case BTRFS_IOC_SUBVOL_CREATE
:
3726 return btrfs_ioctl_snap_create(file
, argp
, 1);
3727 case BTRFS_IOC_SUBVOL_CREATE_V2
:
3728 return btrfs_ioctl_snap_create_v2(file
, argp
, 1);
3729 case BTRFS_IOC_SNAP_DESTROY
:
3730 return btrfs_ioctl_snap_destroy(file
, argp
);
3731 case BTRFS_IOC_SUBVOL_GETFLAGS
:
3732 return btrfs_ioctl_subvol_getflags(file
, argp
);
3733 case BTRFS_IOC_SUBVOL_SETFLAGS
:
3734 return btrfs_ioctl_subvol_setflags(file
, argp
);
3735 case BTRFS_IOC_DEFAULT_SUBVOL
:
3736 return btrfs_ioctl_default_subvol(file
, argp
);
3737 case BTRFS_IOC_DEFRAG
:
3738 return btrfs_ioctl_defrag(file
, NULL
);
3739 case BTRFS_IOC_DEFRAG_RANGE
:
3740 return btrfs_ioctl_defrag(file
, argp
);
3741 case BTRFS_IOC_RESIZE
:
3742 return btrfs_ioctl_resize(root
, argp
);
3743 case BTRFS_IOC_ADD_DEV
:
3744 return btrfs_ioctl_add_dev(root
, argp
);
3745 case BTRFS_IOC_RM_DEV
:
3746 return btrfs_ioctl_rm_dev(root
, argp
);
3747 case BTRFS_IOC_FS_INFO
:
3748 return btrfs_ioctl_fs_info(root
, argp
);
3749 case BTRFS_IOC_DEV_INFO
:
3750 return btrfs_ioctl_dev_info(root
, argp
);
3751 case BTRFS_IOC_BALANCE
:
3752 return btrfs_ioctl_balance(file
, NULL
);
3753 case BTRFS_IOC_CLONE
:
3754 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
3755 case BTRFS_IOC_CLONE_RANGE
:
3756 return btrfs_ioctl_clone_range(file
, argp
);
3757 case BTRFS_IOC_TRANS_START
:
3758 return btrfs_ioctl_trans_start(file
);
3759 case BTRFS_IOC_TRANS_END
:
3760 return btrfs_ioctl_trans_end(file
);
3761 case BTRFS_IOC_TREE_SEARCH
:
3762 return btrfs_ioctl_tree_search(file
, argp
);
3763 case BTRFS_IOC_INO_LOOKUP
:
3764 return btrfs_ioctl_ino_lookup(file
, argp
);
3765 case BTRFS_IOC_INO_PATHS
:
3766 return btrfs_ioctl_ino_to_path(root
, argp
);
3767 case BTRFS_IOC_LOGICAL_INO
:
3768 return btrfs_ioctl_logical_to_ino(root
, argp
);
3769 case BTRFS_IOC_SPACE_INFO
:
3770 return btrfs_ioctl_space_info(root
, argp
);
3771 case BTRFS_IOC_SYNC
:
3772 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
3774 case BTRFS_IOC_START_SYNC
:
3775 return btrfs_ioctl_start_sync(file
, argp
);
3776 case BTRFS_IOC_WAIT_SYNC
:
3777 return btrfs_ioctl_wait_sync(file
, argp
);
3778 case BTRFS_IOC_SCRUB
:
3779 return btrfs_ioctl_scrub(root
, argp
);
3780 case BTRFS_IOC_SCRUB_CANCEL
:
3781 return btrfs_ioctl_scrub_cancel(root
, argp
);
3782 case BTRFS_IOC_SCRUB_PROGRESS
:
3783 return btrfs_ioctl_scrub_progress(root
, argp
);
3784 case BTRFS_IOC_BALANCE_V2
:
3785 return btrfs_ioctl_balance(file
, argp
);
3786 case BTRFS_IOC_BALANCE_CTL
:
3787 return btrfs_ioctl_balance_ctl(root
, arg
);
3788 case BTRFS_IOC_BALANCE_PROGRESS
:
3789 return btrfs_ioctl_balance_progress(root
, argp
);
3790 case BTRFS_IOC_SET_RECEIVED_SUBVOL
:
3791 return btrfs_ioctl_set_received_subvol(file
, argp
);
3792 case BTRFS_IOC_SEND
:
3793 return btrfs_ioctl_send(file
, argp
);
3794 case BTRFS_IOC_GET_DEV_STATS
:
3795 return btrfs_ioctl_get_dev_stats(root
, argp
);
3796 case BTRFS_IOC_QUOTA_CTL
:
3797 return btrfs_ioctl_quota_ctl(root
, argp
);
3798 case BTRFS_IOC_QGROUP_ASSIGN
:
3799 return btrfs_ioctl_qgroup_assign(root
, argp
);
3800 case BTRFS_IOC_QGROUP_CREATE
:
3801 return btrfs_ioctl_qgroup_create(root
, argp
);
3802 case BTRFS_IOC_QGROUP_LIMIT
:
3803 return btrfs_ioctl_qgroup_limit(root
, argp
);