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 static int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
46 struct page **prepared_pages,
47 const char __user * buf)
51 int offset = pos & (PAGE_CACHE_SIZE - 1);
53 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
54 size_t count = min_t(size_t,
55 PAGE_CACHE_SIZE - offset, write_bytes);
56 struct page *page = prepared_pages[i];
57 fault_in_pages_readable(buf, count);
59 /* Copy data from userspace to the current page */
61 page_fault = __copy_from_user(page_address(page) + offset,
63 /* Flush processor's dcache for this page */
64 flush_dcache_page(page);
72 return page_fault ? -EFAULT : 0;
75 static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
78 for (i = 0; i < num_pages; i++) {
81 ClearPageChecked(pages[i]);
82 unlock_page(pages[i]);
83 mark_page_accessed(pages[i]);
84 page_cache_release(pages[i]);
88 static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
89 struct btrfs_root *root, struct inode *inode,
90 u64 offset, size_t size,
91 struct page **pages, size_t page_offset,
95 struct btrfs_path *path;
96 struct extent_buffer *leaf;
99 struct btrfs_file_extent_item *ei;
107 path = btrfs_alloc_path();
111 btrfs_set_trans_block_group(trans, inode);
113 key.objectid = inode->i_ino;
115 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
117 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
123 struct btrfs_key found_key;
125 if (path->slots[0] == 0)
129 leaf = path->nodes[0];
130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
132 if (found_key.objectid != inode->i_ino)
135 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
137 ei = btrfs_item_ptr(leaf, path->slots[0],
138 struct btrfs_file_extent_item);
140 if (btrfs_file_extent_type(leaf, ei) !=
141 BTRFS_FILE_EXTENT_INLINE) {
144 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
151 leaf = path->nodes[0];
152 ei = btrfs_item_ptr(leaf, path->slots[0],
153 struct btrfs_file_extent_item);
155 if (btrfs_file_extent_type(leaf, ei) !=
156 BTRFS_FILE_EXTENT_INLINE) {
158 btrfs_print_leaf(root, leaf);
159 printk("found wasn't inline offset %Lu inode %lu\n",
160 offset, inode->i_ino);
163 found_size = btrfs_file_extent_inline_len(leaf,
164 btrfs_item_nr(leaf, path->slots[0]));
165 found_end = key.offset + found_size;
167 if (found_end < offset + size) {
168 btrfs_release_path(root, path);
169 ret = btrfs_search_slot(trans, root, &key, path,
170 offset + size - found_end, 1);
173 ret = btrfs_extend_item(trans, root, path,
174 offset + size - found_end);
179 leaf = path->nodes[0];
180 ei = btrfs_item_ptr(leaf, path->slots[0],
181 struct btrfs_file_extent_item);
182 inode->i_blocks += (offset + size - found_end) >> 9;
184 if (found_end < offset) {
185 ptr = btrfs_file_extent_inline_start(ei) + found_size;
186 memset_extent_buffer(leaf, 0, ptr, offset - found_end);
190 btrfs_release_path(root, path);
191 datasize = offset + size - key.offset;
192 inode->i_blocks += datasize >> 9;
193 datasize = btrfs_file_extent_calc_inline_size(datasize);
194 ret = btrfs_insert_empty_item(trans, root, path, &key,
198 printk("got bad ret %d\n", ret);
201 leaf = path->nodes[0];
202 ei = btrfs_item_ptr(leaf, path->slots[0],
203 struct btrfs_file_extent_item);
204 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
205 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
207 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
213 kaddr = kmap_atomic(page, KM_USER0);
214 cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
215 write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
216 kunmap_atomic(kaddr, KM_USER0);
220 if (i >= num_pages) {
221 printk("i %d num_pages %d\n", i, num_pages);
225 btrfs_mark_buffer_dirty(leaf);
227 btrfs_free_path(path);
231 static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
232 struct btrfs_root *root,
241 struct inode *inode = fdentry(file)->d_inode;
242 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
246 u64 end_of_last_block;
247 u64 end_pos = pos + write_bytes;
250 loff_t isize = i_size_read(inode);
252 start_pos = pos & ~((u64)root->sectorsize - 1);
253 num_bytes = (write_bytes + pos - start_pos +
254 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
256 end_of_last_block = start_pos + num_bytes - 1;
258 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
259 trans = btrfs_join_transaction(root, 1);
264 btrfs_set_trans_block_group(trans, inode);
267 if ((end_of_last_block & 4095) == 0) {
268 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
270 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
272 /* FIXME...EIEIO, ENOSPC and more */
273 /* insert any holes we need to create */
274 if (isize < start_pos) {
275 u64 last_pos_in_file;
277 u64 mask = root->sectorsize - 1;
278 last_pos_in_file = (isize + mask) & ~mask;
279 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
281 btrfs_wait_ordered_range(inode, last_pos_in_file,
282 last_pos_in_file + hole_size);
283 mutex_lock(&BTRFS_I(inode)->extent_mutex);
284 err = btrfs_drop_extents(trans, root, inode,
286 last_pos_in_file + hole_size,
292 err = btrfs_insert_file_extent(trans, root,
296 btrfs_drop_extent_cache(inode, last_pos_in_file,
297 last_pos_in_file + hole_size - 1, 0);
298 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
299 btrfs_check_file(root, inode);
306 * either allocate an extent for the new bytes or setup the key
307 * to show we are doing inline data in the extent
309 inline_size = end_pos;
310 if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
311 inline_size > root->fs_info->max_inline ||
312 (inline_size & (root->sectorsize -1)) == 0 ||
313 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
314 /* check for reserved extents on each page, we don't want
315 * to reset the delalloc bit on things that already have
318 btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
319 for (i = 0; i < num_pages; i++) {
320 struct page *p = pages[i];
327 /* step one, delete the existing extents in this range */
328 aligned_end = (pos + write_bytes + root->sectorsize - 1) &
329 ~((u64)root->sectorsize - 1);
330 mutex_lock(&BTRFS_I(inode)->extent_mutex);
331 err = btrfs_drop_extents(trans, root, inode, start_pos,
332 aligned_end, aligned_end, &hint_byte);
335 if (isize > inline_size)
336 inline_size = min_t(u64, isize, aligned_end);
337 inline_size -= start_pos;
338 err = insert_inline_extent(trans, root, inode, start_pos,
339 inline_size, pages, 0, num_pages);
340 btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1, 0);
342 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
345 * an ugly way to do all the prop accounting around
346 * the page bits and mapping tags
348 set_page_writeback(pages[0]);
349 end_page_writeback(pages[0]);
352 if (end_pos > isize) {
353 i_size_write(inode, end_pos);
355 BTRFS_I(inode)->disk_i_size = end_pos;
356 btrfs_update_inode(trans, root, inode);
359 err = btrfs_end_transaction(trans, root);
361 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
365 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
368 struct extent_map *em;
369 struct extent_map *split = NULL;
370 struct extent_map *split2 = NULL;
371 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
372 u64 len = end - start + 1;
377 WARN_ON(end < start);
378 if (end == (u64)-1) {
384 split = alloc_extent_map(GFP_NOFS);
386 split2 = alloc_extent_map(GFP_NOFS);
388 spin_lock(&em_tree->lock);
389 em = lookup_extent_mapping(em_tree, start, len);
391 spin_unlock(&em_tree->lock);
395 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
396 spin_unlock(&em_tree->lock);
397 if (em->start <= start &&
398 (!testend || em->start + em->len >= start + len)) {
402 if (start < em->start) {
403 len = em->start - start;
405 len = start + len - (em->start + em->len);
406 start = em->start + em->len;
411 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
412 remove_extent_mapping(em_tree, em);
414 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
416 split->start = em->start;
417 split->len = start - em->start;
418 split->block_start = em->block_start;
419 split->bdev = em->bdev;
420 split->flags = flags;
421 ret = add_extent_mapping(em_tree, split);
423 free_extent_map(split);
427 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
428 testend && em->start + em->len > start + len) {
429 u64 diff = start + len - em->start;
431 split->start = start + len;
432 split->len = em->start + em->len - (start + len);
433 split->bdev = em->bdev;
434 split->flags = flags;
436 split->block_start = em->block_start + diff;
438 ret = add_extent_mapping(em_tree, split);
440 free_extent_map(split);
443 spin_unlock(&em_tree->lock);
447 /* once for the tree*/
451 free_extent_map(split);
453 free_extent_map(split2);
457 int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
461 struct btrfs_path *path;
462 struct btrfs_key found_key;
463 struct extent_buffer *leaf;
464 struct btrfs_file_extent_item *extent;
473 path = btrfs_alloc_path();
474 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
477 nritems = btrfs_header_nritems(path->nodes[0]);
478 if (path->slots[0] >= nritems) {
479 ret = btrfs_next_leaf(root, path);
482 nritems = btrfs_header_nritems(path->nodes[0]);
484 slot = path->slots[0];
485 leaf = path->nodes[0];
486 btrfs_item_key_to_cpu(leaf, &found_key, slot);
487 if (found_key.objectid != inode->i_ino)
489 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
492 if (found_key.offset < last_offset) {
494 btrfs_print_leaf(root, leaf);
495 printk("inode %lu found offset %Lu expected %Lu\n",
496 inode->i_ino, found_key.offset, last_offset);
500 extent = btrfs_item_ptr(leaf, slot,
501 struct btrfs_file_extent_item);
502 found_type = btrfs_file_extent_type(leaf, extent);
503 if (found_type == BTRFS_FILE_EXTENT_REG) {
504 extent_end = found_key.offset +
505 btrfs_file_extent_num_bytes(leaf, extent);
506 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
507 struct btrfs_item *item;
508 item = btrfs_item_nr(leaf, slot);
509 extent_end = found_key.offset +
510 btrfs_file_extent_inline_len(leaf, item);
511 extent_end = (extent_end + root->sectorsize - 1) &
512 ~((u64)root->sectorsize -1 );
514 last_offset = extent_end;
517 if (0 && last_offset < inode->i_size) {
519 btrfs_print_leaf(root, leaf);
520 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
521 last_offset, inode->i_size);
526 btrfs_free_path(path);
532 * this is very complex, but the basic idea is to drop all extents
533 * in the range start - end. hint_block is filled in with a block number
534 * that would be a good hint to the block allocator for this file.
536 * If an extent intersects the range but is not entirely inside the range
537 * it is either truncated or split. Anything entirely inside the range
538 * is deleted from the tree.
540 int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root, struct inode *inode,
542 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
545 u64 search_start = start;
549 struct extent_buffer *leaf;
550 struct btrfs_file_extent_item *extent;
551 struct btrfs_path *path;
552 struct btrfs_key key;
553 struct btrfs_file_extent_item old;
563 btrfs_drop_extent_cache(inode, start, end - 1, 0);
565 path = btrfs_alloc_path();
570 btrfs_release_path(root, path);
571 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
576 if (path->slots[0] == 0) {
591 leaf = path->nodes[0];
592 slot = path->slots[0];
594 btrfs_item_key_to_cpu(leaf, &key, slot);
595 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
599 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
600 key.objectid != inode->i_ino) {
604 search_start = key.offset;
607 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
608 extent = btrfs_item_ptr(leaf, slot,
609 struct btrfs_file_extent_item);
610 found_type = btrfs_file_extent_type(leaf, extent);
611 if (found_type == BTRFS_FILE_EXTENT_REG) {
613 btrfs_file_extent_disk_bytenr(leaf,
616 *hint_byte = extent_end;
618 extent_end = key.offset +
619 btrfs_file_extent_num_bytes(leaf, extent);
621 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
622 struct btrfs_item *item;
623 item = btrfs_item_nr(leaf, slot);
625 extent_end = key.offset +
626 btrfs_file_extent_inline_len(leaf, item);
629 extent_end = search_start;
632 /* we found nothing we can drop */
633 if ((!found_extent && !found_inline) ||
634 search_start >= extent_end) {
637 nritems = btrfs_header_nritems(leaf);
638 if (slot >= nritems - 1) {
639 nextret = btrfs_next_leaf(root, path);
650 u64 mask = root->sectorsize - 1;
651 search_start = (extent_end + mask) & ~mask;
653 search_start = extent_end;
654 if (end <= extent_end && start >= key.offset && found_inline) {
655 *hint_byte = EXTENT_MAP_INLINE;
660 read_extent_buffer(leaf, &old, (unsigned long)extent,
662 root_gen = btrfs_header_generation(leaf);
663 root_owner = btrfs_header_owner(leaf);
664 leaf_start = leaf->start;
667 if (end < extent_end && end >= key.offset) {
669 if (found_inline && start <= key.offset)
672 /* truncate existing extent */
673 if (start > key.offset) {
677 WARN_ON(start & (root->sectorsize - 1));
679 new_num = start - key.offset;
680 old_num = btrfs_file_extent_num_bytes(leaf,
683 btrfs_file_extent_disk_bytenr(leaf,
685 if (btrfs_file_extent_disk_bytenr(leaf,
687 dec_i_blocks(inode, old_num - new_num);
689 btrfs_set_file_extent_num_bytes(leaf, extent,
691 btrfs_mark_buffer_dirty(leaf);
692 } else if (key.offset < inline_limit &&
693 (end > extent_end) &&
694 (inline_limit < extent_end)) {
696 new_size = btrfs_file_extent_calc_inline_size(
697 inline_limit - key.offset);
698 dec_i_blocks(inode, (extent_end - key.offset) -
699 (inline_limit - key.offset));
700 btrfs_truncate_item(trans, root, path,
704 /* delete the entire extent */
706 ret = btrfs_del_item(trans, root, path);
707 /* TODO update progress marker and return */
710 btrfs_release_path(root, path);
711 /* the extent will be freed later */
713 if (bookend && found_inline && start <= key.offset) {
715 new_size = btrfs_file_extent_calc_inline_size(
717 dec_i_blocks(inode, (extent_end - key.offset) -
719 ret = btrfs_truncate_item(trans, root, path,
723 /* create bookend, splitting the extent in two */
724 if (bookend && found_extent) {
726 struct btrfs_key ins;
727 ins.objectid = inode->i_ino;
729 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
730 btrfs_release_path(root, path);
731 ret = btrfs_insert_empty_item(trans, root, path, &ins,
735 leaf = path->nodes[0];
736 extent = btrfs_item_ptr(leaf, path->slots[0],
737 struct btrfs_file_extent_item);
738 write_extent_buffer(leaf, &old,
739 (unsigned long)extent, sizeof(old));
741 btrfs_set_file_extent_offset(leaf, extent,
742 le64_to_cpu(old.offset) + end - key.offset);
743 WARN_ON(le64_to_cpu(old.num_bytes) <
745 btrfs_set_file_extent_num_bytes(leaf, extent,
747 btrfs_set_file_extent_type(leaf, extent,
748 BTRFS_FILE_EXTENT_REG);
750 btrfs_mark_buffer_dirty(path->nodes[0]);
752 disk_bytenr = le64_to_cpu(old.disk_bytenr);
753 if (disk_bytenr != 0) {
754 ret = btrfs_inc_extent_ref(trans, root,
756 le64_to_cpu(old.disk_num_bytes),
758 root->root_key.objectid,
760 ins.objectid, ins.offset);
763 btrfs_release_path(root, path);
764 if (disk_bytenr != 0) {
766 btrfs_file_extent_num_bytes(leaf,
771 if (found_extent && !keep) {
772 u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
774 if (disk_bytenr != 0) {
775 dec_i_blocks(inode, le64_to_cpu(old.num_bytes));
776 ret = btrfs_free_extent(trans, root,
778 le64_to_cpu(old.disk_num_bytes),
779 leaf_start, root_owner,
780 root_gen, key.objectid,
783 *hint_byte = disk_bytenr;
787 if (search_start >= end) {
793 btrfs_free_path(path);
794 btrfs_check_file(root, inode);
799 * this gets pages into the page cache and locks them down
801 static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
802 struct page **pages, size_t num_pages,
803 loff_t pos, unsigned long first_index,
804 unsigned long last_index, size_t write_bytes)
807 unsigned long index = pos >> PAGE_CACHE_SHIFT;
808 struct inode *inode = fdentry(file)->d_inode;
813 start_pos = pos & ~((u64)root->sectorsize - 1);
814 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
816 memset(pages, 0, num_pages * sizeof(struct page *));
818 for (i = 0; i < num_pages; i++) {
819 pages[i] = grab_cache_page(inode->i_mapping, index + i);
824 wait_on_page_writeback(pages[i]);
826 if (start_pos < inode->i_size) {
827 struct btrfs_ordered_extent *ordered;
828 lock_extent(&BTRFS_I(inode)->io_tree,
829 start_pos, last_pos - 1, GFP_NOFS);
830 ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
832 ordered->file_offset + ordered->len > start_pos &&
833 ordered->file_offset < last_pos) {
834 btrfs_put_ordered_extent(ordered);
835 unlock_extent(&BTRFS_I(inode)->io_tree,
836 start_pos, last_pos - 1, GFP_NOFS);
837 for (i = 0; i < num_pages; i++) {
838 unlock_page(pages[i]);
839 page_cache_release(pages[i]);
841 btrfs_wait_ordered_range(inode, start_pos,
842 last_pos - start_pos);
846 btrfs_put_ordered_extent(ordered);
848 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
849 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
851 unlock_extent(&BTRFS_I(inode)->io_tree,
852 start_pos, last_pos - 1, GFP_NOFS);
854 for (i = 0; i < num_pages; i++) {
855 clear_page_dirty_for_io(pages[i]);
856 set_page_extent_mapped(pages[i]);
857 WARN_ON(!PageLocked(pages[i]));
862 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
863 size_t count, loff_t *ppos)
867 ssize_t num_written = 0;
870 struct inode *inode = fdentry(file)->d_inode;
871 struct btrfs_root *root = BTRFS_I(inode)->root;
872 struct page **pages = NULL;
874 struct page *pinned[2];
875 unsigned long first_index;
876 unsigned long last_index;
878 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
879 PAGE_CACHE_SIZE / (sizeof(struct page *)));
886 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
887 current->backing_dev_info = inode->i_mapping->backing_dev_info;
888 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
894 err = file_remove_suid(file);
897 file_update_time(file);
899 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
901 mutex_lock(&inode->i_mutex);
902 first_index = pos >> PAGE_CACHE_SHIFT;
903 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
906 * if this is a nodatasum mount, force summing off for the inode
907 * all the time. That way a later mount with summing on won't
910 if (btrfs_test_opt(root, NODATASUM))
911 btrfs_set_flag(inode, NODATASUM);
914 * there are lots of better ways to do this, but this code
915 * makes sure the first and last page in the file range are
916 * up to date and ready for cow
918 if ((pos & (PAGE_CACHE_SIZE - 1))) {
919 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
920 if (!PageUptodate(pinned[0])) {
921 ret = btrfs_readpage(NULL, pinned[0]);
923 wait_on_page_locked(pinned[0]);
925 unlock_page(pinned[0]);
928 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
929 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
930 if (!PageUptodate(pinned[1])) {
931 ret = btrfs_readpage(NULL, pinned[1]);
933 wait_on_page_locked(pinned[1]);
935 unlock_page(pinned[1]);
940 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
941 size_t write_bytes = min(count, nrptrs *
942 (size_t)PAGE_CACHE_SIZE -
944 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
947 WARN_ON(num_pages > nrptrs);
948 memset(pages, 0, sizeof(pages));
950 ret = btrfs_check_free_space(root, write_bytes, 0);
954 ret = prepare_pages(root, file, pages, num_pages,
955 pos, first_index, last_index,
960 ret = btrfs_copy_from_user(pos, num_pages,
961 write_bytes, pages, buf);
963 btrfs_drop_pages(pages, num_pages);
967 ret = dirty_and_release_pages(NULL, root, file, pages,
968 num_pages, pos, write_bytes);
969 btrfs_drop_pages(pages, num_pages);
974 count -= write_bytes;
976 num_written += write_bytes;
978 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
979 if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
980 btrfs_btree_balance_dirty(root, 1);
981 btrfs_throttle(root);
985 mutex_unlock(&inode->i_mutex);
990 page_cache_release(pinned[0]);
992 page_cache_release(pinned[1]);
995 if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
996 struct btrfs_trans_handle *trans;
998 err = btrfs_fdatawrite_range(inode->i_mapping, start_pos,
999 start_pos + num_written -1,
1004 err = btrfs_wait_on_page_writeback_range(inode->i_mapping,
1005 start_pos, start_pos + num_written - 1);
1009 trans = btrfs_start_transaction(root, 1);
1010 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
1012 btrfs_sync_log(trans, root);
1013 btrfs_end_transaction(trans, root);
1015 btrfs_commit_transaction(trans, root);
1017 } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
1018 do_sync_mapping_range(inode->i_mapping, start_pos,
1019 start_pos + num_written - 1,
1020 SYNC_FILE_RANGE_WRITE |
1021 SYNC_FILE_RANGE_WAIT_AFTER);
1022 invalidate_mapping_pages(inode->i_mapping,
1023 start_pos >> PAGE_CACHE_SHIFT,
1024 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1026 current->backing_dev_info = NULL;
1027 return num_written ? num_written : err;
1030 int btrfs_release_file(struct inode * inode, struct file * filp)
1032 if (filp->private_data)
1033 btrfs_ioctl_trans_end(filp);
1037 int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1039 struct inode *inode = dentry->d_inode;
1040 struct btrfs_root *root = BTRFS_I(inode)->root;
1042 struct btrfs_trans_handle *trans;
1045 * check the transaction that last modified this inode
1046 * and see if its already been committed
1048 if (!BTRFS_I(inode)->last_trans)
1051 mutex_lock(&root->fs_info->trans_mutex);
1052 if (BTRFS_I(inode)->last_trans <=
1053 root->fs_info->last_trans_committed) {
1054 BTRFS_I(inode)->last_trans = 0;
1055 mutex_unlock(&root->fs_info->trans_mutex);
1058 mutex_unlock(&root->fs_info->trans_mutex);
1060 root->fs_info->tree_log_batch++;
1061 filemap_fdatawait(inode->i_mapping);
1062 root->fs_info->tree_log_batch++;
1065 * ok we haven't committed the transaction yet, lets do a commit
1067 if (file->private_data)
1068 btrfs_ioctl_trans_end(file);
1070 trans = btrfs_start_transaction(root, 1);
1076 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
1081 /* we've logged all the items and now have a consistent
1082 * version of the file in the log. It is possible that
1083 * someone will come in and modify the file, but that's
1084 * fine because the log is consistent on disk, and we
1085 * have references to all of the file's extents
1087 * It is possible that someone will come in and log the
1088 * file again, but that will end up using the synchronization
1089 * inside btrfs_sync_log to keep things safe.
1091 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
1094 ret = btrfs_commit_transaction(trans, root);
1096 btrfs_sync_log(trans, root);
1097 ret = btrfs_end_transaction(trans, root);
1099 mutex_lock(&file->f_dentry->d_inode->i_mutex);
1101 return ret > 0 ? EIO : ret;
1104 static struct vm_operations_struct btrfs_file_vm_ops = {
1105 .fault = filemap_fault,
1106 .page_mkwrite = btrfs_page_mkwrite,
1109 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1111 vma->vm_ops = &btrfs_file_vm_ops;
1112 file_accessed(filp);
1116 struct file_operations btrfs_file_operations = {
1117 .llseek = generic_file_llseek,
1118 .read = do_sync_read,
1119 .aio_read = generic_file_aio_read,
1120 .splice_read = generic_file_splice_read,
1121 .write = btrfs_file_write,
1122 .mmap = btrfs_file_mmap,
1123 .open = generic_file_open,
1124 .release = btrfs_release_file,
1125 .fsync = btrfs_sync_file,
1126 .unlocked_ioctl = btrfs_ioctl,
1127 #ifdef CONFIG_COMPAT
1128 .compat_ioctl = btrfs_ioctl,