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/module.h>
20 #include <linux/buffer_head.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.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_root *root, struct inode *inode,
85 u64 offset, ssize_t size,
86 struct buffer_head *bh)
89 struct btrfs_path *path;
91 struct btrfs_trans_handle *trans;
92 struct btrfs_file_extent_item *ei;
97 path = btrfs_alloc_path();
101 mutex_lock(&root->fs_info->fs_mutex);
102 trans = btrfs_start_transaction(root, 1);
103 btrfs_set_trans_block_group(trans, inode);
105 key.objectid = inode->i_ino;
108 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
109 BUG_ON(size >= PAGE_CACHE_SIZE);
110 datasize = btrfs_file_extent_calc_inline_size(size);
112 ret = btrfs_insert_empty_item(trans, root, path, &key,
118 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
119 path->slots[0], struct btrfs_file_extent_item);
120 btrfs_set_file_extent_generation(ei, trans->transid);
121 btrfs_set_file_extent_type(ei,
122 BTRFS_FILE_EXTENT_INLINE);
123 ptr = btrfs_file_extent_inline_start(ei);
125 kaddr = kmap_atomic(bh->b_page, KM_USER0);
126 btrfs_memcpy(root, path->nodes[0]->b_data,
127 ptr, kaddr + bh_offset(bh),
129 kunmap_atomic(kaddr, KM_USER0);
130 mark_buffer_dirty(path->nodes[0]);
132 btrfs_free_path(path);
133 ret = btrfs_end_transaction(trans, root);
136 mutex_unlock(&root->fs_info->fs_mutex);
140 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
141 struct btrfs_root *root,
153 struct inode *inode = file->f_path.dentry->d_inode;
154 struct buffer_head *bh;
156 for (i = 0; i < num_pages; i++) {
157 offset = pos & (PAGE_CACHE_SIZE -1);
158 this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes);
160 /* FIXME, one block at a time */
161 bh = page_buffers(pages[i]);
163 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
164 ret = insert_inline_extent(root, inode,
165 pages[i]->index << PAGE_CACHE_SHIFT,
166 offset + this_write, bh);
173 ret = btrfs_commit_write(file, pages[i], offset,
174 offset + this_write);
180 WARN_ON(this_write > write_bytes);
181 write_bytes -= this_write;
188 * this is very complex, but the basic idea is to drop all extents
189 * in the range start - end. hint_block is filled in with a block number
190 * that would be a good hint to the block allocator for this file.
192 * If an extent intersects the range but is not entirely inside the range
193 * it is either truncated or split. Anything entirely inside the range
194 * is deleted from the tree.
196 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
197 struct btrfs_root *root, struct inode *inode,
198 u64 start, u64 end, u64 *hint_block)
201 struct btrfs_key key;
202 struct btrfs_leaf *leaf;
204 struct btrfs_file_extent_item *extent;
207 struct btrfs_file_extent_item old;
208 struct btrfs_path *path;
209 u64 search_start = start;
215 path = btrfs_alloc_path();
219 btrfs_release_path(root, path);
220 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
225 if (path->slots[0] == 0) {
237 leaf = btrfs_buffer_leaf(path->nodes[0]);
238 slot = path->slots[0];
240 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
241 if (key.offset >= end || key.objectid != inode->i_ino) {
244 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
247 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
248 extent = btrfs_item_ptr(leaf, slot,
249 struct btrfs_file_extent_item);
250 found_type = btrfs_file_extent_type(extent);
251 if (found_type == BTRFS_FILE_EXTENT_REG) {
252 extent_end = key.offset +
253 (btrfs_file_extent_num_blocks(extent) <<
256 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
258 extent_end = key.offset +
259 btrfs_file_extent_inline_len(leaf->items +
263 extent_end = search_start;
266 /* we found nothing we can drop */
267 if ((!found_extent && !found_inline) ||
268 search_start >= extent_end) {
271 nritems = btrfs_header_nritems(
272 btrfs_buffer_header(path->nodes[0]));
273 if (slot >= nritems - 1) {
274 nextret = btrfs_next_leaf(root, path);
283 /* FIXME, there's only one inline extent allowed right now */
285 u64 mask = root->blocksize - 1;
286 search_start = (extent_end + mask) & ~mask;
288 search_start = extent_end;
290 if (end < extent_end && end >= key.offset) {
293 btrfs_file_extent_disk_blocknr(extent);
294 u64 disk_num_blocks =
295 btrfs_file_extent_disk_num_blocks(extent);
296 memcpy(&old, extent, sizeof(old));
297 if (disk_blocknr != 0) {
298 ret = btrfs_inc_extent_ref(trans, root,
299 disk_blocknr, disk_num_blocks);
303 WARN_ON(found_inline);
306 /* truncate existing extent */
307 if (start > key.offset) {
311 WARN_ON(start & (root->blocksize - 1));
313 new_num = (start - key.offset) >>
315 old_num = btrfs_file_extent_num_blocks(extent);
317 btrfs_file_extent_disk_blocknr(extent);
318 if (btrfs_file_extent_disk_blocknr(extent)) {
320 (old_num - new_num) << 3;
322 btrfs_set_file_extent_num_blocks(extent,
324 mark_buffer_dirty(path->nodes[0]);
329 /* delete the entire extent */
331 u64 disk_blocknr = 0;
332 u64 disk_num_blocks = 0;
333 u64 extent_num_blocks = 0;
336 btrfs_file_extent_disk_blocknr(extent);
338 btrfs_file_extent_disk_num_blocks(extent);
340 btrfs_file_extent_num_blocks(extent);
342 btrfs_file_extent_disk_blocknr(extent);
344 ret = btrfs_del_item(trans, root, path);
345 /* TODO update progress marker and return */
347 btrfs_release_path(root, path);
349 if (found_extent && disk_blocknr != 0) {
350 inode->i_blocks -= extent_num_blocks << 3;
351 ret = btrfs_free_extent(trans, root,
357 if (!bookend && search_start >= end) {
364 /* create bookend, splitting the extent in two */
365 if (bookend && found_extent) {
366 struct btrfs_key ins;
367 ins.objectid = inode->i_ino;
370 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
371 btrfs_release_path(root, path);
372 ret = btrfs_insert_empty_item(trans, root, path, &ins,
376 btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0]));
377 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);
380 extent = btrfs_item_ptr(
381 btrfs_buffer_leaf(path->nodes[0]),
383 struct btrfs_file_extent_item);
384 btrfs_set_file_extent_disk_blocknr(extent,
385 btrfs_file_extent_disk_blocknr(&old));
386 btrfs_set_file_extent_disk_num_blocks(extent,
387 btrfs_file_extent_disk_num_blocks(&old));
389 btrfs_set_file_extent_offset(extent,
390 btrfs_file_extent_offset(&old) +
391 ((end - key.offset) >> inode->i_blkbits));
392 WARN_ON(btrfs_file_extent_num_blocks(&old) <
393 (extent_end - end) >> inode->i_blkbits);
394 btrfs_set_file_extent_num_blocks(extent,
395 (extent_end - end) >> inode->i_blkbits);
397 btrfs_set_file_extent_type(extent,
398 BTRFS_FILE_EXTENT_REG);
399 btrfs_set_file_extent_generation(extent,
400 btrfs_file_extent_generation(&old));
401 btrfs_mark_buffer_dirty(path->nodes[0]);
402 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
404 btrfs_file_extent_num_blocks(extent) << 3;
411 btrfs_free_path(path);
416 * this gets pages into the page cache and locks them down
418 static int prepare_pages(struct btrfs_root *root,
423 unsigned long first_index,
424 unsigned long last_index,
428 unsigned long index = pos >> PAGE_CACHE_SHIFT;
429 struct inode *inode = file->f_path.dentry->d_inode;
433 struct buffer_head *bh;
434 struct buffer_head *head;
435 loff_t isize = i_size_read(inode);
436 struct btrfs_trans_handle *trans;
439 u64 alloc_extent_start;
441 struct btrfs_key ins;
443 start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1);
444 num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
447 memset(pages, 0, num_pages * sizeof(struct page *));
449 for (i = 0; i < num_pages; i++) {
450 pages[i] = grab_cache_page(inode->i_mapping, index + i);
457 mutex_lock(&root->fs_info->fs_mutex);
458 trans = btrfs_start_transaction(root, 1);
461 mutex_unlock(&root->fs_info->fs_mutex);
464 btrfs_set_trans_block_group(trans, inode);
465 /* FIXME blocksize != 4096 */
466 inode->i_blocks += num_blocks << 3;
469 /* FIXME...EIEIO, ENOSPC and more */
471 /* step one, delete the existing extents in this range */
472 /* FIXME blocksize != pagesize */
473 if (start_pos < inode->i_size) {
474 err = btrfs_drop_extents(trans, root, inode,
475 start_pos, (pos + write_bytes + root->blocksize -1) &
476 ~((u64)root->blocksize - 1), &hint_block);
481 /* insert any holes we need to create */
482 if (inode->i_size < start_pos) {
483 u64 last_pos_in_file;
485 u64 mask = root->blocksize - 1;
486 last_pos_in_file = (isize + mask) & ~mask;
487 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
488 hole_size >>= inode->i_blkbits;
489 if (last_pos_in_file < start_pos) {
490 err = btrfs_insert_file_extent(trans, root,
500 * either allocate an extent for the new bytes or setup the key
501 * to show we are doing inline data in the extent
503 if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
504 pos + write_bytes - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
505 err = btrfs_alloc_extent(trans, root, inode->i_ino,
506 num_blocks, hint_block, (u64)-1,
509 goto failed_truncate;
510 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
511 start_pos, ins.objectid, ins.offset,
514 goto failed_truncate;
520 alloc_extent_start = ins.objectid;
521 err = btrfs_end_transaction(trans, root);
522 mutex_unlock(&root->fs_info->fs_mutex);
524 for (i = 0; i < num_pages; i++) {
525 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
526 wait_on_page_writeback(pages[i]);
527 offset = pos & (PAGE_CACHE_SIZE -1);
528 this_write = min((size_t)PAGE_CACHE_SIZE - offset, write_bytes);
529 if (!page_has_buffers(pages[i])) {
530 create_empty_buffers(pages[i],
531 root->fs_info->sb->s_blocksize,
534 head = page_buffers(pages[i]);
537 err = btrfs_map_bh_to_logical(root, bh,
541 goto failed_truncate;
542 bh = bh->b_this_page;
543 if (alloc_extent_start)
544 alloc_extent_start++;
545 } while (bh != head);
547 WARN_ON(this_write > write_bytes);
548 write_bytes -= this_write;
553 btrfs_drop_pages(pages, num_pages);
557 btrfs_drop_pages(pages, num_pages);
559 vmtruncate(inode, isize);
563 mutex_unlock(&root->fs_info->fs_mutex);
568 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
569 size_t count, loff_t *ppos)
572 size_t num_written = 0;
575 struct inode *inode = file->f_path.dentry->d_inode;
576 struct btrfs_root *root = BTRFS_I(inode)->root;
577 struct page **pages = NULL;
579 struct page *pinned[2];
580 unsigned long first_index;
581 unsigned long last_index;
583 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
584 PAGE_CACHE_SIZE / (sizeof(struct page *)));
587 if (file->f_flags & O_DIRECT)
590 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
591 current->backing_dev_info = inode->i_mapping->backing_dev_info;
592 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
597 err = remove_suid(file->f_path.dentry);
600 file_update_time(file);
602 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
604 mutex_lock(&inode->i_mutex);
605 first_index = pos >> PAGE_CACHE_SHIFT;
606 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
609 * there are lots of better ways to do this, but this code
610 * makes sure the first and last page in the file range are
611 * up to date and ready for cow
613 if ((pos & (PAGE_CACHE_SIZE - 1))) {
614 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
615 if (!PageUptodate(pinned[0])) {
616 ret = btrfs_readpage(NULL, pinned[0]);
618 wait_on_page_locked(pinned[0]);
620 unlock_page(pinned[0]);
623 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
624 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
625 if (!PageUptodate(pinned[1])) {
626 ret = btrfs_readpage(NULL, pinned[1]);
628 wait_on_page_locked(pinned[1]);
630 unlock_page(pinned[1]);
635 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
636 size_t write_bytes = min(count, nrptrs *
637 (size_t)PAGE_CACHE_SIZE -
639 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
642 WARN_ON(num_pages > nrptrs);
643 memset(pages, 0, sizeof(pages));
644 ret = prepare_pages(root, file, pages, num_pages,
645 pos, first_index, last_index,
650 ret = btrfs_copy_from_user(pos, num_pages,
651 write_bytes, pages, buf);
653 btrfs_drop_pages(pages, num_pages);
657 ret = dirty_and_release_pages(NULL, root, file, pages,
658 num_pages, pos, write_bytes);
659 btrfs_drop_pages(pages, num_pages);
664 count -= write_bytes;
666 num_written += write_bytes;
668 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
669 btrfs_btree_balance_dirty(root);
672 mutex_unlock(&inode->i_mutex);
676 page_cache_release(pinned[0]);
678 page_cache_release(pinned[1]);
680 current->backing_dev_info = NULL;
681 mark_inode_dirty(inode);
682 return num_written ? num_written : err;
685 static int btrfs_sync_file(struct file *file,
686 struct dentry *dentry, int datasync)
688 struct inode *inode = dentry->d_inode;
689 struct btrfs_root *root = BTRFS_I(inode)->root;
691 struct btrfs_trans_handle *trans;
694 * FIXME, use inode generation number to check if we can skip the
697 mutex_lock(&root->fs_info->fs_mutex);
698 trans = btrfs_start_transaction(root, 1);
703 ret = btrfs_commit_transaction(trans, root);
704 mutex_unlock(&root->fs_info->fs_mutex);
706 return ret > 0 ? EIO : ret;
709 static struct vm_operations_struct btrfs_file_vm_ops = {
710 .nopage = filemap_nopage,
711 .populate = filemap_populate,
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 .ioctl = btrfs_ioctl,
730 .fsync = btrfs_sync_file,
732 .compat_ioctl = btrfs_compat_ioctl,