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/version.h>
42 #include <linux/xattr.h>
43 #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
;
72 ret
= btrfs_check_free_space(root
, 1, 0);
76 trans
= btrfs_start_transaction(root
, 1);
79 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
84 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
85 objectid
, trans
->transid
, 0, 0, 0);
91 btrfs_set_header_nritems(leaf
, 0);
92 btrfs_set_header_level(leaf
, 0);
93 btrfs_set_header_bytenr(leaf
, leaf
->start
);
94 btrfs_set_header_generation(leaf
, trans
->transid
);
95 btrfs_set_header_owner(leaf
, objectid
);
97 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
98 (unsigned long)btrfs_header_fsid(leaf
),
100 btrfs_mark_buffer_dirty(leaf
);
102 inode_item
= &root_item
.inode
;
103 memset(inode_item
, 0, sizeof(*inode_item
));
104 inode_item
->generation
= cpu_to_le64(1);
105 inode_item
->size
= cpu_to_le64(3);
106 inode_item
->nlink
= cpu_to_le32(1);
107 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
108 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
110 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
111 btrfs_set_root_generation(&root_item
, trans
->transid
);
112 btrfs_set_root_level(&root_item
, 0);
113 btrfs_set_root_refs(&root_item
, 1);
114 btrfs_set_root_used(&root_item
, 0);
115 btrfs_set_root_last_snapshot(&root_item
, 0);
117 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
118 root_item
.drop_level
= 0;
120 btrfs_tree_unlock(leaf
);
121 free_extent_buffer(leaf
);
124 btrfs_set_root_dirid(&root_item
, new_dirid
);
126 key
.objectid
= objectid
;
128 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
129 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
135 * insert the directory item
137 key
.offset
= (u64
)-1;
138 dir
= root
->fs_info
->sb
->s_root
->d_inode
;
139 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
140 name
, namelen
, dir
->i_ino
, &key
,
145 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
146 name
, namelen
, objectid
,
147 root
->fs_info
->sb
->s_root
->d_inode
->i_ino
, 0);
151 ret
= btrfs_commit_transaction(trans
, root
);
155 new_root
= btrfs_read_fs_root(root
->fs_info
, &key
, name
, namelen
);
158 trans
= btrfs_start_transaction(new_root
, 1);
161 ret
= btrfs_create_subvol_root(new_root
, dentry
, trans
, new_dirid
,
162 BTRFS_I(dir
)->block_group
);
167 nr
= trans
->blocks_used
;
168 err
= btrfs_commit_transaction(trans
, new_root
);
172 btrfs_btree_balance_dirty(root
, nr
);
176 static int create_snapshot(struct btrfs_root
*root
, char *name
, int namelen
)
178 struct btrfs_pending_snapshot
*pending_snapshot
;
179 struct btrfs_trans_handle
*trans
;
182 unsigned long nr
= 0;
187 ret
= btrfs_check_free_space(root
, 1, 0);
191 pending_snapshot
= kmalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
192 if (!pending_snapshot
) {
196 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
197 if (!pending_snapshot
->name
) {
199 kfree(pending_snapshot
);
202 memcpy(pending_snapshot
->name
, name
, namelen
);
203 pending_snapshot
->name
[namelen
] = '\0';
204 trans
= btrfs_start_transaction(root
, 1);
206 pending_snapshot
->root
= root
;
207 list_add(&pending_snapshot
->list
,
208 &trans
->transaction
->pending_snapshots
);
209 ret
= btrfs_update_inode(trans
, root
, root
->inode
);
210 err
= btrfs_commit_transaction(trans
, root
);
213 btrfs_btree_balance_dirty(root
, nr
);
217 /* copy of may_create in fs/namei.c() */
218 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
224 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
228 * Create a new subvolume below @parent. This is largely modeled after
229 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
230 * inside this filesystem so it's quite a bit simpler.
232 static noinline
int btrfs_mksubvol(struct path
*parent
, char *name
,
233 int mode
, int namelen
)
235 struct dentry
*dentry
;
238 mutex_lock_nested(&parent
->dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
240 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
241 error
= PTR_ERR(dentry
);
249 if (!IS_POSIXACL(parent
->dentry
->d_inode
))
250 mode
&= ~current
->fs
->umask
;
251 error
= mnt_want_write(parent
->mnt
);
255 error
= btrfs_may_create(parent
->dentry
->d_inode
, dentry
);
260 * Actually perform the low-level subvolume creation after all
263 * Eventually we want to pass in an inode under which we create this
264 * subvolume, but for now all are under the filesystem root.
266 * Also we should pass on the mode eventually to allow creating new
267 * subvolume with specific mode bits.
269 error
= create_subvol(BTRFS_I(parent
->dentry
->d_inode
)->root
, dentry
,
274 fsnotify_mkdir(parent
->dentry
->d_inode
, dentry
);
276 mnt_drop_write(parent
->mnt
);
280 mutex_unlock(&parent
->dentry
->d_inode
->i_mutex
);
285 int btrfs_defrag_file(struct file
*file
)
287 struct inode
*inode
= fdentry(file
)->d_inode
;
288 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
289 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
290 struct btrfs_ordered_extent
*ordered
;
292 unsigned long last_index
;
293 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
294 unsigned long total_read
= 0;
300 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
304 mutex_lock(&inode
->i_mutex
);
305 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
306 for (i
= 0; i
<= last_index
; i
++) {
307 if (total_read
% ra_pages
== 0) {
308 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
309 min(last_index
, i
+ ra_pages
- 1));
313 page
= grab_cache_page(inode
->i_mapping
, i
);
316 if (!PageUptodate(page
)) {
317 btrfs_readpage(NULL
, page
);
319 if (!PageUptodate(page
)) {
321 page_cache_release(page
);
326 wait_on_page_writeback(page
);
328 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
329 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
330 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
332 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
334 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
336 page_cache_release(page
);
337 btrfs_start_ordered_extent(inode
, ordered
, 1);
338 btrfs_put_ordered_extent(ordered
);
341 set_page_extent_mapped(page
);
344 * this makes sure page_mkwrite is called on the
345 * page if it is dirtied again later
347 clear_page_dirty_for_io(page
);
349 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
351 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
352 set_page_dirty(page
);
354 page_cache_release(page
);
355 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
359 mutex_unlock(&inode
->i_mutex
);
364 * Called inside transaction, so use GFP_NOFS
367 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
372 struct btrfs_ioctl_vol_args
*vol_args
;
373 struct btrfs_trans_handle
*trans
;
374 struct btrfs_device
*device
= NULL
;
381 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
386 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
391 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
392 namelen
= strlen(vol_args
->name
);
394 mutex_lock(&root
->fs_info
->volume_mutex
);
395 sizestr
= vol_args
->name
;
396 devstr
= strchr(sizestr
, ':');
399 sizestr
= devstr
+ 1;
401 devstr
= vol_args
->name
;
402 devid
= simple_strtoull(devstr
, &end
, 10);
403 printk(KERN_INFO
"resizing devid %llu\n", devid
);
405 device
= btrfs_find_device(root
, devid
, NULL
);
407 printk(KERN_INFO
"resizer unable to find device %llu\n", devid
);
411 if (!strcmp(sizestr
, "max"))
412 new_size
= device
->bdev
->bd_inode
->i_size
;
414 if (sizestr
[0] == '-') {
417 } else if (sizestr
[0] == '+') {
421 new_size
= btrfs_parse_size(sizestr
);
428 old_size
= device
->total_bytes
;
431 if (new_size
> old_size
) {
435 new_size
= old_size
- new_size
;
436 } else if (mod
> 0) {
437 new_size
= old_size
+ new_size
;
440 if (new_size
< 256 * 1024 * 1024) {
444 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
449 do_div(new_size
, root
->sectorsize
);
450 new_size
*= root
->sectorsize
;
452 printk(KERN_INFO
"new size for %s is %llu\n",
453 device
->name
, (unsigned long long)new_size
);
455 if (new_size
> old_size
) {
456 trans
= btrfs_start_transaction(root
, 1);
457 ret
= btrfs_grow_device(trans
, device
, new_size
);
458 btrfs_commit_transaction(trans
, root
);
460 ret
= btrfs_shrink_device(device
, new_size
);
464 mutex_unlock(&root
->fs_info
->volume_mutex
);
470 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
473 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
474 struct btrfs_ioctl_vol_args
*vol_args
;
475 struct btrfs_dir_item
*di
;
476 struct btrfs_path
*path
;
481 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
486 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
491 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
492 namelen
= strlen(vol_args
->name
);
493 if (strchr(vol_args
->name
, '/')) {
498 path
= btrfs_alloc_path();
504 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
505 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
507 vol_args
->name
, namelen
, 0);
508 btrfs_free_path(path
);
510 if (di
&& !IS_ERR(di
)) {
520 if (root
== root
->fs_info
->tree_root
) {
521 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
522 file
->f_path
.dentry
->d_inode
->i_mode
,
525 ret
= create_snapshot(root
, vol_args
->name
, namelen
);
533 static int btrfs_ioctl_defrag(struct file
*file
)
535 struct inode
*inode
= fdentry(file
)->d_inode
;
536 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
538 switch (inode
->i_mode
& S_IFMT
) {
540 btrfs_defrag_root(root
, 0);
541 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
544 btrfs_defrag_file(file
);
551 long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
553 struct btrfs_ioctl_vol_args
*vol_args
;
556 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
561 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
565 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
566 ret
= btrfs_init_new_device(root
, vol_args
->name
);
573 long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
575 struct btrfs_ioctl_vol_args
*vol_args
;
578 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
583 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
587 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
588 ret
= btrfs_rm_device(root
, vol_args
->name
);
595 long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
, u64 off
,
596 u64 olen
, u64 destoff
)
598 struct inode
*inode
= fdentry(file
)->d_inode
;
599 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
600 struct file
*src_file
;
602 struct btrfs_trans_handle
*trans
;
603 struct btrfs_path
*path
;
604 struct extent_buffer
*leaf
;
606 struct btrfs_key key
;
611 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
616 * - split compressed inline extents. annoying: we need to
617 * decompress into destination's address_space (the file offset
618 * may change, so source mapping won't do), then recompress (or
619 * otherwise reinsert) a subrange.
620 * - allow ranges within the same file to be cloned (provided
621 * they don't overlap)?
624 src_file
= fget(srcfd
);
627 src
= src_file
->f_dentry
->d_inode
;
634 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
638 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
642 buf
= vmalloc(btrfs_level_size(root
, 0));
646 path
= btrfs_alloc_path();
654 mutex_lock(&inode
->i_mutex
);
655 mutex_lock(&src
->i_mutex
);
657 mutex_lock(&src
->i_mutex
);
658 mutex_lock(&inode
->i_mutex
);
661 /* determine range to clone */
663 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
666 olen
= len
= src
->i_size
- off
;
667 /* if we extend to eof, continue to block boundary */
668 if (off
+ len
== src
->i_size
)
669 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
672 /* verify the end result is block aligned */
673 if ((off
& (bs
-1)) ||
674 ((off
+ len
) & (bs
-1)))
677 printk("final src extent is %llu~%llu\n", off
, len
);
678 printk("final dst extent is %llu~%llu\n", destoff
, len
);
680 /* do any pending delalloc/csum calc on src, one way or
681 another, and lock file content */
683 struct btrfs_ordered_extent
*ordered
;
684 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
685 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
686 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
688 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
690 btrfs_put_ordered_extent(ordered
);
691 btrfs_wait_ordered_range(src
, off
, off
+len
);
694 trans
= btrfs_start_transaction(root
, 1);
697 /* punch hole in destination first */
698 btrfs_drop_extents(trans
, root
, inode
, off
, off
+len
, 0, &hint_byte
);
701 key
.objectid
= src
->i_ino
;
702 key
.type
= BTRFS_EXTENT_DATA_KEY
;
707 * note the key will change type as we walk through the
710 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
714 nritems
= btrfs_header_nritems(path
->nodes
[0]);
715 if (path
->slots
[0] >= nritems
) {
716 ret
= btrfs_next_leaf(root
, path
);
721 nritems
= btrfs_header_nritems(path
->nodes
[0]);
723 leaf
= path
->nodes
[0];
724 slot
= path
->slots
[0];
726 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
727 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
728 key
.objectid
!= src
->i_ino
)
731 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
732 struct btrfs_file_extent_item
*extent
;
735 struct btrfs_key new_key
;
736 u64 disko
= 0, diskl
= 0;
737 u64 datao
= 0, datal
= 0;
740 size
= btrfs_item_size_nr(leaf
, slot
);
741 read_extent_buffer(leaf
, buf
,
742 btrfs_item_ptr_offset(leaf
, slot
),
745 extent
= btrfs_item_ptr(leaf
, slot
,
746 struct btrfs_file_extent_item
);
747 comp
= btrfs_file_extent_compression(leaf
, extent
);
748 type
= btrfs_file_extent_type(leaf
, extent
);
749 if (type
== BTRFS_FILE_EXTENT_REG
) {
750 disko
= btrfs_file_extent_disk_bytenr(leaf
, extent
);
751 diskl
= btrfs_file_extent_disk_num_bytes(leaf
, extent
);
752 datao
= btrfs_file_extent_offset(leaf
, extent
);
753 datal
= btrfs_file_extent_num_bytes(leaf
, extent
);
754 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
755 /* take upper bound, may be compressed */
756 datal
= btrfs_file_extent_ram_bytes(leaf
,
759 btrfs_release_path(root
, path
);
761 if (key
.offset
+ datal
< off
||
762 key
.offset
>= off
+len
)
765 memcpy(&new_key
, &key
, sizeof(new_key
));
766 new_key
.objectid
= inode
->i_ino
;
767 new_key
.offset
= key
.offset
+ destoff
- off
;
769 if (type
== BTRFS_FILE_EXTENT_REG
) {
770 ret
= btrfs_insert_empty_item(trans
, root
, path
,
775 leaf
= path
->nodes
[0];
776 slot
= path
->slots
[0];
777 write_extent_buffer(leaf
, buf
,
778 btrfs_item_ptr_offset(leaf
, slot
),
781 extent
= btrfs_item_ptr(leaf
, slot
,
782 struct btrfs_file_extent_item
);
783 printk(" orig disk %llu~%llu data %llu~%llu\n",
784 disko
, diskl
, datao
, datal
);
786 if (off
> key
.offset
) {
787 datao
+= off
- key
.offset
;
788 datal
-= off
- key
.offset
;
790 if (key
.offset
+ datao
+ datal
+ key
.offset
>
792 datal
= off
+ len
- key
.offset
- datao
;
793 /* disko == 0 means it's a hole */
796 printk(" final disk %llu~%llu data %llu~%llu\n",
797 disko
, diskl
, datao
, datal
);
799 btrfs_set_file_extent_offset(leaf
, extent
,
801 btrfs_set_file_extent_num_bytes(leaf
, extent
,
804 inode_add_bytes(inode
, datal
);
805 ret
= btrfs_inc_extent_ref(trans
, root
,
806 disko
, diskl
, leaf
->start
,
807 root
->root_key
.objectid
,
812 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
815 if (off
> key
.offset
) {
816 skip
= off
- key
.offset
;
817 new_key
.offset
+= skip
;
819 if (key
.offset
+ datal
> off
+len
)
820 trim
= key
.offset
+ datal
- (off
+len
);
821 printk("len %lld skip %lld trim %lld\n",
823 if (comp
&& (skip
|| trim
)) {
824 printk("btrfs clone_range can't split compressed inline extents yet\n");
829 datal
-= skip
+ trim
;
830 ret
= btrfs_insert_empty_item(trans
, root
, path
,
836 u32 start
= btrfs_file_extent_calc_inline_size(0);
837 memmove(buf
+start
, buf
+start
+skip
,
841 leaf
= path
->nodes
[0];
842 slot
= path
->slots
[0];
843 write_extent_buffer(leaf
, buf
,
844 btrfs_item_ptr_offset(leaf
, slot
),
846 inode_add_bytes(inode
, datal
);
849 btrfs_mark_buffer_dirty(leaf
);
852 if (btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
854 struct btrfs_key new_key
;
858 size
= btrfs_item_size_nr(leaf
, slot
);
859 coverslen
= (size
/ BTRFS_CRC32_SIZE
) <<
860 root
->fs_info
->sb
->s_blocksize_bits
;
861 printk("csums for %llu~%llu\n",
862 key
.offset
, coverslen
);
863 if (key
.offset
+ coverslen
< off
||
864 key
.offset
>= off
+len
)
867 read_extent_buffer(leaf
, buf
,
868 btrfs_item_ptr_offset(leaf
, slot
),
870 btrfs_release_path(root
, path
);
873 if (off
> key
.offset
)
874 coff
= ((off
- key
.offset
) >>
875 root
->fs_info
->sb
->s_blocksize_bits
) *
878 if (key
.offset
+ coverslen
> off
+len
)
879 clen
-= ((key
.offset
+coverslen
-off
-len
) >>
880 root
->fs_info
->sb
->s_blocksize_bits
) *
882 printk(" will dup %d~%d of %d\n",
885 memcpy(&new_key
, &key
, sizeof(new_key
));
886 new_key
.objectid
= inode
->i_ino
;
887 new_key
.offset
= key
.offset
+ destoff
- off
;
889 ret
= btrfs_insert_empty_item(trans
, root
, path
,
894 leaf
= path
->nodes
[0];
895 slot
= path
->slots
[0];
896 write_extent_buffer(leaf
, buf
+ coff
,
897 btrfs_item_ptr_offset(leaf
, slot
),
899 btrfs_mark_buffer_dirty(leaf
);
903 btrfs_release_path(root
, path
);
908 btrfs_release_path(root
, path
);
910 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
911 if (destoff
+ olen
> inode
->i_size
)
912 btrfs_i_size_write(inode
, destoff
+ olen
);
913 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
914 ret
= btrfs_update_inode(trans
, root
, inode
);
916 btrfs_end_transaction(trans
, root
);
917 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
919 vmtruncate(inode
, 0);
921 mutex_unlock(&src
->i_mutex
);
922 mutex_unlock(&inode
->i_mutex
);
924 btrfs_free_path(path
);
930 long btrfs_ioctl_clone_range(struct file
*file
, unsigned long argptr
)
932 struct btrfs_ioctl_clone_range_args args
;
934 if (copy_from_user(&args
, (void *)argptr
, sizeof(args
)))
936 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
937 args
.src_length
, args
.dest_offset
);
941 * there are many ways the trans_start and trans_end ioctls can lead
942 * to deadlocks. They should only be used by applications that
943 * basically own the machine, and have a very in depth understanding
944 * of all the possible deadlocks and enospc problems.
946 long btrfs_ioctl_trans_start(struct file
*file
)
948 struct inode
*inode
= fdentry(file
)->d_inode
;
949 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
950 struct btrfs_trans_handle
*trans
;
953 if (!capable(CAP_SYS_ADMIN
))
956 if (file
->private_data
) {
961 mutex_lock(&root
->fs_info
->trans_mutex
);
962 root
->fs_info
->open_ioctl_trans
++;
963 mutex_unlock(&root
->fs_info
->trans_mutex
);
965 trans
= btrfs_start_ioctl_transaction(root
, 0);
967 file
->private_data
= trans
;
970 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
976 * there are many ways the trans_start and trans_end ioctls can lead
977 * to deadlocks. They should only be used by applications that
978 * basically own the machine, and have a very in depth understanding
979 * of all the possible deadlocks and enospc problems.
981 long btrfs_ioctl_trans_end(struct file
*file
)
983 struct inode
*inode
= fdentry(file
)->d_inode
;
984 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
985 struct btrfs_trans_handle
*trans
;
988 trans
= file
->private_data
;
993 btrfs_end_transaction(trans
, root
);
994 file
->private_data
= NULL
;
996 mutex_lock(&root
->fs_info
->trans_mutex
);
997 root
->fs_info
->open_ioctl_trans
--;
998 mutex_unlock(&root
->fs_info
->trans_mutex
);
1004 long btrfs_ioctl(struct file
*file
, unsigned int
1005 cmd
, unsigned long arg
)
1007 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1010 case BTRFS_IOC_SNAP_CREATE
:
1011 return btrfs_ioctl_snap_create(file
, (void __user
*)arg
);
1012 case BTRFS_IOC_DEFRAG
:
1013 return btrfs_ioctl_defrag(file
);
1014 case BTRFS_IOC_RESIZE
:
1015 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
1016 case BTRFS_IOC_ADD_DEV
:
1017 return btrfs_ioctl_add_dev(root
, (void __user
*)arg
);
1018 case BTRFS_IOC_RM_DEV
:
1019 return btrfs_ioctl_rm_dev(root
, (void __user
*)arg
);
1020 case BTRFS_IOC_BALANCE
:
1021 return btrfs_balance(root
->fs_info
->dev_root
);
1022 case BTRFS_IOC_CLONE
:
1023 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
1024 case BTRFS_IOC_CLONE_RANGE
:
1025 return btrfs_ioctl_clone_range(file
, arg
);
1026 case BTRFS_IOC_TRANS_START
:
1027 return btrfs_ioctl_trans_start(file
);
1028 case BTRFS_IOC_TRANS_END
:
1029 return btrfs_ioctl_trans_end(file
);
1030 case BTRFS_IOC_SYNC
:
1031 btrfs_start_delalloc_inodes(root
);
1032 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);