Lines Matching +full:build +full:- +full:only +full:- +full:delayed +full:- +full:rules
1 // SPDX-License-Identifier: GPL-2.0
9 #include <linux/blk-cgroup.h>
17 #include <linux/backing-dev.h>
39 #include "disk-io.h"
42 #include "ordered-data.h"
44 #include "tree-log.h"
50 #include "delalloc-space.h"
51 #include "block-group.h"
52 #include "space-info.h"
55 #include "inode-item.h"
58 #include "extent-tree.h"
59 #include "root-tree.h"
61 #include "dir-item.h"
62 #include "file-item.h"
63 #include "uuid-tree.h"
72 #include "raid-stripe-tree.h"
130 struct btrfs_fs_info *fs_info = warn->fs_info; in data_reloc_print_warning_inode()
151 ret = btrfs_search_slot(NULL, local_root, &key, &warn->path, 0, 0); in data_reloc_print_warning_inode()
154 btrfs_release_path(&warn->path); in data_reloc_print_warning_inode()
158 eb = warn->path.nodes[0]; in data_reloc_print_warning_inode()
159 inode_item = btrfs_item_ptr(eb, warn->path.slots[0], struct btrfs_inode_item); in data_reloc_print_warning_inode()
161 btrfs_release_path(&warn->path); in data_reloc_print_warning_inode()
164 ipath = init_ipath(4096, local_root, &warn->path); in data_reloc_print_warning_inode()
171 * -ENOMEM, not a critical error, just output an generic error in data_reloc_print_warning_inode()
176 warn->logical, warn->mirror_num, root, inum, offset); in data_reloc_print_warning_inode()
189 for (int i = 0; i < ipath->fspath->elem_cnt; i++) { in data_reloc_print_warning_inode()
192 warn->logical, warn->mirror_num, root, inum, offset, in data_reloc_print_warning_inode()
193 fs_info->sectorsize, nlink, in data_reloc_print_warning_inode()
194 (char *)(unsigned long)ipath->fspath->val[i]); in data_reloc_print_warning_inode()
204 warn->logical, warn->mirror_num, root, inum, offset, ret); in data_reloc_print_warning_inode()
211 * Do extra user-friendly error output (e.g. lookup all the affected files).
220 struct btrfs_fs_info *fs_info = inode->root->fs_info; in print_data_reloc_error()
225 const u32 csum_size = fs_info->csum_size; in print_data_reloc_error()
231 mutex_lock(&fs_info->reloc_mutex); in print_data_reloc_error()
233 mutex_unlock(&fs_info->reloc_mutex); in print_data_reloc_error()
239 btrfs_root_id(inode->root), btrfs_ino(inode), file_off, in print_data_reloc_error()
249 btrfs_root_id(inode->root), in print_data_reloc_error()
296 ctx.extent_item_pos = logical - found_key.objectid; in print_data_reloc_error()
312 struct btrfs_root *root = inode->root; in btrfs_print_data_csum_error()
313 const u32 csum_size = root->fs_info->csum_size; in btrfs_print_data_csum_error()
322 btrfs_warn_rl(root->fs_info, in btrfs_print_data_csum_error()
330 btrfs_warn_rl(root->fs_info, in btrfs_print_data_csum_error()
345 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
346 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
347 * return -EAGAIN
348 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
354 if (!inode_trylock_shared(&inode->vfs_inode)) in btrfs_inode_lock()
355 return -EAGAIN; in btrfs_inode_lock()
359 inode_lock_shared(&inode->vfs_inode); in btrfs_inode_lock()
362 if (!inode_trylock(&inode->vfs_inode)) in btrfs_inode_lock()
363 return -EAGAIN; in btrfs_inode_lock()
367 inode_lock(&inode->vfs_inode); in btrfs_inode_lock()
370 down_write(&inode->i_mmap_lock); in btrfs_inode_lock()
383 up_write(&inode->i_mmap_lock); in btrfs_inode_unlock()
385 inode_unlock_shared(&inode->vfs_inode); in btrfs_inode_unlock()
387 inode_unlock(&inode->vfs_inode); in btrfs_inode_unlock()
397 * to be released, which we want to happen only when finishing the ordered
404 const pgoff_t end_index = (offset + bytes - 1) >> PAGE_SHIFT; in btrfs_cleanup_ordered_extents()
408 folio = filemap_get_folio(inode->vfs_inode.i_mapping, index); in btrfs_cleanup_ordered_extents()
420 btrfs_folio_clamp_clear_ordered(inode->root->fs_info, folio, in btrfs_cleanup_ordered_extents()
435 if (args->default_acl) { in btrfs_init_inode_security()
436 ret = __btrfs_set_acl(trans, args->inode, args->default_acl, in btrfs_init_inode_security()
441 if (args->acl) { in btrfs_init_inode_security()
442 ret = __btrfs_set_acl(trans, args->inode, args->acl, ACL_TYPE_ACCESS); in btrfs_init_inode_security()
446 if (!args->default_acl && !args->acl) in btrfs_init_inode_security()
447 cache_no_acl(args->inode); in btrfs_init_inode_security()
448 return btrfs_xattr_security_init(trans, args->inode, args->dir, in btrfs_init_inode_security()
449 &args->dentry->d_name); in btrfs_init_inode_security()
465 struct btrfs_root *root = inode->root; in insert_inline_extent()
467 const u32 sectorsize = trans->fs_info->sectorsize; in insert_inline_extent()
484 * That's also why we only need one page as the parameter. in insert_inline_extent()
508 leaf = path->nodes[0]; in insert_inline_extent()
509 ei = btrfs_item_ptr(leaf, path->slots[0], in insert_inline_extent()
511 btrfs_set_file_extent_generation(leaf, ei, trans->transid); in insert_inline_extent()
528 folio = filemap_get_folio(inode->vfs_inode.i_mapping, 0); in insert_inline_extent()
543 ALIGN(size, root->fs_info->sectorsize)); in insert_inline_extent()
554 i_size = i_size_read(&inode->vfs_inode); in insert_inline_extent()
556 i_size_write(&inode->vfs_inode, size); in insert_inline_extent()
559 inode->disk_i_size = i_size; in insert_inline_extent()
569 struct btrfs_fs_info *fs_info = inode->root->fs_info; in can_cow_file_range_inline()
577 if (size > fs_info->sectorsize) in can_cow_file_range_inline()
580 /* We do not allow a non-compressed extent to be as large as block size. */ in can_cow_file_range_inline()
581 if (data_len >= fs_info->sectorsize) in can_cow_file_range_inline()
589 if (data_len > fs_info->max_inline) in can_cow_file_range_inline()
593 if (size < i_size_read(&inode->vfs_inode)) in can_cow_file_range_inline()
614 struct btrfs_root *root = inode->root; in __cow_file_range_inline()
615 struct btrfs_fs_info *fs_info = root->fs_info; in __cow_file_range_inline()
623 return -ENOMEM; in __cow_file_range_inline()
630 trans->block_rsv = &inode->block_rsv; in __cow_file_range_inline()
634 drop_args.end = fs_info->sectorsize; in __cow_file_range_inline()
647 if (unlikely(ret && ret != -ENOSPC)) { in __cow_file_range_inline()
650 } else if (ret == -ENOSPC) { in __cow_file_range_inline()
657 if (unlikely(ret && ret != -ENOSPC)) { in __cow_file_range_inline()
660 } else if (ret == -ENOSPC) { in __cow_file_range_inline()
673 btrfs_qgroup_free_data(inode, NULL, 0, fs_info->sectorsize, NULL); in __cow_file_range_inline()
690 u64 size = min_t(u64, i_size_read(&inode->vfs_inode), end + 1); in cow_file_range_inline()
696 btrfs_lock_extent(&inode->io_tree, offset, end, &cached); in cow_file_range_inline()
701 btrfs_unlock_extent(&inode->io_tree, offset, end, &cached); in cow_file_range_inline()
766 return -ENOMEM; in add_async_extent()
767 async_extent->start = start; in add_async_extent()
768 async_extent->ram_size = ram_size; in add_async_extent()
769 async_extent->compressed_size = compressed_size; in add_async_extent()
770 async_extent->folios = folios; in add_async_extent()
771 async_extent->nr_folios = nr_folios; in add_async_extent()
772 async_extent->compress_type = compress_type; in add_async_extent()
773 list_add_tail(&async_extent->list, &cow->extents); in add_async_extent()
784 struct btrfs_fs_info *fs_info = inode->root->fs_info; in inode_need_compress()
792 if (inode->defrag_compress == BTRFS_DEFRAG_DONT_COMPRESS) in inode_need_compress()
794 if (BTRFS_COMPRESS_NONE < inode->defrag_compress && in inode_need_compress()
795 inode->defrag_compress < BTRFS_NR_COMPRESS_TYPES) in inode_need_compress()
801 if (inode->flags & BTRFS_INODE_NOCOMPRESS) in inode_need_compress()
804 inode->flags & BTRFS_INODE_COMPRESS || in inode_need_compress()
805 inode->prop_compress) in inode_need_compress()
815 (start > 0 || end + 1 < inode->disk_i_size)) in inode_should_defrag()
826 folio = filemap_get_folio(inode->vfs_inode.i_mapping, index); in extent_range_clear_dirty_for_io()
832 btrfs_folio_clamp_clear_dirty(inode->root->fs_info, folio, start, in extent_range_clear_dirty_for_io()
833 end + 1 - start); in extent_range_clear_dirty_for_io()
856 struct btrfs_inode *inode = async_chunk->inode; in compress_file_range()
857 struct btrfs_fs_info *fs_info = inode->root->fs_info; in compress_file_range()
858 struct address_space *mapping = inode->vfs_inode.i_mapping; in compress_file_range()
859 const u32 min_folio_shift = PAGE_SHIFT + fs_info->block_min_order; in compress_file_range()
861 u64 blocksize = fs_info->sectorsize; in compress_file_range()
862 u64 start = async_chunk->start; in compress_file_range()
863 u64 end = async_chunk->end; in compress_file_range()
873 int compress_type = fs_info->compress_type; in compress_file_range()
874 int compress_level = fs_info->compress_level; in compress_file_range()
876 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K); in compress_file_range()
904 i_size = i_size_read(&inode->vfs_inode); in compress_file_range()
909 nr_folios = (end >> min_folio_shift) - (start >> min_folio_shift) + 1; in compress_file_range()
925 total_compressed = actual_end - start; in compress_file_range()
932 (start > 0 || end + 1 < inode->disk_i_size)) in compress_file_range()
941 * We do compression for mount -o compress and when the inode has not in compress_file_range()
957 if (0 < inode->defrag_compress && inode->defrag_compress < BTRFS_NR_COMPRESS_TYPES) { in compress_file_range()
958 compress_type = inode->defrag_compress; in compress_file_range()
959 compress_level = inode->defrag_compress_level; in compress_file_range()
960 } else if (inode->prop_compress) { in compress_file_range()
961 compress_type = inode->prop_compress; in compress_file_range()
975 loff = (total_compressed & (min_folio_size - 1)); in compress_file_range()
977 folio_zero_range(folios[nr_folios - 1], loff, min_folio_size - loff); in compress_file_range()
996 mapping_set_error(mapping, -EIO); in compress_file_range()
1011 total_in = round_up(total_in, fs_info->sectorsize); in compress_file_range()
1030 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && !inode->prop_compress) in compress_file_range()
1031 inode->flags |= BTRFS_INODE_NOCOMPRESS; in compress_file_range()
1033 ret = add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0, in compress_file_range()
1039 WARN_ON(folios[i]->mapping); in compress_file_range()
1050 if (!async_extent->folios) in free_async_extent_pages()
1053 for (i = 0; i < async_extent->nr_folios; i++) { in free_async_extent_pages()
1054 WARN_ON(async_extent->folios[i]->mapping); in free_async_extent_pages()
1055 btrfs_free_compr_folio(async_extent->folios[i]); in free_async_extent_pages()
1057 kfree(async_extent->folios); in free_async_extent_pages()
1058 async_extent->nr_folios = 0; in free_async_extent_pages()
1059 async_extent->folios = NULL; in free_async_extent_pages()
1066 u64 start = async_extent->start; in submit_uncompressed_range()
1067 u64 end = async_extent->start + async_extent->ram_size - 1; in submit_uncompressed_range()
1076 wbc_attach_fdatawrite_inode(&wbc, &inode->vfs_inode); in submit_uncompressed_range()
1082 btrfs_folio_end_lock(inode->root->fs_info, locked_folio, in submit_uncompressed_range()
1083 start, async_extent->ram_size); in submit_uncompressed_range()
1084 btrfs_err_rl(inode->root->fs_info, in submit_uncompressed_range()
1086 __func__, btrfs_root_id(inode->root), in submit_uncompressed_range()
1087 btrfs_ino(inode), start, async_extent->ram_size, ret); in submit_uncompressed_range()
1095 struct btrfs_inode *inode = async_chunk->inode; in submit_one_async_extent()
1096 struct extent_io_tree *io_tree = &inode->io_tree; in submit_one_async_extent()
1097 struct btrfs_root *root = inode->root; in submit_one_async_extent()
1098 struct btrfs_fs_info *fs_info = root->fs_info; in submit_one_async_extent()
1107 u64 start = async_extent->start; in submit_one_async_extent()
1108 u64 end = async_extent->start + async_extent->ram_size - 1; in submit_one_async_extent()
1110 if (async_chunk->blkcg_css) in submit_one_async_extent()
1111 kthread_associate_blkcg(async_chunk->blkcg_css); in submit_one_async_extent()
1114 * If async_chunk->locked_folio is in the async_extent range, we need to in submit_one_async_extent()
1117 if (async_chunk->locked_folio) { in submit_one_async_extent()
1118 u64 locked_folio_start = folio_pos(async_chunk->locked_folio); in submit_one_async_extent()
1120 folio_size(async_chunk->locked_folio) - 1; in submit_one_async_extent()
1123 locked_folio = async_chunk->locked_folio; in submit_one_async_extent()
1126 if (async_extent->compress_type == BTRFS_COMPRESS_NONE) { in submit_one_async_extent()
1127 ASSERT(!async_extent->folios); in submit_one_async_extent()
1128 ASSERT(async_extent->nr_folios == 0); in submit_one_async_extent()
1134 ret = btrfs_reserve_extent(root, async_extent->ram_size, in submit_one_async_extent()
1135 async_extent->compressed_size, in submit_one_async_extent()
1136 async_extent->compressed_size, in submit_one_async_extent()
1142 * non-contiguous space for the uncompressed size instead. So in submit_one_async_extent()
1155 file_extent.ram_bytes = async_extent->ram_size; in submit_one_async_extent()
1156 file_extent.num_bytes = async_extent->ram_size; in submit_one_async_extent()
1158 file_extent.compression = async_extent->compress_type; in submit_one_async_extent()
1181 async_extent->folios, /* compressed_folios */ in submit_one_async_extent()
1182 async_extent->nr_folios, in submit_one_async_extent()
1183 async_chunk->write_flags, true); in submit_one_async_extent()
1186 if (async_chunk->blkcg_css) in submit_one_async_extent()
1196 mapping_set_error(inode->vfs_inode.i_mapping, -EIO); in submit_one_async_extent()
1205 if (async_chunk->blkcg_css) in submit_one_async_extent()
1210 async_extent->ram_size, ret); in submit_one_async_extent()
1217 struct extent_map_tree *em_tree = &inode->extent_tree; in btrfs_get_extent_allocation_hint()
1221 read_lock(&em_tree->lock); in btrfs_get_extent_allocation_hint()
1229 if (em->disk_bytenr >= EXTENT_MAP_LAST_BYTE) { in btrfs_get_extent_allocation_hint()
1232 if (em && em->disk_bytenr < EXTENT_MAP_LAST_BYTE) in btrfs_get_extent_allocation_hint()
1241 read_unlock(&em_tree->lock); in btrfs_get_extent_allocation_hint()
1247 * when extent_io.c finds a delayed allocation range in the file,
1260 * the only folio handled anyway).
1265 * - If COW_FILE_RANGE_KEEP_LOCKED flag is set, all folios are kept locked.
1266 * - Else all folios except for @locked_folio are unlocked.
1269 * while-loop, the ordered extents created in previous iterations are cleaned up.
1276 struct btrfs_root *root = inode->root; in cow_file_range()
1277 struct btrfs_fs_info *fs_info = root->fs_info; in cow_file_range()
1284 u64 blocksize = fs_info->sectorsize; in cow_file_range()
1292 ret = -EINVAL; in cow_file_range()
1296 num_bytes = ALIGN(end - start + 1, blocksize); in cow_file_range()
1298 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy)); in cow_file_range()
1347 min_alloc_size = fs_info->sectorsize; in cow_file_range()
1356 if (ret == -EAGAIN) { in cow_file_range()
1358 * btrfs_reserve_extent only returns -EAGAIN for zoned in cow_file_range()
1366 * us, or return -ENOSPC if it can't handle retries. in cow_file_range()
1370 wait_on_bit_io(&inode->root->fs_info->flags, in cow_file_range()
1380 end = start - 1; in cow_file_range()
1384 ret = -ENOSPC; in cow_file_range()
1401 btrfs_lock_extent(&inode->io_tree, start, start + cur_alloc_size - 1, in cow_file_range()
1407 btrfs_unlock_extent(&inode->io_tree, start, in cow_file_range()
1408 start + cur_alloc_size - 1, &cached); in cow_file_range()
1417 btrfs_unlock_extent(&inode->io_tree, start, in cow_file_range()
1418 start + cur_alloc_size - 1, &cached); in cow_file_range()
1427 * Only drop cache here, and process as normal. in cow_file_range()
1439 start + cur_alloc_size - 1, in cow_file_range()
1449 num_bytes -= cur_alloc_size; in cow_file_range()
1470 btrfs_drop_extent_map_range(inode, start, start + cur_alloc_size - 1, false); in cow_file_range()
1478 * |-------(1)----|---(2)---|-------------(3)----------| in cow_file_range()
1479 * `- orig_start `- start `- start + cur_alloc_size `- end in cow_file_range()
1490 * So here we only clear EXTENT_LOCKED and EXTENT_DELALLOC flag, and in cow_file_range()
1498 mapping_set_error(inode->vfs_inode.i_mapping, ret); in cow_file_range()
1500 btrfs_cleanup_ordered_extents(inode, orig_start, start - orig_start); in cow_file_range()
1501 extent_clear_unlock_delalloc(inode, orig_start, start - 1, in cow_file_range()
1521 start + cur_alloc_size - 1, in cow_file_range()
1539 end - start - cur_alloc_size + 1, NULL); in cow_file_range()
1543 __func__, btrfs_root_id(inode->root), in cow_file_range()
1544 btrfs_ino(inode), orig_start, end + 1 - orig_start, in cow_file_range()
1551 * which only gets called in the order the work was queued. We walk all the
1569 btrfs_add_delayed_iput(async_chunk->inode); in submit_compressed_extents()
1570 if (async_chunk->blkcg_css) in submit_compressed_extents()
1571 css_put(async_chunk->blkcg_css); in submit_compressed_extents()
1573 async_cow = async_chunk->async_cow; in submit_compressed_extents()
1574 if (atomic_dec_and_test(&async_cow->num_chunks)) in submit_compressed_extents()
1579 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >> in submit_compressed_extents()
1582 while (!list_empty(&async_chunk->extents)) { in submit_compressed_extents()
1583 async_extent = list_first_entry(&async_chunk->extents, in submit_compressed_extents()
1585 list_del(&async_extent->list); in submit_compressed_extents()
1590 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) < in submit_compressed_extents()
1592 cond_wake_up_nomb(&fs_info->async_submit_wait); in submit_compressed_extents()
1599 struct btrfs_fs_info *fs_info = inode->root->fs_info; in run_delalloc_compressed()
1604 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K); in run_delalloc_compressed()
1615 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags); in run_delalloc_compressed()
1617 async_chunk = ctx->chunks; in run_delalloc_compressed()
1618 atomic_set(&ctx->num_chunks, num_chunks); in run_delalloc_compressed()
1621 u64 cur_end = min(end, start + SZ_512K - 1); in run_delalloc_compressed()
1624 * igrab is called higher up in the call chain, take only the in run_delalloc_compressed()
1627 ihold(&inode->vfs_inode); in run_delalloc_compressed()
1639 * structs, only the first struct needs a pointer to in run_delalloc_compressed()
1651 * only for foreign writeback detection and doesn't in run_delalloc_compressed()
1656 cur_end - start); in run_delalloc_compressed()
1674 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE); in run_delalloc_compressed()
1675 atomic_add(nr_pages, &fs_info->async_delalloc_pages); in run_delalloc_compressed()
1677 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work); in run_delalloc_compressed()
1701 extent_write_locked_range(&inode->vfs_inode, locked_folio, in run_delalloc_cow()
1714 const bool is_reloc_ino = btrfs_is_data_reloc_root(inode->root); in fallback_to_cow()
1715 const u64 range_bytes = end + 1 - start; in fallback_to_cow()
1716 struct extent_io_tree *io_tree = &inode->io_tree; in fallback_to_cow()
1759 struct btrfs_fs_info *fs_info = inode->root->fs_info; in fallback_to_cow()
1760 struct btrfs_space_info *sinfo = fs_info->data_sinfo; in fallback_to_cow()
1765 spin_lock(&sinfo->lock); in fallback_to_cow()
1767 spin_unlock(&sinfo->lock); in fallback_to_cow()
1807 * Output fields. Only set when can_nocow_file_extent() returns 1.
1816 * if path->nodes[0] is NULL or not if it needs to use the path afterwards.
1828 struct extent_buffer *leaf = path->nodes[0]; in can_nocow_file_extent()
1829 struct btrfs_root *root = inode->root; in can_nocow_file_extent()
1837 bool nowait = path->nowait; in can_nocow_file_extent()
1839 fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); in can_nocow_file_extent()
1845 if (!(inode->flags & BTRFS_INODE_NODATACOW) && in can_nocow_file_extent()
1855 btrfs_root_last_snapshot(&root->root_item)) in can_nocow_file_extent()
1870 args->file_extent.disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); in can_nocow_file_extent()
1871 args->file_extent.disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); in can_nocow_file_extent()
1872 args->file_extent.ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); in can_nocow_file_extent()
1873 args->file_extent.offset = btrfs_file_extent_offset(leaf, fi); in can_nocow_file_extent()
1874 args->file_extent.compression = btrfs_file_extent_compression(leaf, fi); in can_nocow_file_extent()
1883 ret = btrfs_cross_ref_exist(inode, key->offset - args->file_extent.offset, in can_nocow_file_extent()
1884 args->file_extent.disk_bytenr, path); in can_nocow_file_extent()
1889 if (args->free_path) { in can_nocow_file_extent()
1901 if (args->writeback_path && !is_freespace_inode && in can_nocow_file_extent()
1902 atomic_read(&root->snapshot_force_cow)) in can_nocow_file_extent()
1905 args->file_extent.num_bytes = min(args->end + 1, extent_end) - args->start; in can_nocow_file_extent()
1906 args->file_extent.offset += args->start - key->offset; in can_nocow_file_extent()
1907 io_start = args->file_extent.disk_bytenr + args->file_extent.offset; in can_nocow_file_extent()
1914 csum_root = btrfs_csum_root(root->fs_info, io_start); in can_nocow_file_extent()
1916 io_start + args->file_extent.num_bytes - 1, in can_nocow_file_extent()
1924 if (args->free_path && path) in can_nocow_file_extent()
1936 const u64 len = nocow_args->file_extent.num_bytes; in nocow_one_range()
1937 const u64 end = file_pos + len - 1; in nocow_one_range()
1940 btrfs_lock_extent(&inode->io_tree, file_pos, end, cached); in nocow_one_range()
1945 em = btrfs_create_io_em(inode, file_pos, &nocow_args->file_extent, in nocow_one_range()
1954 ordered = btrfs_alloc_ordered_extent(inode, file_pos, &nocow_args->file_extent, in nocow_one_range()
1965 if (btrfs_is_data_reloc_root(inode->root)) in nocow_one_range()
1989 btrfs_err(inode->root->fs_info, in nocow_one_range()
1991 __func__, btrfs_root_id(inode->root), btrfs_ino(inode), in nocow_one_range()
2007 struct btrfs_fs_info *fs_info = inode->root->fs_info; in run_delalloc_nocow()
2008 struct btrfs_root *root = inode->root; in run_delalloc_nocow()
2010 u64 cow_start = (u64)-1; in run_delalloc_nocow()
2013 * range. Only for error handling. in run_delalloc_nocow()
2033 * Normally on a zoned device we're only doing COW writes, but in case in run_delalloc_nocow()
2034 * of relocation on a zoned filesystem serializes I/O so that we're only in run_delalloc_nocow()
2041 ret = -ENOMEM; in run_delalloc_nocow()
2067 if (ret > 0 && path->slots[0] > 0 && check_prev) { in run_delalloc_nocow()
2068 leaf = path->nodes[0]; in run_delalloc_nocow()
2070 path->slots[0] - 1); in run_delalloc_nocow()
2073 path->slots[0]--; in run_delalloc_nocow()
2078 leaf = path->nodes[0]; in run_delalloc_nocow()
2079 if (path->slots[0] >= btrfs_header_nritems(leaf)) { in run_delalloc_nocow()
2085 leaf = path->nodes[0]; in run_delalloc_nocow()
2088 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in run_delalloc_nocow()
2099 path->slots[0]++; in run_delalloc_nocow()
2113 if (cow_start == (u64)-1) in run_delalloc_nocow()
2123 fi = btrfs_item_ptr(leaf, path->slots[0], in run_delalloc_nocow()
2129 ret = -EUCLEAN; in run_delalloc_nocow()
2139 path->slots[0]++; in run_delalloc_nocow()
2163 if (cow_start == (u64)-1) in run_delalloc_nocow()
2168 if (!path->nodes[0]) in run_delalloc_nocow()
2170 path->slots[0]++; in run_delalloc_nocow()
2175 * COW range from cow_start to found_key.offset - 1. As the key in run_delalloc_nocow()
2179 if (cow_start != (u64)-1) { in run_delalloc_nocow()
2181 found_key.offset - 1); in run_delalloc_nocow()
2183 cow_end = found_key.offset - 1; in run_delalloc_nocow()
2187 cow_start = (u64)-1; in run_delalloc_nocow()
2195 nocow_end = cur_offset + nocow_args.file_extent.num_bytes - 1; in run_delalloc_nocow()
2202 if (cur_offset <= end && cow_start == (u64)-1) in run_delalloc_nocow()
2205 if (cow_start != (u64)-1) { in run_delalloc_nocow()
2211 cow_start = (u64)-1; in run_delalloc_nocow()
2226 if (cow_start == (u64)-1) { in run_delalloc_nocow()
2243 oe_cleanup_len = cur_offset - start; in run_delalloc_nocow()
2248 untouched_len = end + 1 - untouched_start; in run_delalloc_nocow()
2249 } else if (cow_start != (u64)-1 && cow_end == 0) { in run_delalloc_nocow()
2259 oe_cleanup_len = cow_start - start; in run_delalloc_nocow()
2261 untouched_len = end + 1 - untouched_start; in run_delalloc_nocow()
2272 ASSERT(cow_start != (u64)-1 && cow_end != 0); in run_delalloc_nocow()
2274 oe_cleanup_len = cow_start - start; in run_delalloc_nocow()
2276 untouched_len = end + 1 - untouched_start; in run_delalloc_nocow()
2280 const u64 oe_cleanup_end = oe_cleanup_start + oe_cleanup_len - 1; in run_delalloc_nocow()
2291 const u64 untouched_end = untouched_start + untouched_len - 1; in run_delalloc_nocow()
2297 btrfs_lock_extent(&inode->io_tree, untouched_start, untouched_end, &cached); in run_delalloc_nocow()
2310 __func__, btrfs_root_id(inode->root), btrfs_ino(inode), in run_delalloc_nocow()
2311 start, end + 1 - start, cur_offset, oe_cleanup_start, oe_cleanup_len, in run_delalloc_nocow()
2318 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { in should_nocow()
2319 if (inode->defrag_bytes && in should_nocow()
2320 btrfs_test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG)) in should_nocow()
2328 * Function to process delayed allocation (create CoW) for ranges which are
2334 const bool zoned = btrfs_is_zoned(inode->root->fs_info); in btrfs_run_delalloc_range()
2364 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_split_delalloc_extent()
2367 lockdep_assert_held(&inode->io_tree.lock); in btrfs_split_delalloc_extent()
2370 if (!(orig->state & EXTENT_DELALLOC)) in btrfs_split_delalloc_extent()
2373 size = orig->end - orig->start + 1; in btrfs_split_delalloc_extent()
2374 if (size > fs_info->max_extent_size) { in btrfs_split_delalloc_extent()
2382 new_size = orig->end - split + 1; in btrfs_split_delalloc_extent()
2384 new_size = split - orig->start; in btrfs_split_delalloc_extent()
2390 spin_lock(&inode->lock); in btrfs_split_delalloc_extent()
2392 spin_unlock(&inode->lock); in btrfs_split_delalloc_extent()
2396 * Handle merged delayed allocation extents so we can keep track of new extents
2403 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_merge_delalloc_extent()
2407 lockdep_assert_held(&inode->io_tree.lock); in btrfs_merge_delalloc_extent()
2410 if (!(other->state & EXTENT_DELALLOC)) in btrfs_merge_delalloc_extent()
2413 if (new->start > other->start) in btrfs_merge_delalloc_extent()
2414 new_size = new->end - other->start + 1; in btrfs_merge_delalloc_extent()
2416 new_size = other->end - new->start + 1; in btrfs_merge_delalloc_extent()
2419 if (new_size <= fs_info->max_extent_size) { in btrfs_merge_delalloc_extent()
2420 spin_lock(&inode->lock); in btrfs_merge_delalloc_extent()
2421 btrfs_mod_outstanding_extents(inode, -1); in btrfs_merge_delalloc_extent()
2422 spin_unlock(&inode->lock); in btrfs_merge_delalloc_extent()
2441 * they are only 3 extents worth of accounting, so we need to drop in in btrfs_merge_delalloc_extent()
2444 old_size = other->end - other->start + 1; in btrfs_merge_delalloc_extent()
2446 old_size = new->end - new->start + 1; in btrfs_merge_delalloc_extent()
2451 spin_lock(&inode->lock); in btrfs_merge_delalloc_extent()
2452 btrfs_mod_outstanding_extents(inode, -1); in btrfs_merge_delalloc_extent()
2453 spin_unlock(&inode->lock); in btrfs_merge_delalloc_extent()
2458 struct btrfs_root *root = inode->root; in btrfs_add_delalloc_inode()
2459 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_add_delalloc_inode()
2461 spin_lock(&root->delalloc_lock); in btrfs_add_delalloc_inode()
2462 ASSERT(list_empty(&inode->delalloc_inodes)); in btrfs_add_delalloc_inode()
2463 list_add_tail(&inode->delalloc_inodes, &root->delalloc_inodes); in btrfs_add_delalloc_inode()
2464 root->nr_delalloc_inodes++; in btrfs_add_delalloc_inode()
2465 if (root->nr_delalloc_inodes == 1) { in btrfs_add_delalloc_inode()
2466 spin_lock(&fs_info->delalloc_root_lock); in btrfs_add_delalloc_inode()
2467 ASSERT(list_empty(&root->delalloc_root)); in btrfs_add_delalloc_inode()
2468 list_add_tail(&root->delalloc_root, &fs_info->delalloc_roots); in btrfs_add_delalloc_inode()
2469 spin_unlock(&fs_info->delalloc_root_lock); in btrfs_add_delalloc_inode()
2471 spin_unlock(&root->delalloc_lock); in btrfs_add_delalloc_inode()
2476 struct btrfs_root *root = inode->root; in btrfs_del_delalloc_inode()
2477 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_del_delalloc_inode()
2479 lockdep_assert_held(&root->delalloc_lock); in btrfs_del_delalloc_inode()
2485 * still has ->delalloc_bytes > 0. in btrfs_del_delalloc_inode()
2487 if (!list_empty(&inode->delalloc_inodes)) { in btrfs_del_delalloc_inode()
2488 list_del_init(&inode->delalloc_inodes); in btrfs_del_delalloc_inode()
2489 root->nr_delalloc_inodes--; in btrfs_del_delalloc_inode()
2490 if (!root->nr_delalloc_inodes) { in btrfs_del_delalloc_inode()
2491 ASSERT(list_empty(&root->delalloc_inodes)); in btrfs_del_delalloc_inode()
2492 spin_lock(&fs_info->delalloc_root_lock); in btrfs_del_delalloc_inode()
2493 ASSERT(!list_empty(&root->delalloc_root)); in btrfs_del_delalloc_inode()
2494 list_del_init(&root->delalloc_root); in btrfs_del_delalloc_inode()
2495 spin_unlock(&fs_info->delalloc_root_lock); in btrfs_del_delalloc_inode()
2501 * Properly track delayed allocation bytes in the inode and to maintain the
2507 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_set_delalloc_extent()
2509 lockdep_assert_held(&inode->io_tree.lock); in btrfs_set_delalloc_extent()
2515 * but in this case, we are only testing for the DELALLOC in btrfs_set_delalloc_extent()
2516 * bit, which is only set or cleared with irqs on in btrfs_set_delalloc_extent()
2518 if (!(state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { in btrfs_set_delalloc_extent()
2519 u64 len = state->end + 1 - state->start; in btrfs_set_delalloc_extent()
2523 spin_lock(&inode->lock); in btrfs_set_delalloc_extent()
2525 spin_unlock(&inode->lock); in btrfs_set_delalloc_extent()
2531 percpu_counter_add_batch(&fs_info->delalloc_bytes, len, in btrfs_set_delalloc_extent()
2532 fs_info->delalloc_batch); in btrfs_set_delalloc_extent()
2533 spin_lock(&inode->lock); in btrfs_set_delalloc_extent()
2534 prev_delalloc_bytes = inode->delalloc_bytes; in btrfs_set_delalloc_extent()
2535 inode->delalloc_bytes += len; in btrfs_set_delalloc_extent()
2537 inode->defrag_bytes += len; in btrfs_set_delalloc_extent()
2538 spin_unlock(&inode->lock); in btrfs_set_delalloc_extent()
2550 if (!(state->state & EXTENT_DELALLOC_NEW) && in btrfs_set_delalloc_extent()
2552 spin_lock(&inode->lock); in btrfs_set_delalloc_extent()
2553 inode->new_delalloc_bytes += state->end + 1 - state->start; in btrfs_set_delalloc_extent()
2554 spin_unlock(&inode->lock); in btrfs_set_delalloc_extent()
2565 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_clear_delalloc_extent()
2566 u64 len = state->end + 1 - state->start; in btrfs_clear_delalloc_extent()
2569 lockdep_assert_held(&inode->io_tree.lock); in btrfs_clear_delalloc_extent()
2571 if ((state->state & EXTENT_DEFRAG) && (bits & EXTENT_DEFRAG)) { in btrfs_clear_delalloc_extent()
2572 spin_lock(&inode->lock); in btrfs_clear_delalloc_extent()
2573 inode->defrag_bytes -= len; in btrfs_clear_delalloc_extent()
2574 spin_unlock(&inode->lock); in btrfs_clear_delalloc_extent()
2579 * but in this case, we are only testing for the DELALLOC in btrfs_clear_delalloc_extent()
2580 * bit, which is only set or cleared with irqs on in btrfs_clear_delalloc_extent()
2582 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { in btrfs_clear_delalloc_extent()
2583 struct btrfs_root *root = inode->root; in btrfs_clear_delalloc_extent()
2586 spin_lock(&inode->lock); in btrfs_clear_delalloc_extent()
2587 btrfs_mod_outstanding_extents(inode, -num_extents); in btrfs_clear_delalloc_extent()
2588 spin_unlock(&inode->lock); in btrfs_clear_delalloc_extent()
2596 root != fs_info->tree_root) in btrfs_clear_delalloc_extent()
2605 !(state->state & EXTENT_NORESERVE) && in btrfs_clear_delalloc_extent()
2609 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len, in btrfs_clear_delalloc_extent()
2610 fs_info->delalloc_batch); in btrfs_clear_delalloc_extent()
2611 spin_lock(&inode->lock); in btrfs_clear_delalloc_extent()
2612 inode->delalloc_bytes -= len; in btrfs_clear_delalloc_extent()
2613 new_delalloc_bytes = inode->delalloc_bytes; in btrfs_clear_delalloc_extent()
2614 spin_unlock(&inode->lock); in btrfs_clear_delalloc_extent()
2623 spin_lock(&root->delalloc_lock); in btrfs_clear_delalloc_extent()
2625 spin_unlock(&root->delalloc_lock); in btrfs_clear_delalloc_extent()
2629 if ((state->state & EXTENT_DELALLOC_NEW) && in btrfs_clear_delalloc_extent()
2631 spin_lock(&inode->lock); in btrfs_clear_delalloc_extent()
2632 ASSERT(inode->new_delalloc_bytes >= len); in btrfs_clear_delalloc_extent()
2633 inode->new_delalloc_bytes -= len; in btrfs_clear_delalloc_extent()
2635 inode_add_bytes(&inode->vfs_inode, len); in btrfs_clear_delalloc_extent()
2636 spin_unlock(&inode->lock); in btrfs_clear_delalloc_extent()
2652 trans->adding_csums = true; in add_pending_csums()
2654 csum_root = btrfs_csum_root(trans->fs_info, in add_pending_csums()
2655 sum->logical); in add_pending_csums()
2657 trans->adding_csums = false; in add_pending_csums()
2670 const u64 end = start + len - 1; in btrfs_find_new_delalloc_bytes()
2673 const u64 search_len = end - search_start + 1; in btrfs_find_new_delalloc_bytes()
2682 if (em->disk_bytenr != EXTENT_MAP_HOLE) in btrfs_find_new_delalloc_bytes()
2685 em_len = em->len; in btrfs_find_new_delalloc_bytes()
2686 if (em->start < search_start) in btrfs_find_new_delalloc_bytes()
2687 em_len -= search_start - em->start; in btrfs_find_new_delalloc_bytes()
2691 ret = btrfs_set_extent_bit(&inode->io_tree, search_start, in btrfs_find_new_delalloc_bytes()
2692 search_start + em_len - 1, in btrfs_find_new_delalloc_bytes()
2709 if (start >= i_size_read(&inode->vfs_inode) && in btrfs_set_extent_delalloc()
2710 !(inode->flags & BTRFS_INODE_PREALLOC)) { in btrfs_set_extent_delalloc()
2720 end + 1 - start, in btrfs_set_extent_delalloc()
2726 return btrfs_set_extent_bit(&inode->io_tree, start, end, in btrfs_set_extent_delalloc()
2744 struct folio *folio = fixup->folio; in btrfs_writepage_fixup_worker()
2745 struct btrfs_inode *inode = fixup->inode; in btrfs_writepage_fixup_worker()
2746 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_writepage_fixup_worker()
2748 u64 page_end = folio_end(folio) - 1; in btrfs_writepage_fixup_worker()
2763 * folio->mapping may go NULL, but it shouldn't be moved to a different in btrfs_writepage_fixup_worker()
2766 if (!folio->mapping || !folio_test_dirty(folio) || in btrfs_writepage_fixup_worker()
2802 btrfs_lock_extent(&inode->io_tree, page_start, page_end, &cached_state); in btrfs_writepage_fixup_worker()
2810 btrfs_unlock_extent(&inode->io_tree, page_start, page_end, in btrfs_writepage_fixup_worker()
2825 * delayed allocation bits set and space reserved for our COW in btrfs_writepage_fixup_worker()
2837 btrfs_unlock_extent(&inode->io_tree, page_start, page_end, &cached_state); in btrfs_writepage_fixup_worker()
2844 mapping_set_error(folio->mapping, ret); in btrfs_writepage_fixup_worker()
2855 * As a precaution, do a delayed iput in case it would be the last iput in btrfs_writepage_fixup_worker()
2866 * properly happens and the data=ordered rules are followed.
2875 struct inode *inode = folio->mapping->host; in btrfs_writepage_cow_fixup()
2884 * For experimental build, we error out instead of EAGAIN. in btrfs_writepage_cow_fixup()
2886 * We should not hit such out-of-band dirty folios anymore. in btrfs_writepage_cow_fixup()
2892 btrfs_root_id(BTRFS_I(inode)->root), in btrfs_writepage_cow_fixup()
2895 return -EUCLEAN; in btrfs_writepage_cow_fixup()
2907 return -EAGAIN; in btrfs_writepage_cow_fixup()
2911 return -EAGAIN; in btrfs_writepage_cow_fixup()
2917 * folio->mapping outside of the folio lock. in btrfs_writepage_cow_fixup()
2922 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL); in btrfs_writepage_cow_fixup()
2923 fixup->folio = folio; in btrfs_writepage_cow_fixup()
2924 fixup->inode = BTRFS_I(inode); in btrfs_writepage_cow_fixup()
2925 btrfs_queue_work(fs_info->fixup_workers, &fixup->work); in btrfs_writepage_cow_fixup()
2927 return -EAGAIN; in btrfs_writepage_cow_fixup()
2936 struct btrfs_root *root = inode->root; in insert_reserved_file_extent()
2937 const u64 sectorsize = root->fs_info->sectorsize; in insert_reserved_file_extent()
2951 return -ENOMEM; in insert_reserved_file_extent()
2981 leaf = path->nodes[0]; in insert_reserved_file_extent()
2982 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid); in insert_reserved_file_extent()
2984 btrfs_item_ptr_offset(leaf, path->slots[0]), in insert_reserved_file_extent()
2992 * number of bytes only for that range containing the inline extent. in insert_reserved_file_extent()
2999 inline_size = drop_args.bytes_found - inline_size; in insert_reserved_file_extent()
3001 drop_args.bytes_found -= inline_size; in insert_reserved_file_extent()
3002 num_bytes -= sectorsize; in insert_reserved_file_extent()
3017 file_pos - offset, in insert_reserved_file_extent()
3031 spin_lock(&cache->lock); in btrfs_release_delalloc_bytes()
3032 cache->delalloc_bytes -= len; in btrfs_release_delalloc_bytes()
3033 spin_unlock(&cache->lock); in btrfs_release_delalloc_bytes()
3043 u64 num_bytes = oe->num_bytes; in insert_ordered_extent_file_extent()
3044 u64 ram_bytes = oe->ram_bytes; in insert_ordered_extent_file_extent()
3048 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr); in insert_ordered_extent_file_extent()
3050 oe->disk_num_bytes); in insert_ordered_extent_file_extent()
3051 btrfs_set_stack_file_extent_offset(&stack_fi, oe->offset); in insert_ordered_extent_file_extent()
3052 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags)) in insert_ordered_extent_file_extent()
3053 num_bytes = oe->truncated_len; in insert_ordered_extent_file_extent()
3056 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type); in insert_ordered_extent_file_extent()
3065 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) || in insert_ordered_extent_file_extent()
3066 test_bit(BTRFS_ORDERED_ENCODED, &oe->flags) || in insert_ordered_extent_file_extent()
3067 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags); in insert_ordered_extent_file_extent()
3069 return insert_reserved_file_extent(trans, oe->inode, in insert_ordered_extent_file_extent()
3070 oe->file_offset, &stack_fi, in insert_ordered_extent_file_extent()
3071 update_inode_bytes, oe->qgroup_rsv); in insert_ordered_extent_file_extent()
3081 struct btrfs_inode *inode = ordered_extent->inode; in btrfs_finish_one_ordered()
3082 struct btrfs_root *root = inode->root; in btrfs_finish_one_ordered()
3083 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_finish_one_ordered()
3085 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_finish_one_ordered()
3090 u64 logical_len = ordered_extent->num_bytes; in btrfs_finish_one_ordered()
3096 start = ordered_extent->file_offset; in btrfs_finish_one_ordered()
3097 end = start + ordered_extent->num_bytes - 1; in btrfs_finish_one_ordered()
3099 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && in btrfs_finish_one_ordered()
3100 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && in btrfs_finish_one_ordered()
3101 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags) && in btrfs_finish_one_ordered()
3102 !test_bit(BTRFS_ORDERED_ENCODED, &ordered_extent->flags)) in btrfs_finish_one_ordered()
3109 if (unlikely(test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags))) { in btrfs_finish_one_ordered()
3110 ret = -EIO; in btrfs_finish_one_ordered()
3114 ret = btrfs_zone_finish_endio(fs_info, ordered_extent->disk_bytenr, in btrfs_finish_one_ordered()
3115 ordered_extent->disk_num_bytes); in btrfs_finish_one_ordered()
3119 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { in btrfs_finish_one_ordered()
3121 logical_len = ordered_extent->truncated_len; in btrfs_finish_one_ordered()
3135 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { in btrfs_finish_one_ordered()
3152 trans->block_rsv = &inode->block_rsv; in btrfs_finish_one_ordered()
3160 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { in btrfs_finish_one_ordered()
3162 ASSERT(list_empty(&ordered_extent->list)); in btrfs_finish_one_ordered()
3163 if (unlikely(!list_empty(&ordered_extent->list))) { in btrfs_finish_one_ordered()
3164 ret = -EINVAL; in btrfs_finish_one_ordered()
3172 /* -ENOMEM or corruption */ in btrfs_finish_one_ordered()
3178 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) in btrfs_finish_one_ordered()
3179 compress_type = ordered_extent->compress_type; in btrfs_finish_one_ordered()
3180 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { in btrfs_finish_one_ordered()
3183 ordered_extent->file_offset, in btrfs_finish_one_ordered()
3184 ordered_extent->file_offset + in btrfs_finish_one_ordered()
3186 btrfs_zoned_release_data_reloc_bg(fs_info, ordered_extent->disk_bytenr, in btrfs_finish_one_ordered()
3187 ordered_extent->disk_num_bytes); in btrfs_finish_one_ordered()
3189 BUG_ON(root == fs_info->tree_root); in btrfs_finish_one_ordered()
3194 ordered_extent->disk_bytenr, in btrfs_finish_one_ordered()
3195 ordered_extent->disk_num_bytes); in btrfs_finish_one_ordered()
3203 ret = btrfs_unpin_extent_cache(inode, ordered_extent->file_offset, in btrfs_finish_one_ordered()
3204 ordered_extent->num_bytes, trans->transid); in btrfs_finish_one_ordered()
3210 ret = add_pending_csums(trans, &ordered_extent->list); in btrfs_finish_one_ordered()
3222 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) in btrfs_finish_one_ordered()
3223 btrfs_clear_extent_bit(&inode->io_tree, start, end, in btrfs_finish_one_ordered()
3229 if (unlikely(ret)) { /* -ENOMEM or corruption */ in btrfs_finish_one_ordered()
3234 btrfs_clear_extent_bit(&inode->io_tree, start, end, clear_bits, in btrfs_finish_one_ordered()
3259 * writepage where we do ASSERT(em->block_start != in btrfs_finish_one_ordered()
3279 * back to the allocator. We only free the extent in the in btrfs_finish_one_ordered()
3288 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && in btrfs_finish_one_ordered()
3289 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { in btrfs_finish_one_ordered()
3296 ordered_extent->disk_bytenr, in btrfs_finish_one_ordered()
3297 ordered_extent->disk_num_bytes, in btrfs_finish_one_ordered()
3300 ordered_extent->disk_bytenr, in btrfs_finish_one_ordered()
3301 ordered_extent->disk_num_bytes, true); in btrfs_finish_one_ordered()
3306 btrfs_qgroup_free_refroot(fs_info, btrfs_root_id(inode->root), in btrfs_finish_one_ordered()
3307 ordered_extent->qgroup_rsv, in btrfs_finish_one_ordered()
3328 if (btrfs_is_zoned(ordered->inode->root->fs_info) && in btrfs_finish_ordered_io()
3329 !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) && in btrfs_finish_ordered_io()
3330 list_empty(&ordered->bioc_list)) in btrfs_finish_ordered_io()
3339 const u32 blocksize = fs_info->sectorsize; in btrfs_calculate_block_csum()
3340 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); in btrfs_calculate_block_csum()
3342 shash->tfm = fs_info->csum_shash; in btrfs_calculate_block_csum()
3352 size_t len = min(paddr + blocksize - cur, in btrfs_calculate_block_csum()
3353 PAGE_SIZE - offset_in_page(cur)); in btrfs_calculate_block_csum()
3375 if (unlikely(memcmp(csum, csum_expected, fs_info->csum_size) != 0)) in btrfs_check_block_csum()
3376 return -EIO; in btrfs_check_block_csum()
3396 struct btrfs_inode *inode = bbio->inode; in btrfs_data_csum_ok()
3397 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_data_csum_ok()
3398 const u32 blocksize = fs_info->sectorsize; in btrfs_data_csum_ok()
3400 u64 file_offset = bbio->file_offset + bio_offset; in btrfs_data_csum_ok()
3401 u64 end = file_offset + blocksize - 1; in btrfs_data_csum_ok()
3405 if (!bbio->csum) in btrfs_data_csum_ok()
3408 if (btrfs_is_data_reloc_root(inode->root) && in btrfs_data_csum_ok()
3409 btrfs_test_range_bit(&inode->io_tree, file_offset, end, EXTENT_NODATASUM, in btrfs_data_csum_ok()
3412 btrfs_clear_extent_bit(&inode->io_tree, file_offset, end, in btrfs_data_csum_ok()
3417 csum_expected = bbio->csum + (bio_offset >> fs_info->sectorsize_bits) * in btrfs_data_csum_ok()
3418 fs_info->csum_size; in btrfs_data_csum_ok()
3425 bbio->mirror_num); in btrfs_data_csum_ok()
3435 * Perform a delayed iput on @inode.
3441 * the inode to the delayed iput machinery. Delayed iputs are processed at
3446 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_add_delayed_iput()
3449 if (atomic_add_unless(&inode->vfs_inode.i_count, -1, 1)) in btrfs_add_delayed_iput()
3452 WARN_ON_ONCE(test_bit(BTRFS_FS_STATE_NO_DELAYED_IPUT, &fs_info->fs_state)); in btrfs_add_delayed_iput()
3453 atomic_inc(&fs_info->nr_delayed_iputs); in btrfs_add_delayed_iput()
3456 * context (see bio.c and btrfs_put_ordered_extent()) or a non-irq in btrfs_add_delayed_iput()
3459 spin_lock_irqsave(&fs_info->delayed_iput_lock, flags); in btrfs_add_delayed_iput()
3460 ASSERT(list_empty(&inode->delayed_iput)); in btrfs_add_delayed_iput()
3461 list_add_tail(&inode->delayed_iput, &fs_info->delayed_iputs); in btrfs_add_delayed_iput()
3462 spin_unlock_irqrestore(&fs_info->delayed_iput_lock, flags); in btrfs_add_delayed_iput()
3463 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags)) in btrfs_add_delayed_iput()
3464 wake_up_process(fs_info->cleaner_kthread); in btrfs_add_delayed_iput()
3470 list_del_init(&inode->delayed_iput); in run_delayed_iput_locked()
3471 spin_unlock_irq(&fs_info->delayed_iput_lock); in run_delayed_iput_locked()
3472 iput(&inode->vfs_inode); in run_delayed_iput_locked()
3473 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs)) in run_delayed_iput_locked()
3474 wake_up(&fs_info->delayed_iputs_wait); in run_delayed_iput_locked()
3475 spin_lock_irq(&fs_info->delayed_iput_lock); in run_delayed_iput_locked()
3481 if (!list_empty(&inode->delayed_iput)) { in btrfs_run_delayed_iput()
3482 spin_lock_irq(&fs_info->delayed_iput_lock); in btrfs_run_delayed_iput()
3483 if (!list_empty(&inode->delayed_iput)) in btrfs_run_delayed_iput()
3485 spin_unlock_irq(&fs_info->delayed_iput_lock); in btrfs_run_delayed_iput()
3494 * fs_info->delayed_iput_lock. So we need to disable irqs here to in btrfs_run_delayed_iputs()
3497 spin_lock_irq(&fs_info->delayed_iput_lock); in btrfs_run_delayed_iputs()
3498 while (!list_empty(&fs_info->delayed_iputs)) { in btrfs_run_delayed_iputs()
3501 inode = list_first_entry(&fs_info->delayed_iputs, in btrfs_run_delayed_iputs()
3505 spin_unlock_irq(&fs_info->delayed_iput_lock); in btrfs_run_delayed_iputs()
3507 spin_lock_irq(&fs_info->delayed_iput_lock); in btrfs_run_delayed_iputs()
3510 spin_unlock_irq(&fs_info->delayed_iput_lock); in btrfs_run_delayed_iputs()
3514 * Wait for flushing all delayed iputs
3518 * This will wait on any delayed iputs that are currently running with KILLABLE
3527 int ret = wait_event_killable(fs_info->delayed_iputs_wait, in btrfs_wait_on_delayed_iputs()
3528 atomic_read(&fs_info->nr_delayed_iputs) == 0); in btrfs_wait_on_delayed_iputs()
3530 return -EINTR; in btrfs_wait_on_delayed_iputs()
3543 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode)); in btrfs_orphan_add()
3544 if (unlikely(ret && ret != -EEXIST)) { in btrfs_orphan_add()
3559 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode)); in btrfs_orphan_del()
3568 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_orphan_cleanup()
3576 if (test_and_set_bit(BTRFS_ROOT_ORPHAN_CLEANUP, &root->state)) in btrfs_orphan_cleanup()
3581 ret = -ENOMEM; in btrfs_orphan_cleanup()
3584 path->reada = READA_BACK; in btrfs_orphan_cleanup()
3588 key.offset = (u64)-1; in btrfs_orphan_cleanup()
3599 * is weird, but possible, so only screw with path if we didn't in btrfs_orphan_cleanup()
3604 if (path->slots[0] == 0) in btrfs_orphan_cleanup()
3606 path->slots[0]--; in btrfs_orphan_cleanup()
3610 leaf = path->nodes[0]; in btrfs_orphan_cleanup()
3611 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in btrfs_orphan_cleanup()
3633 * due to -ENOSPC for example, so try to grab the error in btrfs_orphan_cleanup()
3638 ret = BTRFS_FS_ERROR(fs_info) ?: -EINVAL; in btrfs_orphan_cleanup()
3651 if (ret != -ENOENT) in btrfs_orphan_cleanup()
3655 if (!inode && root == fs_info->tree_root) { in btrfs_orphan_cleanup()
3670 * fs_info->fs_roots_radix. So here we can find if an in btrfs_orphan_cleanup()
3675 spin_lock(&fs_info->fs_roots_radix_lock); in btrfs_orphan_cleanup()
3676 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix, in btrfs_orphan_cleanup()
3678 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0) in btrfs_orphan_cleanup()
3680 spin_unlock(&fs_info->fs_roots_radix_lock); in btrfs_orphan_cleanup()
3684 key.offset = found_key.objectid - 1; in btrfs_orphan_cleanup()
3707 * only if this filesystem was last used on a pre-v3.12 kernel in btrfs_orphan_cleanup()
3716 if (!inode || inode->vfs_inode.i_nlink) { in btrfs_orphan_cleanup()
3719 iput(&inode->vfs_inode); in btrfs_orphan_cleanup()
3742 iput(&inode->vfs_inode); in btrfs_orphan_cleanup()
3747 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { in btrfs_orphan_cleanup()
3790 *first_xattr_slot = -1; in acls_after_inode_item()
3800 if (*first_xattr_slot == -1) in acls_after_inode_item()
3819 * - inode in acls_after_inode_item()
3820 * - inode backrefs in acls_after_inode_item()
3821 * - xattrs in acls_after_inode_item()
3822 * - extents, in acls_after_inode_item()
3835 if (*first_xattr_slot == -1) in acls_after_inode_item()
3842 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_init_file_extent_tree()
3844 if (WARN_ON_ONCE(inode->file_extent_tree)) in btrfs_init_file_extent_tree()
3848 if (!S_ISREG(inode->vfs_inode.i_mode)) in btrfs_init_file_extent_tree()
3853 inode->file_extent_tree = kmalloc(sizeof(struct extent_io_tree), GFP_KERNEL); in btrfs_init_file_extent_tree()
3854 if (!inode->file_extent_tree) in btrfs_init_file_extent_tree()
3855 return -ENOMEM; in btrfs_init_file_extent_tree()
3857 btrfs_extent_io_tree_init(fs_info, inode->file_extent_tree, in btrfs_init_file_extent_tree()
3859 /* Lockdep class is set only for the file extent tree. */ in btrfs_init_file_extent_tree()
3860 lockdep_set_class(&inode->file_extent_tree->lock, &file_extent_tree_class); in btrfs_init_file_extent_tree()
3867 struct btrfs_root *root = inode->root; in btrfs_add_inode_to_root()
3872 if (inode_unhashed(&inode->vfs_inode)) in btrfs_add_inode_to_root()
3876 ret = xa_reserve(&root->inodes, ino, GFP_NOFS); in btrfs_add_inode_to_root()
3881 existing = xa_store(&root->inodes, ino, inode, GFP_ATOMIC); in btrfs_add_inode_to_root()
3885 ASSERT(ret != -EINVAL); in btrfs_add_inode_to_root()
3886 ASSERT(ret != -ENOMEM); in btrfs_add_inode_to_root()
3889 WARN_ON(!(existing->vfs_inode.i_state & (I_WILL_FREE | I_FREEING))); in btrfs_add_inode_to_root()
3896 * Read a locked inode from the btree into the in-memory inode and add it to
3903 struct btrfs_root *root = inode->root; in btrfs_read_locked_inode()
3904 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_read_locked_inode()
3907 struct inode *vfs_inode = &inode->vfs_inode; in btrfs_read_locked_inode()
3931 ret = -ENOENT; in btrfs_read_locked_inode()
3935 leaf = path->nodes[0]; in btrfs_read_locked_inode()
3940 inode_item = btrfs_item_ptr(leaf, path->slots[0], in btrfs_read_locked_inode()
3942 vfs_inode->i_mode = btrfs_inode_mode(leaf, inode_item); in btrfs_read_locked_inode()
3948 inode_set_atime(vfs_inode, btrfs_timespec_sec(leaf, &inode_item->atime), in btrfs_read_locked_inode()
3949 btrfs_timespec_nsec(leaf, &inode_item->atime)); in btrfs_read_locked_inode()
3951 inode_set_mtime(vfs_inode, btrfs_timespec_sec(leaf, &inode_item->mtime), in btrfs_read_locked_inode()
3952 btrfs_timespec_nsec(leaf, &inode_item->mtime)); in btrfs_read_locked_inode()
3954 inode_set_ctime(vfs_inode, btrfs_timespec_sec(leaf, &inode_item->ctime), in btrfs_read_locked_inode()
3955 btrfs_timespec_nsec(leaf, &inode_item->ctime)); in btrfs_read_locked_inode()
3957 inode->i_otime_sec = btrfs_timespec_sec(leaf, &inode_item->otime); in btrfs_read_locked_inode()
3958 inode->i_otime_nsec = btrfs_timespec_nsec(leaf, &inode_item->otime); in btrfs_read_locked_inode()
3961 inode->generation = btrfs_inode_generation(leaf, inode_item); in btrfs_read_locked_inode()
3962 inode->last_trans = btrfs_inode_transid(leaf, inode_item); in btrfs_read_locked_inode()
3965 vfs_inode->i_generation = inode->generation; in btrfs_read_locked_inode()
3966 vfs_inode->i_rdev = 0; in btrfs_read_locked_inode()
3969 if (S_ISDIR(vfs_inode->i_mode)) in btrfs_read_locked_inode()
3970 inode->index_cnt = (u64)-1; in btrfs_read_locked_inode()
3973 &inode->flags, &inode->ro_flags); in btrfs_read_locked_inode()
3982 round_up(i_size_read(vfs_inode), fs_info->sectorsize)); in btrfs_read_locked_inode()
3985 * and then re-read we need to do a full sync since we don't have any in btrfs_read_locked_inode()
3989 * This is required for both inode re-read from disk and delayed inode in btrfs_read_locked_inode()
3992 if (inode->last_trans == btrfs_get_fs_generation(fs_info)) in btrfs_read_locked_inode()
3993 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); in btrfs_read_locked_inode()
4008 * xfs_io -c fsync mydir/foo in btrfs_read_locked_inode()
4022 inode->last_unlink_trans = inode->last_trans; in btrfs_read_locked_inode()
4030 inode->last_reflink_trans = inode->last_trans; in btrfs_read_locked_inode()
4032 path->slots[0]++; in btrfs_read_locked_inode()
4033 if (vfs_inode->i_nlink != 1 || in btrfs_read_locked_inode()
4034 path->slots[0] >= btrfs_header_nritems(leaf)) in btrfs_read_locked_inode()
4037 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); in btrfs_read_locked_inode()
4041 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); in btrfs_read_locked_inode()
4046 inode->dir_index = btrfs_inode_ref_index(leaf, ref); in btrfs_read_locked_inode()
4051 inode->dir_index = btrfs_inode_extref_index(leaf, extref); in btrfs_read_locked_inode()
4058 maybe_acls = acls_after_inode_item(leaf, path->slots[0], in btrfs_read_locked_inode()
4060 if (first_xattr_slot != -1) { in btrfs_read_locked_inode()
4061 path->slots[0] = first_xattr_slot; in btrfs_read_locked_inode()
4072 switch (vfs_inode->i_mode & S_IFMT) { in btrfs_read_locked_inode()
4074 vfs_inode->i_mapping->a_ops = &btrfs_aops; in btrfs_read_locked_inode()
4075 vfs_inode->i_fop = &btrfs_file_operations; in btrfs_read_locked_inode()
4076 vfs_inode->i_op = &btrfs_file_inode_operations; in btrfs_read_locked_inode()
4079 vfs_inode->i_fop = &btrfs_dir_file_operations; in btrfs_read_locked_inode()
4080 vfs_inode->i_op = &btrfs_dir_inode_operations; in btrfs_read_locked_inode()
4083 vfs_inode->i_op = &btrfs_symlink_inode_operations; in btrfs_read_locked_inode()
4085 vfs_inode->i_mapping->a_ops = &btrfs_aops; in btrfs_read_locked_inode()
4088 vfs_inode->i_op = &btrfs_special_inode_operations; in btrfs_read_locked_inode()
4089 init_special_inode(vfs_inode, vfs_inode->i_mode, rdev); in btrfs_read_locked_inode()
4117 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); in fill_inode_item()
4118 btrfs_set_inode_mode(leaf, item, inode->i_mode); in fill_inode_item()
4119 btrfs_set_inode_nlink(leaf, item, inode->i_nlink); in fill_inode_item()
4121 btrfs_set_timespec_sec(leaf, &item->atime, inode_get_atime_sec(inode)); in fill_inode_item()
4122 btrfs_set_timespec_nsec(leaf, &item->atime, inode_get_atime_nsec(inode)); in fill_inode_item()
4124 btrfs_set_timespec_sec(leaf, &item->mtime, inode_get_mtime_sec(inode)); in fill_inode_item()
4125 btrfs_set_timespec_nsec(leaf, &item->mtime, inode_get_mtime_nsec(inode)); in fill_inode_item()
4127 btrfs_set_timespec_sec(leaf, &item->ctime, inode_get_ctime_sec(inode)); in fill_inode_item()
4128 btrfs_set_timespec_nsec(leaf, &item->ctime, inode_get_ctime_nsec(inode)); in fill_inode_item()
4130 btrfs_set_timespec_sec(leaf, &item->otime, BTRFS_I(inode)->i_otime_sec); in fill_inode_item()
4131 btrfs_set_timespec_nsec(leaf, &item->otime, BTRFS_I(inode)->i_otime_nsec); in fill_inode_item()
4134 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); in fill_inode_item()
4136 btrfs_set_inode_transid(leaf, item, trans->transid); in fill_inode_item()
4137 btrfs_set_inode_rdev(leaf, item, inode->i_rdev); in fill_inode_item()
4138 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags, in fill_inode_item()
4139 BTRFS_I(inode)->ro_flags); in fill_inode_item()
4145 * copy everything in the in-memory inode into the btree.
4158 return -ENOMEM; in btrfs_update_inode_item()
4161 ret = btrfs_lookup_inode(trans, inode->root, path, &key, 1); in btrfs_update_inode_item()
4164 ret = -ENOENT; in btrfs_update_inode_item()
4168 leaf = path->nodes[0]; in btrfs_update_inode_item()
4169 inode_item = btrfs_item_ptr(leaf, path->slots[0], in btrfs_update_inode_item()
4172 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode); in btrfs_update_inode_item()
4178 * copy everything in the in-memory inode into the btree.
4183 struct btrfs_root *root = inode->root; in btrfs_update_inode()
4184 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_update_inode()
4189 * if we put it into the delayed code. in btrfs_update_inode()
4196 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) { in btrfs_update_inode()
4214 if (ret == -ENOSPC) in btrfs_update_inode_fallback()
4229 if (test_bit(BTRFS_FS_LOG_RECOVERING, &dir->root->fs_info->flags)) in update_time_after_link_or_unlink()
4232 now = inode_set_ctime_current(&dir->vfs_inode); in update_time_after_link_or_unlink()
4233 inode_set_mtime_to_ts(&dir->vfs_inode, now); in update_time_after_link_or_unlink()
4247 struct btrfs_root *root = dir->root; in __btrfs_unlink_inode()
4248 struct btrfs_fs_info *fs_info = root->fs_info; in __btrfs_unlink_inode()
4258 return -ENOMEM; in __btrfs_unlink_inode()
4260 di = btrfs_lookup_dir_item(trans, root, path, dir_ino, name, -1); in __btrfs_unlink_inode()
4263 return di ? PTR_ERR(di) : -ENOENT; in __btrfs_unlink_inode()
4277 * it is unnecessary to do delayed deletion. in __btrfs_unlink_inode()
4284 if (inode->dir_index) { in __btrfs_unlink_inode()
4287 index = inode->dir_index; in __btrfs_unlink_inode()
4296 name->len, name->name, btrfs_root_id(root), ino, dir_ino); in __btrfs_unlink_inode()
4302 rename_ctx->index = index; in __btrfs_unlink_inode()
4313 * Besides that, doing it here would only cause extra unnecessary btree in __btrfs_unlink_inode()
4322 * If we have a pending delayed iput we could end up with the final iput in __btrfs_unlink_inode()
4323 * being run in btrfs-cleaner context. If we have enough of these built in __btrfs_unlink_inode()
4324 * up we can end up burning a lot of time in btrfs-cleaner without any in __btrfs_unlink_inode()
4326 * the inode we can run the delayed iput here without any issues as the in __btrfs_unlink_inode()
4332 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name->len * 2); in __btrfs_unlink_inode()
4333 inode_inc_iversion(&inode->vfs_inode); in __btrfs_unlink_inode()
4334 inode_set_ctime_current(&inode->vfs_inode); in __btrfs_unlink_inode()
4335 inode_inc_iversion(&dir->vfs_inode); in __btrfs_unlink_inode()
4349 drop_nlink(&inode->vfs_inode); in btrfs_unlink_inode()
4365 struct btrfs_root *root = dir->root; in __unlink_start_trans()
4378 ret = fscrypt_setup_filename(dir, &dentry->d_name, 1, &fname); in btrfs_unlink()
4382 /* This needs to handle no-key deletions later on */ in btrfs_unlink()
4398 if (inode->i_nlink == 0) { in btrfs_unlink()
4406 btrfs_btree_balance_dirty(BTRFS_I(dir)->root->fs_info); in btrfs_unlink()
4415 struct btrfs_root *root = dir->root; in btrfs_unlink_subvol()
4427 ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname); in btrfs_unlink_subvol()
4431 /* This needs to handle no-key deletions later on */ in btrfs_unlink_subvol()
4434 objectid = btrfs_root_id(inode->root); in btrfs_unlink_subvol()
4436 objectid = inode->ref_root_id; in btrfs_unlink_subvol()
4440 return -EINVAL; in btrfs_unlink_subvol()
4445 ret = -ENOMEM; in btrfs_unlink_subvol()
4450 &fname.disk_name, -1); in btrfs_unlink_subvol()
4452 ret = di ? PTR_ERR(di) : -ENOENT; in btrfs_unlink_subvol()
4456 leaf = path->nodes[0]; in btrfs_unlink_subvol()
4470 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect. in btrfs_unlink_subvol()
4483 leaf = path->nodes[0]; in btrfs_unlink_subvol()
4484 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in btrfs_unlink_subvol()
4503 btrfs_i_size_write(dir, dir->vfs_inode.i_size - fname.disk_name.len * 2); in btrfs_unlink_subvol()
4504 inode_inc_iversion(&dir->vfs_inode); in btrfs_unlink_subvol()
4505 inode_set_mtime_to_ts(&dir->vfs_inode, inode_set_ctime_current(&dir->vfs_inode)); in btrfs_unlink_subvol()
4521 struct btrfs_fs_info *fs_info = root->fs_info; in may_destroy_subvol()
4531 return -ENOMEM; in may_destroy_subvol()
4534 dir_id = btrfs_super_root_dir(fs_info->super_copy); in may_destroy_subvol()
4535 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path, in may_destroy_subvol()
4538 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); in may_destroy_subvol()
4540 ret = -EPERM; in may_destroy_subvol()
4551 key.offset = (u64)-1; in may_destroy_subvol()
4553 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); in may_destroy_subvol()
4558 * Key with offset -1 found, there would have to exist a root in may_destroy_subvol()
4561 return -EUCLEAN; in may_destroy_subvol()
4565 if (path->slots[0] > 0) { in may_destroy_subvol()
4566 path->slots[0]--; in may_destroy_subvol()
4567 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); in may_destroy_subvol()
4569 ret = -ENOTEMPTY; in may_destroy_subvol()
4578 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_prune_dentries()
4583 WARN_ON(btrfs_root_refs(&root->root_item) != 0); in btrfs_prune_dentries()
4587 if (icount_read(&inode->vfs_inode) > 1) in btrfs_prune_dentries()
4588 d_prune_aliases(&inode->vfs_inode); in btrfs_prune_dentries()
4595 iput(&inode->vfs_inode); in btrfs_prune_dentries()
4603 struct btrfs_root *root = dir->root; in btrfs_delete_subvolume()
4604 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_delete_subvolume()
4606 struct btrfs_root *dest = BTRFS_I(inode)->root; in btrfs_delete_subvolume()
4613 down_write(&fs_info->subvol_sem); in btrfs_delete_subvolume()
4620 spin_lock(&dest->root_item_lock); in btrfs_delete_subvolume()
4621 if (dest->send_in_progress) { in btrfs_delete_subvolume()
4622 spin_unlock(&dest->root_item_lock); in btrfs_delete_subvolume()
4626 ret = -EPERM; in btrfs_delete_subvolume()
4629 if (atomic_read(&dest->nr_swapfiles)) { in btrfs_delete_subvolume()
4630 spin_unlock(&dest->root_item_lock); in btrfs_delete_subvolume()
4634 ret = -EPERM; in btrfs_delete_subvolume()
4637 root_flags = btrfs_root_flags(&dest->root_item); in btrfs_delete_subvolume()
4638 btrfs_set_root_flags(&dest->root_item, in btrfs_delete_subvolume()
4640 spin_unlock(&dest->root_item_lock); in btrfs_delete_subvolume()
4664 trans->block_rsv = &block_rsv; in btrfs_delete_subvolume()
4665 trans->bytes_reserved = block_rsv.size; in btrfs_delete_subvolume()
4681 memset(&dest->root_item.drop_progress, 0, in btrfs_delete_subvolume()
4682 sizeof(dest->root_item.drop_progress)); in btrfs_delete_subvolume()
4683 btrfs_set_root_drop_level(&dest->root_item, 0); in btrfs_delete_subvolume()
4684 btrfs_set_root_refs(&dest->root_item, 0); in btrfs_delete_subvolume()
4686 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { in btrfs_delete_subvolume()
4688 fs_info->tree_root, in btrfs_delete_subvolume()
4696 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid, in btrfs_delete_subvolume()
4698 if (unlikely(ret && ret != -ENOENT)) { in btrfs_delete_subvolume()
4702 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { in btrfs_delete_subvolume()
4704 dest->root_item.received_uuid, in btrfs_delete_subvolume()
4707 if (unlikely(ret && ret != -ENOENT)) { in btrfs_delete_subvolume()
4713 free_anon_bdev(dest->anon_dev); in btrfs_delete_subvolume()
4714 dest->anon_dev = 0; in btrfs_delete_subvolume()
4716 trans->block_rsv = NULL; in btrfs_delete_subvolume()
4717 trans->bytes_reserved = 0; in btrfs_delete_subvolume()
4719 inode->i_flags |= S_DEAD; in btrfs_delete_subvolume()
4721 btrfs_block_rsv_release(fs_info, &block_rsv, (u64)-1, NULL); in btrfs_delete_subvolume()
4726 spin_lock(&dest->root_item_lock); in btrfs_delete_subvolume()
4727 root_flags = btrfs_root_flags(&dest->root_item); in btrfs_delete_subvolume()
4728 btrfs_set_root_flags(&dest->root_item, in btrfs_delete_subvolume()
4730 spin_unlock(&dest->root_item_lock); in btrfs_delete_subvolume()
4733 up_write(&fs_info->subvol_sem); in btrfs_delete_subvolume()
4737 ASSERT(dest->send_in_progress == 0); in btrfs_delete_subvolume()
4747 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_rmdir()
4752 if (inode->vfs_inode.i_size > BTRFS_EMPTY_DIR_SIZE) in btrfs_rmdir()
4753 return -ENOTEMPTY; in btrfs_rmdir()
4758 return -EOPNOTSUPP; in btrfs_rmdir()
4763 ret = fscrypt_setup_filename(vfs_dir, &dentry->d_name, 1, &fname); in btrfs_rmdir()
4767 /* This needs to handle no-key deletions later on */ in btrfs_rmdir()
4789 if (inode->last_unlink_trans >= trans->transid) in btrfs_rmdir()
4819 if (blockstart <= bytenr && bytenr <= blockstart + blocksize - 1) in is_inside_block()
4827 struct address_space *mapping = inode->vfs_inode.i_mapping; in truncate_block_zero_beyond_eof()
4842 if (folio->mapping != mapping) { in truncate_block_zero_beyond_eof()
4848 ret = -EIO; in truncate_block_zero_beyond_eof()
4861 folio_zero_range(folio, zero_start - folio_pos(folio), in truncate_block_zero_beyond_eof()
4862 zero_end - zero_start); in truncate_block_zero_beyond_eof()
4873 * @inode - inode that we're zeroing
4874 * @offset - the file offset of the block to truncate
4877 * @start - the start file offset of the range we want to zero
4878 * @end - the end (inclusive) file offset of the range we want to zero.
4889 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_truncate_block()
4890 struct address_space *mapping = inode->vfs_inode.i_mapping; in btrfs_truncate_block()
4891 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_truncate_block()
4896 u32 blocksize = fs_info->sectorsize; in btrfs_truncate_block()
4922 if (end == (u64)-1 && blocksize < PAGE_SIZE) in btrfs_truncate_block()
4946 block_end = block_start + blocksize - 1; in btrfs_truncate_block()
4986 if (folio->mapping != mapping) { in btrfs_truncate_block()
4992 ret = -EIO; in btrfs_truncate_block()
4998 * We unlock the page after the io is completed and then re-lock it in btrfs_truncate_block()
5021 btrfs_clear_extent_bit(&inode->io_tree, block_start, block_end, in btrfs_truncate_block()
5032 if (end == (u64)-1) { in btrfs_truncate_block()
5043 zero_end = min_t(u64, folio_end(folio) - 1, end); in btrfs_truncate_block()
5048 folio_zero_range(folio, zero_start - folio_pos(folio), in btrfs_truncate_block()
5049 zero_end - zero_start + 1); in btrfs_truncate_block()
5052 block_end + 1 - block_start); in btrfs_truncate_block()
5054 block_end + 1 - block_start); in btrfs_truncate_block()
5057 btrfs_set_extent_bit(&inode->io_tree, block_start, block_end, in btrfs_truncate_block()
5082 struct btrfs_root *root = inode->root; in maybe_insert_hole()
5083 struct btrfs_fs_info *fs_info = root->fs_info; in maybe_insert_hole()
5098 * 1 - for the one we're dropping in maybe_insert_hole()
5099 * 1 - for the one we're adding in maybe_insert_hole()
5100 * 1 - for updating the inode. in maybe_insert_hole()
5136 struct btrfs_root *root = inode->root; in btrfs_cont_expand()
5137 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_cont_expand()
5138 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_cont_expand()
5141 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize); in btrfs_cont_expand()
5142 u64 block_end = ALIGN(size, fs_info->sectorsize); in btrfs_cont_expand()
5153 ret = btrfs_truncate_block(inode, oldsize, oldsize, -1); in btrfs_cont_expand()
5160 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1, in btrfs_cont_expand()
5164 em = btrfs_get_extent(inode, NULL, cur_offset, block_end - cur_offset); in btrfs_cont_expand()
5171 last_byte = ALIGN(last_byte, fs_info->sectorsize); in btrfs_cont_expand()
5172 hole_size = last_byte - cur_offset; in btrfs_cont_expand()
5174 if (!(em->flags & EXTENT_FLAG_PREALLOC)) { in btrfs_cont_expand()
5189 cur_offset + hole_size - 1, in btrfs_cont_expand()
5194 hole_em->start = cur_offset; in btrfs_cont_expand()
5195 hole_em->len = hole_size; in btrfs_cont_expand()
5197 hole_em->disk_bytenr = EXTENT_MAP_HOLE; in btrfs_cont_expand()
5198 hole_em->disk_num_bytes = 0; in btrfs_cont_expand()
5199 hole_em->ram_bytes = hole_size; in btrfs_cont_expand()
5200 hole_em->generation = btrfs_get_fs_generation(fs_info); in btrfs_cont_expand()
5218 btrfs_unlock_extent(io_tree, hole_start, block_end - 1, &cached_state); in btrfs_cont_expand()
5224 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_setsize()
5227 loff_t newsize = attr->ia_size; in btrfs_setsize()
5228 int mask = attr->ia_valid; in btrfs_setsize()
5249 * state of this file - if the snapshot captures this expanding in btrfs_setsize()
5253 btrfs_drew_write_lock(&root->snapshot_lock); in btrfs_setsize()
5256 btrfs_drew_write_unlock(&root->snapshot_lock); in btrfs_setsize()
5262 btrfs_drew_write_unlock(&root->snapshot_lock); in btrfs_setsize()
5270 btrfs_drew_write_unlock(&root->snapshot_lock); in btrfs_setsize()
5277 ALIGN(newsize, fs_info->sectorsize), in btrfs_setsize()
5278 (u64)-1); in btrfs_setsize()
5290 &BTRFS_I(inode)->runtime_flags); in btrfs_setsize()
5297 if (ret && inode->i_nlink) { in btrfs_setsize()
5301 * Truncate failed, so fix up the in-memory size. We in btrfs_setsize()
5304 * in-memory size to match. in btrfs_setsize()
5306 ret2 = btrfs_wait_ordered_range(BTRFS_I(inode), 0, (u64)-1); in btrfs_setsize()
5309 i_size_write(inode, BTRFS_I(inode)->disk_i_size); in btrfs_setsize()
5320 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_setattr()
5324 return -EROFS; in btrfs_setattr()
5330 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { in btrfs_setattr()
5336 if (attr->ia_valid) { in btrfs_setattr()
5341 if (!ret && attr->ia_valid & ATTR_MODE) in btrfs_setattr()
5342 ret = posix_acl_chmod(idmap, dentry, inode->i_mode); in btrfs_setattr()
5357 * skip all those expensive operations on a per folio basis and do only
5363 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; in evict_inode_truncate_pages()
5366 ASSERT(inode->i_state & I_FREEING); in evict_inode_truncate_pages()
5367 truncate_inode_pages_final(&inode->i_data); in evict_inode_truncate_pages()
5369 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); in evict_inode_truncate_pages()
5379 * queue kthread), inode references (inode->i_count) were not taken in evict_inode_truncate_pages()
5383 * reference count - if we don't do it, when they access the inode's in evict_inode_truncate_pages()
5385 * use-after-free issue. in evict_inode_truncate_pages()
5387 spin_lock(&io_tree->lock); in evict_inode_truncate_pages()
5388 while (!RB_EMPTY_ROOT(&io_tree->state)) { in evict_inode_truncate_pages()
5395 node = rb_first(&io_tree->state); in evict_inode_truncate_pages()
5397 start = state->start; in evict_inode_truncate_pages()
5398 end = state->end; in evict_inode_truncate_pages()
5399 state_flags = state->state; in evict_inode_truncate_pages()
5400 spin_unlock(&io_tree->lock); in evict_inode_truncate_pages()
5414 end - start + 1, NULL); in evict_inode_truncate_pages()
5421 spin_lock(&io_tree->lock); in evict_inode_truncate_pages()
5423 spin_unlock(&io_tree->lock); in evict_inode_truncate_pages()
5429 struct btrfs_fs_info *fs_info = root->fs_info; in evict_refill_and_join()
5436 * delayed iputs. However the normal flushing code will run delayed in evict_refill_and_join()
5442 * delayed refs activity by truncating. in evict_refill_and_join()
5445 * if we fail to make this reservation we can re-try without the in evict_refill_and_join()
5448 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size + delayed_refs_extra, in evict_refill_and_join()
5451 ret = btrfs_block_rsv_refill(fs_info, rsv, rsv->size, in evict_refill_and_join()
5456 return ERR_PTR(-ENOSPC); in evict_refill_and_join()
5466 trans->block_rsv = &fs_info->trans_block_rsv; in evict_refill_and_join()
5467 trans->bytes_reserved = delayed_refs_extra; in evict_refill_and_join()
5468 btrfs_block_rsv_migrate(rsv, trans->block_rsv, in evict_refill_and_join()
5478 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_evict_inode()
5493 if (inode->i_nlink && in btrfs_evict_inode()
5494 ((btrfs_root_refs(&root->root_item) != 0 && in btrfs_evict_inode()
5502 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) in btrfs_evict_inode()
5505 if (inode->i_nlink > 0) { in btrfs_evict_inode()
5506 BUG_ON(btrfs_root_refs(&root->root_item) != 0 && in btrfs_evict_inode()
5521 * inode. We could have a delayed dir index deletion queued up, but in btrfs_evict_inode()
5545 trans->block_rsv = &rsv; in btrfs_evict_inode()
5548 trans->block_rsv = &fs_info->trans_block_rsv; in btrfs_evict_inode()
5551 * We have not added new delayed items for our inode after we in btrfs_evict_inode()
5552 * have flushed its delayed items, so no need to throttle on in btrfs_evict_inode()
5553 * delayed items. However we have modified extent buffers. in btrfs_evict_inode()
5556 if (ret && ret != -ENOSPC && ret != -EAGAIN) in btrfs_evict_inode()
5573 trans->block_rsv = &rsv; in btrfs_evict_inode()
5575 trans->block_rsv = &fs_info->trans_block_rsv; in btrfs_evict_inode()
5580 btrfs_block_rsv_release(fs_info, &rsv, (u64)-1, NULL); in btrfs_evict_inode()
5596 * If no dir entries were found, returns -ENOENT.
5597 * If found a corrupted location in dir entry, returns -EUCLEAN.
5604 struct btrfs_root *root = dir->root; in btrfs_inode_by_name()
5610 return -ENOMEM; in btrfs_inode_by_name()
5612 ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 1, &fname); in btrfs_inode_by_name()
5621 /* This needs to handle no-key deletions later on */ in btrfs_inode_by_name()
5626 ret = di ? PTR_ERR(di) : -ENOENT; in btrfs_inode_by_name()
5630 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); in btrfs_inode_by_name()
5631 if (unlikely(location->type != BTRFS_INODE_ITEM_KEY && in btrfs_inode_by_name()
5632 location->type != BTRFS_ROOT_ITEM_KEY)) { in btrfs_inode_by_name()
5633 ret = -EUCLEAN; in btrfs_inode_by_name()
5634 btrfs_warn(root->fs_info, in btrfs_inode_by_name()
5637 location->objectid, location->type, location->offset); in btrfs_inode_by_name()
5640 *type = btrfs_dir_ftype(path->nodes[0], di); in btrfs_inode_by_name()
5666 ret = fscrypt_setup_filename(&dir->vfs_inode, &dentry->d_name, 0, &fname); in fixup_tree_root_location()
5672 err = -ENOMEM; in fixup_tree_root_location()
5676 err = -ENOENT; in fixup_tree_root_location()
5677 key.objectid = btrfs_root_id(dir->root); in fixup_tree_root_location()
5679 key.offset = location->objectid; in fixup_tree_root_location()
5681 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); in fixup_tree_root_location()
5688 leaf = path->nodes[0]; in fixup_tree_root_location()
5689 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); in fixup_tree_root_location()
5701 new_root = btrfs_get_fs_root(fs_info, location->objectid, true); in fixup_tree_root_location()
5708 location->objectid = btrfs_root_dirid(&new_root->root_item); in fixup_tree_root_location()
5709 location->type = BTRFS_INODE_ITEM_KEY; in fixup_tree_root_location()
5710 location->offset = 0; in fixup_tree_root_location()
5721 struct btrfs_root *root = inode->root; in btrfs_del_inode_from_root()
5725 xa_lock(&root->inodes); in btrfs_del_inode_from_root()
5735 entry = __xa_cmpxchg(&root->inodes, btrfs_ino(inode), inode, NULL, in btrfs_del_inode_from_root()
5738 empty = xa_empty(&root->inodes); in btrfs_del_inode_from_root()
5739 xa_unlock(&root->inodes); in btrfs_del_inode_from_root()
5741 if (empty && btrfs_root_refs(&root->root_item) == 0) { in btrfs_del_inode_from_root()
5742 xa_lock(&root->inodes); in btrfs_del_inode_from_root()
5743 empty = xa_empty(&root->inodes); in btrfs_del_inode_from_root()
5744 xa_unlock(&root->inodes); in btrfs_del_inode_from_root()
5755 btrfs_set_inode_number(BTRFS_I(inode), args->ino); in btrfs_init_locked_inode()
5756 BTRFS_I(inode)->root = btrfs_grab_root(args->root); in btrfs_init_locked_inode()
5758 if (args->root && args->root == args->root->fs_info->tree_root && in btrfs_init_locked_inode()
5759 args->ino != BTRFS_BTREE_INODE_OBJECTID) in btrfs_init_locked_inode()
5761 &BTRFS_I(inode)->runtime_flags); in btrfs_init_locked_inode()
5769 return args->ino == btrfs_ino(BTRFS_I(inode)) && in btrfs_find_actor()
5770 args->root == BTRFS_I(inode)->root; in btrfs_find_actor()
5782 inode = iget5_locked_rcu(root->fs_info->sb, hashval, btrfs_find_actor, in btrfs_iget_locked()
5802 return ERR_PTR(-ENOMEM); in btrfs_iget_path()
5804 if (!(inode->vfs_inode.i_state & I_NEW)) in btrfs_iget_path()
5811 unlock_new_inode(&inode->vfs_inode); in btrfs_iget_path()
5826 return ERR_PTR(-ENOMEM); in btrfs_iget()
5828 if (!(inode->vfs_inode.i_state & I_NEW)) in btrfs_iget()
5833 iget_failed(&inode->vfs_inode); in btrfs_iget()
5834 return ERR_PTR(-ENOMEM); in btrfs_iget()
5842 unlock_new_inode(&inode->vfs_inode); in btrfs_iget()
5854 vfs_inode = new_inode(dir->i_sb); in new_simple_dir()
5856 return ERR_PTR(-ENOMEM); in new_simple_dir()
5859 inode->root = btrfs_grab_root(root); in new_simple_dir()
5860 inode->ref_root_id = key->objectid; in new_simple_dir()
5861 set_bit(BTRFS_INODE_ROOT_STUB, &inode->runtime_flags); in new_simple_dir()
5862 set_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags); in new_simple_dir()
5866 * We only need lookup, the rest is read-only and there's no inode in new_simple_dir()
5869 vfs_inode->i_op = &simple_dir_inode_operations; in new_simple_dir()
5870 vfs_inode->i_opflags &= ~IOP_XATTR; in new_simple_dir()
5871 vfs_inode->i_fop = &simple_dir_operations; in new_simple_dir()
5872 vfs_inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; in new_simple_dir()
5877 inode->i_otime_sec = ts.tv_sec; in new_simple_dir()
5878 inode->i_otime_nsec = ts.tv_nsec; in new_simple_dir()
5880 vfs_inode->i_uid = dir->i_uid; in new_simple_dir()
5881 vfs_inode->i_gid = dir->i_gid; in new_simple_dir()
5897 return fs_umode_to_ftype(inode->vfs_inode.i_mode); in btrfs_inode_type()
5904 struct btrfs_root *root = BTRFS_I(dir)->root; in btrfs_lookup_dentry()
5910 if (dentry->d_name.len > BTRFS_NAME_LEN) in btrfs_lookup_dentry()
5911 return ERR_PTR(-ENAMETOOLONG); in btrfs_lookup_dentry()
5926 inode->vfs_inode.i_mode, btrfs_inode_type(inode), in btrfs_lookup_dentry()
5928 iput(&inode->vfs_inode); in btrfs_lookup_dentry()
5929 return ERR_PTR(-EUCLEAN); in btrfs_lookup_dentry()
5931 return &inode->vfs_inode; in btrfs_lookup_dentry()
5937 if (ret != -ENOENT) in btrfs_lookup_dentry()
5948 down_read(&fs_info->cleanup_work_sem); in btrfs_lookup_dentry()
5949 if (!sb_rdonly(inode->vfs_inode.i_sb)) in btrfs_lookup_dentry()
5951 up_read(&fs_info->cleanup_work_sem); in btrfs_lookup_dentry()
5953 iput(&inode->vfs_inode); in btrfs_lookup_dentry()
5961 return &inode->vfs_inode; in btrfs_lookup_dentry()
5970 inode = d_inode(dentry->d_parent); in btrfs_dentry_delete()
5973 root = BTRFS_I(inode)->root; in btrfs_dentry_delete()
5974 if (btrfs_root_refs(&root->root_item) == 0) in btrfs_dentry_delete()
5988 if (inode == ERR_PTR(-ENOENT)) in btrfs_lookup()
5995 * in-memory index_cnt variable to the first free sequence number.
5999 struct btrfs_root *root = inode->root; in btrfs_set_inode_index_count()
6007 key.offset = (u64)-1; in btrfs_set_inode_index_count()
6011 return -ENOMEM; in btrfs_set_inode_index_count()
6020 if (path->slots[0] == 0) { in btrfs_set_inode_index_count()
6021 inode->index_cnt = BTRFS_DIR_START_INDEX; in btrfs_set_inode_index_count()
6025 path->slots[0]--; in btrfs_set_inode_index_count()
6027 leaf = path->nodes[0]; in btrfs_set_inode_index_count()
6028 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in btrfs_set_inode_index_count()
6032 inode->index_cnt = BTRFS_DIR_START_INDEX; in btrfs_set_inode_index_count()
6036 inode->index_cnt = found_key.offset + 1; in btrfs_set_inode_index_count()
6046 if (dir->index_cnt == (u64)-1) { in btrfs_get_dir_last_index()
6056 *index = dir->index_cnt - 1; in btrfs_get_dir_last_index()
6067 * similar to what NFS does, only we don't keep the buffer around in pagecache
6084 return -ENOMEM; in btrfs_opendir()
6085 private->last_index = last_index; in btrfs_opendir()
6086 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); in btrfs_opendir()
6087 if (!private->filldir_buf) { in btrfs_opendir()
6089 return -ENOMEM; in btrfs_opendir()
6091 file->private_data = private; in btrfs_opendir()
6097 struct btrfs_file_private *private = file->private_data; in btrfs_dir_llseek()
6101 &private->last_index); in btrfs_dir_llseek()
6117 while (entries--) { in btrfs_filldir()
6121 ctx->pos = get_unaligned(&entry->offset); in btrfs_filldir()
6122 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len), in btrfs_filldir()
6123 get_unaligned(&entry->ino), in btrfs_filldir()
6124 get_unaligned(&entry->type))) in btrfs_filldir()
6127 get_unaligned(&entry->name_len); in btrfs_filldir()
6128 ctx->pos++; in btrfs_filldir()
6136 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_real_readdir()
6137 struct btrfs_file_private *private = file->private_data; in btrfs_real_readdir()
6158 return -ENOMEM; in btrfs_real_readdir()
6160 addr = private->filldir_buf; in btrfs_real_readdir()
6161 path->reada = READA_FORWARD; in btrfs_real_readdir()
6163 put = btrfs_readdir_get_delayed_items(BTRFS_I(inode), private->last_index, in btrfs_real_readdir()
6168 key.offset = ctx->pos; in btrfs_real_readdir()
6173 struct extent_buffer *leaf = path->nodes[0]; in btrfs_real_readdir()
6180 if (found_key.offset < ctx->pos) in btrfs_real_readdir()
6182 if (found_key.offset > private->last_index) in btrfs_real_readdir()
6186 di = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); in btrfs_real_readdir()
6191 ret = btrfs_filldir(private->filldir_buf, entries, ctx); in btrfs_real_readdir()
6194 addr = private->filldir_buf; in btrfs_real_readdir()
6205 put_unaligned(name_len, &entry->name_len); in btrfs_real_readdir()
6206 put_unaligned(fs_ftype_to_dtype(ftype), &entry->type); in btrfs_real_readdir()
6208 put_unaligned(location.objectid, &entry->ino); in btrfs_real_readdir()
6209 put_unaligned(found_key.offset, &entry->offset); in btrfs_real_readdir()
6220 ret = btrfs_filldir(private->filldir_buf, entries, ctx); in btrfs_real_readdir()
6244 if (ctx->pos >= INT_MAX) in btrfs_real_readdir()
6245 ctx->pos = LLONG_MAX; in btrfs_real_readdir()
6247 ctx->pos = INT_MAX; in btrfs_real_readdir()
6264 struct btrfs_root *root = inode->root; in btrfs_dirty_inode()
6265 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_dirty_inode()
6269 if (test_bit(BTRFS_INODE_DUMMY, &inode->runtime_flags)) in btrfs_dirty_inode()
6277 if (ret == -ENOSPC || ret == -EDQUOT) { in btrfs_dirty_inode()
6287 if (inode->delayed_node) in btrfs_dirty_inode()
6299 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_update_time()
6303 return -EROFS; in btrfs_update_time()
6317 if (dir->index_cnt == (u64)-1) { in btrfs_set_inode_index()
6326 *index = dir->index_cnt; in btrfs_set_inode_index()
6327 dir->index_cnt++; in btrfs_set_inode_index()
6337 args.root = BTRFS_I(inode)->root; in btrfs_insert_inode_locked()
6340 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), in btrfs_insert_inode_locked()
6347 struct inode *dir = args->dir; in btrfs_new_inode_prepare()
6348 struct inode *inode = args->inode; in btrfs_new_inode_prepare()
6351 if (!args->orphan) { in btrfs_new_inode_prepare()
6352 ret = fscrypt_setup_filename(dir, &args->dentry->d_name, 0, in btrfs_new_inode_prepare()
6353 &args->fname); in btrfs_new_inode_prepare()
6358 ret = posix_acl_create(dir, &inode->i_mode, &args->default_acl, &args->acl); in btrfs_new_inode_prepare()
6360 fscrypt_free_filename(&args->fname); in btrfs_new_inode_prepare()
6367 if (BTRFS_I(dir)->prop_compress) in btrfs_new_inode_prepare()
6370 if (args->default_acl) in btrfs_new_inode_prepare()
6373 if (args->acl) in btrfs_new_inode_prepare()
6377 if (dir->i_security) in btrfs_new_inode_prepare()
6380 if (args->orphan) { in btrfs_new_inode_prepare()
6400 posix_acl_release(args->acl); in btrfs_new_inode_args_destroy()
6401 posix_acl_release(args->default_acl); in btrfs_new_inode_args_destroy()
6402 fscrypt_free_filename(&args->fname); in btrfs_new_inode_args_destroy()
6408 * Currently only the compression flags and the cow flags are inherited.
6414 flags = dir->flags; in btrfs_inherit_iflags()
6417 inode->flags &= ~BTRFS_INODE_COMPRESS; in btrfs_inherit_iflags()
6418 inode->flags |= BTRFS_INODE_NOCOMPRESS; in btrfs_inherit_iflags()
6420 inode->flags &= ~BTRFS_INODE_NOCOMPRESS; in btrfs_inherit_iflags()
6421 inode->flags |= BTRFS_INODE_COMPRESS; in btrfs_inherit_iflags()
6425 inode->flags |= BTRFS_INODE_NODATACOW; in btrfs_inherit_iflags()
6426 if (S_ISREG(inode->vfs_inode.i_mode)) in btrfs_inherit_iflags()
6427 inode->flags |= BTRFS_INODE_NODATASUM; in btrfs_inherit_iflags()
6437 struct inode *dir = args->dir; in btrfs_create_new_inode()
6438 struct inode *inode = args->inode; in btrfs_create_new_inode()
6439 const struct fscrypt_str *name = args->orphan ? NULL : &args->fname.disk_name; in btrfs_create_new_inode()
6455 return -ENOMEM; in btrfs_create_new_inode()
6457 if (!args->subvol) in btrfs_create_new_inode()
6458 BTRFS_I(inode)->root = btrfs_grab_root(BTRFS_I(dir)->root); in btrfs_create_new_inode()
6459 root = BTRFS_I(inode)->root; in btrfs_create_new_inode()
6470 ret = xa_reserve(&root->inodes, objectid, GFP_NOFS); in btrfs_create_new_inode()
6475 if (args->orphan) { in btrfs_create_new_inode()
6484 ret = btrfs_set_inode_index(BTRFS_I(dir), &BTRFS_I(inode)->dir_index); in btrfs_create_new_inode()
6489 if (S_ISDIR(inode->i_mode)) in btrfs_create_new_inode()
6490 BTRFS_I(inode)->index_cnt = BTRFS_DIR_START_INDEX; in btrfs_create_new_inode()
6492 BTRFS_I(inode)->generation = trans->transid; in btrfs_create_new_inode()
6493 inode->i_generation = BTRFS_I(inode)->generation; in btrfs_create_new_inode()
6500 set_bit(BTRFS_INODE_NO_CAP_XATTR, &BTRFS_I(inode)->runtime_flags); in btrfs_create_new_inode()
6507 if (!args->subvol) in btrfs_create_new_inode()
6511 if (S_ISREG(inode->i_mode)) { in btrfs_create_new_inode()
6513 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; in btrfs_create_new_inode()
6515 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | in btrfs_create_new_inode()
6522 if (!args->orphan) in btrfs_create_new_inode()
6523 BTRFS_I(dir)->index_cnt--; in btrfs_create_new_inode()
6541 if (!args->orphan) { in btrfs_create_new_inode()
6550 if (args->subvol) { in btrfs_create_new_inode()
6555 sizes[1] = name->len + sizeof(*ref); in btrfs_create_new_inode()
6561 batch.total_data_size = sizes[0] + (args->orphan ? 0 : sizes[1]); in btrfs_create_new_inode()
6562 batch.nr = args->orphan ? 1 : 2; in btrfs_create_new_inode()
6570 BTRFS_I(inode)->i_otime_sec = ts.tv_sec; in btrfs_create_new_inode()
6571 BTRFS_I(inode)->i_otime_nsec = ts.tv_nsec; in btrfs_create_new_inode()
6578 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], in btrfs_create_new_inode()
6580 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item, in btrfs_create_new_inode()
6582 fill_inode_item(trans, path->nodes[0], inode_item, inode); in btrfs_create_new_inode()
6584 if (!args->orphan) { in btrfs_create_new_inode()
6585 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, in btrfs_create_new_inode()
6588 if (args->subvol) { in btrfs_create_new_inode()
6589 btrfs_set_inode_ref_name_len(path->nodes[0], ref, 2); in btrfs_create_new_inode()
6590 btrfs_set_inode_ref_index(path->nodes[0], ref, 0); in btrfs_create_new_inode()
6591 write_extent_buffer(path->nodes[0], "..", ptr, 2); in btrfs_create_new_inode()
6593 btrfs_set_inode_ref_name_len(path->nodes[0], ref, in btrfs_create_new_inode()
6594 name->len); in btrfs_create_new_inode()
6595 btrfs_set_inode_ref_index(path->nodes[0], ref, in btrfs_create_new_inode()
6596 BTRFS_I(inode)->dir_index); in btrfs_create_new_inode()
6597 write_extent_buffer(path->nodes[0], name->name, ptr, in btrfs_create_new_inode()
6598 name->len); in btrfs_create_new_inode()
6610 if (args->subvol) { in btrfs_create_new_inode()
6617 parent = btrfs_iget(BTRFS_FIRST_FREE_OBJECTID, BTRFS_I(dir)->root); in btrfs_create_new_inode()
6623 iput(&parent->vfs_inode); in btrfs_create_new_inode()
6639 if (!args->subvol) { in btrfs_create_new_inode()
6659 if (args->orphan) { in btrfs_create_new_inode()
6667 0, BTRFS_I(inode)->dir_index); in btrfs_create_new_inode()
6685 xa_release(&root->inodes, objectid); in btrfs_create_new_inode()
6703 struct btrfs_root *root = parent_inode->root; in btrfs_add_link()
6708 memcpy(&key, &inode->root->root_key, sizeof(key)); in btrfs_add_link()
6730 if (ret == -EEXIST || ret == -EOVERFLOW) in btrfs_add_link()
6737 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size + in btrfs_add_link()
6738 name->len * 2); in btrfs_add_link()
6739 inode_inc_iversion(&parent_inode->vfs_inode); in btrfs_add_link()
6772 struct btrfs_root *root = BTRFS_I(dir)->root; in btrfs_create_common()
6811 inode = new_inode(dir->i_sb); in btrfs_mknod()
6813 return -ENOMEM; in btrfs_mknod()
6815 inode->i_op = &btrfs_special_inode_operations; in btrfs_mknod()
6816 init_special_inode(inode, inode->i_mode, rdev); in btrfs_mknod()
6825 inode = new_inode(dir->i_sb); in btrfs_create()
6827 return -ENOMEM; in btrfs_create()
6829 inode->i_fop = &btrfs_file_operations; in btrfs_create()
6830 inode->i_op = &btrfs_file_inode_operations; in btrfs_create()
6831 inode->i_mapping->a_ops = &btrfs_aops; in btrfs_create()
6839 struct btrfs_root *root = BTRFS_I(dir)->root; in btrfs_link()
6847 if (btrfs_root_id(root) != btrfs_root_id(BTRFS_I(inode)->root)) in btrfs_link()
6848 return -EXDEV; in btrfs_link()
6850 if (inode->i_nlink >= BTRFS_LINK_MAX) in btrfs_link()
6851 return -EMLINK; in btrfs_link()
6853 ret = fscrypt_setup_filename(dir, &dentry->d_name, 0, &fname); in btrfs_link()
6867 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6); in btrfs_link()
6875 BTRFS_I(inode)->dir_index = 0ULL; in btrfs_link()
6892 if (inode->i_nlink == 1) { in btrfs_link()
6907 btrfs_log_new_name(trans, old_dentry, NULL, 0, dentry->d_parent); in btrfs_link()
6922 inode = new_inode(dir->i_sb); in btrfs_mkdir()
6924 return ERR_PTR(-ENOMEM); in btrfs_mkdir()
6926 inode->i_op = &btrfs_dir_inode_operations; in btrfs_mkdir()
6927 inode->i_fop = &btrfs_dir_file_operations; in btrfs_mkdir()
6936 struct extent_buffer *leaf = path->nodes[0]; in uncompress_inline()
6937 const u32 blocksize = leaf->fs_info->sectorsize; in uncompress_inline()
6946 inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]); in uncompress_inline()
6949 return -ENOMEM; in uncompress_inline()
6967 folio_zero_range(folio, max_size, blocksize - max_size); in uncompress_inline()
6974 const u32 blocksize = path->nodes[0]->fs_info->sectorsize; in read_inline_extent()
6984 fi = btrfs_item_ptr(path->nodes[0], path->slots[0], in read_inline_extent()
6986 if (btrfs_file_extent_compression(path->nodes[0], fi) != BTRFS_COMPRESS_NONE) in read_inline_extent()
6990 btrfs_file_extent_ram_bytes(path->nodes[0], fi)); in read_inline_extent()
6992 read_extent_buffer(path->nodes[0], kaddr, in read_inline_extent()
6996 folio_zero_range(folio, copy_size, blocksize - copy_size); in read_inline_extent()
7009 * it from the B-tree and caching it if necessary. Note that there may be more
7015 * Return: ERR_PTR on error, non-NULL extent_map on success.
7020 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_get_extent()
7025 int extent_type = -1; in btrfs_get_extent()
7027 struct btrfs_root *root = inode->root; in btrfs_get_extent()
7032 struct extent_map_tree *em_tree = &inode->extent_tree; in btrfs_get_extent()
7034 read_lock(&em_tree->lock); in btrfs_get_extent()
7036 read_unlock(&em_tree->lock); in btrfs_get_extent()
7039 if (em->start > start || em->start + em->len <= start) in btrfs_get_extent()
7041 else if (em->disk_bytenr == EXTENT_MAP_INLINE && folio) in btrfs_get_extent()
7048 ret = -ENOMEM; in btrfs_get_extent()
7051 em->start = EXTENT_MAP_HOLE; in btrfs_get_extent()
7052 em->disk_bytenr = EXTENT_MAP_HOLE; in btrfs_get_extent()
7053 em->len = (u64)-1; in btrfs_get_extent()
7057 ret = -ENOMEM; in btrfs_get_extent()
7062 path->reada = READA_FORWARD; in btrfs_get_extent()
7066 * we only read when we're loading the free space cache, and at that in btrfs_get_extent()
7070 path->search_commit_root = 1; in btrfs_get_extent()
7071 path->skip_locking = 1; in btrfs_get_extent()
7078 if (path->slots[0] == 0) in btrfs_get_extent()
7080 path->slots[0]--; in btrfs_get_extent()
7084 leaf = path->nodes[0]; in btrfs_get_extent()
7085 item = btrfs_item_ptr(leaf, path->slots[0], in btrfs_get_extent()
7087 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in btrfs_get_extent()
7105 /* Only regular file could have regular/prealloc extent */ in btrfs_get_extent()
7106 if (unlikely(!S_ISREG(inode->vfs_inode.i_mode))) { in btrfs_get_extent()
7107 ret = -EUCLEAN; in btrfs_get_extent()
7109 "regular/prealloc extent found for non-regular inode %llu", in btrfs_get_extent()
7117 path->slots[0], in btrfs_get_extent()
7122 path->slots[0]++; in btrfs_get_extent()
7123 if (path->slots[0] >= btrfs_header_nritems(leaf)) { in btrfs_get_extent()
7130 leaf = path->nodes[0]; in btrfs_get_extent()
7132 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); in btrfs_get_extent()
7142 em->start = start; in btrfs_get_extent()
7143 em->len = found_key.offset - start; in btrfs_get_extent()
7144 em->disk_bytenr = EXTENT_MAP_HOLE; in btrfs_get_extent()
7155 * Inline extent can only exist at file offset 0. This is in btrfs_get_extent()
7156 * ensured by tree-checker and inline extent creation path. in btrfs_get_extent()
7160 ASSERT(em->start == 0); in btrfs_get_extent()
7168 ASSERT(em->disk_bytenr == EXTENT_MAP_INLINE); in btrfs_get_extent()
7169 ASSERT(em->len == fs_info->sectorsize); in btrfs_get_extent()
7177 em->start = start; in btrfs_get_extent()
7178 em->len = len; in btrfs_get_extent()
7179 em->disk_bytenr = EXTENT_MAP_HOLE; in btrfs_get_extent()
7183 if (unlikely(em->start > start || btrfs_extent_map_end(em) <= start)) { in btrfs_get_extent()
7186 em->start, em->len, start, len); in btrfs_get_extent()
7187 ret = -EIO; in btrfs_get_extent()
7191 write_lock(&em_tree->lock); in btrfs_get_extent()
7193 write_unlock(&em_tree->lock); in btrfs_get_extent()
7212 if (!block_group || block_group->ro) in btrfs_extent_readonly()
7226 * @orig_len: (optional) Return the original on-disk length of the file extent
7234 * NOTE: This only checks the file extents, caller is responsible to wait for
7241 struct btrfs_root *root = inode->root; in can_nocow_extent()
7242 struct btrfs_fs_info *fs_info = root->fs_info; in can_nocow_extent()
7247 struct extent_io_tree *io_tree = &inode->io_tree; in can_nocow_extent()
7254 return -ENOMEM; in can_nocow_extent()
7255 path->nowait = nowait; in can_nocow_extent()
7263 if (path->slots[0] == 0) { in can_nocow_extent()
7267 path->slots[0]--; in can_nocow_extent()
7270 leaf = path->nodes[0]; in can_nocow_extent()
7271 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in can_nocow_extent()
7286 fi = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); in can_nocow_extent()
7290 nocow_args.end = offset + *len - 1; in can_nocow_extent()
7307 if (!(inode->flags & BTRFS_INODE_NODATACOW) && in can_nocow_extent()
7312 root->fs_info->sectorsize) - 1; in can_nocow_extent()
7316 return -EAGAIN; in can_nocow_extent()
7340 * Only PREALLOC writes (NOCOW write into preallocated range) can in btrfs_create_io_em()
7349 /* We're only referring part of a larger preallocated extent. */ in btrfs_create_io_em()
7350 ASSERT(file_extent->num_bytes <= file_extent->ram_bytes); in btrfs_create_io_em()
7354 ASSERT(file_extent->disk_num_bytes == file_extent->num_bytes); in btrfs_create_io_em()
7355 ASSERT(file_extent->ram_bytes == file_extent->num_bytes); in btrfs_create_io_em()
7358 ASSERT(file_extent->offset == 0); in btrfs_create_io_em()
7362 ASSERT(file_extent->compression != BTRFS_COMPRESS_NONE); in btrfs_create_io_em()
7368 ASSERT(file_extent->num_bytes <= file_extent->ram_bytes); in btrfs_create_io_em()
7374 return ERR_PTR(-ENOMEM); in btrfs_create_io_em()
7376 em->start = start; in btrfs_create_io_em()
7377 em->len = file_extent->num_bytes; in btrfs_create_io_em()
7378 em->disk_bytenr = file_extent->disk_bytenr; in btrfs_create_io_em()
7379 em->disk_num_bytes = file_extent->disk_num_bytes; in btrfs_create_io_em()
7380 em->ram_bytes = file_extent->ram_bytes; in btrfs_create_io_em()
7381 em->generation = -1; in btrfs_create_io_em()
7382 em->offset = file_extent->offset; in btrfs_create_io_em()
7383 em->flags |= EXTENT_FLAG_PINNED; in btrfs_create_io_em()
7385 btrfs_extent_map_set_compression(em, file_extent->compression); in btrfs_create_io_em()
7400 * If we continue to release/invalidate the page, we could cause use-after-free
7420 * locked, the only possible way to hold a spinlock is from the endio in wait_subpage_spinlock()
7426 spin_lock_irq(&bfs->lock); in wait_subpage_spinlock()
7427 spin_unlock_irq(&bfs->lock); in wait_subpage_spinlock()
7478 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_invalidate_folio()
7479 struct extent_io_tree *tree = &inode->io_tree; in btrfs_invalidate_folio()
7482 u64 page_end = page_start + folio_size(folio) - 1; in btrfs_invalidate_folio()
7484 int inode_evicting = inode->vfs_inode.i_state & I_FREEING; in btrfs_invalidate_folio()
7507 * shouldn't clear page extent mapped, as folio->private can still in btrfs_invalidate_folio()
7530 page_end + 1 - cur); in btrfs_invalidate_folio()
7540 if (ordered->file_offset > cur) { in btrfs_invalidate_folio()
7542 * There is a range between [cur, oe->file_offset) not in btrfs_invalidate_folio()
7547 range_end = ordered->file_offset - 1; in btrfs_invalidate_folio()
7552 range_end = min(ordered->file_offset + ordered->num_bytes - 1, in btrfs_invalidate_folio()
7554 ASSERT(range_end + 1 - cur < U32_MAX); in btrfs_invalidate_folio()
7555 range_len = range_end + 1 - cur; in btrfs_invalidate_folio()
7581 spin_lock_irq(&inode->ordered_tree_lock); in btrfs_invalidate_folio()
7582 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); in btrfs_invalidate_folio()
7583 ordered->truncated_len = min(ordered->truncated_len, in btrfs_invalidate_folio()
7584 cur - ordered->file_offset); in btrfs_invalidate_folio()
7585 spin_unlock_irq(&inode->ordered_tree_lock); in btrfs_invalidate_folio()
7594 cur, range_end + 1 - cur)) { in btrfs_invalidate_folio()
7620 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur, NULL); in btrfs_invalidate_folio()
7648 struct btrfs_root *root = inode->root; in btrfs_truncate()
7649 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_truncate()
7653 u64 mask = fs_info->sectorsize - 1; in btrfs_truncate()
7658 inode->vfs_inode.i_size & (~mask), in btrfs_truncate()
7659 (u64)-1); in btrfs_truncate()
7687 * 1) rsv - for the truncate reservation, which we will steal from the in btrfs_truncate()
7689 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for in btrfs_truncate()
7707 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, &rsv, in btrfs_truncate()
7719 trans->block_rsv = &rsv; in btrfs_truncate()
7723 const u64 new_size = inode->vfs_inode.i_size; in btrfs_truncate()
7724 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize); in btrfs_truncate()
7727 btrfs_lock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state); in btrfs_truncate()
7734 ALIGN(new_size, fs_info->sectorsize), in btrfs_truncate()
7735 (u64)-1, false); in btrfs_truncate()
7739 inode_sub_bytes(&inode->vfs_inode, control.sub_bytes); in btrfs_truncate()
7742 btrfs_unlock_extent(&inode->io_tree, lock_start, (u64)-1, &cached_state); in btrfs_truncate()
7744 trans->block_rsv = &fs_info->trans_block_rsv; in btrfs_truncate()
7745 if (ret != -ENOSPC && ret != -EAGAIN) in btrfs_truncate()
7762 btrfs_block_rsv_release(fs_info, &rsv, -1, NULL); in btrfs_truncate()
7763 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, in btrfs_truncate()
7773 trans->block_rsv = &rsv; in btrfs_truncate()
7786 ret = btrfs_truncate_block(inode, inode->vfs_inode.i_size, in btrfs_truncate()
7787 inode->vfs_inode.i_size, (u64)-1); in btrfs_truncate()
7801 trans->block_rsv = &fs_info->trans_block_rsv; in btrfs_truncate()
7812 btrfs_block_rsv_release(fs_info, &rsv, (u64)-1, NULL); in btrfs_truncate()
7821 * fsync to truncate all the inode's items from the log and re-log them in btrfs_truncate()
7823 * or changed it to a smaller size but there was only an implicit hole in btrfs_truncate()
7838 inode = new_inode(dir->i_sb); in btrfs_new_subvol_inode()
7846 inode->i_op = &btrfs_dir_inode_operations; in btrfs_new_subvol_inode()
7847 inode->i_fop = &btrfs_dir_file_operations; in btrfs_new_subvol_inode()
7862 ei->root = NULL; in btrfs_alloc_inode()
7863 ei->generation = 0; in btrfs_alloc_inode()
7864 ei->last_trans = 0; in btrfs_alloc_inode()
7865 ei->last_sub_trans = 0; in btrfs_alloc_inode()
7866 ei->logged_trans = 0; in btrfs_alloc_inode()
7867 ei->delalloc_bytes = 0; in btrfs_alloc_inode()
7869 ei->new_delalloc_bytes = 0; in btrfs_alloc_inode()
7870 ei->defrag_bytes = 0; in btrfs_alloc_inode()
7871 ei->disk_i_size = 0; in btrfs_alloc_inode()
7872 ei->flags = 0; in btrfs_alloc_inode()
7873 ei->ro_flags = 0; in btrfs_alloc_inode()
7875 * ->index_cnt will be properly initialized later when creating a new in btrfs_alloc_inode()
7879 ei->csum_bytes = 0; in btrfs_alloc_inode()
7880 ei->dir_index = 0; in btrfs_alloc_inode()
7881 ei->last_unlink_trans = 0; in btrfs_alloc_inode()
7882 ei->last_reflink_trans = 0; in btrfs_alloc_inode()
7883 ei->last_log_commit = 0; in btrfs_alloc_inode()
7885 spin_lock_init(&ei->lock); in btrfs_alloc_inode()
7886 ei->outstanding_extents = 0; in btrfs_alloc_inode()
7887 if (sb->s_magic != BTRFS_TEST_MAGIC) in btrfs_alloc_inode()
7888 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv, in btrfs_alloc_inode()
7890 ei->runtime_flags = 0; in btrfs_alloc_inode()
7891 ei->prop_compress = BTRFS_COMPRESS_NONE; in btrfs_alloc_inode()
7892 ei->defrag_compress = BTRFS_COMPRESS_NONE; in btrfs_alloc_inode()
7894 ei->delayed_node = NULL; in btrfs_alloc_inode()
7896 ei->i_otime_sec = 0; in btrfs_alloc_inode()
7897 ei->i_otime_nsec = 0; in btrfs_alloc_inode()
7899 inode = &ei->vfs_inode; in btrfs_alloc_inode()
7900 btrfs_extent_map_tree_init(&ei->extent_tree); in btrfs_alloc_inode()
7903 btrfs_extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO); in btrfs_alloc_inode()
7904 ei->io_tree.inode = ei; in btrfs_alloc_inode()
7906 ei->file_extent_tree = NULL; in btrfs_alloc_inode()
7908 mutex_init(&ei->log_mutex); in btrfs_alloc_inode()
7909 spin_lock_init(&ei->ordered_tree_lock); in btrfs_alloc_inode()
7910 ei->ordered_tree = RB_ROOT; in btrfs_alloc_inode()
7911 ei->ordered_tree_last = NULL; in btrfs_alloc_inode()
7912 INIT_LIST_HEAD(&ei->delalloc_inodes); in btrfs_alloc_inode()
7913 INIT_LIST_HEAD(&ei->delayed_iput); in btrfs_alloc_inode()
7914 init_rwsem(&ei->i_mmap_lock); in btrfs_alloc_inode()
7922 btrfs_drop_extent_map_range(BTRFS_I(inode), 0, (u64)-1, false); in btrfs_test_destroy_inode()
7923 kfree(BTRFS_I(inode)->file_extent_tree); in btrfs_test_destroy_inode()
7930 kfree(BTRFS_I(inode)->file_extent_tree); in btrfs_free_inode()
7938 struct btrfs_root *root = inode->root; in btrfs_destroy_inode()
7941 WARN_ON(!hlist_empty(&vfs_inode->i_dentry)); in btrfs_destroy_inode()
7942 WARN_ON(vfs_inode->i_data.nrpages); in btrfs_destroy_inode()
7943 WARN_ON(inode->block_rsv.reserved); in btrfs_destroy_inode()
7944 WARN_ON(inode->block_rsv.size); in btrfs_destroy_inode()
7945 WARN_ON(inode->outstanding_extents); in btrfs_destroy_inode()
7946 if (!S_ISDIR(vfs_inode->i_mode)) { in btrfs_destroy_inode()
7947 WARN_ON(inode->delalloc_bytes); in btrfs_destroy_inode()
7948 WARN_ON(inode->new_delalloc_bytes); in btrfs_destroy_inode()
7949 WARN_ON(inode->csum_bytes); in btrfs_destroy_inode()
7952 WARN_ON(inode->defrag_bytes); in btrfs_destroy_inode()
7969 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); in btrfs_destroy_inode()
7973 btrfs_err(root->fs_info, in btrfs_destroy_inode()
7975 ordered->file_offset, ordered->num_bytes); in btrfs_destroy_inode()
7978 btrfs_lockdep_acquire(root->fs_info, btrfs_ordered_extent); in btrfs_destroy_inode()
7987 btrfs_drop_extent_map_range(inode, 0, (u64)-1, false); in btrfs_destroy_inode()
7988 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1); in btrfs_destroy_inode()
7989 btrfs_put_root(inode->root); in btrfs_destroy_inode()
7994 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_drop_inode()
8000 if (btrfs_root_refs(&root->root_item) == 0) in btrfs_drop_inode()
8010 inode_init_once(&ei->vfs_inode); in init_once()
8012 ei->i_verity_info = NULL; in init_once()
8019 * Make sure all delayed rcu free inodes are flushed before we in btrfs_destroy_cachep()
8033 return -ENOMEM; in btrfs_init_cachep()
8044 struct inode *inode = d_inode(path->dentry); in btrfs_getattr()
8045 u32 blocksize = btrfs_sb(inode->i_sb)->sectorsize; in btrfs_getattr()
8046 u32 bi_flags = BTRFS_I(inode)->flags; in btrfs_getattr()
8047 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags; in btrfs_getattr()
8049 stat->result_mask |= STATX_BTIME; in btrfs_getattr()
8050 stat->btime.tv_sec = BTRFS_I(inode)->i_otime_sec; in btrfs_getattr()
8051 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime_nsec; in btrfs_getattr()
8053 stat->attributes |= STATX_ATTR_APPEND; in btrfs_getattr()
8055 stat->attributes |= STATX_ATTR_COMPRESSED; in btrfs_getattr()
8057 stat->attributes |= STATX_ATTR_IMMUTABLE; in btrfs_getattr()
8059 stat->attributes |= STATX_ATTR_NODUMP; in btrfs_getattr()
8061 stat->attributes |= STATX_ATTR_VERITY; in btrfs_getattr()
8063 stat->attributes_mask |= (STATX_ATTR_APPEND | in btrfs_getattr()
8069 stat->dev = BTRFS_I(inode)->root->anon_dev; in btrfs_getattr()
8071 stat->subvol = btrfs_root_id(BTRFS_I(inode)->root); in btrfs_getattr()
8072 stat->result_mask |= STATX_SUBVOL; in btrfs_getattr()
8074 spin_lock(&BTRFS_I(inode)->lock); in btrfs_getattr()
8075 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; in btrfs_getattr()
8077 spin_unlock(&BTRFS_I(inode)->lock); in btrfs_getattr()
8078 stat->blocks = (ALIGN(inode_bytes, blocksize) + in btrfs_getattr()
8091 struct btrfs_root *root = BTRFS_I(old_dir)->root; in btrfs_rename_exchange()
8092 struct btrfs_root *dest = BTRFS_I(new_dir)->root; in btrfs_rename_exchange()
8093 struct inode *new_inode = new_dentry->d_inode; in btrfs_rename_exchange()
8094 struct inode *old_inode = old_dentry->d_inode; in btrfs_rename_exchange()
8109 * For non-subvolumes allow exchange only within one subvolume, in the in btrfs_rename_exchange()
8116 return -EXDEV; in btrfs_rename_exchange()
8118 ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); in btrfs_rename_exchange()
8122 ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); in btrfs_rename_exchange()
8134 down_read(&fs_info->subvol_sem); in btrfs_rename_exchange()
8144 * If the parents are the same, we only need to account for one in btrfs_rename_exchange()
8190 BTRFS_I(old_inode)->dir_index = 0ULL; in btrfs_rename_exchange()
8191 BTRFS_I(new_inode)->dir_index = 0ULL; in btrfs_rename_exchange()
8242 * updates for only one of the directories but not for the in btrfs_rename_exchange()
8253 if (old_dentry->d_parent != new_dentry->d_parent) { in btrfs_rename_exchange()
8269 BTRFS_I(old_dentry->d_inode), in btrfs_rename_exchange()
8291 BTRFS_I(new_dentry->d_inode), in btrfs_rename_exchange()
8318 if (old_inode->i_nlink == 1) in btrfs_rename_exchange()
8319 BTRFS_I(old_inode)->dir_index = old_idx; in btrfs_rename_exchange()
8320 if (new_inode->i_nlink == 1) in btrfs_rename_exchange()
8321 BTRFS_I(new_inode)->dir_index = new_idx; in btrfs_rename_exchange()
8331 old_rename_ctx.index, new_dentry->d_parent); in btrfs_rename_exchange()
8333 new_rename_ctx.index, old_dentry->d_parent); in btrfs_rename_exchange()
8346 up_read(&fs_info->subvol_sem); in btrfs_rename_exchange()
8358 inode = new_inode(dir->i_sb); in new_whiteout_inode()
8362 inode->i_op = &btrfs_special_inode_operations; in new_whiteout_inode()
8363 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV); in new_whiteout_inode()
8380 struct btrfs_root *root = BTRFS_I(old_dir)->root; in btrfs_rename()
8381 struct btrfs_root *dest = BTRFS_I(new_dir)->root; in btrfs_rename()
8393 return -EPERM; in btrfs_rename()
8395 /* we only allow rename subvolume link between subvolumes */ in btrfs_rename()
8397 return -EXDEV; in btrfs_rename()
8401 return -ENOTEMPTY; in btrfs_rename()
8403 if (S_ISDIR(old_inode->i_mode) && new_inode && in btrfs_rename()
8404 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) in btrfs_rename()
8405 return -ENOTEMPTY; in btrfs_rename()
8407 ret = fscrypt_setup_filename(old_dir, &old_dentry->d_name, 0, &old_fname); in btrfs_rename()
8411 ret = fscrypt_setup_filename(new_dir, &new_dentry->d_name, 0, &new_fname); in btrfs_rename()
8418 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, &new_fname.disk_name); in btrfs_rename()
8420 if (ret == -EEXIST) { in btrfs_rename()
8427 /* maybe -EOVERFLOW */ in btrfs_rename()
8437 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) in btrfs_rename()
8438 filemap_flush(old_inode->i_mapping); in btrfs_rename()
8443 ret = -ENOMEM; in btrfs_rename()
8456 down_read(&fs_info->subvol_sem); in btrfs_rename()
8508 BTRFS_I(old_inode)->dir_index = 0ULL; in btrfs_rename()
8537 * updates for only one of the directories but not for the in btrfs_rename()
8548 if (old_dentry->d_parent != new_dentry->d_parent) in btrfs_rename()
8582 BUG_ON(new_inode->i_nlink == 0); in btrfs_rename()
8592 if (new_inode->i_nlink == 0) { in btrfs_rename()
8609 if (old_inode->i_nlink == 1) in btrfs_rename()
8610 BTRFS_I(old_inode)->dir_index = index; in btrfs_rename()
8614 rename_ctx.index, new_dentry->d_parent); in btrfs_rename()
8636 up_read(&fs_info->subvol_sem); in btrfs_rename()
8655 return -EINVAL; in btrfs_rename2()
8664 btrfs_btree_balance_dirty(BTRFS_I(new_dir)->root->fs_info); in btrfs_rename2()
8683 inode = delalloc_work->inode; in btrfs_run_delalloc_work()
8684 filemap_flush(inode->i_mapping); in btrfs_run_delalloc_work()
8686 &BTRFS_I(inode)->runtime_flags)) in btrfs_run_delalloc_work()
8687 filemap_flush(inode->i_mapping); in btrfs_run_delalloc_work()
8690 complete(&delalloc_work->completion); in btrfs_run_delalloc_work()
8701 init_completion(&work->completion); in btrfs_alloc_delalloc_work()
8702 INIT_LIST_HEAD(&work->list); in btrfs_alloc_delalloc_work()
8703 work->inode = inode; in btrfs_alloc_delalloc_work()
8704 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL); in btrfs_alloc_delalloc_work()
8721 bool full_flush = wbc->nr_to_write == LONG_MAX; in start_delalloc_inodes()
8723 mutex_lock(&root->delalloc_mutex); in start_delalloc_inodes()
8724 spin_lock(&root->delalloc_lock); in start_delalloc_inodes()
8725 list_splice_init(&root->delalloc_inodes, &splice); in start_delalloc_inodes()
8732 list_move_tail(&inode->delalloc_inodes, &root->delalloc_inodes); in start_delalloc_inodes()
8735 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &inode->runtime_flags)) in start_delalloc_inodes()
8738 tmp_inode = igrab(&inode->vfs_inode); in start_delalloc_inodes()
8740 cond_resched_lock(&root->delalloc_lock); in start_delalloc_inodes()
8743 spin_unlock(&root->delalloc_lock); in start_delalloc_inodes()
8746 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH, &inode->runtime_flags); in start_delalloc_inodes()
8748 work = btrfs_alloc_delalloc_work(&inode->vfs_inode); in start_delalloc_inodes()
8750 iput(&inode->vfs_inode); in start_delalloc_inodes()
8751 ret = -ENOMEM; in start_delalloc_inodes()
8754 list_add_tail(&work->list, &works); in start_delalloc_inodes()
8755 btrfs_queue_work(root->fs_info->flush_workers, in start_delalloc_inodes()
8756 &work->work); in start_delalloc_inodes()
8758 ret = filemap_fdatawrite_wbc(inode->vfs_inode.i_mapping, wbc); in start_delalloc_inodes()
8760 if (ret || wbc->nr_to_write <= 0) in start_delalloc_inodes()
8764 spin_lock(&root->delalloc_lock); in start_delalloc_inodes()
8766 spin_unlock(&root->delalloc_lock); in start_delalloc_inodes()
8770 list_del_init(&work->list); in start_delalloc_inodes()
8771 wait_for_completion(&work->completion); in start_delalloc_inodes()
8776 spin_lock(&root->delalloc_lock); in start_delalloc_inodes()
8777 list_splice_tail(&splice, &root->delalloc_inodes); in start_delalloc_inodes()
8778 spin_unlock(&root->delalloc_lock); in start_delalloc_inodes()
8780 mutex_unlock(&root->delalloc_mutex); in start_delalloc_inodes()
8792 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_start_delalloc_snapshot()
8795 return -EROFS; in btrfs_start_delalloc_snapshot()
8814 return -EROFS; in btrfs_start_delalloc_roots()
8816 mutex_lock(&fs_info->delalloc_root_mutex); in btrfs_start_delalloc_roots()
8817 spin_lock(&fs_info->delalloc_root_lock); in btrfs_start_delalloc_roots()
8818 list_splice_init(&fs_info->delalloc_roots, &splice); in btrfs_start_delalloc_roots()
8831 list_move_tail(&root->delalloc_root, in btrfs_start_delalloc_roots()
8832 &fs_info->delalloc_roots); in btrfs_start_delalloc_roots()
8833 spin_unlock(&fs_info->delalloc_root_lock); in btrfs_start_delalloc_roots()
8839 spin_lock(&fs_info->delalloc_root_lock); in btrfs_start_delalloc_roots()
8841 spin_unlock(&fs_info->delalloc_root_lock); in btrfs_start_delalloc_roots()
8846 spin_lock(&fs_info->delalloc_root_lock); in btrfs_start_delalloc_roots()
8847 list_splice_tail(&splice, &fs_info->delalloc_roots); in btrfs_start_delalloc_roots()
8848 spin_unlock(&fs_info->delalloc_root_lock); in btrfs_start_delalloc_roots()
8850 mutex_unlock(&fs_info->delalloc_root_mutex); in btrfs_start_delalloc_roots()
8859 struct btrfs_root *root = BTRFS_I(dir)->root; in btrfs_symlink()
8881 name_len >= fs_info->sectorsize) in btrfs_symlink()
8882 return -ENAMETOOLONG; in btrfs_symlink()
8884 inode = new_inode(dir->i_sb); in btrfs_symlink()
8886 return -ENOMEM; in btrfs_symlink()
8888 inode->i_op = &btrfs_symlink_inode_operations; in btrfs_symlink()
8890 inode->i_mapping->a_ops = &btrfs_aops; in btrfs_symlink()
8913 ret = -ENOMEM; in btrfs_symlink()
8931 leaf = path->nodes[0]; in btrfs_symlink()
8932 ei = btrfs_item_ptr(leaf, path->slots[0], in btrfs_symlink()
8934 btrfs_set_file_extent_generation(leaf, ei, trans->transid); in btrfs_symlink()
8969 u64 start = ins->objectid; in insert_prealloc_file_extent()
8970 u64 len = ins->offset; in insert_prealloc_file_extent()
9010 ret = -ENOMEM; in insert_prealloc_file_extent()
9015 file_offset + len - 1, &extent_info, in insert_prealloc_file_extent()
9030 btrfs_qgroup_free_refroot(inode->root->fs_info, in insert_prealloc_file_extent()
9031 btrfs_root_id(inode->root), qgroup_released, in insert_prealloc_file_extent()
9043 struct btrfs_root *root = BTRFS_I(inode)->root; in __btrfs_prealloc_file_range()
9049 u64 last_alloc = (u64)-1; in __btrfs_prealloc_file_range()
9052 u64 end = start + num_bytes - 1; in __btrfs_prealloc_file_range()
9062 * chunks lets make its job easier by only searching for those in __btrfs_prealloc_file_range()
9073 * ->bytes_may_use to ->bytes_reserved. Any error that happens in __btrfs_prealloc_file_range()
9074 * from here on out we will only need to clear our reservation in __btrfs_prealloc_file_range()
9100 cur_offset + ins.offset - 1, false); in __btrfs_prealloc_file_range()
9105 em->start = cur_offset; in __btrfs_prealloc_file_range()
9106 em->len = ins.offset; in __btrfs_prealloc_file_range()
9107 em->disk_bytenr = ins.objectid; in __btrfs_prealloc_file_range()
9108 em->offset = 0; in __btrfs_prealloc_file_range()
9109 em->disk_num_bytes = ins.offset; in __btrfs_prealloc_file_range()
9110 em->ram_bytes = ins.offset; in __btrfs_prealloc_file_range()
9111 em->flags |= EXTENT_FLAG_PREALLOC; in __btrfs_prealloc_file_range()
9112 em->generation = trans->transid; in __btrfs_prealloc_file_range()
9117 num_bytes -= ins.offset; in __btrfs_prealloc_file_range()
9123 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; in __btrfs_prealloc_file_range()
9125 (actual_len > inode->i_size) && in __btrfs_prealloc_file_range()
9126 (cur_offset > inode->i_size)) { in __btrfs_prealloc_file_range()
9151 end - clear_offset + 1); in __btrfs_prealloc_file_range()
9176 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_permission()
9177 umode_t mode = inode->i_mode; in btrfs_permission()
9182 return -EROFS; in btrfs_permission()
9183 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) in btrfs_permission()
9184 return -EACCES; in btrfs_permission()
9194 struct btrfs_root *root = BTRFS_I(dir)->root; in btrfs_tmpfile()
9198 .dentry = file->f_path.dentry, in btrfs_tmpfile()
9204 inode = new_inode(dir->i_sb); in btrfs_tmpfile()
9206 return -ENOMEM; in btrfs_tmpfile()
9208 inode->i_fop = &btrfs_file_operations; in btrfs_tmpfile()
9209 inode->i_op = &btrfs_file_inode_operations; in btrfs_tmpfile()
9210 inode->i_mapping->a_ops = &btrfs_aops; in btrfs_tmpfile()
9230 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() in btrfs_tmpfile()
9263 if (fs_info->sectorsize < SZ_4K || fs_info->sectorsize > SZ_64K) in btrfs_encoded_io_compression_from_extent()
9264 return -EINVAL; in btrfs_encoded_io_compression_from_extent()
9266 (fs_info->sectorsize_bits - 12); in btrfs_encoded_io_compression_from_extent()
9270 return -EUCLEAN; in btrfs_encoded_io_compression_from_extent()
9283 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); in btrfs_encoded_read_inline()
9284 struct btrfs_root *root = inode->root; in btrfs_encoded_read_inline()
9285 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_encoded_read_inline()
9286 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_encoded_read_inline()
9294 const bool nowait = (iocb->ki_flags & IOCB_NOWAIT); in btrfs_encoded_read_inline()
9298 return -ENOMEM; in btrfs_encoded_read_inline()
9300 path->nowait = nowait; in btrfs_encoded_read_inline()
9307 return -EIO; in btrfs_encoded_read_inline()
9311 leaf = path->nodes[0]; in btrfs_encoded_read_inline()
9312 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); in btrfs_encoded_read_inline()
9317 encoded->len = min_t(u64, extent_start + ram_bytes, in btrfs_encoded_read_inline()
9318 inode->vfs_inode.i_size) - iocb->ki_pos; in btrfs_encoded_read_inline()
9323 encoded->compression = ret; in btrfs_encoded_read_inline()
9324 if (encoded->compression) { in btrfs_encoded_read_inline()
9328 path->slots[0]); in btrfs_encoded_read_inline()
9330 return -ENOBUFS; in btrfs_encoded_read_inline()
9333 encoded->unencoded_len = ram_bytes; in btrfs_encoded_read_inline()
9334 encoded->unencoded_offset = iocb->ki_pos - extent_start; in btrfs_encoded_read_inline()
9336 count = min_t(u64, count, encoded->len); in btrfs_encoded_read_inline()
9337 encoded->len = count; in btrfs_encoded_read_inline()
9338 encoded->unencoded_len = count; in btrfs_encoded_read_inline()
9339 ptr += iocb->ki_pos - extent_start; in btrfs_encoded_read_inline()
9344 return -ENOMEM; in btrfs_encoded_read_inline()
9354 ret = -EFAULT; in btrfs_encoded_read_inline()
9369 struct btrfs_encoded_read_private *priv = bbio->private; in btrfs_encoded_read_endio()
9371 if (bbio->bio.bi_status) { in btrfs_encoded_read_endio()
9379 WRITE_ONCE(priv->status, bbio->bio.bi_status); in btrfs_encoded_read_endio()
9381 if (refcount_dec_and_test(&priv->pending_refs)) { in btrfs_encoded_read_endio()
9382 int err = blk_status_to_errno(READ_ONCE(priv->status)); in btrfs_encoded_read_endio()
9384 if (priv->uring_ctx) { in btrfs_encoded_read_endio()
9385 btrfs_uring_read_extent_endio(priv->uring_ctx, err); in btrfs_encoded_read_endio()
9388 complete(priv->sync_reads); in btrfs_encoded_read_endio()
9391 bio_put(&bbio->bio); in btrfs_encoded_read_endio()
9398 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_encoded_read_regular_fill_pages()
9412 return -ENOMEM; in btrfs_encoded_read_regular_fill_pages()
9416 priv->sync_reads = &sync_reads; in btrfs_encoded_read_regular_fill_pages()
9419 refcount_set(&priv->pending_refs, 1); in btrfs_encoded_read_regular_fill_pages()
9420 priv->status = 0; in btrfs_encoded_read_regular_fill_pages()
9421 priv->uring_ctx = uring_ctx; in btrfs_encoded_read_regular_fill_pages()
9425 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; in btrfs_encoded_read_regular_fill_pages()
9426 bbio->inode = inode; in btrfs_encoded_read_regular_fill_pages()
9431 if (bio_add_page(&bbio->bio, pages[i], bytes, 0) < bytes) { in btrfs_encoded_read_regular_fill_pages()
9432 refcount_inc(&priv->pending_refs); in btrfs_encoded_read_regular_fill_pages()
9437 bbio->bio.bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT; in btrfs_encoded_read_regular_fill_pages()
9438 bbio->inode = inode; in btrfs_encoded_read_regular_fill_pages()
9444 disk_io_size -= bytes; in btrfs_encoded_read_regular_fill_pages()
9447 refcount_inc(&priv->pending_refs); in btrfs_encoded_read_regular_fill_pages()
9451 if (refcount_dec_and_test(&priv->pending_refs)) { in btrfs_encoded_read_regular_fill_pages()
9452 ret = blk_status_to_errno(READ_ONCE(priv->status)); in btrfs_encoded_read_regular_fill_pages()
9458 return -EIOCBQUEUED; in btrfs_encoded_read_regular_fill_pages()
9460 if (!refcount_dec_and_test(&priv->pending_refs)) in btrfs_encoded_read_regular_fill_pages()
9463 return blk_status_to_errno(READ_ONCE(priv->status)); in btrfs_encoded_read_regular_fill_pages()
9473 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); in btrfs_encoded_read_regular()
9474 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_encoded_read_regular()
9484 return -ENOMEM; in btrfs_encoded_read_regular()
9487 ret = -ENOMEM; in btrfs_encoded_read_regular()
9504 i = (iocb->ki_pos - start) >> PAGE_SHIFT; in btrfs_encoded_read_regular()
9505 page_offset = (iocb->ki_pos - start) & (PAGE_SIZE - 1); in btrfs_encoded_read_regular()
9509 size_t bytes = min_t(size_t, count - cur, in btrfs_encoded_read_regular()
9510 PAGE_SIZE - page_offset); in btrfs_encoded_read_regular()
9514 ret = -EFAULT; in btrfs_encoded_read_regular()
9536 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); in btrfs_encoded_read()
9537 struct btrfs_fs_info *fs_info = inode->root->fs_info; in btrfs_encoded_read()
9538 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_encoded_read()
9543 const bool nowait = (iocb->ki_flags & IOCB_NOWAIT); in btrfs_encoded_read()
9546 file_accessed(iocb->ki_filp); in btrfs_encoded_read()
9553 if (iocb->ki_pos >= inode->vfs_inode.i_size) { in btrfs_encoded_read()
9557 start = ALIGN_DOWN(iocb->ki_pos, fs_info->sectorsize); in btrfs_encoded_read()
9559 * We don't know how long the extent containing iocb->ki_pos is, but if in btrfs_encoded_read()
9562 lockend = start + BTRFS_MAX_UNCOMPRESSED - 1; in btrfs_encoded_read()
9567 if (filemap_range_needs_writeback(inode->vfs_inode.i_mapping, in btrfs_encoded_read()
9569 ret = -EAGAIN; in btrfs_encoded_read()
9574 ret = -EAGAIN; in btrfs_encoded_read()
9579 lockend - start + 1); in btrfs_encoded_read()
9583 ret = -EAGAIN; in btrfs_encoded_read()
9591 lockend - start + 1); in btrfs_encoded_read()
9597 lockend - start + 1); in btrfs_encoded_read()
9606 em = btrfs_get_extent(inode, NULL, start, lockend - start + 1); in btrfs_encoded_read()
9612 if (em->disk_bytenr == EXTENT_MAP_INLINE) { in btrfs_encoded_read()
9613 u64 extent_start = em->start; in btrfs_encoded_read()
9628 * We only want to return up to EOF even if the extent extends beyond in btrfs_encoded_read()
9631 encoded->len = min_t(u64, btrfs_extent_map_end(em), in btrfs_encoded_read()
9632 inode->vfs_inode.i_size) - iocb->ki_pos; in btrfs_encoded_read()
9633 if (em->disk_bytenr == EXTENT_MAP_HOLE || in btrfs_encoded_read()
9634 (em->flags & EXTENT_FLAG_PREALLOC)) { in btrfs_encoded_read()
9636 count = min_t(u64, count, encoded->len); in btrfs_encoded_read()
9637 encoded->len = count; in btrfs_encoded_read()
9638 encoded->unencoded_len = count; in btrfs_encoded_read()
9640 *disk_bytenr = em->disk_bytenr; in btrfs_encoded_read()
9645 if (em->disk_num_bytes > count) { in btrfs_encoded_read()
9646 ret = -ENOBUFS; in btrfs_encoded_read()
9649 *disk_io_size = em->disk_num_bytes; in btrfs_encoded_read()
9650 count = em->disk_num_bytes; in btrfs_encoded_read()
9651 encoded->unencoded_len = em->ram_bytes; in btrfs_encoded_read()
9652 encoded->unencoded_offset = iocb->ki_pos - (em->start - em->offset); in btrfs_encoded_read()
9657 encoded->compression = ret; in btrfs_encoded_read()
9659 *disk_bytenr = btrfs_extent_map_block_start(em) + (start - em->start); in btrfs_encoded_read()
9660 if (encoded->len > count) in btrfs_encoded_read()
9661 encoded->len = count; in btrfs_encoded_read()
9666 *disk_io_size = min(lockend + 1, iocb->ki_pos + encoded->len) - start; in btrfs_encoded_read()
9667 count = start + *disk_io_size - iocb->ki_pos; in btrfs_encoded_read()
9668 encoded->len = count; in btrfs_encoded_read()
9669 encoded->unencoded_len = count; in btrfs_encoded_read()
9670 *disk_io_size = ALIGN(*disk_io_size, fs_info->sectorsize); in btrfs_encoded_read()
9681 ret = -EFAULT; in btrfs_encoded_read()
9683 ret = -EIOCBQUEUED; in btrfs_encoded_read()
9691 if (!unlocked && ret != -EIOCBQUEUED) in btrfs_encoded_read()
9694 if (!unlocked && ret != -EIOCBQUEUED) in btrfs_encoded_read()
9702 struct btrfs_inode *inode = BTRFS_I(file_inode(iocb->ki_filp)); in btrfs_do_encoded_write()
9703 struct btrfs_root *root = inode->root; in btrfs_do_encoded_write()
9704 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_do_encoded_write()
9705 struct extent_io_tree *io_tree = &inode->io_tree; in btrfs_do_encoded_write()
9721 switch (encoded->compression) { in btrfs_do_encoded_write()
9734 if (encoded->compression - in btrfs_do_encoded_write()
9736 fs_info->sectorsize_bits) in btrfs_do_encoded_write()
9737 return -EINVAL; in btrfs_do_encoded_write()
9741 return -EINVAL; in btrfs_do_encoded_write()
9743 if (encoded->encryption != BTRFS_ENCODED_IO_ENCRYPTION_NONE) in btrfs_do_encoded_write()
9744 return -EINVAL; in btrfs_do_encoded_write()
9750 if (inode->flags & BTRFS_INODE_NODATASUM) in btrfs_do_encoded_write()
9751 return -EINVAL; in btrfs_do_encoded_write()
9756 if (encoded->unencoded_len > BTRFS_MAX_UNCOMPRESSED || in btrfs_do_encoded_write()
9758 return -EINVAL; in btrfs_do_encoded_write()
9770 * decompressed data. We only want to enforce the weaker requirement in btrfs_do_encoded_write()
9773 if (orig_count >= encoded->unencoded_len) in btrfs_do_encoded_write()
9774 return -EINVAL; in btrfs_do_encoded_write()
9777 start = iocb->ki_pos; in btrfs_do_encoded_write()
9778 if (!IS_ALIGNED(start, fs_info->sectorsize)) in btrfs_do_encoded_write()
9779 return -EINVAL; in btrfs_do_encoded_write()
9786 if (start + encoded->len < inode->vfs_inode.i_size && in btrfs_do_encoded_write()
9787 !IS_ALIGNED(start + encoded->len, fs_info->sectorsize)) in btrfs_do_encoded_write()
9788 return -EINVAL; in btrfs_do_encoded_write()
9790 /* Finally, the offset in the unencoded data must be sector-aligned. */ in btrfs_do_encoded_write()
9791 if (!IS_ALIGNED(encoded->unencoded_offset, fs_info->sectorsize)) in btrfs_do_encoded_write()
9792 return -EINVAL; in btrfs_do_encoded_write()
9794 num_bytes = ALIGN(encoded->len, fs_info->sectorsize); in btrfs_do_encoded_write()
9795 ram_bytes = ALIGN(encoded->unencoded_len, fs_info->sectorsize); in btrfs_do_encoded_write()
9796 end = start + num_bytes - 1; in btrfs_do_encoded_write()
9800 * sector-aligned. For convenience, we extend it with zeroes if it in btrfs_do_encoded_write()
9803 disk_num_bytes = ALIGN(orig_count, fs_info->sectorsize); in btrfs_do_encoded_write()
9807 return -ENOMEM; in btrfs_do_encoded_write()
9814 ret = -ENOMEM; in btrfs_do_encoded_write()
9820 ret = -EFAULT; in btrfs_do_encoded_write()
9824 memset(kaddr + bytes, 0, PAGE_SIZE - bytes); in btrfs_do_encoded_write()
9834 ret = invalidate_inode_pages2_range(inode->vfs_inode.i_mapping, in btrfs_do_encoded_write()
9842 !filemap_range_has_page(inode->vfs_inode.i_mapping, start, end)) in btrfs_do_encoded_write()
9851 * We don't use the higher-level delalloc space functions because our in btrfs_do_encoded_write()
9866 if (encoded->unencoded_len == encoded->len && in btrfs_do_encoded_write()
9867 encoded->unencoded_offset == 0 && in btrfs_do_encoded_write()
9868 can_cow_file_range_inline(inode, start, encoded->len, orig_count)) { in btrfs_do_encoded_write()
9869 ret = __cow_file_range_inline(inode, encoded->len, in btrfs_do_encoded_write()
9889 file_extent.offset = encoded->unencoded_offset; in btrfs_do_encoded_write()
9908 if (start + encoded->len > inode->vfs_inode.i_size) in btrfs_do_encoded_write()
9909 i_size_write(&inode->vfs_inode, start + encoded->len); in btrfs_do_encoded_write()
9945 iocb->ki_pos += encoded->len; in btrfs_do_encoded_write()
9958 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; in btrfs_add_swapfile_pin()
9965 return -ENOMEM; in btrfs_add_swapfile_pin()
9966 sp->ptr = ptr; in btrfs_add_swapfile_pin()
9967 sp->inode = inode; in btrfs_add_swapfile_pin()
9968 sp->is_block_group = is_block_group; in btrfs_add_swapfile_pin()
9969 sp->bg_extent_count = 1; in btrfs_add_swapfile_pin()
9971 spin_lock(&fs_info->swapfile_pins_lock); in btrfs_add_swapfile_pin()
9972 p = &fs_info->swapfile_pins.rb_node; in btrfs_add_swapfile_pin()
9976 if (sp->ptr < entry->ptr || in btrfs_add_swapfile_pin()
9977 (sp->ptr == entry->ptr && sp->inode < entry->inode)) { in btrfs_add_swapfile_pin()
9978 p = &(*p)->rb_left; in btrfs_add_swapfile_pin()
9979 } else if (sp->ptr > entry->ptr || in btrfs_add_swapfile_pin()
9980 (sp->ptr == entry->ptr && sp->inode > entry->inode)) { in btrfs_add_swapfile_pin()
9981 p = &(*p)->rb_right; in btrfs_add_swapfile_pin()
9984 entry->bg_extent_count++; in btrfs_add_swapfile_pin()
9985 spin_unlock(&fs_info->swapfile_pins_lock); in btrfs_add_swapfile_pin()
9990 rb_link_node(&sp->node, parent, p); in btrfs_add_swapfile_pin()
9991 rb_insert_color(&sp->node, &fs_info->swapfile_pins); in btrfs_add_swapfile_pin()
9992 spin_unlock(&fs_info->swapfile_pins_lock); in btrfs_add_swapfile_pin()
9999 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; in btrfs_free_swapfile_pins()
10003 spin_lock(&fs_info->swapfile_pins_lock); in btrfs_free_swapfile_pins()
10004 node = rb_first(&fs_info->swapfile_pins); in btrfs_free_swapfile_pins()
10008 if (sp->inode == inode) { in btrfs_free_swapfile_pins()
10009 rb_erase(&sp->node, &fs_info->swapfile_pins); in btrfs_free_swapfile_pins()
10010 if (sp->is_block_group) { in btrfs_free_swapfile_pins()
10011 btrfs_dec_block_group_swap_extents(sp->ptr, in btrfs_free_swapfile_pins()
10012 sp->bg_extent_count); in btrfs_free_swapfile_pins()
10013 btrfs_put_block_group(sp->ptr); in btrfs_free_swapfile_pins()
10019 spin_unlock(&fs_info->swapfile_pins_lock); in btrfs_free_swapfile_pins()
10045 if (bsi->nr_pages >= sis->max) in btrfs_add_swap_extent()
10048 max_pages = sis->max - bsi->nr_pages; in btrfs_add_swap_extent()
10049 first_ppage = PAGE_ALIGN(bsi->block_start) >> PAGE_SHIFT; in btrfs_add_swap_extent()
10050 next_ppage = PAGE_ALIGN_DOWN(bsi->block_start + bsi->block_len) >> PAGE_SHIFT; in btrfs_add_swap_extent()
10054 nr_pages = next_ppage - first_ppage; in btrfs_add_swap_extent()
10058 if (bsi->start == 0) in btrfs_add_swap_extent()
10060 if (bsi->lowest_ppage > first_ppage_reported) in btrfs_add_swap_extent()
10061 bsi->lowest_ppage = first_ppage_reported; in btrfs_add_swap_extent()
10062 if (bsi->highest_ppage < (next_ppage - 1)) in btrfs_add_swap_extent()
10063 bsi->highest_ppage = next_ppage - 1; in btrfs_add_swap_extent()
10065 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage); in btrfs_add_swap_extent()
10068 bsi->nr_extents += ret; in btrfs_add_swap_extent()
10069 bsi->nr_pages += nr_pages; in btrfs_add_swap_extent()
10078 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles); in btrfs_swap_deactivate()
10085 struct btrfs_root *root = BTRFS_I(inode)->root; in btrfs_swap_activate()
10086 struct btrfs_fs_info *fs_info = root->fs_info; in btrfs_swap_activate()
10087 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; in btrfs_swap_activate()
10092 .lowest_ppage = (sector_t)-1ULL, in btrfs_swap_activate()
10107 down_write(&BTRFS_I(inode)->i_mmap_lock); in btrfs_swap_activate()
10114 ret = btrfs_wait_ordered_range(BTRFS_I(inode), 0, (u64)-1); in btrfs_swap_activate()
10121 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) { in btrfs_swap_activate()
10123 ret = -EINVAL; in btrfs_swap_activate()
10126 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) { in btrfs_swap_activate()
10127 btrfs_warn(fs_info, "swapfile must not be copy-on-write"); in btrfs_swap_activate()
10128 ret = -EINVAL; in btrfs_swap_activate()
10131 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { in btrfs_swap_activate()
10133 ret = -EINVAL; in btrfs_swap_activate()
10140 ret = -ENOMEM; in btrfs_swap_activate()
10148 * fs_info->swapfile_pins prevents them from running while the swap in btrfs_swap_activate()
10156 ret = -EBUSY; in btrfs_swap_activate()
10167 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) { in btrfs_swap_activate()
10171 ret = -EINVAL; in btrfs_swap_activate()
10184 spin_lock(&root->root_item_lock); in btrfs_swap_activate()
10186 spin_unlock(&root->root_item_lock); in btrfs_swap_activate()
10188 btrfs_drew_write_unlock(&root->snapshot_lock); in btrfs_swap_activate()
10193 ret = -EPERM; in btrfs_swap_activate()
10196 atomic_inc(&root->nr_swapfiles); in btrfs_swap_activate()
10197 spin_unlock(&root->root_item_lock); in btrfs_swap_activate()
10199 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize); in btrfs_swap_activate()
10201 btrfs_lock_extent(io_tree, 0, isize - 1, &cached_state); in btrfs_swap_activate()
10227 ret = -EINVAL; in btrfs_swap_activate()
10231 leaf = path->nodes[0]; in btrfs_swap_activate()
10232 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item); in btrfs_swap_activate()
10243 ret = -EINVAL; in btrfs_swap_activate()
10249 ret = -EINVAL; in btrfs_swap_activate()
10256 ret = -EINVAL; in btrfs_swap_activate()
10265 len = isize - key.offset; in btrfs_swap_activate()
10269 backref_ctx->curr_leaf_bytenr = leaf->start; in btrfs_swap_activate()
10276 * delayed items for example. in btrfs_swap_activate()
10286 "swapfile must not be copy-on-write"); in btrfs_swap_activate()
10287 ret = -EINVAL; in btrfs_swap_activate()
10297 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { in btrfs_swap_activate()
10300 ret = -EINVAL; in btrfs_swap_activate()
10305 device = map->stripes[0].dev; in btrfs_swap_activate()
10311 } else if (device != map->stripes[0].dev) { in btrfs_swap_activate()
10313 ret = -EINVAL; in btrfs_swap_activate()
10317 physical_block_start = (map->stripes[0].physical + in btrfs_swap_activate()
10318 (logical_block_start - map->start)); in btrfs_swap_activate()
10326 ret = -EINVAL; in btrfs_swap_activate()
10332 "block group for swapfile at %llu is read-only%s", in btrfs_swap_activate()
10333 bg->start, in btrfs_swap_activate()
10334 atomic_read(&fs_info->scrubs_running) ? in btrfs_swap_activate()
10337 ret = -EINVAL; in btrfs_swap_activate()
10365 ret = -EINTR; in btrfs_swap_activate()
10379 btrfs_unlock_extent(io_tree, 0, isize - 1, &cached_state); in btrfs_swap_activate()
10384 btrfs_drew_write_unlock(&root->snapshot_lock); in btrfs_swap_activate()
10389 up_write(&BTRFS_I(inode)->i_mmap_lock); in btrfs_swap_activate()
10396 sis->bdev = device->bdev; in btrfs_swap_activate()
10397 *span = bsi.highest_ppage - bsi.lowest_ppage + 1; in btrfs_swap_activate()
10398 sis->max = bsi.nr_pages; in btrfs_swap_activate()
10399 sis->pages = bsi.nr_pages - 1; in btrfs_swap_activate()
10410 return -EOPNOTSUPP; in btrfs_swap_activate()
10427 spin_lock(&inode->lock); in btrfs_update_inode_bytes()
10429 inode_sub_bytes(&inode->vfs_inode, del_bytes); in btrfs_update_inode_bytes()
10431 inode_add_bytes(&inode->vfs_inode, add_bytes); in btrfs_update_inode_bytes()
10432 spin_unlock(&inode->lock); in btrfs_update_inode_bytes()
10451 struct btrfs_root *root = inode->root; in btrfs_assert_inode_range_clean()
10457 ordered = btrfs_lookup_first_ordered_range(inode, start, end + 1 - start); in btrfs_assert_inode_range_clean()
10459 btrfs_err(root->fs_info, in btrfs_assert_inode_range_clean()
10462 ordered->file_offset, in btrfs_assert_inode_range_clean()
10463 ordered->file_offset + ordered->num_bytes - 1); in btrfs_assert_inode_range_clean()
10484 xa_lock(&root->inodes); in btrfs_find_first_inode()
10486 inode = xa_find(&root->inodes, &from, ULONG_MAX, XA_PRESENT); in btrfs_find_first_inode()
10489 if (igrab(&inode->vfs_inode)) in btrfs_find_first_inode()
10493 cond_resched_lock(&root->inodes.xa_lock); in btrfs_find_first_inode()
10495 xa_unlock(&root->inodes); in btrfs_find_first_inode()