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
);
75 * if we get a partial write, we can end up with
76 * partially up to date pages. These add
77 * a lot of complexity, so make sure they don't
78 * happen by forcing this copy to be retried.
80 * The rest of the btrfs_file_write code will fall
81 * back to page at a time copies after we return 0.
83 if (!PageUptodate(page
) && copied
< count
)
86 iov_iter_advance(i
, copied
);
87 write_bytes
-= copied
;
88 total_copied
+= copied
;
90 /* Return to btrfs_file_aio_write to fault page */
91 if (unlikely(copied
== 0)) {
95 if (unlikely(copied
< PAGE_CACHE_SIZE
- offset
)) {
106 * unlocks pages after btrfs_file_write is done with them
108 static noinline
void btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
111 for (i
= 0; i
< num_pages
; i
++) {
114 /* page checked is some magic around finding pages that
115 * have been modified without going through btrfs_set_page_dirty
118 ClearPageChecked(pages
[i
]);
119 unlock_page(pages
[i
]);
120 mark_page_accessed(pages
[i
]);
121 page_cache_release(pages
[i
]);
126 * after copy_from_user, pages need to be dirtied and we need to make
127 * sure holes are created between the current EOF and the start of
128 * any next extents (if required).
130 * this also makes the decision about creating an inline extent vs
131 * doing real data extents, marking pages dirty and delalloc as required.
133 static noinline
int dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
134 struct btrfs_root
*root
,
143 struct inode
*inode
= fdentry(file
)->d_inode
;
146 u64 end_of_last_block
;
147 u64 end_pos
= pos
+ write_bytes
;
148 loff_t isize
= i_size_read(inode
);
150 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
151 num_bytes
= (write_bytes
+ pos
- start_pos
+
152 root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
154 end_of_last_block
= start_pos
+ num_bytes
- 1;
155 err
= btrfs_set_extent_delalloc(inode
, start_pos
, end_of_last_block
,
159 for (i
= 0; i
< num_pages
; i
++) {
160 struct page
*p
= pages
[i
];
165 if (end_pos
> isize
) {
166 i_size_write(inode
, end_pos
);
167 /* we've only changed i_size in ram, and we haven't updated
168 * the disk i_size. There is no need to log the inode
176 * this drops all the extents in the cache that intersect the range
177 * [start, end]. Existing extents are split as required.
179 int btrfs_drop_extent_cache(struct inode
*inode
, u64 start
, u64 end
,
182 struct extent_map
*em
;
183 struct extent_map
*split
= NULL
;
184 struct extent_map
*split2
= NULL
;
185 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
186 u64 len
= end
- start
+ 1;
192 WARN_ON(end
< start
);
193 if (end
== (u64
)-1) {
199 split
= alloc_extent_map(GFP_NOFS
);
201 split2
= alloc_extent_map(GFP_NOFS
);
202 BUG_ON(!split
|| !split2
);
204 write_lock(&em_tree
->lock
);
205 em
= lookup_extent_mapping(em_tree
, start
, len
);
207 write_unlock(&em_tree
->lock
);
211 if (skip_pinned
&& test_bit(EXTENT_FLAG_PINNED
, &em
->flags
)) {
212 if (testend
&& em
->start
+ em
->len
>= start
+ len
) {
214 write_unlock(&em_tree
->lock
);
217 start
= em
->start
+ em
->len
;
219 len
= start
+ len
- (em
->start
+ em
->len
);
221 write_unlock(&em_tree
->lock
);
224 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
225 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
226 remove_extent_mapping(em_tree
, em
);
228 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
230 split
->start
= em
->start
;
231 split
->len
= start
- em
->start
;
232 split
->orig_start
= em
->orig_start
;
233 split
->block_start
= em
->block_start
;
236 split
->block_len
= em
->block_len
;
238 split
->block_len
= split
->len
;
240 split
->bdev
= em
->bdev
;
241 split
->flags
= flags
;
242 split
->compress_type
= em
->compress_type
;
243 ret
= add_extent_mapping(em_tree
, split
);
245 free_extent_map(split
);
249 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
250 testend
&& em
->start
+ em
->len
> start
+ len
) {
251 u64 diff
= start
+ len
- em
->start
;
253 split
->start
= start
+ len
;
254 split
->len
= em
->start
+ em
->len
- (start
+ len
);
255 split
->bdev
= em
->bdev
;
256 split
->flags
= flags
;
257 split
->compress_type
= em
->compress_type
;
260 split
->block_len
= em
->block_len
;
261 split
->block_start
= em
->block_start
;
262 split
->orig_start
= em
->orig_start
;
264 split
->block_len
= split
->len
;
265 split
->block_start
= em
->block_start
+ diff
;
266 split
->orig_start
= split
->start
;
269 ret
= add_extent_mapping(em_tree
, split
);
271 free_extent_map(split
);
274 write_unlock(&em_tree
->lock
);
278 /* once for the tree*/
282 free_extent_map(split
);
284 free_extent_map(split2
);
289 * this is very complex, but the basic idea is to drop all extents
290 * in the range start - end. hint_block is filled in with a block number
291 * that would be a good hint to the block allocator for this file.
293 * If an extent intersects the range but is not entirely inside the range
294 * it is either truncated or split. Anything entirely inside the range
295 * is deleted from the tree.
297 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
, struct inode
*inode
,
298 u64 start
, u64 end
, u64
*hint_byte
, int drop_cache
)
300 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
301 struct extent_buffer
*leaf
;
302 struct btrfs_file_extent_item
*fi
;
303 struct btrfs_path
*path
;
304 struct btrfs_key key
;
305 struct btrfs_key new_key
;
306 u64 search_start
= start
;
309 u64 extent_offset
= 0;
318 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
320 path
= btrfs_alloc_path();
326 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
330 if (ret
> 0 && path
->slots
[0] > 0 && search_start
== start
) {
331 leaf
= path
->nodes
[0];
332 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0] - 1);
333 if (key
.objectid
== inode
->i_ino
&&
334 key
.type
== BTRFS_EXTENT_DATA_KEY
)
339 leaf
= path
->nodes
[0];
340 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
342 ret
= btrfs_next_leaf(root
, path
);
349 leaf
= path
->nodes
[0];
353 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
354 if (key
.objectid
> inode
->i_ino
||
355 key
.type
> BTRFS_EXTENT_DATA_KEY
|| key
.offset
>= end
)
358 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
359 struct btrfs_file_extent_item
);
360 extent_type
= btrfs_file_extent_type(leaf
, fi
);
362 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
363 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
364 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
365 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
366 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
367 extent_end
= key
.offset
+
368 btrfs_file_extent_num_bytes(leaf
, fi
);
369 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
370 extent_end
= key
.offset
+
371 btrfs_file_extent_inline_len(leaf
, fi
);
374 extent_end
= search_start
;
377 if (extent_end
<= search_start
) {
382 search_start
= max(key
.offset
, start
);
384 btrfs_release_path(root
, path
);
389 * | - range to drop - |
390 * | -------- extent -------- |
392 if (start
> key
.offset
&& end
< extent_end
) {
394 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
396 memcpy(&new_key
, &key
, sizeof(new_key
));
397 new_key
.offset
= start
;
398 ret
= btrfs_duplicate_item(trans
, root
, path
,
400 if (ret
== -EAGAIN
) {
401 btrfs_release_path(root
, path
);
407 leaf
= path
->nodes
[0];
408 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
409 struct btrfs_file_extent_item
);
410 btrfs_set_file_extent_num_bytes(leaf
, fi
,
413 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
414 struct btrfs_file_extent_item
);
416 extent_offset
+= start
- key
.offset
;
417 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
418 btrfs_set_file_extent_num_bytes(leaf
, fi
,
420 btrfs_mark_buffer_dirty(leaf
);
422 if (disk_bytenr
> 0) {
423 ret
= btrfs_inc_extent_ref(trans
, root
,
424 disk_bytenr
, num_bytes
, 0,
425 root
->root_key
.objectid
,
427 start
- extent_offset
);
429 *hint_byte
= disk_bytenr
;
434 * | ---- range to drop ----- |
435 * | -------- extent -------- |
437 if (start
<= key
.offset
&& end
< extent_end
) {
438 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
440 memcpy(&new_key
, &key
, sizeof(new_key
));
441 new_key
.offset
= end
;
442 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
444 extent_offset
+= end
- key
.offset
;
445 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
446 btrfs_set_file_extent_num_bytes(leaf
, fi
,
448 btrfs_mark_buffer_dirty(leaf
);
449 if (disk_bytenr
> 0) {
450 inode_sub_bytes(inode
, end
- key
.offset
);
451 *hint_byte
= disk_bytenr
;
456 search_start
= extent_end
;
458 * | ---- range to drop ----- |
459 * | -------- extent -------- |
461 if (start
> key
.offset
&& end
>= extent_end
) {
463 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
465 btrfs_set_file_extent_num_bytes(leaf
, fi
,
467 btrfs_mark_buffer_dirty(leaf
);
468 if (disk_bytenr
> 0) {
469 inode_sub_bytes(inode
, extent_end
- start
);
470 *hint_byte
= disk_bytenr
;
472 if (end
== extent_end
)
480 * | ---- range to drop ----- |
481 * | ------ extent ------ |
483 if (start
<= key
.offset
&& end
>= extent_end
) {
485 del_slot
= path
->slots
[0];
488 BUG_ON(del_slot
+ del_nr
!= path
->slots
[0]);
492 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
493 inode_sub_bytes(inode
,
494 extent_end
- key
.offset
);
495 extent_end
= ALIGN(extent_end
,
497 } else if (disk_bytenr
> 0) {
498 ret
= btrfs_free_extent(trans
, root
,
499 disk_bytenr
, num_bytes
, 0,
500 root
->root_key
.objectid
,
501 key
.objectid
, key
.offset
-
504 inode_sub_bytes(inode
,
505 extent_end
- key
.offset
);
506 *hint_byte
= disk_bytenr
;
509 if (end
== extent_end
)
512 if (path
->slots
[0] + 1 < btrfs_header_nritems(leaf
)) {
517 ret
= btrfs_del_items(trans
, root
, path
, del_slot
,
524 btrfs_release_path(root
, path
);
532 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
536 btrfs_free_path(path
);
540 static int extent_mergeable(struct extent_buffer
*leaf
, int slot
,
541 u64 objectid
, u64 bytenr
, u64 orig_offset
,
542 u64
*start
, u64
*end
)
544 struct btrfs_file_extent_item
*fi
;
545 struct btrfs_key key
;
548 if (slot
< 0 || slot
>= btrfs_header_nritems(leaf
))
551 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
552 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
555 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
556 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
||
557 btrfs_file_extent_disk_bytenr(leaf
, fi
) != bytenr
||
558 btrfs_file_extent_offset(leaf
, fi
) != key
.offset
- orig_offset
||
559 btrfs_file_extent_compression(leaf
, fi
) ||
560 btrfs_file_extent_encryption(leaf
, fi
) ||
561 btrfs_file_extent_other_encoding(leaf
, fi
))
564 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
565 if ((*start
&& *start
!= key
.offset
) || (*end
&& *end
!= extent_end
))
574 * Mark extent in the range start - end as written.
576 * This changes extent type from 'pre-allocated' to 'regular'. If only
577 * part of extent is marked as written, the extent will be split into
580 int btrfs_mark_extent_written(struct btrfs_trans_handle
*trans
,
581 struct inode
*inode
, u64 start
, u64 end
)
583 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
584 struct extent_buffer
*leaf
;
585 struct btrfs_path
*path
;
586 struct btrfs_file_extent_item
*fi
;
587 struct btrfs_key key
;
588 struct btrfs_key new_key
;
601 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
603 path
= btrfs_alloc_path();
608 key
.objectid
= inode
->i_ino
;
609 key
.type
= BTRFS_EXTENT_DATA_KEY
;
612 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
613 if (ret
> 0 && path
->slots
[0] > 0)
616 leaf
= path
->nodes
[0];
617 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
618 BUG_ON(key
.objectid
!= inode
->i_ino
||
619 key
.type
!= BTRFS_EXTENT_DATA_KEY
);
620 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
621 struct btrfs_file_extent_item
);
622 BUG_ON(btrfs_file_extent_type(leaf
, fi
) !=
623 BTRFS_FILE_EXTENT_PREALLOC
);
624 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
625 BUG_ON(key
.offset
> start
|| extent_end
< end
);
627 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
628 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
629 orig_offset
= key
.offset
- btrfs_file_extent_offset(leaf
, fi
);
630 memcpy(&new_key
, &key
, sizeof(new_key
));
632 if (start
== key
.offset
&& end
< extent_end
) {
635 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
636 inode
->i_ino
, bytenr
, orig_offset
,
637 &other_start
, &other_end
)) {
638 new_key
.offset
= end
;
639 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
640 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
641 struct btrfs_file_extent_item
);
642 btrfs_set_file_extent_num_bytes(leaf
, fi
,
644 btrfs_set_file_extent_offset(leaf
, fi
,
646 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
647 struct btrfs_file_extent_item
);
648 btrfs_set_file_extent_num_bytes(leaf
, fi
,
650 btrfs_mark_buffer_dirty(leaf
);
655 if (start
> key
.offset
&& end
== extent_end
) {
658 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
659 inode
->i_ino
, bytenr
, orig_offset
,
660 &other_start
, &other_end
)) {
661 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
662 struct btrfs_file_extent_item
);
663 btrfs_set_file_extent_num_bytes(leaf
, fi
,
666 new_key
.offset
= start
;
667 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
669 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
670 struct btrfs_file_extent_item
);
671 btrfs_set_file_extent_num_bytes(leaf
, fi
,
673 btrfs_set_file_extent_offset(leaf
, fi
,
674 start
- orig_offset
);
675 btrfs_mark_buffer_dirty(leaf
);
680 while (start
> key
.offset
|| end
< extent_end
) {
681 if (key
.offset
== start
)
684 new_key
.offset
= split
;
685 ret
= btrfs_duplicate_item(trans
, root
, path
, &new_key
);
686 if (ret
== -EAGAIN
) {
687 btrfs_release_path(root
, path
);
692 leaf
= path
->nodes
[0];
693 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
694 struct btrfs_file_extent_item
);
695 btrfs_set_file_extent_num_bytes(leaf
, fi
,
698 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
699 struct btrfs_file_extent_item
);
701 btrfs_set_file_extent_offset(leaf
, fi
, split
- orig_offset
);
702 btrfs_set_file_extent_num_bytes(leaf
, fi
,
704 btrfs_mark_buffer_dirty(leaf
);
706 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
, num_bytes
, 0,
707 root
->root_key
.objectid
,
708 inode
->i_ino
, orig_offset
);
711 if (split
== start
) {
714 BUG_ON(start
!= key
.offset
);
723 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
724 inode
->i_ino
, bytenr
, orig_offset
,
725 &other_start
, &other_end
)) {
727 btrfs_release_path(root
, path
);
730 extent_end
= other_end
;
731 del_slot
= path
->slots
[0] + 1;
733 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
734 0, root
->root_key
.objectid
,
735 inode
->i_ino
, orig_offset
);
740 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
741 inode
->i_ino
, bytenr
, orig_offset
,
742 &other_start
, &other_end
)) {
744 btrfs_release_path(root
, path
);
747 key
.offset
= other_start
;
748 del_slot
= path
->slots
[0];
750 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
751 0, root
->root_key
.objectid
,
752 inode
->i_ino
, orig_offset
);
756 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
757 struct btrfs_file_extent_item
);
758 btrfs_set_file_extent_type(leaf
, fi
,
759 BTRFS_FILE_EXTENT_REG
);
760 btrfs_mark_buffer_dirty(leaf
);
762 fi
= btrfs_item_ptr(leaf
, del_slot
- 1,
763 struct btrfs_file_extent_item
);
764 btrfs_set_file_extent_type(leaf
, fi
,
765 BTRFS_FILE_EXTENT_REG
);
766 btrfs_set_file_extent_num_bytes(leaf
, fi
,
767 extent_end
- key
.offset
);
768 btrfs_mark_buffer_dirty(leaf
);
770 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
774 btrfs_free_path(path
);
779 * on error we return an unlocked page and the error value
780 * on success we return a locked page and 0
782 static int prepare_uptodate_page(struct page
*page
, u64 pos
)
786 if ((pos
& (PAGE_CACHE_SIZE
- 1)) && !PageUptodate(page
)) {
787 ret
= btrfs_readpage(NULL
, page
);
791 if (!PageUptodate(page
)) {
800 * this gets pages into the page cache and locks them down, it also properly
801 * waits for data=ordered extents to finish before allowing the pages to be
804 static noinline
int prepare_pages(struct btrfs_root
*root
, struct file
*file
,
805 struct page
**pages
, size_t num_pages
,
806 loff_t pos
, unsigned long first_index
,
807 unsigned long last_index
, size_t write_bytes
)
809 struct extent_state
*cached_state
= NULL
;
811 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
812 struct inode
*inode
= fdentry(file
)->d_inode
;
818 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
819 last_pos
= ((u64
)index
+ num_pages
) << PAGE_CACHE_SHIFT
;
821 if (start_pos
> inode
->i_size
) {
822 err
= btrfs_cont_expand(inode
, start_pos
);
827 memset(pages
, 0, num_pages
* sizeof(struct page
*));
829 for (i
= 0; i
< num_pages
; i
++) {
830 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
838 err
= prepare_uptodate_page(pages
[i
], pos
);
839 if (i
== num_pages
- 1)
840 err
= prepare_uptodate_page(pages
[i
],
843 page_cache_release(pages
[i
]);
847 wait_on_page_writeback(pages
[i
]);
850 if (start_pos
< inode
->i_size
) {
851 struct btrfs_ordered_extent
*ordered
;
852 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
853 start_pos
, last_pos
- 1, 0, &cached_state
,
855 ordered
= btrfs_lookup_first_ordered_extent(inode
,
858 ordered
->file_offset
+ ordered
->len
> start_pos
&&
859 ordered
->file_offset
< last_pos
) {
860 btrfs_put_ordered_extent(ordered
);
861 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
862 start_pos
, last_pos
- 1,
863 &cached_state
, GFP_NOFS
);
864 for (i
= 0; i
< num_pages
; i
++) {
865 unlock_page(pages
[i
]);
866 page_cache_release(pages
[i
]);
868 btrfs_wait_ordered_range(inode
, start_pos
,
869 last_pos
- start_pos
);
873 btrfs_put_ordered_extent(ordered
);
875 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start_pos
,
876 last_pos
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
877 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
879 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
880 start_pos
, last_pos
- 1, &cached_state
,
883 for (i
= 0; i
< num_pages
; i
++) {
884 clear_page_dirty_for_io(pages
[i
]);
885 set_page_extent_mapped(pages
[i
]);
886 WARN_ON(!PageLocked(pages
[i
]));
891 unlock_page(pages
[faili
]);
892 page_cache_release(pages
[faili
]);
899 static ssize_t
btrfs_file_aio_write(struct kiocb
*iocb
,
900 const struct iovec
*iov
,
901 unsigned long nr_segs
, loff_t pos
)
903 struct file
*file
= iocb
->ki_filp
;
904 struct inode
*inode
= fdentry(file
)->d_inode
;
905 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
906 struct page
**pages
= NULL
;
908 loff_t
*ppos
= &iocb
->ki_pos
;
910 ssize_t num_written
= 0;
916 unsigned long first_index
;
917 unsigned long last_index
;
923 will_write
= ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
) ||
924 (file
->f_flags
& O_DIRECT
));
928 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
930 mutex_lock(&inode
->i_mutex
);
932 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
937 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
938 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
945 err
= file_remove_suid(file
);
950 * If BTRFS flips readonly due to some impossible error
951 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
952 * although we have opened a file as writable, we have
953 * to stop this write operation to ensure FS consistency.
955 if (root
->fs_info
->fs_state
& BTRFS_SUPER_FLAG_ERROR
) {
960 file_update_time(file
);
961 BTRFS_I(inode
)->sequence
++;
963 if (unlikely(file
->f_flags
& O_DIRECT
)) {
964 num_written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
968 * the generic O_DIRECT will update in-memory i_size after the
969 * DIOs are done. But our endio handlers that update the on
970 * disk i_size never update past the in memory i_size. So we
971 * need one more update here to catch any additions to the
974 if (inode
->i_size
!= BTRFS_I(inode
)->disk_i_size
) {
975 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
976 mark_inode_dirty(inode
);
979 if (num_written
< 0) {
983 } else if (num_written
== count
) {
984 /* pick up pos changes done by the generic code */
989 * We are going to do buffered for the rest of the range, so we
990 * need to make sure to invalidate the buffered pages when we're
997 iov_iter_init(&i
, iov
, nr_segs
, count
, num_written
);
998 nrptrs
= min((iov_iter_count(&i
) + PAGE_CACHE_SIZE
- 1) /
999 PAGE_CACHE_SIZE
, PAGE_CACHE_SIZE
/
1000 (sizeof(struct page
*)));
1001 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
1007 /* generic_write_checks can change our pos */
1010 first_index
= pos
>> PAGE_CACHE_SHIFT
;
1011 last_index
= (pos
+ iov_iter_count(&i
)) >> PAGE_CACHE_SHIFT
;
1013 while (iov_iter_count(&i
) > 0) {
1014 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1015 size_t write_bytes
= min(iov_iter_count(&i
),
1016 nrptrs
* (size_t)PAGE_CACHE_SIZE
-
1018 size_t num_pages
= (write_bytes
+ offset
+
1019 PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
1021 WARN_ON(num_pages
> nrptrs
);
1022 memset(pages
, 0, sizeof(struct page
*) * nrptrs
);
1025 * Fault pages before locking them in prepare_pages
1026 * to avoid recursive lock
1028 if (unlikely(iov_iter_fault_in_readable(&i
, write_bytes
))) {
1033 ret
= btrfs_delalloc_reserve_space(inode
,
1034 num_pages
<< PAGE_CACHE_SHIFT
);
1038 ret
= prepare_pages(root
, file
, pages
, num_pages
,
1039 pos
, first_index
, last_index
,
1042 btrfs_delalloc_release_space(inode
,
1043 num_pages
<< PAGE_CACHE_SHIFT
);
1047 copied
= btrfs_copy_from_user(pos
, num_pages
,
1048 write_bytes
, pages
, &i
);
1051 * if we have trouble faulting in the pages, fall
1052 * back to one page at a time
1054 if (copied
< write_bytes
)
1060 dirty_pages
= (copied
+ offset
+
1061 PAGE_CACHE_SIZE
- 1) >>
1064 if (num_pages
> dirty_pages
) {
1067 &BTRFS_I(inode
)->outstanding_extents
);
1068 btrfs_delalloc_release_space(inode
,
1069 (num_pages
- dirty_pages
) <<
1074 dirty_and_release_pages(NULL
, root
, file
, pages
,
1075 dirty_pages
, pos
, copied
);
1078 btrfs_drop_pages(pages
, num_pages
);
1082 filemap_fdatawrite_range(inode
->i_mapping
, pos
,
1085 balance_dirty_pages_ratelimited_nr(
1089 (root
->leafsize
>> PAGE_CACHE_SHIFT
) + 1)
1090 btrfs_btree_balance_dirty(root
, 1);
1091 btrfs_throttle(root
);
1096 num_written
+= copied
;
1101 mutex_unlock(&inode
->i_mutex
);
1109 * we want to make sure fsync finds this change
1110 * but we haven't joined a transaction running right now.
1112 * Later on, someone is sure to update the inode and get the
1113 * real transid recorded.
1115 * We set last_trans now to the fs_info generation + 1,
1116 * this will either be one more than the running transaction
1117 * or the generation used for the next transaction if there isn't
1118 * one running right now.
1120 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
+ 1;
1122 if (num_written
> 0 && will_write
) {
1123 struct btrfs_trans_handle
*trans
;
1125 err
= btrfs_wait_ordered_range(inode
, start_pos
, num_written
);
1129 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
1130 trans
= btrfs_start_transaction(root
, 0);
1131 if (IS_ERR(trans
)) {
1132 num_written
= PTR_ERR(trans
);
1135 mutex_lock(&inode
->i_mutex
);
1136 ret
= btrfs_log_dentry_safe(trans
, root
,
1138 mutex_unlock(&inode
->i_mutex
);
1140 ret
= btrfs_sync_log(trans
, root
);
1142 btrfs_end_transaction(trans
, root
);
1144 btrfs_commit_transaction(trans
, root
);
1145 } else if (ret
!= BTRFS_NO_LOG_SYNC
) {
1146 btrfs_commit_transaction(trans
, root
);
1148 btrfs_end_transaction(trans
, root
);
1151 if (file
->f_flags
& O_DIRECT
&& buffered
) {
1152 invalidate_mapping_pages(inode
->i_mapping
,
1153 start_pos
>> PAGE_CACHE_SHIFT
,
1154 (start_pos
+ num_written
- 1) >> PAGE_CACHE_SHIFT
);
1158 current
->backing_dev_info
= NULL
;
1159 return num_written
? num_written
: err
;
1162 int btrfs_release_file(struct inode
*inode
, struct file
*filp
)
1165 * ordered_data_close is set by settattr when we are about to truncate
1166 * a file from a non-zero size to a zero size. This tries to
1167 * flush down new bytes that may have been written if the
1168 * application were using truncate to replace a file in place.
1170 if (BTRFS_I(inode
)->ordered_data_close
) {
1171 BTRFS_I(inode
)->ordered_data_close
= 0;
1172 btrfs_add_ordered_operation(NULL
, BTRFS_I(inode
)->root
, inode
);
1173 if (inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
1174 filemap_flush(inode
->i_mapping
);
1176 if (filp
->private_data
)
1177 btrfs_ioctl_trans_end(filp
);
1182 * fsync call for both files and directories. This logs the inode into
1183 * the tree log instead of forcing full commits whenever possible.
1185 * It needs to call filemap_fdatawait so that all ordered extent updates are
1186 * in the metadata btree are up to date for copying to the log.
1188 * It drops the inode mutex before doing the tree log commit. This is an
1189 * important optimization for directories because holding the mutex prevents
1190 * new operations on the dir while we write to disk.
1192 int btrfs_sync_file(struct file
*file
, int datasync
)
1194 struct dentry
*dentry
= file
->f_path
.dentry
;
1195 struct inode
*inode
= dentry
->d_inode
;
1196 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1198 struct btrfs_trans_handle
*trans
;
1201 /* we wait first, since the writeback may change the inode */
1203 /* the VFS called filemap_fdatawrite for us */
1204 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1208 * check the transaction that last modified this inode
1209 * and see if its already been committed
1211 if (!BTRFS_I(inode
)->last_trans
)
1215 * if the last transaction that changed this file was before
1216 * the current transaction, we can bail out now without any
1219 mutex_lock(&root
->fs_info
->trans_mutex
);
1220 if (BTRFS_I(inode
)->last_trans
<=
1221 root
->fs_info
->last_trans_committed
) {
1222 BTRFS_I(inode
)->last_trans
= 0;
1223 mutex_unlock(&root
->fs_info
->trans_mutex
);
1226 mutex_unlock(&root
->fs_info
->trans_mutex
);
1229 * ok we haven't committed the transaction yet, lets do a commit
1231 if (file
->private_data
)
1232 btrfs_ioctl_trans_end(file
);
1234 trans
= btrfs_start_transaction(root
, 0);
1235 if (IS_ERR(trans
)) {
1236 ret
= PTR_ERR(trans
);
1240 ret
= btrfs_log_dentry_safe(trans
, root
, dentry
);
1244 /* we've logged all the items and now have a consistent
1245 * version of the file in the log. It is possible that
1246 * someone will come in and modify the file, but that's
1247 * fine because the log is consistent on disk, and we
1248 * have references to all of the file's extents
1250 * It is possible that someone will come in and log the
1251 * file again, but that will end up using the synchronization
1252 * inside btrfs_sync_log to keep things safe.
1254 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1256 if (ret
!= BTRFS_NO_LOG_SYNC
) {
1258 ret
= btrfs_commit_transaction(trans
, root
);
1260 ret
= btrfs_sync_log(trans
, root
);
1262 ret
= btrfs_end_transaction(trans
, root
);
1264 ret
= btrfs_commit_transaction(trans
, root
);
1267 ret
= btrfs_end_transaction(trans
, root
);
1269 mutex_lock(&dentry
->d_inode
->i_mutex
);
1271 return ret
> 0 ? -EIO
: ret
;
1274 static const struct vm_operations_struct btrfs_file_vm_ops
= {
1275 .fault
= filemap_fault
,
1276 .page_mkwrite
= btrfs_page_mkwrite
,
1279 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1281 struct address_space
*mapping
= filp
->f_mapping
;
1283 if (!mapping
->a_ops
->readpage
)
1286 file_accessed(filp
);
1287 vma
->vm_ops
= &btrfs_file_vm_ops
;
1288 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1293 static long btrfs_fallocate(struct file
*file
, int mode
,
1294 loff_t offset
, loff_t len
)
1296 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1297 struct extent_state
*cached_state
= NULL
;
1304 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
1305 struct extent_map
*em
;
1308 alloc_start
= offset
& ~mask
;
1309 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
1311 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1312 if (mode
& ~FALLOC_FL_KEEP_SIZE
)
1316 * wait for ordered IO before we have any locks. We'll loop again
1317 * below with the locks held.
1319 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
1321 mutex_lock(&inode
->i_mutex
);
1322 ret
= inode_newsize_ok(inode
, alloc_end
);
1326 if (alloc_start
> inode
->i_size
) {
1327 ret
= btrfs_cont_expand(inode
, alloc_start
);
1332 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
1336 locked_end
= alloc_end
- 1;
1338 struct btrfs_ordered_extent
*ordered
;
1340 /* the extent lock is ordered inside the running
1343 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
1344 locked_end
, 0, &cached_state
, GFP_NOFS
);
1345 ordered
= btrfs_lookup_first_ordered_extent(inode
,
1348 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
1349 ordered
->file_offset
< alloc_end
) {
1350 btrfs_put_ordered_extent(ordered
);
1351 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1352 alloc_start
, locked_end
,
1353 &cached_state
, GFP_NOFS
);
1355 * we can't wait on the range with the transaction
1356 * running or with the extent lock held
1358 btrfs_wait_ordered_range(inode
, alloc_start
,
1359 alloc_end
- alloc_start
);
1362 btrfs_put_ordered_extent(ordered
);
1367 cur_offset
= alloc_start
;
1369 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
1370 alloc_end
- cur_offset
, 0);
1371 BUG_ON(IS_ERR(em
) || !em
);
1372 last_byte
= min(extent_map_end(em
), alloc_end
);
1373 last_byte
= (last_byte
+ mask
) & ~mask
;
1374 if (em
->block_start
== EXTENT_MAP_HOLE
||
1375 (cur_offset
>= inode
->i_size
&&
1376 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
1377 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
1378 last_byte
- cur_offset
,
1379 1 << inode
->i_blkbits
,
1383 free_extent_map(em
);
1387 free_extent_map(em
);
1389 cur_offset
= last_byte
;
1390 if (cur_offset
>= alloc_end
) {
1395 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
1396 &cached_state
, GFP_NOFS
);
1398 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
1400 mutex_unlock(&inode
->i_mutex
);
1404 const struct file_operations btrfs_file_operations
= {
1405 .llseek
= generic_file_llseek
,
1406 .read
= do_sync_read
,
1407 .write
= do_sync_write
,
1408 .aio_read
= generic_file_aio_read
,
1409 .splice_read
= generic_file_splice_read
,
1410 .aio_write
= btrfs_file_aio_write
,
1411 .mmap
= btrfs_file_mmap
,
1412 .open
= generic_file_open
,
1413 .release
= btrfs_release_file
,
1414 .fsync
= btrfs_sync_file
,
1415 .fallocate
= btrfs_fallocate
,
1416 .unlocked_ioctl
= btrfs_ioctl
,
1417 #ifdef CONFIG_COMPAT
1418 .compat_ioctl
= btrfs_ioctl
,