xref: /freebsd/sys/kern/kern_acct.c (revision e5e820fd1f9a5d788798072f1d2a60f7cc798f70)
1df8bae1dSRodney W. Grimes /*-
2c7d893deSDavid Greenman  * Copyright (c) 1994 Christopher G. Demetriou
3df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
6df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
7df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
8df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
10df8bae1dSRodney W. Grimes  *
11df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
12df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
13df8bae1dSRodney W. Grimes  * are met:
14df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
16df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
18df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
20df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
21df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
22df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
23df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
24df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
25df8bae1dSRodney W. Grimes  *    without specific prior written permission.
26df8bae1dSRodney W. Grimes  *
27df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
38df8bae1dSRodney W. Grimes  *
39c7d893deSDavid Greenman  *	@(#)kern_acct.c	8.1 (Berkeley) 6/14/93
40c3aac50fSPeter Wemm  * $FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43e5e820fdSRobert Watson #include "opt_mac.h"
44e5e820fdSRobert Watson 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
460ad076d5SBruce Evans #include <sys/systm.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/mutex.h>
49d2d3e875SBruce Evans #include <sys/sysproto.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
51e5e820fdSRobert Watson #include <sys/mac.h>
52df8bae1dSRodney W. Grimes #include <sys/mount.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
543ac4d1efSBruce Evans #include <sys/fcntl.h>
55df8bae1dSRodney W. Grimes #include <sys/syslog.h>
56df8bae1dSRodney W. Grimes #include <sys/kernel.h>
57996c772fSJohn Dyson #include <sys/sysent.h>
5887b6de2bSPoul-Henning Kamp #include <sys/sysctl.h>
59c7d893deSDavid Greenman #include <sys/namei.h>
60c7d893deSDavid Greenman #include <sys/acct.h>
61c7d893deSDavid Greenman #include <sys/resourcevar.h>
62c7d893deSDavid Greenman #include <sys/tty.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes /*
65c7d893deSDavid Greenman  * The routines implemented in this file are described in:
66c7d893deSDavid Greenman  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
67c7d893deSDavid Greenman  *	    UNIX Operating System (Addison Welley, 1989)
68c7d893deSDavid Greenman  * on pages 62-63.
69c7d893deSDavid Greenman  *
70c7d893deSDavid Greenman  * Arguably, to simplify accounting operations, this mechanism should
71c7d893deSDavid Greenman  * be replaced by one in which an accounting log file (similar to /dev/klog)
72c7d893deSDavid Greenman  * is read by a user process, etc.  However, that has its own problems.
73df8bae1dSRodney W. Grimes  */
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes /*
76c7d893deSDavid Greenman  * Internal accounting functions.
77c7d893deSDavid Greenman  * The former's operation is described in Leffler, et al., and the latter
78c7d893deSDavid Greenman  * was provided by UCB with the 4.4BSD-Lite release
79df8bae1dSRodney W. Grimes  */
804d77a549SAlfred Perlstein static comp_t	encode_comp_t(u_long, u_long);
814d77a549SAlfred Perlstein static void	acctwatch(void *);
82c7d893deSDavid Greenman 
83c7d893deSDavid Greenman /*
844f559836SJake Burkholder  * Accounting callout used for periodic scheduling of acctwatch.
85ab36c067SJustin T. Gibbs  */
864f559836SJake Burkholder static struct	callout acctwatch_callout;
87ab36c067SJustin T. Gibbs 
88ab36c067SJustin T. Gibbs /*
895b606744SJohan Karlsson  * Accounting vnode pointer, saved vnode pointer, and flags for each.
90c7d893deSDavid Greenman  */
9187b6de2bSPoul-Henning Kamp static struct	vnode *acctp;
922d701617SRobert Watson static struct	ucred *acctcred;
935b606744SJohan Karlsson static int	acctflags;
9487b6de2bSPoul-Henning Kamp static struct	vnode *savacctp;
952d701617SRobert Watson static struct	ucred *savacctcred;
965b606744SJohan Karlsson static int	savacctflags;
97df8bae1dSRodney W. Grimes 
984f39d5d5SAndrew R. Reiter static struct mtx	acct_mtx;
994f39d5d5SAndrew R. Reiter MTX_SYSINIT(acct, &acct_mtx, "accounting", MTX_DEF);
1004f39d5d5SAndrew R. Reiter 
101df8bae1dSRodney W. Grimes /*
102df8bae1dSRodney W. Grimes  * Values associated with enabling and disabling accounting
103df8bae1dSRodney W. Grimes  */
10487b6de2bSPoul-Henning Kamp static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
10587b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
10647fdd692SNeil Blakey-Milner 	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
10787b6de2bSPoul-Henning Kamp 
10887b6de2bSPoul-Henning Kamp static int acctresume = 4;	/* resume when free space risen to > 4% */
10987b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
11047fdd692SNeil Blakey-Milner 	&acctresume, 0, "percentage of free disk space above which accounting resumes");
11187b6de2bSPoul-Henning Kamp 
11287b6de2bSPoul-Henning Kamp static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
11387b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_chkfreq, CTLFLAG_RW,
11447fdd692SNeil Blakey-Milner 	&acctchkfreq, 0, "frequency for checking the free space");
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes /*
117c7d893deSDavid Greenman  * Accounting system call.  Written based on the specification and
118c7d893deSDavid Greenman  * previous implementation done by Mark Tinguely.
119116734c4SMatthew Dillon  *
120116734c4SMatthew Dillon  * MPSAFE
121df8bae1dSRodney W. Grimes  */
122c7d893deSDavid Greenman int
123b40ce416SJulian Elischer acct(td, uap)
124b40ce416SJulian Elischer 	struct thread *td;
125996c772fSJohn Dyson 	struct acct_args /* {
126996c772fSJohn Dyson 		syscallarg(char *) path;
127996c772fSJohn Dyson 	} */ *uap;
128c7d893deSDavid Greenman {
129c7d893deSDavid Greenman 	struct nameidata nd;
130e6796b67SKirk McKusick 	int error, flags;
131c7d893deSDavid Greenman 
132c7d893deSDavid Greenman 	/* Make sure that the caller is root. */
13344731cabSJohn Baldwin 	error = suser(td);
134797f2d22SPoul-Henning Kamp 	if (error)
13516e7bc7bSJohn Baldwin 		return (error);
136c7d893deSDavid Greenman 
13716e7bc7bSJohn Baldwin 	mtx_lock(&Giant);
138c7d893deSDavid Greenman 	/*
139c7d893deSDavid Greenman 	 * If accounting is to be started to a file, open that file for
14092da2e76SJohan Karlsson 	 * appending and make sure it's a 'normal'.
141c7d893deSDavid Greenman 	 */
142996c772fSJohn Dyson 	if (SCARG(uap, path) != NULL) {
143996c772fSJohn Dyson 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path),
144b40ce416SJulian Elischer 		       td);
14592da2e76SJohan Karlsson 		flags = FWRITE | O_APPEND;
146e6796b67SKirk McKusick 		error = vn_open(&nd, &flags, 0);
147797f2d22SPoul-Henning Kamp 		if (error)
148116734c4SMatthew Dillon 			goto done2;
149762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
150e5e820fdSRobert Watson #ifdef MAC
151e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
152e5e820fdSRobert Watson 		if (error) {
153e5e820fdSRobert Watson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
154e5e820fdSRobert Watson 			goto done2;
155e5e820fdSRobert Watson 		}
156e5e820fdSRobert Watson #endif
157b40ce416SJulian Elischer 		VOP_UNLOCK(nd.ni_vp, 0, td);
158c7d893deSDavid Greenman 		if (nd.ni_vp->v_type != VREG) {
1595b606744SJohan Karlsson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
160116734c4SMatthew Dillon 			error = EACCES;
161116734c4SMatthew Dillon 			goto done2;
162c7d893deSDavid Greenman 		}
163e5e820fdSRobert Watson #ifdef MAC
164e5e820fdSRobert Watson 	} else {
165e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, NULL);
166e5e820fdSRobert Watson 		if (error)
167e5e820fdSRobert Watson 			goto done2;
168e5e820fdSRobert Watson #endif
169c7d893deSDavid Greenman 	}
170c7d893deSDavid Greenman 
171c7d893deSDavid Greenman 	/*
172c7d893deSDavid Greenman 	 * If accounting was previously enabled, kill the old space-watcher,
173c7d893deSDavid Greenman 	 * close the file, and (if no new file was specified, leave).
174c7d893deSDavid Greenman 	 */
1754f39d5d5SAndrew R. Reiter 
176b4dcc46aSAndrew R. Reiter 	/*
177b4dcc46aSAndrew R. Reiter 	 * XXX arr: Should not hold lock over vnode operation.
178b4dcc46aSAndrew R. Reiter 	 */
179b4dcc46aSAndrew R. Reiter 
1804f39d5d5SAndrew R. Reiter 	mtx_lock(&acct_mtx);
181c7d893deSDavid Greenman 	if (acctp != NULLVP || savacctp != NULLVP) {
1824f559836SJake Burkholder 		callout_stop(&acctwatch_callout);
18392da2e76SJohan Karlsson 		error = vn_close((acctp != NULLVP ? acctp : savacctp),
1845b606744SJohan Karlsson 		    (acctp != NULLVP ? acctflags : savacctflags),
1852d701617SRobert Watson 		    (acctcred != NOCRED ? acctcred : savacctcred), td);
186c7d893deSDavid Greenman 		acctp = savacctp = NULLVP;
1872d701617SRobert Watson 		crfree(acctcred != NOCRED ? acctcred : savacctcred);
1882d701617SRobert Watson 		acctcred = savacctcred = NOCRED;
189c7d893deSDavid Greenman 	}
190b4dcc46aSAndrew R. Reiter 	if (SCARG(uap, path) == NULL) {
191b4dcc46aSAndrew R. Reiter 		mtx_unlock(&acct_mtx);
192116734c4SMatthew Dillon 		goto done2;
193b4dcc46aSAndrew R. Reiter 	}
194c7d893deSDavid Greenman 
195c7d893deSDavid Greenman 	/*
196c7d893deSDavid Greenman 	 * Save the new accounting file vnode, and schedule the new
197c7d893deSDavid Greenman 	 * free space watcher.
198c7d893deSDavid Greenman 	 */
199c7d893deSDavid Greenman 	acctp = nd.ni_vp;
2002d701617SRobert Watson 	acctcred = crhold(td->td_ucred);
2015b606744SJohan Karlsson 	acctflags = flags;
2024f559836SJake Burkholder 	callout_init(&acctwatch_callout, 0);
203b4dcc46aSAndrew R. Reiter 	mtx_unlock(&acct_mtx);
204c7d893deSDavid Greenman 	acctwatch(NULL);
205116734c4SMatthew Dillon done2:
206116734c4SMatthew Dillon 	mtx_unlock(&Giant);
207c7d893deSDavid Greenman 	return (error);
208c7d893deSDavid Greenman }
209c7d893deSDavid Greenman 
210c7d893deSDavid Greenman /*
211c7d893deSDavid Greenman  * Write out process accounting information, on process exit.
212c7d893deSDavid Greenman  * Data to be written out is specified in Leffler, et al.
213c7d893deSDavid Greenman  * and are enumerated below.  (They're also noted in the system
214c7d893deSDavid Greenman  * "acct.h" header file.)
215c7d893deSDavid Greenman  */
216c7d893deSDavid Greenman 
217c7d893deSDavid Greenman int
218b40ce416SJulian Elischer acct_process(td)
219b40ce416SJulian Elischer 	struct thread *td;
220c7d893deSDavid Greenman {
221b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
222c7d893deSDavid Greenman 	struct acct acct;
223c7d893deSDavid Greenman 	struct rusage *r;
224c7d893deSDavid Greenman 	struct timeval ut, st, tmp;
2254f39d5d5SAndrew R. Reiter 	int t, ret;
226c7d893deSDavid Greenman 	struct vnode *vp;
2274f39d5d5SAndrew R. Reiter 	struct ucred *uc;
2284f39d5d5SAndrew R. Reiter 
2294f39d5d5SAndrew R. Reiter 	mtx_lock(&acct_mtx);
230c7d893deSDavid Greenman 
231c7d893deSDavid Greenman 	/* If accounting isn't enabled, don't bother */
232c7d893deSDavid Greenman 	vp = acctp;
2334f39d5d5SAndrew R. Reiter 	if (vp == NULLVP) {
2344f39d5d5SAndrew R. Reiter 		mtx_unlock(&acct_mtx);
235c7d893deSDavid Greenman 		return (0);
2364f39d5d5SAndrew R. Reiter 	}
237c7d893deSDavid Greenman 
238c7d893deSDavid Greenman 	/*
239c7d893deSDavid Greenman 	 * Get process accounting information.
240c7d893deSDavid Greenman 	 */
241c7d893deSDavid Greenman 
242c7d893deSDavid Greenman 	/* (1) The name of the command that ran */
243c7d893deSDavid Greenman 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
244c7d893deSDavid Greenman 
245c7d893deSDavid Greenman 	/* (2) The amount of user and system time that was used */
2469ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
247c7d893deSDavid Greenman 	calcru(p, &ut, &st, NULL);
2489ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
249c7d893deSDavid Greenman 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
250c7d893deSDavid Greenman 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
251c7d893deSDavid Greenman 
252c7d893deSDavid Greenman 	/* (3) The elapsed time the commmand ran (and its starting time) */
253c7d893deSDavid Greenman 	acct.ac_btime = p->p_stats->p_start.tv_sec;
25457585a8eSMike Pritchard 	microtime(&tmp);
255c7d893deSDavid Greenman 	timevalsub(&tmp, &p->p_stats->p_start);
256c7d893deSDavid Greenman 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
257c7d893deSDavid Greenman 
258c7d893deSDavid Greenman 	/* (4) The average amount of memory used */
259c7d893deSDavid Greenman 	r = &p->p_stats->p_ru;
260c7d893deSDavid Greenman 	tmp = ut;
261c7d893deSDavid Greenman 	timevaladd(&tmp, &st);
262c7d893deSDavid Greenman 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
263c7d893deSDavid Greenman 	if (t)
264c7d893deSDavid Greenman 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
265c7d893deSDavid Greenman 	else
266c7d893deSDavid Greenman 		acct.ac_mem = 0;
267c7d893deSDavid Greenman 
268c7d893deSDavid Greenman 	/* (5) The number of disk I/O operations done */
269c7d893deSDavid Greenman 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
270c7d893deSDavid Greenman 
271c7d893deSDavid Greenman 	/* (6) The UID and GID of the process */
272b1fc0ec1SRobert Watson 	acct.ac_uid = p->p_ucred->cr_ruid;
273b1fc0ec1SRobert Watson 	acct.ac_gid = p->p_ucred->cr_rgid;
274c7d893deSDavid Greenman 
275c7d893deSDavid Greenman 	/* (7) The terminal from which the process was started */
276f591779bSSeigo Tanimura 	PROC_LOCK(p);
277f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
278c7d893deSDavid Greenman 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
27923d76283SPoul-Henning Kamp 		acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
280c7d893deSDavid Greenman 	else
28123d76283SPoul-Henning Kamp 		acct.ac_tty = NOUDEV;
282f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
283f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
284c7d893deSDavid Greenman 
285c7d893deSDavid Greenman 	/* (8) The boolean flags that tell how the process terminated, etc. */
286c7d893deSDavid Greenman 	acct.ac_flag = p->p_acflag;
287c7d893deSDavid Greenman 
288c7d893deSDavid Greenman 	/*
289b5afad71SDavid Greenman 	 * Eliminate any file size rlimit.
290b5afad71SDavid Greenman 	 */
291b5afad71SDavid Greenman 	if (p->p_limit->p_refcnt > 1 &&
292b5afad71SDavid Greenman 	    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
293b5afad71SDavid Greenman 		p->p_limit->p_refcnt--;
294b5afad71SDavid Greenman 		p->p_limit = limcopy(p->p_limit);
295b5afad71SDavid Greenman 	}
296b5afad71SDavid Greenman 	p->p_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
297b5afad71SDavid Greenman 
298b5afad71SDavid Greenman 	/*
299b5afad71SDavid Greenman 	 * Write the accounting information to the file.
300c7d893deSDavid Greenman 	 */
3014f39d5d5SAndrew R. Reiter 	uc = crhold(acctcred);
3024f39d5d5SAndrew R. Reiter 	vref(vp);
3034f39d5d5SAndrew R. Reiter 	mtx_unlock(&acct_mtx);
304289c6deaSRobert Watson 	VOP_LEASE(vp, td, uc, LEASE_WRITE);
3054f39d5d5SAndrew R. Reiter 	ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
3064f39d5d5SAndrew R. Reiter 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, uc, NOCRED,
3074f39d5d5SAndrew R. Reiter 	    (int *)0, td);
3084f39d5d5SAndrew R. Reiter 	vrele(vp);
3094f39d5d5SAndrew R. Reiter 	crfree(uc);
3104f39d5d5SAndrew R. Reiter 	return (ret);
311c7d893deSDavid Greenman }
312c7d893deSDavid Greenman 
313c7d893deSDavid Greenman /*
314c7d893deSDavid Greenman  * Encode_comp_t converts from ticks in seconds and microseconds
315c7d893deSDavid Greenman  * to ticks in 1/AHZ seconds.  The encoding is described in
316c7d893deSDavid Greenman  * Leffler, et al., on page 63.
317c7d893deSDavid Greenman  */
318c7d893deSDavid Greenman 
319c7d893deSDavid Greenman #define	MANTSIZE	13			/* 13 bit mantissa. */
320c7d893deSDavid Greenman #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
321c7d893deSDavid Greenman #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
322c7d893deSDavid Greenman 
32387b6de2bSPoul-Henning Kamp static comp_t
324c7d893deSDavid Greenman encode_comp_t(s, us)
325c7d893deSDavid Greenman 	u_long s, us;
326c7d893deSDavid Greenman {
327c7d893deSDavid Greenman 	int exp, rnd;
328c7d893deSDavid Greenman 
329c7d893deSDavid Greenman 	exp = 0;
330c7d893deSDavid Greenman 	rnd = 0;
331c7d893deSDavid Greenman 	s *= AHZ;
332c7d893deSDavid Greenman 	s += us / (1000000 / AHZ);	/* Maximize precision. */
333c7d893deSDavid Greenman 
334c7d893deSDavid Greenman 	while (s > MAXFRACT) {
335c7d893deSDavid Greenman 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
336c7d893deSDavid Greenman 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
337c7d893deSDavid Greenman 		exp++;
338c7d893deSDavid Greenman 	}
339c7d893deSDavid Greenman 
340c7d893deSDavid Greenman 	/* If we need to round up, do it (and handle overflow correctly). */
341c7d893deSDavid Greenman 	if (rnd && (++s > MAXFRACT)) {
342c7d893deSDavid Greenman 		s >>= EXPSIZE;
343c7d893deSDavid Greenman 		exp++;
344c7d893deSDavid Greenman 	}
345c7d893deSDavid Greenman 
346c7d893deSDavid Greenman 	/* Clean it up and polish it off. */
347c7d893deSDavid Greenman 	exp <<= MANTSIZE;		/* Shift the exponent into place */
348c7d893deSDavid Greenman 	exp += s;			/* and add on the mantissa. */
349c7d893deSDavid Greenman 	return (exp);
350c7d893deSDavid Greenman }
351c7d893deSDavid Greenman 
352c7d893deSDavid Greenman /*
353c7d893deSDavid Greenman  * Periodically check the filesystem to see if accounting
354c7d893deSDavid Greenman  * should be turned on or off.  Beware the case where the vnode
355c7d893deSDavid Greenman  * has been vgone()'d out from underneath us, e.g. when the file
356c7d893deSDavid Greenman  * system containing the accounting file has been forcibly unmounted.
357c7d893deSDavid Greenman  */
358df8bae1dSRodney W. Grimes /* ARGSUSED */
35987b6de2bSPoul-Henning Kamp static void
360df8bae1dSRodney W. Grimes acctwatch(a)
361df8bae1dSRodney W. Grimes 	void *a;
362df8bae1dSRodney W. Grimes {
363df8bae1dSRodney W. Grimes 	struct statfs sb;
364df8bae1dSRodney W. Grimes 
3654f39d5d5SAndrew R. Reiter 	mtx_lock(&acct_mtx);
3664f39d5d5SAndrew R. Reiter 
367b4dcc46aSAndrew R. Reiter 	/*
368b4dcc46aSAndrew R. Reiter 	 * XXX arr: Need to fix the issue of holding acct_mtx over
369b4dcc46aSAndrew R. Reiter 	 * the below vnode operations.
370b4dcc46aSAndrew R. Reiter 	 */
371b4dcc46aSAndrew R. Reiter 
372c7d893deSDavid Greenman 	if (savacctp != NULLVP) {
373c7d893deSDavid Greenman 		if (savacctp->v_type == VBAD) {
3742d701617SRobert Watson 			(void) vn_close(savacctp, savacctflags, savacctcred,
3752d701617SRobert Watson 			    NULL);
376c7d893deSDavid Greenman 			savacctp = NULLVP;
3772d701617SRobert Watson 			savacctcred = NOCRED;
3784f39d5d5SAndrew R. Reiter 			mtx_unlock(&acct_mtx);
379c7d893deSDavid Greenman 			return;
380c7d893deSDavid Greenman 		}
381b40ce416SJulian Elischer 		(void)VFS_STATFS(savacctp->v_mount, &sb, (struct thread *)0);
382df8bae1dSRodney W. Grimes 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
383df8bae1dSRodney W. Grimes 			acctp = savacctp;
3842d701617SRobert Watson 			acctcred = savacctcred;
3855b606744SJohan Karlsson 			acctflags = savacctflags;
386c7d893deSDavid Greenman 			savacctp = NULLVP;
3872d701617SRobert Watson 			savacctcred = NOCRED;
388df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting resumed\n");
389df8bae1dSRodney W. Grimes 		}
390996c772fSJohn Dyson 	} else {
391b4dcc46aSAndrew R. Reiter 		if (acctp == NULLVP) {
392b4dcc46aSAndrew R. Reiter 			mtx_unlock(&acct_mtx);
393996c772fSJohn Dyson 			return;
394b4dcc46aSAndrew R. Reiter 		}
395c7d893deSDavid Greenman 		if (acctp->v_type == VBAD) {
3962d701617SRobert Watson 			(void) vn_close(acctp, acctflags, acctcred, NULL);
397c7d893deSDavid Greenman 			acctp = NULLVP;
3982d701617SRobert Watson 			crfree(acctcred);
3992d701617SRobert Watson 			acctcred = NOCRED;
4004f39d5d5SAndrew R. Reiter 			mtx_unlock(&acct_mtx);
401df8bae1dSRodney W. Grimes 			return;
402c7d893deSDavid Greenman 		}
403b40ce416SJulian Elischer 		(void)VFS_STATFS(acctp->v_mount, &sb, (struct thread *)0);
404df8bae1dSRodney W. Grimes 		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
405df8bae1dSRodney W. Grimes 			savacctp = acctp;
4065b606744SJohan Karlsson 			savacctflags = acctflags;
407b497ca81SRobert Watson 			savacctcred = acctcred;
408c7d893deSDavid Greenman 			acctp = NULLVP;
4092d701617SRobert Watson 			acctcred = NOCRED;
410df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting suspended\n");
411df8bae1dSRodney W. Grimes 		}
412996c772fSJohn Dyson 	}
4134f559836SJake Burkholder 	callout_reset(&acctwatch_callout, acctchkfreq * hz, acctwatch, NULL);
4144f39d5d5SAndrew R. Reiter 	mtx_unlock(&acct_mtx);
415df8bae1dSRodney W. Grimes }
416