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
);
190 write_lock(&em_tree
->lock
);
191 em
= lookup_extent_mapping(em_tree
, start
, len
);
193 write_unlock(&em_tree
->lock
);
197 if (skip_pinned
&& test_bit(EXTENT_FLAG_PINNED
, &em
->flags
)) {
198 if (testend
&& em
->start
+ em
->len
>= start
+ len
) {
200 write_unlock(&em_tree
->lock
);
203 start
= em
->start
+ em
->len
;
205 len
= start
+ len
- (em
->start
+ em
->len
);
207 write_unlock(&em_tree
->lock
);
210 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
211 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
212 remove_extent_mapping(em_tree
, em
);
214 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
216 split
->start
= em
->start
;
217 split
->len
= start
- em
->start
;
218 split
->orig_start
= em
->orig_start
;
219 split
->block_start
= em
->block_start
;
222 split
->block_len
= em
->block_len
;
224 split
->block_len
= split
->len
;
226 split
->bdev
= em
->bdev
;
227 split
->flags
= flags
;
228 ret
= add_extent_mapping(em_tree
, split
);
230 free_extent_map(split
);
234 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
235 testend
&& em
->start
+ em
->len
> start
+ len
) {
236 u64 diff
= start
+ len
- em
->start
;
238 split
->start
= start
+ len
;
239 split
->len
= em
->start
+ em
->len
- (start
+ len
);
240 split
->bdev
= em
->bdev
;
241 split
->flags
= flags
;
244 split
->block_len
= em
->block_len
;
245 split
->block_start
= em
->block_start
;
246 split
->orig_start
= em
->orig_start
;
248 split
->block_len
= split
->len
;
249 split
->block_start
= em
->block_start
+ diff
;
250 split
->orig_start
= split
->start
;
253 ret
= add_extent_mapping(em_tree
, split
);
255 free_extent_map(split
);
258 write_unlock(&em_tree
->lock
);
262 /* once for the tree*/
266 free_extent_map(split
);
268 free_extent_map(split2
);
273 * this is very complex, but the basic idea is to drop all extents
274 * in the range start - end. hint_block is filled in with a block number
275 * that would be a good hint to the block allocator for this file.
277 * If an extent intersects the range but is not entirely inside the range
278 * it is either truncated or split. Anything entirely inside the range
279 * is deleted from the tree.
281 int btrfs_drop_extents(struct btrfs_trans_handle
*trans
, struct inode
*inode
,
282 u64 start
, u64 end
, u64
*hint_byte
, int drop_cache
)
284 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
285 struct extent_buffer
*leaf
;
286 struct btrfs_file_extent_item
*fi
;
287 struct btrfs_path
*path
;
288 struct btrfs_key key
;
289 struct btrfs_key new_key
;
290 u64 search_start
= start
;
293 u64 extent_offset
= 0;
302 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
304 path
= btrfs_alloc_path();
310 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
314 if (ret
> 0 && path
->slots
[0] > 0 && search_start
== start
) {
315 leaf
= path
->nodes
[0];
316 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0] - 1);
317 if (key
.objectid
== inode
->i_ino
&&
318 key
.type
== BTRFS_EXTENT_DATA_KEY
)
323 leaf
= path
->nodes
[0];
324 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
326 ret
= btrfs_next_leaf(root
, path
);
333 leaf
= path
->nodes
[0];
337 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
338 if (key
.objectid
> inode
->i_ino
||
339 key
.type
> BTRFS_EXTENT_DATA_KEY
|| key
.offset
>= end
)
342 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
343 struct btrfs_file_extent_item
);
344 extent_type
= btrfs_file_extent_type(leaf
, fi
);
346 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
347 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
348 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
349 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
350 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
351 extent_end
= key
.offset
+
352 btrfs_file_extent_num_bytes(leaf
, fi
);
353 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
354 extent_end
= key
.offset
+
355 btrfs_file_extent_inline_len(leaf
, fi
);
358 extent_end
= search_start
;
361 if (extent_end
<= search_start
) {
366 search_start
= max(key
.offset
, start
);
368 btrfs_release_path(root
, path
);
373 * | - range to drop - |
374 * | -------- extent -------- |
376 if (start
> key
.offset
&& end
< extent_end
) {
378 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
380 memcpy(&new_key
, &key
, sizeof(new_key
));
381 new_key
.offset
= start
;
382 ret
= btrfs_duplicate_item(trans
, root
, path
,
384 if (ret
== -EAGAIN
) {
385 btrfs_release_path(root
, path
);
391 leaf
= path
->nodes
[0];
392 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
393 struct btrfs_file_extent_item
);
394 btrfs_set_file_extent_num_bytes(leaf
, fi
,
397 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
398 struct btrfs_file_extent_item
);
400 extent_offset
+= start
- key
.offset
;
401 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
402 btrfs_set_file_extent_num_bytes(leaf
, fi
,
404 btrfs_mark_buffer_dirty(leaf
);
406 if (disk_bytenr
> 0) {
407 ret
= btrfs_inc_extent_ref(trans
, root
,
408 disk_bytenr
, num_bytes
, 0,
409 root
->root_key
.objectid
,
411 start
- extent_offset
);
413 *hint_byte
= disk_bytenr
;
418 * | ---- range to drop ----- |
419 * | -------- extent -------- |
421 if (start
<= key
.offset
&& end
< extent_end
) {
422 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
424 memcpy(&new_key
, &key
, sizeof(new_key
));
425 new_key
.offset
= end
;
426 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
428 extent_offset
+= end
- key
.offset
;
429 btrfs_set_file_extent_offset(leaf
, fi
, extent_offset
);
430 btrfs_set_file_extent_num_bytes(leaf
, fi
,
432 btrfs_mark_buffer_dirty(leaf
);
433 if (disk_bytenr
> 0) {
434 inode_sub_bytes(inode
, end
- key
.offset
);
435 *hint_byte
= disk_bytenr
;
440 search_start
= extent_end
;
442 * | ---- range to drop ----- |
443 * | -------- extent -------- |
445 if (start
> key
.offset
&& end
>= extent_end
) {
447 BUG_ON(extent_type
== BTRFS_FILE_EXTENT_INLINE
);
449 btrfs_set_file_extent_num_bytes(leaf
, fi
,
451 btrfs_mark_buffer_dirty(leaf
);
452 if (disk_bytenr
> 0) {
453 inode_sub_bytes(inode
, extent_end
- start
);
454 *hint_byte
= disk_bytenr
;
456 if (end
== extent_end
)
464 * | ---- range to drop ----- |
465 * | ------ extent ------ |
467 if (start
<= key
.offset
&& end
>= extent_end
) {
469 del_slot
= path
->slots
[0];
472 BUG_ON(del_slot
+ del_nr
!= path
->slots
[0]);
476 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
477 inode_sub_bytes(inode
,
478 extent_end
- key
.offset
);
479 extent_end
= ALIGN(extent_end
,
481 } else if (disk_bytenr
> 0) {
482 ret
= btrfs_free_extent(trans
, root
,
483 disk_bytenr
, num_bytes
, 0,
484 root
->root_key
.objectid
,
485 key
.objectid
, key
.offset
-
488 inode_sub_bytes(inode
,
489 extent_end
- key
.offset
);
490 *hint_byte
= disk_bytenr
;
493 if (end
== extent_end
)
496 if (path
->slots
[0] + 1 < btrfs_header_nritems(leaf
)) {
501 ret
= btrfs_del_items(trans
, root
, path
, del_slot
,
508 btrfs_release_path(root
, path
);
516 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
520 btrfs_free_path(path
);
524 static int extent_mergeable(struct extent_buffer
*leaf
, int slot
,
525 u64 objectid
, u64 bytenr
, u64 orig_offset
,
526 u64
*start
, u64
*end
)
528 struct btrfs_file_extent_item
*fi
;
529 struct btrfs_key key
;
532 if (slot
< 0 || slot
>= btrfs_header_nritems(leaf
))
535 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
536 if (key
.objectid
!= objectid
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
539 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
540 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
||
541 btrfs_file_extent_disk_bytenr(leaf
, fi
) != bytenr
||
542 btrfs_file_extent_offset(leaf
, fi
) != key
.offset
- orig_offset
||
543 btrfs_file_extent_compression(leaf
, fi
) ||
544 btrfs_file_extent_encryption(leaf
, fi
) ||
545 btrfs_file_extent_other_encoding(leaf
, fi
))
548 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
549 if ((*start
&& *start
!= key
.offset
) || (*end
&& *end
!= extent_end
))
558 * Mark extent in the range start - end as written.
560 * This changes extent type from 'pre-allocated' to 'regular'. If only
561 * part of extent is marked as written, the extent will be split into
564 int btrfs_mark_extent_written(struct btrfs_trans_handle
*trans
,
565 struct inode
*inode
, u64 start
, u64 end
)
567 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
568 struct extent_buffer
*leaf
;
569 struct btrfs_path
*path
;
570 struct btrfs_file_extent_item
*fi
;
571 struct btrfs_key key
;
572 struct btrfs_key new_key
;
585 btrfs_drop_extent_cache(inode
, start
, end
- 1, 0);
587 path
= btrfs_alloc_path();
592 key
.objectid
= inode
->i_ino
;
593 key
.type
= BTRFS_EXTENT_DATA_KEY
;
596 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
597 if (ret
> 0 && path
->slots
[0] > 0)
600 leaf
= path
->nodes
[0];
601 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
602 BUG_ON(key
.objectid
!= inode
->i_ino
||
603 key
.type
!= BTRFS_EXTENT_DATA_KEY
);
604 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
605 struct btrfs_file_extent_item
);
606 BUG_ON(btrfs_file_extent_type(leaf
, fi
) !=
607 BTRFS_FILE_EXTENT_PREALLOC
);
608 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
609 BUG_ON(key
.offset
> start
|| extent_end
< end
);
611 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
612 num_bytes
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
613 orig_offset
= key
.offset
- btrfs_file_extent_offset(leaf
, fi
);
614 memcpy(&new_key
, &key
, sizeof(new_key
));
616 if (start
== key
.offset
&& end
< extent_end
) {
619 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
620 inode
->i_ino
, bytenr
, orig_offset
,
621 &other_start
, &other_end
)) {
622 new_key
.offset
= end
;
623 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
624 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
625 struct btrfs_file_extent_item
);
626 btrfs_set_file_extent_num_bytes(leaf
, fi
,
628 btrfs_set_file_extent_offset(leaf
, fi
,
630 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
631 struct btrfs_file_extent_item
);
632 btrfs_set_file_extent_num_bytes(leaf
, fi
,
634 btrfs_mark_buffer_dirty(leaf
);
639 if (start
> key
.offset
&& end
== extent_end
) {
642 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
643 inode
->i_ino
, bytenr
, orig_offset
,
644 &other_start
, &other_end
)) {
645 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
646 struct btrfs_file_extent_item
);
647 btrfs_set_file_extent_num_bytes(leaf
, fi
,
650 new_key
.offset
= start
;
651 btrfs_set_item_key_safe(trans
, root
, path
, &new_key
);
653 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
654 struct btrfs_file_extent_item
);
655 btrfs_set_file_extent_num_bytes(leaf
, fi
,
657 btrfs_set_file_extent_offset(leaf
, fi
,
658 start
- orig_offset
);
659 btrfs_mark_buffer_dirty(leaf
);
664 while (start
> key
.offset
|| end
< extent_end
) {
665 if (key
.offset
== start
)
668 new_key
.offset
= split
;
669 ret
= btrfs_duplicate_item(trans
, root
, path
, &new_key
);
670 if (ret
== -EAGAIN
) {
671 btrfs_release_path(root
, path
);
676 leaf
= path
->nodes
[0];
677 fi
= btrfs_item_ptr(leaf
, path
->slots
[0] - 1,
678 struct btrfs_file_extent_item
);
679 btrfs_set_file_extent_num_bytes(leaf
, fi
,
682 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
683 struct btrfs_file_extent_item
);
685 btrfs_set_file_extent_offset(leaf
, fi
, split
- orig_offset
);
686 btrfs_set_file_extent_num_bytes(leaf
, fi
,
688 btrfs_mark_buffer_dirty(leaf
);
690 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
, num_bytes
, 0,
691 root
->root_key
.objectid
,
692 inode
->i_ino
, orig_offset
);
695 if (split
== start
) {
698 BUG_ON(start
!= key
.offset
);
707 if (extent_mergeable(leaf
, path
->slots
[0] + 1,
708 inode
->i_ino
, bytenr
, orig_offset
,
709 &other_start
, &other_end
)) {
711 btrfs_release_path(root
, path
);
714 extent_end
= other_end
;
715 del_slot
= path
->slots
[0] + 1;
717 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
718 0, root
->root_key
.objectid
,
719 inode
->i_ino
, orig_offset
);
724 if (extent_mergeable(leaf
, path
->slots
[0] - 1,
725 inode
->i_ino
, bytenr
, orig_offset
,
726 &other_start
, &other_end
)) {
728 btrfs_release_path(root
, path
);
731 key
.offset
= other_start
;
732 del_slot
= path
->slots
[0];
734 ret
= btrfs_free_extent(trans
, root
, bytenr
, num_bytes
,
735 0, root
->root_key
.objectid
,
736 inode
->i_ino
, orig_offset
);
740 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
741 struct btrfs_file_extent_item
);
742 btrfs_set_file_extent_type(leaf
, fi
,
743 BTRFS_FILE_EXTENT_REG
);
744 btrfs_mark_buffer_dirty(leaf
);
746 fi
= btrfs_item_ptr(leaf
, del_slot
- 1,
747 struct btrfs_file_extent_item
);
748 btrfs_set_file_extent_type(leaf
, fi
,
749 BTRFS_FILE_EXTENT_REG
);
750 btrfs_set_file_extent_num_bytes(leaf
, fi
,
751 extent_end
- key
.offset
);
752 btrfs_mark_buffer_dirty(leaf
);
754 ret
= btrfs_del_items(trans
, root
, path
, del_slot
, del_nr
);
758 btrfs_free_path(path
);
763 * this gets pages into the page cache and locks them down, it also properly
764 * waits for data=ordered extents to finish before allowing the pages to be
767 static noinline
int prepare_pages(struct btrfs_root
*root
, struct file
*file
,
768 struct page
**pages
, size_t num_pages
,
769 loff_t pos
, unsigned long first_index
,
770 unsigned long last_index
, size_t write_bytes
)
772 struct extent_state
*cached_state
= NULL
;
774 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
775 struct inode
*inode
= fdentry(file
)->d_inode
;
780 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
781 last_pos
= ((u64
)index
+ num_pages
) << PAGE_CACHE_SHIFT
;
783 if (start_pos
> inode
->i_size
) {
784 err
= btrfs_cont_expand(inode
, start_pos
);
789 memset(pages
, 0, num_pages
* sizeof(struct page
*));
791 for (i
= 0; i
< num_pages
; i
++) {
792 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
797 wait_on_page_writeback(pages
[i
]);
799 if (start_pos
< inode
->i_size
) {
800 struct btrfs_ordered_extent
*ordered
;
801 lock_extent_bits(&BTRFS_I(inode
)->io_tree
,
802 start_pos
, last_pos
- 1, 0, &cached_state
,
804 ordered
= btrfs_lookup_first_ordered_extent(inode
,
807 ordered
->file_offset
+ ordered
->len
> start_pos
&&
808 ordered
->file_offset
< last_pos
) {
809 btrfs_put_ordered_extent(ordered
);
810 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
811 start_pos
, last_pos
- 1,
812 &cached_state
, GFP_NOFS
);
813 for (i
= 0; i
< num_pages
; i
++) {
814 unlock_page(pages
[i
]);
815 page_cache_release(pages
[i
]);
817 btrfs_wait_ordered_range(inode
, start_pos
,
818 last_pos
- start_pos
);
822 btrfs_put_ordered_extent(ordered
);
824 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start_pos
,
825 last_pos
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
|
826 EXTENT_DO_ACCOUNTING
, 0, 0, &cached_state
,
828 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
829 start_pos
, last_pos
- 1, &cached_state
,
832 for (i
= 0; i
< num_pages
; i
++) {
833 clear_page_dirty_for_io(pages
[i
]);
834 set_page_extent_mapped(pages
[i
]);
835 WARN_ON(!PageLocked(pages
[i
]));
840 static ssize_t
btrfs_file_aio_write(struct kiocb
*iocb
,
841 const struct iovec
*iov
,
842 unsigned long nr_segs
, loff_t pos
)
844 struct file
*file
= iocb
->ki_filp
;
845 struct inode
*inode
= fdentry(file
)->d_inode
;
846 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
847 struct page
*pinned
[2];
848 struct page
**pages
= NULL
;
850 loff_t
*ppos
= &iocb
->ki_pos
;
852 ssize_t num_written
= 0;
858 unsigned long first_index
;
859 unsigned long last_index
;
865 will_write
= ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
) ||
866 (file
->f_flags
& O_DIRECT
));
873 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
875 mutex_lock(&inode
->i_mutex
);
877 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
882 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
883 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
890 err
= file_remove_suid(file
);
894 file_update_time(file
);
895 BTRFS_I(inode
)->sequence
++;
897 if (unlikely(file
->f_flags
& O_DIRECT
)) {
898 num_written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
902 * the generic O_DIRECT will update in-memory i_size after the
903 * DIOs are done. But our endio handlers that update the on
904 * disk i_size never update past the in memory i_size. So we
905 * need one more update here to catch any additions to the
908 if (inode
->i_size
!= BTRFS_I(inode
)->disk_i_size
) {
909 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
910 mark_inode_dirty(inode
);
913 if (num_written
< 0) {
917 } else if (num_written
== count
) {
918 /* pick up pos changes done by the generic code */
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 iov_iter_init(&i
, iov
, nr_segs
, count
, num_written
);
932 nrptrs
= min((iov_iter_count(&i
) + PAGE_CACHE_SIZE
- 1) /
933 PAGE_CACHE_SIZE
, PAGE_CACHE_SIZE
/
934 (sizeof(struct page
*)));
935 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
937 /* generic_write_checks can change our pos */
940 first_index
= pos
>> PAGE_CACHE_SHIFT
;
941 last_index
= (pos
+ iov_iter_count(&i
)) >> PAGE_CACHE_SHIFT
;
944 * there are lots of better ways to do this, but this code
945 * makes sure the first and last page in the file range are
946 * up to date and ready for cow
948 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
949 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
950 if (!PageUptodate(pinned
[0])) {
951 ret
= btrfs_readpage(NULL
, pinned
[0]);
953 wait_on_page_locked(pinned
[0]);
955 unlock_page(pinned
[0]);
958 if ((pos
+ iov_iter_count(&i
)) & (PAGE_CACHE_SIZE
- 1)) {
959 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
960 if (!PageUptodate(pinned
[1])) {
961 ret
= btrfs_readpage(NULL
, pinned
[1]);
963 wait_on_page_locked(pinned
[1]);
965 unlock_page(pinned
[1]);
969 while (iov_iter_count(&i
) > 0) {
970 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
971 size_t write_bytes
= min(iov_iter_count(&i
),
972 nrptrs
* (size_t)PAGE_CACHE_SIZE
-
974 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
977 WARN_ON(num_pages
> nrptrs
);
978 memset(pages
, 0, sizeof(struct page
*) * nrptrs
);
981 * Fault pages before locking them in prepare_pages
982 * to avoid recursive lock
984 if (unlikely(iov_iter_fault_in_readable(&i
, write_bytes
))) {
989 ret
= btrfs_delalloc_reserve_space(inode
,
990 num_pages
<< PAGE_CACHE_SHIFT
);
994 ret
= prepare_pages(root
, file
, pages
, num_pages
,
995 pos
, first_index
, last_index
,
998 btrfs_delalloc_release_space(inode
,
999 num_pages
<< PAGE_CACHE_SHIFT
);
1003 copied
= btrfs_copy_from_user(pos
, num_pages
,
1004 write_bytes
, pages
, &i
);
1005 dirty_pages
= (copied
+ PAGE_CACHE_SIZE
- 1) >>
1008 if (num_pages
> dirty_pages
) {
1011 &BTRFS_I(inode
)->outstanding_extents
);
1012 btrfs_delalloc_release_space(inode
,
1013 (num_pages
- dirty_pages
) <<
1018 dirty_and_release_pages(NULL
, root
, file
, pages
,
1019 dirty_pages
, pos
, copied
);
1022 btrfs_drop_pages(pages
, num_pages
);
1026 filemap_fdatawrite_range(inode
->i_mapping
, pos
,
1029 balance_dirty_pages_ratelimited_nr(
1033 (root
->leafsize
>> PAGE_CACHE_SHIFT
) + 1)
1034 btrfs_btree_balance_dirty(root
, 1);
1035 btrfs_throttle(root
);
1040 num_written
+= copied
;
1045 mutex_unlock(&inode
->i_mutex
);
1051 page_cache_release(pinned
[0]);
1053 page_cache_release(pinned
[1]);
1057 * we want to make sure fsync finds this change
1058 * but we haven't joined a transaction running right now.
1060 * Later on, someone is sure to update the inode and get the
1061 * real transid recorded.
1063 * We set last_trans now to the fs_info generation + 1,
1064 * this will either be one more than the running transaction
1065 * or the generation used for the next transaction if there isn't
1066 * one running right now.
1068 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
+ 1;
1070 if (num_written
> 0 && will_write
) {
1071 struct btrfs_trans_handle
*trans
;
1073 err
= btrfs_wait_ordered_range(inode
, start_pos
, num_written
);
1077 if ((file
->f_flags
& O_DSYNC
) || IS_SYNC(inode
)) {
1078 trans
= btrfs_start_transaction(root
, 0);
1079 if (IS_ERR(trans
)) {
1080 num_written
= PTR_ERR(trans
);
1083 mutex_lock(&inode
->i_mutex
);
1084 ret
= btrfs_log_dentry_safe(trans
, root
,
1086 mutex_unlock(&inode
->i_mutex
);
1088 ret
= btrfs_sync_log(trans
, root
);
1090 btrfs_end_transaction(trans
, root
);
1092 btrfs_commit_transaction(trans
, root
);
1093 } else if (ret
!= BTRFS_NO_LOG_SYNC
) {
1094 btrfs_commit_transaction(trans
, root
);
1096 btrfs_end_transaction(trans
, root
);
1099 if (file
->f_flags
& O_DIRECT
&& buffered
) {
1100 invalidate_mapping_pages(inode
->i_mapping
,
1101 start_pos
>> PAGE_CACHE_SHIFT
,
1102 (start_pos
+ num_written
- 1) >> PAGE_CACHE_SHIFT
);
1106 current
->backing_dev_info
= NULL
;
1107 return num_written
? num_written
: err
;
1110 int btrfs_release_file(struct inode
*inode
, struct file
*filp
)
1113 * ordered_data_close is set by settattr when we are about to truncate
1114 * a file from a non-zero size to a zero size. This tries to
1115 * flush down new bytes that may have been written if the
1116 * application were using truncate to replace a file in place.
1118 if (BTRFS_I(inode
)->ordered_data_close
) {
1119 BTRFS_I(inode
)->ordered_data_close
= 0;
1120 btrfs_add_ordered_operation(NULL
, BTRFS_I(inode
)->root
, inode
);
1121 if (inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
1122 filemap_flush(inode
->i_mapping
);
1124 if (filp
->private_data
)
1125 btrfs_ioctl_trans_end(filp
);
1130 * fsync call for both files and directories. This logs the inode into
1131 * the tree log instead of forcing full commits whenever possible.
1133 * It needs to call filemap_fdatawait so that all ordered extent updates are
1134 * in the metadata btree are up to date for copying to the log.
1136 * It drops the inode mutex before doing the tree log commit. This is an
1137 * important optimization for directories because holding the mutex prevents
1138 * new operations on the dir while we write to disk.
1140 int btrfs_sync_file(struct file
*file
, int datasync
)
1142 struct dentry
*dentry
= file
->f_path
.dentry
;
1143 struct inode
*inode
= dentry
->d_inode
;
1144 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1146 struct btrfs_trans_handle
*trans
;
1149 /* we wait first, since the writeback may change the inode */
1151 /* the VFS called filemap_fdatawrite for us */
1152 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1156 * check the transaction that last modified this inode
1157 * and see if its already been committed
1159 if (!BTRFS_I(inode
)->last_trans
)
1163 * if the last transaction that changed this file was before
1164 * the current transaction, we can bail out now without any
1167 mutex_lock(&root
->fs_info
->trans_mutex
);
1168 if (BTRFS_I(inode
)->last_trans
<=
1169 root
->fs_info
->last_trans_committed
) {
1170 BTRFS_I(inode
)->last_trans
= 0;
1171 mutex_unlock(&root
->fs_info
->trans_mutex
);
1174 mutex_unlock(&root
->fs_info
->trans_mutex
);
1177 * ok we haven't committed the transaction yet, lets do a commit
1179 if (file
->private_data
)
1180 btrfs_ioctl_trans_end(file
);
1182 trans
= btrfs_start_transaction(root
, 0);
1183 if (IS_ERR(trans
)) {
1184 ret
= PTR_ERR(trans
);
1188 ret
= btrfs_log_dentry_safe(trans
, root
, dentry
);
1192 /* we've logged all the items and now have a consistent
1193 * version of the file in the log. It is possible that
1194 * someone will come in and modify the file, but that's
1195 * fine because the log is consistent on disk, and we
1196 * have references to all of the file's extents
1198 * It is possible that someone will come in and log the
1199 * file again, but that will end up using the synchronization
1200 * inside btrfs_sync_log to keep things safe.
1202 mutex_unlock(&dentry
->d_inode
->i_mutex
);
1204 if (ret
!= BTRFS_NO_LOG_SYNC
) {
1206 ret
= btrfs_commit_transaction(trans
, root
);
1208 ret
= btrfs_sync_log(trans
, root
);
1210 ret
= btrfs_end_transaction(trans
, root
);
1212 ret
= btrfs_commit_transaction(trans
, root
);
1215 ret
= btrfs_end_transaction(trans
, root
);
1217 mutex_lock(&dentry
->d_inode
->i_mutex
);
1219 return ret
> 0 ? -EIO
: ret
;
1222 static const struct vm_operations_struct btrfs_file_vm_ops
= {
1223 .fault
= filemap_fault
,
1224 .page_mkwrite
= btrfs_page_mkwrite
,
1227 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1229 struct address_space
*mapping
= filp
->f_mapping
;
1231 if (!mapping
->a_ops
->readpage
)
1234 file_accessed(filp
);
1235 vma
->vm_ops
= &btrfs_file_vm_ops
;
1236 vma
->vm_flags
|= VM_CAN_NONLINEAR
;
1241 static long btrfs_fallocate(struct file
*file
, int mode
,
1242 loff_t offset
, loff_t len
)
1244 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1245 struct extent_state
*cached_state
= NULL
;
1252 u64 mask
= BTRFS_I(inode
)->root
->sectorsize
- 1;
1253 struct extent_map
*em
;
1256 alloc_start
= offset
& ~mask
;
1257 alloc_end
= (offset
+ len
+ mask
) & ~mask
;
1259 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1260 if (mode
& ~FALLOC_FL_KEEP_SIZE
)
1264 * wait for ordered IO before we have any locks. We'll loop again
1265 * below with the locks held.
1267 btrfs_wait_ordered_range(inode
, alloc_start
, alloc_end
- alloc_start
);
1269 mutex_lock(&inode
->i_mutex
);
1270 ret
= inode_newsize_ok(inode
, alloc_end
);
1274 if (alloc_start
> inode
->i_size
) {
1275 ret
= btrfs_cont_expand(inode
, alloc_start
);
1280 ret
= btrfs_check_data_free_space(inode
, alloc_end
- alloc_start
);
1284 locked_end
= alloc_end
- 1;
1286 struct btrfs_ordered_extent
*ordered
;
1288 /* the extent lock is ordered inside the running
1291 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, alloc_start
,
1292 locked_end
, 0, &cached_state
, GFP_NOFS
);
1293 ordered
= btrfs_lookup_first_ordered_extent(inode
,
1296 ordered
->file_offset
+ ordered
->len
> alloc_start
&&
1297 ordered
->file_offset
< alloc_end
) {
1298 btrfs_put_ordered_extent(ordered
);
1299 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
1300 alloc_start
, locked_end
,
1301 &cached_state
, GFP_NOFS
);
1303 * we can't wait on the range with the transaction
1304 * running or with the extent lock held
1306 btrfs_wait_ordered_range(inode
, alloc_start
,
1307 alloc_end
- alloc_start
);
1310 btrfs_put_ordered_extent(ordered
);
1315 cur_offset
= alloc_start
;
1317 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
1318 alloc_end
- cur_offset
, 0);
1319 BUG_ON(IS_ERR(em
) || !em
);
1320 last_byte
= min(extent_map_end(em
), alloc_end
);
1321 last_byte
= (last_byte
+ mask
) & ~mask
;
1322 if (em
->block_start
== EXTENT_MAP_HOLE
||
1323 (cur_offset
>= inode
->i_size
&&
1324 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
1325 ret
= btrfs_prealloc_file_range(inode
, mode
, cur_offset
,
1326 last_byte
- cur_offset
,
1327 1 << inode
->i_blkbits
,
1331 free_extent_map(em
);
1335 free_extent_map(em
);
1337 cur_offset
= last_byte
;
1338 if (cur_offset
>= alloc_end
) {
1343 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, alloc_start
, locked_end
,
1344 &cached_state
, GFP_NOFS
);
1346 btrfs_free_reserved_data_space(inode
, alloc_end
- alloc_start
);
1348 mutex_unlock(&inode
->i_mutex
);
1352 const struct file_operations btrfs_file_operations
= {
1353 .llseek
= generic_file_llseek
,
1354 .read
= do_sync_read
,
1355 .write
= do_sync_write
,
1356 .aio_read
= generic_file_aio_read
,
1357 .splice_read
= generic_file_splice_read
,
1358 .aio_write
= btrfs_file_aio_write
,
1359 .mmap
= btrfs_file_mmap
,
1360 .open
= generic_file_open
,
1361 .release
= btrfs_release_file
,
1362 .fsync
= btrfs_sync_file
,
1363 .fallocate
= btrfs_fallocate
,
1364 .unlocked_ioctl
= btrfs_ioctl
,
1365 #ifdef CONFIG_COMPAT
1366 .compat_ioctl
= btrfs_ioctl
,