aboutsummaryrefslogtreecommitdiff
path: root/lib/libufs/libufs.h
blob: 45ac97f43c06b77c5054b40874486f08f7a4d4b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2002 Juli Mallett.  All rights reserved.
 *
 * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
 * FreeBSD project.  Redistribution and use in source and binary forms, with
 * or without modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistribution of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistribution in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef	__LIBUFS_H__
#define	__LIBUFS_H__

/*
 * Various disk controllers require their buffers to be aligned to the size
 * of a cache line. The LIBUFS_BUFALIGN defines the required alignment size.
 * The alignment must be a power of 2.
 */
#define LIBUFS_BUFALIGN	128

/*
 * libufs structures.
 */
union dinodep {
	struct ufs1_dinode *dp1;
	struct ufs2_dinode *dp2;
};

/*
 * userland ufs disk.
 */
struct uufsd {
	union {
		struct fs d_fs;		/* filesystem information */
		char d_sb[SBLOCKSIZE];	/* superblock as buffer */
	} d_sbunion __aligned(LIBUFS_BUFALIGN);
	union {
		struct cg d_cg;		/* cylinder group */
		char d_buf[MAXBSIZE];	/* cylinder group storage */
	} d_cgunion __aligned(LIBUFS_BUFALIGN);
	union {
		union dinodep d_ino[1];	/* inode block */
		char d_inos[MAXBSIZE];	/* inode block as buffer */
	} d_inosunion __aligned(LIBUFS_BUFALIGN);
	const char *d_name;		/* disk name */
	const char *d_error;		/* human readable disk error */
	ufs2_daddr_t d_sblock;		/* superblock location */
	struct fs_summary_info *d_si;	/* Superblock summary info */
	union dinodep d_dp;		/* pointer to currently active inode */
	ino_t d_inomin;			/* low ino */
	ino_t d_inomax;			/* high ino */
	off_t d_sblockloc;		/* where to look for the superblock */
	int64_t d_bsize;		/* device bsize */
	int64_t d_lookupflags;		/* flags to superblock lookup */
	int64_t d_mine;			/* internal flags */
	int32_t d_ccg;			/* current cylinder group */
	int32_t d_ufs;			/* decimal UFS version */
	int32_t d_fd;			/* raw device file descriptor */
	int32_t d_lcg;			/* last cylinder group (in d_cg) */
};
#define	d_inos	d_inosunion.d_inos
#define	d_fs	d_sbunion.d_fs
#define	d_cg	d_cgunion.d_cg

/*
 * libufs macros (internal, non-exported).
 */
#ifdef	_LIBUFS
/*
 * Ensure that the buffer is aligned to the I/O subsystem requirements.
 */
#define BUF_MALLOC(newbufpp, data, size) {				     \
	if (data != NULL && (((intptr_t)data) & (LIBUFS_BUFALIGN - 1)) == 0) \
		*newbufpp = (void *)data;				     \
	else								     \
		*newbufpp = aligned_alloc(LIBUFS_BUFALIGN, size);	     \
}
/*
 * Trace steps through libufs, to be used at entry and erroneous return.
 */
static inline void
ERROR(struct uufsd *u, const char *str)
{

#ifdef	_LIBUFS_DEBUGGING
	if (str != NULL) {
		fprintf(stderr, "libufs: %s", str);
		if (errno != 0)
			fprintf(stderr, ": %s", strerror(errno));
		fprintf(stderr, "\n");
	}
#endif
	if (u != NULL)
		u->d_error = str;
}
#endif	/* _LIBUFS */

__BEGIN_DECLS

/*
 * libufs prototypes.
 */

/*
 * ffs_subr.c
 */
void	ffs_clrblock(struct fs *, u_char *, ufs1_daddr_t);
void	ffs_clusteracct(struct fs *, struct cg *, ufs1_daddr_t, int);
void	ffs_fragacct(struct fs *, int, int32_t [], int);
int	ffs_isblock(struct fs *, u_char *, ufs1_daddr_t);
int	ffs_isfreeblock(struct fs *, u_char *, ufs1_daddr_t);
int	ffs_sbsearch(void *, struct fs **, int, char *,
	    int (*)(void *, off_t, void **, int));
void	ffs_setblock(struct fs *, u_char *, ufs1_daddr_t);
int	ffs_sbget(void *, struct fs **, off_t, int, char *,
	    int (*)(void *, off_t, void **, int));
int	ffs_sbput(void *, struct fs *, off_t,
	    int (*)(void *, off_t, void *, int));
void	ffs_update_dinode_ckhash(struct fs *, struct ufs2_dinode *);
int	ffs_verify_dinode_ckhash(struct fs *, struct ufs2_dinode *);

/*
 * block.c
 */
ssize_t bread(struct uufsd *, ufs2_daddr_t, void *, size_t);
ssize_t bwrite(struct uufsd *, ufs2_daddr_t, const void *, size_t);
int berase(struct uufsd *, ufs2_daddr_t, ufs2_daddr_t);

/*
 * cgroup.c
 */
ufs2_daddr_t cgballoc(struct uufsd *);
int cgbfree(struct uufsd *, ufs2_daddr_t, long);
ino_t cgialloc(struct uufsd *);
int cgget(int, struct fs *, int, struct cg *);
int cgput(int, struct fs *, struct cg *);
int cgread(struct uufsd *);
int cgread1(struct uufsd *, int);
int cgwrite(struct uufsd *);
int cgwrite1(struct uufsd *, int);

/*
 * inode.c
 */
int getinode(struct uufsd *, union dinodep *, ino_t);
int putinode(struct uufsd *);

/*
 * sblock.c
 */
int sbread(struct uufsd *);
int sbfind(struct uufsd *, int);
int sbwrite(struct uufsd *, int);
/* low level superblock read/write functions */
int sbget(int, struct fs **, off_t, int);
int sbsearch(int, struct fs **, int);
int sbput(int, struct fs *, int);

/*
 * type.c
 */
int ufs_disk_close(struct uufsd *);
int ufs_disk_fillout(struct uufsd *, const char *);
int ufs_disk_fillout_blank(struct uufsd *, const char *);
int ufs_disk_write(struct uufsd *);

/*
 * crc32c.c
 */
uint32_t calculate_crc32c(uint32_t, const void *, size_t);

__END_DECLS

#endif	/* __LIBUFS_H__ */