xref: /illumos-gate/usr/src/uts/common/fs/sockfs/nl7curi.c (revision 52aec5b9)
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
52c9e429eSbrutus  * Common Development and Distribution License (the "License").
62c9e429eSbrutus  * 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 /*
22b73114cbSAnil udupa  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
267c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
307c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
317c478bd9Sstevel@tonic-gate #include <sys/condvar_impl.h>
327c478bd9Sstevel@tonic-gate #include <sys/sendfile.h>
337c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
347c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7curi.h>
350f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h>
367c478bd9Sstevel@tonic-gate 
372c9e429eSbrutus #include <inet/common.h>
382c9e429eSbrutus #include <inet/ip.h>
392c9e429eSbrutus #include <inet/ip6.h>
402c9e429eSbrutus #include <inet/tcp.h>
412c9e429eSbrutus #include <inet/led.h>
422c9e429eSbrutus #include <inet/mi.h>
432c9e429eSbrutus 
442c9e429eSbrutus #include <inet/nca/ncadoorhdr.h>
452c9e429eSbrutus #include <inet/nca/ncalogd.h>
462c9e429eSbrutus #include <inet/nca/ncandd.h>
472c9e429eSbrutus 
482c9e429eSbrutus #include <sys/promif.h>
492c9e429eSbrutus 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Some externs:
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
542c9e429eSbrutus extern boolean_t	nl7c_logd_enabled;
552c9e429eSbrutus extern void		nl7c_logd_log(uri_desc_t *, uri_desc_t *,
562c9e429eSbrutus 			    time_t, ipaddr_t);
572c9e429eSbrutus extern boolean_t	nl7c_close_addr(struct sonode *);
582c9e429eSbrutus extern struct sonode	*nl7c_addr2portso(void *);
592c9e429eSbrutus extern uri_desc_t	*nl7c_http_cond(uri_desc_t *, uri_desc_t *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Various global tuneables:
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate clock_t		nl7c_uri_ttl = -1;	/* TTL in seconds (-1 == infinite) */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate boolean_t	nl7c_use_kmem = B_FALSE; /* Force use of kmem (no segmap) */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate uint64_t	nl7c_file_prefetch = 1; /* File cache prefetch pages */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate uint64_t	nl7c_uri_max = 0;	/* Maximum bytes (0 == infinite) */
727c478bd9Sstevel@tonic-gate uint64_t	nl7c_uri_bytes = 0;	/* Bytes of kmem used by URIs */
737c478bd9Sstevel@tonic-gate 
742c9e429eSbrutus /*
752c9e429eSbrutus  * Locals:
762c9e429eSbrutus  */
772c9e429eSbrutus 
782c9e429eSbrutus static int	uri_rd_response(struct sonode *, uri_desc_t *,
792c9e429eSbrutus 		    uri_rd_t *, boolean_t);
802c9e429eSbrutus static int	uri_response(struct sonode *, uri_desc_t *);
812c9e429eSbrutus 
822c9e429eSbrutus /*
832c9e429eSbrutus  * HTTP scheme functions called from nl7chttp.c:
842c9e429eSbrutus  */
852c9e429eSbrutus 
862c9e429eSbrutus boolean_t nl7c_http_request(char **, char *, uri_desc_t *, struct sonode *);
872c9e429eSbrutus boolean_t nl7c_http_response(char **, char *, uri_desc_t *, struct sonode *);
882c9e429eSbrutus boolean_t nl7c_http_cmp(void *, void *);
892c9e429eSbrutus mblk_t *nl7c_http_persist(struct sonode *);
902c9e429eSbrutus void nl7c_http_free(void *arg);
912c9e429eSbrutus void nl7c_http_init(void);
922c9e429eSbrutus 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Counters that need to move to kstat and/or be removed:
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_request = 0;
987c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_hit = 0;
997c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass = 0;
1007c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_miss = 0;
1017c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp = 0;
1027c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more = 0;
1037c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_data = 0;
1047c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_sendfilev = 0;
1057c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_reclaim_calls = 0;
1067c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_reclaim_cnt = 0;
1077c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_urifail = 0;
1087c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_dupbfail = 0;
1097c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_get = 0;
1102c9e429eSbrutus volatile uint64_t nl7c_uri_pass_method = 0;
1117c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_option = 0;
1127c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_eol = 0;
1137c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_more_http = 0;
1147c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_http = 0;
1157c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_addfail = 0;
1167c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_pass_temp = 0;
1177c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_expire = 0;
1182c9e429eSbrutus volatile uint64_t nl7c_uri_purge = 0;
1197c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_NULL1 = 0;
1207c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_NULL2 = 0;
1217c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_close = 0;
1227c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp_close = 0;
1237c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_free = 0;
1247c478bd9Sstevel@tonic-gate volatile uint64_t nl7c_uri_temp_free = 0;
1252c9e429eSbrutus volatile uint64_t nl7c_uri_temp_mk = 0;
1262c9e429eSbrutus volatile uint64_t nl7c_uri_rd_EAGAIN = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Various kmem_cache_t's:
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate 
1322c9e429eSbrutus kmem_cache_t *nl7c_uri_kmc;
1332c9e429eSbrutus kmem_cache_t *nl7c_uri_rd_kmc;
1347c478bd9Sstevel@tonic-gate static kmem_cache_t *uri_desb_kmc;
1357c478bd9Sstevel@tonic-gate static kmem_cache_t *uri_segmap_kmc;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void uri_kmc_reclaim(void *);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void nl7c_uri_reclaim(void);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * The URI hash is a dynamically sized A/B bucket hash, when the current
1437c478bd9Sstevel@tonic-gate  * hash's average bucket chain length exceeds URI_HASH_AVRG a new hash of
1447c478bd9Sstevel@tonic-gate  * the next P2Ps[] size is created.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * All lookups are done in the current hash then the new hash (if any),
1477c478bd9Sstevel@tonic-gate  * if there is a new has then when a current hash bucket chain is examined
1487c478bd9Sstevel@tonic-gate  * any uri_desc_t members will be migrated to the new hash and when the
1497c478bd9Sstevel@tonic-gate  * last uri_desc_t has been migrated then the new hash will become the
1507c478bd9Sstevel@tonic-gate  * current and the previous current hash will be freed leaving a single
1517c478bd9Sstevel@tonic-gate  * hash.
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * uri_hash_t - hash bucket (chain) type, contained in the uri_hash_ab[]
1547c478bd9Sstevel@tonic-gate  * and can be accessed only after aquiring the uri_hash_access lock (for
1557c478bd9Sstevel@tonic-gate  * READER or WRITER) then acquiring the lock uri_hash_t.lock, the uri_hash_t
1567c478bd9Sstevel@tonic-gate  * and all linked uri_desc_t.hash members are protected. Note, a REF_HOLD()
1577c478bd9Sstevel@tonic-gate  * is placed on all uri_desc_t uri_hash_t list members.
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * uri_hash_access - rwlock for all uri_hash_* variables, READER for read
1607c478bd9Sstevel@tonic-gate  * access and WRITER for write access. Note, WRITER is only required for
1617c478bd9Sstevel@tonic-gate  * hash geometry changes.
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * uri_hash_which - which uri_hash_ab[] is the current hash.
1647c478bd9Sstevel@tonic-gate  *
1657c478bd9Sstevel@tonic-gate  * uri_hash_n[] - the P2Ps[] index for each uri_hash_ab[].
1667c478bd9Sstevel@tonic-gate  *
1677c478bd9Sstevel@tonic-gate  * uri_hash_sz[] - the size for each uri_hash_ab[].
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * uri_hash_cnt[] - the total uri_desc_t members for each uri_hash_ab[].
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * uri_hash_overflow[] - the uri_hash_cnt[] for each uri_hash_ab[] when
1727c478bd9Sstevel@tonic-gate  * a new uri_hash_ab[] needs to be created.
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * uri_hash_ab[] - the uri_hash_t entries.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * uri_hash_lru[] - the last uri_hash_ab[] walked for lru reclaim.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate typedef struct uri_hash_s {
1807c478bd9Sstevel@tonic-gate 	struct uri_desc_s	*list;		/* List of uri_t(s) */
1817c478bd9Sstevel@tonic-gate 	kmutex_t		lock;
1827c478bd9Sstevel@tonic-gate } uri_hash_t;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate #define	URI_HASH_AVRG	5	/* Desired average hash chain length */
1857c478bd9Sstevel@tonic-gate #define	URI_HASH_N_INIT	9	/* P2Ps[] initial index */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static krwlock_t	uri_hash_access;
1887c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_which = 0;
1897c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_n[2] = {URI_HASH_N_INIT, 0};
1907c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_sz[2] = {0, 0};
1917c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_cnt[2] = {0, 0};
1927c478bd9Sstevel@tonic-gate static uint32_t		uri_hash_overflow[2] = {0, 0};
1937c478bd9Sstevel@tonic-gate static uri_hash_t	*uri_hash_ab[2] = {NULL, NULL};
1947c478bd9Sstevel@tonic-gate static uri_hash_t	*uri_hash_lru[2] = {NULL, NULL};
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Primes for N of 3 - 24 where P is first prime less then (2^(N-1))+(2^(N-2))
1987c478bd9Sstevel@tonic-gate  * these primes have been foud to be useful for prime sized hash tables.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static const int P2Ps[] = {
2027c478bd9Sstevel@tonic-gate 	0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,
2037c478bd9Sstevel@tonic-gate 	6143, 12281, 24571, 49139, 98299, 196597, 393209,
2047c478bd9Sstevel@tonic-gate 	786431, 1572853, 3145721, 6291449, 12582893, 0};
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Hash macros:
2087c478bd9Sstevel@tonic-gate  *
2097c478bd9Sstevel@tonic-gate  *    H2A(char *cp, char *ep, char c) - convert the escaped octet (ASCII)
2107c478bd9Sstevel@tonic-gate  *    hex multichar of the format "%HH" pointeded to by *cp to a char and
2117c478bd9Sstevel@tonic-gate  *    return in c, *ep points to past end of (char *), on return *cp will
2127c478bd9Sstevel@tonic-gate  *    point to the last char consumed.
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  *    URI_HASH(unsigned hix, char *cp, char *ep) - hash the char(s) from
2157c478bd9Sstevel@tonic-gate  *    *cp to *ep to the unsigned hix, cp nor ep are modified.
2167c478bd9Sstevel@tonic-gate  *
2172c9e429eSbrutus  *    URI_HASH_IX(unsigned hix, int which) - convert the hash value hix to
2182c9e429eSbrutus  *    a hash index 0 - (uri_hash_sz[which] - 1).
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  *    URI_HASH_MIGRATE(from, hp, to) - migrate the uri_hash_t *hp list
2217c478bd9Sstevel@tonic-gate  *    uri_desc_t members from hash from to hash to.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  *    URI_HASH_UNLINK(cur, new, hp, puri, uri) - unlink the uri_desc_t
2247c478bd9Sstevel@tonic-gate  *    *uri which is a member of the uri_hash_t *hp list with a previous
2257c478bd9Sstevel@tonic-gate  *    list member of *puri for the uri_hash_ab[] cur. After unlinking
2267c478bd9Sstevel@tonic-gate  *    check for cur hash empty, if so make new cur. Note, as this macro
2277c478bd9Sstevel@tonic-gate  *    can change a hash chain it needs to be run under hash_access as
2287c478bd9Sstevel@tonic-gate  *    RW_WRITER, futher as it can change the new hash to cur any access
2297c478bd9Sstevel@tonic-gate  *    to the hash state must be done after either dropping locks and
2307c478bd9Sstevel@tonic-gate  *    starting over or making sure the global state is consistent after
2317c478bd9Sstevel@tonic-gate  *    as before.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #define	H2A(cp, ep, c) {						\
2357c478bd9Sstevel@tonic-gate 	int	_h = 2;							\
2367c478bd9Sstevel@tonic-gate 	int	_n = 0;							\
2377c478bd9Sstevel@tonic-gate 	char	_hc;							\
2387c478bd9Sstevel@tonic-gate 									\
2397c478bd9Sstevel@tonic-gate 	while (_h > 0 && ++(cp) < (ep)) {				\
2407c478bd9Sstevel@tonic-gate 		if (_h == 1)						\
2417c478bd9Sstevel@tonic-gate 			_n *= 0x10;					\
2427c478bd9Sstevel@tonic-gate 		_hc = *(cp);						\
2437c478bd9Sstevel@tonic-gate 		if (_hc >= '0' && _hc <= '9')				\
2447c478bd9Sstevel@tonic-gate 			_n += _hc - '0';				\
2457c478bd9Sstevel@tonic-gate 		else if (_hc >= 'a' || _hc <= 'f')			\
2467c478bd9Sstevel@tonic-gate 			_n += _hc - 'W';				\
2477c478bd9Sstevel@tonic-gate 		else if (_hc >= 'A' || _hc <= 'F')			\
2487c478bd9Sstevel@tonic-gate 			_n += _hc - '7';				\
2497c478bd9Sstevel@tonic-gate 		_h--;							\
2507c478bd9Sstevel@tonic-gate 	}								\
2517c478bd9Sstevel@tonic-gate 	(c) = _n;							\
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #define	URI_HASH(hv, cp, ep) {						\
2557c478bd9Sstevel@tonic-gate 	char	*_s = (cp);						\
2567c478bd9Sstevel@tonic-gate 	char	_c;							\
2577c478bd9Sstevel@tonic-gate 									\
2587c478bd9Sstevel@tonic-gate 	while (_s < (ep)) {						\
2597c478bd9Sstevel@tonic-gate 		if ((_c = *_s) == '%') {				\
2607c478bd9Sstevel@tonic-gate 			H2A(_s, (ep), _c);				\
2617c478bd9Sstevel@tonic-gate 		}							\
2622c9e429eSbrutus 		CHASH(hv, _c);						\
2637c478bd9Sstevel@tonic-gate 		_s++;							\
2647c478bd9Sstevel@tonic-gate 	}								\
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2672c9e429eSbrutus #define	URI_HASH_IX(hix, which) (hix) = (hix) % (uri_hash_sz[(which)])
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate #define	URI_HASH_MIGRATE(from, hp, to) {				\
2707c478bd9Sstevel@tonic-gate 	uri_desc_t	*_nuri;						\
2717c478bd9Sstevel@tonic-gate 	uint32_t	_nhix;						\
2727c478bd9Sstevel@tonic-gate 	uri_hash_t	*_nhp;						\
2737c478bd9Sstevel@tonic-gate 									\
2747c478bd9Sstevel@tonic-gate 	mutex_enter(&(hp)->lock);					\
2757c478bd9Sstevel@tonic-gate 	while ((_nuri = (hp)->list) != NULL) {				\
2767c478bd9Sstevel@tonic-gate 		(hp)->list = _nuri->hash;				\
2771a5e258fSJosef 'Jeff' Sipek 		atomic_dec_32(&uri_hash_cnt[(from)]);		\
2781a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&uri_hash_cnt[(to)]);			\
2792c9e429eSbrutus 		_nhix = _nuri->hvalue;					\
2802c9e429eSbrutus 		URI_HASH_IX(_nhix, to);					\
2817c478bd9Sstevel@tonic-gate 		_nhp = &uri_hash_ab[(to)][_nhix];			\
2827c478bd9Sstevel@tonic-gate 		mutex_enter(&_nhp->lock);				\
2837c478bd9Sstevel@tonic-gate 		_nuri->hash = _nhp->list;				\
2847c478bd9Sstevel@tonic-gate 		_nhp->list = _nuri;					\
2857c478bd9Sstevel@tonic-gate 		_nuri->hit = 0;						\
2867c478bd9Sstevel@tonic-gate 		mutex_exit(&_nhp->lock);				\
2877c478bd9Sstevel@tonic-gate 	}								\
2887c478bd9Sstevel@tonic-gate 	mutex_exit(&(hp)->lock);					\
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate #define	URI_HASH_UNLINK(cur, new, hp, puri, uri) {			\
2927c478bd9Sstevel@tonic-gate 	if ((puri) != NULL) {						\
2937c478bd9Sstevel@tonic-gate 		(puri)->hash = (uri)->hash;				\
2947c478bd9Sstevel@tonic-gate 	} else {							\
2957c478bd9Sstevel@tonic-gate 		(hp)->list = (uri)->hash;				\
2967c478bd9Sstevel@tonic-gate 	}								\
2971a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&uri_hash_cnt[(cur)]) == 0 &&		\
2987c478bd9Sstevel@tonic-gate 	    uri_hash_ab[(new)] != NULL) {				\
2997c478bd9Sstevel@tonic-gate 		kmem_free(uri_hash_ab[cur],				\
3007c478bd9Sstevel@tonic-gate 		    sizeof (uri_hash_t) * uri_hash_sz[cur]);		\
3017c478bd9Sstevel@tonic-gate 		uri_hash_ab[(cur)] = NULL;				\
3027c478bd9Sstevel@tonic-gate 		uri_hash_lru[(cur)] = NULL;				\
3037c478bd9Sstevel@tonic-gate 		uri_hash_which = (new);					\
3047c478bd9Sstevel@tonic-gate 	} else {							\
3057c478bd9Sstevel@tonic-gate 		uri_hash_lru[(cur)] = (hp);				\
3067c478bd9Sstevel@tonic-gate 	}								\
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate void
nl7c_uri_init(void)3107c478bd9Sstevel@tonic-gate nl7c_uri_init(void)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	uint32_t	cur = uri_hash_which;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	rw_init(&uri_hash_access, NULL, RW_DEFAULT, NULL);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	uri_hash_sz[cur] = P2Ps[URI_HASH_N_INIT];
3177c478bd9Sstevel@tonic-gate 	uri_hash_overflow[cur] = P2Ps[URI_HASH_N_INIT] * URI_HASH_AVRG;
3187c478bd9Sstevel@tonic-gate 	uri_hash_ab[cur] = kmem_zalloc(sizeof (uri_hash_t) * uri_hash_sz[cur],
3197c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
3207c478bd9Sstevel@tonic-gate 	uri_hash_lru[cur] = uri_hash_ab[cur];
3217c478bd9Sstevel@tonic-gate 
3222c9e429eSbrutus 	nl7c_uri_kmc = kmem_cache_create("NL7C_uri_kmc", sizeof (uri_desc_t),
3232c9e429eSbrutus 	    0, NULL, NULL, uri_kmc_reclaim, NULL, NULL, 0);
3247c478bd9Sstevel@tonic-gate 
3252c9e429eSbrutus 	nl7c_uri_rd_kmc = kmem_cache_create("NL7C_uri_rd_kmc",
3262c9e429eSbrutus 	    sizeof (uri_rd_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	uri_desb_kmc = kmem_cache_create("NL7C_uri_desb_kmc",
3297c478bd9Sstevel@tonic-gate 	    sizeof (uri_desb_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	uri_segmap_kmc = kmem_cache_create("NL7C_uri_segmap_kmc",
3327c478bd9Sstevel@tonic-gate 	    sizeof (uri_segmap_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	nl7c_http_init();
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3372c9e429eSbrutus #define	CV_SZ	16
3382c9e429eSbrutus 
3392c9e429eSbrutus void
nl7c_mi_report_hash(mblk_t * mp)3402c9e429eSbrutus nl7c_mi_report_hash(mblk_t *mp)
3412c9e429eSbrutus {
3422c9e429eSbrutus 	uri_hash_t	*hp, *pend;
3432c9e429eSbrutus 	uri_desc_t	*uri;
3442c9e429eSbrutus 	uint32_t	cur;
3452c9e429eSbrutus 	uint32_t	new;
3462c9e429eSbrutus 	int		n, nz, tot;
3472c9e429eSbrutus 	uint32_t	cv[CV_SZ + 1];
3482c9e429eSbrutus 
3492c9e429eSbrutus 	rw_enter(&uri_hash_access, RW_READER);
3502c9e429eSbrutus 	cur = uri_hash_which;
3512c9e429eSbrutus 	new = cur ? 0 : 1;
3522c9e429eSbrutus next:
3532c9e429eSbrutus 	for (n = 0; n <= CV_SZ; n++)
3542c9e429eSbrutus 		cv[n] = 0;
3552c9e429eSbrutus 	nz = 0;
3562c9e429eSbrutus 	tot = 0;
3572c9e429eSbrutus 	hp = &uri_hash_ab[cur][0];
3582c9e429eSbrutus 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
3592c9e429eSbrutus 	while (hp < pend) {
3602c9e429eSbrutus 		n = 0;
3612c9e429eSbrutus 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
3622c9e429eSbrutus 			n++;
3632c9e429eSbrutus 		}
3642c9e429eSbrutus 		tot += n;
3652c9e429eSbrutus 		if (n > 0)
3662c9e429eSbrutus 			nz++;
3672c9e429eSbrutus 		if (n > CV_SZ)
3682c9e429eSbrutus 			n = CV_SZ;
3692c9e429eSbrutus 		cv[n]++;
3702c9e429eSbrutus 		hp++;
3712c9e429eSbrutus 	}
3722c9e429eSbrutus 
3732c9e429eSbrutus 	(void) mi_mpprintf(mp, "\nHash=%s, Buckets=%d, "
3742c9e429eSbrutus 	    "Avrg=%d\nCount by bucket:", cur != new ? "CUR" : "NEW",
3752c9e429eSbrutus 	    uri_hash_sz[cur], nz != 0 ? ((tot * 10 + 5) / nz) / 10 : 0);
3762c9e429eSbrutus 	(void) mi_mpprintf(mp, "Free=%d", cv[0]);
3772c9e429eSbrutus 	for (n = 1; n < CV_SZ; n++) {
3782c9e429eSbrutus 		int	pn = 0;
3792c9e429eSbrutus 		char	pv[5];
3802c9e429eSbrutus 		char	*pp = pv;
3812c9e429eSbrutus 
3822c9e429eSbrutus 		for (pn = n; pn < 1000; pn *= 10)
3832c9e429eSbrutus 			*pp++ = ' ';
3842c9e429eSbrutus 		*pp = 0;
3852c9e429eSbrutus 		(void) mi_mpprintf(mp, "%s%d=%d", pv, n, cv[n]);
3862c9e429eSbrutus 	}
3872c9e429eSbrutus 	(void) mi_mpprintf(mp, "Long=%d", cv[CV_SZ]);
3882c9e429eSbrutus 
3892c9e429eSbrutus 	if (cur != new && uri_hash_ab[new] != NULL) {
3902c9e429eSbrutus 		cur = new;
3912c9e429eSbrutus 		goto next;
3922c9e429eSbrutus 	}
3932c9e429eSbrutus 	rw_exit(&uri_hash_access);
3942c9e429eSbrutus }
3952c9e429eSbrutus 
3962c9e429eSbrutus void
nl7c_mi_report_uri(mblk_t * mp)3972c9e429eSbrutus nl7c_mi_report_uri(mblk_t *mp)
3982c9e429eSbrutus {
3992c9e429eSbrutus 	uri_hash_t	*hp;
4002c9e429eSbrutus 	uri_desc_t	*uri;
4012c9e429eSbrutus 	uint32_t	cur;
4022c9e429eSbrutus 	uint32_t	new;
4032c9e429eSbrutus 	int		ix;
4042c9e429eSbrutus 	int		ret;
4052c9e429eSbrutus 	char		sc;
4062c9e429eSbrutus 
4072c9e429eSbrutus 	rw_enter(&uri_hash_access, RW_READER);
4082c9e429eSbrutus 	cur = uri_hash_which;
4092c9e429eSbrutus 	new = cur ? 0 : 1;
4102c9e429eSbrutus next:
4112c9e429eSbrutus 	for (ix = 0; ix < uri_hash_sz[cur]; ix++) {
4122c9e429eSbrutus 		hp = &uri_hash_ab[cur][ix];
4132c9e429eSbrutus 		mutex_enter(&hp->lock);
4142c9e429eSbrutus 		uri = hp->list;
4152c9e429eSbrutus 		while (uri != NULL) {
4162c9e429eSbrutus 			sc = *(uri->path.ep);
4172c9e429eSbrutus 			*(uri->path.ep) = 0;
4182c9e429eSbrutus 			ret = mi_mpprintf(mp, "%s: %d %d %d",
4192c9e429eSbrutus 			    uri->path.cp, (int)uri->resplen,
4202c9e429eSbrutus 			    (int)uri->respclen, (int)uri->count);
4212c9e429eSbrutus 			*(uri->path.ep) = sc;
4222c9e429eSbrutus 			if (ret == -1) break;
4232c9e429eSbrutus 			uri = uri->hash;
4242c9e429eSbrutus 		}
4252c9e429eSbrutus 		mutex_exit(&hp->lock);
4262c9e429eSbrutus 		if (ret == -1) break;
4272c9e429eSbrutus 	}
4282c9e429eSbrutus 	if (ret != -1 && cur != new && uri_hash_ab[new] != NULL) {
4292c9e429eSbrutus 		cur = new;
4302c9e429eSbrutus 		goto next;
4312c9e429eSbrutus 	}
4322c9e429eSbrutus 	rw_exit(&uri_hash_access);
4332c9e429eSbrutus }
4342c9e429eSbrutus 
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * The uri_desc_t ref_t inactive function called on the last REF_RELE(),
4377c478bd9Sstevel@tonic-gate  * free all resources contained in the uri_desc_t. Note, the uri_desc_t
4387c478bd9Sstevel@tonic-gate  * will be freed by REF_RELE() on return.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate 
4412c9e429eSbrutus void
nl7c_uri_inactive(uri_desc_t * uri)4422c9e429eSbrutus nl7c_uri_inactive(uri_desc_t *uri)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int64_t	 bytes = 0;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (uri->tail) {
4477c478bd9Sstevel@tonic-gate 		uri_rd_t *rdp = &uri->response;
4487c478bd9Sstevel@tonic-gate 		uri_rd_t *free = NULL;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		while (rdp) {
4517c478bd9Sstevel@tonic-gate 			if (rdp->off == -1) {
4527c478bd9Sstevel@tonic-gate 				bytes += rdp->sz;
4537c478bd9Sstevel@tonic-gate 				kmem_free(rdp->data.kmem, rdp->sz);
4547c478bd9Sstevel@tonic-gate 			} else {
4557c478bd9Sstevel@tonic-gate 				VN_RELE(rdp->data.vnode);
4567c478bd9Sstevel@tonic-gate 			}
4577c478bd9Sstevel@tonic-gate 			rdp = rdp->next;
4587c478bd9Sstevel@tonic-gate 			if (free != NULL) {
4592c9e429eSbrutus 				kmem_cache_free(nl7c_uri_rd_kmc, free);
4607c478bd9Sstevel@tonic-gate 			}
4617c478bd9Sstevel@tonic-gate 			free = rdp;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	if (bytes) {
4657c478bd9Sstevel@tonic-gate 		atomic_add_64(&nl7c_uri_bytes, -bytes);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	if (uri->scheme != NULL) {
4687c478bd9Sstevel@tonic-gate 		nl7c_http_free(uri->scheme);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	if (uri->reqmp) {
4717c478bd9Sstevel@tonic-gate 		freeb(uri->reqmp);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * The reclaim is called by the kmem subsystem when kmem is running
4777c478bd9Sstevel@tonic-gate  * low. More work is needed to determine the best reclaim policy, for
4787c478bd9Sstevel@tonic-gate  * now we just manipulate the nl7c_uri_max global maximum bytes threshold
4797c478bd9Sstevel@tonic-gate  * value using a simple arithmetic backoff of the value every time this
4807c478bd9Sstevel@tonic-gate  * function is called then call uri_reclaim() to enforce it.
4817c478bd9Sstevel@tonic-gate  *
4827c478bd9Sstevel@tonic-gate  * Note, this value remains in place and enforced for all subsequent
4837c478bd9Sstevel@tonic-gate  * URI request/response processing.
4847c478bd9Sstevel@tonic-gate  *
4857c478bd9Sstevel@tonic-gate  * Note, nl7c_uri_max is currently initialized to 0 or infinite such that
4867c478bd9Sstevel@tonic-gate  * the first call here set it to the current uri_bytes value then backoff
4877c478bd9Sstevel@tonic-gate  * from there.
4887c478bd9Sstevel@tonic-gate  *
4897c478bd9Sstevel@tonic-gate  * XXX how do we determine when to increase nl7c_uri_max ???
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4937c478bd9Sstevel@tonic-gate static void
uri_kmc_reclaim(void * arg)4947c478bd9Sstevel@tonic-gate uri_kmc_reclaim(void *arg)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	uint64_t new_max;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if ((new_max = nl7c_uri_max) == 0) {
4997c478bd9Sstevel@tonic-gate 		/* Currently infinite, initialize to current bytes used */
5007c478bd9Sstevel@tonic-gate 		nl7c_uri_max = nl7c_uri_bytes;
5017c478bd9Sstevel@tonic-gate 		new_max = nl7c_uri_bytes;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	if (new_max > 1) {
5047c478bd9Sstevel@tonic-gate 		/* Lower max_bytes to 93% of current value */
5057c478bd9Sstevel@tonic-gate 		new_max >>= 1;			/* 50% */
5067c478bd9Sstevel@tonic-gate 		new_max += (new_max >> 1);	/* 75% */
5077c478bd9Sstevel@tonic-gate 		new_max += (new_max >> 2);	/* 93% */
5087c478bd9Sstevel@tonic-gate 		if (new_max < nl7c_uri_max)
5097c478bd9Sstevel@tonic-gate 			nl7c_uri_max = new_max;
5107c478bd9Sstevel@tonic-gate 		else
5117c478bd9Sstevel@tonic-gate 			nl7c_uri_max = 1;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	nl7c_uri_reclaim();
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  * Delete a uri_desc_t from the URI hash.
5187c478bd9Sstevel@tonic-gate  */
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static void
uri_delete(uri_desc_t * del)5217c478bd9Sstevel@tonic-gate uri_delete(uri_desc_t *del)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	uint32_t	hix;
5247c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp;
5257c478bd9Sstevel@tonic-gate 	uri_desc_t	*uri;
5267c478bd9Sstevel@tonic-gate 	uri_desc_t	*puri;
5277c478bd9Sstevel@tonic-gate 	uint32_t	cur;
5287c478bd9Sstevel@tonic-gate 	uint32_t	new;
5297c478bd9Sstevel@tonic-gate 
5302c9e429eSbrutus 	ASSERT(del->hash != URI_TEMP);
5317c478bd9Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
5327c478bd9Sstevel@tonic-gate 	cur = uri_hash_which;
5337c478bd9Sstevel@tonic-gate 	new = cur ? 0 : 1;
5347c478bd9Sstevel@tonic-gate next:
5357c478bd9Sstevel@tonic-gate 	puri = NULL;
5362c9e429eSbrutus 	hix = del->hvalue;
5372c9e429eSbrutus 	URI_HASH_IX(hix, cur);
5387c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
5397c478bd9Sstevel@tonic-gate 	for (uri = hp->list; uri != NULL; uri = uri->hash) {
5407c478bd9Sstevel@tonic-gate 		if (uri != del) {
5417c478bd9Sstevel@tonic-gate 			puri = uri;
5427c478bd9Sstevel@tonic-gate 			continue;
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 		/*
5457c478bd9Sstevel@tonic-gate 		 * Found the URI, unlink from the hash chain,
5467c478bd9Sstevel@tonic-gate 		 * drop locks, ref release it.
5477c478bd9Sstevel@tonic-gate 		 */
5487c478bd9Sstevel@tonic-gate 		URI_HASH_UNLINK(cur, new, hp, puri, uri);
5497c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
5507c478bd9Sstevel@tonic-gate 		REF_RELE(uri);
5517c478bd9Sstevel@tonic-gate 		return;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
5567c478bd9Sstevel@tonic-gate 		 * check the new hash next.
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		cur = new;
5597c478bd9Sstevel@tonic-gate 		goto next;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Add a uri_desc_t to the URI hash.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate static void
uri_add(uri_desc_t * uri,krw_t rwlock,boolean_t nonblocking)5697c478bd9Sstevel@tonic-gate uri_add(uri_desc_t *uri, krw_t rwlock, boolean_t nonblocking)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	uint32_t	hix;
5727c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp;
5737c478bd9Sstevel@tonic-gate 	uint32_t	cur = uri_hash_which;
5747c478bd9Sstevel@tonic-gate 	uint32_t	new = cur ? 0 : 1;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * Caller of uri_add() must hold the uri_hash_access rwlock.
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	ASSERT((rwlock == RW_READER && RW_READ_HELD(&uri_hash_access)) ||
5807c478bd9Sstevel@tonic-gate 	    (rwlock == RW_WRITER && RW_WRITE_HELD(&uri_hash_access)));
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * uri_add() always succeeds so add a hash ref to the URI now.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	REF_HOLD(uri);
5857c478bd9Sstevel@tonic-gate again:
5862c9e429eSbrutus 	hix = uri->hvalue;
5872c9e429eSbrutus 	URI_HASH_IX(hix, cur);
5887c478bd9Sstevel@tonic-gate 	if (uri_hash_ab[new] == NULL &&
5897c478bd9Sstevel@tonic-gate 	    uri_hash_cnt[cur] < uri_hash_overflow[cur]) {
5907c478bd9Sstevel@tonic-gate 		/*
5917c478bd9Sstevel@tonic-gate 		 * Easy case, no new hash and current hasn't overflowed,
5927c478bd9Sstevel@tonic-gate 		 * add URI to current hash and return.
5937c478bd9Sstevel@tonic-gate 		 *
5947c478bd9Sstevel@tonic-gate 		 * Note, the check for uri_hash_cnt[] above aren't done
5957c478bd9Sstevel@tonic-gate 		 * atomictally, i.e. multiple threads can be in this code
5967c478bd9Sstevel@tonic-gate 		 * as RW_READER and update the cnt[], this isn't a problem
5977c478bd9Sstevel@tonic-gate 		 * as the check is only advisory.
5987c478bd9Sstevel@tonic-gate 		 */
5997c478bd9Sstevel@tonic-gate 	fast:
6001a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&uri_hash_cnt[cur]);
6017c478bd9Sstevel@tonic-gate 		hp = &uri_hash_ab[cur][hix];
6027c478bd9Sstevel@tonic-gate 		mutex_enter(&hp->lock);
6037c478bd9Sstevel@tonic-gate 		uri->hash = hp->list;
6047c478bd9Sstevel@tonic-gate 		hp->list = uri;
6057c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->lock);
6067c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
6077c478bd9Sstevel@tonic-gate 		return;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	if (uri_hash_ab[new] == NULL) {
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * Need a new a or b hash, if not already RW_WRITER
6127c478bd9Sstevel@tonic-gate 		 * try to upgrade our lock to writer.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		if (rwlock != RW_WRITER && ! rw_tryupgrade(&uri_hash_access)) {
6157c478bd9Sstevel@tonic-gate 			/*
6167c478bd9Sstevel@tonic-gate 			 * Upgrade failed, we can't simple exit and reenter
6177c478bd9Sstevel@tonic-gate 			 * the lock as after the exit and before the reenter
6187c478bd9Sstevel@tonic-gate 			 * the whole world can change so just wait for writer
6197c478bd9Sstevel@tonic-gate 			 * then do everything again.
6207c478bd9Sstevel@tonic-gate 			 */
6217c478bd9Sstevel@tonic-gate 			if (nonblocking) {
6227c478bd9Sstevel@tonic-gate 				/*
6237c478bd9Sstevel@tonic-gate 				 * Can't block, use fast-path above.
6247c478bd9Sstevel@tonic-gate 				 *
6257c478bd9Sstevel@tonic-gate 				 * XXX should have a background thread to
6267c478bd9Sstevel@tonic-gate 				 * handle new ab[] in this case so as to
6277c478bd9Sstevel@tonic-gate 				 * not overflow the cur hash to much.
6287c478bd9Sstevel@tonic-gate 				 */
6297c478bd9Sstevel@tonic-gate 				goto fast;
6307c478bd9Sstevel@tonic-gate 			}
6317c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
6327c478bd9Sstevel@tonic-gate 			rwlock = RW_WRITER;
6337c478bd9Sstevel@tonic-gate 			rw_enter(&uri_hash_access, rwlock);
6347c478bd9Sstevel@tonic-gate 			cur = uri_hash_which;
6357c478bd9Sstevel@tonic-gate 			new = cur ? 0 : 1;
6367c478bd9Sstevel@tonic-gate 			goto again;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 		rwlock = RW_WRITER;
6397c478bd9Sstevel@tonic-gate 		if (uri_hash_ab[new] == NULL) {
6407c478bd9Sstevel@tonic-gate 			/*
6417c478bd9Sstevel@tonic-gate 			 * Still need a new hash, allocate and initialize
6427c478bd9Sstevel@tonic-gate 			 * the new hash.
6437c478bd9Sstevel@tonic-gate 			 */
6447c478bd9Sstevel@tonic-gate 			uri_hash_n[new] = uri_hash_n[cur] + 1;
6457c478bd9Sstevel@tonic-gate 			if (uri_hash_n[new] == 0) {
6467c478bd9Sstevel@tonic-gate 				/*
6477c478bd9Sstevel@tonic-gate 				 * No larger P2Ps[] value so use current,
6487c478bd9Sstevel@tonic-gate 				 * i.e. 2 of the largest are better than 1 ?
6497c478bd9Sstevel@tonic-gate 				 */
6507c478bd9Sstevel@tonic-gate 				uri_hash_n[new] = uri_hash_n[cur];
6517c478bd9Sstevel@tonic-gate 				cmn_err(CE_NOTE, "NL7C: hash index overflow");
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			uri_hash_sz[new] = P2Ps[uri_hash_n[new]];
6547c478bd9Sstevel@tonic-gate 			ASSERT(uri_hash_cnt[new] == 0);
6557c478bd9Sstevel@tonic-gate 			uri_hash_overflow[new] = uri_hash_sz[new] *
6567c478bd9Sstevel@tonic-gate 			    URI_HASH_AVRG;
6577c478bd9Sstevel@tonic-gate 			uri_hash_ab[new] = kmem_zalloc(sizeof (uri_hash_t) *
6587c478bd9Sstevel@tonic-gate 			    uri_hash_sz[new], nonblocking ? KM_NOSLEEP :
6597c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6607c478bd9Sstevel@tonic-gate 			if (uri_hash_ab[new] == NULL) {
6617c478bd9Sstevel@tonic-gate 				/*
6627c478bd9Sstevel@tonic-gate 				 * Alloc failed, use fast-path above.
6637c478bd9Sstevel@tonic-gate 				 *
6647c478bd9Sstevel@tonic-gate 				 * XXX should have a background thread to
6657c478bd9Sstevel@tonic-gate 				 * handle new ab[] in this case so as to
6667c478bd9Sstevel@tonic-gate 				 * not overflow the cur hash to much.
6677c478bd9Sstevel@tonic-gate 				 */
6687c478bd9Sstevel@tonic-gate 				goto fast;
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 			uri_hash_lru[new] = uri_hash_ab[new];
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Hashed against current hash so migrate any current hash chain
6757c478bd9Sstevel@tonic-gate 	 * members, if any.
6767c478bd9Sstevel@tonic-gate 	 *
6777c478bd9Sstevel@tonic-gate 	 * Note, the hash chain list can be checked for a non empty list
6787c478bd9Sstevel@tonic-gate 	 * outside of the hash chain list lock as the hash chain struct
6797c478bd9Sstevel@tonic-gate 	 * can't be destroyed while in the uri_hash_access rwlock, worst
6807c478bd9Sstevel@tonic-gate 	 * case is that a non empty list is found and after acquiring the
6817c478bd9Sstevel@tonic-gate 	 * lock another thread beats us to it (i.e. migrated the list).
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
6847c478bd9Sstevel@tonic-gate 	if (hp->list != NULL) {
6857c478bd9Sstevel@tonic-gate 		URI_HASH_MIGRATE(cur, hp, new);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	/*
6887c478bd9Sstevel@tonic-gate 	 * If new hash has overflowed before current hash has been
6897c478bd9Sstevel@tonic-gate 	 * completely migrated then walk all current hash chains and
6907c478bd9Sstevel@tonic-gate 	 * migrate list members now.
6917c478bd9Sstevel@tonic-gate 	 */
6921a5e258fSJosef 'Jeff' Sipek 	if (atomic_inc_32_nv(&uri_hash_cnt[new]) >= uri_hash_overflow[new]) {
6937c478bd9Sstevel@tonic-gate 		for (hix = 0; hix < uri_hash_sz[cur]; hix++) {
6947c478bd9Sstevel@tonic-gate 			hp = &uri_hash_ab[cur][hix];
6957c478bd9Sstevel@tonic-gate 			if (hp->list != NULL) {
6967c478bd9Sstevel@tonic-gate 				URI_HASH_MIGRATE(cur, hp, new);
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Add URI to new hash.
7027c478bd9Sstevel@tonic-gate 	 */
7032c9e429eSbrutus 	hix = uri->hvalue;
7042c9e429eSbrutus 	URI_HASH_IX(hix, new);
7057c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[new][hix];
7067c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->lock);
7077c478bd9Sstevel@tonic-gate 	uri->hash = hp->list;
7087c478bd9Sstevel@tonic-gate 	hp->list = uri;
7097c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->lock);
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Last, check to see if last cur hash chain has been
7127c478bd9Sstevel@tonic-gate 	 * migrated, if so free cur hash and make new hash cur.
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (uri_hash_cnt[cur] == 0) {
7157c478bd9Sstevel@tonic-gate 		/*
7167c478bd9Sstevel@tonic-gate 		 * If we don't already hold the uri_hash_access rwlock for
7177c478bd9Sstevel@tonic-gate 		 * RW_WRITE try to upgrade to RW_WRITE and if successful
7187c478bd9Sstevel@tonic-gate 		 * check again and to see if still need to do the free.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		if ((rwlock == RW_WRITER || rw_tryupgrade(&uri_hash_access)) &&
7217c478bd9Sstevel@tonic-gate 		    uri_hash_cnt[cur] == 0 && uri_hash_ab[new] != 0) {
7227c478bd9Sstevel@tonic-gate 			kmem_free(uri_hash_ab[cur],
7237c478bd9Sstevel@tonic-gate 			    sizeof (uri_hash_t) * uri_hash_sz[cur]);
7247c478bd9Sstevel@tonic-gate 			uri_hash_ab[cur] = NULL;
7257c478bd9Sstevel@tonic-gate 			uri_hash_lru[cur] = NULL;
7267c478bd9Sstevel@tonic-gate 			uri_hash_which = new;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate /*
7337c478bd9Sstevel@tonic-gate  * Lookup a uri_desc_t in the URI hash, if found free the request uri_desc_t
7342c9e429eSbrutus  * and return the found uri_desc_t with a REF_HOLD() placed on it. Else, if
7352c9e429eSbrutus  * add B_TRUE use the request URI to create a new hash entry. Else if add
7362c9e429eSbrutus  * B_FALSE ...
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate static uri_desc_t *
uri_lookup(uri_desc_t * ruri,boolean_t add,boolean_t nonblocking)7407c478bd9Sstevel@tonic-gate uri_lookup(uri_desc_t *ruri, boolean_t add, boolean_t nonblocking)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	uint32_t	hix;
7437c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp;
7447c478bd9Sstevel@tonic-gate 	uri_desc_t	*uri;
7457c478bd9Sstevel@tonic-gate 	uri_desc_t	*puri;
7467c478bd9Sstevel@tonic-gate 	uint32_t	cur;
7477c478bd9Sstevel@tonic-gate 	uint32_t	new;
7487c478bd9Sstevel@tonic-gate 	char		*rcp = ruri->path.cp;
7497c478bd9Sstevel@tonic-gate 	char		*rep = ruri->path.ep;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate again:
7527c478bd9Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_READER);
7537c478bd9Sstevel@tonic-gate 	cur = uri_hash_which;
7547c478bd9Sstevel@tonic-gate 	new = cur ? 0 : 1;
7557c478bd9Sstevel@tonic-gate nexthash:
7567c478bd9Sstevel@tonic-gate 	puri = NULL;
7572c9e429eSbrutus 	hix = ruri->hvalue;
7582c9e429eSbrutus 	URI_HASH_IX(hix, cur);
7597c478bd9Sstevel@tonic-gate 	hp = &uri_hash_ab[cur][hix];
7607c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->lock);
7617c478bd9Sstevel@tonic-gate 	for (uri = hp->list; uri != NULL; uri = uri->hash) {
7627c478bd9Sstevel@tonic-gate 		char	*ap = uri->path.cp;
7637c478bd9Sstevel@tonic-gate 		char	*bp = rcp;
7647c478bd9Sstevel@tonic-gate 		char	a, b;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		/* Compare paths */
7677c478bd9Sstevel@tonic-gate 		while (bp < rep && ap < uri->path.ep) {
7687c478bd9Sstevel@tonic-gate 			if ((a = *ap) == '%') {
7697c478bd9Sstevel@tonic-gate 				/* Escaped hex multichar, convert it */
7707c478bd9Sstevel@tonic-gate 				H2A(ap, uri->path.ep, a);
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 			if ((b = *bp) == '%') {
7737c478bd9Sstevel@tonic-gate 				/* Escaped hex multichar, convert it */
7747c478bd9Sstevel@tonic-gate 				H2A(bp, rep, b);
7757c478bd9Sstevel@tonic-gate 			}
7767c478bd9Sstevel@tonic-gate 			if (a != b) {
7777c478bd9Sstevel@tonic-gate 				/* Char's don't match */
7787c478bd9Sstevel@tonic-gate 				goto nexturi;
7797c478bd9Sstevel@tonic-gate 			}
7807c478bd9Sstevel@tonic-gate 			ap++;
7817c478bd9Sstevel@tonic-gate 			bp++;
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 		if (bp != rep || ap != uri->path.ep) {
7847c478bd9Sstevel@tonic-gate 			/* Not same length */
7857c478bd9Sstevel@tonic-gate 			goto nexturi;
7867c478bd9Sstevel@tonic-gate 		}
7877c478bd9Sstevel@tonic-gate 		ap = uri->auth.cp;
7887c478bd9Sstevel@tonic-gate 		bp = ruri->auth.cp;
7897c478bd9Sstevel@tonic-gate 		if (ap != NULL) {
7907c478bd9Sstevel@tonic-gate 			if (bp == NULL) {
7917c478bd9Sstevel@tonic-gate 				/* URI has auth request URI doesn't */
7927c478bd9Sstevel@tonic-gate 				goto nexturi;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 			while (bp < ruri->auth.ep && ap < uri->auth.ep) {
7957c478bd9Sstevel@tonic-gate 				if ((a = *ap) == '%') {
7967c478bd9Sstevel@tonic-gate 					/* Escaped hex multichar, convert it */
7977c478bd9Sstevel@tonic-gate 					H2A(ap, uri->path.ep, a);
7987c478bd9Sstevel@tonic-gate 				}
7997c478bd9Sstevel@tonic-gate 				if ((b = *bp) == '%') {
8007c478bd9Sstevel@tonic-gate 					/* Escaped hex multichar, convert it */
8017c478bd9Sstevel@tonic-gate 					H2A(bp, rep, b);
8027c478bd9Sstevel@tonic-gate 				}
8037c478bd9Sstevel@tonic-gate 				if (a != b) {
8047c478bd9Sstevel@tonic-gate 					/* Char's don't match */
8057c478bd9Sstevel@tonic-gate 					goto nexturi;
8067c478bd9Sstevel@tonic-gate 				}
8077c478bd9Sstevel@tonic-gate 				ap++;
8087c478bd9Sstevel@tonic-gate 				bp++;
8097c478bd9Sstevel@tonic-gate 			}
8107c478bd9Sstevel@tonic-gate 			if (bp != ruri->auth.ep || ap != uri->auth.ep) {
8117c478bd9Sstevel@tonic-gate 				/* Not same length */
8127c478bd9Sstevel@tonic-gate 				goto nexturi;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		} else if (bp != NULL) {
8157c478bd9Sstevel@tonic-gate 			/* URI doesn't have auth and request URI does */
8167c478bd9Sstevel@tonic-gate 			goto nexturi;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		/*
8192c9e429eSbrutus 		 * Have a path/auth match so before any other processing
8202c9e429eSbrutus 		 * of requested URI, check for expire or request no cache
8212c9e429eSbrutus 		 * purge.
8227c478bd9Sstevel@tonic-gate 		 */
823d3d50737SRafael Vanoni 		if (uri->expire >= 0 && uri->expire <= ddi_get_lbolt() ||
824d3d50737SRafael Vanoni 		    ruri->nocache) {
8257c478bd9Sstevel@tonic-gate 			/*
8267c478bd9Sstevel@tonic-gate 			 * URI has expired or request specified to not use
8277c478bd9Sstevel@tonic-gate 			 * the cached version, unlink the URI from the hash
8287c478bd9Sstevel@tonic-gate 			 * chain, release all locks, release the hash ref
8297c478bd9Sstevel@tonic-gate 			 * on the URI, and last look it up again.
8302c9e429eSbrutus 			 *
8312c9e429eSbrutus 			 * Note, this will cause all variants of the named
8322c9e429eSbrutus 			 * URI to be purged.
8337c478bd9Sstevel@tonic-gate 			 */
8347c478bd9Sstevel@tonic-gate 			if (puri != NULL) {
8357c478bd9Sstevel@tonic-gate 				puri->hash = uri->hash;
8367c478bd9Sstevel@tonic-gate 			} else {
8377c478bd9Sstevel@tonic-gate 				hp->list = uri->hash;
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8401a5e258fSJosef 'Jeff' Sipek 			atomic_dec_32(&uri_hash_cnt[cur]);
8417c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8422c9e429eSbrutus 			if (ruri->nocache)
8432c9e429eSbrutus 				nl7c_uri_purge++;
8442c9e429eSbrutus 			else
8452c9e429eSbrutus 				nl7c_uri_expire++;
8467c478bd9Sstevel@tonic-gate 			REF_RELE(uri);
8477c478bd9Sstevel@tonic-gate 			goto again;
8487c478bd9Sstevel@tonic-gate 		}
8492c9e429eSbrutus 		if (uri->scheme != NULL) {
8502c9e429eSbrutus 			/*
8512c9e429eSbrutus 			 * URI has scheme private qualifier(s), if request
8522c9e429eSbrutus 			 * URI doesn't or if no match skip this URI.
8532c9e429eSbrutus 			 */
8542c9e429eSbrutus 			if (ruri->scheme == NULL ||
8552c9e429eSbrutus 			    ! nl7c_http_cmp(uri->scheme, ruri->scheme))
8562c9e429eSbrutus 				goto nexturi;
8572c9e429eSbrutus 		} else if (ruri->scheme != NULL) {
8582c9e429eSbrutus 			/*
8592c9e429eSbrutus 			 * URI doesn't have scheme private qualifiers but
8602c9e429eSbrutus 			 * request URI does, no match, skip this URI.
8612c9e429eSbrutus 			 */
8622c9e429eSbrutus 			goto nexturi;
8632c9e429eSbrutus 		}
8647c478bd9Sstevel@tonic-gate 		/*
8652c9e429eSbrutus 		 * Have a match, ready URI for return, first put a reference
8662c9e429eSbrutus 		 * hold on the URI, if this URI is currently being processed
8672c9e429eSbrutus 		 * then have to wait for the processing to be completed and
8682c9e429eSbrutus 		 * redo the lookup, else return it.
8697c478bd9Sstevel@tonic-gate 		 */
8707c478bd9Sstevel@tonic-gate 		REF_HOLD(uri);
8717c478bd9Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
8727c478bd9Sstevel@tonic-gate 		if (uri->proc != NULL) {
8737c478bd9Sstevel@tonic-gate 			/* The URI is being processed, wait for completion */
8747c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->lock);
8757c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
8767c478bd9Sstevel@tonic-gate 			if (! nonblocking &&
8777c478bd9Sstevel@tonic-gate 			    cv_wait_sig(&uri->waiting, &uri->proclock)) {
8787c478bd9Sstevel@tonic-gate 				/*
8797c478bd9Sstevel@tonic-gate 				 * URI has been processed but things may
8807c478bd9Sstevel@tonic-gate 				 * have changed while we were away so do
8817c478bd9Sstevel@tonic-gate 				 * most everything again.
8827c478bd9Sstevel@tonic-gate 				 */
8837c478bd9Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8847c478bd9Sstevel@tonic-gate 				REF_RELE(uri);
8857c478bd9Sstevel@tonic-gate 				goto again;
8867c478bd9Sstevel@tonic-gate 			} else {
8877c478bd9Sstevel@tonic-gate 				/*
8887c478bd9Sstevel@tonic-gate 				 * A nonblocking socket or an interrupted
8897c478bd9Sstevel@tonic-gate 				 * cv_wait_sig() in the first case can't
8907c478bd9Sstevel@tonic-gate 				 * block waiting for the processing of the
8917c478bd9Sstevel@tonic-gate 				 * uri hash hit uri to complete, in both
8927c478bd9Sstevel@tonic-gate 				 * cases just return failure to lookup.
8937c478bd9Sstevel@tonic-gate 				 */
8947c478bd9Sstevel@tonic-gate 				mutex_exit(&uri->proclock);
8957c478bd9Sstevel@tonic-gate 				REF_RELE(uri);
8967c478bd9Sstevel@tonic-gate 				return (NULL);
8977c478bd9Sstevel@tonic-gate 			}
8987c478bd9Sstevel@tonic-gate 		}
8992c9e429eSbrutus 		mutex_exit(&uri->proclock);
9007c478bd9Sstevel@tonic-gate 		uri->hit++;
9017c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->lock);
9027c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9037c478bd9Sstevel@tonic-gate 		return (uri);
9047c478bd9Sstevel@tonic-gate 	nexturi:
9057c478bd9Sstevel@tonic-gate 		puri = uri;
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->lock);
9087c478bd9Sstevel@tonic-gate 	if (cur != new && uri_hash_ab[new] != NULL) {
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * Not found in current hash and have a new hash so
9117c478bd9Sstevel@tonic-gate 		 * check the new hash next.
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		cur = new;
9147c478bd9Sstevel@tonic-gate 		goto nexthash;
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate add:
9177c478bd9Sstevel@tonic-gate 	if (! add) {
9182c9e429eSbrutus 		/* Lookup only so return failure */
9197c478bd9Sstevel@tonic-gate 		rw_exit(&uri_hash_access);
9207c478bd9Sstevel@tonic-gate 		return (NULL);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	/*
9237c478bd9Sstevel@tonic-gate 	 * URI not hashed, finish intialization of the
9247c478bd9Sstevel@tonic-gate 	 * request URI, add it to the hash, return it.
9257c478bd9Sstevel@tonic-gate 	 */
9267c478bd9Sstevel@tonic-gate 	ruri->hit = 0;
9277c478bd9Sstevel@tonic-gate 	ruri->expire = -1;
9287c478bd9Sstevel@tonic-gate 	ruri->response.sz = 0;
929*e9f74ea5SToomas Soome 	ruri->proc = (struct sonode *)~0;
9307c478bd9Sstevel@tonic-gate 	cv_init(&ruri->waiting, NULL, CV_DEFAULT, NULL);
9317c478bd9Sstevel@tonic-gate 	mutex_init(&ruri->proclock, NULL, MUTEX_DEFAULT, NULL);
9327c478bd9Sstevel@tonic-gate 	uri_add(ruri, RW_READER, nonblocking);
9337c478bd9Sstevel@tonic-gate 	/* uri_add() has done rw_exit(&uri_hash_access) */
9347c478bd9Sstevel@tonic-gate 	return (ruri);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Reclaim URIs until max cache size threshold has been reached.
9397c478bd9Sstevel@tonic-gate  *
9407c478bd9Sstevel@tonic-gate  * A CLOCK based reclaim modified with a history (hit counter) counter.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate static void
nl7c_uri_reclaim(void)9447c478bd9Sstevel@tonic-gate nl7c_uri_reclaim(void)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	uri_hash_t	*hp, *start, *pend;
9477c478bd9Sstevel@tonic-gate 	uri_desc_t	*uri;
9487c478bd9Sstevel@tonic-gate 	uri_desc_t	*puri;
9497c478bd9Sstevel@tonic-gate 	uint32_t	cur;
9507c478bd9Sstevel@tonic-gate 	uint32_t	new;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	nl7c_uri_reclaim_calls++;
9537c478bd9Sstevel@tonic-gate again:
9547c478bd9Sstevel@tonic-gate 	rw_enter(&uri_hash_access, RW_WRITER);
9557c478bd9Sstevel@tonic-gate 	cur = uri_hash_which;
9567c478bd9Sstevel@tonic-gate 	new = cur ? 0 : 1;
9577c478bd9Sstevel@tonic-gate next:
9587c478bd9Sstevel@tonic-gate 	hp = uri_hash_lru[cur];
9597c478bd9Sstevel@tonic-gate 	start = hp;
9607c478bd9Sstevel@tonic-gate 	pend = &uri_hash_ab[cur][uri_hash_sz[cur]];
9617c478bd9Sstevel@tonic-gate 	while (nl7c_uri_bytes > nl7c_uri_max) {
9627c478bd9Sstevel@tonic-gate 		puri = NULL;
9637c478bd9Sstevel@tonic-gate 		for (uri = hp->list; uri != NULL; uri = uri->hash) {
9647c478bd9Sstevel@tonic-gate 			if (uri->hit != 0) {
9657c478bd9Sstevel@tonic-gate 				/*
9667c478bd9Sstevel@tonic-gate 				 * Decrement URI activity counter and skip.
9677c478bd9Sstevel@tonic-gate 				 */
9687c478bd9Sstevel@tonic-gate 				uri->hit--;
9697c478bd9Sstevel@tonic-gate 				puri = uri;
9707c478bd9Sstevel@tonic-gate 				continue;
9717c478bd9Sstevel@tonic-gate 			}
9727c478bd9Sstevel@tonic-gate 			if (uri->proc != NULL) {
9737c478bd9Sstevel@tonic-gate 				/*
9747c478bd9Sstevel@tonic-gate 				 * Currently being processed by a socket, skip.
9757c478bd9Sstevel@tonic-gate 				 */
9767c478bd9Sstevel@tonic-gate 				continue;
9777c478bd9Sstevel@tonic-gate 			}
9787c478bd9Sstevel@tonic-gate 			/*
9797c478bd9Sstevel@tonic-gate 			 * Found a candidate, no hit(s) since added or last
9807c478bd9Sstevel@tonic-gate 			 * reclaim pass, unlink from it's hash chain, update
9817c478bd9Sstevel@tonic-gate 			 * lru scan pointer, drop lock, ref release it.
9827c478bd9Sstevel@tonic-gate 			 */
9837c478bd9Sstevel@tonic-gate 			URI_HASH_UNLINK(cur, new, hp, puri, uri);
9847c478bd9Sstevel@tonic-gate 			if (cur == uri_hash_which) {
9857c478bd9Sstevel@tonic-gate 				if (++hp == pend) {
9867c478bd9Sstevel@tonic-gate 					/* Wrap pointer */
9877c478bd9Sstevel@tonic-gate 					hp = uri_hash_ab[cur];
9887c478bd9Sstevel@tonic-gate 				}
9897c478bd9Sstevel@tonic-gate 				uri_hash_lru[cur] = hp;
9907c478bd9Sstevel@tonic-gate 			}
9917c478bd9Sstevel@tonic-gate 			rw_exit(&uri_hash_access);
9927c478bd9Sstevel@tonic-gate 			REF_RELE(uri);
9937c478bd9Sstevel@tonic-gate 			nl7c_uri_reclaim_cnt++;
9947c478bd9Sstevel@tonic-gate 			goto again;
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 		if (++hp == pend) {
9977c478bd9Sstevel@tonic-gate 			/* Wrap pointer */
9987c478bd9Sstevel@tonic-gate 			hp = uri_hash_ab[cur];
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 		if (hp == start) {
10017c478bd9Sstevel@tonic-gate 			if (cur != new && uri_hash_ab[new] != NULL) {
10027c478bd9Sstevel@tonic-gate 				/*
10037c478bd9Sstevel@tonic-gate 				 * Done with the current hash and have a
10047c478bd9Sstevel@tonic-gate 				 * new hash so check the new hash next.
10057c478bd9Sstevel@tonic-gate 				 */
10067c478bd9Sstevel@tonic-gate 				cur = new;
10077c478bd9Sstevel@tonic-gate 				goto next;
10087c478bd9Sstevel@tonic-gate 			}
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	rw_exit(&uri_hash_access);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate  * Called for a socket which is being freed prior to close, e.g. errored.
10167c478bd9Sstevel@tonic-gate  */
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate void
nl7c_urifree(struct sonode * so)10197c478bd9Sstevel@tonic-gate nl7c_urifree(struct sonode *so)
10207c478bd9Sstevel@tonic-gate {
10210f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
10220f1702c5SYu Xiangning 	uri_desc_t *uri = (uri_desc_t *)sti->sti_nl7c_uri;
10237c478bd9Sstevel@tonic-gate 
10240f1702c5SYu Xiangning 	sti->sti_nl7c_uri = NULL;
10257c478bd9Sstevel@tonic-gate 	if (uri->hash != URI_TEMP) {
10267c478bd9Sstevel@tonic-gate 		uri_delete(uri);
10277c478bd9Sstevel@tonic-gate 		mutex_enter(&uri->proclock);
10287c478bd9Sstevel@tonic-gate 		uri->proc = NULL;
10297c478bd9Sstevel@tonic-gate 		if (CV_HAS_WAITERS(&uri->waiting)) {
10307c478bd9Sstevel@tonic-gate 			cv_broadcast(&uri->waiting);
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 		mutex_exit(&uri->proclock);
10337c478bd9Sstevel@tonic-gate 		nl7c_uri_free++;
10347c478bd9Sstevel@tonic-gate 	} else {
10357c478bd9Sstevel@tonic-gate 		/* No proclock as uri exclusively owned by so */
10367c478bd9Sstevel@tonic-gate 		uri->proc = NULL;
10377c478bd9Sstevel@tonic-gate 		nl7c_uri_temp_free++;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 	REF_RELE(uri);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate /*
10432c9e429eSbrutus  * ...
10442c9e429eSbrutus  *
10452c9e429eSbrutus  *	< 0	need more data
10462c9e429eSbrutus  *
10472c9e429eSbrutus  *	  0	parse complete
10482c9e429eSbrutus  *
10492c9e429eSbrutus  *	> 0	parse error
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate 
10522c9e429eSbrutus volatile uint64_t nl7c_resp_pfail = 0;
10532c9e429eSbrutus volatile uint64_t nl7c_resp_ntemp = 0;
10542c9e429eSbrutus volatile uint64_t nl7c_resp_pass = 0;
10557c478bd9Sstevel@tonic-gate 
10562c9e429eSbrutus static int
nl7c_resp_parse(struct sonode * so,uri_desc_t * uri,char * data,int sz)10572c9e429eSbrutus nl7c_resp_parse(struct sonode *so, uri_desc_t *uri, char *data, int sz)
10582c9e429eSbrutus {
10592c9e429eSbrutus 	if (! nl7c_http_response(&data, &data[sz], uri, so)) {
10602c9e429eSbrutus 		if (data == NULL) {
10612c9e429eSbrutus 			/* Parse fail */
10622c9e429eSbrutus 			goto pfail;
10632c9e429eSbrutus 		}
10642c9e429eSbrutus 		/* More data */
10652c9e429eSbrutus 		data = NULL;
10662c9e429eSbrutus 	} else if (data == NULL) {
10672c9e429eSbrutus 		goto pass;
10682c9e429eSbrutus 	}
10692c9e429eSbrutus 	if (uri->hash != URI_TEMP && uri->nocache) {
10702c9e429eSbrutus 		/*
10712c9e429eSbrutus 		 * After response parse now no cache,
10722c9e429eSbrutus 		 * delete it from cache, wakeup any
10732c9e429eSbrutus 		 * waiters on this URI, make URI_TEMP.
10742c9e429eSbrutus 		 */
10752c9e429eSbrutus 		uri_delete(uri);
10762c9e429eSbrutus 		mutex_enter(&uri->proclock);
10772c9e429eSbrutus 		if (CV_HAS_WAITERS(&uri->waiting)) {
10782c9e429eSbrutus 			cv_broadcast(&uri->waiting);
10792c9e429eSbrutus 		}
10802c9e429eSbrutus 		mutex_exit(&uri->proclock);
10812c9e429eSbrutus 		uri->hash = URI_TEMP;
10822c9e429eSbrutus 		nl7c_uri_temp_mk++;
10832c9e429eSbrutus 	}
10842c9e429eSbrutus 	if (data == NULL) {
10852c9e429eSbrutus 		/* More data needed */
10862c9e429eSbrutus 		return (-1);
10872c9e429eSbrutus 	}
10882c9e429eSbrutus 	/* Success */
10892c9e429eSbrutus 	return (0);
10902c9e429eSbrutus 
10912c9e429eSbrutus pfail:
10922c9e429eSbrutus 	nl7c_resp_pfail++;
10932c9e429eSbrutus 	return (EINVAL);
10942c9e429eSbrutus 
10952c9e429eSbrutus pass:
10962c9e429eSbrutus 	nl7c_resp_pass++;
10972c9e429eSbrutus 	return (ENOTSUP);
10982c9e429eSbrutus }
10992c9e429eSbrutus 
11002c9e429eSbrutus /*
11012c9e429eSbrutus  * Called to sink application response data, the processing of the data
11022c9e429eSbrutus  * is the same for a cached or temp URI (i.e. a URI for which we aren't
11032c9e429eSbrutus  * going to cache the URI but want to parse it for detecting response
11042c9e429eSbrutus  * data end such that for a persistent connection we can parse the next
11052c9e429eSbrutus  * request).
11062c9e429eSbrutus  *
11072c9e429eSbrutus  * On return 0 is returned for sink success, > 0 on error, and < 0 on
11082c9e429eSbrutus  * no so URI (note, data not sinked).
11092c9e429eSbrutus  */
11102c9e429eSbrutus 
11112c9e429eSbrutus int
nl7c_data(struct sonode * so,uio_t * uio)11122c9e429eSbrutus nl7c_data(struct sonode *so, uio_t *uio)
11137c478bd9Sstevel@tonic-gate {
11140f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
11150f1702c5SYu Xiangning 	uri_desc_t	*uri = (uri_desc_t *)sti->sti_nl7c_uri;
11162c9e429eSbrutus 	iovec_t		*iov;
11172c9e429eSbrutus 	int		cnt;
11182c9e429eSbrutus 	int		sz = uio->uio_resid;
11192c9e429eSbrutus 	char		*data, *alloc;
11207c478bd9Sstevel@tonic-gate 	char		*bp;
11217c478bd9Sstevel@tonic-gate 	uri_rd_t	*rdp;
11222c9e429eSbrutus 	boolean_t	first;
11232c9e429eSbrutus 	int		error, perror;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	nl7c_uri_data++;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (uri == NULL) {
11287c478bd9Sstevel@tonic-gate 		/* Socket & NL7C out of sync, disable NL7C */
11290f1702c5SYu Xiangning 		sti->sti_nl7c_flags = 0;
11307c478bd9Sstevel@tonic-gate 		nl7c_uri_NULL1++;
11312c9e429eSbrutus 		return (-1);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11340f1702c5SYu Xiangning 	if (sti->sti_nl7c_flags & NL7C_WAITWRITE) {
11350f1702c5SYu Xiangning 		sti->sti_nl7c_flags &= ~NL7C_WAITWRITE;
11362c9e429eSbrutus 		first = B_TRUE;
11377c478bd9Sstevel@tonic-gate 	} else {
11382c9e429eSbrutus 		first = B_FALSE;
11397c478bd9Sstevel@tonic-gate 	}
11402c9e429eSbrutus 
11412c9e429eSbrutus 	alloc = kmem_alloc(sz, KM_SLEEP);
11422c9e429eSbrutus 	URI_RD_ADD(uri, rdp, sz, -1);
11432c9e429eSbrutus 
11442c9e429eSbrutus 	if (uri->hash != URI_TEMP && uri->count > nca_max_cache_size) {
11452c9e429eSbrutus 		uri_delete(uri);
11462c9e429eSbrutus 		uri->hash = URI_TEMP;
11477c478bd9Sstevel@tonic-gate 	}
11482c9e429eSbrutus 	data = alloc;
11492c9e429eSbrutus 	alloc = NULL;
11502c9e429eSbrutus 	rdp->data.kmem = data;
11512c9e429eSbrutus 	atomic_add_64(&nl7c_uri_bytes, sz);
11522c9e429eSbrutus 
11537c478bd9Sstevel@tonic-gate 	bp = data;
11542c9e429eSbrutus 	while (uio->uio_resid > 0) {
11552c9e429eSbrutus 		iov = uio->uio_iov;
11562c9e429eSbrutus 		if ((cnt = iov->iov_len) == 0) {
11572c9e429eSbrutus 			goto next;
11587c478bd9Sstevel@tonic-gate 		}
11592c9e429eSbrutus 		cnt = MIN(cnt, uio->uio_resid);
11602c9e429eSbrutus 		error = xcopyin(iov->iov_base, bp, cnt);
1161