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 2004 Sun Microsystems, 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 <unistd.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <libintl.h>
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
37*7c478bd9Sstevel@tonic-gate #include <kvm.h>
38*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/tnf_com.h>
42*7c478bd9Sstevel@tonic-gate #include <nlist.h>
43*7c478bd9Sstevel@tonic-gate #include <errno.h>
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate #define TNFDEV "/dev/tnfmap"
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #define MAXFWTRY 5
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate typedef struct {
50*7c478bd9Sstevel@tonic-gate boolean_t ever_read;
51*7c478bd9Sstevel@tonic-gate tnf_uint32_t generation;
52*7c478bd9Sstevel@tonic-gate tnf_uint16_t bytes_valid;
53*7c478bd9Sstevel@tonic-gate } BLOCK_STATUS;
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate static char *dumpfile; /* Dump file if extracting from crashdump */
56*7c478bd9Sstevel@tonic-gate static char *namelist; /* Symbol tbl. if extracting from crashdump */
57*7c478bd9Sstevel@tonic-gate static kvm_t *kvm_p; /* Handle for kvm_open, kvm_read, ... */
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate static struct nlist kvm_syms[] = {
60*7c478bd9Sstevel@tonic-gate { "tnf_buf" },
61*7c478bd9Sstevel@tonic-gate { "tnf_trace_file_size" },
62*7c478bd9Sstevel@tonic-gate { NULL }
63*7c478bd9Sstevel@tonic-gate };
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate static uintptr_t dump_bufaddr;
66*7c478bd9Sstevel@tonic-gate static size_t tnf_bufsize;
67*7c478bd9Sstevel@tonic-gate static char *program_name;
68*7c478bd9Sstevel@tonic-gate static int input_fd;
69*7c478bd9Sstevel@tonic-gate static int output_fd;
70*7c478bd9Sstevel@tonic-gate static tnf_file_header_t *tnf_header;
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate * usage() - gives a description of the arguments, and exits
74*7c478bd9Sstevel@tonic-gate */
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate static void
usage(char * argv[],const char * msg)77*7c478bd9Sstevel@tonic-gate usage(char *argv[], const char *msg)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate if (msg)
80*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
81*7c478bd9Sstevel@tonic-gate gettext("%s: %s\n"), argv[0], msg);
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
84*7c478bd9Sstevel@tonic-gate "usage: %s [-d <dumpfile> -n <symbolfile> ] "
85*7c478bd9Sstevel@tonic-gate "<output-filename>\n"), argv[0]);
86*7c478bd9Sstevel@tonic-gate exit(1);
87*7c478bd9Sstevel@tonic-gate } /* end usage */
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate * Write 'size' bytes at offset 'offset' from 'addr'
92*7c478bd9Sstevel@tonic-gate * to the output file. Bail out unceremoniously if anything goes wrong.
93*7c478bd9Sstevel@tonic-gate */
94*7c478bd9Sstevel@tonic-gate static void
writeout(char * addr,int offset,int size)95*7c478bd9Sstevel@tonic-gate writeout(char *addr, int offset, int size)
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate if (lseek(output_fd, offset, SEEK_SET) < 0) {
99*7c478bd9Sstevel@tonic-gate perror("lseek");
100*7c478bd9Sstevel@tonic-gate exit(1);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate if (write(output_fd, addr, size) != size) {
103*7c478bd9Sstevel@tonic-gate perror("write");
104*7c478bd9Sstevel@tonic-gate exit(1);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate static void
dumpfile_init()110*7c478bd9Sstevel@tonic-gate dumpfile_init()
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate kvm_p = kvm_open(namelist, dumpfile, NULL, O_RDONLY, program_name);
114*7c478bd9Sstevel@tonic-gate if (kvm_p == NULL) {
115*7c478bd9Sstevel@tonic-gate /* kvm_open prints an error message */
116*7c478bd9Sstevel@tonic-gate exit(1);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate if (kvm_nlist(kvm_p, kvm_syms) != 0) {
119*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
120*7c478bd9Sstevel@tonic-gate "Symbol lookup error in %s\n"), namelist);
121*7c478bd9Sstevel@tonic-gate exit(1);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate if (kvm_read(kvm_p, kvm_syms[0].n_value, (char *) &dump_bufaddr,
124*7c478bd9Sstevel@tonic-gate sizeof (dump_bufaddr)) != sizeof (dump_bufaddr) ||
125*7c478bd9Sstevel@tonic-gate kvm_read(kvm_p, kvm_syms[1].n_value, (char *) &tnf_bufsize,
126*7c478bd9Sstevel@tonic-gate sizeof (tnf_bufsize)) != sizeof (tnf_bufsize)) {
127*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
128*7c478bd9Sstevel@tonic-gate "kvm_read error in %s\n"), dumpfile);
129*7c478bd9Sstevel@tonic-gate exit(1);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate if (dump_bufaddr == NULL || tnf_bufsize == 0) {
132*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
133*7c478bd9Sstevel@tonic-gate "No trace data available in the kernel.\n"));
134*7c478bd9Sstevel@tonic-gate exit(1);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate static void
live_kernel_init()139*7c478bd9Sstevel@tonic-gate live_kernel_init()
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate tifiocstate_t tstate;
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate if ((input_fd = open(TNFDEV, O_RDWR)) < 0) {
145*7c478bd9Sstevel@tonic-gate perror(TNFDEV);
146*7c478bd9Sstevel@tonic-gate exit(1);
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate if (ioctl(input_fd, TIFIOCGSTATE, &tstate) < 0) {
149*7c478bd9Sstevel@tonic-gate perror(gettext("Error getting trace system state"));
150*7c478bd9Sstevel@tonic-gate exit(1);
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate if (tstate.buffer_state != TIFIOCBUF_OK) {
153*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
154*7c478bd9Sstevel@tonic-gate "No trace data available in the kernel.\n"));
155*7c478bd9Sstevel@tonic-gate exit(1);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate tnf_bufsize = tstate.buffer_size;
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate static void
read_tnf_header(char * addr)161*7c478bd9Sstevel@tonic-gate read_tnf_header(char *addr)
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate if (dumpfile != NULL) {
165*7c478bd9Sstevel@tonic-gate if (kvm_read(kvm_p, dump_bufaddr, addr, 512) != 512) {
166*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
167*7c478bd9Sstevel@tonic-gate "Error reading tnf header from dump file.\n"));
168*7c478bd9Sstevel@tonic-gate exit(1);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate } else {
171*7c478bd9Sstevel@tonic-gate if (ioctl(input_fd, TIFIOCGHEADER, addr) != 0) {
172*7c478bd9Sstevel@tonic-gate perror(gettext("Error reading tnf header from kernel"));
173*7c478bd9Sstevel@tonic-gate exit(1);
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate
178*7c478bd9Sstevel@tonic-gate static int
read_tnf_block(tnf_block_header_t * addr,int block_num)179*7c478bd9Sstevel@tonic-gate read_tnf_block(tnf_block_header_t *addr, int block_num)
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate {
182*7c478bd9Sstevel@tonic-gate int offset;
183*7c478bd9Sstevel@tonic-gate tifiocgblock_t ioctl_arg;
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate if (dumpfile != NULL) {
186*7c478bd9Sstevel@tonic-gate offset = tnf_header->directory_size +
187*7c478bd9Sstevel@tonic-gate block_num * tnf_header->block_size;
188*7c478bd9Sstevel@tonic-gate if (kvm_read(kvm_p, dump_bufaddr + offset, (char *) addr,
189*7c478bd9Sstevel@tonic-gate tnf_header->block_size) != tnf_header->block_size) {
190*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
191*7c478bd9Sstevel@tonic-gate "Error reading tnf block.\n"));
192*7c478bd9Sstevel@tonic-gate exit(1);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate } else {
195*7c478bd9Sstevel@tonic-gate ioctl_arg.dst_addr = (char *) addr;
196*7c478bd9Sstevel@tonic-gate ioctl_arg.block_num = block_num;
197*7c478bd9Sstevel@tonic-gate if (ioctl(input_fd, TIFIOCGBLOCK, &ioctl_arg) < 0) {
198*7c478bd9Sstevel@tonic-gate if (errno == EBUSY)
199*7c478bd9Sstevel@tonic-gate return (EBUSY);
200*7c478bd9Sstevel@tonic-gate perror(gettext("Error reading tnf block"));
201*7c478bd9Sstevel@tonic-gate exit(1);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate return (0);
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate static void
read_tnf_fwzone(tnf_ref32_t * dest,int start,int slots)208*7c478bd9Sstevel@tonic-gate read_tnf_fwzone(tnf_ref32_t *dest, int start, int slots)
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate {
211*7c478bd9Sstevel@tonic-gate int offset;
212*7c478bd9Sstevel@tonic-gate int len;
213*7c478bd9Sstevel@tonic-gate tifiocgfw_t ioctl_arg;
214*7c478bd9Sstevel@tonic-gate
215*7c478bd9Sstevel@tonic-gate if (dumpfile != NULL) {
216*7c478bd9Sstevel@tonic-gate /* LINTED assignment of 64-bit integer to 32-bit integer */
217*7c478bd9Sstevel@tonic-gate offset = tnf_header->block_size + start * sizeof (tnf_ref32_t);
218*7c478bd9Sstevel@tonic-gate /* LINTED assignment of 64-bit integer to 32-bit integer */
219*7c478bd9Sstevel@tonic-gate len = slots * sizeof (tnf_ref32_t);
220*7c478bd9Sstevel@tonic-gate if (kvm_read(kvm_p, dump_bufaddr + offset, (char *) dest,
221*7c478bd9Sstevel@tonic-gate len) != len) {
222*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
223*7c478bd9Sstevel@tonic-gate "Error reading tnf forwarding zone.\n"));
224*7c478bd9Sstevel@tonic-gate exit(1);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate } else {
227*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
228*7c478bd9Sstevel@tonic-gate ioctl_arg.dst_addr = (long *) dest;
229*7c478bd9Sstevel@tonic-gate ioctl_arg.start = start;
230*7c478bd9Sstevel@tonic-gate ioctl_arg.slots = slots;
231*7c478bd9Sstevel@tonic-gate if (ioctl(input_fd, TIFIOCGFWZONE, &ioctl_arg) < 0) {
232*7c478bd9Sstevel@tonic-gate perror(gettext("Error reading tnf block"));
233*7c478bd9Sstevel@tonic-gate exit(1);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])239*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate const char *optstr = "d:n:";
242*7c478bd9Sstevel@tonic-gate const char *outfile;
243*7c478bd9Sstevel@tonic-gate char *local_buf;
244*7c478bd9Sstevel@tonic-gate int c;
245*7c478bd9Sstevel@tonic-gate tnf_uint32_t *magicp;
246*7c478bd9Sstevel@tonic-gate tnf_block_header_t *block_base, *blockp;
247*7c478bd9Sstevel@tonic-gate BLOCK_STATUS *block_stat, *bsp;
248*7c478bd9Sstevel@tonic-gate int block_num;
249*7c478bd9Sstevel@tonic-gate boolean_t any_unread, any_different, retry;
250*7c478bd9Sstevel@tonic-gate tnf_ref32_t *fwzone;
251*7c478bd9Sstevel@tonic-gate int fwzonesize;
252*7c478bd9Sstevel@tonic-gate int i;
253*7c478bd9Sstevel@tonic-gate int fwtries;
254*7c478bd9Sstevel@tonic-gate int block_count;
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate program_name = argv[0];
257*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, optstr)) != EOF) {
258*7c478bd9Sstevel@tonic-gate switch (c) {
259*7c478bd9Sstevel@tonic-gate case 'd':
260*7c478bd9Sstevel@tonic-gate dumpfile = optarg;
261*7c478bd9Sstevel@tonic-gate break;
262*7c478bd9Sstevel@tonic-gate case 'n':
263*7c478bd9Sstevel@tonic-gate namelist = optarg;
264*7c478bd9Sstevel@tonic-gate break;
265*7c478bd9Sstevel@tonic-gate case '?':
266*7c478bd9Sstevel@tonic-gate usage(argv, gettext("unrecognized argument"));
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate if (optind != argc - 1) {
270*7c478bd9Sstevel@tonic-gate usage(argv, gettext("too many or too few arguments"));
271*7c478bd9Sstevel@tonic-gate } else {
272*7c478bd9Sstevel@tonic-gate outfile = argv[optind];
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate if ((dumpfile != NULL) ^ (namelist != NULL)) {
275*7c478bd9Sstevel@tonic-gate usage(argv, gettext("must specify both or neither of the "
276*7c478bd9Sstevel@tonic-gate "-d and -n options"));
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate
279*7c478bd9Sstevel@tonic-gate output_fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
280*7c478bd9Sstevel@tonic-gate if (output_fd < 0) {
281*7c478bd9Sstevel@tonic-gate perror(outfile);
282*7c478bd9Sstevel@tonic-gate exit(1);
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate if (dumpfile != NULL)
285*7c478bd9Sstevel@tonic-gate dumpfile_init();
286*7c478bd9Sstevel@tonic-gate else
287*7c478bd9Sstevel@tonic-gate live_kernel_init();
288*7c478bd9Sstevel@tonic-gate
289*7c478bd9Sstevel@tonic-gate if ((local_buf = malloc(tnf_bufsize)) == NULL) {
290*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
291*7c478bd9Sstevel@tonic-gate gettext("tnfxtract memory allocation failure\n"));
292*7c478bd9Sstevel@tonic-gate exit(1);
293*7c478bd9Sstevel@tonic-gate }
294*7c478bd9Sstevel@tonic-gate
295*7c478bd9Sstevel@tonic-gate /* Read header, get block size, check for version mismatch */
296*7c478bd9Sstevel@tonic-gate read_tnf_header(local_buf);
297*7c478bd9Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/
298*7c478bd9Sstevel@tonic-gate magicp = (tnf_uint32_t *) local_buf;
299*7c478bd9Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/
300*7c478bd9Sstevel@tonic-gate tnf_header = (tnf_file_header_t *)(local_buf + sizeof (*magicp));
301*7c478bd9Sstevel@tonic-gate if (*magicp != TNF_MAGIC) {
302*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
303*7c478bd9Sstevel@tonic-gate "Buffer is not in TNF format.\n"));
304*7c478bd9Sstevel@tonic-gate exit(1);
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate if (tnf_header->file_version != TNF_FILE_VERSION) {
307*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
308*7c478bd9Sstevel@tonic-gate gettext("Version mismatch (tnfxtract: %d; buffer: %d)\n"),
309*7c478bd9Sstevel@tonic-gate TNF_FILE_VERSION, tnf_header->file_version);
310*7c478bd9Sstevel@tonic-gate exit(1);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate writeout(local_buf, 0, tnf_header->block_size);
313*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
314*7c478bd9Sstevel@tonic-gate block_base = (tnf_block_header_t *)
315*7c478bd9Sstevel@tonic-gate (local_buf + tnf_header->directory_size);
316*7c478bd9Sstevel@tonic-gate block_count = tnf_header->block_count -
317*7c478bd9Sstevel@tonic-gate tnf_header->directory_size / tnf_header->block_size;
318*7c478bd9Sstevel@tonic-gate fwzonesize = tnf_header->directory_size - tnf_header->block_size;
319*7c478bd9Sstevel@tonic-gate
320*7c478bd9Sstevel@tonic-gate block_stat = (BLOCK_STATUS *)
321*7c478bd9Sstevel@tonic-gate calloc(block_count, sizeof (BLOCK_STATUS));
322*7c478bd9Sstevel@tonic-gate if (block_stat == NULL) {
323*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
324*7c478bd9Sstevel@tonic-gate gettext("tnfxtract memory allocation failure\n"));
325*7c478bd9Sstevel@tonic-gate exit(1);
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate
328*7c478bd9Sstevel@tonic-gate for (bsp = block_stat; bsp != block_stat + block_count; ++bsp)
329*7c478bd9Sstevel@tonic-gate bsp->ever_read = B_FALSE;
330*7c478bd9Sstevel@tonic-gate /*
331*7c478bd9Sstevel@tonic-gate * Make repeated passes until we've read every non-tag block.
332*7c478bd9Sstevel@tonic-gate */
333*7c478bd9Sstevel@tonic-gate do {
334*7c478bd9Sstevel@tonic-gate any_unread = B_FALSE;
335*7c478bd9Sstevel@tonic-gate bsp = block_stat;
336*7c478bd9Sstevel@tonic-gate block_num = 0;
337*7c478bd9Sstevel@tonic-gate blockp = block_base;
338*7c478bd9Sstevel@tonic-gate while (block_num != block_count) {
339*7c478bd9Sstevel@tonic-gate if (!bsp->ever_read) {
340*7c478bd9Sstevel@tonic-gate if (read_tnf_block(blockp, block_num) != 0)
341*7c478bd9Sstevel@tonic-gate any_unread = B_TRUE;
342*7c478bd9Sstevel@tonic-gate else {
343*7c478bd9Sstevel@tonic-gate bsp->ever_read = B_TRUE;
344*7c478bd9Sstevel@tonic-gate bsp->generation = blockp->generation;
345*7c478bd9Sstevel@tonic-gate bsp->bytes_valid = blockp->bytes_valid;
346*7c478bd9Sstevel@tonic-gate writeout((char *) blockp,
347*7c478bd9Sstevel@tonic-gate /* LINTED cast 64 to 32 bit */
348*7c478bd9Sstevel@tonic-gate (int)((char *) blockp - local_buf),
349*7c478bd9Sstevel@tonic-gate tnf_header->block_size);
350*7c478bd9Sstevel@tonic-gate }
351*7c478bd9Sstevel@tonic-gate }
352*7c478bd9Sstevel@tonic-gate ++bsp;
353*7c478bd9Sstevel@tonic-gate ++block_num;
354*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
355*7c478bd9Sstevel@tonic-gate blockp = (tnf_block_header_t *)
356*7c478bd9Sstevel@tonic-gate ((char *) blockp + tnf_header->block_size);
357*7c478bd9Sstevel@tonic-gate }
358*7c478bd9Sstevel@tonic-gate } while (any_unread);
359*7c478bd9Sstevel@tonic-gate
360*7c478bd9Sstevel@tonic-gate /*
361*7c478bd9Sstevel@tonic-gate * Then read tag blocks only, until we have two consecutive,
362*7c478bd9Sstevel@tonic-gate * consistent reads.
363*7c478bd9Sstevel@tonic-gate */
364*7c478bd9Sstevel@tonic-gate do {
365*7c478bd9Sstevel@tonic-gate any_different = B_FALSE;
366*7c478bd9Sstevel@tonic-gate bsp = block_stat;
367*7c478bd9Sstevel@tonic-gate block_num = 0;
368*7c478bd9Sstevel@tonic-gate blockp = block_base;
369*7c478bd9Sstevel@tonic-gate while (block_num != block_count) {
370*7c478bd9Sstevel@tonic-gate if (read_tnf_block(blockp, block_num) == 0 &&
371*7c478bd9Sstevel@tonic-gate blockp->generation == TNF_TAG_GENERATION_NUM &&
372*7c478bd9Sstevel@tonic-gate (bsp->generation != TNF_TAG_GENERATION_NUM ||
373*7c478bd9Sstevel@tonic-gate bsp->bytes_valid != blockp->bytes_valid)) {
374*7c478bd9Sstevel@tonic-gate bsp->generation = TNF_TAG_GENERATION_NUM;
375*7c478bd9Sstevel@tonic-gate bsp->bytes_valid = blockp->bytes_valid;
376*7c478bd9Sstevel@tonic-gate writeout((char *) blockp,
377*7c478bd9Sstevel@tonic-gate /* LINTED cast 64bit to 32 bit */
378*7c478bd9Sstevel@tonic-gate (int)((char *) blockp - local_buf),
379*7c478bd9Sstevel@tonic-gate tnf_header->block_size);
380*7c478bd9Sstevel@tonic-gate any_different = B_TRUE;
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate ++bsp;
383*7c478bd9Sstevel@tonic-gate ++block_num;
384*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
385*7c478bd9Sstevel@tonic-gate blockp = (tnf_block_header_t *)
386*7c478bd9Sstevel@tonic-gate ((char *) blockp + tnf_header->block_size);
387*7c478bd9Sstevel@tonic-gate }
388*7c478bd9Sstevel@tonic-gate } while (any_different);
389*7c478bd9Sstevel@tonic-gate
390*7c478bd9Sstevel@tonic-gate /*
391*7c478bd9Sstevel@tonic-gate * Then read the forwarding pointers. If any are -1:
392*7c478bd9Sstevel@tonic-gate * sleep briefly, then make another pass.
393*7c478bd9Sstevel@tonic-gate */
394*7c478bd9Sstevel@tonic-gate /*LINTED pointer cast may result in improper alignment*/
395*7c478bd9Sstevel@tonic-gate fwzone = (tnf_ref32_t *)(local_buf + tnf_header->block_size);
396*7c478bd9Sstevel@tonic-gate
397*7c478bd9Sstevel@tonic-gate read_tnf_fwzone(fwzone, 0,
398*7c478bd9Sstevel@tonic-gate /* LINTED cast from 64-bit integer to 32-bit integer */
399*7c478bd9Sstevel@tonic-gate (int)(fwzonesize / sizeof (fwzone[0])));
400*7c478bd9Sstevel@tonic-gate fwtries = 0;
401*7c478bd9Sstevel@tonic-gate while (fwtries != MAXFWTRY) {
402*7c478bd9Sstevel@tonic-gate retry = B_FALSE;
403*7c478bd9Sstevel@tonic-gate for (i = 0; i != fwzonesize / sizeof (fwzone[0]); ++i) {
404*7c478bd9Sstevel@tonic-gate if (fwzone[i] == -1) {
405*7c478bd9Sstevel@tonic-gate read_tnf_fwzone(&fwzone[i], i, 1);
406*7c478bd9Sstevel@tonic-gate if (!retry) {
407*7c478bd9Sstevel@tonic-gate retry = B_TRUE;
408*7c478bd9Sstevel@tonic-gate ++fwtries;
409*7c478bd9Sstevel@tonic-gate }
410*7c478bd9Sstevel@tonic-gate }
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate if (!retry)
413*7c478bd9Sstevel@tonic-gate break;
414*7c478bd9Sstevel@tonic-gate sleep(2);
415*7c478bd9Sstevel@tonic-gate }
416*7c478bd9Sstevel@tonic-gate if (fwtries == MAXFWTRY) {
417*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
418*7c478bd9Sstevel@tonic-gate "Warning: forwarding pointers may "
419*7c478bd9Sstevel@tonic-gate "be invalid.\n"));
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate writeout((char *) fwzone, tnf_header->block_size, fwzonesize);
422*7c478bd9Sstevel@tonic-gate return (0);
423*7c478bd9Sstevel@tonic-gate }
424