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 dentry
*parent
= dget_parent(dentry
);
241 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
244 ret
= btrfs_find_free_objectid(NULL
, root
->fs_info
->tree_root
,
251 dir
= parent
->d_inode
;
259 trans
= btrfs_start_transaction(root
, 6);
262 return PTR_ERR(trans
);
265 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
266 0, objectid
, NULL
, 0, 0, 0);
272 memset_extent_buffer(leaf
, 0, 0, sizeof(struct btrfs_header
));
273 btrfs_set_header_bytenr(leaf
, leaf
->start
);
274 btrfs_set_header_generation(leaf
, trans
->transid
);
275 btrfs_set_header_backref_rev(leaf
, BTRFS_MIXED_BACKREF_REV
);
276 btrfs_set_header_owner(leaf
, objectid
);
278 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
279 (unsigned long)btrfs_header_fsid(leaf
),
281 write_extent_buffer(leaf
, root
->fs_info
->chunk_tree_uuid
,
282 (unsigned long)btrfs_header_chunk_tree_uuid(leaf
),
284 btrfs_mark_buffer_dirty(leaf
);
286 inode_item
= &root_item
.inode
;
287 memset(inode_item
, 0, sizeof(*inode_item
));
288 inode_item
->generation
= cpu_to_le64(1);
289 inode_item
->size
= cpu_to_le64(3);
290 inode_item
->nlink
= cpu_to_le32(1);
291 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
292 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
294 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
295 btrfs_set_root_generation(&root_item
, trans
->transid
);
296 btrfs_set_root_level(&root_item
, 0);
297 btrfs_set_root_refs(&root_item
, 1);
298 btrfs_set_root_used(&root_item
, leaf
->len
);
299 btrfs_set_root_last_snapshot(&root_item
, 0);
301 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
302 root_item
.drop_level
= 0;
304 btrfs_tree_unlock(leaf
);
305 free_extent_buffer(leaf
);
308 btrfs_set_root_dirid(&root_item
, new_dirid
);
310 key
.objectid
= objectid
;
312 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
313 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
318 key
.offset
= (u64
)-1;
319 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
320 BUG_ON(IS_ERR(new_root
));
322 btrfs_record_root_in_trans(trans
, new_root
);
324 ret
= btrfs_create_subvol_root(trans
, new_root
, new_dirid
,
325 BTRFS_I(dir
)->block_group
);
327 * insert the directory item
329 ret
= btrfs_set_inode_index(dir
, &index
);
332 ret
= btrfs_insert_dir_item(trans
, root
,
333 name
, namelen
, dir
->i_ino
, &key
,
334 BTRFS_FT_DIR
, index
);
338 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
339 ret
= btrfs_update_inode(trans
, root
, dir
);
342 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
343 objectid
, root
->root_key
.objectid
,
344 dir
->i_ino
, index
, name
, namelen
);
348 d_instantiate(dentry
, btrfs_lookup_dentry(dir
, dentry
));
352 *async_transid
= trans
->transid
;
353 err
= btrfs_commit_transaction_async(trans
, root
, 1);
355 err
= btrfs_commit_transaction(trans
, root
);
362 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
363 char *name
, int namelen
, u64
*async_transid
)
366 struct dentry
*parent
;
367 struct btrfs_pending_snapshot
*pending_snapshot
;
368 struct btrfs_trans_handle
*trans
;
374 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
375 if (!pending_snapshot
)
378 btrfs_init_block_rsv(&pending_snapshot
->block_rsv
);
379 pending_snapshot
->dentry
= dentry
;
380 pending_snapshot
->root
= root
;
382 trans
= btrfs_start_transaction(root
->fs_info
->extent_root
, 5);
384 ret
= PTR_ERR(trans
);
388 ret
= btrfs_snap_reserve_metadata(trans
, pending_snapshot
);
391 list_add(&pending_snapshot
->list
,
392 &trans
->transaction
->pending_snapshots
);
394 *async_transid
= trans
->transid
;
395 ret
= btrfs_commit_transaction_async(trans
,
396 root
->fs_info
->extent_root
, 1);
398 ret
= btrfs_commit_transaction(trans
,
399 root
->fs_info
->extent_root
);
403 ret
= pending_snapshot
->error
;
407 btrfs_orphan_cleanup(pending_snapshot
->snap
);
409 parent
= dget_parent(dentry
);
410 inode
= btrfs_lookup_dentry(parent
->d_inode
, dentry
);
413 ret
= PTR_ERR(inode
);
417 d_instantiate(dentry
, inode
);
420 kfree(pending_snapshot
);
424 /* copy of check_sticky in fs/namei.c()
425 * It's inline, so penalty for filesystems that don't use sticky bit is
428 static inline int btrfs_check_sticky(struct inode
*dir
, struct inode
*inode
)
430 uid_t fsuid
= current_fsuid();
432 if (!(dir
->i_mode
& S_ISVTX
))
434 if (inode
->i_uid
== fsuid
)
436 if (dir
->i_uid
== fsuid
)
438 return !capable(CAP_FOWNER
);
441 /* copy of may_delete in fs/namei.c()
442 * Check whether we can remove a link victim from directory dir, check
443 * whether the type of victim is right.
444 * 1. We can't do it if dir is read-only (done in permission())
445 * 2. We should have write and exec permissions on dir
446 * 3. We can't remove anything from append-only dir
447 * 4. We can't do anything with immutable dir (done in permission())
448 * 5. If the sticky bit on dir is set we should either
449 * a. be owner of dir, or
450 * b. be owner of victim, or
451 * c. have CAP_FOWNER capability
452 * 6. If the victim is append-only or immutable we can't do antyhing with
453 * links pointing to it.
454 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
455 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
456 * 9. We can't remove a root or mountpoint.
457 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
458 * nfs_async_unlink().
461 static int btrfs_may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
465 if (!victim
->d_inode
)
468 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
469 audit_inode_child(victim
, dir
);
471 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
476 if (btrfs_check_sticky(dir
, victim
->d_inode
)||
477 IS_APPEND(victim
->d_inode
)||
478 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
481 if (!S_ISDIR(victim
->d_inode
->i_mode
))
485 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
489 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
494 /* copy of may_create in fs/namei.c() */
495 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
501 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
505 * Create a new subvolume below @parent. This is largely modeled after
506 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
507 * inside this filesystem so it's quite a bit simpler.
509 static noinline
int btrfs_mksubvol(struct path
*parent
,
510 char *name
, int namelen
,
511 struct btrfs_root
*snap_src
,
514 struct inode
*dir
= parent
->dentry
->d_inode
;
515 struct dentry
*dentry
;
518 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
520 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
521 error
= PTR_ERR(dentry
);
529 error
= mnt_want_write(parent
->mnt
);
533 error
= btrfs_may_create(dir
, dentry
);
537 down_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
539 if (btrfs_root_refs(&BTRFS_I(dir
)->root
->root_item
) == 0)
543 error
= create_snapshot(snap_src
, dentry
,
544 name
, namelen
, async_transid
);
546 error
= create_subvol(BTRFS_I(dir
)->root
, dentry
,
547 name
, namelen
, async_transid
);
550 fsnotify_mkdir(dir
, dentry
);
552 up_read(&BTRFS_I(dir
)->root
->fs_info
->subvol_sem
);
554 mnt_drop_write(parent
->mnt
);
558 mutex_unlock(&dir
->i_mutex
);
562 static int should_defrag_range(struct inode
*inode
, u64 start
, u64 len
,
563 int thresh
, u64
*last_len
, u64
*skip
,
566 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
567 struct extent_map
*em
= NULL
;
568 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
576 * make sure that once we start defragging and extent, we keep on
579 if (start
< *defrag_end
)
585 * hopefully we have this extent in the tree already, try without
586 * the full extent lock
588 read_lock(&em_tree
->lock
);
589 em
= lookup_extent_mapping(em_tree
, start
, len
);
590 read_unlock(&em_tree
->lock
);
593 /* get the big lock and read metadata off disk */
594 lock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
595 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
596 unlock_extent(io_tree
, start
, start
+ len
- 1, GFP_NOFS
);
602 /* this will cover holes, and inline extents */
603 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
)
607 * we hit a real extent, if it is big don't bother defragging it again
609 if ((*last_len
== 0 || *last_len
>= thresh
) && em
->len
>= thresh
)
613 * last_len ends up being a counter of how many bytes we've defragged.
614 * every time we choose not to defrag an extent, we reset *last_len
615 * so that the next tiny extent will force a defrag.
617 * The end result of this is that tiny extents before a single big
618 * extent will force at least part of that big extent to be defragged.
622 *defrag_end
= extent_map_end(em
);
625 *skip
= extent_map_end(em
);
633 static int btrfs_defrag_file(struct file
*file
,
634 struct btrfs_ioctl_defrag_range_args
*range
)
636 struct inode
*inode
= fdentry(file
)->d_inode
;
637 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
638 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
639 struct btrfs_ordered_extent
*ordered
;
641 unsigned long last_index
;
642 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
643 unsigned long total_read
= 0;
652 if (inode
->i_size
== 0)
655 if (range
->start
+ range
->len
> range
->start
) {
656 last_index
= min_t(u64
, inode
->i_size
- 1,
657 range
->start
+ range
->len
- 1) >> PAGE_CACHE_SHIFT
;
659 last_index
= (inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
;
662 i
= range
->start
>> PAGE_CACHE_SHIFT
;
663 while (i
<= last_index
) {
664 if (!should_defrag_range(inode
, (u64
)i
<< PAGE_CACHE_SHIFT
,
666 range
->extent_thresh
,
671 * the should_defrag function tells us how much to skip
672 * bump our counter by the suggested amount
674 next
= (skip
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
675 i
= max(i
+ 1, next
);
679 if (total_read
% ra_pages
== 0) {
680 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
681 min(last_index
, i
+ ra_pages
- 1));
684 mutex_lock(&inode
->i_mutex
);
685 if (range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)
686 BTRFS_I(inode
)->force_compress
= 1;
688 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
692 if (inode
->i_size
== 0 ||
693 i
> ((inode
->i_size
- 1) >> PAGE_CACHE_SHIFT
)) {
695 goto err_reservations
;
698 page
= grab_cache_page(inode
->i_mapping
, i
);
701 goto err_reservations
;
704 if (!PageUptodate(page
)) {
705 btrfs_readpage(NULL
, page
);
707 if (!PageUptodate(page
)) {
709 page_cache_release(page
);
711 goto err_reservations
;
715 if (page
->mapping
!= inode
->i_mapping
) {
717 page_cache_release(page
);
721 wait_on_page_writeback(page
);
723 if (PageDirty(page
)) {
724 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
728 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
729 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
730 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
732 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
734 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
736 page_cache_release(page
);
737 btrfs_start_ordered_extent(inode
, ordered
, 1);
738 btrfs_put_ordered_extent(ordered
);
741 set_page_extent_mapped(page
);
744 * this makes sure page_mkwrite is called on the
745 * page if it is dirtied again later
747 clear_page_dirty_for_io(page
);
748 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
,
749 page_end
, EXTENT_DIRTY
| EXTENT_DELALLOC
|
750 EXTENT_DO_ACCOUNTING
, GFP_NOFS
);
752 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, NULL
);
753 ClearPageChecked(page
);
754 set_page_dirty(page
);
755 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
759 page_cache_release(page
);
760 mutex_unlock(&inode
->i_mutex
);
762 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
766 if ((range
->flags
& BTRFS_DEFRAG_RANGE_START_IO
))
767 filemap_flush(inode
->i_mapping
);
769 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
770 /* the filemap_flush will queue IO into the worker threads, but
771 * we have to make sure the IO is actually started and that
772 * ordered extents get created before we return
774 atomic_inc(&root
->fs_info
->async_submit_draining
);
775 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
776 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
777 wait_event(root
->fs_info
->async_submit_wait
,
778 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
779 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
781 atomic_dec(&root
->fs_info
->async_submit_draining
);
783 mutex_lock(&inode
->i_mutex
);
784 BTRFS_I(inode
)->force_compress
= 0;
785 mutex_unlock(&inode
->i_mutex
);
791 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
793 mutex_unlock(&inode
->i_mutex
);
797 static noinline
int btrfs_ioctl_resize(struct btrfs_root
*root
,
803 struct btrfs_ioctl_vol_args
*vol_args
;
804 struct btrfs_trans_handle
*trans
;
805 struct btrfs_device
*device
= NULL
;
811 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
814 if (!capable(CAP_SYS_ADMIN
))
817 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
818 if (IS_ERR(vol_args
))
819 return PTR_ERR(vol_args
);
821 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
823 mutex_lock(&root
->fs_info
->volume_mutex
);
824 sizestr
= vol_args
->name
;
825 devstr
= strchr(sizestr
, ':');
828 sizestr
= devstr
+ 1;
830 devstr
= vol_args
->name
;
831 devid
= simple_strtoull(devstr
, &end
, 10);
832 printk(KERN_INFO
"resizing devid %llu\n",
833 (unsigned long long)devid
);
835 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
837 printk(KERN_INFO
"resizer unable to find device %llu\n",
838 (unsigned long long)devid
);
842 if (!strcmp(sizestr
, "max"))
843 new_size
= device
->bdev
->bd_inode
->i_size
;
845 if (sizestr
[0] == '-') {
848 } else if (sizestr
[0] == '+') {
852 new_size
= memparse(sizestr
, NULL
);
859 old_size
= device
->total_bytes
;
862 if (new_size
> old_size
) {
866 new_size
= old_size
- new_size
;
867 } else if (mod
> 0) {
868 new_size
= old_size
+ new_size
;
871 if (new_size
< 256 * 1024 * 1024) {
875 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
880 do_div(new_size
, root
->sectorsize
);
881 new_size
*= root
->sectorsize
;
883 printk(KERN_INFO
"new size for %s is %llu\n",
884 device
->name
, (unsigned long long)new_size
);
886 if (new_size
> old_size
) {
887 trans
= btrfs_start_transaction(root
, 0);
888 ret
= btrfs_grow_device(trans
, device
, new_size
);
889 btrfs_commit_transaction(trans
, root
);
891 ret
= btrfs_shrink_device(device
, new_size
);
895 mutex_unlock(&root
->fs_info
->volume_mutex
);
900 static noinline
int btrfs_ioctl_snap_create_transid(struct file
*file
,
906 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
907 struct file
*src_file
;
911 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
914 namelen
= strlen(name
);
915 if (strchr(name
, '/')) {
921 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
924 struct inode
*src_inode
;
931 src_inode
= src_file
->f_path
.dentry
->d_inode
;
932 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
933 printk(KERN_INFO
"btrfs: Snapshot src from "
939 ret
= btrfs_mksubvol(&file
->f_path
, name
, namelen
,
940 BTRFS_I(src_inode
)->root
,
948 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
949 void __user
*arg
, int subvol
,
952 struct btrfs_ioctl_vol_args
*vol_args
= NULL
;
953 struct btrfs_ioctl_vol_args_v2
*vol_args_v2
= NULL
;
962 vol_args_v2
= memdup_user(arg
, sizeof(*vol_args_v2
));
963 if (IS_ERR(vol_args_v2
))
964 return PTR_ERR(vol_args_v2
);
966 if (vol_args_v2
->flags
& ~BTRFS_SUBVOL_CREATE_ASYNC
) {
971 name
= vol_args_v2
->name
;
972 fd
= vol_args_v2
->fd
;
973 vol_args_v2
->name
[BTRFS_SUBVOL_NAME_MAX
] = '\0';
975 if (vol_args_v2
->flags
& BTRFS_SUBVOL_CREATE_ASYNC
)
978 ret
= btrfs_ioctl_snap_create_transid(file
, name
, fd
,
981 if (ret
== 0 && ptr
&&
983 offsetof(struct btrfs_ioctl_vol_args_v2
,
984 transid
), ptr
, sizeof(*ptr
)))
987 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
988 if (IS_ERR(vol_args
))
989 return PTR_ERR(vol_args
);
990 name
= vol_args
->name
;
992 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
994 ret
= btrfs_ioctl_snap_create_transid(file
, name
, fd
,
1005 * helper to check if the subvolume references other subvolumes
1007 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
1009 struct btrfs_path
*path
;
1010 struct btrfs_key key
;
1013 path
= btrfs_alloc_path();
1017 key
.objectid
= root
->root_key
.objectid
;
1018 key
.type
= BTRFS_ROOT_REF_KEY
;
1019 key
.offset
= (u64
)-1;
1021 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
,
1028 if (path
->slots
[0] > 0) {
1030 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
1031 if (key
.objectid
== root
->root_key
.objectid
&&
1032 key
.type
== BTRFS_ROOT_REF_KEY
)
1036 btrfs_free_path(path
);
1040 static noinline
int key_in_sk(struct btrfs_key
*key
,
1041 struct btrfs_ioctl_search_key
*sk
)
1043 struct btrfs_key test
;
1046 test
.objectid
= sk
->min_objectid
;
1047 test
.type
= sk
->min_type
;
1048 test
.offset
= sk
->min_offset
;
1050 ret
= btrfs_comp_cpu_keys(key
, &test
);
1054 test
.objectid
= sk
->max_objectid
;
1055 test
.type
= sk
->max_type
;
1056 test
.offset
= sk
->max_offset
;
1058 ret
= btrfs_comp_cpu_keys(key
, &test
);
1064 static noinline
int copy_to_sk(struct btrfs_root
*root
,
1065 struct btrfs_path
*path
,
1066 struct btrfs_key
*key
,
1067 struct btrfs_ioctl_search_key
*sk
,
1069 unsigned long *sk_offset
,
1073 struct extent_buffer
*leaf
;
1074 struct btrfs_ioctl_search_header sh
;
1075 unsigned long item_off
;
1076 unsigned long item_len
;
1083 leaf
= path
->nodes
[0];
1084 slot
= path
->slots
[0];
1085 nritems
= btrfs_header_nritems(leaf
);
1087 if (btrfs_header_generation(leaf
) > sk
->max_transid
) {
1091 found_transid
= btrfs_header_generation(leaf
);
1093 for (i
= slot
; i
< nritems
; i
++) {
1094 item_off
= btrfs_item_ptr_offset(leaf
, i
);
1095 item_len
= btrfs_item_size_nr(leaf
, i
);
1097 if (item_len
> BTRFS_SEARCH_ARGS_BUFSIZE
)
1100 if (sizeof(sh
) + item_len
+ *sk_offset
>
1101 BTRFS_SEARCH_ARGS_BUFSIZE
) {
1106 btrfs_item_key_to_cpu(leaf
, key
, i
);
1107 if (!key_in_sk(key
, sk
))
1110 sh
.objectid
= key
->objectid
;
1111 sh
.offset
= key
->offset
;
1112 sh
.type
= key
->type
;
1114 sh
.transid
= found_transid
;
1116 /* copy search result header */
1117 memcpy(buf
+ *sk_offset
, &sh
, sizeof(sh
));
1118 *sk_offset
+= sizeof(sh
);
1121 char *p
= buf
+ *sk_offset
;
1123 read_extent_buffer(leaf
, p
,
1124 item_off
, item_len
);
1125 *sk_offset
+= item_len
;
1129 if (*num_found
>= sk
->nr_items
)
1134 if (key
->offset
< (u64
)-1 && key
->offset
< sk
->max_offset
)
1136 else if (key
->type
< (u8
)-1 && key
->type
< sk
->max_type
) {
1139 } else if (key
->objectid
< (u64
)-1 && key
->objectid
< sk
->max_objectid
) {
1146 *num_found
+= found
;
1150 static noinline
int search_ioctl(struct inode
*inode
,
1151 struct btrfs_ioctl_search_args
*args
)
1153 struct btrfs_root
*root
;
1154 struct btrfs_key key
;
1155 struct btrfs_key max_key
;
1156 struct btrfs_path
*path
;
1157 struct btrfs_ioctl_search_key
*sk
= &args
->key
;
1158 struct btrfs_fs_info
*info
= BTRFS_I(inode
)->root
->fs_info
;
1161 unsigned long sk_offset
= 0;
1163 path
= btrfs_alloc_path();
1167 if (sk
->tree_id
== 0) {
1168 /* search the root of the inode that was passed */
1169 root
= BTRFS_I(inode
)->root
;
1171 key
.objectid
= sk
->tree_id
;
1172 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1173 key
.offset
= (u64
)-1;
1174 root
= btrfs_read_fs_root_no_name(info
, &key
);
1176 printk(KERN_ERR
"could not find root %llu\n",
1178 btrfs_free_path(path
);
1183 key
.objectid
= sk
->min_objectid
;
1184 key
.type
= sk
->min_type
;
1185 key
.offset
= sk
->min_offset
;
1187 max_key
.objectid
= sk
->max_objectid
;
1188 max_key
.type
= sk
->max_type
;
1189 max_key
.offset
= sk
->max_offset
;
1191 path
->keep_locks
= 1;
1194 ret
= btrfs_search_forward(root
, &key
, &max_key
, path
, 0,
1201 ret
= copy_to_sk(root
, path
, &key
, sk
, args
->buf
,
1202 &sk_offset
, &num_found
);
1203 btrfs_release_path(root
, path
);
1204 if (ret
|| num_found
>= sk
->nr_items
)
1210 sk
->nr_items
= num_found
;
1211 btrfs_free_path(path
);
1215 static noinline
int btrfs_ioctl_tree_search(struct file
*file
,
1218 struct btrfs_ioctl_search_args
*args
;
1219 struct inode
*inode
;
1222 if (!capable(CAP_SYS_ADMIN
))
1225 args
= memdup_user(argp
, sizeof(*args
));
1227 return PTR_ERR(args
);
1229 inode
= fdentry(file
)->d_inode
;
1230 ret
= search_ioctl(inode
, args
);
1231 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1238 * Search INODE_REFs to identify path name of 'dirid' directory
1239 * in a 'tree_id' tree. and sets path name to 'name'.
1241 static noinline
int btrfs_search_path_in_tree(struct btrfs_fs_info
*info
,
1242 u64 tree_id
, u64 dirid
, char *name
)
1244 struct btrfs_root
*root
;
1245 struct btrfs_key key
;
1251 struct btrfs_inode_ref
*iref
;
1252 struct extent_buffer
*l
;
1253 struct btrfs_path
*path
;
1255 if (dirid
== BTRFS_FIRST_FREE_OBJECTID
) {
1260 path
= btrfs_alloc_path();
1264 ptr
= &name
[BTRFS_INO_LOOKUP_PATH_MAX
];
1266 key
.objectid
= tree_id
;
1267 key
.type
= BTRFS_ROOT_ITEM_KEY
;
1268 key
.offset
= (u64
)-1;
1269 root
= btrfs_read_fs_root_no_name(info
, &key
);
1271 printk(KERN_ERR
"could not find root %llu\n", tree_id
);
1276 key
.objectid
= dirid
;
1277 key
.type
= BTRFS_INODE_REF_KEY
;
1278 key
.offset
= (u64
)-1;
1281 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1286 slot
= path
->slots
[0];
1287 if (ret
> 0 && slot
> 0)
1289 btrfs_item_key_to_cpu(l
, &key
, slot
);
1291 if (ret
> 0 && (key
.objectid
!= dirid
||
1292 key
.type
!= BTRFS_INODE_REF_KEY
)) {
1297 iref
= btrfs_item_ptr(l
, slot
, struct btrfs_inode_ref
);
1298 len
= btrfs_inode_ref_name_len(l
, iref
);
1300 total_len
+= len
+ 1;
1305 read_extent_buffer(l
, ptr
,(unsigned long)(iref
+ 1), len
);
1307 if (key
.offset
== BTRFS_FIRST_FREE_OBJECTID
)
1310 btrfs_release_path(root
, path
);
1311 key
.objectid
= key
.offset
;
1312 key
.offset
= (u64
)-1;
1313 dirid
= key
.objectid
;
1318 memcpy(name
, ptr
, total_len
);
1319 name
[total_len
]='\0';
1322 btrfs_free_path(path
);
1326 static noinline
int btrfs_ioctl_ino_lookup(struct file
*file
,
1329 struct btrfs_ioctl_ino_lookup_args
*args
;
1330 struct inode
*inode
;
1333 if (!capable(CAP_SYS_ADMIN
))
1336 args
= memdup_user(argp
, sizeof(*args
));
1338 return PTR_ERR(args
);
1340 inode
= fdentry(file
)->d_inode
;
1342 if (args
->treeid
== 0)
1343 args
->treeid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
1345 ret
= btrfs_search_path_in_tree(BTRFS_I(inode
)->root
->fs_info
,
1346 args
->treeid
, args
->objectid
,
1349 if (ret
== 0 && copy_to_user(argp
, args
, sizeof(*args
)))
1356 static noinline
int btrfs_ioctl_snap_destroy(struct file
*file
,
1359 struct dentry
*parent
= fdentry(file
);
1360 struct dentry
*dentry
;
1361 struct inode
*dir
= parent
->d_inode
;
1362 struct inode
*inode
;
1363 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
1364 struct btrfs_root
*dest
= NULL
;
1365 struct btrfs_ioctl_vol_args
*vol_args
;
1366 struct btrfs_trans_handle
*trans
;
1371 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1372 if (IS_ERR(vol_args
))
1373 return PTR_ERR(vol_args
);
1375 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1376 namelen
= strlen(vol_args
->name
);
1377 if (strchr(vol_args
->name
, '/') ||
1378 strncmp(vol_args
->name
, "..", namelen
) == 0) {
1383 err
= mnt_want_write(file
->f_path
.mnt
);
1387 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
1388 dentry
= lookup_one_len(vol_args
->name
, parent
, namelen
);
1389 if (IS_ERR(dentry
)) {
1390 err
= PTR_ERR(dentry
);
1391 goto out_unlock_dir
;
1394 if (!dentry
->d_inode
) {
1399 inode
= dentry
->d_inode
;
1400 dest
= BTRFS_I(inode
)->root
;
1401 if (!capable(CAP_SYS_ADMIN
)){
1403 * Regular user. Only allow this with a special mount
1404 * option, when the user has write+exec access to the
1405 * subvol root, and when rmdir(2) would have been
1408 * Note that this is _not_ check that the subvol is
1409 * empty or doesn't contain data that we wouldn't
1410 * otherwise be able to delete.
1412 * Users who want to delete empty subvols should try
1416 if (!btrfs_test_opt(root
, USER_SUBVOL_RM_ALLOWED
))
1420 * Do not allow deletion if the parent dir is the same
1421 * as the dir to be deleted. That means the ioctl
1422 * must be called on the dentry referencing the root
1423 * of the subvol, not a random directory contained
1430 err
= inode_permission(inode
, MAY_WRITE
| MAY_EXEC
);
1434 /* check if subvolume may be deleted by a non-root user */
1435 err
= btrfs_may_delete(dir
, dentry
, 1);
1440 if (inode
->i_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
1445 mutex_lock(&inode
->i_mutex
);
1446 err
= d_invalidate(dentry
);
1450 down_write(&root
->fs_info
->subvol_sem
);
1452 err
= may_destroy_subvol(dest
);
1456 trans
= btrfs_start_transaction(root
, 0);
1457 if (IS_ERR(trans
)) {
1458 err
= PTR_ERR(trans
);
1461 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
1463 ret
= btrfs_unlink_subvol(trans
, root
, dir
,
1464 dest
->root_key
.objectid
,
1465 dentry
->d_name
.name
,
1466 dentry
->d_name
.len
);
1469 btrfs_record_root_in_trans(trans
, dest
);
1471 memset(&dest
->root_item
.drop_progress
, 0,
1472 sizeof(dest
->root_item
.drop_progress
));
1473 dest
->root_item
.drop_level
= 0;
1474 btrfs_set_root_refs(&dest
->root_item
, 0);
1476 if (!xchg(&dest
->orphan_item_inserted
, 1)) {
1477 ret
= btrfs_insert_orphan_item(trans
,
1478 root
->fs_info
->tree_root
,
1479 dest
->root_key
.objectid
);
1483 ret
= btrfs_end_transaction(trans
, root
);
1485 inode
->i_flags
|= S_DEAD
;
1487 up_write(&root
->fs_info
->subvol_sem
);
1489 mutex_unlock(&inode
->i_mutex
);
1491 shrink_dcache_sb(root
->fs_info
->sb
);
1492 btrfs_invalidate_inodes(dest
);
1498 mutex_unlock(&dir
->i_mutex
);
1499 mnt_drop_write(file
->f_path
.mnt
);
1505 static int btrfs_ioctl_defrag(struct file
*file
, void __user
*argp
)
1507 struct inode
*inode
= fdentry(file
)->d_inode
;
1508 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1509 struct btrfs_ioctl_defrag_range_args
*range
;
1512 ret
= mnt_want_write(file
->f_path
.mnt
);
1516 switch (inode
->i_mode
& S_IFMT
) {
1518 if (!capable(CAP_SYS_ADMIN
)) {
1522 ret
= btrfs_defrag_root(root
, 0);
1525 ret
= btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
1528 if (!(file
->f_mode
& FMODE_WRITE
)) {
1533 range
= kzalloc(sizeof(*range
), GFP_KERNEL
);
1540 if (copy_from_user(range
, argp
,
1546 /* compression requires us to start the IO */
1547 if ((range
->flags
& BTRFS_DEFRAG_RANGE_COMPRESS
)) {
1548 range
->flags
|= BTRFS_DEFRAG_RANGE_START_IO
;
1549 range
->extent_thresh
= (u32
)-1;
1552 /* the rest are all set to zero by kzalloc */
1553 range
->len
= (u64
)-1;
1555 ret
= btrfs_defrag_file(file
, range
);
1562 mnt_drop_write(file
->f_path
.mnt
);
1566 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
1568 struct btrfs_ioctl_vol_args
*vol_args
;
1571 if (!capable(CAP_SYS_ADMIN
))
1574 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1575 if (IS_ERR(vol_args
))
1576 return PTR_ERR(vol_args
);
1578 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1579 ret
= btrfs_init_new_device(root
, vol_args
->name
);
1585 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
1587 struct btrfs_ioctl_vol_args
*vol_args
;
1590 if (!capable(CAP_SYS_ADMIN
))
1593 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
1596 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
1597 if (IS_ERR(vol_args
))
1598 return PTR_ERR(vol_args
);
1600 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
1601 ret
= btrfs_rm_device(root
, vol_args
->name
);
1607 static noinline
long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
1608 u64 off
, u64 olen
, u64 destoff
)
1610 struct inode
*inode
= fdentry(file
)->d_inode
;
1611 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1612 struct file
*src_file
;
1614 struct btrfs_trans_handle
*trans
;
1615 struct btrfs_path
*path
;
1616 struct extent_buffer
*leaf
;
1618 struct btrfs_key key
;
1623 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
1628 * - split compressed inline extents. annoying: we need to
1629 * decompress into destination's address_space (the file offset
1630 * may change, so source mapping won't do), then recompress (or
1631 * otherwise reinsert) a subrange.
1632 * - allow ranges within the same file to be cloned (provided
1633 * they don't overlap)?
1636 /* the destination must be opened for writing */
1637 if (!(file
->f_mode
& FMODE_WRITE
) || (file
->f_flags
& O_APPEND
))
1640 ret
= mnt_want_write(file
->f_path
.mnt
);
1644 src_file
= fget(srcfd
);
1647 goto out_drop_write
;
1650 src
= src_file
->f_dentry
->d_inode
;
1656 /* the src must be open for reading */
1657 if (!(src_file
->f_mode
& FMODE_READ
))
1661 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
1665 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
1669 buf
= vmalloc(btrfs_level_size(root
, 0));
1673 path
= btrfs_alloc_path();
1681 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_PARENT
);
1682 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_CHILD
);
1684 mutex_lock_nested(&src
->i_mutex
, I_MUTEX_PARENT
);
1685 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1688 /* determine range to clone */
1690 if (off
+ len
> src
->i_size
|| off
+ len
< off
)
1693 olen
= len
= src
->i_size
- off
;
1694 /* if we extend to eof, continue to block boundary */
1695 if (off
+ len
== src
->i_size
)
1696 len
= ALIGN(src
->i_size
, bs
) - off
;
1698 /* verify the end result is block aligned */
1699 if (!IS_ALIGNED(off
, bs
) || !IS_ALIGNED(off
+ len
, bs
) ||
1700 !IS_ALIGNED(destoff
, bs
))
1703 /* do any pending delalloc/csum calc on src, one way or
1704 another, and lock file content */
1706 struct btrfs_ordered_extent
*ordered
;
1707 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1708 ordered
= btrfs_lookup_first_ordered_extent(src
, off
+len
);
1710 !test_range_bit(&BTRFS_I(src
)->io_tree
, off
, off
+len
,
1711 EXTENT_DELALLOC
, 0, NULL
))
1713 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1715 btrfs_put_ordered_extent(ordered
);
1716 btrfs_wait_ordered_range(src
, off
, len
);
1720 key
.objectid
= src
->i_ino
;
1721 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1726 * note the key will change type as we walk through the
1729 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1733 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1734 if (path
->slots
[0] >= nritems
) {
1735 ret
= btrfs_next_leaf(root
, path
);
1740 nritems
= btrfs_header_nritems(path
->nodes
[0]);
1742 leaf
= path
->nodes
[0];
1743 slot
= path
->slots
[0];
1745 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
1746 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
1747 key
.objectid
!= src
->i_ino
)
1750 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
1751 struct btrfs_file_extent_item
*extent
;
1754 struct btrfs_key new_key
;
1755 u64 disko
= 0, diskl
= 0;
1756 u64 datao
= 0, datal
= 0;
1760 size
= btrfs_item_size_nr(leaf
, slot
);
1761 read_extent_buffer(leaf
, buf
,
1762 btrfs_item_ptr_offset(leaf
, slot
),
1765 extent
= btrfs_item_ptr(leaf
, slot
,
1766 struct btrfs_file_extent_item
);
1767 comp
= btrfs_file_extent_compression(leaf
, extent
);
1768 type
= btrfs_file_extent_type(leaf
, extent
);
1769 if (type
== BTRFS_FILE_EXTENT_REG
||
1770 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1771 disko
= btrfs_file_extent_disk_bytenr(leaf
,
1773 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
1775 datao
= btrfs_file_extent_offset(leaf
, extent
);
1776 datal
= btrfs_file_extent_num_bytes(leaf
,
1778 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1779 /* take upper bound, may be compressed */
1780 datal
= btrfs_file_extent_ram_bytes(leaf
,
1783 btrfs_release_path(root
, path
);
1785 if (key
.offset
+ datal
<= off
||
1786 key
.offset
>= off
+len
)
1789 memcpy(&new_key
, &key
, sizeof(new_key
));
1790 new_key
.objectid
= inode
->i_ino
;
1791 if (off
<= key
.offset
)
1792 new_key
.offset
= key
.offset
+ destoff
- off
;
1794 new_key
.offset
= destoff
;
1796 trans
= btrfs_start_transaction(root
, 1);
1797 if (IS_ERR(trans
)) {
1798 ret
= PTR_ERR(trans
);
1802 if (type
== BTRFS_FILE_EXTENT_REG
||
1803 type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1804 if (off
> key
.offset
) {
1805 datao
+= off
- key
.offset
;
1806 datal
-= off
- key
.offset
;
1809 if (key
.offset
+ datal
> off
+ len
)
1810 datal
= off
+ len
- key
.offset
;
1812 ret
= btrfs_drop_extents(trans
, inode
,
1814 new_key
.offset
+ datal
,
1818 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1822 leaf
= path
->nodes
[0];
1823 slot
= path
->slots
[0];
1824 write_extent_buffer(leaf
, buf
,
1825 btrfs_item_ptr_offset(leaf
, slot
),
1828 extent
= btrfs_item_ptr(leaf
, slot
,
1829 struct btrfs_file_extent_item
);
1831 /* disko == 0 means it's a hole */
1835 btrfs_set_file_extent_offset(leaf
, extent
,
1837 btrfs_set_file_extent_num_bytes(leaf
, extent
,
1840 inode_add_bytes(inode
, datal
);
1841 ret
= btrfs_inc_extent_ref(trans
, root
,
1843 root
->root_key
.objectid
,
1845 new_key
.offset
- datao
);
1848 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
1851 if (off
> key
.offset
) {
1852 skip
= off
- key
.offset
;
1853 new_key
.offset
+= skip
;
1856 if (key
.offset
+ datal
> off
+len
)
1857 trim
= key
.offset
+ datal
- (off
+len
);
1859 if (comp
&& (skip
|| trim
)) {
1861 btrfs_end_transaction(trans
, root
);
1864 size
-= skip
+ trim
;
1865 datal
-= skip
+ trim
;
1867 ret
= btrfs_drop_extents(trans
, inode
,
1869 new_key
.offset
+ datal
,
1873 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1879 btrfs_file_extent_calc_inline_size(0);
1880 memmove(buf
+start
, buf
+start
+skip
,
1884 leaf
= path
->nodes
[0];
1885 slot
= path
->slots
[0];
1886 write_extent_buffer(leaf
, buf
,
1887 btrfs_item_ptr_offset(leaf
, slot
),
1889 inode_add_bytes(inode
, datal
);
1892 btrfs_mark_buffer_dirty(leaf
);
1893 btrfs_release_path(root
, path
);
1895 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1898 * we round up to the block size at eof when
1899 * determining which extents to clone above,
1900 * but shouldn't round up the file size
1902 endoff
= new_key
.offset
+ datal
;
1903 if (endoff
> destoff
+olen
)
1904 endoff
= destoff
+olen
;
1905 if (endoff
> inode
->i_size
)
1906 btrfs_i_size_write(inode
, endoff
);
1908 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1909 ret
= btrfs_update_inode(trans
, root
, inode
);
1911 btrfs_end_transaction(trans
, root
);
1914 btrfs_release_path(root
, path
);
1919 btrfs_release_path(root
, path
);
1920 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1922 mutex_unlock(&src
->i_mutex
);
1923 mutex_unlock(&inode
->i_mutex
);
1925 btrfs_free_path(path
);
1929 mnt_drop_write(file
->f_path
.mnt
);
1933 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1935 struct btrfs_ioctl_clone_range_args args
;
1937 if (copy_from_user(&args
, argp
, sizeof(args
)))
1939 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1940 args
.src_length
, args
.dest_offset
);
1944 * there are many ways the trans_start and trans_end ioctls can lead
1945 * to deadlocks. They should only be used by applications that
1946 * basically own the machine, and have a very in depth understanding
1947 * of all the possible deadlocks and enospc problems.
1949 static long btrfs_ioctl_trans_start(struct file
*file
)
1951 struct inode
*inode
= fdentry(file
)->d_inode
;
1952 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1953 struct btrfs_trans_handle
*trans
;
1957 if (!capable(CAP_SYS_ADMIN
))
1961 if (file
->private_data
)
1964 ret
= mnt_want_write(file
->f_path
.mnt
);
1968 mutex_lock(&root
->fs_info
->trans_mutex
);
1969 root
->fs_info
->open_ioctl_trans
++;
1970 mutex_unlock(&root
->fs_info
->trans_mutex
);
1973 trans
= btrfs_start_ioctl_transaction(root
, 0);
1977 file
->private_data
= trans
;
1981 mutex_lock(&root
->fs_info
->trans_mutex
);
1982 root
->fs_info
->open_ioctl_trans
--;
1983 mutex_unlock(&root
->fs_info
->trans_mutex
);
1984 mnt_drop_write(file
->f_path
.mnt
);
1989 static long btrfs_ioctl_default_subvol(struct file
*file
, void __user
*argp
)
1991 struct inode
*inode
= fdentry(file
)->d_inode
;
1992 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1993 struct btrfs_root
*new_root
;
1994 struct btrfs_dir_item
*di
;
1995 struct btrfs_trans_handle
*trans
;
1996 struct btrfs_path
*path
;
1997 struct btrfs_key location
;
1998 struct btrfs_disk_key disk_key
;
1999 struct btrfs_super_block
*disk_super
;
2004 if (!capable(CAP_SYS_ADMIN
))
2007 if (copy_from_user(&objectid
, argp
, sizeof(objectid
)))
2011 objectid
= root
->root_key
.objectid
;
2013 location
.objectid
= objectid
;
2014 location
.type
= BTRFS_ROOT_ITEM_KEY
;
2015 location
.offset
= (u64
)-1;
2017 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &location
);
2018 if (IS_ERR(new_root
))
2019 return PTR_ERR(new_root
);
2021 if (btrfs_root_refs(&new_root
->root_item
) == 0)
2024 path
= btrfs_alloc_path();
2027 path
->leave_spinning
= 1;
2029 trans
= btrfs_start_transaction(root
, 1);
2031 btrfs_free_path(path
);
2035 dir_id
= btrfs_super_root_dir(&root
->fs_info
->super_copy
);
2036 di
= btrfs_lookup_dir_item(trans
, root
->fs_info
->tree_root
, path
,
2037 dir_id
, "default", 7, 1);
2038 if (IS_ERR_OR_NULL(di
)) {
2039 btrfs_free_path(path
);
2040 btrfs_end_transaction(trans
, root
);
2041 printk(KERN_ERR
"Umm, you don't have the default dir item, "
2042 "this isn't going to work\n");
2046 btrfs_cpu_key_to_disk(&disk_key
, &new_root
->root_key
);
2047 btrfs_set_dir_item_key(path
->nodes
[0], di
, &disk_key
);
2048 btrfs_mark_buffer_dirty(path
->nodes
[0]);
2049 btrfs_free_path(path
);
2051 disk_super
= &root
->fs_info
->super_copy
;
2052 features
= btrfs_super_incompat_flags(disk_super
);
2053 if (!(features
& BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
)) {
2054 features
|= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL
;
2055 btrfs_set_super_incompat_flags(disk_super
, features
);
2057 btrfs_end_transaction(trans
, root
);
2062 static void get_block_group_info(struct list_head
*groups_list
,
2063 struct btrfs_ioctl_space_info
*space
)
2065 struct btrfs_block_group_cache
*block_group
;
2067 space
->total_bytes
= 0;
2068 space
->used_bytes
= 0;
2070 list_for_each_entry(block_group
, groups_list
, list
) {
2071 space
->flags
= block_group
->flags
;
2072 space
->total_bytes
+= block_group
->key
.offset
;
2073 space
->used_bytes
+=
2074 btrfs_block_group_used(&block_group
->item
);
2078 long btrfs_ioctl_space_info(struct btrfs_root
*root
, void __user
*arg
)
2080 struct btrfs_ioctl_space_args space_args
;
2081 struct btrfs_ioctl_space_info space
;
2082 struct btrfs_ioctl_space_info
*dest
;
2083 struct btrfs_ioctl_space_info
*dest_orig
;
2084 struct btrfs_ioctl_space_info
*user_dest
;
2085 struct btrfs_space_info
*info
;
2086 u64 types
[] = {BTRFS_BLOCK_GROUP_DATA
,
2087 BTRFS_BLOCK_GROUP_SYSTEM
,
2088 BTRFS_BLOCK_GROUP_METADATA
,
2089 BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
};
2096 if (copy_from_user(&space_args
,
2097 (struct btrfs_ioctl_space_args __user
*)arg
,
2098 sizeof(space_args
)))
2101 for (i
= 0; i
< num_types
; i
++) {
2102 struct btrfs_space_info
*tmp
;
2106 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2108 if (tmp
->flags
== types
[i
]) {
2118 down_read(&info
->groups_sem
);
2119 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2120 if (!list_empty(&info
->block_groups
[c
]))
2123 up_read(&info
->groups_sem
);
2126 /* space_slots == 0 means they are asking for a count */
2127 if (space_args
.space_slots
== 0) {
2128 space_args
.total_spaces
= slot_count
;
2132 slot_count
= min_t(int, space_args
.space_slots
, slot_count
);
2134 alloc_size
= sizeof(*dest
) * slot_count
;
2136 /* we generally have at most 6 or so space infos, one for each raid
2137 * level. So, a whole page should be more than enough for everyone
2139 if (alloc_size
> PAGE_CACHE_SIZE
)
2142 space_args
.total_spaces
= 0;
2143 dest
= kmalloc(alloc_size
, GFP_NOFS
);
2148 /* now we have a buffer to copy into */
2149 for (i
= 0; i
< num_types
; i
++) {
2150 struct btrfs_space_info
*tmp
;
2154 list_for_each_entry_rcu(tmp
, &root
->fs_info
->space_info
,
2156 if (tmp
->flags
== types
[i
]) {
2165 down_read(&info
->groups_sem
);
2166 for (c
= 0; c
< BTRFS_NR_RAID_TYPES
; c
++) {
2167 if (!list_empty(&info
->block_groups
[c
])) {
2168 get_block_group_info(&info
->block_groups
[c
],
2170 memcpy(dest
, &space
, sizeof(space
));
2172 space_args
.total_spaces
++;
2175 up_read(&info
->groups_sem
);
2178 user_dest
= (struct btrfs_ioctl_space_info
*)
2179 (arg
+ sizeof(struct btrfs_ioctl_space_args
));
2181 if (copy_to_user(user_dest
, dest_orig
, alloc_size
))
2186 if (ret
== 0 && copy_to_user(arg
, &space_args
, sizeof(space_args
)))
2193 * there are many ways the trans_start and trans_end ioctls can lead
2194 * to deadlocks. They should only be used by applications that
2195 * basically own the machine, and have a very in depth understanding
2196 * of all the possible deadlocks and enospc problems.
2198 long btrfs_ioctl_trans_end(struct file
*file
)
2200 struct inode
*inode
= fdentry(file
)->d_inode
;
2201 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2202 struct btrfs_trans_handle
*trans
;
2204 trans
= file
->private_data
;
2207 file
->private_data
= NULL
;
2209 btrfs_end_transaction(trans
, root
);
2211 mutex_lock(&root
->fs_info
->trans_mutex
);
2212 root
->fs_info
->open_ioctl_trans
--;
2213 mutex_unlock(&root
->fs_info
->trans_mutex
);
2215 mnt_drop_write(file
->f_path
.mnt
);
2219 static noinline
long btrfs_ioctl_start_sync(struct file
*file
, void __user
*argp
)
2221 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2222 struct btrfs_trans_handle
*trans
;
2225 trans
= btrfs_start_transaction(root
, 0);
2226 transid
= trans
->transid
;
2227 btrfs_commit_transaction_async(trans
, root
, 0);
2230 if (copy_to_user(argp
, &transid
, sizeof(transid
)))
2235 static noinline
long btrfs_ioctl_wait_sync(struct file
*file
, void __user
*argp
)
2237 struct btrfs_root
*root
= BTRFS_I(file
->f_dentry
->d_inode
)->root
;
2241 if (copy_from_user(&transid
, argp
, sizeof(transid
)))
2244 transid
= 0; /* current trans */
2246 return btrfs_wait_for_commit(root
, transid
);
2249 long btrfs_ioctl(struct file
*file
, unsigned int
2250 cmd
, unsigned long arg
)
2252 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
2253 void __user
*argp
= (void __user
*)arg
;
2256 case FS_IOC_GETFLAGS
:
2257 return btrfs_ioctl_getflags(file
, argp
);
2258 case FS_IOC_SETFLAGS
:
2259 return btrfs_ioctl_setflags(file
, argp
);
2260 case FS_IOC_GETVERSION
:
2261 return btrfs_ioctl_getversion(file
, argp
);
2262 case BTRFS_IOC_SNAP_CREATE
:
2263 return btrfs_ioctl_snap_create(file
, argp
, 0, 0);
2264 case BTRFS_IOC_SNAP_CREATE_V2
:
2265 return btrfs_ioctl_snap_create(file
, argp
, 0, 1);
2266 case BTRFS_IOC_SUBVOL_CREATE
:
2267 return btrfs_ioctl_snap_create(file
, argp
, 1, 0);
2268 case BTRFS_IOC_SNAP_DESTROY
:
2269 return btrfs_ioctl_snap_destroy(file
, argp
);
2270 case BTRFS_IOC_DEFAULT_SUBVOL
:
2271 return btrfs_ioctl_default_subvol(file
, argp
);
2272 case BTRFS_IOC_DEFRAG
:
2273 return btrfs_ioctl_defrag(file
, NULL
);
2274 case BTRFS_IOC_DEFRAG_RANGE
:
2275 return btrfs_ioctl_defrag(file
, argp
);
2276 case BTRFS_IOC_RESIZE
:
2277 return btrfs_ioctl_resize(root
, argp
);
2278 case BTRFS_IOC_ADD_DEV
:
2279 return btrfs_ioctl_add_dev(root
, argp
);
2280 case BTRFS_IOC_RM_DEV
:
2281 return btrfs_ioctl_rm_dev(root
, argp
);
2282 case BTRFS_IOC_BALANCE
:
2283 return btrfs_balance(root
->fs_info
->dev_root
);
2284 case BTRFS_IOC_CLONE
:
2285 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
2286 case BTRFS_IOC_CLONE_RANGE
:
2287 return btrfs_ioctl_clone_range(file
, argp
);
2288 case BTRFS_IOC_TRANS_START
:
2289 return btrfs_ioctl_trans_start(file
);
2290 case BTRFS_IOC_TRANS_END
:
2291 return btrfs_ioctl_trans_end(file
);
2292 case BTRFS_IOC_TREE_SEARCH
:
2293 return btrfs_ioctl_tree_search(file
, argp
);
2294 case BTRFS_IOC_INO_LOOKUP
:
2295 return btrfs_ioctl_ino_lookup(file
, argp
);
2296 case BTRFS_IOC_SPACE_INFO
:
2297 return btrfs_ioctl_space_info(root
, argp
);
2298 case BTRFS_IOC_SYNC
:
2299 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);
2301 case BTRFS_IOC_START_SYNC
:
2302 return btrfs_ioctl_start_sync(file
, argp
);
2303 case BTRFS_IOC_WAIT_SYNC
:
2304 return btrfs_ioctl_wait_sync(file
, argp
);