xref: /linux/fs/btrfs/verity.c (revision fa4ad798f6f681031775266044ba1b0cf3c670bd)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/init.h>
4 #include <linux/fs.h>
5 #include <linux/slab.h>
6 #include <linux/rwsem.h>
7 #include <linux/xattr.h>
8 #include <linux/security.h>
9 #include <linux/posix_acl_xattr.h>
10 #include <linux/iversion.h>
11 #include <linux/fsverity.h>
12 #include <linux/sched/mm.h>
13 #include "messages.h"
14 #include "ctree.h"
15 #include "btrfs_inode.h"
16 #include "transaction.h"
17 #include "locking.h"
18 #include "fs.h"
19 #include "accessors.h"
20 #include "ioctl.h"
21 #include "verity.h"
22 #include "orphan.h"
23 
24 /*
25  * Implementation of the interface defined in struct fsverity_operations.
26  *
27  * The main question is how and where to store the verity descriptor and the
28  * Merkle tree. We store both in dedicated btree items in the filesystem tree,
29  * together with the rest of the inode metadata. This means we'll need to do
30  * extra work to encrypt them once encryption is supported in btrfs, but btrfs
31  * has a lot of careful code around i_size and it seems better to make a new key
32  * type than try and adjust all of our expectations for i_size.
33  *
34  * Note that this differs from the implementation in ext4 and f2fs, where
35  * this data is stored as if it were in the file, but past EOF. However, btrfs
36  * does not have a widespread mechanism for caching opaque metadata pages, so we
37  * do pretend that the Merkle tree pages themselves are past EOF for the
38  * purposes of caching them (as opposed to creating a virtual inode).
39  *
40  * fs verity items are stored under two different key types on disk.
41  * The descriptor items:
42  * [ inode objectid, BTRFS_VERITY_DESC_ITEM_KEY, offset ]
43  *
44  * At offset 0, we store a btrfs_verity_descriptor_item which tracks the
45  * size of the descriptor item and some extra data for encryption.
46  * Starting at offset 1, these hold the generic fs verity descriptor.
47  * The latter are opaque to btrfs, we just read and write them as a blob for
48  * the higher level verity code.  The most common descriptor size is 256 bytes.
49  *
50  * The merkle tree items:
51  * [ inode objectid, BTRFS_VERITY_MERKLE_ITEM_KEY, offset ]
52  *
53  * These also start at offset 0, and correspond to the merkle tree bytes.
54  * So when fsverity asks for page 0 of the merkle tree, we pull up one page
55  * starting at offset 0 for this key type.  These are also opaque to btrfs,
56  * we're blindly storing whatever fsverity sends down.
57  *
58  * Another important consideration is the fact that the Merkle tree data scales
59  * linearly with the size of the file (with 4K pages/blocks and SHA-256, it's
60  * ~1/127th the size) so for large files, writing the tree can be a lengthy
61  * operation. For that reason, we guard the whole enable verity operation
62  * (between begin_enable_verity and end_enable_verity) with an orphan item.
63  * Again, because the data can be pretty large, it's quite possible that we
64  * could run out of space writing it, so we try our best to handle errors by
65  * stopping and rolling back rather than aborting the victim transaction.
66  */
67 
68 #define MERKLE_START_ALIGN			65536
69 
70 /*
71  * Compute the logical file offset where we cache the Merkle tree.
72  *
73  * @inode:  inode of the verity file
74  *
75  * For the purposes of caching the Merkle tree pages, as required by
76  * fs-verity, it is convenient to do size computations in terms of a file
77  * offset, rather than in terms of page indices.
78  *
79  * Use 64K to be sure it's past the last page in the file, even with 64K pages.
80  * That rounding operation itself can overflow loff_t, so we do it in u64 and
81  * check.
82  *
83  * Returns the file offset on success, negative error code on failure.
84  */
85 static loff_t merkle_file_pos(const struct inode *inode)
86 {
87 	u64 sz = inode->i_size;
88 	u64 rounded = round_up(sz, MERKLE_START_ALIGN);
89 
90 	if (rounded > inode->i_sb->s_maxbytes)
91 		return -EFBIG;
92 
93 	return rounded;
94 }
95 
96 /*
97  * Start a transaction for removing verity items or the verity orphan.
98  *
99  * Like unlink, this only deletes items and frees space in the end, so the
100  * reservation may come from the global reserve when the filesystem is full
101  * (-ENOSPC) and is not subject to the qgroup limit (-EDQUOT). Otherwise a
102  * failed enable could never be cleaned up in either situation.
103  */
104 static struct btrfs_trans_handle *start_verity_cleanup_trans(struct btrfs_root *root,
105 							     unsigned int num_items)
106 {
107 	return btrfs_start_transaction_fallback_global_rsv(root, num_items);
108 }
109 
110 /*
111  * Drop all the items for this inode with this key_type.
112  *
113  * @inode:     inode to drop items for
114  * @key_type:  type of items to drop (BTRFS_VERITY_DESC_ITEM or
115  *             BTRFS_VERITY_MERKLE_ITEM)
116  *
117  * Before doing a verity enable we cleanup any existing verity items.
118  * This is also used to clean up if a verity enable failed half way through.
119  *
120  * Returns number of dropped items on success, negative error code on failure.
121  */
122 static int drop_verity_items(struct btrfs_inode *inode, u8 key_type)
123 {
124 	struct btrfs_trans_handle *trans;
125 	struct btrfs_root *root = inode->root;
126 	BTRFS_PATH_AUTO_FREE(path);
127 	struct btrfs_key key;
128 	int count = 0;
129 	int ret;
130 
131 	path = btrfs_alloc_path();
132 	if (!path)
133 		return -ENOMEM;
134 
135 	while (1) {
136 		/* 1 for the item being dropped */
137 		trans = start_verity_cleanup_trans(root, 1);
138 		if (IS_ERR(trans))
139 			return PTR_ERR(trans);
140 
141 		/*
142 		 * Walk backwards through all the items until we find one that
143 		 * isn't from our key type or objectid
144 		 */
145 		key.objectid = btrfs_ino(inode);
146 		key.type = key_type;
147 		key.offset = (u64)-1;
148 
149 		ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
150 		if (ret > 0) {
151 			ret = 0;
152 			/* No more keys of this type, we're done */
153 			if (path->slots[0] == 0)
154 				break;
155 			path->slots[0]--;
156 		} else if (ret < 0) {
157 			btrfs_end_transaction(trans);
158 			return ret;
159 		}
160 
161 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
162 
163 		/* No more keys of this type, we're done */
164 		if (key.objectid != btrfs_ino(inode) || key.type != key_type)
165 			break;
166 
167 		/*
168 		 * This shouldn't be a performance sensitive function because
169 		 * it's not used as part of truncate.  If it ever becomes
170 		 * perf sensitive, change this to walk forward and bulk delete
171 		 * items
172 		 */
173 		ret = btrfs_del_items(trans, root, path, path->slots[0], 1);
174 		if (ret) {
175 			btrfs_end_transaction(trans);
176 			return ret;
177 		}
178 		count++;
179 		btrfs_release_path(path);
180 		btrfs_end_transaction(trans);
181 	}
182 	btrfs_end_transaction(trans);
183 	return count;
184 }
185 
186 /*
187  * Drop all verity items
188  *
189  * @inode:  inode to drop verity items for
190  *
191  * In most contexts where we are dropping verity items, we want to do it for all
192  * the types of verity items, not a particular one.
193  *
194  * Returns: 0 on success, negative error code on failure.
195  */
196 int btrfs_drop_verity_items(struct btrfs_inode *inode)
197 {
198 	int ret;
199 
200 	ret = drop_verity_items(inode, BTRFS_VERITY_DESC_ITEM_KEY);
201 	if (ret < 0)
202 		return ret;
203 	ret = drop_verity_items(inode, BTRFS_VERITY_MERKLE_ITEM_KEY);
204 	if (ret < 0)
205 		return ret;
206 
207 	return 0;
208 }
209 
210 /*
211  * Insert and write inode items with a given key type and offset.
212  *
213  * @inode:     inode to insert for
214  * @key_type:  key type to insert
215  * @offset:    item offset to insert at
216  * @src:       source data to write
217  * @len:       length of source data to write
218  *
219  * Write len bytes from src into items of up to 2K length.
220  * The inserted items will have key (ino, key_type, offset + off) where off is
221  * consecutively increasing from 0 up to the last item ending at offset + len.
222  *
223  * Returns 0 on success and a negative error code on failure.
224  */
225 static int write_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
226 			   const char *src, u64 len)
227 {
228 	struct btrfs_trans_handle *trans;
229 	BTRFS_PATH_AUTO_FREE(path);
230 	struct btrfs_root *root = inode->root;
231 	struct extent_buffer *leaf;
232 	struct btrfs_key key;
233 	unsigned long copy_bytes;
234 	unsigned long src_offset = 0;
235 	void *data;
236 	int ret = 0;
237 
238 	path = btrfs_alloc_path();
239 	if (!path)
240 		return -ENOMEM;
241 
242 	while (len > 0) {
243 		/* 1 for the new item being inserted */
244 		trans = btrfs_start_transaction(root, 1);
245 		if (IS_ERR(trans))
246 			return PTR_ERR(trans);
247 
248 		key.objectid = btrfs_ino(inode);
249 		key.type = key_type;
250 		key.offset = offset;
251 
252 		/*
253 		 * Insert 2K at a time mostly to be friendly for smaller leaf
254 		 * size filesystems
255 		 */
256 		copy_bytes = min_t(u64, len, 2048);
257 
258 		ret = btrfs_insert_empty_item(trans, root, path, &key, copy_bytes);
259 		if (ret) {
260 			btrfs_end_transaction(trans);
261 			break;
262 		}
263 
264 		leaf = path->nodes[0];
265 
266 		data = btrfs_item_ptr(leaf, path->slots[0], void);
267 		write_extent_buffer(leaf, src + src_offset,
268 				    (unsigned long)data, copy_bytes);
269 		offset += copy_bytes;
270 		src_offset += copy_bytes;
271 		len -= copy_bytes;
272 
273 		btrfs_release_path(path);
274 		btrfs_end_transaction(trans);
275 	}
276 
277 	return ret;
278 }
279 
280 /*
281  * Read inode items of the given key type and offset from the btree.
282  *
283  * @inode:      inode to read items of
284  * @key_type:   key type to read
285  * @offset:     item offset to read from
286  * @dest:       Buffer to read into. This parameter has slightly tricky
287  *              semantics.  If it is NULL, the function will not do any copying
288  *              and will just return the size of all the items up to len bytes.
289  * @len:        length in bytes to read
290  *
291  * Helper function to read items from the btree.  This returns the number of
292  * bytes read or < 0 for errors.  We can return short reads if the items don't
293  * exist on disk or aren't big enough to fill the desired length.  Supports
294  * reading into a provided buffer (dest).
295  *
296  * Returns number of bytes read or a negative error code on failure.
297  */
298 static int read_key_bytes(struct btrfs_inode *inode, u8 key_type, u64 offset,
299 			  char *dest, u64 len)
300 {
301 	BTRFS_PATH_AUTO_FREE(path);
302 	struct btrfs_root *root = inode->root;
303 	struct extent_buffer *leaf;
304 	struct btrfs_key key;
305 	u64 item_end;
306 	u64 copy_end;
307 	int copied = 0;
308 	u32 copy_offset;
309 	unsigned long copy_bytes;
310 	unsigned long dest_offset = 0;
311 	void *data;
312 	char *kaddr = dest;
313 	int ret;
314 
315 	path = btrfs_alloc_path();
316 	if (!path)
317 		return -ENOMEM;
318 
319 	/*
320 	 * Merkle items can be large and split across multiple items, so enable
321 	 * readahead for such cases.
322 	 */
323 	if (key_type == BTRFS_VERITY_MERKLE_ITEM_KEY)
324 		path->reada = READA_FORWARD;
325 
326 	key.objectid = btrfs_ino(inode);
327 	key.type = key_type;
328 	key.offset = offset;
329 
330 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
331 	if (ret < 0) {
332 		goto out;
333 	} else if (ret > 0) {
334 		ret = 0;
335 		if (path->slots[0] == 0)
336 			goto out;
337 		path->slots[0]--;
338 	}
339 
340 	while (len > 0) {
341 		leaf = path->nodes[0];
342 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
343 
344 		if (key.objectid != btrfs_ino(inode) || key.type != key_type)
345 			break;
346 
347 		item_end = btrfs_item_size(leaf, path->slots[0]) + key.offset;
348 
349 		if (copied > 0) {
350 			/*
351 			 * Once we've copied something, we want all of the items
352 			 * to be sequential
353 			 */
354 			if (key.offset != offset)
355 				break;
356 		} else {
357 			/*
358 			 * Our initial offset might be in the middle of an
359 			 * item.  Make sure it all makes sense.
360 			 */
361 			if (key.offset > offset)
362 				break;
363 			if (item_end <= offset)
364 				break;
365 		}
366 
367 		/* dest == NULL to just sum all the item lengths */
368 		if (!dest)
369 			copy_end = item_end;
370 		else
371 			copy_end = min(offset + len, item_end);
372 
373 		/* Number of bytes in this item we want to copy */
374 		copy_bytes = copy_end - offset;
375 
376 		/* Offset from the start of item for copying */
377 		copy_offset = offset - key.offset;
378 
379 		if (dest) {
380 			data = btrfs_item_ptr(leaf, path->slots[0], void);
381 			read_extent_buffer(leaf, kaddr + dest_offset,
382 					   (unsigned long)data + copy_offset,
383 					   copy_bytes);
384 		}
385 
386 		offset += copy_bytes;
387 		dest_offset += copy_bytes;
388 		len -= copy_bytes;
389 		copied += copy_bytes;
390 
391 		path->slots[0]++;
392 		if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
393 			/*
394 			 * We've reached the last slot in this leaf and we need
395 			 * to go to the next leaf.
396 			 */
397 			ret = btrfs_next_leaf(root, path);
398 			if (ret < 0) {
399 				break;
400 			} else if (ret > 0) {
401 				ret = 0;
402 				break;
403 			}
404 		}
405 	}
406 out:
407 	if (!ret)
408 		ret = copied;
409 	return ret;
410 }
411 
412 /*
413  * Delete an fsverity orphan
414  *
415  * @trans:  transaction to do the delete in
416  * @inode:  inode to orphan
417  *
418  * Capture verity orphan specific logic that is repeated in the couple places
419  * we delete verity orphans. Specifically, handling ENOENT and ignoring inodes
420  * with 0 links.
421  *
422  * Returns zero on success or a negative error code on failure.
423  */
424 static int del_orphan(struct btrfs_trans_handle *trans, struct btrfs_inode *inode)
425 {
426 	struct btrfs_root *root = inode->root;
427 	int ret;
428 
429 	/*
430 	 * If the inode has no links, it is either already unlinked, or was
431 	 * created with O_TMPFILE. In either case, it should have an orphan from
432 	 * that other operation. Rather than reference count the orphans, we
433 	 * simply ignore them here, because we only invoke the verity path in
434 	 * the orphan logic when i_nlink is 1.
435 	 */
436 	if (!inode->vfs_inode.i_nlink)
437 		return 0;
438 
439 	ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
440 	if (ret == -ENOENT)
441 		ret = 0;
442 	return ret;
443 }
444 
445 /*
446  * Rollback in-progress verity if we encounter an error.
447  *
448  * @inode:  inode verity had an error for
449  *
450  * We try to handle recoverable errors while enabling verity by rolling it back
451  * and just failing the operation, rather than having an fs level error no
452  * matter what. However, any error in rollback is unrecoverable.
453  *
454  * Returns 0 on success, negative error code on failure.
455  */
456 static int rollback_verity(struct btrfs_inode *inode)
457 {
458 	struct btrfs_trans_handle *trans = NULL;
459 	struct btrfs_root *root = inode->root;
460 	int ret;
461 
462 	btrfs_assert_inode_locked(inode);
463 	truncate_inode_pages(inode->vfs_inode.i_mapping, inode->vfs_inode.i_size);
464 	clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
465 	ret = btrfs_drop_verity_items(inode);
466 	if (ret) {
467 		btrfs_handle_fs_error(root->fs_info, ret,
468 				"failed to drop verity items in rollback %llu",
469 				inode->vfs_inode.i_ino);
470 		goto out;
471 	}
472 
473 	/*
474 	 * 1 for updating the inode flag
475 	 * 1 for deleting the orphan
476 	 */
477 	trans = start_verity_cleanup_trans(root, 2);
478 	if (IS_ERR(trans)) {
479 		ret = PTR_ERR(trans);
480 		trans = NULL;
481 		btrfs_handle_fs_error(root->fs_info, ret,
482 			"failed to start transaction in verity rollback %llu",
483 			inode->vfs_inode.i_ino);
484 		goto out;
485 	}
486 	ret = btrfs_update_inode(trans, inode);
487 	if (unlikely(ret)) {
488 		btrfs_abort_transaction(trans, ret);
489 		goto out;
490 	}
491 	ret = del_orphan(trans, inode);
492 	if (unlikely(ret)) {
493 		btrfs_abort_transaction(trans, ret);
494 		goto out;
495 	}
496 out:
497 	if (trans)
498 		btrfs_end_transaction(trans);
499 	return ret;
500 }
501 
502 /*
503  * Finalize making the file a valid verity file
504  *
505  * @inode:      inode to be marked as verity
506  * @desc:       contents of the verity descriptor to write (not NULL)
507  * @desc_size:  size of the verity descriptor
508  *
509  * Do the actual work of finalizing verity after successfully writing the Merkle
510  * tree:
511  *
512  * - write out the descriptor items
513  * - mark the inode with the verity flag
514  * - delete the orphan item
515  * - mark the ro compat bit
516  * - clear the in progress bit
517  *
518  * Returns 0 on success, negative error code on failure.
519  */
520 static int finish_verity(struct btrfs_inode *inode, const void *desc,
521 			 size_t desc_size)
522 {
523 	struct btrfs_trans_handle *trans = NULL;
524 	struct btrfs_root *root = inode->root;
525 	struct btrfs_verity_descriptor_item item;
526 	int ret;
527 
528 	/* Write out the descriptor item */
529 	memset(&item, 0, sizeof(item));
530 	btrfs_set_stack_verity_descriptor_size(&item, desc_size);
531 	ret = write_key_bytes(inode, BTRFS_VERITY_DESC_ITEM_KEY, 0,
532 			      (const char *)&item, sizeof(item));
533 	if (ret)
534 		return ret;
535 
536 	/* Write out the descriptor itself */
537 	ret = write_key_bytes(inode, BTRFS_VERITY_DESC_ITEM_KEY, 1,
538 			      desc, desc_size);
539 	if (ret)
540 		return ret;
541 
542 	/*
543 	 * 1 for updating the inode flag
544 	 * 1 for deleting the orphan
545 	 */
546 	trans = btrfs_start_transaction(root, 2);
547 	if (IS_ERR(trans))
548 		return PTR_ERR(trans);
549 	inode->ro_flags |= BTRFS_INODE_RO_VERITY;
550 	ret = btrfs_update_inode(trans, inode);
551 	if (ret)
552 		goto end_trans;
553 	ret = del_orphan(trans, inode);
554 	if (ret)
555 		goto end_trans;
556 	/* Now that rollback is no longer possible, set S_VERITY. */
557 	btrfs_sync_inode_flags_to_i_flags(inode);
558 	clear_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
559 	btrfs_set_fs_compat_ro(root->fs_info, VERITY);
560 end_trans:
561 	if (ret)
562 		inode->ro_flags &= ~BTRFS_INODE_RO_VERITY;
563 	btrfs_end_transaction(trans);
564 	return ret;
565 
566 }
567 
568 /*
569  * fsverity op that begins enabling verity.
570  *
571  * @filp:  file to enable verity on
572  *
573  * Begin enabling fsverity for the file. We drop any existing verity items, add
574  * an orphan and set the in progress bit.
575  *
576  * Returns 0 on success, negative error code on failure.
577  */
578 static int btrfs_begin_enable_verity(struct file *filp)
579 {
580 	struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
581 	struct btrfs_root *root = inode->root;
582 	struct btrfs_trans_handle *trans;
583 	int ret;
584 
585 	btrfs_assert_inode_locked(inode);
586 
587 	if (IS_ENCRYPTED(&inode->vfs_inode))
588 		return -EOPNOTSUPP;
589 
590 	if (test_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags))
591 		return -EBUSY;
592 
593 	/*
594 	 * This should almost never do anything, but theoretically, it's
595 	 * possible that we failed to enable verity on a file, then were
596 	 * interrupted or failed while rolling back, failed to cleanup the
597 	 * orphan, and finally attempt to enable verity again.
598 	 */
599 	ret = btrfs_drop_verity_items(inode);
600 	if (ret)
601 		return ret;
602 
603 	/* 1 for the orphan item */
604 	trans = btrfs_start_transaction(root, 1);
605 	if (IS_ERR(trans))
606 		return PTR_ERR(trans);
607 
608 	ret = btrfs_orphan_add(trans, inode);
609 	if (!ret)
610 		set_bit(BTRFS_INODE_VERITY_IN_PROGRESS, &inode->runtime_flags);
611 	btrfs_end_transaction(trans);
612 
613 	return ret;
614 }
615 
616 /*
617  * fsverity op that ends enabling verity.
618  *
619  * @filp:              file we are finishing enabling verity on
620  * @desc:              verity descriptor to write out (NULL in error conditions)
621  * @desc_size:         size of the verity descriptor (variable with signatures)
622  * @merkle_tree_size:  size of the merkle tree in bytes
623  *
624  * If desc is null, then VFS is signaling an error occurred during verity
625  * enable, and we should try to rollback. Otherwise, attempt to finish verity.
626  *
627  * Returns 0 on success, negative error code on error.
628  */
629 static int btrfs_end_enable_verity(struct file *filp, const void *desc,
630 				   size_t desc_size, u64 merkle_tree_size)
631 {
632 	struct btrfs_inode *inode = BTRFS_I(file_inode(filp));
633 	int ret = 0;
634 	int rollback_ret;
635 
636 	btrfs_assert_inode_locked(inode);
637 
638 	if (desc == NULL)
639 		goto rollback;
640 
641 	ret = finish_verity(inode, desc, desc_size);
642 	if (ret)
643 		goto rollback;
644 	return ret;
645 
646 rollback:
647 	rollback_ret = rollback_verity(inode);
648 	if (rollback_ret)
649 		btrfs_err(inode->root->fs_info,
650 			  "failed to rollback verity items: %pe", ERR_PTR(rollback_ret));
651 	return ret;
652 }
653 
654 /*
655  * fsverity op that gets the struct fsverity_descriptor.
656  *
657  * @inode:     inode to get the descriptor of
658  * @buf:       output buffer for the descriptor contents
659  * @buf_size:  size of the output buffer. 0 to query the size
660  *
661  * fsverity does a two pass setup for reading the descriptor, in the first pass
662  * it calls with buf_size = 0 to query the size of the descriptor, and then in
663  * the second pass it actually reads the descriptor off disk.
664  *
665  * Returns the size on success or a negative error code on failure.
666  */
667 int btrfs_get_verity_descriptor(struct inode *inode, void *buf, size_t buf_size)
668 {
669 	u64 true_size;
670 	int ret = 0;
671 	struct btrfs_verity_descriptor_item item;
672 
673 	memset(&item, 0, sizeof(item));
674 	ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_DESC_ITEM_KEY, 0,
675 			     (char *)&item, sizeof(item));
676 	if (ret < 0)
677 		return ret;
678 
679 	if (unlikely(item.reserved[0] != 0 || item.reserved[1] != 0))
680 		return -EUCLEAN;
681 
682 	true_size = btrfs_stack_verity_descriptor_size(&item);
683 	if (unlikely(true_size > INT_MAX))
684 		return -EUCLEAN;
685 
686 	if (buf_size == 0)
687 		return true_size;
688 	if (buf_size < true_size)
689 		return -ERANGE;
690 
691 	ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_DESC_ITEM_KEY, 1,
692 			     buf, buf_size);
693 	if (ret < 0)
694 		return ret;
695 	if (ret != true_size)
696 		return -EIO;
697 
698 	return true_size;
699 }
700 
701 /*
702  * fsverity op that reads and caches a merkle tree page.
703  *
704  * @inode:         inode to read a merkle tree page for
705  * @index:         page index relative to the start of the merkle tree
706  *
707  * The Merkle tree is stored in the filesystem btree, but its pages are cached
708  * with a logical position past EOF in the inode's mapping.
709  *
710  * Returns the page we read, or an ERR_PTR on error.
711  */
712 static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
713 						pgoff_t index)
714 {
715 	struct folio *folio;
716 	u64 off = (u64)index << PAGE_SHIFT;
717 	loff_t merkle_pos = merkle_file_pos(inode);
718 	void *kaddr;
719 	int ret;
720 
721 	if (merkle_pos < 0)
722 		return ERR_PTR(merkle_pos);
723 	if (merkle_pos > inode->i_sb->s_maxbytes - off - PAGE_SIZE)
724 		return ERR_PTR(-EFBIG);
725 	index += merkle_pos >> PAGE_SHIFT;
726 again:
727 	folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0);
728 	if (!IS_ERR(folio)) {
729 		if (folio_test_uptodate(folio))
730 			goto out;
731 
732 		folio_lock(folio);
733 		/* Folio was truncated from mapping. */
734 		if (!folio->mapping) {
735 			folio_unlock(folio);
736 			folio_put(folio);
737 			goto again;
738 		}
739 		/* Another reader may have filled the folio while we waited. */
740 		if (folio_test_uptodate(folio)) {
741 			folio_unlock(folio);
742 			goto out;
743 		}
744 		goto read_folio;
745 	}
746 
747 	folio = filemap_alloc_folio(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS),
748 				    0, NULL);
749 	if (!folio)
750 		return ERR_PTR(-ENOMEM);
751 
752 	ret = filemap_add_folio(inode->i_mapping, folio, index, GFP_NOFS);
753 	if (ret) {
754 		folio_put(folio);
755 		/* Did someone else insert a folio here? */
756 		if (ret == -EEXIST)
757 			goto again;
758 		return ERR_PTR(ret);
759 	}
760 
761 read_folio:
762 	kaddr = kmap_local_folio(folio, 0);
763 	/*
764 	 * Merkle item keys are indexed from byte 0 in the merkle tree.
765 	 * They have the form:
766 	 *
767 	 * [ inode objectid, BTRFS_MERKLE_ITEM_KEY, offset in bytes ]
768 	 */
769 	ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY, off,
770 			     kaddr, PAGE_SIZE);
771 	kunmap_local(kaddr);
772 	if (ret < 0) {
773 		folio_unlock(folio);
774 		folio_put(folio);
775 		return ERR_PTR(ret);
776 	}
777 	if (ret < PAGE_SIZE)
778 		folio_zero_segment(folio, ret, PAGE_SIZE);
779 
780 	folio_mark_uptodate(folio);
781 	folio_unlock(folio);
782 
783 out:
784 	return folio_file_page(folio, index);
785 }
786 
787 /*
788  * fsverity op that writes a Merkle tree block into the btree.
789  *
790  * @file:	file to write a Merkle tree block for
791  * @buf:	Merkle tree block to write
792  * @pos:	the position of the block in the Merkle tree (in bytes)
793  * @size:	the Merkle tree block size (in bytes)
794  * @zero_digest:	the hash of the all-zeroes block
795  * @digest_size:	size of zero_digest, in bytes
796  *
797  * Returns 0 on success or negative error code on failure
798  */
799 static int btrfs_write_merkle_tree_block(struct file *file, const void *buf,
800 					 u64 pos, unsigned int size,
801 					 const u8 *zero_digest,
802 					 unsigned int digest_size)
803 {
804 	struct inode *inode = file_inode(file);
805 	loff_t merkle_pos = merkle_file_pos(inode);
806 
807 	if (merkle_pos < 0)
808 		return merkle_pos;
809 	if (merkle_pos > inode->i_sb->s_maxbytes - pos - size)
810 		return -EFBIG;
811 
812 	return write_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY,
813 			       pos, buf, size);
814 }
815 
816 const struct fsverity_operations btrfs_verityops = {
817 	.begin_enable_verity     = btrfs_begin_enable_verity,
818 	.end_enable_verity       = btrfs_end_enable_verity,
819 	.get_verity_descriptor   = btrfs_get_verity_descriptor,
820 	.read_merkle_tree_page   = btrfs_read_merkle_tree_page,
821 	.write_merkle_tree_block = btrfs_write_merkle_tree_block,
822 };
823