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