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
;
73 ret
= btrfs_check_free_space(root
, 1, 0);
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 /* add the backref first */
150 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
151 objectid
, BTRFS_ROOT_BACKREF_KEY
,
152 root
->root_key
.objectid
,
153 dir
->i_ino
, index
, name
, namelen
);
157 /* now add the forward ref */
158 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
159 root
->root_key
.objectid
, BTRFS_ROOT_REF_KEY
,
161 dir
->i_ino
, index
, name
, namelen
);
165 ret
= btrfs_commit_transaction(trans
, root
);
169 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
172 trans
= btrfs_start_transaction(new_root
, 1);
175 ret
= btrfs_create_subvol_root(new_root
, dentry
, trans
, new_dirid
,
176 BTRFS_I(dir
)->block_group
);
181 nr
= trans
->blocks_used
;
182 err
= btrfs_commit_transaction(trans
, new_root
);
186 btrfs_btree_balance_dirty(root
, nr
);
190 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
191 char *name
, int namelen
)
193 struct btrfs_pending_snapshot
*pending_snapshot
;
194 struct btrfs_trans_handle
*trans
;
197 unsigned long nr
= 0;
202 ret
= btrfs_check_free_space(root
, 1, 0);
206 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
207 if (!pending_snapshot
) {
211 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
212 if (!pending_snapshot
->name
) {
214 kfree(pending_snapshot
);
217 memcpy(pending_snapshot
->name
, name
, namelen
);
218 pending_snapshot
->name
[namelen
] = '\0';
219 pending_snapshot
->dentry
= dentry
;
220 trans
= btrfs_start_transaction(root
, 1);
222 pending_snapshot
->root
= root
;
223 list_add(&pending_snapshot
->list
,
224 &trans
->transaction
->pending_snapshots
);
225 err
= btrfs_commit_transaction(trans
, root
);
228 btrfs_btree_balance_dirty(root
, nr
);
232 /* copy of may_create in fs/namei.c() */
233 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
239 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
243 * Create a new subvolume below @parent. This is largely modeled after
244 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
245 * inside this filesystem so it's quite a bit simpler.
247 static noinline
int btrfs_mksubvol(struct path
*parent
, char *name
,
248 int mode
, int namelen
,
249 struct btrfs_root
*snap_src
)
251 struct dentry
*dentry
;
254 mutex_lock_nested(&parent
->dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
256 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
257 error
= PTR_ERR(dentry
);
265 if (!IS_POSIXACL(parent
->dentry
->d_inode
))
266 mode
&= ~current
->fs
->umask
;
268 error
= mnt_want_write(parent
->mnt
);
272 error
= btrfs_may_create(parent
->dentry
->d_inode
, dentry
);
277 * Actually perform the low-level subvolume creation after all
280 * Eventually we want to pass in an inode under which we create this
281 * subvolume, but for now all are under the filesystem root.
283 * Also we should pass on the mode eventually to allow creating new
284 * subvolume with specific mode bits.
287 error
= create_snapshot(snap_src
, dentry
, name
, namelen
);
289 error
= create_subvol(BTRFS_I(parent
->dentry
->d_inode
)->root
,
290 dentry
, name
, namelen
);
295 fsnotify_mkdir(parent
->dentry
->d_inode
, dentry
);
297 mnt_drop_write(parent
->mnt
);
301 mutex_unlock(&parent
->dentry
->d_inode
->i_mutex
);
306 int btrfs_defrag_file(struct file
*file
)
308 struct inode
*inode
= fdentry(file
)->d_inode
;
309 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
310 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
311 struct btrfs_ordered_extent
*ordered
;
313 unsigned long last_index
;
314 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
315 unsigned long total_read
= 0;
321 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
325 mutex_lock(&inode
->i_mutex
);
326 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
327 for (i
= 0; i
<= last_index
; i
++) {
328 if (total_read
% ra_pages
== 0) {
329 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
330 min(last_index
, i
+ ra_pages
- 1));
334 page
= grab_cache_page(inode
->i_mapping
, i
);
337 if (!PageUptodate(page
)) {
338 btrfs_readpage(NULL
, page
);
340 if (!PageUptodate(page
)) {
342 page_cache_release(page
);
347 wait_on_page_writeback(page
);
349 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
350 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
351 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
353 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
355 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
357 page_cache_release(page
);
358 btrfs_start_ordered_extent(inode
, ordered
, 1);
359 btrfs_put_ordered_extent(ordered
);
362 set_page_extent_mapped(page
);
365 * this makes sure page_mkwrite is called on the
366 * page if it is dirtied again later
368 clear_page_dirty_for_io(page
);
370 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
372 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
373 set_page_dirty(page
);
375 page_cache_release(page
);
376 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
380 mutex_unlock(&inode
->i_mutex
);
385 * Called inside transaction, so use GFP_NOFS
388 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
393 struct btrfs_ioctl_vol_args
*vol_args
;
394 struct btrfs_trans_handle
*trans
;
395 struct btrfs_device
*device
= NULL
;
402 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
405 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
410 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
415 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
416 namelen
= strlen(vol_args
->name
);
418 mutex_lock(&root
->fs_info
->volume_mutex
);
419 sizestr
= vol_args
->name
;
420 devstr
= strchr(sizestr
, ':');
423 sizestr
= devstr
+ 1;
425 devstr
= vol_args
->name
;
426 devid
= simple_strtoull(devstr
, &end
, 10);
427 printk(KERN_INFO
"resizing devid %llu\n", devid
);
429 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
431 printk(KERN_INFO
"resizer unable to find device %llu\n", devid
);
435 if (!strcmp(sizestr
, "max"))
436 new_size
= device
->bdev
->bd_inode
->i_size
;
438 if (sizestr
[0] == '-') {
441 } else if (sizestr
[0] == '+') {
445 new_size
= btrfs_parse_size(sizestr
);
452 old_size
= device
->total_bytes
;
455 if (new_size
> old_size
) {
459 new_size
= old_size
- new_size
;
460 } else if (mod
> 0) {
461 new_size
= old_size
+ new_size
;
464 if (new_size
< 256 * 1024 * 1024) {
468 if (new_size
> device
->bdev
->bd_inode
->i_size
) {
473 do_div(new_size
, root
->sectorsize
);
474 new_size
*= root
->sectorsize
;
476 printk(KERN_INFO
"new size for %s is %llu\n",
477 device
->name
, (unsigned long long)new_size
);
479 if (new_size
> old_size
) {
480 trans
= btrfs_start_transaction(root
, 1);
481 ret
= btrfs_grow_device(trans
, device
, new_size
);
482 btrfs_commit_transaction(trans
, root
);
484 ret
= btrfs_shrink_device(device
, new_size
);
488 mutex_unlock(&root
->fs_info
->volume_mutex
);
494 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
495 void __user
*arg
, int subvol
)
497 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
498 struct btrfs_ioctl_vol_args
*vol_args
;
499 struct btrfs_dir_item
*di
;
500 struct btrfs_path
*path
;
501 struct file
*src_file
;
506 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
509 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
514 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
519 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
520 namelen
= strlen(vol_args
->name
);
521 if (strchr(vol_args
->name
, '/')) {
526 path
= btrfs_alloc_path();
532 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
533 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
535 vol_args
->name
, namelen
, 0);
536 btrfs_free_path(path
);
538 if (di
&& !IS_ERR(di
)) {
549 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
550 file
->f_path
.dentry
->d_inode
->i_mode
,
553 struct inode
*src_inode
;
554 src_file
= fget(vol_args
->fd
);
560 src_inode
= src_file
->f_path
.dentry
->d_inode
;
561 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
562 printk("btrfs: Snapshot src from another FS\n");
567 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
568 file
->f_path
.dentry
->d_inode
->i_mode
,
569 namelen
, BTRFS_I(src_inode
)->root
);
578 static int btrfs_ioctl_defrag(struct file
*file
)
580 struct inode
*inode
= fdentry(file
)->d_inode
;
581 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
584 ret
= mnt_want_write(file
->f_path
.mnt
);
588 switch (inode
->i_mode
& S_IFMT
) {
590 btrfs_defrag_root(root
, 0);
591 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
594 btrfs_defrag_file(file
);
601 long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
603 struct btrfs_ioctl_vol_args
*vol_args
;
606 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
611 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
615 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
616 ret
= btrfs_init_new_device(root
, vol_args
->name
);
623 long btrfs_ioctl_rm_dev(struct btrfs_root
*root
, void __user
*arg
)
625 struct btrfs_ioctl_vol_args
*vol_args
;
628 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
631 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
636 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
640 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
641 ret
= btrfs_rm_device(root
, vol_args
->name
);
648 long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
, u64 off
,
649 u64 olen
, u64 destoff
)
651 struct inode
*inode
= fdentry(file
)->d_inode
;
652 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
653 struct file
*src_file
;
655 struct btrfs_trans_handle
*trans
;
656 struct btrfs_path
*path
;
657 struct extent_buffer
*leaf
;
659 struct btrfs_key key
;
664 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
669 * - split compressed inline extents. annoying: we need to
670 * decompress into destination's address_space (the file offset
671 * may change, so source mapping won't do), then recompress (or
672 * otherwise reinsert) a subrange.
673 * - allow ranges within the same file to be cloned (provided
674 * they don't overlap)?
677 ret
= mnt_want_write(file
->f_path
.mnt
);
681 src_file
= fget(srcfd
);
684 src
= src_file
->f_dentry
->d_inode
;
691 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
695 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
699 buf
= vmalloc(btrfs_level_size(root
, 0));
703 path
= btrfs_alloc_path();
711 mutex_lock(&inode
->i_mutex
);
712 mutex_lock(&src
->i_mutex
);
714 mutex_lock(&src
->i_mutex
);
715 mutex_lock(&inode
->i_mutex
);
718 /* determine range to clone */
720 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
723 olen
= len
= src
->i_size
- off
;
724 /* if we extend to eof, continue to block boundary */
725 if (off
+ len
== src
->i_size
)
726 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
729 /* verify the end result is block aligned */
730 if ((off
& (bs
-1)) ||
731 ((off
+ len
) & (bs
-1)))
734 printk("final src extent is %llu~%llu\n", off
, len
);
735 printk("final dst extent is %llu~%llu\n", destoff
, len
);
737 /* do any pending delalloc/csum calc on src, one way or
738 another, and lock file content */
740 struct btrfs_ordered_extent
*ordered
;
741 lock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
742 ordered
= btrfs_lookup_first_ordered_extent(inode
, off
+len
);
743 if (BTRFS_I(src
)->delalloc_bytes
== 0 && !ordered
)
745 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
747 btrfs_put_ordered_extent(ordered
);
748 btrfs_wait_ordered_range(src
, off
, off
+len
);
751 trans
= btrfs_start_transaction(root
, 1);
754 /* punch hole in destination first */
755 btrfs_drop_extents(trans
, root
, inode
, off
, off
+len
, 0, &hint_byte
);
758 key
.objectid
= src
->i_ino
;
759 key
.type
= BTRFS_EXTENT_DATA_KEY
;
764 * note the key will change type as we walk through the
767 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
771 nritems
= btrfs_header_nritems(path
->nodes
[0]);
772 if (path
->slots
[0] >= nritems
) {
773 ret
= btrfs_next_leaf(root
, path
);
778 nritems
= btrfs_header_nritems(path
->nodes
[0]);
780 leaf
= path
->nodes
[0];
781 slot
= path
->slots
[0];
783 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
784 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
785 key
.objectid
!= src
->i_ino
)
788 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
789 struct btrfs_file_extent_item
*extent
;
792 struct btrfs_key new_key
;
793 u64 disko
= 0, diskl
= 0;
794 u64 datao
= 0, datal
= 0;
797 size
= btrfs_item_size_nr(leaf
, slot
);
798 read_extent_buffer(leaf
, buf
,
799 btrfs_item_ptr_offset(leaf
, slot
),
802 extent
= btrfs_item_ptr(leaf
, slot
,
803 struct btrfs_file_extent_item
);
804 comp
= btrfs_file_extent_compression(leaf
, extent
);
805 type
= btrfs_file_extent_type(leaf
, extent
);
806 if (type
== BTRFS_FILE_EXTENT_REG
) {
807 disko
= btrfs_file_extent_disk_bytenr(leaf
, extent
);
808 diskl
= btrfs_file_extent_disk_num_bytes(leaf
, extent
);
809 datao
= btrfs_file_extent_offset(leaf
, extent
);
810 datal
= btrfs_file_extent_num_bytes(leaf
, extent
);
811 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
812 /* take upper bound, may be compressed */
813 datal
= btrfs_file_extent_ram_bytes(leaf
,
816 btrfs_release_path(root
, path
);
818 if (key
.offset
+ datal
< off
||
819 key
.offset
>= off
+len
)
822 memcpy(&new_key
, &key
, sizeof(new_key
));
823 new_key
.objectid
= inode
->i_ino
;
824 new_key
.offset
= key
.offset
+ destoff
- off
;
826 if (type
== BTRFS_FILE_EXTENT_REG
) {
827 ret
= btrfs_insert_empty_item(trans
, root
, path
,
832 leaf
= path
->nodes
[0];
833 slot
= path
->slots
[0];
834 write_extent_buffer(leaf
, buf
,
835 btrfs_item_ptr_offset(leaf
, slot
),
838 extent
= btrfs_item_ptr(leaf
, slot
,
839 struct btrfs_file_extent_item
);
840 printk(" orig disk %llu~%llu data %llu~%llu\n",
841 disko
, diskl
, datao
, datal
);
843 if (off
> key
.offset
) {
844 datao
+= off
- key
.offset
;
845 datal
-= off
- key
.offset
;
847 if (key
.offset
+ datao
+ datal
+ key
.offset
>
849 datal
= off
+ len
- key
.offset
- datao
;
850 /* disko == 0 means it's a hole */
853 printk(" final disk %llu~%llu data %llu~%llu\n",
854 disko
, diskl
, datao
, datal
);
856 btrfs_set_file_extent_offset(leaf
, extent
,
858 btrfs_set_file_extent_num_bytes(leaf
, extent
,
861 inode_add_bytes(inode
, datal
);
862 ret
= btrfs_inc_extent_ref(trans
, root
,
863 disko
, diskl
, leaf
->start
,
864 root
->root_key
.objectid
,
869 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
872 if (off
> key
.offset
) {
873 skip
= off
- key
.offset
;
874 new_key
.offset
+= skip
;
876 if (key
.offset
+ datal
> off
+len
)
877 trim
= key
.offset
+ datal
- (off
+len
);
878 printk("len %lld skip %lld trim %lld\n",
880 if (comp
&& (skip
|| trim
)) {
881 printk("btrfs clone_range can't split compressed inline extents yet\n");
886 datal
-= skip
+ trim
;
887 ret
= btrfs_insert_empty_item(trans
, root
, path
,
893 u32 start
= btrfs_file_extent_calc_inline_size(0);
894 memmove(buf
+start
, buf
+start
+skip
,
898 leaf
= path
->nodes
[0];
899 slot
= path
->slots
[0];
900 write_extent_buffer(leaf
, buf
,
901 btrfs_item_ptr_offset(leaf
, slot
),
903 inode_add_bytes(inode
, datal
);
906 btrfs_mark_buffer_dirty(leaf
);
909 if (btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
911 struct btrfs_key new_key
;
915 size
= btrfs_item_size_nr(leaf
, slot
);
916 coverslen
= (size
/ BTRFS_CRC32_SIZE
) <<
917 root
->fs_info
->sb
->s_blocksize_bits
;
918 printk("csums for %llu~%llu\n",
919 key
.offset
, coverslen
);
920 if (key
.offset
+ coverslen
< off
||
921 key
.offset
>= off
+len
)
924 read_extent_buffer(leaf
, buf
,
925 btrfs_item_ptr_offset(leaf
, slot
),
927 btrfs_release_path(root
, path
);
930 if (off
> key
.offset
)
931 coff
= ((off
- key
.offset
) >>
932 root
->fs_info
->sb
->s_blocksize_bits
) *
935 if (key
.offset
+ coverslen
> off
+len
)
936 clen
-= ((key
.offset
+coverslen
-off
-len
) >>
937 root
->fs_info
->sb
->s_blocksize_bits
) *
939 printk(" will dup %d~%d of %d\n",
942 memcpy(&new_key
, &key
, sizeof(new_key
));
943 new_key
.objectid
= inode
->i_ino
;
944 new_key
.offset
= key
.offset
+ destoff
- off
;
946 ret
= btrfs_insert_empty_item(trans
, root
, path
,
951 leaf
= path
->nodes
[0];
952 slot
= path
->slots
[0];
953 write_extent_buffer(leaf
, buf
+ coff
,
954 btrfs_item_ptr_offset(leaf
, slot
),
956 btrfs_mark_buffer_dirty(leaf
);
960 btrfs_release_path(root
, path
);
965 btrfs_release_path(root
, path
);
967 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
968 if (destoff
+ olen
> inode
->i_size
)
969 btrfs_i_size_write(inode
, destoff
+ olen
);
970 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
971 ret
= btrfs_update_inode(trans
, root
, inode
);
973 btrfs_end_transaction(trans
, root
);
974 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
976 vmtruncate(inode
, 0);
978 mutex_unlock(&src
->i_mutex
);
979 mutex_unlock(&inode
->i_mutex
);
981 btrfs_free_path(path
);
987 long btrfs_ioctl_clone_range(struct file
*file
, unsigned long argptr
)
989 struct btrfs_ioctl_clone_range_args args
;
991 if (copy_from_user(&args
, (void *)argptr
, sizeof(args
)))
993 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
994 args
.src_length
, args
.dest_offset
);
998 * there are many ways the trans_start and trans_end ioctls can lead
999 * to deadlocks. They should only be used by applications that
1000 * basically own the machine, and have a very in depth understanding
1001 * of all the possible deadlocks and enospc problems.
1003 long btrfs_ioctl_trans_start(struct file
*file
)
1005 struct inode
*inode
= fdentry(file
)->d_inode
;
1006 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1007 struct btrfs_trans_handle
*trans
;
1010 if (!capable(CAP_SYS_ADMIN
))
1013 if (file
->private_data
) {
1018 ret
= mnt_want_write(file
->f_path
.mnt
);
1022 mutex_lock(&root
->fs_info
->trans_mutex
);
1023 root
->fs_info
->open_ioctl_trans
++;
1024 mutex_unlock(&root
->fs_info
->trans_mutex
);
1026 trans
= btrfs_start_ioctl_transaction(root
, 0);
1028 file
->private_data
= trans
;
1031 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1037 * there are many ways the trans_start and trans_end ioctls can lead
1038 * to deadlocks. They should only be used by applications that
1039 * basically own the machine, and have a very in depth understanding
1040 * of all the possible deadlocks and enospc problems.
1042 long btrfs_ioctl_trans_end(struct file
*file
)
1044 struct inode
*inode
= fdentry(file
)->d_inode
;
1045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1046 struct btrfs_trans_handle
*trans
;
1049 trans
= file
->private_data
;
1054 btrfs_end_transaction(trans
, root
);
1055 file
->private_data
= NULL
;
1057 mutex_lock(&root
->fs_info
->trans_mutex
);
1058 root
->fs_info
->open_ioctl_trans
--;
1059 mutex_unlock(&root
->fs_info
->trans_mutex
);
1065 long btrfs_ioctl(struct file
*file
, unsigned int
1066 cmd
, unsigned long arg
)
1068 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1071 case BTRFS_IOC_SNAP_CREATE
:
1072 return btrfs_ioctl_snap_create(file
, (void __user
*)arg
, 0);
1073 case BTRFS_IOC_SUBVOL_CREATE
:
1074 return btrfs_ioctl_snap_create(file
, (void __user
*)arg
, 1);
1075 case BTRFS_IOC_DEFRAG
:
1076 return btrfs_ioctl_defrag(file
);
1077 case BTRFS_IOC_RESIZE
:
1078 return btrfs_ioctl_resize(root
, (void __user
*)arg
);
1079 case BTRFS_IOC_ADD_DEV
:
1080 return btrfs_ioctl_add_dev(root
, (void __user
*)arg
);
1081 case BTRFS_IOC_RM_DEV
:
1082 return btrfs_ioctl_rm_dev(root
, (void __user
*)arg
);
1083 case BTRFS_IOC_BALANCE
:
1084 return btrfs_balance(root
->fs_info
->dev_root
);
1085 case BTRFS_IOC_CLONE
:
1086 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
1087 case BTRFS_IOC_CLONE_RANGE
:
1088 return btrfs_ioctl_clone_range(file
, arg
);
1089 case BTRFS_IOC_TRANS_START
:
1090 return btrfs_ioctl_trans_start(file
);
1091 case BTRFS_IOC_TRANS_END
:
1092 return btrfs_ioctl_trans_end(file
);
1093 case BTRFS_IOC_SYNC
:
1094 btrfs_start_delalloc_inodes(root
);
1095 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);