1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/sysmacros.h>
#include <sys/callb.h>
#include <sys/fcntl.h>
#include <sys/filio.h>
#include <sys/pathname.h>
#include <sys/cpuvar.h>
#include <sys/promif.h>
#include <fs/sockfs/nl7c.h>
#include <fs/sockfs/nl7curi.h>
#include <inet/nca/ncadoorhdr.h>
#include <inet/nca/ncalogd.h>
extern boolean_t nl7c_logd_enabled;
extern boolean_t nl7c_logd_started;
extern boolean_t nl7c_logd_cycle;
extern void nl7clogd_startup(void);
extern boolean_t nl7c_http_log(uri_desc_t *, uri_desc_t *,
nca_request_log_t *, char **, char **, uint32_t *);
static void logit_flush(void *);
/*
* NL7C reuses the NCA logging scheme, the directory "/var/nca" contains
* the symlink "current" to 1 of up to 16 NCA BLF logging files, by default
* a single logging file "log", optionally paths of up to 16 log files can
* be specified via ncalogd.conf(5), note that these log files need not be
* in the "/var/nca" directory.
*
* NL7C reuses the NCA logging APIs defined in <inet/nca/ncalogd.h>, at
* some future date (when NCA is deprecated or improvements are needed)
* these need to be moved into NL7C.
*
* NL7C implements logging differently in 2 ways, 1st the initialization
* is handled completely in the kernel by NL7C when it's enabled vs NCA
* when the kmod was loaded, 2nd a simple worker thread with a FIFO queue
* is used to process log_buf_t's instead of a squeue_t (this is done as
* squeue_t's are private to NCA and IP at some future date we may us an
* IP squeue_t):
*
* logd_t - used by various functions to manage a singly linked
* grounded list of log_buf_t's and it's worker thread.
*/
typedef struct logd_s {
log_buf_t *head;
log_buf_t *tail;
kthread_t *worker;
kcondvar_t wait;
kmutex_t lock;
} logd_t;
/*
* In-kernel logging:
*
* nl7c_logbuf_max - tunable for the number of preallocated next
* log_buf_t(s) for use by log_buf_alloc(), note if the value is
* 0 (the default) then max_cpus worth will be allocated.
*
* logd - global logd_t used to post log_buf_t's too.
*
* log - global current log_buf_t that logit() logs too.
*
* logv[] - vector of available next logbuf(s) such that when
* logbuf is filled another can be used while being processed by
* the logger() and kmem_cache_alloc() of a replacement is done.
*
* logvcnt - count of logv[] vector element(s) and the index
* plus 1 of the next logbuf.
*
* log_buf_kmc - the kmem_cache to alloc/free log_buf_t's from/to.
*
* fio - the global nca_fio_t used to manage file i/o to a logfile.
*
* dir - path to the directory where the current logfile symlink
* is created and the default directory for logfile(s).
*
* symlink - name of the logfile symlink.
*
* symlink_path - path to the logfile symlink.
*
* log_lock - the kmutex_t used to guarantee atomic access of
* all of the above.
*
* flush_tid - logit_flush() timeout id.
*
* LOGBUFV_ALLOC() - macro used to add log_buf_t(s) to logv[].
*/
int nl7c_logbuf_max = 0;
static logd_t logd;
static log_buf_t *log = NULL;
static log_buf_t **logv = NULL;
static int logvcnt = 0;
static kmem_cache_t *log_buf_kmc;
static nca_fio_t fio;
static caddr_t dir = "/var/nca/";
static caddr_t symlink = "current";
static caddr_t symlink_dir = "/var/nca";
static caddr_t symlink_path = "/var/nca/current";
static kmutex_t log_lock;
static timeout_id_t flush_tid;
#define LOGBUFV_ALLOC(kmflag) { \
log_buf_t *_p; \
\
ASSERT(mutex_owned(&log_lock)); \
while (logvcnt < nl7c_logbuf_max) { \
/*CONSTCOND*/ \
if (kmflag == KM_SLEEP) \
mutex_exit(&log_lock); \
_p = kmem_cache_alloc(log_buf_kmc, kmflag); \
/*CONSTCOND*/ \
if (kmflag == KM_SLEEP) { \
mutex_enter(&log_lock); \
if (logvcnt == nl7c_logbuf_max) { \
mutex_exit(&log_lock); \
kmem_cache_free(log_buf_kmc, _p); \
mutex_enter(&log_lock); \
break; \
} \
} else { \
if (_p == NULL) { \
break; \
} \
} \
logv[logvcnt++] = _p; \
} \
}
/*
* Exports for inet/nca/ncaddi.c:
*/
nca_fio_t *nl7c_logd_fio = &fio;
static void
log_buf_alloc(int kmflag)
{
nca_log_buf_hdr_t *hdr;
static ulong_t seq = 0;
ASSERT(mutex_owned(&log_lock));
if (logvcnt == 0) {
/*
* No logv[] to use for the new log global logbuf,
* try to allocate one or more before giving up.
*/
LOGBUFV_ALLOC(kmflag);
if (logvcnt == 0) {
/* No joy, just give up. */
log = NULL;
return;
}
}
log = logv[--logvcnt];
log->size = NCA_DEFAULT_LOG_BUF_SIZE;
log->cur_pos = sizeof (*hdr);
hdr = (nca_log_buf_hdr_t *)&log->buffer;
hdr->nca_loghdr.nca_version = NCA_LOG_VERSION1;
hdr->nca_loghdr.nca_op = log_op;
hdr->nca_logstats.n_log_size = NCA_DEFAULT_LOG_BUF_SIZE - sizeof (*hdr);
hdr->nca_logstats.n_log_recs = 0;
hdr->nca_logstats.n_log_upcall = seq++;
/* Try to allocate for at least the one we just used */
LOGBUFV_ALLOC(kmflag);
}
static void
logd_off()
{
;
}
static void
logd_log_write(kmutex_t *lock, log_buf_t *lbp)
{
nca_log_buf_hdr_t *hdr = (nca_log_buf_hdr_t *)lbp->buffer;
nca_log_stat_t *sts = &hdr->nca_logstats;
int size = sts->n_log_size + sizeof (*hdr);
vnode_t *vp;
uio_t uio;
iovec_t iov;
int ret;
boolean_t noretry = B_FALSE;
vattr_t attr;
if (size & (DEV_BSIZE - 1)) {
/*
* Not appropriately sized for directio(),
* add some filler so it is.
*/
sts->n_log_size += DEV_BSIZE - (size & (DEV_BSIZE - 1));
size = sts->n_log_size + sizeof (*hdr);
}
retry:
if (nca_fio_offset(&fio) + size <= nca_fio_size(&fio)) {
/*
* Room in the current log file so write the logbuf out,
* exit the logd lock while doing the i/o as to not block
* queuing.
*/
mutex_exit(lock);
vp = nca_fio_vp(&fio);
(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
iov.iov_base = lbp->buffer;
iov.iov_len = size;
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_fmode = 0;
uio.uio_loffset = (u_offset_t)nca_fio_offset(&fio);
uio.uio_llimit = curproc->p_fsz_ctl;
uio.uio_resid = size;
ret = VOP_WRITE(vp, &uio, 0, kcred, NULL);
VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
if (ret != 0) {
if (ret == EFBIG) {
/*
* Out of space for this file,
* retry with the next.
*/
nca_fio_size(&fio) = nca_fio_offset(&fio);
if (noretry) {
nl7c_logd_enabled = B_FALSE;
goto done;
} else
goto next;
}
}
nca_fio_offset(&fio) = uio.uio_loffset;
mutex_enter(lock);
goto done;
}
/*
* Current logfile doesn't have sufficient space
* so move on to next file (if any).
*/
next:
mutex_exit(lock);
/* Close current file */
ret = VOP_CLOSE(nca_fio_vp(&fio), FCREAT|FWRITE|FAPPEND|FTRUNC,
1, (offset_t)0, kcred, NULL);
nca_fio_vp(&fio) = NULL;
if (ret) {
cmn_err(CE_WARN, "nl7c_logd: close of %s failed (error %d)",
nca_fio_name(&fio), ret);
nl7c_logd_enabled = B_FALSE;
logd_off();
return;
}
/* Go to next file */
nca_fio_ix(&fio)++;
if (nca_fio_ix(&fio) == nca_fio_cnt(&fio)) {
/*
* We have reached the last file. If cycling
* is not on, disable logging and bailout.
*/
if (nl7c_logd_cycle) {
/* Start from the first file */
nca_fio_ix(&fio) = 0;
} else {
nca_fio_ix(&fio)--;
nl7c_logd_enabled = B_FALSE;
logd_off();
return;
}
}
/* Open the next log file */
ret = vn_open(nca_fio_name(&fio), UIO_SYSSPACE, FCREAT|FWRITE|FTRUNC,
0600, &nca_fio_vp(&fio), 0, 0);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd: vn_open of %s failed (error %d)",
nca_fio_name(&fio), ret);
nl7c_logd_enabled = B_FALSE;
logd_off();
return;
}
/* Turn on directio */
(void) VOP_IOCTL(nca_fio_vp(&fio), _FIODIRECTIO,
DIRECTIO_ON, 0, kcred, NULL, NULL);
/* Start writing from the begining of the file */
nca_fio_offset(&fio) = 0;
/* Remove the current symlink */
(void) VOP_REMOVE(nca_fio_dvp(&fio), symlink, kcred, NULL, 0);
attr.va_mask = AT_MODE | AT_TYPE;
attr.va_mode = 0777;
attr.va_type = VLNK;
/* Create symlink to the new log file */
ret = VOP_SYMLINK(nca_fio_dvp(&fio), symlink,
&attr, nca_fio_name(&fio), kcred, NULL, 0);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd: symlink of %s to %s failed",
symlink, nca_fio_name(&fio));
nl7c_logd_enabled = B_FALSE;
logd_off();
return;
}
mutex_enter(lock);
goto retry;
done:
if (logvcnt < nl7c_logbuf_max) {
/* May need to allocate some logbuf(s) for logv[] */
mutex_enter(&log_lock);
if (logvcnt < nl7c_logbuf_max) {
/*
* After acquiring the lock still need logbuf(s),
* if the global logbuf pointer is NULL then call
* log_buf_alloc() as it will fill up logbugv[]
* and initialize a new logbuf else fill up just
* the logv[] here.
*/
if (log == NULL) {
log_buf_alloc(KM_SLEEP);
} else {
/*LINTED*/
LOGBUFV_ALLOC(KM_SLEEP);
}
}
mutex_exit(&log_lock);
}
}
static void
logd_worker(logd_t *logdp)
{
log_buf_t *lbp;
kmutex_t *lock = &logdp->lock;
kcondvar_t *wait = &logdp->wait;
callb_cpr_t cprinfo;
CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, "nl7c");
mutex_enter(lock);
for (;;) {
/* Wait for something to do */
while ((lbp = logdp->head) == NULL) {
CALLB_CPR_SAFE_BEGIN(&cprinfo);
cv_wait(wait, lock);
CALLB_CPR_SAFE_END(&cprinfo, lock);
}
if ((logdp->head = lbp->next) == NULL)
logdp->tail = NULL;
/* Got a logbuf to write out */
if (nl7c_logd_enabled)
logd_log_write(lock, lbp);
kmem_cache_free(log_buf_kmc, lbp);
}
}
boolean_t
nl7c_logd_init(int fsz, caddr_t *fnv)
{
vnode_t *dvp;
vnode_t *svp;
vnode_t *vp;
int ret;
caddr_t *fnp;
vattr_t attr;
uio_t uio;
iovec_t iov;
char fbuf[TYPICALMAXPATHLEN + 1];
/*
* Initialize the global logfio.
*/
nca_fio_cnt(&fio) = 0;
nca_fio_ix(&fio) = 0;
fnp = fnv;
while (*fnp != NULL) {
nca_fio_cnt(&fio)++;
nca_fio_name(&fio) = *fnp;
nca_fio_size(&fio) = fsz;
nca_fio_offset(&fio) = 0;
nca_fio_file(&fio) = nca_fio_ix(&fio);
nca_fio_vp(&fio) = NULL;
if (++fnp == &fnv[NCA_FIOV_SZ])
break;
nca_fio_ix(&fio)++;
}
/*
* See if we can start logging from where we left off last time,
* first check if the symlink exists.
*/
dvp = NULL;
ret = lookupname(symlink_path, UIO_SYSSPACE, NO_FOLLOW, &dvp, &svp);
if (ret || dvp == NULL || svp == NULL) {
if (dvp == NULL) {
/* No NCA symlink directory, create one */
attr.va_mask = AT_MODE | AT_TYPE;
attr.va_mode = 0755;
attr.va_type = VDIR;
ret = vn_create(symlink_dir, UIO_SYSSPACE, &attr,
EXCL, 0, &dvp, CRMKDIR, 0, 0);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd_init: create"
" symlink dir of %s failed(%d).",
symlink_dir, ret);
goto error;
}
}
nca_fio_dvp(&fio) = dvp;
/* No symlink so don't know were to start from */
goto fresh_start;
}
/* Save the symlink dir vnode */
nca_fio_dvp(&fio) = dvp;
/* Check if the file pointed by the symlink exists */
ret = lookupname(symlink_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
if (ret || vp == NULL)
goto fresh_start;
VN_RELE(vp);
/* Read the symlink and find it in fnv[], else fresh start */
iov.iov_len = TYPICALMAXPATHLEN;
iov.iov_base = fbuf;
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_resid = iov.iov_len;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_loffset = 0;
uio.uio_fmode = 0;
ret = VOP_READLINK(svp, &uio, kcred, NULL);
if (ret) {
(void) VOP_REMOVE(dvp, symlink, kcred, NULL, 0);
goto fresh_start;
}
/* Null terminate the buf */
fbuf[TYPICALMAXPATHLEN - (int)uio.uio_resid] = '\0';
fnp = fnv;
nca_fio_ix(&fio) = 0;
while (*fnp != NULL) {
if (strcmp(*fnp, fbuf) == 0)
break;
if (++fnp == &fnv[NCA_FIOV_SZ])
goto fresh_start;
nca_fio_ix(&fio)++;
}
if (*fnp == NULL)
goto fresh_start;
/* Start writing to the end of the file */
ret = vn_open(*fnp, UIO_SYSSPACE,
FCREAT|FWRITE|FAPPEND, 0600, &vp, 0, 0);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd_init: vn_open of "
"%s failed (error %d)", *fnp, ret);
goto error;
}
nca_fio_vp(&fio) = vp;
(void) VOP_IOCTL(vp, _FIODIRECTIO, DIRECTIO_ON, 0, kcred, NULL, NULL);
attr.va_mask = AT_SIZE;
ret = VOP_GETATTR(nca_fio_vp(&fio), &attr, 0, NULL, NULL);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd_init: getattr of %s failed", *fnp);
goto error;
}
nca_fio_offset(&fio) = (off64_t)attr.va_size;
goto finish;
fresh_start:
/*
* Here if no previous logging environment found or if the previous
* logging environment isn't usable or isn't consistent with the new
* fnv[]. Remove the existing symlink (if any) then create the new
* symlink to point to the first logfile.
*/
nca_fio_ix(&fio) = 0;
attr.va_mask = AT_MODE | AT_TYPE;
attr.va_mode = 0777;
attr.va_type = VLNK;
(void) VOP_REMOVE(dvp, symlink, kcred, NULL, 0);
ret = VOP_SYMLINK(dvp, symlink, &attr, nca_fio_name(&fio), kcred, NULL,
0);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd_init: symlink of %s to %s failed",
symlink_path, nca_fio_name(&fio));
goto error;
}
ret = vn_open(nca_fio_name(&fio), UIO_SYSSPACE,
FCREAT|FWRITE|FTRUNC, 0600, &nca_fio_vp(&fio), 0, 0);
if (ret) {
cmn_err(CE_WARN, "nl7c_logd_init: vn_open of "
"%s failed (error %d)", nca_fio_name(&fio), ret);
goto error;
}
/* Turn on directio */
(void) VOP_IOCTL(nca_fio_vp(&fio), _FIODIRECTIO,
DIRECTIO_ON, 0, kcred, NULL, NULL);
finish:
log_buf_kmc = kmem_cache_create("NL7C_log_buf_kmc", sizeof (log_buf_t),
0, NULL, NULL, NULL, NULL, NULL, 0);
mutex_init(&log_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_enter(&log_lock);
if (nl7c_logbuf_max == 0)
nl7c_logbuf_max = max_ncpus;
logv = kmem_alloc(nl7c_logbuf_max * sizeof (*logv), KM_SLEEP);
for (logvcnt = 0; logvcnt < nl7c_logbuf_max; logvcnt++) {
logv[logvcnt] = kmem_cache_alloc(log_buf_kmc, KM_SLEEP);
}
log_buf_alloc(KM_SLEEP);
mutex_init(&logd.lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&logd.wait, NULL, CV_DEFAULT, NULL);
logd.head = NULL;
logd.tail = NULL;
logd.worker = thread_create(NULL, 0, logd_worker, &logd,
0, &p0, TS_RUN, maxclsyspri);
mutex_exit(&log_lock);
/* Last, start logger timeout flush */
logit_flush(NULL);
return (B_TRUE);
/*
* Error of some sort, free any resources in reverse order.
*/
error:
nca_fio_ix(&fio) = 0;
while (nca_fio_ix(&fio) < nca_fio_cnt(&fio)) {
char *name = nca_fio_name(&fio);
if ((vp = nca_fio_vp(&fio)) != NULL)
VN_RELE(vp);
kmem_free(name, (strlen(name) + 1));
nca_fio_ix(&fio)++;
}
nca_fio_cnt(&fio) = 0;
if (svp)
VN_RELE(svp);
if (dvp)
VN_RELE(dvp);
return (B_FALSE);
}
/*ARGSUSED*/
static void
logit_flush(void *arg)
{
static log_buf_t *lastlbp = NULL;
static int lastpos;
log_buf_t *lbp = log;
flush_tid = 0;
mutex_enter(&log_lock);
if (log == NULL) {
/* No global logbuf ? Nothing to flush. */
goto out;
}
if (lbp != NULL && lbp->cur_pos > (sizeof (nca_log_buf_hdr_t)) &&
lastlbp == lbp && lastpos == lbp->cur_pos) {
/*
* We have a logbuf and it has log data and it's the
* same logbuf and pos as last time and after lock
* still true, so flush.
*/
nca_log_stat_t *sp;
sp = &(((nca_log_buf_hdr_t *)lbp)->nca_logstats);
sp->n_log_size = lbp->cur_pos;
/* Link new logbuf onto end of logd and wake logd up */
mutex_enter(&logd.lock);
log->next = NULL;
if (logd.tail == NULL)
logd.head = log;
else
logd.tail->next = log;
logd.tail = log;
cv_signal(&logd.wait);
mutex_exit(&logd.lock);
log_buf_alloc(KM_NOSLEEP);
}
if ((lastlbp = lbp) != NULL)
lastpos = lbp->cur_pos;
mutex_exit(&log_lock);
out:
/* Check again in 1 second */
flush_tid = timeout(&logit_flush, NULL, hz);
}
void
nl7c_logd_log(uri_desc_t *quri, uri_desc_t *suri, time_t rtime, ipaddr_t faddr)
{
nca_request_log_t *req;
char *wp;
char *pep;
int sz;
uint32_t off = 0;
int kmflag = servicing_interrupt() ? KM_NOSLEEP : KM_SLEEP;
if (! nl7c_logd_enabled)
return;
if (! nl7c_logd_started) {
/* Startup logging */
nl7clogd_startup();
}
mutex_enter(&log_lock);
again:
if (log == NULL) {
/* No global logbuf, try to allocate one before giving up. */
log_buf_alloc(kmflag);
if (log == NULL) {
/* No joy, just give up. */
mutex_exit(&log_lock);
return;
}
}
/*
* Get a pointer to an aligned write position, a pointer to past
* the end of the logbuf, and a pointer to the request header.
*
* As the request header is filled in field by field addtional
* storage is allcated following the request header.
*
* If at any point an allocation from the logbuf overflows (i.e.
* resulting in a pointer > pep) the current request logging is
* aborted, the current logbuf is posted for write, a new logbuf
* is allocated, and start all over.
*/
pep = &((char *)log)[log->size];
wp = (log->buffer + log->cur_pos);
wp = NCA_LOG_ALIGN(wp);
req = (nca_request_log_t *)wp;
if ((wp + sizeof (*req)) >= pep) goto full;
bzero(wp, sizeof (*req));
wp += sizeof (*req);
sz = MIN((quri->path.ep - quri->path.cp), MAX_URL_LEN);
if ((wp + sz + 1) >= pep) goto full;
bcopy(quri->path.cp, wp, sz);
wp += sz;
*wp++ = 0;
sz++;
req->request_url_len = sz;
req->request_url = off;
off += sz;
/*
* Set response length now as the scheme log function will
* subtract out any header length as we want the entity body
* length returned for the response_len.
*/
req->response_len = (uint_t)suri->resplen;
/* Call scheme log */
if (nl7c_http_log(quri, suri, req, &wp, &pep, &off)) goto full;
/* Update logbuf */
log->cur_pos = (wp - log->buffer);
req->response_status = HS_OK;
req->start_process_time = (time32_t)rtime;
req->end_process_time = (time32_t)gethrestime_sec();
req->remote_host = faddr;
((nca_log_buf_hdr_t *)log)->nca_logstats.n_log_recs++;
mutex_exit(&log_lock);
return;
full:
/*
* The logbuf is full, zero fill from current
* write pointer through the end of the buf.
*/
wp = (log->buffer + log->cur_pos);
sz = pep - wp;
bzero(wp, sz);
/*
* Link new logbuf onto end of logd and wake logd up.
*/
mutex_enter(&logd.lock);
log->next = NULL;
if (logd.tail == NULL)
logd.head = log;
else
logd.tail->next = log;
logd.tail = log;
cv_signal(&logd.wait);
mutex_exit(&logd.lock);
/*
* Try to allocate a new global logbuf.
*/
log_buf_alloc(kmflag);
goto again;
}
|