xref: /titanic_51/usr/src/uts/common/fs/sockfs/nl7clogd.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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
532cacfe4Sfr80241  * Common Development and Distribution License (the "License").
632cacfe4Sfr80241  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2232cacfe4Sfr80241  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <sys/callb.h>
307c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
317c478bd9Sstevel@tonic-gate #include <sys/filio.h>
327c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
337c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
347c478bd9Sstevel@tonic-gate #include <sys/promif.h>
357c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
367c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7curi.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <inet/nca/ncadoorhdr.h>
397c478bd9Sstevel@tonic-gate #include <inet/nca/ncalogd.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern boolean_t	nl7c_logd_enabled;
427c478bd9Sstevel@tonic-gate extern boolean_t	nl7c_logd_started;
437c478bd9Sstevel@tonic-gate extern boolean_t	nl7c_logd_cycle;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern void		nl7clogd_startup(void);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern boolean_t	nl7c_http_log(uri_desc_t *, uri_desc_t *,
487c478bd9Sstevel@tonic-gate 			    nca_request_log_t *, char **, char **, uint32_t *);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void		logit_flush(void *);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * NL7C reuses the NCA logging scheme, the directory "/var/nca" contains
547c478bd9Sstevel@tonic-gate  * the symlink "current" to 1 of up to 16 NCA BLF logging files, by default
557c478bd9Sstevel@tonic-gate  * a single logging file "log", optionally paths of up to 16 log files can
567c478bd9Sstevel@tonic-gate  * be specified via ncalogd.conf(4), note that these log files need not be
577c478bd9Sstevel@tonic-gate  * in the "/var/nca" directory.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * NL7C reuses the NCA logging APIs defined in <inet/nca/ncalogd.h>, at
60*da6c28aaSamw  * some future date (when NCA is deprecated or improvements are needed)
617c478bd9Sstevel@tonic-gate  * these need to be moved into NL7C.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * NL7C implements logging differently in 2 ways, 1st the initialization
647c478bd9Sstevel@tonic-gate  * is handled completely in the kernel by NL7C when it's enabled vs NCA
657c478bd9Sstevel@tonic-gate  * when the kmod was loaded, 2nd a simple worker thread with a FIFO queue
667c478bd9Sstevel@tonic-gate  * is used to process log_buf_t's instead of a squeue_t (this is done as
677c478bd9Sstevel@tonic-gate  * squeue_t's are private to NCA and IP at some future date we may us an
687c478bd9Sstevel@tonic-gate  * IP squeue_t):
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *	logd_t - used by various functions to manage a singly linked
717c478bd9Sstevel@tonic-gate  * 	grounded list of log_buf_t's and it's worker thread.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate typedef struct logd_s {
757c478bd9Sstevel@tonic-gate 	log_buf_t	*head;
767c478bd9Sstevel@tonic-gate 	log_buf_t	*tail;
777c478bd9Sstevel@tonic-gate 	kthread_t	*worker;
787c478bd9Sstevel@tonic-gate 	kcondvar_t	wait;
797c478bd9Sstevel@tonic-gate 	kmutex_t	lock;
807c478bd9Sstevel@tonic-gate } logd_t;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * In-kernel logging:
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *	nl7c_logbuf_max - tunable for the number of preallocated next
867c478bd9Sstevel@tonic-gate  *	log_buf_t(s) for use by log_buf_alloc(), note if the value is
877c478bd9Sstevel@tonic-gate  *	0 (the default) then max_cpus worth will be allocated.
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  *	logd - global logd_t used to post log_buf_t's too.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  *	log - global current log_buf_t that logit() logs too.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  *	logv[] - vector of available next logbuf(s) such that when
947c478bd9Sstevel@tonic-gate  *	logbuf is filled another can be used while being processed by
957c478bd9Sstevel@tonic-gate  *	the logger() and kmem_cache_alloc() of a replacement is done.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	logvcnt - count of logv[] vector element(s) and the index
987c478bd9Sstevel@tonic-gate  *	plus 1 of the next logbuf.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  *	log_buf_kmc - the kmem_cache to alloc/free log_buf_t's from/to.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  *	fio - the global nca_fio_t used to manage file i/o to a logfile.
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  *	dir - path to the directory where the current logfile symlink
1057c478bd9Sstevel@tonic-gate  *	is created and the default directory for logfile(s).
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  *	symlink - name of the logfile symlink.
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  *	symlink_path - path to the logfile symlink.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *	log_lock - the kmutex_t used to guarantee atomic access of
1127c478bd9Sstevel@tonic-gate  * 	all of the above.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  *	flush_tid - logit_flush() timeout id.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  *	LOGBUFV_ALLOC() - macro used to add log_buf_t(s) to logv[].
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate int			nl7c_logbuf_max = 0;
1207c478bd9Sstevel@tonic-gate static logd_t		logd;
1217c478bd9Sstevel@tonic-gate static log_buf_t	*log = NULL;
1227c478bd9Sstevel@tonic-gate static log_buf_t	**logv = NULL;
1237c478bd9Sstevel@tonic-gate static int		logvcnt = 0;
1247c478bd9Sstevel@tonic-gate static kmem_cache_t	*log_buf_kmc;
1257c478bd9Sstevel@tonic-gate static nca_fio_t	fio;
1267c478bd9Sstevel@tonic-gate static caddr_t		dir = "/var/nca/";
1277c478bd9Sstevel@tonic-gate static caddr_t		symlink = "current";
1287c478bd9Sstevel@tonic-gate static caddr_t		symlink_dir = "/var/nca";
1297c478bd9Sstevel@tonic-gate static caddr_t		symlink_path = "/var/nca/current";
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static kmutex_t		log_lock;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static timeout_id_t	flush_tid;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	LOGBUFV_ALLOC(kmflag) {						\
1367c478bd9Sstevel@tonic-gate 	log_buf_t	*_p;						\
1377c478bd9Sstevel@tonic-gate 									\
1387c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&log_lock));					\
1397c478bd9Sstevel@tonic-gate 	while (logvcnt < nl7c_logbuf_max) {				\
1407c478bd9Sstevel@tonic-gate 		/*CONSTCOND*/						\
1417c478bd9Sstevel@tonic-gate 		if (kmflag == KM_SLEEP)					\
1427c478bd9Sstevel@tonic-gate 			mutex_exit(&log_lock);				\
1437c478bd9Sstevel@tonic-gate 		_p = kmem_cache_alloc(log_buf_kmc, kmflag);		\
1447c478bd9Sstevel@tonic-gate 		/*CONSTCOND*/						\
1457c478bd9Sstevel@tonic-gate 		if (kmflag == KM_SLEEP) {				\
1467c478bd9Sstevel@tonic-gate 			mutex_enter(&log_lock);				\
1477c478bd9Sstevel@tonic-gate 			if (logvcnt == nl7c_logbuf_max) {		\
1487c478bd9Sstevel@tonic-gate 				mutex_exit(&log_lock);			\
1497c478bd9Sstevel@tonic-gate 				kmem_cache_free(log_buf_kmc, _p);	\
1507c478bd9Sstevel@tonic-gate 				mutex_enter(&log_lock);			\
1517c478bd9Sstevel@tonic-gate 				break;					\
1527c478bd9Sstevel@tonic-gate 			}						\
1537c478bd9Sstevel@tonic-gate 		} else {						\
1547c478bd9Sstevel@tonic-gate 			if (_p == NULL) {				\
1557c478bd9Sstevel@tonic-gate 				break;					\
1567c478bd9Sstevel@tonic-gate 			}						\
1577c478bd9Sstevel@tonic-gate 		}							\
1587c478bd9Sstevel@tonic-gate 		logv[logvcnt++] = _p;					\
1597c478bd9Sstevel@tonic-gate 	}								\
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Exports for inet/nca/ncaddi.c:
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate nca_fio_t		*nl7c_logd_fio = &fio;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static void
1697c478bd9Sstevel@tonic-gate log_buf_alloc(int kmflag)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	nca_log_buf_hdr_t	*hdr;
1727c478bd9Sstevel@tonic-gate 	static	ulong_t		seq = 0;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&log_lock));
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (logvcnt == 0) {
1777c478bd9Sstevel@tonic-gate 		/*
1787c478bd9Sstevel@tonic-gate 		 * No logv[] to use for the new log global logbuf,
1797c478bd9Sstevel@tonic-gate 		 * try to allocate one or more before giving up.
1807c478bd9Sstevel@tonic-gate 		 */
1817c478bd9Sstevel@tonic-gate 		LOGBUFV_ALLOC(kmflag);
1827c478bd9Sstevel@tonic-gate 		if (logvcnt == 0) {
1837c478bd9Sstevel@tonic-gate 			/* No joy, just give up. */
1847c478bd9Sstevel@tonic-gate 			log = NULL;
1857c478bd9Sstevel@tonic-gate 			return;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	log = logv[--logvcnt];
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	log->size = NCA_DEFAULT_LOG_BUF_SIZE;
1917c478bd9Sstevel@tonic-gate 	log->cur_pos = sizeof (*hdr);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	hdr = (nca_log_buf_hdr_t *)&log->buffer;
1947c478bd9Sstevel@tonic-gate 	hdr->nca_loghdr.nca_version = NCA_LOG_VERSION1;
1957c478bd9Sstevel@tonic-gate 	hdr->nca_loghdr.nca_op = log_op;
1967c478bd9Sstevel@tonic-gate 	hdr->nca_logstats.n_log_size = NCA_DEFAULT_LOG_BUF_SIZE - sizeof (*hdr);
1977c478bd9Sstevel@tonic-gate 	hdr->nca_logstats.n_log_recs = 0;
1987c478bd9Sstevel@tonic-gate 	hdr->nca_logstats.n_log_upcall = seq++;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* Try to allocate for at least the one we just used */
2017c478bd9Sstevel@tonic-gate 	LOGBUFV_ALLOC(kmflag);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static void
2057c478bd9Sstevel@tonic-gate logd_off()
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static void
2117c478bd9Sstevel@tonic-gate logd_log_write(kmutex_t *lock, log_buf_t *lbp)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	nca_log_buf_hdr_t *hdr = (nca_log_buf_hdr_t *)lbp->buffer;
2147c478bd9Sstevel@tonic-gate 	nca_log_stat_t	*sts = &hdr->nca_logstats;
2157c478bd9Sstevel@tonic-gate 	int		size = sts->n_log_size + sizeof (*hdr);
2167c478bd9Sstevel@tonic-gate 	vnode_t		*vp;
2177c478bd9Sstevel@tonic-gate 	uio_t		uio;
2187c478bd9Sstevel@tonic-gate 	iovec_t		iov;
2197c478bd9Sstevel@tonic-gate 	int		ret;
2207c478bd9Sstevel@tonic-gate 	boolean_t	noretry = B_FALSE;
2217c478bd9Sstevel@tonic-gate 	vattr_t		attr;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (size & (DEV_BSIZE - 1)) {
2247c478bd9Sstevel@tonic-gate 		/*
2257c478bd9Sstevel@tonic-gate 		 * Not appropriately sized for directio(),
2267c478bd9Sstevel@tonic-gate 		 * add some filler so it is.
2277c478bd9Sstevel@tonic-gate 		 */
2287c478bd9Sstevel@tonic-gate 		sts->n_log_size += DEV_BSIZE - (size & (DEV_BSIZE - 1));
2297c478bd9Sstevel@tonic-gate 		size = sts->n_log_size + sizeof (*hdr);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate retry:
2327c478bd9Sstevel@tonic-gate 	if (nca_fio_offset(&fio) + size <= nca_fio_size(&fio)) {
2337c478bd9Sstevel@tonic-gate 		/*
2347c478bd9Sstevel@tonic-gate 		 * Room in the current log file so write the logbuf out,
2357c478bd9Sstevel@tonic-gate 		 * exit the logd lock while doing the i/o as to not block
2367c478bd9Sstevel@tonic-gate 		 * queuing.
2377c478bd9Sstevel@tonic-gate 		 */
2387c478bd9Sstevel@tonic-gate 		mutex_exit(lock);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		vp = nca_fio_vp(&fio);
2417c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
2427c478bd9Sstevel@tonic-gate 		iov.iov_base = lbp->buffer;
2437c478bd9Sstevel@tonic-gate 		iov.iov_len = size;
2447c478bd9Sstevel@tonic-gate 		uio.uio_iov = &iov;
2457c478bd9Sstevel@tonic-gate 		uio.uio_iovcnt = 1;
2467c478bd9Sstevel@tonic-gate 		uio.uio_segflg = UIO_SYSSPACE;
2477c478bd9Sstevel@tonic-gate 		uio.uio_fmode = 0;
2487c478bd9Sstevel@tonic-gate 		uio.uio_loffset = (u_offset_t)nca_fio_offset(&fio);
2497c478bd9Sstevel@tonic-gate 		uio.uio_llimit = curproc->p_fsz_ctl;
2507c478bd9Sstevel@tonic-gate 		uio.uio_resid = size;
2517c478bd9Sstevel@tonic-gate 		ret = VOP_WRITE(vp, &uio, 0, kcred, NULL);
2527c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
2537c478bd9Sstevel@tonic-gate 		if (ret != 0) {
2547c478bd9Sstevel@tonic-gate 			if (ret == EFBIG) {
2557c478bd9Sstevel@tonic-gate 				/*
2567c478bd9Sstevel@tonic-gate 				 * Out of space for this file,
2577c478bd9Sstevel@tonic-gate 				 * retry with the next.
2587c478bd9Sstevel@tonic-gate 				 */
2597c478bd9Sstevel@tonic-gate 				nca_fio_size(&fio) = nca_fio_offset(&fio);
2607c478bd9Sstevel@tonic-gate 				if (noretry) {
2617c478bd9Sstevel@tonic-gate 					nl7c_logd_enabled = B_FALSE;
2627c478bd9Sstevel@tonic-gate 					goto done;
2637c478bd9Sstevel@tonic-gate 				} else
2647c478bd9Sstevel@tonic-gate 					goto next;
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		nca_fio_offset(&fio) = uio.uio_loffset;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		mutex_enter(lock);
2707c478bd9Sstevel@tonic-gate 		goto done;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Current logfile doesn't have sufficient space
2757c478bd9Sstevel@tonic-gate 	 * so move on to next file (if any).
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate next:
2787c478bd9Sstevel@tonic-gate 	mutex_exit(lock);
2797c478bd9Sstevel@tonic-gate 	/* Close current file */
2807c478bd9Sstevel@tonic-gate 	ret = VOP_CLOSE(nca_fio_vp(&fio), FCREAT|FWRITE|FAPPEND|FTRUNC,
281*da6c28aaSamw 			1, (offset_t)0, kcred, NULL);
2827c478bd9Sstevel@tonic-gate 	nca_fio_vp(&fio) = NULL;
2837c478bd9Sstevel@tonic-gate 	if (ret) {
2847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd: close of %s failed (error %d)",
2857c478bd9Sstevel@tonic-gate 		    nca_fio_name(&fio), ret);
2867c478bd9Sstevel@tonic-gate 		nl7c_logd_enabled = B_FALSE;
2877c478bd9Sstevel@tonic-gate 		logd_off();
2887c478bd9Sstevel@tonic-gate 		return;
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* Go to next file */
2927c478bd9Sstevel@tonic-gate 	nca_fio_ix(&fio)++;
2937c478bd9Sstevel@tonic-gate 	if (nca_fio_ix(&fio) == nca_fio_cnt(&fio)) {
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * We have reached the last file. If cycling
2967c478bd9Sstevel@tonic-gate 		 * is not on, disable logging and bailout.
2977c478bd9Sstevel@tonic-gate 		 */
2987c478bd9Sstevel@tonic-gate 		if (nl7c_logd_cycle) {
2997c478bd9Sstevel@tonic-gate 			/* Start from the first file */
3007c478bd9Sstevel@tonic-gate 			nca_fio_ix(&fio) = 0;
3017c478bd9Sstevel@tonic-gate 		} else {
3027c478bd9Sstevel@tonic-gate 			nca_fio_ix(&fio)--;
3037c478bd9Sstevel@tonic-gate 			nl7c_logd_enabled = B_FALSE;
3047c478bd9Sstevel@tonic-gate 			logd_off();
3057c478bd9Sstevel@tonic-gate 			return;
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/* Open the next log file */
3107c478bd9Sstevel@tonic-gate 	ret = vn_open(nca_fio_name(&fio), UIO_SYSSPACE, FCREAT|FWRITE|FTRUNC,
3117c478bd9Sstevel@tonic-gate 			0600, &nca_fio_vp(&fio), 0, 0);
3127c478bd9Sstevel@tonic-gate 	if (ret) {
3137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd: vn_open of %s failed (error %d)",
3147c478bd9Sstevel@tonic-gate 			nca_fio_name(&fio), ret);
3157c478bd9Sstevel@tonic-gate 		nl7c_logd_enabled = B_FALSE;
3167c478bd9Sstevel@tonic-gate 		logd_off();
3177c478bd9Sstevel@tonic-gate 		return;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* Turn on directio */
3217c478bd9Sstevel@tonic-gate 	(void) VOP_IOCTL(nca_fio_vp(&fio), _FIODIRECTIO,
322*da6c28aaSamw 			DIRECTIO_ON, 0, kcred, NULL, NULL);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/* Start writing from the begining of the file */
3257c478bd9Sstevel@tonic-gate 	nca_fio_offset(&fio) = 0;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/* Remove the current symlink */
328*da6c28aaSamw 	(void) VOP_REMOVE(nca_fio_dvp(&fio), symlink, kcred, NULL, 0);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	attr.va_mask = AT_MODE | AT_TYPE;
3317c478bd9Sstevel@tonic-gate 	attr.va_mode = 0777;
3327c478bd9Sstevel@tonic-gate 	attr.va_type = VLNK;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* Create symlink to the new log file */
3357c478bd9Sstevel@tonic-gate 	ret = VOP_SYMLINK(nca_fio_dvp(&fio), symlink,
336*da6c28aaSamw 			&attr, nca_fio_name(&fio), kcred, NULL, 0);
3377c478bd9Sstevel@tonic-gate 	if (ret) {
3387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd: symlink of %s to %s failed",
3397c478bd9Sstevel@tonic-gate 			symlink, nca_fio_name(&fio));
3407c478bd9Sstevel@tonic-gate 		nl7c_logd_enabled = B_FALSE;
3417c478bd9Sstevel@tonic-gate 		logd_off();
3427c478bd9Sstevel@tonic-gate 		return;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	mutex_enter(lock);
3457c478bd9Sstevel@tonic-gate 	goto retry;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate done:
3487c478bd9Sstevel@tonic-gate 	if (logvcnt < nl7c_logbuf_max) {
3497c478bd9Sstevel@tonic-gate 		/* May need to allocate some logbuf(s) for logv[] */
3507c478bd9Sstevel@tonic-gate 		mutex_enter(&log_lock);
3517c478bd9Sstevel@tonic-gate 		if (logvcnt < nl7c_logbuf_max) {
3527c478bd9Sstevel@tonic-gate 			/*
3537c478bd9Sstevel@tonic-gate 			 * After acquiring the lock still need logbuf(s),
3547c478bd9Sstevel@tonic-gate 			 * if the global logbuf pointer is NULL then call
3557c478bd9Sstevel@tonic-gate 			 * log_buf_alloc() as it will fill up logbugv[]
3567c478bd9Sstevel@tonic-gate 			 * and initialize a new logbuf else fill up just
3577c478bd9Sstevel@tonic-gate 			 * the logv[] here.
3587c478bd9Sstevel@tonic-gate 			 */
3597c478bd9Sstevel@tonic-gate 			if (log == NULL) {
3607c478bd9Sstevel@tonic-gate 				log_buf_alloc(KM_SLEEP);
3617c478bd9Sstevel@tonic-gate 			} else {
3627c478bd9Sstevel@tonic-gate 				/*LINTED*/
3637c478bd9Sstevel@tonic-gate 				LOGBUFV_ALLOC(KM_SLEEP);
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		mutex_exit(&log_lock);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static void
3717c478bd9Sstevel@tonic-gate logd_worker(logd_t *logdp)
3727c478bd9Sstevel@tonic-gate {
3737c478bd9Sstevel@tonic-gate 	log_buf_t	*lbp;
3747c478bd9Sstevel@tonic-gate 	kmutex_t	*lock = &logdp->lock;
3757c478bd9Sstevel@tonic-gate 	kcondvar_t	*wait = &logdp->wait;
3767c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, "nl7c");
3797c478bd9Sstevel@tonic-gate 	mutex_enter(lock);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	for (;;) {
3827c478bd9Sstevel@tonic-gate 		/* Wait for something to do */
3837c478bd9Sstevel@tonic-gate 		while ((lbp = logdp->head) == NULL) {
3847c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
3857c478bd9Sstevel@tonic-gate 			cv_wait(wait, lock);
3867c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, lock);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 		if ((logdp->head = lbp->next) == NULL)
3897c478bd9Sstevel@tonic-gate 			logdp->tail = NULL;
3907c478bd9Sstevel@tonic-gate 		/* Got a logbuf to write out */
3917c478bd9Sstevel@tonic-gate 		if (nl7c_logd_enabled)
3927c478bd9Sstevel@tonic-gate 			logd_log_write(lock, lbp);
3937c478bd9Sstevel@tonic-gate 		kmem_cache_free(log_buf_kmc, lbp);
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate boolean_t
3987c478bd9Sstevel@tonic-gate nl7c_logd_init(int fsz, caddr_t *fnv)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	vnode_t	*dvp;
4017c478bd9Sstevel@tonic-gate 	vnode_t	*svp;
4027c478bd9Sstevel@tonic-gate 	vnode_t	*vp;
4037c478bd9Sstevel@tonic-gate 	int	ret;
4047c478bd9Sstevel@tonic-gate 	caddr_t	*fnp;
4057c478bd9Sstevel@tonic-gate 	vattr_t	attr;
4067c478bd9Sstevel@tonic-gate 	uio_t	uio;
4077c478bd9Sstevel@tonic-gate 	iovec_t	iov;
4087c478bd9Sstevel@tonic-gate 	char	fbuf[TYPICALMAXPATHLEN + 1];
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * Initialize the global logfio.
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	nca_fio_cnt(&fio) = 0;
4147c478bd9Sstevel@tonic-gate 	nca_fio_ix(&fio) = 0;
4157c478bd9Sstevel@tonic-gate 	fnp = fnv;
4167c478bd9Sstevel@tonic-gate 	while (*fnp != NULL) {
4177c478bd9Sstevel@tonic-gate 		nca_fio_cnt(&fio)++;
4187c478bd9Sstevel@tonic-gate 		nca_fio_name(&fio) = *fnp;
4197c478bd9Sstevel@tonic-gate 		nca_fio_size(&fio) = fsz;
4207c478bd9Sstevel@tonic-gate 		nca_fio_offset(&fio) = 0;
4217c478bd9Sstevel@tonic-gate 		nca_fio_file(&fio) = nca_fio_ix(&fio);
4227c478bd9Sstevel@tonic-gate 		nca_fio_vp(&fio) = NULL;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		if (++fnp == &fnv[NCA_FIOV_SZ])
4257c478bd9Sstevel@tonic-gate 			break;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		nca_fio_ix(&fio)++;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * See if we can start logging from where we left off last time,
4317c478bd9Sstevel@tonic-gate 	 * first check if the symlink exists.
4327c478bd9Sstevel@tonic-gate 	 */
4337c478bd9Sstevel@tonic-gate 	dvp = NULL;
4347c478bd9Sstevel@tonic-gate 	ret = lookupname(symlink_path, UIO_SYSSPACE, NO_FOLLOW, &dvp, &svp);
4357c478bd9Sstevel@tonic-gate 	if (ret || dvp == NULL || svp == NULL) {
4367c478bd9Sstevel@tonic-gate 		if (dvp == NULL) {
4377c478bd9Sstevel@tonic-gate 			/* No NCA symlink directory, create one */
4387c478bd9Sstevel@tonic-gate 			attr.va_mask = AT_MODE | AT_TYPE;
4397c478bd9Sstevel@tonic-gate 			attr.va_mode = 0755;
4407c478bd9Sstevel@tonic-gate 			attr.va_type = VDIR;
4417c478bd9Sstevel@tonic-gate 			ret = vn_create(symlink_dir, UIO_SYSSPACE, &attr,
4427c478bd9Sstevel@tonic-gate 			    EXCL, 0, &dvp, CRMKDIR, 0, 0);
4437c478bd9Sstevel@tonic-gate 			if (ret) {
4447c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "nl7c_logd_init: create"
4457c478bd9Sstevel@tonic-gate 				    " symlink dir of %s failed(%d).",
4467c478bd9Sstevel@tonic-gate 				    symlink_dir, ret);
4477c478bd9Sstevel@tonic-gate 				goto error;
4487c478bd9Sstevel@tonic-gate 			}
4497c478bd9Sstevel@tonic-gate 		}
45032cacfe4Sfr80241 		nca_fio_dvp(&fio) = dvp;
4517c478bd9Sstevel@tonic-gate 		/* No symlink so don't know were to start from */
4527c478bd9Sstevel@tonic-gate 		goto fresh_start;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	/* Save the symlink dir vnode */
4557c478bd9Sstevel@tonic-gate 	nca_fio_dvp(&fio) = dvp;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/* Check if the file pointed by the symlink exists */
4587c478bd9Sstevel@tonic-gate 	ret = lookupname(symlink_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
4597c478bd9Sstevel@tonic-gate 	if (ret || vp == NULL)
4607c478bd9Sstevel@tonic-gate 		goto fresh_start;
4617c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/* Read the symlink and find it in fnv[], else fresh start */
4647c478bd9Sstevel@tonic-gate 	iov.iov_len = TYPICALMAXPATHLEN;
4657c478bd9Sstevel@tonic-gate 	iov.iov_base = fbuf;
4667c478bd9Sstevel@tonic-gate 	uio.uio_iov = &iov;
4677c478bd9Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
4687c478bd9Sstevel@tonic-gate 	uio.uio_resid = iov.iov_len;
4697c478bd9Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
4707c478bd9Sstevel@tonic-gate 	uio.uio_loffset = 0;
4717c478bd9Sstevel@tonic-gate 	uio.uio_fmode = 0;
472*da6c28aaSamw 	ret = VOP_READLINK(svp, &uio, kcred, NULL);
4737c478bd9Sstevel@tonic-gate 	if (ret) {
474*da6c28aaSamw 		(void) VOP_REMOVE(dvp, symlink, kcred, NULL, 0);
4757c478bd9Sstevel@tonic-gate 		goto fresh_start;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/* Null terminate the buf */
4797c478bd9Sstevel@tonic-gate 	fbuf[TYPICALMAXPATHLEN - (int)uio.uio_resid] = '\0';
4807c478bd9Sstevel@tonic-gate 	fnp = fnv;
4817c478bd9Sstevel@tonic-gate 	nca_fio_ix(&fio) = 0;
4827c478bd9Sstevel@tonic-gate 	while (*fnp != NULL) {
4837c478bd9Sstevel@tonic-gate 		if (strcmp(*fnp, fbuf) == 0)
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		if (++fnp == &fnv[NCA_FIOV_SZ])
4867c478bd9Sstevel@tonic-gate 			goto fresh_start;
4877c478bd9Sstevel@tonic-gate 		nca_fio_ix(&fio)++;
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 	if (*fnp == NULL)
4907c478bd9Sstevel@tonic-gate 		goto fresh_start;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/* Start writing to the end of the file */
4937c478bd9Sstevel@tonic-gate 	ret = vn_open(*fnp, UIO_SYSSPACE,
4947c478bd9Sstevel@tonic-gate 	    FCREAT|FWRITE|FAPPEND, 0600, &vp, 0, 0);
4957c478bd9Sstevel@tonic-gate 	if (ret) {
4967c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd_init: vn_open of "
4977c478bd9Sstevel@tonic-gate 		    "%s failed (error %d)", *fnp, ret);
4987c478bd9Sstevel@tonic-gate 		goto error;
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	nca_fio_vp(&fio) = vp;
501*da6c28aaSamw 	(void) VOP_IOCTL(vp, _FIODIRECTIO, DIRECTIO_ON, 0, kcred, NULL, NULL);
5027c478bd9Sstevel@tonic-gate 	attr.va_mask = AT_SIZE;
503*da6c28aaSamw 	ret = VOP_GETATTR(nca_fio_vp(&fio), &attr, 0, NULL, NULL);
5047c478bd9Sstevel@tonic-gate 	if (ret) {
5057c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd_init: getattr of %s failed", *fnp);
5067c478bd9Sstevel@tonic-gate 		goto error;
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	nca_fio_offset(&fio) = (off64_t)attr.va_size;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	goto finish;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate fresh_start:
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * Here if no previous logging environment found or if the previous
5157c478bd9Sstevel@tonic-gate 	 * logging environment isn't usable or isn't consistent with the new
5167c478bd9Sstevel@tonic-gate 	 * fnv[]. Remove the existing symlink (if any) then create the new
5177c478bd9Sstevel@tonic-gate 	 * symlink to point to the first logfile.
5187c478bd9Sstevel@tonic-gate 	 */
5197c478bd9Sstevel@tonic-gate 	nca_fio_ix(&fio) = 0;
5207c478bd9Sstevel@tonic-gate 	attr.va_mask = AT_MODE | AT_TYPE;
5217c478bd9Sstevel@tonic-gate 	attr.va_mode = 0777;
5227c478bd9Sstevel@tonic-gate 	attr.va_type = VLNK;
523*da6c28aaSamw 	(void) VOP_REMOVE(dvp, symlink, kcred, NULL, 0);
524*da6c28aaSamw 	ret = VOP_SYMLINK(dvp, symlink, &attr, nca_fio_name(&fio), kcred, NULL,
525*da6c28aaSamw 	    0);
5267c478bd9Sstevel@tonic-gate 	if (ret) {
5277c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd_init: symlink of %s to %s failed",
5287c478bd9Sstevel@tonic-gate 		    symlink_path, nca_fio_name(&fio));
5297c478bd9Sstevel@tonic-gate 		goto error;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	ret = vn_open(nca_fio_name(&fio), UIO_SYSSPACE,
5327c478bd9Sstevel@tonic-gate 	    FCREAT|FWRITE|FTRUNC, 0600, &nca_fio_vp(&fio), 0, 0);
5337c478bd9Sstevel@tonic-gate 	if (ret) {
5347c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "nl7c_logd_init: vn_open of "
5357c478bd9Sstevel@tonic-gate 		    "%s failed (error %d)", nca_fio_name(&fio), ret);
5367c478bd9Sstevel@tonic-gate 		goto error;
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/* Turn on directio */
5407c478bd9Sstevel@tonic-gate 	(void) VOP_IOCTL(nca_fio_vp(&fio), _FIODIRECTIO,
541*da6c28aaSamw 			DIRECTIO_ON, 0, kcred, NULL, NULL);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate finish:
5447c478bd9Sstevel@tonic-gate 	log_buf_kmc = kmem_cache_create("NL7C_log_buf_kmc", sizeof (log_buf_t),
5457c478bd9Sstevel@tonic-gate 		0, NULL, NULL, NULL, NULL, NULL, 0);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	mutex_init(&log_lock, NULL, MUTEX_DEFAULT, NULL);
5487c478bd9Sstevel@tonic-gate 	mutex_enter(&log_lock);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (nl7c_logbuf_max == 0)
5517c478bd9Sstevel@tonic-gate 		nl7c_logbuf_max = max_ncpus;
5527c478bd9Sstevel@tonic-gate 	logv = kmem_alloc(nl7c_logbuf_max * sizeof (*logv), KM_SLEEP);
5537c478bd9Sstevel@tonic-gate 	for (logvcnt = 0; logvcnt < nl7c_logbuf_max; logvcnt++) {
5547c478bd9Sstevel@tonic-gate 		logv[logvcnt] = kmem_cache_alloc(log_buf_kmc, KM_SLEEP);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	log_buf_alloc(KM_SLEEP);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	mutex_init(&logd.lock, NULL, MUTEX_DEFAULT, NULL);
5607c478bd9Sstevel@tonic-gate 	cv_init(&logd.wait, NULL, CV_DEFAULT, NULL);
5617c478bd9Sstevel@tonic-gate 	logd.head = NULL;
5627c478bd9Sstevel@tonic-gate 	logd.tail = NULL;
5637c478bd9Sstevel@tonic-gate 	logd.worker = thread_create(NULL, 0, logd_worker, &logd,
5647c478bd9Sstevel@tonic-gate 	    0, &p0, TS_RUN, maxclsyspri);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	mutex_exit(&log_lock);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/* Last, start logger timeout flush */
5697c478bd9Sstevel@tonic-gate 	logit_flush(NULL);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	return (B_TRUE);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * Error of some sort, free any resources in reverse order.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate error:
5777c478bd9Sstevel@tonic-gate 	nca_fio_ix(&fio) = 0;
5787c478bd9Sstevel@tonic-gate 	while (nca_fio_ix(&fio) < nca_fio_cnt(&fio)) {
5797c478bd9Sstevel@tonic-gate 		char *name = nca_fio_name(&fio);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		if ((vp = nca_fio_vp(&fio)) != NULL)
5827c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
58393efb88fSgeorges 		kmem_free(name, (strlen(name) + 1));
5847c478bd9Sstevel@tonic-gate 		nca_fio_ix(&fio)++;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	nca_fio_cnt(&fio) = 0;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (svp)
5897c478bd9Sstevel@tonic-gate 		VN_RELE(svp);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if (dvp)
5927c478bd9Sstevel@tonic-gate 		VN_RELE(dvp);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5987c478bd9Sstevel@tonic-gate static void
5997c478bd9Sstevel@tonic-gate logit_flush(void *arg)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	static log_buf_t *lastlbp = NULL;
6027c478bd9Sstevel@tonic-gate 	static int	lastpos;
6037c478bd9Sstevel@tonic-gate 	log_buf_t	*lbp = log;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	flush_tid = 0;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	mutex_enter(&log_lock);
6087c478bd9Sstevel@tonic-gate 	if (log == NULL) {
6097c478bd9Sstevel@tonic-gate 		/* No global logbuf ? Nothing to flush. */
6107c478bd9Sstevel@tonic-gate 		goto out;
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 	if (lbp != NULL && lbp->cur_pos > (sizeof (nca_log_buf_hdr_t)) &&
6137c478bd9Sstevel@tonic-gate 		lastlbp == lbp && lastpos == lbp->cur_pos) {
6147c478bd9Sstevel@tonic-gate 		/*
6157c478bd9Sstevel@tonic-gate 		 * We have a logbuf and it has log data and it's the
6167c478bd9Sstevel@tonic-gate 		 * same logbuf and pos as last time and after lock
6177c478bd9Sstevel@tonic-gate 		 * still true, so flush.
6187c478bd9Sstevel@tonic-gate 		 */
6197c478bd9Sstevel@tonic-gate 		nca_log_stat_t	*sp;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		sp = &(((nca_log_buf_hdr_t *)lbp)->nca_logstats);
6227c478bd9Sstevel@tonic-gate 		sp->n_log_size = lbp->cur_pos;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		/* Link new logbuf onto end of logd and wake logd up */
6257c478bd9Sstevel@tonic-gate 		mutex_enter(&logd.lock);
6267c478bd9Sstevel@tonic-gate 		log->next = NULL;
6277c478bd9Sstevel@tonic-gate 		if (logd.tail == NULL)
6287c478bd9Sstevel@tonic-gate 			logd.head = log;
6297c478bd9Sstevel@tonic-gate 		else
6307c478bd9Sstevel@tonic-gate 			logd.tail->next = log;
6317c478bd9Sstevel@tonic-gate 		logd.tail = log;
6327c478bd9Sstevel@tonic-gate 		cv_signal(&logd.wait);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		mutex_exit(&logd.lock);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		log_buf_alloc(KM_NOSLEEP);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if ((lastlbp = lbp) != NULL)
6407c478bd9Sstevel@tonic-gate 		lastpos = lbp->cur_pos;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	mutex_exit(&log_lock);
6437c478bd9Sstevel@tonic-gate out:
6447c478bd9Sstevel@tonic-gate 	/* Check again in 1 second */
6457c478bd9Sstevel@tonic-gate 	flush_tid = timeout(&logit_flush, NULL, hz);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate void
6497c478bd9Sstevel@tonic-gate nl7c_logd_log(uri_desc_t *quri, uri_desc_t *suri, time_t rtime, ipaddr_t faddr)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	nca_request_log_t *req;
6527c478bd9Sstevel@tonic-gate 	char		*wp;
6537c478bd9Sstevel@tonic-gate 	char		*pep;
6547c478bd9Sstevel@tonic-gate 	int		sz;
6557c478bd9Sstevel@tonic-gate 	uint32_t	off = 0;
6567c478bd9Sstevel@tonic-gate 	int		kmflag = servicing_interrupt() ? KM_NOSLEEP : KM_SLEEP;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if (! nl7c_logd_enabled)
6597c478bd9Sstevel@tonic-gate 		return;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (! nl7c_logd_started) {
6627c478bd9Sstevel@tonic-gate 		/* Startup logging */
6637c478bd9Sstevel@tonic-gate 		nl7clogd_startup();
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 	mutex_enter(&log_lock);
6667c478bd9Sstevel@tonic-gate again:
6677c478bd9Sstevel@tonic-gate 	if (log == NULL) {
6687c478bd9Sstevel@tonic-gate 		/* No global logbuf, try to allocate one before giving up. */
6697c478bd9Sstevel@tonic-gate 		log_buf_alloc(kmflag);
6707c478bd9Sstevel@tonic-gate 		if (log == NULL) {
6717c478bd9Sstevel@tonic-gate 			/* No joy, just give up. */
6727c478bd9Sstevel@tonic-gate 			mutex_exit(&log_lock);
6737c478bd9Sstevel@tonic-gate 			return;
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * Get a pointer to an aligned write position, a pointer to past
6787c478bd9Sstevel@tonic-gate 	 * the end of the logbuf, and a pointer to the request header.
6797c478bd9Sstevel@tonic-gate 	 *
6807c478bd9Sstevel@tonic-gate 	 * As the request header is filled in field by field addtional
6817c478bd9Sstevel@tonic-gate 	 * storage is allcated following the request header.
6827c478bd9Sstevel@tonic-gate 	 *
6837c478bd9Sstevel@tonic-gate 	 * If at any point an allocation from the logbuf overflows (i.e.
6847c478bd9Sstevel@tonic-gate 	 * resulting in a pointer > pep) the current request logging is
6857c478bd9Sstevel@tonic-gate 	 * aborted, the current logbuf is posted for write, a new logbuf
6867c478bd9Sstevel@tonic-gate 	 * is allocated, and start all over.
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	pep = &((char *)log)[log->size];
6897c478bd9Sstevel@tonic-gate 	wp = (log->buffer + log->cur_pos);
6907c478bd9Sstevel@tonic-gate 	wp = NCA_LOG_ALIGN(wp);
6917c478bd9Sstevel@tonic-gate 	req = (nca_request_log_t *)wp;
6927c478bd9Sstevel@tonic-gate 	if ((wp + sizeof (*req)) >= pep) goto full;
6937c478bd9Sstevel@tonic-gate 	bzero(wp, sizeof (*req));
6947c478bd9Sstevel@tonic-gate 	wp += sizeof (*req);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	sz = MIN((quri->path.ep - quri->path.cp), MAX_URL_LEN);
6977c478bd9Sstevel@tonic-gate 	if ((wp + sz + 1) >= pep) goto full;
6987c478bd9Sstevel@tonic-gate 	bcopy(quri->path.cp, wp, sz);
6997c478bd9Sstevel@tonic-gate 	wp += sz;
7007c478bd9Sstevel@tonic-gate 	*wp++ = 0;
7017c478bd9Sstevel@tonic-gate 	sz++;
7027c478bd9Sstevel@tonic-gate 	req->request_url_len = sz;
7037c478bd9Sstevel@tonic-gate 	req->request_url = off;
7047c478bd9Sstevel@tonic-gate 	off += sz;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * Set response length now as the scheme log function will
7087c478bd9Sstevel@tonic-gate 	 * subtract out any header length as we want the entity body
7097c478bd9Sstevel@tonic-gate 	 * length returned for the response_len.
7107c478bd9Sstevel@tonic-gate 	 */
7117c478bd9Sstevel@tonic-gate 	req->response_len = (uint_t)suri->resplen;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/* Call scheme log */
7147c478bd9Sstevel@tonic-gate 	if (nl7c_http_log(quri, suri, req, &wp, &pep, &off)) goto full;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/* Update logbuf */
7177c478bd9Sstevel@tonic-gate 	log->cur_pos = (wp - log->buffer);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	req->response_status = HS_OK;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	req->start_process_time = (time32_t)rtime;
7227c478bd9Sstevel@tonic-gate 	req->end_process_time = (time32_t)gethrestime_sec();
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	req->remote_host = faddr;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	((nca_log_buf_hdr_t *)log)->nca_logstats.n_log_recs++;
7277c478bd9Sstevel@tonic-gate 	mutex_exit(&log_lock);
7287c478bd9Sstevel@tonic-gate 	return;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate full:
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * The logbuf is full, zero fill from current
7337c478bd9Sstevel@tonic-gate 	 * write pointer through the end of the buf.
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	wp = (log->buffer + log->cur_pos);
7367c478bd9Sstevel@tonic-gate 	sz = pep - wp;
7377c478bd9Sstevel@tonic-gate 	bzero(wp, sz);
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Link new logbuf onto end of logd and wake logd up.
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 	mutex_enter(&logd.lock);
7427c478bd9Sstevel@tonic-gate 	log->next = NULL;
7437c478bd9Sstevel@tonic-gate 	if (logd.tail == NULL)
7447c478bd9Sstevel@tonic-gate 		logd.head = log;
7457c478bd9Sstevel@tonic-gate 	else
7467c478bd9Sstevel@tonic-gate 		logd.tail->next = log;
7477c478bd9Sstevel@tonic-gate 	logd.tail = log;
7487c478bd9Sstevel@tonic-gate 	cv_signal(&logd.wait);
7497c478bd9Sstevel@tonic-gate 	mutex_exit(&logd.lock);
7507c478bd9Sstevel@tonic-gate 	/*
7517c478bd9Sstevel@tonic-gate 	 * Try to allocate a new global logbuf.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	log_buf_alloc(kmflag);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	goto again;
7567c478bd9Sstevel@tonic-gate }
757