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