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.
19 #include <linux/buffer_head.h>
21 #include <linux/pagemap.h>
22 #include <linux/highmem.h>
23 #include <linux/time.h>
24 #include <linux/init.h>
25 #include <linux/string.h>
26 #include <linux/smp_lock.h>
27 #include <linux/backing-dev.h>
28 #include <linux/mpage.h>
29 #include <linux/swap.h>
30 #include <linux/writeback.h>
31 #include <linux/statfs.h>
32 #include <linux/compat.h>
33 #include <linux/version.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
39 #include "print-tree.h"
42 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
43 struct page **prepared_pages,
44 const char __user * buf)
48 int offset = pos & (PAGE_CACHE_SIZE - 1);
50 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
51 size_t count = min_t(size_t,
52 PAGE_CACHE_SIZE - offset, write_bytes);
53 struct page *page = prepared_pages[i];
54 fault_in_pages_readable(buf, count);
56 /* Copy data from userspace to the current page */
58 page_fault = __copy_from_user(page_address(page) + offset,
60 /* Flush processor's dcache for this page */
61 flush_dcache_page(page);
69 return page_fault ? -EFAULT : 0;
72 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
75 for (i = 0; i < num_pages; i++) {
78 unlock_page(pages[i]);
79 mark_page_accessed(pages[i]);
80 page_cache_release(pages[i]);
84 static int insert_inline_extent(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root, struct inode *inode,
86 u64 offset, ssize_t size,
87 struct page *page, size_t page_offset)
90 struct btrfs_path *path;
92 struct btrfs_file_extent_item *ei;
97 path = btrfs_alloc_path();
101 btrfs_set_trans_block_group(trans, inode);
103 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 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
117 path->slots[0], struct btrfs_file_extent_item);
118 btrfs_set_file_extent_generation(ei, trans->transid);
119 btrfs_set_file_extent_type(ei,
120 BTRFS_FILE_EXTENT_INLINE);
121 ptr = btrfs_file_extent_inline_start(ei);
123 kaddr = kmap_atomic(page, KM_USER0);
124 btrfs_memcpy(root, path->nodes[0]->b_data,
125 ptr, kaddr + page_offset, size);
126 kunmap_atomic(kaddr, KM_USER0);
127 btrfs_mark_buffer_dirty(path->nodes[0]);
129 btrfs_free_path(path);
133 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root,
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct extent_map *em;
145 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
146 struct btrfs_key ins;
150 u64 end_of_last_block;
151 u64 end_pos = pos + write_bytes;
152 loff_t isize = i_size_read(inode);
154 em = alloc_extent_map(GFP_NOFS);
158 em->bdev = inode->i_sb->s_bdev;
160 start_pos = pos & ~((u64)root->blocksize - 1);
161 num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
164 end_of_last_block = start_pos + (num_blocks << inode->i_blkbits) - 1;
165 mutex_lock(&root->fs_info->fs_mutex);
166 trans = btrfs_start_transaction(root, 1);
171 btrfs_set_trans_block_group(trans, inode);
172 inode->i_blocks += num_blocks << 3;
175 if ((end_of_last_block & 4095) == 0) {
176 printk("strange end of last %Lu %lu %Lu\n", start_pos, write_bytes, end_of_last_block);
178 set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
180 /* FIXME...EIEIO, ENOSPC and more */
182 /* step one, delete the existing extents in this range */
183 /* FIXME blocksize != pagesize */
184 if (start_pos < inode->i_size) {
185 err = btrfs_drop_extents(trans, root, inode,
186 start_pos, (pos + write_bytes + root->blocksize -1) &
187 ~((u64)root->blocksize - 1), &hint_block);
192 /* insert any holes we need to create */
193 if (inode->i_size < start_pos) {
194 u64 last_pos_in_file;
196 u64 mask = root->blocksize - 1;
197 last_pos_in_file = (isize + mask) & ~mask;
198 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
199 hole_size >>= inode->i_blkbits;
200 if (last_pos_in_file < start_pos) {
201 err = btrfs_insert_file_extent(trans, root,
211 * either allocate an extent for the new bytes or setup the key
212 * to show we are doing inline data in the extent
214 if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
215 pos + write_bytes - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
216 err = btrfs_alloc_extent(trans, root, inode->i_ino,
217 num_blocks, 0, hint_block, (u64)-1,
220 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
221 start_pos, ins.objectid, ins.offset,
224 em->start = start_pos;
225 em->end = end_of_last_block;
226 em->block_start = ins.objectid << inode->i_blkbits;
227 em->block_end = em->block_start +
228 (ins.offset << inode->i_blkbits) - 1;
229 set_extent_dirty(em_tree, start_pos, end_of_last_block,
231 err = add_extent_mapping(em_tree, em);
232 for (i = 0; i < num_pages; i++) {
233 struct page *p = pages[i];
235 __set_page_dirty_nobuffers(p);
238 struct page *p = pages[0];
239 err = insert_inline_extent(trans, root, inode, start_pos,
240 end_pos - start_pos, p, 0);
242 em->start = start_pos;
244 em->block_start = EXTENT_MAP_INLINE;
245 em->block_end = EXTENT_MAP_INLINE;
246 add_extent_mapping(em_tree, em);
248 if (end_pos > isize) {
249 i_size_write(inode, end_pos);
250 btrfs_update_inode(trans, root, inode);
253 err = btrfs_end_transaction(trans, root);
255 mutex_unlock(&root->fs_info->fs_mutex);
260 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
262 struct extent_map *em;
263 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
266 em = lookup_extent_mapping(em_tree, start, end);
269 remove_extent_mapping(em_tree, em);
272 /* once for the tree*/
279 * this is very complex, but the basic idea is to drop all extents
280 * in the range start - end. hint_block is filled in with a block number
281 * that would be a good hint to the block allocator for this file.
283 * If an extent intersects the range but is not entirely inside the range
284 * it is either truncated or split. Anything entirely inside the range
285 * is deleted from the tree.
287 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
288 struct btrfs_root *root, struct inode *inode,
289 u64 start, u64 end, u64 *hint_block)
292 struct btrfs_key key;
293 struct btrfs_leaf *leaf;
295 struct btrfs_file_extent_item *extent;
298 struct btrfs_file_extent_item old;
299 struct btrfs_path *path;
300 u64 search_start = start;
307 btrfs_drop_extent_cache(inode, start, end - 1);
309 path = btrfs_alloc_path();
314 btrfs_release_path(root, path);
315 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
320 if (path->slots[0] == 0) {
332 leaf = btrfs_buffer_leaf(path->nodes[0]);
333 slot = path->slots[0];
335 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
336 if (key.offset >= end || key.objectid != inode->i_ino) {
339 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
343 search_start = key.offset;
346 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
347 extent = btrfs_item_ptr(leaf, slot,
348 struct btrfs_file_extent_item);
349 found_type = btrfs_file_extent_type(extent);
350 if (found_type == BTRFS_FILE_EXTENT_REG) {
351 extent_end = key.offset +
352 (btrfs_file_extent_num_blocks(extent) <<
355 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
357 extent_end = key.offset +
358 btrfs_file_extent_inline_len(leaf->items +
362 extent_end = search_start;
365 /* we found nothing we can drop */
366 if ((!found_extent && !found_inline) ||
367 search_start >= extent_end) {
370 nritems = btrfs_header_nritems(
371 btrfs_buffer_header(path->nodes[0]));
372 if (slot >= nritems - 1) {
373 nextret = btrfs_next_leaf(root, path);
383 /* FIXME, there's only one inline extent allowed right now */
385 u64 mask = root->blocksize - 1;
386 search_start = (extent_end + mask) & ~mask;
388 search_start = extent_end;
390 if (end < extent_end && end >= key.offset) {
393 btrfs_file_extent_disk_blocknr(extent);
394 u64 disk_num_blocks =
395 btrfs_file_extent_disk_num_blocks(extent);
396 memcpy(&old, extent, sizeof(old));
397 if (disk_blocknr != 0) {
398 ret = btrfs_inc_extent_ref(trans, root,
399 disk_blocknr, disk_num_blocks);
403 WARN_ON(found_inline);
406 /* truncate existing extent */
407 if (start > key.offset) {
411 WARN_ON(start & (root->blocksize - 1));
413 new_num = (start - key.offset) >>
415 old_num = btrfs_file_extent_num_blocks(extent);
417 btrfs_file_extent_disk_blocknr(extent);
418 if (btrfs_file_extent_disk_blocknr(extent)) {
420 (old_num - new_num) << 3;
422 btrfs_set_file_extent_num_blocks(extent,
424 btrfs_mark_buffer_dirty(path->nodes[0]);
429 /* delete the entire extent */
431 u64 disk_blocknr = 0;
432 u64 disk_num_blocks = 0;
433 u64 extent_num_blocks = 0;
436 btrfs_file_extent_disk_blocknr(extent);
438 btrfs_file_extent_disk_num_blocks(extent);
440 btrfs_file_extent_num_blocks(extent);
442 btrfs_file_extent_disk_blocknr(extent);
444 ret = btrfs_del_item(trans, root, path);
445 /* TODO update progress marker and return */
447 btrfs_release_path(root, path);
449 if (found_extent && disk_blocknr != 0) {
450 inode->i_blocks -= extent_num_blocks << 3;
451 ret = btrfs_free_extent(trans, root,
457 if (!bookend && search_start >= end) {
464 /* create bookend, splitting the extent in two */
465 if (bookend && found_extent) {
466 struct btrfs_key ins;
467 ins.objectid = inode->i_ino;
470 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
471 btrfs_release_path(root, path);
472 ret = btrfs_insert_empty_item(trans, root, path, &ins,
476 btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0]));
477 printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu\n", ret , ins.objectid, ins.flags, ins.offset, start, end, key.offset, extent_end);
480 extent = btrfs_item_ptr(
481 btrfs_buffer_leaf(path->nodes[0]),
483 struct btrfs_file_extent_item);
484 btrfs_set_file_extent_disk_blocknr(extent,
485 btrfs_file_extent_disk_blocknr(&old));
486 btrfs_set_file_extent_disk_num_blocks(extent,
487 btrfs_file_extent_disk_num_blocks(&old));
489 btrfs_set_file_extent_offset(extent,
490 btrfs_file_extent_offset(&old) +
491 ((end - key.offset) >> inode->i_blkbits));
492 WARN_ON(btrfs_file_extent_num_blocks(&old) <
493 (extent_end - end) >> inode->i_blkbits);
494 btrfs_set_file_extent_num_blocks(extent,
495 (extent_end - end) >> inode->i_blkbits);
497 btrfs_set_file_extent_type(extent,
498 BTRFS_FILE_EXTENT_REG);
499 btrfs_set_file_extent_generation(extent,
500 btrfs_file_extent_generation(&old));
501 btrfs_mark_buffer_dirty(path->nodes[0]);
502 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
504 btrfs_file_extent_num_blocks(extent) << 3;
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;
534 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
535 num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
538 memset(pages, 0, num_pages * sizeof(struct page *));
540 for (i = 0; i < num_pages; i++) {
541 pages[i] = grab_cache_page(inode->i_mapping, index + i);
546 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
547 wait_on_page_writeback(pages[i]);
548 if (!PagePrivate(pages[i])) {
549 SetPagePrivate(pages[i]);
550 set_page_private(pages[i], 1);
551 page_cache_get(pages[i]);
557 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
558 size_t count, loff_t *ppos)
561 size_t num_written = 0;
564 struct inode *inode = file->f_path.dentry->d_inode;
565 struct btrfs_root *root = BTRFS_I(inode)->root;
566 struct page **pages = NULL;
568 struct page *pinned[2];
569 unsigned long first_index;
570 unsigned long last_index;
572 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
573 PAGE_CACHE_SIZE / (sizeof(struct page *)));
576 if (file->f_flags & O_DIRECT)
579 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
580 current->backing_dev_info = inode->i_mapping->backing_dev_info;
581 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
586 err = remove_suid(file->f_path.dentry);
589 file_update_time(file);
591 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
593 mutex_lock(&inode->i_mutex);
594 first_index = pos >> PAGE_CACHE_SHIFT;
595 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
598 * there are lots of better ways to do this, but this code
599 * makes sure the first and last page in the file range are
600 * up to date and ready for cow
602 if ((pos & (PAGE_CACHE_SIZE - 1))) {
603 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
604 if (!PageUptodate(pinned[0])) {
605 ret = btrfs_readpage(NULL, pinned[0]);
607 wait_on_page_locked(pinned[0]);
609 unlock_page(pinned[0]);
612 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
613 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
614 if (!PageUptodate(pinned[1])) {
615 ret = btrfs_readpage(NULL, pinned[1]);
617 wait_on_page_locked(pinned[1]);
619 unlock_page(pinned[1]);
624 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
625 size_t write_bytes = min(count, nrptrs *
626 (size_t)PAGE_CACHE_SIZE -
628 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
631 WARN_ON(num_pages > nrptrs);
632 memset(pages, 0, sizeof(pages));
633 ret = prepare_pages(root, file, pages, num_pages,
634 pos, first_index, last_index,
639 ret = btrfs_copy_from_user(pos, num_pages,
640 write_bytes, pages, buf);
642 btrfs_drop_pages(pages, num_pages);
646 ret = dirty_and_release_pages(NULL, root, file, pages,
647 num_pages, pos, write_bytes);
648 btrfs_drop_pages(pages, num_pages);
653 count -= write_bytes;
655 num_written += write_bytes;
657 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
658 btrfs_btree_balance_dirty(root);
661 mutex_unlock(&inode->i_mutex);
665 page_cache_release(pinned[0]);
667 page_cache_release(pinned[1]);
669 current->backing_dev_info = NULL;
670 return num_written ? num_written : err;
673 static int btrfs_sync_file(struct file *file,
674 struct dentry *dentry, int datasync)
676 struct inode *inode = dentry->d_inode;
677 struct btrfs_root *root = BTRFS_I(inode)->root;
679 struct btrfs_trans_handle *trans;
682 * check the transaction that last modified this inode
683 * and see if its already been committed
685 mutex_lock(&root->fs_info->fs_mutex);
686 if (!BTRFS_I(inode)->last_trans)
688 mutex_lock(&root->fs_info->trans_mutex);
689 if (BTRFS_I(inode)->last_trans <=
690 root->fs_info->last_trans_committed) {
691 BTRFS_I(inode)->last_trans = 0;
692 mutex_unlock(&root->fs_info->trans_mutex);
695 mutex_unlock(&root->fs_info->trans_mutex);
698 * ok we haven't committed the transaction yet, lets do a commit
700 trans = btrfs_start_transaction(root, 1);
705 ret = btrfs_commit_transaction(trans, root);
707 mutex_unlock(&root->fs_info->fs_mutex);
708 return ret > 0 ? EIO : ret;
711 static struct vm_operations_struct btrfs_file_vm_ops = {
712 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
713 .nopage = filemap_nopage,
714 .populate = filemap_populate,
716 .fault = filemap_fault,
718 .page_mkwrite = btrfs_page_mkwrite,
721 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
723 vma->vm_ops = &btrfs_file_vm_ops;
728 struct file_operations btrfs_file_operations = {
729 .llseek = generic_file_llseek,
730 .read = do_sync_read,
731 .aio_read = generic_file_aio_read,
732 .write = btrfs_file_write,
733 .mmap = btrfs_file_mmap,
734 .open = generic_file_open,
735 .ioctl = btrfs_ioctl,
736 .fsync = btrfs_sync_file,
738 .compat_ioctl = btrfs_compat_ioctl,