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/smp_lock.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mpage.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/statfs.h>
31 #include <linux/compat.h>
32 #include <linux/version.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 int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
49 struct page **prepared_pages,
50 const char __user * buf)
54 int offset = pos & (PAGE_CACHE_SIZE - 1);
56 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
57 size_t count = min_t(size_t,
58 PAGE_CACHE_SIZE - offset, write_bytes);
59 struct page *page = prepared_pages[i];
60 fault_in_pages_readable(buf, count);
62 /* Copy data from userspace to the current page */
64 page_fault = __copy_from_user(page_address(page) + offset,
66 /* Flush processor's dcache for this page */
67 flush_dcache_page(page);
75 return page_fault ? -EFAULT : 0;
79 * unlocks pages after btrfs_file_write is done with them
81 static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
84 for (i = 0; i < num_pages; i++) {
87 /* page checked is some magic around finding pages that
88 * have been modified without going through btrfs_set_page_dirty
91 ClearPageChecked(pages[i]);
92 unlock_page(pages[i]);
93 mark_page_accessed(pages[i]);
94 page_cache_release(pages[i]);
99 * after copy_from_user, pages need to be dirtied and we need to make
100 * sure holes are created between the current EOF and the start of
101 * any next extents (if required).
103 * this also makes the decision about creating an inline extent vs
104 * doing real data extents, marking pages dirty and delalloc as required.
106 static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
116 struct inode *inode = fdentry(file)->d_inode;
117 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
121 u64 end_of_last_block;
122 u64 end_pos = pos + write_bytes;
123 loff_t isize = i_size_read(inode);
125 start_pos = pos & ~((u64)root->sectorsize - 1);
126 num_bytes = (write_bytes + pos - start_pos +
127 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
129 end_of_last_block = start_pos + num_bytes - 1;
131 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
132 trans = btrfs_join_transaction(root, 1);
137 btrfs_set_trans_block_group(trans, inode);
140 if ((end_of_last_block & 4095) == 0) {
141 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
143 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
145 /* FIXME...EIEIO, ENOSPC and more */
146 /* insert any holes we need to create */
147 if (isize < start_pos) {
148 u64 last_pos_in_file;
150 u64 mask = root->sectorsize - 1;
151 last_pos_in_file = (isize + mask) & ~mask;
152 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
154 btrfs_wait_ordered_range(inode, last_pos_in_file,
155 last_pos_in_file + hole_size);
156 mutex_lock(&BTRFS_I(inode)->extent_mutex);
157 err = btrfs_drop_extents(trans, root, inode,
159 last_pos_in_file + hole_size,
165 err = btrfs_insert_file_extent(trans, root,
170 btrfs_drop_extent_cache(inode, last_pos_in_file,
171 last_pos_in_file + hole_size - 1, 0);
172 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
173 btrfs_check_file(root, inode);
179 /* check for reserved extents on each page, we don't want
180 * to reset the delalloc bit on things that already have
183 btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
184 for (i = 0; i < num_pages; i++) {
185 struct page *p = pages[i];
190 if (end_pos > isize) {
191 i_size_write(inode, end_pos);
192 btrfs_update_inode(trans, root, inode);
195 err = btrfs_end_transaction(trans, root);
197 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
202 * this drops all the extents in the cache that intersect the range
203 * [start, end]. Existing extents are split as required.
205 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
208 struct extent_map *em;
209 struct extent_map *split = NULL;
210 struct extent_map *split2 = NULL;
211 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
212 u64 len = end - start + 1;
218 WARN_ON(end < start);
219 if (end == (u64)-1) {
225 split = alloc_extent_map(GFP_NOFS);
227 split2 = alloc_extent_map(GFP_NOFS);
229 spin_lock(&em_tree->lock);
230 em = lookup_extent_mapping(em_tree, start, len);
232 spin_unlock(&em_tree->lock);
236 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
237 spin_unlock(&em_tree->lock);
238 if (em->start <= start &&
239 (!testend || em->start + em->len >= start + len)) {
243 if (start < em->start) {
244 len = em->start - start;
246 len = start + len - (em->start + em->len);
247 start = em->start + em->len;
252 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
253 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
254 remove_extent_mapping(em_tree, em);
256 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
258 split->start = em->start;
259 split->len = start - em->start;
260 split->block_start = em->block_start;
263 split->block_len = em->block_len;
265 split->block_len = split->len;
267 split->bdev = em->bdev;
268 split->flags = flags;
269 ret = add_extent_mapping(em_tree, split);
271 free_extent_map(split);
275 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
276 testend && em->start + em->len > start + len) {
277 u64 diff = start + len - em->start;
279 split->start = start + len;
280 split->len = em->start + em->len - (start + len);
281 split->bdev = em->bdev;
282 split->flags = flags;
285 split->block_len = em->block_len;
286 split->block_start = em->block_start;
288 split->block_len = split->len;
289 split->block_start = em->block_start + diff;
292 ret = add_extent_mapping(em_tree, split);
294 free_extent_map(split);
297 spin_unlock(&em_tree->lock);
301 /* once for the tree*/
305 free_extent_map(split);
307 free_extent_map(split2);
311 int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
315 struct btrfs_path *path;
316 struct btrfs_key found_key;
317 struct extent_buffer *leaf;
318 struct btrfs_file_extent_item *extent;
327 path = btrfs_alloc_path();
328 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
331 nritems = btrfs_header_nritems(path->nodes[0]);
332 if (path->slots[0] >= nritems) {
333 ret = btrfs_next_leaf(root, path);
336 nritems = btrfs_header_nritems(path->nodes[0]);
338 slot = path->slots[0];
339 leaf = path->nodes[0];
340 btrfs_item_key_to_cpu(leaf, &found_key, slot);
341 if (found_key.objectid != inode->i_ino)
343 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
346 if (found_key.offset < last_offset) {
348 btrfs_print_leaf(root, leaf);
349 printk("inode %lu found offset %Lu expected %Lu\n",
350 inode->i_ino, found_key.offset, last_offset);
354 extent = btrfs_item_ptr(leaf, slot,
355 struct btrfs_file_extent_item);
356 found_type = btrfs_file_extent_type(leaf, extent);
357 if (found_type == BTRFS_FILE_EXTENT_REG) {
358 extent_end = found_key.offset +
359 btrfs_file_extent_num_bytes(leaf, extent);
360 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
361 struct btrfs_item *item;
362 item = btrfs_item_nr(leaf, slot);
363 extent_end = found_key.offset +
364 btrfs_file_extent_inline_len(leaf, extent);
365 extent_end = (extent_end + root->sectorsize - 1) &
366 ~((u64)root->sectorsize -1 );
368 last_offset = extent_end;
371 if (0 && last_offset < inode->i_size) {
373 btrfs_print_leaf(root, leaf);
374 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
375 last_offset, inode->i_size);
380 btrfs_free_path(path);
386 * this is very complex, but the basic idea is to drop all extents
387 * in the range start - end. hint_block is filled in with a block number
388 * that would be a good hint to the block allocator for this file.
390 * If an extent intersects the range but is not entirely inside the range
391 * it is either truncated or split. Anything entirely inside the range
392 * is deleted from the tree.
394 * inline_limit is used to tell this code which offsets in the file to keep
395 * if they contain inline extents.
397 int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root, struct inode *inode,
399 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
402 u64 search_start = start;
407 u16 other_encoding = 0;
410 struct extent_buffer *leaf;
411 struct btrfs_file_extent_item *extent;
412 struct btrfs_path *path;
413 struct btrfs_key key;
414 struct btrfs_file_extent_item old;
425 btrfs_drop_extent_cache(inode, start, end - 1, 0);
427 path = btrfs_alloc_path();
432 btrfs_release_path(root, path);
433 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
438 if (path->slots[0] == 0) {
453 leaf = path->nodes[0];
454 slot = path->slots[0];
456 btrfs_item_key_to_cpu(leaf, &key, slot);
457 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
461 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
462 key.objectid != inode->i_ino) {
466 search_start = key.offset;
469 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
470 extent = btrfs_item_ptr(leaf, slot,
471 struct btrfs_file_extent_item);
472 found_type = btrfs_file_extent_type(leaf, extent);
473 compression = btrfs_file_extent_compression(leaf,
475 encryption = btrfs_file_extent_encryption(leaf,
477 other_encoding = btrfs_file_extent_other_encoding(leaf,
479 if (found_type == BTRFS_FILE_EXTENT_REG) {
481 btrfs_file_extent_disk_bytenr(leaf,
484 *hint_byte = extent_end;
486 extent_end = key.offset +
487 btrfs_file_extent_num_bytes(leaf, extent);
488 ram_bytes = btrfs_file_extent_ram_bytes(leaf,
491 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
493 extent_end = key.offset +
494 btrfs_file_extent_inline_len(leaf, extent);
497 extent_end = search_start;
500 /* we found nothing we can drop */
501 if ((!found_extent && !found_inline) ||
502 search_start >= extent_end) {
505 nritems = btrfs_header_nritems(leaf);
506 if (slot >= nritems - 1) {
507 nextret = btrfs_next_leaf(root, path);
518 u64 mask = root->sectorsize - 1;
519 search_start = (extent_end + mask) & ~mask;
521 search_start = extent_end;
523 if (end <= extent_end && start >= key.offset && found_inline)
524 *hint_byte = EXTENT_MAP_INLINE;
527 read_extent_buffer(leaf, &old, (unsigned long)extent,
529 root_gen = btrfs_header_generation(leaf);
530 root_owner = btrfs_header_owner(leaf);
531 leaf_start = leaf->start;
534 if (end < extent_end && end >= key.offset) {
536 if (found_inline && start <= key.offset)
539 /* truncate existing extent */
540 if (start > key.offset) {
544 WARN_ON(start & (root->sectorsize - 1));
546 new_num = start - key.offset;
547 old_num = btrfs_file_extent_num_bytes(leaf,
550 btrfs_file_extent_disk_bytenr(leaf,
552 if (btrfs_file_extent_disk_bytenr(leaf,
554 inode_sub_bytes(inode, old_num -
557 btrfs_set_file_extent_num_bytes(leaf, extent,
559 btrfs_mark_buffer_dirty(leaf);
560 } else if (key.offset < inline_limit &&
561 (end > extent_end) &&
562 (inline_limit < extent_end)) {
564 new_size = btrfs_file_extent_calc_inline_size(
565 inline_limit - key.offset);
566 inode_sub_bytes(inode, extent_end -
568 btrfs_truncate_item(trans, root, path,
572 /* delete the entire extent */
575 inode_sub_bytes(inode, extent_end -
577 ret = btrfs_del_item(trans, root, path);
578 /* TODO update progress marker and return */
581 btrfs_release_path(root, path);
582 /* the extent will be freed later */
584 if (bookend && found_inline && start <= key.offset) {
586 new_size = btrfs_file_extent_calc_inline_size(
588 inode_sub_bytes(inode, end - key.offset);
589 ret = btrfs_truncate_item(trans, root, path,
593 /* create bookend, splitting the extent in two */
594 if (bookend && found_extent) {
596 struct btrfs_key ins;
597 ins.objectid = inode->i_ino;
599 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
600 btrfs_release_path(root, path);
601 ret = btrfs_insert_empty_item(trans, root, path, &ins,
605 leaf = path->nodes[0];
606 extent = btrfs_item_ptr(leaf, path->slots[0],
607 struct btrfs_file_extent_item);
608 write_extent_buffer(leaf, &old,
609 (unsigned long)extent, sizeof(old));
611 btrfs_set_file_extent_compression(leaf, extent,
613 btrfs_set_file_extent_encryption(leaf, extent,
615 btrfs_set_file_extent_other_encoding(leaf, extent,
617 btrfs_set_file_extent_offset(leaf, extent,
618 le64_to_cpu(old.offset) + end - key.offset);
619 WARN_ON(le64_to_cpu(old.num_bytes) <
621 btrfs_set_file_extent_num_bytes(leaf, extent,
625 * set the ram bytes to the size of the full extent
626 * before splitting. This is a worst case flag,
627 * but its the best we can do because we don't know
628 * how splitting affects compression
630 btrfs_set_file_extent_ram_bytes(leaf, extent,
632 btrfs_set_file_extent_type(leaf, extent,
633 BTRFS_FILE_EXTENT_REG);
635 btrfs_mark_buffer_dirty(path->nodes[0]);
637 disk_bytenr = le64_to_cpu(old.disk_bytenr);
638 if (disk_bytenr != 0) {
639 ret = btrfs_inc_extent_ref(trans, root,
641 le64_to_cpu(old.disk_num_bytes),
643 root->root_key.objectid,
644 trans->transid, ins.objectid);
647 btrfs_release_path(root, path);
648 if (disk_bytenr != 0) {
649 inode_add_bytes(inode, extent_end - end);
653 if (found_extent && !keep) {
654 u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
656 if (disk_bytenr != 0) {
657 inode_sub_bytes(inode,
658 le64_to_cpu(old.num_bytes));
659 ret = btrfs_free_extent(trans, root,
661 le64_to_cpu(old.disk_num_bytes),
662 leaf_start, root_owner,
663 root_gen, key.objectid, 0);
665 *hint_byte = disk_bytenr;
669 if (search_start >= end) {
675 btrfs_free_path(path);
676 btrfs_check_file(root, inode);
681 * this gets pages into the page cache and locks them down, it also properly
682 * waits for data=ordered extents to finish before allowing the pages to be
685 static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
686 struct page **pages, size_t num_pages,
687 loff_t pos, unsigned long first_index,
688 unsigned long last_index, size_t write_bytes)
691 unsigned long index = pos >> PAGE_CACHE_SHIFT;
692 struct inode *inode = fdentry(file)->d_inode;
697 start_pos = pos & ~((u64)root->sectorsize - 1);
698 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
700 memset(pages, 0, num_pages * sizeof(struct page *));
702 for (i = 0; i < num_pages; i++) {
703 pages[i] = grab_cache_page(inode->i_mapping, index + i);
708 wait_on_page_writeback(pages[i]);
710 if (start_pos < inode->i_size) {
711 struct btrfs_ordered_extent *ordered;
712 lock_extent(&BTRFS_I(inode)->io_tree,
713 start_pos, last_pos - 1, GFP_NOFS);
714 ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
716 ordered->file_offset + ordered->len > start_pos &&
717 ordered->file_offset < last_pos) {
718 btrfs_put_ordered_extent(ordered);
719 unlock_extent(&BTRFS_I(inode)->io_tree,
720 start_pos, last_pos - 1, GFP_NOFS);
721 for (i = 0; i < num_pages; i++) {
722 unlock_page(pages[i]);
723 page_cache_release(pages[i]);
725 btrfs_wait_ordered_range(inode, start_pos,
726 last_pos - start_pos);
730 btrfs_put_ordered_extent(ordered);
732 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
733 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
735 unlock_extent(&BTRFS_I(inode)->io_tree,
736 start_pos, last_pos - 1, GFP_NOFS);
738 for (i = 0; i < num_pages; i++) {
739 clear_page_dirty_for_io(pages[i]);
740 set_page_extent_mapped(pages[i]);
741 WARN_ON(!PageLocked(pages[i]));
746 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
747 size_t count, loff_t *ppos)
751 ssize_t num_written = 0;
754 struct inode *inode = fdentry(file)->d_inode;
755 struct btrfs_root *root = BTRFS_I(inode)->root;
756 struct page **pages = NULL;
758 struct page *pinned[2];
759 unsigned long first_index;
760 unsigned long last_index;
763 will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
764 (file->f_flags & O_DIRECT));
766 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
767 PAGE_CACHE_SIZE / (sizeof(struct page *)));
774 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
775 current->backing_dev_info = inode->i_mapping->backing_dev_info;
776 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
782 err = file_remove_suid(file);
785 file_update_time(file);
787 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
789 mutex_lock(&inode->i_mutex);
790 first_index = pos >> PAGE_CACHE_SHIFT;
791 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
794 * if this is a nodatasum mount, force summing off for the inode
795 * all the time. That way a later mount with summing on won't
798 if (btrfs_test_opt(root, NODATASUM))
799 btrfs_set_flag(inode, NODATASUM);
802 * there are lots of better ways to do this, but this code
803 * makes sure the first and last page in the file range are
804 * up to date and ready for cow
806 if ((pos & (PAGE_CACHE_SIZE - 1))) {
807 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
808 if (!PageUptodate(pinned[0])) {
809 ret = btrfs_readpage(NULL, pinned[0]);
811 wait_on_page_locked(pinned[0]);
813 unlock_page(pinned[0]);
816 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
817 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
818 if (!PageUptodate(pinned[1])) {
819 ret = btrfs_readpage(NULL, pinned[1]);
821 wait_on_page_locked(pinned[1]);
823 unlock_page(pinned[1]);
828 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
829 size_t write_bytes = min(count, nrptrs *
830 (size_t)PAGE_CACHE_SIZE -
832 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
835 WARN_ON(num_pages > nrptrs);
836 memset(pages, 0, sizeof(pages));
838 ret = btrfs_check_free_space(root, write_bytes, 0);
842 ret = prepare_pages(root, file, pages, num_pages,
843 pos, first_index, last_index,
848 ret = btrfs_copy_from_user(pos, num_pages,
849 write_bytes, pages, buf);
851 btrfs_drop_pages(pages, num_pages);
855 ret = dirty_and_release_pages(NULL, root, file, pages,
856 num_pages, pos, write_bytes);
857 btrfs_drop_pages(pages, num_pages);
862 btrfs_fdatawrite_range(inode->i_mapping, pos,
863 pos + write_bytes - 1,
866 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
869 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
870 btrfs_btree_balance_dirty(root, 1);
871 btrfs_throttle(root);
875 count -= write_bytes;
877 num_written += write_bytes;
882 mutex_unlock(&inode->i_mutex);
887 page_cache_release(pinned[0]);
889 page_cache_release(pinned[1]);
892 if (num_written > 0 && will_write) {
893 struct btrfs_trans_handle *trans;
895 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
899 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
900 trans = btrfs_start_transaction(root, 1);
901 ret = btrfs_log_dentry_safe(trans, root,
904 btrfs_sync_log(trans, root);
905 btrfs_end_transaction(trans, root);
907 btrfs_commit_transaction(trans, root);
910 if (file->f_flags & O_DIRECT) {
911 invalidate_mapping_pages(inode->i_mapping,
912 start_pos >> PAGE_CACHE_SHIFT,
913 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
916 current->backing_dev_info = NULL;
917 return num_written ? num_written : err;
920 int btrfs_release_file(struct inode * inode, struct file * filp)
922 if (filp->private_data)
923 btrfs_ioctl_trans_end(filp);
928 * fsync call for both files and directories. This logs the inode into
929 * the tree log instead of forcing full commits whenever possible.
931 * It needs to call filemap_fdatawait so that all ordered extent updates are
932 * in the metadata btree are up to date for copying to the log.
934 * It drops the inode mutex before doing the tree log commit. This is an
935 * important optimization for directories because holding the mutex prevents
936 * new operations on the dir while we write to disk.
938 int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
940 struct inode *inode = dentry->d_inode;
941 struct btrfs_root *root = BTRFS_I(inode)->root;
943 struct btrfs_trans_handle *trans;
946 * check the transaction that last modified this inode
947 * and see if its already been committed
949 if (!BTRFS_I(inode)->last_trans)
952 mutex_lock(&root->fs_info->trans_mutex);
953 if (BTRFS_I(inode)->last_trans <=
954 root->fs_info->last_trans_committed) {
955 BTRFS_I(inode)->last_trans = 0;
956 mutex_unlock(&root->fs_info->trans_mutex);
959 mutex_unlock(&root->fs_info->trans_mutex);
961 root->fs_info->tree_log_batch++;
962 filemap_fdatawait(inode->i_mapping);
963 root->fs_info->tree_log_batch++;
966 * ok we haven't committed the transaction yet, lets do a commit
968 if (file->private_data)
969 btrfs_ioctl_trans_end(file);
971 trans = btrfs_start_transaction(root, 1);
977 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
982 /* we've logged all the items and now have a consistent
983 * version of the file in the log. It is possible that
984 * someone will come in and modify the file, but that's
985 * fine because the log is consistent on disk, and we
986 * have references to all of the file's extents
988 * It is possible that someone will come in and log the
989 * file again, but that will end up using the synchronization
990 * inside btrfs_sync_log to keep things safe.
992 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
995 ret = btrfs_commit_transaction(trans, root);
997 btrfs_sync_log(trans, root);
998 ret = btrfs_end_transaction(trans, root);
1000 mutex_lock(&file->f_dentry->d_inode->i_mutex);
1002 return ret > 0 ? EIO : ret;
1005 static struct vm_operations_struct btrfs_file_vm_ops = {
1006 .fault = filemap_fault,
1007 .page_mkwrite = btrfs_page_mkwrite,
1010 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1012 vma->vm_ops = &btrfs_file_vm_ops;
1013 file_accessed(filp);
1017 struct file_operations btrfs_file_operations = {
1018 .llseek = generic_file_llseek,
1019 .read = do_sync_read,
1020 .aio_read = generic_file_aio_read,
1021 .splice_read = generic_file_splice_read,
1022 .write = btrfs_file_write,
1023 .mmap = btrfs_file_mmap,
1024 .open = generic_file_open,
1025 .release = btrfs_release_file,
1026 .fsync = btrfs_sync_file,
1027 .unlocked_ioctl = btrfs_ioctl,
1028 #ifdef CONFIG_COMPAT
1029 .compat_ioctl = btrfs_ioctl,