xref: /linux/fs/ocfs2/blockcheck.h (revision d6869352cb3c3cf3450637a52349e2e87c1354aa)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* -*- mode: c; c-basic-offset: 8; -*-
3  * vim: noexpandtab sw=8 ts=8 sts=0:
4  *
5  * blockcheck.h
6  *
7  * Checksum and ECC codes for the OCFS2 userspace library.
8  *
9  * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
10  */
11 
12 #ifndef OCFS2_BLOCKCHECK_H
13 #define OCFS2_BLOCKCHECK_H
14 
15 
16 /* Count errors and error correction from blockcheck.c */
17 struct ocfs2_blockcheck_stats {
18 	spinlock_t b_lock;
19 	u64 b_check_count;	/* Number of blocks we've checked */
20 	u64 b_failure_count;	/* Number of failed checksums */
21 	u64 b_recover_count;	/* Number of blocks fixed by ecc */
22 
23 	/*
24 	 * debugfs entries, used if this is passed to
25 	 * ocfs2_blockcheck_stats_debugfs_install()
26 	 */
27 	struct dentry *b_debug_dir;	/* Parent of the debugfs  files */
28 	struct dentry *b_debug_check;	/* Exposes b_check_count */
29 	struct dentry *b_debug_failure;	/* Exposes b_failure_count */
30 	struct dentry *b_debug_recover;	/* Exposes b_recover_count */
31 };
32 
33 
34 /* High level block API */
35 void ocfs2_compute_meta_ecc(struct super_block *sb, void *data,
36 			    struct ocfs2_block_check *bc);
37 int ocfs2_validate_meta_ecc(struct super_block *sb, void *data,
38 			    struct ocfs2_block_check *bc);
39 void ocfs2_compute_meta_ecc_bhs(struct super_block *sb,
40 				struct buffer_head **bhs, int nr,
41 				struct ocfs2_block_check *bc);
42 int ocfs2_validate_meta_ecc_bhs(struct super_block *sb,
43 				struct buffer_head **bhs, int nr,
44 				struct ocfs2_block_check *bc);
45 
46 /* Lower level API */
47 void ocfs2_block_check_compute(void *data, size_t blocksize,
48 			       struct ocfs2_block_check *bc);
49 int ocfs2_block_check_validate(void *data, size_t blocksize,
50 			       struct ocfs2_block_check *bc,
51 			       struct ocfs2_blockcheck_stats *stats);
52 void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr,
53 				   struct ocfs2_block_check *bc);
54 int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr,
55 				   struct ocfs2_block_check *bc,
56 				   struct ocfs2_blockcheck_stats *stats);
57 
58 /* Debug Initialization */
59 int ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats *stats,
60 					   struct dentry *parent);
61 void ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats *stats);
62 
63 /*
64  * Hamming code functions
65  */
66 
67 /*
68  * Encoding hamming code parity bits for a buffer.
69  *
70  * This is the low level encoder function.  It can be called across
71  * multiple hunks just like the crc32 code.  'd' is the number of bits
72  * _in_this_hunk_.  nr is the bit offset of this hunk.  So, if you had
73  * two 512B buffers, you would do it like so:
74  *
75  * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0);
76  * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8);
77  *
78  * If you just have one buffer, use ocfs2_hamming_encode_block().
79  */
80 u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d,
81 			 unsigned int nr);
82 /*
83  * Fix a buffer with a bit error.  The 'fix' is the original parity
84  * xor'd with the parity calculated now.
85  *
86  * Like ocfs2_hamming_encode(), this can handle hunks.  nr is the bit
87  * offset of the current hunk.  If bit to be fixed is not part of the
88  * current hunk, this does nothing.
89  *
90  * If you only have one buffer, use ocfs2_hamming_fix_block().
91  */
92 void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
93 		       unsigned int fix);
94 
95 /* Convenience wrappers for a single buffer of data */
96 extern u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize);
97 extern void ocfs2_hamming_fix_block(void *data, unsigned int blocksize,
98 				    unsigned int fix);
99 #endif
100