xref: /illumos-gate/usr/src/cmd/fs.d/nfs/nfslog/readbuf.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*11606941Sjwahlig  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * nfs log - read buffer file and return structs in usable form
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <stddef.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
437c478bd9Sstevel@tonic-gate #include <sys/mman.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <syslog.h>
477c478bd9Sstevel@tonic-gate #include <time.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <libintl.h>
507c478bd9Sstevel@tonic-gate #include <values.h>
517c478bd9Sstevel@tonic-gate #include <search.h>
527c478bd9Sstevel@tonic-gate #include <pwd.h>
537c478bd9Sstevel@tonic-gate #include <netdb.h>
547c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
557c478bd9Sstevel@tonic-gate #include <netconfig.h>
567c478bd9Sstevel@tonic-gate #include <netdir.h>
577c478bd9Sstevel@tonic-gate #include <nfs/nfs_sec.h>
587c478bd9Sstevel@tonic-gate #include <nfs/export.h>
597c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
607c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
617c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
627c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
637c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
647c478bd9Sstevel@tonic-gate #include <nfs/nfs_log.h>
657c478bd9Sstevel@tonic-gate #include "nfslogd.h"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	MAX_LRS_READ_AHEAD 2048
687c478bd9Sstevel@tonic-gate #define	MAX_RECS_TO_DELAY 32768
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int 		nfslog_init_buf(char *, struct nfslog_buf *, int *);
717c478bd9Sstevel@tonic-gate static void		nfslog_free_buf(struct nfslog_buf *, int);
727c478bd9Sstevel@tonic-gate static struct nfslog_lr *nfslog_read_buffer(struct nfslog_buf *);
737c478bd9Sstevel@tonic-gate static void		free_lrp(struct nfslog_lr *);
747c478bd9Sstevel@tonic-gate static struct nfslog_lr *remove_lrp_from_lb(struct nfslog_buf *,
757c478bd9Sstevel@tonic-gate 			struct nfslog_lr *);
767c478bd9Sstevel@tonic-gate static void		insert_lrp_to_lb(struct nfslog_buf *,
777c478bd9Sstevel@tonic-gate 			struct nfslog_lr *);
787c478bd9Sstevel@tonic-gate static void		nfslog_rewrite_bufheader(struct nfslog_buf *);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Treat the provided path name as an NFS log buffer file.
827c478bd9Sstevel@tonic-gate  * Allocate a data structure for its handling and initialize it.
837c478bd9Sstevel@tonic-gate  * *error contains the previous error condition encountered for
847c478bd9Sstevel@tonic-gate  * this object. This value can be used to avoid printing the last
857c478bd9Sstevel@tonic-gate  * error endlessly.
867c478bd9Sstevel@tonic-gate  * It will set *error appropriately after processing.
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate struct nfslog_buf *
nfslog_open_buf(char * bufpath,int * error)897c478bd9Sstevel@tonic-gate nfslog_open_buf(char *bufpath, int *error)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	struct nfslog_buf	*lbp = NULL;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (bufpath == NULL) {
947c478bd9Sstevel@tonic-gate 		*error = EINVAL;
957c478bd9Sstevel@tonic-gate 		return (NULL);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if ((lbp = malloc(sizeof (struct nfslog_buf))) == NULL) {
997c478bd9Sstevel@tonic-gate 		*error = ENOMEM;
1007c478bd9Sstevel@tonic-gate 		return (NULL);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	bzero(lbp, sizeof (struct nfslog_buf));
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (nfslog_init_buf(bufpath, lbp, error)) {
1057c478bd9Sstevel@tonic-gate 		free(lbp);
1067c478bd9Sstevel@tonic-gate 		return (NULL);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	return (lbp);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Free the log buffer struct with all of its baggage and free the data struct
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate void
nfslog_close_buf(struct nfslog_buf * lbp,int close_quick)1157c478bd9Sstevel@tonic-gate nfslog_close_buf(struct nfslog_buf *lbp, int close_quick)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	nfslog_free_buf(lbp, close_quick);
1187c478bd9Sstevel@tonic-gate 	free(lbp);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Set up the log buffer struct; simple things are opening and locking
1237c478bd9Sstevel@tonic-gate  * the buffer file and then on to mmap()ing it for later use by the
1247c478bd9Sstevel@tonic-gate  * XDR decode path.  Make sure to read the buffer header before
1257c478bd9Sstevel@tonic-gate  * returning so that we will be at the first true log record.
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * *error contains the last error encountered on this object. It can
1287c478bd9Sstevel@tonic-gate  * be used to avoid reporting the same error endlessly. It is reset
1297c478bd9Sstevel@tonic-gate  * to the current error code on return.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate static int
nfslog_init_buf(char * bufpath,struct nfslog_buf * lbp,int * error)1327c478bd9Sstevel@tonic-gate nfslog_init_buf(char *bufpath, struct nfslog_buf *lbp, int *error)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	struct stat sb;
1357c478bd9Sstevel@tonic-gate 	int preverror = *error;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	lbp->next = lbp;
1387c478bd9Sstevel@tonic-gate 	lbp->prev = lbp;
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * set these values so that the free routine will know what to do
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	lbp->mmap_addr = (intptr_t)MAP_FAILED;
1437c478bd9Sstevel@tonic-gate 	lbp->last_rec_id = MAXINT - 1;
1447c478bd9Sstevel@tonic-gate 	lbp->bh.bh_length = 0;
1457c478bd9Sstevel@tonic-gate 	lbp->bh_lrp = NULL;
1467c478bd9Sstevel@tonic-gate 	lbp->num_lrps = 0;
1477c478bd9Sstevel@tonic-gate 	lbp->lrps = NULL;
1487c478bd9Sstevel@tonic-gate 	lbp->last_record_offset = 0;
1497c478bd9Sstevel@tonic-gate 	lbp->prp = NULL;
1507c478bd9Sstevel@tonic-gate 	lbp->num_pr_queued = 0;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	lbp->bufpath = strdup(bufpath);
1537c478bd9Sstevel@tonic-gate 	if (lbp->bufpath == NULL) {
1547c478bd9Sstevel@tonic-gate 		*error = ENOMEM;
1557c478bd9Sstevel@tonic-gate 		if (preverror != *error) {
1567c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("Cannot strdup '%s': %s"),
1577c478bd9Sstevel@tonic-gate 				bufpath, strerror(*error));
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 		nfslog_free_buf(lbp, FALSE);
1607c478bd9Sstevel@tonic-gate 		return (*error);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((lbp->fd = open(bufpath, O_RDWR)) < 0) {
1647c478bd9Sstevel@tonic-gate 		*error = errno;
1657c478bd9Sstevel@tonic-gate 		if (preverror != *error) {
1667c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("Cannot open '%s': %s"),
1677c478bd9Sstevel@tonic-gate 				bufpath, strerror(*error));
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		nfslog_free_buf(lbp, FALSE);
1707c478bd9Sstevel@tonic-gate 		return (*error);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Lock the entire buffer file to prevent conflicting access.
1757c478bd9Sstevel@tonic-gate 	 * We get a write lock because we want only 1 process to be
1767c478bd9Sstevel@tonic-gate 	 * generating records from it.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	lbp->fl.l_type = F_WRLCK;
1797c478bd9Sstevel@tonic-gate 	lbp->fl.l_whence = SEEK_SET;		/* beginning of file */
1807c478bd9Sstevel@tonic-gate 	lbp->fl.l_start = (offset_t)0;
1817c478bd9Sstevel@tonic-gate 	lbp->fl.l_len = 0;			/* entire file */
1827c478bd9Sstevel@tonic-gate 	lbp->fl.l_sysid = 0;
1837c478bd9Sstevel@tonic-gate 	lbp->fl.l_pid = 0;
1847c478bd9Sstevel@tonic-gate 	if (fcntl(lbp->fd, F_SETLKW, &lbp->fl) == -1) {
1857c478bd9Sstevel@tonic-gate 		*error = errno;
1867c478bd9Sstevel@tonic-gate 		if (preverror != *error) {
1877c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("Cannot lock (%s): %s"),
1887c478bd9Sstevel@tonic-gate 				bufpath, strerror(*error));
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		nfslog_free_buf(lbp, FALSE);
1917c478bd9Sstevel@tonic-gate 		return (*error);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (fstat(lbp->fd, &sb)) {
1957c478bd9Sstevel@tonic-gate 		*error = errno;
1967c478bd9Sstevel@tonic-gate 		if (preverror != *error) {
1977c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("Cannot stat (%s): %s"),
1987c478bd9Sstevel@tonic-gate 				bufpath, strerror(*error));
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		nfslog_free_buf(lbp, FALSE);
2017c478bd9Sstevel@tonic-gate 		return (*error);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	lbp->filesize = sb.st_size;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	lbp->mmap_addr = (intptr_t)mmap(0, lbp->filesize, PROT_READ|PROT_WRITE,
2067c478bd9Sstevel@tonic-gate 		MAP_SHARED|MAP_NORESERVE, lbp->fd, 0);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* This is part of the duality of the use of either mmap()|read() */
2097c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr == (intptr_t)MAP_FAILED) {
2107c478bd9Sstevel@tonic-gate 		lbp->next_rec = 0;
2117c478bd9Sstevel@tonic-gate 	} else {
2127c478bd9Sstevel@tonic-gate 		lbp->next_rec = lbp->mmap_addr;
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/* Read the header */
2167c478bd9Sstevel@tonic-gate 	if ((lbp->bh_lrp = nfslog_read_buffer(lbp)) == NULL) {
2177c478bd9Sstevel@tonic-gate 		*error = EIO;
2187c478bd9Sstevel@tonic-gate 		if (preverror != *error) {
2197c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
2207c478bd9Sstevel@tonic-gate 				"error in reading file '%s': %s"),
2217c478bd9Sstevel@tonic-gate 				bufpath, strerror(EIO));
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 		nfslog_free_buf(lbp, FALSE);
2247c478bd9Sstevel@tonic-gate 		return (*error);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (!xdr_nfslog_buffer_header(&lbp->bh_lrp->xdrs, &lbp->bh)) {
2287c478bd9Sstevel@tonic-gate 		*error = EIO;
2297c478bd9Sstevel@tonic-gate 		if (preverror != *error) {
2307c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
2317c478bd9Sstevel@tonic-gate 				"error in reading file '%s': %s"),
2327c478bd9Sstevel@tonic-gate 				bufpath, strerror(*error));
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		nfslog_free_buf(lbp, FALSE);
2357c478bd9Sstevel@tonic-gate 		return (*error);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * Set the pointer to the next record based on the buffer header.
2407c478bd9Sstevel@tonic-gate 	 * 'lbp->bh.bh_offset' contains the offset of where to begin
2417c478bd9Sstevel@tonic-gate 	 * processing relative to the buffer header.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	lbp->next_rec += lbp->bh.bh_offset;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * If we are going to be using read() for file data, then we may
2477c478bd9Sstevel@tonic-gate 	 * have to adjust the current file pointer to take into account
2487c478bd9Sstevel@tonic-gate 	 * a starting point other than the beginning of the file.
2497c478bd9Sstevel@tonic-gate 	 * If mmap is being used, this is taken care of as a side effect of
2507c478bd9Sstevel@tonic-gate 	 * setting up the value of next_rec.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr == (intptr_t)MAP_FAILED && lbp->next_rec != 0) {
2537c478bd9Sstevel@tonic-gate 		(void) lseek(lbp->fd, lbp->next_rec, SEEK_SET);
2547c478bd9Sstevel@tonic-gate 		/* This is a special case of setting the last_record_offset */
2557c478bd9Sstevel@tonic-gate 		lbp->last_record_offset = lbp->next_rec;
2567c478bd9Sstevel@tonic-gate 	} else {
2577c478bd9Sstevel@tonic-gate 		lbp->last_record_offset = lbp->next_rec - lbp->mmap_addr;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	return (*error = 0);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * Free the nfslog buffer and its associated allocations
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static void
nfslog_free_buf(struct nfslog_buf * lbp,int close_quick)2677c478bd9Sstevel@tonic-gate nfslog_free_buf(struct nfslog_buf *lbp, int close_quick)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	XDR	xdrs;
2707c478bd9Sstevel@tonic-gate 	int	error;
2717c478bd9Sstevel@tonic-gate 	caddr_t buffer;
2727c478bd9Sstevel@tonic-gate 	struct nfslog_lr *lrp, *lrp_next;
2737c478bd9Sstevel@tonic-gate 	struct processed_records *prp, *tprp;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/* work to free the offset records and rewrite header */
2767c478bd9Sstevel@tonic-gate 	if (lbp->prp) {
2777c478bd9Sstevel@tonic-gate 		if (lbp->last_record_offset == lbp->prp->start_offset) {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			/* adjust the offset for the entire buffer */
2807c478bd9Sstevel@tonic-gate 			lbp->last_record_offset =
2817c478bd9Sstevel@tonic-gate 				lbp->prp->start_offset + lbp->prp->len;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			nfslog_rewrite_bufheader(lbp);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 		if (close_quick)
2867c478bd9Sstevel@tonic-gate 			return;
2877c478bd9Sstevel@tonic-gate 		prp = lbp->prp;
2887c478bd9Sstevel@tonic-gate 		do {
2897c478bd9Sstevel@tonic-gate 			tprp = prp->next;
2907c478bd9Sstevel@tonic-gate 			free(prp);
2917c478bd9Sstevel@tonic-gate 			prp = tprp;
2927c478bd9Sstevel@tonic-gate 		} while (lbp->prp != prp);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (close_quick)
2967c478bd9Sstevel@tonic-gate 		return;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/* Take care of the queue log records first */
2997c478bd9Sstevel@tonic-gate 	if (lbp->lrps != NULL) {
3007c478bd9Sstevel@tonic-gate 		lrp = lbp->lrps;
3017c478bd9Sstevel@tonic-gate 		do {
3027c478bd9Sstevel@tonic-gate 			lrp_next = lrp->next;
3037c478bd9Sstevel@tonic-gate 			nfslog_free_logrecord(lrp, FALSE);
3047c478bd9Sstevel@tonic-gate 			lrp = lrp_next;
3057c478bd9Sstevel@tonic-gate 		} while (lrp != lbp->lrps);
3067c478bd9Sstevel@tonic-gate 		lbp->lrps = NULL;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/* The buffer header was decoded and needs to be freed */
3107c478bd9Sstevel@tonic-gate 	if (lbp->bh.bh_length != 0) {
3117c478bd9Sstevel@tonic-gate 		buffer = (lbp->bh_lrp->buffer != NULL ?
3127c478bd9Sstevel@tonic-gate 			lbp->bh_lrp->buffer : (caddr_t)lbp->mmap_addr);
3137c478bd9Sstevel@tonic-gate 		xdrmem_create(&xdrs, buffer, lbp->bh_lrp->recsize, XDR_FREE);
3147c478bd9Sstevel@tonic-gate 		(void) xdr_nfslog_buffer_header(&xdrs, &lbp->bh);
3157c478bd9Sstevel@tonic-gate 		lbp->bh.bh_length = 0;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* get rid of the bufheader lrp */
3197c478bd9Sstevel@tonic-gate 	if (lbp->bh_lrp != NULL) {
3207c478bd9Sstevel@tonic-gate 		free_lrp(lbp->bh_lrp);
3217c478bd9Sstevel@tonic-gate 		lbp->bh_lrp = NULL;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/* Clean up for mmap() usage */
3257c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr != (intptr_t)MAP_FAILED) {
3267c478bd9Sstevel@tonic-gate 		if (munmap((void *)lbp->mmap_addr, lbp->filesize)) {
3277c478bd9Sstevel@tonic-gate 			error = errno;
3287c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("munmap failed: %s: %s"),
3297c478bd9Sstevel@tonic-gate 				(lbp->bufpath != NULL ? lbp->bufpath : ""),
3307c478bd9Sstevel@tonic-gate 				strerror(error));
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 		lbp->mmap_addr = (intptr_t)MAP_FAILED;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* Finally close the buffer file */
3367c478bd9Sstevel@tonic-gate 	if (lbp->fd >= 0) {
3377c478bd9Sstevel@tonic-gate 		lbp->fl.l_type = F_UNLCK;
3387c478bd9Sstevel@tonic-gate 		if (fcntl(lbp->fd, F_SETLK, &lbp->fl) == -1) {
3397c478bd9Sstevel@tonic-gate 			error = errno;
3407c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
3417c478bd9Sstevel@tonic-gate 				gettext("Cannot unlock file %s: %s"),
3427c478bd9Sstevel@tonic-gate 				(lbp->bufpath != NULL ? lbp->bufpath : ""),
3437c478bd9Sstevel@tonic-gate 				strerror(error));
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		(void) close(lbp->fd);
3467c478bd9Sstevel@tonic-gate 		lbp->fd = -1;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	if (lbp->bufpath != NULL)
3497c478bd9Sstevel@tonic-gate 		free(lbp->bufpath);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * We are reading a record from the log buffer file.  Since we are reading
3547c478bd9Sstevel@tonic-gate  * an XDR stream, we first have to read the first integer to determine
3557c478bd9Sstevel@tonic-gate  * how much to read in whole for this record.  Our preference is to use
3567c478bd9Sstevel@tonic-gate  * mmap() but if failed initially we will be using read().  Need to be
3577c478bd9Sstevel@tonic-gate  * careful about proper initialization of the log record both from a field
3587c478bd9Sstevel@tonic-gate  * perspective and for XDR decoding.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate static struct nfslog_lr *
nfslog_read_buffer(struct nfslog_buf * lbp)3617c478bd9Sstevel@tonic-gate nfslog_read_buffer(struct nfslog_buf *lbp)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	XDR xdrs;
3647c478bd9Sstevel@tonic-gate 	unsigned int	record_size;
3657c478bd9Sstevel@tonic-gate 	struct nfslog_lr *lrp;
3667c478bd9Sstevel@tonic-gate 	char		*sizebuf, tbuf[16];
3677c478bd9Sstevel@tonic-gate 	caddr_t		buffer;
3687c478bd9Sstevel@tonic-gate 	offset_t	next_rec;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	lrp = (struct nfslog_lr *)malloc(sizeof (*lrp));
3717c478bd9Sstevel@tonic-gate 	bzero(lrp, sizeof (*lrp));
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/* Check to see if mmap worked */
3747c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr == (intptr_t)MAP_FAILED) {
3757c478bd9Sstevel@tonic-gate 		/*
3767c478bd9Sstevel@tonic-gate 		 * EOF or other failure; we don't try to recover, just return
3777c478bd9Sstevel@tonic-gate 		 */
3787c478bd9Sstevel@tonic-gate 		if (read(lbp->fd, tbuf, BYTES_PER_XDR_UNIT) <= 0) {
3797c478bd9Sstevel@tonic-gate 			free_lrp(lrp);
3807c478bd9Sstevel@tonic-gate 			return (NULL);
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		sizebuf = tbuf;
3837c478bd9Sstevel@tonic-gate 	} else {
3847c478bd9Sstevel@tonic-gate 		/* EOF check for the mmap() case */
3857c478bd9Sstevel@tonic-gate 		if (lbp->filesize <= lbp->next_rec - lbp->mmap_addr) {
3867c478bd9Sstevel@tonic-gate 			free_lrp(lrp);
3877c478bd9Sstevel@tonic-gate 			return (NULL);
3887c478bd9Sstevel@tonic-gate 		}
389*11606941Sjwahlig 		sizebuf = (char *)(uintptr_t)lbp->next_rec;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* We have to XDR the first int so we know how much is in this record */
3937c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, sizebuf, sizeof (unsigned int), XDR_DECODE);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (!xdr_u_int(&xdrs, &record_size)) {
3967c478bd9Sstevel@tonic-gate 		free_lrp(lrp);
3977c478bd9Sstevel@tonic-gate 		return (NULL);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	lrp->recsize = record_size;
4017c478bd9Sstevel@tonic-gate 	next_rec = lbp->next_rec + lrp->recsize;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr == (intptr_t)MAP_FAILED) {
4047c478bd9Sstevel@tonic-gate 		/*
4057c478bd9Sstevel@tonic-gate 		 * Read() case - shouldn't be used very much.
4067c478bd9Sstevel@tonic-gate 		 * Note: The 'buffer' field is used later on
4077c478bd9Sstevel@tonic-gate 		 * to determine which method is being used mmap()|read()
4087c478bd9Sstevel@tonic-gate 		 */
4097c478bd9Sstevel@tonic-gate 		if (lbp->filesize < next_rec) {
4107c478bd9Sstevel@tonic-gate 			/* partial record from buffer */
4117c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
4127c478bd9Sstevel@tonic-gate 				"Last partial record in work buffer %s "
4137c478bd9Sstevel@tonic-gate 				"discarded\n"), lbp->bufpath);
4147c478bd9Sstevel@tonic-gate 			free_lrp(lrp);
4157c478bd9Sstevel@tonic-gate 			return (NULL);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		if ((lrp->buffer = malloc(lrp->recsize)) == NULL) {
4197c478bd9Sstevel@tonic-gate 			free_lrp(lrp);
4207c478bd9Sstevel@tonic-gate 			return (NULL);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		bcopy(sizebuf, lrp->buffer, BYTES_PER_XDR_UNIT);
4237c478bd9Sstevel@tonic-gate 		if (read(lbp->fd, &lrp->buffer[BYTES_PER_XDR_UNIT],
4247c478bd9Sstevel@tonic-gate 			lrp->recsize - BYTES_PER_XDR_UNIT) <= 0) {
4257c478bd9Sstevel@tonic-gate 			free_lrp(lrp);
4267c478bd9Sstevel@tonic-gate 			return (NULL);
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	} else if (lbp->filesize < next_rec - lbp->mmap_addr) {
4297c478bd9Sstevel@tonic-gate 			/* partial record from buffer */
4307c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
4317c478bd9Sstevel@tonic-gate 				"Last partial record in work buffer %s "
4327c478bd9Sstevel@tonic-gate 				"discarded\n"), lbp->bufpath);
4337c478bd9Sstevel@tonic-gate 			free_lrp(lrp);
4347c478bd9Sstevel@tonic-gate 			return (NULL);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/* other initializations */
4397c478bd9Sstevel@tonic-gate 	lrp->next = lrp->prev = lrp;
4407c478bd9Sstevel@tonic-gate 	/* Keep track of the offset at which this record was read */
4417c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr == (intptr_t)MAP_FAILED)
4427c478bd9Sstevel@tonic-gate 		lrp->f_offset = lbp->next_rec;
4437c478bd9Sstevel@tonic-gate 	else
4447c478bd9Sstevel@tonic-gate 		lrp->f_offset = lbp->next_rec - lbp->mmap_addr;
4457c478bd9Sstevel@tonic-gate 	/* This is the true address of the record */
4467c478bd9Sstevel@tonic-gate 	lrp->record = lbp->next_rec;
4477c478bd9Sstevel@tonic-gate 	lrp->xdrargs = lrp->xdrres = NULL;
4487c478bd9Sstevel@tonic-gate 	lrp->lbp = lbp;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* Here is the logic for mmap() vs. read() */
4517c478bd9Sstevel@tonic-gate 	buffer = (lrp->buffer != NULL ? lrp->buffer : (caddr_t)lrp->record);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* Setup for the 'real' XDR decode of the entire record */
4547c478bd9Sstevel@tonic-gate 	xdrmem_create(&lrp->xdrs, buffer, lrp->recsize, XDR_DECODE);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/* calculate the offset for the next record */
4577c478bd9Sstevel@tonic-gate 	lbp->next_rec = next_rec;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	return (lrp);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Simple removal of the log record from the log buffer queue.
4647c478bd9Sstevel@tonic-gate  * Make sure to manage the count of records queued.
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate static struct nfslog_lr *
remove_lrp_from_lb(struct nfslog_buf * lbp,struct nfslog_lr * lrp)4677c478bd9Sstevel@tonic-gate remove_lrp_from_lb(struct nfslog_buf *lbp, struct nfslog_lr *lrp)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	if (lbp->lrps == lrp) {
4707c478bd9Sstevel@tonic-gate 		if (lbp->lrps == lbp->lrps->next) {
4717c478bd9Sstevel@tonic-gate 			lbp->lrps = NULL;
4727c478bd9Sstevel@tonic-gate 		} else {
4737c478bd9Sstevel@tonic-gate 			lbp->lrps = lrp->next;
4747c478bd9Sstevel@tonic-gate 			remque(lrp);
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 	} else {
4777c478bd9Sstevel@tonic-gate 		remque(lrp);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	lbp->num_lrps--;
4807c478bd9Sstevel@tonic-gate 	return (lrp);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Insert a log record struct on the log buffer struct.  The log buffer
4857c478bd9Sstevel@tonic-gate  * has a pointer to the head of a queue of log records that have been
4867c478bd9Sstevel@tonic-gate  * read from the buffer file but have not been processed yet because
4877c478bd9Sstevel@tonic-gate  * the record id did not match the sequence desired for processing.
4887c478bd9Sstevel@tonic-gate  * The insertion must be in the 'correct'/sorted order which adds
4897c478bd9Sstevel@tonic-gate  * to the complexity of this function.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate static void
insert_lrp_to_lb(struct nfslog_buf * lbp,struct nfslog_lr * lrp)4927c478bd9Sstevel@tonic-gate insert_lrp_to_lb(struct nfslog_buf *lbp, struct nfslog_lr *lrp)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	int ins_rec_id = lrp->log_record.re_header.rh_rec_id;
4957c478bd9Sstevel@tonic-gate 	struct nfslog_lr *curlrp;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (lbp->lrps == NULL) {
4987c478bd9Sstevel@tonic-gate 		/* that was easy */
4997c478bd9Sstevel@tonic-gate 		lbp->lrps = lrp;
5007c478bd9Sstevel@tonic-gate 	} else {
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * Does this lrp go before the first on the list?
5037c478bd9Sstevel@tonic-gate 		 * If so, do the insertion by hand since insque is not
5047c478bd9Sstevel@tonic-gate 		 * as flexible when queueing an element to the head of
5057c478bd9Sstevel@tonic-gate 		 * a list.
5067c478bd9Sstevel@tonic-gate 		 */
5077c478bd9Sstevel@tonic-gate 		if (ins_rec_id < lbp->lrps->log_record.re_header.rh_rec_id) {
5087c478bd9Sstevel@tonic-gate 			lrp->next = lbp->lrps;
5097c478bd9Sstevel@tonic-gate 			lrp->prev = lbp->lrps->prev;
5107c478bd9Sstevel@tonic-gate 			lbp->lrps->prev->next = lrp;
5117c478bd9Sstevel@tonic-gate 			lbp->lrps->prev = lrp;
5127c478bd9Sstevel@tonic-gate 			lbp->lrps = lrp;
5137c478bd9Sstevel@tonic-gate 		} else {
5147c478bd9Sstevel@tonic-gate 			/*
5157c478bd9Sstevel@tonic-gate 			 * Search the queue for the correct insertion point.
5167c478bd9Sstevel@tonic-gate 			 * Be careful about the insque so that the record
5177c478bd9Sstevel@tonic-gate 			 * ends up in the right place.
5187c478bd9Sstevel@tonic-gate 			 */
5197c478bd9Sstevel@tonic-gate 			curlrp = lbp->lrps;
5207c478bd9Sstevel@tonic-gate 			do {
5217c478bd9Sstevel@tonic-gate 				if (ins_rec_id <
5227c478bd9Sstevel@tonic-gate 				curlrp->next->log_record.re_header.rh_rec_id)
5237c478bd9Sstevel@tonic-gate 					break;
5247c478bd9Sstevel@tonic-gate 				curlrp = curlrp->next;
5257c478bd9Sstevel@tonic-gate 			} while (curlrp != lbp->lrps);
5267c478bd9Sstevel@tonic-gate 			if (curlrp == lbp->lrps)
5277c478bd9Sstevel@tonic-gate 				insque(lrp, lbp->lrps->prev);
5287c478bd9Sstevel@tonic-gate 			else
5297c478bd9Sstevel@tonic-gate 				insque(lrp, curlrp);
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	/* always keep track of how many we have */
5337c478bd9Sstevel@tonic-gate 	lbp->num_lrps++;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
5377c478bd9Sstevel@tonic-gate  * We are rewriting the buffer header at the start of the log buffer
5387c478bd9Sstevel@tonic-gate  * for the sole purpose of resetting the bh_offset field.  This is
5397c478bd9Sstevel@tonic-gate  * supposed to represent the progress that the nfslogd daemon has made
5407c478bd9Sstevel@tonic-gate  * in its processing of the log buffer file.
5417c478bd9Sstevel@tonic-gate  * 'lbp->last_record_offset' contains the absolute offset of the end
5427c478bd9Sstevel@tonic-gate  * of the last element processed. The on-disk buffer offset is relative
5437c478bd9Sstevel@tonic-gate  * to the buffer header, therefore we subtract the length of the buffer
5447c478bd9Sstevel@tonic-gate  * header from the absolute offset.
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate static void
nfslog_rewrite_bufheader(struct nfslog_buf * lbp)5477c478bd9Sstevel@tonic-gate nfslog_rewrite_bufheader(struct nfslog_buf *lbp)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	XDR xdrs;
5507c478bd9Sstevel@tonic-gate 	nfslog_buffer_header bh;
5517c478bd9Sstevel@tonic-gate 	/* size big enough for buffer header encode */
5527c478bd9Sstevel@tonic-gate #define	XBUFSIZE 128
5537c478bd9Sstevel@tonic-gate 	char buffer[XBUFSIZE];
5547c478bd9Sstevel@tonic-gate 	unsigned int wsize;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/*
5577c478bd9Sstevel@tonic-gate 	 * if version 1 buffer is large and the current offset cannot be
5587c478bd9Sstevel@tonic-gate 	 * represented, then don't update the offset in the buffer.
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	if (lbp->bh.bh_flags & NFSLOG_BH_OFFSET_OVERFLOW) {
5617c478bd9Sstevel@tonic-gate 		/* No need to update the header - offset too big */
5627c478bd9Sstevel@tonic-gate 		return;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * build the buffer header from the original that was saved
5667c478bd9Sstevel@tonic-gate 	 * on initialization; note that the offset is taken from the
5677c478bd9Sstevel@tonic-gate 	 * last record processed (the last offset that represents
5687c478bd9Sstevel@tonic-gate 	 * all records processed without any holes in the processing)
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	bh = lbp->bh;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * if version 1 buffer is large and the current offset cannot be
5747c478bd9Sstevel@tonic-gate 	 * represented in 32 bits, then save only the last valid offset
5757c478bd9Sstevel@tonic-gate 	 * in the buffer and mark the flags to indicate that.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	if ((bh.bh_version > 1) ||
5787c478bd9Sstevel@tonic-gate 		(lbp->last_record_offset - bh.bh_length < UINT32_MAX)) {
5797c478bd9Sstevel@tonic-gate 		bh.bh_offset = lbp->last_record_offset - bh.bh_length;
5807c478bd9Sstevel@tonic-gate 	} else {
5817c478bd9Sstevel@tonic-gate 		/* don't update the offset in the buffer */
5827c478bd9Sstevel@tonic-gate 		bh.bh_flags |= NFSLOG_BH_OFFSET_OVERFLOW;
5837c478bd9Sstevel@tonic-gate 		lbp->bh.bh_flags = bh.bh_flags;
5847c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
5857c478bd9Sstevel@tonic-gate 			"nfslog_rewrite_bufheader: %s: offset does not fit "
5867c478bd9Sstevel@tonic-gate 			"in a 32 bit field\n"), lbp->bufpath);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, buffer, XBUFSIZE, XDR_ENCODE);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (!xdr_nfslog_buffer_header(&xdrs, &bh)) {
5927c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
5937c478bd9Sstevel@tonic-gate 			"error in re-writing buffer file %s header\n"),
5947c478bd9Sstevel@tonic-gate 			lbp->bufpath);
5957c478bd9Sstevel@tonic-gate 		return;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	wsize = xdr_getpos(&xdrs);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (lbp->mmap_addr == (intptr_t)MAP_FAILED) {
6017c478bd9Sstevel@tonic-gate 		/* go to the beginning of the file */
6027c478bd9Sstevel@tonic-gate 		(void) lseek(lbp->fd, 0, SEEK_SET);
6037c478bd9Sstevel@tonic-gate 		(void) write(lbp->fd, buffer, wsize);
6047c478bd9Sstevel@tonic-gate 		(void) lseek(lbp->fd, lbp->next_rec, SEEK_SET);
6057c478bd9Sstevel@tonic-gate 		(void) fsync(lbp->fd);
6067c478bd9Sstevel@tonic-gate 	} else {
6077c478bd9Sstevel@tonic-gate 		bcopy(buffer, (void *)lbp->mmap_addr, wsize);
6087c478bd9Sstevel@tonic-gate 		(void) msync((void *)lbp->mmap_addr, wsize, MS_SYNC);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * With the provided lrp, we will take and 'insert' the range that the
6147c478bd9Sstevel@tonic-gate  * record covered in the buffer file into a list of processed ranges
6157c478bd9Sstevel@tonic-gate  * for the buffer file.  These ranges represent the records processed
6167c478bd9Sstevel@tonic-gate  * but not 'marked' in the buffer header as being processed.
6177c478bd9Sstevel@tonic-gate  * This insertion process is being done for two reasons.  The first is that
6187c478bd9Sstevel@tonic-gate  * we do not want to pay the performance penalty of re-writing the buffer header
6197c478bd9Sstevel@tonic-gate  * for each record that we process.  The second reason is that the records
6207c478bd9Sstevel@tonic-gate  * may be processed out of order because of the unique ids.  This will occur
6217c478bd9Sstevel@tonic-gate  * if the kernel has written the records to the buffer file out of order.
6227c478bd9Sstevel@tonic-gate  * The read routine will 'sort' them as the records are read.
6237c478bd9Sstevel@tonic-gate  *
6247c478bd9Sstevel@tonic-gate  * We do not want to re-write the buffer header such that a record is
6257c478bd9Sstevel@tonic-gate  * represented and being processed when it has not been.  In the case
6267c478bd9Sstevel@tonic-gate  * that the nfslogd daemon restarts processing and the buffer header
6277c478bd9Sstevel@tonic-gate  * has been re-written improperly, some records could be skipped.
6287c478bd9Sstevel@tonic-gate  * We will be taking the conservative approach and only writing buffer
6297c478bd9Sstevel@tonic-gate  * header offsets when the entire offset range has been processed.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate static void
nfslog_ins_last_rec_processed(struct nfslog_lr * lrp)6327c478bd9Sstevel@tonic-gate nfslog_ins_last_rec_processed(struct nfslog_lr *lrp)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	struct processed_records *prp, *tp;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* init the data struct as if it were the only one */
6377c478bd9Sstevel@tonic-gate 	prp = malloc(sizeof (*prp));
6387c478bd9Sstevel@tonic-gate 	prp->next = prp->prev = prp;
6397c478bd9Sstevel@tonic-gate 	prp->start_offset = lrp->f_offset;
6407c478bd9Sstevel@tonic-gate 	prp->len = lrp->recsize;
6417c478bd9Sstevel@tonic-gate 	prp->num_recs = 1;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/* always add since we know we are going to insert */
6447c478bd9Sstevel@tonic-gate 	lrp->lbp->num_pr_queued++;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	/* Is this the first one?  If so, take the easy way out */
6477c478bd9Sstevel@tonic-gate 	if (lrp->lbp->prp == NULL) {
6487c478bd9Sstevel@tonic-gate 		lrp->lbp->prp = prp;
6497c478bd9Sstevel@tonic-gate 	} else {
6507c478bd9Sstevel@tonic-gate 		/* sort on insertion... */
6517c478bd9Sstevel@tonic-gate 		tp = lrp->lbp->prp;
6527c478bd9Sstevel@tonic-gate 		do {
6537c478bd9Sstevel@tonic-gate 			if (prp->start_offset < tp->start_offset)
6547c478bd9Sstevel@tonic-gate 				break;
6557c478bd9Sstevel@tonic-gate 			tp = tp->next;
6567c478bd9Sstevel@tonic-gate 		} while (tp != lrp->lbp->prp);
6577c478bd9Sstevel@tonic-gate 		/* insert where appropriate (before the one we found */
6587c478bd9Sstevel@tonic-gate 		insque(prp, tp->prev);
6597c478bd9Sstevel@tonic-gate 		/*
6607c478bd9Sstevel@tonic-gate 		 * special case where the insertion was done at the
6617c478bd9Sstevel@tonic-gate 		 * head of the list
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (tp == lrp->lbp->prp && prp->start_offset < tp->start_offset)
6647c478bd9Sstevel@tonic-gate 			lrp->lbp->prp = prp;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		/*
6677c478bd9Sstevel@tonic-gate 		 * now that the entry is in place, we need to see if it can
6687c478bd9Sstevel@tonic-gate 		 * be combined with the previous or following entries.
6697c478bd9Sstevel@tonic-gate 		 * combination is done by adding to the length.
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		if (prp->start_offset ==
6727c478bd9Sstevel@tonic-gate 			(prp->prev->start_offset + prp->prev->len)) {
6737c478bd9Sstevel@tonic-gate 			tp = prp->prev;
6747c478bd9Sstevel@tonic-gate 			remque(prp);
6757c478bd9Sstevel@tonic-gate 			tp->len += prp->len;
6767c478bd9Sstevel@tonic-gate 			tp->num_recs += prp->num_recs;
6777c478bd9Sstevel@tonic-gate 			free(prp);
6787c478bd9Sstevel@tonic-gate 			prp = tp;
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 		if (prp->next->start_offset ==
6817c478bd9Sstevel@tonic-gate 			(prp->start_offset + prp->len)) {
6827c478bd9Sstevel@tonic-gate 			prp->len += prp->next->len;
6837c478bd9Sstevel@tonic-gate 			prp->num_recs += prp->next->num_recs;
6847c478bd9Sstevel@tonic-gate 			tp = prp->next;
6857c478bd9Sstevel@tonic-gate 			remque(tp);
6867c478bd9Sstevel@tonic-gate 			free(tp);
6877c478bd9Sstevel@tonic-gate 		}
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (lrp->lbp->num_pr_queued > MAX_RECS_TO_DELAY) {
6917c478bd9Sstevel@tonic-gate 		prp = lrp->lbp->prp;
6927c478bd9Sstevel@tonic-gate 		if (lrp->lbp->last_record_offset ==
6937c478bd9Sstevel@tonic-gate 			prp->start_offset) {
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 			/* adjust the offset for the entire buffer */
6967c478bd9Sstevel@tonic-gate 			lrp->lbp->last_record_offset =
6977c478bd9Sstevel@tonic-gate 				prp->start_offset + prp->len;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 			nfslog_rewrite_bufheader(lrp->lbp);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			tp = prp->next;
7027c478bd9Sstevel@tonic-gate 			if (tp != prp)
7037c478bd9Sstevel@tonic-gate 				remque(prp);
7047c478bd9Sstevel@tonic-gate 			else
7057c478bd9Sstevel@tonic-gate 				tp = NULL;
7067c478bd9Sstevel@tonic-gate 			lrp->lbp->prp = tp;
7077c478bd9Sstevel@tonic-gate 			lrp->lbp->num_pr_queued -= prp->num_recs;
7087c478bd9Sstevel@tonic-gate 			free(prp);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * nfslog_get_logrecord is responsible for retrieving the next log record
7157c478bd9Sstevel@tonic-gate  * from the buffer file. This would normally be very straightforward but there
7167c478bd9Sstevel@tonic-gate  * is the added complexity of attempting to order the requests coming out of
7177c478bd9Sstevel@tonic-gate  * the buffer file.  The fundamental problems is that the kernel nfs logging
7187c478bd9Sstevel@tonic-gate  * functionality does not guarantee that the records were written to the file
7197c478bd9Sstevel@tonic-gate  * in the order that the NFS server processed them.  This can cause a problem
7207c478bd9Sstevel@tonic-gate  * in the fh -> pathname mapping in the case were a lookup for a file comes
7217c478bd9Sstevel@tonic-gate  * later in the buffer file than other operations on the lookup's target.
7227c478bd9Sstevel@tonic-gate  * The fh mapping database will not have an entry and will therefore not
7237c478bd9Sstevel@tonic-gate  * be able to map the fh to a name.
7247c478bd9Sstevel@tonic-gate  *
7257c478bd9Sstevel@tonic-gate  * So to solve this problem, the kernel nfs logging code tags each record
7267c478bd9Sstevel@tonic-gate  * with a monotonically increasing id and is guaranteed to be allocated
7277c478bd9Sstevel@tonic-gate  * in the order that the requests were processed.  Realize however that
7287c478bd9Sstevel@tonic-gate  * this processing guarantee is essentially for one thread on one client.
7297c478bd9Sstevel@tonic-gate  * This id mechanism does not order all requests since it is only the
7307c478bd9Sstevel@tonic-gate  * single client/single thread case that is most concerning to us here.
7317c478bd9Sstevel@tonic-gate  *
7327c478bd9Sstevel@tonic-gate  * This function will do the 'sorting' of the requests as they are
7337c478bd9Sstevel@tonic-gate  * read from the buffer file.  The sorting needs to take into account
7347c478bd9Sstevel@tonic-gate  * that some ids may be missing (operations not logged but ids allocated)
7357c478bd9Sstevel@tonic-gate  * and that the id field will eventually wrap over MAXINT.
7367c478bd9Sstevel@tonic-gate  *
7377c478bd9Sstevel@tonic-gate  * Complexity to solve the fh -> pathname mapping issue.
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate struct nfslog_lr *
nfslog_get_logrecord(struct nfslog_buf * lbp)7407c478bd9Sstevel@tonic-gate nfslog_get_logrecord(struct nfslog_buf *lbp)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	/* figure out what the next should be if the world were perfect */
7437c478bd9Sstevel@tonic-gate 	unsigned int next_rec_id = lbp->last_rec_id + 1;
7447c478bd9Sstevel@tonic-gate 	struct nfslog_lr *lrp = NULL;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate 	 * First we check the queued records on the log buffer struct
7487c478bd9Sstevel@tonic-gate 	 * to see if the one we want is there.  The records are sorted
7497c478bd9Sstevel@tonic-gate 	 * on the record id during the insertions to the queue so that
7507c478bd9Sstevel@tonic-gate 	 * this check is easy.
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 	if (lbp->lrps != NULL) {
7537c478bd9Sstevel@tonic-gate 		/* Does the first record match ? */
7547c478bd9Sstevel@tonic-gate 		if (lbp->lrps->log_record.re_header.rh_rec_id == next_rec_id) {
7557c478bd9Sstevel@tonic-gate 			lrp = remove_lrp_from_lb(lbp, lbp->lrps);
7567c478bd9Sstevel@tonic-gate 			lbp->last_rec_id = lrp->log_record.re_header.rh_rec_id;
7577c478bd9Sstevel@tonic-gate 		} else {
7587c478bd9Sstevel@tonic-gate 			/*
7597c478bd9Sstevel@tonic-gate 			 * Here we are checking for wrap of the record id
7607c478bd9Sstevel@tonic-gate 			 * since it is an unsigned in.  The idea is that
7617c478bd9Sstevel@tonic-gate 			 * if there is a huge span between what we expect
7627c478bd9Sstevel@tonic-gate 			 * and what is queued then we need to flush/empty
7637c478bd9Sstevel@tonic-gate 			 * the queued records first.
7647c478bd9Sstevel@tonic-gate 			 */
7657c478bd9Sstevel@tonic-gate 			if (next_rec_id <
7667c478bd9Sstevel@tonic-gate 				lbp->lrps->log_record.re_header.rh_rec_id &&
7677c478bd9Sstevel@tonic-gate 				((lbp->lrps->log_record.re_header.rh_rec_id -
7687c478bd9Sstevel@tonic-gate 					next_rec_id) > (MAXINT / 2))) {
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 				lrp = remove_lrp_from_lb(lbp, lbp->lrps);
7717c478bd9Sstevel@tonic-gate 				lbp->last_rec_id =
7727c478bd9Sstevel@tonic-gate 					lrp->log_record.re_header.rh_rec_id;
7737c478bd9Sstevel@tonic-gate 			}
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * So the first queued record didn't match (or there were no queued
7787c478bd9Sstevel@tonic-gate 	 * records to look at).  Now we go to the buffer file looking for
7797c478bd9Sstevel@tonic-gate 	 * the expected log record based on its id.  We loop looking for
7807c478bd9Sstevel@tonic-gate 	 * a matching records and save/queue the records that don't match.
7817c478bd9Sstevel@tonic-gate 	 * Note that we will queue a maximum number to handle the case
7827c478bd9Sstevel@tonic-gate 	 * of a missing record id or a queue that is very confused.  We don't
7837c478bd9Sstevel@tonic-gate 	 * want to consume too much memory.
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	while (lrp == NULL) {
7867c478bd9Sstevel@tonic-gate 		/* Have we queued too many for this buffer? */
7877c478bd9Sstevel@tonic-gate 		if (lbp->num_lrps >= MAX_LRS_READ_AHEAD) {
7887c478bd9Sstevel@tonic-gate 			lrp = remove_lrp_from_lb(lbp, lbp->lrps);
7897c478bd9Sstevel@tonic-gate 			lbp->last_rec_id = lrp->log_record.re_header.rh_rec_id;
7907c478bd9Sstevel@tonic-gate 			break;
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 		/*
7937c478bd9Sstevel@tonic-gate 		 * Get a record from the buffer file.  If none are available,
7947c478bd9Sstevel@tonic-gate 		 * this is probably and EOF condition (could be a read error
7957c478bd9Sstevel@tonic-gate 		 * as well but that is masked. :-().  No records in the
7967c478bd9Sstevel@tonic-gate 		 * file means that we need to pull any queued records
7977c478bd9Sstevel@tonic-gate 		 * so that we don't miss any in the processing.
7987c478bd9Sstevel@tonic-gate 		 */
7997c478bd9Sstevel@tonic-gate 		if ((lrp = nfslog_read_buffer(lbp)) == NULL) {
8007c478bd9Sstevel@tonic-gate 			if (lbp->lrps != NULL) {
8017c478bd9Sstevel@tonic-gate 				lrp = remove_lrp_from_lb(lbp, lbp->lrps);
8027c478bd9Sstevel@tonic-gate 				lbp->last_rec_id =
8037c478bd9Sstevel@tonic-gate 					lrp->log_record.re_header.rh_rec_id;
8047c478bd9Sstevel@tonic-gate 			} else {
8057c478bd9Sstevel@tonic-gate 				return (NULL);  /* it was really and EOF */
8067c478bd9Sstevel@tonic-gate 			}
8077c478bd9Sstevel@tonic-gate 		} else {
8087c478bd9Sstevel@tonic-gate 			/*
8097c478bd9Sstevel@tonic-gate 			 * Just read a record from the buffer file and now we
8107c478bd9Sstevel@tonic-gate 			 * need to XDR the record header so that we can take
8117c478bd9Sstevel@tonic-gate 			 * a look at the record id.
8127c478bd9Sstevel@tonic-gate 			 */
8137c478bd9Sstevel@tonic-gate 			if (!xdr_nfslog_request_record(&lrp->xdrs,
8147c478bd9Sstevel@tonic-gate 				&lrp->log_record)) {
8157c478bd9Sstevel@tonic-gate 				/* Free and return EOF/NULL on error */
8167c478bd9Sstevel@tonic-gate 				nfslog_free_logrecord(lrp, FALSE);
8177c478bd9Sstevel@tonic-gate 				return (NULL);
8187c478bd9Sstevel@tonic-gate 			}
8197c478bd9Sstevel@tonic-gate 			/*
8207c478bd9Sstevel@tonic-gate 			 * If the new record is less than or matches the
8217c478bd9Sstevel@tonic-gate 			 * expected record id, then we return this record
8227c478bd9Sstevel@tonic-gate 			 */
8237c478bd9Sstevel@tonic-gate 			if (lrp->log_record.re_header.rh_rec_id <=
8247c478bd9Sstevel@tonic-gate 				next_rec_id) {
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 				lbp->last_rec_id =
8277c478bd9Sstevel@tonic-gate 					lrp->log_record.re_header.rh_rec_id;
8287c478bd9Sstevel@tonic-gate 			} else {
8297c478bd9Sstevel@tonic-gate 				/*
8307c478bd9Sstevel@tonic-gate 				 * This is not the one we were looking
8317c478bd9Sstevel@tonic-gate 				 * for; queue it for later processing
8327c478bd9Sstevel@tonic-gate 				 * (queueing sorts on record id)
8337c478bd9Sstevel@tonic-gate 				 */
8347c478bd9Sstevel@tonic-gate 				insert_lrp_to_lb(lbp, lrp);
8357c478bd9Sstevel@tonic-gate 				lrp = NULL;
8367c478bd9Sstevel@tonic-gate 			}
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	return (lrp);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Free the log record provided.
8447c478bd9Sstevel@tonic-gate  * This is complex because the associated XDR streams also need to be freed
8457c478bd9Sstevel@tonic-gate  * since allocation could have occured during the DECODE phase.  The record
8467c478bd9Sstevel@tonic-gate  * header, args and results need to be XDR_FREEd.  The xdr funtions will
8477c478bd9Sstevel@tonic-gate  * be provided if a free needs to be done.
8487c478bd9Sstevel@tonic-gate  *
8497c478bd9Sstevel@tonic-gate  * Note that caller tells us if the record being freed was processed.
8507c478bd9Sstevel@tonic-gate  * If so, then the buffer header should be updated.  Updating the buffer
8517c478bd9Sstevel@tonic-gate  * header keeps track of where the nfslogd daemon left off in its processing
8527c478bd9Sstevel@tonic-gate  * if it is unable to complete the entire file.
8537c478bd9Sstevel@tonic-gate  */
8547c478bd9Sstevel@tonic-gate void
nfslog_free_logrecord(struct nfslog_lr * lrp,bool_t processing_complete)8557c478bd9Sstevel@tonic-gate nfslog_free_logrecord(struct nfslog_lr *lrp, bool_t processing_complete)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	caddr_t			buffer;
8587c478bd9Sstevel@tonic-gate 	nfslog_request_record 	*reqrec;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (processing_complete) {
8617c478bd9Sstevel@tonic-gate 		nfslog_ins_last_rec_processed(lrp);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	reqrec = &lrp->log_record;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	buffer = (lrp->buffer != NULL ? lrp->buffer : (caddr_t)lrp->record);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	xdrmem_create(&lrp->xdrs, buffer, lrp->recsize, XDR_FREE);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	(void) xdr_nfslog_request_record(&lrp->xdrs, reqrec);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	if (lrp->xdrargs != NULL && reqrec->re_rpc_arg)
8737c478bd9Sstevel@tonic-gate 		(*lrp->xdrargs)(&lrp->xdrs, reqrec->re_rpc_arg);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (reqrec->re_rpc_arg)
8767c478bd9Sstevel@tonic-gate 		free(reqrec->re_rpc_arg);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (lrp->xdrres != NULL && reqrec->re_rpc_res)
8797c478bd9Sstevel@tonic-gate 		(*lrp->xdrres)(&lrp->xdrs, reqrec->re_rpc_res);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	if (reqrec->re_rpc_res)
8827c478bd9Sstevel@tonic-gate 		free(reqrec->re_rpc_res);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	free_lrp(lrp);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate static void
free_lrp(struct nfslog_lr * lrp)8887c478bd9Sstevel@tonic-gate free_lrp(struct nfslog_lr *lrp)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	if (lrp->buffer != NULL)
8917c478bd9Sstevel@tonic-gate 		free(lrp->buffer);
8927c478bd9Sstevel@tonic-gate 	free(lrp);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate  * Utility function used elsewhere
8977c478bd9Sstevel@tonic-gate  */
8987c478bd9Sstevel@tonic-gate void
nfslog_opaque_print_buf(void * buf,int len,char * outbuf,int * outbufoffsetp,int maxoffset)8997c478bd9Sstevel@tonic-gate nfslog_opaque_print_buf(void *buf, int len, char *outbuf, int *outbufoffsetp,
9007c478bd9Sstevel@tonic-gate 	int maxoffset)
9017c478bd9Sstevel@tonic-gate {
9027c478bd9Sstevel@tonic-gate 	int	i, j;
9037c478bd9Sstevel@tonic-gate 	uint_t	*ip;
9047c478bd9Sstevel@tonic-gate 	uchar_t	*u_buf = (uchar_t *)buf;
9057c478bd9Sstevel@tonic-gate 	int	outbufoffset = *outbufoffsetp;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	outbufoffset += sprintf(&outbuf[outbufoffset], " \"");
9087c478bd9Sstevel@tonic-gate 	if (len <= sizeof (int)) {
9097c478bd9Sstevel@tonic-gate 		for (j = 0; (j < len) && (outbufoffset < maxoffset);
9107c478bd9Sstevel@tonic-gate 			j++, u_buf++)
9117c478bd9Sstevel@tonic-gate 			outbufoffset += sprintf(&outbuf[outbufoffset],
9127c478bd9Sstevel@tonic-gate 						"%02x", *u_buf);
9137c478bd9Sstevel@tonic-gate 		return;
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 	/* More than 4 bytes, print with spaces in integer offsets */
9167c478bd9Sstevel@tonic-gate 	j = (int)((uintptr_t)buf % sizeof (int));
9177c478bd9Sstevel@tonic-gate 	i = 0;
9187c478bd9Sstevel@tonic-gate 	if (j > 0) {
9197c478bd9Sstevel@tonic-gate 		i = sizeof (int) - j;
9207c478bd9Sstevel@tonic-gate 		for (; (j < sizeof (int)) && (outbufoffset < maxoffset);
9217c478bd9Sstevel@tonic-gate 			j++, u_buf++)
9227c478bd9Sstevel@tonic-gate 			outbufoffset += sprintf(&outbuf[outbufoffset],
9237c478bd9Sstevel@tonic-gate 						"%02x", *u_buf);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	/* LINTED */
9267c478bd9Sstevel@tonic-gate 	ip = (uint_t *)u_buf;
9277c478bd9Sstevel@tonic-gate 	for (; ((i + sizeof (int)) <= len) && (outbufoffset < maxoffset);
9287c478bd9Sstevel@tonic-gate 		i += sizeof (int), ip++) {
9297c478bd9Sstevel@tonic-gate 		outbufoffset += sprintf(&outbuf[outbufoffset], " %08x", *ip);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 	if (i < len) {
9327c478bd9Sstevel@tonic-gate 		/* Last element not int */
9337c478bd9Sstevel@tonic-gate 		u_buf = (uchar_t *)ip;
9347c478bd9Sstevel@tonic-gate 		if (i > j)	/* not first element */
9357c478bd9Sstevel@tonic-gate 			outbufoffset += sprintf(&outbuf[outbufoffset], " ");
9367c478bd9Sstevel@tonic-gate 		for (; (i < len) && (outbufoffset < maxoffset); i++, u_buf++) {
9377c478bd9Sstevel@tonic-gate 			outbufoffset += sprintf(&outbuf[outbufoffset],
9387c478bd9Sstevel@tonic-gate 						"%02x", *u_buf);
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 	if (outbufoffset < maxoffset)
9427c478bd9Sstevel@tonic-gate 		outbufoffset += sprintf(&outbuf[outbufoffset], "\"");
9437c478bd9Sstevel@tonic-gate 	*outbufoffsetp = outbufoffset;
9447c478bd9Sstevel@tonic-gate }
945