1 #include <linux/module.h>
2 #include <linux/buffer_head.h>
4 #include <linux/pagemap.h>
5 #include <linux/highmem.h>
6 #include <linux/time.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9 #include <linux/smp_lock.h>
10 #include <linux/backing-dev.h>
11 #include <linux/mpage.h>
12 #include <linux/swap.h>
13 #include <linux/writeback.h>
14 #include <linux/statfs.h>
15 #include <linux/compat.h>
18 #include "transaction.h"
19 #include "btrfs_inode.h"
21 #include "print-tree.h"
24 static int btrfs_copy_from_user(loff_t pos
, int num_pages
, int write_bytes
,
25 struct page
**prepared_pages
,
26 const char __user
* buf
)
30 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
32 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
33 size_t count
= min_t(size_t,
34 PAGE_CACHE_SIZE
- offset
, write_bytes
);
35 struct page
*page
= prepared_pages
[i
];
36 fault_in_pages_readable(buf
, count
);
38 /* Copy data from userspace to the current page */
40 page_fault
= __copy_from_user(page_address(page
) + offset
,
42 /* Flush processor's dcache for this page */
43 flush_dcache_page(page
);
51 return page_fault
? -EFAULT
: 0;
54 static void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
57 for (i
= 0; i
< num_pages
; i
++) {
60 unlock_page(pages
[i
]);
61 mark_page_accessed(pages
[i
]);
62 page_cache_release(pages
[i
]);
66 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
67 struct btrfs_root
*root
,
79 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
80 struct buffer_head
*bh
;
81 struct btrfs_file_extent_item
*ei
;
83 for (i
= 0; i
< num_pages
; i
++) {
84 offset
= pos
& (PAGE_CACHE_SIZE
-1);
85 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
86 /* FIXME, one block at a time */
88 mutex_lock(&root
->fs_info
->fs_mutex
);
89 trans
= btrfs_start_transaction(root
, 1);
90 btrfs_set_trans_block_group(trans
, inode
);
92 bh
= page_buffers(pages
[i
]);
94 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
96 struct btrfs_path
*path
;
100 /* create an inline extent, and copy the data in */
101 path
= btrfs_alloc_path();
103 key
.objectid
= inode
->i_ino
;
104 key
.offset
= pages
[i
]->index
<< PAGE_CACHE_SHIFT
;
106 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
107 BUG_ON(write_bytes
>= PAGE_CACHE_SIZE
);
109 btrfs_file_extent_calc_inline_size(write_bytes
);
111 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
114 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
115 path
->slots
[0], struct btrfs_file_extent_item
);
116 btrfs_set_file_extent_generation(ei
, trans
->transid
);
117 btrfs_set_file_extent_type(ei
,
118 BTRFS_FILE_EXTENT_INLINE
);
119 ptr
= btrfs_file_extent_inline_start(ei
);
120 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
121 ptr
, bh
->b_data
, offset
+ write_bytes
);
122 mark_buffer_dirty(path
->nodes
[0]);
123 btrfs_free_path(path
);
124 } else if (buffer_mapped(bh
)) {
125 /* csum the file data */
126 btrfs_csum_file_block(trans
, root
, inode
->i_ino
,
127 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
128 kmap(pages
[i
]), PAGE_CACHE_SIZE
);
131 SetPageChecked(pages
[i
]);
132 ret
= btrfs_end_transaction(trans
, root
);
134 mutex_unlock(&root
->fs_info
->fs_mutex
);
136 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
137 offset
+ this_write
);
143 WARN_ON(this_write
> write_bytes
);
144 write_bytes
-= this_write
;
151 * this is very complex, but the basic idea is to drop all extents
152 * in the range start - end. hint_block is filled in with a block number
153 * that would be a good hint to the block allocator for this file.
155 * If an extent intersects the range but is not entirely inside the range
156 * it is either truncated or split. Anything entirely inside the range
157 * is deleted from the tree.
159 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
,
160 struct btrfs_root
*root
, struct inode
*inode
,
161 u64 start
, u64 end
, u64
*hint_block
)
164 struct btrfs_key key
;
165 struct btrfs_leaf
*leaf
;
167 struct btrfs_file_extent_item
*extent
;
170 struct btrfs_file_extent_item old
;
171 struct btrfs_path
*path
;
172 u64 search_start
= start
;
178 path
= btrfs_alloc_path();
182 btrfs_release_path(root
, path
);
183 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
188 if (path
->slots
[0] == 0) {
199 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
200 slot
= path
->slots
[0];
201 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
202 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
206 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
210 extent
= btrfs_item_ptr(leaf
, slot
,
211 struct btrfs_file_extent_item
);
212 found_type
= btrfs_file_extent_type(extent
);
213 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
214 extent_end
= key
.offset
+
215 (btrfs_file_extent_num_blocks(extent
) <<
218 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
220 extent_end
= key
.offset
+
221 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
224 /* we found nothing we can drop */
225 if (!found_extent
&& !found_inline
) {
230 /* we found nothing inside the range */
231 if (search_start
>= extent_end
) {
236 /* FIXME, there's only one inline extent allowed right now */
238 u64 mask
= root
->blocksize
- 1;
239 search_start
= (extent_end
+ mask
) & ~mask
;
241 search_start
= extent_end
;
243 if (end
< extent_end
&& end
>= key
.offset
) {
246 btrfs_file_extent_disk_blocknr(extent
);
247 u64 disk_num_blocks
=
248 btrfs_file_extent_disk_num_blocks(extent
);
249 memcpy(&old
, extent
, sizeof(old
));
250 if (disk_blocknr
!= 0) {
251 ret
= btrfs_inc_extent_ref(trans
, root
,
252 disk_blocknr
, disk_num_blocks
);
256 WARN_ON(found_inline
);
260 /* truncate existing extent */
261 if (start
> key
.offset
) {
265 WARN_ON(start
& (root
->blocksize
- 1));
267 new_num
= (start
- key
.offset
) >>
269 old_num
= btrfs_file_extent_num_blocks(extent
);
271 btrfs_file_extent_disk_blocknr(extent
);
272 if (btrfs_file_extent_disk_blocknr(extent
)) {
274 (old_num
- new_num
) << 3;
276 btrfs_set_file_extent_num_blocks(extent
,
278 mark_buffer_dirty(path
->nodes
[0]);
283 /* delete the entire extent */
285 u64 disk_blocknr
= 0;
286 u64 disk_num_blocks
= 0;
287 u64 extent_num_blocks
= 0;
290 btrfs_file_extent_disk_blocknr(extent
);
292 btrfs_file_extent_disk_num_blocks(extent
);
294 btrfs_file_extent_num_blocks(extent
);
296 btrfs_file_extent_disk_blocknr(extent
);
298 ret
= btrfs_del_item(trans
, root
, path
);
300 btrfs_release_path(root
, path
);
302 if (found_extent
&& disk_blocknr
!= 0) {
303 inode
->i_blocks
-= extent_num_blocks
<< 3;
304 ret
= btrfs_free_extent(trans
, root
,
310 if (!bookend
&& search_start
>= end
) {
317 /* create bookend, splitting the extent in two */
318 if (bookend
&& found_extent
) {
319 struct btrfs_key ins
;
320 ins
.objectid
= inode
->i_ino
;
323 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
325 btrfs_release_path(root
, path
);
326 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
329 extent
= btrfs_item_ptr(
330 btrfs_buffer_leaf(path
->nodes
[0]),
332 struct btrfs_file_extent_item
);
333 btrfs_set_file_extent_disk_blocknr(extent
,
334 btrfs_file_extent_disk_blocknr(&old
));
335 btrfs_set_file_extent_disk_num_blocks(extent
,
336 btrfs_file_extent_disk_num_blocks(&old
));
338 btrfs_set_file_extent_offset(extent
,
339 btrfs_file_extent_offset(&old
) +
340 ((end
- key
.offset
) >> inode
->i_blkbits
));
341 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
342 (extent_end
- end
) >> inode
->i_blkbits
);
343 btrfs_set_file_extent_num_blocks(extent
,
344 (extent_end
- end
) >> inode
->i_blkbits
);
346 btrfs_set_file_extent_type(extent
,
347 BTRFS_FILE_EXTENT_REG
);
348 btrfs_set_file_extent_generation(extent
,
349 btrfs_file_extent_generation(&old
));
350 btrfs_mark_buffer_dirty(path
->nodes
[0]);
351 if (btrfs_file_extent_disk_blocknr(&old
) != 0) {
353 btrfs_file_extent_num_blocks(extent
) << 3;
360 btrfs_free_path(path
);
365 * this gets pages into the page cache and locks them down
367 static int prepare_pages(struct btrfs_root
*root
,
372 unsigned long first_index
,
373 unsigned long last_index
,
375 u64 alloc_extent_start
)
378 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
379 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
383 struct buffer_head
*bh
;
384 struct buffer_head
*head
;
385 loff_t isize
= i_size_read(inode
);
387 memset(pages
, 0, num_pages
* sizeof(struct page
*));
389 for (i
= 0; i
< num_pages
; i
++) {
390 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
395 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
396 wait_on_page_writeback(pages
[i
]);
397 offset
= pos
& (PAGE_CACHE_SIZE
-1);
398 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
399 if (!page_has_buffers(pages
[i
])) {
400 create_empty_buffers(pages
[i
],
401 root
->fs_info
->sb
->s_blocksize
,
404 head
= page_buffers(pages
[i
]);
407 err
= btrfs_map_bh_to_logical(root
, bh
,
411 goto failed_truncate
;
412 bh
= bh
->b_this_page
;
413 if (alloc_extent_start
)
414 alloc_extent_start
++;
415 } while (bh
!= head
);
417 WARN_ON(this_write
> write_bytes
);
418 write_bytes
-= this_write
;
423 btrfs_drop_pages(pages
, num_pages
);
427 btrfs_drop_pages(pages
, num_pages
);
429 vmtruncate(inode
, isize
);
433 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
434 size_t count
, loff_t
*ppos
)
437 size_t num_written
= 0;
440 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
441 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
442 struct page
*pages
[8];
443 struct page
*pinned
[2];
444 unsigned long first_index
;
445 unsigned long last_index
;
448 u64 alloc_extent_start
;
450 struct btrfs_trans_handle
*trans
;
451 struct btrfs_key ins
;
454 if (file
->f_flags
& O_DIRECT
)
457 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
458 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
459 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
464 err
= remove_suid(file
->f_path
.dentry
);
467 file_update_time(file
);
469 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
470 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
473 mutex_lock(&inode
->i_mutex
);
474 first_index
= pos
>> PAGE_CACHE_SHIFT
;
475 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
478 * there are lots of better ways to do this, but this code
479 * makes sure the first and last page in the file range are
480 * up to date and ready for cow
482 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
483 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
484 if (!PageUptodate(pinned
[0])) {
485 ret
= mpage_readpage(pinned
[0], btrfs_get_block
);
487 wait_on_page_locked(pinned
[0]);
489 unlock_page(pinned
[0]);
492 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
493 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
494 if (!PageUptodate(pinned
[1])) {
495 ret
= mpage_readpage(pinned
[1], btrfs_get_block
);
497 wait_on_page_locked(pinned
[1]);
499 unlock_page(pinned
[1]);
503 mutex_lock(&root
->fs_info
->fs_mutex
);
504 trans
= btrfs_start_transaction(root
, 1);
507 mutex_unlock(&root
->fs_info
->fs_mutex
);
510 btrfs_set_trans_block_group(trans
, inode
);
511 /* FIXME blocksize != 4096 */
512 inode
->i_blocks
+= num_blocks
<< 3;
515 /* FIXME...EIEIO, ENOSPC and more */
517 /* step one, delete the existing extents in this range */
518 if (start_pos
< inode
->i_size
) {
519 /* FIXME blocksize != pagesize */
520 ret
= btrfs_drop_extents(trans
, root
, inode
,
522 (pos
+ count
+ root
->blocksize
-1) &
523 ~((u64
)root
->blocksize
- 1),
528 /* insert any holes we need to create */
529 if (inode
->i_size
< start_pos
) {
530 u64 last_pos_in_file
;
532 u64 mask
= root
->blocksize
- 1;
533 last_pos_in_file
= (inode
->i_size
+ mask
) & ~mask
;
534 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
535 hole_size
>>= inode
->i_blkbits
;
536 if (last_pos_in_file
< start_pos
) {
537 ret
= btrfs_insert_file_extent(trans
, root
,
546 * either allocate an extent for the new bytes or setup the key
547 * to show we are doing inline data in the extent
549 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
550 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
551 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
552 num_blocks
, hint_block
, (u64
)-1,
555 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
556 start_pos
, ins
.objectid
, ins
.offset
,
564 alloc_extent_start
= ins
.objectid
;
565 ret
= btrfs_end_transaction(trans
, root
);
566 mutex_unlock(&root
->fs_info
->fs_mutex
);
569 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
570 size_t write_bytes
= min(count
,
571 (size_t)PAGE_CACHE_SIZE
- offset
);
572 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
575 memset(pages
, 0, sizeof(pages
));
576 ret
= prepare_pages(root
, file
, pages
, num_pages
,
577 pos
, first_index
, last_index
,
578 write_bytes
, alloc_extent_start
);
581 /* FIXME blocks != pagesize */
582 if (alloc_extent_start
)
583 alloc_extent_start
+= num_pages
;
584 ret
= btrfs_copy_from_user(pos
, num_pages
,
585 write_bytes
, pages
, buf
);
588 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
589 num_pages
, pos
, write_bytes
);
591 btrfs_drop_pages(pages
, num_pages
);
594 count
-= write_bytes
;
596 num_written
+= write_bytes
;
598 balance_dirty_pages_ratelimited(inode
->i_mapping
);
599 btrfs_btree_balance_dirty(root
);
603 mutex_unlock(&inode
->i_mutex
);
606 page_cache_release(pinned
[0]);
608 page_cache_release(pinned
[1]);
610 current
->backing_dev_info
= NULL
;
611 mark_inode_dirty(inode
);
612 return num_written
? num_written
: err
;
616 * FIXME, do this by stuffing the csum we want in the info hanging off
617 * page->private. For now, verify file csums on read
619 static int btrfs_read_actor(read_descriptor_t
*desc
, struct page
*page
,
620 unsigned long offset
, unsigned long size
)
623 unsigned long left
, count
= desc
->count
;
624 struct inode
*inode
= page
->mapping
->host
;
629 if (!PageChecked(page
)) {
630 /* FIXME, do it per block */
631 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
633 struct buffer_head
*bh
;
635 if (page_has_buffers(page
)) {
636 bh
= page_buffers(page
);
637 if (!buffer_mapped(bh
)) {
638 SetPageChecked(page
);
643 ret
= btrfs_csum_verify_file_block(root
,
644 page
->mapping
->host
->i_ino
,
645 page
->index
<< PAGE_CACHE_SHIFT
,
646 kmap(page
), PAGE_CACHE_SIZE
);
648 if (ret
!= -ENOENT
) {
649 printk("failed to verify ino %lu page %lu ret %d\n",
650 page
->mapping
->host
->i_ino
,
652 memset(page_address(page
), 1, PAGE_CACHE_SIZE
);
653 flush_dcache_page(page
);
656 SetPageChecked(page
);
661 * Faults on the destination of a read are common, so do it before
664 if (!fault_in_pages_writeable(desc
->arg
.buf
, size
)) {
665 kaddr
= kmap_atomic(page
, KM_USER0
);
666 left
= __copy_to_user_inatomic(desc
->arg
.buf
,
667 kaddr
+ offset
, size
);
668 kunmap_atomic(kaddr
, KM_USER0
);
673 /* Do it the slow way */
675 left
= __copy_to_user(desc
->arg
.buf
, kaddr
+ offset
, size
);
680 desc
->error
= -EFAULT
;
683 desc
->count
= count
- size
;
684 desc
->written
+= size
;
685 desc
->arg
.buf
+= size
;
690 * btrfs_file_aio_read - filesystem read routine, with a mod to csum verify
691 * @iocb: kernel I/O control block
692 * @iov: io vector request
693 * @nr_segs: number of segments in the iovec
694 * @pos: current file position
696 static ssize_t
btrfs_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
697 unsigned long nr_segs
, loff_t pos
)
699 struct file
*filp
= iocb
->ki_filp
;
703 loff_t
*ppos
= &iocb
->ki_pos
;
706 for (seg
= 0; seg
< nr_segs
; seg
++) {
707 const struct iovec
*iv
= &iov
[seg
];
710 * If any segment has a negative length, or the cumulative
711 * length ever wraps negative then return -EINVAL.
713 count
+= iv
->iov_len
;
714 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
716 if (access_ok(VERIFY_WRITE
, iv
->iov_base
, iv
->iov_len
))
721 count
-= iv
->iov_len
; /* This segment is no good */
726 for (seg
= 0; seg
< nr_segs
; seg
++) {
727 read_descriptor_t desc
;
730 desc
.arg
.buf
= iov
[seg
].iov_base
;
731 desc
.count
= iov
[seg
].iov_len
;
735 do_generic_file_read(filp
, ppos
, &desc
,
737 retval
+= desc
.written
;
739 retval
= retval
?: desc
.error
;
747 static int btrfs_sync_file(struct file
*file
,
748 struct dentry
*dentry
, int datasync
)
750 struct inode
*inode
= dentry
->d_inode
;
751 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
753 struct btrfs_trans_handle
*trans
;
756 * FIXME, use inode generation number to check if we can skip the
759 mutex_lock(&root
->fs_info
->fs_mutex
);
760 trans
= btrfs_start_transaction(root
, 1);
765 ret
= btrfs_commit_transaction(trans
, root
);
766 mutex_unlock(&root
->fs_info
->fs_mutex
);
768 return ret
> 0 ? EIO
: ret
;
771 struct file_operations btrfs_file_operations
= {
772 .llseek
= generic_file_llseek
,
773 .read
= do_sync_read
,
774 .aio_read
= btrfs_file_aio_read
,
775 .write
= btrfs_file_write
,
776 .mmap
= generic_file_mmap
,
777 .open
= generic_file_open
,
778 .ioctl
= btrfs_ioctl
,
779 .fsync
= btrfs_sync_file
,
781 .compat_ioctl
= btrfs_compat_ioctl
,