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