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