Lines Matching refs:inode

48 #include "inode-map.h"
81 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
82 static int btrfs_truncate(struct inode *inode, bool skip_writeback);
84 static noinline int cow_file_range(struct btrfs_inode *inode,
88 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
94 static void __endio_write_update_ordered(struct btrfs_inode *inode,
108 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
120 page = find_get_page(inode->vfs_inode.i_mapping, index);
138 return __endio_write_update_ordered(inode, offset, bytes, false);
141 static int btrfs_dirty_inode(struct inode *inode);
144 struct inode *inode, struct inode *dir,
149 err = btrfs_init_acl(trans, inode, dir);
151 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
162 struct btrfs_root *root, struct inode *inode,
182 inode_add_bytes(inode, size);
188 key.objectid = btrfs_ino(BTRFS_I(inode));
228 page = find_get_page(inode->i_mapping,
245 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
254 * We must do any isize and inode updates
258 BTRFS_I(inode)->disk_i_size = inode->i_size;
259 ret = btrfs_update_inode(trans, root, inode);
271 static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
276 struct btrfs_root *root = inode->root;
279 u64 isize = i_size_read(&inode->vfs_inode);
311 trans->block_rsv = &inode->block_rsv;
320 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
331 root, &inode->vfs_inode, start,
342 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
343 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
351 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
368 struct inode *inode;
407 * Check if the inode has flags compatible with compression
409 static inline bool inode_can_compress(struct btrfs_inode *inode)
411 if (inode->flags & BTRFS_INODE_NODATACOW ||
412 inode->flags & BTRFS_INODE_NODATASUM)
418 * Check if the inode needs to be submitted to compression, based on mount
421 static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
424 struct btrfs_fs_info *fs_info = inode->root->fs_info;
426 if (!inode_can_compress(inode)) {
429 btrfs_ino(inode));
436 if (inode->defrag_compress)
439 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
442 inode->flags & BTRFS_INODE_COMPRESS ||
443 inode->prop_compress)
444 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
448 static inline void inode_should_defrag(struct btrfs_inode *inode,
453 (start > 0 || end + 1 < inode->disk_i_size))
454 btrfs_add_inode_defrag(NULL, inode);
476 struct inode *inode = async_chunk->inode;
477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
494 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
507 i_size = i_size_read(inode);
537 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
547 * inode has not been flagged as nocompress. This flag can
550 if (inode_need_compress(BTRFS_I(inode), start, end)) {
559 if (BTRFS_I(inode)->defrag_compress)
560 compress_type = BTRFS_I(inode)->defrag_compress;
561 else if (BTRFS_I(inode)->prop_compress)
562 compress_type = BTRFS_I(inode)->prop_compress;
577 extent_range_clear_dirty_for_io(inode, start, end);
584 inode->i_mapping, start,
614 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
619 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
641 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
717 !(BTRFS_I(inode)->prop_compress)) {
718 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
736 extent_range_redirty_for_io(inode, start, end);
768 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
769 struct btrfs_fs_info *fs_info = inode->root->fs_info;
774 struct btrfs_root *root = inode->root;
775 struct extent_io_tree *io_tree = &inode->io_tree;
793 ret = cow_file_range(inode, async_chunk->locked_page,
808 extent_write_locked_range(&inode->vfs_inode,
838 extent_range_redirty_for_io(&inode->vfs_inode,
851 em = create_io_em(inode, async_extent->start,
865 ret = btrfs_add_ordered_extent_compress(inode,
873 btrfs_drop_extent_cache(inode, async_extent->start,
883 extent_clear_unlock_delalloc(inode, async_extent->start,
889 if (btrfs_submit_compressed_write(inode, async_extent->start,
900 p->mapping = inode->vfs_inode.i_mapping;
904 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
918 extent_clear_unlock_delalloc(inode, async_extent->start,
932 static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
935 struct extent_map_tree *em_tree = &inode->extent_tree;
944 * first block in this inode and use that as a hint. If that
977 static noinline int cow_file_range(struct btrfs_inode *inode,
982 struct btrfs_root *root = inode->root;
997 if (btrfs_is_free_space_inode(inode)) {
1006 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
1010 ret = cow_file_range_inline(inode, start, end, 0,
1019 extent_clear_unlock_delalloc(inode, start, end, NULL,
1034 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1035 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
1064 em = create_io_em(inode, start, ins.offset, /* len */
1078 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1085 ret = btrfs_reloc_clone_csums(inode, start,
1099 btrfs_drop_extent_cache(inode, start,
1115 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1139 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1159 extent_clear_unlock_delalloc(inode, start,
1168 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1186 btrfs_add_delayed_iput(async_chunk->inode);
1187 async_chunk->inode = NULL;
1205 * ->inode could be NULL if async_chunk_start has failed to compress,
1210 if (async_chunk->inode)
1224 if (async_chunk->inode)
1225 btrfs_add_delayed_iput(async_chunk->inode);
1236 static int cow_file_range_async(struct btrfs_inode *inode,
1242 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1254 unlock_extent(&inode->io_tree, start, end);
1256 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
1276 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1294 ihold(&inode->vfs_inode);
1296 async_chunk[i].inode = &inode->vfs_inode;
1373 static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
1377 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1378 const bool is_reloc_ino = (inode->root->root_key.objectid ==
1381 struct extent_io_tree *io_tree = &inode->io_tree;
1389 * respective file range (either there is prealloc extent or the inode
1409 * If we need to fallback to cow and the inode corresponds to a free
1410 * space cache inode or an inode of the data relocation tree, we must
1421 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1436 return cow_file_range(inode, locked_page, start, end, page_started,
1447 static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
1453 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1454 struct btrfs_root *root = inode->root;
1460 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1461 u64 ino = btrfs_ino(inode);
1467 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1531 * more extents for this inode
1687 ret = fallback_to_cow(inode, locked_page,
1699 em = create_io_em(inode, cur_offset, num_bytes,
1711 ret = btrfs_add_ordered_extent(inode, cur_offset,
1716 btrfs_drop_extent_cache(inode, cur_offset,
1722 ret = btrfs_add_ordered_extent(inode, cur_offset,
1741 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1744 extent_clear_unlock_delalloc(inode, cur_offset,
1770 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1781 extent_clear_unlock_delalloc(inode, cur_offset, end,
1792 static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
1795 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1796 !(inode->flags & BTRFS_INODE_PREALLOC))
1804 if (inode->defrag_bytes &&
1805 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
1815 int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
1820 int force_cow = need_force_cow(inode, start, end);
1822 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1823 ret = run_delalloc_nocow(inode, locked_page, start, end,
1825 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1826 ret = run_delalloc_nocow(inode, locked_page, start, end,
1828 } else if (!inode_can_compress(inode) ||
1829 !inode_need_compress(inode, start, end)) {
1830 ret = cow_file_range(inode, locked_page, start, end,
1833 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1834 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
1838 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1843 void btrfs_split_delalloc_extent(struct inode *inode,
1869 spin_lock(&BTRFS_I(inode)->lock);
1870 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1871 spin_unlock(&BTRFS_I(inode)->lock);
1879 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1896 spin_lock(&BTRFS_I(inode)->lock);
1897 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1898 spin_unlock(&BTRFS_I(inode)->lock);
1927 spin_lock(&BTRFS_I(inode)->lock);
1928 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1929 spin_unlock(&BTRFS_I(inode)->lock);
1933 struct inode *inode)
1935 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1938 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1939 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1942 &BTRFS_I(inode)->runtime_flags);
1957 struct btrfs_inode *inode)
1961 if (!list_empty(&inode->delalloc_inodes)) {
1962 list_del_init(&inode->delalloc_inodes);
1964 &inode->runtime_flags);
1977 struct btrfs_inode *inode)
1980 __btrfs_del_delalloc_inode(root, inode);
1985 * Properly track delayed allocation bytes in the inode and to maintain the
1988 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2001 struct btrfs_root *root = BTRFS_I(inode)->root;
2004 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
2006 spin_lock(&BTRFS_I(inode)->lock);
2007 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2008 spin_unlock(&BTRFS_I(inode)->lock);
2016 spin_lock(&BTRFS_I(inode)->lock);
2017 BTRFS_I(inode)->delalloc_bytes += len;
2019 BTRFS_I(inode)->defrag_bytes += len;
2021 &BTRFS_I(inode)->runtime_flags))
2022 btrfs_add_delalloc_inodes(root, inode);
2023 spin_unlock(&BTRFS_I(inode)->lock);
2028 spin_lock(&BTRFS_I(inode)->lock);
2029 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2031 spin_unlock(&BTRFS_I(inode)->lock);
2039 void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2042 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2048 spin_lock(&inode->lock);
2049 inode->defrag_bytes -= len;
2050 spin_unlock(&inode->lock);
2059 struct btrfs_root *root = inode->root;
2060 bool do_list = !btrfs_is_free_space_inode(inode);
2062 spin_lock(&inode->lock);
2063 btrfs_mod_outstanding_extents(inode, -num_extents);
2064 spin_unlock(&inode->lock);
2073 btrfs_delalloc_release_metadata(inode, len, false);
2086 spin_lock(&inode->lock);
2087 inode->delalloc_bytes -= len;
2088 if (do_list && inode->delalloc_bytes == 0 &&
2090 &inode->runtime_flags))
2091 btrfs_del_delalloc_inode(root, inode);
2092 spin_unlock(&inode->lock);
2097 spin_lock(&inode->lock);
2098 ASSERT(inode->new_delalloc_bytes >= len);
2099 inode->new_delalloc_bytes -= len;
2100 spin_unlock(&inode->lock);
2121 struct inode *inode = page->mapping->host;
2122 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2155 struct inode *inode = private_data;
2157 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
2178 blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2183 struct btrfs_root *root = BTRFS_I(inode)->root;
2187 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
2189 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
2191 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
2200 ret = btrfs_submit_compressed_read(inode, bio,
2205 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
2216 0, inode, btrfs_submit_bio_start);
2219 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
2236 * given a list of ordered sums record them in the inode. This happens
2255 static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2269 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2282 ret = set_extent_bit(&inode->io_tree, search_start,
2295 int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2301 if (start >= i_size_read(&inode->vfs_inode) &&
2302 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2311 ret = btrfs_find_new_delalloc_bytes(inode, start,
2318 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2325 struct inode *inode;
2336 struct btrfs_inode *inode;
2344 inode = BTRFS_I(fixup->inode);
2352 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2381 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2382 btrfs_delalloc_release_space(inode, data_reserved,
2397 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
2403 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
2405 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2413 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2428 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2430 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2432 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2455 btrfs_add_delayed_iput(&inode->vfs_inode);
2471 struct inode *inode = page->mapping->host;
2472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2494 * We are already holding a reference to this inode from
2499 ihold(inode);
2504 fixup->inode = inode;
2511 struct btrfs_inode *inode, u64 file_pos,
2515 struct btrfs_root *root = inode->root;
2539 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2546 ins.objectid = btrfs_ino(inode);
2565 inode_add_bytes(&inode->vfs_inode, num_bytes);
2571 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
2575 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
2618 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2630 struct inode *inode = ordered_extent->inode;
2631 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2632 struct btrfs_root *root = BTRFS_I(inode)->root;
2634 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2655 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
2662 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
2675 btrfs_inode_safe_disk_i_size_write(inode, 0);
2685 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
2686 ret = btrfs_update_inode_fallback(trans, root, inode);
2705 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
2711 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
2725 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2739 btrfs_inode_safe_disk_i_size_write(inode, 0);
2740 ret = btrfs_update_inode_fallback(trans, root, inode);
2752 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2765 * extent, and mark the inode with the error if it wasn't
2772 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
2779 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
2814 btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent);
2834 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2835 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2842 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2846 if (btrfs_is_free_space_inode(inode))
2855 static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2859 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2879 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2899 struct inode *inode = page->mapping->host;
2900 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2901 struct btrfs_root *root = BTRFS_I(inode)->root;
2908 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
2917 phy_offset >>= inode->i_sb->s_blocksize_bits;
2918 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2923 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2925 * @inode: The inode we want to perform iput on
2929 * the inode to the delayed iput machinery. Delayed iputs are processed at
2932 void btrfs_add_delayed_iput(struct inode *inode)
2934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2935 struct btrfs_inode *binode = BTRFS_I(inode);
2937 if (atomic_add_unless(&inode->i_count, -1, 1))
2950 struct btrfs_inode *inode)
2952 list_del_init(&inode->delayed_iput);
2954 iput(&inode->vfs_inode);
2961 struct btrfs_inode *inode)
2963 if (!list_empty(&inode->delayed_iput)) {
2965 if (!list_empty(&inode->delayed_iput))
2966 run_delayed_iput_locked(fs_info, inode);
2976 struct btrfs_inode *inode;
2978 inode = list_first_entry(&fs_info->delayed_iputs,
2980 run_delayed_iput_locked(fs_info, inode);
3006 * This creates an orphan entry for the given inode in case something goes wrong
3010 struct btrfs_inode *inode)
3014 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3025 * this particular inode.
3028 struct btrfs_inode *inode)
3030 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3044 struct inode *inode;
3094 * crossing root thing. we store the inode number in the
3110 inode = btrfs_iget(fs_info->sb, last_objectid, root);
3111 ret = PTR_ERR_OR_ZERO(inode);
3123 * b) a free space cache inode
3147 * If we have an inode with links, there are a couple of
3162 * deleted but wasn't. The inode number may have been reused,
3165 if (ret == -ENOENT || inode->i_nlink) {
3167 iput(inode);
3186 iput(inode);
3213 * slot is the slot the inode is in, objectid is the objectid of the inode
3261 * it goes inode, inode backrefs, xattrs, extents,
3262 * so if there are a ton of hard links to an inode there can
3270 * something larger than an xattr. We have to assume the inode
3279 * read an inode from the btree into the in-memory inode
3281 static int btrfs_read_locked_inode(struct inode *inode,
3284 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3288 struct btrfs_root *root = BTRFS_I(inode)->root;
3297 ret = btrfs_fill_inode(inode, &rdev);
3307 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3323 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3324 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3325 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3326 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3327 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3328 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3329 round_up(i_size_read(inode), fs_info->sectorsize));
3331 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3332 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3334 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3335 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3337 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3338 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3340 BTRFS_I(inode)->i_otime.tv_sec =
3342 BTRFS_I(inode)->i_otime.tv_nsec =
3345 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3346 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3347 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3349 inode_set_iversion_queried(inode,
3351 inode->i_generation = BTRFS_I(inode)->generation;
3352 inode->i_rdev = 0;
3355 BTRFS_I(inode)->index_cnt = (u64)-1;
3356 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3365 * This is required for both inode re-read from disk and delayed inode
3368 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3370 &BTRFS_I(inode)->runtime_flags);
3374 * against the inode was last made. So here we assume the inode might
3377 * between the inode and its parent if the inode is fsync'ed and the log
3384 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3389 * We must make sure that when we fsync our inode foo we also log its
3390 * parent inode, otherwise after log replay the parent still has the
3391 * dentry with the "bar" name but our inode foo has a link count of 1
3392 * and doesn't have an inode ref with the name "bar" anymore.
3396 * transaction commits on fsync if our inode is a directory, or if our
3397 * inode is not a directory, logging its parent unnecessarily.
3399 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3403 * of the last transaction where this inode was used for a reflink
3404 * operation, so after eviction and reloading the inode we must be
3405 * pessimistic and assume the last transaction that modified the inode.
3407 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3410 if (inode->i_nlink != 1 ||
3415 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3423 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3428 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3437 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3440 ret = btrfs_load_inode_props(inode, path);
3444 btrfs_ino(BTRFS_I(inode)),
3451 cache_no_acl(inode);
3453 switch (inode->i_mode & S_IFMT) {
3455 inode->i_mapping->a_ops = &btrfs_aops;
3456 inode->i_fop = &btrfs_file_operations;
3457 inode->i_op = &btrfs_file_inode_operations;
3460 inode->i_fop = &btrfs_dir_file_operations;
3461 inode->i_op = &btrfs_dir_inode_operations;
3464 inode->i_op = &btrfs_symlink_inode_operations;
3465 inode_nohighmem(inode);
3466 inode->i_mapping->a_ops = &btrfs_aops;
3469 inode->i_op = &btrfs_special_inode_operations;
3470 init_special_inode(inode, inode->i_mode, rdev);
3474 btrfs_sync_inode_flags_to_i_flags(inode);
3479 * given a leaf and an inode, copy the inode fields into the leaf
3484 struct inode *inode)
3490 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3491 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3492 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3493 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3494 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3497 inode->i_atime.tv_sec);
3499 inode->i_atime.tv_nsec);
3502 inode->i_mtime.tv_sec);
3504 inode->i_mtime.tv_nsec);
3507 inode->i_ctime.tv_sec);
3509 inode->i_ctime.tv_nsec);
3512 BTRFS_I(inode)->i_otime.tv_sec);
3514 BTRFS_I(inode)->i_otime.tv_nsec);
3516 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3518 BTRFS_I(inode)->generation);
3519 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3521 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3522 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3527 * copy everything in the in-memory inode into the btree.
3530 struct btrfs_root *root, struct inode *inode)
3542 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3554 fill_inode_item(trans, leaf, inode_item, inode);
3556 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
3564 * copy everything in the in-memory inode into the btree.
3567 struct btrfs_root *root, struct inode *inode)
3573 * If the inode is a free space inode, we can deadlock during commit
3576 * The data relocation inode should also be directly updated
3579 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
3584 ret = btrfs_delayed_update_inode(trans, root, inode);
3586 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
3590 return btrfs_update_inode_item(trans, root, inode);
3595 struct inode *inode)
3599 ret = btrfs_update_inode(trans, root, inode);
3601 return btrfs_update_inode_item(trans, root, inode);
3606 * unlink helper that gets used here in inode.c and in the tree logging
3608 * also drops the back refs in the inode to the directory
3613 struct btrfs_inode *inode,
3621 u64 ino = btrfs_ino(inode);
3644 * the inode ref, since we get the inode ref, remove it directly,
3647 * But if we have dir index, needn't search inode ref to get it.
3648 * Since the inode ref is close to the inode item, it is better
3650 * we update the inode item.
3652 if (inode->dir_index) {
3653 ret = btrfs_delayed_delete_inode_ref(inode);
3655 index = inode->dir_index;
3664 "failed to delete reference to %.*s, inode %llu parent %llu",
3676 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3695 * the inode we can run the delayed iput here without any issues as the
3699 btrfs_run_delayed_iput(fs_info, inode);
3706 inode_inc_iversion(&inode->vfs_inode);
3708 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3709 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3717 struct btrfs_inode *dir, struct btrfs_inode *inode,
3721 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3723 drop_nlink(&inode->vfs_inode);
3724 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
3737 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
3745 * 1 for the inode ref
3746 * 1 for the inode
3751 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3755 struct inode *inode = d_inode(dentry);
3771 if (inode->i_nlink == 0) {
3772 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
3784 struct inode *dir, struct dentry *dentry)
3787 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
3799 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3800 objectid = inode->root->root_key.objectid;
3801 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3802 objectid = inode->location.objectid;
3830 * This is a placeholder inode for a subvolume we didn't have a
3838 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3943 struct inode *inode;
3977 inode = igrab(&entry->vfs_inode);
3978 if (inode) {
3980 if (atomic_read(&inode->i_count) > 1)
3981 d_prune_aliases(inode);
3983 * btrfs_drop_inode will have it removed from the inode
3986 iput(inode);
4000 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4004 struct inode *inode = d_inode(dentry);
4005 struct btrfs_root *dest = BTRFS_I(inode)->root;
4015 * inside the inode lock so the error handling that has to drop the bit
4046 * One for dir inode,
4111 inode->i_flags |= S_DEAD;
4139 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4141 struct inode *inode = d_inode(dentry);
4147 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4149 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
4156 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4161 err = btrfs_orphan_add(trans, BTRFS_I(inode));
4165 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4172 btrfs_i_size_write(BTRFS_I(inode), 0);
4209 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4213 struct inode *inode,
4234 u64 ino = btrfs_ino(BTRFS_I(inode));
4248 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
4258 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4266 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
4273 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4277 if (min_type == 0 && root == BTRFS_I(inode)->root)
4278 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
4334 BTRFS_I(inode), leaf, fi,
4341 BTRFS_I(inode), leaf, fi, path->slots[0],
4380 inode_sub_bytes(inode, num_dec);
4395 inode_sub_bytes(inode, num_dec);
4430 inode_sub_bytes(inode, item_end + 1 - new_size);
4438 if (root == BTRFS_I(inode)->root) {
4439 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4547 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4548 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4558 * @inode - inode that we're zeroing
4567 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4571 struct address_space *mapping = inode->i_mapping;
4572 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4595 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4598 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4606 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4609 btrfs_free_reserved_data_space(BTRFS_I(inode),
4616 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
4618 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
4641 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
4652 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
4656 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
4682 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4689 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4692 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
4695 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
4700 btrfs_check_nocow_unlock(BTRFS_I(inode));
4705 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4713 * Still need to make sure the inode looks like it's been updated so
4717 BTRFS_I(inode)->last_trans = fs_info->generation;
4718 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4719 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4726 * 1 - for updating the inode.
4732 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4739 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4744 btrfs_update_inode(trans, root, inode);
4755 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4757 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4758 struct btrfs_root *root = BTRFS_I(inode)->root;
4759 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4762 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4775 err = btrfs_truncate_block(inode, oldsize, 0, 0);
4782 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
4786 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
4800 err = maybe_insert_hole(root, inode, cur_offset,
4805 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4810 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
4815 &BTRFS_I(inode)->runtime_flags);
4835 btrfs_drop_extent_cache(BTRFS_I(inode),
4842 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4859 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4861 struct btrfs_root *root = BTRFS_I(inode)->root;
4863 loff_t oldsize = i_size_read(inode);
4875 inode_inc_iversion(inode);
4877 inode->i_ctime = inode->i_mtime =
4878 current_time(inode);
4890 ret = btrfs_cont_expand(inode, oldsize, newsize);
4902 i_size_write(inode, newsize);
4903 btrfs_inode_safe_disk_i_size_write(inode, 0);
4904 pagecache_isize_extended(inode, oldsize, newsize);
4905 ret = btrfs_update_inode(trans, root, inode);
4917 &BTRFS_I(inode)->runtime_flags);
4919 truncate_setsize(inode, newsize);
4921 inode_dio_wait(inode);
4923 ret = btrfs_truncate(inode, newsize == oldsize);
4924 if (ret && inode->i_nlink) {
4933 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
4936 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
4945 struct inode *inode = d_inode(dentry);
4946 struct btrfs_root *root = BTRFS_I(inode)->root;
4956 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
4957 err = btrfs_setsize(inode, attr);
4963 setattr_copy(inode, attr);
4964 inode_inc_iversion(inode);
4965 err = btrfs_dirty_inode(inode);
4968 err = posix_acl_chmod(inode, inode->i_mode);
4975 * While truncating the inode pages during eviction, we get the VFS calling
4976 * btrfs_invalidatepage() against each page of the inode. This is slow because
4981 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4986 static void evict_inode_truncate_pages(struct inode *inode)
4988 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4989 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4992 ASSERT(inode->i_state & I_FREEING);
4993 truncate_inode_pages_final(&inode->i_data);
5021 * queue kthread), inode references (inode->i_count) were not taken
5024 * anyone else holding locked ranges without having bumped the inode's
5025 * reference count - if we don't do it, when they access the inode's
5055 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5120 void btrfs_evict_inode(struct inode *inode)
5122 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5124 struct btrfs_root *root = BTRFS_I(inode)->root;
5128 trace_btrfs_inode_evict(inode);
5131 clear_inode(inode);
5135 evict_inode_truncate_pages(inode);
5137 if (inode->i_nlink &&
5140 btrfs_is_free_space_inode(BTRFS_I(inode))))
5143 if (is_bad_inode(inode))
5146 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
5151 if (inode->i_nlink > 0) {
5157 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5167 btrfs_i_size_write(BTRFS_I(inode), 0);
5176 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
5188 * the tree. They will be cleaned up on the next mount. If the inode
5198 btrfs_orphan_del(trans, BTRFS_I(inode));
5205 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
5215 btrfs_remove_delayed_node(BTRFS_I(inode));
5216 clear_inode(inode);
5226 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5264 * when we hit a tree root in a directory, the btrfs part of the inode
5269 struct inode *dir,
5330 static void inode_tree_add(struct inode *inode)
5332 struct btrfs_root *root = BTRFS_I(inode)->root;
5336 struct rb_node *new = &BTRFS_I(inode)->rb_node;
5337 u64 ino = btrfs_ino(BTRFS_I(inode));
5339 if (inode_unhashed(inode))
5366 static void inode_tree_del(struct btrfs_inode *inode)
5368 struct btrfs_root *root = inode->root;
5372 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5373 rb_erase(&inode->rb_node, &root->inode_tree);
5374 RB_CLEAR_NODE(&inode->rb_node);
5389 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5393 inode->i_ino = args->ino;
5394 BTRFS_I(inode)->location.objectid = args->ino;
5395 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5396 BTRFS_I(inode)->location.offset = 0;
5397 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5398 BUG_ON(args->root && !BTRFS_I(inode)->root);
5402 static int btrfs_find_actor(struct inode *inode, void *opaque)
5406 return args->ino == BTRFS_I(inode)->location.objectid &&
5407 args->root == BTRFS_I(inode)->root;
5410 static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5413 struct inode *inode;
5420 inode = iget5_locked(s, hashval, btrfs_find_actor,
5423 return inode;
5427 * Get an inode object given its inode number and corresponding root.
5432 struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5435 struct inode *inode;
5437 inode = btrfs_iget_locked(s, ino, root);
5438 if (!inode)
5441 if (inode->i_state & I_NEW) {
5444 ret = btrfs_read_locked_inode(inode, path);
5446 inode_tree_add(inode);
5447 unlock_new_inode(inode);
5449 iget_failed(inode);
5452 * btrfs_read_locked_inode, this means the inode item
5457 inode = ERR_PTR(ret);
5461 return inode;
5464 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
5469 static struct inode *new_simple_dir(struct super_block *s,
5473 struct inode *inode = new_inode(s);
5475 if (!inode)
5478 BTRFS_I(inode)->root = btrfs_grab_root(root);
5479 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5480 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5482 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5484 * We only need lookup, the rest is read-only and there's no inode
5487 inode->i_op = &simple_dir_inode_operations;
5488 inode->i_opflags &= ~IOP_XATTR;
5489 inode->i_fop = &simple_dir_operations;
5490 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5491 inode->i_mtime = current_time(inode);
5492 inode->i_atime = inode->i_mtime;
5493 inode->i_ctime = inode->i_mtime;
5494 BTRFS_I(inode)->i_otime = inode->i_mtime;
5496 return inode;
5499 static inline u8 btrfs_inode_type(struct inode *inode)
5514 return fs_umode_to_ftype(inode->i_mode);
5517 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5520 struct inode *inode;
5535 inode = btrfs_iget(dir->i_sb, location.objectid, root);
5536 if (IS_ERR(inode))
5537 return inode;
5539 /* Do extra check against inode mode with di_type */
5540 if (btrfs_inode_type(inode) != di_type) {
5542 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5543 inode->i_mode, btrfs_inode_type(inode),
5545 iput(inode);
5548 return inode;
5555 inode = ERR_PTR(ret);
5557 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5559 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
5564 if (!IS_ERR(inode) && root != sub_root) {
5566 if (!sb_rdonly(inode->i_sb))
5570 iput(inode);
5571 inode = ERR_PTR(ret);
5575 return inode;
5581 struct inode *inode = d_inode(dentry);
5583 if (!inode && !IS_ROOT(dentry))
5584 inode = d_inode(dentry->d_parent);
5586 if (inode) {
5587 root = BTRFS_I(inode)->root;
5591 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5597 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5600 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
5602 if (inode == ERR_PTR(-ENOENT))
5603 inode = NULL;
5604 return d_splice_alias(inode, dentry);
5616 static int btrfs_opendir(struct inode *inode, struct file *file)
5659 struct inode *inode = file_inode(file);
5660 struct btrfs_root *root = BTRFS_I(inode)->root;
5691 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
5696 key.objectid = btrfs_ino(BTRFS_I(inode));
5791 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
5798 * inode changes. But, it is most likely to find the inode in cache.
5802 static int btrfs_dirty_inode(struct inode *inode)
5804 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5805 struct btrfs_root *root = BTRFS_I(inode)->root;
5809 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5816 ret = btrfs_update_inode(trans, root, inode);
5824 ret = btrfs_update_inode(trans, root, inode);
5827 if (BTRFS_I(inode)->delayed_node)
5835 * ENOSPC for updating the inode in the case of file write and mmap writes.
5837 static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
5840 struct btrfs_root *root = BTRFS_I(inode)->root;
5847 dirty |= inode_maybe_inc_iversion(inode, dirty);
5849 inode->i_ctime = *now;
5851 inode->i_mtime = *now;
5853 inode->i_atime = *now;
5854 return dirty ? btrfs_dirty_inode(inode) : 0;
5862 static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
5864 struct btrfs_root *root = inode->root;
5870 key.objectid = btrfs_ino(inode);
5893 inode->index_cnt = 2;
5902 if (found_key.objectid != btrfs_ino(inode) ||
5904 inode->index_cnt = 2;
5908 inode->index_cnt = found_key.offset + 1;
5937 static int btrfs_insert_inode_locked(struct inode *inode)
5941 args.ino = BTRFS_I(inode)->location.objectid;
5942 args.root = BTRFS_I(inode)->root;
5944 return insert_inode_locked4(inode,
5945 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5950 * Inherit flags from the parent inode.
5954 static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5964 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5965 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5967 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5968 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5972 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5973 if (S_ISREG(inode->i_mode))
5974 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5977 btrfs_sync_inode_flags_to_i_flags(inode);
5980 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5982 struct inode *dir,
5988 struct inode *inode;
6005 inode = new_inode(fs_info->sb);
6007 if (!inode) {
6014 * we fill in an inode item with the correct link count.
6017 set_nlink(inode, 0);
6020 * we have to initialize this early, so we can reclaim the inode
6023 inode->i_ino = objectid;
6031 iput(inode);
6042 BTRFS_I(inode)->index_cnt = 2;
6043 BTRFS_I(inode)->dir_index = *index;
6044 BTRFS_I(inode)->root = btrfs_grab_root(root);
6045 BTRFS_I(inode)->generation = trans->transid;
6046 inode->i_generation = BTRFS_I(inode)->generation;
6049 * We could have gotten an inode number from somebody who was fsynced
6054 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6076 location = &BTRFS_I(inode)->location;
6081 ret = btrfs_insert_inode_locked(inode);
6083 iput(inode);
6092 inode_init_owner(inode, dir, mode);
6093 inode_set_bytes(inode, 0);
6095 inode->i_mtime = current_time(inode);
6096 inode->i_atime = inode->i_mtime;
6097 inode->i_ctime = inode->i_mtime;
6098 BTRFS_I(inode)->i_otime = inode->i_mtime;
6104 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6118 btrfs_inherit_iflags(inode, dir);
6122 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6124 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6128 inode_tree_add(inode);
6130 trace_btrfs_inode_new(inode);
6131 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
6135 ret = btrfs_inode_inherit_props(trans, inode, dir);
6139 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
6141 return inode;
6144 discard_new_inode(inode);
6153 * utility function to add 'inode' into 'parent_inode' with
6156 * inode to the parent directory.
6159 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
6165 u64 ino = btrfs_ino(inode);
6169 memcpy(&key, &inode->root->root_key, sizeof(key));
6190 btrfs_inode_type(&inode->vfs_inode), index);
6243 struct btrfs_inode *inode, int backref, u64 index)
6245 int err = btrfs_add_link(trans, dir, inode,
6253 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6259 struct inode *inode = NULL;
6265 * 2 for inode item and ref
6277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6280 if (IS_ERR(inode)) {
6281 err = PTR_ERR(inode);
6282 inode = NULL;
6287 * If the active LSM wants to access the inode during
6292 inode->i_op = &btrfs_special_inode_operations;
6293 init_special_inode(inode, inode->i_mode, rdev);
6295 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6299 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6304 btrfs_update_inode(trans, root, inode);
6305 d_instantiate_new(dentry, inode);
6310 if (err && inode) {
6311 inode_dec_link_count(inode);
6312 discard_new_inode(inode);
6317 static int btrfs_create(struct inode *dir, struct dentry *dentry,
6323 struct inode *inode = NULL;
6329 * 2 for inode item and ref
6341 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6344 if (IS_ERR(inode)) {
6345 err = PTR_ERR(inode);
6346 inode = NULL;
6350 * If the active LSM wants to access the inode during
6355 inode->i_fop = &btrfs_file_operations;
6356 inode->i_op = &btrfs_file_inode_operations;
6357 inode->i_mapping->a_ops = &btrfs_aops;
6359 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6363 err = btrfs_update_inode(trans, root, inode);
6367 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6372 d_instantiate_new(dentry, inode);
6376 if (err && inode) {
6377 inode_dec_link_count(inode);
6378 discard_new_inode(inode);
6384 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6389 struct inode *inode = d_inode(old_dentry);
6390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6396 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
6399 if (inode->i_nlink >= BTRFS_LINK_MAX)
6407 * 2 items for inode and inode ref
6409 * 1 item for parent inode
6412 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
6419 /* There are several dir indexes for this inode, clear the cache. */
6420 BTRFS_I(inode)->dir_index = 0ULL;
6421 inc_nlink(inode);
6422 inode_inc_iversion(inode);
6423 inode->i_ctime = current_time(inode);
6424 ihold(inode);
6425 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6427 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6435 err = btrfs_update_inode(trans, root, inode);
6438 if (inode->i_nlink == 1) {
6443 err = btrfs_orphan_del(trans, BTRFS_I(inode));
6447 d_instantiate(dentry, inode);
6448 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
6455 inode_dec_link_count(inode);
6456 iput(inode);
6462 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6465 struct inode *inode = NULL;
6473 * 2 items for inode and ref
6485 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6488 if (IS_ERR(inode)) {
6489 err = PTR_ERR(inode);
6490 inode = NULL;
6494 /* these must be set before we unlock the inode */
6495 inode->i_op = &btrfs_dir_inode_operations;
6496 inode->i_fop = &btrfs_dir_file_operations;
6498 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6502 btrfs_i_size_write(BTRFS_I(inode), 0);
6503 err = btrfs_update_inode(trans, root, inode);
6507 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6513 d_instantiate_new(dentry, inode);
6517 if (err && inode) {
6518 inode_dec_link_count(inode);
6519 discard_new_inode(inode);
6573 * @inode: file to search in
6589 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6593 struct btrfs_fs_info *fs_info = inode->root->fs_info;
6597 u64 objectid = btrfs_ino(inode);
6600 struct btrfs_root *root = inode->root;
6605 struct extent_map_tree *em_tree = &inode->extent_tree;
6606 struct extent_io_tree *io_tree = &inode->io_tree;
6645 path->recurse = btrfs_is_free_space_inode(inode);
6679 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6682 "regular/prealloc extent found for non-regular inode %llu",
6683 btrfs_ino(inode));
6686 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6689 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6722 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
6794 trace_btrfs_get_extent(root, inode, em);
6803 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
6814 em = btrfs_get_extent(inode, NULL, 0, start, len);
6839 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
6930 static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
6944 em = create_io_em(inode, start, len, orig_start, block_start,
6951 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6956 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
6965 static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
6968 struct btrfs_root *root = inode->root;
6975 alloc_hint = get_extent_allocation_hint(inode, start, len);
6981 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7015 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7019 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7023 struct btrfs_root *root = BTRFS_I(inode)->root;
7024 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7033 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7040 btrfs_ino(BTRFS_I(inode)), offset, 0);
7056 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7132 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
7161 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7168 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7175 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
7186 (!writing || !filemap_range_has_page(inode->i_mapping,
7190 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7202 * and locked its range in the inode's io tree, and a
7242 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7257 em_tree = &inode->extent_tree;
7279 btrfs_drop_extent_cache(inode, em->start,
7301 struct inode *inode,
7305 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7312 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7319 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7331 if (can_nocow_extent(inode, start, &len, &orig_start,
7336 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
7351 * For inode marked NODATACOW or extent marked PREALLOC,
7362 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
7375 if (start + len > i_size_read(inode))
7376 i_size_write(inode, start + len);
7383 static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7387 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7421 &BTRFS_I(inode)->runtime_flags)) {
7422 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7436 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7452 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
7457 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
7498 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7516 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7543 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7547 btrfs_delalloc_release_space(BTRFS_I(inode),
7550 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7557 static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7567 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7575 __endio_write_update_ordered(BTRFS_I(inode), pos,
7578 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7585 btrfs_delalloc_release_space(BTRFS_I(inode),
7588 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7616 __endio_write_update_ordered(BTRFS_I(dip->inode),
7621 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7630 static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7635 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7651 static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7655 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7657 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
7658 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7659 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7674 (!csum || !check_data_csum(inode, io_bio, icsum,
7679 btrfs_ino(BTRFS_I(inode)),
7684 status = btrfs_submit_read_repair(inode,
7703 static void __endio_write_update_ordered(struct btrfs_inode *inode,
7707 struct btrfs_fs_info *fs_info = inode->root->fs_info;
7714 if (btrfs_is_free_space_inode(inode))
7721 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7749 struct inode *inode = private_data;
7751 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
7760 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
7762 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7768 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
7780 struct inode *inode, u64 file_offset, int async_submit)
7782 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7789 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7797 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
7802 file_offset, inode,
7810 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
7817 csum_offset >>= inode->i_sb->s_blocksize_bits;
7832 struct inode *inode,
7836 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7842 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7846 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7854 dip->inode = inode;
7863 static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7867 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7883 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7886 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7899 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7953 status = btrfs_submit_dio_bio(bio, inode, file_offset,
8049 struct inode *inode = file->f_mapping->host;
8050 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8071 if (offset + count <= inode->i_size) {
8072 inode_unlock(inode);
8075 down_read(&BTRFS_I(inode)->dio_sem);
8093 up_read(&BTRFS_I(inode)->dio_sem);
8096 inode_lock(inode);
8102 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8107 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
8111 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
8116 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8123 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8133 struct inode *inode = page->mapping->host;
8144 * VM, we need to make sure we have the inode referenced for the ordered
8147 if (!igrab(inode)) {
8152 btrfs_add_delayed_iput(inode);
8212 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8213 struct extent_io_tree *tree = &inode->io_tree;
8220 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
8253 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
8274 tree = &inode->ordered_tree;
8283 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8313 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
8337 * truncate_setsize() writes the inode size before removing pages, once we have
8345 struct inode *inode = file_inode(vmf->vma->vm_file);
8346 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8347 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8364 sb_start_pagefault(inode->i_sb);
8377 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8393 size = i_size_read(inode);
8395 if ((page->mapping != inode->i_mapping) ||
8409 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8425 btrfs_delalloc_release_space(BTRFS_I(inode),
8438 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8442 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
8467 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
8471 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8472 sb_end_pagefault(inode->i_sb);
8479 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8480 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
8483 sb_end_pagefault(inode->i_sb);
8488 static int btrfs_truncate(struct inode *inode, bool skip_writeback)
8490 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8491 struct btrfs_root *root = BTRFS_I(inode)->root;
8499 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8509 * 1) We need to reserve space to update our inode.
8519 * doesn't end up using space reserved for updating the inode. We also
8521 * means we need to be able to update the inode several times, and we
8531 * updating the inode.
8541 * 1 for updating the inode.
8557 * first truncate that entire inode. So set this flag so we write out
8558 * all of the extents in the inode to the sync log so we're completely
8561 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
8565 ret = btrfs_truncate_inode_items(trans, root, inode,
8566 inode->i_size,
8572 ret = btrfs_update_inode(trans, root, inode);
8603 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8611 btrfs_inode_safe_disk_i_size_write(inode, 0);
8618 ret2 = btrfs_update_inode(trans, root, inode);
8634 * create a new subvolume directory/inode (helper for the ioctl).
8641 struct inode *inode;
8645 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8649 if (IS_ERR(inode))
8650 return PTR_ERR(inode);
8651 inode->i_op = &btrfs_dir_inode_operations;
8652 inode->i_fop = &btrfs_dir_file_operations;
8654 set_nlink(inode, 1);
8655 btrfs_i_size_write(BTRFS_I(inode), 0);
8656 unlock_new_inode(inode);
8664 err = btrfs_update_inode(trans, new_root, inode);
8666 iput(inode);
8670 struct inode *btrfs_alloc_inode(struct super_block *sb)
8674 struct inode *inode;
8711 inode = &ei->vfs_inode;
8713 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8715 IO_TREE_INODE_IO_FAILURE, inode);
8717 IO_TREE_INODE_FILE_EXTENT, inode);
8728 return inode;
8732 void btrfs_test_destroy_inode(struct inode *inode)
8734 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
8735 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8739 void btrfs_free_inode(struct inode *inode)
8741 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8744 void btrfs_destroy_inode(struct inode *vfs_inode)
8747 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8748 struct btrfs_root *root = inode->root;
8752 WARN_ON(inode->block_rsv.reserved);
8753 WARN_ON(inode->block_rsv.size);
8754 WARN_ON(inode->outstanding_extents);
8755 WARN_ON(inode->delalloc_bytes);
8756 WARN_ON(inode->new_delalloc_bytes);
8757 WARN_ON(inode->csum_bytes);
8758 WARN_ON(inode->defrag_bytes);
8761 * This can happen where we create an inode, but somebody else also
8762 * created the same inode and we need to destroy the one we already
8769 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8774 "found ordered extent %llu %llu on inode cleanup",
8776 btrfs_remove_ordered_extent(inode, ordered);
8781 btrfs_qgroup_check_reserved_leak(inode);
8782 inode_tree_del(inode);
8783 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8784 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8785 btrfs_put_root(inode->root);
8788 int btrfs_drop_inode(struct inode *inode)
8790 struct btrfs_root *root = BTRFS_I(inode)->root;
8799 return generic_drop_inode(inode);
8866 struct inode *inode = d_inode(path->dentry);
8867 u32 blocksize = inode->i_sb->s_blocksize;
8868 u32 bi_flags = BTRFS_I(inode)->flags;
8871 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8872 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8887 generic_fillattr(inode, stat);
8888 stat->dev = BTRFS_I(inode)->root->anon_dev;
8890 spin_lock(&BTRFS_I(inode)->lock);
8891 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
8892 spin_unlock(&BTRFS_I(inode)->lock);
8893 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
8898 static int btrfs_rename_exchange(struct inode *old_dir,
8900 struct inode *new_dir,
8907 struct inode *new_inode = new_dentry->d_inode;
8908 struct inode *old_inode = old_dentry->d_inode;
8922 * same inode namespace. Two subvolumes (represented as directory) can
8923 * be exchanged as they're a logical link and have a fixed inode number.
9004 /* Update inode version and ctime/mtime. */
9024 } else { /* src is an inode */
9040 } else { /* dest is an inode */
9127 struct inode *dir,
9131 struct inode *inode;
9139 inode = btrfs_new_inode(trans, root, dir,
9147 if (IS_ERR(inode)) {
9148 ret = PTR_ERR(inode);
9152 inode->i_op = &btrfs_special_inode_operations;
9153 init_special_inode(inode, inode->i_mode,
9156 ret = btrfs_init_inode_security(trans, inode, dir,
9162 BTRFS_I(inode), 0, index);
9166 ret = btrfs_update_inode(trans, root, inode);
9168 unlock_new_inode(inode);
9170 inode_dec_link_count(inode);
9171 iput(inode);
9176 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9177 struct inode *new_dir, struct dentry *new_dentry,
9185 struct inode *new_inode = d_inode(new_dentry);
9186 struct inode *old_inode = d_inode(old_dentry);
9246 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9388 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9389 struct inode *new_dir, struct dentry *new_dentry,
9403 struct inode *inode;
9412 struct inode *inode;
9416 inode = delalloc_work->inode;
9417 filemap_flush(inode->i_mapping);
9419 &BTRFS_I(inode)->runtime_flags))
9420 filemap_flush(inode->i_mapping);
9422 iput(inode);
9426 static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
9436 work->inode = inode;
9451 struct inode *inode;
9475 inode = igrab(&binode->vfs_inode);
9476 if (!inode) {
9486 work = btrfs_alloc_delalloc_work(inode);
9488 iput(inode);
9496 ret = sync_inode(inode, wbc);
9499 &BTRFS_I(inode)->runtime_flags))
9500 ret = sync_inode(inode, wbc);
9501 btrfs_add_delayed_iput(inode);
9598 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9606 struct inode *inode = NULL;
9621 * 2 items for inode item and ref
9623 * 1 item for updating parent inode item
9635 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9638 if (IS_ERR(inode)) {
9639 err = PTR_ERR(inode);
9640 inode = NULL;
9645 * If the active LSM wants to access the inode during
9650 inode->i_fop = &btrfs_file_operations;
9651 inode->i_op = &btrfs_file_inode_operations;
9652 inode->i_mapping->a_ops = &btrfs_aops;
9654 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9663 key.objectid = btrfs_ino(BTRFS_I(inode));
9689 inode->i_op = &btrfs_symlink_inode_operations;
9690 inode_nohighmem(inode);
9691 inode_set_bytes(inode, name_len);
9692 btrfs_i_size_write(BTRFS_I(inode), name_len);
9693 err = btrfs_update_inode(trans, root, inode);
9695 * Last step, add directory indexes for our symlink inode. This is the
9701 BTRFS_I(inode), 0, index);
9705 d_instantiate_new(dentry, inode);
9709 if (err && inode) {
9710 inode_dec_link_count(inode);
9711 discard_new_inode(inode);
9719 struct inode *inode, struct btrfs_key *ins,
9740 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
9745 ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
9766 ret = btrfs_replace_file_extents(inode, path, file_offset,
9776 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9781 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9782 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9784 struct btrfs_root *root = BTRFS_I(inode)->root;
9822 trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
9837 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
9843 &BTRFS_I(inode)->runtime_flags);
9863 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
9873 inode_inc_iversion(inode);
9874 inode->i_ctime = current_time(inode);
9875 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
9877 (actual_len > inode->i_size) &&
9878 (cur_offset > inode->i_size)) {
9883 i_size_write(inode, i_size);
9884 btrfs_inode_safe_disk_i_size_write(inode, 0);
9887 ret = btrfs_update_inode(trans, root, inode);
9902 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
9907 int btrfs_prealloc_file_range(struct inode *inode, int mode,
9911 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9916 int btrfs_prealloc_file_range_trans(struct inode *inode,
9921 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9930 static int btrfs_permission(struct inode *inode, int mask)
9932 struct btrfs_root *root = BTRFS_I(inode)->root;
9933 umode_t mode = inode->i_mode;
9939 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9942 return generic_permission(inode, mask);
9945 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9950 struct inode *inode = NULL;
9966 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
9968 if (IS_ERR(inode)) {
9969 ret = PTR_ERR(inode);
9970 inode = NULL;
9974 inode->i_fop = &btrfs_file_operations;
9975 inode->i_op = &btrfs_file_inode_operations;
9977 inode->i_mapping->a_ops = &btrfs_aops;
9979 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9983 ret = btrfs_update_inode(trans, root, inode);
9986 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
9997 set_nlink(inode, 1);
9998 d_tmpfile(dentry, inode);
9999 unlock_new_inode(inode);
10000 mark_inode_dirty(inode);
10003 if (ret && inode)
10004 discard_new_inode(inode);
10011 struct inode *inode = tree->private_data;
10017 page = find_get_page(inode->i_mapping, index);
10031 static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10034 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10043 sp->inode = inode;
10053 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10056 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10073 static void btrfs_free_swapfile_pins(struct inode *inode)
10075 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10084 if (sp->inode == inode) {
10152 struct inode *inode = file_inode(file);
10154 btrfs_free_swapfile_pins(inode);
10155 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10161 struct inode *inode = file_inode(file);
10162 struct btrfs_root *root = BTRFS_I(inode)->root;
10164 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10180 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10185 * The inode is locked, so these flags won't change after we check them.
10187 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10191 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10195 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10251 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10260 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
10294 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
10321 ret = btrfs_add_swapfile_pin(inode, device, false);
10357 ret = btrfs_add_swapfile_pin(inode, bg, true);