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.
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/time.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/backing-dev.h>
26 #include <linux/mpage.h>
27 #include <linux/swap.h>
28 #include <linux/writeback.h>
29 #include <linux/statfs.h>
30 #include <linux/compat.h>
31 #include <linux/slab.h>
34 #include "transaction.h"
35 #include "btrfs_inode.h"
37 #include "print-tree.h"
43 /* simple helper to fault in pages and copy. This should go away
44 * and be replaced with calls into generic code.
46 static noinline
int btrfs_copy_from_user(loff_t pos
, int num_pages
,
48 struct page
**prepared_pages
,
49 const char __user
*buf
)
53 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
55 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
56 size_t count
= min_t(size_t,
57 PAGE_CACHE_SIZE
- offset
, write_bytes
);
58 struct page
*page
= prepared_pages
[i
];
59 fault_in_pages_readable(buf
, count
);
61 /* Copy data from userspace to the current page */
63 page_fault
= __copy_from_user(page_address(page
) + offset
,
65 /* Flush processor's dcache for this page */
66 flush_dcache_page(page
);
74 return page_fault
? -EFAULT
: 0;
78 * unlocks pages after btrfs_file_write is done with them
80 static noinline
void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
83 for (i
= 0; i
< num_pages
; i
++) {
86 /* page checked is some magic around finding pages that
87 * have been modified without going through btrfs_set_page_dirty
90 ClearPageChecked(pages
[i
]);
91 unlock_page(pages
[i
]);
92 mark_page_accessed(pages
[i
]);
93 page_cache_release(pages
[i
]);
98 * after copy_from_user, pages need to be dirtied and we need to make
99 * sure holes are created between the current EOF and the start of
100 * any next extents (if required).
102 * this also makes the decision about creating an inline extent vs
103 * doing real data extents, marking pages dirty and delalloc as required.
105 static noinline
int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
106 struct btrfs_root
*root
,
115 struct inode
*inode
= fdentry(file
)->d_inode
;
118 u64 end_of_last_block
;
119 u64 end_pos
= pos
+ write_bytes
;
120 loff_t isize
= i_size_read(inode
);
122 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
123 num_bytes
= (write_bytes
+ pos
- start_pos
+
124 root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
126 end_of_last_block
= start_pos
+ num_bytes
- 1;
127 err
= btrfs_set_extent_delalloc(inode
, start_pos
, end_of_last_block
,
131 for (i
= 0; i
< num_pages
; i
++) {
132 struct page
*p
= pages
[i
];
137 if (end_pos
> isize
) {
138 i_size_write(inode
, end_pos
);
139 /* we've only changed i_size in ram, and we haven't updated
140 * the disk i_size. There is no need to log the inode
148 * this drops all the extents in the cache that intersect the range
149 * [start, end]. Existing extents are split as required.
151 int btrfs_drop_extent_cache(struct inode
*inode
, u64 start
, u64 end
,
154 struct extent_map
*em
;
155 struct extent_map
*split
= NULL
;
156 struct extent_map
*split2
= NULL
;
157 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
158 u64 len
= end
- start
+ 1;
164 WARN_ON(end
< start
);
165 if (end
== (u64
)-1) {
171 split
= alloc_extent_map(GFP_NOFS
);
173 split2
= alloc_extent_map(GFP_NOFS
);
175 write_lock(&em_tree
->lock
);
176 em
= lookup_extent_mapping(em_tree
, start
, len
);
178 write_unlock(&em_tree
->lock
);
182 if (skip_pinned
&& test_bit(EXTENT_FLAG_PINNED
, &em
->flags
)) {
183 if (testend
&& em
->start
+ em
->len
>= start
+ len
) {
185 write_unlock(&em_tree
->lock
);
188 start
= em
->start
+ em
->len
;
190 len
= start
+ len
- (em
->start
+ em
->len
);
192 write_unlock(&em_tree
->lock
);
195 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
196 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
197 remove_extent_mapping(em_tree
, em
);
199 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
201 split
->start
= em
->start
;
202 split
->len
= start
- em
->start
;
203 split
->orig_start
= em
->orig_start
;
204 split
->block_start
= em
->block_start
;
207 split
->block_len
= em
->block_len
;
209 split
->block_len
= split
->len
;
211 split
->bdev
= em
->bdev
;
212 split
->flags
= flags
;
213 ret
= add_extent_mapping(em_tree
, split
);
215 free_extent_map(split
);
219 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
220 testend
&& em
->start
+ em
->len
> start
+ len
) {
221 u64 diff
= start
+ len
- em
->start
;
223 split
->start
= start
+ len
;
224 split
->len
= em
->start
+ em
->len
- (start
+ len
);
225 split
->bdev
= em
->bdev
;
226 split
->flags
= flags
;
229 split
->block_len
= em
->block_len
;
230 split
->block_start
= em
->block_start
;
231 split
->orig_start
= em
->orig_start
;
233 split
->block_len
= split
->len
;
234 split
->block_start
= em
->block_start
+ diff
;
235 split
->orig_start
= split
->start
;
238 ret
= add_extent_mapping(em_tree
, split
);
240 free_extent_map(split
);
243 write_unlock(&em_tree
->lock
);
247 /* once for the tree*/
251 free_extent_map(split
);
253 free_extent_map(split2
);
258 * this is very complex, but the basic idea is to drop all extents
259 * in the range start - end. hint_block is filled in with a block number
260 * that would be a good hint to the block allocator for this file.
262 * If an extent intersects the range but is not entirely inside the range
263 * it is either truncated or split. Anything entirely inside the range
264 * is deleted from the tree.
266 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
, struct inode
*inode
,
267 u64 start
, u64 end
, u64
*hint_byte
, int drop_cache
)
269 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
270 struct extent_buffer
*leaf
;
271 struct btrfs_file_extent_item
*fi
;
272 struct btrfs_path
*path
;
273 struct btrfs_key key
;
274 struct btrfs_key new_key
;
275 u64 search_start
= start
;
278 u64 extent_offset
= 0;
287 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
289 path
= btrfs_alloc_path();
295 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
299 if (ret
> 0 && path
->slots
[0] > 0 && search_start
== start
) {
300 leaf
= path
->nodes
[0];
301 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0] - 1);
302 if (key
.objectid
== inode
->i_ino
&&
303 key
.type
== BTRFS_EXTENT_DATA_KEY
)
308 leaf
= path
->nodes
[0];
309 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
311 ret
= btrfs_next_leaf(root
, path
);
318 leaf
= path
->nodes
[0];
322 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
323 if (key
.objectid
> inode
->i_ino
||
324 key
.type
> BTRFS_EXTENT_DATA_KEY
|| key
.offset
>= end
)
327 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
328 struct btrfs_file_extent_item
);
329 extent_type
= btrfs_file_extent_type(leaf
, fi
);
331 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
332 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
333 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
334 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
335 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
336 extent_end
= key
.offset
+
337 btrfs_file_extent_num_bytes(leaf
, fi
);
338 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
339 extent_end
= key
.offset
+
340 btrfs_file_extent_inline_len(leaf
, fi
);
343 extent_end
= search_start
;
346 if (extent_end
<= search_start
) {
351 search_start
= max(key
.offset
, start
);
353 btrfs_release_path(root
, path
);
358 * | - range to drop - |
359 * | -------- extent -------- |
361 if (start
> key
.offset
&& end
< extent_end
) {
363 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
365 memcpy(&new_key
, &key
, sizeof(new_key
));
366 new_key
.offset
= start
;
367 ret
= btrfs_duplicate_item(trans
, root
, path
,
369 if (ret
== -EAGAIN
) {
370 btrfs_release_path(root
, path
);
376 leaf
= path
->nodes
[0];
377 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
378 struct btrfs_file_extent_item
);
379 btrfs_set_file_extent_num_bytes(leaf
, fi
,
382 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
383 struct btrfs_file_extent_item
);
385 extent_offset
+= start
- key
.offset
;
386 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
387 btrfs_set_file_extent_num_bytes(leaf
, fi
,
389 btrfs_mark_buffer_dirty(leaf
);
391 if (disk_bytenr
> 0) {
392 ret
= btrfs_inc_extent_ref(trans
, root
,
393 disk_bytenr
, num_bytes
, 0,
394 root
->root_key
.objectid
,
396 start
- extent_offset
);
398 *hint_byte
= disk_bytenr
;
403 * | ---- range to drop ----- |
404 * | -------- extent -------- |
406 if (start
<= key
.offset
&& end
< extent_end
) {
407 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
409 memcpy(&new_key
, &key
, sizeof(new_key
));
410 new_key
.offset
= end
;
411 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
413 extent_offset
+= end
- key
.offset
;
414 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
415 btrfs_set_file_extent_num_bytes(leaf
, fi
,
417 btrfs_mark_buffer_dirty(leaf
);
418 if (disk_bytenr
> 0) {
419 inode_sub_bytes(inode
, end
- key
.offset
);
420 *hint_byte
= disk_bytenr
;
425 search_start
= extent_end
;
427 * | ---- range to drop ----- |
428 * | -------- extent -------- |
430 if (start
> key
.offset
&& end
>= extent_end
) {
432 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
434 btrfs_set_file_extent_num_bytes(leaf
, fi
,
436 btrfs_mark_buffer_dirty(leaf
);
437 if (disk_bytenr
> 0) {
438 inode_sub_bytes(inode
, extent_end
- start
);
439 *hint_byte
= disk_bytenr
;
441 if (end
== extent_end
)
449 * | ---- range to drop ----- |
450 * | ------ extent ------ |
452 if (start
<= key
.offset
&& end
>= extent_end
) {
454 del_slot
= path
->slots
[0];
457 BUG_ON(del_slot
+ del_nr
!= path
->slots
[0]);
461 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
462 inode_sub_bytes(inode
,
463 extent_end
- key
.offset
);
464 extent_end
= ALIGN(extent_end
,
466 } else if (disk_bytenr
> 0) {
467 ret
= btrfs_free_extent(trans
, root
,
468 disk_bytenr
, num_bytes
, 0,
469 root
->root_key
.objectid
,
470 key
.objectid
, key
.offset
-
473 inode_sub_bytes(inode
,
474 extent_end
- key
.offset
);
475 *hint_byte
= disk_bytenr
;
478 if (end
== extent_end
)
481 if (path
->slots
[0] + 1 < btrfs_header_nritems(leaf
)) {
486 ret
= btrfs_del_items(trans
, root
, path
, del_slot
,
493 btrfs_release_path(root
, path
);
501 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
505 btrfs_free_path(path
);
509 static int extent_mergeable(struct extent_buffer
*leaf
, int slot
,
510 u64 objectid
, u64 bytenr
, u64 orig_offset
,
511 u64
*start
, u64
*end
)
513 struct btrfs_file_extent_item
*fi
;
514 struct btrfs_key key
;
517 if (slot
< 0 || slot
>= btrfs_header_nritems(leaf
))
520 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
521 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
524 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
525 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
||
526 btrfs_file_extent_disk_bytenr(leaf
, fi
) != bytenr
||
527 btrfs_file_extent_offset(leaf
, fi
) != key
.offset
- orig_offset
||
528 btrfs_file_extent_compression(leaf
, fi
) ||
529 btrfs_file_extent_encryption(leaf
, fi
) ||
530 btrfs_file_extent_other_encoding(leaf
, fi
))
533 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
534 if ((*start
&& *start
!= key
.offset
) || (*end
&& *end
!= extent_end
))
543 * Mark extent in the range start - end as written.
545 * This changes extent type from 'pre-allocated' to 'regular'. If only
546 * part of extent is marked as written, the extent will be split into
549 int btrfs_mark_extent_written(struct btrfs_trans_handle
*trans
,
550 struct inode
*inode
, u64 start
, u64 end
)
552 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
553 struct extent_buffer
*leaf
;
554 struct btrfs_path
*path
;
555 struct btrfs_file_extent_item
*fi
;
556 struct btrfs_key key
;
557 struct btrfs_key new_key
;
570 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
572 path
= btrfs_alloc_path();
577 key
.objectid
= inode
->i_ino
;
578 key
.type
= BTRFS_EXTENT_DATA_KEY
;
581 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
582 if (ret
> 0 && path
->slots
[0] > 0)
585 leaf
= path
->nodes
[0];
586 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
587 BUG_ON(key
.objectid
!= inode
->i_ino
||
588 key
.type
!= BTRFS_EXTENT_DATA_KEY
);
589 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
590 struct btrfs_file_extent_item
);
591 BUG_ON(btrfs_file_extent_type(leaf
, fi
) !=
592 BTRFS_FILE_EXTENT_PREALLOC
);
593 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
594 BUG_ON(key
.offset
> start
|| extent_end
< end
);
596 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
597 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
598 orig_offset
= key
.offset
- btrfs_file_extent_offset(leaf
, fi
);
599 memcpy(&new_key
, &key
, sizeof(new_key
));
601 if (start
== key
.offset
&& end
< extent_end
) {
604 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
605 inode
->i_ino
, bytenr
, orig_offset
,
606 &other_start
, &other_end
)) {
607 new_key
.offset
= end
;
608 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
609 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
610 struct btrfs_file_extent_item
);
611 btrfs_set_file_extent_num_bytes(leaf
, fi
,
613 btrfs_set_file_extent_offset(leaf
, fi
,
615 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
616 struct btrfs_file_extent_item
);
617 btrfs_set_file_extent_num_bytes(leaf
, fi
,
619 btrfs_mark_buffer_dirty(leaf
);
624 if (start
> key
.offset
&& end
== extent_end
) {
627 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
628 inode
->i_ino
, bytenr
, orig_offset
,
629 &other_start
, &other_end
)) {
630 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
631 struct btrfs_file_extent_item
);
632 btrfs_set_file_extent_num_bytes(leaf
, fi
,
635 new_key
.offset
= start
;
636 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
638 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
639 struct btrfs_file_extent_item
);
640 btrfs_set_file_extent_num_bytes(leaf
, fi
,
642 btrfs_set_file_extent_offset(leaf
, fi
,
643 start
- orig_offset
);
644 btrfs_mark_buffer_dirty(leaf
);
649 while (start
> key
.offset
|| end
< extent_end
) {
650 if (key
.offset
== start
)
653 new_key
.offset
= split
;
654 ret
= btrfs_duplicate_item(trans
, root
, path
, &new_key
);
655 if (ret
== -EAGAIN
) {
656 btrfs_release_path(root
, path
);
661 leaf
= path
->nodes
[0];
662 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
663 struct btrfs_file_extent_item
);
664 btrfs_set_file_extent_num_bytes(leaf
, fi
,
667 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
668 struct btrfs_file_extent_item
);
670 btrfs_set_file_extent_offset(leaf
, fi
, split
- orig_offset
);
671 btrfs_set_file_extent_num_bytes(leaf
, fi
,
673 btrfs_mark_buffer_dirty(leaf
);
675 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
, num_bytes
, 0,
676 root
->root_key
.objectid
,
677 inode
->i_ino
, orig_offset
);
680 if (split
== start
) {
683 BUG_ON(start
!= key
.offset
);
692 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
693 inode
->i_ino
, bytenr
, orig_offset
,
694 &other_start
, &other_end
)) {
696 btrfs_release_path(root
, path
);
699 extent_end
= other_end
;
700 del_slot
= path
->slots
[0] + 1;
702 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
703 0, root
->root_key
.objectid
,
704 inode
->i_ino
, orig_offset
);
709 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
710 inode
->i_ino
, bytenr
, orig_offset
,
711 &other_start
, &other_end
)) {
713 btrfs_release_path(root
, path
);
716 key
.offset
= other_start
;
717 del_slot
= path
->slots
[0];
719 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
720 0, root
->root_key
.objectid
,
721 inode
->i_ino
, orig_offset
);
725 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
726 struct btrfs_file_extent_item
);
727 btrfs_set_file_extent_type(leaf
, fi
,
728 BTRFS_FILE_EXTENT_REG
);
729 btrfs_mark_buffer_dirty(leaf
);
731 fi
= btrfs_item_ptr(leaf
, del_slot
- 1,
732 struct btrfs_file_extent_item
);
733 btrfs_set_file_extent_type(leaf
, fi
,
734 BTRFS_FILE_EXTENT_REG
);
735 btrfs_set_file_extent_num_bytes(leaf
, fi
,
736 extent_end
- key
.offset
);
737 btrfs_mark_buffer_dirty(leaf
);
739 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
743 btrfs_free_path(path
);
748 * this gets pages into the page cache and locks them down, it also properly
749 * waits for data=ordered extents to finish before allowing the pages to be
752 static noinline
int prepare_pages(struct btrfs_root
*root
, struct file
*file
,
753 struct page
**pages
, size_t num_pages
,
754 loff_t pos
, unsigned long first_index
,
755 unsigned long last_index
, size_t write_bytes
)
757 struct extent_state
*cached_state
= NULL
;
759 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
760 struct inode
*inode
= fdentry(file
)->d_inode
;
765 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
766 last_pos
= ((u64
)index
+ num_pages
) << PAGE_CACHE_SHIFT
;
768 if (start_pos
> inode
->i_size
) {
769 err
= btrfs_cont_expand(inode
, start_pos
);
774 memset(pages
, 0, num_pages
* sizeof(struct page
*));
776 for (i
= 0; i
< num_pages
; i
++) {
777 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
782 wait_on_page_writeback(pages
[i
]);
784 if (start_pos
< inode
->i_size
) {
785 struct btrfs_ordered_extent
*ordered
;
786 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
787 start_pos
, last_pos
- 1, 0, &cached_state
,
789 ordered
= btrfs_lookup_first_ordered_extent(inode
,
792 ordered
->file_offset
+ ordered
->len
> start_pos
&&
793 ordered
->file_offset
< last_pos
) {
794 btrfs_put_ordered_extent(ordered
);
795 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
796 start_pos
, last_pos
- 1,
797 &cached_state
, GFP_NOFS
);
798 for (i
= 0; i
< num_pages
; i
++) {
799 unlock_page(pages
[i
]);
800 page_cache_release(pages
[i
]);
802 btrfs_wait_ordered_range(inode
, start_pos
,
803 last_pos
- start_pos
);
807 btrfs_put_ordered_extent(ordered
);
809 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start_pos
,
810 last_pos
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
811 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
813 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
814 start_pos
, last_pos
- 1, &cached_state
,
817 for (i
= 0; i
< num_pages
; i
++) {
818 clear_page_dirty_for_io(pages
[i
]);
819 set_page_extent_mapped(pages
[i
]);
820 WARN_ON(!PageLocked(pages
[i
]));
825 /* Copied from read-write.c */
826 static void wait_on_retry_sync_kiocb(struct kiocb
*iocb
)
828 set_current_state(TASK_UNINTERRUPTIBLE
);
829 if (!kiocbIsKicked(iocb
))
832 kiocbClearKicked(iocb
);
833 __set_current_state(TASK_RUNNING
);
837 * Just a copy of what do_sync_write does.
839 static ssize_t
__btrfs_direct_write(struct file
*file
, const char __user
*buf
,
840 size_t count
, loff_t pos
, loff_t
*ppos
)
842 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
843 unsigned long nr_segs
= 1;
847 init_sync_kiocb(&kiocb
, file
);
849 kiocb
.ki_left
= count
;
850 kiocb
.ki_nbytes
= count
;
853 ret
= generic_file_direct_write(&kiocb
, &iov
, &nr_segs
, pos
,
855 if (ret
!= -EIOCBRETRY
)
857 wait_on_retry_sync_kiocb(&kiocb
);
860 if (ret
== -EIOCBQUEUED
)
861 ret
= wait_on_sync_kiocb(&kiocb
);
862 *ppos
= kiocb
.ki_pos
;
866 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
867 size_t count
, loff_t
*ppos
)
871 ssize_t num_written
= 0;
874 struct inode
*inode
= fdentry(file
)->d_inode
;
875 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
876 struct page
**pages
= NULL
;
878 struct page
*pinned
[2];
879 unsigned long first_index
;
880 unsigned long last_index
;
884 will_write
= ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
) ||
885 (file
->f_flags
& O_DIRECT
));
893 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
895 mutex_lock(&inode
->i_mutex
);
897 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
898 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
905 err
= file_remove_suid(file
);
909 file_update_time(file
);
910 BTRFS_I(inode
)->sequence
++;
912 if (unlikely(file
->f_flags
& O_DIRECT
)) {
913 num_written
= __btrfs_direct_write(file
, buf
, count
, pos
,
916 count
-= num_written
;
918 /* We've written everything we wanted to, exit */
919 if (num_written
< 0 || !count
)
923 * We are going to do buffered for the rest of the range, so we
924 * need to make sure to invalidate the buffered pages when we're
931 nrptrs
= min((count
+ PAGE_CACHE_SIZE
- 1) / PAGE_CACHE_SIZE
,
932 PAGE_CACHE_SIZE
/ (sizeof(struct page
*)));
933 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
935 /* generic_write_checks can change our pos */
938 first_index
= pos
>> PAGE_CACHE_SHIFT
;
939 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
942 * there are lots of better ways to do this, but this code
943 * makes sure the first and last page in the file range are
944 * up to date and ready for cow
946 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
947 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
948 if (!PageUptodate(pinned
[0])) {
949 ret
= btrfs_readpage(NULL
, pinned
[0]);
951 wait_on_page_locked(pinned
[0]);
953 unlock_page(pinned
[0]);
956 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
957 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
958 if (!PageUptodate(pinned
[1])) {
959 ret
= btrfs_readpage(NULL
, pinned
[1]);
961 wait_on_page_locked(pinned
[1]);
963 unlock_page(pinned
[1]);
968 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
969 size_t write_bytes
= min(count
, nrptrs
*
970 (size_t)PAGE_CACHE_SIZE
-
972 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
975 WARN_ON(num_pages
> nrptrs
);
976 memset(pages
, 0, sizeof(struct page
*) * nrptrs
);
978 ret
= btrfs_delalloc_reserve_space(inode
, write_bytes
);
982 ret
= prepare_pages(root
, file
, pages
, num_pages
,
983 pos
, first_index
, last_index
,
986 btrfs_delalloc_release_space(inode
, write_bytes
);
990 ret
= btrfs_copy_from_user(pos
, num_pages
,
991 write_bytes
, pages
, buf
);
993 dirty_and_release_pages(NULL
, root
, file
, pages
,
994 num_pages
, pos
, write_bytes
);
997 btrfs_drop_pages(pages
, num_pages
);
999 btrfs_delalloc_release_space(inode
, write_bytes
);
1004 filemap_fdatawrite_range(inode
->i_mapping
, pos
,
1005 pos
+ write_bytes
- 1);
1007 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
,
1010 (root
->leafsize
>> PAGE_CACHE_SHIFT
) + 1)
1011 btrfs_btree_balance_dirty(root
, 1);
1012 btrfs_throttle(root
);
1016 count
-= write_bytes
;
1018 num_written
+= write_bytes
;
1023 mutex_unlock(&inode
->i_mutex
);
1029 page_cache_release(pinned
[0]);
1031 page_cache_release(pinned
[1]);
1035 * we want to make sure fsync finds this change
1036 * but we haven't joined a transaction running right now.
1038 * Later on, someone is sure to update the inode and get the
1039 * real transid recorded.
1041 * We set last_trans now to the fs_info generation + 1,
1042 * this will either be one more than the running transaction
1043 * or the generation used for the next transaction if there isn't
1044 * one running right now.
1046 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
+ 1;
1048 if (num_written
> 0 && will_write
) {
1049 struct btrfs_trans_handle
*trans
;
1051 err
= btrfs_wait_ordered_range(inode
, start_pos
, num_written
);
1055 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
1056 trans
= btrfs_start_transaction(root
, 0);
1057 ret
= btrfs_log_dentry_safe(trans
, root
,
1060 ret
= btrfs_sync_log(trans
, root
);
1062 btrfs_end_transaction(trans
, root
);
1064 btrfs_commit_transaction(trans
, root
);
1065 } else if (ret
!= BTRFS_NO_LOG_SYNC
) {
1066 btrfs_commit_transaction(trans
, root
);
1068 btrfs_end_transaction(trans
, root
);
1071 if (file
->f_flags
& O_DIRECT
&& buffered
) {
1072 invalidate_mapping_pages(inode
->i_mapping
,
1073 start_pos
>> PAGE_CACHE_SHIFT
,
1074 (start_pos
+ num_written
- 1) >> PAGE_CACHE_SHIFT
);
1077 current
->backing_dev_info
= NULL
;
1078 return num_written
? num_written
: err
;
1081 int btrfs_release_file(struct inode
*inode
, struct file
*filp
)
1084 * ordered_data_close is set by settattr when we are about to truncate
1085 * a file from a non-zero size to a zero size. This tries to
1086 * flush down new bytes that may have been written if the
1087 * application were using truncate to replace a file in place.
1089 if (BTRFS_I(inode
)->ordered_data_close
) {
1090 BTRFS_I(inode
)->ordered_data_close
= 0;
1091 btrfs_add_ordered_operation(NULL
, BTRFS_I(inode
)->root
, inode
);
1092 if (inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
1093 filemap_flush(inode
->i_mapping
);
1095 if (filp
->private_data
)
1096 btrfs_ioctl_trans_end(filp
);
1101 * fsync call for both files and directories. This logs the inode into
1102 * the tree log instead of forcing full commits whenever possible.
1104 * It needs to call filemap_fdatawait so that all ordered extent updates are
1105 * in the metadata btree are up to date for copying to the log.
1107 * It drops the inode mutex before doing the tree log commit. This is an
1108 * important optimization for directories because holding the mutex prevents
1109 * new operations on the dir while we write to disk.
1111 int btrfs_sync_file(struct file
*file
, struct dentry
*dentry
, int datasync
)
1113 struct inode
*inode
= dentry
->d_inode
;
1114 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1116 struct btrfs_trans_handle
*trans
;
1119 /* we wait first, since the writeback may change the inode */
1121 /* the VFS called filemap_fdatawrite for us */
1122 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1126 * check the transaction that last modified this inode
1127 * and see if its already been committed
1129 if (!BTRFS_I(inode
)->last_trans
)
1133 * if the last transaction that changed this file was before
1134 * the current transaction, we can bail out now without any
1137 mutex_lock(&root
->fs_info
->trans_mutex
);
1138 if (BTRFS_I(inode
)->last_trans
<=
1139 root
->fs_info
->last_trans_committed
) {
1140 BTRFS_I(inode
)->last_trans
= 0;
1141 mutex_unlock(&root
->fs_info
->trans_mutex
);
1144 mutex_unlock(&root
->fs_info
->trans_mutex
);
1147 * ok we haven't committed the transaction yet, lets do a commit
1149 if (file
&& file
->private_data
)
1150 btrfs_ioctl_trans_end(file
);
1152 trans
= btrfs_start_transaction(root
, 0);
1153 if (IS_ERR(trans
)) {
1154 ret
= PTR_ERR(trans
);
1158 ret
= btrfs_log_dentry_safe(trans
, root
, dentry
);
1162 /* we've logged all the items and now have a consistent
1163 * version of the file in the log. It is possible that
1164 * someone will come in and modify the file, but that's
1165 * fine because the log is consistent on disk, and we
1166 * have references to all of the file's extents
1168 * It is possible that someone will come in and log the
1169 * file again, but that will end up using the synchronization
1170 * inside btrfs_sync_log to keep things safe.
1172 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1174 if (ret
!= BTRFS_NO_LOG_SYNC
) {
1176 ret
= btrfs_commit_transaction(trans
, root
);
1178 ret
= btrfs_sync_log(trans
, root
);
1180 ret
= btrfs_end_transaction(trans
, root
);
1182 ret
= btrfs_commit_transaction(trans
, root
);
1185 ret
= btrfs_end_transaction(trans
, root
);
1187 mutex_lock(&dentry
->d_inode
->i_mutex
);
1189 return ret
> 0 ? -EIO
: ret
;
1192 static const struct vm_operations_struct btrfs_file_vm_ops
= {
1193 .fault
= filemap_fault
,
1194 .page_mkwrite
= btrfs_page_mkwrite
,
1197 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1199 vma
->vm_ops
= &btrfs_file_vm_ops
;
1200 file_accessed(filp
);
1204 const struct file_operations btrfs_file_operations
= {
1205 .llseek
= generic_file_llseek
,
1206 .read
= do_sync_read
,
1207 .aio_read
= generic_file_aio_read
,
1208 .splice_read
= generic_file_splice_read
,
1209 .write
= btrfs_file_write
,
1210 .mmap
= btrfs_file_mmap
,
1211 .open
= generic_file_open
,
1212 .release
= btrfs_release_file
,
1213 .fsync
= btrfs_sync_file
,
1214 .unlocked_ioctl
= btrfs_ioctl
,
1215 #ifdef CONFIG_COMPAT
1216 .compat_ioctl
= btrfs_ioctl
,