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/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
42 #include "transaction.h"
43 #include "btrfs_inode.h"
45 #include "print-tree.h"
47 #include "ordered-data.h"
51 #include "ref-cache.h"
52 #include "compression.h"
54 struct btrfs_iget_args {
56 struct btrfs_root *root;
59 static struct inode_operations btrfs_dir_inode_operations;
60 static struct inode_operations btrfs_symlink_inode_operations;
61 static struct inode_operations btrfs_dir_ro_inode_operations;
62 static struct inode_operations btrfs_special_inode_operations;
63 static struct inode_operations btrfs_file_inode_operations;
64 static struct address_space_operations btrfs_aops;
65 static struct address_space_operations btrfs_symlink_aops;
66 static struct file_operations btrfs_dir_file_operations;
67 static struct extent_io_ops btrfs_extent_io_ops;
69 static struct kmem_cache *btrfs_inode_cachep;
70 struct kmem_cache *btrfs_trans_handle_cachep;
71 struct kmem_cache *btrfs_transaction_cachep;
72 struct kmem_cache *btrfs_bit_radix_cachep;
73 struct kmem_cache *btrfs_path_cachep;
76 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
77 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
78 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
79 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
80 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
81 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
82 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
83 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
86 static void btrfs_truncate(struct inode *inode);
87 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
90 * a very lame attempt at stopping writes when the FS is 85% full. There
91 * are countless ways this is incorrect, but it is better than nothing.
93 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
102 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
103 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
104 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
112 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
114 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
119 * this does all the hard work for inserting an inline extent into
120 * the btree. The caller should have done a btrfs_drop_extents so that
121 * no overlapping inline items exist in the btree
123 static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root, struct inode *inode,
125 u64 start, size_t size, size_t compressed_size,
126 struct page **compressed_pages)
128 struct btrfs_key key;
129 struct btrfs_path *path;
130 struct extent_buffer *leaf;
131 struct page *page = NULL;
134 struct btrfs_file_extent_item *ei;
137 size_t cur_size = size;
139 unsigned long offset;
140 int use_compress = 0;
142 if (compressed_size && compressed_pages) {
144 cur_size = compressed_size;
147 path = btrfs_alloc_path(); if (!path)
150 btrfs_set_trans_block_group(trans, inode);
152 key.objectid = inode->i_ino;
154 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
155 inode_add_bytes(inode, size);
156 datasize = btrfs_file_extent_calc_inline_size(cur_size);
158 inode_add_bytes(inode, size);
159 ret = btrfs_insert_empty_item(trans, root, path, &key,
164 printk("got bad ret %d\n", ret);
167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0],
169 struct btrfs_file_extent_item);
170 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
171 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
172 btrfs_set_file_extent_encryption(leaf, ei, 0);
173 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
174 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
175 ptr = btrfs_file_extent_inline_start(ei);
180 while(compressed_size > 0) {
181 cpage = compressed_pages[i];
182 cur_size = min(compressed_size,
186 write_extent_buffer(leaf, kaddr, ptr, cur_size);
191 compressed_size -= cur_size;
193 btrfs_set_file_extent_compression(leaf, ei,
194 BTRFS_COMPRESS_ZLIB);
196 page = find_get_page(inode->i_mapping,
197 start >> PAGE_CACHE_SHIFT);
198 btrfs_set_file_extent_compression(leaf, ei, 0);
199 kaddr = kmap_atomic(page, KM_USER0);
200 offset = start & (PAGE_CACHE_SIZE - 1);
201 write_extent_buffer(leaf, kaddr + offset, ptr, size);
202 kunmap_atomic(kaddr, KM_USER0);
203 page_cache_release(page);
205 btrfs_mark_buffer_dirty(leaf);
206 btrfs_free_path(path);
208 BTRFS_I(inode)->disk_i_size = inode->i_size;
209 btrfs_update_inode(trans, root, inode);
212 btrfs_free_path(path);
218 * conditionally insert an inline extent into the file. This
219 * does the checks required to make sure the data is small enough
220 * to fit as an inline extent.
222 static int cow_file_range_inline(struct btrfs_trans_handle *trans,
223 struct btrfs_root *root,
224 struct inode *inode, u64 start, u64 end,
225 size_t compressed_size,
226 struct page **compressed_pages)
228 u64 isize = i_size_read(inode);
229 u64 actual_end = min(end + 1, isize);
230 u64 inline_len = actual_end - start;
231 u64 aligned_end = (end + root->sectorsize - 1) &
232 ~((u64)root->sectorsize - 1);
234 u64 data_len = inline_len;
238 data_len = compressed_size;
241 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
243 (actual_end & (root->sectorsize - 1)) == 0) ||
245 data_len > root->fs_info->max_inline) {
249 mutex_lock(&BTRFS_I(inode)->extent_mutex);
250 ret = btrfs_drop_extents(trans, root, inode, start,
251 aligned_end, aligned_end, &hint_byte);
254 if (isize > actual_end)
255 inline_len = min_t(u64, isize, actual_end);
256 ret = insert_inline_extent(trans, root, inode, start,
257 inline_len, compressed_size,
260 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
261 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
266 * when extent_io.c finds a delayed allocation range in the file,
267 * the call backs end up in this code. The basic idea is to
268 * allocate extents on disk for the range, and create ordered data structs
269 * in ram to track those extents.
271 * locked_page is the page that writepage had locked already. We use
272 * it to make sure we don't do extra locks or unlocks.
274 * *page_started is set to one if we unlock locked_page and do everything
275 * required to start IO on it. It may be clean and already done with
278 static int cow_file_range(struct inode *inode, struct page *locked_page,
279 u64 start, u64 end, int *page_started)
281 struct btrfs_root *root = BTRFS_I(inode)->root;
282 struct btrfs_trans_handle *trans;
285 unsigned long ram_size;
289 u64 blocksize = root->sectorsize;
291 struct btrfs_key ins;
292 struct extent_map *em;
293 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
295 struct page **pages = NULL;
296 unsigned long nr_pages;
297 unsigned long nr_pages_ret = 0;
298 unsigned long total_compressed = 0;
299 unsigned long total_in = 0;
300 unsigned long max_compressed = 128 * 1024;
301 unsigned long max_uncompressed = 256 * 1024;
305 trans = btrfs_join_transaction(root, 1);
307 btrfs_set_trans_block_group(trans, inode);
311 * compression made this loop a bit ugly, but the basic idea is to
312 * compress some pages but keep the total size of the compressed
313 * extent relatively small. If compression is off, this goto target
318 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
319 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
321 actual_end = min_t(u64, i_size_read(inode), end + 1);
322 total_compressed = actual_end - start;
324 /* we want to make sure that amount of ram required to uncompress
325 * an extent is reasonable, so we limit the total size in ram
326 * of a compressed extent to 256k
328 total_compressed = min(total_compressed, max_uncompressed);
329 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
330 num_bytes = max(blocksize, num_bytes);
331 disk_num_bytes = num_bytes;
335 /* we do compression for mount -o compress and when the
336 * inode has not been flagged as nocompress
338 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
339 btrfs_test_opt(root, COMPRESS)) {
341 pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
343 /* we want to make sure the amount of IO required to satisfy
344 * a random read is reasonably small, so we limit the size
345 * of a compressed extent to 128k
347 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
348 total_compressed, pages,
349 nr_pages, &nr_pages_ret,
355 unsigned long offset = total_compressed &
356 (PAGE_CACHE_SIZE - 1);
357 struct page *page = pages[nr_pages_ret - 1];
360 /* zero the tail end of the last page, we might be
361 * sending it down to disk
364 kaddr = kmap_atomic(page, KM_USER0);
365 memset(kaddr + offset, 0,
366 PAGE_CACHE_SIZE - offset);
367 kunmap_atomic(kaddr, KM_USER0);
373 /* lets try to make an inline extent */
374 if (ret || total_in < (end - start + 1)) {
375 /* we didn't compress the entire range, try
376 * to make an uncompressed inline extent. This
377 * is almost sure to fail, but maybe inline sizes
378 * will get bigger later
380 ret = cow_file_range_inline(trans, root, inode,
381 start, end, 0, NULL);
383 ret = cow_file_range_inline(trans, root, inode,
385 total_compressed, pages);
388 extent_clear_unlock_delalloc(inode,
389 &BTRFS_I(inode)->io_tree,
400 * we aren't doing an inline extent round the compressed size
401 * up to a block size boundary so the allocator does sane
404 total_compressed = (total_compressed + blocksize - 1) &
408 * one last check to make sure the compression is really a
409 * win, compare the page count read with the blocks on disk
411 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
412 ~(PAGE_CACHE_SIZE - 1);
413 if (total_compressed >= total_in) {
416 disk_num_bytes = total_compressed;
417 num_bytes = total_in;
420 if (!will_compress && pages) {
422 * the compression code ran but failed to make things smaller,
423 * free any pages it allocated and our page pointer array
425 for (i = 0; i < nr_pages_ret; i++) {
426 page_cache_release(pages[i]);
430 total_compressed = 0;
433 /* flag the file so we don't compress in the future */
434 btrfs_set_flag(inode, NOCOMPRESS);
437 BUG_ON(disk_num_bytes >
438 btrfs_super_total_bytes(&root->fs_info->super_copy));
440 mutex_lock(&BTRFS_I(inode)->extent_mutex);
441 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
442 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
444 while(disk_num_bytes > 0) {
445 unsigned long min_bytes;
448 * the max size of a compressed extent is pretty small,
449 * make the code a little less complex by forcing
450 * the allocator to find a whole compressed extent at once
453 min_bytes = disk_num_bytes;
455 min_bytes = root->sectorsize;
457 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
458 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
459 min_bytes, 0, alloc_hint,
463 goto free_pages_out_fail;
465 em = alloc_extent_map(GFP_NOFS);
469 ram_size = num_bytes;
472 /* ramsize == disk size */
473 ram_size = ins.offset;
474 em->len = ins.offset;
477 em->block_start = ins.objectid;
478 em->block_len = ins.offset;
479 em->bdev = root->fs_info->fs_devices->latest_bdev;
481 mutex_lock(&BTRFS_I(inode)->extent_mutex);
482 set_bit(EXTENT_FLAG_PINNED, &em->flags);
485 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
488 spin_lock(&em_tree->lock);
489 ret = add_extent_mapping(em_tree, em);
490 spin_unlock(&em_tree->lock);
491 if (ret != -EEXIST) {
495 btrfs_drop_extent_cache(inode, start,
496 start + ram_size - 1, 0);
498 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
500 cur_alloc_size = ins.offset;
501 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
502 ram_size, cur_alloc_size, 0,
506 if (disk_num_bytes < cur_alloc_size) {
507 printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
514 * we're doing compression, we and we need to
515 * submit the compressed extents down to the device.
517 * We lock down all the file pages, clearing their
518 * dirty bits and setting them writeback. Everyone
519 * that wants to modify the page will wait on the
520 * ordered extent above.
522 * The writeback bits on the file pages are
523 * cleared when the compressed pages are on disk
525 btrfs_end_transaction(trans, root);
527 if (start <= page_offset(locked_page) &&
528 page_offset(locked_page) < start + ram_size) {
532 extent_clear_unlock_delalloc(inode,
533 &BTRFS_I(inode)->io_tree,
535 start + ram_size - 1,
538 ret = btrfs_submit_compressed_write(inode, start,
539 ram_size, ins.objectid,
540 cur_alloc_size, pages,
544 trans = btrfs_join_transaction(root, 1);
545 if (start + ram_size < end) {
547 alloc_hint = ins.objectid + ins.offset;
548 /* pages will be freed at end_bio time */
552 /* we've written everything, time to go */
556 /* we're not doing compressed IO, don't unlock the first
557 * page (which the caller expects to stay locked), don't
558 * clear any dirty bits and don't set any writeback bits
560 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
561 start, start + ram_size - 1,
562 locked_page, 0, 0, 0);
563 disk_num_bytes -= cur_alloc_size;
564 num_bytes -= cur_alloc_size;
565 alloc_hint = ins.objectid + ins.offset;
566 start += cur_alloc_size;
571 btrfs_end_transaction(trans, root);
576 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
577 start, end, locked_page, 0, 0, 0);
579 for (i = 0; i < nr_pages_ret; i++)
580 page_cache_release(pages[i]);
588 * when nowcow writeback call back. This checks for snapshots or COW copies
589 * of the extents that exist in the file, and COWs the file as required.
591 * If no cow copies or snapshots exist, we write directly to the existing
594 static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
595 u64 start, u64 end, int *page_started)
602 struct btrfs_root *root = BTRFS_I(inode)->root;
603 struct btrfs_block_group_cache *block_group;
604 struct btrfs_trans_handle *trans;
605 struct extent_buffer *leaf;
607 struct btrfs_path *path;
608 struct btrfs_file_extent_item *item;
611 struct btrfs_key found_key;
613 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
614 path = btrfs_alloc_path();
616 trans = btrfs_join_transaction(root, 1);
619 ret = btrfs_lookup_file_extent(NULL, root, path,
620 inode->i_ino, start, 0);
627 if (path->slots[0] == 0)
632 leaf = path->nodes[0];
633 item = btrfs_item_ptr(leaf, path->slots[0],
634 struct btrfs_file_extent_item);
636 /* are we inside the extent that was found? */
637 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
638 found_type = btrfs_key_type(&found_key);
639 if (found_key.objectid != inode->i_ino ||
640 found_type != BTRFS_EXTENT_DATA_KEY)
643 found_type = btrfs_file_extent_type(leaf, item);
644 extent_start = found_key.offset;
645 if (found_type == BTRFS_FILE_EXTENT_REG) {
646 u64 extent_num_bytes;
648 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
649 extent_end = extent_start + extent_num_bytes;
652 if (btrfs_file_extent_compression(leaf, item) ||
653 btrfs_file_extent_encryption(leaf,item) ||
654 btrfs_file_extent_other_encoding(leaf, item))
657 if (loops && start != extent_start)
660 if (start < extent_start || start >= extent_end)
663 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
667 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
670 * we may be called by the resizer, make sure we're inside
671 * the limits of the FS
673 block_group = btrfs_lookup_block_group(root->fs_info,
675 if (!block_group || block_group->ro)
678 bytenr += btrfs_file_extent_offset(leaf, item);
679 extent_num_bytes = min(end + 1, extent_end) - start;
680 ret = btrfs_add_ordered_extent(inode, start, bytenr,
682 extent_num_bytes, 1, 0);
688 btrfs_release_path(root, path);
696 btrfs_end_transaction(trans, root);
697 btrfs_free_path(path);
698 return cow_file_range(inode, locked_page, start, end,
703 btrfs_end_transaction(trans, root);
704 btrfs_free_path(path);
709 * extent_io.c call back to do delayed allocation processing
711 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
712 u64 start, u64 end, int *page_started)
714 struct btrfs_root *root = BTRFS_I(inode)->root;
717 if (btrfs_test_opt(root, NODATACOW) ||
718 btrfs_test_flag(inode, NODATACOW))
719 ret = run_delalloc_nocow(inode, locked_page, start, end,
722 ret = cow_file_range(inode, locked_page, start, end,
729 * extent_io.c set_bit_hook, used to track delayed allocation
730 * bytes in this file, and to maintain the list of inodes that
731 * have pending delalloc work to be done.
733 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
734 unsigned long old, unsigned long bits)
737 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
738 struct btrfs_root *root = BTRFS_I(inode)->root;
739 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
740 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
741 root->fs_info->delalloc_bytes += end - start + 1;
742 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
743 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
744 &root->fs_info->delalloc_inodes);
746 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
752 * extent_io.c clear_bit_hook, see set_bit_hook for why
754 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
755 unsigned long old, unsigned long bits)
757 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
758 struct btrfs_root *root = BTRFS_I(inode)->root;
761 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
762 if (end - start + 1 > root->fs_info->delalloc_bytes) {
763 printk("warning: delalloc account %Lu %Lu\n",
764 end - start + 1, root->fs_info->delalloc_bytes);
765 root->fs_info->delalloc_bytes = 0;
766 BTRFS_I(inode)->delalloc_bytes = 0;
768 root->fs_info->delalloc_bytes -= end - start + 1;
769 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
771 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
772 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
773 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
775 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
781 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
782 * we don't create bios that span stripes or chunks
784 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
785 size_t size, struct bio *bio,
786 unsigned long bio_flags)
788 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
789 struct btrfs_mapping_tree *map_tree;
790 u64 logical = (u64)bio->bi_sector << 9;
795 length = bio->bi_size;
796 map_tree = &root->fs_info->mapping_tree;
798 ret = btrfs_map_block(map_tree, READ, logical,
799 &map_length, NULL, 0);
801 if (map_length < length + size) {
808 * in order to insert checksums into the metadata in large chunks,
809 * we wait until bio submission time. All the pages in the bio are
810 * checksummed and sums are attached onto the ordered extent record.
812 * At IO completion time the cums attached on the ordered extent record
813 * are inserted into the btree
815 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
816 int mirror_num, unsigned long bio_flags)
818 struct btrfs_root *root = BTRFS_I(inode)->root;
821 ret = btrfs_csum_one_bio(root, inode, bio);
824 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
828 * extent_io.c submission hook. This does the right thing for csum calculation on write,
829 * or reading the csums from the tree before a read
831 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
832 int mirror_num, unsigned long bio_flags)
834 struct btrfs_root *root = BTRFS_I(inode)->root;
837 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
840 if (btrfs_test_opt(root, NODATASUM) ||
841 btrfs_test_flag(inode, NODATASUM)) {
845 if (!(rw & (1 << BIO_RW))) {
846 btrfs_lookup_bio_sums(root, inode, bio);
848 if (bio_flags & EXTENT_BIO_COMPRESSED) {
849 return btrfs_submit_compressed_read(inode, bio,
850 mirror_num, bio_flags);
855 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
856 inode, rw, bio, mirror_num,
857 bio_flags, __btrfs_submit_bio_hook);
859 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
863 * given a list of ordered sums record them in the inode. This happens
864 * at IO completion time based on sums calculated at bio submission time.
866 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
867 struct inode *inode, u64 file_offset,
868 struct list_head *list)
870 struct list_head *cur;
871 struct btrfs_ordered_sum *sum;
873 btrfs_set_trans_block_group(trans, inode);
874 list_for_each(cur, list) {
875 sum = list_entry(cur, struct btrfs_ordered_sum, list);
876 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
882 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
884 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
888 /* see btrfs_writepage_start_hook for details on why this is required */
889 struct btrfs_writepage_fixup {
891 struct btrfs_work work;
894 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
896 struct btrfs_writepage_fixup *fixup;
897 struct btrfs_ordered_extent *ordered;
903 fixup = container_of(work, struct btrfs_writepage_fixup, work);
907 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
908 ClearPageChecked(page);
912 inode = page->mapping->host;
913 page_start = page_offset(page);
914 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
916 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
918 /* already ordered? We're done */
919 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
920 EXTENT_ORDERED, 0)) {
924 ordered = btrfs_lookup_ordered_extent(inode, page_start);
926 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
929 btrfs_start_ordered_extent(inode, ordered, 1);
933 btrfs_set_extent_delalloc(inode, page_start, page_end);
934 ClearPageChecked(page);
936 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
939 page_cache_release(page);
943 * There are a few paths in the higher layers of the kernel that directly
944 * set the page dirty bit without asking the filesystem if it is a
945 * good idea. This causes problems because we want to make sure COW
946 * properly happens and the data=ordered rules are followed.
948 * In our case any range that doesn't have the ORDERED bit set
949 * hasn't been properly setup for IO. We kick off an async process
950 * to fix it up. The async helper will wait for ordered extents, set
951 * the delalloc bit and make it safe to write the page.
953 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
955 struct inode *inode = page->mapping->host;
956 struct btrfs_writepage_fixup *fixup;
957 struct btrfs_root *root = BTRFS_I(inode)->root;
960 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
965 if (PageChecked(page))
968 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
972 SetPageChecked(page);
973 page_cache_get(page);
974 fixup->work.func = btrfs_writepage_fixup_worker;
976 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
980 /* as ordered data IO finishes, this gets called so we can finish
981 * an ordered extent if the range of bytes in the file it covers are
984 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
986 struct btrfs_root *root = BTRFS_I(inode)->root;
987 struct btrfs_trans_handle *trans;
988 struct btrfs_ordered_extent *ordered_extent;
989 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
990 struct btrfs_file_extent_item *extent_item;
991 struct btrfs_path *path = NULL;
992 struct extent_buffer *leaf;
994 struct list_head list;
995 struct btrfs_key ins;
998 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
1002 trans = btrfs_join_transaction(root, 1);
1004 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1005 BUG_ON(!ordered_extent);
1006 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1009 path = btrfs_alloc_path();
1012 lock_extent(io_tree, ordered_extent->file_offset,
1013 ordered_extent->file_offset + ordered_extent->len - 1,
1016 INIT_LIST_HEAD(&list);
1018 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1020 ret = btrfs_drop_extents(trans, root, inode,
1021 ordered_extent->file_offset,
1022 ordered_extent->file_offset +
1023 ordered_extent->len,
1024 ordered_extent->file_offset, &alloc_hint);
1027 ins.objectid = inode->i_ino;
1028 ins.offset = ordered_extent->file_offset;
1029 ins.type = BTRFS_EXTENT_DATA_KEY;
1030 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1031 sizeof(*extent_item));
1033 leaf = path->nodes[0];
1034 extent_item = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_file_extent_item);
1036 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
1037 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
1038 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
1039 ordered_extent->start);
1040 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
1041 ordered_extent->disk_len);
1042 btrfs_set_file_extent_offset(leaf, extent_item, 0);
1044 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1045 btrfs_set_file_extent_compression(leaf, extent_item, 1);
1047 btrfs_set_file_extent_compression(leaf, extent_item, 0);
1048 btrfs_set_file_extent_encryption(leaf, extent_item, 0);
1049 btrfs_set_file_extent_other_encoding(leaf, extent_item, 0);
1051 /* ram bytes = extent_num_bytes for now */
1052 btrfs_set_file_extent_num_bytes(leaf, extent_item,
1053 ordered_extent->len);
1054 btrfs_set_file_extent_ram_bytes(leaf, extent_item,
1055 ordered_extent->len);
1056 btrfs_mark_buffer_dirty(leaf);
1058 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
1059 ordered_extent->file_offset +
1060 ordered_extent->len - 1, 0);
1061 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1063 ins.objectid = ordered_extent->start;
1064 ins.offset = ordered_extent->disk_len;
1065 ins.type = BTRFS_EXTENT_ITEM_KEY;
1066 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1067 root->root_key.objectid,
1068 trans->transid, inode->i_ino, &ins);
1070 btrfs_release_path(root, path);
1072 inode_add_bytes(inode, ordered_extent->len);
1073 unlock_extent(io_tree, ordered_extent->file_offset,
1074 ordered_extent->file_offset + ordered_extent->len - 1,
1077 add_pending_csums(trans, inode, ordered_extent->file_offset,
1078 &ordered_extent->list);
1080 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1081 btrfs_ordered_update_i_size(inode, ordered_extent);
1082 btrfs_update_inode(trans, root, inode);
1083 btrfs_remove_ordered_extent(inode, ordered_extent);
1084 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1087 btrfs_put_ordered_extent(ordered_extent);
1088 /* once for the tree */
1089 btrfs_put_ordered_extent(ordered_extent);
1091 btrfs_end_transaction(trans, root);
1093 btrfs_free_path(path);
1097 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1098 struct extent_state *state, int uptodate)
1100 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1104 * When IO fails, either with EIO or csum verification fails, we
1105 * try other mirrors that might have a good copy of the data. This
1106 * io_failure_record is used to record state as we go through all the
1107 * mirrors. If another mirror has good data, the page is set up to date
1108 * and things continue. If a good mirror can't be found, the original
1109 * bio end_io callback is called to indicate things have failed.
1111 struct io_failure_record {
1119 int btrfs_io_failed_hook(struct bio *failed_bio,
1120 struct page *page, u64 start, u64 end,
1121 struct extent_state *state)
1123 struct io_failure_record *failrec = NULL;
1125 struct extent_map *em;
1126 struct inode *inode = page->mapping->host;
1127 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1128 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1134 unsigned long bio_flags = 0;
1136 ret = get_state_private(failure_tree, start, &private);
1138 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1141 failrec->start = start;
1142 failrec->len = end - start + 1;
1143 failrec->last_mirror = 0;
1145 spin_lock(&em_tree->lock);
1146 em = lookup_extent_mapping(em_tree, start, failrec->len);
1147 if (em->start > start || em->start + em->len < start) {
1148 free_extent_map(em);
1151 spin_unlock(&em_tree->lock);
1153 if (!em || IS_ERR(em)) {
1157 logical = start - em->start;
1158 logical = em->block_start + logical;
1159 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1160 bio_flags = EXTENT_BIO_COMPRESSED;
1161 failrec->logical = logical;
1162 free_extent_map(em);
1163 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1164 EXTENT_DIRTY, GFP_NOFS);
1165 set_state_private(failure_tree, start,
1166 (u64)(unsigned long)failrec);
1168 failrec = (struct io_failure_record *)(unsigned long)private;
1170 num_copies = btrfs_num_copies(
1171 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1172 failrec->logical, failrec->len);
1173 failrec->last_mirror++;
1175 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
1176 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1179 if (state && state->start != failrec->start)
1181 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
1183 if (!state || failrec->last_mirror > num_copies) {
1184 set_state_private(failure_tree, failrec->start, 0);
1185 clear_extent_bits(failure_tree, failrec->start,
1186 failrec->start + failrec->len - 1,
1187 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1191 bio = bio_alloc(GFP_NOFS, 1);
1192 bio->bi_private = state;
1193 bio->bi_end_io = failed_bio->bi_end_io;
1194 bio->bi_sector = failrec->logical >> 9;
1195 bio->bi_bdev = failed_bio->bi_bdev;
1197 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1198 if (failed_bio->bi_rw & (1 << BIO_RW))
1203 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1204 failrec->last_mirror,
1210 * each time an IO finishes, we do a fast check in the IO failure tree
1211 * to see if we need to process or clean up an io_failure_record
1213 int btrfs_clean_io_failures(struct inode *inode, u64 start)
1216 u64 private_failure;
1217 struct io_failure_record *failure;
1221 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1222 (u64)-1, 1, EXTENT_DIRTY)) {
1223 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1224 start, &private_failure);
1226 failure = (struct io_failure_record *)(unsigned long)
1228 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1230 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1232 failure->start + failure->len - 1,
1233 EXTENT_DIRTY | EXTENT_LOCKED,
1242 * when reads are done, we need to check csums to verify the data is correct
1243 * if there's a match, we allow the bio to finish. If not, we go through
1244 * the io_failure_record routines to find good copies
1246 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1247 struct extent_state *state)
1249 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1250 struct inode *inode = page->mapping->host;
1251 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1253 u64 private = ~(u32)0;
1255 struct btrfs_root *root = BTRFS_I(inode)->root;
1257 unsigned long flags;
1259 if (btrfs_test_opt(root, NODATASUM) ||
1260 btrfs_test_flag(inode, NODATASUM))
1262 if (state && state->start == start) {
1263 private = state->private;
1266 ret = get_state_private(io_tree, start, &private);
1268 local_irq_save(flags);
1269 kaddr = kmap_atomic(page, KM_IRQ0);
1273 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1274 btrfs_csum_final(csum, (char *)&csum);
1275 if (csum != private) {
1278 kunmap_atomic(kaddr, KM_IRQ0);
1279 local_irq_restore(flags);
1281 /* if the io failure tree for this inode is non-empty,
1282 * check to see if we've recovered from a failed IO
1284 btrfs_clean_io_failures(inode, start);
1288 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
1289 page->mapping->host->i_ino, (unsigned long long)start, csum,
1291 memset(kaddr + offset, 1, end - start + 1);
1292 flush_dcache_page(page);
1293 kunmap_atomic(kaddr, KM_IRQ0);
1294 local_irq_restore(flags);
1301 * This creates an orphan entry for the given inode in case something goes
1302 * wrong in the middle of an unlink/truncate.
1304 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1306 struct btrfs_root *root = BTRFS_I(inode)->root;
1309 spin_lock(&root->list_lock);
1311 /* already on the orphan list, we're good */
1312 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
1313 spin_unlock(&root->list_lock);
1317 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1319 spin_unlock(&root->list_lock);
1322 * insert an orphan item to track this unlinked/truncated file
1324 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1330 * We have done the truncate/delete so we can go ahead and remove the orphan
1331 * item for this particular inode.
1333 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1335 struct btrfs_root *root = BTRFS_I(inode)->root;
1338 spin_lock(&root->list_lock);
1340 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
1341 spin_unlock(&root->list_lock);
1345 list_del_init(&BTRFS_I(inode)->i_orphan);
1347 spin_unlock(&root->list_lock);
1351 spin_unlock(&root->list_lock);
1353 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1359 * this cleans up any orphans that may be left on the list from the last use
1362 void btrfs_orphan_cleanup(struct btrfs_root *root)
1364 struct btrfs_path *path;
1365 struct extent_buffer *leaf;
1366 struct btrfs_item *item;
1367 struct btrfs_key key, found_key;
1368 struct btrfs_trans_handle *trans;
1369 struct inode *inode;
1370 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1372 /* don't do orphan cleanup if the fs is readonly. */
1373 if (root->fs_info->sb->s_flags & MS_RDONLY)
1376 path = btrfs_alloc_path();
1381 key.objectid = BTRFS_ORPHAN_OBJECTID;
1382 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1383 key.offset = (u64)-1;
1387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1389 printk(KERN_ERR "Error searching slot for orphan: %d"
1395 * if ret == 0 means we found what we were searching for, which
1396 * is weird, but possible, so only screw with path if we didnt
1397 * find the key and see if we have stuff that matches
1400 if (path->slots[0] == 0)
1405 /* pull out the item */
1406 leaf = path->nodes[0];
1407 item = btrfs_item_nr(leaf, path->slots[0]);
1408 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1410 /* make sure the item matches what we want */
1411 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1413 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1416 /* release the path since we're done with it */
1417 btrfs_release_path(root, path);
1420 * this is where we are basically btrfs_lookup, without the
1421 * crossing root thing. we store the inode number in the
1422 * offset of the orphan item.
1424 inode = btrfs_iget_locked(root->fs_info->sb,
1425 found_key.offset, root);
1429 if (inode->i_state & I_NEW) {
1430 BTRFS_I(inode)->root = root;
1432 /* have to set the location manually */
1433 BTRFS_I(inode)->location.objectid = inode->i_ino;
1434 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1435 BTRFS_I(inode)->location.offset = 0;
1437 btrfs_read_locked_inode(inode);
1438 unlock_new_inode(inode);
1442 * add this inode to the orphan list so btrfs_orphan_del does
1443 * the proper thing when we hit it
1445 spin_lock(&root->list_lock);
1446 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1447 spin_unlock(&root->list_lock);
1450 * if this is a bad inode, means we actually succeeded in
1451 * removing the inode, but not the orphan record, which means
1452 * we need to manually delete the orphan since iput will just
1453 * do a destroy_inode
1455 if (is_bad_inode(inode)) {
1456 trans = btrfs_start_transaction(root, 1);
1457 btrfs_orphan_del(trans, inode);
1458 btrfs_end_transaction(trans, root);
1463 /* if we have links, this was a truncate, lets do that */
1464 if (inode->i_nlink) {
1466 btrfs_truncate(inode);
1471 /* this will do delete_inode and everything for us */
1476 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1478 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1480 btrfs_free_path(path);
1484 * read an inode from the btree into the in-memory inode
1486 void btrfs_read_locked_inode(struct inode *inode)
1488 struct btrfs_path *path;
1489 struct extent_buffer *leaf;
1490 struct btrfs_inode_item *inode_item;
1491 struct btrfs_timespec *tspec;
1492 struct btrfs_root *root = BTRFS_I(inode)->root;
1493 struct btrfs_key location;
1494 u64 alloc_group_block;
1498 path = btrfs_alloc_path();
1500 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1502 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1506 leaf = path->nodes[0];
1507 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1508 struct btrfs_inode_item);
1510 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1511 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1512 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1513 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
1514 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
1516 tspec = btrfs_inode_atime(inode_item);
1517 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1518 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1520 tspec = btrfs_inode_mtime(inode_item);
1521 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1522 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1524 tspec = btrfs_inode_ctime(inode_item);
1525 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1526 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1528 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
1529 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1530 inode->i_generation = BTRFS_I(inode)->generation;
1532 rdev = btrfs_inode_rdev(leaf, inode_item);
1534 BTRFS_I(inode)->index_cnt = (u64)-1;
1536 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1537 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1539 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1540 if (!BTRFS_I(inode)->block_group) {
1541 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1543 BTRFS_BLOCK_GROUP_METADATA, 0);
1545 btrfs_free_path(path);
1548 switch (inode->i_mode & S_IFMT) {
1550 inode->i_mapping->a_ops = &btrfs_aops;
1551 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1552 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1553 inode->i_fop = &btrfs_file_operations;
1554 inode->i_op = &btrfs_file_inode_operations;
1557 inode->i_fop = &btrfs_dir_file_operations;
1558 if (root == root->fs_info->tree_root)
1559 inode->i_op = &btrfs_dir_ro_inode_operations;
1561 inode->i_op = &btrfs_dir_inode_operations;
1564 inode->i_op = &btrfs_symlink_inode_operations;
1565 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1566 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1569 init_special_inode(inode, inode->i_mode, rdev);
1575 btrfs_free_path(path);
1576 make_bad_inode(inode);
1580 * given a leaf and an inode, copy the inode fields into the leaf
1582 static void fill_inode_item(struct btrfs_trans_handle *trans,
1583 struct extent_buffer *leaf,
1584 struct btrfs_inode_item *item,
1585 struct inode *inode)
1587 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1588 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1589 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1590 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1591 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1593 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1594 inode->i_atime.tv_sec);
1595 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1596 inode->i_atime.tv_nsec);
1598 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1599 inode->i_mtime.tv_sec);
1600 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1601 inode->i_mtime.tv_nsec);
1603 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1604 inode->i_ctime.tv_sec);
1605 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1606 inode->i_ctime.tv_nsec);
1608 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
1609 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1610 btrfs_set_inode_transid(leaf, item, trans->transid);
1611 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1612 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1613 btrfs_set_inode_block_group(leaf, item,
1614 BTRFS_I(inode)->block_group->key.objectid);
1618 * copy everything in the in-memory inode into the btree.
1620 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1621 struct btrfs_root *root,
1622 struct inode *inode)
1624 struct btrfs_inode_item *inode_item;
1625 struct btrfs_path *path;
1626 struct extent_buffer *leaf;
1629 path = btrfs_alloc_path();
1631 ret = btrfs_lookup_inode(trans, root, path,
1632 &BTRFS_I(inode)->location, 1);
1639 leaf = path->nodes[0];
1640 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1641 struct btrfs_inode_item);
1643 fill_inode_item(trans, leaf, inode_item, inode);
1644 btrfs_mark_buffer_dirty(leaf);
1645 btrfs_set_inode_last_trans(trans, inode);
1648 btrfs_free_path(path);
1654 * unlink helper that gets used here in inode.c and in the tree logging
1655 * recovery code. It remove a link in a directory with a given name, and
1656 * also drops the back refs in the inode to the directory
1658 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1659 struct btrfs_root *root,
1660 struct inode *dir, struct inode *inode,
1661 const char *name, int name_len)
1663 struct btrfs_path *path;
1665 struct extent_buffer *leaf;
1666 struct btrfs_dir_item *di;
1667 struct btrfs_key key;
1670 path = btrfs_alloc_path();
1676 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1677 name, name_len, -1);
1686 leaf = path->nodes[0];
1687 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1688 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1691 btrfs_release_path(root, path);
1693 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1695 dir->i_ino, &index);
1697 printk("failed to delete reference to %.*s, "
1698 "inode %lu parent %lu\n", name_len, name,
1699 inode->i_ino, dir->i_ino);
1703 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1704 index, name, name_len, -1);
1713 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1714 btrfs_release_path(root, path);
1716 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1718 BUG_ON(ret != 0 && ret != -ENOENT);
1720 BTRFS_I(dir)->log_dirty_trans = trans->transid;
1722 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1726 btrfs_free_path(path);
1730 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1731 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1732 btrfs_update_inode(trans, root, dir);
1733 btrfs_drop_nlink(inode);
1734 ret = btrfs_update_inode(trans, root, inode);
1735 dir->i_sb->s_dirt = 1;
1740 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1742 struct btrfs_root *root;
1743 struct btrfs_trans_handle *trans;
1744 struct inode *inode = dentry->d_inode;
1746 unsigned long nr = 0;
1748 root = BTRFS_I(dir)->root;
1750 ret = btrfs_check_free_space(root, 1, 1);
1754 trans = btrfs_start_transaction(root, 1);
1756 btrfs_set_trans_block_group(trans, dir);
1757 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1758 dentry->d_name.name, dentry->d_name.len);
1760 if (inode->i_nlink == 0)
1761 ret = btrfs_orphan_add(trans, inode);
1763 nr = trans->blocks_used;
1765 btrfs_end_transaction_throttle(trans, root);
1767 btrfs_btree_balance_dirty(root, nr);
1771 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1773 struct inode *inode = dentry->d_inode;
1776 struct btrfs_root *root = BTRFS_I(dir)->root;
1777 struct btrfs_trans_handle *trans;
1778 unsigned long nr = 0;
1780 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1784 ret = btrfs_check_free_space(root, 1, 1);
1788 trans = btrfs_start_transaction(root, 1);
1789 btrfs_set_trans_block_group(trans, dir);
1791 err = btrfs_orphan_add(trans, inode);
1795 /* now the directory is empty */
1796 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1797 dentry->d_name.name, dentry->d_name.len);
1799 btrfs_i_size_write(inode, 0);
1803 nr = trans->blocks_used;
1804 ret = btrfs_end_transaction_throttle(trans, root);
1806 btrfs_btree_balance_dirty(root, nr);
1814 * when truncating bytes in a file, it is possible to avoid reading
1815 * the leaves that contain only checksum items. This can be the
1816 * majority of the IO required to delete a large file, but it must
1817 * be done carefully.
1819 * The keys in the level just above the leaves are checked to make sure
1820 * the lowest key in a given leaf is a csum key, and starts at an offset
1821 * after the new size.
1823 * Then the key for the next leaf is checked to make sure it also has
1824 * a checksum item for the same file. If it does, we know our target leaf
1825 * contains only checksum items, and it can be safely freed without reading
1828 * This is just an optimization targeted at large files. It may do
1829 * nothing. It will return 0 unless things went badly.
1831 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root,
1833 struct btrfs_path *path,
1834 struct inode *inode, u64 new_size)
1836 struct btrfs_key key;
1839 struct btrfs_key found_key;
1840 struct btrfs_key other_key;
1841 struct btrfs_leaf_ref *ref;
1845 path->lowest_level = 1;
1846 key.objectid = inode->i_ino;
1847 key.type = BTRFS_CSUM_ITEM_KEY;
1848 key.offset = new_size;
1850 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1854 if (path->nodes[1] == NULL) {
1859 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1860 nritems = btrfs_header_nritems(path->nodes[1]);
1865 if (path->slots[1] >= nritems)
1868 /* did we find a key greater than anything we want to delete? */
1869 if (found_key.objectid > inode->i_ino ||
1870 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1873 /* we check the next key in the node to make sure the leave contains
1874 * only checksum items. This comparison doesn't work if our
1875 * leaf is the last one in the node
1877 if (path->slots[1] + 1 >= nritems) {
1879 /* search forward from the last key in the node, this
1880 * will bring us into the next node in the tree
1882 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1884 /* unlikely, but we inc below, so check to be safe */
1885 if (found_key.offset == (u64)-1)
1888 /* search_forward needs a path with locks held, do the
1889 * search again for the original key. It is possible
1890 * this will race with a balance and return a path that
1891 * we could modify, but this drop is just an optimization
1892 * and is allowed to miss some leaves.
1894 btrfs_release_path(root, path);
1897 /* setup a max key for search_forward */
1898 other_key.offset = (u64)-1;
1899 other_key.type = key.type;
1900 other_key.objectid = key.objectid;
1902 path->keep_locks = 1;
1903 ret = btrfs_search_forward(root, &found_key, &other_key,
1905 path->keep_locks = 0;
1906 if (ret || found_key.objectid != key.objectid ||
1907 found_key.type != key.type) {
1912 key.offset = found_key.offset;
1913 btrfs_release_path(root, path);
1918 /* we know there's one more slot after us in the tree,
1919 * read that key so we can verify it is also a checksum item
1921 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1923 if (found_key.objectid < inode->i_ino)
1926 if (found_key.type != key.type || found_key.offset < new_size)
1930 * if the key for the next leaf isn't a csum key from this objectid,
1931 * we can't be sure there aren't good items inside this leaf.
1934 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1937 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
1938 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
1940 * it is safe to delete this leaf, it contains only
1941 * csum items from this inode at an offset >= new_size
1943 ret = btrfs_del_leaf(trans, root, path, leaf_start);
1946 if (root->ref_cows && leaf_gen < trans->transid) {
1947 ref = btrfs_alloc_leaf_ref(root, 0);
1949 ref->root_gen = root->root_key.offset;
1950 ref->bytenr = leaf_start;
1952 ref->generation = leaf_gen;
1955 ret = btrfs_add_leaf_ref(root, ref, 0);
1957 btrfs_free_leaf_ref(root, ref);
1963 btrfs_release_path(root, path);
1965 if (other_key.objectid == inode->i_ino &&
1966 other_key.type == key.type && other_key.offset > key.offset) {
1967 key.offset = other_key.offset;
1973 /* fixup any changes we've made to the path */
1974 path->lowest_level = 0;
1975 path->keep_locks = 0;
1976 btrfs_release_path(root, path);
1981 * this can truncate away extent items, csum items and directory items.
1982 * It starts at a high offset and removes keys until it can't find
1983 * any higher than new_size
1985 * csum items that cross the new i_size are truncated to the new size
1988 * min_type is the minimum key type to truncate down to. If set to 0, this
1989 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1991 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1992 struct btrfs_root *root,
1993 struct inode *inode,
1994 u64 new_size, u32 min_type)
1997 struct btrfs_path *path;
1998 struct btrfs_key key;
1999 struct btrfs_key found_key;
2001 struct extent_buffer *leaf;
2002 struct btrfs_file_extent_item *fi;
2003 u64 extent_start = 0;
2004 u64 extent_num_bytes = 0;
2010 int pending_del_nr = 0;
2011 int pending_del_slot = 0;
2012 int extent_type = -1;
2013 u64 mask = root->sectorsize - 1;
2016 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2017 path = btrfs_alloc_path();
2021 /* FIXME, add redo link to tree so we don't leak on crash */
2022 key.objectid = inode->i_ino;
2023 key.offset = (u64)-1;
2026 btrfs_init_path(path);
2028 ret = drop_csum_leaves(trans, root, path, inode, new_size);
2032 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2037 /* there are no items in the tree for us to truncate, we're
2040 if (path->slots[0] == 0) {
2049 leaf = path->nodes[0];
2050 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2051 found_type = btrfs_key_type(&found_key);
2053 if (found_key.objectid != inode->i_ino)
2056 if (found_type < min_type)
2059 item_end = found_key.offset;
2060 if (found_type == BTRFS_EXTENT_DATA_KEY) {
2061 fi = btrfs_item_ptr(leaf, path->slots[0],
2062 struct btrfs_file_extent_item);
2063 extent_type = btrfs_file_extent_type(leaf, fi);
2064 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2066 btrfs_file_extent_num_bytes(leaf, fi);
2067 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2068 item_end += btrfs_file_extent_inline_len(leaf,
2073 if (found_type == BTRFS_CSUM_ITEM_KEY) {
2074 ret = btrfs_csum_truncate(trans, root, path,
2078 if (item_end < new_size) {
2079 if (found_type == BTRFS_DIR_ITEM_KEY) {
2080 found_type = BTRFS_INODE_ITEM_KEY;
2081 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
2082 found_type = BTRFS_CSUM_ITEM_KEY;
2083 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
2084 found_type = BTRFS_XATTR_ITEM_KEY;
2085 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
2086 found_type = BTRFS_INODE_REF_KEY;
2087 } else if (found_type) {
2092 btrfs_set_key_type(&key, found_type);
2095 if (found_key.offset >= new_size)
2101 /* FIXME, shrink the extent if the ref count is only 1 */
2102 if (found_type != BTRFS_EXTENT_DATA_KEY)
2105 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2107 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
2109 u64 orig_num_bytes =
2110 btrfs_file_extent_num_bytes(leaf, fi);
2111 extent_num_bytes = new_size -
2112 found_key.offset + root->sectorsize - 1;
2113 extent_num_bytes = extent_num_bytes &
2114 ~((u64)root->sectorsize - 1);
2115 btrfs_set_file_extent_num_bytes(leaf, fi,
2117 num_dec = (orig_num_bytes -
2119 if (root->ref_cows && extent_start != 0)
2120 inode_sub_bytes(inode, num_dec);
2121 btrfs_mark_buffer_dirty(leaf);
2124 btrfs_file_extent_disk_num_bytes(leaf,
2126 /* FIXME blocksize != 4096 */
2127 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
2128 if (extent_start != 0) {
2131 inode_sub_bytes(inode, num_dec);
2133 root_gen = btrfs_header_generation(leaf);
2134 root_owner = btrfs_header_owner(leaf);
2136 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2138 * we can't truncate inline items that have had
2142 btrfs_file_extent_compression(leaf, fi) == 0 &&
2143 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2144 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
2145 u32 size = new_size - found_key.offset;
2147 if (root->ref_cows) {
2148 inode_sub_bytes(inode, item_end + 1 -
2152 btrfs_file_extent_calc_inline_size(size);
2153 ret = btrfs_truncate_item(trans, root, path,
2156 } else if (root->ref_cows) {
2157 inode_sub_bytes(inode, item_end + 1 -
2163 if (!pending_del_nr) {
2164 /* no pending yet, add ourselves */
2165 pending_del_slot = path->slots[0];
2167 } else if (pending_del_nr &&
2168 path->slots[0] + 1 == pending_del_slot) {
2169 /* hop on the pending chunk */
2171 pending_del_slot = path->slots[0];
2173 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
2179 ret = btrfs_free_extent(trans, root, extent_start,
2181 leaf->start, root_owner,
2182 root_gen, inode->i_ino, 0);
2186 if (path->slots[0] == 0) {
2189 btrfs_release_path(root, path);
2194 if (pending_del_nr &&
2195 path->slots[0] + 1 != pending_del_slot) {
2196 struct btrfs_key debug;
2198 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2200 ret = btrfs_del_items(trans, root, path,
2205 btrfs_release_path(root, path);
2211 if (pending_del_nr) {
2212 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2215 btrfs_free_path(path);
2216 inode->i_sb->s_dirt = 1;
2221 * taken from block_truncate_page, but does cow as it zeros out
2222 * any bytes left in the last page in the file.
2224 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2226 struct inode *inode = mapping->host;
2227 struct btrfs_root *root = BTRFS_I(inode)->root;
2228 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2229 struct btrfs_ordered_extent *ordered;
2231 u32 blocksize = root->sectorsize;
2232 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2233 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2239 if ((offset & (blocksize - 1)) == 0)
2244 page = grab_cache_page(mapping, index);
2248 page_start = page_offset(page);
2249 page_end = page_start + PAGE_CACHE_SIZE - 1;
2251 if (!PageUptodate(page)) {
2252 ret = btrfs_readpage(NULL, page);
2254 if (page->mapping != mapping) {
2256 page_cache_release(page);
2259 if (!PageUptodate(page)) {
2264 wait_on_page_writeback(page);
2266 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2267 set_page_extent_mapped(page);
2269 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2271 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2273 page_cache_release(page);
2274 btrfs_start_ordered_extent(inode, ordered, 1);
2275 btrfs_put_ordered_extent(ordered);
2279 btrfs_set_extent_delalloc(inode, page_start, page_end);
2281 if (offset != PAGE_CACHE_SIZE) {
2283 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2284 flush_dcache_page(page);
2287 ClearPageChecked(page);
2288 set_page_dirty(page);
2289 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2293 page_cache_release(page);
2298 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2300 struct inode *inode = dentry->d_inode;
2303 err = inode_change_ok(inode, attr);
2307 if (S_ISREG(inode->i_mode) &&
2308 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
2309 struct btrfs_trans_handle *trans;
2310 struct btrfs_root *root = BTRFS_I(inode)->root;
2311 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2313 u64 mask = root->sectorsize - 1;
2314 u64 hole_start = (inode->i_size + mask) & ~mask;
2315 u64 block_end = (attr->ia_size + mask) & ~mask;
2319 if (attr->ia_size <= hole_start)
2322 err = btrfs_check_free_space(root, 1, 0);
2326 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2328 hole_size = block_end - hole_start;
2330 struct btrfs_ordered_extent *ordered;
2331 btrfs_wait_ordered_range(inode, hole_start, hole_size);
2333 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2334 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2336 unlock_extent(io_tree, hole_start,
2337 block_end - 1, GFP_NOFS);
2338 btrfs_put_ordered_extent(ordered);
2344 trans = btrfs_start_transaction(root, 1);
2345 btrfs_set_trans_block_group(trans, inode);
2346 mutex_lock(&BTRFS_I(inode)->extent_mutex);
2347 err = btrfs_drop_extents(trans, root, inode,
2348 hole_start, block_end, hole_start,
2351 if (alloc_hint != EXTENT_MAP_INLINE) {
2352 err = btrfs_insert_file_extent(trans, root,
2355 hole_size, 0, hole_size,
2357 btrfs_drop_extent_cache(inode, hole_start,
2359 btrfs_check_file(root, inode);
2361 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
2362 btrfs_end_transaction(trans, root);
2363 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2368 err = inode_setattr(inode, attr);
2370 if (!err && ((attr->ia_valid & ATTR_MODE)))
2371 err = btrfs_acl_chmod(inode);
2376 void btrfs_delete_inode(struct inode *inode)
2378 struct btrfs_trans_handle *trans;
2379 struct btrfs_root *root = BTRFS_I(inode)->root;
2383 truncate_inode_pages(&inode->i_data, 0);
2384 if (is_bad_inode(inode)) {
2385 btrfs_orphan_del(NULL, inode);
2388 btrfs_wait_ordered_range(inode, 0, (u64)-1);
2390 btrfs_i_size_write(inode, 0);
2391 trans = btrfs_start_transaction(root, 1);
2393 btrfs_set_trans_block_group(trans, inode);
2394 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
2396 btrfs_orphan_del(NULL, inode);
2397 goto no_delete_lock;
2400 btrfs_orphan_del(trans, inode);
2402 nr = trans->blocks_used;
2405 btrfs_end_transaction(trans, root);
2406 btrfs_btree_balance_dirty(root, nr);
2410 nr = trans->blocks_used;
2411 btrfs_end_transaction(trans, root);
2412 btrfs_btree_balance_dirty(root, nr);
2418 * this returns the key found in the dir entry in the location pointer.
2419 * If no dir entries were found, location->objectid is 0.
2421 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2422 struct btrfs_key *location)
2424 const char *name = dentry->d_name.name;
2425 int namelen = dentry->d_name.len;
2426 struct btrfs_dir_item *di;
2427 struct btrfs_path *path;
2428 struct btrfs_root *root = BTRFS_I(dir)->root;
2431 path = btrfs_alloc_path();
2434 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2438 if (!di || IS_ERR(di)) {
2441 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
2443 btrfs_free_path(path);
2446 location->objectid = 0;
2451 * when we hit a tree root in a directory, the btrfs part of the inode
2452 * needs to be changed to reflect the root directory of the tree root. This
2453 * is kind of like crossing a mount point.
2455 static int fixup_tree_root_location(struct btrfs_root *root,
2456 struct btrfs_key *location,
2457 struct btrfs_root **sub_root,
2458 struct dentry *dentry)
2460 struct btrfs_root_item *ri;
2462 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2464 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2467 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2468 dentry->d_name.name,
2469 dentry->d_name.len);
2470 if (IS_ERR(*sub_root))
2471 return PTR_ERR(*sub_root);
2473 ri = &(*sub_root)->root_item;
2474 location->objectid = btrfs_root_dirid(ri);
2475 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2476 location->offset = 0;
2481 static noinline void init_btrfs_i(struct inode *inode)
2483 struct btrfs_inode *bi = BTRFS_I(inode);
2486 bi->i_default_acl = NULL;
2490 bi->logged_trans = 0;
2491 bi->delalloc_bytes = 0;
2492 bi->disk_i_size = 0;
2494 bi->index_cnt = (u64)-1;
2495 bi->log_dirty_trans = 0;
2496 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2497 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2498 inode->i_mapping, GFP_NOFS);
2499 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2500 inode->i_mapping, GFP_NOFS);
2501 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2502 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2503 mutex_init(&BTRFS_I(inode)->csum_mutex);
2504 mutex_init(&BTRFS_I(inode)->extent_mutex);
2505 mutex_init(&BTRFS_I(inode)->log_mutex);
2508 static int btrfs_init_locked_inode(struct inode *inode, void *p)
2510 struct btrfs_iget_args *args = p;
2511 inode->i_ino = args->ino;
2512 init_btrfs_i(inode);
2513 BTRFS_I(inode)->root = args->root;
2517 static int btrfs_find_actor(struct inode *inode, void *opaque)
2519 struct btrfs_iget_args *args = opaque;
2520 return (args->ino == inode->i_ino &&
2521 args->root == BTRFS_I(inode)->root);
2524 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2525 struct btrfs_root *root, int wait)
2527 struct inode *inode;
2528 struct btrfs_iget_args args;
2529 args.ino = objectid;
2533 inode = ilookup5(s, objectid, btrfs_find_actor,
2536 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2542 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2543 struct btrfs_root *root)
2545 struct inode *inode;
2546 struct btrfs_iget_args args;
2547 args.ino = objectid;
2550 inode = iget5_locked(s, objectid, btrfs_find_actor,
2551 btrfs_init_locked_inode,
2556 /* Get an inode object given its location and corresponding root.
2557 * Returns in *is_new if the inode was read from disk
2559 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2560 struct btrfs_root *root, int *is_new)
2562 struct inode *inode;
2564 inode = btrfs_iget_locked(s, location->objectid, root);
2566 return ERR_PTR(-EACCES);
2568 if (inode->i_state & I_NEW) {
2569 BTRFS_I(inode)->root = root;
2570 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2571 btrfs_read_locked_inode(inode);
2572 unlock_new_inode(inode);
2583 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2584 struct nameidata *nd)
2586 struct inode * inode;
2587 struct btrfs_inode *bi = BTRFS_I(dir);
2588 struct btrfs_root *root = bi->root;
2589 struct btrfs_root *sub_root = root;
2590 struct btrfs_key location;
2591 int ret, new, do_orphan = 0;
2593 if (dentry->d_name.len > BTRFS_NAME_LEN)
2594 return ERR_PTR(-ENAMETOOLONG);
2596 ret = btrfs_inode_by_name(dir, dentry, &location);
2599 return ERR_PTR(ret);
2602 if (location.objectid) {
2603 ret = fixup_tree_root_location(root, &location, &sub_root,
2606 return ERR_PTR(ret);
2608 return ERR_PTR(-ENOENT);
2609 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2611 return ERR_CAST(inode);
2613 /* the inode and parent dir are two different roots */
2614 if (new && root != sub_root) {
2616 sub_root->inode = inode;
2621 if (unlikely(do_orphan))
2622 btrfs_orphan_cleanup(sub_root);
2624 return d_splice_alias(inode, dentry);
2627 static unsigned char btrfs_filetype_table[] = {
2628 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2631 static int btrfs_real_readdir(struct file *filp, void *dirent,
2634 struct inode *inode = filp->f_dentry->d_inode;
2635 struct btrfs_root *root = BTRFS_I(inode)->root;
2636 struct btrfs_item *item;
2637 struct btrfs_dir_item *di;
2638 struct btrfs_key key;
2639 struct btrfs_key found_key;
2640 struct btrfs_path *path;
2643 struct extent_buffer *leaf;
2646 unsigned char d_type;
2651 int key_type = BTRFS_DIR_INDEX_KEY;
2656 /* FIXME, use a real flag for deciding about the key type */
2657 if (root->fs_info->tree_root == root)
2658 key_type = BTRFS_DIR_ITEM_KEY;
2660 /* special case for "." */
2661 if (filp->f_pos == 0) {
2662 over = filldir(dirent, ".", 1,
2669 /* special case for .., just use the back ref */
2670 if (filp->f_pos == 1) {
2671 u64 pino = parent_ino(filp->f_path.dentry);
2672 over = filldir(dirent, "..", 2,
2679 path = btrfs_alloc_path();
2682 btrfs_set_key_type(&key, key_type);
2683 key.offset = filp->f_pos;
2684 key.objectid = inode->i_ino;
2686 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2692 leaf = path->nodes[0];
2693 nritems = btrfs_header_nritems(leaf);
2694 slot = path->slots[0];
2695 if (advance || slot >= nritems) {
2696 if (slot >= nritems - 1) {
2697 ret = btrfs_next_leaf(root, path);
2700 leaf = path->nodes[0];
2701 nritems = btrfs_header_nritems(leaf);
2702 slot = path->slots[0];
2709 item = btrfs_item_nr(leaf, slot);
2710 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2712 if (found_key.objectid != key.objectid)
2714 if (btrfs_key_type(&found_key) != key_type)
2716 if (found_key.offset < filp->f_pos)
2719 filp->f_pos = found_key.offset;
2721 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2723 di_total = btrfs_item_size(leaf, item);
2725 while (di_cur < di_total) {
2726 struct btrfs_key location;
2728 name_len = btrfs_dir_name_len(leaf, di);
2729 if (name_len <= sizeof(tmp_name)) {
2730 name_ptr = tmp_name;
2732 name_ptr = kmalloc(name_len, GFP_NOFS);
2738 read_extent_buffer(leaf, name_ptr,
2739 (unsigned long)(di + 1), name_len);
2741 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2742 btrfs_dir_item_key_to_cpu(leaf, di, &location);
2743 over = filldir(dirent, name_ptr, name_len,
2744 found_key.offset, location.objectid,
2747 if (name_ptr != tmp_name)
2753 di_len = btrfs_dir_name_len(leaf, di) +
2754 btrfs_dir_data_len(leaf, di) + sizeof(*di);
2756 di = (struct btrfs_dir_item *)((char *)di + di_len);
2760 /* Reached end of directory/root. Bump pos past the last item. */
2761 if (key_type == BTRFS_DIR_INDEX_KEY)
2762 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2768 btrfs_free_path(path);
2772 int btrfs_write_inode(struct inode *inode, int wait)
2774 struct btrfs_root *root = BTRFS_I(inode)->root;
2775 struct btrfs_trans_handle *trans;
2778 if (root->fs_info->closing > 1)
2782 trans = btrfs_join_transaction(root, 1);
2783 btrfs_set_trans_block_group(trans, inode);
2784 ret = btrfs_commit_transaction(trans, root);
2790 * This is somewhat expensive, updating the tree every time the
2791 * inode changes. But, it is most likely to find the inode in cache.
2792 * FIXME, needs more benchmarking...there are no reasons other than performance
2793 * to keep or drop this code.
2795 void btrfs_dirty_inode(struct inode *inode)
2797 struct btrfs_root *root = BTRFS_I(inode)->root;
2798 struct btrfs_trans_handle *trans;
2800 trans = btrfs_join_transaction(root, 1);
2801 btrfs_set_trans_block_group(trans, inode);
2802 btrfs_update_inode(trans, root, inode);
2803 btrfs_end_transaction(trans, root);
2807 * find the highest existing sequence number in a directory
2808 * and then set the in-memory index_cnt variable to reflect
2809 * free sequence numbers
2811 static int btrfs_set_inode_index_count(struct inode *inode)
2813 struct btrfs_root *root = BTRFS_I(inode)->root;
2814 struct btrfs_key key, found_key;
2815 struct btrfs_path *path;
2816 struct extent_buffer *leaf;
2819 key.objectid = inode->i_ino;
2820 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2821 key.offset = (u64)-1;
2823 path = btrfs_alloc_path();
2827 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2830 /* FIXME: we should be able to handle this */
2836 * MAGIC NUMBER EXPLANATION:
2837 * since we search a directory based on f_pos we have to start at 2
2838 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2839 * else has to start at 2
2841 if (path->slots[0] == 0) {
2842 BTRFS_I(inode)->index_cnt = 2;
2848 leaf = path->nodes[0];
2849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2851 if (found_key.objectid != inode->i_ino ||
2852 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2853 BTRFS_I(inode)->index_cnt = 2;
2857 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2859 btrfs_free_path(path);
2864 * helper to find a free sequence number in a given directory. This current
2865 * code is very simple, later versions will do smarter things in the btree
2867 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2872 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2873 ret = btrfs_set_inode_index_count(dir);
2879 *index = BTRFS_I(dir)->index_cnt;
2880 BTRFS_I(dir)->index_cnt++;
2885 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2886 struct btrfs_root *root,
2888 const char *name, int name_len,
2891 struct btrfs_block_group_cache *group,
2892 int mode, u64 *index)
2894 struct inode *inode;
2895 struct btrfs_inode_item *inode_item;
2896 struct btrfs_block_group_cache *new_inode_group;
2897 struct btrfs_key *location;
2898 struct btrfs_path *path;
2899 struct btrfs_inode_ref *ref;
2900 struct btrfs_key key[2];
2906 path = btrfs_alloc_path();
2909 inode = new_inode(root->fs_info->sb);
2911 return ERR_PTR(-ENOMEM);
2914 ret = btrfs_set_inode_index(dir, inode, index);
2916 return ERR_PTR(ret);
2919 * index_cnt is ignored for everything but a dir,
2920 * btrfs_get_inode_index_count has an explanation for the magic
2923 init_btrfs_i(inode);
2924 BTRFS_I(inode)->index_cnt = 2;
2925 BTRFS_I(inode)->root = root;
2926 BTRFS_I(inode)->generation = trans->transid;
2932 new_inode_group = btrfs_find_block_group(root, group, 0,
2933 BTRFS_BLOCK_GROUP_METADATA, owner);
2934 if (!new_inode_group) {
2935 printk("find_block group failed\n");
2936 new_inode_group = group;
2938 BTRFS_I(inode)->block_group = new_inode_group;
2940 key[0].objectid = objectid;
2941 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2944 key[1].objectid = objectid;
2945 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2946 key[1].offset = ref_objectid;
2948 sizes[0] = sizeof(struct btrfs_inode_item);
2949 sizes[1] = name_len + sizeof(*ref);
2951 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2955 if (objectid > root->highest_inode)
2956 root->highest_inode = objectid;
2958 inode->i_uid = current->fsuid;
2959 inode->i_gid = current->fsgid;
2960 inode->i_mode = mode;
2961 inode->i_ino = objectid;
2962 inode_set_bytes(inode, 0);
2963 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2964 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2965 struct btrfs_inode_item);
2966 fill_inode_item(trans, path->nodes[0], inode_item, inode);
2968 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2969 struct btrfs_inode_ref);
2970 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2971 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
2972 ptr = (unsigned long)(ref + 1);
2973 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2975 btrfs_mark_buffer_dirty(path->nodes[0]);
2976 btrfs_free_path(path);
2978 location = &BTRFS_I(inode)->location;
2979 location->objectid = objectid;
2980 location->offset = 0;
2981 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2983 insert_inode_hash(inode);
2987 BTRFS_I(dir)->index_cnt--;
2988 btrfs_free_path(path);
2989 return ERR_PTR(ret);
2992 static inline u8 btrfs_inode_type(struct inode *inode)
2994 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2998 * utility function to add 'inode' into 'parent_inode' with
2999 * a give name and a given sequence number.
3000 * if 'add_backref' is true, also insert a backref from the
3001 * inode to the parent directory.
3003 int btrfs_add_link(struct btrfs_trans_handle *trans,
3004 struct inode *parent_inode, struct inode *inode,
3005 const char *name, int name_len, int add_backref, u64 index)
3008 struct btrfs_key key;
3009 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
3011 key.objectid = inode->i_ino;
3012 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3015 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3016 parent_inode->i_ino,
3017 &key, btrfs_inode_type(inode),
3021 ret = btrfs_insert_inode_ref(trans, root,
3024 parent_inode->i_ino,
3027 btrfs_i_size_write(parent_inode, parent_inode->i_size +
3029 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
3030 ret = btrfs_update_inode(trans, root, parent_inode);
3035 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
3036 struct dentry *dentry, struct inode *inode,
3037 int backref, u64 index)
3039 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3040 inode, dentry->d_name.name,
3041 dentry->d_name.len, backref, index);
3043 d_instantiate(dentry, inode);
3051 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3052 int mode, dev_t rdev)
3054 struct btrfs_trans_handle *trans;
3055 struct btrfs_root *root = BTRFS_I(dir)->root;
3056 struct inode *inode = NULL;
3060 unsigned long nr = 0;
3063 if (!new_valid_dev(rdev))
3066 err = btrfs_check_free_space(root, 1, 0);
3070 trans = btrfs_start_transaction(root, 1);
3071 btrfs_set_trans_block_group(trans, dir);
3073 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3079 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3081 dentry->d_parent->d_inode->i_ino, objectid,
3082 BTRFS_I(dir)->block_group, mode, &index);
3083 err = PTR_ERR(inode);
3087 err = btrfs_init_acl(inode, dir);
3093 btrfs_set_trans_block_group(trans, inode);
3094 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3098 inode->i_op = &btrfs_special_inode_operations;
3099 init_special_inode(inode, inode->i_mode, rdev);
3100 btrfs_update_inode(trans, root, inode);
3102 dir->i_sb->s_dirt = 1;
3103 btrfs_update_inode_block_group(trans, inode);
3104 btrfs_update_inode_block_group(trans, dir);
3106 nr = trans->blocks_used;
3107 btrfs_end_transaction_throttle(trans, root);
3110 inode_dec_link_count(inode);
3113 btrfs_btree_balance_dirty(root, nr);
3117 static int btrfs_create(struct inode *dir, struct dentry *dentry,
3118 int mode, struct nameidata *nd)
3120 struct btrfs_trans_handle *trans;
3121 struct btrfs_root *root = BTRFS_I(dir)->root;
3122 struct inode *inode = NULL;
3125 unsigned long nr = 0;
3129 err = btrfs_check_free_space(root, 1, 0);
3132 trans = btrfs_start_transaction(root, 1);
3133 btrfs_set_trans_block_group(trans, dir);
3135 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3141 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3143 dentry->d_parent->d_inode->i_ino,
3144 objectid, BTRFS_I(dir)->block_group, mode,
3146 err = PTR_ERR(inode);
3150 err = btrfs_init_acl(inode, dir);
3156 btrfs_set_trans_block_group(trans, inode);
3157 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3161 inode->i_mapping->a_ops = &btrfs_aops;
3162 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3163 inode->i_fop = &btrfs_file_operations;
3164 inode->i_op = &btrfs_file_inode_operations;
3165 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3167 dir->i_sb->s_dirt = 1;
3168 btrfs_update_inode_block_group(trans, inode);
3169 btrfs_update_inode_block_group(trans, dir);
3171 nr = trans->blocks_used;
3172 btrfs_end_transaction_throttle(trans, root);
3175 inode_dec_link_count(inode);
3178 btrfs_btree_balance_dirty(root, nr);
3182 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3183 struct dentry *dentry)
3185 struct btrfs_trans_handle *trans;
3186 struct btrfs_root *root = BTRFS_I(dir)->root;
3187 struct inode *inode = old_dentry->d_inode;
3189 unsigned long nr = 0;
3193 if (inode->i_nlink == 0)
3196 btrfs_inc_nlink(inode);
3197 err = btrfs_check_free_space(root, 1, 0);
3200 err = btrfs_set_inode_index(dir, inode, &index);
3204 trans = btrfs_start_transaction(root, 1);
3206 btrfs_set_trans_block_group(trans, dir);
3207 atomic_inc(&inode->i_count);
3209 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
3214 dir->i_sb->s_dirt = 1;
3215 btrfs_update_inode_block_group(trans, dir);
3216 err = btrfs_update_inode(trans, root, inode);
3221 nr = trans->blocks_used;
3222 btrfs_end_transaction_throttle(trans, root);
3225 inode_dec_link_count(inode);
3228 btrfs_btree_balance_dirty(root, nr);
3232 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3234 struct inode *inode = NULL;
3235 struct btrfs_trans_handle *trans;
3236 struct btrfs_root *root = BTRFS_I(dir)->root;
3238 int drop_on_err = 0;
3241 unsigned long nr = 1;
3243 err = btrfs_check_free_space(root, 1, 0);
3247 trans = btrfs_start_transaction(root, 1);
3248 btrfs_set_trans_block_group(trans, dir);
3250 if (IS_ERR(trans)) {
3251 err = PTR_ERR(trans);
3255 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3261 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3263 dentry->d_parent->d_inode->i_ino, objectid,
3264 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3266 if (IS_ERR(inode)) {
3267 err = PTR_ERR(inode);
3273 err = btrfs_init_acl(inode, dir);
3277 inode->i_op = &btrfs_dir_inode_operations;
3278 inode->i_fop = &btrfs_dir_file_operations;
3279 btrfs_set_trans_block_group(trans, inode);
3281 btrfs_i_size_write(inode, 0);
3282 err = btrfs_update_inode(trans, root, inode);
3286 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3287 inode, dentry->d_name.name,
3288 dentry->d_name.len, 0, index);
3292 d_instantiate(dentry, inode);
3294 dir->i_sb->s_dirt = 1;
3295 btrfs_update_inode_block_group(trans, inode);
3296 btrfs_update_inode_block_group(trans, dir);
3299 nr = trans->blocks_used;
3300 btrfs_end_transaction_throttle(trans, root);
3305 btrfs_btree_balance_dirty(root, nr);
3309 /* helper for btfs_get_extent. Given an existing extent in the tree,
3310 * and an extent that you want to insert, deal with overlap and insert
3311 * the new extent into the tree.
3313 static int merge_extent_mapping(struct extent_map_tree *em_tree,
3314 struct extent_map *existing,
3315 struct extent_map *em,
3316 u64 map_start, u64 map_len)
3320 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3321 start_diff = map_start - em->start;
3322 em->start = map_start;
3324 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3325 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3326 em->block_start += start_diff;
3327 em->block_len -= start_diff;
3329 return add_extent_mapping(em_tree, em);
3332 static noinline int uncompress_inline(struct btrfs_path *path,
3333 struct inode *inode, struct page *page,
3334 size_t pg_offset, u64 extent_offset,
3335 struct btrfs_file_extent_item *item)
3338 struct extent_buffer *leaf = path->nodes[0];
3341 unsigned long inline_size;
3344 WARN_ON(pg_offset != 0);
3345 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3346 inline_size = btrfs_file_extent_inline_item_len(leaf,
3347 btrfs_item_nr(leaf, path->slots[0]));
3348 tmp = kmalloc(inline_size, GFP_NOFS);
3349 ptr = btrfs_file_extent_inline_start(item);
3351 read_extent_buffer(leaf, tmp, ptr, inline_size);
3353 max_size = min(PAGE_CACHE_SIZE, max_size);
3354 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3355 inline_size, max_size);
3357 char *kaddr = kmap_atomic(page, KM_USER0);
3358 unsigned long copy_size = min_t(u64,
3359 PAGE_CACHE_SIZE - pg_offset,
3360 max_size - extent_offset);
3361 memset(kaddr + pg_offset, 0, copy_size);
3362 kunmap_atomic(kaddr, KM_USER0);
3369 * a bit scary, this does extent mapping from logical file offset to the disk.
3370 * the ugly parts come from merging extents from the disk with the
3371 * in-ram representation. This gets more complex because of the data=ordered code,
3372 * where the in-ram extents might be locked pending data=ordered completion.
3374 * This also copies inline extents directly into the page.
3376 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
3377 size_t pg_offset, u64 start, u64 len,
3383 u64 extent_start = 0;
3385 u64 objectid = inode->i_ino;
3387 struct btrfs_path *path = NULL;
3388 struct btrfs_root *root = BTRFS_I(inode)->root;
3389 struct btrfs_file_extent_item *item;
3390 struct extent_buffer *leaf;
3391 struct btrfs_key found_key;
3392 struct extent_map *em = NULL;
3393 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3394 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3395 struct btrfs_trans_handle *trans = NULL;
3399 spin_lock(&em_tree->lock);
3400 em = lookup_extent_mapping(em_tree, start, len);
3402 em->bdev = root->fs_info->fs_devices->latest_bdev;
3403 spin_unlock(&em_tree->lock);
3406 if (em->start > start || em->start + em->len <= start)
3407 free_extent_map(em);
3408 else if (em->block_start == EXTENT_MAP_INLINE && page)
3409 free_extent_map(em);
3413 em = alloc_extent_map(GFP_NOFS);
3418 em->bdev = root->fs_info->fs_devices->latest_bdev;
3419 em->start = EXTENT_MAP_HOLE;
3421 em->block_len = (u64)-1;
3424 path = btrfs_alloc_path();
3428 ret = btrfs_lookup_file_extent(trans, root, path,
3429 objectid, start, trans != NULL);
3436 if (path->slots[0] == 0)
3441 leaf = path->nodes[0];
3442 item = btrfs_item_ptr(leaf, path->slots[0],
3443 struct btrfs_file_extent_item);
3444 /* are we inside the extent that was found? */
3445 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3446 found_type = btrfs_key_type(&found_key);
3447 if (found_key.objectid != objectid ||
3448 found_type != BTRFS_EXTENT_DATA_KEY) {
3452 found_type = btrfs_file_extent_type(leaf, item);
3453 extent_start = found_key.offset;
3454 compressed = btrfs_file_extent_compression(leaf, item);
3455 if (found_type == BTRFS_FILE_EXTENT_REG) {
3456 extent_end = extent_start +
3457 btrfs_file_extent_num_bytes(leaf, item);
3459 if (start < extent_start || start >= extent_end) {
3461 if (start < extent_start) {
3462 if (start + len <= extent_start)
3464 em->len = extent_end - extent_start;
3470 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3472 em->start = extent_start;
3473 em->len = extent_end - extent_start;
3474 em->block_start = EXTENT_MAP_HOLE;
3477 em->start = extent_start;
3478 em->len = extent_end - extent_start;
3480 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3481 em->block_start = bytenr;
3482 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
3485 bytenr += btrfs_file_extent_offset(leaf, item);
3486 em->block_start = bytenr;
3487 em->block_len = em->len;
3490 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3495 size_t extent_offset;
3498 size = btrfs_file_extent_inline_len(leaf, item);
3499 extent_end = (extent_start + size + root->sectorsize - 1) &
3500 ~((u64)root->sectorsize - 1);
3501 if (start < extent_start || start >= extent_end) {
3503 if (start < extent_start) {
3504 if (start + len <= extent_start)
3506 em->len = extent_end - extent_start;
3512 em->block_start = EXTENT_MAP_INLINE;
3514 if (!page || create) {
3515 em->start = extent_start;
3516 em->len = (size + root->sectorsize - 1) &
3517 ~((u64)root->sectorsize - 1);
3521 page_start = page_offset(page) + pg_offset;
3522 extent_offset = page_start - extent_start;
3523 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3524 size - extent_offset);
3525 em->start = extent_start + extent_offset;
3526 em->len = (copy_size + root->sectorsize - 1) &
3527 ~((u64)root->sectorsize - 1);
3529 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3530 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
3531 if (create == 0 && !PageUptodate(page)) {
3532 if (btrfs_file_extent_compression(leaf, item) ==
3533 BTRFS_COMPRESS_ZLIB) {
3534 ret = uncompress_inline(path, inode, page,
3536 extent_offset, item);
3540 read_extent_buffer(leaf, map + pg_offset, ptr,
3544 flush_dcache_page(page);
3545 } else if (create && PageUptodate(page)) {
3548 free_extent_map(em);
3550 btrfs_release_path(root, path);
3551 trans = btrfs_join_transaction(root, 1);
3555 write_extent_buffer(leaf, map + pg_offset, ptr,
3558 btrfs_mark_buffer_dirty(leaf);
3560 set_extent_uptodate(io_tree, em->start,
3561 extent_map_end(em) - 1, GFP_NOFS);
3564 printk("unkknown found_type %d\n", found_type);
3571 em->block_start = EXTENT_MAP_HOLE;
3573 btrfs_release_path(root, path);
3574 if (em->start > start || extent_map_end(em) <= start) {
3575 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
3581 spin_lock(&em_tree->lock);
3582 ret = add_extent_mapping(em_tree, em);
3583 /* it is possible that someone inserted the extent into the tree
3584 * while we had the lock dropped. It is also possible that
3585 * an overlapping map exists in the tree
3587 if (ret == -EEXIST) {
3588 struct extent_map *existing;
3592 existing = lookup_extent_mapping(em_tree, start, len);
3593 if (existing && (existing->start > start ||
3594 existing->start + existing->len <= start)) {
3595 free_extent_map(existing);
3599 existing = lookup_extent_mapping(em_tree, em->start,
3602 err = merge_extent_mapping(em_tree, existing,
3605 free_extent_map(existing);
3607 free_extent_map(em);
3612 printk("failing to insert %Lu %Lu\n",
3614 free_extent_map(em);
3618 free_extent_map(em);
3623 spin_unlock(&em_tree->lock);
3626 btrfs_free_path(path);
3628 ret = btrfs_end_transaction(trans, root);
3634 free_extent_map(em);
3636 return ERR_PTR(err);
3641 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3642 const struct iovec *iov, loff_t offset,
3643 unsigned long nr_segs)
3648 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
3650 return extent_bmap(mapping, iblock, btrfs_get_extent);
3653 int btrfs_readpage(struct file *file, struct page *page)
3655 struct extent_io_tree *tree;
3656 tree = &BTRFS_I(page->mapping->host)->io_tree;
3657 return extent_read_full_page(tree, page, btrfs_get_extent);
3660 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3662 struct extent_io_tree *tree;
3665 if (current->flags & PF_MEMALLOC) {
3666 redirty_page_for_writepage(wbc, page);
3670 tree = &BTRFS_I(page->mapping->host)->io_tree;
3671 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3674 int btrfs_writepages(struct address_space *mapping,
3675 struct writeback_control *wbc)
3677 struct extent_io_tree *tree;
3678 tree = &BTRFS_I(mapping->host)->io_tree;
3679 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3683 btrfs_readpages(struct file *file, struct address_space *mapping,
3684 struct list_head *pages, unsigned nr_pages)
3686 struct extent_io_tree *tree;
3687 tree = &BTRFS_I(mapping->host)->io_tree;
3688 return extent_readpages(tree, mapping, pages, nr_pages,
3691 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3693 struct extent_io_tree *tree;
3694 struct extent_map_tree *map;
3697 tree = &BTRFS_I(page->mapping->host)->io_tree;
3698 map = &BTRFS_I(page->mapping->host)->extent_tree;
3699 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3701 ClearPagePrivate(page);
3702 set_page_private(page, 0);
3703 page_cache_release(page);
3708 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3710 if (PageWriteback(page) || PageDirty(page))
3712 return __btrfs_releasepage(page, gfp_flags);
3715 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3717 struct extent_io_tree *tree;
3718 struct btrfs_ordered_extent *ordered;
3719 u64 page_start = page_offset(page);
3720 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3722 wait_on_page_writeback(page);
3723 tree = &BTRFS_I(page->mapping->host)->io_tree;
3725 btrfs_releasepage(page, GFP_NOFS);
3729 lock_extent(tree, page_start, page_end, GFP_NOFS);
3730 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3734 * IO on this page will never be started, so we need
3735 * to account for any ordered extents now
3737 clear_extent_bit(tree, page_start, page_end,
3738 EXTENT_DIRTY | EXTENT_DELALLOC |
3739 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3740 btrfs_finish_ordered_io(page->mapping->host,
3741 page_start, page_end);
3742 btrfs_put_ordered_extent(ordered);
3743 lock_extent(tree, page_start, page_end, GFP_NOFS);
3745 clear_extent_bit(tree, page_start, page_end,
3746 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3749 __btrfs_releasepage(page, GFP_NOFS);
3751 ClearPageChecked(page);
3752 if (PagePrivate(page)) {
3753 ClearPagePrivate(page);
3754 set_page_private(page, 0);
3755 page_cache_release(page);
3760 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3761 * called from a page fault handler when a page is first dirtied. Hence we must
3762 * be careful to check for EOF conditions here. We set the page up correctly
3763 * for a written page which means we get ENOSPC checking when writing into
3764 * holes and correct delalloc and unwritten extent mapping on filesystems that
3765 * support these features.
3767 * We are not allowed to take the i_mutex here so we have to play games to
3768 * protect against truncate races as the page could now be beyond EOF. Because
3769 * vmtruncate() writes the inode size before removing pages, once we have the
3770 * page lock we can determine safely if the page is beyond EOF. If it is not
3771 * beyond EOF, then the page is guaranteed safe against truncation until we
3774 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3776 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3777 struct btrfs_root *root = BTRFS_I(inode)->root;
3778 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3779 struct btrfs_ordered_extent *ordered;
3781 unsigned long zero_start;
3787 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3794 size = i_size_read(inode);
3795 page_start = page_offset(page);
3796 page_end = page_start + PAGE_CACHE_SIZE - 1;
3798 if ((page->mapping != inode->i_mapping) ||
3799 (page_start >= size)) {
3800 /* page got truncated out from underneath us */
3803 wait_on_page_writeback(page);
3805 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3806 set_page_extent_mapped(page);
3809 * we can't set the delalloc bits if there are pending ordered
3810 * extents. Drop our locks and wait for them to finish
3812 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3814 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3816 btrfs_start_ordered_extent(inode, ordered, 1);
3817 btrfs_put_ordered_extent(ordered);
3821 btrfs_set_extent_delalloc(inode, page_start, page_end);
3824 /* page is wholly or partially inside EOF */
3825 if (page_start + PAGE_CACHE_SIZE > size)
3826 zero_start = size & ~PAGE_CACHE_MASK;
3828 zero_start = PAGE_CACHE_SIZE;
3830 if (zero_start != PAGE_CACHE_SIZE) {
3832 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3833 flush_dcache_page(page);
3836 ClearPageChecked(page);
3837 set_page_dirty(page);
3838 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3846 static void btrfs_truncate(struct inode *inode)
3848 struct btrfs_root *root = BTRFS_I(inode)->root;
3850 struct btrfs_trans_handle *trans;
3852 u64 mask = root->sectorsize - 1;
3854 if (!S_ISREG(inode->i_mode))
3856 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3859 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3860 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3862 trans = btrfs_start_transaction(root, 1);
3863 btrfs_set_trans_block_group(trans, inode);
3864 btrfs_i_size_write(inode, inode->i_size);
3866 ret = btrfs_orphan_add(trans, inode);
3869 /* FIXME, add redo link to tree so we don't leak on crash */
3870 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
3871 BTRFS_EXTENT_DATA_KEY);
3872 btrfs_update_inode(trans, root, inode);
3874 ret = btrfs_orphan_del(trans, inode);
3878 nr = trans->blocks_used;
3879 ret = btrfs_end_transaction_throttle(trans, root);
3881 btrfs_btree_balance_dirty(root, nr);
3885 * Invalidate a single dcache entry at the root of the filesystem.
3886 * Needed after creation of snapshot or subvolume.
3888 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3891 struct dentry *alias, *entry;
3894 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3898 /* change me if btrfs ever gets a d_hash operation */
3899 qstr.hash = full_name_hash(qstr.name, qstr.len);
3900 entry = d_lookup(alias, &qstr);
3903 d_invalidate(entry);
3910 * create a new subvolume directory/inode (helper for the ioctl).
3912 int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
3913 struct btrfs_trans_handle *trans, u64 new_dirid,
3914 struct btrfs_block_group_cache *block_group)
3916 struct inode *inode;
3920 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3921 new_dirid, block_group, S_IFDIR | 0700, &index);
3923 return PTR_ERR(inode);
3924 inode->i_op = &btrfs_dir_inode_operations;
3925 inode->i_fop = &btrfs_dir_file_operations;
3926 new_root->inode = inode;
3929 btrfs_i_size_write(inode, 0);
3931 error = btrfs_update_inode(trans, new_root, inode);
3935 d_instantiate(dentry, inode);
3939 /* helper function for file defrag and space balancing. This
3940 * forces readahead on a given range of bytes in an inode
3942 unsigned long btrfs_force_ra(struct address_space *mapping,
3943 struct file_ra_state *ra, struct file *file,
3944 pgoff_t offset, pgoff_t last_index)
3946 pgoff_t req_size = last_index - offset + 1;
3948 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3949 return offset + req_size;
3952 struct inode *btrfs_alloc_inode(struct super_block *sb)
3954 struct btrfs_inode *ei;
3956 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3960 ei->logged_trans = 0;
3961 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3962 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3963 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3964 INIT_LIST_HEAD(&ei->i_orphan);
3965 return &ei->vfs_inode;
3968 void btrfs_destroy_inode(struct inode *inode)
3970 struct btrfs_ordered_extent *ordered;
3971 WARN_ON(!list_empty(&inode->i_dentry));
3972 WARN_ON(inode->i_data.nrpages);
3974 if (BTRFS_I(inode)->i_acl &&
3975 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3976 posix_acl_release(BTRFS_I(inode)->i_acl);
3977 if (BTRFS_I(inode)->i_default_acl &&
3978 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3979 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3981 spin_lock(&BTRFS_I(inode)->root->list_lock);
3982 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3983 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3984 " list\n", inode->i_ino);
3987 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3990 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3994 printk("found ordered extent %Lu %Lu\n",
3995 ordered->file_offset, ordered->len);
3996 btrfs_remove_ordered_extent(inode, ordered);
3997 btrfs_put_ordered_extent(ordered);
3998 btrfs_put_ordered_extent(ordered);
4001 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
4002 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4005 static void init_once(void *foo)
4007 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4009 inode_init_once(&ei->vfs_inode);
4012 void btrfs_destroy_cachep(void)
4014 if (btrfs_inode_cachep)
4015 kmem_cache_destroy(btrfs_inode_cachep);
4016 if (btrfs_trans_handle_cachep)
4017 kmem_cache_destroy(btrfs_trans_handle_cachep);
4018 if (btrfs_transaction_cachep)
4019 kmem_cache_destroy(btrfs_transaction_cachep);
4020 if (btrfs_bit_radix_cachep)
4021 kmem_cache_destroy(btrfs_bit_radix_cachep);
4022 if (btrfs_path_cachep)
4023 kmem_cache_destroy(btrfs_path_cachep);
4026 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
4027 unsigned long extra_flags,
4028 void (*ctor)(void *))
4030 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
4031 SLAB_MEM_SPREAD | extra_flags), ctor);
4034 int btrfs_init_cachep(void)
4036 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
4037 sizeof(struct btrfs_inode),
4039 if (!btrfs_inode_cachep)
4041 btrfs_trans_handle_cachep =
4042 btrfs_cache_create("btrfs_trans_handle_cache",
4043 sizeof(struct btrfs_trans_handle),
4045 if (!btrfs_trans_handle_cachep)
4047 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
4048 sizeof(struct btrfs_transaction),
4050 if (!btrfs_transaction_cachep)
4052 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
4053 sizeof(struct btrfs_path),
4055 if (!btrfs_path_cachep)
4057 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
4058 SLAB_DESTROY_BY_RCU, NULL);
4059 if (!btrfs_bit_radix_cachep)
4063 btrfs_destroy_cachep();
4067 static int btrfs_getattr(struct vfsmount *mnt,
4068 struct dentry *dentry, struct kstat *stat)
4070 struct inode *inode = dentry->d_inode;
4071 generic_fillattr(inode, stat);
4072 stat->blksize = PAGE_CACHE_SIZE;
4073 stat->blocks = (inode_get_bytes(inode) +
4074 BTRFS_I(inode)->delalloc_bytes) >> 9;
4078 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
4079 struct inode * new_dir,struct dentry *new_dentry)
4081 struct btrfs_trans_handle *trans;
4082 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4083 struct inode *new_inode = new_dentry->d_inode;
4084 struct inode *old_inode = old_dentry->d_inode;
4085 struct timespec ctime = CURRENT_TIME;
4089 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4090 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4094 ret = btrfs_check_free_space(root, 1, 0);
4098 trans = btrfs_start_transaction(root, 1);
4100 btrfs_set_trans_block_group(trans, new_dir);
4102 btrfs_inc_nlink(old_dentry->d_inode);
4103 old_dir->i_ctime = old_dir->i_mtime = ctime;
4104 new_dir->i_ctime = new_dir->i_mtime = ctime;
4105 old_inode->i_ctime = ctime;
4107 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4108 old_dentry->d_name.name,
4109 old_dentry->d_name.len);
4114 new_inode->i_ctime = CURRENT_TIME;
4115 ret = btrfs_unlink_inode(trans, root, new_dir,
4116 new_dentry->d_inode,
4117 new_dentry->d_name.name,
4118 new_dentry->d_name.len);
4121 if (new_inode->i_nlink == 0) {
4122 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4128 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
4132 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4133 old_inode, new_dentry->d_name.name,
4134 new_dentry->d_name.len, 1, index);
4139 btrfs_end_transaction_throttle(trans, root);
4145 * some fairly slow code that needs optimization. This walks the list
4146 * of all the inodes with pending delalloc and forces them to disk.
4148 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4150 struct list_head *head = &root->fs_info->delalloc_inodes;
4151 struct btrfs_inode *binode;
4152 struct inode *inode;
4153 unsigned long flags;
4155 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4156 while(!list_empty(head)) {
4157 binode = list_entry(head->next, struct btrfs_inode,
4159 inode = igrab(&binode->vfs_inode);
4161 list_del_init(&binode->delalloc_inodes);
4162 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
4164 filemap_flush(inode->i_mapping);
4168 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4170 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
4172 /* the filemap_flush will queue IO into the worker threads, but
4173 * we have to make sure the IO is actually started and that
4174 * ordered extents get created before we return
4176 atomic_inc(&root->fs_info->async_submit_draining);
4177 while(atomic_read(&root->fs_info->nr_async_submits)) {
4178 wait_event(root->fs_info->async_submit_wait,
4179 (atomic_read(&root->fs_info->nr_async_submits) == 0));
4181 atomic_dec(&root->fs_info->async_submit_draining);
4185 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4186 const char *symname)
4188 struct btrfs_trans_handle *trans;
4189 struct btrfs_root *root = BTRFS_I(dir)->root;
4190 struct btrfs_path *path;
4191 struct btrfs_key key;
4192 struct inode *inode = NULL;
4200 struct btrfs_file_extent_item *ei;
4201 struct extent_buffer *leaf;
4202 unsigned long nr = 0;
4204 name_len = strlen(symname) + 1;
4205 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4206 return -ENAMETOOLONG;
4208 err = btrfs_check_free_space(root, 1, 0);
4212 trans = btrfs_start_transaction(root, 1);
4213 btrfs_set_trans_block_group(trans, dir);
4215 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4221 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4223 dentry->d_parent->d_inode->i_ino, objectid,
4224 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4226 err = PTR_ERR(inode);
4230 err = btrfs_init_acl(inode, dir);
4236 btrfs_set_trans_block_group(trans, inode);
4237 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4241 inode->i_mapping->a_ops = &btrfs_aops;
4242 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4243 inode->i_fop = &btrfs_file_operations;
4244 inode->i_op = &btrfs_file_inode_operations;
4245 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4247 dir->i_sb->s_dirt = 1;
4248 btrfs_update_inode_block_group(trans, inode);
4249 btrfs_update_inode_block_group(trans, dir);
4253 path = btrfs_alloc_path();
4255 key.objectid = inode->i_ino;
4257 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4258 datasize = btrfs_file_extent_calc_inline_size(name_len);
4259 err = btrfs_insert_empty_item(trans, root, path, &key,
4265 leaf = path->nodes[0];
4266 ei = btrfs_item_ptr(leaf, path->slots[0],
4267 struct btrfs_file_extent_item);
4268 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4269 btrfs_set_file_extent_type(leaf, ei,
4270 BTRFS_FILE_EXTENT_INLINE);
4271 btrfs_set_file_extent_encryption(leaf, ei, 0);
4272 btrfs_set_file_extent_compression(leaf, ei, 0);
4273 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4274 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4276 ptr = btrfs_file_extent_inline_start(ei);
4277 write_extent_buffer(leaf, symname, ptr, name_len);
4278 btrfs_mark_buffer_dirty(leaf);
4279 btrfs_free_path(path);
4281 inode->i_op = &btrfs_symlink_inode_operations;
4282 inode->i_mapping->a_ops = &btrfs_symlink_aops;
4283 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4284 btrfs_i_size_write(inode, name_len - 1);
4285 err = btrfs_update_inode(trans, root, inode);
4290 nr = trans->blocks_used;
4291 btrfs_end_transaction_throttle(trans, root);
4294 inode_dec_link_count(inode);
4297 btrfs_btree_balance_dirty(root, nr);
4301 static int btrfs_set_page_dirty(struct page *page)
4303 return __set_page_dirty_nobuffers(page);
4306 static int btrfs_permission(struct inode *inode, int mask)
4308 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4310 return generic_permission(inode, mask, btrfs_check_acl);
4313 static struct inode_operations btrfs_dir_inode_operations = {
4314 .lookup = btrfs_lookup,
4315 .create = btrfs_create,
4316 .unlink = btrfs_unlink,
4318 .mkdir = btrfs_mkdir,
4319 .rmdir = btrfs_rmdir,
4320 .rename = btrfs_rename,
4321 .symlink = btrfs_symlink,
4322 .setattr = btrfs_setattr,
4323 .mknod = btrfs_mknod,
4324 .setxattr = btrfs_setxattr,
4325 .getxattr = btrfs_getxattr,
4326 .listxattr = btrfs_listxattr,
4327 .removexattr = btrfs_removexattr,
4328 .permission = btrfs_permission,
4330 static struct inode_operations btrfs_dir_ro_inode_operations = {
4331 .lookup = btrfs_lookup,
4332 .permission = btrfs_permission,
4334 static struct file_operations btrfs_dir_file_operations = {
4335 .llseek = generic_file_llseek,
4336 .read = generic_read_dir,
4337 .readdir = btrfs_real_readdir,
4338 .unlocked_ioctl = btrfs_ioctl,
4339 #ifdef CONFIG_COMPAT
4340 .compat_ioctl = btrfs_ioctl,
4342 .release = btrfs_release_file,
4343 .fsync = btrfs_sync_file,
4346 static struct extent_io_ops btrfs_extent_io_ops = {
4347 .fill_delalloc = run_delalloc_range,
4348 .submit_bio_hook = btrfs_submit_bio_hook,
4349 .merge_bio_hook = btrfs_merge_bio_hook,
4350 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
4351 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
4352 .writepage_start_hook = btrfs_writepage_start_hook,
4353 .readpage_io_failed_hook = btrfs_io_failed_hook,
4354 .set_bit_hook = btrfs_set_bit_hook,
4355 .clear_bit_hook = btrfs_clear_bit_hook,
4358 static struct address_space_operations btrfs_aops = {
4359 .readpage = btrfs_readpage,
4360 .writepage = btrfs_writepage,
4361 .writepages = btrfs_writepages,
4362 .readpages = btrfs_readpages,
4363 .sync_page = block_sync_page,
4365 .direct_IO = btrfs_direct_IO,
4366 .invalidatepage = btrfs_invalidatepage,
4367 .releasepage = btrfs_releasepage,
4368 .set_page_dirty = btrfs_set_page_dirty,
4371 static struct address_space_operations btrfs_symlink_aops = {
4372 .readpage = btrfs_readpage,
4373 .writepage = btrfs_writepage,
4374 .invalidatepage = btrfs_invalidatepage,
4375 .releasepage = btrfs_releasepage,
4378 static struct inode_operations btrfs_file_inode_operations = {
4379 .truncate = btrfs_truncate,
4380 .getattr = btrfs_getattr,
4381 .setattr = btrfs_setattr,
4382 .setxattr = btrfs_setxattr,
4383 .getxattr = btrfs_getxattr,
4384 .listxattr = btrfs_listxattr,
4385 .removexattr = btrfs_removexattr,
4386 .permission = btrfs_permission,
4388 static struct inode_operations btrfs_special_inode_operations = {
4389 .getattr = btrfs_getattr,
4390 .setattr = btrfs_setattr,
4391 .permission = btrfs_permission,
4392 .setxattr = btrfs_setxattr,
4393 .getxattr = btrfs_getxattr,
4394 .listxattr = btrfs_listxattr,
4395 .removexattr = btrfs_removexattr,
4397 static struct inode_operations btrfs_symlink_inode_operations = {
4398 .readlink = generic_readlink,
4399 .follow_link = page_follow_link_light,
4400 .put_link = page_put_link,
4401 .permission = btrfs_permission,