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/smp_lock.h>
31 #include <linux/backing-dev.h>
32 #include <linux/mount.h>
33 #include <linux/mpage.h>
34 #include <linux/namei.h>
35 #include <linux/swap.h>
36 #include <linux/writeback.h>
37 #include <linux/statfs.h>
38 #include <linux/compat.h>
39 #include <linux/bit_spinlock.h>
40 #include <linux/security.h>
41 #include <linux/xattr.h>
42 #include <linux/vmalloc.h>
46 #include "transaction.h"
47 #include "btrfs_inode.h"
49 #include "print-tree.h"
55 static noinline
int create_subvol(struct btrfs_root
*root
,
56 struct dentry
*dentry
,
57 char *name
, int namelen
)
59 struct btrfs_trans_handle
*trans
;
61 struct btrfs_root_item root_item
;
62 struct btrfs_inode_item
*inode_item
;
63 struct extent_buffer
*leaf
;
64 struct btrfs_root
*new_root
= root
;
69 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
73 ret
= btrfs_check_metadata_free_space(root
);
77 trans
= btrfs_start_transaction(root
, 1);
80 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
85 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
86 objectid
, trans
->transid
, 0, 0, 0);
92 btrfs_set_header_nritems(leaf
, 0);
93 btrfs_set_header_level(leaf
, 0);
94 btrfs_set_header_bytenr(leaf
, leaf
->start
);
95 btrfs_set_header_generation(leaf
, trans
->transid
);
96 btrfs_set_header_owner(leaf
, objectid
);
98 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
99 (unsigned long)btrfs_header_fsid(leaf
),
101 btrfs_mark_buffer_dirty(leaf
);
103 inode_item
= &root_item
.inode
;
104 memset(inode_item
, 0, sizeof(*inode_item
));
105 inode_item
->generation
= cpu_to_le64(1);
106 inode_item
->size
= cpu_to_le64(3);
107 inode_item
->nlink
= cpu_to_le32(1);
108 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
109 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
111 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
112 btrfs_set_root_generation(&root_item
, trans
->transid
);
113 btrfs_set_root_level(&root_item
, 0);
114 btrfs_set_root_refs(&root_item
, 1);
115 btrfs_set_root_used(&root_item
, 0);
116 btrfs_set_root_last_snapshot(&root_item
, 0);
118 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
119 root_item
.drop_level
= 0;
121 btrfs_tree_unlock(leaf
);
122 free_extent_buffer(leaf
);
125 btrfs_set_root_dirid(&root_item
, new_dirid
);
127 key
.objectid
= objectid
;
129 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
130 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
136 * insert the directory item
138 key
.offset
= (u64
)-1;
139 dir
= dentry
->d_parent
->d_inode
;
140 ret
= btrfs_set_inode_index(dir
, &index
);
143 ret
= btrfs_insert_dir_item(trans
, root
,
144 name
, namelen
, dir
->i_ino
, &key
,
145 BTRFS_FT_DIR
, index
);
149 btrfs_i_size_write(dir
, dir
->i_size
+ namelen
* 2);
150 ret
= btrfs_update_inode(trans
, root
, dir
);
153 /* add the backref first */
154 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
155 objectid
, BTRFS_ROOT_BACKREF_KEY
,
156 root
->root_key
.objectid
,
157 dir
->i_ino
, index
, name
, namelen
);
161 /* now add the forward ref */
162 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
163 root
->root_key
.objectid
, BTRFS_ROOT_REF_KEY
,
165 dir
->i_ino
, index
, name
, namelen
);
169 ret
= btrfs_commit_transaction(trans
, root
);
173 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
176 trans
= btrfs_start_transaction(new_root
, 1);
179 ret
= btrfs_create_subvol_root(trans
, new_root
, dentry
, new_dirid
,
180 BTRFS_I(dir
)->block_group
);
185 nr
= trans
->blocks_used
;
186 err
= btrfs_commit_transaction(trans
, new_root
);
190 btrfs_btree_balance_dirty(root
, nr
);
194 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
195 char *name
, int namelen
)
197 struct btrfs_pending_snapshot
*pending_snapshot
;
198 struct btrfs_trans_handle
*trans
;
201 unsigned long nr
= 0;
206 ret
= btrfs_check_metadata_free_space(root
);
210 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
211 if (!pending_snapshot
) {
215 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
216 if (!pending_snapshot
->name
) {
218 kfree(pending_snapshot
);
221 memcpy(pending_snapshot
->name
, name
, namelen
);
222 pending_snapshot
->name
[namelen
] = '\0';
223 pending_snapshot
->dentry
= dentry
;
224 trans
= btrfs_start_transaction(root
, 1);
226 pending_snapshot
->root
= root
;
227 list_add(&pending_snapshot
->list
,
228 &trans
->transaction
->pending_snapshots
);
229 err
= btrfs_commit_transaction(trans
, root
);
232 btrfs_btree_balance_dirty(root
, nr
);
236 /* copy of may_create in fs/namei.c() */
237 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
243 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
247 * Create a new subvolume below @parent. This is largely modeled after
248 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
249 * inside this filesystem so it's quite a bit simpler.
251 static noinline
int btrfs_mksubvol(struct path
*parent
, char *name
,
252 int mode
, int namelen
,
253 struct btrfs_root
*snap_src
)
255 struct dentry
*dentry
;
258 mutex_lock_nested(&parent
->dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
260 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
261 error
= PTR_ERR(dentry
);
269 if (!IS_POSIXACL(parent
->dentry
->d_inode
))
270 mode
&= ~current_umask();
272 error
= mnt_want_write(parent
->mnt
);
276 error
= btrfs_may_create(parent
->dentry
->d_inode
, dentry
);
281 * Actually perform the low-level subvolume creation after all
284 * Eventually we want to pass in an inode under which we create this
285 * subvolume, but for now all are under the filesystem root.
287 * Also we should pass on the mode eventually to allow creating new
288 * subvolume with specific mode bits.
291 struct dentry
*dir
= dentry
->d_parent
;
292 struct dentry
*test
= dir
->d_parent
;
293 struct btrfs_path
*path
= btrfs_alloc_path();
296 u64 parent_oid
= BTRFS_I(dir
->d_inode
)->root
->root_key
.objectid
;
298 test_oid
= snap_src
->root_key
.objectid
;
300 ret
= btrfs_find_root_ref(snap_src
->fs_info
->tree_root
,
301 path
, parent_oid
, test_oid
);
304 btrfs_release_path(snap_src
->fs_info
->tree_root
, path
);
306 /* we need to make sure we aren't creating a directory loop
307 * by taking a snapshot of something that has our current
308 * subvol in its directory tree. So, this loops through
309 * the dentries and checks the forward refs for each subvolume
310 * to see if is references the subvolume where we are
311 * placing this new snapshot.
315 dir
== snap_src
->fs_info
->sb
->s_root
||
316 test
== snap_src
->fs_info
->sb
->s_root
||
317 test
->d_inode
->i_sb
!= snap_src
->fs_info
->sb
) {
320 if (S_ISLNK(test
->d_inode
->i_mode
)) {
321 printk(KERN_INFO
"Btrfs symlink in snapshot "
324 btrfs_free_path(path
);
328 BTRFS_I(test
->d_inode
)->root
->root_key
.objectid
;
329 ret
= btrfs_find_root_ref(snap_src
->fs_info
->tree_root
,
330 path
, test_oid
, parent_oid
);
332 printk(KERN_INFO
"Btrfs snapshot creation "
333 "failed, looping\n");
335 btrfs_free_path(path
);
338 btrfs_release_path(snap_src
->fs_info
->tree_root
, path
);
339 test
= test
->d_parent
;
342 btrfs_free_path(path
);
343 error
= create_snapshot(snap_src
, dentry
, name
, namelen
);
345 error
= create_subvol(BTRFS_I(parent
->dentry
->d_inode
)->root
,
346 dentry
, name
, namelen
);
351 fsnotify_mkdir(parent
->dentry
->d_inode
, dentry
);
353 mnt_drop_write(parent
->mnt
);
357 mutex_unlock(&parent
->dentry
->d_inode
->i_mutex
);
362 static int btrfs_defrag_file(struct file
*file
)
364 struct inode
*inode
= fdentry(file
)->d_inode
;
365 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
366 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
367 struct btrfs_ordered_extent
*ordered
;
369 unsigned long last_index
;
370 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
371 unsigned long total_read
= 0;
377 ret
= btrfs_check_data_free_space(root
, inode
, inode
->i_size
);
381 mutex_lock(&inode
->i_mutex
);
382 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
383 for (i
= 0; i
<= last_index
; i
++) {
384 if (total_read
% ra_pages
== 0) {
385 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
386 min(last_index
, i
+ ra_pages
- 1));
390 page
= grab_cache_page(inode
->i_mapping
, i
);
393 if (!PageUptodate(page
)) {
394 btrfs_readpage(NULL
, page
);
396 if (!PageUptodate(page
)) {
398 page_cache_release(page
);
403 wait_on_page_writeback(page
);
405 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
406 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
407 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
409 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
411 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
413 page_cache_release(page
);
414 btrfs_start_ordered_extent(inode
, ordered
, 1);
415 btrfs_put_ordered_extent(ordered
);
418 set_page_extent_mapped(page
);
421 * this makes sure page_mkwrite is called on the
422 * page if it is dirtied again later
424 clear_page_dirty_for_io(page
);
426 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
428 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
429 set_page_dirty(page
);
431 page_cache_release(page
);
432 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
436 mutex_unlock(&inode
->i_mutex
);
440 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
445 struct btrfs_ioctl_vol_args
*vol_args
;
446 struct btrfs_trans_handle
*trans
;
447 struct btrfs_device
*device
= NULL
;
454 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
457 if (!capable(CAP_SYS_ADMIN
))
460 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
461 if (IS_ERR(vol_args
))
462 return PTR_ERR(vol_args
);
464 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
465 namelen
= strlen(vol_args
->name
);
467 mutex_lock(&root
->fs_info
->volume_mutex
);
468 sizestr
= vol_args
->name
;
469 devstr
= strchr(sizestr
, ':');
472 sizestr
= devstr
+ 1;
474 devstr
= vol_args
->name
;
475 devid
= simple_strtoull(devstr
, &end
, 10);
476 printk(KERN_INFO
"resizing devid %llu\n",
477 (unsigned long long)devid
);
479 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
481 printk(KERN_INFO
"resizer unable to find device %llu\n",
482 (unsigned long long)devid
);
486 if (!strcmp(sizestr
, "max"))
487 new_size
= device
->bdev
->bd_inode
->i_size
;
489 if (sizestr
[0] == '-') {
492 } else if (sizestr
[0] == '+') {
496 new_size
= btrfs_parse_size(sizestr
);
503 old_size
= device
->total_bytes
;
506 if (new_size
> old_size
) {
510 new_size
= old_size
- new_size
;
511 } else if (mod
> 0) {
512 new_size
= old_size
+ new_size
;
515 if (new_size
< 256 * 1024 * 1024) {
519 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
524 do_div(new_size
, root
->sectorsize
);
525 new_size
*= root
->sectorsize
;
527 printk(KERN_INFO
"new size for %s is %llu\n",
528 device
->name
, (unsigned long long)new_size
);
530 if (new_size
> old_size
) {
531 trans
= btrfs_start_transaction(root
, 1);
532 ret
= btrfs_grow_device(trans
, device
, new_size
);
533 btrfs_commit_transaction(trans
, root
);
535 ret
= btrfs_shrink_device(device
, new_size
);
539 mutex_unlock(&root
->fs_info
->volume_mutex
);
544 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
545 void __user
*arg
, int subvol
)
547 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
548 struct btrfs_ioctl_vol_args
*vol_args
;
549 struct btrfs_dir_item
*di
;
550 struct btrfs_path
*path
;
551 struct file
*src_file
;
556 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
559 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
560 if (IS_ERR(vol_args
))
561 return PTR_ERR(vol_args
);
563 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
564 namelen
= strlen(vol_args
->name
);
565 if (strchr(vol_args
->name
, '/')) {
570 path
= btrfs_alloc_path();
576 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
577 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
579 vol_args
->name
, namelen
, 0);
580 btrfs_free_path(path
);
582 if (di
&& !IS_ERR(di
)) {
593 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
594 file
->f_path
.dentry
->d_inode
->i_mode
,
597 struct inode
*src_inode
;
598 src_file
= fget(vol_args
->fd
);
604 src_inode
= src_file
->f_path
.dentry
->d_inode
;
605 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
606 printk(KERN_INFO
"btrfs: Snapshot src from "
612 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
613 file
->f_path
.dentry
->d_inode
->i_mode
,
614 namelen
, BTRFS_I(src_inode
)->root
);
623 static int btrfs_ioctl_defrag(struct file
*file
)
625 struct inode
*inode
= fdentry(file
)->d_inode
;
626 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
629 ret
= mnt_want_write(file
->f_path
.mnt
);
633 switch (inode
->i_mode
& S_IFMT
) {
635 if (!capable(CAP_SYS_ADMIN
)) {
639 btrfs_defrag_root(root
, 0);
640 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
643 if (!(file
->f_mode
& FMODE_WRITE
)) {
647 btrfs_defrag_file(file
);
651 mnt_drop_write(file
->f_path
.mnt
);
655 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
657 struct btrfs_ioctl_vol_args
*vol_args
;
660 if (!capable(CAP_SYS_ADMIN
))
663 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
664 if (IS_ERR(vol_args
))
665 return PTR_ERR(vol_args
);
667 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
668 ret
= btrfs_init_new_device(root
, vol_args
->name
);
674 static long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
676 struct btrfs_ioctl_vol_args
*vol_args
;
679 if (!capable(CAP_SYS_ADMIN
))
682 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
685 vol_args
= memdup_user(arg
, sizeof(*vol_args
));
686 if (IS_ERR(vol_args
))
687 return PTR_ERR(vol_args
);
689 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
690 ret
= btrfs_rm_device(root
, vol_args
->name
);
696 static long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
697 u64 off
, u64 olen
, u64 destoff
)
699 struct inode
*inode
= fdentry(file
)->d_inode
;
700 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
701 struct file
*src_file
;
703 struct btrfs_trans_handle
*trans
;
704 struct btrfs_path
*path
;
705 struct extent_buffer
*leaf
;
707 struct btrfs_key key
;
712 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
717 * - split compressed inline extents. annoying: we need to
718 * decompress into destination's address_space (the file offset
719 * may change, so source mapping won't do), then recompress (or
720 * otherwise reinsert) a subrange.
721 * - allow ranges within the same file to be cloned (provided
722 * they don't overlap)?
725 /* the destination must be opened for writing */
726 if (!(file
->f_mode
& FMODE_WRITE
))
729 ret
= mnt_want_write(file
->f_path
.mnt
);
733 src_file
= fget(srcfd
);
738 src
= src_file
->f_dentry
->d_inode
;
745 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
749 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
753 buf
= vmalloc(btrfs_level_size(root
, 0));
757 path
= btrfs_alloc_path();
765 mutex_lock(&inode
->i_mutex
);
766 mutex_lock(&src
->i_mutex
);
768 mutex_lock(&src
->i_mutex
);
769 mutex_lock(&inode
->i_mutex
);
772 /* determine range to clone */
774 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
777 olen
= len
= src
->i_size
- off
;
778 /* if we extend to eof, continue to block boundary */
779 if (off
+ len
== src
->i_size
)
780 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
783 /* verify the end result is block aligned */
784 if ((off
& (bs
-1)) ||
785 ((off
+ len
) & (bs
-1)))
788 /* do any pending delalloc/csum calc on src, one way or
789 another, and lock file content */
791 struct btrfs_ordered_extent
*ordered
;
792 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
793 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
794 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
796 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
798 btrfs_put_ordered_extent(ordered
);
799 btrfs_wait_ordered_range(src
, off
, off
+len
);
802 trans
= btrfs_start_transaction(root
, 1);
805 /* punch hole in destination first */
806 btrfs_drop_extents(trans
, root
, inode
, off
, off
+ len
,
807 off
+ len
, 0, &hint_byte
);
810 key
.objectid
= src
->i_ino
;
811 key
.type
= BTRFS_EXTENT_DATA_KEY
;
816 * note the key will change type as we walk through the
819 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
823 nritems
= btrfs_header_nritems(path
->nodes
[0]);
824 if (path
->slots
[0] >= nritems
) {
825 ret
= btrfs_next_leaf(root
, path
);
830 nritems
= btrfs_header_nritems(path
->nodes
[0]);
832 leaf
= path
->nodes
[0];
833 slot
= path
->slots
[0];
835 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
836 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
837 key
.objectid
!= src
->i_ino
)
840 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
841 struct btrfs_file_extent_item
*extent
;
844 struct btrfs_key new_key
;
845 u64 disko
= 0, diskl
= 0;
846 u64 datao
= 0, datal
= 0;
849 size
= btrfs_item_size_nr(leaf
, slot
);
850 read_extent_buffer(leaf
, buf
,
851 btrfs_item_ptr_offset(leaf
, slot
),
854 extent
= btrfs_item_ptr(leaf
, slot
,
855 struct btrfs_file_extent_item
);
856 comp
= btrfs_file_extent_compression(leaf
, extent
);
857 type
= btrfs_file_extent_type(leaf
, extent
);
858 if (type
== BTRFS_FILE_EXTENT_REG
) {
859 disko
= btrfs_file_extent_disk_bytenr(leaf
,
861 diskl
= btrfs_file_extent_disk_num_bytes(leaf
,
863 datao
= btrfs_file_extent_offset(leaf
, extent
);
864 datal
= btrfs_file_extent_num_bytes(leaf
,
866 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
867 /* take upper bound, may be compressed */
868 datal
= btrfs_file_extent_ram_bytes(leaf
,
871 btrfs_release_path(root
, path
);
873 if (key
.offset
+ datal
< off
||
874 key
.offset
>= off
+len
)
877 memcpy(&new_key
, &key
, sizeof(new_key
));
878 new_key
.objectid
= inode
->i_ino
;
879 new_key
.offset
= key
.offset
+ destoff
- off
;
881 if (type
== BTRFS_FILE_EXTENT_REG
) {
882 ret
= btrfs_insert_empty_item(trans
, root
, path
,
887 leaf
= path
->nodes
[0];
888 slot
= path
->slots
[0];
889 write_extent_buffer(leaf
, buf
,
890 btrfs_item_ptr_offset(leaf
, slot
),
893 extent
= btrfs_item_ptr(leaf
, slot
,
894 struct btrfs_file_extent_item
);
896 if (off
> key
.offset
) {
897 datao
+= off
- key
.offset
;
898 datal
-= off
- key
.offset
;
900 if (key
.offset
+ datao
+ datal
+ key
.offset
>
902 datal
= off
+ len
- key
.offset
- datao
;
903 /* disko == 0 means it's a hole */
907 btrfs_set_file_extent_offset(leaf
, extent
,
909 btrfs_set_file_extent_num_bytes(leaf
, extent
,
912 inode_add_bytes(inode
, datal
);
913 ret
= btrfs_inc_extent_ref(trans
, root
,
914 disko
, diskl
, leaf
->start
,
915 root
->root_key
.objectid
,
920 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
923 if (off
> key
.offset
) {
924 skip
= off
- key
.offset
;
925 new_key
.offset
+= skip
;
928 if (key
.offset
+ datal
> off
+len
)
929 trim
= key
.offset
+ datal
- (off
+len
);
931 if (comp
&& (skip
|| trim
)) {
936 datal
-= skip
+ trim
;
937 ret
= btrfs_insert_empty_item(trans
, root
, path
,
944 btrfs_file_extent_calc_inline_size(0);
945 memmove(buf
+start
, buf
+start
+skip
,
949 leaf
= path
->nodes
[0];
950 slot
= path
->slots
[0];
951 write_extent_buffer(leaf
, buf
,
952 btrfs_item_ptr_offset(leaf
, slot
),
954 inode_add_bytes(inode
, datal
);
957 btrfs_mark_buffer_dirty(leaf
);
961 btrfs_release_path(root
, path
);
966 btrfs_release_path(root
, path
);
968 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
969 if (destoff
+ olen
> inode
->i_size
)
970 btrfs_i_size_write(inode
, destoff
+ olen
);
971 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
972 ret
= btrfs_update_inode(trans
, root
, inode
);
974 btrfs_end_transaction(trans
, root
);
975 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
977 vmtruncate(inode
, 0);
979 mutex_unlock(&src
->i_mutex
);
980 mutex_unlock(&inode
->i_mutex
);
982 btrfs_free_path(path
);
986 mnt_drop_write(file
->f_path
.mnt
);
990 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
992 struct btrfs_ioctl_clone_range_args args
;
994 if (copy_from_user(&args
, argp
, sizeof(args
)))
996 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
997 args
.src_length
, args
.dest_offset
);
1001 * there are many ways the trans_start and trans_end ioctls can lead
1002 * to deadlocks. They should only be used by applications that
1003 * basically own the machine, and have a very in depth understanding
1004 * of all the possible deadlocks and enospc problems.
1006 static long btrfs_ioctl_trans_start(struct file
*file
)
1008 struct inode
*inode
= fdentry(file
)->d_inode
;
1009 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1010 struct btrfs_trans_handle
*trans
;
1013 if (!capable(CAP_SYS_ADMIN
))
1016 if (file
->private_data
) {
1021 ret
= mnt_want_write(file
->f_path
.mnt
);
1025 mutex_lock(&root
->fs_info
->trans_mutex
);
1026 root
->fs_info
->open_ioctl_trans
++;
1027 mutex_unlock(&root
->fs_info
->trans_mutex
);
1029 trans
= btrfs_start_ioctl_transaction(root
, 0);
1031 file
->private_data
= trans
;
1034 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1040 * there are many ways the trans_start and trans_end ioctls can lead
1041 * to deadlocks. They should only be used by applications that
1042 * basically own the machine, and have a very in depth understanding
1043 * of all the possible deadlocks and enospc problems.
1045 long btrfs_ioctl_trans_end(struct file
*file
)
1047 struct inode
*inode
= fdentry(file
)->d_inode
;
1048 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1049 struct btrfs_trans_handle
*trans
;
1052 trans
= file
->private_data
;
1057 btrfs_end_transaction(trans
, root
);
1058 file
->private_data
= NULL
;
1060 mutex_lock(&root
->fs_info
->trans_mutex
);
1061 root
->fs_info
->open_ioctl_trans
--;
1062 mutex_unlock(&root
->fs_info
->trans_mutex
);
1064 mnt_drop_write(file
->f_path
.mnt
);
1070 long btrfs_ioctl(struct file
*file
, unsigned int
1071 cmd
, unsigned long arg
)
1073 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1074 void __user
*argp
= (void __user
*)arg
;
1077 case BTRFS_IOC_SNAP_CREATE
:
1078 return btrfs_ioctl_snap_create(file
, argp
, 0);
1079 case BTRFS_IOC_SUBVOL_CREATE
:
1080 return btrfs_ioctl_snap_create(file
, argp
, 1);
1081 case BTRFS_IOC_DEFRAG
:
1082 return btrfs_ioctl_defrag(file
);
1083 case BTRFS_IOC_RESIZE
:
1084 return btrfs_ioctl_resize(root
, argp
);
1085 case BTRFS_IOC_ADD_DEV
:
1086 return btrfs_ioctl_add_dev(root
, argp
);
1087 case BTRFS_IOC_RM_DEV
:
1088 return btrfs_ioctl_rm_dev(root
, argp
);
1089 case BTRFS_IOC_BALANCE
:
1090 return btrfs_balance(root
->fs_info
->dev_root
);
1091 case BTRFS_IOC_CLONE
:
1092 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
1093 case BTRFS_IOC_CLONE_RANGE
:
1094 return btrfs_ioctl_clone_range(file
, argp
);
1095 case BTRFS_IOC_TRANS_START
:
1096 return btrfs_ioctl_trans_start(file
);
1097 case BTRFS_IOC_TRANS_END
:
1098 return btrfs_ioctl_trans_end(file
);
1099 case BTRFS_IOC_SYNC
:
1100 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);