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/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
47 #include "ordered-data.h"
50 #include "compression.h"
53 struct btrfs_iget_args {
55 struct btrfs_root *root;
58 static const struct inode_operations btrfs_dir_inode_operations;
59 static const struct inode_operations btrfs_symlink_inode_operations;
60 static const struct inode_operations btrfs_dir_ro_inode_operations;
61 static const struct inode_operations btrfs_special_inode_operations;
62 static const struct inode_operations btrfs_file_inode_operations;
63 static const struct address_space_operations btrfs_aops;
64 static const struct address_space_operations btrfs_symlink_aops;
65 static const struct file_operations btrfs_dir_file_operations;
66 static struct extent_io_ops btrfs_extent_io_ops;
68 static struct kmem_cache *btrfs_inode_cachep;
69 struct kmem_cache *btrfs_trans_handle_cachep;
70 struct kmem_cache *btrfs_transaction_cachep;
71 struct kmem_cache *btrfs_path_cachep;
74 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
84 static void btrfs_truncate(struct inode *inode);
85 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
86 static noinline int cow_file_range(struct inode *inode,
87 struct page *locked_page,
88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock);
91 static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
95 err = btrfs_init_acl(inode, dir);
97 err = btrfs_xattr_security_init(inode, dir);
102 * this does all the hard work for inserting an inline extent into
103 * the btree. The caller should have done a btrfs_drop_extents so that
104 * no overlapping inline items exist in the btree
106 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root, struct inode *inode,
108 u64 start, size_t size, size_t compressed_size,
109 struct page **compressed_pages)
111 struct btrfs_key key;
112 struct btrfs_path *path;
113 struct extent_buffer *leaf;
114 struct page *page = NULL;
117 struct btrfs_file_extent_item *ei;
120 size_t cur_size = size;
122 unsigned long offset;
123 int use_compress = 0;
125 if (compressed_size && compressed_pages) {
127 cur_size = compressed_size;
130 path = btrfs_alloc_path();
134 path->leave_spinning = 1;
135 btrfs_set_trans_block_group(trans, inode);
137 key.objectid = inode->i_ino;
139 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
140 datasize = btrfs_file_extent_calc_inline_size(cur_size);
142 inode_add_bytes(inode, size);
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
150 leaf = path->nodes[0];
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
153 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
154 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
155 btrfs_set_file_extent_encryption(leaf, ei, 0);
156 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
157 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
158 ptr = btrfs_file_extent_inline_start(ei);
163 while (compressed_size > 0) {
164 cpage = compressed_pages[i];
165 cur_size = min_t(unsigned long, compressed_size,
168 kaddr = kmap_atomic(cpage, KM_USER0);
169 write_extent_buffer(leaf, kaddr, ptr, cur_size);
170 kunmap_atomic(kaddr, KM_USER0);
174 compressed_size -= cur_size;
176 btrfs_set_file_extent_compression(leaf, ei,
177 BTRFS_COMPRESS_ZLIB);
179 page = find_get_page(inode->i_mapping,
180 start >> PAGE_CACHE_SHIFT);
181 btrfs_set_file_extent_compression(leaf, ei, 0);
182 kaddr = kmap_atomic(page, KM_USER0);
183 offset = start & (PAGE_CACHE_SIZE - 1);
184 write_extent_buffer(leaf, kaddr + offset, ptr, size);
185 kunmap_atomic(kaddr, KM_USER0);
186 page_cache_release(page);
188 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path);
192 * we're an inline extent, so nobody can
193 * extend the file past i_size without locking
194 * a page we already have locked.
196 * We must do any isize and inode updates
197 * before we unlock the pages. Otherwise we
198 * could end up racing with unlink.
200 BTRFS_I(inode)->disk_i_size = inode->i_size;
201 btrfs_update_inode(trans, root, inode);
205 btrfs_free_path(path);
211 * conditionally insert an inline extent into the file. This
212 * does the checks required to make sure the data is small enough
213 * to fit as an inline extent.
215 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
216 struct btrfs_root *root,
217 struct inode *inode, u64 start, u64 end,
218 size_t compressed_size,
219 struct page **compressed_pages)
221 u64 isize = i_size_read(inode);
222 u64 actual_end = min(end + 1, isize);
223 u64 inline_len = actual_end - start;
224 u64 aligned_end = (end + root->sectorsize - 1) &
225 ~((u64)root->sectorsize - 1);
227 u64 data_len = inline_len;
231 data_len = compressed_size;
234 actual_end >= PAGE_CACHE_SIZE ||
235 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
237 (actual_end & (root->sectorsize - 1)) == 0) ||
239 data_len > root->fs_info->max_inline) {
243 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
247 if (isize > actual_end)
248 inline_len = min_t(u64, isize, actual_end);
249 ret = insert_inline_extent(trans, root, inode, start,
250 inline_len, compressed_size,
253 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
257 struct async_extent {
262 unsigned long nr_pages;
263 struct list_head list;
268 struct btrfs_root *root;
269 struct page *locked_page;
272 struct list_head extents;
273 struct btrfs_work work;
276 static noinline int add_async_extent(struct async_cow *cow,
277 u64 start, u64 ram_size,
280 unsigned long nr_pages)
282 struct async_extent *async_extent;
284 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
285 async_extent->start = start;
286 async_extent->ram_size = ram_size;
287 async_extent->compressed_size = compressed_size;
288 async_extent->pages = pages;
289 async_extent->nr_pages = nr_pages;
290 list_add_tail(&async_extent->list, &cow->extents);
295 * we create compressed extents in two phases. The first
296 * phase compresses a range of pages that have already been
297 * locked (both pages and state bits are locked).
299 * This is done inside an ordered work queue, and the compression
300 * is spread across many cpus. The actual IO submission is step
301 * two, and the ordered work queue takes care of making sure that
302 * happens in the same order things were put onto the queue by
303 * writepages and friends.
305 * If this code finds it can't get good compression, it puts an
306 * entry onto the work queue to write the uncompressed bytes. This
307 * makes sure that both compressed inodes and uncompressed inodes
308 * are written in the same order that pdflush sent them down.
310 static noinline int compress_file_range(struct inode *inode,
311 struct page *locked_page,
313 struct async_cow *async_cow,
316 struct btrfs_root *root = BTRFS_I(inode)->root;
317 struct btrfs_trans_handle *trans;
321 u64 blocksize = root->sectorsize;
323 u64 isize = i_size_read(inode);
325 struct page **pages = NULL;
326 unsigned long nr_pages;
327 unsigned long nr_pages_ret = 0;
328 unsigned long total_compressed = 0;
329 unsigned long total_in = 0;
330 unsigned long max_compressed = 128 * 1024;
331 unsigned long max_uncompressed = 128 * 1024;
337 actual_end = min_t(u64, isize, end + 1);
340 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
341 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
344 * we don't want to send crud past the end of i_size through
345 * compression, that's just a waste of CPU time. So, if the
346 * end of the file is before the start of our current
347 * requested range of bytes, we bail out to the uncompressed
348 * cleanup code that can deal with all of this.
350 * It isn't really the fastest way to fix things, but this is a
351 * very uncommon corner.
353 if (actual_end <= start)
354 goto cleanup_and_bail_uncompressed;
356 total_compressed = actual_end - start;
358 /* we want to make sure that amount of ram required to uncompress
359 * an extent is reasonable, so we limit the total size in ram
360 * of a compressed extent to 128k. This is a crucial number
361 * because it also controls how easily we can spread reads across
362 * cpus for decompression.
364 * We also want to make sure the amount of IO required to do
365 * a random read is reasonably small, so we limit the size of
366 * a compressed extent to 128k.
368 total_compressed = min(total_compressed, max_uncompressed);
369 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
370 num_bytes = max(blocksize, num_bytes);
371 disk_num_bytes = num_bytes;
376 * we do compression for mount -o compress and when the
377 * inode has not been flagged as nocompress. This flag can
378 * change at any time if we discover bad compression ratios.
380 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
381 btrfs_test_opt(root, COMPRESS)) {
383 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
385 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
386 total_compressed, pages,
387 nr_pages, &nr_pages_ret,
393 unsigned long offset = total_compressed &
394 (PAGE_CACHE_SIZE - 1);
395 struct page *page = pages[nr_pages_ret - 1];
398 /* zero the tail end of the last page, we might be
399 * sending it down to disk
402 kaddr = kmap_atomic(page, KM_USER0);
403 memset(kaddr + offset, 0,
404 PAGE_CACHE_SIZE - offset);
405 kunmap_atomic(kaddr, KM_USER0);
411 trans = btrfs_join_transaction(root, 1);
413 btrfs_set_trans_block_group(trans, inode);
415 /* lets try to make an inline extent */
416 if (ret || total_in < (actual_end - start)) {
417 /* we didn't compress the entire range, try
418 * to make an uncompressed inline extent.
420 ret = cow_file_range_inline(trans, root, inode,
421 start, end, 0, NULL);
423 /* try making a compressed inline extent */
424 ret = cow_file_range_inline(trans, root, inode,
426 total_compressed, pages);
430 * inline extent creation worked, we don't need
431 * to create any more async work items. Unlock
432 * and free up our temp pages.
434 extent_clear_unlock_delalloc(inode,
435 &BTRFS_I(inode)->io_tree,
437 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
438 EXTENT_CLEAR_DELALLOC |
439 EXTENT_CLEAR_ACCOUNTING |
440 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
442 btrfs_end_transaction(trans, root);
445 btrfs_end_transaction(trans, root);
450 * we aren't doing an inline extent round the compressed size
451 * up to a block size boundary so the allocator does sane
454 total_compressed = (total_compressed + blocksize - 1) &
458 * one last check to make sure the compression is really a
459 * win, compare the page count read with the blocks on disk
461 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
462 ~(PAGE_CACHE_SIZE - 1);
463 if (total_compressed >= total_in) {
466 disk_num_bytes = total_compressed;
467 num_bytes = total_in;
470 if (!will_compress && pages) {
472 * the compression code ran but failed to make things smaller,
473 * free any pages it allocated and our page pointer array
475 for (i = 0; i < nr_pages_ret; i++) {
476 WARN_ON(pages[i]->mapping);
477 page_cache_release(pages[i]);
481 total_compressed = 0;
484 /* flag the file so we don't compress in the future */
485 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
490 /* the async work queues will take care of doing actual
491 * allocation on disk for these compressed pages,
492 * and will submit them to the elevator.
494 add_async_extent(async_cow, start, num_bytes,
495 total_compressed, pages, nr_pages_ret);
497 if (start + num_bytes < end && start + num_bytes < actual_end) {
504 cleanup_and_bail_uncompressed:
506 * No compression, but we still need to write the pages in
507 * the file we've been given so far. redirty the locked
508 * page if it corresponds to our extent and set things up
509 * for the async work queue to run cow_file_range to do
510 * the normal delalloc dance
512 if (page_offset(locked_page) >= start &&
513 page_offset(locked_page) <= end) {
514 __set_page_dirty_nobuffers(locked_page);
515 /* unlocked later on in the async handlers */
517 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
525 for (i = 0; i < nr_pages_ret; i++) {
526 WARN_ON(pages[i]->mapping);
527 page_cache_release(pages[i]);
535 * phase two of compressed writeback. This is the ordered portion
536 * of the code, which only gets called in the order the work was
537 * queued. We walk all the async extents created by compress_file_range
538 * and send them down to the disk.
540 static noinline int submit_compressed_extents(struct inode *inode,
541 struct async_cow *async_cow)
543 struct async_extent *async_extent;
545 struct btrfs_trans_handle *trans;
546 struct btrfs_key ins;
547 struct extent_map *em;
548 struct btrfs_root *root = BTRFS_I(inode)->root;
549 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
550 struct extent_io_tree *io_tree;
553 if (list_empty(&async_cow->extents))
557 while (!list_empty(&async_cow->extents)) {
558 async_extent = list_entry(async_cow->extents.next,
559 struct async_extent, list);
560 list_del(&async_extent->list);
562 io_tree = &BTRFS_I(inode)->io_tree;
565 /* did the compression code fall back to uncompressed IO? */
566 if (!async_extent->pages) {
567 int page_started = 0;
568 unsigned long nr_written = 0;
570 lock_extent(io_tree, async_extent->start,
571 async_extent->start +
572 async_extent->ram_size - 1, GFP_NOFS);
574 /* allocate blocks */
575 ret = cow_file_range(inode, async_cow->locked_page,
577 async_extent->start +
578 async_extent->ram_size - 1,
579 &page_started, &nr_written, 0);
582 * if page_started, cow_file_range inserted an
583 * inline extent and took care of all the unlocking
584 * and IO for us. Otherwise, we need to submit
585 * all those pages down to the drive.
587 if (!page_started && !ret)
588 extent_write_locked_range(io_tree,
589 inode, async_extent->start,
590 async_extent->start +
591 async_extent->ram_size - 1,
599 lock_extent(io_tree, async_extent->start,
600 async_extent->start + async_extent->ram_size - 1,
603 trans = btrfs_join_transaction(root, 1);
604 ret = btrfs_reserve_extent(trans, root,
605 async_extent->compressed_size,
606 async_extent->compressed_size,
609 btrfs_end_transaction(trans, root);
613 for (i = 0; i < async_extent->nr_pages; i++) {
614 WARN_ON(async_extent->pages[i]->mapping);
615 page_cache_release(async_extent->pages[i]);
617 kfree(async_extent->pages);
618 async_extent->nr_pages = 0;
619 async_extent->pages = NULL;
620 unlock_extent(io_tree, async_extent->start,
621 async_extent->start +
622 async_extent->ram_size - 1, GFP_NOFS);
627 * here we're doing allocation and writeback of the
630 btrfs_drop_extent_cache(inode, async_extent->start,
631 async_extent->start +
632 async_extent->ram_size - 1, 0);
634 em = alloc_extent_map(GFP_NOFS);
635 em->start = async_extent->start;
636 em->len = async_extent->ram_size;
637 em->orig_start = em->start;
639 em->block_start = ins.objectid;
640 em->block_len = ins.offset;
641 em->bdev = root->fs_info->fs_devices->latest_bdev;
642 set_bit(EXTENT_FLAG_PINNED, &em->flags);
643 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
646 write_lock(&em_tree->lock);
647 ret = add_extent_mapping(em_tree, em);
648 write_unlock(&em_tree->lock);
649 if (ret != -EEXIST) {
653 btrfs_drop_extent_cache(inode, async_extent->start,
654 async_extent->start +
655 async_extent->ram_size - 1, 0);
658 ret = btrfs_add_ordered_extent(inode, async_extent->start,
660 async_extent->ram_size,
662 BTRFS_ORDERED_COMPRESSED);
666 * clear dirty, set writeback and unlock the pages.
668 extent_clear_unlock_delalloc(inode,
669 &BTRFS_I(inode)->io_tree,
671 async_extent->start +
672 async_extent->ram_size - 1,
673 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
674 EXTENT_CLEAR_UNLOCK |
675 EXTENT_CLEAR_DELALLOC |
676 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
678 ret = btrfs_submit_compressed_write(inode,
680 async_extent->ram_size,
682 ins.offset, async_extent->pages,
683 async_extent->nr_pages);
686 alloc_hint = ins.objectid + ins.offset;
695 * when extent_io.c finds a delayed allocation range in the file,
696 * the call backs end up in this code. The basic idea is to
697 * allocate extents on disk for the range, and create ordered data structs
698 * in ram to track those extents.
700 * locked_page is the page that writepage had locked already. We use
701 * it to make sure we don't do extra locks or unlocks.
703 * *page_started is set to one if we unlock locked_page and do everything
704 * required to start IO on it. It may be clean and already done with
707 static noinline int cow_file_range(struct inode *inode,
708 struct page *locked_page,
709 u64 start, u64 end, int *page_started,
710 unsigned long *nr_written,
713 struct btrfs_root *root = BTRFS_I(inode)->root;
714 struct btrfs_trans_handle *trans;
717 unsigned long ram_size;
720 u64 blocksize = root->sectorsize;
722 u64 isize = i_size_read(inode);
723 struct btrfs_key ins;
724 struct extent_map *em;
725 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
728 trans = btrfs_join_transaction(root, 1);
730 btrfs_set_trans_block_group(trans, inode);
732 actual_end = min_t(u64, isize, end + 1);
734 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
735 num_bytes = max(blocksize, num_bytes);
736 disk_num_bytes = num_bytes;
740 /* lets try to make an inline extent */
741 ret = cow_file_range_inline(trans, root, inode,
742 start, end, 0, NULL);
744 extent_clear_unlock_delalloc(inode,
745 &BTRFS_I(inode)->io_tree,
747 EXTENT_CLEAR_UNLOCK_PAGE |
748 EXTENT_CLEAR_UNLOCK |
749 EXTENT_CLEAR_DELALLOC |
750 EXTENT_CLEAR_ACCOUNTING |
752 EXTENT_SET_WRITEBACK |
753 EXTENT_END_WRITEBACK);
755 *nr_written = *nr_written +
756 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
763 BUG_ON(disk_num_bytes >
764 btrfs_super_total_bytes(&root->fs_info->super_copy));
767 read_lock(&BTRFS_I(inode)->extent_tree.lock);
768 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
772 * if block start isn't an actual block number then find the
773 * first block in this inode and use that as a hint. If that
774 * block is also bogus then just don't worry about it.
776 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
778 em = search_extent_mapping(em_tree, 0, 0);
779 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
780 alloc_hint = em->block_start;
784 alloc_hint = em->block_start;
788 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
789 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
791 while (disk_num_bytes > 0) {
794 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
795 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
796 root->sectorsize, 0, alloc_hint,
800 em = alloc_extent_map(GFP_NOFS);
802 em->orig_start = em->start;
803 ram_size = ins.offset;
804 em->len = ins.offset;
806 em->block_start = ins.objectid;
807 em->block_len = ins.offset;
808 em->bdev = root->fs_info->fs_devices->latest_bdev;
809 set_bit(EXTENT_FLAG_PINNED, &em->flags);
812 write_lock(&em_tree->lock);
813 ret = add_extent_mapping(em_tree, em);
814 write_unlock(&em_tree->lock);
815 if (ret != -EEXIST) {
819 btrfs_drop_extent_cache(inode, start,
820 start + ram_size - 1, 0);
823 cur_alloc_size = ins.offset;
824 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
825 ram_size, cur_alloc_size, 0);
828 if (root->root_key.objectid ==
829 BTRFS_DATA_RELOC_TREE_OBJECTID) {
830 ret = btrfs_reloc_clone_csums(inode, start,
835 if (disk_num_bytes < cur_alloc_size)
838 /* we're not doing compressed IO, don't unlock the first
839 * page (which the caller expects to stay locked), don't
840 * clear any dirty bits and don't set any writeback bits
842 * Do set the Private2 bit so we know this page was properly
843 * setup for writepage
845 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
846 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
849 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
850 start, start + ram_size - 1,
852 disk_num_bytes -= cur_alloc_size;
853 num_bytes -= cur_alloc_size;
854 alloc_hint = ins.objectid + ins.offset;
855 start += cur_alloc_size;
859 btrfs_end_transaction(trans, root);
865 * work queue call back to started compression on a file and pages
867 static noinline void async_cow_start(struct btrfs_work *work)
869 struct async_cow *async_cow;
871 async_cow = container_of(work, struct async_cow, work);
873 compress_file_range(async_cow->inode, async_cow->locked_page,
874 async_cow->start, async_cow->end, async_cow,
877 async_cow->inode = NULL;
881 * work queue call back to submit previously compressed pages
883 static noinline void async_cow_submit(struct btrfs_work *work)
885 struct async_cow *async_cow;
886 struct btrfs_root *root;
887 unsigned long nr_pages;
889 async_cow = container_of(work, struct async_cow, work);
891 root = async_cow->root;
892 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
895 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
897 if (atomic_read(&root->fs_info->async_delalloc_pages) <
899 waitqueue_active(&root->fs_info->async_submit_wait))
900 wake_up(&root->fs_info->async_submit_wait);
902 if (async_cow->inode)
903 submit_compressed_extents(async_cow->inode, async_cow);
906 static noinline void async_cow_free(struct btrfs_work *work)
908 struct async_cow *async_cow;
909 async_cow = container_of(work, struct async_cow, work);
913 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
914 u64 start, u64 end, int *page_started,
915 unsigned long *nr_written)
917 struct async_cow *async_cow;
918 struct btrfs_root *root = BTRFS_I(inode)->root;
919 unsigned long nr_pages;
921 int limit = 10 * 1024 * 1042;
923 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
924 1, 0, NULL, GFP_NOFS);
925 while (start < end) {
926 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
927 async_cow->inode = inode;
928 async_cow->root = root;
929 async_cow->locked_page = locked_page;
930 async_cow->start = start;
932 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
935 cur_end = min(end, start + 512 * 1024 - 1);
937 async_cow->end = cur_end;
938 INIT_LIST_HEAD(&async_cow->extents);
940 async_cow->work.func = async_cow_start;
941 async_cow->work.ordered_func = async_cow_submit;
942 async_cow->work.ordered_free = async_cow_free;
943 async_cow->work.flags = 0;
945 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
947 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
949 btrfs_queue_worker(&root->fs_info->delalloc_workers,
952 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
953 wait_event(root->fs_info->async_submit_wait,
954 (atomic_read(&root->fs_info->async_delalloc_pages) <
958 while (atomic_read(&root->fs_info->async_submit_draining) &&
959 atomic_read(&root->fs_info->async_delalloc_pages)) {
960 wait_event(root->fs_info->async_submit_wait,
961 (atomic_read(&root->fs_info->async_delalloc_pages) ==
965 *nr_written += nr_pages;
972 static noinline int csum_exist_in_range(struct btrfs_root *root,
973 u64 bytenr, u64 num_bytes)
976 struct btrfs_ordered_sum *sums;
979 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
980 bytenr + num_bytes - 1, &list);
981 if (ret == 0 && list_empty(&list))
984 while (!list_empty(&list)) {
985 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
986 list_del(&sums->list);
993 * when nowcow writeback call back. This checks for snapshots or COW copies
994 * of the extents that exist in the file, and COWs the file as required.
996 * If no cow copies or snapshots exist, we write directly to the existing
999 static noinline int run_delalloc_nocow(struct inode *inode,
1000 struct page *locked_page,
1001 u64 start, u64 end, int *page_started, int force,
1002 unsigned long *nr_written)
1004 struct btrfs_root *root = BTRFS_I(inode)->root;
1005 struct btrfs_trans_handle *trans;
1006 struct extent_buffer *leaf;
1007 struct btrfs_path *path;
1008 struct btrfs_file_extent_item *fi;
1009 struct btrfs_key found_key;
1022 path = btrfs_alloc_path();
1024 trans = btrfs_join_transaction(root, 1);
1027 cow_start = (u64)-1;
1030 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1033 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1034 leaf = path->nodes[0];
1035 btrfs_item_key_to_cpu(leaf, &found_key,
1036 path->slots[0] - 1);
1037 if (found_key.objectid == inode->i_ino &&
1038 found_key.type == BTRFS_EXTENT_DATA_KEY)
1043 leaf = path->nodes[0];
1044 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1045 ret = btrfs_next_leaf(root, path);
1050 leaf = path->nodes[0];
1056 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1058 if (found_key.objectid > inode->i_ino ||
1059 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1060 found_key.offset > end)
1063 if (found_key.offset > cur_offset) {
1064 extent_end = found_key.offset;
1069 fi = btrfs_item_ptr(leaf, path->slots[0],
1070 struct btrfs_file_extent_item);
1071 extent_type = btrfs_file_extent_type(leaf, fi);
1073 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1074 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1075 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1076 extent_offset = btrfs_file_extent_offset(leaf, fi);
1077 extent_end = found_key.offset +
1078 btrfs_file_extent_num_bytes(leaf, fi);
1079 if (extent_end <= start) {
1083 if (disk_bytenr == 0)
1085 if (btrfs_file_extent_compression(leaf, fi) ||
1086 btrfs_file_extent_encryption(leaf, fi) ||
1087 btrfs_file_extent_other_encoding(leaf, fi))
1089 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1091 if (btrfs_extent_readonly(root, disk_bytenr))
1093 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1095 extent_offset, disk_bytenr))
1097 disk_bytenr += extent_offset;
1098 disk_bytenr += cur_offset - found_key.offset;
1099 num_bytes = min(end + 1, extent_end) - cur_offset;
1101 * force cow if csum exists in the range.
1102 * this ensure that csum for a given extent are
1103 * either valid or do not exist.
1105 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1108 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1109 extent_end = found_key.offset +
1110 btrfs_file_extent_inline_len(leaf, fi);
1111 extent_end = ALIGN(extent_end, root->sectorsize);
1116 if (extent_end <= start) {
1121 if (cow_start == (u64)-1)
1122 cow_start = cur_offset;
1123 cur_offset = extent_end;
1124 if (cur_offset > end)
1130 btrfs_release_path(root, path);
1131 if (cow_start != (u64)-1) {
1132 ret = cow_file_range(inode, locked_page, cow_start,
1133 found_key.offset - 1, page_started,
1136 cow_start = (u64)-1;
1139 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1140 struct extent_map *em;
1141 struct extent_map_tree *em_tree;
1142 em_tree = &BTRFS_I(inode)->extent_tree;
1143 em = alloc_extent_map(GFP_NOFS);
1144 em->start = cur_offset;
1145 em->orig_start = em->start;
1146 em->len = num_bytes;
1147 em->block_len = num_bytes;
1148 em->block_start = disk_bytenr;
1149 em->bdev = root->fs_info->fs_devices->latest_bdev;
1150 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1152 write_lock(&em_tree->lock);
1153 ret = add_extent_mapping(em_tree, em);
1154 write_unlock(&em_tree->lock);
1155 if (ret != -EEXIST) {
1156 free_extent_map(em);
1159 btrfs_drop_extent_cache(inode, em->start,
1160 em->start + em->len - 1, 0);
1162 type = BTRFS_ORDERED_PREALLOC;
1164 type = BTRFS_ORDERED_NOCOW;
1167 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1168 num_bytes, num_bytes, type);
1171 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1172 cur_offset, cur_offset + num_bytes - 1,
1173 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1174 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1175 EXTENT_SET_PRIVATE2);
1176 cur_offset = extent_end;
1177 if (cur_offset > end)
1180 btrfs_release_path(root, path);
1182 if (cur_offset <= end && cow_start == (u64)-1)
1183 cow_start = cur_offset;
1184 if (cow_start != (u64)-1) {
1185 ret = cow_file_range(inode, locked_page, cow_start, end,
1186 page_started, nr_written, 1);
1190 ret = btrfs_end_transaction(trans, root);
1192 btrfs_free_path(path);
1197 * extent_io.c call back to do delayed allocation processing
1199 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1200 u64 start, u64 end, int *page_started,
1201 unsigned long *nr_written)
1204 struct btrfs_root *root = BTRFS_I(inode)->root;
1206 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1207 ret = run_delalloc_nocow(inode, locked_page, start, end,
1208 page_started, 1, nr_written);
1209 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1210 ret = run_delalloc_nocow(inode, locked_page, start, end,
1211 page_started, 0, nr_written);
1212 else if (!btrfs_test_opt(root, COMPRESS))
1213 ret = cow_file_range(inode, locked_page, start, end,
1214 page_started, nr_written, 1);
1216 ret = cow_file_range_async(inode, locked_page, start, end,
1217 page_started, nr_written);
1221 static int btrfs_split_extent_hook(struct inode *inode,
1222 struct extent_state *orig, u64 split)
1224 struct btrfs_root *root = BTRFS_I(inode)->root;
1227 if (!(orig->state & EXTENT_DELALLOC))
1230 size = orig->end - orig->start + 1;
1231 if (size > root->fs_info->max_extent) {
1235 new_size = orig->end - split + 1;
1236 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1237 root->fs_info->max_extent);
1240 * if we break a large extent up then leave oustanding_extents
1241 * be, since we've already accounted for the large extent.
1243 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1244 root->fs_info->max_extent) < num_extents)
1248 spin_lock(&BTRFS_I(inode)->accounting_lock);
1249 BTRFS_I(inode)->outstanding_extents++;
1250 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1256 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1257 * extents so we can keep track of new extents that are just merged onto old
1258 * extents, such as when we are doing sequential writes, so we can properly
1259 * account for the metadata space we'll need.
1261 static int btrfs_merge_extent_hook(struct inode *inode,
1262 struct extent_state *new,
1263 struct extent_state *other)
1265 struct btrfs_root *root = BTRFS_I(inode)->root;
1266 u64 new_size, old_size;
1269 /* not delalloc, ignore it */
1270 if (!(other->state & EXTENT_DELALLOC))
1273 old_size = other->end - other->start + 1;
1274 if (new->start < other->start)
1275 new_size = other->end - new->start + 1;
1277 new_size = new->end - other->start + 1;
1279 /* we're not bigger than the max, unreserve the space and go */
1280 if (new_size <= root->fs_info->max_extent) {
1281 spin_lock(&BTRFS_I(inode)->accounting_lock);
1282 BTRFS_I(inode)->outstanding_extents--;
1283 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1288 * If we grew by another max_extent, just return, we want to keep that
1291 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1292 root->fs_info->max_extent);
1293 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1294 root->fs_info->max_extent) > num_extents)
1297 spin_lock(&BTRFS_I(inode)->accounting_lock);
1298 BTRFS_I(inode)->outstanding_extents--;
1299 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1305 * extent_io.c set_bit_hook, used to track delayed allocation
1306 * bytes in this file, and to maintain the list of inodes that
1307 * have pending delalloc work to be done.
1309 static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1310 unsigned long old, unsigned long bits)
1314 * set_bit and clear bit hooks normally require _irqsave/restore
1315 * but in this case, we are only testeing for the DELALLOC
1316 * bit, which is only set or cleared with irqs on
1318 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1319 struct btrfs_root *root = BTRFS_I(inode)->root;
1321 spin_lock(&BTRFS_I(inode)->accounting_lock);
1322 BTRFS_I(inode)->outstanding_extents++;
1323 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1324 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1325 spin_lock(&root->fs_info->delalloc_lock);
1326 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
1327 root->fs_info->delalloc_bytes += end - start + 1;
1328 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1329 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1330 &root->fs_info->delalloc_inodes);
1332 spin_unlock(&root->fs_info->delalloc_lock);
1338 * extent_io.c clear_bit_hook, see set_bit_hook for why
1340 static int btrfs_clear_bit_hook(struct inode *inode,
1341 struct extent_state *state, unsigned long bits)
1344 * set_bit and clear bit hooks normally require _irqsave/restore
1345 * but in this case, we are only testeing for the DELALLOC
1346 * bit, which is only set or cleared with irqs on
1348 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1349 struct btrfs_root *root = BTRFS_I(inode)->root;
1351 if (bits & EXTENT_DO_ACCOUNTING) {
1352 spin_lock(&BTRFS_I(inode)->accounting_lock);
1353 BTRFS_I(inode)->outstanding_extents--;
1354 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1355 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1358 spin_lock(&root->fs_info->delalloc_lock);
1359 if (state->end - state->start + 1 >
1360 root->fs_info->delalloc_bytes) {
1361 printk(KERN_INFO "btrfs warning: delalloc account "
1363 (unsigned long long)
1364 state->end - state->start + 1,
1365 (unsigned long long)
1366 root->fs_info->delalloc_bytes);
1367 btrfs_delalloc_free_space(root, inode, (u64)-1);
1368 root->fs_info->delalloc_bytes = 0;
1369 BTRFS_I(inode)->delalloc_bytes = 0;
1371 btrfs_delalloc_free_space(root, inode,
1374 root->fs_info->delalloc_bytes -= state->end -
1376 BTRFS_I(inode)->delalloc_bytes -= state->end -
1379 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1380 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1381 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1383 spin_unlock(&root->fs_info->delalloc_lock);
1389 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1390 * we don't create bios that span stripes or chunks
1392 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1393 size_t size, struct bio *bio,
1394 unsigned long bio_flags)
1396 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1397 struct btrfs_mapping_tree *map_tree;
1398 u64 logical = (u64)bio->bi_sector << 9;
1403 if (bio_flags & EXTENT_BIO_COMPRESSED)
1406 length = bio->bi_size;
1407 map_tree = &root->fs_info->mapping_tree;
1408 map_length = length;
1409 ret = btrfs_map_block(map_tree, READ, logical,
1410 &map_length, NULL, 0);
1412 if (map_length < length + size)
1418 * in order to insert checksums into the metadata in large chunks,
1419 * we wait until bio submission time. All the pages in the bio are
1420 * checksummed and sums are attached onto the ordered extent record.
1422 * At IO completion time the cums attached on the ordered extent record
1423 * are inserted into the btree
1425 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1426 struct bio *bio, int mirror_num,
1427 unsigned long bio_flags)
1429 struct btrfs_root *root = BTRFS_I(inode)->root;
1432 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1438 * in order to insert checksums into the metadata in large chunks,
1439 * we wait until bio submission time. All the pages in the bio are
1440 * checksummed and sums are attached onto the ordered extent record.
1442 * At IO completion time the cums attached on the ordered extent record
1443 * are inserted into the btree
1445 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1446 int mirror_num, unsigned long bio_flags)
1448 struct btrfs_root *root = BTRFS_I(inode)->root;
1449 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1453 * extent_io.c submission hook. This does the right thing for csum calculation
1454 * on write, or reading the csums from the tree before a read
1456 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1457 int mirror_num, unsigned long bio_flags)
1459 struct btrfs_root *root = BTRFS_I(inode)->root;
1463 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1465 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1468 if (!(rw & (1 << BIO_RW))) {
1469 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1470 return btrfs_submit_compressed_read(inode, bio,
1471 mirror_num, bio_flags);
1472 } else if (!skip_sum)
1473 btrfs_lookup_bio_sums(root, inode, bio, NULL);
1475 } else if (!skip_sum) {
1476 /* csum items have already been cloned */
1477 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1479 /* we're doing a write, do the async checksumming */
1480 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1481 inode, rw, bio, mirror_num,
1482 bio_flags, __btrfs_submit_bio_start,
1483 __btrfs_submit_bio_done);
1487 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1491 * given a list of ordered sums record them in the inode. This happens
1492 * at IO completion time based on sums calculated at bio submission time.
1494 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1495 struct inode *inode, u64 file_offset,
1496 struct list_head *list)
1498 struct btrfs_ordered_sum *sum;
1500 btrfs_set_trans_block_group(trans, inode);
1502 list_for_each_entry(sum, list, list) {
1503 btrfs_csum_file_blocks(trans,
1504 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1509 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1511 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1513 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1517 /* see btrfs_writepage_start_hook for details on why this is required */
1518 struct btrfs_writepage_fixup {
1520 struct btrfs_work work;
1523 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1525 struct btrfs_writepage_fixup *fixup;
1526 struct btrfs_ordered_extent *ordered;
1528 struct inode *inode;
1532 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1536 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1537 ClearPageChecked(page);
1541 inode = page->mapping->host;
1542 page_start = page_offset(page);
1543 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1545 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1547 /* already ordered? We're done */
1548 if (PagePrivate2(page))
1551 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1553 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1554 page_end, GFP_NOFS);
1556 btrfs_start_ordered_extent(inode, ordered, 1);
1560 btrfs_set_extent_delalloc(inode, page_start, page_end);
1561 ClearPageChecked(page);
1563 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1566 page_cache_release(page);
1570 * There are a few paths in the higher layers of the kernel that directly
1571 * set the page dirty bit without asking the filesystem if it is a
1572 * good idea. This causes problems because we want to make sure COW
1573 * properly happens and the data=ordered rules are followed.
1575 * In our case any range that doesn't have the ORDERED bit set
1576 * hasn't been properly setup for IO. We kick off an async process
1577 * to fix it up. The async helper will wait for ordered extents, set
1578 * the delalloc bit and make it safe to write the page.
1580 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1582 struct inode *inode = page->mapping->host;
1583 struct btrfs_writepage_fixup *fixup;
1584 struct btrfs_root *root = BTRFS_I(inode)->root;
1586 /* this page is properly in the ordered list */
1587 if (TestClearPagePrivate2(page))
1590 if (PageChecked(page))
1593 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1597 SetPageChecked(page);
1598 page_cache_get(page);
1599 fixup->work.func = btrfs_writepage_fixup_worker;
1601 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1605 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1606 struct inode *inode, u64 file_pos,
1607 u64 disk_bytenr, u64 disk_num_bytes,
1608 u64 num_bytes, u64 ram_bytes,
1609 u8 compression, u8 encryption,
1610 u16 other_encoding, int extent_type)
1612 struct btrfs_root *root = BTRFS_I(inode)->root;
1613 struct btrfs_file_extent_item *fi;
1614 struct btrfs_path *path;
1615 struct extent_buffer *leaf;
1616 struct btrfs_key ins;
1620 path = btrfs_alloc_path();
1623 path->leave_spinning = 1;
1626 * we may be replacing one extent in the tree with another.
1627 * The new extent is pinned in the extent map, and we don't want
1628 * to drop it from the cache until it is completely in the btree.
1630 * So, tell btrfs_drop_extents to leave this extent in the cache.
1631 * the caller is expected to unpin it and allow it to be merged
1634 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1638 ins.objectid = inode->i_ino;
1639 ins.offset = file_pos;
1640 ins.type = BTRFS_EXTENT_DATA_KEY;
1641 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1643 leaf = path->nodes[0];
1644 fi = btrfs_item_ptr(leaf, path->slots[0],
1645 struct btrfs_file_extent_item);
1646 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1647 btrfs_set_file_extent_type(leaf, fi, extent_type);
1648 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1649 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1650 btrfs_set_file_extent_offset(leaf, fi, 0);
1651 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1652 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1653 btrfs_set_file_extent_compression(leaf, fi, compression);
1654 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1655 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1657 btrfs_unlock_up_safe(path, 1);
1658 btrfs_set_lock_blocking(leaf);
1660 btrfs_mark_buffer_dirty(leaf);
1662 inode_add_bytes(inode, num_bytes);
1664 ins.objectid = disk_bytenr;
1665 ins.offset = disk_num_bytes;
1666 ins.type = BTRFS_EXTENT_ITEM_KEY;
1667 ret = btrfs_alloc_reserved_file_extent(trans, root,
1668 root->root_key.objectid,
1669 inode->i_ino, file_pos, &ins);
1671 btrfs_free_path(path);
1677 * helper function for btrfs_finish_ordered_io, this
1678 * just reads in some of the csum leaves to prime them into ram
1679 * before we start the transaction. It limits the amount of btree
1680 * reads required while inside the transaction.
1682 static noinline void reada_csum(struct btrfs_root *root,
1683 struct btrfs_path *path,
1684 struct btrfs_ordered_extent *ordered_extent)
1686 struct btrfs_ordered_sum *sum;
1689 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1691 bytenr = sum->sums[0].bytenr;
1694 * we don't care about the results, the point of this search is
1695 * just to get the btree leaves into ram
1697 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1700 /* as ordered data IO finishes, this gets called so we can finish
1701 * an ordered extent if the range of bytes in the file it covers are
1704 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1706 struct btrfs_root *root = BTRFS_I(inode)->root;
1707 struct btrfs_trans_handle *trans;
1708 struct btrfs_ordered_extent *ordered_extent = NULL;
1709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1710 struct btrfs_path *path;
1714 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
1719 * before we join the transaction, try to do some of our IO.
1720 * This will limit the amount of IO that we have to do with
1721 * the transaction running. We're unlikely to need to do any
1722 * IO if the file extents are new, the disk_i_size checks
1723 * covers the most common case.
1725 if (start < BTRFS_I(inode)->disk_i_size) {
1726 path = btrfs_alloc_path();
1728 ret = btrfs_lookup_file_extent(NULL, root, path,
1731 ordered_extent = btrfs_lookup_ordered_extent(inode,
1733 if (!list_empty(&ordered_extent->list)) {
1734 btrfs_release_path(root, path);
1735 reada_csum(root, path, ordered_extent);
1737 btrfs_free_path(path);
1741 if (!ordered_extent)
1742 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1743 BUG_ON(!ordered_extent);
1744 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1745 BUG_ON(!list_empty(&ordered_extent->list));
1746 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1748 trans = btrfs_join_transaction(root, 1);
1749 ret = btrfs_update_inode(trans, root, inode);
1751 btrfs_end_transaction(trans, root);
1756 lock_extent(io_tree, ordered_extent->file_offset,
1757 ordered_extent->file_offset + ordered_extent->len - 1,
1760 trans = btrfs_join_transaction(root, 1);
1762 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1764 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1766 ret = btrfs_mark_extent_written(trans, inode,
1767 ordered_extent->file_offset,
1768 ordered_extent->file_offset +
1769 ordered_extent->len);
1772 ret = insert_reserved_file_extent(trans, inode,
1773 ordered_extent->file_offset,
1774 ordered_extent->start,
1775 ordered_extent->disk_len,
1776 ordered_extent->len,
1777 ordered_extent->len,
1779 BTRFS_FILE_EXTENT_REG);
1780 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1781 ordered_extent->file_offset,
1782 ordered_extent->len);
1785 unlock_extent(io_tree, ordered_extent->file_offset,
1786 ordered_extent->file_offset + ordered_extent->len - 1,
1788 add_pending_csums(trans, inode, ordered_extent->file_offset,
1789 &ordered_extent->list);
1791 /* this also removes the ordered extent from the tree */
1792 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1793 ret = btrfs_update_inode(trans, root, inode);
1795 btrfs_end_transaction(trans, root);
1798 btrfs_put_ordered_extent(ordered_extent);
1799 /* once for the tree */
1800 btrfs_put_ordered_extent(ordered_extent);
1805 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1806 struct extent_state *state, int uptodate)
1808 ClearPagePrivate2(page);
1809 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1813 * When IO fails, either with EIO or csum verification fails, we
1814 * try other mirrors that might have a good copy of the data. This
1815 * io_failure_record is used to record state as we go through all the
1816 * mirrors. If another mirror has good data, the page is set up to date
1817 * and things continue. If a good mirror can't be found, the original
1818 * bio end_io callback is called to indicate things have failed.
1820 struct io_failure_record {
1825 unsigned long bio_flags;
1829 static int btrfs_io_failed_hook(struct bio *failed_bio,
1830 struct page *page, u64 start, u64 end,
1831 struct extent_state *state)
1833 struct io_failure_record *failrec = NULL;
1835 struct extent_map *em;
1836 struct inode *inode = page->mapping->host;
1837 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1838 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1845 ret = get_state_private(failure_tree, start, &private);
1847 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1850 failrec->start = start;
1851 failrec->len = end - start + 1;
1852 failrec->last_mirror = 0;
1853 failrec->bio_flags = 0;
1855 read_lock(&em_tree->lock);
1856 em = lookup_extent_mapping(em_tree, start, failrec->len);
1857 if (em->start > start || em->start + em->len < start) {
1858 free_extent_map(em);
1861 read_unlock(&em_tree->lock);
1863 if (!em || IS_ERR(em)) {
1867 logical = start - em->start;
1868 logical = em->block_start + logical;
1869 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1870 logical = em->block_start;
1871 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1873 failrec->logical = logical;
1874 free_extent_map(em);
1875 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1876 EXTENT_DIRTY, GFP_NOFS);
1877 set_state_private(failure_tree, start,
1878 (u64)(unsigned long)failrec);
1880 failrec = (struct io_failure_record *)(unsigned long)private;
1882 num_copies = btrfs_num_copies(
1883 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1884 failrec->logical, failrec->len);
1885 failrec->last_mirror++;
1887 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1888 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1891 if (state && state->start != failrec->start)
1893 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1895 if (!state || failrec->last_mirror > num_copies) {
1896 set_state_private(failure_tree, failrec->start, 0);
1897 clear_extent_bits(failure_tree, failrec->start,
1898 failrec->start + failrec->len - 1,
1899 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1903 bio = bio_alloc(GFP_NOFS, 1);
1904 bio->bi_private = state;
1905 bio->bi_end_io = failed_bio->bi_end_io;
1906 bio->bi_sector = failrec->logical >> 9;
1907 bio->bi_bdev = failed_bio->bi_bdev;
1910 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1911 if (failed_bio->bi_rw & (1 << BIO_RW))
1916 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1917 failrec->last_mirror,
1918 failrec->bio_flags);
1923 * each time an IO finishes, we do a fast check in the IO failure tree
1924 * to see if we need to process or clean up an io_failure_record
1926 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1929 u64 private_failure;
1930 struct io_failure_record *failure;
1934 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1935 (u64)-1, 1, EXTENT_DIRTY)) {
1936 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1937 start, &private_failure);
1939 failure = (struct io_failure_record *)(unsigned long)
1941 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1943 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1945 failure->start + failure->len - 1,
1946 EXTENT_DIRTY | EXTENT_LOCKED,
1955 * when reads are done, we need to check csums to verify the data is correct
1956 * if there's a match, we allow the bio to finish. If not, we go through
1957 * the io_failure_record routines to find good copies
1959 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1960 struct extent_state *state)
1962 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1963 struct inode *inode = page->mapping->host;
1964 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1966 u64 private = ~(u32)0;
1968 struct btrfs_root *root = BTRFS_I(inode)->root;
1971 if (PageChecked(page)) {
1972 ClearPageChecked(page);
1976 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1979 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1980 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1981 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1986 if (state && state->start == start) {
1987 private = state->private;
1990 ret = get_state_private(io_tree, start, &private);
1992 kaddr = kmap_atomic(page, KM_USER0);
1996 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1997 btrfs_csum_final(csum, (char *)&csum);
1998 if (csum != private)
2001 kunmap_atomic(kaddr, KM_USER0);
2003 /* if the io failure tree for this inode is non-empty,
2004 * check to see if we've recovered from a failed IO
2006 btrfs_clean_io_failures(inode, start);
2010 if (printk_ratelimit()) {
2011 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
2012 "private %llu\n", page->mapping->host->i_ino,
2013 (unsigned long long)start, csum,
2014 (unsigned long long)private);
2016 memset(kaddr + offset, 1, end - start + 1);
2017 flush_dcache_page(page);
2018 kunmap_atomic(kaddr, KM_USER0);
2025 * This creates an orphan entry for the given inode in case something goes
2026 * wrong in the middle of an unlink/truncate.
2028 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2030 struct btrfs_root *root = BTRFS_I(inode)->root;
2033 spin_lock(&root->list_lock);
2035 /* already on the orphan list, we're good */
2036 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2037 spin_unlock(&root->list_lock);
2041 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2043 spin_unlock(&root->list_lock);
2046 * insert an orphan item to track this unlinked/truncated file
2048 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2054 * We have done the truncate/delete so we can go ahead and remove the orphan
2055 * item for this particular inode.
2057 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2059 struct btrfs_root *root = BTRFS_I(inode)->root;
2062 spin_lock(&root->list_lock);
2064 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2065 spin_unlock(&root->list_lock);
2069 list_del_init(&BTRFS_I(inode)->i_orphan);
2071 spin_unlock(&root->list_lock);
2075 spin_unlock(&root->list_lock);
2077 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2083 * this cleans up any orphans that may be left on the list from the last use
2086 void btrfs_orphan_cleanup(struct btrfs_root *root)
2088 struct btrfs_path *path;
2089 struct extent_buffer *leaf;
2090 struct btrfs_item *item;
2091 struct btrfs_key key, found_key;
2092 struct btrfs_trans_handle *trans;
2093 struct inode *inode;
2094 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2096 if (!xchg(&root->clean_orphans, 0))
2099 path = btrfs_alloc_path();
2103 key.objectid = BTRFS_ORPHAN_OBJECTID;
2104 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2105 key.offset = (u64)-1;
2108 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2110 printk(KERN_ERR "Error searching slot for orphan: %d"
2116 * if ret == 0 means we found what we were searching for, which
2117 * is weird, but possible, so only screw with path if we didnt
2118 * find the key and see if we have stuff that matches
2121 if (path->slots[0] == 0)
2126 /* pull out the item */
2127 leaf = path->nodes[0];
2128 item = btrfs_item_nr(leaf, path->slots[0]);
2129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2131 /* make sure the item matches what we want */
2132 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2134 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2137 /* release the path since we're done with it */
2138 btrfs_release_path(root, path);
2141 * this is where we are basically btrfs_lookup, without the
2142 * crossing root thing. we store the inode number in the
2143 * offset of the orphan item.
2145 found_key.objectid = found_key.offset;
2146 found_key.type = BTRFS_INODE_ITEM_KEY;
2147 found_key.offset = 0;
2148 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2153 * add this inode to the orphan list so btrfs_orphan_del does
2154 * the proper thing when we hit it
2156 spin_lock(&root->list_lock);
2157 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2158 spin_unlock(&root->list_lock);
2161 * if this is a bad inode, means we actually succeeded in
2162 * removing the inode, but not the orphan record, which means
2163 * we need to manually delete the orphan since iput will just
2164 * do a destroy_inode
2166 if (is_bad_inode(inode)) {
2167 trans = btrfs_start_transaction(root, 1);
2168 btrfs_orphan_del(trans, inode);
2169 btrfs_end_transaction(trans, root);
2174 /* if we have links, this was a truncate, lets do that */
2175 if (inode->i_nlink) {
2177 btrfs_truncate(inode);
2182 /* this will do delete_inode and everything for us */
2187 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2189 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2191 btrfs_free_path(path);
2195 * very simple check to peek ahead in the leaf looking for xattrs. If we
2196 * don't find any xattrs, we know there can't be any acls.
2198 * slot is the slot the inode is in, objectid is the objectid of the inode
2200 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2201 int slot, u64 objectid)
2203 u32 nritems = btrfs_header_nritems(leaf);
2204 struct btrfs_key found_key;
2208 while (slot < nritems) {
2209 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2211 /* we found a different objectid, there must not be acls */
2212 if (found_key.objectid != objectid)
2215 /* we found an xattr, assume we've got an acl */
2216 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2220 * we found a key greater than an xattr key, there can't
2221 * be any acls later on
2223 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2230 * it goes inode, inode backrefs, xattrs, extents,
2231 * so if there are a ton of hard links to an inode there can
2232 * be a lot of backrefs. Don't waste time searching too hard,
2233 * this is just an optimization
2238 /* we hit the end of the leaf before we found an xattr or
2239 * something larger than an xattr. We have to assume the inode
2246 * read an inode from the btree into the in-memory inode
2248 static void btrfs_read_locked_inode(struct inode *inode)
2250 struct btrfs_path *path;
2251 struct extent_buffer *leaf;
2252 struct btrfs_inode_item *inode_item;
2253 struct btrfs_timespec *tspec;
2254 struct btrfs_root *root = BTRFS_I(inode)->root;
2255 struct btrfs_key location;
2257 u64 alloc_group_block;
2261 path = btrfs_alloc_path();
2263 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2265 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2269 leaf = path->nodes[0];
2270 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2271 struct btrfs_inode_item);
2273 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2274 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2275 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2276 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2277 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2279 tspec = btrfs_inode_atime(inode_item);
2280 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2281 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2283 tspec = btrfs_inode_mtime(inode_item);
2284 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2285 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2287 tspec = btrfs_inode_ctime(inode_item);
2288 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2289 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2291 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2292 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2293 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2294 inode->i_generation = BTRFS_I(inode)->generation;
2296 rdev = btrfs_inode_rdev(leaf, inode_item);
2298 BTRFS_I(inode)->index_cnt = (u64)-1;
2299 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2301 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2304 * try to precache a NULL acl entry for files that don't have
2305 * any xattrs or acls
2307 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
2309 cache_no_acl(inode);
2311 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2312 alloc_group_block, 0);
2313 btrfs_free_path(path);
2316 switch (inode->i_mode & S_IFMT) {
2318 inode->i_mapping->a_ops = &btrfs_aops;
2319 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2320 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2321 inode->i_fop = &btrfs_file_operations;
2322 inode->i_op = &btrfs_file_inode_operations;
2325 inode->i_fop = &btrfs_dir_file_operations;
2326 if (root == root->fs_info->tree_root)
2327 inode->i_op = &btrfs_dir_ro_inode_operations;
2329 inode->i_op = &btrfs_dir_inode_operations;
2332 inode->i_op = &btrfs_symlink_inode_operations;
2333 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2334 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2337 inode->i_op = &btrfs_special_inode_operations;
2338 init_special_inode(inode, inode->i_mode, rdev);
2342 btrfs_update_iflags(inode);
2346 btrfs_free_path(path);
2347 make_bad_inode(inode);
2351 * given a leaf and an inode, copy the inode fields into the leaf
2353 static void fill_inode_item(struct btrfs_trans_handle *trans,
2354 struct extent_buffer *leaf,
2355 struct btrfs_inode_item *item,
2356 struct inode *inode)
2358 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2359 btrfs_set_inode_gid(leaf, item, inode->i_gid);
2360 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2361 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2362 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2364 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2365 inode->i_atime.tv_sec);
2366 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2367 inode->i_atime.tv_nsec);
2369 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2370 inode->i_mtime.tv_sec);
2371 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2372 inode->i_mtime.tv_nsec);
2374 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2375 inode->i_ctime.tv_sec);
2376 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2377 inode->i_ctime.tv_nsec);
2379 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2380 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2381 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2382 btrfs_set_inode_transid(leaf, item, trans->transid);
2383 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2384 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2385 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2389 * copy everything in the in-memory inode into the btree.
2391 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root, struct inode *inode)
2394 struct btrfs_inode_item *inode_item;
2395 struct btrfs_path *path;
2396 struct extent_buffer *leaf;
2399 path = btrfs_alloc_path();
2401 path->leave_spinning = 1;
2402 ret = btrfs_lookup_inode(trans, root, path,
2403 &BTRFS_I(inode)->location, 1);
2410 btrfs_unlock_up_safe(path, 1);
2411 leaf = path->nodes[0];
2412 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2413 struct btrfs_inode_item);
2415 fill_inode_item(trans, leaf, inode_item, inode);
2416 btrfs_mark_buffer_dirty(leaf);
2417 btrfs_set_inode_last_trans(trans, inode);
2420 btrfs_free_path(path);
2426 * unlink helper that gets used here in inode.c and in the tree logging
2427 * recovery code. It remove a link in a directory with a given name, and
2428 * also drops the back refs in the inode to the directory
2430 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2431 struct btrfs_root *root,
2432 struct inode *dir, struct inode *inode,
2433 const char *name, int name_len)
2435 struct btrfs_path *path;
2437 struct extent_buffer *leaf;
2438 struct btrfs_dir_item *di;
2439 struct btrfs_key key;
2442 path = btrfs_alloc_path();
2448 path->leave_spinning = 1;
2449 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2450 name, name_len, -1);
2459 leaf = path->nodes[0];
2460 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2461 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2464 btrfs_release_path(root, path);
2466 ret = btrfs_del_inode_ref(trans, root, name, name_len,
2468 dir->i_ino, &index);
2470 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2471 "inode %lu parent %lu\n", name_len, name,
2472 inode->i_ino, dir->i_ino);
2476 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2477 index, name, name_len, -1);
2486 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2487 btrfs_release_path(root, path);
2489 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2491 BUG_ON(ret != 0 && ret != -ENOENT);
2493 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2497 btrfs_free_path(path);
2501 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2502 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2503 btrfs_update_inode(trans, root, dir);
2504 btrfs_drop_nlink(inode);
2505 ret = btrfs_update_inode(trans, root, inode);
2510 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2512 struct btrfs_root *root;
2513 struct btrfs_trans_handle *trans;
2514 struct inode *inode = dentry->d_inode;
2516 unsigned long nr = 0;
2518 root = BTRFS_I(dir)->root;
2521 * 5 items for unlink inode
2524 ret = btrfs_reserve_metadata_space(root, 6);
2528 trans = btrfs_start_transaction(root, 1);
2529 if (IS_ERR(trans)) {
2530 btrfs_unreserve_metadata_space(root, 6);
2531 return PTR_ERR(trans);
2534 btrfs_set_trans_block_group(trans, dir);
2536 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2538 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2539 dentry->d_name.name, dentry->d_name.len);
2541 if (inode->i_nlink == 0)
2542 ret = btrfs_orphan_add(trans, inode);
2544 nr = trans->blocks_used;
2546 btrfs_end_transaction_throttle(trans, root);
2547 btrfs_unreserve_metadata_space(root, 6);
2548 btrfs_btree_balance_dirty(root, nr);
2552 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2553 struct btrfs_root *root,
2554 struct inode *dir, u64 objectid,
2555 const char *name, int name_len)
2557 struct btrfs_path *path;
2558 struct extent_buffer *leaf;
2559 struct btrfs_dir_item *di;
2560 struct btrfs_key key;
2564 path = btrfs_alloc_path();
2568 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2569 name, name_len, -1);
2570 BUG_ON(!di || IS_ERR(di));
2572 leaf = path->nodes[0];
2573 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2574 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2575 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2577 btrfs_release_path(root, path);
2579 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2580 objectid, root->root_key.objectid,
2581 dir->i_ino, &index, name, name_len);
2583 BUG_ON(ret != -ENOENT);
2584 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2586 BUG_ON(!di || IS_ERR(di));
2588 leaf = path->nodes[0];
2589 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2590 btrfs_release_path(root, path);
2594 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2595 index, name, name_len, -1);
2596 BUG_ON(!di || IS_ERR(di));
2598 leaf = path->nodes[0];
2599 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2600 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2601 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2603 btrfs_release_path(root, path);
2605 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2606 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2607 ret = btrfs_update_inode(trans, root, dir);
2609 dir->i_sb->s_dirt = 1;
2611 btrfs_free_path(path);
2615 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2617 struct inode *inode = dentry->d_inode;
2620 struct btrfs_root *root = BTRFS_I(dir)->root;
2621 struct btrfs_trans_handle *trans;
2622 unsigned long nr = 0;
2624 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2625 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2628 ret = btrfs_reserve_metadata_space(root, 5);
2632 trans = btrfs_start_transaction(root, 1);
2633 if (IS_ERR(trans)) {
2634 btrfs_unreserve_metadata_space(root, 5);
2635 return PTR_ERR(trans);
2638 btrfs_set_trans_block_group(trans, dir);
2640 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2641 err = btrfs_unlink_subvol(trans, root, dir,
2642 BTRFS_I(inode)->location.objectid,
2643 dentry->d_name.name,
2644 dentry->d_name.len);
2648 err = btrfs_orphan_add(trans, inode);
2652 /* now the directory is empty */
2653 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2654 dentry->d_name.name, dentry->d_name.len);
2656 btrfs_i_size_write(inode, 0);
2658 nr = trans->blocks_used;
2659 ret = btrfs_end_transaction_throttle(trans, root);
2660 btrfs_unreserve_metadata_space(root, 5);
2661 btrfs_btree_balance_dirty(root, nr);
2670 * when truncating bytes in a file, it is possible to avoid reading
2671 * the leaves that contain only checksum items. This can be the
2672 * majority of the IO required to delete a large file, but it must
2673 * be done carefully.
2675 * The keys in the level just above the leaves are checked to make sure
2676 * the lowest key in a given leaf is a csum key, and starts at an offset
2677 * after the new size.
2679 * Then the key for the next leaf is checked to make sure it also has
2680 * a checksum item for the same file. If it does, we know our target leaf
2681 * contains only checksum items, and it can be safely freed without reading
2684 * This is just an optimization targeted at large files. It may do
2685 * nothing. It will return 0 unless things went badly.
2687 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2688 struct btrfs_root *root,
2689 struct btrfs_path *path,
2690 struct inode *inode, u64 new_size)
2692 struct btrfs_key key;
2695 struct btrfs_key found_key;
2696 struct btrfs_key other_key;
2697 struct btrfs_leaf_ref *ref;
2701 path->lowest_level = 1;
2702 key.objectid = inode->i_ino;
2703 key.type = BTRFS_CSUM_ITEM_KEY;
2704 key.offset = new_size;
2706 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2710 if (path->nodes[1] == NULL) {
2715 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2716 nritems = btrfs_header_nritems(path->nodes[1]);
2721 if (path->slots[1] >= nritems)
2724 /* did we find a key greater than anything we want to delete? */
2725 if (found_key.objectid > inode->i_ino ||
2726 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2729 /* we check the next key in the node to make sure the leave contains
2730 * only checksum items. This comparison doesn't work if our
2731 * leaf is the last one in the node
2733 if (path->slots[1] + 1 >= nritems) {
2735 /* search forward from the last key in the node, this
2736 * will bring us into the next node in the tree
2738 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2740 /* unlikely, but we inc below, so check to be safe */
2741 if (found_key.offset == (u64)-1)
2744 /* search_forward needs a path with locks held, do the
2745 * search again for the original key. It is possible
2746 * this will race with a balance and return a path that
2747 * we could modify, but this drop is just an optimization
2748 * and is allowed to miss some leaves.
2750 btrfs_release_path(root, path);
2753 /* setup a max key for search_forward */
2754 other_key.offset = (u64)-1;
2755 other_key.type = key.type;
2756 other_key.objectid = key.objectid;
2758 path->keep_locks = 1;
2759 ret = btrfs_search_forward(root, &found_key, &other_key,
2761 path->keep_locks = 0;
2762 if (ret || found_key.objectid != key.objectid ||
2763 found_key.type != key.type) {
2768 key.offset = found_key.offset;
2769 btrfs_release_path(root, path);
2774 /* we know there's one more slot after us in the tree,
2775 * read that key so we can verify it is also a checksum item
2777 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2779 if (found_key.objectid < inode->i_ino)
2782 if (found_key.type != key.type || found_key.offset < new_size)
2786 * if the key for the next leaf isn't a csum key from this objectid,
2787 * we can't be sure there aren't good items inside this leaf.
2790 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2793 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2794 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
2796 * it is safe to delete this leaf, it contains only
2797 * csum items from this inode at an offset >= new_size
2799 ret = btrfs_del_leaf(trans, root, path, leaf_start);
2802 if (root->ref_cows && leaf_gen < trans->transid) {
2803 ref = btrfs_alloc_leaf_ref(root, 0);
2805 ref->root_gen = root->root_key.offset;
2806 ref->bytenr = leaf_start;
2808 ref->generation = leaf_gen;
2811 btrfs_sort_leaf_ref(ref);
2813 ret = btrfs_add_leaf_ref(root, ref, 0);
2815 btrfs_free_leaf_ref(root, ref);
2821 btrfs_release_path(root, path);
2823 if (other_key.objectid == inode->i_ino &&
2824 other_key.type == key.type && other_key.offset > key.offset) {
2825 key.offset = other_key.offset;
2831 /* fixup any changes we've made to the path */
2832 path->lowest_level = 0;
2833 path->keep_locks = 0;
2834 btrfs_release_path(root, path);
2841 * this can truncate away extent items, csum items and directory items.
2842 * It starts at a high offset and removes keys until it can't find
2843 * any higher than new_size
2845 * csum items that cross the new i_size are truncated to the new size
2848 * min_type is the minimum key type to truncate down to. If set to 0, this
2849 * will kill all the items on this inode, including the INODE_ITEM_KEY.
2851 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2852 struct btrfs_root *root,
2853 struct inode *inode,
2854 u64 new_size, u32 min_type)
2857 struct btrfs_path *path;
2858 struct btrfs_key key;
2859 struct btrfs_key found_key;
2860 u32 found_type = (u8)-1;
2861 struct extent_buffer *leaf;
2862 struct btrfs_file_extent_item *fi;
2863 u64 extent_start = 0;
2864 u64 extent_num_bytes = 0;
2865 u64 extent_offset = 0;
2869 int pending_del_nr = 0;
2870 int pending_del_slot = 0;
2871 int extent_type = -1;
2873 u64 mask = root->sectorsize - 1;
2876 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2877 path = btrfs_alloc_path();
2881 /* FIXME, add redo link to tree so we don't leak on crash */
2882 key.objectid = inode->i_ino;
2883 key.offset = (u64)-1;
2887 path->leave_spinning = 1;
2888 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2893 /* there are no items in the tree for us to truncate, we're
2896 if (path->slots[0] == 0) {
2905 leaf = path->nodes[0];
2906 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2907 found_type = btrfs_key_type(&found_key);
2910 if (found_key.objectid != inode->i_ino)
2913 if (found_type < min_type)
2916 item_end = found_key.offset;
2917 if (found_type == BTRFS_EXTENT_DATA_KEY) {
2918 fi = btrfs_item_ptr(leaf, path->slots[0],
2919 struct btrfs_file_extent_item);
2920 extent_type = btrfs_file_extent_type(leaf, fi);
2921 encoding = btrfs_file_extent_compression(leaf, fi);
2922 encoding |= btrfs_file_extent_encryption(leaf, fi);
2923 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2925 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2927 btrfs_file_extent_num_bytes(leaf, fi);
2928 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2929 item_end += btrfs_file_extent_inline_len(leaf,
2934 if (item_end < new_size) {
2935 if (found_type == BTRFS_DIR_ITEM_KEY)
2936 found_type = BTRFS_INODE_ITEM_KEY;
2937 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
2938 found_type = BTRFS_EXTENT_DATA_KEY;
2939 else if (found_type == BTRFS_EXTENT_DATA_KEY)
2940 found_type = BTRFS_XATTR_ITEM_KEY;
2941 else if (found_type == BTRFS_XATTR_ITEM_KEY)
2942 found_type = BTRFS_INODE_REF_KEY;
2943 else if (found_type)
2947 btrfs_set_key_type(&key, found_type);
2950 if (found_key.offset >= new_size)
2956 /* FIXME, shrink the extent if the ref count is only 1 */
2957 if (found_type != BTRFS_EXTENT_DATA_KEY)
2960 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2962 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
2963 if (!del_item && !encoding) {
2964 u64 orig_num_bytes =
2965 btrfs_file_extent_num_bytes(leaf, fi);
2966 extent_num_bytes = new_size -
2967 found_key.offset + root->sectorsize - 1;
2968 extent_num_bytes = extent_num_bytes &
2969 ~((u64)root->sectorsize - 1);
2970 btrfs_set_file_extent_num_bytes(leaf, fi,
2972 num_dec = (orig_num_bytes -
2974 if (root->ref_cows && extent_start != 0)
2975 inode_sub_bytes(inode, num_dec);
2976 btrfs_mark_buffer_dirty(leaf);
2979 btrfs_file_extent_disk_num_bytes(leaf,
2981 extent_offset = found_key.offset -
2982 btrfs_file_extent_offset(leaf, fi);
2984 /* FIXME blocksize != 4096 */
2985 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
2986 if (extent_start != 0) {
2989 inode_sub_bytes(inode, num_dec);
2992 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2994 * we can't truncate inline items that have had
2998 btrfs_file_extent_compression(leaf, fi) == 0 &&
2999 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3000 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3001 u32 size = new_size - found_key.offset;
3003 if (root->ref_cows) {
3004 inode_sub_bytes(inode, item_end + 1 -
3008 btrfs_file_extent_calc_inline_size(size);
3009 ret = btrfs_truncate_item(trans, root, path,
3012 } else if (root->ref_cows) {
3013 inode_sub_bytes(inode, item_end + 1 -
3019 if (!pending_del_nr) {
3020 /* no pending yet, add ourselves */
3021 pending_del_slot = path->slots[0];
3023 } else if (pending_del_nr &&
3024 path->slots[0] + 1 == pending_del_slot) {
3025 /* hop on the pending chunk */
3027 pending_del_slot = path->slots[0];
3034 if (found_extent && root->ref_cows) {
3035 btrfs_set_path_blocking(path);
3036 ret = btrfs_free_extent(trans, root, extent_start,
3037 extent_num_bytes, 0,
3038 btrfs_header_owner(leaf),
3039 inode->i_ino, extent_offset);
3043 if (path->slots[0] == 0) {
3046 btrfs_release_path(root, path);
3047 if (found_type == BTRFS_INODE_ITEM_KEY)
3053 if (pending_del_nr &&
3054 path->slots[0] + 1 != pending_del_slot) {
3055 struct btrfs_key debug;
3057 btrfs_item_key_to_cpu(path->nodes[0], &debug,
3059 ret = btrfs_del_items(trans, root, path,
3064 btrfs_release_path(root, path);
3065 if (found_type == BTRFS_INODE_ITEM_KEY)
3072 if (pending_del_nr) {
3073 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3076 btrfs_free_path(path);
3081 * taken from block_truncate_page, but does cow as it zeros out
3082 * any bytes left in the last page in the file.
3084 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3086 struct inode *inode = mapping->host;
3087 struct btrfs_root *root = BTRFS_I(inode)->root;
3088 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3089 struct btrfs_ordered_extent *ordered;
3091 u32 blocksize = root->sectorsize;
3092 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3093 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3099 if ((offset & (blocksize - 1)) == 0)
3101 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3105 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3111 page = grab_cache_page(mapping, index);
3113 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3114 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3118 page_start = page_offset(page);
3119 page_end = page_start + PAGE_CACHE_SIZE - 1;
3121 if (!PageUptodate(page)) {
3122 ret = btrfs_readpage(NULL, page);
3124 if (page->mapping != mapping) {
3126 page_cache_release(page);
3129 if (!PageUptodate(page)) {
3134 wait_on_page_writeback(page);
3136 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3137 set_page_extent_mapped(page);
3139 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3141 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3143 page_cache_release(page);
3144 btrfs_start_ordered_extent(inode, ordered, 1);
3145 btrfs_put_ordered_extent(ordered);
3149 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3150 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3153 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3155 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3160 if (offset != PAGE_CACHE_SIZE) {
3162 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3163 flush_dcache_page(page);
3166 ClearPageChecked(page);
3167 set_page_dirty(page);
3168 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3172 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3173 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3175 page_cache_release(page);
3180 int btrfs_cont_expand(struct inode *inode, loff_t size)
3182 struct btrfs_trans_handle *trans;
3183 struct btrfs_root *root = BTRFS_I(inode)->root;
3184 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3185 struct extent_map *em;
3186 u64 mask = root->sectorsize - 1;
3187 u64 hole_start = (inode->i_size + mask) & ~mask;
3188 u64 block_end = (size + mask) & ~mask;
3194 if (size <= hole_start)
3197 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3202 struct btrfs_ordered_extent *ordered;
3203 btrfs_wait_ordered_range(inode, hole_start,
3204 block_end - hole_start);
3205 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3206 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3209 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3210 btrfs_put_ordered_extent(ordered);
3213 trans = btrfs_start_transaction(root, 1);
3214 btrfs_set_trans_block_group(trans, inode);
3216 cur_offset = hole_start;
3218 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3219 block_end - cur_offset, 0);
3220 BUG_ON(IS_ERR(em) || !em);
3221 last_byte = min(extent_map_end(em), block_end);
3222 last_byte = (last_byte + mask) & ~mask;
3223 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
3225 hole_size = last_byte - cur_offset;
3226 err = btrfs_drop_extents(trans, inode, cur_offset,
3227 cur_offset + hole_size,
3232 err = btrfs_reserve_metadata_space(root, 1);
3236 err = btrfs_insert_file_extent(trans, root,
3237 inode->i_ino, cur_offset, 0,
3238 0, hole_size, 0, hole_size,
3240 btrfs_drop_extent_cache(inode, hole_start,
3242 btrfs_unreserve_metadata_space(root, 1);
3244 free_extent_map(em);
3245 cur_offset = last_byte;
3246 if (err || cur_offset >= block_end)
3250 btrfs_end_transaction(trans, root);
3251 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3255 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3257 struct inode *inode = dentry->d_inode;
3260 err = inode_change_ok(inode, attr);
3264 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3265 if (attr->ia_size > inode->i_size) {
3266 err = btrfs_cont_expand(inode, attr->ia_size);
3269 } else if (inode->i_size > 0 &&
3270 attr->ia_size == 0) {
3272 /* we're truncating a file that used to have good
3273 * data down to zero. Make sure it gets into
3274 * the ordered flush list so that any new writes
3275 * get down to disk quickly.
3277 BTRFS_I(inode)->ordered_data_close = 1;
3281 err = inode_setattr(inode, attr);
3283 if (!err && ((attr->ia_valid & ATTR_MODE)))
3284 err = btrfs_acl_chmod(inode);
3288 void btrfs_delete_inode(struct inode *inode)
3290 struct btrfs_trans_handle *trans;
3291 struct btrfs_root *root = BTRFS_I(inode)->root;
3295 truncate_inode_pages(&inode->i_data, 0);
3296 if (is_bad_inode(inode)) {
3297 btrfs_orphan_del(NULL, inode);
3300 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3302 if (root->fs_info->log_root_recovering) {
3303 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3307 if (inode->i_nlink > 0) {
3308 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3312 btrfs_i_size_write(inode, 0);
3313 trans = btrfs_join_transaction(root, 1);
3315 btrfs_set_trans_block_group(trans, inode);
3316 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
3318 btrfs_orphan_del(NULL, inode);
3319 goto no_delete_lock;
3322 btrfs_orphan_del(trans, inode);
3324 nr = trans->blocks_used;
3327 btrfs_end_transaction(trans, root);
3328 btrfs_btree_balance_dirty(root, nr);
3332 nr = trans->blocks_used;
3333 btrfs_end_transaction(trans, root);
3334 btrfs_btree_balance_dirty(root, nr);
3340 * this returns the key found in the dir entry in the location pointer.
3341 * If no dir entries were found, location->objectid is 0.
3343 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3344 struct btrfs_key *location)
3346 const char *name = dentry->d_name.name;
3347 int namelen = dentry->d_name.len;
3348 struct btrfs_dir_item *di;
3349 struct btrfs_path *path;
3350 struct btrfs_root *root = BTRFS_I(dir)->root;
3353 path = btrfs_alloc_path();
3356 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3361 if (!di || IS_ERR(di))
3364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3366 btrfs_free_path(path);
3369 location->objectid = 0;
3374 * when we hit a tree root in a directory, the btrfs part of the inode
3375 * needs to be changed to reflect the root directory of the tree root. This
3376 * is kind of like crossing a mount point.
3378 static int fixup_tree_root_location(struct btrfs_root *root,
3380 struct dentry *dentry,
3381 struct btrfs_key *location,
3382 struct btrfs_root **sub_root)
3384 struct btrfs_path *path;
3385 struct btrfs_root *new_root;
3386 struct btrfs_root_ref *ref;
3387 struct extent_buffer *leaf;
3391 path = btrfs_alloc_path();
3398 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3399 BTRFS_I(dir)->root->root_key.objectid,
3400 location->objectid);
3407 leaf = path->nodes[0];
3408 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3409 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3410 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3413 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3414 (unsigned long)(ref + 1),
3415 dentry->d_name.len);
3419 btrfs_release_path(root->fs_info->tree_root, path);
3421 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3422 if (IS_ERR(new_root)) {
3423 err = PTR_ERR(new_root);
3427 if (btrfs_root_refs(&new_root->root_item) == 0) {
3432 *sub_root = new_root;
3433 location->objectid = btrfs_root_dirid(&new_root->root_item);
3434 location->type = BTRFS_INODE_ITEM_KEY;
3435 location->offset = 0;
3438 btrfs_free_path(path);
3442 static void inode_tree_add(struct inode *inode)
3444 struct btrfs_root *root = BTRFS_I(inode)->root;
3445 struct btrfs_inode *entry;
3447 struct rb_node *parent;
3449 p = &root->inode_tree.rb_node;
3452 if (hlist_unhashed(&inode->i_hash))
3455 spin_lock(&root->inode_lock);
3458 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3460 if (inode->i_ino < entry->vfs_inode.i_ino)
3461 p = &parent->rb_left;
3462 else if (inode->i_ino > entry->vfs_inode.i_ino)
3463 p = &parent->rb_right;
3465 WARN_ON(!(entry->vfs_inode.i_state &
3466 (I_WILL_FREE | I_FREEING | I_CLEAR)));
3467 rb_erase(parent, &root->inode_tree);
3468 RB_CLEAR_NODE(parent);
3469 spin_unlock(&root->inode_lock);
3473 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3474 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3475 spin_unlock(&root->inode_lock);
3478 static void inode_tree_del(struct inode *inode)
3480 struct btrfs_root *root = BTRFS_I(inode)->root;
3483 spin_lock(&root->inode_lock);
3484 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3485 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3486 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3487 empty = RB_EMPTY_ROOT(&root->inode_tree);
3489 spin_unlock(&root->inode_lock);
3491 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3492 synchronize_srcu(&root->fs_info->subvol_srcu);
3493 spin_lock(&root->inode_lock);
3494 empty = RB_EMPTY_ROOT(&root->inode_tree);
3495 spin_unlock(&root->inode_lock);
3497 btrfs_add_dead_root(root);
3501 int btrfs_invalidate_inodes(struct btrfs_root *root)
3503 struct rb_node *node;
3504 struct rb_node *prev;
3505 struct btrfs_inode *entry;
3506 struct inode *inode;
3509 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3511 spin_lock(&root->inode_lock);
3513 node = root->inode_tree.rb_node;
3517 entry = rb_entry(node, struct btrfs_inode, rb_node);
3519 if (objectid < entry->vfs_inode.i_ino)
3520 node = node->rb_left;
3521 else if (objectid > entry->vfs_inode.i_ino)
3522 node = node->rb_right;
3528 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3529 if (objectid <= entry->vfs_inode.i_ino) {
3533 prev = rb_next(prev);
3537 entry = rb_entry(node, struct btrfs_inode, rb_node);
3538 objectid = entry->vfs_inode.i_ino + 1;
3539 inode = igrab(&entry->vfs_inode);
3541 spin_unlock(&root->inode_lock);
3542 if (atomic_read(&inode->i_count) > 1)
3543 d_prune_aliases(inode);
3545 * btrfs_drop_inode will remove it from
3546 * the inode cache when its usage count
3551 spin_lock(&root->inode_lock);
3555 if (cond_resched_lock(&root->inode_lock))
3558 node = rb_next(node);
3560 spin_unlock(&root->inode_lock);
3564 static noinline void init_btrfs_i(struct inode *inode)
3566 struct btrfs_inode *bi = BTRFS_I(inode);
3571 bi->last_sub_trans = 0;
3572 bi->logged_trans = 0;
3573 bi->delalloc_bytes = 0;
3574 bi->reserved_bytes = 0;
3575 bi->disk_i_size = 0;
3577 bi->index_cnt = (u64)-1;
3578 bi->last_unlink_trans = 0;
3579 bi->ordered_data_close = 0;
3580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3582 inode->i_mapping, GFP_NOFS);
3583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3584 inode->i_mapping, GFP_NOFS);
3585 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3586 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
3587 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3588 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3589 mutex_init(&BTRFS_I(inode)->log_mutex);
3592 static int btrfs_init_locked_inode(struct inode *inode, void *p)
3594 struct btrfs_iget_args *args = p;
3595 inode->i_ino = args->ino;
3596 init_btrfs_i(inode);
3597 BTRFS_I(inode)->root = args->root;
3598 btrfs_set_inode_space_info(args->root, inode);
3602 static int btrfs_find_actor(struct inode *inode, void *opaque)
3604 struct btrfs_iget_args *args = opaque;
3605 return args->ino == inode->i_ino &&
3606 args->root == BTRFS_I(inode)->root;
3609 static struct inode *btrfs_iget_locked(struct super_block *s,
3611 struct btrfs_root *root)
3613 struct inode *inode;
3614 struct btrfs_iget_args args;
3615 args.ino = objectid;
3618 inode = iget5_locked(s, objectid, btrfs_find_actor,
3619 btrfs_init_locked_inode,
3624 /* Get an inode object given its location and corresponding root.
3625 * Returns in *is_new if the inode was read from disk
3627 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3628 struct btrfs_root *root)
3630 struct inode *inode;
3632 inode = btrfs_iget_locked(s, location->objectid, root);
3634 return ERR_PTR(-ENOMEM);
3636 if (inode->i_state & I_NEW) {
3637 BTRFS_I(inode)->root = root;
3638 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3639 btrfs_read_locked_inode(inode);
3641 inode_tree_add(inode);
3642 unlock_new_inode(inode);
3648 static struct inode *new_simple_dir(struct super_block *s,
3649 struct btrfs_key *key,
3650 struct btrfs_root *root)
3652 struct inode *inode = new_inode(s);
3655 return ERR_PTR(-ENOMEM);
3657 init_btrfs_i(inode);
3659 BTRFS_I(inode)->root = root;
3660 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3661 BTRFS_I(inode)->dummy_inode = 1;
3663 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3664 inode->i_op = &simple_dir_inode_operations;
3665 inode->i_fop = &simple_dir_operations;
3666 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3667 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3672 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3674 struct inode *inode;
3675 struct btrfs_root *root = BTRFS_I(dir)->root;
3676 struct btrfs_root *sub_root = root;
3677 struct btrfs_key location;
3681 dentry->d_op = &btrfs_dentry_operations;
3683 if (dentry->d_name.len > BTRFS_NAME_LEN)
3684 return ERR_PTR(-ENAMETOOLONG);
3686 ret = btrfs_inode_by_name(dir, dentry, &location);
3689 return ERR_PTR(ret);
3691 if (location.objectid == 0)
3694 if (location.type == BTRFS_INODE_ITEM_KEY) {
3695 inode = btrfs_iget(dir->i_sb, &location, root);
3699 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3701 index = srcu_read_lock(&root->fs_info->subvol_srcu);
3702 ret = fixup_tree_root_location(root, dir, dentry,
3703 &location, &sub_root);
3706 inode = ERR_PTR(ret);
3708 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3710 inode = btrfs_iget(dir->i_sb, &location, sub_root);
3712 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3714 if (root != sub_root) {
3715 down_read(&root->fs_info->cleanup_work_sem);
3716 if (!(inode->i_sb->s_flags & MS_RDONLY))
3717 btrfs_orphan_cleanup(sub_root);
3718 up_read(&root->fs_info->cleanup_work_sem);
3724 static int btrfs_dentry_delete(struct dentry *dentry)
3726 struct btrfs_root *root;
3728 if (!dentry->d_inode && !IS_ROOT(dentry))
3729 dentry = dentry->d_parent;
3731 if (dentry->d_inode) {
3732 root = BTRFS_I(dentry->d_inode)->root;
3733 if (btrfs_root_refs(&root->root_item) == 0)
3739 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3740 struct nameidata *nd)
3742 struct inode *inode;
3744 inode = btrfs_lookup_dentry(dir, dentry);
3746 return ERR_CAST(inode);
3748 return d_splice_alias(inode, dentry);
3751 static unsigned char btrfs_filetype_table[] = {
3752 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3755 static int btrfs_real_readdir(struct file *filp, void *dirent,
3758 struct inode *inode = filp->f_dentry->d_inode;
3759 struct btrfs_root *root = BTRFS_I(inode)->root;
3760 struct btrfs_item *item;
3761 struct btrfs_dir_item *di;
3762 struct btrfs_key key;
3763 struct btrfs_key found_key;
3764 struct btrfs_path *path;
3767 struct extent_buffer *leaf;
3770 unsigned char d_type;
3775 int key_type = BTRFS_DIR_INDEX_KEY;
3780 /* FIXME, use a real flag for deciding about the key type */
3781 if (root->fs_info->tree_root == root)
3782 key_type = BTRFS_DIR_ITEM_KEY;
3784 /* special case for "." */
3785 if (filp->f_pos == 0) {
3786 over = filldir(dirent, ".", 1,
3793 /* special case for .., just use the back ref */
3794 if (filp->f_pos == 1) {
3795 u64 pino = parent_ino(filp->f_path.dentry);
3796 over = filldir(dirent, "..", 2,
3802 path = btrfs_alloc_path();
3805 btrfs_set_key_type(&key, key_type);
3806 key.offset = filp->f_pos;
3807 key.objectid = inode->i_ino;
3809 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3815 leaf = path->nodes[0];
3816 nritems = btrfs_header_nritems(leaf);
3817 slot = path->slots[0];
3818 if (advance || slot >= nritems) {
3819 if (slot >= nritems - 1) {
3820 ret = btrfs_next_leaf(root, path);
3823 leaf = path->nodes[0];
3824 nritems = btrfs_header_nritems(leaf);
3825 slot = path->slots[0];
3833 item = btrfs_item_nr(leaf, slot);
3834 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3836 if (found_key.objectid != key.objectid)
3838 if (btrfs_key_type(&found_key) != key_type)
3840 if (found_key.offset < filp->f_pos)
3843 filp->f_pos = found_key.offset;
3845 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3847 di_total = btrfs_item_size(leaf, item);
3849 while (di_cur < di_total) {
3850 struct btrfs_key location;
3852 name_len = btrfs_dir_name_len(leaf, di);
3853 if (name_len <= sizeof(tmp_name)) {
3854 name_ptr = tmp_name;
3856 name_ptr = kmalloc(name_len, GFP_NOFS);
3862 read_extent_buffer(leaf, name_ptr,
3863 (unsigned long)(di + 1), name_len);
3865 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3866 btrfs_dir_item_key_to_cpu(leaf, di, &location);
3868 /* is this a reference to our own snapshot? If so
3871 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3872 location.objectid == root->root_key.objectid) {
3876 over = filldir(dirent, name_ptr, name_len,
3877 found_key.offset, location.objectid,
3881 if (name_ptr != tmp_name)
3886 di_len = btrfs_dir_name_len(leaf, di) +
3887 btrfs_dir_data_len(leaf, di) + sizeof(*di);
3889 di = (struct btrfs_dir_item *)((char *)di + di_len);
3893 /* Reached end of directory/root. Bump pos past the last item. */
3894 if (key_type == BTRFS_DIR_INDEX_KEY)
3895 filp->f_pos = INT_LIMIT(off_t);
3901 btrfs_free_path(path);
3905 int btrfs_write_inode(struct inode *inode, int wait)
3907 struct btrfs_root *root = BTRFS_I(inode)->root;
3908 struct btrfs_trans_handle *trans;
3911 if (root->fs_info->btree_inode == inode)
3915 trans = btrfs_join_transaction(root, 1);
3916 btrfs_set_trans_block_group(trans, inode);
3917 ret = btrfs_commit_transaction(trans, root);
3923 * This is somewhat expensive, updating the tree every time the
3924 * inode changes. But, it is most likely to find the inode in cache.
3925 * FIXME, needs more benchmarking...there are no reasons other than performance
3926 * to keep or drop this code.
3928 void btrfs_dirty_inode(struct inode *inode)
3930 struct btrfs_root *root = BTRFS_I(inode)->root;
3931 struct btrfs_trans_handle *trans;
3933 trans = btrfs_join_transaction(root, 1);
3934 btrfs_set_trans_block_group(trans, inode);
3935 btrfs_update_inode(trans, root, inode);
3936 btrfs_end_transaction(trans, root);
3940 * find the highest existing sequence number in a directory
3941 * and then set the in-memory index_cnt variable to reflect
3942 * free sequence numbers
3944 static int btrfs_set_inode_index_count(struct inode *inode)
3946 struct btrfs_root *root = BTRFS_I(inode)->root;
3947 struct btrfs_key key, found_key;
3948 struct btrfs_path *path;
3949 struct extent_buffer *leaf;
3952 key.objectid = inode->i_ino;
3953 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3954 key.offset = (u64)-1;
3956 path = btrfs_alloc_path();
3960 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3963 /* FIXME: we should be able to handle this */
3969 * MAGIC NUMBER EXPLANATION:
3970 * since we search a directory based on f_pos we have to start at 2
3971 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3972 * else has to start at 2
3974 if (path->slots[0] == 0) {
3975 BTRFS_I(inode)->index_cnt = 2;
3981 leaf = path->nodes[0];
3982 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3984 if (found_key.objectid != inode->i_ino ||
3985 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3986 BTRFS_I(inode)->index_cnt = 2;
3990 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3992 btrfs_free_path(path);
3997 * helper to find a free sequence number in a given directory. This current
3998 * code is very simple, later versions will do smarter things in the btree
4000 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4004 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4005 ret = btrfs_set_inode_index_count(dir);
4010 *index = BTRFS_I(dir)->index_cnt;
4011 BTRFS_I(dir)->index_cnt++;
4016 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4017 struct btrfs_root *root,
4019 const char *name, int name_len,
4020 u64 ref_objectid, u64 objectid,
4021 u64 alloc_hint, int mode, u64 *index)
4023 struct inode *inode;
4024 struct btrfs_inode_item *inode_item;
4025 struct btrfs_key *location;
4026 struct btrfs_path *path;
4027 struct btrfs_inode_ref *ref;
4028 struct btrfs_key key[2];
4034 path = btrfs_alloc_path();
4037 inode = new_inode(root->fs_info->sb);
4039 return ERR_PTR(-ENOMEM);
4042 ret = btrfs_set_inode_index(dir, index);
4045 return ERR_PTR(ret);
4049 * index_cnt is ignored for everything but a dir,
4050 * btrfs_get_inode_index_count has an explanation for the magic
4053 init_btrfs_i(inode);
4054 BTRFS_I(inode)->index_cnt = 2;
4055 BTRFS_I(inode)->root = root;
4056 BTRFS_I(inode)->generation = trans->transid;
4057 btrfs_set_inode_space_info(root, inode);
4063 BTRFS_I(inode)->block_group =
4064 btrfs_find_block_group(root, 0, alloc_hint, owner);
4066 key[0].objectid = objectid;
4067 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4070 key[1].objectid = objectid;
4071 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4072 key[1].offset = ref_objectid;
4074 sizes[0] = sizeof(struct btrfs_inode_item);
4075 sizes[1] = name_len + sizeof(*ref);
4077 path->leave_spinning = 1;
4078 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4082 inode->i_uid = current_fsuid();
4084 if (dir && (dir->i_mode & S_ISGID)) {
4085 inode->i_gid = dir->i_gid;
4089 inode->i_gid = current_fsgid();
4091 inode->i_mode = mode;
4092 inode->i_ino = objectid;
4093 inode_set_bytes(inode, 0);
4094 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4095 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4096 struct btrfs_inode_item);
4097 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4099 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4100 struct btrfs_inode_ref);
4101 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4102 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4103 ptr = (unsigned long)(ref + 1);
4104 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4106 btrfs_mark_buffer_dirty(path->nodes[0]);
4107 btrfs_free_path(path);
4109 location = &BTRFS_I(inode)->location;
4110 location->objectid = objectid;
4111 location->offset = 0;
4112 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4114 btrfs_inherit_iflags(inode, dir);
4116 if ((mode & S_IFREG)) {
4117 if (btrfs_test_opt(root, NODATASUM))
4118 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4119 if (btrfs_test_opt(root, NODATACOW))
4120 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4123 insert_inode_hash(inode);
4124 inode_tree_add(inode);
4128 BTRFS_I(dir)->index_cnt--;
4129 btrfs_free_path(path);
4131 return ERR_PTR(ret);
4134 static inline u8 btrfs_inode_type(struct inode *inode)
4136 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4140 * utility function to add 'inode' into 'parent_inode' with
4141 * a give name and a given sequence number.
4142 * if 'add_backref' is true, also insert a backref from the
4143 * inode to the parent directory.
4145 int btrfs_add_link(struct btrfs_trans_handle *trans,
4146 struct inode *parent_inode, struct inode *inode,
4147 const char *name, int name_len, int add_backref, u64 index)
4150 struct btrfs_key key;
4151 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4153 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4154 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4156 key.objectid = inode->i_ino;
4157 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4161 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4162 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4163 key.objectid, root->root_key.objectid,
4164 parent_inode->i_ino,
4165 index, name, name_len);
4166 } else if (add_backref) {
4167 ret = btrfs_insert_inode_ref(trans, root,
4168 name, name_len, inode->i_ino,
4169 parent_inode->i_ino, index);
4173 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4174 parent_inode->i_ino, &key,
4175 btrfs_inode_type(inode), index);
4178 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4180 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4181 ret = btrfs_update_inode(trans, root, parent_inode);
4186 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4187 struct dentry *dentry, struct inode *inode,
4188 int backref, u64 index)
4190 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4191 inode, dentry->d_name.name,
4192 dentry->d_name.len, backref, index);
4194 d_instantiate(dentry, inode);
4202 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4203 int mode, dev_t rdev)
4205 struct btrfs_trans_handle *trans;
4206 struct btrfs_root *root = BTRFS_I(dir)->root;
4207 struct inode *inode = NULL;
4211 unsigned long nr = 0;
4214 if (!new_valid_dev(rdev))
4218 * 2 for inode item and ref
4220 * 1 for xattr if selinux is on
4222 err = btrfs_reserve_metadata_space(root, 5);
4226 trans = btrfs_start_transaction(root, 1);
4229 btrfs_set_trans_block_group(trans, dir);
4231 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4237 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4239 dentry->d_parent->d_inode->i_ino, objectid,
4240 BTRFS_I(dir)->block_group, mode, &index);
4241 err = PTR_ERR(inode);
4245 err = btrfs_init_inode_security(inode, dir);
4251 btrfs_set_trans_block_group(trans, inode);
4252 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4256 inode->i_op = &btrfs_special_inode_operations;
4257 init_special_inode(inode, inode->i_mode, rdev);
4258 btrfs_update_inode(trans, root, inode);
4260 btrfs_update_inode_block_group(trans, inode);
4261 btrfs_update_inode_block_group(trans, dir);
4263 nr = trans->blocks_used;
4264 btrfs_end_transaction_throttle(trans, root);
4266 btrfs_unreserve_metadata_space(root, 5);
4268 inode_dec_link_count(inode);
4271 btrfs_btree_balance_dirty(root, nr);
4275 static int btrfs_create(struct inode *dir, struct dentry *dentry,
4276 int mode, struct nameidata *nd)
4278 struct btrfs_trans_handle *trans;
4279 struct btrfs_root *root = BTRFS_I(dir)->root;
4280 struct inode *inode = NULL;
4283 unsigned long nr = 0;
4288 * 2 for inode item and ref
4290 * 1 for xattr if selinux is on
4292 err = btrfs_reserve_metadata_space(root, 5);
4296 trans = btrfs_start_transaction(root, 1);
4299 btrfs_set_trans_block_group(trans, dir);
4301 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4307 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4309 dentry->d_parent->d_inode->i_ino,
4310 objectid, BTRFS_I(dir)->block_group, mode,
4312 err = PTR_ERR(inode);
4316 err = btrfs_init_inode_security(inode, dir);
4322 btrfs_set_trans_block_group(trans, inode);
4323 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4327 inode->i_mapping->a_ops = &btrfs_aops;
4328 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4329 inode->i_fop = &btrfs_file_operations;
4330 inode->i_op = &btrfs_file_inode_operations;
4331 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4333 btrfs_update_inode_block_group(trans, inode);
4334 btrfs_update_inode_block_group(trans, dir);
4336 nr = trans->blocks_used;
4337 btrfs_end_transaction_throttle(trans, root);
4339 btrfs_unreserve_metadata_space(root, 5);
4341 inode_dec_link_count(inode);
4344 btrfs_btree_balance_dirty(root, nr);
4348 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4349 struct dentry *dentry)
4351 struct btrfs_trans_handle *trans;
4352 struct btrfs_root *root = BTRFS_I(dir)->root;
4353 struct inode *inode = old_dentry->d_inode;
4355 unsigned long nr = 0;
4359 if (inode->i_nlink == 0)
4363 * 1 item for inode ref
4364 * 2 items for dir items
4366 err = btrfs_reserve_metadata_space(root, 3);
4370 btrfs_inc_nlink(inode);
4372 err = btrfs_set_inode_index(dir, &index);
4376 trans = btrfs_start_transaction(root, 1);
4378 btrfs_set_trans_block_group(trans, dir);
4379 atomic_inc(&inode->i_count);
4381 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
4386 btrfs_update_inode_block_group(trans, dir);
4387 err = btrfs_update_inode(trans, root, inode);
4389 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4392 nr = trans->blocks_used;
4393 btrfs_end_transaction_throttle(trans, root);
4395 btrfs_unreserve_metadata_space(root, 3);
4397 inode_dec_link_count(inode);
4400 btrfs_btree_balance_dirty(root, nr);
4404 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4406 struct inode *inode = NULL;
4407 struct btrfs_trans_handle *trans;
4408 struct btrfs_root *root = BTRFS_I(dir)->root;
4410 int drop_on_err = 0;
4413 unsigned long nr = 1;
4416 * 2 items for inode and ref
4417 * 2 items for dir items
4418 * 1 for xattr if selinux is on
4420 err = btrfs_reserve_metadata_space(root, 5);
4424 trans = btrfs_start_transaction(root, 1);
4429 btrfs_set_trans_block_group(trans, dir);
4431 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4437 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4439 dentry->d_parent->d_inode->i_ino, objectid,
4440 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4442 if (IS_ERR(inode)) {
4443 err = PTR_ERR(inode);
4449 err = btrfs_init_inode_security(inode, dir);
4453 inode->i_op = &btrfs_dir_inode_operations;
4454 inode->i_fop = &btrfs_dir_file_operations;
4455 btrfs_set_trans_block_group(trans, inode);
4457 btrfs_i_size_write(inode, 0);
4458 err = btrfs_update_inode(trans, root, inode);
4462 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4463 inode, dentry->d_name.name,
4464 dentry->d_name.len, 0, index);
4468 d_instantiate(dentry, inode);
4470 btrfs_update_inode_block_group(trans, inode);
4471 btrfs_update_inode_block_group(trans, dir);
4474 nr = trans->blocks_used;
4475 btrfs_end_transaction_throttle(trans, root);
4478 btrfs_unreserve_metadata_space(root, 5);
4481 btrfs_btree_balance_dirty(root, nr);
4485 /* helper for btfs_get_extent. Given an existing extent in the tree,
4486 * and an extent that you want to insert, deal with overlap and insert
4487 * the new extent into the tree.
4489 static int merge_extent_mapping(struct extent_map_tree *em_tree,
4490 struct extent_map *existing,
4491 struct extent_map *em,
4492 u64 map_start, u64 map_len)
4496 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4497 start_diff = map_start - em->start;
4498 em->start = map_start;
4500 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4501 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
4502 em->block_start += start_diff;
4503 em->block_len -= start_diff;
4505 return add_extent_mapping(em_tree, em);
4508 static noinline int uncompress_inline(struct btrfs_path *path,
4509 struct inode *inode, struct page *page,
4510 size_t pg_offset, u64 extent_offset,
4511 struct btrfs_file_extent_item *item)
4514 struct extent_buffer *leaf = path->nodes[0];
4517 unsigned long inline_size;
4520 WARN_ON(pg_offset != 0);
4521 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4522 inline_size = btrfs_file_extent_inline_item_len(leaf,
4523 btrfs_item_nr(leaf, path->slots[0]));
4524 tmp = kmalloc(inline_size, GFP_NOFS);
4525 ptr = btrfs_file_extent_inline_start(item);
4527 read_extent_buffer(leaf, tmp, ptr, inline_size);
4529 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
4530 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4531 inline_size, max_size);
4533 char *kaddr = kmap_atomic(page, KM_USER0);
4534 unsigned long copy_size = min_t(u64,
4535 PAGE_CACHE_SIZE - pg_offset,
4536 max_size - extent_offset);
4537 memset(kaddr + pg_offset, 0, copy_size);
4538 kunmap_atomic(kaddr, KM_USER0);
4545 * a bit scary, this does extent mapping from logical file offset to the disk.
4546 * the ugly parts come from merging extents from the disk with the in-ram
4547 * representation. This gets more complex because of the data=ordered code,
4548 * where the in-ram extents might be locked pending data=ordered completion.
4550 * This also copies inline extents directly into the page.
4553 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4554 size_t pg_offset, u64 start, u64 len,
4560 u64 extent_start = 0;
4562 u64 objectid = inode->i_ino;
4564 struct btrfs_path *path = NULL;
4565 struct btrfs_root *root = BTRFS_I(inode)->root;
4566 struct btrfs_file_extent_item *item;
4567 struct extent_buffer *leaf;
4568 struct btrfs_key found_key;
4569 struct extent_map *em = NULL;
4570 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4571 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4572 struct btrfs_trans_handle *trans = NULL;
4576 read_lock(&em_tree->lock);
4577 em = lookup_extent_mapping(em_tree, start, len);
4579 em->bdev = root->fs_info->fs_devices->latest_bdev;
4580 read_unlock(&em_tree->lock);
4583 if (em->start > start || em->start + em->len <= start)
4584 free_extent_map(em);
4585 else if (em->block_start == EXTENT_MAP_INLINE && page)
4586 free_extent_map(em);
4590 em = alloc_extent_map(GFP_NOFS);
4595 em->bdev = root->fs_info->fs_devices->latest_bdev;
4596 em->start = EXTENT_MAP_HOLE;
4597 em->orig_start = EXTENT_MAP_HOLE;
4599 em->block_len = (u64)-1;
4602 path = btrfs_alloc_path();
4606 ret = btrfs_lookup_file_extent(trans, root, path,
4607 objectid, start, trans != NULL);
4614 if (path->slots[0] == 0)
4619 leaf = path->nodes[0];
4620 item = btrfs_item_ptr(leaf, path->slots[0],
4621 struct btrfs_file_extent_item);
4622 /* are we inside the extent that was found? */
4623 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4624 found_type = btrfs_key_type(&found_key);
4625 if (found_key.objectid != objectid ||
4626 found_type != BTRFS_EXTENT_DATA_KEY) {
4630 found_type = btrfs_file_extent_type(leaf, item);
4631 extent_start = found_key.offset;
4632 compressed = btrfs_file_extent_compression(leaf, item);
4633 if (found_type == BTRFS_FILE_EXTENT_REG ||
4634 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
4635 extent_end = extent_start +
4636 btrfs_file_extent_num_bytes(leaf, item);
4637 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4639 size = btrfs_file_extent_inline_len(leaf, item);
4640 extent_end = (extent_start + size + root->sectorsize - 1) &
4641 ~((u64)root->sectorsize - 1);
4644 if (start >= extent_end) {
4646 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4647 ret = btrfs_next_leaf(root, path);
4654 leaf = path->nodes[0];
4656 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4657 if (found_key.objectid != objectid ||
4658 found_key.type != BTRFS_EXTENT_DATA_KEY)
4660 if (start + len <= found_key.offset)
4663 em->len = found_key.offset - start;
4667 if (found_type == BTRFS_FILE_EXTENT_REG ||
4668 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
4669 em->start = extent_start;
4670 em->len = extent_end - extent_start;
4671 em->orig_start = extent_start -
4672 btrfs_file_extent_offset(leaf, item);
4673 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4675 em->block_start = EXTENT_MAP_HOLE;
4679 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4680 em->block_start = bytenr;
4681 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4684 bytenr += btrfs_file_extent_offset(leaf, item);
4685 em->block_start = bytenr;
4686 em->block_len = em->len;
4687 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4688 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
4691 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4695 size_t extent_offset;
4698 em->block_start = EXTENT_MAP_INLINE;
4699 if (!page || create) {
4700 em->start = extent_start;
4701 em->len = extent_end - extent_start;
4705 size = btrfs_file_extent_inline_len(leaf, item);
4706 extent_offset = page_offset(page) + pg_offset - extent_start;
4707 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
4708 size - extent_offset);
4709 em->start = extent_start + extent_offset;
4710 em->len = (copy_size + root->sectorsize - 1) &
4711 ~((u64)root->sectorsize - 1);
4712 em->orig_start = EXTENT_MAP_INLINE;
4714 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4715 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
4716 if (create == 0 && !PageUptodate(page)) {
4717 if (btrfs_file_extent_compression(leaf, item) ==
4718 BTRFS_COMPRESS_ZLIB) {
4719 ret = uncompress_inline(path, inode, page,
4721 extent_offset, item);
4725 read_extent_buffer(leaf, map + pg_offset, ptr,
4727 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4728 memset(map + pg_offset + copy_size, 0,
4729 PAGE_CACHE_SIZE - pg_offset -
4734 flush_dcache_page(page);
4735 } else if (create && PageUptodate(page)) {
4738 free_extent_map(em);
4740 btrfs_release_path(root, path);
4741 trans = btrfs_join_transaction(root, 1);
4745 write_extent_buffer(leaf, map + pg_offset, ptr,
4748 btrfs_mark_buffer_dirty(leaf);
4750 set_extent_uptodate(io_tree, em->start,
4751 extent_map_end(em) - 1, GFP_NOFS);
4754 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
4761 em->block_start = EXTENT_MAP_HOLE;
4762 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
4764 btrfs_release_path(root, path);
4765 if (em->start > start || extent_map_end(em) <= start) {
4766 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4767 "[%llu %llu]\n", (unsigned long long)em->start,
4768 (unsigned long long)em->len,
4769 (unsigned long long)start,
4770 (unsigned long long)len);
4776 write_lock(&em_tree->lock);
4777 ret = add_extent_mapping(em_tree, em);
4778 /* it is possible that someone inserted the extent into the tree
4779 * while we had the lock dropped. It is also possible that
4780 * an overlapping map exists in the tree
4782 if (ret == -EEXIST) {
4783 struct extent_map *existing;
4787 existing = lookup_extent_mapping(em_tree, start, len);
4788 if (existing && (existing->start > start ||
4789 existing->start + existing->len <= start)) {
4790 free_extent_map(existing);
4794 existing = lookup_extent_mapping(em_tree, em->start,
4797 err = merge_extent_mapping(em_tree, existing,
4800 free_extent_map(existing);
4802 free_extent_map(em);
4807 free_extent_map(em);
4811 free_extent_map(em);
4816 write_unlock(&em_tree->lock);
4819 btrfs_free_path(path);
4821 ret = btrfs_end_transaction(trans, root);
4826 free_extent_map(em);
4827 return ERR_PTR(err);
4832 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4833 const struct iovec *iov, loff_t offset,
4834 unsigned long nr_segs)
4839 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4840 __u64 start, __u64 len)
4842 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4845 int btrfs_readpage(struct file *file, struct page *page)
4847 struct extent_io_tree *tree;
4848 tree = &BTRFS_I(page->mapping->host)->io_tree;
4849 return extent_read_full_page(tree, page, btrfs_get_extent);
4852 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4854 struct extent_io_tree *tree;
4857 if (current->flags & PF_MEMALLOC) {
4858 redirty_page_for_writepage(wbc, page);
4862 tree = &BTRFS_I(page->mapping->host)->io_tree;
4863 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4866 int btrfs_writepages(struct address_space *mapping,
4867 struct writeback_control *wbc)
4869 struct extent_io_tree *tree;
4871 tree = &BTRFS_I(mapping->host)->io_tree;
4872 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4876 btrfs_readpages(struct file *file, struct address_space *mapping,
4877 struct list_head *pages, unsigned nr_pages)
4879 struct extent_io_tree *tree;
4880 tree = &BTRFS_I(mapping->host)->io_tree;
4881 return extent_readpages(tree, mapping, pages, nr_pages,
4884 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4886 struct extent_io_tree *tree;
4887 struct extent_map_tree *map;
4890 tree = &BTRFS_I(page->mapping->host)->io_tree;
4891 map = &BTRFS_I(page->mapping->host)->extent_tree;
4892 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
4894 ClearPagePrivate(page);
4895 set_page_private(page, 0);
4896 page_cache_release(page);
4901 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4903 if (PageWriteback(page) || PageDirty(page))
4905 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
4908 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4910 struct extent_io_tree *tree;
4911 struct btrfs_ordered_extent *ordered;
4912 u64 page_start = page_offset(page);
4913 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
4917 * we have the page locked, so new writeback can't start,
4918 * and the dirty bit won't be cleared while we are here.
4920 * Wait for IO on this page so that we can safely clear
4921 * the PagePrivate2 bit and do ordered accounting
4923 wait_on_page_writeback(page);
4925 tree = &BTRFS_I(page->mapping->host)->io_tree;
4927 btrfs_releasepage(page, GFP_NOFS);
4930 lock_extent(tree, page_start, page_end, GFP_NOFS);
4931 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4935 * IO on this page will never be started, so we need
4936 * to account for any ordered extents now
4938 clear_extent_bit(tree, page_start, page_end,
4939 EXTENT_DIRTY | EXTENT_DELALLOC |
4940 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4943 * whoever cleared the private bit is responsible
4944 * for the finish_ordered_io
4946 if (TestClearPagePrivate2(page)) {
4947 btrfs_finish_ordered_io(page->mapping->host,
4948 page_start, page_end);
4950 btrfs_put_ordered_extent(ordered);
4951 lock_extent(tree, page_start, page_end, GFP_NOFS);
4953 clear_extent_bit(tree, page_start, page_end,
4954 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4955 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
4956 __btrfs_releasepage(page, GFP_NOFS);
4958 ClearPageChecked(page);
4959 if (PagePrivate(page)) {
4960 ClearPagePrivate(page);
4961 set_page_private(page, 0);
4962 page_cache_release(page);
4967 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4968 * called from a page fault handler when a page is first dirtied. Hence we must
4969 * be careful to check for EOF conditions here. We set the page up correctly
4970 * for a written page which means we get ENOSPC checking when writing into
4971 * holes and correct delalloc and unwritten extent mapping on filesystems that
4972 * support these features.
4974 * We are not allowed to take the i_mutex here so we have to play games to
4975 * protect against truncate races as the page could now be beyond EOF. Because
4976 * vmtruncate() writes the inode size before removing pages, once we have the
4977 * page lock we can determine safely if the page is beyond EOF. If it is not
4978 * beyond EOF, then the page is guaranteed safe against truncation until we
4981 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
4983 struct page *page = vmf->page;
4984 struct inode *inode = fdentry(vma->vm_file)->d_inode;
4985 struct btrfs_root *root = BTRFS_I(inode)->root;
4986 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4987 struct btrfs_ordered_extent *ordered;
4989 unsigned long zero_start;
4995 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
4999 else /* -ENOSPC, -EIO, etc */
5000 ret = VM_FAULT_SIGBUS;
5004 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5006 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5007 ret = VM_FAULT_SIGBUS;
5011 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
5014 size = i_size_read(inode);
5015 page_start = page_offset(page);
5016 page_end = page_start + PAGE_CACHE_SIZE - 1;
5018 if ((page->mapping != inode->i_mapping) ||
5019 (page_start >= size)) {
5020 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5021 /* page got truncated out from underneath us */
5024 wait_on_page_writeback(page);
5026 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5027 set_page_extent_mapped(page);
5030 * we can't set the delalloc bits if there are pending ordered
5031 * extents. Drop our locks and wait for them to finish
5033 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5035 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5037 btrfs_start_ordered_extent(inode, ordered, 1);
5038 btrfs_put_ordered_extent(ordered);
5043 * XXX - page_mkwrite gets called every time the page is dirtied, even
5044 * if it was already dirty, so for space accounting reasons we need to
5045 * clear any delalloc bits for the range we are fixing to save. There
5046 * is probably a better way to do this, but for now keep consistent with
5047 * prepare_pages in the normal write path.
5049 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
5050 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5053 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5055 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5056 ret = VM_FAULT_SIGBUS;
5057 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5062 /* page is wholly or partially inside EOF */
5063 if (page_start + PAGE_CACHE_SIZE > size)
5064 zero_start = size & ~PAGE_CACHE_MASK;
5066 zero_start = PAGE_CACHE_SIZE;
5068 if (zero_start != PAGE_CACHE_SIZE) {
5070 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5071 flush_dcache_page(page);
5074 ClearPageChecked(page);
5075 set_page_dirty(page);
5076 SetPageUptodate(page);
5078 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5079 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5081 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5084 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
5086 return VM_FAULT_LOCKED;
5092 static void btrfs_truncate(struct inode *inode)
5094 struct btrfs_root *root = BTRFS_I(inode)->root;
5096 struct btrfs_trans_handle *trans;
5098 u64 mask = root->sectorsize - 1;
5100 if (!S_ISREG(inode->i_mode))
5102 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
5105 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5108 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
5110 trans = btrfs_start_transaction(root, 1);
5113 * setattr is responsible for setting the ordered_data_close flag,
5114 * but that is only tested during the last file release. That
5115 * could happen well after the next commit, leaving a great big
5116 * window where new writes may get lost if someone chooses to write
5117 * to this file after truncating to zero
5119 * The inode doesn't have any dirty data here, and so if we commit
5120 * this is a noop. If someone immediately starts writing to the inode
5121 * it is very likely we'll catch some of their writes in this
5122 * transaction, and the commit will find this file on the ordered
5123 * data list with good things to send down.
5125 * This is a best effort solution, there is still a window where
5126 * using truncate to replace the contents of the file will
5127 * end up with a zero length file after a crash.
5129 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5130 btrfs_add_ordered_operation(trans, root, inode);
5132 btrfs_set_trans_block_group(trans, inode);
5133 btrfs_i_size_write(inode, inode->i_size);
5135 ret = btrfs_orphan_add(trans, inode);
5138 /* FIXME, add redo link to tree so we don't leak on crash */
5139 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
5140 BTRFS_EXTENT_DATA_KEY);
5141 btrfs_update_inode(trans, root, inode);
5143 ret = btrfs_orphan_del(trans, inode);
5147 nr = trans->blocks_used;
5148 ret = btrfs_end_transaction_throttle(trans, root);
5150 btrfs_btree_balance_dirty(root, nr);
5154 * create a new subvolume directory/inode (helper for the ioctl).
5156 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
5157 struct btrfs_root *new_root,
5158 u64 new_dirid, u64 alloc_hint)
5160 struct inode *inode;
5164 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
5165 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
5167 return PTR_ERR(inode);
5168 inode->i_op = &btrfs_dir_inode_operations;
5169 inode->i_fop = &btrfs_dir_file_operations;
5172 btrfs_i_size_write(inode, 0);
5174 err = btrfs_update_inode(trans, new_root, inode);
5181 /* helper function for file defrag and space balancing. This
5182 * forces readahead on a given range of bytes in an inode
5184 unsigned long btrfs_force_ra(struct address_space *mapping,
5185 struct file_ra_state *ra, struct file *file,
5186 pgoff_t offset, pgoff_t last_index)
5188 pgoff_t req_size = last_index - offset + 1;
5190 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5191 return offset + req_size;
5194 struct inode *btrfs_alloc_inode(struct super_block *sb)
5196 struct btrfs_inode *ei;
5198 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5202 ei->last_sub_trans = 0;
5203 ei->logged_trans = 0;
5204 ei->outstanding_extents = 0;
5205 ei->reserved_extents = 0;
5207 spin_lock_init(&ei->accounting_lock);
5208 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
5209 INIT_LIST_HEAD(&ei->i_orphan);
5210 INIT_LIST_HEAD(&ei->ordered_operations);
5211 return &ei->vfs_inode;
5214 void btrfs_destroy_inode(struct inode *inode)
5216 struct btrfs_ordered_extent *ordered;
5217 struct btrfs_root *root = BTRFS_I(inode)->root;
5219 WARN_ON(!list_empty(&inode->i_dentry));
5220 WARN_ON(inode->i_data.nrpages);
5223 * This can happen where we create an inode, but somebody else also
5224 * created the same inode and we need to destroy the one we already
5231 * Make sure we're properly removed from the ordered operation
5235 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5236 spin_lock(&root->fs_info->ordered_extent_lock);
5237 list_del_init(&BTRFS_I(inode)->ordered_operations);
5238 spin_unlock(&root->fs_info->ordered_extent_lock);
5241 spin_lock(&root->list_lock);
5242 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5243 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
5244 " list\n", inode->i_ino);
5247 spin_unlock(&root->list_lock);
5250 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5254 printk(KERN_ERR "btrfs found ordered "
5255 "extent %llu %llu on inode cleanup\n",
5256 (unsigned long long)ordered->file_offset,
5257 (unsigned long long)ordered->len);
5258 btrfs_remove_ordered_extent(inode, ordered);
5259 btrfs_put_ordered_extent(ordered);
5260 btrfs_put_ordered_extent(ordered);
5263 inode_tree_del(inode);
5264 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
5266 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5269 void btrfs_drop_inode(struct inode *inode)
5271 struct btrfs_root *root = BTRFS_I(inode)->root;
5273 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5274 generic_delete_inode(inode);
5276 generic_drop_inode(inode);
5279 static void init_once(void *foo)
5281 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5283 inode_init_once(&ei->vfs_inode);
5286 void btrfs_destroy_cachep(void)
5288 if (btrfs_inode_cachep)
5289 kmem_cache_destroy(btrfs_inode_cachep);
5290 if (btrfs_trans_handle_cachep)
5291 kmem_cache_destroy(btrfs_trans_handle_cachep);
5292 if (btrfs_transaction_cachep)
5293 kmem_cache_destroy(btrfs_transaction_cachep);
5294 if (btrfs_path_cachep)
5295 kmem_cache_destroy(btrfs_path_cachep);
5298 int btrfs_init_cachep(void)
5300 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5301 sizeof(struct btrfs_inode), 0,
5302 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
5303 if (!btrfs_inode_cachep)
5306 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5307 sizeof(struct btrfs_trans_handle), 0,
5308 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
5309 if (!btrfs_trans_handle_cachep)
5312 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5313 sizeof(struct btrfs_transaction), 0,
5314 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
5315 if (!btrfs_transaction_cachep)
5318 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5319 sizeof(struct btrfs_path), 0,
5320 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
5321 if (!btrfs_path_cachep)
5326 btrfs_destroy_cachep();
5330 static int btrfs_getattr(struct vfsmount *mnt,
5331 struct dentry *dentry, struct kstat *stat)
5333 struct inode *inode = dentry->d_inode;
5334 generic_fillattr(inode, stat);
5335 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
5336 stat->blksize = PAGE_CACHE_SIZE;
5337 stat->blocks = (inode_get_bytes(inode) +
5338 BTRFS_I(inode)->delalloc_bytes) >> 9;
5342 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5343 struct inode *new_dir, struct dentry *new_dentry)
5345 struct btrfs_trans_handle *trans;
5346 struct btrfs_root *root = BTRFS_I(old_dir)->root;
5347 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
5348 struct inode *new_inode = new_dentry->d_inode;
5349 struct inode *old_inode = old_dentry->d_inode;
5350 struct timespec ctime = CURRENT_TIME;
5355 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5358 /* we only allow rename subvolume link between subvolumes */
5359 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
5362 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5363 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5366 if (S_ISDIR(old_inode->i_mode) && new_inode &&
5367 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
5371 * We want to reserve the absolute worst case amount of items. So if
5372 * both inodes are subvols and we need to unlink them then that would
5373 * require 4 item modifications, but if they are both normal inodes it
5374 * would require 5 item modifications, so we'll assume their normal
5375 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5376 * should cover the worst case number of items we'll modify.
5378 ret = btrfs_reserve_metadata_space(root, 11);
5383 * we're using rename to replace one file with another.
5384 * and the replacement file is large. Start IO on it now so
5385 * we don't add too much work to the end of the transaction
5387 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
5388 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5389 filemap_flush(old_inode->i_mapping);
5391 /* close the racy window with snapshot create/destroy ioctl */
5392 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5393 down_read(&root->fs_info->subvol_sem);
5395 trans = btrfs_start_transaction(root, 1);
5396 btrfs_set_trans_block_group(trans, new_dir);
5399 btrfs_record_root_in_trans(trans, dest);
5401 ret = btrfs_set_inode_index(new_dir, &index);
5405 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5406 /* force full log commit if subvolume involved. */
5407 root->fs_info->last_trans_log_full_commit = trans->transid;
5409 ret = btrfs_insert_inode_ref(trans, dest,
5410 new_dentry->d_name.name,
5411 new_dentry->d_name.len,
5413 new_dir->i_ino, index);
5417 * this is an ugly little race, but the rename is required
5418 * to make sure that if we crash, the inode is either at the
5419 * old name or the new one. pinning the log transaction lets
5420 * us make sure we don't allow a log commit to come in after
5421 * we unlink the name but before we add the new name back in.
5423 btrfs_pin_log_trans(root);
5426 * make sure the inode gets flushed if it is replacing
5429 if (new_inode && new_inode->i_size &&
5430 old_inode && S_ISREG(old_inode->i_mode)) {
5431 btrfs_add_ordered_operation(trans, root, old_inode);
5434 old_dir->i_ctime = old_dir->i_mtime = ctime;
5435 new_dir->i_ctime = new_dir->i_mtime = ctime;
5436 old_inode->i_ctime = ctime;
5438 if (old_dentry->d_parent != new_dentry->d_parent)
5439 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5441 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5442 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5443 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5444 old_dentry->d_name.name,
5445 old_dentry->d_name.len);
5447 btrfs_inc_nlink(old_dentry->d_inode);
5448 ret = btrfs_unlink_inode(trans, root, old_dir,
5449 old_dentry->d_inode,
5450 old_dentry->d_name.name,
5451 old_dentry->d_name.len);
5456 new_inode->i_ctime = CURRENT_TIME;
5457 if (unlikely(new_inode->i_ino ==
5458 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5459 root_objectid = BTRFS_I(new_inode)->location.objectid;
5460 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5462 new_dentry->d_name.name,
5463 new_dentry->d_name.len);
5464 BUG_ON(new_inode->i_nlink == 0);
5466 ret = btrfs_unlink_inode(trans, dest, new_dir,
5467 new_dentry->d_inode,
5468 new_dentry->d_name.name,
5469 new_dentry->d_name.len);
5472 if (new_inode->i_nlink == 0) {
5473 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
5478 ret = btrfs_add_link(trans, new_dir, old_inode,
5479 new_dentry->d_name.name,
5480 new_dentry->d_name.len, 0, index);
5483 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5484 btrfs_log_new_name(trans, old_inode, old_dir,
5485 new_dentry->d_parent);
5486 btrfs_end_log_trans(root);
5489 btrfs_end_transaction_throttle(trans, root);
5491 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5492 up_read(&root->fs_info->subvol_sem);
5494 btrfs_unreserve_metadata_space(root, 11);
5499 * some fairly slow code that needs optimization. This walks the list
5500 * of all the inodes with pending delalloc and forces them to disk.
5502 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5504 struct list_head *head = &root->fs_info->delalloc_inodes;
5505 struct btrfs_inode *binode;
5506 struct inode *inode;
5508 if (root->fs_info->sb->s_flags & MS_RDONLY)
5511 spin_lock(&root->fs_info->delalloc_lock);
5512 while (!list_empty(head)) {
5513 binode = list_entry(head->next, struct btrfs_inode,
5515 inode = igrab(&binode->vfs_inode);
5517 list_del_init(&binode->delalloc_inodes);
5518 spin_unlock(&root->fs_info->delalloc_lock);
5520 filemap_flush(inode->i_mapping);
5524 spin_lock(&root->fs_info->delalloc_lock);
5526 spin_unlock(&root->fs_info->delalloc_lock);
5528 /* the filemap_flush will queue IO into the worker threads, but
5529 * we have to make sure the IO is actually started and that
5530 * ordered extents get created before we return
5532 atomic_inc(&root->fs_info->async_submit_draining);
5533 while (atomic_read(&root->fs_info->nr_async_submits) ||
5534 atomic_read(&root->fs_info->async_delalloc_pages)) {
5535 wait_event(root->fs_info->async_submit_wait,
5536 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5537 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
5539 atomic_dec(&root->fs_info->async_submit_draining);
5543 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5544 const char *symname)
5546 struct btrfs_trans_handle *trans;
5547 struct btrfs_root *root = BTRFS_I(dir)->root;
5548 struct btrfs_path *path;
5549 struct btrfs_key key;
5550 struct inode *inode = NULL;
5558 struct btrfs_file_extent_item *ei;
5559 struct extent_buffer *leaf;
5560 unsigned long nr = 0;
5562 name_len = strlen(symname) + 1;
5563 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5564 return -ENAMETOOLONG;
5567 * 2 items for inode item and ref
5568 * 2 items for dir items
5569 * 1 item for xattr if selinux is on
5571 err = btrfs_reserve_metadata_space(root, 5);
5575 trans = btrfs_start_transaction(root, 1);
5578 btrfs_set_trans_block_group(trans, dir);
5580 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5588 dentry->d_parent->d_inode->i_ino, objectid,
5589 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5591 err = PTR_ERR(inode);
5595 err = btrfs_init_inode_security(inode, dir);
5601 btrfs_set_trans_block_group(trans, inode);
5602 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
5606 inode->i_mapping->a_ops = &btrfs_aops;
5607 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5608 inode->i_fop = &btrfs_file_operations;
5609 inode->i_op = &btrfs_file_inode_operations;
5610 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5612 btrfs_update_inode_block_group(trans, inode);
5613 btrfs_update_inode_block_group(trans, dir);
5617 path = btrfs_alloc_path();
5619 key.objectid = inode->i_ino;
5621 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5622 datasize = btrfs_file_extent_calc_inline_size(name_len);
5623 err = btrfs_insert_empty_item(trans, root, path, &key,
5629 leaf = path->nodes[0];
5630 ei = btrfs_item_ptr(leaf, path->slots[0],
5631 struct btrfs_file_extent_item);
5632 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5633 btrfs_set_file_extent_type(leaf, ei,
5634 BTRFS_FILE_EXTENT_INLINE);
5635 btrfs_set_file_extent_encryption(leaf, ei, 0);
5636 btrfs_set_file_extent_compression(leaf, ei, 0);
5637 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5638 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5640 ptr = btrfs_file_extent_inline_start(ei);
5641 write_extent_buffer(leaf, symname, ptr, name_len);
5642 btrfs_mark_buffer_dirty(leaf);
5643 btrfs_free_path(path);
5645 inode->i_op = &btrfs_symlink_inode_operations;
5646 inode->i_mapping->a_ops = &btrfs_symlink_aops;
5647 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5648 inode_set_bytes(inode, name_len);
5649 btrfs_i_size_write(inode, name_len - 1);
5650 err = btrfs_update_inode(trans, root, inode);
5655 nr = trans->blocks_used;
5656 btrfs_end_transaction_throttle(trans, root);
5658 btrfs_unreserve_metadata_space(root, 5);
5660 inode_dec_link_count(inode);
5663 btrfs_btree_balance_dirty(root, nr);
5667 static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5668 u64 alloc_hint, int mode)
5670 struct btrfs_trans_handle *trans;
5671 struct btrfs_root *root = BTRFS_I(inode)->root;
5672 struct btrfs_key ins;
5674 u64 cur_offset = start;
5675 u64 num_bytes = end - start;
5678 while (num_bytes > 0) {
5679 alloc_size = min(num_bytes, root->fs_info->max_extent);
5681 ret = btrfs_reserve_extent(trans, root, alloc_size,
5682 root->sectorsize, 0, alloc_hint,
5689 ret = btrfs_reserve_metadata_space(root, 3);
5691 btrfs_free_reserved_extent(root, ins.objectid,
5696 trans = btrfs_start_transaction(root, 1);
5698 ret = insert_reserved_file_extent(trans, inode,
5699 cur_offset, ins.objectid,
5700 ins.offset, ins.offset,
5701 ins.offset, 0, 0, 0,
5702 BTRFS_FILE_EXTENT_PREALLOC);
5704 btrfs_drop_extent_cache(inode, cur_offset,
5705 cur_offset + ins.offset -1, 0);
5707 num_bytes -= ins.offset;
5708 cur_offset += ins.offset;
5709 alloc_hint = ins.objectid + ins.offset;
5711 inode->i_ctime = CURRENT_TIME;
5712 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
5713 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5714 cur_offset > inode->i_size) {
5715 i_size_write(inode, cur_offset);
5716 btrfs_ordered_update_i_size(inode, cur_offset, NULL);
5719 ret = btrfs_update_inode(trans, root, inode);
5722 btrfs_end_transaction(trans, root);
5723 btrfs_unreserve_metadata_space(root, 3);
5728 static long btrfs_fallocate(struct inode *inode, int mode,
5729 loff_t offset, loff_t len)
5737 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5738 struct extent_map *em;
5741 alloc_start = offset & ~mask;
5742 alloc_end = (offset + len + mask) & ~mask;
5745 * wait for ordered IO before we have any locks. We'll loop again
5746 * below with the locks held.
5748 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5750 mutex_lock(&inode->i_mutex);
5751 if (alloc_start > inode->i_size) {
5752 ret = btrfs_cont_expand(inode, alloc_start);
5757 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
5758 alloc_end - alloc_start);
5762 locked_end = alloc_end - 1;
5764 struct btrfs_ordered_extent *ordered;
5766 /* the extent lock is ordered inside the running
5769 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5771 ordered = btrfs_lookup_first_ordered_extent(inode,
5774 ordered->file_offset + ordered->len > alloc_start &&
5775 ordered->file_offset < alloc_end) {
5776 btrfs_put_ordered_extent(ordered);
5777 unlock_extent(&BTRFS_I(inode)->io_tree,
5778 alloc_start, locked_end, GFP_NOFS);
5780 * we can't wait on the range with the transaction
5781 * running or with the extent lock held
5783 btrfs_wait_ordered_range(inode, alloc_start,
5784 alloc_end - alloc_start);
5787 btrfs_put_ordered_extent(ordered);
5792 cur_offset = alloc_start;
5794 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5795 alloc_end - cur_offset, 0);
5796 BUG_ON(IS_ERR(em) || !em);
5797 last_byte = min(extent_map_end(em), alloc_end);
5798 last_byte = (last_byte + mask) & ~mask;
5799 if (em->block_start == EXTENT_MAP_HOLE ||
5800 (cur_offset >= inode->i_size &&
5801 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5802 ret = prealloc_file_range(inode,
5803 cur_offset, last_byte,
5806 free_extent_map(em);
5810 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5811 alloc_hint = em->block_start;
5812 free_extent_map(em);
5814 cur_offset = last_byte;
5815 if (cur_offset >= alloc_end) {
5820 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5823 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5824 alloc_end - alloc_start);
5826 mutex_unlock(&inode->i_mutex);
5830 static int btrfs_set_page_dirty(struct page *page)
5832 return __set_page_dirty_nobuffers(page);
5835 static int btrfs_permission(struct inode *inode, int mask)
5837 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
5839 return generic_permission(inode, mask, btrfs_check_acl);
5842 static const struct inode_operations btrfs_dir_inode_operations = {
5843 .getattr = btrfs_getattr,
5844 .lookup = btrfs_lookup,
5845 .create = btrfs_create,
5846 .unlink = btrfs_unlink,
5848 .mkdir = btrfs_mkdir,
5849 .rmdir = btrfs_rmdir,
5850 .rename = btrfs_rename,
5851 .symlink = btrfs_symlink,
5852 .setattr = btrfs_setattr,
5853 .mknod = btrfs_mknod,
5854 .setxattr = btrfs_setxattr,
5855 .getxattr = btrfs_getxattr,
5856 .listxattr = btrfs_listxattr,
5857 .removexattr = btrfs_removexattr,
5858 .permission = btrfs_permission,
5860 static const struct inode_operations btrfs_dir_ro_inode_operations = {
5861 .lookup = btrfs_lookup,
5862 .permission = btrfs_permission,
5865 static const struct file_operations btrfs_dir_file_operations = {
5866 .llseek = generic_file_llseek,
5867 .read = generic_read_dir,
5868 .readdir = btrfs_real_readdir,
5869 .unlocked_ioctl = btrfs_ioctl,
5870 #ifdef CONFIG_COMPAT
5871 .compat_ioctl = btrfs_ioctl,
5873 .release = btrfs_release_file,
5874 .fsync = btrfs_sync_file,
5877 static struct extent_io_ops btrfs_extent_io_ops = {
5878 .fill_delalloc = run_delalloc_range,
5879 .submit_bio_hook = btrfs_submit_bio_hook,
5880 .merge_bio_hook = btrfs_merge_bio_hook,
5881 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
5882 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
5883 .writepage_start_hook = btrfs_writepage_start_hook,
5884 .readpage_io_failed_hook = btrfs_io_failed_hook,
5885 .set_bit_hook = btrfs_set_bit_hook,
5886 .clear_bit_hook = btrfs_clear_bit_hook,
5887 .merge_extent_hook = btrfs_merge_extent_hook,
5888 .split_extent_hook = btrfs_split_extent_hook,
5892 * btrfs doesn't support the bmap operation because swapfiles
5893 * use bmap to make a mapping of extents in the file. They assume
5894 * these extents won't change over the life of the file and they
5895 * use the bmap result to do IO directly to the drive.
5897 * the btrfs bmap call would return logical addresses that aren't
5898 * suitable for IO and they also will change frequently as COW
5899 * operations happen. So, swapfile + btrfs == corruption.
5901 * For now we're avoiding this by dropping bmap.
5903 static const struct address_space_operations btrfs_aops = {
5904 .readpage = btrfs_readpage,
5905 .writepage = btrfs_writepage,
5906 .writepages = btrfs_writepages,
5907 .readpages = btrfs_readpages,
5908 .sync_page = block_sync_page,
5909 .direct_IO = btrfs_direct_IO,
5910 .invalidatepage = btrfs_invalidatepage,
5911 .releasepage = btrfs_releasepage,
5912 .set_page_dirty = btrfs_set_page_dirty,
5913 .error_remove_page = generic_error_remove_page,
5916 static const struct address_space_operations btrfs_symlink_aops = {
5917 .readpage = btrfs_readpage,
5918 .writepage = btrfs_writepage,
5919 .invalidatepage = btrfs_invalidatepage,
5920 .releasepage = btrfs_releasepage,
5923 static const struct inode_operations btrfs_file_inode_operations = {
5924 .truncate = btrfs_truncate,
5925 .getattr = btrfs_getattr,
5926 .setattr = btrfs_setattr,
5927 .setxattr = btrfs_setxattr,
5928 .getxattr = btrfs_getxattr,
5929 .listxattr = btrfs_listxattr,
5930 .removexattr = btrfs_removexattr,
5931 .permission = btrfs_permission,
5932 .fallocate = btrfs_fallocate,
5933 .fiemap = btrfs_fiemap,
5935 static const struct inode_operations btrfs_special_inode_operations = {
5936 .getattr = btrfs_getattr,
5937 .setattr = btrfs_setattr,
5938 .permission = btrfs_permission,
5939 .setxattr = btrfs_setxattr,
5940 .getxattr = btrfs_getxattr,
5941 .listxattr = btrfs_listxattr,
5942 .removexattr = btrfs_removexattr,
5944 static const struct inode_operations btrfs_symlink_inode_operations = {
5945 .readlink = generic_readlink,
5946 .follow_link = page_follow_link_light,
5947 .put_link = page_put_link,
5948 .permission = btrfs_permission,
5949 .setxattr = btrfs_setxattr,
5950 .getxattr = btrfs_getxattr,
5951 .listxattr = btrfs_listxattr,
5952 .removexattr = btrfs_removexattr,
5955 const struct dentry_operations btrfs_dentry_operations = {
5956 .d_delete = btrfs_dentry_delete,