xref: /freebsd/sys/kern/kern_acct.c (revision 01e3f3ae4fbff81bfc3aa7a2e569c3f1b7f6e5d2)
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
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42677b542eSDavid E. O'Brien #include <sys/cdefs.h>
43677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
44677b542eSDavid E. O'Brien 
45e5e820fdSRobert Watson #include "opt_mac.h"
46e5e820fdSRobert Watson 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
480ad076d5SBruce Evans #include <sys/systm.h>
49fb919e4dSMark Murray #include <sys/lock.h>
50fb919e4dSMark Murray #include <sys/mutex.h>
51d2d3e875SBruce Evans #include <sys/sysproto.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
53e5e820fdSRobert Watson #include <sys/mac.h>
54df8bae1dSRodney W. Grimes #include <sys/mount.h>
55df8bae1dSRodney W. Grimes #include <sys/vnode.h>
563ac4d1efSBruce Evans #include <sys/fcntl.h>
57df8bae1dSRodney W. Grimes #include <sys/syslog.h>
58df8bae1dSRodney W. Grimes #include <sys/kernel.h>
59996c772fSJohn Dyson #include <sys/sysent.h>
6087b6de2bSPoul-Henning Kamp #include <sys/sysctl.h>
61c7d893deSDavid Greenman #include <sys/namei.h>
62c7d893deSDavid Greenman #include <sys/acct.h>
63c7d893deSDavid Greenman #include <sys/resourcevar.h>
64c7d893deSDavid Greenman #include <sys/tty.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes /*
67c7d893deSDavid Greenman  * The routines implemented in this file are described in:
68c7d893deSDavid Greenman  *      Leffler, et al.: The Design and Implementation of the 4.3BSD
69c7d893deSDavid Greenman  *	    UNIX Operating System (Addison Welley, 1989)
70c7d893deSDavid Greenman  * on pages 62-63.
71c7d893deSDavid Greenman  *
72c7d893deSDavid Greenman  * Arguably, to simplify accounting operations, this mechanism should
73c7d893deSDavid Greenman  * be replaced by one in which an accounting log file (similar to /dev/klog)
74c7d893deSDavid Greenman  * is read by a user process, etc.  However, that has its own problems.
75df8bae1dSRodney W. Grimes  */
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes /*
78c7d893deSDavid Greenman  * Internal accounting functions.
79c7d893deSDavid Greenman  * The former's operation is described in Leffler, et al., and the latter
80c7d893deSDavid Greenman  * was provided by UCB with the 4.4BSD-Lite release
81df8bae1dSRodney W. Grimes  */
824d77a549SAlfred Perlstein static comp_t	encode_comp_t(u_long, u_long);
834d77a549SAlfred Perlstein static void	acctwatch(void *);
84c7d893deSDavid Greenman 
85c7d893deSDavid Greenman /*
864f559836SJake Burkholder  * Accounting callout used for periodic scheduling of acctwatch.
87ab36c067SJustin T. Gibbs  */
884f559836SJake Burkholder static struct	callout acctwatch_callout;
89ab36c067SJustin T. Gibbs 
90ab36c067SJustin T. Gibbs /*
915b606744SJohan Karlsson  * Accounting vnode pointer, saved vnode pointer, and flags for each.
92c7d893deSDavid Greenman  */
9387b6de2bSPoul-Henning Kamp static struct	vnode *acctp;
942d701617SRobert Watson static struct	ucred *acctcred;
955b606744SJohan Karlsson static int	acctflags;
9687b6de2bSPoul-Henning Kamp static struct	vnode *savacctp;
972d701617SRobert Watson static struct	ucred *savacctcred;
985b606744SJohan Karlsson static int	savacctflags;
99df8bae1dSRodney W. Grimes 
1004f39d5d5SAndrew R. Reiter static struct mtx	acct_mtx;
1014f39d5d5SAndrew R. Reiter MTX_SYSINIT(acct, &acct_mtx, "accounting", MTX_DEF);
1024f39d5d5SAndrew R. Reiter 
103df8bae1dSRodney W. Grimes /*
104df8bae1dSRodney W. Grimes  * Values associated with enabling and disabling accounting
105df8bae1dSRodney W. Grimes  */
10687b6de2bSPoul-Henning Kamp static int acctsuspend = 2;	/* stop accounting when < 2% free space left */
10787b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_suspend, CTLFLAG_RW,
10847fdd692SNeil Blakey-Milner 	&acctsuspend, 0, "percentage of free disk space below which accounting stops");
10987b6de2bSPoul-Henning Kamp 
11087b6de2bSPoul-Henning Kamp static int acctresume = 4;	/* resume when free space risen to > 4% */
11187b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_resume, CTLFLAG_RW,
11247fdd692SNeil Blakey-Milner 	&acctresume, 0, "percentage of free disk space above which accounting resumes");
11387b6de2bSPoul-Henning Kamp 
11487b6de2bSPoul-Henning Kamp static int acctchkfreq = 15;	/* frequency (in seconds) to check space */
11587b6de2bSPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, acct_chkfreq, CTLFLAG_RW,
11647fdd692SNeil Blakey-Milner 	&acctchkfreq, 0, "frequency for checking the free space");
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes /*
119c7d893deSDavid Greenman  * Accounting system call.  Written based on the specification and
120c7d893deSDavid Greenman  * previous implementation done by Mark Tinguely.
121116734c4SMatthew Dillon  *
122116734c4SMatthew Dillon  * MPSAFE
123df8bae1dSRodney W. Grimes  */
124c7d893deSDavid Greenman int
125b40ce416SJulian Elischer acct(td, uap)
126b40ce416SJulian Elischer 	struct thread *td;
127996c772fSJohn Dyson 	struct acct_args /* {
128b80521feSAlfred Perlstein 		char *path;
129996c772fSJohn Dyson 	} */ *uap;
130c7d893deSDavid Greenman {
131c7d893deSDavid Greenman 	struct nameidata nd;
132e6796b67SKirk McKusick 	int error, flags;
133c7d893deSDavid Greenman 
134c7d893deSDavid Greenman 	/* Make sure that the caller is root. */
13544731cabSJohn Baldwin 	error = suser(td);
136797f2d22SPoul-Henning Kamp 	if (error)
13716e7bc7bSJohn Baldwin 		return (error);
138c7d893deSDavid Greenman 
13916e7bc7bSJohn Baldwin 	mtx_lock(&Giant);
14001e3f3aeSBruce Evans 
141c7d893deSDavid Greenman 	/*
142c7d893deSDavid Greenman 	 * If accounting is to be started to a file, open that file for
14392da2e76SJohan Karlsson 	 * appending and make sure it's a 'normal'.
144c7d893deSDavid Greenman 	 */
145d1e405c5SAlfred Perlstein 	if (uap->path != NULL) {
146f97182acSAlfred Perlstein 		NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, uap->path, td);
14792da2e76SJohan Karlsson 		flags = FWRITE | O_APPEND;
1487c89f162SPoul-Henning Kamp 		error = vn_open(&nd, &flags, 0, -1);
149797f2d22SPoul-Henning Kamp 		if (error)
150116734c4SMatthew Dillon 			goto done2;
151762e6b85SEivind Eklund 		NDFREE(&nd, NDF_ONLY_PNBUF);
152e5e820fdSRobert Watson #ifdef MAC
153e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, nd.ni_vp);
154e5e820fdSRobert Watson 		if (error) {
155e5e820fdSRobert Watson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
156e5e820fdSRobert Watson 			goto done2;
157e5e820fdSRobert Watson 		}
158e5e820fdSRobert Watson #endif
159b40ce416SJulian Elischer 		VOP_UNLOCK(nd.ni_vp, 0, td);
160c7d893deSDavid Greenman 		if (nd.ni_vp->v_type != VREG) {
1615b606744SJohan Karlsson 			vn_close(nd.ni_vp, flags, td->td_ucred, td);
162116734c4SMatthew Dillon 			error = EACCES;
163116734c4SMatthew Dillon 			goto done2;
164c7d893deSDavid Greenman 		}
165e5e820fdSRobert Watson #ifdef MAC
166e5e820fdSRobert Watson 	} else {
167e5e820fdSRobert Watson 		error = mac_check_system_acct(td->td_ucred, NULL);
168e5e820fdSRobert Watson 		if (error)
169e5e820fdSRobert Watson 			goto done2;
170e5e820fdSRobert Watson #endif
171c7d893deSDavid Greenman 	}
172c7d893deSDavid Greenman 
17301e3f3aeSBruce Evans 	mtx_lock(&acct_mtx);
17401e3f3aeSBruce Evans 
175c7d893deSDavid Greenman 	/*
176c7d893deSDavid Greenman 	 * If accounting was previously enabled, kill the old space-watcher,
177c7d893deSDavid Greenman 	 * close the file, and (if no new file was specified, leave).
17801e3f3aeSBruce Evans 	 *
17901e3f3aeSBruce Evans 	 * XXX arr: should not hold lock over vnode operation.
180c7d893deSDavid Greenman 	 */
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;
18948719ca7SBosko Milekic 		log(LOG_NOTICE, "Accounting disabled\n");
190c7d893deSDavid Greenman 	}
191d1e405c5SAlfred Perlstein 	if (uap->path == NULL) {
192b4dcc46aSAndrew R. Reiter 		mtx_unlock(&acct_mtx);
193116734c4SMatthew Dillon 		goto done2;
194b4dcc46aSAndrew R. Reiter 	}
195c7d893deSDavid Greenman 
196c7d893deSDavid Greenman 	/*
197c7d893deSDavid Greenman 	 * Save the new accounting file vnode, and schedule the new
198c7d893deSDavid Greenman 	 * free space watcher.
199c7d893deSDavid Greenman 	 */
200c7d893deSDavid Greenman 	acctp = nd.ni_vp;
2012d701617SRobert Watson 	acctcred = crhold(td->td_ucred);
2025b606744SJohan Karlsson 	acctflags = flags;
2034f559836SJake Burkholder 	callout_init(&acctwatch_callout, 0);
204b4dcc46aSAndrew R. Reiter 	mtx_unlock(&acct_mtx);
20548719ca7SBosko Milekic 	log(LOG_NOTICE, "Accounting enabled\n");
206c7d893deSDavid Greenman 	acctwatch(NULL);
20701e3f3aeSBruce Evans 
208116734c4SMatthew Dillon done2:
209116734c4SMatthew Dillon 	mtx_unlock(&Giant);
210c7d893deSDavid Greenman 	return (error);
211c7d893deSDavid Greenman }
212c7d893deSDavid Greenman 
213c7d893deSDavid Greenman /*
214c7d893deSDavid Greenman  * Write out process accounting information, on process exit.
215c7d893deSDavid Greenman  * Data to be written out is specified in Leffler, et al.
216c7d893deSDavid Greenman  * and are enumerated below.  (They're also noted in the system
217c7d893deSDavid Greenman  * "acct.h" header file.)
218c7d893deSDavid Greenman  */
219c7d893deSDavid Greenman int
220b40ce416SJulian Elischer acct_process(td)
221b40ce416SJulian Elischer 	struct thread *td;
222c7d893deSDavid Greenman {
223c7d893deSDavid Greenman 	struct acct acct;
224c7d893deSDavid Greenman 	struct timeval ut, st, tmp;
22591d5354aSJohn Baldwin 	struct plimit *newlim, *oldlim;
22601e3f3aeSBruce Evans 	struct proc *p;
22701e3f3aeSBruce Evans 	struct rusage *r;
22801e3f3aeSBruce Evans 	struct ucred *uc;
22901e3f3aeSBruce Evans 	struct vnode *vp;
23001e3f3aeSBruce Evans 	int t, ret;
2314f39d5d5SAndrew R. Reiter 
2324f39d5d5SAndrew R. Reiter 	mtx_lock(&acct_mtx);
233c7d893deSDavid Greenman 
234c7d893deSDavid Greenman 	/* If accounting isn't enabled, don't bother */
235c7d893deSDavid Greenman 	vp = acctp;
2364f39d5d5SAndrew R. Reiter 	if (vp == NULLVP) {
2374f39d5d5SAndrew R. Reiter 		mtx_unlock(&acct_mtx);
238c7d893deSDavid Greenman 		return (0);
2394f39d5d5SAndrew R. Reiter 	}
240c7d893deSDavid Greenman 
24101e3f3aeSBruce Evans 	p = td->td_proc;
24201e3f3aeSBruce Evans 
243c7d893deSDavid Greenman 	/*
244c7d893deSDavid Greenman 	 * Get process accounting information.
245c7d893deSDavid Greenman 	 */
246c7d893deSDavid Greenman 
2477e653dbdSJohn Baldwin 	PROC_LOCK(p);
248c7d893deSDavid Greenman 	/* (1) The name of the command that ran */
249c7d893deSDavid Greenman 	bcopy(p->p_comm, acct.ac_comm, sizeof acct.ac_comm);
250c7d893deSDavid Greenman 
251c7d893deSDavid Greenman 	/* (2) The amount of user and system time that was used */
2529ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
253c7d893deSDavid Greenman 	calcru(p, &ut, &st, NULL);
2549ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
255c7d893deSDavid Greenman 	acct.ac_utime = encode_comp_t(ut.tv_sec, ut.tv_usec);
256c7d893deSDavid Greenman 	acct.ac_stime = encode_comp_t(st.tv_sec, st.tv_usec);
257c7d893deSDavid Greenman 
2585f9ae8e0SGiorgos Keramidas 	/* (3) The elapsed time the command ran (and its starting time) */
25987ccef7bSDag-Erling Smørgrav 	tmp = boottime;
26087ccef7bSDag-Erling Smørgrav 	timevaladd(&tmp, &p->p_stats->p_start);
26187ccef7bSDag-Erling Smørgrav 	acct.ac_btime = tmp.tv_sec;
26287ccef7bSDag-Erling Smørgrav 	microuptime(&tmp);
263c7d893deSDavid Greenman 	timevalsub(&tmp, &p->p_stats->p_start);
264c7d893deSDavid Greenman 	acct.ac_etime = encode_comp_t(tmp.tv_sec, tmp.tv_usec);
265c7d893deSDavid Greenman 
266c7d893deSDavid Greenman 	/* (4) The average amount of memory used */
267c7d893deSDavid Greenman 	r = &p->p_stats->p_ru;
268c7d893deSDavid Greenman 	tmp = ut;
269c7d893deSDavid Greenman 	timevaladd(&tmp, &st);
270c7d893deSDavid Greenman 	t = tmp.tv_sec * hz + tmp.tv_usec / tick;
271c7d893deSDavid Greenman 	if (t)
272c7d893deSDavid Greenman 		acct.ac_mem = (r->ru_ixrss + r->ru_idrss + r->ru_isrss) / t;
273c7d893deSDavid Greenman 	else
274c7d893deSDavid Greenman 		acct.ac_mem = 0;
275c7d893deSDavid Greenman 
276c7d893deSDavid Greenman 	/* (5) The number of disk I/O operations done */
277c7d893deSDavid Greenman 	acct.ac_io = encode_comp_t(r->ru_inblock + r->ru_oublock, 0);
278c7d893deSDavid Greenman 
279c7d893deSDavid Greenman 	/* (6) The UID and GID of the process */
280b1fc0ec1SRobert Watson 	acct.ac_uid = p->p_ucred->cr_ruid;
281b1fc0ec1SRobert Watson 	acct.ac_gid = p->p_ucred->cr_rgid;
282c7d893deSDavid Greenman 
283c7d893deSDavid Greenman 	/* (7) The terminal from which the process was started */
284f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
285c7d893deSDavid Greenman 	if ((p->p_flag & P_CONTROLT) && p->p_pgrp->pg_session->s_ttyp)
28623d76283SPoul-Henning Kamp 		acct.ac_tty = dev2udev(p->p_pgrp->pg_session->s_ttyp->t_dev);
287c7d893deSDavid Greenman 	else
28823d76283SPoul-Henning Kamp 		acct.ac_tty = NOUDEV;
289f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
290c7d893deSDavid Greenman 
291c7d893deSDavid Greenman 	/* (8) The boolean flags that tell how the process terminated, etc. */
292c7d893deSDavid Greenman 	acct.ac_flag = p->p_acflag;
2937e653dbdSJohn Baldwin 	PROC_UNLOCK(p);
294c7d893deSDavid Greenman 
295c7d893deSDavid Greenman 	/*
29601e3f3aeSBruce Evans 	 * Finish doing things that require acct_mtx, and release acct_mtx.
2978b5f8b06SBill Fenner 	 */
2988b5f8b06SBill Fenner 	uc = crhold(acctcred);
2998b5f8b06SBill Fenner 	vref(vp);
3008b5f8b06SBill Fenner 	mtx_unlock(&acct_mtx);
3018b5f8b06SBill Fenner 
3028b5f8b06SBill Fenner 	/*
303b5afad71SDavid Greenman 	 * Eliminate any file size rlimit.
304b5afad71SDavid Greenman 	 */
30591d5354aSJohn Baldwin 	newlim = lim_alloc();
30691d5354aSJohn Baldwin 	PROC_LOCK(p);
30791d5354aSJohn Baldwin 	oldlim = p->p_limit;
30891d5354aSJohn Baldwin 	lim_copy(newlim, oldlim);
30991d5354aSJohn Baldwin 	newlim->pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
31091d5354aSJohn Baldwin 	p->p_limit = newlim;
31191d5354aSJohn Baldwin 	PROC_UNLOCK(p);
31291d5354aSJohn Baldwin 	lim_free(oldlim);
313b5afad71SDavid Greenman 
31401e3f3aeSBruce Evans 	/*
31501e3f3aeSBruce Evans 	 * Write the accounting information to the file.
31601e3f3aeSBruce Evans 	 */
317289c6deaSRobert Watson 	VOP_LEASE(vp, td, uc, LEASE_WRITE);
3184f39d5d5SAndrew R. Reiter 	ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)&acct, sizeof (acct),
3194f39d5d5SAndrew R. Reiter 	    (off_t)0, UIO_SYSSPACE, IO_APPEND|IO_UNIT, uc, NOCRED,
3204f39d5d5SAndrew R. Reiter 	    (int *)0, td);
3214f39d5d5SAndrew R. Reiter 	vrele(vp);
3224f39d5d5SAndrew R. Reiter 	crfree(uc);
3234f39d5d5SAndrew R. Reiter 	return (ret);
324c7d893deSDavid Greenman }
325c7d893deSDavid Greenman 
326c7d893deSDavid Greenman /*
327c7d893deSDavid Greenman  * Encode_comp_t converts from ticks in seconds and microseconds
328c7d893deSDavid Greenman  * to ticks in 1/AHZ seconds.  The encoding is described in
329c7d893deSDavid Greenman  * Leffler, et al., on page 63.
330c7d893deSDavid Greenman  */
331c7d893deSDavid Greenman 
332c7d893deSDavid Greenman #define	MANTSIZE	13			/* 13 bit mantissa. */
333c7d893deSDavid Greenman #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
334c7d893deSDavid Greenman #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
335c7d893deSDavid Greenman 
33687b6de2bSPoul-Henning Kamp static comp_t
337c7d893deSDavid Greenman encode_comp_t(s, us)
338c7d893deSDavid Greenman 	u_long s, us;
339c7d893deSDavid Greenman {
340c7d893deSDavid Greenman 	int exp, rnd;
341c7d893deSDavid Greenman 
342c7d893deSDavid Greenman 	exp = 0;
343c7d893deSDavid Greenman 	rnd = 0;
344c7d893deSDavid Greenman 	s *= AHZ;
345c7d893deSDavid Greenman 	s += us / (1000000 / AHZ);	/* Maximize precision. */
346c7d893deSDavid Greenman 
347c7d893deSDavid Greenman 	while (s > MAXFRACT) {
348c7d893deSDavid Greenman 	rnd = s & (1 << (EXPSIZE - 1));	/* Round up? */
349c7d893deSDavid Greenman 		s >>= EXPSIZE;		/* Base 8 exponent == 3 bit shift. */
350c7d893deSDavid Greenman 		exp++;
351c7d893deSDavid Greenman 	}
352c7d893deSDavid Greenman 
353c7d893deSDavid Greenman 	/* If we need to round up, do it (and handle overflow correctly). */
354c7d893deSDavid Greenman 	if (rnd && (++s > MAXFRACT)) {
355c7d893deSDavid Greenman 		s >>= EXPSIZE;
356c7d893deSDavid Greenman 		exp++;
357c7d893deSDavid Greenman 	}
358c7d893deSDavid Greenman 
359c7d893deSDavid Greenman 	/* Clean it up and polish it off. */
360c7d893deSDavid Greenman 	exp <<= MANTSIZE;		/* Shift the exponent into place */
361c7d893deSDavid Greenman 	exp += s;			/* and add on the mantissa. */
362c7d893deSDavid Greenman 	return (exp);
363c7d893deSDavid Greenman }
364c7d893deSDavid Greenman 
365c7d893deSDavid Greenman /*
366c7d893deSDavid Greenman  * Periodically check the filesystem to see if accounting
367c7d893deSDavid Greenman  * should be turned on or off.  Beware the case where the vnode
368c7d893deSDavid Greenman  * has been vgone()'d out from underneath us, e.g. when the file
369c7d893deSDavid Greenman  * system containing the accounting file has been forcibly unmounted.
370c7d893deSDavid Greenman  */
371df8bae1dSRodney W. Grimes /* ARGSUSED */
37287b6de2bSPoul-Henning Kamp static void
373df8bae1dSRodney W. Grimes acctwatch(a)
374df8bae1dSRodney W. Grimes 	void *a;
375df8bae1dSRodney W. Grimes {
376df8bae1dSRodney W. Grimes 	struct statfs sb;
377df8bae1dSRodney W. Grimes 
3784f39d5d5SAndrew R. Reiter 	mtx_lock(&acct_mtx);
3794f39d5d5SAndrew R. Reiter 
380b4dcc46aSAndrew R. Reiter 	/*
38101e3f3aeSBruce Evans 	 * XXX arr: need to fix the issue of holding acct_mtx over
382b4dcc46aSAndrew R. Reiter 	 * the below vnode operations.
383b4dcc46aSAndrew R. Reiter 	 */
384c7d893deSDavid Greenman 	if (savacctp != NULLVP) {
385c7d893deSDavid Greenman 		if (savacctp->v_type == VBAD) {
3862d701617SRobert Watson 			(void) vn_close(savacctp, savacctflags, savacctcred,
3872d701617SRobert Watson 			    NULL);
388c7d893deSDavid Greenman 			savacctp = NULLVP;
3892d701617SRobert Watson 			savacctcred = NOCRED;
3904f39d5d5SAndrew R. Reiter 			mtx_unlock(&acct_mtx);
391c7d893deSDavid Greenman 			return;
392c7d893deSDavid Greenman 		}
393b40ce416SJulian Elischer 		(void)VFS_STATFS(savacctp->v_mount, &sb, (struct thread *)0);
394df8bae1dSRodney W. Grimes 		if (sb.f_bavail > acctresume * sb.f_blocks / 100) {
395df8bae1dSRodney W. Grimes 			acctp = savacctp;
3962d701617SRobert Watson 			acctcred = savacctcred;
3975b606744SJohan Karlsson 			acctflags = savacctflags;
398c7d893deSDavid Greenman 			savacctp = NULLVP;
3992d701617SRobert Watson 			savacctcred = NOCRED;
400df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting resumed\n");
401df8bae1dSRodney W. Grimes 		}
402996c772fSJohn Dyson 	} else {
403b4dcc46aSAndrew R. Reiter 		if (acctp == NULLVP) {
404b4dcc46aSAndrew R. Reiter 			mtx_unlock(&acct_mtx);
405996c772fSJohn Dyson 			return;
406b4dcc46aSAndrew R. Reiter 		}
407c7d893deSDavid Greenman 		if (acctp->v_type == VBAD) {
4082d701617SRobert Watson 			(void) vn_close(acctp, acctflags, acctcred, NULL);
409c7d893deSDavid Greenman 			acctp = NULLVP;
4102d701617SRobert Watson 			crfree(acctcred);
4112d701617SRobert Watson 			acctcred = NOCRED;
4124f39d5d5SAndrew R. Reiter 			mtx_unlock(&acct_mtx);
413df8bae1dSRodney W. Grimes 			return;
414c7d893deSDavid Greenman 		}
415b40ce416SJulian Elischer 		(void)VFS_STATFS(acctp->v_mount, &sb, (struct thread *)0);
416df8bae1dSRodney W. Grimes 		if (sb.f_bavail <= acctsuspend * sb.f_blocks / 100) {
417df8bae1dSRodney W. Grimes 			savacctp = acctp;
4185b606744SJohan Karlsson 			savacctflags = acctflags;
419b497ca81SRobert Watson 			savacctcred = acctcred;
420c7d893deSDavid Greenman 			acctp = NULLVP;
4212d701617SRobert Watson 			acctcred = NOCRED;
422df8bae1dSRodney W. Grimes 			log(LOG_NOTICE, "Accounting suspended\n");
423df8bae1dSRodney W. Grimes 		}
424996c772fSJohn Dyson 	}
4254f559836SJake Burkholder 	callout_reset(&acctwatch_callout, acctchkfreq * hz, acctwatch, NULL);
4264f39d5d5SAndrew R. Reiter 	mtx_unlock(&acct_mtx);
427df8bae1dSRodney W. Grimes }
428