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]);
98 /* this does all the hard work for inserting an inline extent into
99 * the btree. Any existing inline extent is extended as required to make room,
100 * otherwise things are inserted as required into the btree
102 static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root, struct inode *inode,
104 u64 offset, size_t size,
105 struct page **pages, size_t page_offset,
108 struct btrfs_key key;
109 struct btrfs_path *path;
110 struct extent_buffer *leaf;
113 struct btrfs_file_extent_item *ei;
121 path = btrfs_alloc_path();
125 btrfs_set_trans_block_group(trans, inode);
127 key.objectid = inode->i_ino;
129 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
131 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
137 struct btrfs_key found_key;
139 if (path->slots[0] == 0)
143 leaf = path->nodes[0];
144 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
146 if (found_key.objectid != inode->i_ino)
149 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
154 if (btrfs_file_extent_type(leaf, ei) !=
155 BTRFS_FILE_EXTENT_INLINE) {
158 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
165 leaf = path->nodes[0];
166 ei = btrfs_item_ptr(leaf, path->slots[0],
167 struct btrfs_file_extent_item);
169 if (btrfs_file_extent_type(leaf, ei) !=
170 BTRFS_FILE_EXTENT_INLINE) {
172 btrfs_print_leaf(root, leaf);
173 printk("found wasn't inline offset %Lu inode %lu\n",
174 offset, inode->i_ino);
177 found_size = btrfs_file_extent_inline_len(leaf,
178 btrfs_item_nr(leaf, path->slots[0]));
179 found_end = key.offset + found_size;
181 if (found_end < offset + size) {
182 btrfs_release_path(root, path);
183 ret = btrfs_search_slot(trans, root, &key, path,
184 offset + size - found_end, 1);
187 ret = btrfs_extend_item(trans, root, path,
188 offset + size - found_end);
193 leaf = path->nodes[0];
194 ei = btrfs_item_ptr(leaf, path->slots[0],
195 struct btrfs_file_extent_item);
196 inode_add_bytes(inode, offset + size - found_end);
198 if (found_end < offset) {
199 ptr = btrfs_file_extent_inline_start(ei) + found_size;
200 memset_extent_buffer(leaf, 0, ptr, offset - found_end);
204 btrfs_release_path(root, path);
205 datasize = offset + size - key.offset;
206 inode_add_bytes(inode, datasize);
207 datasize = btrfs_file_extent_calc_inline_size(datasize);
208 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 printk("got bad ret %d\n", ret);
215 leaf = path->nodes[0];
216 ei = btrfs_item_ptr(leaf, path->slots[0],
217 struct btrfs_file_extent_item);
218 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
219 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
227 kaddr = kmap_atomic(page, KM_USER0);
228 cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
229 write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
230 kunmap_atomic(kaddr, KM_USER0);
234 if (i >= num_pages) {
235 printk("i %d num_pages %d\n", i, num_pages);
239 btrfs_mark_buffer_dirty(leaf);
241 btrfs_free_path(path);
246 * after copy_from_user, pages need to be dirtied and we need to make
247 * sure holes are created between the current EOF and the start of
248 * any next extents (if required).
250 * this also makes the decision about creating an inline extent vs
251 * doing real data extents, marking pages dirty and delalloc as required.
253 static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
254 struct btrfs_root *root,
263 struct inode *inode = fdentry(file)->d_inode;
264 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
268 u64 end_of_last_block;
269 u64 end_pos = pos + write_bytes;
272 loff_t isize = i_size_read(inode);
274 start_pos = pos & ~((u64)root->sectorsize - 1);
275 num_bytes = (write_bytes + pos - start_pos +
276 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
278 end_of_last_block = start_pos + num_bytes - 1;
280 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
281 trans = btrfs_join_transaction(root, 1);
286 btrfs_set_trans_block_group(trans, inode);
289 if ((end_of_last_block & 4095) == 0) {
290 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
292 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
294 /* FIXME...EIEIO, ENOSPC and more */
295 /* insert any holes we need to create */
296 if (isize < start_pos) {
297 u64 last_pos_in_file;
299 u64 mask = root->sectorsize - 1;
300 last_pos_in_file = (isize + mask) & ~mask;
301 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
303 btrfs_wait_ordered_range(inode, last_pos_in_file,
304 last_pos_in_file + hole_size);
305 mutex_lock(&BTRFS_I(inode)->extent_mutex);
306 err = btrfs_drop_extents(trans, root, inode,
308 last_pos_in_file + hole_size,
314 err = btrfs_insert_file_extent(trans, root,
318 btrfs_drop_extent_cache(inode, last_pos_in_file,
319 last_pos_in_file + hole_size - 1, 0);
320 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
321 btrfs_check_file(root, inode);
328 * either allocate an extent for the new bytes or setup the key
329 * to show we are doing inline data in the extent
331 inline_size = end_pos;
332 if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
333 inline_size > root->fs_info->max_inline ||
334 (inline_size & (root->sectorsize -1)) == 0 ||
335 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
336 /* check for reserved extents on each page, we don't want
337 * to reset the delalloc bit on things that already have
340 btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
341 for (i = 0; i < num_pages; i++) {
342 struct page *p = pages[i];
349 /* step one, delete the existing extents in this range */
350 aligned_end = (pos + write_bytes + root->sectorsize - 1) &
351 ~((u64)root->sectorsize - 1);
352 mutex_lock(&BTRFS_I(inode)->extent_mutex);
353 err = btrfs_drop_extents(trans, root, inode, start_pos,
354 aligned_end, aligned_end, &hint_byte);
357 if (isize > inline_size)
358 inline_size = min_t(u64, isize, aligned_end);
359 inline_size -= start_pos;
360 err = insert_inline_extent(trans, root, inode, start_pos,
361 inline_size, pages, 0, num_pages);
362 btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1, 0);
364 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
367 * an ugly way to do all the prop accounting around
368 * the page bits and mapping tags
370 set_page_writeback(pages[0]);
371 end_page_writeback(pages[0]);
374 if (end_pos > isize) {
375 i_size_write(inode, end_pos);
377 BTRFS_I(inode)->disk_i_size = end_pos;
378 btrfs_update_inode(trans, root, inode);
381 err = btrfs_end_transaction(trans, root);
383 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
388 * this drops all the extents in the cache that intersect the range
389 * [start, end]. Existing extents are split as required.
391 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
394 struct extent_map *em;
395 struct extent_map *split = NULL;
396 struct extent_map *split2 = NULL;
397 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
398 u64 len = end - start + 1;
403 WARN_ON(end < start);
404 if (end == (u64)-1) {
410 split = alloc_extent_map(GFP_NOFS);
412 split2 = alloc_extent_map(GFP_NOFS);
414 spin_lock(&em_tree->lock);
415 em = lookup_extent_mapping(em_tree, start, len);
417 spin_unlock(&em_tree->lock);
421 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
422 spin_unlock(&em_tree->lock);
423 if (em->start <= start &&
424 (!testend || em->start + em->len >= start + len)) {
428 if (start < em->start) {
429 len = em->start - start;
431 len = start + len - (em->start + em->len);
432 start = em->start + em->len;
437 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
438 remove_extent_mapping(em_tree, em);
440 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
442 split->start = em->start;
443 split->len = start - em->start;
444 split->block_start = em->block_start;
445 split->bdev = em->bdev;
446 split->flags = flags;
447 ret = add_extent_mapping(em_tree, split);
449 free_extent_map(split);
453 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
454 testend && em->start + em->len > start + len) {
455 u64 diff = start + len - em->start;
457 split->start = start + len;
458 split->len = em->start + em->len - (start + len);
459 split->bdev = em->bdev;
460 split->flags = flags;
462 split->block_start = em->block_start + diff;
464 ret = add_extent_mapping(em_tree, split);
466 free_extent_map(split);
469 spin_unlock(&em_tree->lock);
473 /* once for the tree*/
477 free_extent_map(split);
479 free_extent_map(split2);
483 int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
487 struct btrfs_path *path;
488 struct btrfs_key found_key;
489 struct extent_buffer *leaf;
490 struct btrfs_file_extent_item *extent;
499 path = btrfs_alloc_path();
500 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
503 nritems = btrfs_header_nritems(path->nodes[0]);
504 if (path->slots[0] >= nritems) {
505 ret = btrfs_next_leaf(root, path);
508 nritems = btrfs_header_nritems(path->nodes[0]);
510 slot = path->slots[0];
511 leaf = path->nodes[0];
512 btrfs_item_key_to_cpu(leaf, &found_key, slot);
513 if (found_key.objectid != inode->i_ino)
515 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
518 if (found_key.offset < last_offset) {
520 btrfs_print_leaf(root, leaf);
521 printk("inode %lu found offset %Lu expected %Lu\n",
522 inode->i_ino, found_key.offset, last_offset);
526 extent = btrfs_item_ptr(leaf, slot,
527 struct btrfs_file_extent_item);
528 found_type = btrfs_file_extent_type(leaf, extent);
529 if (found_type == BTRFS_FILE_EXTENT_REG) {
530 extent_end = found_key.offset +
531 btrfs_file_extent_num_bytes(leaf, extent);
532 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
533 struct btrfs_item *item;
534 item = btrfs_item_nr(leaf, slot);
535 extent_end = found_key.offset +
536 btrfs_file_extent_inline_len(leaf, item);
537 extent_end = (extent_end + root->sectorsize - 1) &
538 ~((u64)root->sectorsize -1 );
540 last_offset = extent_end;
543 if (0 && last_offset < inode->i_size) {
545 btrfs_print_leaf(root, leaf);
546 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
547 last_offset, inode->i_size);
552 btrfs_free_path(path);
558 * this is very complex, but the basic idea is to drop all extents
559 * in the range start - end. hint_block is filled in with a block number
560 * that would be a good hint to the block allocator for this file.
562 * If an extent intersects the range but is not entirely inside the range
563 * it is either truncated or split. Anything entirely inside the range
564 * is deleted from the tree.
566 * inline_limit is used to tell this code which offsets in the file to keep
567 * if they contain inline extents.
569 int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root, struct inode *inode,
571 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
574 u64 search_start = start;
578 struct extent_buffer *leaf;
579 struct btrfs_file_extent_item *extent;
580 struct btrfs_path *path;
581 struct btrfs_key key;
582 struct btrfs_file_extent_item old;
592 btrfs_drop_extent_cache(inode, start, end - 1, 0);
594 path = btrfs_alloc_path();
599 btrfs_release_path(root, path);
600 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
605 if (path->slots[0] == 0) {
620 leaf = path->nodes[0];
621 slot = path->slots[0];
623 btrfs_item_key_to_cpu(leaf, &key, slot);
624 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
628 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
629 key.objectid != inode->i_ino) {
633 search_start = key.offset;
636 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
637 extent = btrfs_item_ptr(leaf, slot,
638 struct btrfs_file_extent_item);
639 found_type = btrfs_file_extent_type(leaf, extent);
640 if (found_type == BTRFS_FILE_EXTENT_REG) {
642 btrfs_file_extent_disk_bytenr(leaf,
645 *hint_byte = extent_end;
647 extent_end = key.offset +
648 btrfs_file_extent_num_bytes(leaf, extent);
650 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
651 struct btrfs_item *item;
652 item = btrfs_item_nr(leaf, slot);
654 extent_end = key.offset +
655 btrfs_file_extent_inline_len(leaf, item);
658 extent_end = search_start;
661 /* we found nothing we can drop */
662 if ((!found_extent && !found_inline) ||
663 search_start >= extent_end) {
666 nritems = btrfs_header_nritems(leaf);
667 if (slot >= nritems - 1) {
668 nextret = btrfs_next_leaf(root, path);
679 u64 mask = root->sectorsize - 1;
680 search_start = (extent_end + mask) & ~mask;
682 search_start = extent_end;
683 if (end <= extent_end && start >= key.offset && found_inline) {
684 *hint_byte = EXTENT_MAP_INLINE;
689 read_extent_buffer(leaf, &old, (unsigned long)extent,
691 root_gen = btrfs_header_generation(leaf);
692 root_owner = btrfs_header_owner(leaf);
693 leaf_start = leaf->start;
696 if (end < extent_end && end >= key.offset) {
698 if (found_inline && start <= key.offset)
701 /* truncate existing extent */
702 if (start > key.offset) {
706 WARN_ON(start & (root->sectorsize - 1));
708 new_num = start - key.offset;
709 old_num = btrfs_file_extent_num_bytes(leaf,
712 btrfs_file_extent_disk_bytenr(leaf,
714 if (btrfs_file_extent_disk_bytenr(leaf,
716 inode_sub_bytes(inode, old_num -
719 btrfs_set_file_extent_num_bytes(leaf, extent,
721 btrfs_mark_buffer_dirty(leaf);
722 } else if (key.offset < inline_limit &&
723 (end > extent_end) &&
724 (inline_limit < extent_end)) {
726 new_size = btrfs_file_extent_calc_inline_size(
727 inline_limit - key.offset);
728 inode_sub_bytes(inode, extent_end -
730 btrfs_truncate_item(trans, root, path,
734 /* delete the entire extent */
737 inode_sub_bytes(inode, extent_end -
739 ret = btrfs_del_item(trans, root, path);
740 /* TODO update progress marker and return */
743 btrfs_release_path(root, path);
744 /* the extent will be freed later */
746 if (bookend && found_inline && start <= key.offset) {
748 new_size = btrfs_file_extent_calc_inline_size(
750 inode_sub_bytes(inode, end - key.offset);
751 ret = btrfs_truncate_item(trans, root, path,
755 /* create bookend, splitting the extent in two */
756 if (bookend && found_extent) {
758 struct btrfs_key ins;
759 ins.objectid = inode->i_ino;
761 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
762 btrfs_release_path(root, path);
763 ret = btrfs_insert_empty_item(trans, root, path, &ins,
767 leaf = path->nodes[0];
768 extent = btrfs_item_ptr(leaf, path->slots[0],
769 struct btrfs_file_extent_item);
770 write_extent_buffer(leaf, &old,
771 (unsigned long)extent, sizeof(old));
773 btrfs_set_file_extent_offset(leaf, extent,
774 le64_to_cpu(old.offset) + end - key.offset);
775 WARN_ON(le64_to_cpu(old.num_bytes) <
777 btrfs_set_file_extent_num_bytes(leaf, extent,
779 btrfs_set_file_extent_type(leaf, extent,
780 BTRFS_FILE_EXTENT_REG);
782 btrfs_mark_buffer_dirty(path->nodes[0]);
784 disk_bytenr = le64_to_cpu(old.disk_bytenr);
785 if (disk_bytenr != 0) {
786 ret = btrfs_inc_extent_ref(trans, root,
788 le64_to_cpu(old.disk_num_bytes),
790 root->root_key.objectid,
791 trans->transid, ins.objectid);
794 btrfs_release_path(root, path);
795 if (disk_bytenr != 0) {
796 inode_add_bytes(inode, extent_end - end);
800 if (found_extent && !keep) {
801 u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
803 if (disk_bytenr != 0) {
804 inode_sub_bytes(inode,
805 le64_to_cpu(old.num_bytes));
806 ret = btrfs_free_extent(trans, root,
808 le64_to_cpu(old.disk_num_bytes),
809 leaf_start, root_owner,
810 root_gen, key.objectid, 0);
812 *hint_byte = disk_bytenr;
816 if (search_start >= end) {
822 btrfs_free_path(path);
823 btrfs_check_file(root, inode);
828 * this gets pages into the page cache and locks them down, it also properly
829 * waits for data=ordered extents to finish before allowing the pages to be
832 static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
833 struct page **pages, size_t num_pages,
834 loff_t pos, unsigned long first_index,
835 unsigned long last_index, size_t write_bytes)
838 unsigned long index = pos >> PAGE_CACHE_SHIFT;
839 struct inode *inode = fdentry(file)->d_inode;
844 start_pos = pos & ~((u64)root->sectorsize - 1);
845 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
847 memset(pages, 0, num_pages * sizeof(struct page *));
849 for (i = 0; i < num_pages; i++) {
850 pages[i] = grab_cache_page(inode->i_mapping, index + i);
855 wait_on_page_writeback(pages[i]);
857 if (start_pos < inode->i_size) {
858 struct btrfs_ordered_extent *ordered;
859 lock_extent(&BTRFS_I(inode)->io_tree,
860 start_pos, last_pos - 1, GFP_NOFS);
861 ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
863 ordered->file_offset + ordered->len > start_pos &&
864 ordered->file_offset < last_pos) {
865 btrfs_put_ordered_extent(ordered);
866 unlock_extent(&BTRFS_I(inode)->io_tree,
867 start_pos, last_pos - 1, GFP_NOFS);
868 for (i = 0; i < num_pages; i++) {
869 unlock_page(pages[i]);
870 page_cache_release(pages[i]);
872 btrfs_wait_ordered_range(inode, start_pos,
873 last_pos - start_pos);
877 btrfs_put_ordered_extent(ordered);
879 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
880 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
882 unlock_extent(&BTRFS_I(inode)->io_tree,
883 start_pos, last_pos - 1, GFP_NOFS);
885 for (i = 0; i < num_pages; i++) {
886 clear_page_dirty_for_io(pages[i]);
887 set_page_extent_mapped(pages[i]);
888 WARN_ON(!PageLocked(pages[i]));
893 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
894 size_t count, loff_t *ppos)
898 ssize_t num_written = 0;
901 struct inode *inode = fdentry(file)->d_inode;
902 struct btrfs_root *root = BTRFS_I(inode)->root;
903 struct page **pages = NULL;
905 struct page *pinned[2];
906 unsigned long first_index;
907 unsigned long last_index;
910 will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
911 (file->f_flags & O_DIRECT));
913 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
914 PAGE_CACHE_SIZE / (sizeof(struct page *)));
921 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
922 current->backing_dev_info = inode->i_mapping->backing_dev_info;
923 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
929 err = file_remove_suid(file);
932 file_update_time(file);
934 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
936 mutex_lock(&inode->i_mutex);
937 first_index = pos >> PAGE_CACHE_SHIFT;
938 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
941 * if this is a nodatasum mount, force summing off for the inode
942 * all the time. That way a later mount with summing on won't
945 if (btrfs_test_opt(root, NODATASUM))
946 btrfs_set_flag(inode, NODATASUM);
949 * there are lots of better ways to do this, but this code
950 * makes sure the first and last page in the file range are
951 * up to date and ready for cow
953 if ((pos & (PAGE_CACHE_SIZE - 1))) {
954 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
955 if (!PageUptodate(pinned[0])) {
956 ret = btrfs_readpage(NULL, pinned[0]);
958 wait_on_page_locked(pinned[0]);
960 unlock_page(pinned[0]);
963 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
964 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
965 if (!PageUptodate(pinned[1])) {
966 ret = btrfs_readpage(NULL, pinned[1]);
968 wait_on_page_locked(pinned[1]);
970 unlock_page(pinned[1]);
975 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
976 size_t write_bytes = min(count, nrptrs *
977 (size_t)PAGE_CACHE_SIZE -
979 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
982 WARN_ON(num_pages > nrptrs);
983 memset(pages, 0, sizeof(pages));
985 ret = btrfs_check_free_space(root, write_bytes, 0);
989 ret = prepare_pages(root, file, pages, num_pages,
990 pos, first_index, last_index,
995 ret = btrfs_copy_from_user(pos, num_pages,
996 write_bytes, pages, buf);
998 btrfs_drop_pages(pages, num_pages);
1002 ret = dirty_and_release_pages(NULL, root, file, pages,
1003 num_pages, pos, write_bytes);
1004 btrfs_drop_pages(pages, num_pages);
1009 btrfs_fdatawrite_range(inode->i_mapping, pos,
1010 pos + write_bytes - 1,
1013 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1016 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1017 btrfs_btree_balance_dirty(root, 1);
1018 btrfs_throttle(root);
1022 count -= write_bytes;
1024 num_written += write_bytes;
1029 mutex_unlock(&inode->i_mutex);
1034 page_cache_release(pinned[0]);
1036 page_cache_release(pinned[1]);
1039 if (num_written > 0 && will_write) {
1040 struct btrfs_trans_handle *trans;
1042 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1046 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
1047 trans = btrfs_start_transaction(root, 1);
1048 ret = btrfs_log_dentry_safe(trans, root,
1051 btrfs_sync_log(trans, root);
1052 btrfs_end_transaction(trans, root);
1054 btrfs_commit_transaction(trans, root);
1057 if (file->f_flags & O_DIRECT) {
1058 invalidate_mapping_pages(inode->i_mapping,
1059 start_pos >> PAGE_CACHE_SHIFT,
1060 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1063 current->backing_dev_info = NULL;
1064 return num_written ? num_written : err;
1067 int btrfs_release_file(struct inode * inode, struct file * filp)
1069 if (filp->private_data)
1070 btrfs_ioctl_trans_end(filp);
1075 * fsync call for both files and directories. This logs the inode into
1076 * the tree log instead of forcing full commits whenever possible.
1078 * It needs to call filemap_fdatawait so that all ordered extent updates are
1079 * in the metadata btree are up to date for copying to the log.
1081 * It drops the inode mutex before doing the tree log commit. This is an
1082 * important optimization for directories because holding the mutex prevents
1083 * new operations on the dir while we write to disk.
1085 int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
1087 struct inode *inode = dentry->d_inode;
1088 struct btrfs_root *root = BTRFS_I(inode)->root;
1090 struct btrfs_trans_handle *trans;
1093 * check the transaction that last modified this inode
1094 * and see if its already been committed
1096 if (!BTRFS_I(inode)->last_trans)
1099 mutex_lock(&root->fs_info->trans_mutex);
1100 if (BTRFS_I(inode)->last_trans <=
1101 root->fs_info->last_trans_committed) {
1102 BTRFS_I(inode)->last_trans = 0;
1103 mutex_unlock(&root->fs_info->trans_mutex);
1106 mutex_unlock(&root->fs_info->trans_mutex);
1108 root->fs_info->tree_log_batch++;
1109 filemap_fdatawait(inode->i_mapping);
1110 root->fs_info->tree_log_batch++;
1113 * ok we haven't committed the transaction yet, lets do a commit
1115 if (file->private_data)
1116 btrfs_ioctl_trans_end(file);
1118 trans = btrfs_start_transaction(root, 1);
1124 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
1129 /* we've logged all the items and now have a consistent
1130 * version of the file in the log. It is possible that
1131 * someone will come in and modify the file, but that's
1132 * fine because the log is consistent on disk, and we
1133 * have references to all of the file's extents
1135 * It is possible that someone will come in and log the
1136 * file again, but that will end up using the synchronization
1137 * inside btrfs_sync_log to keep things safe.
1139 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
1142 ret = btrfs_commit_transaction(trans, root);
1144 btrfs_sync_log(trans, root);
1145 ret = btrfs_end_transaction(trans, root);
1147 mutex_lock(&file->f_dentry->d_inode->i_mutex);
1149 return ret > 0 ? EIO : ret;
1152 static struct vm_operations_struct btrfs_file_vm_ops = {
1153 .fault = filemap_fault,
1154 .page_mkwrite = btrfs_page_mkwrite,
1157 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1159 vma->vm_ops = &btrfs_file_vm_ops;
1160 file_accessed(filp);
1164 struct file_operations btrfs_file_operations = {
1165 .llseek = generic_file_llseek,
1166 .read = do_sync_read,
1167 .aio_read = generic_file_aio_read,
1168 .splice_read = generic_file_splice_read,
1169 .write = btrfs_file_write,
1170 .mmap = btrfs_file_mmap,
1171 .open = generic_file_open,
1172 .release = btrfs_release_file,
1173 .fsync = btrfs_sync_file,
1174 .unlocked_ioctl = btrfs_ioctl,
1175 #ifdef CONFIG_COMPAT
1176 .compat_ioctl = btrfs_ioctl,