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/module.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
39 #include "print-tree.h"
42 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
43 struct page
**prepared_pages
,
44 const char __user
* buf
)
48 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
50 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
51 size_t count
= min_t(size_t,
52 PAGE_CACHE_SIZE
- offset
, write_bytes
);
53 struct page
*page
= prepared_pages
[i
];
54 fault_in_pages_readable(buf
, count
);
56 /* Copy data from userspace to the current page */
58 page_fault
= __copy_from_user(page_address(page
) + offset
,
60 /* Flush processor's dcache for this page */
61 flush_dcache_page(page
);
69 return page_fault
? -EFAULT
: 0;
72 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
75 for (i
= 0; i
< num_pages
; i
++) {
78 unlock_page(pages
[i
]);
79 mark_page_accessed(pages
[i
]);
80 page_cache_release(pages
[i
]);
84 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
85 struct btrfs_root
*root
,
97 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
98 struct buffer_head
*bh
;
99 struct btrfs_file_extent_item
*ei
;
101 for (i
= 0; i
< num_pages
; i
++) {
102 offset
= pos
& (PAGE_CACHE_SIZE
-1);
103 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
104 /* FIXME, one block at a time */
106 mutex_lock(&root
->fs_info
->fs_mutex
);
107 trans
= btrfs_start_transaction(root
, 1);
108 btrfs_set_trans_block_group(trans
, inode
);
110 bh
= page_buffers(pages
[i
]);
112 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
113 struct btrfs_key key
;
114 struct btrfs_path
*path
;
118 /* create an inline extent, and copy the data in */
119 path
= btrfs_alloc_path();
121 key
.objectid
= inode
->i_ino
;
122 key
.offset
= pages
[i
]->index
<< PAGE_CACHE_SHIFT
;
124 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
125 BUG_ON(write_bytes
>= PAGE_CACHE_SIZE
);
127 btrfs_file_extent_calc_inline_size(write_bytes
);
129 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
132 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
133 path
->slots
[0], struct btrfs_file_extent_item
);
134 btrfs_set_file_extent_generation(ei
, trans
->transid
);
135 btrfs_set_file_extent_type(ei
,
136 BTRFS_FILE_EXTENT_INLINE
);
137 ptr
= btrfs_file_extent_inline_start(ei
);
138 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
139 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
140 ptr
, kaddr
+ bh_offset(bh
),
141 offset
+ write_bytes
);
142 kunmap_atomic(kaddr
, KM_USER0
);
143 mark_buffer_dirty(path
->nodes
[0]);
144 btrfs_free_path(path
);
145 } else if (buffer_mapped(bh
)) {
146 /* csum the file data */
147 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
148 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
149 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
152 SetPageChecked(pages
[i
]);
153 ret
= btrfs_end_transaction(trans
, root
);
155 mutex_unlock(&root
->fs_info
->fs_mutex
);
157 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
158 offset
+ this_write
);
164 WARN_ON(this_write
> write_bytes
);
165 write_bytes
-= this_write
;
172 * this is very complex, but the basic idea is to drop all extents
173 * in the range start - end. hint_block is filled in with a block number
174 * that would be a good hint to the block allocator for this file.
176 * If an extent intersects the range but is not entirely inside the range
177 * it is either truncated or split. Anything entirely inside the range
178 * is deleted from the tree.
180 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
,
181 struct btrfs_root
*root
, struct inode
*inode
,
182 u64 start
, u64 end
, u64
*hint_block
)
185 struct btrfs_key key
;
186 struct btrfs_leaf
*leaf
;
188 struct btrfs_file_extent_item
*extent
;
191 struct btrfs_file_extent_item old
;
192 struct btrfs_path
*path
;
193 u64 search_start
= start
;
199 path
= btrfs_alloc_path();
203 btrfs_release_path(root
, path
);
204 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
209 if (path
->slots
[0] == 0) {
220 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
221 slot
= path
->slots
[0];
222 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
223 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
227 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
231 extent
= btrfs_item_ptr(leaf
, slot
,
232 struct btrfs_file_extent_item
);
233 found_type
= btrfs_file_extent_type(extent
);
234 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
235 extent_end
= key
.offset
+
236 (btrfs_file_extent_num_blocks(extent
) <<
239 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
241 extent_end
= key
.offset
+
242 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
245 /* we found nothing we can drop */
246 if (!found_extent
&& !found_inline
) {
251 /* we found nothing inside the range */
252 if (search_start
>= extent_end
) {
257 /* FIXME, there's only one inline extent allowed right now */
259 u64 mask
= root
->blocksize
- 1;
260 search_start
= (extent_end
+ mask
) & ~mask
;
262 search_start
= extent_end
;
264 if (end
< extent_end
&& end
>= key
.offset
) {
267 btrfs_file_extent_disk_blocknr(extent
);
268 u64 disk_num_blocks
=
269 btrfs_file_extent_disk_num_blocks(extent
);
270 memcpy(&old
, extent
, sizeof(old
));
271 if (disk_blocknr
!= 0) {
272 ret
= btrfs_inc_extent_ref(trans
, root
,
273 disk_blocknr
, disk_num_blocks
);
277 WARN_ON(found_inline
);
281 /* truncate existing extent */
282 if (start
> key
.offset
) {
286 WARN_ON(start
& (root
->blocksize
- 1));
288 new_num
= (start
- key
.offset
) >>
290 old_num
= btrfs_file_extent_num_blocks(extent
);
292 btrfs_file_extent_disk_blocknr(extent
);
293 if (btrfs_file_extent_disk_blocknr(extent
)) {
295 (old_num
- new_num
) << 3;
297 btrfs_set_file_extent_num_blocks(extent
,
299 mark_buffer_dirty(path
->nodes
[0]);
304 /* delete the entire extent */
306 u64 disk_blocknr
= 0;
307 u64 disk_num_blocks
= 0;
308 u64 extent_num_blocks
= 0;
311 btrfs_file_extent_disk_blocknr(extent
);
313 btrfs_file_extent_disk_num_blocks(extent
);
315 btrfs_file_extent_num_blocks(extent
);
317 btrfs_file_extent_disk_blocknr(extent
);
319 ret
= btrfs_del_item(trans
, root
, path
);
321 btrfs_release_path(root
, path
);
323 if (found_extent
&& disk_blocknr
!= 0) {
324 inode
->i_blocks
-= extent_num_blocks
<< 3;
325 ret
= btrfs_free_extent(trans
, root
,
331 if (!bookend
&& search_start
>= end
) {
338 /* create bookend, splitting the extent in two */
339 if (bookend
&& found_extent
) {
340 struct btrfs_key ins
;
341 ins
.objectid
= inode
->i_ino
;
344 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
346 btrfs_release_path(root
, path
);
347 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
350 extent
= btrfs_item_ptr(
351 btrfs_buffer_leaf(path
->nodes
[0]),
353 struct btrfs_file_extent_item
);
354 btrfs_set_file_extent_disk_blocknr(extent
,
355 btrfs_file_extent_disk_blocknr(&old
));
356 btrfs_set_file_extent_disk_num_blocks(extent
,
357 btrfs_file_extent_disk_num_blocks(&old
));
359 btrfs_set_file_extent_offset(extent
,
360 btrfs_file_extent_offset(&old
) +
361 ((end
- key
.offset
) >> inode
->i_blkbits
));
362 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
363 (extent_end
- end
) >> inode
->i_blkbits
);
364 btrfs_set_file_extent_num_blocks(extent
,
365 (extent_end
- end
) >> inode
->i_blkbits
);
367 btrfs_set_file_extent_type(extent
,
368 BTRFS_FILE_EXTENT_REG
);
369 btrfs_set_file_extent_generation(extent
,
370 btrfs_file_extent_generation(&old
));
371 btrfs_mark_buffer_dirty(path
->nodes
[0]);
372 if (btrfs_file_extent_disk_blocknr(&old
) != 0) {
374 btrfs_file_extent_num_blocks(extent
) << 3;
381 btrfs_free_path(path
);
386 * this gets pages into the page cache and locks them down
388 static int prepare_pages(struct btrfs_root
*root
,
393 unsigned long first_index
,
394 unsigned long last_index
,
396 u64 alloc_extent_start
)
399 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
400 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
404 struct buffer_head
*bh
;
405 struct buffer_head
*head
;
406 loff_t isize
= i_size_read(inode
);
408 memset(pages
, 0, num_pages
* sizeof(struct page
*));
410 for (i
= 0; i
< num_pages
; i
++) {
411 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
416 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
417 wait_on_page_writeback(pages
[i
]);
418 offset
= pos
& (PAGE_CACHE_SIZE
-1);
419 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
420 if (!page_has_buffers(pages
[i
])) {
421 create_empty_buffers(pages
[i
],
422 root
->fs_info
->sb
->s_blocksize
,
425 head
= page_buffers(pages
[i
]);
428 err
= btrfs_map_bh_to_logical(root
, bh
,
432 goto failed_truncate
;
433 bh
= bh
->b_this_page
;
434 if (alloc_extent_start
)
435 alloc_extent_start
++;
436 } while (bh
!= head
);
438 WARN_ON(this_write
> write_bytes
);
439 write_bytes
-= this_write
;
444 btrfs_drop_pages(pages
, num_pages
);
448 btrfs_drop_pages(pages
, num_pages
);
450 vmtruncate(inode
, isize
);
454 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
455 size_t count
, loff_t
*ppos
)
458 size_t num_written
= 0;
461 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
462 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
463 struct page
*pages
[8];
464 struct page
*pinned
[2];
465 unsigned long first_index
;
466 unsigned long last_index
;
469 u64 alloc_extent_start
;
471 struct btrfs_trans_handle
*trans
;
472 struct btrfs_key ins
;
475 if (file
->f_flags
& O_DIRECT
)
478 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
479 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
480 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
485 err
= remove_suid(file
->f_path
.dentry
);
488 file_update_time(file
);
490 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
491 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
494 mutex_lock(&inode
->i_mutex
);
495 first_index
= pos
>> PAGE_CACHE_SHIFT
;
496 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
499 * there are lots of better ways to do this, but this code
500 * makes sure the first and last page in the file range are
501 * up to date and ready for cow
503 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
504 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
505 if (!PageUptodate(pinned
[0])) {
506 ret
= mpage_readpage(pinned
[0], btrfs_get_block
);
508 wait_on_page_locked(pinned
[0]);
510 unlock_page(pinned
[0]);
513 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
514 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
515 if (!PageUptodate(pinned
[1])) {
516 ret
= mpage_readpage(pinned
[1], btrfs_get_block
);
518 wait_on_page_locked(pinned
[1]);
520 unlock_page(pinned
[1]);
524 mutex_lock(&root
->fs_info
->fs_mutex
);
525 trans
= btrfs_start_transaction(root
, 1);
528 mutex_unlock(&root
->fs_info
->fs_mutex
);
531 btrfs_set_trans_block_group(trans
, inode
);
532 /* FIXME blocksize != 4096 */
533 inode
->i_blocks
+= num_blocks
<< 3;
536 /* FIXME...EIEIO, ENOSPC and more */
538 /* step one, delete the existing extents in this range */
539 if (start_pos
< inode
->i_size
) {
540 /* FIXME blocksize != pagesize */
541 ret
= btrfs_drop_extents(trans
, root
, inode
,
543 (pos
+ count
+ root
->blocksize
-1) &
544 ~((u64
)root
->blocksize
- 1),
549 /* insert any holes we need to create */
550 if (inode
->i_size
< start_pos
) {
551 u64 last_pos_in_file
;
553 u64 mask
= root
->blocksize
- 1;
554 last_pos_in_file
= (inode
->i_size
+ mask
) & ~mask
;
555 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
556 hole_size
>>= inode
->i_blkbits
;
557 if (last_pos_in_file
< start_pos
) {
558 ret
= btrfs_insert_file_extent(trans
, root
,
567 * either allocate an extent for the new bytes or setup the key
568 * to show we are doing inline data in the extent
570 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
571 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
572 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
573 num_blocks
, hint_block
, (u64
)-1,
576 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
577 start_pos
, ins
.objectid
, ins
.offset
,
585 alloc_extent_start
= ins
.objectid
;
586 ret
= btrfs_end_transaction(trans
, root
);
587 mutex_unlock(&root
->fs_info
->fs_mutex
);
590 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
591 size_t write_bytes
= min(count
,
592 (size_t)PAGE_CACHE_SIZE
- offset
);
593 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
596 memset(pages
, 0, sizeof(pages
));
597 ret
= prepare_pages(root
, file
, pages
, num_pages
,
598 pos
, first_index
, last_index
,
599 write_bytes
, alloc_extent_start
);
602 /* FIXME blocks != pagesize */
603 if (alloc_extent_start
)
604 alloc_extent_start
+= num_pages
;
605 ret
= btrfs_copy_from_user(pos
, num_pages
,
606 write_bytes
, pages
, buf
);
609 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
610 num_pages
, pos
, write_bytes
);
612 btrfs_drop_pages(pages
, num_pages
);
615 count
-= write_bytes
;
617 num_written
+= write_bytes
;
619 balance_dirty_pages_ratelimited(inode
->i_mapping
);
620 btrfs_btree_balance_dirty(root
);
624 mutex_unlock(&inode
->i_mutex
);
627 page_cache_release(pinned
[0]);
629 page_cache_release(pinned
[1]);
631 current
->backing_dev_info
= NULL
;
632 mark_inode_dirty(inode
);
633 return num_written
? num_written
: err
;
637 * FIXME, do this by stuffing the csum we want in the info hanging off
638 * page->private. For now, verify file csums on read
640 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
641 unsigned long offset
, unsigned long size
)
644 unsigned long left
, count
= desc
->count
;
645 struct inode
*inode
= page
->mapping
->host
;
650 if (!PageChecked(page
)) {
651 /* FIXME, do it per block */
652 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
654 struct buffer_head
*bh
;
656 if (page_has_buffers(page
)) {
657 bh
= page_buffers(page
);
658 if (!buffer_mapped(bh
)) {
659 SetPageChecked(page
);
664 ret
= btrfs_csum_verify_file_block(root
,
665 page
->mapping
->host
->i_ino
,
666 page
->index
<< PAGE_CACHE_SHIFT
,
667 kmap(page
), PAGE_CACHE_SIZE
);
669 if (ret
!= -ENOENT
) {
670 printk("failed to verify ino %lu page %lu ret %d\n",
671 page
->mapping
->host
->i_ino
,
673 memset(page_address(page
), 1, PAGE_CACHE_SIZE
);
674 flush_dcache_page(page
);
677 SetPageChecked(page
);
682 * Faults on the destination of a read are common, so do it before
685 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
686 kaddr
= kmap_atomic(page
, KM_USER0
);
687 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
688 kaddr
+ offset
, size
);
689 kunmap_atomic(kaddr
, KM_USER0
);
694 /* Do it the slow way */
696 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
701 desc
->error
= -EFAULT
;
704 desc
->count
= count
- size
;
705 desc
->written
+= size
;
706 desc
->arg
.buf
+= size
;
711 * btrfs_file_aio_read - filesystem read routine, with a mod to csum verify
712 * @iocb: kernel I/O control block
713 * @iov: io vector request
714 * @nr_segs: number of segments in the iovec
715 * @pos: current file position
717 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
718 unsigned long nr_segs
, loff_t pos
)
720 struct file
*filp
= iocb
->ki_filp
;
724 loff_t
*ppos
= &iocb
->ki_pos
;
727 for (seg
= 0; seg
< nr_segs
; seg
++) {
728 const struct iovec
*iv
= &iov
[seg
];
731 * If any segment has a negative length, or the cumulative
732 * length ever wraps negative then return -EINVAL.
734 count
+= iv
->iov_len
;
735 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
737 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
742 count
-= iv
->iov_len
; /* This segment is no good */
747 for (seg
= 0; seg
< nr_segs
; seg
++) {
748 read_descriptor_t desc
;
751 desc
.arg
.buf
= iov
[seg
].iov_base
;
752 desc
.count
= iov
[seg
].iov_len
;
756 do_generic_file_read(filp
, ppos
, &desc
,
758 retval
+= desc
.written
;
760 retval
= retval
?: desc
.error
;
768 static int btrfs_sync_file(struct file
*file
,
769 struct dentry
*dentry
, int datasync
)
771 struct inode
*inode
= dentry
->d_inode
;
772 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
774 struct btrfs_trans_handle
*trans
;
777 * FIXME, use inode generation number to check if we can skip the
780 mutex_lock(&root
->fs_info
->fs_mutex
);
781 trans
= btrfs_start_transaction(root
, 1);
786 ret
= btrfs_commit_transaction(trans
, root
);
787 mutex_unlock(&root
->fs_info
->fs_mutex
);
789 return ret
> 0 ? EIO
: ret
;
792 struct file_operations btrfs_file_operations
= {
793 .llseek
= generic_file_llseek
,
794 .read
= do_sync_read
,
795 .aio_read
= btrfs_file_aio_read
,
796 .write
= btrfs_file_write
,
797 .mmap
= generic_file_mmap
,
798 .open
= generic_file_open
,
799 .ioctl
= btrfs_ioctl
,
800 .fsync
= btrfs_sync_file
,
802 .compat_ioctl
= btrfs_compat_ioctl
,