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"
41 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
42 struct page **prepared_pages,
43 const char __user * buf)
47 int offset = pos & (PAGE_CACHE_SIZE - 1);
49 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
50 size_t count = min_t(size_t,
51 PAGE_CACHE_SIZE - offset, write_bytes);
52 struct page *page = prepared_pages[i];
53 fault_in_pages_readable(buf, count);
55 /* Copy data from userspace to the current page */
57 page_fault = __copy_from_user(page_address(page) + offset,
59 /* Flush processor's dcache for this page */
60 flush_dcache_page(page);
68 return page_fault ? -EFAULT : 0;
71 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
74 for (i = 0; i < num_pages; i++) {
77 unlock_page(pages[i]);
78 mark_page_accessed(pages[i]);
79 page_cache_release(pages[i]);
83 static int insert_inline_extent(struct btrfs_trans_handle *trans,
84 struct btrfs_root *root, struct inode *inode,
85 u64 offset, ssize_t size,
86 struct page *page, size_t page_offset)
89 struct btrfs_path *path;
90 struct extent_buffer *leaf;
93 struct btrfs_file_extent_item *ei;
98 path = btrfs_alloc_path();
102 btrfs_set_trans_block_group(trans, inode);
104 key.objectid = inode->i_ino;
106 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
107 BUG_ON(size >= PAGE_CACHE_SIZE);
108 datasize = btrfs_file_extent_calc_inline_size(size);
110 ret = btrfs_insert_empty_item(trans, root, path, &key,
116 leaf = path->nodes[0];
117 ei = btrfs_item_ptr(leaf, path->slots[0],
118 struct btrfs_file_extent_item);
119 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
120 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
121 ptr = btrfs_file_extent_inline_start(ei);
123 kaddr = kmap_atomic(page, KM_USER1);
124 write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
125 kunmap_atomic(kaddr, KM_USER1);
126 btrfs_mark_buffer_dirty(leaf);
128 btrfs_free_path(path);
132 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
133 struct btrfs_root *root,
142 struct inode *inode = file->f_path.dentry->d_inode;
143 struct extent_map *em;
144 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
148 u64 end_of_last_block;
149 u64 end_pos = pos + write_bytes;
151 loff_t isize = i_size_read(inode);
153 em = alloc_extent_map(GFP_NOFS);
157 em->bdev = inode->i_sb->s_bdev;
159 start_pos = pos & ~((u64)root->sectorsize - 1);
160 num_bytes = (write_bytes + pos - start_pos +
161 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
163 down_read(&BTRFS_I(inode)->root->snap_sem);
164 end_of_last_block = start_pos + num_bytes - 1;
166 lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
167 mutex_lock(&root->fs_info->fs_mutex);
168 trans = btrfs_start_transaction(root, 1);
173 btrfs_set_trans_block_group(trans, inode);
174 inode->i_blocks += num_bytes >> 9;
177 if ((end_of_last_block & 4095) == 0) {
178 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
180 set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
182 /* FIXME...EIEIO, ENOSPC and more */
184 /* insert any holes we need to create */
185 if (inode->i_size < start_pos) {
186 u64 last_pos_in_file;
188 u64 mask = root->sectorsize - 1;
189 last_pos_in_file = (isize + mask) & ~mask;
190 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
192 if (last_pos_in_file < start_pos) {
193 err = btrfs_drop_extents(trans, root, inode,
195 last_pos_in_file + hole_size,
200 err = btrfs_insert_file_extent(trans, root,
210 * either allocate an extent for the new bytes or setup the key
211 * to show we are doing inline data in the extent
213 inline_size = end_pos - start_pos;
214 if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
215 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
216 inline_size >= PAGE_CACHE_SIZE) {
218 for (i = 0; i < num_pages; i++) {
219 struct page *p = pages[i];
223 last_end = pages[num_pages -1]->index << PAGE_CACHE_SHIFT;
224 last_end += PAGE_CACHE_SIZE - 1;
225 set_extent_delalloc(em_tree, start_pos, end_of_last_block,
228 struct page *p = pages[0];
229 /* step one, delete the existing extents in this range */
230 err = btrfs_drop_extents(trans, root, inode, start_pos,
231 (pos + write_bytes + root->sectorsize -1) &
232 ~((u64)root->sectorsize - 1), &hint_byte);
236 err = insert_inline_extent(trans, root, inode, start_pos,
237 end_pos - start_pos, p, 0);
239 em->start = start_pos;
240 em->end = end_pos - 1;
241 em->block_start = EXTENT_MAP_INLINE;
242 em->block_end = EXTENT_MAP_INLINE;
243 add_extent_mapping(em_tree, em);
245 if (end_pos > isize) {
246 i_size_write(inode, end_pos);
247 btrfs_update_inode(trans, root, inode);
250 err = btrfs_end_transaction(trans, root);
252 mutex_unlock(&root->fs_info->fs_mutex);
253 unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
255 up_read(&BTRFS_I(inode)->root->snap_sem);
259 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
261 struct extent_map *em;
262 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
265 em = lookup_extent_mapping(em_tree, start, end);
268 remove_extent_mapping(em_tree, em);
271 /* once for the tree*/
278 * this is very complex, but the basic idea is to drop all extents
279 * in the range start - end. hint_block is filled in with a block number
280 * that would be a good hint to the block allocator for this file.
282 * If an extent intersects the range but is not entirely inside the range
283 * it is either truncated or split. Anything entirely inside the range
284 * is deleted from the tree.
286 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
287 struct btrfs_root *root, struct inode *inode,
288 u64 start, u64 end, u64 *hint_byte)
291 struct btrfs_key key;
292 struct extent_buffer *leaf;
294 struct btrfs_file_extent_item *extent;
297 struct btrfs_file_extent_item old;
298 struct btrfs_path *path;
299 u64 search_start = start;
306 btrfs_drop_extent_cache(inode, start, end - 1);
308 path = btrfs_alloc_path();
313 btrfs_release_path(root, path);
314 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
319 if (path->slots[0] == 0) {
331 leaf = path->nodes[0];
332 slot = path->slots[0];
334 btrfs_item_key_to_cpu(leaf, &key, slot);
335 if (key.offset >= end || key.objectid != inode->i_ino) {
338 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
342 search_start = key.offset;
345 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
346 extent = btrfs_item_ptr(leaf, slot,
347 struct btrfs_file_extent_item);
348 found_type = btrfs_file_extent_type(leaf, extent);
349 if (found_type == BTRFS_FILE_EXTENT_REG) {
350 extent_end = key.offset +
351 btrfs_file_extent_num_bytes(leaf, extent);
353 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
354 struct btrfs_item *item;
355 item = btrfs_item_nr(leaf, slot);
357 extent_end = key.offset +
358 btrfs_file_extent_inline_len(leaf, item);
361 extent_end = search_start;
364 /* we found nothing we can drop */
365 if ((!found_extent && !found_inline) ||
366 search_start >= extent_end) {
369 nritems = btrfs_header_nritems(leaf);
370 if (slot >= nritems - 1) {
371 nextret = btrfs_next_leaf(root, path);
381 /* FIXME, there's only one inline extent allowed right now */
383 u64 mask = root->sectorsize - 1;
384 search_start = (extent_end + mask) & ~mask;
386 search_start = extent_end;
388 if (end < extent_end && end >= key.offset) {
391 btrfs_file_extent_disk_bytenr(leaf, extent);
393 btrfs_file_extent_disk_num_bytes(leaf,
395 read_extent_buffer(leaf, &old,
396 (unsigned long)extent,
398 if (disk_bytenr != 0) {
399 ret = btrfs_inc_extent_ref(trans, root,
400 disk_bytenr, disk_num_bytes);
404 WARN_ON(found_inline);
407 /* truncate existing extent */
408 if (start > key.offset) {
412 WARN_ON(start & (root->sectorsize - 1));
414 new_num = start - key.offset;
415 old_num = btrfs_file_extent_num_bytes(leaf,
418 btrfs_file_extent_disk_bytenr(leaf,
420 if (btrfs_file_extent_disk_bytenr(leaf,
423 (old_num - new_num) >> 9;
425 btrfs_set_file_extent_num_bytes(leaf, extent,
427 btrfs_mark_buffer_dirty(leaf);
432 /* delete the entire extent */
435 u64 disk_num_bytes = 0;
436 u64 extent_num_bytes = 0;
439 btrfs_file_extent_disk_bytenr(leaf,
442 btrfs_file_extent_disk_num_bytes(leaf,
445 btrfs_file_extent_num_bytes(leaf, extent);
447 btrfs_file_extent_disk_bytenr(leaf,
450 ret = btrfs_del_item(trans, root, path);
451 /* TODO update progress marker and return */
453 btrfs_release_path(root, path);
455 if (found_extent && disk_bytenr != 0) {
456 inode->i_blocks -= extent_num_bytes >> 9;
457 ret = btrfs_free_extent(trans, root,
463 if (!bookend && search_start >= end) {
470 /* create bookend, splitting the extent in two */
471 if (bookend && found_extent) {
472 struct btrfs_key ins;
473 ins.objectid = inode->i_ino;
475 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
476 btrfs_release_path(root, path);
477 ret = btrfs_insert_empty_item(trans, root, path, &ins,
480 leaf = path->nodes[0];
482 btrfs_print_leaf(root, leaf);
483 printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
486 extent = btrfs_item_ptr(leaf, path->slots[0],
487 struct btrfs_file_extent_item);
488 write_extent_buffer(leaf, &old,
489 (unsigned long)extent, sizeof(old));
491 btrfs_set_file_extent_offset(leaf, extent,
492 le64_to_cpu(old.offset) + end - key.offset);
493 WARN_ON(le64_to_cpu(old.num_bytes) <
495 btrfs_set_file_extent_num_bytes(leaf, extent,
497 btrfs_set_file_extent_type(leaf, extent,
498 BTRFS_FILE_EXTENT_REG);
500 btrfs_mark_buffer_dirty(path->nodes[0]);
501 if (le64_to_cpu(old.disk_bytenr) != 0) {
503 btrfs_file_extent_num_bytes(leaf,
511 btrfs_free_path(path);
516 * this gets pages into the page cache and locks them down
518 static int prepare_pages(struct btrfs_root *root,
523 unsigned long first_index,
524 unsigned long last_index,
528 unsigned long index = pos >> PAGE_CACHE_SHIFT;
529 struct inode *inode = file->f_path.dentry->d_inode;
533 start_pos = pos & ~((u64)root->sectorsize - 1);
535 memset(pages, 0, num_pages * sizeof(struct page *));
537 for (i = 0; i < num_pages; i++) {
538 pages[i] = grab_cache_page(inode->i_mapping, index + i);
543 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
544 wait_on_page_writeback(pages[i]);
545 set_page_extent_mapped(pages[i]);
546 WARN_ON(!PageLocked(pages[i]));
551 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
552 size_t count, loff_t *ppos)
555 size_t num_written = 0;
558 struct inode *inode = file->f_path.dentry->d_inode;
559 struct btrfs_root *root = BTRFS_I(inode)->root;
560 struct page **pages = NULL;
562 struct page *pinned[2];
563 unsigned long first_index;
564 unsigned long last_index;
566 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
567 PAGE_CACHE_SIZE / (sizeof(struct page *)));
570 if (file->f_flags & O_DIRECT)
573 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
574 current->backing_dev_info = inode->i_mapping->backing_dev_info;
575 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
580 err = remove_suid(file->f_path.dentry);
583 file_update_time(file);
585 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
587 mutex_lock(&inode->i_mutex);
588 first_index = pos >> PAGE_CACHE_SHIFT;
589 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
592 * there are lots of better ways to do this, but this code
593 * makes sure the first and last page in the file range are
594 * up to date and ready for cow
596 if ((pos & (PAGE_CACHE_SIZE - 1))) {
597 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
598 if (!PageUptodate(pinned[0])) {
599 ret = btrfs_readpage(NULL, pinned[0]);
601 wait_on_page_locked(pinned[0]);
603 unlock_page(pinned[0]);
606 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
607 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
608 if (!PageUptodate(pinned[1])) {
609 ret = btrfs_readpage(NULL, pinned[1]);
611 wait_on_page_locked(pinned[1]);
613 unlock_page(pinned[1]);
618 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
619 size_t write_bytes = min(count, nrptrs *
620 (size_t)PAGE_CACHE_SIZE -
622 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
625 WARN_ON(num_pages > nrptrs);
626 memset(pages, 0, sizeof(pages));
627 ret = prepare_pages(root, file, pages, num_pages,
628 pos, first_index, last_index,
633 ret = btrfs_copy_from_user(pos, num_pages,
634 write_bytes, pages, buf);
636 btrfs_drop_pages(pages, num_pages);
640 ret = dirty_and_release_pages(NULL, root, file, pages,
641 num_pages, pos, write_bytes);
642 btrfs_drop_pages(pages, num_pages);
647 count -= write_bytes;
649 num_written += write_bytes;
651 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
652 btrfs_btree_balance_dirty(root, 1);
655 mutex_unlock(&inode->i_mutex);
659 page_cache_release(pinned[0]);
661 page_cache_release(pinned[1]);
663 current->backing_dev_info = NULL;
664 return num_written ? num_written : err;
667 static int btrfs_sync_file(struct file *file,
668 struct dentry *dentry, int datasync)
670 struct inode *inode = dentry->d_inode;
671 struct btrfs_root *root = BTRFS_I(inode)->root;
673 struct btrfs_trans_handle *trans;
676 * check the transaction that last modified this inode
677 * and see if its already been committed
679 mutex_lock(&root->fs_info->fs_mutex);
680 if (!BTRFS_I(inode)->last_trans)
682 mutex_lock(&root->fs_info->trans_mutex);
683 if (BTRFS_I(inode)->last_trans <=
684 root->fs_info->last_trans_committed) {
685 BTRFS_I(inode)->last_trans = 0;
686 mutex_unlock(&root->fs_info->trans_mutex);
689 mutex_unlock(&root->fs_info->trans_mutex);
692 * ok we haven't committed the transaction yet, lets do a commit
694 trans = btrfs_start_transaction(root, 1);
699 ret = btrfs_commit_transaction(trans, root);
701 mutex_unlock(&root->fs_info->fs_mutex);
702 return ret > 0 ? EIO : ret;
705 static struct vm_operations_struct btrfs_file_vm_ops = {
706 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
707 .nopage = filemap_nopage,
708 .populate = filemap_populate,
710 .fault = filemap_fault,
712 .page_mkwrite = btrfs_page_mkwrite,
715 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
717 vma->vm_ops = &btrfs_file_vm_ops;
722 struct file_operations btrfs_file_operations = {
723 .llseek = generic_file_llseek,
724 .read = do_sync_read,
725 .aio_read = generic_file_aio_read,
726 .write = btrfs_file_write,
727 .mmap = btrfs_file_mmap,
728 .open = generic_file_open,
729 .fsync = btrfs_sync_file,
730 .unlocked_ioctl = btrfs_ioctl,
732 .compat_ioctl = btrfs_ioctl,