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/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/version.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 insert_inline_extent(struct btrfs_root
*root
, struct inode
*inode
,
85 u64 offset
, ssize_t size
,
86 struct buffer_head
*bh
)
89 struct btrfs_path
*path
;
91 struct btrfs_trans_handle
*trans
;
92 struct btrfs_file_extent_item
*ei
;
97 path
= btrfs_alloc_path();
101 mutex_lock(&root
->fs_info
->fs_mutex
);
102 trans
= btrfs_start_transaction(root
, 1);
103 btrfs_set_trans_block_group(trans
, inode
);
105 key
.objectid
= inode
->i_ino
;
108 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
109 BUG_ON(size
>= PAGE_CACHE_SIZE
);
110 datasize
= btrfs_file_extent_calc_inline_size(size
);
112 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
118 ei
= btrfs_item_ptr(btrfs_buffer_leaf(path
->nodes
[0]),
119 path
->slots
[0], struct btrfs_file_extent_item
);
120 btrfs_set_file_extent_generation(ei
, trans
->transid
);
121 btrfs_set_file_extent_type(ei
,
122 BTRFS_FILE_EXTENT_INLINE
);
123 ptr
= btrfs_file_extent_inline_start(ei
);
125 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
126 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
127 ptr
, kaddr
+ bh_offset(bh
),
129 kunmap_atomic(kaddr
, KM_USER0
);
130 btrfs_mark_buffer_dirty(path
->nodes
[0]);
132 btrfs_free_path(path
);
133 ret
= btrfs_end_transaction(trans
, root
);
136 mutex_unlock(&root
->fs_info
->fs_mutex
);
140 static int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
141 struct btrfs_root
*root
,
153 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
154 struct buffer_head
*bh
;
156 for (i
= 0; i
< num_pages
; i
++) {
157 offset
= pos
& (PAGE_CACHE_SIZE
-1);
158 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
160 /* FIXME, one block at a time */
161 bh
= page_buffers(pages
[i
]);
163 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
164 ret
= insert_inline_extent(root
, inode
,
165 pages
[i
]->index
<< PAGE_CACHE_SHIFT
,
166 offset
+ this_write
, bh
);
173 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
174 offset
+ this_write
);
180 WARN_ON(this_write
> write_bytes
);
181 write_bytes
-= this_write
;
188 * this is very complex, but the basic idea is to drop all extents
189 * in the range start - end. hint_block is filled in with a block number
190 * that would be a good hint to the block allocator for this file.
192 * If an extent intersects the range but is not entirely inside the range
193 * it is either truncated or split. Anything entirely inside the range
194 * is deleted from the tree.
196 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
,
197 struct btrfs_root
*root
, struct inode
*inode
,
198 u64 start
, u64 end
, u64
*hint_block
)
201 struct btrfs_key key
;
202 struct btrfs_leaf
*leaf
;
204 struct btrfs_file_extent_item
*extent
;
207 struct btrfs_file_extent_item old
;
208 struct btrfs_path
*path
;
209 u64 search_start
= start
;
216 path
= btrfs_alloc_path();
221 btrfs_release_path(root
, path
);
222 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
227 if (path
->slots
[0] == 0) {
239 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
240 slot
= path
->slots
[0];
242 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
243 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
246 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
) {
250 search_start
= key
.offset
;
253 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
254 extent
= btrfs_item_ptr(leaf
, slot
,
255 struct btrfs_file_extent_item
);
256 found_type
= btrfs_file_extent_type(extent
);
257 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
258 extent_end
= key
.offset
+
259 (btrfs_file_extent_num_blocks(extent
) <<
262 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
264 extent_end
= key
.offset
+
265 btrfs_file_extent_inline_len(leaf
->items
+
269 extent_end
= search_start
;
272 /* we found nothing we can drop */
273 if ((!found_extent
&& !found_inline
) ||
274 search_start
>= extent_end
) {
277 nritems
= btrfs_header_nritems(
278 btrfs_buffer_header(path
->nodes
[0]));
279 if (slot
>= nritems
- 1) {
280 nextret
= btrfs_next_leaf(root
, path
);
290 /* FIXME, there's only one inline extent allowed right now */
292 u64 mask
= root
->blocksize
- 1;
293 search_start
= (extent_end
+ mask
) & ~mask
;
295 search_start
= extent_end
;
297 if (end
< extent_end
&& end
>= key
.offset
) {
300 btrfs_file_extent_disk_blocknr(extent
);
301 u64 disk_num_blocks
=
302 btrfs_file_extent_disk_num_blocks(extent
);
303 memcpy(&old
, extent
, sizeof(old
));
304 if (disk_blocknr
!= 0) {
305 ret
= btrfs_inc_extent_ref(trans
, root
,
306 disk_blocknr
, disk_num_blocks
);
310 WARN_ON(found_inline
);
313 /* truncate existing extent */
314 if (start
> key
.offset
) {
318 WARN_ON(start
& (root
->blocksize
- 1));
320 new_num
= (start
- key
.offset
) >>
322 old_num
= btrfs_file_extent_num_blocks(extent
);
324 btrfs_file_extent_disk_blocknr(extent
);
325 if (btrfs_file_extent_disk_blocknr(extent
)) {
327 (old_num
- new_num
) << 3;
329 btrfs_set_file_extent_num_blocks(extent
,
331 btrfs_mark_buffer_dirty(path
->nodes
[0]);
336 /* delete the entire extent */
338 u64 disk_blocknr
= 0;
339 u64 disk_num_blocks
= 0;
340 u64 extent_num_blocks
= 0;
343 btrfs_file_extent_disk_blocknr(extent
);
345 btrfs_file_extent_disk_num_blocks(extent
);
347 btrfs_file_extent_num_blocks(extent
);
349 btrfs_file_extent_disk_blocknr(extent
);
351 ret
= btrfs_del_item(trans
, root
, path
);
352 /* TODO update progress marker and return */
354 btrfs_release_path(root
, path
);
356 if (found_extent
&& disk_blocknr
!= 0) {
357 inode
->i_blocks
-= extent_num_blocks
<< 3;
358 ret
= btrfs_free_extent(trans
, root
,
364 if (!bookend
&& search_start
>= end
) {
371 /* create bookend, splitting the extent in two */
372 if (bookend
&& found_extent
) {
373 struct btrfs_key ins
;
374 ins
.objectid
= inode
->i_ino
;
377 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
378 btrfs_release_path(root
, path
);
379 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
383 btrfs_print_leaf(root
, btrfs_buffer_leaf(path
->nodes
[0]));
384 printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu\n", ret
, ins
.objectid
, ins
.flags
, ins
.offset
, start
, end
, key
.offset
, extent_end
);
387 extent
= btrfs_item_ptr(
388 btrfs_buffer_leaf(path
->nodes
[0]),
390 struct btrfs_file_extent_item
);
391 btrfs_set_file_extent_disk_blocknr(extent
,
392 btrfs_file_extent_disk_blocknr(&old
));
393 btrfs_set_file_extent_disk_num_blocks(extent
,
394 btrfs_file_extent_disk_num_blocks(&old
));
396 btrfs_set_file_extent_offset(extent
,
397 btrfs_file_extent_offset(&old
) +
398 ((end
- key
.offset
) >> inode
->i_blkbits
));
399 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
400 (extent_end
- end
) >> inode
->i_blkbits
);
401 btrfs_set_file_extent_num_blocks(extent
,
402 (extent_end
- end
) >> inode
->i_blkbits
);
404 btrfs_set_file_extent_type(extent
,
405 BTRFS_FILE_EXTENT_REG
);
406 btrfs_set_file_extent_generation(extent
,
407 btrfs_file_extent_generation(&old
));
408 btrfs_mark_buffer_dirty(path
->nodes
[0]);
409 if (btrfs_file_extent_disk_blocknr(&old
) != 0) {
411 btrfs_file_extent_num_blocks(extent
) << 3;
418 btrfs_free_path(path
);
423 * this gets pages into the page cache and locks them down
425 static int prepare_pages(struct btrfs_root
*root
,
430 unsigned long first_index
,
431 unsigned long last_index
,
435 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
436 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
440 struct buffer_head
*bh
;
441 struct buffer_head
*head
;
442 loff_t isize
= i_size_read(inode
);
443 struct btrfs_trans_handle
*trans
;
446 u64 alloc_extent_start
;
448 struct btrfs_key ins
;
450 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
451 num_blocks
= (write_bytes
+ pos
- start_pos
+ root
->blocksize
- 1) >>
454 memset(pages
, 0, num_pages
* sizeof(struct page
*));
456 for (i
= 0; i
< num_pages
; i
++) {
457 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
462 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
463 wait_on_page_writeback(pages
[i
]);
466 mutex_lock(&root
->fs_info
->fs_mutex
);
467 trans
= btrfs_start_transaction(root
, 1);
470 mutex_unlock(&root
->fs_info
->fs_mutex
);
473 btrfs_set_trans_block_group(trans
, inode
);
474 /* FIXME blocksize != 4096 */
475 inode
->i_blocks
+= num_blocks
<< 3;
478 /* FIXME...EIEIO, ENOSPC and more */
480 /* step one, delete the existing extents in this range */
481 /* FIXME blocksize != pagesize */
482 if (start_pos
< inode
->i_size
) {
483 err
= btrfs_drop_extents(trans
, root
, inode
,
484 start_pos
, (pos
+ write_bytes
+ root
->blocksize
-1) &
485 ~((u64
)root
->blocksize
- 1), &hint_block
);
490 /* insert any holes we need to create */
491 if (inode
->i_size
< start_pos
) {
492 u64 last_pos_in_file
;
494 u64 mask
= root
->blocksize
- 1;
495 last_pos_in_file
= (isize
+ mask
) & ~mask
;
496 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
497 hole_size
>>= inode
->i_blkbits
;
498 if (last_pos_in_file
< start_pos
) {
499 err
= btrfs_insert_file_extent(trans
, root
,
509 * either allocate an extent for the new bytes or setup the key
510 * to show we are doing inline data in the extent
512 if (isize
>= PAGE_CACHE_SIZE
|| pos
+ write_bytes
< inode
->i_size
||
513 pos
+ write_bytes
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
514 err
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
515 num_blocks
, 0, hint_block
, (u64
)-1,
518 goto failed_truncate
;
519 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
520 start_pos
, ins
.objectid
, ins
.offset
,
523 goto failed_truncate
;
529 alloc_extent_start
= ins
.objectid
;
530 err
= btrfs_end_transaction(trans
, root
);
531 mutex_unlock(&root
->fs_info
->fs_mutex
);
533 for (i
= 0; i
< num_pages
; i
++) {
534 offset
= pos
& (PAGE_CACHE_SIZE
-1);
535 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
536 if (!page_has_buffers(pages
[i
])) {
537 create_empty_buffers(pages
[i
],
538 root
->fs_info
->sb
->s_blocksize
,
541 head
= page_buffers(pages
[i
]);
544 err
= btrfs_map_bh_to_logical(root
, bh
,
548 goto failed_truncate
;
549 bh
= bh
->b_this_page
;
550 if (alloc_extent_start
)
551 alloc_extent_start
++;
552 } while (bh
!= head
);
554 WARN_ON(this_write
> write_bytes
);
555 write_bytes
-= this_write
;
560 btrfs_drop_pages(pages
, num_pages
);
564 btrfs_drop_pages(pages
, num_pages
);
566 vmtruncate(inode
, isize
);
570 mutex_unlock(&root
->fs_info
->fs_mutex
);
575 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
576 size_t count
, loff_t
*ppos
)
579 size_t num_written
= 0;
582 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
583 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
584 struct page
**pages
= NULL
;
586 struct page
*pinned
[2];
587 unsigned long first_index
;
588 unsigned long last_index
;
590 nrptrs
= min((count
+ PAGE_CACHE_SIZE
- 1) / PAGE_CACHE_SIZE
,
591 PAGE_CACHE_SIZE
/ (sizeof(struct page
*)));
594 if (file
->f_flags
& O_DIRECT
)
597 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
598 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
599 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
604 err
= remove_suid(file
->f_path
.dentry
);
607 file_update_time(file
);
609 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
611 mutex_lock(&inode
->i_mutex
);
612 first_index
= pos
>> PAGE_CACHE_SHIFT
;
613 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
616 * there are lots of better ways to do this, but this code
617 * makes sure the first and last page in the file range are
618 * up to date and ready for cow
620 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
621 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
622 if (!PageUptodate(pinned
[0])) {
623 ret
= btrfs_readpage(NULL
, pinned
[0]);
625 wait_on_page_locked(pinned
[0]);
627 unlock_page(pinned
[0]);
630 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
631 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
632 if (!PageUptodate(pinned
[1])) {
633 ret
= btrfs_readpage(NULL
, pinned
[1]);
635 wait_on_page_locked(pinned
[1]);
637 unlock_page(pinned
[1]);
642 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
643 size_t write_bytes
= min(count
, nrptrs
*
644 (size_t)PAGE_CACHE_SIZE
-
646 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
649 WARN_ON(num_pages
> nrptrs
);
650 memset(pages
, 0, sizeof(pages
));
651 ret
= prepare_pages(root
, file
, pages
, num_pages
,
652 pos
, first_index
, last_index
,
657 ret
= btrfs_copy_from_user(pos
, num_pages
,
658 write_bytes
, pages
, buf
);
660 btrfs_drop_pages(pages
, num_pages
);
664 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
665 num_pages
, pos
, write_bytes
);
666 btrfs_drop_pages(pages
, num_pages
);
671 count
-= write_bytes
;
673 num_written
+= write_bytes
;
675 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, num_pages
);
676 btrfs_btree_balance_dirty(root
);
679 mutex_unlock(&inode
->i_mutex
);
683 page_cache_release(pinned
[0]);
685 page_cache_release(pinned
[1]);
687 current
->backing_dev_info
= NULL
;
688 mark_inode_dirty(inode
);
689 return num_written
? num_written
: err
;
692 static int btrfs_sync_file(struct file
*file
,
693 struct dentry
*dentry
, int datasync
)
695 struct inode
*inode
= dentry
->d_inode
;
696 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
698 struct btrfs_trans_handle
*trans
;
701 * FIXME, use inode generation number to check if we can skip the
704 mutex_lock(&root
->fs_info
->fs_mutex
);
705 trans
= btrfs_start_transaction(root
, 1);
710 ret
= btrfs_commit_transaction(trans
, root
);
711 mutex_unlock(&root
->fs_info
->fs_mutex
);
713 return ret
> 0 ? EIO
: ret
;
716 static struct vm_operations_struct btrfs_file_vm_ops
= {
717 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
718 .nopage
= filemap_nopage
,
719 .populate
= filemap_populate
,
721 .fault
= filemap_fault
,
723 .page_mkwrite
= btrfs_page_mkwrite
,
726 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
728 vma
->vm_ops
= &btrfs_file_vm_ops
;
733 struct file_operations btrfs_file_operations
= {
734 .llseek
= generic_file_llseek
,
735 .read
= do_sync_read
,
736 .aio_read
= generic_file_aio_read
,
737 .write
= btrfs_file_write
,
738 .mmap
= btrfs_file_mmap
,
739 .open
= generic_file_open
,
740 .ioctl
= btrfs_ioctl
,
741 .fsync
= btrfs_sync_file
,
743 .compat_ioctl
= btrfs_compat_ioctl
,