lfs_accessors.h revision 1.21 1 /* $NetBSD: lfs_accessors.h,v 1.21 2015/09/01 06:13:09 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 * dinodes
206 */
207
208 /*
209 * Maximum length of a symlink that can be stored within the inode.
210 */
211 #define LFS32_MAXSYMLINKLEN ((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int32_t))
212 #define LFS64_MAXSYMLINKLEN ((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int64_t))
213
214 #define LFS_MAXSYMLINKLEN(fs) \
215 ((fs)->lfs_is64 ? LFS64_MAXSYMLINKLEN : LFS32_MAXSYMLINKLEN)
216
217 #define DINOSIZE(fs) ((fs)->lfs_is64 ? sizeof(struct lfs64_dinode) : sizeof(struct lfs32_dinode))
218
219 #define DINO_IN_BLOCK(fs, base, ix) \
220 ((union lfs_dinode *)((char *)(base) + DINOSIZE(fs) * (ix)))
221
222 static __unused inline void
223 lfs_copy_dinode(STRUCT_LFS *fs,
224 union lfs_dinode *dst, const union lfs_dinode *src)
225 {
226 /*
227 * We can do structure assignment of the structs, but not of
228 * the whole union, as the union is the size of the (larger)
229 * 64-bit struct and on a 32-bit fs the upper half of it might
230 * be off the end of a buffer or otherwise invalid.
231 */
232 if (fs->lfs_is64) {
233 dst->u_64 = src->u_64;
234 } else {
235 dst->u_32 = src->u_32;
236 }
237 }
238
239 #define LFS_DEF_DINO_ACCESSOR(type, type32, field) \
240 static __unused inline type \
241 lfs_dino_get##field(STRUCT_LFS *fs, union lfs_dinode *dip) \
242 { \
243 if (fs->lfs_is64) { \
244 return LFS_SWAP_##type(fs, dip->u_64.di_##field); \
245 } else { \
246 return LFS_SWAP_##type32(fs, dip->u_32.di_##field); \
247 } \
248 } \
249 static __unused inline void \
250 lfs_dino_set##field(STRUCT_LFS *fs, union lfs_dinode *dip, type val) \
251 { \
252 if (fs->lfs_is64) { \
253 type *p = &dip->u_64.di_##field; \
254 (void)p; \
255 dip->u_64.di_##field = LFS_SWAP_##type(fs, val); \
256 } else { \
257 type32 *p = &dip->u_32.di_##field; \
258 (void)p; \
259 dip->u_32.di_##field = LFS_SWAP_##type32(fs, val); \
260 } \
261 } \
262
263 LFS_DEF_DINO_ACCESSOR(uint16_t, uint16_t, mode);
264 LFS_DEF_DINO_ACCESSOR(int16_t, int16_t, nlink);
265 LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, inumber);
266 LFS_DEF_DINO_ACCESSOR(uint64_t, uint64_t, size);
267 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, atime);
268 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, atimensec);
269 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, mtime);
270 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, mtimensec);
271 LFS_DEF_DINO_ACCESSOR(int64_t, int32_t, ctime);
272 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, ctimensec);
273 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, flags);
274 LFS_DEF_DINO_ACCESSOR(uint64_t, uint32_t, blocks);
275 LFS_DEF_DINO_ACCESSOR(int32_t, int32_t, gen);
276 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, uid);
277 LFS_DEF_DINO_ACCESSOR(uint32_t, uint32_t, gid);
278
279 /* XXX this should be done differently (it's a fake field) */
280 LFS_DEF_DINO_ACCESSOR(uint64_t, int32_t, rdev);
281
282 static __unused inline daddr_t
283 lfs_dino_getdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
284 {
285 KASSERT(ix < ULFS_NDADDR);
286 if (fs->lfs_is64) {
287 return dip->u_64.di_db[ix];
288 } else {
289 return dip->u_32.di_db[ix];
290 }
291 }
292
293 static __unused inline daddr_t
294 lfs_dino_getib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix)
295 {
296 KASSERT(ix < ULFS_NIADDR);
297 if (fs->lfs_is64) {
298 return dip->u_64.di_ib[ix];
299 } else {
300 return dip->u_32.di_ib[ix];
301 }
302 }
303
304 static __unused inline void
305 lfs_dino_setdb(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
306 {
307 KASSERT(ix < ULFS_NDADDR);
308 if (fs->lfs_is64) {
309 dip->u_64.di_db[ix] = val;
310 } else {
311 dip->u_32.di_db[ix] = val;
312 }
313 }
314
315 static __unused inline void
316 lfs_dino_setib(STRUCT_LFS *fs, union lfs_dinode *dip, unsigned ix, daddr_t val)
317 {
318 KASSERT(ix < ULFS_NIADDR);
319 if (fs->lfs_is64) {
320 dip->u_64.di_ib[ix] = val;
321 } else {
322 dip->u_32.di_ib[ix] = val;
323 }
324 }
325
326 /* birthtime is present only in the 64-bit inode */
327 static __unused inline void
328 lfs_dino_setbirthtime(STRUCT_LFS *fs, union lfs_dinode *dip,
329 const struct timespec *ts)
330 {
331 if (fs->lfs_is64) {
332 dip->u_64.di_birthtime = ts->tv_sec;
333 dip->u_64.di_birthnsec = ts->tv_nsec;
334 } else {
335 /* drop it on the floor */
336 }
337 }
338
339 /*
340 * indirect blocks
341 */
342
343 static __unused inline daddr_t
344 lfs_iblock_get(STRUCT_LFS *fs, void *block, unsigned ix)
345 {
346 if (fs->lfs_is64) {
347 // XXX re-enable these asserts after reorging this file
348 //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int64_t));
349 return (daddr_t)(((int64_t *)block)[ix]);
350 } else {
351 //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int32_t));
352 /* must sign-extend or UNWRITTEN gets trashed */
353 return (daddr_t)(int64_t)(((int32_t *)block)[ix]);
354 }
355 }
356
357 static __unused inline void
358 lfs_iblock_set(STRUCT_LFS *fs, void *block, unsigned ix, daddr_t val)
359 {
360 if (fs->lfs_is64) {
361 //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int64_t));
362 ((int64_t *)block)[ix] = val;
363 } else {
364 //KASSERT(ix < lfs_sb_getbsize(fs) / sizeof(int32_t));
365 ((int32_t *)block)[ix] = val;
366 }
367 }
368
369 /*
370 * "struct buf" associated definitions
371 */
372
373 # define LFS_LOCK_BUF(bp) do { \
374 if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) { \
375 mutex_enter(&lfs_lock); \
376 ++locked_queue_count; \
377 locked_queue_bytes += bp->b_bufsize; \
378 mutex_exit(&lfs_lock); \
379 } \
380 (bp)->b_flags |= B_LOCKED; \
381 } while (0)
382
383 # define LFS_UNLOCK_BUF(bp) do { \
384 if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) { \
385 mutex_enter(&lfs_lock); \
386 --locked_queue_count; \
387 locked_queue_bytes -= bp->b_bufsize; \
388 if (locked_queue_count < LFS_WAIT_BUFS && \
389 locked_queue_bytes < LFS_WAIT_BYTES) \
390 cv_broadcast(&locked_queue_cv); \
391 mutex_exit(&lfs_lock); \
392 } \
393 (bp)->b_flags &= ~B_LOCKED; \
394 } while (0)
395
396 /*
397 * "struct inode" associated definitions
398 */
399
400 #define LFS_SET_UINO(ip, flags) do { \
401 if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED)) \
402 lfs_sb_adduinodes((ip)->i_lfs, 1); \
403 if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING)) \
404 lfs_sb_adduinodes((ip)->i_lfs, 1); \
405 if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED)) \
406 lfs_sb_adduinodes((ip)->i_lfs, 1); \
407 (ip)->i_flag |= (flags); \
408 } while (0)
409
410 #define LFS_CLR_UINO(ip, flags) do { \
411 if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED)) \
412 lfs_sb_subuinodes((ip)->i_lfs, 1); \
413 if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING)) \
414 lfs_sb_subuinodes((ip)->i_lfs, 1); \
415 if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED)) \
416 lfs_sb_subuinodes((ip)->i_lfs, 1); \
417 (ip)->i_flag &= ~(flags); \
418 if (lfs_sb_getuinodes((ip)->i_lfs) < 0) { \
419 panic("lfs_uinodes < 0"); \
420 } \
421 } while (0)
422
423 #define LFS_ITIMES(ip, acc, mod, cre) \
424 while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
425 lfs_itimes(ip, acc, mod, cre)
426
427 /*
428 * On-disk and in-memory checkpoint segment usage structure.
429 */
430
431 #define SEGUPB(fs) (lfs_sb_getsepb(fs))
432 #define SEGTABSIZE_SU(fs) \
433 ((lfs_sb_getnseg(fs) + SEGUPB(fs) - 1) / lfs_sb_getsepb(fs))
434
435 #ifdef _KERNEL
436 # define SHARE_IFLOCK(F) \
437 do { \
438 rw_enter(&(F)->lfs_iflock, RW_READER); \
439 } while(0)
440 # define UNSHARE_IFLOCK(F) \
441 do { \
442 rw_exit(&(F)->lfs_iflock); \
443 } while(0)
444 #else /* ! _KERNEL */
445 # define SHARE_IFLOCK(F)
446 # define UNSHARE_IFLOCK(F)
447 #endif /* ! _KERNEL */
448
449 /* Read in the block with a specific segment usage entry from the ifile. */
450 #define LFS_SEGENTRY(SP, F, IN, BP) do { \
451 int _e; \
452 SHARE_IFLOCK(F); \
453 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
454 if ((_e = bread((F)->lfs_ivnode, \
455 ((IN) / lfs_sb_getsepb(F)) + lfs_sb_getcleansz(F), \
456 lfs_sb_getbsize(F), 0, &(BP))) != 0) \
457 panic("lfs: ifile read: %d", _e); \
458 if (lfs_sb_getversion(F) == 1) \
459 (SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data + \
460 ((IN) & (lfs_sb_getsepb(F) - 1))); \
461 else \
462 (SP) = (SEGUSE *)(BP)->b_data + ((IN) % lfs_sb_getsepb(F)); \
463 UNSHARE_IFLOCK(F); \
464 } while (0)
465
466 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do { \
467 if ((SP)->su_nbytes == 0) \
468 (SP)->su_flags |= SEGUSE_EMPTY; \
469 else \
470 (SP)->su_flags &= ~SEGUSE_EMPTY; \
471 (F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags; \
472 LFS_BWRITE_LOG(BP); \
473 } while (0)
474
475 /*
476 * FINFO (file info) entries.
477 */
478
479 /* Size of an on-disk block pointer, e.g. in an indirect block. */
480 /* XXX: move to a more suitable location in this file */
481 #define LFS_BLKPTRSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
482
483 /* Size of an on-disk inode number. */
484 /* XXX: move to a more suitable location in this file */
485 #define LFS_INUMSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
486
487 /* size of a FINFO, without the block pointers */
488 #define FINFOSIZE(fs) ((fs)->lfs_is64 ? sizeof(FINFO64) : sizeof(FINFO32))
489
490 /* Full size of the provided FINFO record, including its block pointers. */
491 #define FINFO_FULLSIZE(fs, fip) \
492 (FINFOSIZE(fs) + lfs_fi_getnblocks(fs, fip) * LFS_BLKPTRSIZE(fs))
493
494 #define NEXT_FINFO(fs, fip) \
495 ((FINFO *)((char *)(fip) + FINFO_FULLSIZE(fs, fip)))
496
497 #define LFS_DEF_FI_ACCESSOR(type, type32, field) \
498 static __unused inline type \
499 lfs_fi_get##field(STRUCT_LFS *fs, FINFO *fip) \
500 { \
501 if (fs->lfs_is64) { \
502 return fip->u_64.fi_##field; \
503 } else { \
504 return fip->u_32.fi_##field; \
505 } \
506 } \
507 static __unused inline void \
508 lfs_fi_set##field(STRUCT_LFS *fs, FINFO *fip, type val) \
509 { \
510 if (fs->lfs_is64) { \
511 type *p = &fip->u_64.fi_##field; \
512 (void)p; \
513 fip->u_64.fi_##field = val; \
514 } else { \
515 type32 *p = &fip->u_32.fi_##field; \
516 (void)p; \
517 fip->u_32.fi_##field = val; \
518 } \
519 } \
520
521 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, nblocks);
522 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, version);
523 LFS_DEF_FI_ACCESSOR(uint64_t, uint32_t, ino);
524 LFS_DEF_FI_ACCESSOR(uint32_t, uint32_t, lastlength);
525
526 static __unused inline daddr_t
527 lfs_fi_getblock(STRUCT_LFS *fs, FINFO *fip, unsigned index)
528 {
529 void *firstblock;
530
531 firstblock = (char *)fip + FINFOSIZE(fs);
532 KASSERT(index < lfs_fi_getnblocks(fs, fip));
533 if (fs->lfs_is64) {
534 return ((int64_t *)firstblock)[index];
535 } else {
536 return ((int32_t *)firstblock)[index];
537 }
538 }
539
540 static __unused inline void
541 lfs_fi_setblock(STRUCT_LFS *fs, FINFO *fip, unsigned index, daddr_t blk)
542 {
543 void *firstblock;
544
545 firstblock = (char *)fip + FINFOSIZE(fs);
546 KASSERT(index < lfs_fi_getnblocks(fs, fip));
547 if (fs->lfs_is64) {
548 ((int64_t *)firstblock)[index] = blk;
549 } else {
550 ((int32_t *)firstblock)[index] = blk;
551 }
552 }
553
554 /*
555 * Index file inode entries.
556 */
557
558 /*
559 * LFSv1 compatibility code is not allowed to touch if_atime, since it
560 * may not be mapped!
561 */
562 /* Read in the block with a specific inode from the ifile. */
563 #define LFS_IENTRY(IP, F, IN, BP) do { \
564 int _e; \
565 SHARE_IFLOCK(F); \
566 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
567 if ((_e = bread((F)->lfs_ivnode, \
568 (IN) / lfs_sb_getifpb(F) + lfs_sb_getcleansz(F) + lfs_sb_getsegtabsz(F), \
569 lfs_sb_getbsize(F), 0, &(BP))) != 0) \
570 panic("lfs: ifile ino %d read %d", (int)(IN), _e); \
571 if ((F)->lfs_is64) { \
572 (IP) = (IFILE *)((IFILE64 *)(BP)->b_data + \
573 (IN) % lfs_sb_getifpb(F)); \
574 } else if (lfs_sb_getversion(F) > 1) { \
575 (IP) = (IFILE *)((IFILE32 *)(BP)->b_data + \
576 (IN) % lfs_sb_getifpb(F)); \
577 } else { \
578 (IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data + \
579 (IN) % lfs_sb_getifpb(F)); \
580 } \
581 UNSHARE_IFLOCK(F); \
582 } while (0)
583 #define LFS_IENTRY_NEXT(IP, F) do { \
584 if ((F)->lfs_is64) { \
585 (IP) = (IFILE *)((IFILE64 *)(IP) + 1); \
586 } else if (lfs_sb_getversion(F) > 1) { \
587 (IP) = (IFILE *)((IFILE32 *)(IP) + 1); \
588 } else { \
589 (IP) = (IFILE *)((IFILE_V1 *)(IP) + 1); \
590 } \
591 } while (0)
592
593 #define LFS_DEF_IF_ACCESSOR(type, type32, field) \
594 static __unused inline type \
595 lfs_if_get##field(STRUCT_LFS *fs, IFILE *ifp) \
596 { \
597 if (fs->lfs_is64) { \
598 return ifp->u_64.if_##field; \
599 } else { \
600 return ifp->u_32.if_##field; \
601 } \
602 } \
603 static __unused inline void \
604 lfs_if_set##field(STRUCT_LFS *fs, IFILE *ifp, type val) \
605 { \
606 if (fs->lfs_is64) { \
607 type *p = &ifp->u_64.if_##field; \
608 (void)p; \
609 ifp->u_64.if_##field = val; \
610 } else { \
611 type32 *p = &ifp->u_32.if_##field; \
612 (void)p; \
613 ifp->u_32.if_##field = val; \
614 } \
615 } \
616
617 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, version);
618 LFS_DEF_IF_ACCESSOR(int64_t, int32_t, daddr);
619 LFS_DEF_IF_ACCESSOR(u_int64_t, u_int32_t, nextfree);
620 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_sec);
621 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_nsec);
622
623 /*
624 * Cleaner information structure. This resides in the ifile and is used
625 * to pass information from the kernel to the cleaner.
626 */
627
628 #define CLEANSIZE_SU(fs) \
629 ((((fs)->lfs_is64 ? sizeof(CLEANERINFO64) : sizeof(CLEANERINFO32)) + \
630 lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs))
631
632 #define LFS_DEF_CI_ACCESSOR(type, type32, field) \
633 static __unused inline type \
634 lfs_ci_get##field(STRUCT_LFS *fs, CLEANERINFO *cip) \
635 { \
636 if (fs->lfs_is64) { \
637 return cip->u_64.field; \
638 } else { \
639 return cip->u_32.field; \
640 } \
641 } \
642 static __unused inline void \
643 lfs_ci_set##field(STRUCT_LFS *fs, CLEANERINFO *cip, type val) \
644 { \
645 if (fs->lfs_is64) { \
646 type *p = &cip->u_64.field; \
647 (void)p; \
648 cip->u_64.field = val; \
649 } else { \
650 type32 *p = &cip->u_32.field; \
651 (void)p; \
652 cip->u_32.field = val; \
653 } \
654 } \
655
656 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, clean);
657 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, dirty);
658 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, bfree);
659 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, avail);
660 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_head);
661 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_tail);
662 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, flags);
663
664 static __unused inline void
665 lfs_ci_shiftcleantodirty(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
666 {
667 lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) - num);
668 lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) + num);
669 }
670
671 static __unused inline void
672 lfs_ci_shiftdirtytoclean(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
673 {
674 lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) - num);
675 lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) + num);
676 }
677
678 /* Read in the block with the cleaner info from the ifile. */
679 #define LFS_CLEANERINFO(CP, F, BP) do { \
680 SHARE_IFLOCK(F); \
681 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
682 if (bread((F)->lfs_ivnode, \
683 (daddr_t)0, lfs_sb_getbsize(F), 0, &(BP))) \
684 panic("lfs: ifile read"); \
685 (CP) = (CLEANERINFO *)(BP)->b_data; \
686 UNSHARE_IFLOCK(F); \
687 } while (0)
688
689 /*
690 * Synchronize the Ifile cleaner info with current avail and bfree.
691 */
692 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do { \
693 mutex_enter(&lfs_lock); \
694 if ((w) || lfs_ci_getbfree(fs, cip) != lfs_sb_getbfree(fs) || \
695 lfs_ci_getavail(fs, cip) != lfs_sb_getavail(fs) - fs->lfs_ravail - \
696 fs->lfs_favail) { \
697 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs)); \
698 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs) - fs->lfs_ravail - \
699 fs->lfs_favail); \
700 if (((bp)->b_flags & B_GATHERED) == 0) { \
701 fs->lfs_flags |= LFS_IFDIRTY; \
702 } \
703 mutex_exit(&lfs_lock); \
704 (void) LFS_BWRITE_LOG(bp); /* Ifile */ \
705 } else { \
706 mutex_exit(&lfs_lock); \
707 brelse(bp, 0); \
708 } \
709 } while (0)
710
711 /*
712 * Get the head of the inode free list.
713 * Always called with the segment lock held.
714 */
715 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do { \
716 if (lfs_sb_getversion(FS) > 1) { \
717 LFS_CLEANERINFO((CIP), (FS), (BP)); \
718 lfs_sb_setfreehd(FS, lfs_ci_getfree_head(FS, CIP)); \
719 brelse(BP, 0); \
720 } \
721 *(FREEP) = lfs_sb_getfreehd(FS); \
722 } while (0)
723
724 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do { \
725 lfs_sb_setfreehd(FS, VAL); \
726 if (lfs_sb_getversion(FS) > 1) { \
727 LFS_CLEANERINFO((CIP), (FS), (BP)); \
728 lfs_ci_setfree_head(FS, CIP, VAL); \
729 LFS_BWRITE_LOG(BP); \
730 mutex_enter(&lfs_lock); \
731 (FS)->lfs_flags |= LFS_IFDIRTY; \
732 mutex_exit(&lfs_lock); \
733 } \
734 } while (0)
735
736 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do { \
737 LFS_CLEANERINFO((CIP), (FS), (BP)); \
738 *(FREEP) = lfs_ci_getfree_tail(FS, CIP); \
739 brelse(BP, 0); \
740 } while (0)
741
742 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do { \
743 LFS_CLEANERINFO((CIP), (FS), (BP)); \
744 lfs_ci_setfree_tail(FS, CIP, VAL); \
745 LFS_BWRITE_LOG(BP); \
746 mutex_enter(&lfs_lock); \
747 (FS)->lfs_flags |= LFS_IFDIRTY; \
748 mutex_exit(&lfs_lock); \
749 } while (0)
750
751 /*
752 * On-disk segment summary information
753 */
754
755 #define SEGSUM_SIZE(fs) \
756 (fs->lfs_is64 ? sizeof(SEGSUM64) : \
757 lfs_sb_getversion(fs) > 1 ? sizeof(SEGSUM32) : sizeof(SEGSUM_V1))
758
759 /*
760 * The SEGSUM structure is followed by FINFO structures. Get the pointer
761 * to the first FINFO.
762 *
763 * XXX this can't be a macro yet; this file needs to be resorted.
764 */
765 #if 0
766 static __unused inline FINFO *
767 segsum_finfobase(STRUCT_LFS *fs, SEGSUM *ssp)
768 {
769 return (FINFO *)((char *)ssp + SEGSUM_SIZE(fs));
770 }
771 #else
772 #define SEGSUM_FINFOBASE(fs, ssp) \
773 ((FINFO *)((char *)(ssp) + SEGSUM_SIZE(fs)));
774 #endif
775
776 #define LFS_DEF_SS_ACCESSOR(type, type32, field) \
777 static __unused inline type \
778 lfs_ss_get##field(STRUCT_LFS *fs, SEGSUM *ssp) \
779 { \
780 if (fs->lfs_is64) { \
781 return ssp->u_64.ss_##field; \
782 } else { \
783 return ssp->u_32.ss_##field; \
784 } \
785 } \
786 static __unused inline void \
787 lfs_ss_set##field(STRUCT_LFS *fs, SEGSUM *ssp, type val) \
788 { \
789 if (fs->lfs_is64) { \
790 type *p = &ssp->u_64.ss_##field; \
791 (void)p; \
792 ssp->u_64.ss_##field = val; \
793 } else { \
794 type32 *p = &ssp->u_32.ss_##field; \
795 (void)p; \
796 ssp->u_32.ss_##field = val; \
797 } \
798 } \
799
800 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, sumsum);
801 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, datasum);
802 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, magic);
803 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, ident);
804 LFS_DEF_SS_ACCESSOR(int64_t, int32_t, next);
805 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, nfinfo);
806 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, ninos);
807 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, flags);
808 LFS_DEF_SS_ACCESSOR(uint64_t, uint32_t, reclino);
809 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, serial);
810 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, create);
811
812 static __unused inline size_t
813 lfs_ss_getsumstart(STRUCT_LFS *fs)
814 {
815 /* These are actually all the same. */
816 if (fs->lfs_is64) {
817 return offsetof(SEGSUM64, ss_datasum);
818 } else /* if (lfs_sb_getversion(fs) > 1) */ {
819 return offsetof(SEGSUM32, ss_datasum);
820 } /* else {
821 return offsetof(SEGSUM_V1, ss_datasum);
822 } */
823 /*
824 * XXX ^^^ until this file is resorted lfs_sb_getversion isn't
825 * defined yet.
826 */
827 }
828
829 static __unused inline uint32_t
830 lfs_ss_getocreate(STRUCT_LFS *fs, SEGSUM *ssp)
831 {
832 KASSERT(fs->lfs_is64 == 0);
833 /* XXX need to resort this file before we can do this */
834 //KASSERT(lfs_sb_getversion(fs) == 1);
835
836 return ssp->u_v1.ss_create;
837 }
838
839 static __unused inline void
840 lfs_ss_setocreate(STRUCT_LFS *fs, SEGSUM *ssp, uint32_t val)
841 {
842 KASSERT(fs->lfs_is64 == 0);
843 /* XXX need to resort this file before we can do this */
844 //KASSERT(lfs_sb_getversion(fs) == 1);
845
846 ssp->u_v1.ss_create = val;
847 }
848
849
850 /*
851 * Super block.
852 */
853
854 /*
855 * Generate accessors for the on-disk superblock fields with cpp.
856 */
857
858 #define LFS_DEF_SB_ACCESSOR_FULL(type, type32, field) \
859 static __unused inline type \
860 lfs_sb_get##field(STRUCT_LFS *fs) \
861 { \
862 if (fs->lfs_is64) { \
863 return fs->lfs_dlfs_u.u_64.dlfs_##field; \
864 } else { \
865 return fs->lfs_dlfs_u.u_32.dlfs_##field; \
866 } \
867 } \
868 static __unused inline void \
869 lfs_sb_set##field(STRUCT_LFS *fs, type val) \
870 { \
871 if (fs->lfs_is64) { \
872 fs->lfs_dlfs_u.u_64.dlfs_##field = val; \
873 } else { \
874 fs->lfs_dlfs_u.u_32.dlfs_##field = val; \
875 } \
876 } \
877 static __unused inline void \
878 lfs_sb_add##field(STRUCT_LFS *fs, type val) \
879 { \
880 if (fs->lfs_is64) { \
881 type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
882 *p64 += val; \
883 } else { \
884 type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
885 *p32 += val; \
886 } \
887 } \
888 static __unused inline void \
889 lfs_sb_sub##field(STRUCT_LFS *fs, type val) \
890 { \
891 if (fs->lfs_is64) { \
892 type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
893 *p64 -= val; \
894 } else { \
895 type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
896 *p32 -= val; \
897 } \
898 }
899
900 #define LFS_DEF_SB_ACCESSOR(t, f) LFS_DEF_SB_ACCESSOR_FULL(t, t, f)
901
902 #define LFS_DEF_SB_ACCESSOR_32ONLY(type, field, val64) \
903 static __unused inline type \
904 lfs_sb_get##field(STRUCT_LFS *fs) \
905 { \
906 if (fs->lfs_is64) { \
907 return val64; \
908 } else { \
909 return fs->lfs_dlfs_u.u_32.dlfs_##field; \
910 } \
911 }
912
913 #define lfs_magic lfs_dlfs.dlfs_magic
914 LFS_DEF_SB_ACCESSOR(u_int32_t, version);
915 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, size);
916 LFS_DEF_SB_ACCESSOR(u_int32_t, ssize);
917 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, dsize);
918 LFS_DEF_SB_ACCESSOR(u_int32_t, bsize);
919 LFS_DEF_SB_ACCESSOR(u_int32_t, fsize);
920 LFS_DEF_SB_ACCESSOR(u_int32_t, frag);
921 LFS_DEF_SB_ACCESSOR_FULL(uint64_t, uint32_t, freehd);
922 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, bfree);
923 LFS_DEF_SB_ACCESSOR_FULL(uint64_t, uint32_t, nfiles);
924 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, avail);
925 LFS_DEF_SB_ACCESSOR(int32_t, uinodes);
926 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, idaddr);
927 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, ifile, LFS_IFILE_INUM);
928 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastseg);
929 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, nextseg);
930 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, curseg);
931 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, offset);
932 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastpseg);
933 LFS_DEF_SB_ACCESSOR(u_int32_t, inopf);
934 LFS_DEF_SB_ACCESSOR(u_int32_t, minfree);
935 LFS_DEF_SB_ACCESSOR(uint64_t, maxfilesize);
936 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbpseg);
937 LFS_DEF_SB_ACCESSOR(u_int32_t, inopb);
938 LFS_DEF_SB_ACCESSOR(u_int32_t, ifpb);
939 LFS_DEF_SB_ACCESSOR(u_int32_t, sepb);
940 LFS_DEF_SB_ACCESSOR(u_int32_t, nindir);
941 LFS_DEF_SB_ACCESSOR(u_int32_t, nseg);
942 LFS_DEF_SB_ACCESSOR(u_int32_t, nspf);
943 LFS_DEF_SB_ACCESSOR(u_int32_t, cleansz);
944 LFS_DEF_SB_ACCESSOR(u_int32_t, segtabsz);
945 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segmask, 0);
946 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segshift, 0);
947 LFS_DEF_SB_ACCESSOR(u_int64_t, bmask);
948 LFS_DEF_SB_ACCESSOR(u_int32_t, bshift);
949 LFS_DEF_SB_ACCESSOR(u_int64_t, ffmask);
950 LFS_DEF_SB_ACCESSOR(u_int32_t, ffshift);
951 LFS_DEF_SB_ACCESSOR(u_int64_t, fbmask);
952 LFS_DEF_SB_ACCESSOR(u_int32_t, fbshift);
953 LFS_DEF_SB_ACCESSOR(u_int32_t, blktodb);
954 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbtodb);
955 LFS_DEF_SB_ACCESSOR(u_int32_t, sushift);
956 LFS_DEF_SB_ACCESSOR(int32_t, maxsymlinklen);
957 LFS_DEF_SB_ACCESSOR(u_int32_t, cksum);
958 LFS_DEF_SB_ACCESSOR(u_int16_t, pflags);
959 LFS_DEF_SB_ACCESSOR(u_int32_t, nclean);
960 LFS_DEF_SB_ACCESSOR(int32_t, dmeta);
961 LFS_DEF_SB_ACCESSOR(u_int32_t, minfreeseg);
962 LFS_DEF_SB_ACCESSOR(u_int32_t, sumsize);
963 LFS_DEF_SB_ACCESSOR(u_int64_t, serial);
964 LFS_DEF_SB_ACCESSOR(u_int32_t, ibsize);
965 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, s0addr);
966 LFS_DEF_SB_ACCESSOR(u_int64_t, tstamp);
967 LFS_DEF_SB_ACCESSOR(u_int32_t, inodefmt);
968 LFS_DEF_SB_ACCESSOR(u_int32_t, interleave);
969 LFS_DEF_SB_ACCESSOR(u_int32_t, ident);
970 LFS_DEF_SB_ACCESSOR(u_int32_t, resvseg);
971
972 /* special-case accessors */
973
974 /*
975 * the v1 otstamp field lives in what's now dlfs_inopf
976 */
977 #define lfs_sb_getotstamp(fs) lfs_sb_getinopf(fs)
978 #define lfs_sb_setotstamp(fs, val) lfs_sb_setinopf(fs, val)
979
980 /*
981 * lfs_sboffs is an array
982 */
983 static __unused inline int32_t
984 lfs_sb_getsboff(STRUCT_LFS *fs, unsigned n)
985 {
986 #ifdef KASSERT /* ugh */
987 KASSERT(n < LFS_MAXNUMSB);
988 #endif
989 if (fs->lfs_is64) {
990 return fs->lfs_dlfs_u.u_64.dlfs_sboffs[n];
991 } else {
992 return fs->lfs_dlfs_u.u_32.dlfs_sboffs[n];
993 }
994 }
995 static __unused inline void
996 lfs_sb_setsboff(STRUCT_LFS *fs, unsigned n, int32_t val)
997 {
998 #ifdef KASSERT /* ugh */
999 KASSERT(n < LFS_MAXNUMSB);
1000 #endif
1001 if (fs->lfs_is64) {
1002 fs->lfs_dlfs_u.u_64.dlfs_sboffs[n] = val;
1003 } else {
1004 fs->lfs_dlfs_u.u_32.dlfs_sboffs[n] = val;
1005 }
1006 }
1007
1008 /*
1009 * lfs_fsmnt is a string
1010 */
1011 static __unused inline const char *
1012 lfs_sb_getfsmnt(STRUCT_LFS *fs)
1013 {
1014 if (fs->lfs_is64) {
1015 return fs->lfs_dlfs_u.u_64.dlfs_fsmnt;
1016 } else {
1017 return fs->lfs_dlfs_u.u_32.dlfs_fsmnt;
1018 }
1019 }
1020
1021 static __unused inline void
1022 lfs_sb_setfsmnt(STRUCT_LFS *fs, const char *str)
1023 {
1024 if (fs->lfs_is64) {
1025 (void)strncpy(fs->lfs_dlfs_u.u_64.dlfs_fsmnt, str,
1026 sizeof(fs->lfs_dlfs_u.u_64.dlfs_fsmnt));
1027 } else {
1028 (void)strncpy(fs->lfs_dlfs_u.u_32.dlfs_fsmnt, str,
1029 sizeof(fs->lfs_dlfs_u.u_32.dlfs_fsmnt));
1030 }
1031 }
1032
1033 /* Highest addressable fsb */
1034 #define LFS_MAX_DADDR(fs) \
1035 ((fs)->lfs_is64 ? 0x7fffffffffffffff : 0x7fffffff)
1036
1037 /* LFS_NINDIR is the number of indirects in a file system block. */
1038 #define LFS_NINDIR(fs) (lfs_sb_getnindir(fs))
1039
1040 /* LFS_INOPB is the number of inodes in a secondary storage block. */
1041 #define LFS_INOPB(fs) (lfs_sb_getinopb(fs))
1042 /* LFS_INOPF is the number of inodes in a fragment. */
1043 #define LFS_INOPF(fs) (lfs_sb_getinopf(fs))
1044
1045 #define lfs_blkoff(fs, loc) ((int)((loc) & lfs_sb_getbmask(fs)))
1046 #define lfs_fragoff(fs, loc) /* calculates (loc % fs->lfs_fsize) */ \
1047 ((int)((loc) & lfs_sb_getffmask(fs)))
1048
1049 /* XXX: lowercase these as they're no longer macros */
1050 /* Frags to diskblocks */
1051 static __unused inline uint64_t
1052 LFS_FSBTODB(STRUCT_LFS *fs, uint64_t b)
1053 {
1054 #if defined(_KERNEL)
1055 return b << (lfs_sb_getffshift(fs) - DEV_BSHIFT);
1056 #else
1057 return b << lfs_sb_getfsbtodb(fs);
1058 #endif
1059 }
1060 /* Diskblocks to frags */
1061 static __unused inline uint64_t
1062 LFS_DBTOFSB(STRUCT_LFS *fs, uint64_t b)
1063 {
1064 #if defined(_KERNEL)
1065 return b >> (lfs_sb_getffshift(fs) - DEV_BSHIFT);
1066 #else
1067 return b >> lfs_sb_getfsbtodb(fs);
1068 #endif
1069 }
1070
1071 #define lfs_lblkno(fs, loc) ((loc) >> lfs_sb_getbshift(fs))
1072 #define lfs_lblktosize(fs, blk) ((blk) << lfs_sb_getbshift(fs))
1073
1074 /* Frags to bytes */
1075 static __unused inline uint64_t
1076 lfs_fsbtob(STRUCT_LFS *fs, uint64_t b)
1077 {
1078 return b << lfs_sb_getffshift(fs);
1079 }
1080 /* Bytes to frags */
1081 static __unused inline uint64_t
1082 lfs_btofsb(STRUCT_LFS *fs, uint64_t b)
1083 {
1084 return b >> lfs_sb_getffshift(fs);
1085 }
1086
1087 #define lfs_numfrags(fs, loc) /* calculates (loc / fs->lfs_fsize) */ \
1088 ((loc) >> lfs_sb_getffshift(fs))
1089 #define lfs_blkroundup(fs, size)/* calculates roundup(size, lfs_sb_getbsize(fs)) */ \
1090 ((off_t)(((size) + lfs_sb_getbmask(fs)) & (~lfs_sb_getbmask(fs))))
1091 #define lfs_fragroundup(fs, size)/* calculates roundup(size, fs->lfs_fsize) */ \
1092 ((off_t)(((size) + lfs_sb_getffmask(fs)) & (~lfs_sb_getffmask(fs))))
1093 #define lfs_fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
1094 ((frags) >> lfs_sb_getfbshift(fs))
1095 #define lfs_blkstofrags(fs, blks)/* calculates (blks * fs->fs_frag) */ \
1096 ((blks) << lfs_sb_getfbshift(fs))
1097 #define lfs_fragnum(fs, fsb) /* calculates (fsb % fs->lfs_frag) */ \
1098 ((fsb) & ((fs)->lfs_frag - 1))
1099 #define lfs_blknum(fs, fsb) /* calculates rounddown(fsb, fs->lfs_frag) */ \
1100 ((fsb) &~ ((fs)->lfs_frag - 1))
1101 #define lfs_dblksize(fs, dp, lbn) \
1102 (((lbn) >= ULFS_NDADDR || lfs_dino_getsize(fs, dp) >= ((lbn) + 1) << lfs_sb_getbshift(fs)) \
1103 ? lfs_sb_getbsize(fs) \
1104 : (lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, dp)))))
1105
1106 #define lfs_segsize(fs) (lfs_sb_getversion(fs) == 1 ? \
1107 lfs_lblktosize((fs), lfs_sb_getssize(fs)) : \
1108 lfs_sb_getssize(fs))
1109 /* XXX segtod produces a result in frags despite the 'd' */
1110 #define lfs_segtod(fs, seg) (lfs_btofsb(fs, lfs_segsize(fs)) * (seg))
1111 #define lfs_dtosn(fs, daddr) /* block address to segment number */ \
1112 ((uint32_t)(((daddr) - lfs_sb_gets0addr(fs)) / lfs_segtod((fs), 1)))
1113 #define lfs_sntod(fs, sn) /* segment number to disk address */ \
1114 ((daddr_t)(lfs_segtod((fs), (sn)) + lfs_sb_gets0addr(fs)))
1115
1116 /* XXX, blah. make this appear only if struct inode is defined */
1117 #ifdef _UFS_LFS_LFS_INODE_H_
1118 static __unused inline uint32_t
1119 lfs_blksize(STRUCT_LFS *fs, struct inode *ip, uint64_t lbn)
1120 {
1121 if (lbn >= ULFS_NDADDR || lfs_dino_getsize(fs, ip->i_din) >= (lbn + 1) << lfs_sb_getbshift(fs)) {
1122 return lfs_sb_getbsize(fs);
1123 } else {
1124 return lfs_fragroundup(fs, lfs_blkoff(fs, lfs_dino_getsize(fs, ip->i_din)));
1125 }
1126 }
1127 #endif
1128
1129 /*
1130 * union lfs_blocks
1131 */
1132
1133 static __unused inline void
1134 lfs_blocks_fromvoid(STRUCT_LFS *fs, union lfs_blocks *bp, void *p)
1135 {
1136 if (fs->lfs_is64) {
1137 bp->b64 = p;
1138 } else {
1139 bp->b32 = p;
1140 }
1141 }
1142
1143 static __unused inline void
1144 lfs_blocks_fromfinfo(STRUCT_LFS *fs, union lfs_blocks *bp, FINFO *fip)
1145 {
1146 void *firstblock;
1147
1148 firstblock = (char *)fip + FINFOSIZE(fs);
1149 if (fs->lfs_is64) {
1150 bp->b64 = (int64_t *)firstblock;
1151 } else {
1152 bp->b32 = (int32_t *)firstblock;
1153 }
1154 }
1155
1156 static __unused inline daddr_t
1157 lfs_blocks_get(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index)
1158 {
1159 if (fs->lfs_is64) {
1160 return bp->b64[index];
1161 } else {
1162 return bp->b32[index];
1163 }
1164 }
1165
1166 static __unused inline void
1167 lfs_blocks_set(STRUCT_LFS *fs, union lfs_blocks *bp, unsigned index, daddr_t val)
1168 {
1169 if (fs->lfs_is64) {
1170 bp->b64[index] = val;
1171 } else {
1172 bp->b32[index] = val;
1173 }
1174 }
1175
1176 static __unused inline void
1177 lfs_blocks_inc(STRUCT_LFS *fs, union lfs_blocks *bp)
1178 {
1179 if (fs->lfs_is64) {
1180 bp->b64++;
1181 } else {
1182 bp->b32++;
1183 }
1184 }
1185
1186 static __unused inline int
1187 lfs_blocks_eq(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
1188 {
1189 if (fs->lfs_is64) {
1190 return bp1->b64 == bp2->b64;
1191 } else {
1192 return bp1->b32 == bp2->b32;
1193 }
1194 }
1195
1196 static __unused inline int
1197 lfs_blocks_sub(STRUCT_LFS *fs, union lfs_blocks *bp1, union lfs_blocks *bp2)
1198 {
1199 /* (remember that the pointers are typed) */
1200 if (fs->lfs_is64) {
1201 return bp1->b64 - bp2->b64;
1202 } else {
1203 return bp1->b32 - bp2->b32;
1204 }
1205 }
1206
1207 /*
1208 * struct segment
1209 */
1210
1211
1212 /*
1213 * Macros for determining free space on the disk, with the variable metadata
1214 * of segment summaries and inode blocks taken into account.
1215 */
1216 /*
1217 * Estimate number of clean blocks not available for writing because
1218 * they will contain metadata or overhead. This is calculated as
1219 *
1220 * E = ((C * M / D) * D + (0) * (T - D)) / T
1221 * or more simply
1222 * E = (C * M) / T
1223 *
1224 * where
1225 * C is the clean space,
1226 * D is the dirty space,
1227 * M is the dirty metadata, and
1228 * T = C + D is the total space on disk.
1229 *
1230 * This approximates the old formula of E = C * M / D when D is close to T,
1231 * but avoids falsely reporting "disk full" when the sample size (D) is small.
1232 */
1233 #define LFS_EST_CMETA(F) (int32_t)(( \
1234 (lfs_sb_getdmeta(F) * (int64_t)lfs_sb_getnclean(F)) / \
1235 (lfs_sb_getnseg(F))))
1236
1237 /* Estimate total size of the disk not including metadata */
1238 #define LFS_EST_NONMETA(F) (lfs_sb_getdsize(F) - lfs_sb_getdmeta(F) - LFS_EST_CMETA(F))
1239
1240 /* Estimate number of blocks actually available for writing */
1241 #define LFS_EST_BFREE(F) (lfs_sb_getbfree(F) > LFS_EST_CMETA(F) ? \
1242 lfs_sb_getbfree(F) - LFS_EST_CMETA(F) : 0)
1243
1244 /* Amount of non-meta space not available to mortal man */
1245 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) * \
1246 (u_int64_t)lfs_sb_getminfree(F)) / \
1247 100)
1248
1249 /* Can credential C write BB blocks? XXX: kauth_cred_geteuid is abusive */
1250 #define ISSPACE(F, BB, C) \
1251 ((((C) == NOCRED || kauth_cred_geteuid(C) == 0) && \
1252 LFS_EST_BFREE(F) >= (BB)) || \
1253 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
1254
1255 /* Can an ordinary user write BB blocks */
1256 #define IS_FREESPACE(F, BB) \
1257 (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
1258
1259 /*
1260 * The minimum number of blocks to create a new inode. This is:
1261 * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
1262 * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
1263 */
1264 #define LFS_NRESERVE(F) (lfs_btofsb((F), (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(F)))
1265
1266
1267
1268 #endif /* _UFS_LFS_LFS_ACCESSORS_H_ */
1269