xref: /freebsd/lib/libufs/libufs.h (revision 405c3050f102b8c74782f0366c8ead927bd07b68)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Juli Mallett.  All rights reserved.
5  *
6  * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
7  * FreeBSD project.  Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * 1. Redistribution of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistribution in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef	__LIBUFS_H__
33 #define	__LIBUFS_H__
34 
35 /*
36  * libufs structures.
37  */
38 union dinodep {
39 	struct ufs1_dinode *dp1;
40 	struct ufs2_dinode *dp2;
41 };
42 
43 /*
44  * userland ufs disk.
45  */
46 struct uufsd {
47 	const char *d_name;	/* disk name */
48 	int d_ufs;		/* decimal UFS version */
49 	int d_fd;		/* raw device file descriptor */
50 	long d_bsize;		/* device bsize */
51 	ufs2_daddr_t d_sblock;	/* superblock location */
52 	struct csum *d_sbcsum;	/* Superblock summary info */
53 	caddr_t d_inoblock;	/* inode block */
54 	uint32_t d_inomin;	/* low inode (not ino_t for ABI compat) */
55 	uint32_t d_inomax;	/* high inode (not ino_t for ABI compat) */
56 	union dinodep d_dp;	/* pointer to currently active inode */
57 	union {
58 		struct fs d_fs;	/* filesystem information */
59 		char d_sb[MAXBSIZE];
60 				/* superblock as buffer */
61 	} d_sbunion;
62 	union {
63 		struct cg d_cg;	/* cylinder group */
64 		char d_buf[MAXBSIZE];
65 				/* cylinder group storage */
66 	} d_cgunion;
67 	int d_ccg;		/* current cylinder group */
68 	int d_lcg;		/* last cylinder group (in d_cg) */
69 	const char *d_error;	/* human readable disk error */
70 	int d_mine;		/* internal flags */
71 #define	d_fs	d_sbunion.d_fs
72 #define	d_sb	d_sbunion.d_sb
73 #define	d_cg	d_cgunion.d_cg
74 };
75 
76 /*
77  * libufs macros (internal, non-exported).
78  */
79 #ifdef	_LIBUFS
80 /*
81  * Trace steps through libufs, to be used at entry and erroneous return.
82  */
83 static inline void
84 ERROR(struct uufsd *u, const char *str)
85 {
86 
87 #ifdef	_LIBUFS_DEBUGGING
88 	if (str != NULL) {
89 		fprintf(stderr, "libufs: %s", str);
90 		if (errno != 0)
91 			fprintf(stderr, ": %s", strerror(errno));
92 		fprintf(stderr, "\n");
93 	}
94 #endif
95 	if (u != NULL)
96 		u->d_error = str;
97 }
98 #endif	/* _LIBUFS */
99 
100 __BEGIN_DECLS
101 
102 /*
103  * libufs prototypes.
104  */
105 
106 /*
107  * ffs_subr.c
108  */
109 void	ffs_clrblock(struct fs *, u_char *, ufs1_daddr_t);
110 void	ffs_clusteracct(struct fs *, struct cg *, ufs1_daddr_t, int);
111 void	ffs_fragacct(struct fs *, int, int32_t [], int);
112 int	ffs_isblock(struct fs *, u_char *, ufs1_daddr_t);
113 int	ffs_isfreeblock(struct fs *, u_char *, ufs1_daddr_t);
114 void	ffs_setblock(struct fs *, u_char *, ufs1_daddr_t);
115 int	ffs_sbget(void *, struct fs **, off_t, char *,
116 	    int (*)(void *, off_t, void **, int));
117 int	ffs_sbput(void *, struct fs *, off_t,
118 	    int (*)(void *, off_t, void *, int));
119 void	ffs_update_dinode_ckhash(struct fs *, struct ufs2_dinode *);
120 int	ffs_verify_dinode_ckhash(struct fs *, struct ufs2_dinode *);
121 
122 /*
123  * Request standard superblock location in ffs_sbget
124  */
125 #define	STDSB			-1	/* Fail if check-hash is bad */
126 #define	STDSB_NOHASHFAIL	-2	/* Ignore check-hash failure */
127 
128 /*
129  * block.c
130  */
131 ssize_t bread(struct uufsd *, ufs2_daddr_t, void *, size_t);
132 ssize_t bwrite(struct uufsd *, ufs2_daddr_t, const void *, size_t);
133 int berase(struct uufsd *, ufs2_daddr_t, ufs2_daddr_t);
134 
135 /*
136  * cgroup.c
137  */
138 ufs2_daddr_t cgballoc(struct uufsd *);
139 int cgbfree(struct uufsd *, ufs2_daddr_t, long);
140 ino_t cgialloc(struct uufsd *);
141 int cgget(struct uufsd *, int, struct cg *);
142 int cgput(struct uufsd *, struct cg *);
143 int cgread(struct uufsd *);
144 int cgread1(struct uufsd *, int);
145 int cgwrite(struct uufsd *);
146 int cgwrite1(struct uufsd *, int);
147 
148 /*
149  * inode.c
150  */
151 int getinode(struct uufsd *, union dinodep *, ino_t);
152 int putinode(struct uufsd *);
153 
154 /*
155  * sblock.c
156  */
157 int sbread(struct uufsd *);
158 int sbwrite(struct uufsd *, int);
159 /* low level superblock read/write functions */
160 int sbget(int, struct fs **, off_t);
161 int sbput(int, struct fs *, int);
162 
163 /*
164  * type.c
165  */
166 int ufs_disk_close(struct uufsd *);
167 int ufs_disk_fillout(struct uufsd *, const char *);
168 int ufs_disk_fillout_blank(struct uufsd *, const char *);
169 int ufs_disk_write(struct uufsd *);
170 
171 /*
172  * crc32c.c
173  */
174 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
175 
176 __END_DECLS
177 
178 #endif	/* __LIBUFS_H__ */
179