2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/time.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/backing-dev.h>
26 #include <linux/mpage.h>
27 #include <linux/swap.h>
28 #include <linux/writeback.h>
29 #include <linux/statfs.h>
30 #include <linux/compat.h>
31 #include <linux/slab.h>
34 #include "transaction.h"
35 #include "btrfs_inode.h"
37 #include "print-tree.h"
43 /* simple helper to fault in pages and copy. This should go away
44 * and be replaced with calls into generic code.
46 static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
48 struct page **prepared_pages,
49 const char __user *buf)
53 int offset = pos & (PAGE_CACHE_SIZE - 1);
55 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
56 size_t count = min_t(size_t,
57 PAGE_CACHE_SIZE - offset, write_bytes);
58 struct page *page = prepared_pages[i];
59 fault_in_pages_readable(buf, count);
61 /* Copy data from userspace to the current page */
63 page_fault = __copy_from_user(page_address(page) + offset,
65 /* Flush processor's dcache for this page */
66 flush_dcache_page(page);
74 return page_fault ? -EFAULT : 0;
78 * unlocks pages after btrfs_file_write is done with them
80 static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
83 for (i = 0; i < num_pages; i++) {
86 /* page checked is some magic around finding pages that
87 * have been modified without going through btrfs_set_page_dirty
90 ClearPageChecked(pages[i]);
91 unlock_page(pages[i]);
92 mark_page_accessed(pages[i]);
93 page_cache_release(pages[i]);
98 * after copy_from_user, pages need to be dirtied and we need to make
99 * sure holes are created between the current EOF and the start of
100 * any next extents (if required).
102 * this also makes the decision about creating an inline extent vs
103 * doing real data extents, marking pages dirty and delalloc as required.
105 static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
106 struct btrfs_root *root,
115 struct inode *inode = fdentry(file)->d_inode;
118 u64 end_of_last_block;
119 u64 end_pos = pos + write_bytes;
120 loff_t isize = i_size_read(inode);
122 start_pos = pos & ~((u64)root->sectorsize - 1);
123 num_bytes = (write_bytes + pos - start_pos +
124 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
126 end_of_last_block = start_pos + num_bytes - 1;
127 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
132 for (i = 0; i < num_pages; i++) {
133 struct page *p = pages[i];
138 if (end_pos > isize) {
139 i_size_write(inode, end_pos);
140 /* we've only changed i_size in ram, and we haven't updated
141 * the disk i_size. There is no need to log the inode
149 * this drops all the extents in the cache that intersect the range
150 * [start, end]. Existing extents are split as required.
152 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
155 struct extent_map *em;
156 struct extent_map *split = NULL;
157 struct extent_map *split2 = NULL;
158 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
159 u64 len = end - start + 1;
165 WARN_ON(end < start);
166 if (end == (u64)-1) {
172 split = alloc_extent_map(GFP_NOFS);
174 split2 = alloc_extent_map(GFP_NOFS);
176 write_lock(&em_tree->lock);
177 em = lookup_extent_mapping(em_tree, start, len);
179 write_unlock(&em_tree->lock);
183 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
184 if (testend && em->start + em->len >= start + len) {
186 write_unlock(&em_tree->lock);
189 start = em->start + em->len;
191 len = start + len - (em->start + em->len);
193 write_unlock(&em_tree->lock);
196 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
197 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
198 remove_extent_mapping(em_tree, em);
200 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
202 split->start = em->start;
203 split->len = start - em->start;
204 split->orig_start = em->orig_start;
205 split->block_start = em->block_start;
208 split->block_len = em->block_len;
210 split->block_len = split->len;
212 split->bdev = em->bdev;
213 split->flags = flags;
214 ret = add_extent_mapping(em_tree, split);
216 free_extent_map(split);
220 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
221 testend && em->start + em->len > start + len) {
222 u64 diff = start + len - em->start;
224 split->start = start + len;
225 split->len = em->start + em->len - (start + len);
226 split->bdev = em->bdev;
227 split->flags = flags;
230 split->block_len = em->block_len;
231 split->block_start = em->block_start;
232 split->orig_start = em->orig_start;
234 split->block_len = split->len;
235 split->block_start = em->block_start + diff;
236 split->orig_start = split->start;
239 ret = add_extent_mapping(em_tree, split);
241 free_extent_map(split);
244 write_unlock(&em_tree->lock);
248 /* once for the tree*/
252 free_extent_map(split);
254 free_extent_map(split2);
259 * this is very complex, but the basic idea is to drop all extents
260 * in the range start - end. hint_block is filled in with a block number
261 * that would be a good hint to the block allocator for this file.
263 * If an extent intersects the range but is not entirely inside the range
264 * it is either truncated or split. Anything entirely inside the range
265 * is deleted from the tree.
267 int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
268 u64 start, u64 end, u64 *hint_byte, int drop_cache)
270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 struct extent_buffer *leaf;
272 struct btrfs_file_extent_item *fi;
273 struct btrfs_path *path;
274 struct btrfs_key key;
275 struct btrfs_key new_key;
276 u64 search_start = start;
279 u64 extent_offset = 0;
288 btrfs_drop_extent_cache(inode, start, end - 1, 0);
290 path = btrfs_alloc_path();
296 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
300 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
301 leaf = path->nodes[0];
302 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
303 if (key.objectid == inode->i_ino &&
304 key.type == BTRFS_EXTENT_DATA_KEY)
309 leaf = path->nodes[0];
310 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
312 ret = btrfs_next_leaf(root, path);
319 leaf = path->nodes[0];
323 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
324 if (key.objectid > inode->i_ino ||
325 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
328 fi = btrfs_item_ptr(leaf, path->slots[0],
329 struct btrfs_file_extent_item);
330 extent_type = btrfs_file_extent_type(leaf, fi);
332 if (extent_type == BTRFS_FILE_EXTENT_REG ||
333 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
334 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
335 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
336 extent_offset = btrfs_file_extent_offset(leaf, fi);
337 extent_end = key.offset +
338 btrfs_file_extent_num_bytes(leaf, fi);
339 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
340 extent_end = key.offset +
341 btrfs_file_extent_inline_len(leaf, fi);
344 extent_end = search_start;
347 if (extent_end <= search_start) {
352 search_start = max(key.offset, start);
354 btrfs_release_path(root, path);
359 * | - range to drop - |
360 * | -------- extent -------- |
362 if (start > key.offset && end < extent_end) {
364 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
366 memcpy(&new_key, &key, sizeof(new_key));
367 new_key.offset = start;
368 ret = btrfs_duplicate_item(trans, root, path,
370 if (ret == -EAGAIN) {
371 btrfs_release_path(root, path);
377 leaf = path->nodes[0];
378 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
379 struct btrfs_file_extent_item);
380 btrfs_set_file_extent_num_bytes(leaf, fi,
383 fi = btrfs_item_ptr(leaf, path->slots[0],
384 struct btrfs_file_extent_item);
386 extent_offset += start - key.offset;
387 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
388 btrfs_set_file_extent_num_bytes(leaf, fi,
390 btrfs_mark_buffer_dirty(leaf);
392 if (disk_bytenr > 0) {
393 ret = btrfs_inc_extent_ref(trans, root,
394 disk_bytenr, num_bytes, 0,
395 root->root_key.objectid,
397 start - extent_offset);
399 *hint_byte = disk_bytenr;
404 * | ---- range to drop ----- |
405 * | -------- extent -------- |
407 if (start <= key.offset && end < extent_end) {
408 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
410 memcpy(&new_key, &key, sizeof(new_key));
411 new_key.offset = end;
412 btrfs_set_item_key_safe(trans, root, path, &new_key);
414 extent_offset += end - key.offset;
415 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
416 btrfs_set_file_extent_num_bytes(leaf, fi,
418 btrfs_mark_buffer_dirty(leaf);
419 if (disk_bytenr > 0) {
420 inode_sub_bytes(inode, end - key.offset);
421 *hint_byte = disk_bytenr;
426 search_start = extent_end;
428 * | ---- range to drop ----- |
429 * | -------- extent -------- |
431 if (start > key.offset && end >= extent_end) {
433 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
435 btrfs_set_file_extent_num_bytes(leaf, fi,
437 btrfs_mark_buffer_dirty(leaf);
438 if (disk_bytenr > 0) {
439 inode_sub_bytes(inode, extent_end - start);
440 *hint_byte = disk_bytenr;
442 if (end == extent_end)
450 * | ---- range to drop ----- |
451 * | ------ extent ------ |
453 if (start <= key.offset && end >= extent_end) {
455 del_slot = path->slots[0];
458 BUG_ON(del_slot + del_nr != path->slots[0]);
462 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
463 inode_sub_bytes(inode,
464 extent_end - key.offset);
465 extent_end = ALIGN(extent_end,
467 } else if (disk_bytenr > 0) {
468 ret = btrfs_free_extent(trans, root,
469 disk_bytenr, num_bytes, 0,
470 root->root_key.objectid,
471 key.objectid, key.offset -
474 inode_sub_bytes(inode,
475 extent_end - key.offset);
476 *hint_byte = disk_bytenr;
479 if (end == extent_end)
482 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
487 ret = btrfs_del_items(trans, root, path, del_slot,
494 btrfs_release_path(root, path);
502 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
506 btrfs_free_path(path);
510 static int extent_mergeable(struct extent_buffer *leaf, int slot,
511 u64 objectid, u64 bytenr, u64 orig_offset,
512 u64 *start, u64 *end)
514 struct btrfs_file_extent_item *fi;
515 struct btrfs_key key;
518 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
521 btrfs_item_key_to_cpu(leaf, &key, slot);
522 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
525 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
526 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
527 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
528 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
529 btrfs_file_extent_compression(leaf, fi) ||
530 btrfs_file_extent_encryption(leaf, fi) ||
531 btrfs_file_extent_other_encoding(leaf, fi))
534 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
535 if ((*start && *start != key.offset) || (*end && *end != extent_end))
544 * Mark extent in the range start - end as written.
546 * This changes extent type from 'pre-allocated' to 'regular'. If only
547 * part of extent is marked as written, the extent will be split into
550 int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
551 struct inode *inode, u64 start, u64 end)
553 struct btrfs_root *root = BTRFS_I(inode)->root;
554 struct extent_buffer *leaf;
555 struct btrfs_path *path;
556 struct btrfs_file_extent_item *fi;
557 struct btrfs_key key;
558 struct btrfs_key new_key;
571 btrfs_drop_extent_cache(inode, start, end - 1, 0);
573 path = btrfs_alloc_path();
578 key.objectid = inode->i_ino;
579 key.type = BTRFS_EXTENT_DATA_KEY;
582 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
583 if (ret > 0 && path->slots[0] > 0)
586 leaf = path->nodes[0];
587 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
588 BUG_ON(key.objectid != inode->i_ino ||
589 key.type != BTRFS_EXTENT_DATA_KEY);
590 fi = btrfs_item_ptr(leaf, path->slots[0],
591 struct btrfs_file_extent_item);
592 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
593 BTRFS_FILE_EXTENT_PREALLOC);
594 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
595 BUG_ON(key.offset > start || extent_end < end);
597 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
598 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
599 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
600 memcpy(&new_key, &key, sizeof(new_key));
602 if (start == key.offset && end < extent_end) {
605 if (extent_mergeable(leaf, path->slots[0] - 1,
606 inode->i_ino, bytenr, orig_offset,
607 &other_start, &other_end)) {
608 new_key.offset = end;
609 btrfs_set_item_key_safe(trans, root, path, &new_key);
610 fi = btrfs_item_ptr(leaf, path->slots[0],
611 struct btrfs_file_extent_item);
612 btrfs_set_file_extent_num_bytes(leaf, fi,
614 btrfs_set_file_extent_offset(leaf, fi,
616 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
617 struct btrfs_file_extent_item);
618 btrfs_set_file_extent_num_bytes(leaf, fi,
620 btrfs_mark_buffer_dirty(leaf);
625 if (start > key.offset && end == extent_end) {
628 if (extent_mergeable(leaf, path->slots[0] + 1,
629 inode->i_ino, bytenr, orig_offset,
630 &other_start, &other_end)) {
631 fi = btrfs_item_ptr(leaf, path->slots[0],
632 struct btrfs_file_extent_item);
633 btrfs_set_file_extent_num_bytes(leaf, fi,
636 new_key.offset = start;
637 btrfs_set_item_key_safe(trans, root, path, &new_key);
639 fi = btrfs_item_ptr(leaf, path->slots[0],
640 struct btrfs_file_extent_item);
641 btrfs_set_file_extent_num_bytes(leaf, fi,
643 btrfs_set_file_extent_offset(leaf, fi,
644 start - orig_offset);
645 btrfs_mark_buffer_dirty(leaf);
650 while (start > key.offset || end < extent_end) {
651 if (key.offset == start)
654 new_key.offset = split;
655 ret = btrfs_duplicate_item(trans, root, path, &new_key);
656 if (ret == -EAGAIN) {
657 btrfs_release_path(root, path);
662 leaf = path->nodes[0];
663 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
664 struct btrfs_file_extent_item);
665 btrfs_set_file_extent_num_bytes(leaf, fi,
668 fi = btrfs_item_ptr(leaf, path->slots[0],
669 struct btrfs_file_extent_item);
671 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
672 btrfs_set_file_extent_num_bytes(leaf, fi,
674 btrfs_mark_buffer_dirty(leaf);
676 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
677 root->root_key.objectid,
678 inode->i_ino, orig_offset);
681 if (split == start) {
684 BUG_ON(start != key.offset);
693 if (extent_mergeable(leaf, path->slots[0] + 1,
694 inode->i_ino, bytenr, orig_offset,
695 &other_start, &other_end)) {
697 btrfs_release_path(root, path);
700 extent_end = other_end;
701 del_slot = path->slots[0] + 1;
703 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
704 0, root->root_key.objectid,
705 inode->i_ino, orig_offset);
710 if (extent_mergeable(leaf, path->slots[0] - 1,
711 inode->i_ino, bytenr, orig_offset,
712 &other_start, &other_end)) {
714 btrfs_release_path(root, path);
717 key.offset = other_start;
718 del_slot = path->slots[0];
720 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
721 0, root->root_key.objectid,
722 inode->i_ino, orig_offset);
726 fi = btrfs_item_ptr(leaf, path->slots[0],
727 struct btrfs_file_extent_item);
728 btrfs_set_file_extent_type(leaf, fi,
729 BTRFS_FILE_EXTENT_REG);
730 btrfs_mark_buffer_dirty(leaf);
732 fi = btrfs_item_ptr(leaf, del_slot - 1,
733 struct btrfs_file_extent_item);
734 btrfs_set_file_extent_type(leaf, fi,
735 BTRFS_FILE_EXTENT_REG);
736 btrfs_set_file_extent_num_bytes(leaf, fi,
737 extent_end - key.offset);
738 btrfs_mark_buffer_dirty(leaf);
740 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
744 btrfs_free_path(path);
749 * this gets pages into the page cache and locks them down, it also properly
750 * waits for data=ordered extents to finish before allowing the pages to be
753 static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
754 struct page **pages, size_t num_pages,
755 loff_t pos, unsigned long first_index,
756 unsigned long last_index, size_t write_bytes)
758 struct extent_state *cached_state = NULL;
760 unsigned long index = pos >> PAGE_CACHE_SHIFT;
761 struct inode *inode = fdentry(file)->d_inode;
766 start_pos = pos & ~((u64)root->sectorsize - 1);
767 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
769 if (start_pos > inode->i_size) {
770 err = btrfs_cont_expand(inode, start_pos);
775 memset(pages, 0, num_pages * sizeof(struct page *));
777 for (i = 0; i < num_pages; i++) {
778 pages[i] = grab_cache_page(inode->i_mapping, index + i);
783 wait_on_page_writeback(pages[i]);
785 if (start_pos < inode->i_size) {
786 struct btrfs_ordered_extent *ordered;
787 lock_extent_bits(&BTRFS_I(inode)->io_tree,
788 start_pos, last_pos - 1, 0, &cached_state,
790 ordered = btrfs_lookup_first_ordered_extent(inode,
793 ordered->file_offset + ordered->len > start_pos &&
794 ordered->file_offset < last_pos) {
795 btrfs_put_ordered_extent(ordered);
796 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
797 start_pos, last_pos - 1,
798 &cached_state, GFP_NOFS);
799 for (i = 0; i < num_pages; i++) {
800 unlock_page(pages[i]);
801 page_cache_release(pages[i]);
803 btrfs_wait_ordered_range(inode, start_pos,
804 last_pos - start_pos);
808 btrfs_put_ordered_extent(ordered);
810 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
811 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
812 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
814 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
815 start_pos, last_pos - 1, &cached_state,
818 for (i = 0; i < num_pages; i++) {
819 clear_page_dirty_for_io(pages[i]);
820 set_page_extent_mapped(pages[i]);
821 WARN_ON(!PageLocked(pages[i]));
826 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
827 size_t count, loff_t *ppos)
831 ssize_t num_written = 0;
834 struct inode *inode = fdentry(file)->d_inode;
835 struct btrfs_root *root = BTRFS_I(inode)->root;
836 struct page **pages = NULL;
838 struct page *pinned[2];
839 unsigned long first_index;
840 unsigned long last_index;
843 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
844 (file->f_flags & O_DIRECT));
846 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
847 PAGE_CACHE_SIZE / (sizeof(struct page *)));
854 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
856 /* do the reserve before the mutex lock in case we have to do some
857 * flushing. We wouldn't deadlock, but this is more polite.
859 err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
863 mutex_lock(&inode->i_mutex);
865 current->backing_dev_info = inode->i_mapping->backing_dev_info;
866 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
873 err = file_remove_suid(file);
877 file_update_time(file);
879 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
881 /* generic_write_checks can change our pos */
884 BTRFS_I(inode)->sequence++;
885 first_index = pos >> PAGE_CACHE_SHIFT;
886 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
889 * there are lots of better ways to do this, but this code
890 * makes sure the first and last page in the file range are
891 * up to date and ready for cow
893 if ((pos & (PAGE_CACHE_SIZE - 1))) {
894 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
895 if (!PageUptodate(pinned[0])) {
896 ret = btrfs_readpage(NULL, pinned[0]);
898 wait_on_page_locked(pinned[0]);
900 unlock_page(pinned[0]);
903 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
904 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
905 if (!PageUptodate(pinned[1])) {
906 ret = btrfs_readpage(NULL, pinned[1]);
908 wait_on_page_locked(pinned[1]);
910 unlock_page(pinned[1]);
915 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
916 size_t write_bytes = min(count, nrptrs *
917 (size_t)PAGE_CACHE_SIZE -
919 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
922 WARN_ON(num_pages > nrptrs);
923 memset(pages, 0, sizeof(struct page *) * nrptrs);
925 ret = btrfs_check_data_free_space(root, inode, write_bytes);
929 ret = prepare_pages(root, file, pages, num_pages,
930 pos, first_index, last_index,
933 btrfs_free_reserved_data_space(root, inode,
938 ret = btrfs_copy_from_user(pos, num_pages,
939 write_bytes, pages, buf);
941 btrfs_free_reserved_data_space(root, inode,
943 btrfs_drop_pages(pages, num_pages);
947 ret = dirty_and_release_pages(NULL, root, file, pages,
948 num_pages, pos, write_bytes);
949 btrfs_drop_pages(pages, num_pages);
951 btrfs_free_reserved_data_space(root, inode,
957 filemap_fdatawrite_range(inode->i_mapping, pos,
958 pos + write_bytes - 1);
960 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
963 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
964 btrfs_btree_balance_dirty(root, 1);
965 btrfs_throttle(root);
969 count -= write_bytes;
971 num_written += write_bytes;
976 mutex_unlock(&inode->i_mutex);
979 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
984 page_cache_release(pinned[0]);
986 page_cache_release(pinned[1]);
990 * we want to make sure fsync finds this change
991 * but we haven't joined a transaction running right now.
993 * Later on, someone is sure to update the inode and get the
994 * real transid recorded.
996 * We set last_trans now to the fs_info generation + 1,
997 * this will either be one more than the running transaction
998 * or the generation used for the next transaction if there isn't
999 * one running right now.
1001 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1003 if (num_written > 0 && will_write) {
1004 struct btrfs_trans_handle *trans;
1006 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1010 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
1011 trans = btrfs_start_transaction(root, 1);
1012 ret = btrfs_log_dentry_safe(trans, root,
1015 ret = btrfs_sync_log(trans, root);
1017 btrfs_end_transaction(trans, root);
1019 btrfs_commit_transaction(trans, root);
1020 } else if (ret != BTRFS_NO_LOG_SYNC) {
1021 btrfs_commit_transaction(trans, root);
1023 btrfs_end_transaction(trans, root);
1026 if (file->f_flags & O_DIRECT) {
1027 invalidate_mapping_pages(inode->i_mapping,
1028 start_pos >> PAGE_CACHE_SHIFT,
1029 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1032 current->backing_dev_info = NULL;
1033 return num_written ? num_written : err;
1036 int btrfs_release_file(struct inode *inode, struct file *filp)
1039 * ordered_data_close is set by settattr when we are about to truncate
1040 * a file from a non-zero size to a zero size. This tries to
1041 * flush down new bytes that may have been written if the
1042 * application were using truncate to replace a file in place.
1044 if (BTRFS_I(inode)->ordered_data_close) {
1045 BTRFS_I(inode)->ordered_data_close = 0;
1046 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1047 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1048 filemap_flush(inode->i_mapping);
1050 if (filp->private_data)
1051 btrfs_ioctl_trans_end(filp);
1056 * fsync call for both files and directories. This logs the inode into
1057 * the tree log instead of forcing full commits whenever possible.
1059 * It needs to call filemap_fdatawait so that all ordered extent updates are
1060 * in the metadata btree are up to date for copying to the log.
1062 * It drops the inode mutex before doing the tree log commit. This is an
1063 * important optimization for directories because holding the mutex prevents
1064 * new operations on the dir while we write to disk.
1066 int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1068 struct inode *inode = dentry->d_inode;
1069 struct btrfs_root *root = BTRFS_I(inode)->root;
1071 struct btrfs_trans_handle *trans;
1074 /* we wait first, since the writeback may change the inode */
1076 /* the VFS called filemap_fdatawrite for us */
1077 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1081 * check the transaction that last modified this inode
1082 * and see if its already been committed
1084 if (!BTRFS_I(inode)->last_trans)
1088 * if the last transaction that changed this file was before
1089 * the current transaction, we can bail out now without any
1092 mutex_lock(&root->fs_info->trans_mutex);
1093 if (BTRFS_I(inode)->last_trans <=
1094 root->fs_info->last_trans_committed) {
1095 BTRFS_I(inode)->last_trans = 0;
1096 mutex_unlock(&root->fs_info->trans_mutex);
1099 mutex_unlock(&root->fs_info->trans_mutex);
1102 * ok we haven't committed the transaction yet, lets do a commit
1104 if (file && file->private_data)
1105 btrfs_ioctl_trans_end(file);
1107 trans = btrfs_start_transaction(root, 1);
1113 ret = btrfs_log_dentry_safe(trans, root, dentry);
1117 /* we've logged all the items and now have a consistent
1118 * version of the file in the log. It is possible that
1119 * someone will come in and modify the file, but that's
1120 * fine because the log is consistent on disk, and we
1121 * have references to all of the file's extents
1123 * It is possible that someone will come in and log the
1124 * file again, but that will end up using the synchronization
1125 * inside btrfs_sync_log to keep things safe.
1127 mutex_unlock(&dentry->d_inode->i_mutex);
1129 if (ret != BTRFS_NO_LOG_SYNC) {
1131 ret = btrfs_commit_transaction(trans, root);
1133 ret = btrfs_sync_log(trans, root);
1135 ret = btrfs_end_transaction(trans, root);
1137 ret = btrfs_commit_transaction(trans, root);
1140 ret = btrfs_end_transaction(trans, root);
1142 mutex_lock(&dentry->d_inode->i_mutex);
1144 return ret > 0 ? -EIO : ret;
1147 static const struct vm_operations_struct btrfs_file_vm_ops = {
1148 .fault = filemap_fault,
1149 .page_mkwrite = btrfs_page_mkwrite,
1152 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1154 vma->vm_ops = &btrfs_file_vm_ops;
1155 file_accessed(filp);
1159 const struct file_operations btrfs_file_operations = {
1160 .llseek = generic_file_llseek,
1161 .read = do_sync_read,
1162 .aio_read = generic_file_aio_read,
1163 .splice_read = generic_file_splice_read,
1164 .write = btrfs_file_write,
1165 .mmap = btrfs_file_mmap,
1166 .open = generic_file_open,
1167 .release = btrfs_release_file,
1168 .fsync = btrfs_sync_file,
1169 .unlocked_ioctl = btrfs_ioctl,
1170 #ifdef CONFIG_COMPAT
1171 .compat_ioctl = btrfs_ioctl,