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