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/falloc.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/statfs.h>
31 #include <linux/compat.h>
32 #include <linux/slab.h>
35 #include "transaction.h"
36 #include "btrfs_inode.h"
38 #include "print-tree.h"
44 /* simple helper to fault in pages and copy. This should go away
45 * and be replaced with calls into generic code.
47 static noinline
int btrfs_copy_from_user(loff_t pos
, int num_pages
,
49 struct page
**prepared_pages
,
54 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
57 while (write_bytes
> 0) {
58 size_t count
= min_t(size_t,
59 PAGE_CACHE_SIZE
- offset
, write_bytes
);
60 struct page
*page
= prepared_pages
[pg
];
62 * Copy data from userspace to the current page
64 * Disable pagefault to avoid recursive lock since
65 * the pages are already locked
68 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, count
);
71 /* Flush processor's dcache for this page */
72 flush_dcache_page(page
);
73 iov_iter_advance(i
, copied
);
74 write_bytes
-= copied
;
75 total_copied
+= copied
;
77 /* Return to btrfs_file_aio_write to fault page */
78 if (unlikely(copied
== 0)) {
82 if (unlikely(copied
< PAGE_CACHE_SIZE
- offset
)) {
93 * unlocks pages after btrfs_file_write is done with them
95 static noinline
void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
98 for (i
= 0; i
< num_pages
; i
++) {
101 /* page checked is some magic around finding pages that
102 * have been modified without going through btrfs_set_page_dirty
105 ClearPageChecked(pages
[i
]);
106 unlock_page(pages
[i
]);
107 mark_page_accessed(pages
[i
]);
108 page_cache_release(pages
[i
]);
113 * after copy_from_user, pages need to be dirtied and we need to make
114 * sure holes are created between the current EOF and the start of
115 * any next extents (if required).
117 * this also makes the decision about creating an inline extent vs
118 * doing real data extents, marking pages dirty and delalloc as required.
120 static noinline
int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
121 struct btrfs_root
*root
,
130 struct inode
*inode
= fdentry(file
)->d_inode
;
133 u64 end_of_last_block
;
134 u64 end_pos
= pos
+ write_bytes
;
135 loff_t isize
= i_size_read(inode
);
137 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
138 num_bytes
= (write_bytes
+ pos
- start_pos
+
139 root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
141 end_of_last_block
= start_pos
+ num_bytes
- 1;
142 err
= btrfs_set_extent_delalloc(inode
, start_pos
, end_of_last_block
,
146 for (i
= 0; i
< num_pages
; i
++) {
147 struct page
*p
= pages
[i
];
152 if (end_pos
> isize
) {
153 i_size_write(inode
, end_pos
);
154 /* we've only changed i_size in ram, and we haven't updated
155 * the disk i_size. There is no need to log the inode
163 * this drops all the extents in the cache that intersect the range
164 * [start, end]. Existing extents are split as required.
166 int btrfs_drop_extent_cache(struct inode
*inode
, u64 start
, u64 end
,
169 struct extent_map
*em
;
170 struct extent_map
*split
= NULL
;
171 struct extent_map
*split2
= NULL
;
172 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
173 u64 len
= end
- start
+ 1;
179 WARN_ON(end
< start
);
180 if (end
== (u64
)-1) {
186 split
= alloc_extent_map(GFP_NOFS
);
188 split2
= alloc_extent_map(GFP_NOFS
);
189 BUG_ON(!split
|| !split2
);
191 write_lock(&em_tree
->lock
);
192 em
= lookup_extent_mapping(em_tree
, start
, len
);
194 write_unlock(&em_tree
->lock
);
198 if (skip_pinned
&& test_bit(EXTENT_FLAG_PINNED
, &em
->flags
)) {
199 if (testend
&& em
->start
+ em
->len
>= start
+ len
) {
201 write_unlock(&em_tree
->lock
);
204 start
= em
->start
+ em
->len
;
206 len
= start
+ len
- (em
->start
+ em
->len
);
208 write_unlock(&em_tree
->lock
);
211 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
212 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
213 remove_extent_mapping(em_tree
, em
);
215 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
217 split
->start
= em
->start
;
218 split
->len
= start
- em
->start
;
219 split
->orig_start
= em
->orig_start
;
220 split
->block_start
= em
->block_start
;
223 split
->block_len
= em
->block_len
;
225 split
->block_len
= split
->len
;
227 split
->bdev
= em
->bdev
;
228 split
->flags
= flags
;
229 split
->compress_type
= em
->compress_type
;
230 ret
= add_extent_mapping(em_tree
, split
);
232 free_extent_map(split
);
236 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
237 testend
&& em
->start
+ em
->len
> start
+ len
) {
238 u64 diff
= start
+ len
- em
->start
;
240 split
->start
= start
+ len
;
241 split
->len
= em
->start
+ em
->len
- (start
+ len
);
242 split
->bdev
= em
->bdev
;
243 split
->flags
= flags
;
244 split
->compress_type
= em
->compress_type
;
247 split
->block_len
= em
->block_len
;
248 split
->block_start
= em
->block_start
;
249 split
->orig_start
= em
->orig_start
;
251 split
->block_len
= split
->len
;
252 split
->block_start
= em
->block_start
+ diff
;
253 split
->orig_start
= split
->start
;
256 ret
= add_extent_mapping(em_tree
, split
);
258 free_extent_map(split
);
261 write_unlock(&em_tree
->lock
);
265 /* once for the tree*/
269 free_extent_map(split
);
271 free_extent_map(split2
);
276 * this is very complex, but the basic idea is to drop all extents
277 * in the range start - end. hint_block is filled in with a block number
278 * that would be a good hint to the block allocator for this file.
280 * If an extent intersects the range but is not entirely inside the range
281 * it is either truncated or split. Anything entirely inside the range
282 * is deleted from the tree.
284 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
, struct inode
*inode
,
285 u64 start
, u64 end
, u64
*hint_byte
, int drop_cache
)
287 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
288 struct extent_buffer
*leaf
;
289 struct btrfs_file_extent_item
*fi
;
290 struct btrfs_path
*path
;
291 struct btrfs_key key
;
292 struct btrfs_key new_key
;
293 u64 search_start
= start
;
296 u64 extent_offset
= 0;
305 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
307 path
= btrfs_alloc_path();
313 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
317 if (ret
> 0 && path
->slots
[0] > 0 && search_start
== start
) {
318 leaf
= path
->nodes
[0];
319 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0] - 1);
320 if (key
.objectid
== inode
->i_ino
&&
321 key
.type
== BTRFS_EXTENT_DATA_KEY
)
326 leaf
= path
->nodes
[0];
327 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
329 ret
= btrfs_next_leaf(root
, path
);
336 leaf
= path
->nodes
[0];
340 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
341 if (key
.objectid
> inode
->i_ino
||
342 key
.type
> BTRFS_EXTENT_DATA_KEY
|| key
.offset
>= end
)
345 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
346 struct btrfs_file_extent_item
);
347 extent_type
= btrfs_file_extent_type(leaf
, fi
);
349 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
350 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
351 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
352 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
353 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
354 extent_end
= key
.offset
+
355 btrfs_file_extent_num_bytes(leaf
, fi
);
356 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
357 extent_end
= key
.offset
+
358 btrfs_file_extent_inline_len(leaf
, fi
);
361 extent_end
= search_start
;
364 if (extent_end
<= search_start
) {
369 search_start
= max(key
.offset
, start
);
371 btrfs_release_path(root
, path
);
376 * | - range to drop - |
377 * | -------- extent -------- |
379 if (start
> key
.offset
&& end
< extent_end
) {
381 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
383 memcpy(&new_key
, &key
, sizeof(new_key
));
384 new_key
.offset
= start
;
385 ret
= btrfs_duplicate_item(trans
, root
, path
,
387 if (ret
== -EAGAIN
) {
388 btrfs_release_path(root
, path
);
394 leaf
= path
->nodes
[0];
395 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
396 struct btrfs_file_extent_item
);
397 btrfs_set_file_extent_num_bytes(leaf
, fi
,
400 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
401 struct btrfs_file_extent_item
);
403 extent_offset
+= start
- key
.offset
;
404 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
405 btrfs_set_file_extent_num_bytes(leaf
, fi
,
407 btrfs_mark_buffer_dirty(leaf
);
409 if (disk_bytenr
> 0) {
410 ret
= btrfs_inc_extent_ref(trans
, root
,
411 disk_bytenr
, num_bytes
, 0,
412 root
->root_key
.objectid
,
414 start
- extent_offset
);
416 *hint_byte
= disk_bytenr
;
421 * | ---- range to drop ----- |
422 * | -------- extent -------- |
424 if (start
<= key
.offset
&& end
< extent_end
) {
425 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
427 memcpy(&new_key
, &key
, sizeof(new_key
));
428 new_key
.offset
= end
;
429 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
431 extent_offset
+= end
- key
.offset
;
432 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
433 btrfs_set_file_extent_num_bytes(leaf
, fi
,
435 btrfs_mark_buffer_dirty(leaf
);
436 if (disk_bytenr
> 0) {
437 inode_sub_bytes(inode
, end
- key
.offset
);
438 *hint_byte
= disk_bytenr
;
443 search_start
= extent_end
;
445 * | ---- range to drop ----- |
446 * | -------- extent -------- |
448 if (start
> key
.offset
&& end
>= extent_end
) {
450 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
452 btrfs_set_file_extent_num_bytes(leaf
, fi
,
454 btrfs_mark_buffer_dirty(leaf
);
455 if (disk_bytenr
> 0) {
456 inode_sub_bytes(inode
, extent_end
- start
);
457 *hint_byte
= disk_bytenr
;
459 if (end
== extent_end
)
467 * | ---- range to drop ----- |
468 * | ------ extent ------ |
470 if (start
<= key
.offset
&& end
>= extent_end
) {
472 del_slot
= path
->slots
[0];
475 BUG_ON(del_slot
+ del_nr
!= path
->slots
[0]);
479 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
480 inode_sub_bytes(inode
,
481 extent_end
- key
.offset
);
482 extent_end
= ALIGN(extent_end
,
484 } else if (disk_bytenr
> 0) {
485 ret
= btrfs_free_extent(trans
, root
,
486 disk_bytenr
, num_bytes
, 0,
487 root
->root_key
.objectid
,
488 key
.objectid
, key
.offset
-
491 inode_sub_bytes(inode
,
492 extent_end
- key
.offset
);
493 *hint_byte
= disk_bytenr
;
496 if (end
== extent_end
)
499 if (path
->slots
[0] + 1 < btrfs_header_nritems(leaf
)) {
504 ret
= btrfs_del_items(trans
, root
, path
, del_slot
,
511 btrfs_release_path(root
, path
);
519 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
523 btrfs_free_path(path
);
527 static int extent_mergeable(struct extent_buffer
*leaf
, int slot
,
528 u64 objectid
, u64 bytenr
, u64 orig_offset
,
529 u64
*start
, u64
*end
)
531 struct btrfs_file_extent_item
*fi
;
532 struct btrfs_key key
;
535 if (slot
< 0 || slot
>= btrfs_header_nritems(leaf
))
538 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
539 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
542 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
543 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
||
544 btrfs_file_extent_disk_bytenr(leaf
, fi
) != bytenr
||
545 btrfs_file_extent_offset(leaf
, fi
) != key
.offset
- orig_offset
||
546 btrfs_file_extent_compression(leaf
, fi
) ||
547 btrfs_file_extent_encryption(leaf
, fi
) ||
548 btrfs_file_extent_other_encoding(leaf
, fi
))
551 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
552 if ((*start
&& *start
!= key
.offset
) || (*end
&& *end
!= extent_end
))
561 * Mark extent in the range start - end as written.
563 * This changes extent type from 'pre-allocated' to 'regular'. If only
564 * part of extent is marked as written, the extent will be split into
567 int btrfs_mark_extent_written(struct btrfs_trans_handle
*trans
,
568 struct inode
*inode
, u64 start
, u64 end
)
570 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
571 struct extent_buffer
*leaf
;
572 struct btrfs_path
*path
;
573 struct btrfs_file_extent_item
*fi
;
574 struct btrfs_key key
;
575 struct btrfs_key new_key
;
588 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
590 path
= btrfs_alloc_path();
595 key
.objectid
= inode
->i_ino
;
596 key
.type
= BTRFS_EXTENT_DATA_KEY
;
599 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
600 if (ret
> 0 && path
->slots
[0] > 0)
603 leaf
= path
->nodes
[0];
604 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
605 BUG_ON(key
.objectid
!= inode
->i_ino
||
606 key
.type
!= BTRFS_EXTENT_DATA_KEY
);
607 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
608 struct btrfs_file_extent_item
);
609 BUG_ON(btrfs_file_extent_type(leaf
, fi
) !=
610 BTRFS_FILE_EXTENT_PREALLOC
);
611 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
612 BUG_ON(key
.offset
> start
|| extent_end
< end
);
614 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
615 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
616 orig_offset
= key
.offset
- btrfs_file_extent_offset(leaf
, fi
);
617 memcpy(&new_key
, &key
, sizeof(new_key
));
619 if (start
== key
.offset
&& end
< extent_end
) {
622 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
623 inode
->i_ino
, bytenr
, orig_offset
,
624 &other_start
, &other_end
)) {
625 new_key
.offset
= end
;
626 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
627 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
628 struct btrfs_file_extent_item
);
629 btrfs_set_file_extent_num_bytes(leaf
, fi
,
631 btrfs_set_file_extent_offset(leaf
, fi
,
633 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
634 struct btrfs_file_extent_item
);
635 btrfs_set_file_extent_num_bytes(leaf
, fi
,
637 btrfs_mark_buffer_dirty(leaf
);
642 if (start
> key
.offset
&& end
== extent_end
) {
645 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
646 inode
->i_ino
, bytenr
, orig_offset
,
647 &other_start
, &other_end
)) {
648 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
649 struct btrfs_file_extent_item
);
650 btrfs_set_file_extent_num_bytes(leaf
, fi
,
653 new_key
.offset
= start
;
654 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
656 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
657 struct btrfs_file_extent_item
);
658 btrfs_set_file_extent_num_bytes(leaf
, fi
,
660 btrfs_set_file_extent_offset(leaf
, fi
,
661 start
- orig_offset
);
662 btrfs_mark_buffer_dirty(leaf
);
667 while (start
> key
.offset
|| end
< extent_end
) {
668 if (key
.offset
== start
)
671 new_key
.offset
= split
;
672 ret
= btrfs_duplicate_item(trans
, root
, path
, &new_key
);
673 if (ret
== -EAGAIN
) {
674 btrfs_release_path(root
, path
);
679 leaf
= path
->nodes
[0];
680 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
681 struct btrfs_file_extent_item
);
682 btrfs_set_file_extent_num_bytes(leaf
, fi
,
685 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
686 struct btrfs_file_extent_item
);
688 btrfs_set_file_extent_offset(leaf
, fi
, split
- orig_offset
);
689 btrfs_set_file_extent_num_bytes(leaf
, fi
,
691 btrfs_mark_buffer_dirty(leaf
);
693 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
, num_bytes
, 0,
694 root
->root_key
.objectid
,
695 inode
->i_ino
, orig_offset
);
698 if (split
== start
) {
701 BUG_ON(start
!= key
.offset
);
710 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
711 inode
->i_ino
, bytenr
, orig_offset
,
712 &other_start
, &other_end
)) {
714 btrfs_release_path(root
, path
);
717 extent_end
= other_end
;
718 del_slot
= path
->slots
[0] + 1;
720 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
721 0, root
->root_key
.objectid
,
722 inode
->i_ino
, orig_offset
);
727 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
728 inode
->i_ino
, bytenr
, orig_offset
,
729 &other_start
, &other_end
)) {
731 btrfs_release_path(root
, path
);
734 key
.offset
= other_start
;
735 del_slot
= path
->slots
[0];
737 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
738 0, root
->root_key
.objectid
,
739 inode
->i_ino
, orig_offset
);
743 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
744 struct btrfs_file_extent_item
);
745 btrfs_set_file_extent_type(leaf
, fi
,
746 BTRFS_FILE_EXTENT_REG
);
747 btrfs_mark_buffer_dirty(leaf
);
749 fi
= btrfs_item_ptr(leaf
, del_slot
- 1,
750 struct btrfs_file_extent_item
);
751 btrfs_set_file_extent_type(leaf
, fi
,
752 BTRFS_FILE_EXTENT_REG
);
753 btrfs_set_file_extent_num_bytes(leaf
, fi
,
754 extent_end
- key
.offset
);
755 btrfs_mark_buffer_dirty(leaf
);
757 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
761 btrfs_free_path(path
);
766 * this gets pages into the page cache and locks them down, it also properly
767 * waits for data=ordered extents to finish before allowing the pages to be
770 static noinline
int prepare_pages(struct btrfs_root
*root
, struct file
*file
,
771 struct page
**pages
, size_t num_pages
,
772 loff_t pos
, unsigned long first_index
,
773 unsigned long last_index
, size_t write_bytes
)
775 struct extent_state
*cached_state
= NULL
;
777 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
778 struct inode
*inode
= fdentry(file
)->d_inode
;
783 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
784 last_pos
= ((u64
)index
+ num_pages
) << PAGE_CACHE_SHIFT
;
786 if (start_pos
> inode
->i_size
) {
787 err
= btrfs_cont_expand(inode
, start_pos
);
792 memset(pages
, 0, num_pages
* sizeof(struct page
*));
794 for (i
= 0; i
< num_pages
; i
++) {
795 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
798 for (c
= i
- 1; c
>= 0; c
--) {
799 unlock_page(pages
[c
]);
800 page_cache_release(pages
[c
]);
804 wait_on_page_writeback(pages
[i
]);
806 if (start_pos
< inode
->i_size
) {
807 struct btrfs_ordered_extent
*ordered
;
808 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
809 start_pos
, last_pos
- 1, 0, &cached_state
,
811 ordered
= btrfs_lookup_first_ordered_extent(inode
,
814 ordered
->file_offset
+ ordered
->len
> start_pos
&&
815 ordered
->file_offset
< last_pos
) {
816 btrfs_put_ordered_extent(ordered
);
817 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
818 start_pos
, last_pos
- 1,
819 &cached_state
, GFP_NOFS
);
820 for (i
= 0; i
< num_pages
; i
++) {
821 unlock_page(pages
[i
]);
822 page_cache_release(pages
[i
]);
824 btrfs_wait_ordered_range(inode
, start_pos
,
825 last_pos
- start_pos
);
829 btrfs_put_ordered_extent(ordered
);
831 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start_pos
,
832 last_pos
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
833 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
835 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
836 start_pos
, last_pos
- 1, &cached_state
,
839 for (i
= 0; i
< num_pages
; i
++) {
840 clear_page_dirty_for_io(pages
[i
]);
841 set_page_extent_mapped(pages
[i
]);
842 WARN_ON(!PageLocked(pages
[i
]));
847 static ssize_t
btrfs_file_aio_write(struct kiocb
*iocb
,
848 const struct iovec
*iov
,
849 unsigned long nr_segs
, loff_t pos
)
851 struct file
*file
= iocb
->ki_filp
;
852 struct inode
*inode
= fdentry(file
)->d_inode
;
853 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
854 struct page
*pinned
[2];
855 struct page
**pages
= NULL
;
857 loff_t
*ppos
= &iocb
->ki_pos
;
859 ssize_t num_written
= 0;
865 unsigned long first_index
;
866 unsigned long last_index
;
872 will_write
= ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
) ||
873 (file
->f_flags
& O_DIRECT
));
880 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
882 mutex_lock(&inode
->i_mutex
);
884 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
889 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
890 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
897 err
= file_remove_suid(file
);
902 * If BTRFS flips readonly due to some impossible error
903 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
904 * although we have opened a file as writable, we have
905 * to stop this write operation to ensure FS consistency.
907 if (root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
) {
912 file_update_time(file
);
913 BTRFS_I(inode
)->sequence
++;
915 if (unlikely(file
->f_flags
& O_DIRECT
)) {
916 num_written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
920 * the generic O_DIRECT will update in-memory i_size after the
921 * DIOs are done. But our endio handlers that update the on
922 * disk i_size never update past the in memory i_size. So we
923 * need one more update here to catch any additions to the
926 if (inode
->i_size
!= BTRFS_I(inode
)->disk_i_size
) {
927 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
928 mark_inode_dirty(inode
);
931 if (num_written
< 0) {
935 } else if (num_written
== count
) {
936 /* pick up pos changes done by the generic code */
941 * We are going to do buffered for the rest of the range, so we
942 * need to make sure to invalidate the buffered pages when we're
949 iov_iter_init(&i
, iov
, nr_segs
, count
, num_written
);
950 nrptrs
= min((iov_iter_count(&i
) + PAGE_CACHE_SIZE
- 1) /
951 PAGE_CACHE_SIZE
, PAGE_CACHE_SIZE
/
952 (sizeof(struct page
*)));
953 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
959 /* generic_write_checks can change our pos */
962 first_index
= pos
>> PAGE_CACHE_SHIFT
;
963 last_index
= (pos
+ iov_iter_count(&i
)) >> PAGE_CACHE_SHIFT
;
966 * there are lots of better ways to do this, but this code
967 * makes sure the first and last page in the file range are
968 * up to date and ready for cow
970 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
971 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
972 if (!PageUptodate(pinned
[0])) {
973 ret
= btrfs_readpage(NULL
, pinned
[0]);
975 wait_on_page_locked(pinned
[0]);
977 unlock_page(pinned
[0]);
980 if ((pos
+ iov_iter_count(&i
)) & (PAGE_CACHE_SIZE
- 1)) {
981 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
982 if (!PageUptodate(pinned
[1])) {
983 ret
= btrfs_readpage(NULL
, pinned
[1]);
985 wait_on_page_locked(pinned
[1]);
987 unlock_page(pinned
[1]);
991 while (iov_iter_count(&i
) > 0) {
992 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
993 size_t write_bytes
= min(iov_iter_count(&i
),
994 nrptrs
* (size_t)PAGE_CACHE_SIZE
-
996 size_t num_pages
= (write_bytes
+ offset
+
997 PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
999 WARN_ON(num_pages
> nrptrs
);
1000 memset(pages
, 0, sizeof(struct page
*) * nrptrs
);
1003 * Fault pages before locking them in prepare_pages
1004 * to avoid recursive lock
1006 if (unlikely(iov_iter_fault_in_readable(&i
, write_bytes
))) {
1011 ret
= btrfs_delalloc_reserve_space(inode
,
1012 num_pages
<< PAGE_CACHE_SHIFT
);
1016 ret
= prepare_pages(root
, file
, pages
, num_pages
,
1017 pos
, first_index
, last_index
,
1020 btrfs_delalloc_release_space(inode
,
1021 num_pages
<< PAGE_CACHE_SHIFT
);
1025 copied
= btrfs_copy_from_user(pos
, num_pages
,
1026 write_bytes
, pages
, &i
);
1027 dirty_pages
= (copied
+ offset
+ PAGE_CACHE_SIZE
- 1) >>
1030 if (num_pages
> dirty_pages
) {
1033 &BTRFS_I(inode
)->outstanding_extents
);
1034 btrfs_delalloc_release_space(inode
,
1035 (num_pages
- dirty_pages
) <<
1040 dirty_and_release_pages(NULL
, root
, file
, pages
,
1041 dirty_pages
, pos
, copied
);
1044 btrfs_drop_pages(pages
, num_pages
);
1048 filemap_fdatawrite_range(inode
->i_mapping
, pos
,
1051 balance_dirty_pages_ratelimited_nr(
1055 (root
->leafsize
>> PAGE_CACHE_SHIFT
) + 1)
1056 btrfs_btree_balance_dirty(root
, 1);
1057 btrfs_throttle(root
);
1062 num_written
+= copied
;
1067 mutex_unlock(&inode
->i_mutex
);
1073 page_cache_release(pinned
[0]);
1075 page_cache_release(pinned
[1]);
1079 * we want to make sure fsync finds this change
1080 * but we haven't joined a transaction running right now.
1082 * Later on, someone is sure to update the inode and get the
1083 * real transid recorded.
1085 * We set last_trans now to the fs_info generation + 1,
1086 * this will either be one more than the running transaction
1087 * or the generation used for the next transaction if there isn't
1088 * one running right now.
1090 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
+ 1;
1092 if (num_written
> 0 && will_write
) {
1093 struct btrfs_trans_handle
*trans
;
1095 err
= btrfs_wait_ordered_range(inode
, start_pos
, num_written
);
1099 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
1100 trans
= btrfs_start_transaction(root
, 0);
1101 if (IS_ERR(trans
)) {
1102 num_written
= PTR_ERR(trans
);
1105 mutex_lock(&inode
->i_mutex
);
1106 ret
= btrfs_log_dentry_safe(trans
, root
,
1108 mutex_unlock(&inode
->i_mutex
);
1110 ret
= btrfs_sync_log(trans
, root
);
1112 btrfs_end_transaction(trans
, root
);
1114 btrfs_commit_transaction(trans
, root
);
1115 } else if (ret
!= BTRFS_NO_LOG_SYNC
) {
1116 btrfs_commit_transaction(trans
, root
);
1118 btrfs_end_transaction(trans
, root
);
1121 if (file
->f_flags
& O_DIRECT
&& buffered
) {
1122 invalidate_mapping_pages(inode
->i_mapping
,
1123 start_pos
>> PAGE_CACHE_SHIFT
,
1124 (start_pos
+ num_written
- 1) >> PAGE_CACHE_SHIFT
);
1128 current
->backing_dev_info
= NULL
;
1129 return num_written
? num_written
: err
;
1132 int btrfs_release_file(struct inode
*inode
, struct file
*filp
)
1135 * ordered_data_close is set by settattr when we are about to truncate
1136 * a file from a non-zero size to a zero size. This tries to
1137 * flush down new bytes that may have been written if the
1138 * application were using truncate to replace a file in place.
1140 if (BTRFS_I(inode
)->ordered_data_close
) {
1141 BTRFS_I(inode
)->ordered_data_close
= 0;
1142 btrfs_add_ordered_operation(NULL
, BTRFS_I(inode
)->root
, inode
);
1143 if (inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
1144 filemap_flush(inode
->i_mapping
);
1146 if (filp
->private_data
)
1147 btrfs_ioctl_trans_end(filp
);
1152 * fsync call for both files and directories. This logs the inode into
1153 * the tree log instead of forcing full commits whenever possible.
1155 * It needs to call filemap_fdatawait so that all ordered extent updates are
1156 * in the metadata btree are up to date for copying to the log.
1158 * It drops the inode mutex before doing the tree log commit. This is an
1159 * important optimization for directories because holding the mutex prevents
1160 * new operations on the dir while we write to disk.
1162 int btrfs_sync_file(struct file
*file
, int datasync
)
1164 struct dentry
*dentry
= file
->f_path
.dentry
;
1165 struct inode
*inode
= dentry
->d_inode
;
1166 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1168 struct btrfs_trans_handle
*trans
;
1171 /* we wait first, since the writeback may change the inode */
1173 /* the VFS called filemap_fdatawrite for us */
1174 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1178 * check the transaction that last modified this inode
1179 * and see if its already been committed
1181 if (!BTRFS_I(inode
)->last_trans
)
1185 * if the last transaction that changed this file was before
1186 * the current transaction, we can bail out now without any
1189 mutex_lock(&root
->fs_info
->trans_mutex
);
1190 if (BTRFS_I(inode
)->last_trans
<=
1191 root
->fs_info
->last_trans_committed
) {
1192 BTRFS_I(inode
)->last_trans
= 0;
1193 mutex_unlock(&root
->fs_info
->trans_mutex
);
1196 mutex_unlock(&root
->fs_info
->trans_mutex
);
1199 * ok we haven't committed the transaction yet, lets do a commit
1201 if (file
->private_data
)
1202 btrfs_ioctl_trans_end(file
);
1204 trans
= btrfs_start_transaction(root
, 0);
1205 if (IS_ERR(trans
)) {
1206 ret
= PTR_ERR(trans
);
1210 ret
= btrfs_log_dentry_safe(trans
, root
, dentry
);
1214 /* we've logged all the items and now have a consistent
1215 * version of the file in the log. It is possible that
1216 * someone will come in and modify the file, but that's
1217 * fine because the log is consistent on disk, and we
1218 * have references to all of the file's extents
1220 * It is possible that someone will come in and log the
1221 * file again, but that will end up using the synchronization
1222 * inside btrfs_sync_log to keep things safe.
1224 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1226 if (ret
!= BTRFS_NO_LOG_SYNC
) {
1228 ret
= btrfs_commit_transaction(trans
, root
);
1230 ret
= btrfs_sync_log(trans
, root
);
1232 ret
= btrfs_end_transaction(trans
, root
);
1234 ret
= btrfs_commit_transaction(trans
, root
);
1237 ret
= btrfs_end_transaction(trans
, root
);
1239 mutex_lock(&dentry
->d_inode
->i_mutex
);
1241 return ret
> 0 ? -EIO
: ret
;
1244 static const struct vm_operations_struct btrfs_file_vm_ops
= {
1245 .fault
= filemap_fault
,
1246 .page_mkwrite
= btrfs_page_mkwrite
,
1249 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1251 struct address_space
*mapping
= filp
->f_mapping
;
1253 if (!mapping
->a_ops
->readpage
)
1256 file_accessed(filp
);
1257 vma
->vm_ops
= &btrfs_file_vm_ops
;
1258 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1263 static long btrfs_fallocate(struct file
*file
, int mode
,
1264 loff_t offset
, loff_t len
)
1266 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1267 struct extent_state
*cached_state
= NULL
;
1274 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
1275 struct extent_map
*em
;
1278 alloc_start
= offset
& ~mask
;
1279 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
1281 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1282 if (mode
& ~FALLOC_FL_KEEP_SIZE
)
1286 * wait for ordered IO before we have any locks. We'll loop again
1287 * below with the locks held.
1289 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
1291 mutex_lock(&inode
->i_mutex
);
1292 ret
= inode_newsize_ok(inode
, alloc_end
);
1296 if (alloc_start
> inode
->i_size
) {
1297 ret
= btrfs_cont_expand(inode
, alloc_start
);
1302 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
1306 locked_end
= alloc_end
- 1;
1308 struct btrfs_ordered_extent
*ordered
;
1310 /* the extent lock is ordered inside the running
1313 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
1314 locked_end
, 0, &cached_state
, GFP_NOFS
);
1315 ordered
= btrfs_lookup_first_ordered_extent(inode
,
1318 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
1319 ordered
->file_offset
< alloc_end
) {
1320 btrfs_put_ordered_extent(ordered
);
1321 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1322 alloc_start
, locked_end
,
1323 &cached_state
, GFP_NOFS
);
1325 * we can't wait on the range with the transaction
1326 * running or with the extent lock held
1328 btrfs_wait_ordered_range(inode
, alloc_start
,
1329 alloc_end
- alloc_start
);
1332 btrfs_put_ordered_extent(ordered
);
1337 cur_offset
= alloc_start
;
1339 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
1340 alloc_end
- cur_offset
, 0);
1341 BUG_ON(IS_ERR(em
) || !em
);
1342 last_byte
= min(extent_map_end(em
), alloc_end
);
1343 last_byte
= (last_byte
+ mask
) & ~mask
;
1344 if (em
->block_start
== EXTENT_MAP_HOLE
||
1345 (cur_offset
>= inode
->i_size
&&
1346 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
1347 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
1348 last_byte
- cur_offset
,
1349 1 << inode
->i_blkbits
,
1353 free_extent_map(em
);
1357 free_extent_map(em
);
1359 cur_offset
= last_byte
;
1360 if (cur_offset
>= alloc_end
) {
1365 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
1366 &cached_state
, GFP_NOFS
);
1368 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
1370 mutex_unlock(&inode
->i_mutex
);
1374 const struct file_operations btrfs_file_operations
= {
1375 .llseek
= generic_file_llseek
,
1376 .read
= do_sync_read
,
1377 .write
= do_sync_write
,
1378 .aio_read
= generic_file_aio_read
,
1379 .splice_read
= generic_file_splice_read
,
1380 .aio_write
= btrfs_file_aio_write
,
1381 .mmap
= btrfs_file_mmap
,
1382 .open
= generic_file_open
,
1383 .release
= btrfs_release_file
,
1384 .fsync
= btrfs_sync_file
,
1385 .fallocate
= btrfs_fallocate
,
1386 .unlocked_ioctl
= btrfs_ioctl
,
1387 #ifdef CONFIG_COMPAT
1388 .compat_ioctl
= btrfs_ioctl
,