Lines Matching +full:page +full:- +full:offset

1 // SPDX-License-Identifier: GPL-2.0
16 #include "disk-io.h"
37 * [ inode objectid, BTRFS_VERITY_DESC_ITEM_KEY, offset ]
39 * At offset 0, we store a btrfs_verity_descriptor_item which tracks the
41 * Starting at offset 1, these hold the generic fs verity descriptor.
46 * [ inode objectid, BTRFS_VERITY_MERKLE_ITEM_KEY, offset ]
48 * These also start at offset 0, and correspond to the merkle tree bytes.
49 * So when fsverity asks for page 0 of the merkle tree, we pull up one page
50 * starting at offset 0 for this key type. These are also opaque to btrfs,
54 * linearly with the size of the file (with 4K pages/blocks and SHA-256, it's
66 * Compute the logical file offset where we cache the Merkle tree.
71 * fs-verity, it is convenient to do size computations in terms of a file
72 * offset, rather than in terms of page indices.
74 * Use 64K to be sure it's past the last page in the file, even with 64K pages.
78 * Returns the file offset on success, negative error code on failure.
82 u64 sz = inode->i_size; in merkle_file_pos()
85 if (rounded > inode->i_sb->s_maxbytes) in merkle_file_pos()
86 return -EFBIG; in merkle_file_pos()
106 struct btrfs_root *root = inode->root; in drop_verity_items()
114 return -ENOMEM; in drop_verity_items()
130 key.offset = (u64)-1; in drop_verity_items()
132 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); in drop_verity_items()
136 if (path->slots[0] == 0) in drop_verity_items()
138 path->slots[0]--; in drop_verity_items()
144 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); in drop_verity_items()
156 ret = btrfs_del_items(trans, root, path, path->slots[0], 1); in drop_verity_items()
197 * Insert and write inode items with a given key type and offset.
201 * @offset: item offset to insert at
206 * The inserted items will have key (ino, key_type, offset + off) where off is
207 * consecutively increasing from 0 up to the last item ending at offset + len.
211 static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, in write_key_bytes() argument
216 struct btrfs_root *root = inode->root; in write_key_bytes()
226 return -ENOMEM; in write_key_bytes()
238 key.offset = offset; in write_key_bytes()
252 leaf = path->nodes[0]; in write_key_bytes()
254 data = btrfs_item_ptr(leaf, path->slots[0], void); in write_key_bytes()
257 offset += copy_bytes; in write_key_bytes()
259 len -= copy_bytes; in write_key_bytes()
270 * Read inode items of the given key type and offset from the btree.
274 * @offset: item offset to read from
279 * page and ignore dest, but it must still be non-NULL to avoid the
280 * counting-only behavior.
282 * @dest_page: copy into this page instead of the dest buffer
287 * reading into a provided buffer (dest) or into the page cache
291 static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset, in read_key_bytes() argument
292 char *dest, u64 len, struct page *dest_page) in read_key_bytes()
295 struct btrfs_root *root = inode->root; in read_key_bytes()
310 return -ENOMEM; in read_key_bytes()
313 path->reada = READA_FORWARD; in read_key_bytes()
317 key.offset = offset; in read_key_bytes()
324 if (path->slots[0] == 0) in read_key_bytes()
326 path->slots[0]--; in read_key_bytes()
330 leaf = path->nodes[0]; in read_key_bytes()
331 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); in read_key_bytes()
336 item_end = btrfs_item_size(leaf, path->slots[0]) + key.offset; in read_key_bytes()
343 if (key.offset != offset) in read_key_bytes()
347 * Our initial offset might be in the middle of an in read_key_bytes()
350 if (key.offset > offset) in read_key_bytes()
352 if (item_end <= offset) in read_key_bytes()
360 copy_end = min(offset + len, item_end); in read_key_bytes()
363 copy_bytes = copy_end - offset; in read_key_bytes()
365 /* Offset from the start of item for copying */ in read_key_bytes()
366 copy_offset = offset - key.offset; in read_key_bytes()
372 data = btrfs_item_ptr(leaf, path->slots[0], void); in read_key_bytes()
381 offset += copy_bytes; in read_key_bytes()
383 len -= copy_bytes; in read_key_bytes()
386 path->slots[0]++; in read_key_bytes()
387 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { in read_key_bytes()
422 struct btrfs_root *root = inode->root; in del_orphan()
432 if (!inode->vfs_inode.i_nlink) in del_orphan()
436 if (ret == -ENOENT) in del_orphan()
442 * Rollback in-progress verity if we encounter an error.
455 struct btrfs_root *root = inode->root; in rollback_verity()
458 ASSERT(inode_is_locked(&inode->vfs_inode)); in rollback_verity()
459 truncate_inode_pages(inode->vfs_inode.i_mapping, inode->vfs_inode.i_size); in rollback_verity()
460 clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags); in rollback_verity()
463 btrfs_handle_fs_error(root->fs_info, ret, in rollback_verity()
465 (u64)inode->vfs_inode.i_ino); in rollback_verity()
477 btrfs_handle_fs_error(root->fs_info, ret, in rollback_verity()
479 (u64)inode->vfs_inode.i_ino); in rollback_verity()
482 inode->ro_flags &= ~BTRFS_INODE_RO_VERITY; in rollback_verity()
483 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode); in rollback_verity()
510 * - write out the descriptor items
511 * - mark the inode with the verity flag
512 * - delete the orphan item
513 * - mark the ro compat bit
514 * - clear the in progress bit
522 struct btrfs_root *root = inode->root; in finish_verity()
549 inode->ro_flags |= BTRFS_INODE_RO_VERITY; in finish_verity()
550 btrfs_sync_inode_flags_to_i_flags(&inode->vfs_inode); in finish_verity()
557 clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags); in finish_verity()
558 btrfs_set_fs_compat_ro(root->fs_info, VERITY); in finish_verity()
579 struct btrfs_root *root = inode->root; in btrfs_begin_enable_verity()
585 if (test_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags)) in btrfs_begin_enable_verity()
586 return -EBUSY; in btrfs_begin_enable_verity()
605 set_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags); in btrfs_begin_enable_verity()
644 btrfs_err(inode->root->fs_info, in btrfs_end_enable_verity()
675 return -EUCLEAN; in btrfs_get_verity_descriptor()
679 return -EUCLEAN; in btrfs_get_verity_descriptor()
684 return -ERANGE; in btrfs_get_verity_descriptor()
691 return -EIO; in btrfs_get_verity_descriptor()
697 * fsverity op that reads and caches a merkle tree page.
699 * @inode: inode to read a merkle tree page for
700 * @index: page index relative to the start of the merkle tree
706 * Returns the page we read, or an ERR_PTR on error.
708 static struct page *btrfs_read_merkle_tree_page(struct inode *inode, in btrfs_read_merkle_tree_page()
712 struct page *page; in btrfs_read_merkle_tree_page() local
719 if (merkle_pos > inode->i_sb->s_maxbytes - off - PAGE_SIZE) in btrfs_read_merkle_tree_page()
720 return ERR_PTR(-EFBIG); in btrfs_read_merkle_tree_page()
723 page = find_get_page_flags(inode->i_mapping, index, FGP_ACCESSED); in btrfs_read_merkle_tree_page()
724 if (page) { in btrfs_read_merkle_tree_page()
725 if (PageUptodate(page)) in btrfs_read_merkle_tree_page()
726 return page; in btrfs_read_merkle_tree_page()
728 lock_page(page); in btrfs_read_merkle_tree_page()
733 if (!PageUptodate(page)) { in btrfs_read_merkle_tree_page()
734 unlock_page(page); in btrfs_read_merkle_tree_page()
735 put_page(page); in btrfs_read_merkle_tree_page()
736 return ERR_PTR(-EIO); in btrfs_read_merkle_tree_page()
738 unlock_page(page); in btrfs_read_merkle_tree_page()
739 return page; in btrfs_read_merkle_tree_page()
742 page = __page_cache_alloc(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS)); in btrfs_read_merkle_tree_page()
743 if (!page) in btrfs_read_merkle_tree_page()
744 return ERR_PTR(-ENOMEM); in btrfs_read_merkle_tree_page()
750 * [ inode objectid, BTRFS_MERKLE_ITEM_KEY, offset in bytes ] in btrfs_read_merkle_tree_page()
753 page_address(page), PAGE_SIZE, page); in btrfs_read_merkle_tree_page()
755 put_page(page); in btrfs_read_merkle_tree_page()
759 memzero_page(page, ret, PAGE_SIZE - ret); in btrfs_read_merkle_tree_page()
761 SetPageUptodate(page); in btrfs_read_merkle_tree_page()
762 ret = add_to_page_cache_lru(page, inode->i_mapping, index, GFP_NOFS); in btrfs_read_merkle_tree_page()
766 unlock_page(page); in btrfs_read_merkle_tree_page()
768 put_page(page); in btrfs_read_merkle_tree_page()
769 /* Did someone race us into inserting this page? */ in btrfs_read_merkle_tree_page()
770 if (ret == -EEXIST) in btrfs_read_merkle_tree_page()
772 page = ERR_PTR(ret); in btrfs_read_merkle_tree_page()
774 return page; in btrfs_read_merkle_tree_page()
785 * Note that the block size could be different from the page size, so it is not
786 * safe to assume that index is a page index.
799 if (merkle_pos > inode->i_sb->s_maxbytes - off - len) in btrfs_write_merkle_tree_block()
800 return -EFBIG; in btrfs_write_merkle_tree_block()