Lines Matching defs:inode
57 #include "inode-item.h"
125 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
126 static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback);
128 static noinline int run_delalloc_cow(struct btrfs_inode *inode,
132 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
187 "checksum error at logical %llu mirror %u root %llu, inode %llu offset %llu",
201 "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu length %u links %u (path: %s)",
213 "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu, path resolving failed with ret=%d",
226 static void print_data_reloc_error(const struct btrfs_inode *inode, u64 file_off,
230 struct btrfs_fs_info *fs_info = inode->root->fs_info;
249 inode->root->root_key.objectid, btrfs_ino(inode), file_off,
259 inode->root->root_key.objectid,
260 btrfs_ino(inode), file_off, logical,
319 static void __cold btrfs_print_data_csum_error(struct btrfs_inode *inode,
322 struct btrfs_root *root = inode->root;
327 return print_data_reloc_error(inode, logical_start, csum,
334 root->root_key.objectid, btrfs_ino(inode),
342 root->root_key.objectid, btrfs_ino(inode),
351 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
355 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
360 int btrfs_inode_lock(struct btrfs_inode *inode, unsigned int ilock_flags)
364 if (!inode_trylock_shared(&inode->vfs_inode))
369 inode_lock_shared(&inode->vfs_inode);
372 if (!inode_trylock(&inode->vfs_inode))
377 inode_lock(&inode->vfs_inode);
380 down_write(&inode->i_mmap_lock);
385 * btrfs_inode_unlock - unock inode i_rwsem
390 void btrfs_inode_unlock(struct btrfs_inode *inode, unsigned int ilock_flags)
393 up_write(&inode->i_mmap_lock);
395 inode_unlock_shared(&inode->vfs_inode);
397 inode_unlock(&inode->vfs_inode);
410 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
439 page = find_get_page(inode->vfs_inode.i_mapping, index);
449 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
470 return btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes, false);
473 static int btrfs_dirty_inode(struct btrfs_inode *inode);
481 err = __btrfs_set_acl(trans, args->inode, args->default_acl,
487 err = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS);
492 cache_no_acl(args->inode);
493 return btrfs_xattr_security_init(trans, args->inode, args->dir,
504 struct btrfs_inode *inode, bool extent_inserted,
510 struct btrfs_root *root = inode->root;
530 key.objectid = btrfs_ino(inode);
569 page = find_get_page(inode->vfs_inode.i_mapping, 0);
583 ret = btrfs_inode_set_file_extent_range(inode, 0,
592 * We must do any i_size and inode updates before we unlock the pages.
595 i_size = i_size_read(&inode->vfs_inode);
597 i_size_write(&inode->vfs_inode, size);
600 inode->disk_i_size = i_size;
612 static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 size,
619 struct btrfs_root *root = inode->root;
632 if (size < i_size_read(&inode->vfs_inode) ||
647 trans->block_rsv = &inode->block_rsv;
655 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
661 ret = insert_inline_extent(trans, path, inode, drop_args.extent_inserted,
672 btrfs_update_inode_bytes(inode, size, drop_args.bytes_found);
673 ret = btrfs_update_inode(trans, root, inode);
682 btrfs_set_inode_full_sync(inode);
690 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE, NULL);
707 struct btrfs_inode *inode;
745 * Check if the inode needs to be submitted to compression, based on mount
748 static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
751 struct btrfs_fs_info *fs_info = inode->root->fs_info;
753 if (!btrfs_inode_can_compress(inode)) {
756 btrfs_ino(inode));
795 if (inode->defrag_compress)
798 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
801 inode->flags & BTRFS_INODE_COMPRESS ||
802 inode->prop_compress)
803 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
807 static inline void inode_should_defrag(struct btrfs_inode *inode,
812 (start > 0 || end + 1 < inode->disk_i_size))
813 btrfs_add_inode_defrag(NULL, inode, small_write);
833 struct btrfs_inode *inode = async_chunk->inode;
834 struct btrfs_fs_info *fs_info = inode->root->fs_info;
835 struct address_space *mapping = inode->vfs_inode.i_mapping;
850 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
857 extent_range_clear_dirty_for_io(&inode->vfs_inode, start, end);
869 i_size = i_size_read(&inode->vfs_inode);
897 (start > 0 || end + 1 < inode->disk_i_size))
917 * We do compression for mount -o compress and when the inode has not
921 if (!inode_need_compress(inode, start, end))
933 if (inode->defrag_compress)
934 compress_type = inode->defrag_compress;
935 else if (inode->prop_compress)
936 compress_type = inode->prop_compress;
964 ret = cow_file_range_inline(inode, actual_end, 0,
968 ret = cow_file_range_inline(inode, actual_end,
991 extent_clear_unlock_delalloc(inode, start, end,
1030 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && !inode->prop_compress)
1031 inode->flags |= BTRFS_INODE_NOCOMPRESS;
1061 static void submit_uncompressed_range(struct btrfs_inode *inode,
1075 wbc_attach_fdatawrite_inode(&wbc, &inode->vfs_inode);
1076 ret = run_delalloc_cow(inode, locked_page, start, end, &wbc, false);
1079 btrfs_cleanup_ordered_extents(inode, locked_page, start, end - start + 1);
1085 btrfs_mark_ordered_io_finished(inode, locked_page,
1098 struct btrfs_inode *inode = async_chunk->inode;
1099 struct extent_io_tree *io_tree = &inode->io_tree;
1100 struct btrfs_root *root = inode->root;
1127 submit_uncompressed_range(inode, async_extent, locked_page);
1147 em = create_io_em(inode, start,
1162 ordered = btrfs_alloc_ordered_extent(inode, start, /* file_offset */
1171 btrfs_drop_extent_map_range(inode, start, end, false);
1178 extent_clear_unlock_delalloc(inode, start, end,
1196 mapping_set_error(inode->vfs_inode.i_mapping, -EIO);
1197 extent_clear_unlock_delalloc(inode, start, end,
1207 "async extent submission failed root=%lld inode=%llu start=%llu len=%llu ret=%d",
1208 root->root_key.objectid, btrfs_ino(inode), start,
1213 static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
1216 struct extent_map_tree *em_tree = &inode->extent_tree;
1225 * first block in this inode and use that as a hint. If that
1273 static noinline int cow_file_range(struct btrfs_inode *inode,
1278 struct btrfs_root *root = inode->root;
1294 if (btrfs_is_free_space_inode(inode)) {
1303 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
1316 u64 actual_end = min_t(u64, i_size_read(&inode->vfs_inode),
1320 ret = cow_file_range_inline(inode, actual_end, 0,
1329 extent_clear_unlock_delalloc(inode, start, end,
1355 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1394 wait_on_bit_io(&inode->root->fs_info->flags,
1411 em = create_io_em(inode, start, ins.offset, /* len */
1425 ordered = btrfs_alloc_ordered_extent(inode, start, ram_size,
1449 btrfs_drop_extent_map_range(inode, start,
1468 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1494 btrfs_drop_extent_map_range(inode, start, start + ram_size - 1, false);
1526 mapping_set_error(inode->vfs_inode.i_mapping, ret);
1527 extent_clear_unlock_delalloc(inode, orig_start, start - 1,
1542 extent_clear_unlock_delalloc(inode, start,
1558 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1600 btrfs_add_delayed_iput(async_chunk->inode);
1609 static bool run_delalloc_compressed(struct btrfs_inode *inode,
1613 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1629 unlock_extent(&inode->io_tree, start, end, NULL);
1630 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1642 ihold(&inode->vfs_inode);
1644 async_chunk[i].inode = inode;
1702 static noinline int run_delalloc_cow(struct btrfs_inode *inode,
1711 ret = cow_file_range(inode, locked_page, start, end, &done_offset,
1715 extent_write_locked_range(&inode->vfs_inode, locked_page, start,
1746 static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
1749 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1750 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root);
1752 struct extent_io_tree *io_tree = &inode->io_tree;
1761 * respective file range (either there is prealloc extent or the inode
1781 * If we need to fallback to cow and the inode corresponds to a free
1782 * space cache inode or an inode of the data relocation tree, we must
1793 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1813 ret = cow_file_range(inode, locked_page, start, end, NULL, false, true);
1853 struct btrfs_inode *inode,
1856 const bool is_freespace_inode = btrfs_is_free_space_inode(inode);
1858 struct btrfs_root *root = inode->root;
1877 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1910 ret = btrfs_cross_ref_exist(root, btrfs_ino(inode),
1962 static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
1966 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1967 struct btrfs_root *root = inode->root;
1973 u64 ino = btrfs_ino(inode);
2042 * more extents for this inode
2091 ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args);
2125 ret = fallback_to_cow(inode, locked_page,
2140 em = create_io_em(inode, cur_offset, nocow_args.num_bytes,
2155 ordered = btrfs_alloc_ordered_extent(inode, cur_offset,
2165 btrfs_drop_extent_map_range(inode, cur_offset,
2181 extent_clear_unlock_delalloc(inode, cur_offset, nocow_end,
2206 ret = fallback_to_cow(inode, locked_page, cow_start, end);
2224 extent_clear_unlock_delalloc(inode, cur_offset, end,
2234 static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
2236 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
2237 if (inode->defrag_bytes &&
2238 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
2250 int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
2253 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
2263 if (should_nocow(inode, start, end)) {
2264 ret = run_delalloc_nocow(inode, locked_page, start, end);
2268 if (btrfs_inode_can_compress(inode) &&
2269 inode_need_compress(inode, start, end) &&
2270 run_delalloc_compressed(inode, locked_page, start, end, wbc))
2274 ret = run_delalloc_cow(inode, locked_page, start, end, wbc,
2277 ret = cow_file_range(inode, locked_page, start, end, NULL,
2282 btrfs_cleanup_ordered_extents(inode, locked_page, start,
2287 void btrfs_split_delalloc_extent(struct btrfs_inode *inode,
2290 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2314 spin_lock(&inode->lock);
2315 btrfs_mod_outstanding_extents(inode, 1);
2316 spin_unlock(&inode->lock);
2324 void btrfs_merge_delalloc_extent(struct btrfs_inode *inode, struct extent_state *new,
2327 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2342 spin_lock(&inode->lock);
2343 btrfs_mod_outstanding_extents(inode, -1);
2344 spin_unlock(&inode->lock);
2373 spin_lock(&inode->lock);
2374 btrfs_mod_outstanding_extents(inode, -1);
2375 spin_unlock(&inode->lock);
2379 struct btrfs_inode *inode)
2381 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2384 if (list_empty(&inode->delalloc_inodes)) {
2385 list_add_tail(&inode->delalloc_inodes, &root->delalloc_inodes);
2386 set_bit(BTRFS_INODE_IN_DELALLOC_LIST, &inode->runtime_flags);
2400 struct btrfs_inode *inode)
2404 if (!list_empty(&inode->delalloc_inodes)) {
2405 list_del_init(&inode->delalloc_inodes);
2407 &inode->runtime_flags);
2420 struct btrfs_inode *inode)
2423 __btrfs_del_delalloc_inode(root, inode);
2428 * Properly track delayed allocation bytes in the inode and to maintain the
2431 void btrfs_set_delalloc_extent(struct btrfs_inode *inode, struct extent_state *state,
2434 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2444 struct btrfs_root *root = inode->root;
2447 bool do_list = !btrfs_is_free_space_inode(inode);
2449 spin_lock(&inode->lock);
2450 btrfs_mod_outstanding_extents(inode, num_extents);
2451 spin_unlock(&inode->lock);
2459 spin_lock(&inode->lock);
2460 inode->delalloc_bytes += len;
2462 inode->defrag_bytes += len;
2464 &inode->runtime_flags))
2465 btrfs_add_delalloc_inodes(root, inode);
2466 spin_unlock(&inode->lock);
2471 spin_lock(&inode->lock);
2472 inode->new_delalloc_bytes += state->end + 1 - state->start;
2473 spin_unlock(&inode->lock);
2481 void btrfs_clear_delalloc_extent(struct btrfs_inode *inode,
2484 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2489 spin_lock(&inode->lock);
2490 inode->defrag_bytes -= len;
2491 spin_unlock(&inode->lock);
2500 struct btrfs_root *root = inode->root;
2501 bool do_list = !btrfs_is_free_space_inode(inode);
2503 spin_lock(&inode->lock);
2504 btrfs_mod_outstanding_extents(inode, -num_extents);
2505 spin_unlock(&inode->lock);
2514 btrfs_delalloc_release_metadata(inode, len, false);
2527 spin_lock(&inode->lock);
2528 inode->delalloc_bytes -= len;
2529 if (do_list && inode->delalloc_bytes == 0 &&
2531 &inode->runtime_flags))
2532 btrfs_del_delalloc_inode(root, inode);
2533 spin_unlock(&inode->lock);
2538 spin_lock(&inode->lock);
2539 ASSERT(inode->new_delalloc_bytes >= len);
2540 inode->new_delalloc_bytes -= len;
2542 inode_add_bytes(&inode->vfs_inode, len);
2543 spin_unlock(&inode->lock);
2571 ret = split_extent_map(bbio->inode, bbio->file_offset,
2586 * given a list of ordered sums record them in the inode. This happens
2609 static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2623 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2636 ret = set_extent_bit(&inode->io_tree, search_start,
2648 int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2654 if (start >= i_size_read(&inode->vfs_inode) &&
2655 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2664 ret = btrfs_find_new_delalloc_bytes(inode, start,
2671 return set_extent_bit(&inode->io_tree, start, end,
2678 struct btrfs_inode *inode;
2690 struct btrfs_inode *inode = fixup->inode;
2691 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2701 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2730 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2731 btrfs_delalloc_release_space(inode, data_reserved,
2746 lock_extent(&inode->io_tree, page_start, page_end, &cached_state);
2752 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
2754 unlock_extent(&inode->io_tree, page_start, page_end,
2762 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2777 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2779 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2781 unlock_extent(&inode->io_tree, page_start, page_end, &cached_state);
2789 btrfs_mark_ordered_io_finished(inode, page, page_start,
2803 btrfs_add_delayed_iput(inode);
2819 struct inode *inode = page->mapping->host;
2820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2842 * We are already holding a reference to this inode from
2847 ihold(inode);
2852 fixup->inode = BTRFS_I(inode);
2859 struct btrfs_inode *inode, u64 file_pos,
2864 struct btrfs_root *root = inode->root;
2895 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2900 ins.objectid = btrfs_ino(inode);
2929 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2935 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
2941 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
2945 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
2993 * For delalloc, when completing an ordered extent we update the inode's
2994 * bytes when clearing the range in the inode's io tree, so pass false
3002 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
3014 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
3015 struct btrfs_root *root = inode->root;
3018 struct extent_io_tree *io_tree = &inode->io_tree;
3038 freespace_inode = btrfs_is_free_space_inode(inode);
3062 btrfs_inode_safe_disk_i_size_write(inode, 0);
3072 trans->block_rsv = &inode->block_rsv;
3073 ret = btrfs_update_inode_fallback(trans, root, inode);
3092 trans->block_rsv = &inode->block_rsv;
3098 ret = btrfs_mark_extent_written(trans, inode,
3114 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
3129 * update the inode's number of bytes. This needs to be done first
3130 * before updating the inode item.
3134 clear_extent_bit(&inode->io_tree, start, end,
3138 btrfs_inode_safe_disk_i_size_write(inode, 0);
3139 ret = btrfs_update_inode_fallback(trans, root, inode);
3146 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
3158 * extent, and mark the inode with the error if it wasn't
3165 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3175 * because when we do btrfs_get_extent() on the free space inode
3183 * for now simply skip this if we are the free space inode.
3185 if (!btrfs_is_free_space_inode(inode))
3186 btrfs_drop_extent_map_range(inode, unwritten_start,
3219 btrfs_qgroup_free_refroot(fs_info, inode->root->root_key.objectid,
3229 btrfs_remove_ordered_extent(inode, ordered_extent);
3241 if (btrfs_is_zoned(btrfs_sb(ordered->inode->i_sb)) &&
3286 struct btrfs_inode *inode = bbio->inode;
3287 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3298 if (btrfs_is_data_reloc_root(inode->root) &&
3299 test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM,
3301 /* Skip the range without csum for data reloc inode */
3302 clear_extent_bits(&inode->io_tree, file_offset, end,
3315 btrfs_print_data_csum_error(inode, file_offset, csum, csum_expected,
3324 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3326 * @inode: The inode we want to perform iput on
3330 * the inode to the delayed iput machinery. Delayed iputs are processed at
3333 void btrfs_add_delayed_iput(struct btrfs_inode *inode)
3335 struct btrfs_fs_info *fs_info = inode->root->fs_info;
3338 if (atomic_add_unless(&inode->vfs_inode.i_count, -1, 1))
3348 ASSERT(list_empty(&inode->delayed_iput));
3349 list_add_tail(&inode->delayed_iput, &fs_info->delayed_iputs);
3356 struct btrfs_inode *inode)
3358 list_del_init(&inode->delayed_iput);
3360 iput(&inode->vfs_inode);
3367 struct btrfs_inode *inode)
3369 if (!list_empty(&inode->delayed_iput)) {
3371 if (!list_empty(&inode->delayed_iput))
3372 run_delayed_iput_locked(fs_info, inode);
3387 struct btrfs_inode *inode;
3389 inode = list_first_entry(&fs_info->delayed_iputs,
3391 run_delayed_iput_locked(fs_info, inode);
3423 * This creates an orphan entry for the given inode in case something goes wrong
3427 struct btrfs_inode *inode)
3431 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3442 * this particular inode.
3445 struct btrfs_inode *inode)
3447 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3461 struct inode *inode;
3511 * crossing root thing. we store the inode number in the
3517 * We found the same inode as before. This means we were
3534 inode = btrfs_iget(fs_info->sb, last_objectid, root);
3535 if (IS_ERR(inode)) {
3536 ret = PTR_ERR(inode);
3537 inode = NULL;
3542 if (!inode && root == fs_info->tree_root) {
3550 * b) a free space cache inode
3578 * If we have an inode with links, there are a couple of
3600 * deleted but wasn't. The inode number may have been reused,
3603 if (!inode || inode->i_nlink) {
3604 if (inode) {
3605 ret = btrfs_drop_verity_items(BTRFS_I(inode));
3606 iput(inode);
3607 inode = NULL;
3629 iput(inode);
3654 * slot is the slot the inode is in, objectid is the objectid of the inode
3702 * it goes inode, inode backrefs, xattrs, extents,
3703 * so if there are a ton of hard links to an inode there can
3711 * something larger than an xattr. We have to assume the inode
3720 * read an inode from the btree into the in-memory inode
3722 static int btrfs_read_locked_inode(struct inode *inode,
3725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3729 struct btrfs_root *root = BTRFS_I(inode)->root;
3738 ret = btrfs_fill_inode(inode, &rdev);
3748 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3764 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3765 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3766 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3767 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3768 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3769 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3770 round_up(i_size_read(inode), fs_info->sectorsize));
3772 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3773 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3775 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3776 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3778 inode_set_ctime(inode, btrfs_timespec_sec(leaf, &inode_item->ctime),
3781 BTRFS_I(inode)->i_otime.tv_sec =
3783 BTRFS_I(inode)->i_otime.tv_nsec =
3786 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3787 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3788 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3790 inode_set_iversion_queried(inode,
3792 inode->i_generation = BTRFS_I(inode)->generation;
3793 inode->i_rdev = 0;
3796 BTRFS_I(inode)->index_cnt = (u64)-1;
3798 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
3807 * This is required for both inode re-read from disk and delayed inode
3810 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3812 &BTRFS_I(inode)->runtime_flags);
3816 * against the inode was last made. So here we assume the inode might
3819 * between the inode and its parent if the inode is fsync'ed and the log
3826 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3831 * We must make sure that when we fsync our inode foo we also log its
3832 * parent inode, otherwise after log replay the parent still has the
3833 * dentry with the "bar" name but our inode foo has a link count of 1
3834 * and doesn't have an inode ref with the name "bar" anymore.
3838 * transaction commits on fsync if our inode is a directory, or if our
3839 * inode is not a directory, logging its parent unnecessarily.
3841 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3845 * of the last transaction where this inode was used for a reflink
3846 * operation, so after eviction and reloading the inode we must be
3847 * pessimistic and assume the last transaction that modified the inode.
3849 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3852 if (inode->i_nlink != 1 ||
3857 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3865 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3870 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3879 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3882 ret = btrfs_load_inode_props(inode, path);
3886 btrfs_ino(BTRFS_I(inode)),
3893 cache_no_acl(inode);
3895 switch (inode->i_mode & S_IFMT) {
3897 inode->i_mapping->a_ops = &btrfs_aops;
3898 inode->i_fop = &btrfs_file_operations;
3899 inode->i_op = &btrfs_file_inode_operations;
3902 inode->i_fop = &btrfs_dir_file_operations;
3903 inode->i_op = &btrfs_dir_inode_operations;
3906 inode->i_op = &btrfs_symlink_inode_operations;
3907 inode_nohighmem(inode);
3908 inode->i_mapping->a_ops = &btrfs_aops;
3911 inode->i_op = &btrfs_special_inode_operations;
3912 init_special_inode(inode, inode->i_mode, rdev);
3916 btrfs_sync_inode_flags_to_i_flags(inode);
3921 * given a leaf and an inode, copy the inode fields into the leaf
3926 struct inode *inode)
3933 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3934 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3935 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3936 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3937 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3940 inode->i_atime.tv_sec);
3942 inode->i_atime.tv_nsec);
3945 inode->i_mtime.tv_sec);
3947 inode->i_mtime.tv_nsec);
3950 inode_get_ctime(inode).tv_sec);
3952 inode_get_ctime(inode).tv_nsec);
3955 BTRFS_I(inode)->i_otime.tv_sec);
3957 BTRFS_I(inode)->i_otime.tv_nsec);
3959 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3961 BTRFS_I(inode)->generation);
3962 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3964 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3965 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3966 BTRFS_I(inode)->ro_flags);
3972 * copy everything in the in-memory inode into the btree.
3976 struct btrfs_inode *inode)
3987 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
3998 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
4000 btrfs_set_inode_last_trans(trans, inode);
4008 * copy everything in the in-memory inode into the btree.
4012 struct btrfs_inode *inode)
4018 * If the inode is a free space inode, we can deadlock during commit
4021 * The data relocation inode should also be directly updated
4024 if (!btrfs_is_free_space_inode(inode)
4029 ret = btrfs_delayed_update_inode(trans, root, inode);
4031 btrfs_set_inode_last_trans(trans, inode);
4035 return btrfs_update_inode_item(trans, root, inode);
4039 struct btrfs_root *root, struct btrfs_inode *inode)
4043 ret = btrfs_update_inode(trans, root, inode);
4045 return btrfs_update_inode_item(trans, root, inode);
4050 * unlink helper that gets used here in inode.c and in the tree logging
4052 * also drops the back refs in the inode to the directory
4056 struct btrfs_inode *inode,
4066 u64 ino = btrfs_ino(inode);
4087 * the inode ref, since we get the inode ref, remove it directly,
4090 * But if we have dir index, needn't search inode ref to get it.
4091 * Since the inode ref is close to the inode item, it is better
4093 * we update the inode item.
4095 if (inode->dir_index) {
4096 ret = btrfs_delayed_delete_inode_ref(inode);
4098 index = inode->dir_index;
4106 "failed to delete reference to %.*s, inode %llu parent %llu",
4128 btrfs_del_inode_ref_in_log(trans, root, name, inode, dir_ino);
4137 * the inode we can run the delayed iput here without any issues as the
4141 btrfs_run_delayed_iput(fs_info, inode);
4148 inode_inc_iversion(&inode->vfs_inode);
4150 inode_set_ctime_current(&inode->vfs_inode);
4158 struct btrfs_inode *dir, struct btrfs_inode *inode,
4163 ret = __btrfs_unlink_inode(trans, dir, inode, name, NULL);
4165 drop_nlink(&inode->vfs_inode);
4166 ret = btrfs_update_inode(trans, inode->root, inode);
4187 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4190 struct inode *inode = d_inode(dentry);
4214 if (inode->i_nlink == 0) {
4215 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4232 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4249 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4250 objectid = inode->root->root_key.objectid;
4251 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4252 objectid = inode->location.objectid;
4283 * This is a placeholder inode for a subvolume we didn't have a
4291 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4397 struct inode *inode;
4431 inode = igrab(&entry->vfs_inode);
4432 if (inode) {
4434 if (atomic_read(&inode->i_count) > 1)
4435 d_prune_aliases(inode);
4437 * btrfs_drop_inode will have it removed from the inode
4440 iput(inode);
4458 struct inode *inode = d_inode(dentry);
4459 struct btrfs_root *dest = BTRFS_I(inode)->root;
4469 * inside the inode lock so the error handling that has to drop the bit
4500 * One for dir inode,
4569 inode->i_flags |= S_DEAD;
4591 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4593 struct inode *inode = d_inode(dentry);
4594 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
4600 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4602 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID) {
4623 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4628 err = btrfs_orphan_add(trans, BTRFS_I(inode));
4632 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4638 btrfs_i_size_write(BTRFS_I(inode), 0);
4664 * @inode - inode that we're zeroing
4673 int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4676 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4677 struct address_space *mapping = inode->vfs_inode.i_mapping;
4678 struct extent_io_tree *io_tree = &inode->io_tree;
4700 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4703 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes, false) > 0) {
4710 ret = btrfs_delalloc_reserve_metadata(inode, blocksize, blocksize, false);
4713 btrfs_free_reserved_data_space(inode, data_reserved,
4720 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4722 btrfs_delalloc_release_extents(inode, blocksize);
4755 ordered = btrfs_lookup_ordered_extent(inode, block_start);
4765 clear_extent_bit(&inode->io_tree, block_start, block_end,
4769 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
4792 set_extent_bit(&inode->io_tree, block_start, block_end,
4798 btrfs_delalloc_release_metadata(inode, blocksize, true);
4800 btrfs_delalloc_release_space(inode, data_reserved,
4803 btrfs_delalloc_release_extents(inode, blocksize);
4808 btrfs_check_nocow_unlock(inode);
4813 static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
4825 * fsync will know this inode was changed and needs to be logged.
4833 * 1 - for updating the inode.
4843 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
4850 ret = btrfs_insert_hole_extent(trans, root, btrfs_ino(inode), offset, len);
4854 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4855 btrfs_update_inode(trans, root, inode);
4867 int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
4869 struct btrfs_root *root = inode->root;
4871 struct extent_io_tree *io_tree = &inode->io_tree;
4886 err = btrfs_truncate_block(inode, oldsize, 0, 0);
4893 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4897 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4911 err = maybe_insert_hole(root, inode, cur_offset,
4916 err = btrfs_inode_set_file_extent_range(inode,
4923 btrfs_drop_extent_map_range(inode, cur_offset,
4926 btrfs_set_inode_full_sync(inode);
4940 err = btrfs_replace_extent_map_range(inode, hole_em, true);
4943 err = btrfs_inode_set_file_extent_range(inode,
4960 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4962 struct btrfs_root *root = BTRFS_I(inode)->root;
4964 loff_t oldsize = i_size_read(inode);
4976 inode_inc_iversion(inode);
4978 inode->i_mtime = inode_set_ctime_current(inode);
4991 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
5003 i_size_write(inode, newsize);
5004 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
5005 pagecache_isize_extended(inode, oldsize, newsize);
5006 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
5010 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5013 ret = btrfs_wait_ordered_range(inode,
5027 &BTRFS_I(inode)->runtime_flags);
5029 truncate_setsize(inode, newsize);
5031 inode_dio_wait(inode);
5033 ret = btrfs_truncate(BTRFS_I(inode), newsize == oldsize);
5034 if (ret && inode->i_nlink) {
5043 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5046 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5056 struct inode *inode = d_inode(dentry);
5057 struct btrfs_root *root = BTRFS_I(inode)->root;
5067 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5068 err = btrfs_setsize(inode, attr);
5074 setattr_copy(idmap, inode, attr);
5075 inode_inc_iversion(inode);
5076 err = btrfs_dirty_inode(BTRFS_I(inode));
5079 err = posix_acl_chmod(idmap, dentry, inode->i_mode);
5086 * While truncating the inode pages during eviction, we get the VFS
5087 * calling btrfs_invalidate_folio() against each folio of the inode. This
5093 * Therefore if the inode is being evicted, let btrfs_invalidate_folio()
5098 static void evict_inode_truncate_pages(struct inode *inode)
5100 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5103 ASSERT(inode->i_state & I_FREEING);
5104 truncate_inode_pages_final(&inode->i_data);
5106 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
5116 * queue kthread), inode references (inode->i_count) were not taken
5119 * anyone else holding locked ranges without having bumped the inode's
5120 * reference count - if we don't do it, when they access the inode's
5150 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5211 void btrfs_evict_inode(struct inode *inode)
5213 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5215 struct btrfs_root *root = BTRFS_I(inode)->root;
5219 trace_btrfs_inode_evict(inode);
5222 fsverity_cleanup_inode(inode);
5223 clear_inode(inode);
5227 evict_inode_truncate_pages(inode);
5229 if (inode->i_nlink &&
5232 btrfs_is_free_space_inode(BTRFS_I(inode))))
5235 if (is_bad_inode(inode))
5241 if (inode->i_nlink > 0) {
5248 * This makes sure the inode item in tree is uptodate and the space for
5249 * the inode update is released.
5251 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5257 * inode. We could have a delayed dir index deletion queued up, but
5258 * we're removing the inode completely so that'll be taken care of in
5261 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
5269 btrfs_i_size_write(BTRFS_I(inode), 0);
5273 .inode = BTRFS_I(inode),
5274 .ino = btrfs_ino(BTRFS_I(inode)),
5289 * We have not added new delayed items for our inode after we
5302 * the tree. They will be cleaned up on the next mount. If the inode
5312 btrfs_orphan_del(trans, BTRFS_I(inode));
5324 btrfs_remove_delayed_node(BTRFS_I(inode));
5325 fsverity_cleanup_inode(inode);
5326 clear_inode(inode);
5385 * when we hit a tree root in a directory, the btrfs part of the inode
5456 static void inode_tree_add(struct btrfs_inode *inode)
5458 struct btrfs_root *root = inode->root;
5462 struct rb_node *new = &inode->rb_node;
5463 u64 ino = btrfs_ino(inode);
5465 if (inode_unhashed(&inode->vfs_inode))
5492 static void inode_tree_del(struct btrfs_inode *inode)
5494 struct btrfs_root *root = inode->root;
5498 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5499 rb_erase(&inode->rb_node, &root->inode_tree);
5500 RB_CLEAR_NODE(&inode->rb_node);
5515 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5519 inode->i_ino = args->ino;
5520 BTRFS_I(inode)->location.objectid = args->ino;
5521 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5522 BTRFS_I(inode)->location.offset = 0;
5523 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5524 BUG_ON(args->root && !BTRFS_I(inode)->root);
5529 &BTRFS_I(inode)->runtime_flags);
5533 static int btrfs_find_actor(struct inode *inode, void *opaque)
5537 return args->ino == BTRFS_I(inode)->location.objectid &&
5538 args->root == BTRFS_I(inode)->root;
5541 static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5544 struct inode *inode;
5551 inode = iget5_locked(s, hashval, btrfs_find_actor,
5554 return inode;
5558 * Get an inode object given its inode number and corresponding root.
5563 struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5566 struct inode *inode;
5568 inode = btrfs_iget_locked(s, ino, root);
5569 if (!inode)
5572 if (inode->i_state & I_NEW) {
5575 ret = btrfs_read_locked_inode(inode, path);
5577 inode_tree_add(BTRFS_I(inode));
5578 unlock_new_inode(inode);
5580 iget_failed(inode);
5583 * btrfs_read_locked_inode, this means the inode item
5588 inode = ERR_PTR(ret);
5592 return inode;
5595 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
5600 static struct inode *new_simple_dir(struct inode *dir,
5604 struct inode *inode = new_inode(dir->i_sb);
5606 if (!inode)
5609 BTRFS_I(inode)->root = btrfs_grab_root(root);
5610 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5611 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5613 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5615 * We only need lookup, the rest is read-only and there's no inode
5618 inode->i_op = &simple_dir_inode_operations;
5619 inode->i_opflags &= ~IOP_XATTR;
5620 inode->i_fop = &simple_dir_operations;
5621 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5622 inode->i_mtime = inode_set_ctime_current(inode);
5623 inode->i_atime = dir->i_atime;
5624 BTRFS_I(inode)->i_otime = inode->i_mtime;
5625 inode->i_uid = dir->i_uid;
5626 inode->i_gid = dir->i_gid;
5628 return inode;
5640 static inline u8 btrfs_inode_type(struct inode *inode)
5642 return fs_umode_to_ftype(inode->i_mode);
5645 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5648 struct inode *inode;
5663 inode = btrfs_iget(dir->i_sb, location.objectid, root);
5664 if (IS_ERR(inode))
5665 return inode;
5667 /* Do extra check against inode mode with di_type */
5668 if (btrfs_inode_type(inode) != di_type) {
5670 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5671 inode->i_mode, btrfs_inode_type(inode),
5673 iput(inode);
5676 return inode;
5683 inode = ERR_PTR(ret);
5685 inode = new_simple_dir(dir, &location, root);
5687 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
5690 if (IS_ERR(inode))
5691 return inode;
5694 if (!sb_rdonly(inode->i_sb))
5698 iput(inode);
5699 inode = ERR_PTR(ret);
5703 return inode;
5709 struct inode *inode = d_inode(dentry);
5711 if (!inode && !IS_ROOT(dentry))
5712 inode = d_inode(dentry->d_parent);
5714 if (inode) {
5715 root = BTRFS_I(inode)->root;
5719 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5725 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5728 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
5730 if (inode == ERR_PTR(-ENOENT))
5731 inode = NULL;
5732 return d_splice_alias(inode, dentry);
5739 static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
5741 struct btrfs_root *root = inode->root;
5747 key.objectid = btrfs_ino(inode);
5764 inode->index_cnt = BTRFS_DIR_START_INDEX;
5773 if (found_key.objectid != btrfs_ino(inode) ||
5775 inode->index_cnt = BTRFS_DIR_START_INDEX;
5779 inode->index_cnt = found_key.offset + 1;
5816 static int btrfs_opendir(struct inode *inode, struct file *file)
5822 ret = btrfs_get_dir_last_index(BTRFS_I(inode), &last_index);
5879 struct inode *inode = file_inode(file);
5880 struct btrfs_root *root = BTRFS_I(inode)->root;
5907 put = btrfs_readdir_get_delayed_items(inode, private->last_index,
5913 key.objectid = btrfs_ino(BTRFS_I(inode));
5997 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
6004 * inode changes. But, it is most likely to find the inode in cache.
6008 static int btrfs_dirty_inode(struct btrfs_inode *inode)
6010 struct btrfs_root *root = inode->root;
6015 if (test_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags))
6022 ret = btrfs_update_inode(trans, root, inode);
6030 ret = btrfs_update_inode(trans, root, inode);
6033 if (inode->delayed_node)
6041 * ENOSPC for updating the inode in the case of file write and mmap writes.
6043 static int btrfs_update_time(struct inode *inode, int flags)
6045 struct btrfs_root *root = BTRFS_I(inode)->root;
6051 dirty = inode_update_timestamps(inode, flags);
6052 return dirty ? btrfs_dirty_inode(BTRFS_I(inode)) : 0;
6078 static int btrfs_insert_inode_locked(struct inode *inode)
6082 args.ino = BTRFS_I(inode)->location.objectid;
6083 args.root = BTRFS_I(inode)->root;
6085 return insert_inode_locked4(inode,
6086 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6093 struct inode *dir = args->dir;
6094 struct inode *inode = args->inode;
6104 ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl);
6110 /* 1 to add inode item */
6133 * 1 to update parent inode item
6135 * No need for 1 unit for the inode ref item because it is
6136 * inserted in a batch together with the inode item at
6152 * Inherit flags from the parent inode.
6156 static void btrfs_inherit_iflags(struct btrfs_inode *inode, struct btrfs_inode *dir)
6163 inode->flags &= ~BTRFS_INODE_COMPRESS;
6164 inode->flags |= BTRFS_INODE_NOCOMPRESS;
6166 inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
6167 inode->flags |= BTRFS_INODE_COMPRESS;
6171 inode->flags |= BTRFS_INODE_NODATACOW;
6172 if (S_ISREG(inode->vfs_inode.i_mode))
6173 inode->flags |= BTRFS_INODE_NODATASUM;
6176 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode);
6182 struct inode *dir = args->dir;
6183 struct inode *inode = args->inode;
6203 BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root);
6204 root = BTRFS_I(inode)->root;
6209 inode->i_ino = objectid;
6214 * fill in an inode item with the correct link count.
6216 set_nlink(inode, 0);
6220 ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index);
6225 BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX;
6226 BTRFS_I(inode)->generation = trans->transid;
6227 inode->i_generation = BTRFS_I(inode)->generation;
6235 btrfs_inherit_iflags(BTRFS_I(inode), BTRFS_I(dir));
6237 if (S_ISREG(inode->i_mode)) {
6239 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6241 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6245 location = &BTRFS_I(inode)->location;
6250 ret = btrfs_insert_inode_locked(inode);
6258 * We could have gotten an inode number from somebody who was fsynced
6263 btrfs_set_inode_full_sync(BTRFS_I(inode));
6299 inode->i_mtime = inode_set_ctime_current(inode);
6300 inode->i_atime = inode->i_mtime;
6301 BTRFS_I(inode)->i_otime = inode->i_mtime;
6304 * We're going to fill the inode item now, so at this point the inode
6312 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6326 BTRFS_I(inode)->dir_index);
6342 struct inode *parent;
6353 ret = btrfs_inode_inherit_props(trans, inode, parent);
6357 ret = btrfs_inode_inherit_props(trans, inode, dir);
6362 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid,
6378 inode_tree_add(BTRFS_I(inode));
6380 trace_btrfs_inode_new(inode);
6381 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
6386 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
6388 ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
6389 0, BTRFS_I(inode)->dir_index);
6401 * to the inode.
6403 ihold(inode);
6404 discard_new_inode(inode);
6411 * utility function to add 'inode' into 'parent_inode' with
6414 * inode to the parent directory.
6417 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
6423 u64 ino = btrfs_ino(inode);
6427 memcpy(&key, &inode->root->root_key, sizeof(key));
6448 btrfs_inode_type(&inode->vfs_inode), index);
6497 static int btrfs_create_common(struct inode *dir, struct dentry *dentry,
6498 struct inode *inode)
6505 .inode = inode,
6523 d_instantiate_new(dentry, inode);
6531 iput(inode);
6535 static int btrfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
6538 struct inode *inode;
6540 inode = new_inode(dir->i_sb);
6541 if (!inode)
6543 inode_init_owner(idmap, inode, dir, mode);
6544 inode->i_op = &btrfs_special_inode_operations;
6545 init_special_inode(inode, inode->i_mode, rdev);
6546 return btrfs_create_common(dir, dentry, inode);
6549 static int btrfs_create(struct mnt_idmap *idmap, struct inode *dir,
6552 struct inode *inode;
6554 inode = new_inode(dir->i_sb);
6555 if (!inode)
6557 inode_init_owner(idmap, inode, dir, mode);
6558 inode->i_fop = &btrfs_file_operations;
6559 inode->i_op = &btrfs_file_inode_operations;
6560 inode->i_mapping->a_ops = &btrfs_aops;
6561 return btrfs_create_common(dir, dentry, inode);
6564 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6569 struct inode *inode = d_inode(old_dentry);
6570 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6577 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
6580 if (inode->i_nlink >= BTRFS_LINK_MAX)
6592 * 2 items for inode and inode ref
6594 * 1 item for parent inode
6597 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
6604 /* There are several dir indexes for this inode, clear the cache. */
6605 BTRFS_I(inode)->dir_index = 0ULL;
6606 inc_nlink(inode);
6607 inode_inc_iversion(inode);
6608 inode_set_ctime_current(inode);
6609 ihold(inode);
6610 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6612 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6620 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
6623 if (inode->i_nlink == 1) {
6628 err = btrfs_orphan_del(trans, BTRFS_I(inode));
6632 d_instantiate(dentry, inode);
6641 inode_dec_link_count(inode);
6642 iput(inode);
6648 static int btrfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
6651 struct inode *inode;
6653 inode = new_inode(dir->i_sb);
6654 if (!inode)
6656 inode_init_owner(idmap, inode, dir, S_IFDIR | mode);
6657 inode->i_op = &btrfs_dir_inode_operations;
6658 inode->i_fop = &btrfs_dir_file_operations;
6659 return btrfs_create_common(dir, dentry, inode);
6701 static int read_inline_extent(struct btrfs_inode *inode, struct btrfs_path *path,
6732 * @inode: file to search in
6747 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6751 struct btrfs_fs_info *fs_info = inode->root->fs_info;
6755 u64 objectid = btrfs_ino(inode);
6758 struct btrfs_root *root = inode->root;
6763 struct extent_map_tree *em_tree = &inode->extent_tree;
6801 if (btrfs_is_free_space_inode(inode)) {
6838 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6841 "regular/prealloc extent found for non-regular inode %llu",
6842 btrfs_ino(inode));
6845 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6848 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6881 btrfs_extent_item_to_extent_map(inode, path, item, em);
6905 ret = read_inline_extent(inode, path, page);
6932 trace_btrfs_get_extent(root, inode, em);
6941 static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
6956 em = create_io_em(inode, start, len, orig_start, block_start,
6963 ordered = btrfs_alloc_ordered_extent(inode, start, len, len,
6971 btrfs_drop_extent_map_range(inode, start,
6984 static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
6988 struct btrfs_root *root = inode->root;
6995 alloc_hint = get_extent_allocation_hint(inode, start, len);
7001 wait_on_bit_io(&inode->root->fs_info->flags, BTRFS_FS_NEED_ZONE_FINISH,
7008 em = btrfs_create_dio_extent(inode, dio_data, start, ins.offset, start,
7052 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7056 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7061 struct btrfs_root *root = BTRFS_I(inode)->root;
7062 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7073 btrfs_ino(BTRFS_I(inode)), offset, 0);
7088 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7112 ret = can_nocow_file_extent(path, &key, BTRFS_I(inode), &nocow_args);
7126 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7152 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7158 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7175 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
7186 (!writing || !filemap_range_has_page(inode->i_mapping,
7206 * and locked its range in the inode's io tree, and a
7246 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7280 ret = btrfs_replace_extent_map_range(inode, em, true);
7292 struct inode *inode,
7298 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) &&
7328 if (can_nocow_extent(inode, start, &len, &orig_start,
7341 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7354 em2 = btrfs_create_dio_extent(BTRFS_I(inode), dio_data, start, len,
7394 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len, len,
7400 em = btrfs_new_extent_direct(BTRFS_I(inode), dio_data, start, len);
7408 btrfs_delalloc_release_metadata(BTRFS_I(inode),
7416 btrfs_delalloc_release_extents(BTRFS_I(inode), prev_len);
7422 if (start + len > i_size_read(inode))
7423 i_size_write(inode, start + len);
7426 btrfs_delalloc_release_extents(BTRFS_I(inode), len);
7427 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, true);
7433 static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7438 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7492 &BTRFS_I(inode)->runtime_flags)) {
7494 if (filemap_range_needs_writeback(inode->i_mapping,
7498 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7511 * current i_size (the inode lock is taken in shared mode). If we fail to
7517 ret = btrfs_check_data_free_space(BTRFS_I(inode),
7522 else if (ret && !(BTRFS_I(inode)->flags &
7532 ret = lock_extent_direct(inode, lockstart, lockend, &cached_state, flags);
7536 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
7605 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7625 btrfs_free_reserved_data_space(BTRFS_I(inode),
7641 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7667 unlock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7671 btrfs_free_reserved_data_space(BTRFS_I(inode),
7680 static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7691 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1,
7703 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7721 struct btrfs_inode *inode = bbio->inode;
7725 btrfs_warn(inode->root->fs_info,
7727 btrfs_ino(inode), bio->bi_opf,
7736 unlock_extent(&inode->io_tree, dip->file_offset,
7752 btrfs_bio_init(bbio, BTRFS_I(iter->inode)->root->fs_info,
7754 bbio->inode = BTRFS_I(iter->inode);
7813 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7818 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
7836 ret = btrfs_wait_ordered_range(inode, 0, LLONG_MAX);
7841 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
7930 struct btrfs_inode *inode = BTRFS_I(folio->mapping->host);
7931 struct btrfs_fs_info *fs_info = inode->root->fs_info;
7932 struct extent_io_tree *tree = &inode->io_tree;
7937 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
7982 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8034 spin_lock_irq(&inode->ordered_tree.lock);
8038 spin_unlock_irq(&inode->ordered_tree.lock);
8046 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8073 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL);
8104 * truncate_setsize() writes the inode size before removing pages, once we have
8112 struct inode *inode = file_inode(vmf->vma->vm_file);
8113 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8114 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8130 sb_start_pagefault(inode->i_sb);
8143 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8158 down_read(&BTRFS_I(inode)->i_mmap_lock);
8160 size = i_size_read(inode);
8162 if ((page->mapping != inode->i_mapping) ||
8181 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8186 up_read(&BTRFS_I(inode)->i_mmap_lock);
8197 btrfs_delalloc_release_space(BTRFS_I(inode),
8210 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8214 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
8235 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
8238 up_read(&BTRFS_I(inode)->i_mmap_lock);
8240 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8241 sb_end_pagefault(inode->i_sb);
8247 up_read(&BTRFS_I(inode)->i_mmap_lock);
8249 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8250 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
8253 sb_end_pagefault(inode->i_sb);
8258 static int btrfs_truncate(struct btrfs_inode *inode, bool skip_writeback)
8261 .inode = inode,
8262 .ino = btrfs_ino(inode),
8266 struct btrfs_root *root = inode->root;
8275 ret = btrfs_wait_ordered_range(&inode->vfs_inode,
8276 inode->vfs_inode.i_size & (~mask),
8286 * 1) We need to reserve space to update our inode.
8296 * doesn't end up using space reserved for updating the inode. We also
8298 * means we need to be able to update the inode several times, and we
8308 * updating the inode.
8318 * 1 for updating the inode.
8343 const u64 new_size = inode->vfs_inode.i_size;
8347 lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
8353 btrfs_drop_extent_map_range(inode,
8359 inode_sub_bytes(&inode->vfs_inode, control.sub_bytes);
8360 btrfs_inode_safe_disk_i_size_write(inode, control.last_size);
8362 unlock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state);
8368 ret = btrfs_update_inode(trans, root, inode);
8406 ret = btrfs_truncate_block(inode, inode->vfs_inode.i_size, 0, 0);
8414 btrfs_inode_safe_disk_i_size_write(inode, 0);
8421 ret2 = btrfs_update_inode(trans, root, inode);
8435 * first truncate that entire inode. So set this flag so we write out
8436 * all of the extents in the inode to the sync log so we're completely
8440 * fsync to truncate all the inode's items from the log and re-log them
8447 btrfs_set_inode_full_sync(inode);
8452 struct inode *btrfs_new_subvol_inode(struct mnt_idmap *idmap,
8453 struct inode *dir)
8455 struct inode *inode;
8457 inode = new_inode(dir->i_sb);
8458 if (inode) {
8463 inode_init_owner(idmap, inode, NULL,
8465 inode->i_op = &btrfs_dir_inode_operations;
8466 inode->i_fop = &btrfs_dir_file_operations;
8468 return inode;
8471 struct inode *btrfs_alloc_inode(struct super_block *sb)
8475 struct inode *inode;
8513 inode = &ei->vfs_inode;
8516 ei->io_tree.inode = ei;
8526 return inode;
8530 void btrfs_test_destroy_inode(struct inode *inode)
8532 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false);
8533 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8537 void btrfs_free_inode(struct inode *inode)
8539 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8542 void btrfs_destroy_inode(struct inode *vfs_inode)
8545 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8546 struct btrfs_root *root = inode->root;
8551 WARN_ON(inode->block_rsv.reserved);
8552 WARN_ON(inode->block_rsv.size);
8553 WARN_ON(inode->outstanding_extents);
8555 WARN_ON(inode->delalloc_bytes);
8556 WARN_ON(inode->new_delalloc_bytes);
8558 WARN_ON(inode->csum_bytes);
8559 WARN_ON(inode->defrag_bytes);
8562 * This can happen where we create an inode, but somebody else also
8563 * created the same inode and we need to destroy the one we already
8570 * If this is a free space inode do not take the ordered extents lockdep
8573 freespace_inode = btrfs_is_free_space_inode(inode);
8576 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8581 "found ordered extent %llu %llu on inode cleanup",
8587 btrfs_remove_ordered_extent(inode, ordered);
8592 btrfs_qgroup_check_reserved_leak(inode);
8593 inode_tree_del(inode);
8594 btrfs_drop_extent_map_range(inode, 0, (u64)-1, false);
8595 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8596 btrfs_put_root(inode->root);
8599 int btrfs_drop_inode(struct inode *inode)
8601 struct btrfs_root *root = BTRFS_I(inode)->root;
8610 return generic_drop_inode(inode);
8657 struct inode *inode = d_inode(path->dentry);
8658 u32 blocksize = inode->i_sb->s_blocksize;
8659 u32 bi_flags = BTRFS_I(inode)->flags;
8660 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
8663 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8664 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8681 generic_fillattr(idmap, request_mask, inode, stat);
8682 stat->dev = BTRFS_I(inode)->root->anon_dev;
8684 spin_lock(&BTRFS_I(inode)->lock);
8685 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
8686 inode_bytes = inode_get_bytes(inode);
8687 spin_unlock(&BTRFS_I(inode)->lock);
8693 static int btrfs_rename_exchange(struct inode *old_dir,
8695 struct inode *new_dir,
8703 struct inode *new_inode = new_dentry->d_inode;
8704 struct inode *old_inode = old_dentry->d_inode;
8719 * same inode namespace. Two subvolumes (represented as directory) can
8720 * be exchanged as they're a logical link and have a fixed inode number.
8746 * For each inode:
8751 * 1 to update parent inode
8766 * 1 to update inode item
8767 * 1 to remove old inode ref
8768 * 1 to add new inode ref
8830 /* Update inode version and ctime/mtime. */
8847 } else { /* src is an inode */
8862 } else { /* dest is an inode */
8930 static struct inode *new_whiteout_inode(struct mnt_idmap *idmap,
8931 struct inode *dir)
8933 struct inode *inode;
8935 inode = new_inode(dir->i_sb);
8936 if (inode) {
8937 inode_init_owner(idmap, inode, dir,
8939 inode->i_op = &btrfs_special_inode_operations;
8940 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
8942 return inode;
8946 struct inode *old_dir, struct dentry *old_dentry,
8947 struct inode *new_dir, struct dentry *new_dentry,
8959 struct inode *new_inode = d_inode(new_dentry);
8960 struct inode *old_inode = d_inode(old_dentry);
9017 whiteout_args.inode = new_whiteout_inode(idmap, old_dir);
9018 if (!whiteout_args.inode) {
9026 /* 1 to update the old parent inode. */
9042 * 1 to update inode
9043 * 1 to remove old inode ref
9044 * 1 to add new inode ref
9055 /* 1 to update new parent inode if it's not the same as the old parent */
9060 * 1 to update inode
9061 * 1 to remove inode ref
9159 unlock_new_inode(whiteout_args.inode);
9160 iput(whiteout_args.inode);
9161 whiteout_args.inode = NULL;
9174 iput(whiteout_args.inode);
9181 static int btrfs_rename2(struct mnt_idmap *idmap, struct inode *old_dir,
9182 struct dentry *old_dentry, struct inode *new_dir,
9203 struct inode *inode;
9212 struct inode *inode;
9216 inode = delalloc_work->inode;
9217 filemap_flush(inode->i_mapping);
9219 &BTRFS_I(inode)->runtime_flags))
9220 filemap_flush(inode->i_mapping);
9222 iput(inode);
9226 static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
9236 work->inode = inode;
9251 struct inode *inode;
9272 inode = igrab(&binode->vfs_inode);
9273 if (!inode) {
9283 work = btrfs_alloc_delalloc_work(inode);
9285 iput(inode);
9293 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
9294 btrfs_add_delayed_iput(BTRFS_I(inode));
9389 static int btrfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
9397 struct inode *inode;
9414 inode = new_inode(dir->i_sb);
9415 if (!inode)
9417 inode_init_owner(idmap, inode, dir, S_IFLNK | S_IRWXUGO);
9418 inode->i_op = &btrfs_symlink_inode_operations;
9419 inode_nohighmem(inode);
9420 inode->i_mapping->a_ops = &btrfs_aops;
9421 btrfs_i_size_write(BTRFS_I(inode), name_len);
9422 inode_set_bytes(inode, name_len);
9424 new_inode_args.inode = inode;
9445 discard_new_inode(inode);
9446 inode = NULL;
9449 key.objectid = btrfs_ino(BTRFS_I(inode));
9458 discard_new_inode(inode);
9459 inode = NULL;
9478 d_instantiate_new(dentry, inode);
9487 iput(inode);
9493 struct btrfs_inode *inode,
9516 ret = btrfs_qgroup_release_data(inode, file_offset, len, &qgroup_released);
9521 ret = insert_reserved_file_extent(trans, inode,
9546 ret = btrfs_replace_file_extents(inode, path, file_offset,
9562 btrfs_qgroup_free_refroot(inode->root->fs_info,
9563 inode->root->root_key.objectid, qgroup_released,
9568 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9573 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9575 struct btrfs_root *root = BTRFS_I(inode)->root;
9613 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9631 btrfs_drop_extent_map_range(BTRFS_I(inode), cur_offset,
9633 btrfs_set_inode_full_sync(BTRFS_I(inode));
9647 ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, true);
9654 inode_inc_iversion(inode);
9655 inode_set_ctime_current(inode);
9656 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
9658 (actual_len > inode->i_size) &&
9659 (cur_offset > inode->i_size)) {
9664 i_size_write(inode, i_size);
9665 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
9668 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
9683 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
9688 int btrfs_prealloc_file_range(struct inode *inode, int mode,
9692 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9697 int btrfs_prealloc_file_range_trans(struct inode *inode,
9702 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9707 struct inode *inode, int mask)
9709 struct btrfs_root *root = BTRFS_I(inode)->root;
9710 umode_t mode = inode->i_mode;
9716 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9719 return generic_permission(idmap, inode, mask);
9722 static int btrfs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
9728 struct inode *inode;
9737 inode = new_inode(dir->i_sb);
9738 if (!inode)
9740 inode_init_owner(idmap, inode, dir, mode);
9741 inode->i_fop = &btrfs_file_operations;
9742 inode->i_op = &btrfs_file_inode_operations;
9743 inode->i_mapping->a_ops = &btrfs_aops;
9745 new_inode_args.inode = inode;
9765 set_nlink(inode, 1);
9768 d_tmpfile(file, inode);
9769 unlock_new_inode(inode);
9770 mark_inode_dirty(inode);
9779 iput(inode);
9783 void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
9785 struct btrfs_fs_info *fs_info = inode->root->fs_info;
9794 page = find_get_page(inode->vfs_inode.i_mapping, index);
9836 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
9837 struct btrfs_root *root = inode->root;
9839 struct extent_io_tree *io_tree = &inode->io_tree;
9853 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
9869 inode->vfs_inode.i_size) - iocb->ki_pos;
9902 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
9940 int btrfs_encoded_read_regular_fill_pages(struct btrfs_inode *inode,
9944 struct btrfs_fs_info *fs_info = inode->root->fs_info;
9956 bbio->inode = inode;
9968 bbio->inode = inode;
9994 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
9995 struct extent_io_tree *io_tree = &inode->io_tree;
10012 ret = btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr,
10018 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10055 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10056 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10057 struct extent_io_tree *io_tree = &inode->io_tree;
10067 btrfs_inode_lock(inode, BTRFS_ILOCK_SHARED);
10069 if (iocb->ki_pos >= inode->vfs_inode.i_size) {
10070 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10083 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start,
10088 ordered = btrfs_lookup_ordered_range(inode, start,
10097 em = btrfs_get_extent(inode, NULL, 0, start, lockend - start + 1);
10123 inode->vfs_inode.i_size) - iocb->ki_pos;
10168 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10191 btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED);
10198 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp));
10199 struct btrfs_root *root = inode->root;
10201 struct extent_io_tree *io_tree = &inode->io_tree;
10243 * have a NOCOW file or inode was created while mounted with NODATASUM.
10245 if (inode->flags & BTRFS_INODE_NODATASUM)
10281 if (start + encoded->len < inode->vfs_inode.i_size &&
10326 ret = btrfs_wait_ordered_range(&inode->vfs_inode, start, num_bytes);
10329 ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping,
10335 ordered = btrfs_lookup_ordered_range(inode, start, num_bytes);
10337 !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end))
10349 ret = btrfs_alloc_data_chunk_ondemand(inode, disk_num_bytes);
10352 ret = btrfs_qgroup_reserve_data(inode, &data_reserved, start, num_bytes);
10355 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes, disk_num_bytes,
10363 ret = cow_file_range_inline(inode, encoded->len, orig_count,
10378 em = create_io_em(inode, start, num_bytes,
10388 ordered = btrfs_alloc_ordered_extent(inode, start, num_bytes, ram_bytes,
10395 btrfs_drop_extent_map_range(inode, start, end, false);
10401 if (start + encoded->len > inode->vfs_inode.i_size)
10402 i_size_write(&inode->vfs_inode, start + encoded->len);
10406 btrfs_delalloc_release_extents(inode, num_bytes);
10416 btrfs_delalloc_release_extents(inode, num_bytes);
10417 btrfs_delalloc_release_metadata(inode, disk_num_bytes, ret < 0);
10420 btrfs_qgroup_free_data(inode, data_reserved, start, num_bytes, NULL);
10448 static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10451 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10460 sp->inode = inode;
10470 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10473 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10490 static void btrfs_free_swapfile_pins(struct inode *inode)
10492 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10501 if (sp->inode == inode) {
10568 struct inode *inode = file_inode(file);
10570 btrfs_free_swapfile_pins(inode);
10571 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10577 struct inode *inode = file_inode(file);
10578 struct btrfs_root *root = BTRFS_I(inode)->root;
10580 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10596 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10601 * The inode is locked, so these flags won't change after we check them.
10603 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10607 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10611 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10667 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10676 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
10710 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, false, true);
10737 ret = btrfs_add_swapfile_pin(inode, device, false);
10773 ret = btrfs_add_swapfile_pin(inode, bg, true);
10839 * Update the number of bytes used in the VFS' inode. When we replace extents in
10841 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10844 void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10851 spin_lock(&inode->lock);
10853 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10855 inode_add_bytes(&inode->vfs_inode, add_bytes);
10856 spin_unlock(&inode->lock);
10862 * @inode: The target inode.
10867 * This should typically be used for cases where we locked an inode's VFS lock in
10868 * exclusive mode, we have also locked the inode's i_mmap_lock in exclusive mode,
10871 * the inode's io_tree.
10873 void btrfs_assert_inode_range_clean(struct btrfs_inode *inode, u64 start, u64 end)
10875 struct btrfs_root *root = inode->root;
10881 ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start);
10884 "found unexpected ordered extent in file range [%llu, %llu] for inode %llu root %llu (ordered range [%llu, %llu])",
10885 start, end, btrfs_ino(inode), root->root_key.objectid,