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