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