2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/time.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/backing-dev.h>
26 #include <linux/mpage.h>
27 #include <linux/swap.h>
28 #include <linux/writeback.h>
29 #include <linux/statfs.h>
30 #include <linux/compat.h>
31 #include <linux/slab.h>
34 #include "transaction.h"
35 #include "btrfs_inode.h"
37 #include "print-tree.h"
43 /* simple helper to fault in pages and copy. This should go away
44 * and be replaced with calls into generic code.
46 static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
48 struct page **prepared_pages,
53 int offset = pos & (PAGE_CACHE_SIZE - 1);
55 while (write_bytes > 0) {
56 size_t count = min_t(size_t,
57 PAGE_CACHE_SIZE - offset, write_bytes);
58 struct page *page = prepared_pages[pg];
60 if (unlikely(iov_iter_fault_in_readable(i, count)))
63 /* Copy data from userspace to the current page */
64 copied = iov_iter_copy_from_user(page, i, offset, count);
66 /* Flush processor's dcache for this page */
67 flush_dcache_page(page);
68 iov_iter_advance(i, copied);
69 write_bytes -= copied;
71 if (unlikely(copied == 0)) {
72 count = min_t(size_t, PAGE_CACHE_SIZE - offset,
73 iov_iter_single_seg_count(i));
77 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
88 * unlocks pages after btrfs_file_write is done with them
90 static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
93 for (i = 0; i < num_pages; i++) {
96 /* page checked is some magic around finding pages that
97 * have been modified without going through btrfs_set_page_dirty
100 ClearPageChecked(pages[i]);
101 unlock_page(pages[i]);
102 mark_page_accessed(pages[i]);
103 page_cache_release(pages[i]);
108 * after copy_from_user, pages need to be dirtied and we need to make
109 * sure holes are created between the current EOF and the start of
110 * any next extents (if required).
112 * this also makes the decision about creating an inline extent vs
113 * doing real data extents, marking pages dirty and delalloc as required.
115 static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
116 struct btrfs_root *root,
125 struct inode *inode = fdentry(file)->d_inode;
128 u64 end_of_last_block;
129 u64 end_pos = pos + write_bytes;
130 loff_t isize = i_size_read(inode);
132 start_pos = pos & ~((u64)root->sectorsize - 1);
133 num_bytes = (write_bytes + pos - start_pos +
134 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
136 end_of_last_block = start_pos + num_bytes - 1;
137 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
141 for (i = 0; i < num_pages; i++) {
142 struct page *p = pages[i];
147 if (end_pos > isize) {
148 i_size_write(inode, end_pos);
149 /* we've only changed i_size in ram, and we haven't updated
150 * the disk i_size. There is no need to log the inode
158 * this drops all the extents in the cache that intersect the range
159 * [start, end]. Existing extents are split as required.
161 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
164 struct extent_map *em;
165 struct extent_map *split = NULL;
166 struct extent_map *split2 = NULL;
167 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
168 u64 len = end - start + 1;
174 WARN_ON(end < start);
175 if (end == (u64)-1) {
181 split = alloc_extent_map(GFP_NOFS);
183 split2 = alloc_extent_map(GFP_NOFS);
185 write_lock(&em_tree->lock);
186 em = lookup_extent_mapping(em_tree, start, len);
188 write_unlock(&em_tree->lock);
192 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
193 if (testend && em->start + em->len >= start + len) {
195 write_unlock(&em_tree->lock);
198 start = em->start + em->len;
200 len = start + len - (em->start + em->len);
202 write_unlock(&em_tree->lock);
205 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
206 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
207 remove_extent_mapping(em_tree, em);
209 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
211 split->start = em->start;
212 split->len = start - em->start;
213 split->orig_start = em->orig_start;
214 split->block_start = em->block_start;
217 split->block_len = em->block_len;
219 split->block_len = split->len;
221 split->bdev = em->bdev;
222 split->flags = flags;
223 ret = add_extent_mapping(em_tree, split);
225 free_extent_map(split);
229 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
230 testend && em->start + em->len > start + len) {
231 u64 diff = start + len - em->start;
233 split->start = start + len;
234 split->len = em->start + em->len - (start + len);
235 split->bdev = em->bdev;
236 split->flags = flags;
239 split->block_len = em->block_len;
240 split->block_start = em->block_start;
241 split->orig_start = em->orig_start;
243 split->block_len = split->len;
244 split->block_start = em->block_start + diff;
245 split->orig_start = split->start;
248 ret = add_extent_mapping(em_tree, split);
250 free_extent_map(split);
253 write_unlock(&em_tree->lock);
257 /* once for the tree*/
261 free_extent_map(split);
263 free_extent_map(split2);
268 * this is very complex, but the basic idea is to drop all extents
269 * in the range start - end. hint_block is filled in with a block number
270 * that would be a good hint to the block allocator for this file.
272 * If an extent intersects the range but is not entirely inside the range
273 * it is either truncated or split. Anything entirely inside the range
274 * is deleted from the tree.
276 int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
277 u64 start, u64 end, u64 *hint_byte, int drop_cache)
279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 struct extent_buffer *leaf;
281 struct btrfs_file_extent_item *fi;
282 struct btrfs_path *path;
283 struct btrfs_key key;
284 struct btrfs_key new_key;
285 u64 search_start = start;
288 u64 extent_offset = 0;
297 btrfs_drop_extent_cache(inode, start, end - 1, 0);
299 path = btrfs_alloc_path();
305 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
309 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
310 leaf = path->nodes[0];
311 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
312 if (key.objectid == inode->i_ino &&
313 key.type == BTRFS_EXTENT_DATA_KEY)
318 leaf = path->nodes[0];
319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
321 ret = btrfs_next_leaf(root, path);
328 leaf = path->nodes[0];
332 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
333 if (key.objectid > inode->i_ino ||
334 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
337 fi = btrfs_item_ptr(leaf, path->slots[0],
338 struct btrfs_file_extent_item);
339 extent_type = btrfs_file_extent_type(leaf, fi);
341 if (extent_type == BTRFS_FILE_EXTENT_REG ||
342 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
343 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
344 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
345 extent_offset = btrfs_file_extent_offset(leaf, fi);
346 extent_end = key.offset +
347 btrfs_file_extent_num_bytes(leaf, fi);
348 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
349 extent_end = key.offset +
350 btrfs_file_extent_inline_len(leaf, fi);
353 extent_end = search_start;
356 if (extent_end <= search_start) {
361 search_start = max(key.offset, start);
363 btrfs_release_path(root, path);
368 * | - range to drop - |
369 * | -------- extent -------- |
371 if (start > key.offset && end < extent_end) {
373 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
375 memcpy(&new_key, &key, sizeof(new_key));
376 new_key.offset = start;
377 ret = btrfs_duplicate_item(trans, root, path,
379 if (ret == -EAGAIN) {
380 btrfs_release_path(root, path);
386 leaf = path->nodes[0];
387 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
388 struct btrfs_file_extent_item);
389 btrfs_set_file_extent_num_bytes(leaf, fi,
392 fi = btrfs_item_ptr(leaf, path->slots[0],
393 struct btrfs_file_extent_item);
395 extent_offset += start - key.offset;
396 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
397 btrfs_set_file_extent_num_bytes(leaf, fi,
399 btrfs_mark_buffer_dirty(leaf);
401 if (disk_bytenr > 0) {
402 ret = btrfs_inc_extent_ref(trans, root,
403 disk_bytenr, num_bytes, 0,
404 root->root_key.objectid,
406 start - extent_offset);
408 *hint_byte = disk_bytenr;
413 * | ---- range to drop ----- |
414 * | -------- extent -------- |
416 if (start <= key.offset && end < extent_end) {
417 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
419 memcpy(&new_key, &key, sizeof(new_key));
420 new_key.offset = end;
421 btrfs_set_item_key_safe(trans, root, path, &new_key);
423 extent_offset += end - key.offset;
424 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
425 btrfs_set_file_extent_num_bytes(leaf, fi,
427 btrfs_mark_buffer_dirty(leaf);
428 if (disk_bytenr > 0) {
429 inode_sub_bytes(inode, end - key.offset);
430 *hint_byte = disk_bytenr;
435 search_start = extent_end;
437 * | ---- range to drop ----- |
438 * | -------- extent -------- |
440 if (start > key.offset && end >= extent_end) {
442 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
444 btrfs_set_file_extent_num_bytes(leaf, fi,
446 btrfs_mark_buffer_dirty(leaf);
447 if (disk_bytenr > 0) {
448 inode_sub_bytes(inode, extent_end - start);
449 *hint_byte = disk_bytenr;
451 if (end == extent_end)
459 * | ---- range to drop ----- |
460 * | ------ extent ------ |
462 if (start <= key.offset && end >= extent_end) {
464 del_slot = path->slots[0];
467 BUG_ON(del_slot + del_nr != path->slots[0]);
471 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
472 inode_sub_bytes(inode,
473 extent_end - key.offset);
474 extent_end = ALIGN(extent_end,
476 } else if (disk_bytenr > 0) {
477 ret = btrfs_free_extent(trans, root,
478 disk_bytenr, num_bytes, 0,
479 root->root_key.objectid,
480 key.objectid, key.offset -
483 inode_sub_bytes(inode,
484 extent_end - key.offset);
485 *hint_byte = disk_bytenr;
488 if (end == extent_end)
491 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
496 ret = btrfs_del_items(trans, root, path, del_slot,
503 btrfs_release_path(root, path);
511 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
515 btrfs_free_path(path);
519 static int extent_mergeable(struct extent_buffer *leaf, int slot,
520 u64 objectid, u64 bytenr, u64 orig_offset,
521 u64 *start, u64 *end)
523 struct btrfs_file_extent_item *fi;
524 struct btrfs_key key;
527 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
530 btrfs_item_key_to_cpu(leaf, &key, slot);
531 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
534 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
535 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
536 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
537 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
538 btrfs_file_extent_compression(leaf, fi) ||
539 btrfs_file_extent_encryption(leaf, fi) ||
540 btrfs_file_extent_other_encoding(leaf, fi))
543 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
544 if ((*start && *start != key.offset) || (*end && *end != extent_end))
553 * Mark extent in the range start - end as written.
555 * This changes extent type from 'pre-allocated' to 'regular'. If only
556 * part of extent is marked as written, the extent will be split into
559 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
560 struct inode *inode, u64 start, u64 end)
562 struct btrfs_root *root = BTRFS_I(inode)->root;
563 struct extent_buffer *leaf;
564 struct btrfs_path *path;
565 struct btrfs_file_extent_item *fi;
566 struct btrfs_key key;
567 struct btrfs_key new_key;
580 btrfs_drop_extent_cache(inode, start, end - 1, 0);
582 path = btrfs_alloc_path();
587 key.objectid = inode->i_ino;
588 key.type = BTRFS_EXTENT_DATA_KEY;
591 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
592 if (ret > 0 && path->slots[0] > 0)
595 leaf = path->nodes[0];
596 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
597 BUG_ON(key.objectid != inode->i_ino ||
598 key.type != BTRFS_EXTENT_DATA_KEY);
599 fi = btrfs_item_ptr(leaf, path->slots[0],
600 struct btrfs_file_extent_item);
601 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
602 BTRFS_FILE_EXTENT_PREALLOC);
603 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
604 BUG_ON(key.offset > start || extent_end < end);
606 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
607 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
608 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
609 memcpy(&new_key, &key, sizeof(new_key));
611 if (start == key.offset && end < extent_end) {
614 if (extent_mergeable(leaf, path->slots[0] - 1,
615 inode->i_ino, bytenr, orig_offset,
616 &other_start, &other_end)) {
617 new_key.offset = end;
618 btrfs_set_item_key_safe(trans, root, path, &new_key);
619 fi = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_file_extent_item);
621 btrfs_set_file_extent_num_bytes(leaf, fi,
623 btrfs_set_file_extent_offset(leaf, fi,
625 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
626 struct btrfs_file_extent_item);
627 btrfs_set_file_extent_num_bytes(leaf, fi,
629 btrfs_mark_buffer_dirty(leaf);
634 if (start > key.offset && end == extent_end) {
637 if (extent_mergeable(leaf, path->slots[0] + 1,
638 inode->i_ino, bytenr, orig_offset,
639 &other_start, &other_end)) {
640 fi = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_file_extent_item);
642 btrfs_set_file_extent_num_bytes(leaf, fi,
645 new_key.offset = start;
646 btrfs_set_item_key_safe(trans, root, path, &new_key);
648 fi = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_file_extent_item);
650 btrfs_set_file_extent_num_bytes(leaf, fi,
652 btrfs_set_file_extent_offset(leaf, fi,
653 start - orig_offset);
654 btrfs_mark_buffer_dirty(leaf);
659 while (start > key.offset || end < extent_end) {
660 if (key.offset == start)
663 new_key.offset = split;
664 ret = btrfs_duplicate_item(trans, root, path, &new_key);
665 if (ret == -EAGAIN) {
666 btrfs_release_path(root, path);
671 leaf = path->nodes[0];
672 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
673 struct btrfs_file_extent_item);
674 btrfs_set_file_extent_num_bytes(leaf, fi,
677 fi = btrfs_item_ptr(leaf, path->slots[0],
678 struct btrfs_file_extent_item);
680 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
681 btrfs_set_file_extent_num_bytes(leaf, fi,
683 btrfs_mark_buffer_dirty(leaf);
685 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
686 root->root_key.objectid,
687 inode->i_ino, orig_offset);
690 if (split == start) {
693 BUG_ON(start != key.offset);
702 if (extent_mergeable(leaf, path->slots[0] + 1,
703 inode->i_ino, bytenr, orig_offset,
704 &other_start, &other_end)) {
706 btrfs_release_path(root, path);
709 extent_end = other_end;
710 del_slot = path->slots[0] + 1;
712 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
713 0, root->root_key.objectid,
714 inode->i_ino, orig_offset);
719 if (extent_mergeable(leaf, path->slots[0] - 1,
720 inode->i_ino, bytenr, orig_offset,
721 &other_start, &other_end)) {
723 btrfs_release_path(root, path);
726 key.offset = other_start;
727 del_slot = path->slots[0];
729 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
730 0, root->root_key.objectid,
731 inode->i_ino, orig_offset);
735 fi = btrfs_item_ptr(leaf, path->slots[0],
736 struct btrfs_file_extent_item);
737 btrfs_set_file_extent_type(leaf, fi,
738 BTRFS_FILE_EXTENT_REG);
739 btrfs_mark_buffer_dirty(leaf);
741 fi = btrfs_item_ptr(leaf, del_slot - 1,
742 struct btrfs_file_extent_item);
743 btrfs_set_file_extent_type(leaf, fi,
744 BTRFS_FILE_EXTENT_REG);
745 btrfs_set_file_extent_num_bytes(leaf, fi,
746 extent_end - key.offset);
747 btrfs_mark_buffer_dirty(leaf);
749 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
753 btrfs_free_path(path);
758 * this gets pages into the page cache and locks them down, it also properly
759 * waits for data=ordered extents to finish before allowing the pages to be
762 static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
763 struct page **pages, size_t num_pages,
764 loff_t pos, unsigned long first_index,
765 unsigned long last_index, size_t write_bytes)
767 struct extent_state *cached_state = NULL;
769 unsigned long index = pos >> PAGE_CACHE_SHIFT;
770 struct inode *inode = fdentry(file)->d_inode;
775 start_pos = pos & ~((u64)root->sectorsize - 1);
776 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
778 if (start_pos > inode->i_size) {
779 err = btrfs_cont_expand(inode, start_pos);
784 memset(pages, 0, num_pages * sizeof(struct page *));
786 for (i = 0; i < num_pages; i++) {
787 pages[i] = grab_cache_page(inode->i_mapping, index + i);
792 wait_on_page_writeback(pages[i]);
794 if (start_pos < inode->i_size) {
795 struct btrfs_ordered_extent *ordered;
796 lock_extent_bits(&BTRFS_I(inode)->io_tree,
797 start_pos, last_pos - 1, 0, &cached_state,
799 ordered = btrfs_lookup_first_ordered_extent(inode,
802 ordered->file_offset + ordered->len > start_pos &&
803 ordered->file_offset < last_pos) {
804 btrfs_put_ordered_extent(ordered);
805 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
806 start_pos, last_pos - 1,
807 &cached_state, GFP_NOFS);
808 for (i = 0; i < num_pages; i++) {
809 unlock_page(pages[i]);
810 page_cache_release(pages[i]);
812 btrfs_wait_ordered_range(inode, start_pos,
813 last_pos - start_pos);
817 btrfs_put_ordered_extent(ordered);
819 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
820 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
821 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
823 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
824 start_pos, last_pos - 1, &cached_state,
827 for (i = 0; i < num_pages; i++) {
828 clear_page_dirty_for_io(pages[i]);
829 set_page_extent_mapped(pages[i]);
830 WARN_ON(!PageLocked(pages[i]));
835 static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
836 const struct iovec *iov,
837 unsigned long nr_segs, loff_t pos)
839 struct file *file = iocb->ki_filp;
840 struct inode *inode = fdentry(file)->d_inode;
841 struct btrfs_root *root = BTRFS_I(inode)->root;
842 struct page *pinned[2];
843 struct page **pages = NULL;
845 loff_t *ppos = &iocb->ki_pos;
847 ssize_t num_written = 0;
853 unsigned long first_index;
854 unsigned long last_index;
858 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
859 (file->f_flags & O_DIRECT));
866 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
868 mutex_lock(&inode->i_mutex);
870 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
875 current->backing_dev_info = inode->i_mapping->backing_dev_info;
876 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
883 err = file_remove_suid(file);
887 file_update_time(file);
888 BTRFS_I(inode)->sequence++;
890 if (unlikely(file->f_flags & O_DIRECT)) {
891 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
895 * the generic O_DIRECT will update in-memory i_size after the
896 * DIOs are done. But our endio handlers that update the on
897 * disk i_size never update past the in memory i_size. So we
898 * need one more update here to catch any additions to the
901 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
902 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
903 mark_inode_dirty(inode);
906 if (num_written < 0) {
910 } else if (num_written == count) {
911 /* pick up pos changes done by the generic code */
916 * We are going to do buffered for the rest of the range, so we
917 * need to make sure to invalidate the buffered pages when we're
924 iov_iter_init(&i, iov, nr_segs, count, num_written);
925 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
926 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
927 (sizeof(struct page *)));
928 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
930 /* generic_write_checks can change our pos */
933 first_index = pos >> PAGE_CACHE_SHIFT;
934 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
937 * there are lots of better ways to do this, but this code
938 * makes sure the first and last page in the file range are
939 * up to date and ready for cow
941 if ((pos & (PAGE_CACHE_SIZE - 1))) {
942 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
943 if (!PageUptodate(pinned[0])) {
944 ret = btrfs_readpage(NULL, pinned[0]);
946 wait_on_page_locked(pinned[0]);
948 unlock_page(pinned[0]);
951 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
952 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
953 if (!PageUptodate(pinned[1])) {
954 ret = btrfs_readpage(NULL, pinned[1]);
956 wait_on_page_locked(pinned[1]);
958 unlock_page(pinned[1]);
962 while (iov_iter_count(&i) > 0) {
963 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
964 size_t write_bytes = min(iov_iter_count(&i),
965 nrptrs * (size_t)PAGE_CACHE_SIZE -
967 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
970 WARN_ON(num_pages > nrptrs);
971 memset(pages, 0, sizeof(struct page *) * nrptrs);
973 ret = btrfs_delalloc_reserve_space(inode, write_bytes);
977 ret = prepare_pages(root, file, pages, num_pages,
978 pos, first_index, last_index,
981 btrfs_delalloc_release_space(inode, write_bytes);
985 ret = btrfs_copy_from_user(pos, num_pages,
986 write_bytes, pages, &i);
988 dirty_and_release_pages(NULL, root, file, pages,
989 num_pages, pos, write_bytes);
992 btrfs_drop_pages(pages, num_pages);
994 btrfs_delalloc_release_space(inode, write_bytes);
999 filemap_fdatawrite_range(inode->i_mapping, pos,
1000 pos + write_bytes - 1);
1002 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1005 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1006 btrfs_btree_balance_dirty(root, 1);
1007 btrfs_throttle(root);
1011 num_written += write_bytes;
1016 mutex_unlock(&inode->i_mutex);
1022 page_cache_release(pinned[0]);
1024 page_cache_release(pinned[1]);
1028 * we want to make sure fsync finds this change
1029 * but we haven't joined a transaction running right now.
1031 * Later on, someone is sure to update the inode and get the
1032 * real transid recorded.
1034 * We set last_trans now to the fs_info generation + 1,
1035 * this will either be one more than the running transaction
1036 * or the generation used for the next transaction if there isn't
1037 * one running right now.
1039 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1041 if (num_written > 0 && will_write) {
1042 struct btrfs_trans_handle *trans;
1044 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1048 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
1049 trans = btrfs_start_transaction(root, 0);
1050 ret = btrfs_log_dentry_safe(trans, root,
1053 ret = btrfs_sync_log(trans, root);
1055 btrfs_end_transaction(trans, root);
1057 btrfs_commit_transaction(trans, root);
1058 } else if (ret != BTRFS_NO_LOG_SYNC) {
1059 btrfs_commit_transaction(trans, root);
1061 btrfs_end_transaction(trans, root);
1064 if (file->f_flags & O_DIRECT && buffered) {
1065 invalidate_mapping_pages(inode->i_mapping,
1066 start_pos >> PAGE_CACHE_SHIFT,
1067 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1070 current->backing_dev_info = NULL;
1071 return num_written ? num_written : err;
1074 int btrfs_release_file(struct inode *inode, struct file *filp)
1077 * ordered_data_close is set by settattr when we are about to truncate
1078 * a file from a non-zero size to a zero size. This tries to
1079 * flush down new bytes that may have been written if the
1080 * application were using truncate to replace a file in place.
1082 if (BTRFS_I(inode)->ordered_data_close) {
1083 BTRFS_I(inode)->ordered_data_close = 0;
1084 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1085 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1086 filemap_flush(inode->i_mapping);
1088 if (filp->private_data)
1089 btrfs_ioctl_trans_end(filp);
1094 * fsync call for both files and directories. This logs the inode into
1095 * the tree log instead of forcing full commits whenever possible.
1097 * It needs to call filemap_fdatawait so that all ordered extent updates are
1098 * in the metadata btree are up to date for copying to the log.
1100 * It drops the inode mutex before doing the tree log commit. This is an
1101 * important optimization for directories because holding the mutex prevents
1102 * new operations on the dir while we write to disk.
1104 int btrfs_sync_file(struct file *file, int datasync)
1106 struct dentry *dentry = file->f_path.dentry;
1107 struct inode *inode = dentry->d_inode;
1108 struct btrfs_root *root = BTRFS_I(inode)->root;
1110 struct btrfs_trans_handle *trans;
1113 /* we wait first, since the writeback may change the inode */
1115 /* the VFS called filemap_fdatawrite for us */
1116 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1120 * check the transaction that last modified this inode
1121 * and see if its already been committed
1123 if (!BTRFS_I(inode)->last_trans)
1127 * if the last transaction that changed this file was before
1128 * the current transaction, we can bail out now without any
1131 mutex_lock(&root->fs_info->trans_mutex);
1132 if (BTRFS_I(inode)->last_trans <=
1133 root->fs_info->last_trans_committed) {
1134 BTRFS_I(inode)->last_trans = 0;
1135 mutex_unlock(&root->fs_info->trans_mutex);
1138 mutex_unlock(&root->fs_info->trans_mutex);
1141 * ok we haven't committed the transaction yet, lets do a commit
1143 if (file->private_data)
1144 btrfs_ioctl_trans_end(file);
1146 trans = btrfs_start_transaction(root, 0);
1147 if (IS_ERR(trans)) {
1148 ret = PTR_ERR(trans);
1152 ret = btrfs_log_dentry_safe(trans, root, dentry);
1156 /* we've logged all the items and now have a consistent
1157 * version of the file in the log. It is possible that
1158 * someone will come in and modify the file, but that's
1159 * fine because the log is consistent on disk, and we
1160 * have references to all of the file's extents
1162 * It is possible that someone will come in and log the
1163 * file again, but that will end up using the synchronization
1164 * inside btrfs_sync_log to keep things safe.
1166 mutex_unlock(&dentry->d_inode->i_mutex);
1168 if (ret != BTRFS_NO_LOG_SYNC) {
1170 ret = btrfs_commit_transaction(trans, root);
1172 ret = btrfs_sync_log(trans, root);
1174 ret = btrfs_end_transaction(trans, root);
1176 ret = btrfs_commit_transaction(trans, root);
1179 ret = btrfs_end_transaction(trans, root);
1181 mutex_lock(&dentry->d_inode->i_mutex);
1183 return ret > 0 ? -EIO : ret;
1186 static const struct vm_operations_struct btrfs_file_vm_ops = {
1187 .fault = filemap_fault,
1188 .page_mkwrite = btrfs_page_mkwrite,
1191 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1193 struct address_space *mapping = filp->f_mapping;
1195 if (!mapping->a_ops->readpage)
1198 file_accessed(filp);
1199 vma->vm_ops = &btrfs_file_vm_ops;
1200 vma->vm_flags |= VM_CAN_NONLINEAR;
1205 const struct file_operations btrfs_file_operations = {
1206 .llseek = generic_file_llseek,
1207 .read = do_sync_read,
1208 .write = do_sync_write,
1209 .aio_read = generic_file_aio_read,
1210 .splice_read = generic_file_splice_read,
1211 .aio_write = btrfs_file_aio_write,
1212 .mmap = btrfs_file_mmap,
1213 .open = generic_file_open,
1214 .release = btrfs_release_file,
1215 .fsync = btrfs_sync_file,
1216 .unlocked_ioctl = btrfs_ioctl,
1217 #ifdef CONFIG_COMPAT
1218 .compat_ioctl = btrfs_ioctl,