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