Home | History | Annotate | Line # | Download | only in common
linux_misc_notalpha.c revision 1.102.2.3
      1  1.102.2.3  wrstuden /*	$NetBSD: linux_misc_notalpha.c,v 1.102.2.3 2008/09/18 04:36:45 wrstuden Exp $	*/
      2       1.47       erh 
      3       1.47       erh /*-
      4      1.101        ad  * Copyright (c) 1995, 1998, 2008 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  *
     20       1.47       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.47       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.47       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.47       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.47       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.47       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.47       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.47       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.47       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.47       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.47       erh  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1      fvdl  */
     32       1.61     lukem 
     33       1.61     lukem #include <sys/cdefs.h>
     34  1.102.2.3  wrstuden __KERNEL_RCSID(0, "$NetBSD: linux_misc_notalpha.c,v 1.102.2.3 2008/09/18 04:36:45 wrstuden Exp $");
     35        1.1      fvdl 
     36        1.1      fvdl #include <sys/param.h>
     37        1.1      fvdl #include <sys/systm.h>
     38        1.1      fvdl #include <sys/kernel.h>
     39       1.47       erh #include <sys/mman.h>
     40       1.47       erh #include <sys/mount.h>
     41        1.1      fvdl #include <sys/malloc.h>
     42        1.1      fvdl #include <sys/mbuf.h>
     43       1.47       erh #include <sys/namei.h>
     44       1.57     itohy #include <sys/proc.h>
     45       1.93       dsl #include <sys/prot.h>
     46        1.1      fvdl #include <sys/ptrace.h>
     47        1.1      fvdl #include <sys/resource.h>
     48        1.1      fvdl #include <sys/resourcevar.h>
     49       1.64   thorpej #include <sys/time.h>
     50       1.91       dsl #include <sys/vfs_syscalls.h>
     51        1.1      fvdl #include <sys/wait.h>
     52       1.79      elad #include <sys/kauth.h>
     53        1.1      fvdl 
     54        1.1      fvdl #include <sys/syscallargs.h>
     55        1.1      fvdl 
     56       1.48  christos #include <compat/linux/common/linux_types.h>
     57       1.48  christos #include <compat/linux/common/linux_fcntl.h>
     58       1.48  christos #include <compat/linux/common/linux_misc.h>
     59       1.48  christos #include <compat/linux/common/linux_mmap.h>
     60       1.48  christos #include <compat/linux/common/linux_signal.h>
     61       1.48  christos #include <compat/linux/common/linux_util.h>
     62       1.95     njoly #include <compat/linux/common/linux_ipc.h>
     63       1.95     njoly #include <compat/linux/common/linux_sem.h>
     64       1.48  christos 
     65        1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     66       1.26  christos 
     67        1.1      fvdl /*
     68       1.47       erh  * This file contains routines which are used
     69       1.47       erh  * on every linux architechture except the Alpha.
     70        1.1      fvdl  */
     71       1.21   mycroft 
     72       1.47       erh /* Used on: arm, i386, m68k, mips, ppc, sparc, sparc64 */
     73       1.47       erh /* Not used on: alpha */
     74        1.1      fvdl 
     75       1.70  jdolecek #ifdef DEBUG_LINUX
     76       1.70  jdolecek #define DPRINTF(a)	uprintf a
     77       1.70  jdolecek #else
     78       1.70  jdolecek #define DPRINTF(a)
     79       1.70  jdolecek #endif
     80       1.70  jdolecek 
     81       1.77      manu #ifndef COMPAT_LINUX32
     82       1.87     njoly #if !defined(__m68k__) && !defined(__amd64__)
     83       1.70  jdolecek static void bsd_to_linux_statfs64(const struct statvfs *,
     84       1.70  jdolecek 	struct linux_statfs64  *);
     85       1.71        he #endif
     86       1.70  jdolecek 
     87        1.1      fvdl /*
     88       1.47       erh  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
     89       1.47       erh  * Fiddle with the timers to make it work.
     90      1.101        ad  *
     91      1.101        ad  * XXX This shouldn't be dicking about with the ptimer stuff directly.
     92        1.1      fvdl  */
     93        1.1      fvdl int
     94       1.99       dsl linux_sys_alarm(struct lwp *l, const struct linux_sys_alarm_args *uap, register_t *retval)
     95       1.20   thorpej {
     96       1.99       dsl 	/* {
     97       1.47       erh 		syscallarg(unsigned int) secs;
     98       1.99       dsl 	} */
     99       1.64   thorpej 	struct proc *p = l->l_proc;
    100  1.102.2.3  wrstuden 	struct timespec now;
    101  1.102.2.3  wrstuden 	struct itimerspec *itp, it;
    102      1.101        ad 	struct ptimer *ptp, *spare;
    103      1.101        ad 	extern kmutex_t timer_lock;
    104      1.101        ad 	struct ptimers *pts;
    105      1.101        ad 
    106      1.101        ad 	if ((pts = p->p_timers) == NULL)
    107      1.101        ad 		pts = timers_alloc(p);
    108      1.101        ad 	spare = NULL;
    109      1.101        ad 
    110      1.101        ad  retry:
    111      1.101        ad 	mutex_spin_enter(&timer_lock);
    112      1.101        ad 	if (pts && pts->pts_timers[ITIMER_REAL])
    113      1.101        ad 		itp = &pts->pts_timers[ITIMER_REAL]->pt_time;
    114       1.64   thorpej 	else
    115       1.64   thorpej 		itp = NULL;
    116       1.47       erh 	/*
    117       1.47       erh 	 * Clear any pending timer alarms.
    118       1.47       erh 	 */
    119       1.64   thorpej 	if (itp) {
    120      1.101        ad 		callout_stop(&pts->pts_timers[ITIMER_REAL]->pt_ch);
    121  1.102.2.3  wrstuden 		timespecclear(&itp->it_interval);
    122  1.102.2.3  wrstuden 		getnanotime(&now);
    123  1.102.2.3  wrstuden 		if (timespecisset(&itp->it_value) &&
    124  1.102.2.3  wrstuden 		    timespeccmp(&itp->it_value, &now, >))
    125  1.102.2.3  wrstuden 			timespecsub(&itp->it_value, &now, &itp->it_value);
    126       1.64   thorpej 		/*
    127       1.64   thorpej 		 * Return how many seconds were left (rounded up)
    128       1.64   thorpej 		 */
    129       1.64   thorpej 		retval[0] = itp->it_value.tv_sec;
    130  1.102.2.3  wrstuden 		if (itp->it_value.tv_nsec)
    131       1.64   thorpej 			retval[0]++;
    132       1.64   thorpej 	} else {
    133       1.64   thorpej 		retval[0] = 0;
    134       1.73     perry 	}
    135        1.1      fvdl 
    136       1.47       erh 	/*
    137       1.47       erh 	 * alarm(0) just resets the timer.
    138       1.47       erh 	 */
    139       1.47       erh 	if (SCARG(uap, secs) == 0) {
    140       1.64   thorpej 		if (itp)
    141  1.102.2.3  wrstuden 			timespecclear(&itp->it_value);
    142      1.101        ad 		mutex_spin_exit(&timer_lock);
    143       1.47       erh 		return 0;
    144       1.47       erh 	}
    145        1.1      fvdl 
    146       1.47       erh 	/*
    147       1.47       erh 	 * Check the new alarm time for sanity, and set it.
    148       1.47       erh 	 */
    149  1.102.2.3  wrstuden 	timespecclear(&it.it_interval);
    150       1.47       erh 	it.it_value.tv_sec = SCARG(uap, secs);
    151  1.102.2.3  wrstuden 	it.it_value.tv_nsec = 0;
    152  1.102.2.3  wrstuden 	if (itimespecfix(&it.it_value) || itimespecfix(&it.it_interval)) {
    153      1.101        ad 		mutex_spin_exit(&timer_lock);
    154       1.47       erh 		return (EINVAL);
    155       1.16      fvdl 	}
    156        1.1      fvdl 
    157      1.101        ad 	ptp = pts->pts_timers[ITIMER_REAL];
    158       1.64   thorpej 	if (ptp == NULL) {
    159      1.101        ad 		if (spare == NULL) {
    160      1.101        ad 			mutex_spin_exit(&timer_lock);
    161      1.101        ad 			spare = pool_get(&ptimer_pool, PR_WAITOK);
    162      1.101        ad 			goto retry;
    163      1.101        ad 		}
    164      1.101        ad 		ptp = spare;
    165      1.101        ad 		spare = NULL;
    166       1.64   thorpej 		ptp->pt_ev.sigev_notify = SIGEV_SIGNAL;
    167       1.64   thorpej 		ptp->pt_ev.sigev_signo = SIGALRM;
    168       1.65  jdolecek 		ptp->pt_overruns = 0;
    169       1.65  jdolecek 		ptp->pt_proc = p;
    170       1.64   thorpej 		ptp->pt_type = CLOCK_REALTIME;
    171       1.65  jdolecek 		ptp->pt_entry = CLOCK_REALTIME;
    172      1.101        ad 		ptp->pt_active = 0;
    173      1.101        ad 		ptp->pt_queued = 0;
    174      1.101        ad 		callout_init(&ptp->pt_ch, CALLOUT_MPSAFE);
    175      1.101        ad 		pts->pts_timers[ITIMER_REAL] = ptp;
    176       1.64   thorpej 	}
    177       1.64   thorpej 
    178  1.102.2.3  wrstuden 	if (timespecisset(&it.it_value)) {
    179       1.56   thorpej 		/*
    180  1.102.2.3  wrstuden 		 * Don't need to check tvhzto() return value, here.
    181       1.56   thorpej 		 * callout_reset() does it for us.
    182       1.56   thorpej 		 */
    183  1.102.2.3  wrstuden 		getnanotime(&now);
    184  1.102.2.3  wrstuden 		timespecadd(&it.it_value, &now, &it.it_value);
    185  1.102.2.3  wrstuden 		callout_reset(&ptp->pt_ch, tshzto(&it.it_value),
    186       1.64   thorpej 		    realtimerexpire, ptp);
    187       1.16      fvdl 	}
    188       1.64   thorpej 	ptp->pt_time = it;
    189      1.101        ad 	mutex_spin_exit(&timer_lock);
    190        1.1      fvdl 
    191       1.16      fvdl 	return 0;
    192        1.1      fvdl }
    193       1.77      manu #endif /* !COMPAT_LINUX32 */
    194        1.1      fvdl 
    195       1.96     njoly #if !defined(__amd64__)
    196        1.1      fvdl int
    197       1.99       dsl linux_sys_nice(struct lwp *l, const struct linux_sys_nice_args *uap, register_t *retval)
    198       1.20   thorpej {
    199       1.99       dsl 	/* {
    200       1.47       erh 		syscallarg(int) incr;
    201       1.99       dsl 	} */
    202       1.47       erh         struct sys_setpriority_args bsa;
    203       1.20   thorpej 
    204       1.47       erh         SCARG(&bsa, which) = PRIO_PROCESS;
    205       1.47       erh         SCARG(&bsa, who) = 0;
    206       1.47       erh 	SCARG(&bsa, prio) = SCARG(uap, incr);
    207       1.64   thorpej         return sys_setpriority(l, &bsa, retval);
    208        1.1      fvdl }
    209       1.96     njoly #endif /* !__amd64__ */
    210        1.1      fvdl 
    211       1.77      manu #ifndef COMPAT_LINUX32
    212       1.74      manu #ifndef __amd64__
    213        1.1      fvdl /*
    214       1.47       erh  * The old Linux readdir was only able to read one entry at a time,
    215       1.47       erh  * even though it had a 'count' argument. In fact, the emulation
    216       1.47       erh  * of the old call was better than the original, because it did handle
    217       1.47       erh  * the count arg properly. Don't bother with it anymore now, and use
    218       1.47       erh  * it to distinguish between old and new. The difference is that the
    219       1.47       erh  * newer one actually does multiple entries, and the reclen field
    220       1.47       erh  * really is the reclen, not the namelength.
    221        1.1      fvdl  */
    222        1.1      fvdl int
    223       1.99       dsl linux_sys_readdir(struct lwp *l, const struct linux_sys_readdir_args *uap, register_t *retval)
    224       1.20   thorpej {
    225       1.99       dsl 	/* {
    226       1.47       erh 		syscallarg(int) fd;
    227       1.47       erh 		syscallarg(struct linux_dirent *) dent;
    228       1.47       erh 		syscallarg(unsigned int) count;
    229       1.99       dsl 	} */
    230      1.100     njoly 	int error;
    231       1.99       dsl 	struct linux_sys_getdents_args da;
    232        1.1      fvdl 
    233       1.99       dsl 	SCARG(&da, fd) = SCARG(uap, fd);
    234       1.99       dsl 	SCARG(&da, dent) = SCARG(uap, dent);
    235       1.99       dsl 	SCARG(&da, count) = 1;
    236      1.100     njoly 
    237      1.100     njoly 	error = linux_sys_getdents(l, &da, retval);
    238      1.100     njoly 	if (error == 0 && *retval > 1)
    239      1.100     njoly 		*retval = 1;
    240      1.100     njoly 
    241      1.100     njoly 	return error;
    242        1.1      fvdl }
    243       1.74      manu #endif /* !amd64 */
    244        1.1      fvdl 
    245        1.1      fvdl /*
    246        1.1      fvdl  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    247        1.1      fvdl  * need to deal with it.
    248        1.1      fvdl  */
    249        1.1      fvdl int
    250       1.99       dsl linux_sys_time(struct lwp *l, const struct linux_sys_time_args *uap, register_t *retval)
    251       1.20   thorpej {
    252       1.99       dsl 	/* {
    253       1.99       dsl 		syscallarg(linux_time_t) *t;
    254       1.99       dsl 	} */
    255        1.1      fvdl 	struct timeval atv;
    256        1.1      fvdl 	linux_time_t tt;
    257        1.1      fvdl 	int error;
    258        1.1      fvdl 
    259        1.1      fvdl 	microtime(&atv);
    260        1.1      fvdl 
    261        1.1      fvdl 	tt = atv.tv_sec;
    262        1.1      fvdl 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    263        1.1      fvdl 		return error;
    264        1.1      fvdl 
    265        1.1      fvdl 	retval[0] = tt;
    266        1.1      fvdl 	return 0;
    267        1.1      fvdl }
    268        1.1      fvdl 
    269        1.1      fvdl /*
    270       1.73     perry  * utime(). Do conversion to things that utimes() understands,
    271       1.47       erh  * and pass it on.
    272        1.1      fvdl  */
    273        1.1      fvdl int
    274       1.99       dsl linux_sys_utime(struct lwp *l, const struct linux_sys_utime_args *uap, register_t *retval)
    275       1.20   thorpej {
    276       1.99       dsl 	/* {
    277       1.50  christos 		syscallarg(const char *) path;
    278       1.47       erh 		syscallarg(struct linux_utimbuf *)times;
    279       1.99       dsl 	} */
    280        1.2      fvdl 	int error;
    281       1.47       erh 	struct timeval tv[2], *tvp;
    282       1.47       erh 	struct linux_utimbuf lut;
    283        1.2      fvdl 
    284       1.47       erh 	if (SCARG(uap, times) != NULL) {
    285       1.47       erh 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    286       1.47       erh 			return error;
    287       1.47       erh 		tv[0].tv_usec = tv[1].tv_usec = 0;
    288       1.47       erh 		tv[0].tv_sec = lut.l_actime;
    289       1.47       erh 		tv[1].tv_sec = lut.l_modtime;
    290       1.92       dsl 		tvp = tv;
    291       1.92       dsl 	} else
    292       1.92       dsl 		tvp = NULL;
    293        1.2      fvdl 
    294       1.92       dsl 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
    295       1.92       dsl 			   tvp,  UIO_SYSSPACE);
    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.99       dsl linux_sys_waitpid(struct lwp *l, const struct linux_sys_waitpid_args *uap, register_t *retval)
    304       1.20   thorpej {
    305       1.99       dsl 	/* {
    306       1.47       erh 		syscallarg(int) pid;
    307       1.47       erh 		syscallarg(int *) status;
    308       1.47       erh 		syscallarg(int) options;
    309       1.99       dsl 	} */
    310       1.72       erh 	struct linux_sys_wait4_args linux_w4a;
    311        1.2      fvdl 
    312       1.72       erh 	SCARG(&linux_w4a, pid) = SCARG(uap, pid);
    313       1.72       erh 	SCARG(&linux_w4a, status) = SCARG(uap, status);
    314       1.72       erh 	SCARG(&linux_w4a, options) = SCARG(uap, options);
    315       1.72       erh 	SCARG(&linux_w4a, rusage) = NULL;
    316        1.2      fvdl 
    317       1.72       erh 	return linux_sys_wait4(l, &linux_w4a, retval);
    318       1.51   thorpej }
    319       1.74      manu #endif /* !amd64 */
    320       1.51   thorpej 
    321       1.51   thorpej int
    322       1.99       dsl linux_sys_setresgid(struct lwp *l, const struct linux_sys_setresgid_args *uap, register_t *retval)
    323       1.51   thorpej {
    324       1.99       dsl 	/* {
    325       1.51   thorpej 		syscallarg(gid_t) rgid;
    326       1.51   thorpej 		syscallarg(gid_t) egid;
    327       1.51   thorpej 		syscallarg(gid_t) sgid;
    328       1.99       dsl 	} */
    329       1.51   thorpej 
    330       1.51   thorpej 	/*
    331       1.51   thorpej 	 * Note: These checks are a little different than the NetBSD
    332       1.51   thorpej 	 * setregid(2) call performs.  This precisely follows the
    333       1.51   thorpej 	 * behavior of the Linux kernel.
    334       1.51   thorpej 	 */
    335       1.67       dsl 	return do_setresgid(l, SCARG(uap,rgid), SCARG(uap, egid),
    336       1.67       dsl 			    SCARG(uap, sgid),
    337       1.67       dsl 			    ID_R_EQ_R | ID_R_EQ_E | ID_R_EQ_S |
    338       1.67       dsl 			    ID_E_EQ_R | ID_E_EQ_E | ID_E_EQ_S |
    339       1.67       dsl 			    ID_S_EQ_R | ID_S_EQ_E | ID_S_EQ_S );
    340       1.51   thorpej }
    341       1.51   thorpej 
    342       1.51   thorpej int
    343       1.99       dsl linux_sys_getresgid(struct lwp *l, const struct linux_sys_getresgid_args *uap, register_t *retval)
    344       1.51   thorpej {
    345       1.99       dsl 	/* {
    346       1.51   thorpej 		syscallarg(gid_t *) rgid;
    347       1.51   thorpej 		syscallarg(gid_t *) egid;
    348       1.51   thorpej 		syscallarg(gid_t *) sgid;
    349       1.99       dsl 	} */
    350       1.81        ad 	kauth_cred_t pc = l->l_cred;
    351       1.51   thorpej 	int error;
    352       1.79      elad 	gid_t gid;
    353       1.51   thorpej 
    354       1.51   thorpej 	/*
    355       1.51   thorpej 	 * Linux copies these values out to userspace like so:
    356       1.51   thorpej 	 *
    357       1.51   thorpej 	 *	1. Copy out rgid.
    358       1.51   thorpej 	 *	2. If that succeeds, copy out egid.
    359       1.51   thorpej 	 *	3. If both of those succeed, copy out sgid.
    360       1.51   thorpej 	 */
    361       1.79      elad 	gid = kauth_cred_getgid(pc);
    362       1.79      elad 	if ((error = copyout(&gid, SCARG(uap, rgid), sizeof(gid_t))) != 0)
    363       1.51   thorpej 		return (error);
    364       1.51   thorpej 
    365       1.79      elad 	gid = kauth_cred_getegid(pc);
    366       1.79      elad 	if ((error = copyout(&gid, SCARG(uap, egid), sizeof(gid_t))) != 0)
    367       1.51   thorpej 		return (error);
    368       1.51   thorpej 
    369       1.79      elad 	gid = kauth_cred_getsvgid(pc);
    370       1.79      elad 
    371       1.79      elad 	return (copyout(&gid, SCARG(uap, sgid), sizeof(gid_t)));
    372       1.52      tron }
    373       1.52      tron 
    374       1.74      manu #ifndef __amd64__
    375       1.52      tron /*
    376       1.52      tron  * I wonder why Linux has settimeofday() _and_ stime().. Still, we
    377       1.52      tron  * need to deal with it.
    378       1.52      tron  */
    379       1.52      tron int
    380       1.99       dsl linux_sys_stime(struct lwp *l, const struct linux_sys_stime_args *uap, register_t *retval)
    381       1.52      tron {
    382       1.99       dsl 	/* {
    383       1.99       dsl 		syscallarg(linux_time_t) *t;
    384       1.99       dsl 	} */
    385       1.75  christos 	struct timespec ats;
    386       1.52      tron 	linux_time_t tt;
    387       1.52      tron 	int error;
    388       1.52      tron 
    389       1.52      tron 	if ((error = copyin(&tt, SCARG(uap, t), sizeof tt)) != 0)
    390       1.52      tron 		return error;
    391       1.52      tron 
    392       1.75  christos 	ats.tv_sec = tt;
    393       1.75  christos 	ats.tv_nsec = 0;
    394       1.52      tron 
    395       1.81        ad 	if ((error = settime(l->l_proc, &ats)))
    396       1.52      tron 		return (error);
    397       1.52      tron 
    398       1.52      tron 	return 0;
    399        1.1      fvdl }
    400       1.74      manu #endif /* !amd64 */
    401       1.70  jdolecek 
    402       1.87     njoly #if !defined(__m68k__) && !defined(__amd64__)
    403       1.70  jdolecek /*
    404       1.70  jdolecek  * Convert NetBSD statvfs structure to Linux statfs64 structure.
    405       1.70  jdolecek  * See comments in bsd_to_linux_statfs() for further background.
    406       1.70  jdolecek  * We can safely pass correct bsize and frsize here, since Linux glibc
    407       1.70  jdolecek  * statvfs() doesn't use statfs64().
    408       1.70  jdolecek  */
    409       1.70  jdolecek static void
    410       1.98       dsl bsd_to_linux_statfs64(const struct statvfs *bsp, struct linux_statfs64 *lsp)
    411       1.70  jdolecek {
    412       1.70  jdolecek 	int i, div;
    413       1.70  jdolecek 
    414       1.70  jdolecek 	for (i = 0; i < linux_fstypes_cnt; i++) {
    415       1.70  jdolecek 		if (strcmp(bsp->f_fstypename, linux_fstypes[i].bsd) == 0) {
    416       1.70  jdolecek 			lsp->l_ftype = linux_fstypes[i].linux;
    417       1.70  jdolecek 			break;
    418       1.70  jdolecek 		}
    419       1.70  jdolecek 	}
    420       1.70  jdolecek 
    421       1.70  jdolecek 	if (i == linux_fstypes_cnt) {
    422       1.70  jdolecek 		DPRINTF(("unhandled fstype in linux emulation: %s\n",
    423       1.70  jdolecek 		    bsp->f_fstypename));
    424       1.70  jdolecek 		lsp->l_ftype = LINUX_DEFAULT_SUPER_MAGIC;
    425       1.70  jdolecek 	}
    426       1.70  jdolecek 
    427       1.89  christos 	div = bsp->f_frsize ? (bsp->f_bsize / bsp->f_frsize) : 1;
    428       1.89  christos 	if (div == 0)
    429       1.89  christos 		div = 1;
    430       1.70  jdolecek 	lsp->l_fbsize = bsp->f_bsize;
    431       1.70  jdolecek 	lsp->l_ffrsize = bsp->f_frsize;
    432       1.70  jdolecek 	lsp->l_fblocks = bsp->f_blocks / div;
    433       1.70  jdolecek 	lsp->l_fbfree = bsp->f_bfree / div;
    434       1.70  jdolecek 	lsp->l_fbavail = bsp->f_bavail / div;
    435       1.70  jdolecek 	lsp->l_ffiles = bsp->f_files;
    436       1.70  jdolecek 	lsp->l_fffree = bsp->f_ffree / div;
    437       1.70  jdolecek 	/* Linux sets the fsid to 0..., we don't */
    438       1.70  jdolecek 	lsp->l_ffsid.val[0] = bsp->f_fsidx.__fsid_val[0];
    439       1.70  jdolecek 	lsp->l_ffsid.val[1] = bsp->f_fsidx.__fsid_val[1];
    440       1.70  jdolecek 	lsp->l_fnamelen = bsp->f_namemax;
    441       1.70  jdolecek 	(void)memset(lsp->l_fspare, 0, sizeof(lsp->l_fspare));
    442       1.70  jdolecek }
    443       1.70  jdolecek 
    444       1.70  jdolecek /*
    445       1.70  jdolecek  * Implement the fs stat functions. Straightforward.
    446       1.70  jdolecek  */
    447       1.70  jdolecek int
    448       1.99       dsl linux_sys_statfs64(struct lwp *l, const struct linux_sys_statfs64_args *uap, register_t *retval)
    449       1.70  jdolecek {
    450       1.99       dsl 	/* {
    451       1.70  jdolecek 		syscallarg(const char *) path;
    452       1.70  jdolecek 		syscallarg(size_t) sz;
    453       1.70  jdolecek 		syscallarg(struct linux_statfs64 *) sp;
    454       1.99       dsl 	} */
    455       1.90       dsl 	struct statvfs *sb;
    456       1.70  jdolecek 	struct linux_statfs64 ltmp;
    457       1.70  jdolecek 	int error;
    458       1.70  jdolecek 
    459       1.70  jdolecek 	if (SCARG(uap, sz) != sizeof ltmp)
    460       1.70  jdolecek 		return (EINVAL);
    461       1.70  jdolecek 
    462       1.90       dsl 	sb = STATVFSBUF_GET();
    463       1.90       dsl 	error = do_sys_pstatvfs(l, SCARG(uap, path), ST_WAIT, sb);
    464       1.90       dsl 	if (error == 0) {
    465       1.90       dsl 		bsd_to_linux_statfs64(sb, &ltmp);
    466       1.90       dsl 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    467       1.78      yamt 	}
    468       1.90       dsl 	STATVFSBUF_PUT(sb);
    469       1.78      yamt 	return error;
    470       1.70  jdolecek }
    471       1.70  jdolecek 
    472       1.70  jdolecek int
    473       1.99       dsl linux_sys_fstatfs64(struct lwp *l, const struct linux_sys_fstatfs64_args *uap, register_t *retval)
    474       1.70  jdolecek {
    475       1.99       dsl 	/* {
    476       1.70  jdolecek 		syscallarg(int) fd;
    477       1.70  jdolecek 		syscallarg(size_t) sz;
    478       1.70  jdolecek 		syscallarg(struct linux_statfs64 *) sp;
    479       1.99       dsl 	} */
    480       1.90       dsl 	struct statvfs *sb;
    481       1.70  jdolecek 	struct linux_statfs64 ltmp;
    482       1.70  jdolecek 	int error;
    483       1.70  jdolecek 
    484       1.70  jdolecek 	if (SCARG(uap, sz) != sizeof ltmp)
    485       1.70  jdolecek 		return (EINVAL);
    486       1.70  jdolecek 
    487       1.90       dsl 	sb = STATVFSBUF_GET();
    488       1.90       dsl 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    489       1.90       dsl 	if (error == 0) {
    490       1.90       dsl 		bsd_to_linux_statfs64(sb, &ltmp);
    491       1.90       dsl 		error = copyout(&ltmp, SCARG(uap, sp), sizeof ltmp);
    492       1.78      yamt 	}
    493       1.90       dsl 	STATVFSBUF_PUT(sb);
    494       1.78      yamt 	return error;
    495       1.70  jdolecek }
    496       1.87     njoly #endif /* !__m68k__ && !__amd64__ */
    497       1.77      manu #endif /* !COMPAT_LINUX32 */
    498