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 bh
= page_buffers(pages
[i
]);
108 if (buffer_mapped(bh
) && bh
->b_blocknr
== 0) {
109 struct btrfs_key key
;
110 struct btrfs_path
*path
;
114 mutex_lock(&root
->fs_info
->fs_mutex
);
115 trans
= btrfs_start_transaction(root
, 1);
116 btrfs_set_trans_block_group(trans
, inode
);
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
);
139 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
140 btrfs_memcpy(root
, path
->nodes
[0]->b_data
,
141 ptr
, kaddr
+ bh_offset(bh
),
142 offset
+ write_bytes
);
143 kunmap_atomic(kaddr
, KM_USER0
);
145 mark_buffer_dirty(path
->nodes
[0]);
146 btrfs_free_path(path
);
147 ret
= btrfs_end_transaction(trans
, root
);
149 mutex_unlock(&root
->fs_info
->fs_mutex
);
152 ret
= btrfs_commit_write(file
, pages
[i
], offset
,
153 offset
+ this_write
);
159 WARN_ON(this_write
> write_bytes
);
160 write_bytes
-= this_write
;
167 * this is very complex, but the basic idea is to drop all extents
168 * in the range start - end. hint_block is filled in with a block number
169 * that would be a good hint to the block allocator for this file.
171 * If an extent intersects the range but is not entirely inside the range
172 * it is either truncated or split. Anything entirely inside the range
173 * is deleted from the tree.
175 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
,
176 struct btrfs_root
*root
, struct inode
*inode
,
177 u64 start
, u64 end
, u64
*hint_block
)
180 struct btrfs_key key
;
181 struct btrfs_leaf
*leaf
;
183 struct btrfs_file_extent_item
*extent
;
186 struct btrfs_file_extent_item old
;
187 struct btrfs_path
*path
;
188 u64 search_start
= start
;
194 path
= btrfs_alloc_path();
198 btrfs_release_path(root
, path
);
199 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
204 if (path
->slots
[0] == 0) {
216 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
217 slot
= path
->slots
[0];
219 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
220 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
223 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
) {
226 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
227 extent
= btrfs_item_ptr(leaf
, slot
,
228 struct btrfs_file_extent_item
);
229 found_type
= btrfs_file_extent_type(extent
);
230 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
231 extent_end
= key
.offset
+
232 (btrfs_file_extent_num_blocks(extent
) <<
235 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
237 extent_end
= key
.offset
+
238 btrfs_file_extent_inline_len(leaf
->items
+
242 extent_end
= search_start
;
245 /* we found nothing we can drop */
246 if ((!found_extent
&& !found_inline
) ||
247 search_start
>= extent_end
) {
250 nritems
= btrfs_header_nritems(
251 btrfs_buffer_header(path
->nodes
[0]));
252 if (slot
>= nritems
- 1) {
253 nextret
= btrfs_next_leaf(root
, path
);
262 /* FIXME, there's only one inline extent allowed right now */
264 u64 mask
= root
->blocksize
- 1;
265 search_start
= (extent_end
+ mask
) & ~mask
;
267 search_start
= extent_end
;
269 if (end
< extent_end
&& end
>= key
.offset
) {
272 btrfs_file_extent_disk_blocknr(extent
);
273 u64 disk_num_blocks
=
274 btrfs_file_extent_disk_num_blocks(extent
);
275 memcpy(&old
, extent
, sizeof(old
));
276 if (disk_blocknr
!= 0) {
277 ret
= btrfs_inc_extent_ref(trans
, root
,
278 disk_blocknr
, disk_num_blocks
);
282 WARN_ON(found_inline
);
285 /* truncate existing extent */
286 if (start
> key
.offset
) {
290 WARN_ON(start
& (root
->blocksize
- 1));
292 new_num
= (start
- key
.offset
) >>
294 old_num
= btrfs_file_extent_num_blocks(extent
);
296 btrfs_file_extent_disk_blocknr(extent
);
297 if (btrfs_file_extent_disk_blocknr(extent
)) {
299 (old_num
- new_num
) << 3;
301 btrfs_set_file_extent_num_blocks(extent
,
303 mark_buffer_dirty(path
->nodes
[0]);
308 /* delete the entire extent */
310 u64 disk_blocknr
= 0;
311 u64 disk_num_blocks
= 0;
312 u64 extent_num_blocks
= 0;
315 btrfs_file_extent_disk_blocknr(extent
);
317 btrfs_file_extent_disk_num_blocks(extent
);
319 btrfs_file_extent_num_blocks(extent
);
321 btrfs_file_extent_disk_blocknr(extent
);
323 ret
= btrfs_del_item(trans
, root
, path
);
325 btrfs_release_path(root
, path
);
327 if (found_extent
&& disk_blocknr
!= 0) {
328 inode
->i_blocks
-= extent_num_blocks
<< 3;
329 ret
= btrfs_free_extent(trans
, root
,
335 if (!bookend
&& search_start
>= end
) {
342 /* create bookend, splitting the extent in two */
343 if (bookend
&& found_extent
) {
344 struct btrfs_key ins
;
345 ins
.objectid
= inode
->i_ino
;
348 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
349 btrfs_release_path(root
, path
);
350 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
354 btrfs_print_leaf(root
, btrfs_buffer_leaf(path
->nodes
[0]));
355 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
);
358 extent
= btrfs_item_ptr(
359 btrfs_buffer_leaf(path
->nodes
[0]),
361 struct btrfs_file_extent_item
);
362 btrfs_set_file_extent_disk_blocknr(extent
,
363 btrfs_file_extent_disk_blocknr(&old
));
364 btrfs_set_file_extent_disk_num_blocks(extent
,
365 btrfs_file_extent_disk_num_blocks(&old
));
367 btrfs_set_file_extent_offset(extent
,
368 btrfs_file_extent_offset(&old
) +
369 ((end
- key
.offset
) >> inode
->i_blkbits
));
370 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
371 (extent_end
- end
) >> inode
->i_blkbits
);
372 btrfs_set_file_extent_num_blocks(extent
,
373 (extent_end
- end
) >> inode
->i_blkbits
);
375 btrfs_set_file_extent_type(extent
,
376 BTRFS_FILE_EXTENT_REG
);
377 btrfs_set_file_extent_generation(extent
,
378 btrfs_file_extent_generation(&old
));
379 btrfs_mark_buffer_dirty(path
->nodes
[0]);
380 if (btrfs_file_extent_disk_blocknr(&old
) != 0) {
382 btrfs_file_extent_num_blocks(extent
) << 3;
389 btrfs_free_path(path
);
394 * this gets pages into the page cache and locks them down
396 static int prepare_pages(struct btrfs_root
*root
,
401 unsigned long first_index
,
402 unsigned long last_index
,
406 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
407 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
411 struct buffer_head
*bh
;
412 struct buffer_head
*head
;
413 loff_t isize
= i_size_read(inode
);
414 struct btrfs_trans_handle
*trans
;
417 u64 alloc_extent_start
;
419 struct btrfs_key ins
;
421 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
422 num_blocks
= (write_bytes
+ pos
- start_pos
+ root
->blocksize
- 1) >>
425 memset(pages
, 0, num_pages
* sizeof(struct page
*));
427 for (i
= 0; i
< num_pages
; i
++) {
428 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
435 mutex_lock(&root
->fs_info
->fs_mutex
);
436 trans
= btrfs_start_transaction(root
, 1);
439 mutex_unlock(&root
->fs_info
->fs_mutex
);
442 btrfs_set_trans_block_group(trans
, inode
);
443 /* FIXME blocksize != 4096 */
444 inode
->i_blocks
+= num_blocks
<< 3;
447 /* FIXME...EIEIO, ENOSPC and more */
449 /* step one, delete the existing extents in this range */
450 /* FIXME blocksize != pagesize */
451 if (start_pos
< inode
->i_size
) {
452 err
= btrfs_drop_extents(trans
, root
, inode
,
453 start_pos
, (pos
+ write_bytes
+ root
->blocksize
-1) &
454 ~((u64
)root
->blocksize
- 1), &hint_block
);
458 /* insert any holes we need to create */
459 if (inode
->i_size
< start_pos
) {
460 u64 last_pos_in_file
;
462 u64 mask
= root
->blocksize
- 1;
463 last_pos_in_file
= (isize
+ mask
) & ~mask
;
464 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
465 hole_size
>>= inode
->i_blkbits
;
466 if (last_pos_in_file
< start_pos
) {
467 err
= btrfs_insert_file_extent(trans
, root
,
476 * either allocate an extent for the new bytes or setup the key
477 * to show we are doing inline data in the extent
479 if (isize
>= PAGE_CACHE_SIZE
|| pos
+ write_bytes
< inode
->i_size
||
480 pos
+ write_bytes
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
481 err
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
482 num_blocks
, hint_block
, (u64
)-1,
485 err
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
486 start_pos
, ins
.objectid
, ins
.offset
,
494 alloc_extent_start
= ins
.objectid
;
495 err
= btrfs_end_transaction(trans
, root
);
496 mutex_unlock(&root
->fs_info
->fs_mutex
);
498 for (i
= 0; i
< num_pages
; i
++) {
499 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
500 wait_on_page_writeback(pages
[i
]);
501 offset
= pos
& (PAGE_CACHE_SIZE
-1);
502 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
503 if (!page_has_buffers(pages
[i
])) {
504 create_empty_buffers(pages
[i
],
505 root
->fs_info
->sb
->s_blocksize
,
508 head
= page_buffers(pages
[i
]);
511 err
= btrfs_map_bh_to_logical(root
, bh
,
515 goto failed_truncate
;
516 bh
= bh
->b_this_page
;
517 if (alloc_extent_start
)
518 alloc_extent_start
++;
519 } while (bh
!= head
);
521 WARN_ON(this_write
> write_bytes
);
522 write_bytes
-= this_write
;
527 btrfs_drop_pages(pages
, num_pages
);
531 btrfs_drop_pages(pages
, num_pages
);
533 vmtruncate(inode
, isize
);
537 mutex_unlock(&root
->fs_info
->fs_mutex
);
542 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
543 size_t count
, loff_t
*ppos
)
546 size_t num_written
= 0;
549 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
550 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
551 struct page
**pages
= NULL
;
553 struct page
*pinned
[2];
554 unsigned long first_index
;
555 unsigned long last_index
;
557 nrptrs
= min((count
+ PAGE_CACHE_SIZE
- 1) / PAGE_CACHE_SIZE
,
558 PAGE_CACHE_SIZE
/ (sizeof(struct page
*)));
561 if (file
->f_flags
& O_DIRECT
)
564 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
565 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
566 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
571 err
= remove_suid(file
->f_path
.dentry
);
574 file_update_time(file
);
576 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
578 mutex_lock(&inode
->i_mutex
);
579 first_index
= pos
>> PAGE_CACHE_SHIFT
;
580 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
583 * there are lots of better ways to do this, but this code
584 * makes sure the first and last page in the file range are
585 * up to date and ready for cow
587 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
588 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
589 if (!PageUptodate(pinned
[0])) {
590 ret
= btrfs_readpage(NULL
, pinned
[0]);
592 wait_on_page_locked(pinned
[0]);
594 unlock_page(pinned
[0]);
597 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
598 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
599 if (!PageUptodate(pinned
[1])) {
600 ret
= btrfs_readpage(NULL
, pinned
[1]);
602 wait_on_page_locked(pinned
[1]);
604 unlock_page(pinned
[1]);
609 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
610 size_t write_bytes
= min(count
, nrptrs
*
611 (size_t)PAGE_CACHE_SIZE
-
613 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
616 WARN_ON(num_pages
> nrptrs
);
617 memset(pages
, 0, sizeof(pages
));
618 ret
= prepare_pages(root
, file
, pages
, num_pages
,
619 pos
, first_index
, last_index
,
623 ret
= btrfs_copy_from_user(pos
, num_pages
,
624 write_bytes
, pages
, buf
);
627 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
628 num_pages
, pos
, write_bytes
);
630 btrfs_drop_pages(pages
, num_pages
);
633 count
-= write_bytes
;
635 num_written
+= write_bytes
;
637 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, num_pages
);
638 btrfs_btree_balance_dirty(root
);
641 mutex_unlock(&inode
->i_mutex
);
645 page_cache_release(pinned
[0]);
647 page_cache_release(pinned
[1]);
649 current
->backing_dev_info
= NULL
;
650 mark_inode_dirty(inode
);
651 return num_written
? num_written
: err
;
654 static int btrfs_sync_file(struct file
*file
,
655 struct dentry
*dentry
, int datasync
)
657 struct inode
*inode
= dentry
->d_inode
;
658 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
660 struct btrfs_trans_handle
*trans
;
663 * FIXME, use inode generation number to check if we can skip the
666 mutex_lock(&root
->fs_info
->fs_mutex
);
667 trans
= btrfs_start_transaction(root
, 1);
672 ret
= btrfs_commit_transaction(trans
, root
);
673 mutex_unlock(&root
->fs_info
->fs_mutex
);
675 return ret
> 0 ? EIO
: ret
;
678 static struct vm_operations_struct btrfs_file_vm_ops
= {
679 .nopage
= filemap_nopage
,
680 .populate
= filemap_populate
,
681 .page_mkwrite
= btrfs_page_mkwrite
,
684 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
686 vma
->vm_ops
= &btrfs_file_vm_ops
;
691 struct file_operations btrfs_file_operations
= {
692 .llseek
= generic_file_llseek
,
693 .read
= do_sync_read
,
694 .aio_read
= generic_file_aio_read
,
695 .write
= btrfs_file_write
,
696 .mmap
= btrfs_file_mmap
,
697 .open
= generic_file_open
,
698 .ioctl
= btrfs_ioctl
,
699 .fsync
= btrfs_sync_file
,
701 .compat_ioctl
= btrfs_compat_ioctl
,