xref: /freebsd-14-stable/lib/libufs/libufs.h (revision 5572827428bb54e5a22aa81c8c8826d033205406)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
30 #ifndef	__LIBUFS_H__
31 #define	__LIBUFS_H__
32 #include <stdbool.h>
33 
34 /*
35  * userland ufs disk.
36  */
37 struct uufsd {
38 	const char *d_name;		/* disk name */
39 	int d_ufs;			/* decimal UFS version */
40 	int d_fd;			/* raw device file descriptor */
41 	long d_bsize;			/* device bsize */
42 	ufs2_daddr_t d_sblock;		/* superblock location */
43 	struct fs_summary_info *d_si;	/* Superblock summary info */
44 	caddr_t d_inoblock;		/* inode block */
45 	uint32_t d_inomin;		/* low ino, not ino_t for ABI compat */
46 	uint32_t d_inomax;		/* high ino, not ino_t for ABI compat */
47 	union dinodep d_dp;		/* pointer to currently active inode */
48 	union {
49 		struct fs d_fs;		/* filesystem information */
50 		char d_sb[MAXBSIZE];	/* superblock as buffer */
51 	} d_sbunion;
52 	union {
53 		struct cg d_cg;		/* cylinder group */
54 		char d_buf[MAXBSIZE];	/* cylinder group storage */
55 	} d_cgunion;
56 	int d_ccg;			/* current cylinder group */
57 	int d_lcg;			/* last cylinder group (in d_cg) */
58 	const char *d_error;		/* human readable disk error */
59 	off_t d_sblockloc;		/* where to look for the superblock */
60 	int d_lookupflags;		/* flags to superblock lookup */
61 	int d_mine;			/* internal flags */
62 #define	d_fs	d_sbunion.d_fs
63 #define	d_sb	d_sbunion.d_sb
64 #define	d_cg	d_cgunion.d_cg
65 };
66 
67 /*
68  * libufs macros (internal, non-exported).
69  */
70 #ifdef	_LIBUFS
71 /*
72  * Trace steps through libufs, to be used at entry and erroneous return.
73  */
74 static inline void
ERROR(struct uufsd * u,const char * str)75 ERROR(struct uufsd *u, const char *str)
76 {
77 
78 #ifdef	_LIBUFS_DEBUGGING
79 	if (str != NULL) {
80 		fprintf(stderr, "libufs: %s", str);
81 		if (errno != 0)
82 			fprintf(stderr, ": %s", strerror(errno));
83 		fprintf(stderr, "\n");
84 	}
85 #endif
86 	if (u != NULL)
87 		u->d_error = str;
88 }
89 #endif	/* _LIBUFS */
90 
91 __BEGIN_DECLS
92 
93 /*
94  * libufs prototypes.
95  */
96 
97 /*
98  * ffs_subr.c
99  */
100 void	ffs_clrblock(struct fs *, u_char *, ufs1_daddr_t);
101 void	ffs_clusteracct(struct fs *, struct cg *, ufs1_daddr_t, int);
102 void	ffs_fragacct(struct fs *, int, int32_t [], int);
103 int	ffs_isblock(struct fs *, u_char *, ufs1_daddr_t);
104 int	ffs_isfreeblock(struct fs *, u_char *, ufs1_daddr_t);
105 bool	ffs_oldfscompat_inode_read(struct fs *, union dinodep, time_t);
106 int	ffs_sbsearch(void *, struct fs **, int, char *,
107 	    int (*)(void *, off_t, void **, int));
108 void	ffs_setblock(struct fs *, u_char *, ufs1_daddr_t);
109 int	ffs_sbget(void *, struct fs **, off_t, int, char *,
110 	    int (*)(void *, off_t, void **, int));
111 int	ffs_sbput(void *, struct fs *, off_t,
112 	    int (*)(void *, off_t, void *, int));
113 void	ffs_update_dinode_ckhash(struct fs *, struct ufs2_dinode *);
114 int	ffs_verify_dinode_ckhash(struct fs *, struct ufs2_dinode *);
115 
116 /*
117  * block.c
118  */
119 ssize_t bread(struct uufsd *, ufs2_daddr_t, void *, size_t);
120 ssize_t bwrite(struct uufsd *, ufs2_daddr_t, const void *, size_t);
121 int berase(struct uufsd *, ufs2_daddr_t, ufs2_daddr_t);
122 
123 /*
124  * cgroup.c
125  */
126 ufs2_daddr_t cgballoc(struct uufsd *);
127 int cgbfree(struct uufsd *, ufs2_daddr_t, long);
128 ino_t cgialloc(struct uufsd *);
129 int cgget(int, struct fs *, int, struct cg *);
130 int cgput(int, struct fs *, struct cg *);
131 int cgread(struct uufsd *);
132 int cgread1(struct uufsd *, int);
133 int cgwrite(struct uufsd *);
134 int cgwrite1(struct uufsd *, int);
135 
136 /*
137  * inode.c
138  */
139 int getinode(struct uufsd *, union dinodep *, ino_t);
140 int putinode(struct uufsd *);
141 
142 /*
143  * sblock.c
144  */
145 int sbread(struct uufsd *);
146 int sbfind(struct uufsd *, int);
147 int sbwrite(struct uufsd *, int);
148 /* low level superblock read/write functions */
149 int sbget(int, struct fs **, off_t, int);
150 int sbsearch(int, struct fs **, int);
151 int sbput(int, struct fs *, int);
152 
153 /*
154  * type.c
155  */
156 int ufs_disk_close(struct uufsd *);
157 int ufs_disk_fillout(struct uufsd *, const char *);
158 int ufs_disk_fillout_blank(struct uufsd *, const char *);
159 int ufs_disk_write(struct uufsd *);
160 
161 /*
162  * crc32c.c
163  */
164 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
165 
166 __END_DECLS
167 
168 #endif	/* __LIBUFS_H__ */
169