xref: /onnv-gate/usr/src/lib/libtnfprobe/tnf_buf.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *  Copyright 1994-2003 Sun Microsytems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  *  Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/param.h>
31*0Sstevel@tonic-gate #ifdef _KERNEL
32*0Sstevel@tonic-gate #include <sys/systm.h>		/* for bzero */
33*0Sstevel@tonic-gate #include <sys/spl.h>
34*0Sstevel@tonic-gate #include <sys/cmn_err.h>
35*0Sstevel@tonic-gate #else  /* _KERNEL */
36*0Sstevel@tonic-gate #include <string.h>		/* for memset */
37*0Sstevel@tonic-gate #endif /* _KERNEL */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "tnf_buf.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
42*0Sstevel@tonic-gate #ifdef _KERNEL
43*0Sstevel@tonic-gate #error TNFWB_DEBUG
44*0Sstevel@tonic-gate #else  /* _KERNEL */
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <thread.h>
47*0Sstevel@tonic-gate #endif /* _KERNEL */
48*0Sstevel@tonic-gate #endif /* TNFW_DEBUG */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * Defines
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #define	TNFW_B_FW_INVALID 		0xffffffff
55*0Sstevel@tonic-gate #define	TNFW_B_ALLOC_LO_SELECTOR 	0x1
56*0Sstevel@tonic-gate #define	TNFW_B_MAXALLOCTRY 		200
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #ifdef TNF_BLOCK_STATS
59*0Sstevel@tonic-gate static struct {
60*0Sstevel@tonic-gate 	int tnf_block_allocs;
61*0Sstevel@tonic-gate 	int tnf_block_tries;
62*0Sstevel@tonic-gate 	int tnf_max_block_tries;
63*0Sstevel@tonic-gate 	int tnf_tag_blocks;
64*0Sstevel@tonic-gate 	int tnf_generation_laps;
65*0Sstevel@tonic-gate 	int tnf_a_locks;
66*0Sstevel@tonic-gate 	int tnf_b_locks;
67*0Sstevel@tonic-gate } tnf_block_stats;
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate  * Regular record tag pointer - CAUTION - has to be in sync with tnf_tag
72*0Sstevel@tonic-gate  * macro in writer.h
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate #define	TNFW_B_TAG_DIFF(item, ref)				\
75*0Sstevel@tonic-gate 	((TNF_REF32_MAKE_PERMANENT((tnf_ref32_t)		\
76*0Sstevel@tonic-gate 	    ((char *)(item) - (char *)(ref)))) | TNF_REF32_T_TAG)
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * Exported interface by buffering layer to indicate where fowarding ptrs
80*0Sstevel@tonic-gate  * for file header and block header are.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate static tnf_buf_header_t forwarding_ptrs = {NULL, NULL, NULL};
83*0Sstevel@tonic-gate tnf_buf_header_t *_tnf_buf_headers_p = &forwarding_ptrs;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #ifdef _KERNEL
86*0Sstevel@tonic-gate extern volatile caddr_t tnf_buf;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static kmutex_t hintlock;
89*0Sstevel@tonic-gate #endif
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * (Private) Allocate a new block.  Return NULL on failure.  'istag'
93*0Sstevel@tonic-gate  * is true if the block is to be non-reclaimable.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate static tnf_block_header_t *
tnfw_b_alloc_block(TNFW_B_WCB * wcb,enum tnf_alloc_mode istag)96*0Sstevel@tonic-gate tnfw_b_alloc_block(TNFW_B_WCB *wcb, enum tnf_alloc_mode istag)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	tnf_block_header_t 	*block;
99*0Sstevel@tonic-gate 	uint_t 			hint_hi, hint_lo;
100*0Sstevel@tonic-gate 	uint_t			new_hint_hi, new_hint_lo;
101*0Sstevel@tonic-gate 	uint_t 			generation;
102*0Sstevel@tonic-gate 	uint_t			blocknum;
103*0Sstevel@tonic-gate 	uint_t 			prev_gen = 0;
104*0Sstevel@tonic-gate 	uint_t			prev_block = 0;
105*0Sstevel@tonic-gate 	uint_t			i, b;
106*0Sstevel@tonic-gate 	boolean_t 		gotit = B_FALSE;
107*0Sstevel@tonic-gate 	volatile tnf_buf_file_header_t 	*fh;
108*0Sstevel@tonic-gate #ifdef TNF_BLOCK_STATS
109*0Sstevel@tonic-gate 	register int tag_blocks = 0, generation_laps = 0, a_locks = 0,
110*0Sstevel@tonic-gate 		b_locks = 0;
111*0Sstevel@tonic-gate #endif
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate #ifdef _TNF_VERBOSE
114*0Sstevel@tonic-gate 	fprintf(stderr, "tnfw_b_alloc_block: \n");
115*0Sstevel@tonic-gate #endif
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	if (_tnfw_b_control->tnf_state != TNFW_B_RUNNING) {
118*0Sstevel@tonic-gate #ifndef _KERNEL
119*0Sstevel@tonic-gate 		if (_tnfw_b_control->tnf_state == TNFW_B_NOBUFFER)
120*0Sstevel@tonic-gate 			if (_tnfw_b_control->tnf_init_callback() == 0)
121*0Sstevel@tonic-gate 				return (NULL);
122*0Sstevel@tonic-gate #endif /* _KERNEL */
123*0Sstevel@tonic-gate 		if (TNFW_B_IS_STOPPED(_tnfw_b_control->tnf_state))
124*0Sstevel@tonic-gate 			return (NULL);
125*0Sstevel@tonic-gate 		if (_tnfw_b_control->tnf_state == TNFW_B_BROKEN)
126*0Sstevel@tonic-gate 			return (NULL);
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
130*0Sstevel@tonic-gate 	fh = (volatile tnf_buf_file_header_t *)_tnfw_b_control->tnf_buffer;
131*0Sstevel@tonic-gate 	if (!wcb->tnfw_w_initialized) {
132*0Sstevel@tonic-gate 		/* Get the block shift and generation shift values. */
133*0Sstevel@tonic-gate 		b = 1;
134*0Sstevel@tonic-gate 		wcb->tnfw_w_block_shift = wcb->tnfw_w_gen_shift = 0;
135*0Sstevel@tonic-gate 		while (b != fh->com.block_size) {
136*0Sstevel@tonic-gate 			b <<= 1;
137*0Sstevel@tonic-gate 			++wcb->tnfw_w_block_shift;
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 		b = 1;
140*0Sstevel@tonic-gate 		while (b < fh->com.block_count) {
141*0Sstevel@tonic-gate 			b <<= 1;
142*0Sstevel@tonic-gate 			++wcb->tnfw_w_gen_shift;
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 		wcb->tnfw_w_pid = _tnfw_b_control->tnf_pid;
145*0Sstevel@tonic-gate 		wcb->tnfw_w_initialized = B_TRUE;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	/*
149*0Sstevel@tonic-gate 	 * If we need a tag block, check the reserved tag block space
150*0Sstevel@tonic-gate 	 * first.  fh->next_tag_alloc is only a hint; it is updated
151*0Sstevel@tonic-gate 	 * without concurrency control.
152*0Sstevel@tonic-gate 	 */
153*0Sstevel@tonic-gate 	if (istag && fh->next_tag_alloc < TNFW_B_DATA_BLOCK_BEGIN) {
154*0Sstevel@tonic-gate 		i = fh->next_tag_alloc;
155*0Sstevel@tonic-gate 		do {
156*0Sstevel@tonic-gate 			/* LINTED pointer cast */
157*0Sstevel@tonic-gate 			block = (tnf_block_header_t *) ((char *) fh + i);
158*0Sstevel@tonic-gate 			if (!tnfw_b_get_lock(&block->A_lock) &&
159*0Sstevel@tonic-gate 			    block->generation == 0)
160*0Sstevel@tonic-gate 				break;
161*0Sstevel@tonic-gate 			i += fh->com.block_size;
162*0Sstevel@tonic-gate 		} while (i < TNFW_B_DATA_BLOCK_BEGIN);
163*0Sstevel@tonic-gate 		if (i < TNFW_B_DATA_BLOCK_BEGIN) {
164*0Sstevel@tonic-gate 			if (i > fh->next_tag_alloc)
165*0Sstevel@tonic-gate 				fh->next_tag_alloc = i;
166*0Sstevel@tonic-gate 			blocknum = i >> wcb->tnfw_w_block_shift;
167*0Sstevel@tonic-gate 			if (blocknum > fh->com.blocks_valid)
168*0Sstevel@tonic-gate 				fh->com.blocks_valid = blocknum;
169*0Sstevel@tonic-gate 			/* LINTED pointer subtraction casted to 32 bits */
170*0Sstevel@tonic-gate 			block->tag = TNFW_B_TAG_DIFF(
171*0Sstevel@tonic-gate 			    forwarding_ptrs.fw_block_header, fh);
172*0Sstevel@tonic-gate 			/* LINTED constant truncated by assignment */
173*0Sstevel@tonic-gate 			block->generation = TNF_TAG_GENERATION_NUM;
174*0Sstevel@tonic-gate 			block->bytes_valid = sizeof (tnf_block_header_t);
175*0Sstevel@tonic-gate 			block->next_block = NULL;
176*0Sstevel@tonic-gate 			tnfw_b_clear_lock(&block->A_lock);
177*0Sstevel@tonic-gate 			return (block);
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	for (i = 0; !gotit && i != TNFW_B_MAXALLOCTRY; ++i) {
182*0Sstevel@tonic-gate 		hint_hi = fh->next_alloc.hi;
183*0Sstevel@tonic-gate 		hint_lo = (hint_hi & TNFW_B_ALLOC_LO_SELECTOR)
184*0Sstevel@tonic-gate 			? fh->next_alloc.lo[1] : fh->next_alloc.lo[0];
185*0Sstevel@tonic-gate 		generation = (hint_hi << (32 - wcb->tnfw_w_gen_shift)) |
186*0Sstevel@tonic-gate 			(hint_lo >> wcb->tnfw_w_gen_shift);
187*0Sstevel@tonic-gate 		blocknum = hint_lo & ((1 << wcb->tnfw_w_gen_shift) - 1);
188*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
189*0Sstevel@tonic-gate 		fprintf(stderr, "alloc_block (%d): read hint (%d, %d)\n",
190*0Sstevel@tonic-gate 		    thr_self(), generation, blocknum);
191*0Sstevel@tonic-gate #endif
192*0Sstevel@tonic-gate 		if ((prev_gen == generation && prev_block > blocknum) ||
193*0Sstevel@tonic-gate 		    prev_gen > generation) {
194*0Sstevel@tonic-gate 			generation = prev_gen;
195*0Sstevel@tonic-gate 			blocknum = prev_block;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
198*0Sstevel@tonic-gate 		fprintf(stderr,
199*0Sstevel@tonic-gate 		    "alloc_block (%d): trying blocknum = %d, gen %d\n",
200*0Sstevel@tonic-gate 		    thr_self(), blocknum, generation);
201*0Sstevel@tonic-gate #endif
202*0Sstevel@tonic-gate 		block = (tnf_block_header_t *)
203*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
204*0Sstevel@tonic-gate 			((char *)fh + blocknum * fh->com.block_size);
205*0Sstevel@tonic-gate #ifdef TNF_BLOCK_STATS
206*0Sstevel@tonic-gate 		if (block->generation == TNF_TAG_GENERATION_NUM)
207*0Sstevel@tonic-gate 			++tag_blocks;
208*0Sstevel@tonic-gate 		else if (block->generation >= generation)
209*0Sstevel@tonic-gate 			++generation_laps;
210*0Sstevel@tonic-gate 		else if (tnfw_b_get_lock(&block->A_lock))
211*0Sstevel@tonic-gate 			++a_locks;
212*0Sstevel@tonic-gate 		else if (block->generation == TNF_TAG_GENERATION_NUM)
213*0Sstevel@tonic-gate 			++tag_blocks;
214*0Sstevel@tonic-gate 		else if (block->generation >= generation)
215*0Sstevel@tonic-gate 			++generation_laps;
216*0Sstevel@tonic-gate 		else if (tnfw_b_get_lock(&block->B_lock)) {
217*0Sstevel@tonic-gate 			tnfw_b_clear_lock(&block->A_lock);
218*0Sstevel@tonic-gate 			++b_locks;
219*0Sstevel@tonic-gate 		} else
220*0Sstevel@tonic-gate 			gotit = B_TRUE;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate #else
223*0Sstevel@tonic-gate 		if (block->generation < generation &&
224*0Sstevel@tonic-gate 		    !tnfw_b_get_lock(&block->A_lock)) {
225*0Sstevel@tonic-gate 			if (block->generation < generation &&
226*0Sstevel@tonic-gate 			    !tnfw_b_get_lock(&block->B_lock)) {
227*0Sstevel@tonic-gate 				gotit = B_TRUE;
228*0Sstevel@tonic-gate 			} else {
229*0Sstevel@tonic-gate 				tnfw_b_clear_lock(&block->A_lock);
230*0Sstevel@tonic-gate 			}
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate #endif
233*0Sstevel@tonic-gate 		prev_block = blocknum + 1;
234*0Sstevel@tonic-gate 		prev_gen = generation;
235*0Sstevel@tonic-gate 		if (prev_block == fh->com.block_count) {
236*0Sstevel@tonic-gate 			prev_block =
237*0Sstevel@tonic-gate 			    TNFW_B_DATA_BLOCK_BEGIN >> wcb->tnfw_w_block_shift;
238*0Sstevel@tonic-gate 			++prev_gen;
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 		if (blocknum > fh->com.blocks_valid) {
241*0Sstevel@tonic-gate 			fh->com.blocks_valid = blocknum;
242*0Sstevel@tonic-gate 		}
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	if (i == TNFW_B_MAXALLOCTRY) {
246*0Sstevel@tonic-gate 		_tnfw_b_control->tnf_state = TNFW_B_BROKEN;
247*0Sstevel@tonic-gate 		return (NULL);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
250*0Sstevel@tonic-gate 	fprintf(stderr,
251*0Sstevel@tonic-gate 	    "alloc_block (%d): got blocknum = %d, gen %d, block at 0x%x\n",
252*0Sstevel@tonic-gate 	    thr_self(), blocknum, generation, block);
253*0Sstevel@tonic-gate #endif
254*0Sstevel@tonic-gate 	/* LINTED pointer subtraction casted to 32 bits */
255*0Sstevel@tonic-gate 	block->tag = TNFW_B_TAG_DIFF(forwarding_ptrs.fw_block_header, fh);
256*0Sstevel@tonic-gate 	block->generation = (istag) ? TNF_TAG_GENERATION_NUM : generation;
257*0Sstevel@tonic-gate 	block->bytes_valid = sizeof (tnf_block_header_t);
258*0Sstevel@tonic-gate 	block->next_block = NULL;
259*0Sstevel@tonic-gate 	if (istag) {
260*0Sstevel@tonic-gate 		tnfw_b_clear_lock(&block->A_lock);
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate 	tnfw_b_clear_lock(&block->B_lock);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	/*
265*0Sstevel@tonic-gate 	 * Read the hint one more time, only update it if we'll be increasing
266*0Sstevel@tonic-gate 	 * it
267*0Sstevel@tonic-gate 	 */
268*0Sstevel@tonic-gate 	new_hint_hi = prev_gen >> (32 - wcb->tnfw_w_gen_shift);
269*0Sstevel@tonic-gate 	new_hint_lo = prev_block | (prev_gen << wcb->tnfw_w_gen_shift);
270*0Sstevel@tonic-gate #ifdef _KERNEL
271*0Sstevel@tonic-gate 	mutex_enter(&hintlock);
272*0Sstevel@tonic-gate #endif
273*0Sstevel@tonic-gate 	hint_hi = fh->next_alloc.hi;
274*0Sstevel@tonic-gate 	hint_lo = (hint_hi & TNFW_B_ALLOC_LO_SELECTOR) ?
275*0Sstevel@tonic-gate 		fh->next_alloc.lo[1] : fh->next_alloc.lo[0];
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if ((new_hint_hi == hint_hi && new_hint_lo > hint_lo) ||
278*0Sstevel@tonic-gate 	    new_hint_hi > hint_hi) {
279*0Sstevel@tonic-gate 		/*
280*0Sstevel@tonic-gate 		 * Order is important here!  It is the write to next_alloc.hi
281*0Sstevel@tonic-gate 		 * that atomically records the new value.
282*0Sstevel@tonic-gate 		 */
283*0Sstevel@tonic-gate 		if (new_hint_hi & TNFW_B_ALLOC_LO_SELECTOR)
284*0Sstevel@tonic-gate 			fh->next_alloc.lo[1] = new_hint_lo;
285*0Sstevel@tonic-gate 		else
286*0Sstevel@tonic-gate 			fh->next_alloc.lo[0] = new_hint_lo;
287*0Sstevel@tonic-gate 		fh->next_alloc.hi = new_hint_hi;
288*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
289*0Sstevel@tonic-gate 		fprintf(stderr, "alloc_block (%d): wrote hint (%d, %d)\n",
290*0Sstevel@tonic-gate 		    thr_self(), prev_gen, prev_block);
291*0Sstevel@tonic-gate #endif
292*0Sstevel@tonic-gate 	}
293*0Sstevel@tonic-gate #ifdef _KERNEL
294*0Sstevel@tonic-gate 	mutex_exit(&hintlock);
295*0Sstevel@tonic-gate #endif
296*0Sstevel@tonic-gate #ifdef TNF_BLOCK_STATS
297*0Sstevel@tonic-gate 	++tnf_block_stats.tnf_block_allocs;
298*0Sstevel@tonic-gate 	tnf_block_stats.tnf_block_tries += i;
299*0Sstevel@tonic-gate 	if (i > tnf_block_stats.tnf_max_block_tries) {
300*0Sstevel@tonic-gate 		tnf_block_stats.tnf_max_block_tries = i;
301*0Sstevel@tonic-gate 		tnf_block_stats.tnf_tag_blocks = tag_blocks;
302*0Sstevel@tonic-gate 		tnf_block_stats.tnf_generation_laps = generation_laps;
303*0Sstevel@tonic-gate 		tnf_block_stats.tnf_a_locks = a_locks;
304*0Sstevel@tonic-gate 		tnf_block_stats.tnf_b_locks = b_locks;
305*0Sstevel@tonic-gate 	}
306*0Sstevel@tonic-gate #endif
307*0Sstevel@tonic-gate 	return (block);
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate 
release_block_from_pos(TNFW_B_POS * pos)310*0Sstevel@tonic-gate static void release_block_from_pos(TNFW_B_POS * pos)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	if (pos->tnfw_w_block == NULL)
313*0Sstevel@tonic-gate 		return;
314*0Sstevel@tonic-gate 	if (pos->tnfw_w_uncommitted != NULL)
315*0Sstevel@tonic-gate 		return;
316*0Sstevel@tonic-gate 	tnfw_b_clear_lock(&pos->tnfw_w_block->A_lock);
317*0Sstevel@tonic-gate 	pos->tnfw_w_block = NULL;
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate void
tnfw_b_release_block(TNFW_B_WCB * wcb)321*0Sstevel@tonic-gate tnfw_b_release_block(TNFW_B_WCB * wcb)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	if (wcb == NULL)
324*0Sstevel@tonic-gate 		return;
325*0Sstevel@tonic-gate 	release_block_from_pos(&wcb->tnfw_w_tag_pos);
326*0Sstevel@tonic-gate 	release_block_from_pos(&wcb->tnfw_w_pos);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate /*
330*0Sstevel@tonic-gate  * Initialize a buffer.  NOT RE-ENTRANT!  Block sizes other than 512
331*0Sstevel@tonic-gate  * are currently rejected.  The code "ought to work" with any block
332*0Sstevel@tonic-gate  * size that is an integral power of 2.  'zfod' states whether we
333*0Sstevel@tonic-gate  * can assume that the buffer is zero-filled (or paged-in zero-fill-on-demand).
334*0Sstevel@tonic-gate  */
335*0Sstevel@tonic-gate TNFW_B_STATUS
tnfw_b_init_buffer(char * buf,int blocks,int block_size,boolean_t zfod)336*0Sstevel@tonic-gate tnfw_b_init_buffer(char *buf, int blocks, int block_size, boolean_t zfod)
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	int 	block_shift, gen_shift;
340*0Sstevel@tonic-gate 	int 	i;
341*0Sstevel@tonic-gate 	int	file_size;
342*0Sstevel@tonic-gate 	unsigned b;
343*0Sstevel@tonic-gate 	tnf_block_header_t *block;
344*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
345*0Sstevel@tonic-gate 	tnf_buf_file_header_t *fh = (tnf_buf_file_header_t *)buf;
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate #ifdef _TNF_VERBOSE
348*0Sstevel@tonic-gate 	fprintf(stderr, "tnfw_b_init_buffer: \n");
349*0Sstevel@tonic-gate #endif
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	/* Check for 512 could go away. */
352*0Sstevel@tonic-gate 	if (block_size != 512 || block_size < sizeof (tnf_buf_file_header_t))
353*0Sstevel@tonic-gate 		return (TNFW_B_BAD_BLOCK_SIZE);
354*0Sstevel@tonic-gate 	/*
355*0Sstevel@tonic-gate 	 * Check to see if block size is a power of 2, and get
356*0Sstevel@tonic-gate 	 * log2(block size).
357*0Sstevel@tonic-gate 	 */
358*0Sstevel@tonic-gate 	for (b = (unsigned)block_size, block_shift = 0; (b & 1) == 0; b >>= 1)
359*0Sstevel@tonic-gate 		++block_shift;
360*0Sstevel@tonic-gate 	if (b != 1)
361*0Sstevel@tonic-gate 		return (TNFW_B_BAD_BLOCK_SIZE);
362*0Sstevel@tonic-gate 	gen_shift = 0;
363*0Sstevel@tonic-gate 	while (b < blocks) {
364*0Sstevel@tonic-gate 		b <<= 1;
365*0Sstevel@tonic-gate 		++gen_shift;
366*0Sstevel@tonic-gate 	}
367*0Sstevel@tonic-gate 	/* reserve first two words for file header tag and block header tag */
368*0Sstevel@tonic-gate 	forwarding_ptrs.fw_file_header  = (char *)fh + block_size;
369*0Sstevel@tonic-gate 	forwarding_ptrs.fw_block_header = (char *)fh + block_size +
370*0Sstevel@tonic-gate 		sizeof (tnf_ref32_t);
371*0Sstevel@tonic-gate 	forwarding_ptrs.fw_root = (char *)fh + block_size +
372*0Sstevel@tonic-gate 		(2 * sizeof (tnf_ref32_t));
373*0Sstevel@tonic-gate 	/* LINTED size of tnf_ref_32_t known to be 32 */
374*0Sstevel@tonic-gate 	fh->next_fw_alloc = block_size + (3 * sizeof (tnf_ref32_t));
375*0Sstevel@tonic-gate 	/* fill in rest of file header */
376*0Sstevel@tonic-gate 	fh->magic = TNF_MAGIC;
377*0Sstevel@tonic-gate 	/* Self relative pointer to tag */
378*0Sstevel@tonic-gate 	/* LINTED pointer subtraction casted to 32 bits */
379*0Sstevel@tonic-gate 	fh->com.tag = TNFW_B_TAG_DIFF(forwarding_ptrs.fw_file_header, fh);
380*0Sstevel@tonic-gate 	fh->com.file_version = TNF_FILE_VERSION;
381*0Sstevel@tonic-gate 	fh->com.file_header_size = sizeof (tnf_file_header_t);
382*0Sstevel@tonic-gate 	/* fill in fh->com.file_log_size */
383*0Sstevel@tonic-gate 	b = 1;
384*0Sstevel@tonic-gate 	file_size = blocks * block_size;
385*0Sstevel@tonic-gate 	fh->com.file_log_size = 0;
386*0Sstevel@tonic-gate 	while (b < file_size) {
387*0Sstevel@tonic-gate 		b <<= 1;
388*0Sstevel@tonic-gate 		++fh->com.file_log_size;
389*0Sstevel@tonic-gate 	}
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 	fh->com.block_header_size = sizeof (tnf_block_header_t);
392*0Sstevel@tonic-gate 	fh->com.block_size = block_size;
393*0Sstevel@tonic-gate 	fh->com.directory_size = TNFW_B_FW_ZONE;
394*0Sstevel@tonic-gate 	fh->com.block_count = blocks;
395*0Sstevel@tonic-gate 	fh->com.blocks_valid = TNFW_B_FW_ZONE >> block_shift;
396*0Sstevel@tonic-gate 	if (fh->com.blocks_valid == 0)
397*0Sstevel@tonic-gate 		fh->com.blocks_valid = 1;
398*0Sstevel@tonic-gate 	fh->next_tag_alloc = TNFW_B_FW_ZONE;
399*0Sstevel@tonic-gate 	fh->next_alloc.hi = 0;
400*0Sstevel@tonic-gate 	fh->next_alloc.lo[0] =
401*0Sstevel@tonic-gate 	    (1 << gen_shift) | (TNFW_B_DATA_BLOCK_BEGIN >> block_shift);
402*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
403*0Sstevel@tonic-gate 	fprintf(stderr, "gen_shift = %d, blocks_valid = %d\n",
404*0Sstevel@tonic-gate 	    gen_shift, fh->com.blocks_valid);
405*0Sstevel@tonic-gate 	fprintf(stderr, "alloc hint initialized to (%d, %d, %d)\n",
406*0Sstevel@tonic-gate 	    fh->next_alloc.hi, fh->next_alloc.lo[0], fh->next_alloc.lo[1]);
407*0Sstevel@tonic-gate #endif
408*0Sstevel@tonic-gate 	if (!zfod) {
409*0Sstevel@tonic-gate 		for (i = 1; i < (TNFW_B_FW_ZONE >> block_shift); ++i) {
410*0Sstevel@tonic-gate #ifdef _KERNEL
411*0Sstevel@tonic-gate 			bzero(buf + (i << block_shift), block_size);
412*0Sstevel@tonic-gate #else
413*0Sstevel@tonic-gate 			(void) memset(buf + (i << block_shift), 0, block_size);
414*0Sstevel@tonic-gate #endif
415*0Sstevel@tonic-gate 		}
416*0Sstevel@tonic-gate 		for (; i != blocks; ++i) {
417*0Sstevel@tonic-gate 			block =	(tnf_block_header_t *)
418*0Sstevel@tonic-gate 				/* LINTED pointer cast */
419*0Sstevel@tonic-gate 				(buf + (i << block_shift));
420*0Sstevel@tonic-gate 			block->tag = 0;
421*0Sstevel@tonic-gate 			block->generation = 0;
422*0Sstevel@tonic-gate 			tnfw_b_clear_lock(&block->A_lock);
423*0Sstevel@tonic-gate 			tnfw_b_clear_lock(&block->B_lock);
424*0Sstevel@tonic-gate 		}
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate #ifdef _KERNEL
427*0Sstevel@tonic-gate 	mutex_init(&hintlock, "tnf buffer hint lock", MUTEX_SPIN_DEFAULT,
428*0Sstevel@tonic-gate 	    (void *) ipltospl(LOCK_LEVEL));
429*0Sstevel@tonic-gate #endif
430*0Sstevel@tonic-gate 	return (TNFW_B_OK);
431*0Sstevel@tonic-gate }
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate /*
434*0Sstevel@tonic-gate  *
435*0Sstevel@tonic-gate  */
436*0Sstevel@tonic-gate void *
tnfw_b_alloc(TNFW_B_WCB * wcb,size_t size,enum tnf_alloc_mode istag)437*0Sstevel@tonic-gate tnfw_b_alloc(TNFW_B_WCB *wcb, size_t size, enum tnf_alloc_mode istag)
438*0Sstevel@tonic-gate {
439*0Sstevel@tonic-gate 	TNFW_B_POS 	*pos;
440*0Sstevel@tonic-gate 	int 		offset;
441*0Sstevel@tonic-gate 	void 		*destp;
442*0Sstevel@tonic-gate 	volatile tnf_buf_file_header_t *fh;
443*0Sstevel@tonic-gate 	tnf_block_header_t *block, *new_block;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate #ifdef _TNF_VERBOSE
446*0Sstevel@tonic-gate 	fprintf(stderr, "tnfw_b_alloc: \n");
447*0Sstevel@tonic-gate #endif
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	if (_tnfw_b_control->tnf_state != TNFW_B_RUNNING) {
450*0Sstevel@tonic-gate 		if (TNFW_B_IS_STOPPED(_tnfw_b_control->tnf_state))
451*0Sstevel@tonic-gate 			return (NULL);
452*0Sstevel@tonic-gate 		if (_tnfw_b_control->tnf_state == TNFW_B_FORKED &&
453*0Sstevel@tonic-gate 		    _tnfw_b_control->tnf_pid != wcb->tnfw_w_pid) {
454*0Sstevel@tonic-gate 			wcb->tnfw_w_pos.tnfw_w_block =
455*0Sstevel@tonic-gate 				wcb->tnfw_w_pos.tnfw_w_uncommitted =
456*0Sstevel@tonic-gate 				wcb->tnfw_w_tag_pos.tnfw_w_block =
457*0Sstevel@tonic-gate 				wcb->tnfw_w_tag_pos.tnfw_w_uncommitted = NULL;
458*0Sstevel@tonic-gate 			wcb->tnfw_w_pid = _tnfw_b_control->tnf_pid;
459*0Sstevel@tonic-gate 			_tnfw_b_control->tnf_fork_callback();
460*0Sstevel@tonic-gate 		}
461*0Sstevel@tonic-gate 	}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	/* Round size up to a multiple of 8. */
464*0Sstevel@tonic-gate 	size = (size + 7) & ~7;
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
467*0Sstevel@tonic-gate 	fh = (volatile tnf_buf_file_header_t *)_tnfw_b_control->tnf_buffer;
468*0Sstevel@tonic-gate 	pos = (istag) ? &wcb->tnfw_w_tag_pos : &wcb->tnfw_w_pos;
469*0Sstevel@tonic-gate 	block = pos->tnfw_w_block;
470*0Sstevel@tonic-gate 	/* Check size within range. */
471*0Sstevel@tonic-gate #ifdef TNFWB_SAFER
472*0Sstevel@tonic-gate 	if (size > fh->com.block_size - sizeof (tnf_block_header_t))
473*0Sstevel@tonic-gate 		/* TNFW_B_RECORD_TOO_BIG */
474*0Sstevel@tonic-gate 		return (NULL);
475*0Sstevel@tonic-gate #endif
476*0Sstevel@tonic-gate 	offset = pos->tnfw_w_write_off;
477*0Sstevel@tonic-gate #ifdef TNFWB_MAY_RELEASE_A_LOCK
478*0Sstevel@tonic-gate 	if (block != NULL && wcb->tnfw_w_a_lock_released) {
479*0Sstevel@tonic-gate 		/* re-acquire the A-lock for the current block */
480*0Sstevel@tonic-gate 		if (!tnfw_b_get_lock(&block->A_lock)) {
481*0Sstevel@tonic-gate 			wcb->tnfw_w_a_lock_released = B_FALSE;
482*0Sstevel@tonic-gate 			if (wcb->tnfw_w_generation != block->generation) {
483*0Sstevel@tonic-gate 				tnfw_b_clear_lock(&block->A_lock);
484*0Sstevel@tonic-gate 				wcb->tnfw_w_pos.tnfw_w_block = NULL;
485*0Sstevel@tonic-gate 			}
486*0Sstevel@tonic-gate 		} else {
487*0Sstevel@tonic-gate 			wcb->tnfw_w_pos.tnfw_w_block = NULL;
488*0Sstevel@tonic-gate 		}
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate #endif
491*0Sstevel@tonic-gate 	if (block == NULL || offset + size > fh->com.block_size) {
492*0Sstevel@tonic-gate 		new_block = tnfw_b_alloc_block(wcb, istag);
493*0Sstevel@tonic-gate 		if (new_block == NULL) {
494*0Sstevel@tonic-gate 			/* TNFW_B_ACKPHT */
495*0Sstevel@tonic-gate 			return (NULL);
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
498*0Sstevel@tonic-gate 		fprintf(stderr,
499*0Sstevel@tonic-gate 		    "wcb 0x%x: new block at 0x%x, old block is 0x%x, "
500*0Sstevel@tonic-gate 		    "uncommitted is 0x%x\n",
501*0Sstevel@tonic-gate 		    wcb, new_block, block, pos->tnfw_w_uncommitted);
502*0Sstevel@tonic-gate #endif
503*0Sstevel@tonic-gate 		if (block != NULL) {
504*0Sstevel@tonic-gate 			/* XXXX is this what we want for padding? */
505*0Sstevel@tonic-gate #ifdef _KERNEL
506*0Sstevel@tonic-gate 			(void) bzero((char *)block + offset,
507*0Sstevel@tonic-gate 			    fh->com.block_size - offset);
508*0Sstevel@tonic-gate #else
509*0Sstevel@tonic-gate 			(void) memset((char *)block + offset, 0,
510*0Sstevel@tonic-gate 			    fh->com.block_size - offset);
511*0Sstevel@tonic-gate #endif
512*0Sstevel@tonic-gate 			if (pos->tnfw_w_uncommitted == NULL) {
513*0Sstevel@tonic-gate #ifdef TNFWB_MAY_RELEASE_A_LOCK
514*0Sstevel@tonic-gate 				/* Could still be holding the A-lock on block */
515*0Sstevel@tonic-gate 				if (!wcb->tnfw_w_a_lock_released)
516*0Sstevel@tonic-gate 					tnfw_b_clear_lock(&block->A_lock);
517*0Sstevel@tonic-gate #else
518*0Sstevel@tonic-gate 				/* Definitely still holding the A-lock */
519*0Sstevel@tonic-gate 				tnfw_b_clear_lock(&block->A_lock);
520*0Sstevel@tonic-gate #endif	/* TNFWB_MAY_RELEASE_A_LOCK */
521*0Sstevel@tonic-gate 			}
522*0Sstevel@tonic-gate 		}
523*0Sstevel@tonic-gate 		/* Add new_block to the list of uncommitted blocks. */
524*0Sstevel@tonic-gate 		if (pos->tnfw_w_uncommitted == NULL) {
525*0Sstevel@tonic-gate 			pos->tnfw_w_uncommitted = new_block;
526*0Sstevel@tonic-gate 		} else {
527*0Sstevel@tonic-gate 			/* Assert(block != NULL); */
528*0Sstevel@tonic-gate 			block->next_block = new_block;
529*0Sstevel@tonic-gate 		}
530*0Sstevel@tonic-gate 		pos->tnfw_w_block = new_block;
531*0Sstevel@tonic-gate 		pos->tnfw_w_write_off = new_block->bytes_valid;
532*0Sstevel@tonic-gate 	} else if (pos->tnfw_w_uncommitted == NULL) {
533*0Sstevel@tonic-gate 		pos->tnfw_w_uncommitted = block;
534*0Sstevel@tonic-gate 	}
535*0Sstevel@tonic-gate 	destp = (char *)pos->tnfw_w_block + pos->tnfw_w_write_off;
536*0Sstevel@tonic-gate 	pos->tnfw_w_write_off += size;
537*0Sstevel@tonic-gate 	/*
538*0Sstevel@tonic-gate 	 * Unconditionally write a 0 into the last word allocated,
539*0Sstevel@tonic-gate 	 * in case we left an alignment gap.  (Assume that doing an
540*0Sstevel@tonic-gate 	 * unconditional write is cheaper than testing and branching
541*0Sstevel@tonic-gate 	 * around the write half the time.)
542*0Sstevel@tonic-gate 	 */
543*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
544*0Sstevel@tonic-gate 	*((int *)((char *) destp + size - sizeof (int))) = 0;
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate #ifdef _TNF_VERBOSE
547*0Sstevel@tonic-gate 	fprintf(stderr, "tnfw_b_alloc returning %p\n", destp);
548*0Sstevel@tonic-gate #endif
549*0Sstevel@tonic-gate 	return (destp);
550*0Sstevel@tonic-gate }
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate /*
553*0Sstevel@tonic-gate  *
554*0Sstevel@tonic-gate  */
555*0Sstevel@tonic-gate TNFW_B_STATUS
tnfw_b_xcommit(TNFW_B_WCB * wcb)556*0Sstevel@tonic-gate tnfw_b_xcommit(TNFW_B_WCB *wcb)
557*0Sstevel@tonic-gate {
558*0Sstevel@tonic-gate 	TNFW_B_POS *pos;
559*0Sstevel@tonic-gate 	tnf_block_header_t *block;
560*0Sstevel@tonic-gate 	volatile tnf_buf_file_header_t *fh =
561*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
562*0Sstevel@tonic-gate 		(volatile tnf_buf_file_header_t *)_tnfw_b_control->tnf_buffer;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
565*0Sstevel@tonic-gate 	fprintf(stderr, "tnfw_b_xcommit \n");
566*0Sstevel@tonic-gate #endif
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	/*
569*0Sstevel@tonic-gate 	 * cope with the normal record block(s) first
570*0Sstevel@tonic-gate 	 */
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	pos = &wcb->tnfw_w_pos;
573*0Sstevel@tonic-gate 	block = pos->tnfw_w_uncommitted;
574*0Sstevel@tonic-gate 	while (block && (block != pos->tnfw_w_block)) {
575*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
576*0Sstevel@tonic-gate 		fprintf(stderr, "commit %d: block = 0x%x, last = 0x%x\n",
577*0Sstevel@tonic-gate 		    block->generation, block, pos->tnfw_w_block);
578*0Sstevel@tonic-gate #endif
579*0Sstevel@tonic-gate 		block->bytes_valid = fh->com.block_size;
580*0Sstevel@tonic-gate 		pos->tnfw_w_uncommitted = block->next_block;
581*0Sstevel@tonic-gate 		tnfw_b_clear_lock(&block->A_lock);
582*0Sstevel@tonic-gate 		block = pos->tnfw_w_uncommitted;
583*0Sstevel@tonic-gate 	}
584*0Sstevel@tonic-gate 	if (block != NULL) {
585*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
586*0Sstevel@tonic-gate 		fprintf(stderr, "commit last %d: block = 0x%x, offset = 0x%x\n",
587*0Sstevel@tonic-gate 		    block->generation, block, pos->tnfw_w_write_off);
588*0Sstevel@tonic-gate #endif
589*0Sstevel@tonic-gate 		block->bytes_valid = pos->tnfw_w_write_off;
590*0Sstevel@tonic-gate 	}
591*0Sstevel@tonic-gate 	pos->tnfw_w_uncommitted = NULL;
592*0Sstevel@tonic-gate #ifdef TNFWB_MAY_RELEASE_A_LOCK
593*0Sstevel@tonic-gate 	if (0) {	/* XXXX Do we or don't we clear this lock? */
594*0Sstevel@tonic-gate 		wcb->tnfw_w_generation = block->generation;
595*0Sstevel@tonic-gate 		tnfw_b_clear_lock(&block->A_lock);
596*0Sstevel@tonic-gate 		wcb->tnfw_w_a_lock_released = B_TRUE;
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate #endif
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 	/*
601*0Sstevel@tonic-gate 	 * cope with the tag block(s)
602*0Sstevel@tonic-gate 	 */
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	pos = &wcb->tnfw_w_tag_pos;
605*0Sstevel@tonic-gate 	block = pos->tnfw_w_uncommitted;
606*0Sstevel@tonic-gate 	while (block && (block != pos->tnfw_w_block)) {
607*0Sstevel@tonic-gate #ifdef TNFWB_DEBUG
608*0Sstevel@tonic-gate 		fprintf(stderr, "commit %d: block = 0x%x, last = 0x%x\n",
609*0Sstevel@tonic-gate 		    thr_self(), block, pos->tnfw_w_block);
610*0Sstevel@tonic-gate #endif
611*0Sstevel@tonic-gate 		block->bytes_valid = fh->com.block_size;
612*0Sstevel@tonic-gate 		pos->tnfw_w_uncommitted = block->next_block;
613*0Sstevel@tonic-gate 		block = pos->tnfw_w_uncommitted;
614*0Sstevel@tonic-gate 	}
615*0Sstevel@tonic-gate 	if (block != NULL)
616*0Sstevel@tonic-gate 		block->bytes_valid = pos->tnfw_w_write_off;
617*0Sstevel@tonic-gate 	pos->tnfw_w_uncommitted = NULL;
618*0Sstevel@tonic-gate 	return (TNFW_B_OK);
619*0Sstevel@tonic-gate }
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate /*
622*0Sstevel@tonic-gate  *
623*0Sstevel@tonic-gate  */
624*0Sstevel@tonic-gate TNFW_B_STATUS
tnfw_b_xabort(TNFW_B_WCB * wcb)625*0Sstevel@tonic-gate tnfw_b_xabort(TNFW_B_WCB *wcb)
626*0Sstevel@tonic-gate {
627*0Sstevel@tonic-gate 	TNFW_B_POS *pos = &wcb->tnfw_w_pos;
628*0Sstevel@tonic-gate 	tnf_block_header_t *block, *next;
629*0Sstevel@tonic-gate 	volatile tnf_buf_file_header_t *fh =
630*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
631*0Sstevel@tonic-gate 		(volatile tnf_buf_file_header_t *)_tnfw_b_control->tnf_buffer;
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	block = pos->tnfw_w_block = pos->tnfw_w_uncommitted;
634*0Sstevel@tonic-gate 	if (block != NULL) {
635*0Sstevel@tonic-gate 		pos->tnfw_w_write_off = block->bytes_valid;
636*0Sstevel@tonic-gate #ifdef TNFWB_MAY_RELEASE_A_LOCK
637*0Sstevel@tonic-gate 		if (0) {		/* XXXX */
638*0Sstevel@tonic-gate 			tnfw_b_clear_lock(&block->A_lock);
639*0Sstevel@tonic-gate 			wcb->tnfw_w_generation = block->generation;
640*0Sstevel@tonic-gate 			wcb->tnfw_w_a_lock_released = B_TRUE;
641*0Sstevel@tonic-gate 		}
642*0Sstevel@tonic-gate #endif
643*0Sstevel@tonic-gate 		block = block->next_block;
644*0Sstevel@tonic-gate 	}
645*0Sstevel@tonic-gate 	while (block != NULL) {
646*0Sstevel@tonic-gate 		next = block->next_block;
647*0Sstevel@tonic-gate 		tnfw_b_clear_lock(&block->A_lock);
648*0Sstevel@tonic-gate 		block = next;
649*0Sstevel@tonic-gate 	}
650*0Sstevel@tonic-gate 	pos->tnfw_w_uncommitted = NULL;
651*0Sstevel@tonic-gate 	pos = &wcb->tnfw_w_tag_pos;
652*0Sstevel@tonic-gate 	block = pos->tnfw_w_uncommitted;
653*0Sstevel@tonic-gate 	while (block && (block != pos->tnfw_w_block)) {
654*0Sstevel@tonic-gate 		block->bytes_valid = fh->com.block_size;
655*0Sstevel@tonic-gate 		pos->tnfw_w_uncommitted = block->next_block;
656*0Sstevel@tonic-gate 		block = pos->tnfw_w_uncommitted;
657*0Sstevel@tonic-gate 	}
658*0Sstevel@tonic-gate 	if (block != NULL)
659*0Sstevel@tonic-gate 		block->bytes_valid = pos->tnfw_w_write_off;
660*0Sstevel@tonic-gate 	pos->tnfw_w_uncommitted = NULL;
661*0Sstevel@tonic-gate 	return (TNFW_B_OK);
662*0Sstevel@tonic-gate }
663*0Sstevel@tonic-gate 
664*0Sstevel@tonic-gate /*
665*0Sstevel@tonic-gate  * The kernel version is different because we can use a spin mutex
666*0Sstevel@tonic-gate  * in the kernel, and not all SPARC systems support the SWAP instruction.
667*0Sstevel@tonic-gate  */
668*0Sstevel@tonic-gate #ifdef _KERNEL
669*0Sstevel@tonic-gate /*ARGSUSED0*/
670*0Sstevel@tonic-gate tnf_uint32_t *
tnfw_b_fw_alloc(TNFW_B_WCB * wcb)671*0Sstevel@tonic-gate tnfw_b_fw_alloc(TNFW_B_WCB *wcb)
672*0Sstevel@tonic-gate {
673*0Sstevel@tonic-gate 	tnf_uint32_t *ret_val;
674*0Sstevel@tonic-gate 	volatile tnf_buf_file_header_t *fh =
675*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
676*0Sstevel@tonic-gate 		(volatile tnf_buf_file_header_t *)_tnfw_b_control->tnf_buffer;
677*0Sstevel@tonic-gate 	tnf_uint32_t *zone_end = (tnf_uint32_t *)((char *)fh + TNFW_B_FW_ZONE);
678*0Sstevel@tonic-gate 	mutex_enter(&hintlock);
679*0Sstevel@tonic-gate 	ret_val = (tnf_uint32_t *)((char *)fh + fh->next_fw_alloc);
680*0Sstevel@tonic-gate 	if (ret_val != zone_end)
681*0Sstevel@tonic-gate 		fh->next_fw_alloc += sizeof (tnf_uint32_t);
682*0Sstevel@tonic-gate 	mutex_exit(&hintlock);
683*0Sstevel@tonic-gate 	return ((ret_val != zone_end) ? ret_val : NULL);
684*0Sstevel@tonic-gate }
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate #else
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate /*ARGSUSED0*/
689*0Sstevel@tonic-gate tnf_uint32_t *
tnfw_b_fw_alloc(TNFW_B_WCB * wcb)690*0Sstevel@tonic-gate tnfw_b_fw_alloc(TNFW_B_WCB *wcb)
691*0Sstevel@tonic-gate {
692*0Sstevel@tonic-gate 	volatile tnf_buf_file_header_t *fh =
693*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
694*0Sstevel@tonic-gate 		(volatile tnf_buf_file_header_t *)_tnfw_b_control->tnf_buffer;
695*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
696*0Sstevel@tonic-gate 	uint_t *hint = (uint_t *)((uintptr_t)fh + fh->next_fw_alloc);
697*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
698*0Sstevel@tonic-gate 	ulong_t *zone_end = (ulong_t *)((uintptr_t)fh + TNFW_B_FW_ZONE);
699*0Sstevel@tonic-gate 	u_long swapin;
700*0Sstevel@tonic-gate 	char tmp_buf[512];
701*0Sstevel@tonic-gate 	tnf_uint32_t *retval;
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate #ifdef VERYVERBOSE
704*0Sstevel@tonic-gate 	    sprintf(tmp_buf, "tnfw_b_vw_alloc: begin\n");
705*0Sstevel@tonic-gate 	    (void) write(2, tmp_buf, strlen(tmp_buf));
706*0Sstevel@tonic-gate #endif
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate #ifdef VERYVERBOSE
709*0Sstevel@tonic-gate 	    sprintf(tmp_buf, "tnfw_b_vw_alloc: (1)hint=%p\n", hint);
710*0Sstevel@tonic-gate 	    (void) write(2, tmp_buf, strlen(tmp_buf));
711*0Sstevel@tonic-gate #endif
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	while ((uintptr_t)hint != (uintptr_t)zone_end) {
714*0Sstevel@tonic-gate #ifdef VERYVERBOSE
715*0Sstevel@tonic-gate 	    sprintf(tmp_buf, "tnfw_b_vw_alloc: (2)hint=%p,zone_end=%p\n",
716*0Sstevel@tonic-gate 		    hint, zone_end);
717*0Sstevel@tonic-gate 	    (void) write(2, tmp_buf, strlen(tmp_buf));
718*0Sstevel@tonic-gate #endif
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate #ifdef VERYVERBOSE
721*0Sstevel@tonic-gate 	sprintf(tmp_buf, "tnfw_b_fw_alloc: fh = %p, next->alloc = %d\n",
722*0Sstevel@tonic-gate 		fh, fh->next_fw_alloc);
723*0Sstevel@tonic-gate 	(void) write(2, tmp_buf, strlen(tmp_buf));
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	    sprintf(tmp_buf, "tnfw_b_vw_alloc: about to deref hint\n");
726*0Sstevel@tonic-gate 	    (void) write(2, tmp_buf, strlen(tmp_buf));
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	    sprintf(tmp_buf, "tnfw_b_vw_alloc: *hint=%ld\n", *hint);
729*0Sstevel@tonic-gate 	    (void) write(2, tmp_buf, strlen(tmp_buf));
730*0Sstevel@tonic-gate #endif
731*0Sstevel@tonic-gate 		if (*hint == 0) {
732*0Sstevel@tonic-gate 			swapin = tnfw_b_atomic_swap(hint, TNFW_B_FW_INVALID);
733*0Sstevel@tonic-gate 			if (swapin != 0) {
734*0Sstevel@tonic-gate 				if (swapin != (unsigned)TNFW_B_FW_INVALID) {
735*0Sstevel@tonic-gate 					/* restore */
736*0Sstevel@tonic-gate 					*hint = swapin;
737*0Sstevel@tonic-gate 				}
738*0Sstevel@tonic-gate 			} else {
739*0Sstevel@tonic-gate 				break;
740*0Sstevel@tonic-gate 			}
741*0Sstevel@tonic-gate 		}
742*0Sstevel@tonic-gate 		++hint;
743*0Sstevel@tonic-gate #ifdef VERYVERBOSE
744*0Sstevel@tonic-gate 	    sprintf(tmp_buf, "tnfw_b_vw_alloc: (3)hint=%p\n", hint);
745*0Sstevel@tonic-gate 	    (void) write(2, tmp_buf, strlen(tmp_buf));
746*0Sstevel@tonic-gate #endif
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	}
749*0Sstevel@tonic-gate 	/* LINTED pointer subtraction casted to 32 bits */
750*0Sstevel@tonic-gate 	fh->next_fw_alloc = (uint_t) ((char *)hint - (char *)fh);
751*0Sstevel@tonic-gate 	retval = (((uintptr_t)hint != (uintptr_t)zone_end) ?
752*0Sstevel@tonic-gate 		(tnf_uint32_t *)hint : NULL);
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate #ifdef VERYVERBOSE
755*0Sstevel@tonic-gate 	sprintf(tmp_buf, "tnfw_b_vw_alloc: returning %p", retval);
756*0Sstevel@tonic-gate 	(void) write(2, tmp_buf, strlen(tmp_buf));
757*0Sstevel@tonic-gate #endif
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	return (retval);
760*0Sstevel@tonic-gate }
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate #endif	/* _KERNEL */
763