Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.74
      1  1.74      manu /*	$NetBSD: linux_misc_notalpha.c,v 1.74 2005/05/03 16:26:28 manu Exp $	*/
      2  1.47       erh 
      3  1.47       erh /*-
      4  1.49      fvdl  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5  1.47       erh  * All rights reserved.
      6  1.47       erh  *
      7  1.47       erh  * This code is derived from software contributed to The NetBSD Foundation
      8  1.51   thorpej  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
      9  1.51   thorpej  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
     10  1.47       erh  *
     11  1.47       erh  * Redistribution and use in source and binary forms, with or without
     12  1.47       erh  * modification, are permitted provided that the following conditions
     13  1.47       erh  * are met:
     14  1.47       erh  * 1. Redistributions of source code must retain the above copyright
     15  1.47       erh  *    notice, this list of conditions and the following disclaimer.
     16  1.47       erh  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.47       erh  *    notice, this list of conditions and the following disclaimer in the
     18  1.47       erh  *    documentation and/or other materials provided with the distribution.
     19  1.47       erh  * 3. All advertising materials mentioning features or use of this software
     20  1.47       erh  *    must display the following acknowledgement:
     21  1.47       erh  *	This product includes software developed by the NetBSD
     22  1.47       erh  *	Foundation, Inc. and its contributors.
     23  1.47       erh  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.47       erh  *    contributors may be used to endorse or promote products derived
     25  1.47       erh  *    from this software without specific prior written permission.
     26  1.47       erh  *
     27  1.47       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.47       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.47       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.47       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.47       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.47       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.47       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.47       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.47       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.47       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.47       erh  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1      fvdl  */
     39  1.61     lukem 
     40  1.61     lukem #include <sys/cdefs.h>
     41  1.74      manu __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.74 2005/05/03 16:26:28 manu Exp $");
     42   1.1      fvdl 
     43   1.1      fvdl #include <sys/param.h>
     44   1.1      fvdl #include <sys/systm.h>
     45   1.1      fvdl #include <sys/kernel.h>
     46  1.47       erh #include <sys/mman.h>
     47  1.47       erh #include <sys/mount.h>
     48   1.1      fvdl #include <sys/malloc.h>
     49   1.1      fvdl #include <sys/mbuf.h>
     50  1.47       erh #include <sys/namei.h>
     51  1.57     itohy #include <sys/proc.h>
     52   1.1      fvdl #include <sys/ptrace.h>
     53   1.1      fvdl #include <sys/resource.h>
     54   1.1      fvdl #include <sys/resourcevar.h>
     55  1.64   thorpej #include <sys/time.h>
     56   1.1      fvdl #include <sys/wait.h>
     57   1.1      fvdl 
     58  1.64   thorpej #include <sys/sa.h>
     59   1.1      fvdl #include <sys/syscallargs.h>
     60   1.1      fvdl 
     61  1.48  christos #include <compat/linux/common/linux_types.h>
     62  1.48  christos #include <compat/linux/common/linux_fcntl.h>
     63  1.48  christos #include <compat/linux/common/linux_misc.h>
     64  1.48  christos #include <compat/linux/common/linux_mmap.h>
     65  1.48  christos #include <compat/linux/common/linux_signal.h>
     66  1.48  christos #include <compat/linux/common/linux_util.h>
     67  1.48  christos 
     68   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     69  1.26  christos 
     70   1.1      fvdl /*
     71  1.47       erh  * This file contains routines which are used
     72  1.47       erh  * on every linux architechture except the Alpha.
     73   1.1      fvdl  */
     74  1.21   mycroft 
     75  1.47       erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     76  1.47       erh /* Not used on: alpha */
     77   1.1      fvdl 
     78  1.70  jdolecek #ifdef DEBUG_LINUX
     79  1.70  jdolecek #define DPRINTF(a)	uprintf a
     80  1.70  jdolecek #else
     81  1.70  jdolecek #define DPRINTF(a)
     82  1.70  jdolecek #endif
     83  1.70  jdolecek 
     84  1.74      manu #if !defined(__m68k__)
     85  1.70  jdolecek static void bsd_to_linux_statfs64(const struct statvfs *,
     86  1.70  jdolecek 	struct linux_statfs64  *);
     87  1.71        he #endif
     88  1.70  jdolecek 
     89   1.1      fvdl /*
     90  1.47       erh  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     91  1.47       erh  * Fiddle with the timers to make it work.
     92   1.1      fvdl  */
     93   1.1      fvdl int
     94  1.64   thorpej linux_sys_alarm(l, v, retval)
     95  1.64   thorpej 	struct lwp *l;
     96  1.20   thorpej 	void *v;
     97  1.20   thorpej 	register_t *retval;
     98  1.20   thorpej {
     99  1.47       erh 	struct linux_sys_alarm_args /* {
    100  1.47       erh 		syscallarg(unsigned int) secs;
    101  1.20   thorpej 	} */ *uap = v;
    102  1.64   thorpej 	struct proc *p = l->l_proc;
    103  1.47       erh 	int s;
    104  1.47       erh 	struct itimerval *itp, it;
    105  1.64   thorpej 	struct ptimer *ptp;
    106   1.1      fvdl 
    107  1.64   thorpej 	if (p->p_timers && p->p_timers->pts_timers[ITIMER_REAL])
    108  1.64   thorpej 		itp = &p->p_timers->pts_timers[ITIMER_REAL]->pt_time;
    109  1.64   thorpej 	else
    110  1.64   thorpej 		itp = NULL;
    111  1.47       erh 	s = splclock();
    112  1.47       erh 	/*
    113  1.47       erh 	 * Clear any pending timer alarms.
    114  1.47       erh 	 */
    115  1.64   thorpej 	if (itp) {
    116  1.64   thorpej 		callout_stop(&p->p_timers->pts_timers[ITIMER_REAL]->pt_ch);
    117  1.64   thorpej 		timerclear(&itp->it_interval);
    118  1.64   thorpej 		if (timerisset(&itp->it_value) &&
    119  1.64   thorpej 		    timercmp(&itp->it_value, &time, >))
    120  1.64   thorpej 			timersub(&itp->it_value, &time, &itp->it_value);
    121  1.64   thorpej 		/*
    122  1.64   thorpej 		 * Return how many seconds were left (rounded up)
    123  1.64   thorpej 		 */
    124  1.64   thorpej 		retval[0] = itp->it_value.tv_sec;
    125  1.64   thorpej 		if (itp->it_value.tv_usec)
    126  1.64   thorpej 			retval[0]++;
    127  1.64   thorpej 	} else {
    128  1.64   thorpej 		retval[0] = 0;
    129  1.73     perry 	}
    130   1.1      fvdl 
    131  1.47       erh 	/*
    132  1.47       erh 	 * alarm(0) just resets the timer.
    133  1.47       erh 	 */
    134  1.47       erh 	if (SCARG(uap, secs) == 0) {
    135  1.64   thorpej 		if (itp)
    136  1.64   thorpej 			timerclear(&itp->it_value);
    137  1.47       erh 		splx(s);
    138  1.47       erh 		return 0;
    139  1.47       erh 	}
    140   1.1      fvdl 
    141  1.47       erh 	/*
    142  1.47       erh 	 * Check the new alarm time for sanity, and set it.
    143  1.47       erh 	 */
    144  1.47       erh 	timerclear(&it.it_interval);
    145  1.47       erh 	it.it_value.tv_sec = SCARG(uap, secs);
    146  1.47       erh 	it.it_value.tv_usec = 0;
    147  1.47       erh 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    148  1.47       erh 		splx(s);
    149  1.47       erh 		return (EINVAL);
    150  1.16      fvdl 	}
    151   1.1      fvdl 
    152  1.64   thorpej 	if (p->p_timers == NULL)
    153  1.64   thorpej 		timers_alloc(p);
    154  1.64   thorpej 	ptp = p->p_timers->pts_timers[ITIMER_REAL];
    155  1.64   thorpej 	if (ptp == NULL) {
    156  1.64   thorpej 		ptp = pool_get(&ptimer_pool, PR_WAITOK);
    157  1.64   thorpej 		ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
    158  1.64   thorpej 		ptp->pt_ev.sigev_signo = SIGALRM;
    159  1.65  jdolecek 		ptp->pt_overruns = 0;
    160  1.65  jdolecek 		ptp->pt_proc = p;
    161  1.64   thorpej 		ptp->pt_type = CLOCK_REALTIME;
    162  1.65  jdolecek 		ptp->pt_entry = CLOCK_REALTIME;
    163  1.64   thorpej 		callout_init(&ptp->pt_ch);
    164  1.66     enami 		p->p_timers->pts_timers[ITIMER_REAL] = ptp;
    165  1.64   thorpej 	}
    166  1.64   thorpej 
    167  1.47       erh 	if (timerisset(&it.it_value)) {
    168  1.56   thorpej 		/*
    169  1.56   thorpej 		 * Don't need to check hzto() return value, here.
    170  1.56   thorpej 		 * callout_reset() does it for us.
    171  1.56   thorpej 		 */
    172  1.47       erh 		timeradd(&it.it_value, &time, &it.it_value);
    173  1.64   thorpej 		callout_reset(&ptp->pt_ch, hzto(&it.it_value),
    174  1.64   thorpej 		    realtimerexpire, ptp);
    175  1.16      fvdl 	}
    176  1.64   thorpej 	ptp->pt_time = it;
    177  1.47       erh 	splx(s);
    178   1.1      fvdl 
    179  1.16      fvdl 	return 0;
    180   1.1      fvdl }
    181   1.1      fvdl 
    182  1.74      manu #ifndef __amd64__
    183   1.1      fvdl int
    184  1.64   thorpej linux_sys_nice(l, v, retval)
    185  1.64   thorpej 	struct lwp *l;
    186  1.20   thorpej 	void *v;
    187  1.20   thorpej 	register_t *retval;
    188  1.20   thorpej {
    189  1.47       erh 	struct linux_sys_nice_args /* {
    190  1.47       erh 		syscallarg(int) incr;
    191  1.20   thorpej 	} */ *uap = v;
    192  1.47       erh         struct sys_setpriority_args bsa;
    193  1.20   thorpej 
    194  1.47       erh         SCARG(&bsa, which) = PRIO_PROCESS;
    195  1.47       erh         SCARG(&bsa, who) = 0;
    196  1.47       erh 	SCARG(&bsa, prio) = SCARG(uap, incr);
    197  1.64   thorpej         return sys_setpriority(l, &bsa, retval);
    198   1.1      fvdl }
    199  1.74      manu #endif
    200   1.1      fvdl 
    201  1.74      manu #ifndef __amd64__
    202   1.1      fvdl /*
    203  1.47       erh  * The old Linux readdir was only able to read one entry at a time,
    204  1.47       erh  * even though it had a 'count' argument. In fact, the emulation
    205  1.47       erh  * of the old call was better than the original, because it did handle
    206  1.47       erh  * the count arg properly. Don't bother with it anymore now, and use
    207  1.47       erh  * it to distinguish between old and new. The difference is that the
    208  1.47       erh  * newer one actually does multiple entries, and the reclen field
    209  1.47       erh  * really is the reclen, not the namelength.
    210   1.1      fvdl  */
    211   1.1      fvdl int
    212  1.64   thorpej linux_sys_readdir(l, v, retval)
    213  1.64   thorpej 	struct lwp *l;
    214  1.20   thorpej 	void *v;
    215  1.20   thorpej 	register_t *retval;
    216  1.20   thorpej {
    217  1.47       erh 	struct linux_sys_readdir_args /* {
    218  1.47       erh 		syscallarg(int) fd;
    219  1.47       erh 		syscallarg(struct linux_dirent *) dent;
    220  1.47       erh 		syscallarg(unsigned int) count;
    221  1.20   thorpej 	} */ *uap = v;
    222   1.1      fvdl 
    223  1.47       erh 	SCARG(uap, count) = 1;
    224  1.64   thorpej 	return linux_sys_getdents(l, uap, retval);
    225   1.1      fvdl }
    226  1.74      manu #endif /* !amd64 */
    227   1.1      fvdl 
    228   1.1      fvdl /*
    229   1.1      fvdl  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    230   1.1      fvdl  * need to deal with it.
    231   1.1      fvdl  */
    232   1.1      fvdl int
    233  1.64   thorpej linux_sys_time(l, v, retval)
    234  1.64   thorpej 	struct lwp *l;
    235  1.20   thorpej 	void *v;
    236  1.20   thorpej 	register_t *retval;
    237  1.20   thorpej {
    238  1.21   mycroft 	struct linux_sys_time_args /* {
    239   1.1      fvdl 		linux_time_t *t;
    240  1.20   thorpej 	} */ *uap = v;
    241   1.1      fvdl 	struct timeval atv;
    242   1.1      fvdl 	linux_time_t tt;
    243   1.1      fvdl 	int error;
    244   1.1      fvdl 
    245   1.1      fvdl 	microtime(&atv);
    246   1.1      fvdl 
    247   1.1      fvdl 	tt = atv.tv_sec;
    248   1.1      fvdl 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    249   1.1      fvdl 		return error;
    250   1.1      fvdl 
    251   1.1      fvdl 	retval[0] = tt;
    252   1.1      fvdl 	return 0;
    253   1.1      fvdl }
    254   1.1      fvdl 
    255   1.1      fvdl /*
    256  1.73     perry  * utime(). Do conversion to things that utimes() understands,
    257  1.47       erh  * and pass it on.
    258   1.1      fvdl  */
    259   1.1      fvdl int
    260  1.64   thorpej linux_sys_utime(l, v, retval)
    261  1.64   thorpej 	struct lwp *l;
    262  1.20   thorpej 	void *v;
    263  1.20   thorpej 	register_t *retval;
    264  1.20   thorpej {
    265  1.47       erh 	struct linux_sys_utime_args /* {
    266  1.50  christos 		syscallarg(const char *) path;
    267  1.47       erh 		syscallarg(struct linux_utimbuf *)times;
    268  1.20   thorpej 	} */ *uap = v;
    269  1.64   thorpej 	struct proc *p = l->l_proc;
    270   1.2      fvdl 	caddr_t sg;
    271   1.2      fvdl 	int error;
    272  1.47       erh 	struct sys_utimes_args ua;
    273  1.47       erh 	struct timeval tv[2], *tvp;
    274  1.47       erh 	struct linux_utimbuf lut;
    275   1.2      fvdl 
    276  1.62  christos 	sg = stackgap_init(p, 0);
    277  1.62  christos 	tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
    278  1.69      fvdl 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    279   1.2      fvdl 
    280  1.47       erh 	SCARG(&ua, path) = SCARG(uap, path);
    281   1.2      fvdl 
    282  1.47       erh 	if (SCARG(uap, times) != NULL) {
    283  1.47       erh 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    284  1.47       erh 			return error;
    285  1.47       erh 		tv[0].tv_usec = tv[1].tv_usec = 0;
    286  1.47       erh 		tv[0].tv_sec = lut.l_actime;
    287  1.47       erh 		tv[1].tv_sec = lut.l_modtime;
    288  1.47       erh 		if ((error = copyout(tv, tvp, sizeof tv)))
    289  1.47       erh 			return error;
    290  1.47       erh 		SCARG(&ua, tptr) = tvp;
    291  1.47       erh 	}
    292  1.47       erh 	else
    293  1.47       erh 		SCARG(&ua, tptr) = NULL;
    294   1.2      fvdl 
    295  1.64   thorpej 	return sys_utimes(l, &ua, retval);
    296   1.1      fvdl }
    297   1.1      fvdl 
    298  1.74      manu #ifndef __amd64__
    299  1.47       erh /*
    300  1.72       erh  * waitpid(2).  Just forward on to linux_sys_wait4 with a NULL rusage.
    301  1.47       erh  */
    302   1.1      fvdl int
    303  1.64   thorpej linux_sys_waitpid(l, v, retval)
    304  1.64   thorpej 	struct lwp *l;
    305  1.20   thorpej 	void *v;
    306  1.20   thorpej 	register_t *retval;
    307  1.20   thorpej {
    308  1.47       erh 	struct linux_sys_waitpid_args /* {
    309  1.47       erh 		syscallarg(int) pid;
    310  1.47       erh 		syscallarg(int *) status;
    311  1.47       erh 		syscallarg(int) options;
    312  1.20   thorpej 	} */ *uap = v;
    313  1.72       erh 	struct linux_sys_wait4_args linux_w4a;
    314   1.2      fvdl 
    315  1.72       erh 	SCARG(&linux_w4a, pid) = SCARG(uap, pid);
    316  1.72       erh 	SCARG(&linux_w4a, status) = SCARG(uap, status);
    317  1.72       erh 	SCARG(&linux_w4a, options) = SCARG(uap, options);
    318  1.72       erh 	SCARG(&linux_w4a, rusage) = NULL;
    319   1.2      fvdl 
    320  1.72       erh 	return linux_sys_wait4(l, &linux_w4a, retval);
    321  1.51   thorpej }
    322  1.74      manu #endif /* !amd64 */
    323  1.51   thorpej 
    324  1.51   thorpej int
    325  1.64   thorpej linux_sys_setresgid(l, v, retval)
    326  1.64   thorpej 	struct lwp *l;
    327  1.51   thorpej 	void *v;
    328  1.51   thorpej 	register_t *retval;
    329  1.51   thorpej {
    330  1.51   thorpej 	struct linux_sys_setresgid_args /* {
    331  1.51   thorpej 		syscallarg(gid_t) rgid;
    332  1.51   thorpej 		syscallarg(gid_t) egid;
    333  1.51   thorpej 		syscallarg(gid_t) sgid;
    334  1.51   thorpej 	} */ *uap = v;
    335  1.51   thorpej 
    336  1.51   thorpej 	/*
    337  1.51   thorpej 	 * Note: These checks are a little different than the NetBSD
    338  1.51   thorpej 	 * setregid(2) call performs.  This precisely follows the
    339  1.51   thorpej 	 * behavior of the Linux kernel.
    340  1.51   thorpej 	 */
    341  1.67       dsl 	return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
    342  1.67       dsl 			    SCARG(uap, sgid),
    343  1.67       dsl 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
    344  1.67       dsl 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    345  1.67       dsl 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
    346  1.51   thorpej }
    347  1.51   thorpej 
    348  1.51   thorpej int
    349  1.64   thorpej linux_sys_getresgid(l, v, retval)
    350  1.64   thorpej 	struct lwp *l;
    351  1.51   thorpej 	void *v;
    352  1.51   thorpej 	register_t *retval;
    353  1.51   thorpej {
    354  1.51   thorpej 	struct linux_sys_getresgid_args /* {
    355  1.51   thorpej 		syscallarg(gid_t *) rgid;
    356  1.51   thorpej 		syscallarg(gid_t *) egid;
    357  1.51   thorpej 		syscallarg(gid_t *) sgid;
    358  1.51   thorpej 	} */ *uap = v;
    359  1.64   thorpej 	struct proc *p = l->l_proc;
    360  1.51   thorpej 	struct pcred *pc = p->p_cred;
    361  1.51   thorpej 	int error;
    362  1.51   thorpej 
    363  1.51   thorpej 	/*
    364  1.51   thorpej 	 * Linux copies these values out to userspace like so:
    365  1.51   thorpej 	 *
    366  1.51   thorpej 	 *	1. Copy out rgid.
    367  1.51   thorpej 	 *	2. If that succeeds, copy out egid.
    368  1.51   thorpej 	 *	3. If both of those succeed, copy out sgid.
    369  1.51   thorpej 	 */
    370  1.51   thorpej 	if ((error = copyout(&pc->p_rgid, SCARG(uap, rgid),
    371  1.51   thorpej 			     sizeof(gid_t))) != 0)
    372  1.51   thorpej 		return (error);
    373  1.51   thorpej 
    374  1.63      tron 	if ((error = copyout(&pc->pc_ucred->cr_gid, SCARG(uap, egid),
    375  1.51   thorpej 			     sizeof(gid_t))) != 0)
    376  1.51   thorpej 		return (error);
    377  1.51   thorpej 
    378  1.51   thorpej 	return (copyout(&pc->p_svgid, SCARG(uap, sgid), sizeof(gid_t)));
    379  1.52      tron }
    380  1.52      tron 
    381  1.74      manu #ifndef __amd64__
    382  1.52      tron /*
    383  1.52      tron  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    384  1.52      tron  * need to deal with it.
    385  1.52      tron  */
    386  1.52      tron int
    387  1.64   thorpej linux_sys_stime(l, v, retval)
    388  1.64   thorpej 	struct lwp *l;
    389  1.52      tron 	void *v;
    390  1.52      tron 	register_t *retval;
    391  1.52      tron {
    392  1.52      tron 	struct linux_sys_time_args /* {
    393  1.52      tron 		linux_time_t *t;
    394  1.52      tron 	} */ *uap = v;
    395  1.64   thorpej 	struct proc *p = l->l_proc;
    396  1.52      tron 	struct timeval atv;
    397  1.52      tron 	linux_time_t tt;
    398  1.52      tron 	int error;
    399  1.52      tron 
    400  1.52      tron 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    401  1.52      tron 		return (error);
    402  1.52      tron 
    403  1.52      tron 	if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
    404  1.52      tron 		return error;
    405  1.52      tron 
    406  1.52      tron 	atv.tv_sec = tt;
    407  1.52      tron 	atv.tv_usec = 0;
    408  1.52      tron 
    409  1.52      tron 	if ((error = settime(&atv)))
    410  1.52      tron 		return (error);
    411  1.52      tron 
    412  1.52      tron 	return 0;
    413   1.1      fvdl }
    414  1.74      manu #endif /* !amd64 */
    415  1.70  jdolecek 
    416  1.74      manu #if !defined(__m68k__)
    417  1.70  jdolecek /*
    418  1.70  jdolecek  * Convert NetBSD statvfs structure to Linux statfs64 structure.
    419  1.70  jdolecek  * See comments in bsd_to_linux_statfs() for further background.
    420  1.70  jdolecek  * We can safely pass correct bsize and frsize here, since Linux glibc
    421  1.70  jdolecek  * statvfs() doesn't use statfs64().
    422  1.70  jdolecek  */
    423  1.70  jdolecek static void
    424  1.70  jdolecek bsd_to_linux_statfs64(bsp, lsp)
    425  1.70  jdolecek 	const struct statvfs *bsp;
    426  1.70  jdolecek 	struct linux_statfs64 *lsp;
    427  1.70  jdolecek {
    428  1.70  jdolecek 	int i, div;
    429  1.70  jdolecek 
    430  1.70  jdolecek 	for (i = 0; i < linux_fstypes_cnt; i++) {
    431  1.70  jdolecek 		if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
    432  1.70  jdolecek 			lsp->l_ftype = linux_fstypes[i].linux;
    433  1.70  jdolecek 			break;
    434  1.70  jdolecek 		}
    435  1.70  jdolecek 	}
    436  1.70  jdolecek 
    437  1.70  jdolecek 	if (i == linux_fstypes_cnt) {
    438  1.70  jdolecek 		DPRINTF(("unhandled fstype in linux emulation: %s\n",
    439  1.70  jdolecek 		    bsp->f_fstypename));
    440  1.70  jdolecek 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
    441  1.70  jdolecek 	}
    442  1.70  jdolecek 
    443  1.70  jdolecek 	div = bsp->f_bsize / bsp->f_frsize;
    444  1.70  jdolecek 	lsp->l_fbsize = bsp->f_bsize;
    445  1.70  jdolecek 	lsp->l_ffrsize = bsp->f_frsize;
    446  1.70  jdolecek 	lsp->l_fblocks = bsp->f_blocks / div;
    447  1.70  jdolecek 	lsp->l_fbfree = bsp->f_bfree / div;
    448  1.70  jdolecek 	lsp->l_fbavail = bsp->f_bavail / div;
    449  1.70  jdolecek 	lsp->l_ffiles = bsp->f_files;
    450  1.70  jdolecek 	lsp->l_fffree = bsp->f_ffree / div;
    451  1.70  jdolecek 	/* Linux sets the fsid to 0..., we don't */
    452  1.70  jdolecek 	lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
    453  1.70  jdolecek 	lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
    454  1.70  jdolecek 	lsp->l_fnamelen = bsp->f_namemax;
    455  1.70  jdolecek 	(void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
    456  1.70  jdolecek }
    457  1.70  jdolecek 
    458  1.70  jdolecek /*
    459  1.70  jdolecek  * Implement the fs stat functions. Straightforward.
    460  1.70  jdolecek  */
    461  1.70  jdolecek int
    462  1.70  jdolecek linux_sys_statfs64(l, v, retval)
    463  1.70  jdolecek 	struct lwp *l;
    464  1.70  jdolecek 	void *v;
    465  1.70  jdolecek 	register_t *retval;
    466  1.70  jdolecek {
    467  1.70  jdolecek 	struct linux_sys_statfs64_args /* {
    468  1.70  jdolecek 		syscallarg(const char *) path;
    469  1.70  jdolecek 		syscallarg(size_t) sz;
    470  1.70  jdolecek 		syscallarg(struct linux_statfs64 *) sp;
    471  1.70  jdolecek 	} */ *uap = v;
    472  1.70  jdolecek 	struct proc *p = l->l_proc;
    473  1.70  jdolecek 	struct statvfs btmp, *bsp;
    474  1.70  jdolecek 	struct linux_statfs64 ltmp;
    475  1.70  jdolecek 	struct sys_statvfs1_args bsa;
    476  1.70  jdolecek 	caddr_t sg;
    477  1.70  jdolecek 	int error;
    478  1.70  jdolecek 
    479  1.70  jdolecek 	if (SCARG(uap, sz) != sizeof ltmp)
    480  1.70  jdolecek 		return (EINVAL);
    481  1.70  jdolecek 
    482  1.70  jdolecek 	sg = stackgap_init(p, 0);
    483  1.70  jdolecek 	bsp = (struct statvfs *) stackgap_alloc(p, &sg, sizeof (struct statvfs));
    484  1.70  jdolecek 
    485  1.70  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    486  1.70  jdolecek 
    487  1.70  jdolecek 	SCARG(&bsa, path) = SCARG(uap, path);
    488  1.70  jdolecek 	SCARG(&bsa, buf) = bsp;
    489  1.70  jdolecek 	SCARG(&bsa, flags) = ST_WAIT;
    490  1.70  jdolecek 
    491  1.70  jdolecek 	if ((error = sys_statvfs1(l, &bsa, retval)))
    492  1.70  jdolecek 		return error;
    493  1.70  jdolecek 
    494  1.70  jdolecek 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    495  1.70  jdolecek 		return error;
    496  1.70  jdolecek 
    497  1.70  jdolecek 	bsd_to_linux_statfs64(&btmp, &ltmp);
    498  1.70  jdolecek 
    499  1.70  jdolecek 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    500  1.70  jdolecek }
    501  1.70  jdolecek 
    502  1.70  jdolecek int
    503  1.70  jdolecek linux_sys_fstatfs64(l, v, retval)
    504  1.70  jdolecek 	struct lwp *l;
    505  1.70  jdolecek 	void *v;
    506  1.70  jdolecek 	register_t *retval;
    507  1.70  jdolecek {
    508  1.70  jdolecek 	struct linux_sys_fstatfs64_args /* {
    509  1.70  jdolecek 		syscallarg(int) fd;
    510  1.70  jdolecek 		syscallarg(size_t) sz;
    511  1.70  jdolecek 		syscallarg(struct linux_statfs64 *) sp;
    512  1.70  jdolecek 	} */ *uap = v;
    513  1.70  jdolecek 	struct proc *p = l->l_proc;
    514  1.70  jdolecek 	struct statvfs btmp, *bsp;
    515  1.70  jdolecek 	struct linux_statfs64 ltmp;
    516  1.70  jdolecek 	struct sys_fstatvfs1_args bsa;
    517  1.70  jdolecek 	caddr_t sg;
    518  1.70  jdolecek 	int error;
    519  1.70  jdolecek 
    520  1.70  jdolecek 	if (SCARG(uap, sz) != sizeof ltmp)
    521  1.70  jdolecek 		return (EINVAL);
    522  1.70  jdolecek 
    523  1.70  jdolecek 	sg = stackgap_init(p, 0);
    524  1.70  jdolecek 	bsp = (struct statvfs *) stackgap_alloc(p, &sg, sizeof (struct statvfs));
    525  1.70  jdolecek 
    526  1.70  jdolecek 	SCARG(&bsa, fd) = SCARG(uap, fd);
    527  1.70  jdolecek 	SCARG(&bsa, buf) = bsp;
    528  1.70  jdolecek 	SCARG(&bsa, flags) = ST_WAIT;
    529  1.70  jdolecek 
    530  1.70  jdolecek 	if ((error = sys_fstatvfs1(l, &bsa, retval)))
    531  1.70  jdolecek 		return error;
    532  1.70  jdolecek 
    533  1.70  jdolecek 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    534  1.70  jdolecek 		return error;
    535  1.70  jdolecek 
    536  1.70  jdolecek 	bsd_to_linux_statfs64(&btmp, &ltmp);
    537  1.70  jdolecek 
    538  1.70  jdolecek 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    539  1.70  jdolecek }
    540  1.70  jdolecek #endif /* !__m68k__ */
    541