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>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
53 /* Mask out flags that are inappropriate for the given type of inode. */
54 static inline __u32
btrfs_mask_flags(umode_t mode
, __u32 flags
)
58 else if (S_ISREG(mode
))
59 return flags
& ~FS_DIRSYNC_FL
;
61 return flags
& (FS_NODUMP_FL
| FS_NOATIME_FL
);
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 static unsigned int btrfs_flags_to_ioctl(unsigned int flags
)
69 unsigned int iflags
= 0;
71 if (flags
& BTRFS_INODE_SYNC
)
73 if (flags
& BTRFS_INODE_IMMUTABLE
)
74 iflags
|= FS_IMMUTABLE_FL
;
75 if (flags
& BTRFS_INODE_APPEND
)
76 iflags
|= FS_APPEND_FL
;
77 if (flags
& BTRFS_INODE_NODUMP
)
78 iflags
|= FS_NODUMP_FL
;
79 if (flags
& BTRFS_INODE_NOATIME
)
80 iflags
|= FS_NOATIME_FL
;
81 if (flags
& BTRFS_INODE_DIRSYNC
)
82 iflags
|= FS_DIRSYNC_FL
;
88 * Update inode->i_flags based on the btrfs internal flags.
90 void btrfs_update_iflags(struct inode
*inode
)
92 struct btrfs_inode
*ip
= BTRFS_I(inode
);
94 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
96 if (ip
->flags
& BTRFS_INODE_SYNC
)
97 inode
->i_flags
|= S_SYNC
;
98 if (ip
->flags
& BTRFS_INODE_IMMUTABLE
)
99 inode
->i_flags
|= S_IMMUTABLE
;
100 if (ip
->flags
& BTRFS_INODE_APPEND
)
101 inode
->i_flags
|= S_APPEND
;
102 if (ip
->flags
& BTRFS_INODE_NOATIME
)
103 inode
->i_flags
|= S_NOATIME
;
104 if (ip
->flags
& BTRFS_INODE_DIRSYNC
)
105 inode
->i_flags
|= S_DIRSYNC
;
109 * Inherit flags from the parent inode.
111 * Unlike extN we don't have any flags we don't want to inherit currently.
113 void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
120 flags
= BTRFS_I(dir
)->flags
;
122 if (S_ISREG(inode
->i_mode
))
123 flags
&= ~BTRFS_INODE_DIRSYNC
;
124 else if (!S_ISDIR(inode
->i_mode
))
125 flags
&= (BTRFS_INODE_NODUMP
| BTRFS_INODE_NOATIME
);
127 BTRFS_I(inode
)->flags
= flags
;
128 btrfs_update_iflags(inode
);
131 static int btrfs_ioctl_getflags(struct file
*file
, void __user
*arg
)
133 struct btrfs_inode
*ip
= BTRFS_I(file
->f_path
.dentry
->d_inode
);
134 unsigned int flags
= btrfs_flags_to_ioctl(ip
->flags
);
136 if (copy_to_user(arg
, &flags
, sizeof(flags
)))
141 static int btrfs_ioctl_setflags(struct file
*file
, void __user
*arg
)
143 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
144 struct btrfs_inode
*ip
= BTRFS_I(inode
);
145 struct btrfs_root
*root
= ip
->root
;
146 struct btrfs_trans_handle
*trans
;
147 unsigned int flags
, oldflags
;
150 if (copy_from_user(&flags
, arg
, sizeof(flags
)))
153 if (flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| \
154 FS_NOATIME_FL
| FS_NODUMP_FL
| \
155 FS_SYNC_FL
| FS_DIRSYNC_FL
))
158 if (!is_owner_or_cap(inode
))
161 mutex_lock(&inode
->i_mutex
);
163 flags
= btrfs_mask_flags(inode
->i_mode
, flags
);
164 oldflags
= btrfs_flags_to_ioctl(ip
->flags
);
165 if ((flags
^ oldflags
) & (FS_APPEND_FL
| FS_IMMUTABLE_FL
)) {
166 if (!capable(CAP_LINUX_IMMUTABLE
)) {
172 ret
= mnt_want_write(file
->f_path
.mnt
);
176 if (flags
& FS_SYNC_FL
)
177 ip
->flags
|= BTRFS_INODE_SYNC
;
179 ip
->flags
&= ~BTRFS_INODE_SYNC
;
180 if (flags
& FS_IMMUTABLE_FL
)
181 ip
->flags
|= BTRFS_INODE_IMMUTABLE
;
183 ip
->flags
&= ~BTRFS_INODE_IMMUTABLE
;
184 if (flags
& FS_APPEND_FL
)
185 ip
->flags
|= BTRFS_INODE_APPEND
;
187 ip
->flags
&= ~BTRFS_INODE_APPEND
;
188 if (flags
& FS_NODUMP_FL
)
189 ip
->flags
|= BTRFS_INODE_NODUMP
;
191 ip
->flags
&= ~BTRFS_INODE_NODUMP
;
192 if (flags
& FS_NOATIME_FL
)
193 ip
->flags
|= BTRFS_INODE_NOATIME
;
195 ip
->flags
&= ~BTRFS_INODE_NOATIME
;
196 if (flags
& FS_DIRSYNC_FL
)
197 ip
->flags
|= BTRFS_INODE_DIRSYNC
;
199 ip
->flags
&= ~BTRFS_INODE_DIRSYNC
;
202 trans
= btrfs_join_transaction(root
, 1);
205 ret
= btrfs_update_inode(trans
, root
, inode
);
208 btrfs_update_iflags(inode
);
209 inode
->i_ctime
= CURRENT_TIME
;
210 btrfs_end_transaction(trans
, root
);
212 mnt_drop_write(file
->f_path
.mnt
);
214 mutex_unlock(&inode
->i_mutex
);
218 static int btrfs_ioctl_getversion(struct file
*file
, int __user
*arg
)
220 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
222 return put_user(inode
->i_generation
, arg
);
225 static noinline
int create_subvol(struct btrfs_root
*root
,
226 struct dentry
*dentry
,
227 char *name
, int namelen
,
230 struct btrfs_trans_handle
*trans
;
231 struct btrfs_key key
;
232 struct btrfs_root_item root_item
;
233 struct btrfs_inode_item
*inode_item
;
234 struct extent_buffer
*leaf
;
235 struct btrfs_root
*new_root
;
236 struct inode
*dir
= dentry
->d_parent
->d_inode
;
240 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
243 ret
= btrfs_find_free_objectid(NULL
, root
->fs_info
->tree_root
,
253 trans
= btrfs_start_transaction(root
, 6);
255 return PTR_ERR(trans
);
257 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
258 0, objectid
, NULL
, 0, 0, 0);
264 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
265 btrfs_set_header_bytenr(leaf
, leaf
->start
);
266 btrfs_set_header_generation(leaf
, trans
->transid
);
267 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
268 btrfs_set_header_owner(leaf
, objectid
);
270 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
271 (unsigned long)btrfs_header_fsid(leaf
),
273 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
274 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
276 btrfs_mark_buffer_dirty(leaf
);
278 inode_item
= &root_item
.inode
;
279 memset(inode_item
, 0, sizeof(*inode_item
));
280 inode_item
->generation
= cpu_to_le64(1);
281 inode_item
->size
= cpu_to_le64(3);
282 inode_item
->nlink
= cpu_to_le32(1);
283 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
284 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
286 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
287 btrfs_set_root_generation(&root_item
, trans
->transid
);
288 btrfs_set_root_level(&root_item
, 0);
289 btrfs_set_root_refs(&root_item
, 1);
290 btrfs_set_root_used(&root_item
, leaf
->len
);
291 btrfs_set_root_last_snapshot(&root_item
, 0);
293 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
294 root_item
.drop_level
= 0;
296 btrfs_tree_unlock(leaf
);
297 free_extent_buffer(leaf
);
300 btrfs_set_root_dirid(&root_item
, new_dirid
);
302 key
.objectid
= objectid
;
304 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
305 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
310 key
.offset
= (u64
)-1;
311 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
312 BUG_ON(IS_ERR(new_root
));
314 btrfs_record_root_in_trans(trans
, new_root
);
316 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
317 BTRFS_I(dir
)->block_group
);
319 * insert the directory item
321 ret
= btrfs_set_inode_index(dir
, &index
);
324 ret
= btrfs_insert_dir_item(trans
, root
,
325 name
, namelen
, dir
->i_ino
, &key
,
326 BTRFS_FT_DIR
, index
);
330 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
331 ret
= btrfs_update_inode(trans
, root
, dir
);
334 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
335 objectid
, root
->root_key
.objectid
,
336 dir
->i_ino
, index
, name
, namelen
);
340 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
343 *async_transid
= trans
->transid
;
344 err
= btrfs_commit_transaction_async(trans
, root
, 1);
346 err
= btrfs_commit_transaction(trans
, root
);
353 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
354 char *name
, int namelen
, u64
*async_transid
)
357 struct btrfs_pending_snapshot
*pending_snapshot
;
358 struct btrfs_trans_handle
*trans
;
364 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
365 if (!pending_snapshot
)
368 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
369 pending_snapshot
->dentry
= dentry
;
370 pending_snapshot
->root
= root
;
372 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
374 ret
= PTR_ERR(trans
);
378 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
381 list_add(&pending_snapshot
->list
,
382 &trans
->transaction
->pending_snapshots
);
384 *async_transid
= trans
->transid
;
385 ret
= btrfs_commit_transaction_async(trans
,
386 root
->fs_info
->extent_root
, 1);
388 ret
= btrfs_commit_transaction(trans
,
389 root
->fs_info
->extent_root
);
393 ret
= pending_snapshot
->error
;
397 btrfs_orphan_cleanup(pending_snapshot
->snap
);
399 inode
= btrfs_lookup_dentry(dentry
->d_parent
->d_inode
, dentry
);
401 ret
= PTR_ERR(inode
);
405 d_instantiate(dentry
, inode
);
408 kfree(pending_snapshot
);
412 /* copy of check_sticky in fs/namei.c()
413 * It's inline, so penalty for filesystems that don't use sticky bit is
416 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
418 uid_t fsuid
= current_fsuid();
420 if (!(dir
->i_mode
& S_ISVTX
))
422 if (inode
->i_uid
== fsuid
)
424 if (dir
->i_uid
== fsuid
)
426 return !capable(CAP_FOWNER
);
429 /* copy of may_delete in fs/namei.c()
430 * Check whether we can remove a link victim from directory dir, check
431 * whether the type of victim is right.
432 * 1. We can't do it if dir is read-only (done in permission())
433 * 2. We should have write and exec permissions on dir
434 * 3. We can't remove anything from append-only dir
435 * 4. We can't do anything with immutable dir (done in permission())
436 * 5. If the sticky bit on dir is set we should either
437 * a. be owner of dir, or
438 * b. be owner of victim, or
439 * c. have CAP_FOWNER capability
440 * 6. If the victim is append-only or immutable we can't do antyhing with
441 * links pointing to it.
442 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
443 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
444 * 9. We can't remove a root or mountpoint.
445 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
446 * nfs_async_unlink().
449 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
453 if (!victim
->d_inode
)
456 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
457 audit_inode_child(victim
, dir
);
459 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
464 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
465 IS_APPEND(victim
->d_inode
)||
466 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
469 if (!S_ISDIR(victim
->d_inode
->i_mode
))
473 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
477 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
482 /* copy of may_create in fs/namei.c() */
483 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
489 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
493 * Create a new subvolume below @parent. This is largely modeled after
494 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
495 * inside this filesystem so it's quite a bit simpler.
497 static noinline
int btrfs_mksubvol(struct path
*parent
,
498 char *name
, int namelen
,
499 struct btrfs_root
*snap_src
,
502 struct inode
*dir
= parent
->dentry
->d_inode
;
503 struct dentry
*dentry
;
506 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
508 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
509 error
= PTR_ERR(dentry
);
517 error
= mnt_want_write(parent
->mnt
);
521 error
= btrfs_may_create(dir
, dentry
);
525 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
527 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
531 error
= create_snapshot(snap_src
, dentry
,
532 name
, namelen
, async_transid
);
534 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
535 name
, namelen
, async_transid
);
538 fsnotify_mkdir(dir
, dentry
);
540 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
542 mnt_drop_write(parent
->mnt
);
546 mutex_unlock(&dir
->i_mutex
);
550 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
551 int thresh
, u64
*last_len
, u64
*skip
,
554 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
555 struct extent_map
*em
= NULL
;
556 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
564 * make sure that once we start defragging and extent, we keep on
567 if (start
< *defrag_end
)
573 * hopefully we have this extent in the tree already, try without
574 * the full extent lock
576 read_lock(&em_tree
->lock
);
577 em
= lookup_extent_mapping(em_tree
, start
, len
);
578 read_unlock(&em_tree
->lock
);
581 /* get the big lock and read metadata off disk */
582 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
583 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
584 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
590 /* this will cover holes, and inline extents */
591 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
595 * we hit a real extent, if it is big don't bother defragging it again
597 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
601 * last_len ends up being a counter of how many bytes we've defragged.
602 * every time we choose not to defrag an extent, we reset *last_len
603 * so that the next tiny extent will force a defrag.
605 * The end result of this is that tiny extents before a single big
606 * extent will force at least part of that big extent to be defragged.
610 *defrag_end
= extent_map_end(em
);
613 *skip
= extent_map_end(em
);
621 static int btrfs_defrag_file(struct file
*file
,
622 struct btrfs_ioctl_defrag_range_args
*range
)
624 struct inode
*inode
= fdentry(file
)->d_inode
;
625 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
626 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
627 struct btrfs_ordered_extent
*ordered
;
629 unsigned long last_index
;
630 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
631 unsigned long total_read
= 0;
640 if (inode
->i_size
== 0)
643 if (range
->start
+ range
->len
> range
->start
) {
644 last_index
= min_t(u64
, inode
->i_size
- 1,
645 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
647 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
650 i
= range
->start
>> PAGE_CACHE_SHIFT
;
651 while (i
<= last_index
) {
652 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
654 range
->extent_thresh
,
659 * the should_defrag function tells us how much to skip
660 * bump our counter by the suggested amount
662 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
663 i
= max(i
+ 1, next
);
667 if (total_read
% ra_pages
== 0) {
668 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
669 min(last_index
, i
+ ra_pages
- 1));
672 mutex_lock(&inode
->i_mutex
);
673 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
674 BTRFS_I(inode
)->force_compress
= 1;
676 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
680 if (inode
->i_size
== 0 ||
681 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
683 goto err_reservations
;
686 page
= grab_cache_page(inode
->i_mapping
, i
);
689 goto err_reservations
;
692 if (!PageUptodate(page
)) {
693 btrfs_readpage(NULL
, page
);
695 if (!PageUptodate(page
)) {
697 page_cache_release(page
);
699 goto err_reservations
;
703 if (page
->mapping
!= inode
->i_mapping
) {
705 page_cache_release(page
);
709 wait_on_page_writeback(page
);
711 if (PageDirty(page
)) {
712 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
716 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
717 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
718 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
720 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
722 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
724 page_cache_release(page
);
725 btrfs_start_ordered_extent(inode
, ordered
, 1);
726 btrfs_put_ordered_extent(ordered
);
729 set_page_extent_mapped(page
);
732 * this makes sure page_mkwrite is called on the
733 * page if it is dirtied again later
735 clear_page_dirty_for_io(page
);
736 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
737 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
738 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
740 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
741 ClearPageChecked(page
);
742 set_page_dirty(page
);
743 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
747 page_cache_release(page
);
748 mutex_unlock(&inode
->i_mutex
);
750 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
754 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
755 filemap_flush(inode
->i_mapping
);
757 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
758 /* the filemap_flush will queue IO into the worker threads, but
759 * we have to make sure the IO is actually started and that
760 * ordered extents get created before we return
762 atomic_inc(&root
->fs_info
->async_submit_draining
);
763 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
764 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
765 wait_event(root
->fs_info
->async_submit_wait
,
766 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
767 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
769 atomic_dec(&root
->fs_info
->async_submit_draining
);
771 mutex_lock(&inode
->i_mutex
);
772 BTRFS_I(inode
)->force_compress
= 0;
773 mutex_unlock(&inode
->i_mutex
);
779 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
781 mutex_unlock(&inode
->i_mutex
);
785 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
791 struct btrfs_ioctl_vol_args
*vol_args
;
792 struct btrfs_trans_handle
*trans
;
793 struct btrfs_device
*device
= NULL
;
799 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
802 if (!capable(CAP_SYS_ADMIN
))
805 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
806 if (IS_ERR(vol_args
))
807 return PTR_ERR(vol_args
);
809 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
811 mutex_lock(&root
->fs_info
->volume_mutex
);
812 sizestr
= vol_args
->name
;
813 devstr
= strchr(sizestr
, ':');
816 sizestr
= devstr
+ 1;
818 devstr
= vol_args
->name
;
819 devid
= simple_strtoull(devstr
, &end
, 10);
820 printk(KERN_INFO
"resizing devid %llu\n",
821 (unsigned long long)devid
);
823 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
825 printk(KERN_INFO
"resizer unable to find device %llu\n",
826 (unsigned long long)devid
);
830 if (!strcmp(sizestr
, "max"))
831 new_size
= device
->bdev
->bd_inode
->i_size
;
833 if (sizestr
[0] == '-') {
836 } else if (sizestr
[0] == '+') {
840 new_size
= memparse(sizestr
, NULL
);
847 old_size
= device
->total_bytes
;
850 if (new_size
> old_size
) {
854 new_size
= old_size
- new_size
;
855 } else if (mod
> 0) {
856 new_size
= old_size
+ new_size
;
859 if (new_size
< 256 * 1024 * 1024) {
863 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
868 do_div(new_size
, root
->sectorsize
);
869 new_size
*= root
->sectorsize
;
871 printk(KERN_INFO
"new size for %s is %llu\n",
872 device
->name
, (unsigned long long)new_size
);
874 if (new_size
> old_size
) {
875 trans
= btrfs_start_transaction(root
, 0);
876 ret
= btrfs_grow_device(trans
, device
, new_size
);
877 btrfs_commit_transaction(trans
, root
);
879 ret
= btrfs_shrink_device(device
, new_size
);
883 mutex_unlock(&root
->fs_info
->volume_mutex
);
888 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
894 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
895 struct file
*src_file
;
899 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
902 namelen
= strlen(name
);
903 if (strchr(name
, '/')) {
909 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
912 struct inode
*src_inode
;
919 src_inode
= src_file
->f_path
.dentry
->d_inode
;
920 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
921 printk(KERN_INFO
"btrfs: Snapshot src from "
927 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
928 BTRFS_I(src_inode
)->root
,
936 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
937 void __user
*arg
, int subvol
,
940 struct btrfs_ioctl_vol_args
*vol_args
= NULL
;
941 struct btrfs_ioctl_async_vol_args
*async_vol_args
= NULL
;
948 async_vol_args
= memdup_user(arg
, sizeof(*async_vol_args
));
949 if (IS_ERR(async_vol_args
))
950 return PTR_ERR(async_vol_args
);
952 name
= async_vol_args
->name
;
953 fd
= async_vol_args
->fd
;
954 async_vol_args
->name
[BTRFS_SNAPSHOT_NAME_MAX
] = '\0';
956 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
957 if (IS_ERR(vol_args
))
958 return PTR_ERR(vol_args
);
959 name
= vol_args
->name
;
961 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
964 ret
= btrfs_ioctl_snap_create_transid(file
, name
, fd
,
968 if (copy_to_user(arg
+
969 offsetof(struct btrfs_ioctl_async_vol_args
,
970 transid
), &transid
, sizeof(transid
)))
975 kfree(async_vol_args
);
981 * helper to check if the subvolume references other subvolumes
983 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
985 struct btrfs_path
*path
;
986 struct btrfs_key key
;
989 path
= btrfs_alloc_path();
993 key
.objectid
= root
->root_key
.objectid
;
994 key
.type
= BTRFS_ROOT_REF_KEY
;
995 key
.offset
= (u64
)-1;
997 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1004 if (path
->slots
[0] > 0) {
1006 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1007 if (key
.objectid
== root
->root_key
.objectid
&&
1008 key
.type
== BTRFS_ROOT_REF_KEY
)
1012 btrfs_free_path(path
);
1016 static noinline
int key_in_sk(struct btrfs_key
*key
,
1017 struct btrfs_ioctl_search_key
*sk
)
1019 struct btrfs_key test
;
1022 test
.objectid
= sk
->min_objectid
;
1023 test
.type
= sk
->min_type
;
1024 test
.offset
= sk
->min_offset
;
1026 ret
= btrfs_comp_cpu_keys(key
, &test
);
1030 test
.objectid
= sk
->max_objectid
;
1031 test
.type
= sk
->max_type
;
1032 test
.offset
= sk
->max_offset
;
1034 ret
= btrfs_comp_cpu_keys(key
, &test
);
1040 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1041 struct btrfs_path
*path
,
1042 struct btrfs_key
*key
,
1043 struct btrfs_ioctl_search_key
*sk
,
1045 unsigned long *sk_offset
,
1049 struct extent_buffer
*leaf
;
1050 struct btrfs_ioctl_search_header sh
;
1051 unsigned long item_off
;
1052 unsigned long item_len
;
1059 leaf
= path
->nodes
[0];
1060 slot
= path
->slots
[0];
1061 nritems
= btrfs_header_nritems(leaf
);
1063 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1067 found_transid
= btrfs_header_generation(leaf
);
1069 for (i
= slot
; i
< nritems
; i
++) {
1070 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1071 item_len
= btrfs_item_size_nr(leaf
, i
);
1073 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1076 if (sizeof(sh
) + item_len
+ *sk_offset
>
1077 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1082 btrfs_item_key_to_cpu(leaf
, key
, i
);
1083 if (!key_in_sk(key
, sk
))
1086 sh
.objectid
= key
->objectid
;
1087 sh
.offset
= key
->offset
;
1088 sh
.type
= key
->type
;
1090 sh
.transid
= found_transid
;
1092 /* copy search result header */
1093 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1094 *sk_offset
+= sizeof(sh
);
1097 char *p
= buf
+ *sk_offset
;
1099 read_extent_buffer(leaf
, p
,
1100 item_off
, item_len
);
1101 *sk_offset
+= item_len
;
1105 if (*num_found
>= sk
->nr_items
)
1110 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1112 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1115 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1122 *num_found
+= found
;
1126 static noinline
int search_ioctl(struct inode
*inode
,
1127 struct btrfs_ioctl_search_args
*args
)
1129 struct btrfs_root
*root
;
1130 struct btrfs_key key
;
1131 struct btrfs_key max_key
;
1132 struct btrfs_path
*path
;
1133 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1134 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1137 unsigned long sk_offset
= 0;
1139 path
= btrfs_alloc_path();
1143 if (sk
->tree_id
== 0) {
1144 /* search the root of the inode that was passed */
1145 root
= BTRFS_I(inode
)->root
;
1147 key
.objectid
= sk
->tree_id
;
1148 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1149 key
.offset
= (u64
)-1;
1150 root
= btrfs_read_fs_root_no_name(info
, &key
);
1152 printk(KERN_ERR
"could not find root %llu\n",
1154 btrfs_free_path(path
);
1159 key
.objectid
= sk
->min_objectid
;
1160 key
.type
= sk
->min_type
;
1161 key
.offset
= sk
->min_offset
;
1163 max_key
.objectid
= sk
->max_objectid
;
1164 max_key
.type
= sk
->max_type
;
1165 max_key
.offset
= sk
->max_offset
;
1167 path
->keep_locks
= 1;
1170 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1177 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1178 &sk_offset
, &num_found
);
1179 btrfs_release_path(root
, path
);
1180 if (ret
|| num_found
>= sk
->nr_items
)
1186 sk
->nr_items
= num_found
;
1187 btrfs_free_path(path
);
1191 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1194 struct btrfs_ioctl_search_args
*args
;
1195 struct inode
*inode
;
1198 if (!capable(CAP_SYS_ADMIN
))
1201 args
= memdup_user(argp
, sizeof(*args
));
1203 return PTR_ERR(args
);
1205 inode
= fdentry(file
)->d_inode
;
1206 ret
= search_ioctl(inode
, args
);
1207 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1214 * Search INODE_REFs to identify path name of 'dirid' directory
1215 * in a 'tree_id' tree. and sets path name to 'name'.
1217 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1218 u64 tree_id
, u64 dirid
, char *name
)
1220 struct btrfs_root
*root
;
1221 struct btrfs_key key
;
1227 struct btrfs_inode_ref
*iref
;
1228 struct extent_buffer
*l
;
1229 struct btrfs_path
*path
;
1231 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1236 path
= btrfs_alloc_path();
1240 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1242 key
.objectid
= tree_id
;
1243 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1244 key
.offset
= (u64
)-1;
1245 root
= btrfs_read_fs_root_no_name(info
, &key
);
1247 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1252 key
.objectid
= dirid
;
1253 key
.type
= BTRFS_INODE_REF_KEY
;
1254 key
.offset
= (u64
)-1;
1257 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1262 slot
= path
->slots
[0];
1263 if (ret
> 0 && slot
> 0)
1265 btrfs_item_key_to_cpu(l
, &key
, slot
);
1267 if (ret
> 0 && (key
.objectid
!= dirid
||
1268 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1273 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1274 len
= btrfs_inode_ref_name_len(l
, iref
);
1276 total_len
+= len
+ 1;
1281 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1283 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1286 btrfs_release_path(root
, path
);
1287 key
.objectid
= key
.offset
;
1288 key
.offset
= (u64
)-1;
1289 dirid
= key
.objectid
;
1294 memcpy(name
, ptr
, total_len
);
1295 name
[total_len
]='\0';
1298 btrfs_free_path(path
);
1302 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1305 struct btrfs_ioctl_ino_lookup_args
*args
;
1306 struct inode
*inode
;
1309 if (!capable(CAP_SYS_ADMIN
))
1312 args
= memdup_user(argp
, sizeof(*args
));
1314 return PTR_ERR(args
);
1316 inode
= fdentry(file
)->d_inode
;
1318 if (args
->treeid
== 0)
1319 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1321 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1322 args
->treeid
, args
->objectid
,
1325 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1332 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1335 struct dentry
*parent
= fdentry(file
);
1336 struct dentry
*dentry
;
1337 struct inode
*dir
= parent
->d_inode
;
1338 struct inode
*inode
;
1339 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1340 struct btrfs_root
*dest
= NULL
;
1341 struct btrfs_ioctl_vol_args
*vol_args
;
1342 struct btrfs_trans_handle
*trans
;
1347 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1348 if (IS_ERR(vol_args
))
1349 return PTR_ERR(vol_args
);
1351 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1352 namelen
= strlen(vol_args
->name
);
1353 if (strchr(vol_args
->name
, '/') ||
1354 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1359 err
= mnt_want_write(file
->f_path
.mnt
);
1363 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1364 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1365 if (IS_ERR(dentry
)) {
1366 err
= PTR_ERR(dentry
);
1367 goto out_unlock_dir
;
1370 if (!dentry
->d_inode
) {
1375 inode
= dentry
->d_inode
;
1376 dest
= BTRFS_I(inode
)->root
;
1377 if (!capable(CAP_SYS_ADMIN
)){
1379 * Regular user. Only allow this with a special mount
1380 * option, when the user has write+exec access to the
1381 * subvol root, and when rmdir(2) would have been
1384 * Note that this is _not_ check that the subvol is
1385 * empty or doesn't contain data that we wouldn't
1386 * otherwise be able to delete.
1388 * Users who want to delete empty subvols should try
1392 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1396 * Do not allow deletion if the parent dir is the same
1397 * as the dir to be deleted. That means the ioctl
1398 * must be called on the dentry referencing the root
1399 * of the subvol, not a random directory contained
1406 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1410 /* check if subvolume may be deleted by a non-root user */
1411 err
= btrfs_may_delete(dir
, dentry
, 1);
1416 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1421 mutex_lock(&inode
->i_mutex
);
1422 err
= d_invalidate(dentry
);
1426 down_write(&root
->fs_info
->subvol_sem
);
1428 err
= may_destroy_subvol(dest
);
1432 trans
= btrfs_start_transaction(root
, 0);
1433 if (IS_ERR(trans
)) {
1434 err
= PTR_ERR(trans
);
1437 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1439 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1440 dest
->root_key
.objectid
,
1441 dentry
->d_name
.name
,
1442 dentry
->d_name
.len
);
1445 btrfs_record_root_in_trans(trans
, dest
);
1447 memset(&dest
->root_item
.drop_progress
, 0,
1448 sizeof(dest
->root_item
.drop_progress
));
1449 dest
->root_item
.drop_level
= 0;
1450 btrfs_set_root_refs(&dest
->root_item
, 0);
1452 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1453 ret
= btrfs_insert_orphan_item(trans
,
1454 root
->fs_info
->tree_root
,
1455 dest
->root_key
.objectid
);
1459 ret
= btrfs_end_transaction(trans
, root
);
1461 inode
->i_flags
|= S_DEAD
;
1463 up_write(&root
->fs_info
->subvol_sem
);
1465 mutex_unlock(&inode
->i_mutex
);
1467 shrink_dcache_sb(root
->fs_info
->sb
);
1468 btrfs_invalidate_inodes(dest
);
1474 mutex_unlock(&dir
->i_mutex
);
1475 mnt_drop_write(file
->f_path
.mnt
);
1481 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1483 struct inode
*inode
= fdentry(file
)->d_inode
;
1484 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1485 struct btrfs_ioctl_defrag_range_args
*range
;
1488 ret
= mnt_want_write(file
->f_path
.mnt
);
1492 switch (inode
->i_mode
& S_IFMT
) {
1494 if (!capable(CAP_SYS_ADMIN
)) {
1498 ret
= btrfs_defrag_root(root
, 0);
1501 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1504 if (!(file
->f_mode
& FMODE_WRITE
)) {
1509 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1516 if (copy_from_user(range
, argp
,
1522 /* compression requires us to start the IO */
1523 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1524 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1525 range
->extent_thresh
= (u32
)-1;
1528 /* the rest are all set to zero by kzalloc */
1529 range
->len
= (u64
)-1;
1531 ret
= btrfs_defrag_file(file
, range
);
1538 mnt_drop_write(file
->f_path
.mnt
);
1542 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1544 struct btrfs_ioctl_vol_args
*vol_args
;
1547 if (!capable(CAP_SYS_ADMIN
))
1550 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1551 if (IS_ERR(vol_args
))
1552 return PTR_ERR(vol_args
);
1554 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1555 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1561 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1563 struct btrfs_ioctl_vol_args
*vol_args
;
1566 if (!capable(CAP_SYS_ADMIN
))
1569 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1572 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1573 if (IS_ERR(vol_args
))
1574 return PTR_ERR(vol_args
);
1576 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1577 ret
= btrfs_rm_device(root
, vol_args
->name
);
1583 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1584 u64 off
, u64 olen
, u64 destoff
)
1586 struct inode
*inode
= fdentry(file
)->d_inode
;
1587 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1588 struct file
*src_file
;
1590 struct btrfs_trans_handle
*trans
;
1591 struct btrfs_path
*path
;
1592 struct extent_buffer
*leaf
;
1594 struct btrfs_key key
;
1599 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1604 * - split compressed inline extents. annoying: we need to
1605 * decompress into destination's address_space (the file offset
1606 * may change, so source mapping won't do), then recompress (or
1607 * otherwise reinsert) a subrange.
1608 * - allow ranges within the same file to be cloned (provided
1609 * they don't overlap)?
1612 /* the destination must be opened for writing */
1613 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1616 ret
= mnt_want_write(file
->f_path
.mnt
);
1620 src_file
= fget(srcfd
);
1623 goto out_drop_write
;
1626 src
= src_file
->f_dentry
->d_inode
;
1632 /* the src must be open for reading */
1633 if (!(src_file
->f_mode
& FMODE_READ
))
1637 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1641 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1645 buf
= vmalloc(btrfs_level_size(root
, 0));
1649 path
= btrfs_alloc_path();
1657 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
1658 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
1660 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
1661 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1664 /* determine range to clone */
1666 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1669 olen
= len
= src
->i_size
- off
;
1670 /* if we extend to eof, continue to block boundary */
1671 if (off
+ len
== src
->i_size
)
1672 len
= ALIGN(src
->i_size
, bs
) - off
;
1674 /* verify the end result is block aligned */
1675 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
1676 !IS_ALIGNED(destoff
, bs
))
1679 /* do any pending delalloc/csum calc on src, one way or
1680 another, and lock file content */
1682 struct btrfs_ordered_extent
*ordered
;
1683 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1684 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
1686 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
1687 EXTENT_DELALLOC
, 0, NULL
))
1689 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1691 btrfs_put_ordered_extent(ordered
);
1692 btrfs_wait_ordered_range(src
, off
, len
);
1696 key
.objectid
= src
->i_ino
;
1697 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1702 * note the key will change type as we walk through the
1705 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1709 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1710 if (path
->slots
[0] >= nritems
) {
1711 ret
= btrfs_next_leaf(root
, path
);
1716 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1718 leaf
= path
->nodes
[0];
1719 slot
= path
->slots
[0];
1721 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1722 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1723 key
.objectid
!= src
->i_ino
)
1726 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1727 struct btrfs_file_extent_item
*extent
;
1730 struct btrfs_key new_key
;
1731 u64 disko
= 0, diskl
= 0;
1732 u64 datao
= 0, datal
= 0;
1736 size
= btrfs_item_size_nr(leaf
, slot
);
1737 read_extent_buffer(leaf
, buf
,
1738 btrfs_item_ptr_offset(leaf
, slot
),
1741 extent
= btrfs_item_ptr(leaf
, slot
,
1742 struct btrfs_file_extent_item
);
1743 comp
= btrfs_file_extent_compression(leaf
, extent
);
1744 type
= btrfs_file_extent_type(leaf
, extent
);
1745 if (type
== BTRFS_FILE_EXTENT_REG
||
1746 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1747 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1749 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1751 datao
= btrfs_file_extent_offset(leaf
, extent
);
1752 datal
= btrfs_file_extent_num_bytes(leaf
,
1754 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1755 /* take upper bound, may be compressed */
1756 datal
= btrfs_file_extent_ram_bytes(leaf
,
1759 btrfs_release_path(root
, path
);
1761 if (key
.offset
+ datal
<= off
||
1762 key
.offset
>= off
+len
)
1765 memcpy(&new_key
, &key
, sizeof(new_key
));
1766 new_key
.objectid
= inode
->i_ino
;
1767 new_key
.offset
= key
.offset
+ destoff
- off
;
1769 trans
= btrfs_start_transaction(root
, 1);
1770 if (IS_ERR(trans
)) {
1771 ret
= PTR_ERR(trans
);
1775 if (type
== BTRFS_FILE_EXTENT_REG
||
1776 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1777 if (off
> key
.offset
) {
1778 datao
+= off
- key
.offset
;
1779 datal
-= off
- key
.offset
;
1782 if (key
.offset
+ datal
> off
+ len
)
1783 datal
= off
+ len
- key
.offset
;
1785 ret
= btrfs_drop_extents(trans
, inode
,
1787 new_key
.offset
+ datal
,
1791 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1795 leaf
= path
->nodes
[0];
1796 slot
= path
->slots
[0];
1797 write_extent_buffer(leaf
, buf
,
1798 btrfs_item_ptr_offset(leaf
, slot
),
1801 extent
= btrfs_item_ptr(leaf
, slot
,
1802 struct btrfs_file_extent_item
);
1804 /* disko == 0 means it's a hole */
1808 btrfs_set_file_extent_offset(leaf
, extent
,
1810 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1813 inode_add_bytes(inode
, datal
);
1814 ret
= btrfs_inc_extent_ref(trans
, root
,
1816 root
->root_key
.objectid
,
1818 new_key
.offset
- datao
);
1821 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1824 if (off
> key
.offset
) {
1825 skip
= off
- key
.offset
;
1826 new_key
.offset
+= skip
;
1829 if (key
.offset
+ datal
> off
+len
)
1830 trim
= key
.offset
+ datal
- (off
+len
);
1832 if (comp
&& (skip
|| trim
)) {
1834 btrfs_end_transaction(trans
, root
);
1837 size
-= skip
+ trim
;
1838 datal
-= skip
+ trim
;
1840 ret
= btrfs_drop_extents(trans
, inode
,
1842 new_key
.offset
+ datal
,
1846 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1852 btrfs_file_extent_calc_inline_size(0);
1853 memmove(buf
+start
, buf
+start
+skip
,
1857 leaf
= path
->nodes
[0];
1858 slot
= path
->slots
[0];
1859 write_extent_buffer(leaf
, buf
,
1860 btrfs_item_ptr_offset(leaf
, slot
),
1862 inode_add_bytes(inode
, datal
);
1865 btrfs_mark_buffer_dirty(leaf
);
1866 btrfs_release_path(root
, path
);
1868 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1871 * we round up to the block size at eof when
1872 * determining which extents to clone above,
1873 * but shouldn't round up the file size
1875 endoff
= new_key
.offset
+ datal
;
1876 if (endoff
> destoff
+olen
)
1877 endoff
= destoff
+olen
;
1878 if (endoff
> inode
->i_size
)
1879 btrfs_i_size_write(inode
, endoff
);
1881 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1882 ret
= btrfs_update_inode(trans
, root
, inode
);
1884 btrfs_end_transaction(trans
, root
);
1887 btrfs_release_path(root
, path
);
1892 btrfs_release_path(root
, path
);
1893 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1895 mutex_unlock(&src
->i_mutex
);
1896 mutex_unlock(&inode
->i_mutex
);
1898 btrfs_free_path(path
);
1902 mnt_drop_write(file
->f_path
.mnt
);
1906 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1908 struct btrfs_ioctl_clone_range_args args
;
1910 if (copy_from_user(&args
, argp
, sizeof(args
)))
1912 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1913 args
.src_length
, args
.dest_offset
);
1917 * there are many ways the trans_start and trans_end ioctls can lead
1918 * to deadlocks. They should only be used by applications that
1919 * basically own the machine, and have a very in depth understanding
1920 * of all the possible deadlocks and enospc problems.
1922 static long btrfs_ioctl_trans_start(struct file
*file
)
1924 struct inode
*inode
= fdentry(file
)->d_inode
;
1925 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1926 struct btrfs_trans_handle
*trans
;
1930 if (!capable(CAP_SYS_ADMIN
))
1934 if (file
->private_data
)
1937 ret
= mnt_want_write(file
->f_path
.mnt
);
1941 mutex_lock(&root
->fs_info
->trans_mutex
);
1942 root
->fs_info
->open_ioctl_trans
++;
1943 mutex_unlock(&root
->fs_info
->trans_mutex
);
1946 trans
= btrfs_start_ioctl_transaction(root
, 0);
1950 file
->private_data
= trans
;
1954 mutex_lock(&root
->fs_info
->trans_mutex
);
1955 root
->fs_info
->open_ioctl_trans
--;
1956 mutex_unlock(&root
->fs_info
->trans_mutex
);
1957 mnt_drop_write(file
->f_path
.mnt
);
1962 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
1964 struct inode
*inode
= fdentry(file
)->d_inode
;
1965 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1966 struct btrfs_root
*new_root
;
1967 struct btrfs_dir_item
*di
;
1968 struct btrfs_trans_handle
*trans
;
1969 struct btrfs_path
*path
;
1970 struct btrfs_key location
;
1971 struct btrfs_disk_key disk_key
;
1972 struct btrfs_super_block
*disk_super
;
1977 if (!capable(CAP_SYS_ADMIN
))
1980 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
1984 objectid
= root
->root_key
.objectid
;
1986 location
.objectid
= objectid
;
1987 location
.type
= BTRFS_ROOT_ITEM_KEY
;
1988 location
.offset
= (u64
)-1;
1990 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
1991 if (IS_ERR(new_root
))
1992 return PTR_ERR(new_root
);
1994 if (btrfs_root_refs(&new_root
->root_item
) == 0)
1997 path
= btrfs_alloc_path();
2000 path
->leave_spinning
= 1;
2002 trans
= btrfs_start_transaction(root
, 1);
2004 btrfs_free_path(path
);
2008 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2009 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2010 dir_id
, "default", 7, 1);
2011 if (IS_ERR_OR_NULL(di
)) {
2012 btrfs_free_path(path
);
2013 btrfs_end_transaction(trans
, root
);
2014 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2015 "this isn't going to work\n");
2019 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2020 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2021 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2022 btrfs_free_path(path
);
2024 disk_super
= &root
->fs_info
->super_copy
;
2025 features
= btrfs_super_incompat_flags(disk_super
);
2026 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2027 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2028 btrfs_set_super_incompat_flags(disk_super
, features
);
2030 btrfs_end_transaction(trans
, root
);
2035 static void get_block_group_info(struct list_head
*groups_list
,
2036 struct btrfs_ioctl_space_info
*space
)
2038 struct btrfs_block_group_cache
*block_group
;
2040 space
->total_bytes
= 0;
2041 space
->used_bytes
= 0;
2043 list_for_each_entry(block_group
, groups_list
, list
) {
2044 space
->flags
= block_group
->flags
;
2045 space
->total_bytes
+= block_group
->key
.offset
;
2046 space
->used_bytes
+=
2047 btrfs_block_group_used(&block_group
->item
);
2051 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2053 struct btrfs_ioctl_space_args space_args
;
2054 struct btrfs_ioctl_space_info space
;
2055 struct btrfs_ioctl_space_info
*dest
;
2056 struct btrfs_ioctl_space_info
*dest_orig
;
2057 struct btrfs_ioctl_space_info
*user_dest
;
2058 struct btrfs_space_info
*info
;
2059 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2060 BTRFS_BLOCK_GROUP_SYSTEM
,
2061 BTRFS_BLOCK_GROUP_METADATA
,
2062 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2069 if (copy_from_user(&space_args
,
2070 (struct btrfs_ioctl_space_args __user
*)arg
,
2071 sizeof(space_args
)))
2074 for (i
= 0; i
< num_types
; i
++) {
2075 struct btrfs_space_info
*tmp
;
2079 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2081 if (tmp
->flags
== types
[i
]) {
2091 down_read(&info
->groups_sem
);
2092 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2093 if (!list_empty(&info
->block_groups
[c
]))
2096 up_read(&info
->groups_sem
);
2099 /* space_slots == 0 means they are asking for a count */
2100 if (space_args
.space_slots
== 0) {
2101 space_args
.total_spaces
= slot_count
;
2105 slot_count
= min_t(int, space_args
.space_slots
, slot_count
);
2107 alloc_size
= sizeof(*dest
) * slot_count
;
2109 /* we generally have at most 6 or so space infos, one for each raid
2110 * level. So, a whole page should be more than enough for everyone
2112 if (alloc_size
> PAGE_CACHE_SIZE
)
2115 space_args
.total_spaces
= 0;
2116 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2121 /* now we have a buffer to copy into */
2122 for (i
= 0; i
< num_types
; i
++) {
2123 struct btrfs_space_info
*tmp
;
2127 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2129 if (tmp
->flags
== types
[i
]) {
2138 down_read(&info
->groups_sem
);
2139 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2140 if (!list_empty(&info
->block_groups
[c
])) {
2141 get_block_group_info(&info
->block_groups
[c
],
2143 memcpy(dest
, &space
, sizeof(space
));
2145 space_args
.total_spaces
++;
2148 up_read(&info
->groups_sem
);
2151 user_dest
= (struct btrfs_ioctl_space_info
*)
2152 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2154 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2159 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2166 * there are many ways the trans_start and trans_end ioctls can lead
2167 * to deadlocks. They should only be used by applications that
2168 * basically own the machine, and have a very in depth understanding
2169 * of all the possible deadlocks and enospc problems.
2171 long btrfs_ioctl_trans_end(struct file
*file
)
2173 struct inode
*inode
= fdentry(file
)->d_inode
;
2174 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2175 struct btrfs_trans_handle
*trans
;
2177 trans
= file
->private_data
;
2180 file
->private_data
= NULL
;
2182 btrfs_end_transaction(trans
, root
);
2184 mutex_lock(&root
->fs_info
->trans_mutex
);
2185 root
->fs_info
->open_ioctl_trans
--;
2186 mutex_unlock(&root
->fs_info
->trans_mutex
);
2188 mnt_drop_write(file
->f_path
.mnt
);
2192 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2194 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2195 struct btrfs_trans_handle
*trans
;
2198 trans
= btrfs_start_transaction(root
, 0);
2199 transid
= trans
->transid
;
2200 btrfs_commit_transaction_async(trans
, root
, 0);
2203 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2208 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2210 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2214 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2217 transid
= 0; /* current trans */
2219 return btrfs_wait_for_commit(root
, transid
);
2222 long btrfs_ioctl(struct file
*file
, unsigned int
2223 cmd
, unsigned long arg
)
2225 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2226 void __user
*argp
= (void __user
*)arg
;
2229 case FS_IOC_GETFLAGS
:
2230 return btrfs_ioctl_getflags(file
, argp
);
2231 case FS_IOC_SETFLAGS
:
2232 return btrfs_ioctl_setflags(file
, argp
);
2233 case FS_IOC_GETVERSION
:
2234 return btrfs_ioctl_getversion(file
, argp
);
2235 case BTRFS_IOC_SNAP_CREATE
:
2236 return btrfs_ioctl_snap_create(file
, argp
, 0, 0);
2237 case BTRFS_IOC_SNAP_CREATE_ASYNC
:
2238 return btrfs_ioctl_snap_create(file
, argp
, 0, 1);
2239 case BTRFS_IOC_SUBVOL_CREATE
:
2240 return btrfs_ioctl_snap_create(file
, argp
, 1, 0);
2241 case BTRFS_IOC_SNAP_DESTROY
:
2242 return btrfs_ioctl_snap_destroy(file
, argp
);
2243 case BTRFS_IOC_DEFAULT_SUBVOL
:
2244 return btrfs_ioctl_default_subvol(file
, argp
);
2245 case BTRFS_IOC_DEFRAG
:
2246 return btrfs_ioctl_defrag(file
, NULL
);
2247 case BTRFS_IOC_DEFRAG_RANGE
:
2248 return btrfs_ioctl_defrag(file
, argp
);
2249 case BTRFS_IOC_RESIZE
:
2250 return btrfs_ioctl_resize(root
, argp
);
2251 case BTRFS_IOC_ADD_DEV
:
2252 return btrfs_ioctl_add_dev(root
, argp
);
2253 case BTRFS_IOC_RM_DEV
:
2254 return btrfs_ioctl_rm_dev(root
, argp
);
2255 case BTRFS_IOC_BALANCE
:
2256 return btrfs_balance(root
->fs_info
->dev_root
);
2257 case BTRFS_IOC_CLONE
:
2258 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2259 case BTRFS_IOC_CLONE_RANGE
:
2260 return btrfs_ioctl_clone_range(file
, argp
);
2261 case BTRFS_IOC_TRANS_START
:
2262 return btrfs_ioctl_trans_start(file
);
2263 case BTRFS_IOC_TRANS_END
:
2264 return btrfs_ioctl_trans_end(file
);
2265 case BTRFS_IOC_TREE_SEARCH
:
2266 return btrfs_ioctl_tree_search(file
, argp
);
2267 case BTRFS_IOC_INO_LOOKUP
:
2268 return btrfs_ioctl_ino_lookup(file
, argp
);
2269 case BTRFS_IOC_SPACE_INFO
:
2270 return btrfs_ioctl_space_info(root
, argp
);
2271 case BTRFS_IOC_SYNC
:
2272 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
2274 case BTRFS_IOC_START_SYNC
:
2275 return btrfs_ioctl_start_sync(file
, argp
);
2276 case BTRFS_IOC_WAIT_SYNC
:
2277 return btrfs_ioctl_wait_sync(file
, argp
);