lfs_accessors.h revision 1.22 1 1.22 dholland /* $NetBSD: lfs_accessors.h,v 1.22 2015/09/01 06:16:59 dholland Exp $ */
2 1.1 dholland
3 1.1 dholland /* from NetBSD: lfs.h,v 1.165 2015/07/24 06:59:32 dholland Exp */
4 1.1 dholland /* from NetBSD: dinode.h,v 1.22 2013/01/22 09:39:18 dholland Exp */
5 1.1 dholland /* from NetBSD: dir.h,v 1.21 2009/07/22 04:49:19 dholland Exp */
6 1.1 dholland
7 1.1 dholland /*-
8 1.1 dholland * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
9 1.1 dholland * All rights reserved.
10 1.1 dholland *
11 1.1 dholland * This code is derived from software contributed to The NetBSD Foundation
12 1.1 dholland * by Konrad E. Schroder <perseant (at) hhhh.org>.
13 1.1 dholland *
14 1.1 dholland * Redistribution and use in source and binary forms, with or without
15 1.1 dholland * modification, are permitted provided that the following conditions
16 1.1 dholland * are met:
17 1.1 dholland * 1. Redistributions of source code must retain the above copyright
18 1.1 dholland * notice, this list of conditions and the following disclaimer.
19 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 dholland * notice, this list of conditions and the following disclaimer in the
21 1.1 dholland * documentation and/or other materials provided with the distribution.
22 1.1 dholland *
23 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 dholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 dholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 dholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 dholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 dholland * POSSIBILITY OF SUCH DAMAGE.
34 1.1 dholland */
35 1.1 dholland /*-
36 1.1 dholland * Copyright (c) 1991, 1993
37 1.1 dholland * The Regents of the University of California. All rights reserved.
38 1.1 dholland *
39 1.1 dholland * Redistribution and use in source and binary forms, with or without
40 1.1 dholland * modification, are permitted provided that the following conditions
41 1.1 dholland * are met:
42 1.1 dholland * 1. Redistributions of source code must retain the above copyright
43 1.1 dholland * notice, this list of conditions and the following disclaimer.
44 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 dholland * notice, this list of conditions and the following disclaimer in the
46 1.1 dholland * documentation and/or other materials provided with the distribution.
47 1.1 dholland * 3. Neither the name of the University nor the names of its contributors
48 1.1 dholland * may be used to endorse or promote products derived from this software
49 1.1 dholland * without specific prior written permission.
50 1.1 dholland *
51 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 dholland * SUCH DAMAGE.
62 1.1 dholland *
63 1.1 dholland * @(#)lfs.h 8.9 (Berkeley) 5/8/95
64 1.1 dholland */
65 1.1 dholland /*
66 1.1 dholland * Copyright (c) 2002 Networks Associates Technology, Inc.
67 1.1 dholland * All rights reserved.
68 1.1 dholland *
69 1.1 dholland * This software was developed for the FreeBSD Project by Marshall
70 1.1 dholland * Kirk McKusick and Network Associates Laboratories, the Security
71 1.1 dholland * Research Division of Network Associates, Inc. under DARPA/SPAWAR
72 1.1 dholland * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
73 1.1 dholland * research program
74 1.1 dholland *
75 1.1 dholland * Copyright (c) 1982, 1989, 1993
76 1.1 dholland * The Regents of the University of California. All rights reserved.
77 1.1 dholland * (c) UNIX System Laboratories, Inc.
78 1.1 dholland * All or some portions of this file are derived from material licensed
79 1.1 dholland * to the University of California by American Telephone and Telegraph
80 1.1 dholland * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81 1.1 dholland * the permission of UNIX System Laboratories, Inc.
82 1.1 dholland *
83 1.1 dholland * Redistribution and use in source and binary forms, with or without
84 1.1 dholland * modification, are permitted provided that the following conditions
85 1.1 dholland * are met:
86 1.1 dholland * 1. Redistributions of source code must retain the above copyright
87 1.1 dholland * notice, this list of conditions and the following disclaimer.
88 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
89 1.1 dholland * notice, this list of conditions and the following disclaimer in the
90 1.1 dholland * documentation and/or other materials provided with the distribution.
91 1.1 dholland * 3. Neither the name of the University nor the names of its contributors
92 1.1 dholland * may be used to endorse or promote products derived from this software
93 1.1 dholland * without specific prior written permission.
94 1.1 dholland *
95 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
96 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
99 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
100 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
101 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
103 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
104 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105 1.1 dholland * SUCH DAMAGE.
106 1.1 dholland *
107 1.1 dholland * @(#)dinode.h 8.9 (Berkeley) 3/29/95
108 1.1 dholland */
109 1.1 dholland /*
110 1.1 dholland * Copyright (c) 1982, 1986, 1989, 1993
111 1.1 dholland * The Regents of the University of California. All rights reserved.
112 1.1 dholland * (c) UNIX System Laboratories, Inc.
113 1.1 dholland * All or some portions of this file are derived from material licensed
114 1.1 dholland * to the University of California by American Telephone and Telegraph
115 1.1 dholland * Co. or Unix System Laboratories, Inc. and are reproduced herein with
116 1.1 dholland * the permission of UNIX System Laboratories, Inc.
117 1.1 dholland *
118 1.1 dholland * Redistribution and use in source and binary forms, with or without
119 1.1 dholland * modification, are permitted provided that the following conditions
120 1.1 dholland * are met:
121 1.1 dholland * 1. Redistributions of source code must retain the above copyright
122 1.1 dholland * notice, this list of conditions and the following disclaimer.
123 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
124 1.1 dholland * notice, this list of conditions and the following disclaimer in the
125 1.1 dholland * documentation and/or other materials provided with the distribution.
126 1.1 dholland * 3. Neither the name of the University nor the names of its contributors
127 1.1 dholland * may be used to endorse or promote products derived from this software
128 1.1 dholland * without specific prior written permission.
129 1.1 dholland *
130 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
131 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
132 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
133 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
134 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
135 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
136 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
137 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
138 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
139 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
140 1.1 dholland * SUCH DAMAGE.
141 1.1 dholland *
142 1.1 dholland * @(#)dir.h 8.5 (Berkeley) 4/27/95
143 1.1 dholland */
144 1.1 dholland
145 1.1 dholland #ifndef _UFS_LFS_LFS_ACCESSORS_H_
146 1.1 dholland #define _UFS_LFS_LFS_ACCESSORS_H_
147 1.1 dholland
148 1.17 dholland #if defined(_KERNEL_OPT)
149 1.17 dholland #include "opt_lfs.h"
150 1.17 dholland #endif
151 1.17 dholland
152 1.17 dholland #include <sys/bswap.h>
153 1.17 dholland
154 1.11 dholland #if !defined(_KERNEL) && !defined(_STANDALONE)
155 1.11 dholland #include <assert.h>
156 1.11 dholland #define KASSERT assert
157 1.11 dholland #endif
158 1.11 dholland
159 1.1 dholland /*
160 1.9 dholland * STRUCT_LFS is used by the libsa code to get accessors that work
161 1.9 dholland * with struct salfs instead of struct lfs, and by the cleaner to
162 1.9 dholland * get accessors that work with struct clfs.
163 1.9 dholland */
164 1.9 dholland
165 1.9 dholland #ifndef STRUCT_LFS
166 1.9 dholland #define STRUCT_LFS struct lfs
167 1.9 dholland #endif
168 1.9 dholland
169 1.13 dholland /*
170 1.17 dholland * byte order
171 1.17 dholland */
172 1.17 dholland
173 1.17 dholland /*
174 1.17 dholland * For now at least, the bootblocks shall not be endian-independent.
175 1.17 dholland * We can see later if it fits in the size budget. Also disable the
176 1.17 dholland * byteswapping if LFS_EI is off.
177 1.17 dholland *
178 1.17 dholland * Caution: these functions "know" that bswap16/32/64 are unsigned,
179 1.17 dholland * and if that changes will likely break silently.
180 1.17 dholland */
181 1.17 dholland
182 1.17 dholland #if defined(_STANDALONE) || (defined(_KERNEL) && !defined(LFS_EI))
183 1.17 dholland #define LFS_SWAP_int16_t(fs, val) (val)
184 1.17 dholland #define LFS_SWAP_int32_t(fs, val) (val)
185 1.17 dholland #define LFS_SWAP_int64_t(fs, val) (val)
186 1.17 dholland #define LFS_SWAP_uint16_t(fs, val) (val)
187 1.17 dholland #define LFS_SWAP_uint32_t(fs, val) (val)
188 1.17 dholland #define LFS_SWAP_uint64_t(fs, val) (val)
189 1.17 dholland #else
190 1.17 dholland #define LFS_SWAP_int16_t(fs, val) \
191 1.17 dholland ((fs)->lfs_dobyteswap ? (int16_t)bswap16(val) : (val))
192 1.17 dholland #define LFS_SWAP_int32_t(fs, val) \
193 1.17 dholland ((fs)->lfs_dobyteswap ? (int32_t)bswap32(val) : (val))
194 1.17 dholland #define LFS_SWAP_int64_t(fs, val) \
195 1.17 dholland ((fs)->lfs_dobyteswap ? (int64_t)bswap64(val) : (val))
196 1.17 dholland #define LFS_SWAP_uint16_t(fs, val) \
197 1.17 dholland ((fs)->lfs_dobyteswap ? bswap16(val) : (val))
198 1.17 dholland #define LFS_SWAP_uint32_t(fs, val) \
199 1.17 dholland ((fs)->lfs_dobyteswap ? bswap32(val) : (val))
200 1.17 dholland #define LFS_SWAP_uint64_t(fs, val) \
201 1.17 dholland ((fs)->lfs_dobyteswap ? bswap64(val) : (val))
202 1.17 dholland #endif
203 1.17 dholland
204 1.17 dholland /*
205 1.22 dholland * For handling directories we will need to know if the volume is
206 1.22 dholland * little-endian.
207 1.22 dholland */
208 1.22 dholland #if BYTE_ORDER == LITTLE_ENDIAN
209 1.22 dholland #define LFS_LITTLE_ENDIAN_ONDISK(fs) (!(fs)->lfs_dobyteswap)
210 1.22 dholland #else
211 1.22 dholland #define LFS_LITTLE_ENDIAN_ONDISK(fs) ((fs)->lfs_dobyteswap)
212 1.22 dholland #endif
213 1.22 dholland
214 1.22 dholland
215 1.22 dholland /*
216 1.22 dholland * directories
217 1.22 dholland */
218 1.22 dholland
219 1.22 dholland /*
220 1.22 dholland * The LFS_DIRSIZ macro gives the minimum record length which will hold
221 1.22 dholland * the directory entry. This requires the amount of space in struct lfs_direct
222 1.22 dholland * without the d_name field, plus enough space for the name with a terminating
223 1.22 dholland * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
224 1.22 dholland */
225 1.22 dholland #define LFS_DIRECTSIZ(namlen) \
226 1.22 dholland ((sizeof(struct lfs_direct) - (LFS_MAXNAMLEN+1)) + (((namlen)+1 + 3) &~ 3))
227 1.22 dholland
228 1.22 dholland #if (BYTE_ORDER == LITTLE_ENDIAN)
229 1.22 dholland #define LFS_OLDDIRSIZ(oldfmt, dp, needswap) \
230 1.22 dholland (((oldfmt) && !(needswap)) ? \
231 1.22 dholland LFS_DIRECTSIZ((dp)->d_type) : LFS_DIRECTSIZ((dp)->d_namlen))
232 1.22 dholland #else
233 1.22 dholland #define LFS_OLDDIRSIZ(oldfmt, dp, needswap) \
234 1.22 dholland (((oldfmt) && (needswap)) ? \
235 1.22 dholland LFS_DIRECTSIZ((dp)->d_type) : LFS_DIRECTSIZ((dp)->d_namlen))
236 1.22 dholland #endif
237 1.22 dholland
238 1.22 dholland #define LFS_DIRSIZ(fs, dp) LFS_DIRECTSIZ(lfs_dir_getnamlen(fs, dp))
239 1.22 dholland
240 1.22 dholland /* Constants for the first argument of LFS_OLDDIRSIZ */
241 1.22 dholland #define LFS_OLDDIRFMT 1
242 1.22 dholland #define LFS_NEWDIRFMT 0
243 1.22 dholland
244 1.22 dholland static __unused inline uint8_t
245 1.22 dholland lfs_dir_gettype(const STRUCT_LFS *fs, const struct lfs_direct *dp)
246 1.22 dholland {
247 1.22 dholland if (fs->lfs_hasolddirfmt) {
248 1.22 dholland return LFS_DT_UNKNOWN;
249 1.22 dholland }
250 1.22 dholland return dp->d_type;
251 1.22 dholland }
252 1.22 dholland
253 1.22 dholland static __unused inline uint8_t
254 1.22 dholland lfs_dir_getnamlen(const STRUCT_LFS *fs, const struct lfs_direct *dp)
255 1.22 dholland {
256 1.22 dholland if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
257 1.22 dholland /* low-order byte of old 16-bit namlen field */
258 1.22 dholland return dp->d_type;
259 1.22 dholland }
260 1.22 dholland return dp->d_namlen;
261 1.22 dholland }
262 1.22 dholland
263 1.22 dholland static __unused inline void
264 1.22 dholland lfs_dir_settype(const STRUCT_LFS *fs, struct lfs_direct *dp, uint8_t type)
265 1.22 dholland {
266 1.22 dholland if (fs->lfs_hasolddirfmt) {
267 1.22 dholland /* do nothing */
268 1.22 dholland return;
269 1.22 dholland }
270 1.22 dholland dp->d_type = type;
271 1.22 dholland }
272 1.22 dholland
273 1.22 dholland static __unused inline void
274 1.22 dholland lfs_dir_setnamlen(const STRUCT_LFS *fs, struct lfs_direct *dp, uint8_t namlen)
275 1.22 dholland {
276 1.22 dholland if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
277 1.22 dholland /* low-order byte of old 16-bit namlen field */
278 1.22 dholland dp->d_type = namlen;
279 1.22 dholland }
280 1.22 dholland dp->d_namlen = namlen;
281 1.22 dholland }
282 1.22 dholland
283 1.22 dholland /*
284 1.22 dholland * These are called "dirt" because they ought to be cleaned up.
285 1.22 dholland */
286 1.22 dholland
287 1.22 dholland static __unused inline uint8_t
288 1.22 dholland lfs_dirt_getdottype(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
289 1.22 dholland {
290 1.22 dholland if (fs->lfs_hasolddirfmt) {
291 1.22 dholland return LFS_DT_UNKNOWN;
292 1.22 dholland }
293 1.22 dholland return dp->dot_type;
294 1.22 dholland }
295 1.22 dholland
296 1.22 dholland static __unused inline uint8_t
297 1.22 dholland lfs_dirt_getdotnamlen(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
298 1.22 dholland {
299 1.22 dholland if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
300 1.22 dholland /* low-order byte of old 16-bit namlen field */
301 1.22 dholland return dp->dot_type;
302 1.22 dholland }
303 1.22 dholland return dp->dot_namlen;
304 1.22 dholland }
305 1.22 dholland
306 1.22 dholland static __unused inline uint8_t
307 1.22 dholland lfs_dirt_getdotdottype(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
308 1.22 dholland {
309 1.22 dholland if (fs->lfs_hasolddirfmt) {
310 1.22 dholland return LFS_DT_UNKNOWN;
311 1.22 dholland }
312 1.22 dholland return dp->dotdot_type;
313 1.22 dholland }
314 1.22 dholland
315 1.22 dholland static __unused inline uint8_t
316 1.22 dholland lfs_dirt_getdotdotnamlen(const STRUCT_LFS *fs, const struct lfs_dirtemplate *dp)
317 1.22 dholland {
318 1.22 dholland if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
319 1.22 dholland /* low-order byte of old 16-bit namlen field */
320 1.22 dholland return dp->dotdot_type;
321 1.22 dholland }
322 1.22 dholland return dp->dotdot_namlen;
323 1.22 dholland }
324 1.22 dholland
325 1.22 dholland static __unused inline void
326 1.22 dholland lfs_dirt_settypes(const STRUCT_LFS *fs, struct lfs_dirtemplate *dtp,
327 1.22 dholland unsigned dt1, unsigned dt2)
328 1.22 dholland {
329 1.22 dholland if (fs->lfs_hasolddirfmt) {
330 1.22 dholland /* do nothing */
331 1.22 dholland return;
332 1.22 dholland }
333 1.22 dholland dtp->dot_type = dt1;
334 1.22 dholland dtp->dotdot_type = dt2;
335 1.22 dholland }
336 1.22 dholland
337 1.22 dholland static __unused inline void
338 1.22 dholland lfs_dirt_setnamlens(const STRUCT_LFS *fs, struct lfs_dirtemplate *dtp,
339 1.22 dholland unsigned len1, unsigned len2)
340 1.22 dholland {
341 1.22 dholland if (fs->lfs_hasolddirfmt && LFS_LITTLE_ENDIAN_ONDISK(fs)) {
342 1.22 dholland /* low-order bytes of old 16-bit namlen field */
343 1.22 dholland dtp->dot_type = len1;
344 1.22 dholland dtp->dotdot_type = len2;
345 1.22 dholland /* clear the high-order bytes */
346 1.22 dholland dtp->dot_namlen = 0;
347 1.22 dholland dtp->dotdot_namlen = 0;
348 1.22 dholland return;
349 1.22 dholland }
350 1.22 dholland dtp->dot_namlen = len1;
351 1.22 dholland dtp->dotdot_namlen = len2;
352 1.22 dholland }
353 1.22 dholland
354 1.22 dholland
355 1.22 dholland /*
356 1.13 dholland * dinodes
357 1.13 dholland */
358 1.9 dholland
359 1.9 dholland /*
360 1.1 dholland * Maximum length of a symlink that can be stored within the inode.
361 1.1 dholland */
362 1.20 dholland #define LFS32_MAXSYMLINKLEN ((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int32_t))
363 1.20 dholland #define LFS64_MAXSYMLINKLEN ((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int64_t))
364 1.1 dholland
365 1.20 dholland #define LFS_MAXSYMLINKLEN(fs) \
366 1.20 dholland ((fs)->lfs_is64 ? LFS64_MAXSYMLINKLEN : LFS32_MAXSYMLINKLEN)
367 1.20 dholland
368 1.13 dholland #define DINOSIZE(fs) ((fs)->lfs_is64 ? sizeof(struct lfs64_dinode) : sizeof(struct lfs32_dinode))
369 1.13 dholland
370 1.13 dholland #define DINO_IN_BLOCK(fs, base, ix) \
371 1.13 dholland ((union lfs_dinode *)((char *)(base) + DINOSIZE(fs) * (ix)))
372 1.13 dholland
373 1.14 dholland static __unused inline void
374 1.14 dholland lfs_copy_dinode(STRUCT_LFS *fs,
375 1.14 dholland union lfs_dinode *dst, const union lfs_dinode *src)
376 1.14 dholland {
377 1.14 dholland /*
378 1.14 dholland * We can do structure assignment of the structs, but not of
379 1.14 dholland * the whole union, as the union is the size of the (larger)
380 1.14 dholland * 64-bit struct and on a 32-bit fs the upper half of it might
381 1.14 dholland * be off the end of a buffer or otherwise invalid.
382 1.14 dholland */
383 1.14 dholland if (fs->lfs_is64) {
384 1.14 dholland dst->u_64 = src->u_64;
385 1.14 dholland } else {
386 1.14 dholland dst->u_32 = src->u_32;
387 1.14 dholland }
388 1.14 dholland }
389 1.14 dholland
390 1.13 dholland #define LFS_DEF_DINO_ACCESSOR(type, type32, field) \
391 1.13 dholland static __unused inline type \
392 1.13 dholland lfs_dino_get##field(STRUCT_LFS *fs, union lfs_dinode *dip) \
393 1.13 dholland { \
394 1.13 dholland if (fs->lfs_is64) { \
395 1.17 dholland return LFS_SWAP_##type(fs, dip->u_64.di_##field); \
396 1.13 dholland } else { \
397 1.17 dholland return LFS_SWAP_##type32(fs, dip->u_32.di_##field); \
398 1.13 dholland } \
399 1.13 dholland } \
400 1.13 dholland static __unused inline void \
401 1.13 dholland lfs_dino_set##field(STRUCT_LFS *fs, union lfs_dinode *dip, type val) \
402 1.13 dholland { \
403 1.13 dholland if (fs->lfs_is64) { \
404 1.13 dholland type *p = &dip->u_64.di_##field; \
405 1.13 dholland (void)p; \
406 1.17 dholland dip->u_64.di_##field = LFS_SWAP_##type(fs, val); \
407 1.13 dholland } else { \
408 1.13 dholland type32 *p = &dip->u_32.di_##field; \
409 1.13 dholland (void)p; \
410 1.17 dholland dip->u_32.di_##field = LFS_SWAP_##type32(fs, val); \
411 1.13 dholland } \
412 1.13 dholland } \
413 1.13 dholland
414 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint16_t, uint16_t, mode);
415 1.13 dholland LFS_DEF_DINO_ACCESSOR(int16_t, int16_t, nlink);
416 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, inumber);
417 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint64_t, uint64_t, size);
418 1.13 dholland LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, atime);
419 1.13 dholland LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, atimensec);
420 1.13 dholland LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, mtime);
421 1.13 dholland LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, mtimensec);
422 1.13 dholland LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, ctime);
423 1.13 dholland LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, ctimensec);
424 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, flags);
425 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, blocks);
426 1.13 dholland LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, gen);
427 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, uid);
428 1.13 dholland LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, gid);
429 1.13 dholland
430 1.16 dholland /* XXX this should be done differently (it's a fake field) */
431 1.16 dholland LFS_DEF_DINO_ACCESSOR(uint64_t, int32_t, rdev);
432 1.16 dholland
433 1.13 dholland static __unused inline daddr_t
434 1.13 dholland lfs_dino_getdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
435 1.13 dholland {
436 1.13 dholland KASSERT(ix < ULFS_NDADDR);
437 1.13 dholland if (fs->lfs_is64) {
438 1.13 dholland return dip->u_64.di_db[ix];
439 1.13 dholland } else {
440 1.13 dholland return dip->u_32.di_db[ix];
441 1.13 dholland }
442 1.13 dholland }
443 1.13 dholland
444 1.13 dholland static __unused inline daddr_t
445 1.13 dholland lfs_dino_getib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
446 1.13 dholland {
447 1.13 dholland KASSERT(ix < ULFS_NIADDR);
448 1.13 dholland if (fs->lfs_is64) {
449 1.13 dholland return dip->u_64.di_ib[ix];
450 1.13 dholland } else {
451 1.13 dholland return dip->u_32.di_ib[ix];
452 1.13 dholland }
453 1.13 dholland }
454 1.13 dholland
455 1.13 dholland static __unused inline void
456 1.13 dholland lfs_dino_setdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
457 1.13 dholland {
458 1.13 dholland KASSERT(ix < ULFS_NDADDR);
459 1.13 dholland if (fs->lfs_is64) {
460 1.13 dholland dip->u_64.di_db[ix] = val;
461 1.13 dholland } else {
462 1.13 dholland dip->u_32.di_db[ix] = val;
463 1.13 dholland }
464 1.13 dholland }
465 1.13 dholland
466 1.13 dholland static __unused inline void
467 1.13 dholland lfs_dino_setib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
468 1.13 dholland {
469 1.13 dholland KASSERT(ix < ULFS_NIADDR);
470 1.13 dholland if (fs->lfs_is64) {
471 1.13 dholland dip->u_64.di_ib[ix] = val;
472 1.13 dholland } else {
473 1.13 dholland dip->u_32.di_ib[ix] = val;
474 1.13 dholland }
475 1.13 dholland }
476 1.13 dholland
477 1.16 dholland /* birthtime is present only in the 64-bit inode */
478 1.16 dholland static __unused inline void
479 1.16 dholland lfs_dino_setbirthtime(STRUCT_LFS *fs, union lfs_dinode *dip,
480 1.16 dholland const struct timespec *ts)
481 1.16 dholland {
482 1.16 dholland if (fs->lfs_is64) {
483 1.16 dholland dip->u_64.di_birthtime = ts->tv_sec;
484 1.16 dholland dip->u_64.di_birthnsec = ts->tv_nsec;
485 1.16 dholland } else {
486 1.16 dholland /* drop it on the floor */
487 1.16 dholland }
488 1.16 dholland }
489 1.16 dholland
490 1.16 dholland /*
491 1.16 dholland * indirect blocks
492 1.16 dholland */
493 1.16 dholland
494 1.16 dholland static __unused inline daddr_t
495 1.16 dholland lfs_iblock_get(STRUCT_LFS *fs, void *block, unsigned ix)
496 1.16 dholland {
497 1.16 dholland if (fs->lfs_is64) {
498 1.16 dholland // XXX re-enable these asserts after reorging this file
499 1.16 dholland //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int64_t));
500 1.16 dholland return (daddr_t)(((int64_t *)block)[ix]);
501 1.16 dholland } else {
502 1.16 dholland //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int32_t));
503 1.16 dholland /* must sign-extend or UNWRITTEN gets trashed */
504 1.16 dholland return (daddr_t)(int64_t)(((int32_t *)block)[ix]);
505 1.16 dholland }
506 1.16 dholland }
507 1.16 dholland
508 1.16 dholland static __unused inline void
509 1.16 dholland lfs_iblock_set(STRUCT_LFS *fs, void *block, unsigned ix, daddr_t val)
510 1.16 dholland {
511 1.16 dholland if (fs->lfs_is64) {
512 1.16 dholland //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int64_t));
513 1.16 dholland ((int64_t *)block)[ix] = val;
514 1.16 dholland } else {
515 1.16 dholland //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int32_t));
516 1.16 dholland ((int32_t *)block)[ix] = val;
517 1.16 dholland }
518 1.16 dholland }
519 1.16 dholland
520 1.1 dholland /*
521 1.1 dholland * "struct buf" associated definitions
522 1.1 dholland */
523 1.1 dholland
524 1.1 dholland # define LFS_LOCK_BUF(bp) do { \
525 1.1 dholland if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) { \
526 1.1 dholland mutex_enter(&lfs_lock); \
527 1.1 dholland ++locked_queue_count; \
528 1.1 dholland locked_queue_bytes += bp->b_bufsize; \
529 1.1 dholland mutex_exit(&lfs_lock); \
530 1.1 dholland } \
531 1.1 dholland (bp)->b_flags |= B_LOCKED; \
532 1.1 dholland } while (0)
533 1.1 dholland
534 1.1 dholland # define LFS_UNLOCK_BUF(bp) do { \
535 1.1 dholland if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) { \
536 1.1 dholland mutex_enter(&lfs_lock); \
537 1.1 dholland --locked_queue_count; \
538 1.1 dholland locked_queue_bytes -= bp->b_bufsize; \
539 1.1 dholland if (locked_queue_count < LFS_WAIT_BUFS && \
540 1.1 dholland locked_queue_bytes < LFS_WAIT_BYTES) \
541 1.1 dholland cv_broadcast(&locked_queue_cv); \
542 1.1 dholland mutex_exit(&lfs_lock); \
543 1.1 dholland } \
544 1.1 dholland (bp)->b_flags &= ~B_LOCKED; \
545 1.1 dholland } while (0)
546 1.1 dholland
547 1.1 dholland /*
548 1.1 dholland * "struct inode" associated definitions
549 1.1 dholland */
550 1.1 dholland
551 1.1 dholland #define LFS_SET_UINO(ip, flags) do { \
552 1.1 dholland if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED)) \
553 1.1 dholland lfs_sb_adduinodes((ip)->i_lfs, 1); \
554 1.1 dholland if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING)) \
555 1.1 dholland lfs_sb_adduinodes((ip)->i_lfs, 1); \
556 1.1 dholland if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED)) \
557 1.1 dholland lfs_sb_adduinodes((ip)->i_lfs, 1); \
558 1.1 dholland (ip)->i_flag |= (flags); \
559 1.1 dholland } while (0)
560 1.1 dholland
561 1.1 dholland #define LFS_CLR_UINO(ip, flags) do { \
562 1.1 dholland if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED)) \
563 1.1 dholland lfs_sb_subuinodes((ip)->i_lfs, 1); \
564 1.1 dholland if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING)) \
565 1.1 dholland lfs_sb_subuinodes((ip)->i_lfs, 1); \
566 1.1 dholland if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED)) \
567 1.1 dholland lfs_sb_subuinodes((ip)->i_lfs, 1); \
568 1.1 dholland (ip)->i_flag &= ~(flags); \
569 1.1 dholland if (lfs_sb_getuinodes((ip)->i_lfs) < 0) { \
570 1.1 dholland panic("lfs_uinodes < 0"); \
571 1.1 dholland } \
572 1.1 dholland } while (0)
573 1.1 dholland
574 1.1 dholland #define LFS_ITIMES(ip, acc, mod, cre) \
575 1.1 dholland while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
576 1.1 dholland lfs_itimes(ip, acc, mod, cre)
577 1.1 dholland
578 1.1 dholland /*
579 1.1 dholland * On-disk and in-memory checkpoint segment usage structure.
580 1.1 dholland */
581 1.1 dholland
582 1.1 dholland #define SEGUPB(fs) (lfs_sb_getsepb(fs))
583 1.1 dholland #define SEGTABSIZE_SU(fs) \
584 1.1 dholland ((lfs_sb_getnseg(fs) + SEGUPB(fs) - 1) / lfs_sb_getsepb(fs))
585 1.1 dholland
586 1.1 dholland #ifdef _KERNEL
587 1.1 dholland # define SHARE_IFLOCK(F) \
588 1.1 dholland do { \
589 1.1 dholland rw_enter(&(F)->lfs_iflock, RW_READER); \
590 1.1 dholland } while(0)
591 1.1 dholland # define UNSHARE_IFLOCK(F) \
592 1.1 dholland do { \
593 1.1 dholland rw_exit(&(F)->lfs_iflock); \
594 1.1 dholland } while(0)
595 1.1 dholland #else /* ! _KERNEL */
596 1.1 dholland # define SHARE_IFLOCK(F)
597 1.1 dholland # define UNSHARE_IFLOCK(F)
598 1.1 dholland #endif /* ! _KERNEL */
599 1.1 dholland
600 1.1 dholland /* Read in the block with a specific segment usage entry from the ifile. */
601 1.1 dholland #define LFS_SEGENTRY(SP, F, IN, BP) do { \
602 1.1 dholland int _e; \
603 1.1 dholland SHARE_IFLOCK(F); \
604 1.1 dholland VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
605 1.1 dholland if ((_e = bread((F)->lfs_ivnode, \
606 1.1 dholland ((IN) / lfs_sb_getsepb(F)) + lfs_sb_getcleansz(F), \
607 1.1 dholland lfs_sb_getbsize(F), 0, &(BP))) != 0) \
608 1.1 dholland panic("lfs: ifile read: %d", _e); \
609 1.6 dholland if (lfs_sb_getversion(F) == 1) \
610 1.1 dholland (SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data + \
611 1.1 dholland ((IN) & (lfs_sb_getsepb(F) - 1))); \
612 1.1 dholland else \
613 1.1 dholland (SP) = (SEGUSE *)(BP)->b_data + ((IN) % lfs_sb_getsepb(F)); \
614 1.1 dholland UNSHARE_IFLOCK(F); \
615 1.1 dholland } while (0)
616 1.1 dholland
617 1.1 dholland #define LFS_WRITESEGENTRY(SP, F, IN, BP) do { \
618 1.1 dholland if ((SP)->su_nbytes == 0) \
619 1.1 dholland (SP)->su_flags |= SEGUSE_EMPTY; \
620 1.1 dholland else \
621 1.1 dholland (SP)->su_flags &= ~SEGUSE_EMPTY; \
622 1.1 dholland (F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags; \
623 1.1 dholland LFS_BWRITE_LOG(BP); \
624 1.1 dholland } while (0)
625 1.1 dholland
626 1.1 dholland /*
627 1.11 dholland * FINFO (file info) entries.
628 1.11 dholland */
629 1.11 dholland
630 1.11 dholland /* Size of an on-disk block pointer, e.g. in an indirect block. */
631 1.11 dholland /* XXX: move to a more suitable location in this file */
632 1.11 dholland #define LFS_BLKPTRSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
633 1.11 dholland
634 1.12 dholland /* Size of an on-disk inode number. */
635 1.12 dholland /* XXX: move to a more suitable location in this file */
636 1.12 dholland #define LFS_INUMSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
637 1.12 dholland
638 1.12 dholland /* size of a FINFO, without the block pointers */
639 1.12 dholland #define FINFOSIZE(fs) ((fs)->lfs_is64 ? sizeof(FINFO64) : sizeof(FINFO32))
640 1.12 dholland
641 1.11 dholland /* Full size of the provided FINFO record, including its block pointers. */
642 1.11 dholland #define FINFO_FULLSIZE(fs, fip) \
643 1.12 dholland (FINFOSIZE(fs) + lfs_fi_getnblocks(fs, fip) * LFS_BLKPTRSIZE(fs))
644 1.11 dholland
645 1.11 dholland #define NEXT_FINFO(fs, fip) \
646 1.11 dholland ((FINFO *)((char *)(fip) + FINFO_FULLSIZE(fs, fip)))
647 1.11 dholland
648 1.12 dholland #define LFS_DEF_FI_ACCESSOR(type, type32, field) \
649 1.12 dholland static __unused inline type \
650 1.12 dholland lfs_fi_get##field(STRUCT_LFS *fs, FINFO *fip) \
651 1.12 dholland { \
652 1.12 dholland if (fs->lfs_is64) { \
653 1.12 dholland return fip->u_64.fi_##field; \
654 1.12 dholland } else { \
655 1.12 dholland return fip->u_32.fi_##field; \
656 1.12 dholland } \
657 1.12 dholland } \
658 1.12 dholland static __unused inline void \
659 1.12 dholland lfs_fi_set##field(STRUCT_LFS *fs, FINFO *fip, type val) \
660 1.12 dholland { \
661 1.12 dholland if (fs->lfs_is64) { \
662 1.12 dholland type *p = &fip->u_64.fi_##field; \
663 1.12 dholland (void)p; \
664 1.12 dholland fip->u_64.fi_##field = val; \
665 1.12 dholland } else { \
666 1.12 dholland type32 *p = &fip->u_32.fi_##field; \
667 1.12 dholland (void)p; \
668 1.12 dholland fip->u_32.fi_##field = val; \
669 1.12 dholland } \
670 1.12 dholland } \
671 1.12 dholland
672 1.12 dholland LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, nblocks);
673 1.12 dholland LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, version);
674 1.12 dholland LFS_DEF_FI_ACCESSOR(uint64_t, uint32_t, ino);
675 1.12 dholland LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, lastlength);
676 1.12 dholland
677 1.12 dholland static __unused inline daddr_t
678 1.12 dholland lfs_fi_getblock(STRUCT_LFS *fs, FINFO *fip, unsigned index)
679 1.12 dholland {
680 1.12 dholland void *firstblock;
681 1.12 dholland
682 1.12 dholland firstblock = (char *)fip + FINFOSIZE(fs);
683 1.12 dholland KASSERT(index < lfs_fi_getnblocks(fs, fip));
684 1.12 dholland if (fs->lfs_is64) {
685 1.12 dholland return ((int64_t *)firstblock)[index];
686 1.12 dholland } else {
687 1.12 dholland return ((int32_t *)firstblock)[index];
688 1.12 dholland }
689 1.12 dholland }
690 1.12 dholland
691 1.12 dholland static __unused inline void
692 1.12 dholland lfs_fi_setblock(STRUCT_LFS *fs, FINFO *fip, unsigned index, daddr_t blk)
693 1.12 dholland {
694 1.12 dholland void *firstblock;
695 1.12 dholland
696 1.12 dholland firstblock = (char *)fip + FINFOSIZE(fs);
697 1.12 dholland KASSERT(index < lfs_fi_getnblocks(fs, fip));
698 1.12 dholland if (fs->lfs_is64) {
699 1.12 dholland ((int64_t *)firstblock)[index] = blk;
700 1.12 dholland } else {
701 1.12 dholland ((int32_t *)firstblock)[index] = blk;
702 1.12 dholland }
703 1.12 dholland }
704 1.12 dholland
705 1.11 dholland /*
706 1.1 dholland * Index file inode entries.
707 1.1 dholland */
708 1.1 dholland
709 1.1 dholland /*
710 1.1 dholland * LFSv1 compatibility code is not allowed to touch if_atime, since it
711 1.1 dholland * may not be mapped!
712 1.1 dholland */
713 1.1 dholland /* Read in the block with a specific inode from the ifile. */
714 1.1 dholland #define LFS_IENTRY(IP, F, IN, BP) do { \
715 1.1 dholland int _e; \
716 1.1 dholland SHARE_IFLOCK(F); \
717 1.1 dholland VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
718 1.1 dholland if ((_e = bread((F)->lfs_ivnode, \
719 1.1 dholland (IN) / lfs_sb_getifpb(F) + lfs_sb_getcleansz(F) + lfs_sb_getsegtabsz(F), \
720 1.1 dholland lfs_sb_getbsize(F), 0, &(BP))) != 0) \
721 1.1 dholland panic("lfs: ifile ino %d read %d", (int)(IN), _e); \
722 1.10 dholland if ((F)->lfs_is64) { \
723 1.10 dholland (IP) = (IFILE *)((IFILE64 *)(BP)->b_data + \
724 1.10 dholland (IN) % lfs_sb_getifpb(F)); \
725 1.10 dholland } else if (lfs_sb_getversion(F) > 1) { \
726 1.10 dholland (IP) = (IFILE *)((IFILE32 *)(BP)->b_data + \
727 1.10 dholland (IN) % lfs_sb_getifpb(F)); \
728 1.10 dholland } else { \
729 1.1 dholland (IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data + \
730 1.1 dholland (IN) % lfs_sb_getifpb(F)); \
731 1.10 dholland } \
732 1.1 dholland UNSHARE_IFLOCK(F); \
733 1.1 dholland } while (0)
734 1.15 mlelstv #define LFS_IENTRY_NEXT(IP, F) do { \
735 1.15 mlelstv if ((F)->lfs_is64) { \
736 1.15 mlelstv (IP) = (IFILE *)((IFILE64 *)(IP) + 1); \
737 1.15 mlelstv } else if (lfs_sb_getversion(F) > 1) { \
738 1.15 mlelstv (IP) = (IFILE *)((IFILE32 *)(IP) + 1); \
739 1.15 mlelstv } else { \
740 1.15 mlelstv (IP) = (IFILE *)((IFILE_V1 *)(IP) + 1); \
741 1.15 mlelstv } \
742 1.15 mlelstv } while (0)
743 1.1 dholland
744 1.10 dholland #define LFS_DEF_IF_ACCESSOR(type, type32, field) \
745 1.10 dholland static __unused inline type \
746 1.10 dholland lfs_if_get##field(STRUCT_LFS *fs, IFILE *ifp) \
747 1.10 dholland { \
748 1.10 dholland if (fs->lfs_is64) { \
749 1.10 dholland return ifp->u_64.if_##field; \
750 1.10 dholland } else { \
751 1.10 dholland return ifp->u_32.if_##field; \
752 1.10 dholland } \
753 1.10 dholland } \
754 1.10 dholland static __unused inline void \
755 1.10 dholland lfs_if_set##field(STRUCT_LFS *fs, IFILE *ifp, type val) \
756 1.10 dholland { \
757 1.10 dholland if (fs->lfs_is64) { \
758 1.10 dholland type *p = &ifp->u_64.if_##field; \
759 1.10 dholland (void)p; \
760 1.10 dholland ifp->u_64.if_##field = val; \
761 1.10 dholland } else { \
762 1.10 dholland type32 *p = &ifp->u_32.if_##field; \
763 1.10 dholland (void)p; \
764 1.10 dholland ifp->u_32.if_##field = val; \
765 1.10 dholland } \
766 1.10 dholland } \
767 1.10 dholland
768 1.10 dholland LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, version);
769 1.10 dholland LFS_DEF_IF_ACCESSOR(int64_t, int32_t, daddr);
770 1.10 dholland LFS_DEF_IF_ACCESSOR(u_int64_t, u_int32_t, nextfree);
771 1.10 dholland LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_sec);
772 1.10 dholland LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_nsec);
773 1.10 dholland
774 1.1 dholland /*
775 1.1 dholland * Cleaner information structure. This resides in the ifile and is used
776 1.1 dholland * to pass information from the kernel to the cleaner.
777 1.1 dholland */
778 1.1 dholland
779 1.1 dholland #define CLEANSIZE_SU(fs) \
780 1.9 dholland ((((fs)->lfs_is64 ? sizeof(CLEANERINFO64) : sizeof(CLEANERINFO32)) + \
781 1.9 dholland lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs))
782 1.9 dholland
783 1.9 dholland #define LFS_DEF_CI_ACCESSOR(type, type32, field) \
784 1.9 dholland static __unused inline type \
785 1.9 dholland lfs_ci_get##field(STRUCT_LFS *fs, CLEANERINFO *cip) \
786 1.9 dholland { \
787 1.9 dholland if (fs->lfs_is64) { \
788 1.9 dholland return cip->u_64.field; \
789 1.9 dholland } else { \
790 1.9 dholland return cip->u_32.field; \
791 1.9 dholland } \
792 1.9 dholland } \
793 1.9 dholland static __unused inline void \
794 1.9 dholland lfs_ci_set##field(STRUCT_LFS *fs, CLEANERINFO *cip, type val) \
795 1.9 dholland { \
796 1.9 dholland if (fs->lfs_is64) { \
797 1.9 dholland type *p = &cip->u_64.field; \
798 1.9 dholland (void)p; \
799 1.9 dholland cip->u_64.field = val; \
800 1.9 dholland } else { \
801 1.9 dholland type32 *p = &cip->u_32.field; \
802 1.9 dholland (void)p; \
803 1.9 dholland cip->u_32.field = val; \
804 1.9 dholland } \
805 1.9 dholland } \
806 1.9 dholland
807 1.9 dholland LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, clean);
808 1.9 dholland LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, dirty);
809 1.9 dholland LFS_DEF_CI_ACCESSOR(int64_t, int32_t, bfree);
810 1.9 dholland LFS_DEF_CI_ACCESSOR(int64_t, int32_t, avail);
811 1.9 dholland LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_head);
812 1.9 dholland LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_tail);
813 1.9 dholland LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, flags);
814 1.9 dholland
815 1.9 dholland static __unused inline void
816 1.9 dholland lfs_ci_shiftcleantodirty(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
817 1.9 dholland {
818 1.9 dholland lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) - num);
819 1.9 dholland lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) + num);
820 1.9 dholland }
821 1.9 dholland
822 1.9 dholland static __unused inline void
823 1.9 dholland lfs_ci_shiftdirtytoclean(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
824 1.9 dholland {
825 1.9 dholland lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) - num);
826 1.9 dholland lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) + num);
827 1.9 dholland }
828 1.1 dholland
829 1.1 dholland /* Read in the block with the cleaner info from the ifile. */
830 1.1 dholland #define LFS_CLEANERINFO(CP, F, BP) do { \
831 1.1 dholland SHARE_IFLOCK(F); \
832 1.1 dholland VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
833 1.1 dholland if (bread((F)->lfs_ivnode, \
834 1.1 dholland (daddr_t)0, lfs_sb_getbsize(F), 0, &(BP))) \
835 1.1 dholland panic("lfs: ifile read"); \
836 1.1 dholland (CP) = (CLEANERINFO *)(BP)->b_data; \
837 1.1 dholland UNSHARE_IFLOCK(F); \
838 1.1 dholland } while (0)
839 1.1 dholland
840 1.1 dholland /*
841 1.1 dholland * Synchronize the Ifile cleaner info with current avail and bfree.
842 1.1 dholland */
843 1.1 dholland #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do { \
844 1.1 dholland mutex_enter(&lfs_lock); \
845 1.9 dholland if ((w) || lfs_ci_getbfree(fs, cip) != lfs_sb_getbfree(fs) || \
846 1.9 dholland lfs_ci_getavail(fs, cip) != lfs_sb_getavail(fs) - fs->lfs_ravail - \
847 1.1 dholland fs->lfs_favail) { \
848 1.9 dholland lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs)); \
849 1.9 dholland lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs) - fs->lfs_ravail - \
850 1.9 dholland fs->lfs_favail); \
851 1.1 dholland if (((bp)->b_flags & B_GATHERED) == 0) { \
852 1.1 dholland fs->lfs_flags |= LFS_IFDIRTY; \
853 1.1 dholland } \
854 1.1 dholland mutex_exit(&lfs_lock); \
855 1.1 dholland (void) LFS_BWRITE_LOG(bp); /* Ifile */ \
856 1.1 dholland } else { \
857 1.1 dholland mutex_exit(&lfs_lock); \
858 1.1 dholland brelse(bp, 0); \
859 1.1 dholland } \
860 1.1 dholland } while (0)
861 1.1 dholland
862 1.1 dholland /*
863 1.1 dholland * Get the head of the inode free list.
864 1.1 dholland * Always called with the segment lock held.
865 1.1 dholland */
866 1.1 dholland #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do { \
867 1.6 dholland if (lfs_sb_getversion(FS) > 1) { \
868 1.1 dholland LFS_CLEANERINFO((CIP), (FS), (BP)); \
869 1.9 dholland lfs_sb_setfreehd(FS, lfs_ci_getfree_head(FS, CIP)); \
870 1.1 dholland brelse(BP, 0); \
871 1.1 dholland } \
872 1.1 dholland *(FREEP) = lfs_sb_getfreehd(FS); \
873 1.1 dholland } while (0)
874 1.1 dholland
875 1.1 dholland #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do { \
876 1.1 dholland lfs_sb_setfreehd(FS, VAL); \
877 1.6 dholland if (lfs_sb_getversion(FS) > 1) { \
878 1.1 dholland LFS_CLEANERINFO((CIP), (FS), (BP)); \
879 1.9 dholland lfs_ci_setfree_head(FS, CIP, VAL); \
880 1.1 dholland LFS_BWRITE_LOG(BP); \
881 1.1 dholland mutex_enter(&lfs_lock); \
882 1.1 dholland (FS)->lfs_flags |= LFS_IFDIRTY; \
883 1.1 dholland mutex_exit(&lfs_lock); \
884 1.1 dholland } \
885 1.1 dholland } while (0)
886 1.1 dholland
887 1.1 dholland #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do { \
888 1.1 dholland LFS_CLEANERINFO((CIP), (FS), (BP)); \
889 1.9 dholland *(FREEP) = lfs_ci_getfree_tail(FS, CIP); \
890 1.1 dholland brelse(BP, 0); \
891 1.1 dholland } while (0)
892 1.1 dholland
893 1.1 dholland #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do { \
894 1.1 dholland LFS_CLEANERINFO((CIP), (FS), (BP)); \
895 1.9 dholland lfs_ci_setfree_tail(FS, CIP, VAL); \
896 1.1 dholland LFS_BWRITE_LOG(BP); \
897 1.1 dholland mutex_enter(&lfs_lock); \
898 1.1 dholland (FS)->lfs_flags |= LFS_IFDIRTY; \
899 1.1 dholland mutex_exit(&lfs_lock); \
900 1.1 dholland } while (0)
901 1.1 dholland
902 1.1 dholland /*
903 1.1 dholland * On-disk segment summary information
904 1.1 dholland */
905 1.1 dholland
906 1.11 dholland #define SEGSUM_SIZE(fs) \
907 1.11 dholland (fs->lfs_is64 ? sizeof(SEGSUM64) : \
908 1.11 dholland lfs_sb_getversion(fs) > 1 ? sizeof(SEGSUM32) : sizeof(SEGSUM_V1))
909 1.11 dholland
910 1.11 dholland /*
911 1.11 dholland * The SEGSUM structure is followed by FINFO structures. Get the pointer
912 1.11 dholland * to the first FINFO.
913 1.11 dholland *
914 1.11 dholland * XXX this can't be a macro yet; this file needs to be resorted.
915 1.11 dholland */
916 1.11 dholland #if 0
917 1.11 dholland static __unused inline FINFO *
918 1.11 dholland segsum_finfobase(STRUCT_LFS *fs, SEGSUM *ssp)
919 1.11 dholland {
920 1.12 dholland return (FINFO *)((char *)ssp + SEGSUM_SIZE(fs));
921 1.11 dholland }
922 1.11 dholland #else
923 1.11 dholland #define SEGSUM_FINFOBASE(fs, ssp) \
924 1.12 dholland ((FINFO *)((char *)(ssp) + SEGSUM_SIZE(fs)));
925 1.11 dholland #endif
926 1.11 dholland
927 1.11 dholland #define LFS_DEF_SS_ACCESSOR(type, type32, field) \
928 1.11 dholland static __unused inline type \
929 1.11 dholland lfs_ss_get##field(STRUCT_LFS *fs, SEGSUM *ssp) \
930 1.11 dholland { \
931 1.11 dholland if (fs->lfs_is64) { \
932 1.11 dholland return ssp->u_64.ss_##field; \
933 1.11 dholland } else { \
934 1.11 dholland return ssp->u_32.ss_##field; \
935 1.11 dholland } \
936 1.11 dholland } \
937 1.11 dholland static __unused inline void \
938 1.11 dholland lfs_ss_set##field(STRUCT_LFS *fs, SEGSUM *ssp, type val) \
939 1.11 dholland { \
940 1.11 dholland if (fs->lfs_is64) { \
941 1.11 dholland type *p = &ssp->u_64.ss_##field; \
942 1.11 dholland (void)p; \
943 1.11 dholland ssp->u_64.ss_##field = val; \
944 1.11 dholland } else { \
945 1.11 dholland type32 *p = &ssp->u_32.ss_##field; \
946 1.11 dholland (void)p; \
947 1.11 dholland ssp->u_32.ss_##field = val; \
948 1.11 dholland } \
949 1.11 dholland } \
950 1.11 dholland
951 1.11 dholland LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, sumsum);
952 1.11 dholland LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, datasum);
953 1.11 dholland LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, magic);
954 1.11 dholland LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, ident);
955 1.11 dholland LFS_DEF_SS_ACCESSOR(int64_t, int32_t, next);
956 1.11 dholland LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, nfinfo);
957 1.11 dholland LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, ninos);
958 1.11 dholland LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, flags);
959 1.11 dholland LFS_DEF_SS_ACCESSOR(uint64_t, uint32_t, reclino);
960 1.11 dholland LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, serial);
961 1.11 dholland LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, create);
962 1.11 dholland
963 1.11 dholland static __unused inline size_t
964 1.11 dholland lfs_ss_getsumstart(STRUCT_LFS *fs)
965 1.11 dholland {
966 1.11 dholland /* These are actually all the same. */
967 1.11 dholland if (fs->lfs_is64) {
968 1.11 dholland return offsetof(SEGSUM64, ss_datasum);
969 1.11 dholland } else /* if (lfs_sb_getversion(fs) > 1) */ {
970 1.11 dholland return offsetof(SEGSUM32, ss_datasum);
971 1.11 dholland } /* else {
972 1.11 dholland return offsetof(SEGSUM_V1, ss_datasum);
973 1.11 dholland } */
974 1.11 dholland /*
975 1.11 dholland * XXX ^^^ until this file is resorted lfs_sb_getversion isn't
976 1.11 dholland * defined yet.
977 1.11 dholland */
978 1.11 dholland }
979 1.11 dholland
980 1.11 dholland static __unused inline uint32_t
981 1.11 dholland lfs_ss_getocreate(STRUCT_LFS *fs, SEGSUM *ssp)
982 1.11 dholland {
983 1.11 dholland KASSERT(fs->lfs_is64 == 0);
984 1.11 dholland /* XXX need to resort this file before we can do this */
985 1.11 dholland //KASSERT(lfs_sb_getversion(fs) == 1);
986 1.11 dholland
987 1.11 dholland return ssp->u_v1.ss_create;
988 1.11 dholland }
989 1.11 dholland
990 1.11 dholland static __unused inline void
991 1.11 dholland lfs_ss_setocreate(STRUCT_LFS *fs, SEGSUM *ssp, uint32_t val)
992 1.11 dholland {
993 1.11 dholland KASSERT(fs->lfs_is64 == 0);
994 1.11 dholland /* XXX need to resort this file before we can do this */
995 1.11 dholland //KASSERT(lfs_sb_getversion(fs) == 1);
996 1.11 dholland
997 1.11 dholland ssp->u_v1.ss_create = val;
998 1.11 dholland }
999 1.11 dholland
1000 1.1 dholland
1001 1.1 dholland /*
1002 1.1 dholland * Super block.
1003 1.1 dholland */
1004 1.1 dholland
1005 1.1 dholland /*
1006 1.1 dholland * Generate accessors for the on-disk superblock fields with cpp.
1007 1.1 dholland */
1008 1.1 dholland
1009 1.3 dholland #define LFS_DEF_SB_ACCESSOR_FULL(type, type32, field) \
1010 1.1 dholland static __unused inline type \
1011 1.1 dholland lfs_sb_get##field(STRUCT_LFS *fs) \
1012 1.1 dholland { \
1013 1.7 dholland if (fs->lfs_is64) { \
1014 1.7 dholland return fs->lfs_dlfs_u.u_64.dlfs_##field; \
1015 1.7 dholland } else { \
1016 1.7 dholland return fs->lfs_dlfs_u.u_32.dlfs_##field; \
1017 1.7 dholland } \
1018 1.1 dholland } \
1019 1.1 dholland static __unused inline void \
1020 1.1 dholland lfs_sb_set##field(STRUCT_LFS *fs, type val) \
1021 1.1 dholland { \
1022 1.7 dholland if (fs->lfs_is64) { \
1023 1.7 dholland fs->lfs_dlfs_u.u_64.dlfs_##field = val; \
1024 1.7 dholland } else { \
1025 1.7 dholland fs->lfs_dlfs_u.u_32.dlfs_##field = val; \
1026 1.7 dholland } \
1027 1.1 dholland } \
1028 1.1 dholland static __unused inline void \
1029 1.1 dholland lfs_sb_add##field(STRUCT_LFS *fs, type val) \
1030 1.1 dholland { \
1031 1.7 dholland if (fs->lfs_is64) { \
1032 1.7 dholland type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
1033 1.7 dholland *p64 += val; \
1034 1.7 dholland } else { \
1035 1.7 dholland type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
1036 1.7 dholland *p32 += val; \
1037 1.7 dholland } \
1038 1.1 dholland } \
1039 1.1 dholland static __unused inline void \
1040 1.1 dholland lfs_sb_sub##field(STRUCT_LFS *fs, type val) \
1041 1.1 dholland { \
1042 1.7 dholland if (fs->lfs_is64) { \
1043 1.7 dholland type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
1044 1.7 dholland *p64 -= val; \
1045 1.7 dholland } else { \
1046 1.7 dholland type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
1047 1.7 dholland *p32 -= val; \
1048 1.7 dholland } \
1049 1.1 dholland }
1050 1.1 dholland
1051 1.3 dholland #define LFS_DEF_SB_ACCESSOR(t, f) LFS_DEF_SB_ACCESSOR_FULL(t, t, f)
1052 1.3 dholland
1053 1.7 dholland #define LFS_DEF_SB_ACCESSOR_32ONLY(type, field, val64) \
1054 1.7 dholland static __unused inline type \
1055 1.7 dholland lfs_sb_get##field(STRUCT_LFS *fs) \
1056 1.7 dholland { \
1057 1.7 dholland if (fs->lfs_is64) { \
1058 1.7 dholland return val64; \
1059 1.7 dholland } else { \
1060 1.7 dholland return fs->lfs_dlfs_u.u_32.dlfs_##field; \
1061 1.7 dholland } \
1062 1.7 dholland }
1063 1.7 dholland
1064 1.1 dholland #define lfs_magic lfs_dlfs.dlfs_magic
1065 1.6 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, version);
1066 1.3 dholland LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, size);
1067 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, ssize);
1068 1.4 dholland LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, dsize);
1069 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, bsize);
1070 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, fsize);
1071 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, frag);
1072 1.18 dholland LFS_DEF_SB_ACCESSOR_FULL(uint64_t, uint32_t, freehd);
1073 1.4 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, bfree);
1074 1.18 dholland LFS_DEF_SB_ACCESSOR_FULL(uint64_t, uint32_t, nfiles);
1075 1.4 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, avail);
1076 1.1 dholland LFS_DEF_SB_ACCESSOR(int32_t, uinodes);
1077 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, idaddr);
1078 1.19 dholland LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, ifile, LFS_IFILE_INUM);
1079 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastseg);
1080 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, nextseg);
1081 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, curseg);
1082 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, offset);
1083 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastpseg);
1084 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, inopf);
1085 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, minfree);
1086 1.1 dholland LFS_DEF_SB_ACCESSOR(uint64_t, maxfilesize);
1087 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, fsbpseg);
1088 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, inopb);
1089 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, ifpb);
1090 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, sepb);
1091 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, nindir);
1092 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, nseg);
1093 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, nspf);
1094 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, cleansz);
1095 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, segtabsz);
1096 1.7 dholland LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segmask, 0);
1097 1.7 dholland LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segshift, 0);
1098 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int64_t, bmask);
1099 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, bshift);
1100 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int64_t, ffmask);
1101 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, ffshift);
1102 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int64_t, fbmask);
1103 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, fbshift);
1104 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, blktodb);
1105 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, fsbtodb);
1106 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, sushift);
1107 1.1 dholland LFS_DEF_SB_ACCESSOR(int32_t, maxsymlinklen);
1108 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, cksum);
1109 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int16_t, pflags);
1110 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, nclean);
1111 1.1 dholland LFS_DEF_SB_ACCESSOR(int32_t, dmeta);
1112 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, minfreeseg);
1113 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, sumsize);
1114 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int64_t, serial);
1115 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, ibsize);
1116 1.5 dholland LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, s0addr);
1117 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int64_t, tstamp);
1118 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, inodefmt);
1119 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, interleave);
1120 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, ident);
1121 1.1 dholland LFS_DEF_SB_ACCESSOR(u_int32_t, resvseg);
1122 1.1 dholland
1123 1.1 dholland /* special-case accessors */
1124 1.1 dholland
1125 1.1 dholland /*
1126 1.1 dholland * the v1 otstamp field lives in what's now dlfs_inopf
1127 1.1 dholland */
1128 1.1 dholland #define lfs_sb_getotstamp(fs) lfs_sb_getinopf(fs)
1129 1.1 dholland #define lfs_sb_setotstamp(fs, val) lfs_sb_setinopf(fs, val)
1130 1.1 dholland
1131 1.1 dholland /*
1132 1.1 dholland * lfs_sboffs is an array
1133 1.1 dholland */
1134 1.1 dholland static __unused inline int32_t
1135 1.2 dholland lfs_sb_getsboff(STRUCT_LFS *fs, unsigned n)
1136 1.1 dholland {
1137 1.1 dholland #ifdef KASSERT /* ugh */
1138 1.1 dholland KASSERT(n < LFS_MAXNUMSB);
1139 1.1 dholland #endif
1140 1.7 dholland if (fs->lfs_is64) {
1141 1.7 dholland return fs->lfs_dlfs_u.u_64.dlfs_sboffs[n];
1142 1.7 dholland } else {
1143 1.7 dholland return fs->lfs_dlfs_u.u_32.dlfs_sboffs[n];
1144 1.7 dholland }
1145 1.1 dholland }
1146 1.1 dholland static __unused inline void
1147 1.2 dholland lfs_sb_setsboff(STRUCT_LFS *fs, unsigned n, int32_t val)
1148 1.1 dholland {
1149 1.1 dholland #ifdef KASSERT /* ugh */
1150 1.1 dholland KASSERT(n < LFS_MAXNUMSB);
1151 1.1 dholland #endif
1152 1.7 dholland if (fs->lfs_is64) {
1153 1.7 dholland fs->lfs_dlfs_u.u_64.dlfs_sboffs[n] = val;
1154 1.7 dholland } else {
1155 1.7 dholland fs->lfs_dlfs_u.u_32.dlfs_sboffs[n] = val;
1156 1.7 dholland }
1157 1.1 dholland }
1158 1.1 dholland
1159 1.1 dholland /*
1160 1.1 dholland * lfs_fsmnt is a string
1161 1.1 dholland */
1162 1.1 dholland static __unused inline const char *
1163 1.2 dholland lfs_sb_getfsmnt(STRUCT_LFS *fs)
1164 1.1 dholland {
1165 1.7 dholland if (fs->lfs_is64) {
1166 1.7 dholland return fs->lfs_dlfs_u.u_64.dlfs_fsmnt;
1167 1.7 dholland } else {
1168 1.7 dholland return fs->lfs_dlfs_u.u_32.dlfs_fsmnt;
1169 1.7 dholland }
1170 1.7 dholland }
1171 1.7 dholland
1172 1.7 dholland static __unused inline void
1173 1.7 dholland lfs_sb_setfsmnt(STRUCT_LFS *fs, const char *str)
1174 1.7 dholland {
1175 1.7 dholland if (fs->lfs_is64) {
1176 1.7 dholland (void)strncpy(fs->lfs_dlfs_u.u_64.dlfs_fsmnt, str,
1177 1.7 dholland sizeof(fs->lfs_dlfs_u.u_64.dlfs_fsmnt));
1178 1.7 dholland } else {
1179 1.7 dholland (void)strncpy(fs->lfs_dlfs_u.u_32.dlfs_fsmnt, str,
1180 1.7 dholland sizeof(fs->lfs_dlfs_u.u_32.dlfs_fsmnt));
1181 1.7 dholland }
1182 1.1 dholland }
1183 1.1 dholland
1184 1.8 dholland /* Highest addressable fsb */
1185 1.8 dholland #define LFS_MAX_DADDR(fs) \
1186 1.8 dholland ((fs)->lfs_is64 ? 0x7fffffffffffffff : 0x7fffffff)
1187 1.8 dholland
1188 1.1 dholland /* LFS_NINDIR is the number of indirects in a file system block. */
1189 1.1 dholland #define LFS_NINDIR(fs) (lfs_sb_getnindir(fs))
1190 1.1 dholland
1191 1.1 dholland /* LFS_INOPB is the number of inodes in a secondary storage block. */
1192 1.1 dholland #define LFS_INOPB(fs) (lfs_sb_getinopb(fs))
1193 1.1 dholland /* LFS_INOPF is the number of inodes in a fragment. */
1194 1.1 dholland #define LFS_INOPF(fs) (lfs_sb_getinopf(fs))
1195 1.1 dholland
1196 1.1 dholland #define lfs_blkoff(fs, loc) ((int)((loc) & lfs_sb_getbmask(fs)))
1197 1.1 dholland #define lfs_fragoff(fs, loc) /* calculates (loc % fs->lfs_fsize) */ \
1198 1.1 dholland ((int)((loc) & lfs_sb_getffmask(fs)))
1199 1.1 dholland
1200 1.4 dholland /* XXX: lowercase these as they're no longer macros */
1201 1.4 dholland /* Frags to diskblocks */
1202 1.4 dholland static __unused inline uint64_t
1203 1.4 dholland LFS_FSBTODB(STRUCT_LFS *fs, uint64_t b)
1204 1.4 dholland {
1205 1.1 dholland #if defined(_KERNEL)
1206 1.4 dholland return b << (lfs_sb_getffshift(fs) - DEV_BSHIFT);
1207 1.1 dholland #else
1208 1.4 dholland return b << lfs_sb_getfsbtodb(fs);
1209 1.1 dholland #endif
1210 1.4 dholland }
1211 1.4 dholland /* Diskblocks to frags */
1212 1.4 dholland static __unused inline uint64_t
1213 1.4 dholland LFS_DBTOFSB(STRUCT_LFS *fs, uint64_t b)
1214 1.4 dholland {
1215 1.4 dholland #if defined(_KERNEL)
1216 1.4 dholland return b >> (lfs_sb_getffshift(fs) - DEV_BSHIFT);
1217 1.4 dholland #else
1218 1.4 dholland return b >> lfs_sb_getfsbtodb(fs);
1219 1.4 dholland #endif
1220 1.4 dholland }
1221 1.1 dholland
1222 1.1 dholland #define lfs_lblkno(fs, loc) ((loc) >> lfs_sb_getbshift(fs))
1223 1.1 dholland #define lfs_lblktosize(fs, blk) ((blk) << lfs_sb_getbshift(fs))
1224 1.1 dholland
1225 1.4 dholland /* Frags to bytes */
1226 1.4 dholland static __unused inline uint64_t
1227 1.4 dholland lfs_fsbtob(STRUCT_LFS *fs, uint64_t b)
1228 1.4 dholland {
1229 1.4 dholland return b << lfs_sb_getffshift(fs);
1230 1.4 dholland }
1231 1.4 dholland /* Bytes to frags */
1232 1.4 dholland static __unused inline uint64_t
1233 1.4 dholland lfs_btofsb(STRUCT_LFS *fs, uint64_t b)
1234 1.4 dholland {
1235 1.4 dholland return b >> lfs_sb_getffshift(fs);
1236 1.4 dholland }
1237 1.1 dholland
1238 1.1 dholland #define lfs_numfrags(fs, loc) /* calculates (loc / fs->lfs_fsize) */ \
1239 1.1 dholland ((loc) >> lfs_sb_getffshift(fs))
1240 1.1 dholland #define lfs_blkroundup(fs, size)/* calculates roundup(size, lfs_sb_getbsize(fs)) */ \
1241 1.1 dholland ((off_t)(((size) + lfs_sb_getbmask(fs)) & (~lfs_sb_getbmask(fs))))
1242 1.1 dholland #define lfs_fragroundup(fs, size)/* calculates roundup(size, fs->lfs_fsize) */ \
1243 1.1 dholland ((off_t)(((size) + lfs_sb_getffmask(fs)) & (~lfs_sb_getffmask(fs))))
1244 1.1 dholland #define lfs_fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
1245 1.1 dholland ((frags) >> lfs_sb_getfbshift(fs))
1246 1.1 dholland #define lfs_blkstofrags(fs, blks)/* calculates (blks * fs->fs_frag) */ \
1247 1.1 dholland ((blks) << lfs_sb_getfbshift(fs))
1248 1.1 dholland #define lfs_fragnum(fs, fsb) /* calculates (fsb % fs->lfs_frag) */ \
1249 1.1 dholland ((fsb) & ((fs)->lfs_frag - 1))
1250 1.1 dholland #define lfs_blknum(fs, fsb) /* calculates rounddown(fsb, fs->lfs_frag) */ \
1251 1.1 dholland ((fsb) &~ ((fs)->lfs_frag - 1))
1252 1.1 dholland #define lfs_dblksize(fs, dp, lbn) \
1253 1.13 dholland (((lbn) >= ULFS_NDADDR || lfs_dino_getsize(fs, dp) >= ((lbn) + 1) << lfs_sb_getbshift(fs)) \
1254 1.1 dholland ? lfs_sb_getbsize(fs) \
1255 1.13 dholland : (lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, dp)))))
1256 1.1 dholland
1257 1.6 dholland #define lfs_segsize(fs) (lfs_sb_getversion(fs) == 1 ? \
1258 1.1 dholland lfs_lblktosize((fs), lfs_sb_getssize(fs)) : \
1259 1.1 dholland lfs_sb_getssize(fs))
1260 1.4 dholland /* XXX segtod produces a result in frags despite the 'd' */
1261 1.4 dholland #define lfs_segtod(fs, seg) (lfs_btofsb(fs, lfs_segsize(fs)) * (seg))
1262 1.1 dholland #define lfs_dtosn(fs, daddr) /* block address to segment number */ \
1263 1.1 dholland ((uint32_t)(((daddr) - lfs_sb_gets0addr(fs)) / lfs_segtod((fs), 1)))
1264 1.1 dholland #define lfs_sntod(fs, sn) /* segment number to disk address */ \
1265 1.1 dholland ((daddr_t)(lfs_segtod((fs), (sn)) + lfs_sb_gets0addr(fs)))
1266 1.1 dholland
1267 1.4 dholland /* XXX, blah. make this appear only if struct inode is defined */
1268 1.4 dholland #ifdef _UFS_LFS_LFS_INODE_H_
1269 1.4 dholland static __unused inline uint32_t
1270 1.4 dholland lfs_blksize(STRUCT_LFS *fs, struct inode *ip, uint64_t lbn)
1271 1.4 dholland {
1272 1.16 dholland if (lbn >= ULFS_NDADDR || lfs_dino_getsize(fs, ip->i_din) >= (lbn + 1) << lfs_sb_getbshift(fs)) {
1273 1.4 dholland return lfs_sb_getbsize(fs);
1274 1.4 dholland } else {
1275 1.16 dholland return lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, ip->i_din)));
1276 1.4 dholland }
1277 1.4 dholland }
1278 1.4 dholland #endif
1279 1.4 dholland
1280 1.12 dholland /*
1281 1.12 dholland * union lfs_blocks
1282 1.12 dholland */
1283 1.12 dholland
1284 1.12 dholland static __unused inline void
1285 1.12 dholland lfs_blocks_fromvoid(STRUCT_LFS *fs, union lfs_blocks *bp, void *p)
1286 1.12 dholland {
1287 1.12 dholland if (fs->lfs_is64) {
1288 1.12 dholland bp->b64 = p;
1289 1.12 dholland } else {
1290 1.12 dholland bp->b32 = p;
1291 1.12 dholland }
1292 1.12 dholland }
1293 1.12 dholland
1294 1.12 dholland static __unused inline void
1295 1.12 dholland lfs_blocks_fromfinfo(STRUCT_LFS *fs, union lfs_blocks *bp, FINFO *fip)
1296 1.12 dholland {
1297 1.12 dholland void *firstblock;
1298 1.12 dholland
1299 1.12 dholland firstblock = (char *)fip + FINFOSIZE(fs);
1300 1.12 dholland if (fs->lfs_is64) {
1301 1.12 dholland bp->b64 = (int64_t *)firstblock;
1302 1.12 dholland } else {
1303 1.12 dholland bp->b32 = (int32_t *)firstblock;
1304 1.12 dholland }
1305 1.12 dholland }
1306 1.12 dholland
1307 1.12 dholland static __unused inline daddr_t
1308 1.12 dholland lfs_blocks_get(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index)
1309 1.12 dholland {
1310 1.12 dholland if (fs->lfs_is64) {
1311 1.12 dholland return bp->b64[index];
1312 1.12 dholland } else {
1313 1.12 dholland return bp->b32[index];
1314 1.12 dholland }
1315 1.12 dholland }
1316 1.12 dholland
1317 1.12 dholland static __unused inline void
1318 1.12 dholland lfs_blocks_set(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index, daddr_t val)
1319 1.12 dholland {
1320 1.12 dholland if (fs->lfs_is64) {
1321 1.12 dholland bp->b64[index] = val;
1322 1.12 dholland } else {
1323 1.12 dholland bp->b32[index] = val;
1324 1.12 dholland }
1325 1.12 dholland }
1326 1.12 dholland
1327 1.12 dholland static __unused inline void
1328 1.12 dholland lfs_blocks_inc(STRUCT_LFS *fs, union lfs_blocks *bp)
1329 1.12 dholland {
1330 1.12 dholland if (fs->lfs_is64) {
1331 1.12 dholland bp->b64++;
1332 1.12 dholland } else {
1333 1.12 dholland bp->b32++;
1334 1.12 dholland }
1335 1.12 dholland }
1336 1.12 dholland
1337 1.12 dholland static __unused inline int
1338 1.12 dholland lfs_blocks_eq(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
1339 1.12 dholland {
1340 1.12 dholland if (fs->lfs_is64) {
1341 1.12 dholland return bp1->b64 == bp2->b64;
1342 1.12 dholland } else {
1343 1.12 dholland return bp1->b32 == bp2->b32;
1344 1.12 dholland }
1345 1.12 dholland }
1346 1.12 dholland
1347 1.12 dholland static __unused inline int
1348 1.12 dholland lfs_blocks_sub(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
1349 1.12 dholland {
1350 1.12 dholland /* (remember that the pointers are typed) */
1351 1.12 dholland if (fs->lfs_is64) {
1352 1.12 dholland return bp1->b64 - bp2->b64;
1353 1.12 dholland } else {
1354 1.12 dholland return bp1->b32 - bp2->b32;
1355 1.12 dholland }
1356 1.12 dholland }
1357 1.12 dholland
1358 1.12 dholland /*
1359 1.12 dholland * struct segment
1360 1.12 dholland */
1361 1.12 dholland
1362 1.4 dholland
1363 1.1 dholland /*
1364 1.1 dholland * Macros for determining free space on the disk, with the variable metadata
1365 1.1 dholland * of segment summaries and inode blocks taken into account.
1366 1.1 dholland */
1367 1.1 dholland /*
1368 1.1 dholland * Estimate number of clean blocks not available for writing because
1369 1.1 dholland * they will contain metadata or overhead. This is calculated as
1370 1.1 dholland *
1371 1.1 dholland * E = ((C * M / D) * D + (0) * (T - D)) / T
1372 1.1 dholland * or more simply
1373 1.1 dholland * E = (C * M) / T
1374 1.1 dholland *
1375 1.1 dholland * where
1376 1.1 dholland * C is the clean space,
1377 1.1 dholland * D is the dirty space,
1378 1.1 dholland * M is the dirty metadata, and
1379 1.1 dholland * T = C + D is the total space on disk.
1380 1.1 dholland *
1381 1.1 dholland * This approximates the old formula of E = C * M / D when D is close to T,
1382 1.1 dholland * but avoids falsely reporting "disk full" when the sample size (D) is small.
1383 1.1 dholland */
1384 1.1 dholland #define LFS_EST_CMETA(F) (int32_t)(( \
1385 1.1 dholland (lfs_sb_getdmeta(F) * (int64_t)lfs_sb_getnclean(F)) / \
1386 1.1 dholland (lfs_sb_getnseg(F))))
1387 1.1 dholland
1388 1.1 dholland /* Estimate total size of the disk not including metadata */
1389 1.1 dholland #define LFS_EST_NONMETA(F) (lfs_sb_getdsize(F) - lfs_sb_getdmeta(F) - LFS_EST_CMETA(F))
1390 1.1 dholland
1391 1.1 dholland /* Estimate number of blocks actually available for writing */
1392 1.1 dholland #define LFS_EST_BFREE(F) (lfs_sb_getbfree(F) > LFS_EST_CMETA(F) ? \
1393 1.1 dholland lfs_sb_getbfree(F) - LFS_EST_CMETA(F) : 0)
1394 1.1 dholland
1395 1.1 dholland /* Amount of non-meta space not available to mortal man */
1396 1.1 dholland #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) * \
1397 1.1 dholland (u_int64_t)lfs_sb_getminfree(F)) / \
1398 1.1 dholland 100)
1399 1.1 dholland
1400 1.4 dholland /* Can credential C write BB blocks? XXX: kauth_cred_geteuid is abusive */
1401 1.1 dholland #define ISSPACE(F, BB, C) \
1402 1.1 dholland ((((C) == NOCRED || kauth_cred_geteuid(C) == 0) && \
1403 1.1 dholland LFS_EST_BFREE(F) >= (BB)) || \
1404 1.1 dholland (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
1405 1.1 dholland
1406 1.1 dholland /* Can an ordinary user write BB blocks */
1407 1.1 dholland #define IS_FREESPACE(F, BB) \
1408 1.1 dholland (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
1409 1.1 dholland
1410 1.1 dholland /*
1411 1.1 dholland * The minimum number of blocks to create a new inode. This is:
1412 1.1 dholland * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
1413 1.1 dholland * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
1414 1.1 dholland */
1415 1.1 dholland #define LFS_NRESERVE(F) (lfs_btofsb((F), (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(F)))
1416 1.1 dholland
1417 1.1 dholland
1418 1.1 dholland
1419 1.1 dholland #endif /* _UFS_LFS_LFS_ACCESSORS_H_ */
1420