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>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
50 #include "print-tree.h"
56 static noinline
int create_subvol(struct btrfs_root
*root
,
57 struct dentry
*dentry
,
58 char *name
, int namelen
)
60 struct btrfs_trans_handle
*trans
;
62 struct btrfs_root_item root_item
;
63 struct btrfs_inode_item
*inode_item
;
64 struct extent_buffer
*leaf
;
65 struct btrfs_root
*new_root
= root
;
70 u64 new_dirid
= BTRFS_FIRST_FREE_OBJECTID
;
74 ret
= btrfs_check_free_space(root
, 1, 0);
78 trans
= btrfs_start_transaction(root
, 1);
81 ret
= btrfs_find_free_objectid(trans
, root
->fs_info
->tree_root
,
86 leaf
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
87 objectid
, trans
->transid
, 0, 0, 0);
93 btrfs_set_header_nritems(leaf
, 0);
94 btrfs_set_header_level(leaf
, 0);
95 btrfs_set_header_bytenr(leaf
, leaf
->start
);
96 btrfs_set_header_generation(leaf
, trans
->transid
);
97 btrfs_set_header_owner(leaf
, objectid
);
99 write_extent_buffer(leaf
, root
->fs_info
->fsid
,
100 (unsigned long)btrfs_header_fsid(leaf
),
102 btrfs_mark_buffer_dirty(leaf
);
104 inode_item
= &root_item
.inode
;
105 memset(inode_item
, 0, sizeof(*inode_item
));
106 inode_item
->generation
= cpu_to_le64(1);
107 inode_item
->size
= cpu_to_le64(3);
108 inode_item
->nlink
= cpu_to_le32(1);
109 inode_item
->nbytes
= cpu_to_le64(root
->leafsize
);
110 inode_item
->mode
= cpu_to_le32(S_IFDIR
| 0755);
112 btrfs_set_root_bytenr(&root_item
, leaf
->start
);
113 btrfs_set_root_generation(&root_item
, trans
->transid
);
114 btrfs_set_root_level(&root_item
, 0);
115 btrfs_set_root_refs(&root_item
, 1);
116 btrfs_set_root_used(&root_item
, 0);
117 btrfs_set_root_last_snapshot(&root_item
, 0);
119 memset(&root_item
.drop_progress
, 0, sizeof(root_item
.drop_progress
));
120 root_item
.drop_level
= 0;
122 btrfs_tree_unlock(leaf
);
123 free_extent_buffer(leaf
);
126 btrfs_set_root_dirid(&root_item
, new_dirid
);
128 key
.objectid
= objectid
;
130 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
131 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
, &key
,
137 * insert the directory item
139 key
.offset
= (u64
)-1;
140 dir
= dentry
->d_parent
->d_inode
;
141 ret
= btrfs_set_inode_index(dir
, &index
);
144 ret
= btrfs_insert_dir_item(trans
, root
,
145 name
, namelen
, dir
->i_ino
, &key
,
146 BTRFS_FT_DIR
, index
);
150 /* add the backref first */
151 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
152 objectid
, BTRFS_ROOT_BACKREF_KEY
,
153 root
->root_key
.objectid
,
154 dir
->i_ino
, index
, name
, namelen
);
158 /* now add the forward ref */
159 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
160 root
->root_key
.objectid
, BTRFS_ROOT_REF_KEY
,
162 dir
->i_ino
, index
, name
, namelen
);
166 ret
= btrfs_commit_transaction(trans
, root
);
170 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, &key
);
173 trans
= btrfs_start_transaction(new_root
, 1);
176 ret
= btrfs_create_subvol_root(new_root
, dentry
, trans
, new_dirid
,
177 BTRFS_I(dir
)->block_group
);
182 nr
= trans
->blocks_used
;
183 err
= btrfs_commit_transaction(trans
, new_root
);
187 btrfs_btree_balance_dirty(root
, nr
);
191 static int create_snapshot(struct btrfs_root
*root
, struct dentry
*dentry
,
192 char *name
, int namelen
)
194 struct btrfs_pending_snapshot
*pending_snapshot
;
195 struct btrfs_trans_handle
*trans
;
198 unsigned long nr
= 0;
203 ret
= btrfs_check_free_space(root
, 1, 0);
207 pending_snapshot
= kzalloc(sizeof(*pending_snapshot
), GFP_NOFS
);
208 if (!pending_snapshot
) {
212 pending_snapshot
->name
= kmalloc(namelen
+ 1, GFP_NOFS
);
213 if (!pending_snapshot
->name
) {
215 kfree(pending_snapshot
);
218 memcpy(pending_snapshot
->name
, name
, namelen
);
219 pending_snapshot
->name
[namelen
] = '\0';
220 pending_snapshot
->dentry
= dentry
;
221 trans
= btrfs_start_transaction(root
, 1);
223 pending_snapshot
->root
= root
;
224 list_add(&pending_snapshot
->list
,
225 &trans
->transaction
->pending_snapshots
);
226 err
= btrfs_commit_transaction(trans
, root
);
229 btrfs_btree_balance_dirty(root
, nr
);
233 /* copy of may_create in fs/namei.c() */
234 static inline int btrfs_may_create(struct inode
*dir
, struct dentry
*child
)
240 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
244 * Create a new subvolume below @parent. This is largely modeled after
245 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
246 * inside this filesystem so it's quite a bit simpler.
248 static noinline
int btrfs_mksubvol(struct path
*parent
, char *name
,
249 int mode
, int namelen
,
250 struct btrfs_root
*snap_src
)
252 struct dentry
*dentry
;
255 mutex_lock_nested(&parent
->dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
257 dentry
= lookup_one_len(name
, parent
->dentry
, namelen
);
258 error
= PTR_ERR(dentry
);
266 if (!IS_POSIXACL(parent
->dentry
->d_inode
))
267 mode
&= ~current
->fs
->umask
;
269 error
= mnt_want_write(parent
->mnt
);
273 error
= btrfs_may_create(parent
->dentry
->d_inode
, dentry
);
278 * Actually perform the low-level subvolume creation after all
281 * Eventually we want to pass in an inode under which we create this
282 * subvolume, but for now all are under the filesystem root.
284 * Also we should pass on the mode eventually to allow creating new
285 * subvolume with specific mode bits.
288 struct dentry
*dir
= dentry
->d_parent
;
289 struct dentry
*test
= dir
->d_parent
;
290 struct btrfs_path
*path
= btrfs_alloc_path();
293 u64 parent_oid
= BTRFS_I(dir
->d_inode
)->root
->root_key
.objectid
;
295 test_oid
= snap_src
->root_key
.objectid
;
297 ret
= btrfs_find_root_ref(snap_src
->fs_info
->tree_root
,
298 path
, parent_oid
, test_oid
);
301 btrfs_release_path(snap_src
->fs_info
->tree_root
, path
);
303 /* we need to make sure we aren't creating a directory loop
304 * by taking a snapshot of something that has our current
305 * subvol in its directory tree. So, this loops through
306 * the dentries and checks the forward refs for each subvolume
307 * to see if is references the subvolume where we are
308 * placing this new snapshot.
312 dir
== snap_src
->fs_info
->sb
->s_root
||
313 test
== snap_src
->fs_info
->sb
->s_root
||
314 test
->d_inode
->i_sb
!= snap_src
->fs_info
->sb
) {
317 if (S_ISLNK(test
->d_inode
->i_mode
)) {
318 printk("Symlink in snapshot path, failed\n");
320 btrfs_free_path(path
);
324 BTRFS_I(test
->d_inode
)->root
->root_key
.objectid
;
325 ret
= btrfs_find_root_ref(snap_src
->fs_info
->tree_root
,
326 path
, test_oid
, parent_oid
);
328 printk("Snapshot creation failed, looping\n");
330 btrfs_free_path(path
);
333 btrfs_release_path(snap_src
->fs_info
->tree_root
, path
);
334 test
= test
->d_parent
;
337 btrfs_free_path(path
);
338 error
= create_snapshot(snap_src
, dentry
, name
, namelen
);
340 error
= create_subvol(BTRFS_I(parent
->dentry
->d_inode
)->root
,
341 dentry
, name
, namelen
);
346 fsnotify_mkdir(parent
->dentry
->d_inode
, dentry
);
348 mnt_drop_write(parent
->mnt
);
352 mutex_unlock(&parent
->dentry
->d_inode
->i_mutex
);
357 static int btrfs_defrag_file(struct file
*file
)
359 struct inode
*inode
= fdentry(file
)->d_inode
;
360 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
361 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
362 struct btrfs_ordered_extent
*ordered
;
364 unsigned long last_index
;
365 unsigned long ra_pages
= root
->fs_info
->bdi
.ra_pages
;
366 unsigned long total_read
= 0;
372 ret
= btrfs_check_free_space(root
, inode
->i_size
, 0);
376 mutex_lock(&inode
->i_mutex
);
377 last_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
378 for (i
= 0; i
<= last_index
; i
++) {
379 if (total_read
% ra_pages
== 0) {
380 btrfs_force_ra(inode
->i_mapping
, &file
->f_ra
, file
, i
,
381 min(last_index
, i
+ ra_pages
- 1));
385 page
= grab_cache_page(inode
->i_mapping
, i
);
388 if (!PageUptodate(page
)) {
389 btrfs_readpage(NULL
, page
);
391 if (!PageUptodate(page
)) {
393 page_cache_release(page
);
398 wait_on_page_writeback(page
);
400 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
401 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
402 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
404 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
406 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
408 page_cache_release(page
);
409 btrfs_start_ordered_extent(inode
, ordered
, 1);
410 btrfs_put_ordered_extent(ordered
);
413 set_page_extent_mapped(page
);
416 * this makes sure page_mkwrite is called on the
417 * page if it is dirtied again later
419 clear_page_dirty_for_io(page
);
421 btrfs_set_extent_delalloc(inode
, page_start
, page_end
);
423 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
424 set_page_dirty(page
);
426 page_cache_release(page
);
427 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
431 mutex_unlock(&inode
->i_mutex
);
436 * Called inside transaction, so use GFP_NOFS
439 static int btrfs_ioctl_resize(struct btrfs_root
*root
, void __user
*arg
)
444 struct btrfs_ioctl_vol_args
*vol_args
;
445 struct btrfs_trans_handle
*trans
;
446 struct btrfs_device
*device
= NULL
;
453 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
456 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
461 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
466 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
467 namelen
= strlen(vol_args
->name
);
469 mutex_lock(&root
->fs_info
->volume_mutex
);
470 sizestr
= vol_args
->name
;
471 devstr
= strchr(sizestr
, ':');
474 sizestr
= devstr
+ 1;
476 devstr
= vol_args
->name
;
477 devid
= simple_strtoull(devstr
, &end
, 10);
478 printk(KERN_INFO
"resizing devid %llu\n", devid
);
480 device
= btrfs_find_device(root
, devid
, NULL
, NULL
);
482 printk(KERN_INFO
"resizer unable to find device %llu\n", 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
);
545 static noinline
int btrfs_ioctl_snap_create(struct file
*file
,
546 void __user
*arg
, int subvol
)
548 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
549 struct btrfs_ioctl_vol_args
*vol_args
;
550 struct btrfs_dir_item
*di
;
551 struct btrfs_path
*path
;
552 struct file
*src_file
;
557 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
560 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
565 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
570 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
571 namelen
= strlen(vol_args
->name
);
572 if (strchr(vol_args
->name
, '/')) {
577 path
= btrfs_alloc_path();
583 root_dirid
= root
->fs_info
->sb
->s_root
->d_inode
->i_ino
,
584 di
= btrfs_lookup_dir_item(NULL
, root
->fs_info
->tree_root
,
586 vol_args
->name
, namelen
, 0);
587 btrfs_free_path(path
);
589 if (di
&& !IS_ERR(di
)) {
600 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
601 file
->f_path
.dentry
->d_inode
->i_mode
,
604 struct inode
*src_inode
;
605 src_file
= fget(vol_args
->fd
);
611 src_inode
= src_file
->f_path
.dentry
->d_inode
;
612 if (src_inode
->i_sb
!= file
->f_path
.dentry
->d_inode
->i_sb
) {
613 printk("btrfs: Snapshot src from another FS\n");
618 ret
= btrfs_mksubvol(&file
->f_path
, vol_args
->name
,
619 file
->f_path
.dentry
->d_inode
->i_mode
,
620 namelen
, BTRFS_I(src_inode
)->root
);
629 static int btrfs_ioctl_defrag(struct file
*file
)
631 struct inode
*inode
= fdentry(file
)->d_inode
;
632 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
635 ret
= mnt_want_write(file
->f_path
.mnt
);
639 switch (inode
->i_mode
& S_IFMT
) {
641 btrfs_defrag_root(root
, 0);
642 btrfs_defrag_root(root
->fs_info
->extent_root
, 0);
645 btrfs_defrag_file(file
);
652 static long btrfs_ioctl_add_dev(struct btrfs_root
*root
, void __user
*arg
)
654 struct btrfs_ioctl_vol_args
*vol_args
;
657 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
662 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
666 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
667 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 (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
682 vol_args
= kmalloc(sizeof(*vol_args
), GFP_NOFS
);
687 if (copy_from_user(vol_args
, arg
, sizeof(*vol_args
))) {
691 vol_args
->name
[BTRFS_PATH_NAME_MAX
] = '\0';
692 ret
= btrfs_rm_device(root
, vol_args
->name
);
699 static long btrfs_ioctl_clone(struct file
*file
, unsigned long srcfd
,
700 u64 off
, u64 olen
, u64 destoff
)
702 struct inode
*inode
= fdentry(file
)->d_inode
;
703 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
704 struct file
*src_file
;
706 struct btrfs_trans_handle
*trans
;
707 struct btrfs_path
*path
;
708 struct extent_buffer
*leaf
;
710 struct btrfs_key key
;
715 u64 bs
= root
->fs_info
->sb
->s_blocksize
;
720 * - split compressed inline extents. annoying: we need to
721 * decompress into destination's address_space (the file offset
722 * may change, so source mapping won't do), then recompress (or
723 * otherwise reinsert) a subrange.
724 * - allow ranges within the same file to be cloned (provided
725 * they don't overlap)?
728 ret
= mnt_want_write(file
->f_path
.mnt
);
732 src_file
= fget(srcfd
);
735 src
= src_file
->f_dentry
->d_inode
;
742 if (S_ISDIR(src
->i_mode
) || S_ISDIR(inode
->i_mode
))
746 if (src
->i_sb
!= inode
->i_sb
|| BTRFS_I(src
)->root
!= root
)
750 buf
= vmalloc(btrfs_level_size(root
, 0));
754 path
= btrfs_alloc_path();
762 mutex_lock(&inode
->i_mutex
);
763 mutex_lock(&src
->i_mutex
);
765 mutex_lock(&src
->i_mutex
);
766 mutex_lock(&inode
->i_mutex
);
769 /* determine range to clone */
771 if (off
>= src
->i_size
|| off
+ len
> src
->i_size
)
774 olen
= len
= src
->i_size
- off
;
775 /* if we extend to eof, continue to block boundary */
776 if (off
+ len
== src
->i_size
)
777 len
= ((src
->i_size
+ bs
-1) & ~(bs
-1))
780 /* verify the end result is block aligned */
781 if ((off
& (bs
-1)) ||
782 ((off
+ len
) & (bs
-1)))
785 printk("final src extent is %llu~%llu\n", off
, len
);
786 printk("final dst extent is %llu~%llu\n", destoff
, len
);
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
, 0, &hint_byte
);
809 key
.objectid
= src
->i_ino
;
810 key
.type
= BTRFS_EXTENT_DATA_KEY
;
815 * note the key will change type as we walk through the
818 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 0);
822 nritems
= btrfs_header_nritems(path
->nodes
[0]);
823 if (path
->slots
[0] >= nritems
) {
824 ret
= btrfs_next_leaf(root
, path
);
829 nritems
= btrfs_header_nritems(path
->nodes
[0]);
831 leaf
= path
->nodes
[0];
832 slot
= path
->slots
[0];
834 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
835 if (btrfs_key_type(&key
) > BTRFS_CSUM_ITEM_KEY
||
836 key
.objectid
!= src
->i_ino
)
839 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
840 struct btrfs_file_extent_item
*extent
;
843 struct btrfs_key new_key
;
844 u64 disko
= 0, diskl
= 0;
845 u64 datao
= 0, datal
= 0;
848 size
= btrfs_item_size_nr(leaf
, slot
);
849 read_extent_buffer(leaf
, buf
,
850 btrfs_item_ptr_offset(leaf
, slot
),
853 extent
= btrfs_item_ptr(leaf
, slot
,
854 struct btrfs_file_extent_item
);
855 comp
= btrfs_file_extent_compression(leaf
, extent
);
856 type
= btrfs_file_extent_type(leaf
, extent
);
857 if (type
== BTRFS_FILE_EXTENT_REG
) {
858 disko
= btrfs_file_extent_disk_bytenr(leaf
, extent
);
859 diskl
= btrfs_file_extent_disk_num_bytes(leaf
, extent
);
860 datao
= btrfs_file_extent_offset(leaf
, extent
);
861 datal
= btrfs_file_extent_num_bytes(leaf
, extent
);
862 } else if (type
== BTRFS_FILE_EXTENT_INLINE
) {
863 /* take upper bound, may be compressed */
864 datal
= btrfs_file_extent_ram_bytes(leaf
,
867 btrfs_release_path(root
, path
);
869 if (key
.offset
+ datal
< off
||
870 key
.offset
>= off
+len
)
873 memcpy(&new_key
, &key
, sizeof(new_key
));
874 new_key
.objectid
= inode
->i_ino
;
875 new_key
.offset
= key
.offset
+ destoff
- off
;
877 if (type
== BTRFS_FILE_EXTENT_REG
) {
878 ret
= btrfs_insert_empty_item(trans
, root
, path
,
883 leaf
= path
->nodes
[0];
884 slot
= path
->slots
[0];
885 write_extent_buffer(leaf
, buf
,
886 btrfs_item_ptr_offset(leaf
, slot
),
889 extent
= btrfs_item_ptr(leaf
, slot
,
890 struct btrfs_file_extent_item
);
891 printk(" orig disk %llu~%llu data %llu~%llu\n",
892 disko
, diskl
, datao
, datal
);
894 if (off
> key
.offset
) {
895 datao
+= off
- key
.offset
;
896 datal
-= off
- key
.offset
;
898 if (key
.offset
+ datao
+ datal
+ key
.offset
>
900 datal
= off
+ len
- key
.offset
- datao
;
901 /* disko == 0 means it's a hole */
904 printk(" final disk %llu~%llu data %llu~%llu\n",
905 disko
, diskl
, datao
, datal
);
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
;
927 if (key
.offset
+ datal
> off
+len
)
928 trim
= key
.offset
+ datal
- (off
+len
);
929 printk("len %lld skip %lld trim %lld\n",
931 if (comp
&& (skip
|| trim
)) {
932 printk("btrfs clone_range can't split compressed inline extents yet\n");
937 datal
-= skip
+ trim
;
938 ret
= btrfs_insert_empty_item(trans
, root
, path
,
944 u32 start
= 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
);
960 if (btrfs_key_type(&key
) == BTRFS_CSUM_ITEM_KEY
) {
962 struct btrfs_key new_key
;
966 size
= btrfs_item_size_nr(leaf
, slot
);
967 coverslen
= (size
/ BTRFS_CRC32_SIZE
) <<
968 root
->fs_info
->sb
->s_blocksize_bits
;
969 printk("csums for %llu~%llu\n",
970 key
.offset
, coverslen
);
971 if (key
.offset
+ coverslen
< off
||
972 key
.offset
>= off
+len
)
975 read_extent_buffer(leaf
, buf
,
976 btrfs_item_ptr_offset(leaf
, slot
),
978 btrfs_release_path(root
, path
);
981 if (off
> key
.offset
)
982 coff
= ((off
- key
.offset
) >>
983 root
->fs_info
->sb
->s_blocksize_bits
) *
986 if (key
.offset
+ coverslen
> off
+len
)
987 clen
-= ((key
.offset
+coverslen
-off
-len
) >>
988 root
->fs_info
->sb
->s_blocksize_bits
) *
990 printk(" will dup %d~%d of %d\n",
993 memcpy(&new_key
, &key
, sizeof(new_key
));
994 new_key
.objectid
= inode
->i_ino
;
995 new_key
.offset
= key
.offset
+ destoff
- off
;
997 ret
= btrfs_insert_empty_item(trans
, root
, path
,
1002 leaf
= path
->nodes
[0];
1003 slot
= path
->slots
[0];
1004 write_extent_buffer(leaf
, buf
+ coff
,
1005 btrfs_item_ptr_offset(leaf
, slot
),
1007 btrfs_mark_buffer_dirty(leaf
);
1011 btrfs_release_path(root
, path
);
1016 btrfs_release_path(root
, path
);
1018 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1019 if (destoff
+ olen
> inode
->i_size
)
1020 btrfs_i_size_write(inode
, destoff
+ olen
);
1021 BTRFS_I(inode
)->flags
= BTRFS_I(src
)->flags
;
1022 ret
= btrfs_update_inode(trans
, root
, inode
);
1024 btrfs_end_transaction(trans
, root
);
1025 unlock_extent(&BTRFS_I(src
)->io_tree
, off
, off
+len
, GFP_NOFS
);
1027 vmtruncate(inode
, 0);
1029 mutex_unlock(&src
->i_mutex
);
1030 mutex_unlock(&inode
->i_mutex
);
1032 btrfs_free_path(path
);
1038 static long btrfs_ioctl_clone_range(struct file
*file
, void __user
*argp
)
1040 struct btrfs_ioctl_clone_range_args args
;
1042 if (copy_from_user(&args
, argp
, sizeof(args
)))
1044 return btrfs_ioctl_clone(file
, args
.src_fd
, args
.src_offset
,
1045 args
.src_length
, args
.dest_offset
);
1049 * there are many ways the trans_start and trans_end ioctls can lead
1050 * to deadlocks. They should only be used by applications that
1051 * basically own the machine, and have a very in depth understanding
1052 * of all the possible deadlocks and enospc problems.
1054 static long btrfs_ioctl_trans_start(struct file
*file
)
1056 struct inode
*inode
= fdentry(file
)->d_inode
;
1057 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1058 struct btrfs_trans_handle
*trans
;
1061 if (!capable(CAP_SYS_ADMIN
))
1064 if (file
->private_data
) {
1069 ret
= mnt_want_write(file
->f_path
.mnt
);
1073 mutex_lock(&root
->fs_info
->trans_mutex
);
1074 root
->fs_info
->open_ioctl_trans
++;
1075 mutex_unlock(&root
->fs_info
->trans_mutex
);
1077 trans
= btrfs_start_ioctl_transaction(root
, 0);
1079 file
->private_data
= trans
;
1082 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
1088 * there are many ways the trans_start and trans_end ioctls can lead
1089 * to deadlocks. They should only be used by applications that
1090 * basically own the machine, and have a very in depth understanding
1091 * of all the possible deadlocks and enospc problems.
1093 long btrfs_ioctl_trans_end(struct file
*file
)
1095 struct inode
*inode
= fdentry(file
)->d_inode
;
1096 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1097 struct btrfs_trans_handle
*trans
;
1100 trans
= file
->private_data
;
1105 btrfs_end_transaction(trans
, root
);
1106 file
->private_data
= NULL
;
1108 mutex_lock(&root
->fs_info
->trans_mutex
);
1109 root
->fs_info
->open_ioctl_trans
--;
1110 mutex_unlock(&root
->fs_info
->trans_mutex
);
1116 long btrfs_ioctl(struct file
*file
, unsigned int
1117 cmd
, unsigned long arg
)
1119 struct btrfs_root
*root
= BTRFS_I(fdentry(file
)->d_inode
)->root
;
1120 void __user
*argp
= (void __user
*)arg
;
1123 case BTRFS_IOC_SNAP_CREATE
:
1124 return btrfs_ioctl_snap_create(file
, argp
, 0);
1125 case BTRFS_IOC_SUBVOL_CREATE
:
1126 return btrfs_ioctl_snap_create(file
, argp
, 1);
1127 case BTRFS_IOC_DEFRAG
:
1128 return btrfs_ioctl_defrag(file
);
1129 case BTRFS_IOC_RESIZE
:
1130 return btrfs_ioctl_resize(root
, argp
);
1131 case BTRFS_IOC_ADD_DEV
:
1132 return btrfs_ioctl_add_dev(root
, argp
);
1133 case BTRFS_IOC_RM_DEV
:
1134 return btrfs_ioctl_rm_dev(root
, argp
);
1135 case BTRFS_IOC_BALANCE
:
1136 return btrfs_balance(root
->fs_info
->dev_root
);
1137 case BTRFS_IOC_CLONE
:
1138 return btrfs_ioctl_clone(file
, arg
, 0, 0, 0);
1139 case BTRFS_IOC_CLONE_RANGE
:
1140 return btrfs_ioctl_clone_range(file
, argp
);
1141 case BTRFS_IOC_TRANS_START
:
1142 return btrfs_ioctl_trans_start(file
);
1143 case BTRFS_IOC_TRANS_END
:
1144 return btrfs_ioctl_trans_end(file
);
1145 case BTRFS_IOC_SYNC
:
1146 btrfs_sync_fs(file
->f_dentry
->d_sb
, 1);