Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: misc.c,v 1.10 2022/07/30 13:09:19 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd (at) FreeBSD.org>
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 /* __FBSDID("$FreeBSD: head/sys/cddl/compat/opensolaris/kern/opensolaris_misc.c 219089 2011-02-27 19:41:40Z pjd $"); */
     60 
     61 
     62 #include <sys/mount.h>
     63 #include <sys/param.h>
     64 #include <sys/kernel.h>
     65 #include <sys/systm.h>
     66 #include <sys/misc.h>
     67 #include <sys/sunddi.h>
     68 #include <sys/utsname.h>
     69 #include <sys/vnode.h>
     70 #include <sys/mount.h>
     71 #include <sys/pool.h>
     72 #include <sys/buf.h>
     73 
     74 char hw_serial[11] = "0";
     75 
     76 struct utsname utsname;
     77 
     78 void
     79 opensolaris_utsname_init(void *arg)
     80 {
     81 
     82 	strlcpy(utsname.sysname, ostype, sizeof(utsname.sysname));
     83 	strlcpy(utsname.nodename, hostname, sizeof(utsname.nodename));
     84 	strlcpy(utsname.release, osrelease, sizeof(utsname.release));
     85 	strlcpy(utsname.version, version, sizeof(utsname.version));
     86 	strlcpy(utsname.machine, machine, sizeof(utsname.machine));
     87 }
     88 
     89 int
     90 vn_is_readonly(vnode_t *vp)
     91 {
     92 
     93 	return (vp->v_mount->mnt_flag & MNT_RDONLY);
     94 }
     95 
     96 kthread_t *
     97 solaris__thread_create(void * stk, size_t stksize, void (*proc)(), void *arg,
     98     size_t len, proc_t *pp, int state, pri_t pri, const char *name)
     99 {
    100 	int error;
    101 	lwp_t *thr;
    102 
    103 	//ASSERT(stk == NULL && stksize == 0 && len == 0);
    104 	ASSERT(stk == NULL && len == 0);
    105 	ASSERT(state == TS_RUN);
    106 
    107 	error = kthread_create(pri, KTHREAD_MPSAFE, NULL,
    108 	    proc, arg, &thr, "%s", name);
    109 	KASSERT(error == 0);
    110 	return thr;
    111 }
    112 
    113 void
    114 thread_exit(void)
    115 {
    116 
    117 	kthread_exit(0);
    118 }
    119 
    120 void
    121 thread_join(uint64_t kid)
    122 {
    123 
    124 	return;
    125 }
    126 
    127 void
    128 kmem_reap(void)
    129 {
    130 	int bufcnt;
    131 	struct pool *pp;
    132 
    133 	bufcnt = uvmexp.freetarg - uvm_availmem(false);
    134 	if (bufcnt < 0)
    135 		bufcnt = 0;
    136 
    137 	/*
    138 	 * kill unused metadata buffers.
    139 	 */
    140 	mutex_enter(&bufcache_lock);
    141 	buf_drain(bufcnt << PAGE_SHIFT);
    142 	mutex_exit(&bufcache_lock);
    143 
    144 	/*
    145 	 * drain the pools.
    146 	 */
    147 	pool_drain(&pp);
    148 }
    149 
    150 /*
    151  * Zero out the structure, set the size of the requested/returned bitmaps,
    152  * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
    153  * to the returned attributes array.
    154  */
    155 void
    156 xva_init(xvattr_t *xvap)
    157 {
    158 	bzero(xvap, sizeof (xvattr_t));
    159 	xvap->xva_mapsize = XVA_MAPSIZE;
    160 	xvap->xva_magic = XVA_MAGIC;
    161 	xvap->xva_vattr.va_mask = AT_XVATTR;
    162 	xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
    163 }
    164 
    165 
    166 /*
    167  * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
    168  * structure.  Otherwise, returns NULL.
    169  */
    170 xoptattr_t *
    171 xva_getxoptattr(xvattr_t *xvap)
    172 {
    173 	xoptattr_t *xoap = NULL;
    174 	if (xvap->xva_vattr.va_mask & AT_XVATTR)
    175 		xoap = &xvap->xva_xoptattrs;
    176 	return (xoap);
    177 }
    178