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