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) {
215 leaf
= btrfs_buffer_leaf(path
->nodes
[0]);
216 slot
= path
->slots
[0];
217 btrfs_disk_key_to_cpu(&key
, &leaf
->items
[slot
].key
);
218 if (key
.offset
>= end
|| key
.objectid
!= inode
->i_ino
) {
222 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
) {
226 extent
= btrfs_item_ptr(leaf
, slot
,
227 struct btrfs_file_extent_item
);
228 found_type
= btrfs_file_extent_type(extent
);
229 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
230 extent_end
= key
.offset
+
231 (btrfs_file_extent_num_blocks(extent
) <<
234 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
236 extent_end
= key
.offset
+
237 btrfs_file_extent_inline_len(leaf
->items
+ slot
);
240 /* we found nothing we can drop */
241 if (!found_extent
&& !found_inline
) {
246 /* we found nothing inside the range */
247 if (search_start
>= extent_end
) {
252 /* FIXME, there's only one inline extent allowed right now */
254 u64 mask
= root
->blocksize
- 1;
255 search_start
= (extent_end
+ mask
) & ~mask
;
257 search_start
= extent_end
;
259 if (end
< extent_end
&& end
>= key
.offset
) {
262 btrfs_file_extent_disk_blocknr(extent
);
263 u64 disk_num_blocks
=
264 btrfs_file_extent_disk_num_blocks(extent
);
265 memcpy(&old
, extent
, sizeof(old
));
266 if (disk_blocknr
!= 0) {
267 ret
= btrfs_inc_extent_ref(trans
, root
,
268 disk_blocknr
, disk_num_blocks
);
272 WARN_ON(found_inline
);
276 /* truncate existing extent */
277 if (start
> key
.offset
) {
281 WARN_ON(start
& (root
->blocksize
- 1));
283 new_num
= (start
- key
.offset
) >>
285 old_num
= btrfs_file_extent_num_blocks(extent
);
287 btrfs_file_extent_disk_blocknr(extent
);
288 if (btrfs_file_extent_disk_blocknr(extent
)) {
290 (old_num
- new_num
) << 3;
292 btrfs_set_file_extent_num_blocks(extent
,
294 mark_buffer_dirty(path
->nodes
[0]);
299 /* delete the entire extent */
301 u64 disk_blocknr
= 0;
302 u64 disk_num_blocks
= 0;
303 u64 extent_num_blocks
= 0;
306 btrfs_file_extent_disk_blocknr(extent
);
308 btrfs_file_extent_disk_num_blocks(extent
);
310 btrfs_file_extent_num_blocks(extent
);
312 btrfs_file_extent_disk_blocknr(extent
);
314 ret
= btrfs_del_item(trans
, root
, path
);
316 btrfs_release_path(root
, path
);
318 if (found_extent
&& disk_blocknr
!= 0) {
319 inode
->i_blocks
-= extent_num_blocks
<< 3;
320 ret
= btrfs_free_extent(trans
, root
,
326 if (!bookend
&& search_start
>= end
) {
333 /* create bookend, splitting the extent in two */
334 if (bookend
&& found_extent
) {
335 struct btrfs_key ins
;
336 ins
.objectid
= inode
->i_ino
;
339 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
341 btrfs_release_path(root
, path
);
342 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
345 extent
= btrfs_item_ptr(
346 btrfs_buffer_leaf(path
->nodes
[0]),
348 struct btrfs_file_extent_item
);
349 btrfs_set_file_extent_disk_blocknr(extent
,
350 btrfs_file_extent_disk_blocknr(&old
));
351 btrfs_set_file_extent_disk_num_blocks(extent
,
352 btrfs_file_extent_disk_num_blocks(&old
));
354 btrfs_set_file_extent_offset(extent
,
355 btrfs_file_extent_offset(&old
) +
356 ((end
- key
.offset
) >> inode
->i_blkbits
));
357 WARN_ON(btrfs_file_extent_num_blocks(&old
) <
358 (extent_end
- end
) >> inode
->i_blkbits
);
359 btrfs_set_file_extent_num_blocks(extent
,
360 (extent_end
- end
) >> inode
->i_blkbits
);
362 btrfs_set_file_extent_type(extent
,
363 BTRFS_FILE_EXTENT_REG
);
364 btrfs_set_file_extent_generation(extent
,
365 btrfs_file_extent_generation(&old
));
366 btrfs_mark_buffer_dirty(path
->nodes
[0]);
367 if (btrfs_file_extent_disk_blocknr(&old
) != 0) {
369 btrfs_file_extent_num_blocks(extent
) << 3;
376 btrfs_free_path(path
);
381 * this gets pages into the page cache and locks them down
383 static int prepare_pages(struct btrfs_root
*root
,
388 unsigned long first_index
,
389 unsigned long last_index
,
391 u64 alloc_extent_start
)
394 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
395 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
399 struct buffer_head
*bh
;
400 struct buffer_head
*head
;
401 loff_t isize
= i_size_read(inode
);
403 memset(pages
, 0, num_pages
* sizeof(struct page
*));
405 for (i
= 0; i
< num_pages
; i
++) {
406 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
411 cancel_dirty_page(pages
[i
], PAGE_CACHE_SIZE
);
412 wait_on_page_writeback(pages
[i
]);
413 offset
= pos
& (PAGE_CACHE_SIZE
-1);
414 this_write
= min((size_t)PAGE_CACHE_SIZE
- offset
, write_bytes
);
415 if (!page_has_buffers(pages
[i
])) {
416 create_empty_buffers(pages
[i
],
417 root
->fs_info
->sb
->s_blocksize
,
420 head
= page_buffers(pages
[i
]);
423 err
= btrfs_map_bh_to_logical(root
, bh
,
427 goto failed_truncate
;
428 bh
= bh
->b_this_page
;
429 if (alloc_extent_start
)
430 alloc_extent_start
++;
431 } while (bh
!= head
);
433 WARN_ON(this_write
> write_bytes
);
434 write_bytes
-= this_write
;
439 btrfs_drop_pages(pages
, num_pages
);
443 btrfs_drop_pages(pages
, num_pages
);
445 vmtruncate(inode
, isize
);
449 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
450 size_t count
, loff_t
*ppos
)
453 size_t num_written
= 0;
456 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
457 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
458 struct page
*pages
[8];
459 struct page
*pinned
[2];
460 unsigned long first_index
;
461 unsigned long last_index
;
464 u64 alloc_extent_start
;
466 struct btrfs_trans_handle
*trans
;
467 struct btrfs_key ins
;
470 if (file
->f_flags
& O_DIRECT
)
473 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
474 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
475 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
480 err
= remove_suid(file
->f_path
.dentry
);
483 file_update_time(file
);
485 start_pos
= pos
& ~((u64
)PAGE_CACHE_SIZE
- 1);
486 num_blocks
= (count
+ pos
- start_pos
+ root
->blocksize
- 1) >>
489 mutex_lock(&inode
->i_mutex
);
490 first_index
= pos
>> PAGE_CACHE_SHIFT
;
491 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
494 * there are lots of better ways to do this, but this code
495 * makes sure the first and last page in the file range are
496 * up to date and ready for cow
498 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
499 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
500 if (!PageUptodate(pinned
[0])) {
501 ret
= btrfs_readpage(NULL
, pinned
[0]);
503 wait_on_page_locked(pinned
[0]);
505 unlock_page(pinned
[0]);
508 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
509 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
510 if (!PageUptodate(pinned
[1])) {
511 ret
= btrfs_readpage(NULL
, pinned
[1]);
513 wait_on_page_locked(pinned
[1]);
515 unlock_page(pinned
[1]);
519 mutex_lock(&root
->fs_info
->fs_mutex
);
520 trans
= btrfs_start_transaction(root
, 1);
523 mutex_unlock(&root
->fs_info
->fs_mutex
);
526 btrfs_set_trans_block_group(trans
, inode
);
527 /* FIXME blocksize != 4096 */
528 inode
->i_blocks
+= num_blocks
<< 3;
531 /* FIXME...EIEIO, ENOSPC and more */
533 /* step one, delete the existing extents in this range */
534 if (start_pos
< inode
->i_size
) {
535 /* FIXME blocksize != pagesize */
536 ret
= btrfs_drop_extents(trans
, root
, inode
,
538 (pos
+ count
+ root
->blocksize
-1) &
539 ~((u64
)root
->blocksize
- 1),
544 /* insert any holes we need to create */
545 if (inode
->i_size
< start_pos
) {
546 u64 last_pos_in_file
;
548 u64 mask
= root
->blocksize
- 1;
549 last_pos_in_file
= (inode
->i_size
+ mask
) & ~mask
;
550 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
551 hole_size
>>= inode
->i_blkbits
;
552 if (last_pos_in_file
< start_pos
) {
553 ret
= btrfs_insert_file_extent(trans
, root
,
562 * either allocate an extent for the new bytes or setup the key
563 * to show we are doing inline data in the extent
565 if (inode
->i_size
>= PAGE_CACHE_SIZE
|| pos
+ count
< inode
->i_size
||
566 pos
+ count
- start_pos
> BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
567 ret
= btrfs_alloc_extent(trans
, root
, inode
->i_ino
,
568 num_blocks
, hint_block
, (u64
)-1,
571 ret
= btrfs_insert_file_extent(trans
, root
, inode
->i_ino
,
572 start_pos
, ins
.objectid
, ins
.offset
,
580 alloc_extent_start
= ins
.objectid
;
581 ret
= btrfs_end_transaction(trans
, root
);
582 mutex_unlock(&root
->fs_info
->fs_mutex
);
585 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
586 size_t write_bytes
= min(count
,
587 (size_t)PAGE_CACHE_SIZE
- offset
);
588 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
591 memset(pages
, 0, sizeof(pages
));
592 ret
= prepare_pages(root
, file
, pages
, num_pages
,
593 pos
, first_index
, last_index
,
594 write_bytes
, alloc_extent_start
);
597 /* FIXME blocks != pagesize */
598 if (alloc_extent_start
)
599 alloc_extent_start
+= num_pages
;
600 ret
= btrfs_copy_from_user(pos
, num_pages
,
601 write_bytes
, pages
, buf
);
604 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
605 num_pages
, pos
, write_bytes
);
607 btrfs_drop_pages(pages
, num_pages
);
610 count
-= write_bytes
;
612 num_written
+= write_bytes
;
614 balance_dirty_pages_ratelimited(inode
->i_mapping
);
615 btrfs_btree_balance_dirty(root
);
619 mutex_unlock(&inode
->i_mutex
);
622 page_cache_release(pinned
[0]);
624 page_cache_release(pinned
[1]);
626 current
->backing_dev_info
= NULL
;
627 mark_inode_dirty(inode
);
628 return num_written
? num_written
: err
;
631 static int btrfs_sync_file(struct file
*file
,
632 struct dentry
*dentry
, int datasync
)
634 struct inode
*inode
= dentry
->d_inode
;
635 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
637 struct btrfs_trans_handle
*trans
;
640 * FIXME, use inode generation number to check if we can skip the
643 mutex_lock(&root
->fs_info
->fs_mutex
);
644 trans
= btrfs_start_transaction(root
, 1);
649 ret
= btrfs_commit_transaction(trans
, root
);
650 mutex_unlock(&root
->fs_info
->fs_mutex
);
652 return ret
> 0 ? EIO
: ret
;
655 static struct vm_operations_struct btrfs_file_vm_ops
= {
656 .nopage
= filemap_nopage
,
657 .populate
= filemap_populate
,
658 .page_mkwrite
= btrfs_page_mkwrite
,
661 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
663 vma
->vm_ops
= &btrfs_file_vm_ops
;
668 struct file_operations btrfs_file_operations
= {
669 .llseek
= generic_file_llseek
,
670 .read
= do_sync_read
,
671 .aio_read
= generic_file_aio_read
,
672 .write
= btrfs_file_write
,
673 .mmap
= btrfs_file_mmap
,
674 .open
= generic_file_open
,
675 .ioctl
= btrfs_ioctl
,
676 .fsync
= btrfs_sync_file
,
678 .compat_ioctl
= btrfs_compat_ioctl
,