lfs_accessors.h revision 1.11 1 /* $NetBSD: lfs_accessors.h,v 1.11 2015/08/12 18:26:27 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) && !defined(_STANDALONE)
149 #include <assert.h>
150 #define KASSERT assert
151 #endif
152
153 /*
154 * STRUCT_LFS is used by the libsa code to get accessors that work
155 * with struct salfs instead of struct lfs, and by the cleaner to
156 * get accessors that work with struct clfs.
157 */
158
159 #ifndef STRUCT_LFS
160 #define STRUCT_LFS struct lfs
161 #endif
162
163
164 /*
165 * Maximum length of a symlink that can be stored within the inode.
166 */
167 #define ULFS1_MAXSYMLINKLEN ((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int32_t))
168 #define ULFS2_MAXSYMLINKLEN ((ULFS_NDADDR + ULFS_NIADDR) * sizeof(int64_t))
169
170 #define ULFS_MAXSYMLINKLEN(ip) \
171 ((ip)->i_ump->um_fstype == ULFS1) ? \
172 ULFS1_MAXSYMLINKLEN : ULFS2_MAXSYMLINKLEN
173
174 /*
175 * "struct buf" associated definitions
176 */
177
178 # define LFS_LOCK_BUF(bp) do { \
179 if (((bp)->b_flags & B_LOCKED) == 0 && bp->b_iodone == NULL) { \
180 mutex_enter(&lfs_lock); \
181 ++locked_queue_count; \
182 locked_queue_bytes += bp->b_bufsize; \
183 mutex_exit(&lfs_lock); \
184 } \
185 (bp)->b_flags |= B_LOCKED; \
186 } while (0)
187
188 # define LFS_UNLOCK_BUF(bp) do { \
189 if (((bp)->b_flags & B_LOCKED) != 0 && bp->b_iodone == NULL) { \
190 mutex_enter(&lfs_lock); \
191 --locked_queue_count; \
192 locked_queue_bytes -= bp->b_bufsize; \
193 if (locked_queue_count < LFS_WAIT_BUFS && \
194 locked_queue_bytes < LFS_WAIT_BYTES) \
195 cv_broadcast(&locked_queue_cv); \
196 mutex_exit(&lfs_lock); \
197 } \
198 (bp)->b_flags &= ~B_LOCKED; \
199 } while (0)
200
201 /*
202 * "struct inode" associated definitions
203 */
204
205 #define LFS_SET_UINO(ip, flags) do { \
206 if (((flags) & IN_ACCESSED) && !((ip)->i_flag & IN_ACCESSED)) \
207 lfs_sb_adduinodes((ip)->i_lfs, 1); \
208 if (((flags) & IN_CLEANING) && !((ip)->i_flag & IN_CLEANING)) \
209 lfs_sb_adduinodes((ip)->i_lfs, 1); \
210 if (((flags) & IN_MODIFIED) && !((ip)->i_flag & IN_MODIFIED)) \
211 lfs_sb_adduinodes((ip)->i_lfs, 1); \
212 (ip)->i_flag |= (flags); \
213 } while (0)
214
215 #define LFS_CLR_UINO(ip, flags) do { \
216 if (((flags) & IN_ACCESSED) && ((ip)->i_flag & IN_ACCESSED)) \
217 lfs_sb_subuinodes((ip)->i_lfs, 1); \
218 if (((flags) & IN_CLEANING) && ((ip)->i_flag & IN_CLEANING)) \
219 lfs_sb_subuinodes((ip)->i_lfs, 1); \
220 if (((flags) & IN_MODIFIED) && ((ip)->i_flag & IN_MODIFIED)) \
221 lfs_sb_subuinodes((ip)->i_lfs, 1); \
222 (ip)->i_flag &= ~(flags); \
223 if (lfs_sb_getuinodes((ip)->i_lfs) < 0) { \
224 panic("lfs_uinodes < 0"); \
225 } \
226 } while (0)
227
228 #define LFS_ITIMES(ip, acc, mod, cre) \
229 while ((ip)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
230 lfs_itimes(ip, acc, mod, cre)
231
232 /*
233 * On-disk and in-memory checkpoint segment usage structure.
234 */
235
236 #define SEGUPB(fs) (lfs_sb_getsepb(fs))
237 #define SEGTABSIZE_SU(fs) \
238 ((lfs_sb_getnseg(fs) + SEGUPB(fs) - 1) / lfs_sb_getsepb(fs))
239
240 #ifdef _KERNEL
241 # define SHARE_IFLOCK(F) \
242 do { \
243 rw_enter(&(F)->lfs_iflock, RW_READER); \
244 } while(0)
245 # define UNSHARE_IFLOCK(F) \
246 do { \
247 rw_exit(&(F)->lfs_iflock); \
248 } while(0)
249 #else /* ! _KERNEL */
250 # define SHARE_IFLOCK(F)
251 # define UNSHARE_IFLOCK(F)
252 #endif /* ! _KERNEL */
253
254 /* Read in the block with a specific segment usage entry from the ifile. */
255 #define LFS_SEGENTRY(SP, F, IN, BP) do { \
256 int _e; \
257 SHARE_IFLOCK(F); \
258 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
259 if ((_e = bread((F)->lfs_ivnode, \
260 ((IN) / lfs_sb_getsepb(F)) + lfs_sb_getcleansz(F), \
261 lfs_sb_getbsize(F), 0, &(BP))) != 0) \
262 panic("lfs: ifile read: %d", _e); \
263 if (lfs_sb_getversion(F) == 1) \
264 (SP) = (SEGUSE *)((SEGUSE_V1 *)(BP)->b_data + \
265 ((IN) & (lfs_sb_getsepb(F) - 1))); \
266 else \
267 (SP) = (SEGUSE *)(BP)->b_data + ((IN) % lfs_sb_getsepb(F)); \
268 UNSHARE_IFLOCK(F); \
269 } while (0)
270
271 #define LFS_WRITESEGENTRY(SP, F, IN, BP) do { \
272 if ((SP)->su_nbytes == 0) \
273 (SP)->su_flags |= SEGUSE_EMPTY; \
274 else \
275 (SP)->su_flags &= ~SEGUSE_EMPTY; \
276 (F)->lfs_suflags[(F)->lfs_activesb][(IN)] = (SP)->su_flags; \
277 LFS_BWRITE_LOG(BP); \
278 } while (0)
279
280 /*
281 * FINFO (file info) entries.
282 */
283
284 /* Size of an on-disk block pointer, e.g. in an indirect block. */
285 /* XXX: move to a more suitable location in this file */
286 #define LFS_BLKPTRSIZE(fs) ((fs)->lfs_is64 ? sizeof(int64_t) : sizeof(int32_t))
287
288 /* Full size of the provided FINFO record, including its block pointers. */
289 #define FINFO_FULLSIZE(fs, fip) \
290 (FINFOSIZE + (fip)->fi_nblocks * LFS_BLKPTRSIZE(fs))
291
292 #define NEXT_FINFO(fs, fip) \
293 ((FINFO *)((char *)(fip) + FINFO_FULLSIZE(fs, fip)))
294
295 /*
296 * Index file inode entries.
297 */
298
299 /*
300 * LFSv1 compatibility code is not allowed to touch if_atime, since it
301 * may not be mapped!
302 */
303 /* Read in the block with a specific inode from the ifile. */
304 #define LFS_IENTRY(IP, F, IN, BP) do { \
305 int _e; \
306 SHARE_IFLOCK(F); \
307 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
308 if ((_e = bread((F)->lfs_ivnode, \
309 (IN) / lfs_sb_getifpb(F) + lfs_sb_getcleansz(F) + lfs_sb_getsegtabsz(F), \
310 lfs_sb_getbsize(F), 0, &(BP))) != 0) \
311 panic("lfs: ifile ino %d read %d", (int)(IN), _e); \
312 if ((F)->lfs_is64) { \
313 (IP) = (IFILE *)((IFILE64 *)(BP)->b_data + \
314 (IN) % lfs_sb_getifpb(F)); \
315 } else if (lfs_sb_getversion(F) > 1) { \
316 (IP) = (IFILE *)((IFILE32 *)(BP)->b_data + \
317 (IN) % lfs_sb_getifpb(F)); \
318 } else { \
319 (IP) = (IFILE *)((IFILE_V1 *)(BP)->b_data + \
320 (IN) % lfs_sb_getifpb(F)); \
321 } \
322 UNSHARE_IFLOCK(F); \
323 } while (0)
324
325 #define LFS_DEF_IF_ACCESSOR(type, type32, field) \
326 static __unused inline type \
327 lfs_if_get##field(STRUCT_LFS *fs, IFILE *ifp) \
328 { \
329 if (fs->lfs_is64) { \
330 return ifp->u_64.if_##field; \
331 } else { \
332 return ifp->u_32.if_##field; \
333 } \
334 } \
335 static __unused inline void \
336 lfs_if_set##field(STRUCT_LFS *fs, IFILE *ifp, type val) \
337 { \
338 if (fs->lfs_is64) { \
339 type *p = &ifp->u_64.if_##field; \
340 (void)p; \
341 ifp->u_64.if_##field = val; \
342 } else { \
343 type32 *p = &ifp->u_32.if_##field; \
344 (void)p; \
345 ifp->u_32.if_##field = val; \
346 } \
347 } \
348
349 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, version);
350 LFS_DEF_IF_ACCESSOR(int64_t, int32_t, daddr);
351 LFS_DEF_IF_ACCESSOR(u_int64_t, u_int32_t, nextfree);
352 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_sec);
353 LFS_DEF_IF_ACCESSOR(u_int32_t, u_int32_t, atime_nsec);
354
355 /*
356 * Cleaner information structure. This resides in the ifile and is used
357 * to pass information from the kernel to the cleaner.
358 */
359
360 #define CLEANSIZE_SU(fs) \
361 ((((fs)->lfs_is64 ? sizeof(CLEANERINFO64) : sizeof(CLEANERINFO32)) + \
362 lfs_sb_getbsize(fs) - 1) >> lfs_sb_getbshift(fs))
363
364 #define LFS_DEF_CI_ACCESSOR(type, type32, field) \
365 static __unused inline type \
366 lfs_ci_get##field(STRUCT_LFS *fs, CLEANERINFO *cip) \
367 { \
368 if (fs->lfs_is64) { \
369 return cip->u_64.field; \
370 } else { \
371 return cip->u_32.field; \
372 } \
373 } \
374 static __unused inline void \
375 lfs_ci_set##field(STRUCT_LFS *fs, CLEANERINFO *cip, type val) \
376 { \
377 if (fs->lfs_is64) { \
378 type *p = &cip->u_64.field; \
379 (void)p; \
380 cip->u_64.field = val; \
381 } else { \
382 type32 *p = &cip->u_32.field; \
383 (void)p; \
384 cip->u_32.field = val; \
385 } \
386 } \
387
388 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, clean);
389 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, dirty);
390 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, bfree);
391 LFS_DEF_CI_ACCESSOR(int64_t, int32_t, avail);
392 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_head);
393 LFS_DEF_CI_ACCESSOR(u_int64_t, u_int32_t, free_tail);
394 LFS_DEF_CI_ACCESSOR(u_int32_t, u_int32_t, flags);
395
396 static __unused inline void
397 lfs_ci_shiftcleantodirty(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
398 {
399 lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) - num);
400 lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) + num);
401 }
402
403 static __unused inline void
404 lfs_ci_shiftdirtytoclean(STRUCT_LFS *fs, CLEANERINFO *cip, unsigned num)
405 {
406 lfs_ci_setdirty(fs, cip, lfs_ci_getdirty(fs, cip) - num);
407 lfs_ci_setclean(fs, cip, lfs_ci_getclean(fs, cip) + num);
408 }
409
410 /* Read in the block with the cleaner info from the ifile. */
411 #define LFS_CLEANERINFO(CP, F, BP) do { \
412 SHARE_IFLOCK(F); \
413 VTOI((F)->lfs_ivnode)->i_flag |= IN_ACCESS; \
414 if (bread((F)->lfs_ivnode, \
415 (daddr_t)0, lfs_sb_getbsize(F), 0, &(BP))) \
416 panic("lfs: ifile read"); \
417 (CP) = (CLEANERINFO *)(BP)->b_data; \
418 UNSHARE_IFLOCK(F); \
419 } while (0)
420
421 /*
422 * Synchronize the Ifile cleaner info with current avail and bfree.
423 */
424 #define LFS_SYNC_CLEANERINFO(cip, fs, bp, w) do { \
425 mutex_enter(&lfs_lock); \
426 if ((w) || lfs_ci_getbfree(fs, cip) != lfs_sb_getbfree(fs) || \
427 lfs_ci_getavail(fs, cip) != lfs_sb_getavail(fs) - fs->lfs_ravail - \
428 fs->lfs_favail) { \
429 lfs_ci_setbfree(fs, cip, lfs_sb_getbfree(fs)); \
430 lfs_ci_setavail(fs, cip, lfs_sb_getavail(fs) - fs->lfs_ravail - \
431 fs->lfs_favail); \
432 if (((bp)->b_flags & B_GATHERED) == 0) { \
433 fs->lfs_flags |= LFS_IFDIRTY; \
434 } \
435 mutex_exit(&lfs_lock); \
436 (void) LFS_BWRITE_LOG(bp); /* Ifile */ \
437 } else { \
438 mutex_exit(&lfs_lock); \
439 brelse(bp, 0); \
440 } \
441 } while (0)
442
443 /*
444 * Get the head of the inode free list.
445 * Always called with the segment lock held.
446 */
447 #define LFS_GET_HEADFREE(FS, CIP, BP, FREEP) do { \
448 if (lfs_sb_getversion(FS) > 1) { \
449 LFS_CLEANERINFO((CIP), (FS), (BP)); \
450 lfs_sb_setfreehd(FS, lfs_ci_getfree_head(FS, CIP)); \
451 brelse(BP, 0); \
452 } \
453 *(FREEP) = lfs_sb_getfreehd(FS); \
454 } while (0)
455
456 #define LFS_PUT_HEADFREE(FS, CIP, BP, VAL) do { \
457 lfs_sb_setfreehd(FS, VAL); \
458 if (lfs_sb_getversion(FS) > 1) { \
459 LFS_CLEANERINFO((CIP), (FS), (BP)); \
460 lfs_ci_setfree_head(FS, CIP, VAL); \
461 LFS_BWRITE_LOG(BP); \
462 mutex_enter(&lfs_lock); \
463 (FS)->lfs_flags |= LFS_IFDIRTY; \
464 mutex_exit(&lfs_lock); \
465 } \
466 } while (0)
467
468 #define LFS_GET_TAILFREE(FS, CIP, BP, FREEP) do { \
469 LFS_CLEANERINFO((CIP), (FS), (BP)); \
470 *(FREEP) = lfs_ci_getfree_tail(FS, CIP); \
471 brelse(BP, 0); \
472 } while (0)
473
474 #define LFS_PUT_TAILFREE(FS, CIP, BP, VAL) do { \
475 LFS_CLEANERINFO((CIP), (FS), (BP)); \
476 lfs_ci_setfree_tail(FS, CIP, VAL); \
477 LFS_BWRITE_LOG(BP); \
478 mutex_enter(&lfs_lock); \
479 (FS)->lfs_flags |= LFS_IFDIRTY; \
480 mutex_exit(&lfs_lock); \
481 } while (0)
482
483 /*
484 * On-disk segment summary information
485 */
486
487 #define SEGSUM_SIZE(fs) \
488 (fs->lfs_is64 ? sizeof(SEGSUM64) : \
489 lfs_sb_getversion(fs) > 1 ? sizeof(SEGSUM32) : sizeof(SEGSUM_V1))
490
491 /*
492 * The SEGSUM structure is followed by FINFO structures. Get the pointer
493 * to the first FINFO.
494 *
495 * XXX this can't be a macro yet; this file needs to be resorted.
496 */
497 #if 0
498 static __unused inline FINFO *
499 segsum_finfobase(STRUCT_LFS *fs, SEGSUM *ssp)
500 {
501 return (FINFO *)((char *)ssp) + SEGSUM_SIZE(fs);
502 }
503 #else
504 #define SEGSUM_FINFOBASE(fs, ssp) \
505 ((FINFO *)((char *)(ssp)) + SEGSUM_SIZE(fs));
506 #endif
507
508 #define LFS_DEF_SS_ACCESSOR(type, type32, field) \
509 static __unused inline type \
510 lfs_ss_get##field(STRUCT_LFS *fs, SEGSUM *ssp) \
511 { \
512 if (fs->lfs_is64) { \
513 return ssp->u_64.ss_##field; \
514 } else { \
515 return ssp->u_32.ss_##field; \
516 } \
517 } \
518 static __unused inline void \
519 lfs_ss_set##field(STRUCT_LFS *fs, SEGSUM *ssp, type val) \
520 { \
521 if (fs->lfs_is64) { \
522 type *p = &ssp->u_64.ss_##field; \
523 (void)p; \
524 ssp->u_64.ss_##field = val; \
525 } else { \
526 type32 *p = &ssp->u_32.ss_##field; \
527 (void)p; \
528 ssp->u_32.ss_##field = val; \
529 } \
530 } \
531
532 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, sumsum);
533 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, datasum);
534 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, magic);
535 LFS_DEF_SS_ACCESSOR(uint32_t, uint32_t, ident);
536 LFS_DEF_SS_ACCESSOR(int64_t, int32_t, next);
537 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, nfinfo);
538 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, ninos);
539 LFS_DEF_SS_ACCESSOR(uint16_t, uint16_t, flags);
540 LFS_DEF_SS_ACCESSOR(uint64_t, uint32_t, reclino);
541 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, serial);
542 LFS_DEF_SS_ACCESSOR(uint64_t, uint64_t, create);
543
544 static __unused inline size_t
545 lfs_ss_getsumstart(STRUCT_LFS *fs)
546 {
547 /* These are actually all the same. */
548 if (fs->lfs_is64) {
549 return offsetof(SEGSUM64, ss_datasum);
550 } else /* if (lfs_sb_getversion(fs) > 1) */ {
551 return offsetof(SEGSUM32, ss_datasum);
552 } /* else {
553 return offsetof(SEGSUM_V1, ss_datasum);
554 } */
555 /*
556 * XXX ^^^ until this file is resorted lfs_sb_getversion isn't
557 * defined yet.
558 */
559 }
560
561 static __unused inline uint32_t
562 lfs_ss_getocreate(STRUCT_LFS *fs, SEGSUM *ssp)
563 {
564 KASSERT(fs->lfs_is64 == 0);
565 /* XXX need to resort this file before we can do this */
566 //KASSERT(lfs_sb_getversion(fs) == 1);
567
568 return ssp->u_v1.ss_create;
569 }
570
571 static __unused inline void
572 lfs_ss_setocreate(STRUCT_LFS *fs, SEGSUM *ssp, uint32_t val)
573 {
574 KASSERT(fs->lfs_is64 == 0);
575 /* XXX need to resort this file before we can do this */
576 //KASSERT(lfs_sb_getversion(fs) == 1);
577
578 ssp->u_v1.ss_create = val;
579 }
580
581
582 /*
583 * Super block.
584 */
585
586 /*
587 * Generate accessors for the on-disk superblock fields with cpp.
588 */
589
590 #define LFS_DEF_SB_ACCESSOR_FULL(type, type32, field) \
591 static __unused inline type \
592 lfs_sb_get##field(STRUCT_LFS *fs) \
593 { \
594 if (fs->lfs_is64) { \
595 return fs->lfs_dlfs_u.u_64.dlfs_##field; \
596 } else { \
597 return fs->lfs_dlfs_u.u_32.dlfs_##field; \
598 } \
599 } \
600 static __unused inline void \
601 lfs_sb_set##field(STRUCT_LFS *fs, type val) \
602 { \
603 if (fs->lfs_is64) { \
604 fs->lfs_dlfs_u.u_64.dlfs_##field = val; \
605 } else { \
606 fs->lfs_dlfs_u.u_32.dlfs_##field = val; \
607 } \
608 } \
609 static __unused inline void \
610 lfs_sb_add##field(STRUCT_LFS *fs, type val) \
611 { \
612 if (fs->lfs_is64) { \
613 type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
614 *p64 += val; \
615 } else { \
616 type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
617 *p32 += val; \
618 } \
619 } \
620 static __unused inline void \
621 lfs_sb_sub##field(STRUCT_LFS *fs, type val) \
622 { \
623 if (fs->lfs_is64) { \
624 type *p64 = &fs->lfs_dlfs_u.u_64.dlfs_##field; \
625 *p64 -= val; \
626 } else { \
627 type32 *p32 = &fs->lfs_dlfs_u.u_32.dlfs_##field; \
628 *p32 -= val; \
629 } \
630 }
631
632 #define LFS_DEF_SB_ACCESSOR(t, f) LFS_DEF_SB_ACCESSOR_FULL(t, t, f)
633
634 #define LFS_DEF_SB_ACCESSOR_32ONLY(type, field, val64) \
635 static __unused inline type \
636 lfs_sb_get##field(STRUCT_LFS *fs) \
637 { \
638 if (fs->lfs_is64) { \
639 return val64; \
640 } else { \
641 return fs->lfs_dlfs_u.u_32.dlfs_##field; \
642 } \
643 }
644
645 #define lfs_magic lfs_dlfs.dlfs_magic
646 LFS_DEF_SB_ACCESSOR(u_int32_t, version);
647 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, size);
648 LFS_DEF_SB_ACCESSOR(u_int32_t, ssize);
649 LFS_DEF_SB_ACCESSOR_FULL(u_int64_t, u_int32_t, dsize);
650 LFS_DEF_SB_ACCESSOR(u_int32_t, bsize);
651 LFS_DEF_SB_ACCESSOR(u_int32_t, fsize);
652 LFS_DEF_SB_ACCESSOR(u_int32_t, frag);
653 LFS_DEF_SB_ACCESSOR(u_int32_t, freehd);
654 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, bfree);
655 LFS_DEF_SB_ACCESSOR(u_int32_t, nfiles);
656 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, avail);
657 LFS_DEF_SB_ACCESSOR(int32_t, uinodes);
658 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, idaddr);
659 LFS_DEF_SB_ACCESSOR(u_int32_t, ifile);
660 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastseg);
661 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, nextseg);
662 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, curseg);
663 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, offset);
664 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, lastpseg);
665 LFS_DEF_SB_ACCESSOR(u_int32_t, inopf);
666 LFS_DEF_SB_ACCESSOR(u_int32_t, minfree);
667 LFS_DEF_SB_ACCESSOR(uint64_t, maxfilesize);
668 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbpseg);
669 LFS_DEF_SB_ACCESSOR(u_int32_t, inopb);
670 LFS_DEF_SB_ACCESSOR(u_int32_t, ifpb);
671 LFS_DEF_SB_ACCESSOR(u_int32_t, sepb);
672 LFS_DEF_SB_ACCESSOR(u_int32_t, nindir);
673 LFS_DEF_SB_ACCESSOR(u_int32_t, nseg);
674 LFS_DEF_SB_ACCESSOR(u_int32_t, nspf);
675 LFS_DEF_SB_ACCESSOR(u_int32_t, cleansz);
676 LFS_DEF_SB_ACCESSOR(u_int32_t, segtabsz);
677 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segmask, 0);
678 LFS_DEF_SB_ACCESSOR_32ONLY(u_int32_t, segshift, 0);
679 LFS_DEF_SB_ACCESSOR(u_int64_t, bmask);
680 LFS_DEF_SB_ACCESSOR(u_int32_t, bshift);
681 LFS_DEF_SB_ACCESSOR(u_int64_t, ffmask);
682 LFS_DEF_SB_ACCESSOR(u_int32_t, ffshift);
683 LFS_DEF_SB_ACCESSOR(u_int64_t, fbmask);
684 LFS_DEF_SB_ACCESSOR(u_int32_t, fbshift);
685 LFS_DEF_SB_ACCESSOR(u_int32_t, blktodb);
686 LFS_DEF_SB_ACCESSOR(u_int32_t, fsbtodb);
687 LFS_DEF_SB_ACCESSOR(u_int32_t, sushift);
688 LFS_DEF_SB_ACCESSOR(int32_t, maxsymlinklen);
689 LFS_DEF_SB_ACCESSOR(u_int32_t, cksum);
690 LFS_DEF_SB_ACCESSOR(u_int16_t, pflags);
691 LFS_DEF_SB_ACCESSOR(u_int32_t, nclean);
692 LFS_DEF_SB_ACCESSOR(int32_t, dmeta);
693 LFS_DEF_SB_ACCESSOR(u_int32_t, minfreeseg);
694 LFS_DEF_SB_ACCESSOR(u_int32_t, sumsize);
695 LFS_DEF_SB_ACCESSOR(u_int64_t, serial);
696 LFS_DEF_SB_ACCESSOR(u_int32_t, ibsize);
697 LFS_DEF_SB_ACCESSOR_FULL(int64_t, int32_t, s0addr);
698 LFS_DEF_SB_ACCESSOR(u_int64_t, tstamp);
699 LFS_DEF_SB_ACCESSOR(u_int32_t, inodefmt);
700 LFS_DEF_SB_ACCESSOR(u_int32_t, interleave);
701 LFS_DEF_SB_ACCESSOR(u_int32_t, ident);
702 LFS_DEF_SB_ACCESSOR(u_int32_t, resvseg);
703
704 /* special-case accessors */
705
706 /*
707 * the v1 otstamp field lives in what's now dlfs_inopf
708 */
709 #define lfs_sb_getotstamp(fs) lfs_sb_getinopf(fs)
710 #define lfs_sb_setotstamp(fs, val) lfs_sb_setinopf(fs, val)
711
712 /*
713 * lfs_sboffs is an array
714 */
715 static __unused inline int32_t
716 lfs_sb_getsboff(STRUCT_LFS *fs, unsigned n)
717 {
718 #ifdef KASSERT /* ugh */
719 KASSERT(n < LFS_MAXNUMSB);
720 #endif
721 if (fs->lfs_is64) {
722 return fs->lfs_dlfs_u.u_64.dlfs_sboffs[n];
723 } else {
724 return fs->lfs_dlfs_u.u_32.dlfs_sboffs[n];
725 }
726 }
727 static __unused inline void
728 lfs_sb_setsboff(STRUCT_LFS *fs, unsigned n, int32_t val)
729 {
730 #ifdef KASSERT /* ugh */
731 KASSERT(n < LFS_MAXNUMSB);
732 #endif
733 if (fs->lfs_is64) {
734 fs->lfs_dlfs_u.u_64.dlfs_sboffs[n] = val;
735 } else {
736 fs->lfs_dlfs_u.u_32.dlfs_sboffs[n] = val;
737 }
738 }
739
740 /*
741 * lfs_fsmnt is a string
742 */
743 static __unused inline const char *
744 lfs_sb_getfsmnt(STRUCT_LFS *fs)
745 {
746 if (fs->lfs_is64) {
747 return fs->lfs_dlfs_u.u_64.dlfs_fsmnt;
748 } else {
749 return fs->lfs_dlfs_u.u_32.dlfs_fsmnt;
750 }
751 }
752
753 static __unused inline void
754 lfs_sb_setfsmnt(STRUCT_LFS *fs, const char *str)
755 {
756 if (fs->lfs_is64) {
757 (void)strncpy(fs->lfs_dlfs_u.u_64.dlfs_fsmnt, str,
758 sizeof(fs->lfs_dlfs_u.u_64.dlfs_fsmnt));
759 } else {
760 (void)strncpy(fs->lfs_dlfs_u.u_32.dlfs_fsmnt, str,
761 sizeof(fs->lfs_dlfs_u.u_32.dlfs_fsmnt));
762 }
763 }
764
765 /* Highest addressable fsb */
766 #define LFS_MAX_DADDR(fs) \
767 ((fs)->lfs_is64 ? 0x7fffffffffffffff : 0x7fffffff)
768
769 /* LFS_NINDIR is the number of indirects in a file system block. */
770 #define LFS_NINDIR(fs) (lfs_sb_getnindir(fs))
771
772 /* LFS_INOPB is the number of inodes in a secondary storage block. */
773 #define LFS_INOPB(fs) (lfs_sb_getinopb(fs))
774 /* LFS_INOPF is the number of inodes in a fragment. */
775 #define LFS_INOPF(fs) (lfs_sb_getinopf(fs))
776
777 #define lfs_blkoff(fs, loc) ((int)((loc) & lfs_sb_getbmask(fs)))
778 #define lfs_fragoff(fs, loc) /* calculates (loc % fs->lfs_fsize) */ \
779 ((int)((loc) & lfs_sb_getffmask(fs)))
780
781 /* XXX: lowercase these as they're no longer macros */
782 /* Frags to diskblocks */
783 static __unused inline uint64_t
784 LFS_FSBTODB(STRUCT_LFS *fs, uint64_t b)
785 {
786 #if defined(_KERNEL)
787 return b << (lfs_sb_getffshift(fs) - DEV_BSHIFT);
788 #else
789 return b << lfs_sb_getfsbtodb(fs);
790 #endif
791 }
792 /* Diskblocks to frags */
793 static __unused inline uint64_t
794 LFS_DBTOFSB(STRUCT_LFS *fs, uint64_t b)
795 {
796 #if defined(_KERNEL)
797 return b >> (lfs_sb_getffshift(fs) - DEV_BSHIFT);
798 #else
799 return b >> lfs_sb_getfsbtodb(fs);
800 #endif
801 }
802
803 #define lfs_lblkno(fs, loc) ((loc) >> lfs_sb_getbshift(fs))
804 #define lfs_lblktosize(fs, blk) ((blk) << lfs_sb_getbshift(fs))
805
806 /* Frags to bytes */
807 static __unused inline uint64_t
808 lfs_fsbtob(STRUCT_LFS *fs, uint64_t b)
809 {
810 return b << lfs_sb_getffshift(fs);
811 }
812 /* Bytes to frags */
813 static __unused inline uint64_t
814 lfs_btofsb(STRUCT_LFS *fs, uint64_t b)
815 {
816 return b >> lfs_sb_getffshift(fs);
817 }
818
819 #define lfs_numfrags(fs, loc) /* calculates (loc / fs->lfs_fsize) */ \
820 ((loc) >> lfs_sb_getffshift(fs))
821 #define lfs_blkroundup(fs, size)/* calculates roundup(size, lfs_sb_getbsize(fs)) */ \
822 ((off_t)(((size) + lfs_sb_getbmask(fs)) & (~lfs_sb_getbmask(fs))))
823 #define lfs_fragroundup(fs, size)/* calculates roundup(size, fs->lfs_fsize) */ \
824 ((off_t)(((size) + lfs_sb_getffmask(fs)) & (~lfs_sb_getffmask(fs))))
825 #define lfs_fragstoblks(fs, frags)/* calculates (frags / fs->fs_frag) */ \
826 ((frags) >> lfs_sb_getfbshift(fs))
827 #define lfs_blkstofrags(fs, blks)/* calculates (blks * fs->fs_frag) */ \
828 ((blks) << lfs_sb_getfbshift(fs))
829 #define lfs_fragnum(fs, fsb) /* calculates (fsb % fs->lfs_frag) */ \
830 ((fsb) & ((fs)->lfs_frag - 1))
831 #define lfs_blknum(fs, fsb) /* calculates rounddown(fsb, fs->lfs_frag) */ \
832 ((fsb) &~ ((fs)->lfs_frag - 1))
833 #define lfs_dblksize(fs, dp, lbn) \
834 (((lbn) >= ULFS_NDADDR || (dp)->di_size >= ((lbn) + 1) << lfs_sb_getbshift(fs)) \
835 ? lfs_sb_getbsize(fs) \
836 : (lfs_fragroundup(fs, lfs_blkoff(fs, (dp)->di_size))))
837
838 #define lfs_segsize(fs) (lfs_sb_getversion(fs) == 1 ? \
839 lfs_lblktosize((fs), lfs_sb_getssize(fs)) : \
840 lfs_sb_getssize(fs))
841 /* XXX segtod produces a result in frags despite the 'd' */
842 #define lfs_segtod(fs, seg) (lfs_btofsb(fs, lfs_segsize(fs)) * (seg))
843 #define lfs_dtosn(fs, daddr) /* block address to segment number */ \
844 ((uint32_t)(((daddr) - lfs_sb_gets0addr(fs)) / lfs_segtod((fs), 1)))
845 #define lfs_sntod(fs, sn) /* segment number to disk address */ \
846 ((daddr_t)(lfs_segtod((fs), (sn)) + lfs_sb_gets0addr(fs)))
847
848 /* XXX, blah. make this appear only if struct inode is defined */
849 #ifdef _UFS_LFS_LFS_INODE_H_
850 static __unused inline uint32_t
851 lfs_blksize(STRUCT_LFS *fs, struct inode *ip, uint64_t lbn)
852 {
853 if (lbn >= ULFS_NDADDR || ip->i_ffs1_size >= (lbn + 1) << lfs_sb_getbshift(fs)) {
854 return lfs_sb_getbsize(fs);
855 } else {
856 return lfs_fragroundup(fs, lfs_blkoff(fs, ip->i_ffs1_size));
857 }
858 }
859 #endif
860
861
862 /*
863 * Macros for determining free space on the disk, with the variable metadata
864 * of segment summaries and inode blocks taken into account.
865 */
866 /*
867 * Estimate number of clean blocks not available for writing because
868 * they will contain metadata or overhead. This is calculated as
869 *
870 * E = ((C * M / D) * D + (0) * (T - D)) / T
871 * or more simply
872 * E = (C * M) / T
873 *
874 * where
875 * C is the clean space,
876 * D is the dirty space,
877 * M is the dirty metadata, and
878 * T = C + D is the total space on disk.
879 *
880 * This approximates the old formula of E = C * M / D when D is close to T,
881 * but avoids falsely reporting "disk full" when the sample size (D) is small.
882 */
883 #define LFS_EST_CMETA(F) (int32_t)(( \
884 (lfs_sb_getdmeta(F) * (int64_t)lfs_sb_getnclean(F)) / \
885 (lfs_sb_getnseg(F))))
886
887 /* Estimate total size of the disk not including metadata */
888 #define LFS_EST_NONMETA(F) (lfs_sb_getdsize(F) - lfs_sb_getdmeta(F) - LFS_EST_CMETA(F))
889
890 /* Estimate number of blocks actually available for writing */
891 #define LFS_EST_BFREE(F) (lfs_sb_getbfree(F) > LFS_EST_CMETA(F) ? \
892 lfs_sb_getbfree(F) - LFS_EST_CMETA(F) : 0)
893
894 /* Amount of non-meta space not available to mortal man */
895 #define LFS_EST_RSVD(F) (int32_t)((LFS_EST_NONMETA(F) * \
896 (u_int64_t)lfs_sb_getminfree(F)) / \
897 100)
898
899 /* Can credential C write BB blocks? XXX: kauth_cred_geteuid is abusive */
900 #define ISSPACE(F, BB, C) \
901 ((((C) == NOCRED || kauth_cred_geteuid(C) == 0) && \
902 LFS_EST_BFREE(F) >= (BB)) || \
903 (kauth_cred_geteuid(C) != 0 && IS_FREESPACE(F, BB)))
904
905 /* Can an ordinary user write BB blocks */
906 #define IS_FREESPACE(F, BB) \
907 (LFS_EST_BFREE(F) >= (BB) + LFS_EST_RSVD(F))
908
909 /*
910 * The minimum number of blocks to create a new inode. This is:
911 * directory direct block (1) + ULFS_NIADDR indirect blocks + inode block (1) +
912 * ifile direct block (1) + ULFS_NIADDR indirect blocks = 3 + 2 * ULFS_NIADDR blocks.
913 */
914 #define LFS_NRESERVE(F) (lfs_btofsb((F), (2 * ULFS_NIADDR + 3) << lfs_sb_getbshift(F)))
915
916
917
918 #endif /* _UFS_LFS_LFS_ACCESSORS_H_ */
919