quotacheck.c revision 1.36 1 1.36 christos /* $NetBSD: quotacheck.c,v 1.36 2004/12/12 05:57:03 christos Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.5 mycroft * Copyright (c) 1980, 1990, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Robert Elz at The University of Melbourne.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.28 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.15 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
38 1.15 lukem The Regents of the University of California. All rights reserved.\n");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.10 cgd #if 0
43 1.15 lukem static char sccsid[] = "@(#)quotacheck.c 8.6 (Berkeley) 4/28/95";
44 1.10 cgd #else
45 1.36 christos __RCSID("$NetBSD: quotacheck.c,v 1.36 2004/12/12 05:57:03 christos Exp $");
46 1.10 cgd #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Fix up / report on disk quotas & usage
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.1 cgd #include <sys/stat.h>
54 1.15 lukem #include <sys/queue.h>
55 1.5 mycroft
56 1.5 mycroft #include <ufs/ufs/dinode.h>
57 1.5 mycroft #include <ufs/ufs/quota.h>
58 1.17 bouyer #include <ufs/ufs/ufs_bswap.h>
59 1.5 mycroft #include <ufs/ffs/fs.h>
60 1.17 bouyer #include <ufs/ffs/ffs_extern.h>
61 1.5 mycroft
62 1.15 lukem #include <err.h>
63 1.1 cgd #include <fcntl.h>
64 1.1 cgd #include <fstab.h>
65 1.1 cgd #include <pwd.h>
66 1.1 cgd #include <grp.h>
67 1.1 cgd #include <errno.h>
68 1.1 cgd #include <unistd.h>
69 1.1 cgd #include <stdio.h>
70 1.1 cgd #include <stdlib.h>
71 1.1 cgd #include <string.h>
72 1.1 cgd
73 1.13 christos #include "fsutil.h"
74 1.1 cgd
75 1.36 christos #ifndef FS_UFS1_MAGIC
76 1.36 christos # define FS_UFS1_MAGIC FS_MAGIC /* 0x011954 */
77 1.36 christos # define FS_UFS1_MAGIC_SWAPPED 0x54190100 /* bswap32(0x011954) */
78 1.36 christos # define DINODE1_SIZE sizeof(struct dinode)
79 1.36 christos # define DINODE2_SIZE 0
80 1.36 christos #else
81 1.36 christos # define HAVE_UFSv2 1
82 1.36 christos #endif
83 1.36 christos
84 1.36 christos #ifndef SBLOCKSIZE
85 1.36 christos # define SBLOCKSIZE SBSIZE
86 1.36 christos #endif
87 1.36 christos #ifndef SBLOCKSEARCH
88 1.36 christos # define SBLOCKSEARCH { SBSIZE, -1 }
89 1.36 christos #endif
90 1.36 christos
91 1.36 christos static const char *qfname = QUOTAFILENAME;
92 1.36 christos static const char *qfextension[] = INITQFNAMES;
93 1.36 christos static const char *quotagroup = QUOTAGROUP;
94 1.13 christos
95 1.13 christos static union {
96 1.1 cgd struct fs sblk;
97 1.1 cgd char dummy[MAXBSIZE];
98 1.1 cgd } un;
99 1.1 cgd #define sblock un.sblk
100 1.13 christos static long dev_bsize;
101 1.13 christos static long maxino;
102 1.1 cgd
103 1.1 cgd struct quotaname {
104 1.1 cgd long flags;
105 1.1 cgd char grpqfname[MAXPATHLEN + 1];
106 1.1 cgd char usrqfname[MAXPATHLEN + 1];
107 1.1 cgd };
108 1.1 cgd #define HASUSR 1
109 1.1 cgd #define HASGRP 2
110 1.1 cgd
111 1.1 cgd struct fileusage {
112 1.1 cgd struct fileusage *fu_next;
113 1.1 cgd u_long fu_curinodes;
114 1.1 cgd u_long fu_curblocks;
115 1.36 christos u_int32_t fu_id; /* uid_t, gid_t */
116 1.1 cgd char fu_name[1];
117 1.1 cgd /* actually bigger */
118 1.1 cgd };
119 1.1 cgd #define FUHASH 1024 /* must be power of two */
120 1.13 christos static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
121 1.1 cgd
122 1.25 fvdl
123 1.36 christos union comb_dinode {
124 1.36 christos #ifdef HAVE_UFSv2
125 1.25 fvdl struct ufs1_dinode dp1;
126 1.25 fvdl struct ufs2_dinode dp2;
127 1.36 christos #else
128 1.36 christos struct dinode dp1;
129 1.36 christos #endif
130 1.25 fvdl };
131 1.36 christos #ifdef HAVE_UFSv2
132 1.25 fvdl #define DIP(dp, field) \
133 1.25 fvdl (is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
134 1.36 christos #else
135 1.36 christos #define DIP(dp, field) (dp)->dp1.di_##field
136 1.36 christos #endif
137 1.25 fvdl
138 1.25 fvdl
139 1.13 christos static int aflag; /* all file systems */
140 1.13 christos static int gflag; /* check group quotas */
141 1.13 christos static int uflag; /* check user quotas */
142 1.13 christos static int vflag; /* verbose */
143 1.29 christos static int qflag; /* quick but untidy mode */
144 1.13 christos static int fi; /* open disk file descriptor */
145 1.36 christos static u_int32_t highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
146 1.17 bouyer static int needswap; /* FS is in swapped order */
147 1.23 bouyer static int got_siginfo = 0; /* got a siginfo signal */
148 1.25 fvdl static int is_ufs2;
149 1.13 christos
150 1.13 christos
151 1.13 christos int main __P((int, char *[]));
152 1.13 christos static void usage __P((void));
153 1.13 christos static void *needchk __P((struct fstab *));
154 1.14 christos static int chkquota __P((const char *, const char *, const char *, void *,
155 1.14 christos pid_t *));
156 1.13 christos static int update __P((const char *, const char *, int));
157 1.36 christos static u_int32_t skipforward __P((u_int32_t, u_int32_t, FILE *));
158 1.18 mycroft static int oneof __P((const char *, char *[], int));
159 1.13 christos static int getquotagid __P((void));
160 1.13 christos static int hasquota __P((struct fstab *, int, char **));
161 1.36 christos static struct fileusage *lookup __P((u_int32_t, int));
162 1.36 christos static struct fileusage *addid __P((u_int32_t, int, const char *));
163 1.36 christos static u_int32_t subsequent __P((u_int32_t, int));
164 1.36 christos static union comb_dinode *getnextinode __P((ino_t));
165 1.25 fvdl static void setinodebuf __P((ino_t));
166 1.13 christos static void freeinodebuf __P((void));
167 1.13 christos static void bread __P((daddr_t, char *, long));
168 1.23 bouyer static void infohandler __P((int sig));
169 1.36 christos static void swap_dinode1(union comb_dinode *, int);
170 1.36 christos #ifdef HAVE_UFSv2
171 1.36 christos static void swap_dinode2(union comb_dinode *, int);
172 1.36 christos #endif
173 1.5 mycroft
174 1.5 mycroft int
175 1.1 cgd main(argc, argv)
176 1.1 cgd int argc;
177 1.5 mycroft char *argv[];
178 1.1 cgd {
179 1.13 christos struct fstab *fs;
180 1.13 christos struct passwd *pw;
181 1.13 christos struct group *gr;
182 1.5 mycroft struct quotaname *auxdata;
183 1.5 mycroft int i, argnum, maxrun, errs;
184 1.5 mycroft long done = 0;
185 1.14 christos int flags = CHECK_PREEN;
186 1.18 mycroft const char *name;
187 1.12 mark int ch;
188 1.1 cgd
189 1.5 mycroft errs = maxrun = 0;
190 1.29 christos while ((ch = getopt(argc, argv, "aguvqdl:")) != -1) {
191 1.1 cgd switch(ch) {
192 1.1 cgd case 'a':
193 1.1 cgd aflag++;
194 1.1 cgd break;
195 1.14 christos case 'd':
196 1.14 christos flags |= CHECK_DEBUG;
197 1.14 christos break;
198 1.1 cgd case 'g':
199 1.1 cgd gflag++;
200 1.1 cgd break;
201 1.1 cgd case 'u':
202 1.1 cgd uflag++;
203 1.1 cgd break;
204 1.29 christos case 'q':
205 1.29 christos qflag++;
206 1.29 christos break;
207 1.1 cgd case 'v':
208 1.1 cgd vflag++;
209 1.1 cgd break;
210 1.1 cgd case 'l':
211 1.1 cgd maxrun = atoi(optarg);
212 1.1 cgd break;
213 1.1 cgd default:
214 1.1 cgd usage();
215 1.1 cgd }
216 1.1 cgd }
217 1.1 cgd argc -= optind;
218 1.1 cgd argv += optind;
219 1.31 christos if ((argc == 0 && !aflag) || (argc > 0 && aflag) || (!aflag && maxrun))
220 1.1 cgd usage();
221 1.1 cgd if (!gflag && !uflag) {
222 1.1 cgd gflag++;
223 1.1 cgd uflag++;
224 1.1 cgd }
225 1.20 abs
226 1.20 abs /* If -a, we do not want to pay the cost of processing every
227 1.20 abs * group and password entry if there are no filesystems with quotas
228 1.20 abs */
229 1.20 abs if (aflag) {
230 1.20 abs i = 0;
231 1.20 abs while ((fs = getfsent()) != NULL) {
232 1.20 abs if (needchk(fs))
233 1.20 abs i=1;
234 1.20 abs }
235 1.20 abs endfsent();
236 1.20 abs if (!i) /* No filesystems with quotas */
237 1.20 abs exit(0);
238 1.20 abs }
239 1.20 abs
240 1.1 cgd if (gflag) {
241 1.1 cgd setgrent();
242 1.1 cgd while ((gr = getgrent()) != 0)
243 1.36 christos (void) addid((u_int32_t)gr->gr_gid, GRPQUOTA, gr->gr_name);
244 1.1 cgd endgrent();
245 1.1 cgd }
246 1.1 cgd if (uflag) {
247 1.1 cgd setpwent();
248 1.1 cgd while ((pw = getpwent()) != 0)
249 1.36 christos (void) addid((u_int32_t)pw->pw_uid, USRQUOTA, pw->pw_name);
250 1.1 cgd endpwent();
251 1.1 cgd }
252 1.1 cgd if (aflag)
253 1.14 christos exit(checkfstab(flags, maxrun, needchk, chkquota));
254 1.5 mycroft if (setfsent() == 0)
255 1.6 mycroft err(1, "%s: can't open", FSTAB);
256 1.1 cgd while ((fs = getfsent()) != NULL) {
257 1.1 cgd if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
258 1.1 cgd (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
259 1.1 cgd (auxdata = needchk(fs)) &&
260 1.1 cgd (name = blockcheck(fs->fs_spec))) {
261 1.1 cgd done |= 1 << argnum;
262 1.13 christos errs += chkquota(fs->fs_type, name, fs->fs_file,
263 1.14 christos auxdata, NULL);
264 1.1 cgd }
265 1.1 cgd }
266 1.1 cgd endfsent();
267 1.1 cgd for (i = 0; i < argc; i++)
268 1.1 cgd if ((done & (1 << i)) == 0)
269 1.1 cgd fprintf(stderr, "%s not found in %s\n",
270 1.1 cgd argv[i], FSTAB);
271 1.1 cgd exit(errs);
272 1.1 cgd }
273 1.1 cgd
274 1.13 christos static void
275 1.1 cgd usage()
276 1.1 cgd {
277 1.21 cgd
278 1.13 christos (void)fprintf(stderr,
279 1.33 jmmv "usage:\t%s -a [-gquv] [-l maxparallel]\n\t%s [-gquv] filesys ...\n", getprogname(),
280 1.21 cgd getprogname());
281 1.1 cgd exit(1);
282 1.1 cgd }
283 1.1 cgd
284 1.13 christos static void *
285 1.1 cgd needchk(fs)
286 1.13 christos struct fstab *fs;
287 1.1 cgd {
288 1.13 christos struct quotaname *qnp;
289 1.1 cgd char *qfnp;
290 1.1 cgd
291 1.11 jtc if (strcmp(fs->fs_vfstype, "ffs") ||
292 1.1 cgd strcmp(fs->fs_type, FSTAB_RW))
293 1.5 mycroft return (NULL);
294 1.5 mycroft if ((qnp = malloc(sizeof(*qnp))) == NULL)
295 1.6 mycroft err(1, "%s", strerror(errno));
296 1.1 cgd qnp->flags = 0;
297 1.1 cgd if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
298 1.27 itojun strlcpy(qnp->grpqfname, qfnp, sizeof(qnp->grpqfname));
299 1.1 cgd qnp->flags |= HASGRP;
300 1.1 cgd }
301 1.1 cgd if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
302 1.27 itojun strlcpy(qnp->usrqfname, qfnp, sizeof(qnp->usrqfname));
303 1.1 cgd qnp->flags |= HASUSR;
304 1.1 cgd }
305 1.1 cgd if (qnp->flags)
306 1.5 mycroft return (qnp);
307 1.5 mycroft free(qnp);
308 1.5 mycroft return (NULL);
309 1.1 cgd }
310 1.1 cgd
311 1.25 fvdl off_t sblock_try[] = SBLOCKSEARCH;
312 1.25 fvdl
313 1.1 cgd /*
314 1.1 cgd * Scan the specified filesystem to check quota(s) present on it.
315 1.1 cgd */
316 1.13 christos static int
317 1.14 christos chkquota(type, fsname, mntpt, v, pid)
318 1.13 christos const char *type, *fsname, *mntpt;
319 1.13 christos void *v;
320 1.14 christos pid_t *pid;
321 1.13 christos {
322 1.13 christos struct quotaname *qnp = v;
323 1.13 christos struct fileusage *fup;
324 1.36 christos union comb_dinode *dp;
325 1.25 fvdl int cg, i, mode, errs = 0, inosused;
326 1.1 cgd ino_t ino;
327 1.25 fvdl struct cg *cgp;
328 1.29 christos char msgbuf[4096];
329 1.1 cgd
330 1.14 christos if (pid != NULL) {
331 1.29 christos fflush(stdout);
332 1.14 christos switch ((*pid = fork())) {
333 1.14 christos default:
334 1.29 christos return 0;
335 1.29 christos case 0:
336 1.14 christos break;
337 1.14 christos case -1:
338 1.14 christos err(1, "Cannot fork");
339 1.14 christos }
340 1.29 christos setvbuf(stdout, msgbuf, _IOFBF, sizeof msgbuf);
341 1.14 christos }
342 1.14 christos
343 1.5 mycroft if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
344 1.14 christos warn("Cannot open %s", fsname);
345 1.29 christos if (pid != NULL)
346 1.29 christos exit(1);
347 1.25 fvdl return 1;
348 1.1 cgd }
349 1.1 cgd if (vflag) {
350 1.5 mycroft (void)printf("*** Checking ");
351 1.1 cgd if (qnp->flags & HASUSR)
352 1.5 mycroft (void)printf("%s%s", qfextension[USRQUOTA],
353 1.1 cgd (qnp->flags & HASGRP) ? " and " : "");
354 1.1 cgd if (qnp->flags & HASGRP)
355 1.5 mycroft (void)printf("%s", qfextension[GRPQUOTA]);
356 1.5 mycroft (void)printf(" quotas for %s (%s)\n", fsname, mntpt);
357 1.29 christos fflush(stdout);
358 1.1 cgd }
359 1.23 bouyer signal(SIGINFO, infohandler);
360 1.1 cgd sync();
361 1.6 mycroft dev_bsize = 1;
362 1.25 fvdl
363 1.25 fvdl cgp = malloc(sblock.fs_cgsize);
364 1.25 fvdl if (cgp == NULL) {
365 1.25 fvdl warn("%s: can't allocate %d bytes of cg space", fsname,
366 1.25 fvdl sblock.fs_cgsize);
367 1.29 christos if (pid != NULL)
368 1.29 christos exit(1);
369 1.25 fvdl return 1;
370 1.25 fvdl }
371 1.25 fvdl
372 1.34 dsl for (i = 0; ; i++) {
373 1.34 dsl if (sblock_try[i] == -1) {
374 1.34 dsl warnx("%s: superblock not found", fsname);
375 1.34 dsl free(cgp);
376 1.34 dsl if (pid != NULL)
377 1.34 dsl exit(1);
378 1.34 dsl return 1;
379 1.34 dsl }
380 1.25 fvdl bread(sblock_try[i], (char *)&sblock, SBLOCKSIZE);
381 1.25 fvdl switch (sblock.fs_magic) {
382 1.36 christos #ifdef HAVE_UFSv2
383 1.25 fvdl case FS_UFS2_MAGIC:
384 1.25 fvdl is_ufs2 = 1;
385 1.25 fvdl /*FALLTHROUGH*/
386 1.36 christos #endif
387 1.25 fvdl case FS_UFS1_MAGIC:
388 1.34 dsl break;
389 1.36 christos #ifdef HAVE_UFSv2
390 1.25 fvdl case FS_UFS2_MAGIC_SWAPPED:
391 1.25 fvdl is_ufs2 = 1;
392 1.25 fvdl /*FALLTHROUGH*/
393 1.36 christos #endif
394 1.25 fvdl case FS_UFS1_MAGIC_SWAPPED:
395 1.17 bouyer needswap = 1;
396 1.34 dsl ffs_sb_swap(&sblock, &sblock);
397 1.34 dsl break;
398 1.25 fvdl default:
399 1.25 fvdl continue;
400 1.25 fvdl }
401 1.34 dsl
402 1.36 christos #ifdef HAVE_UFSv2
403 1.35 dsl if (is_ufs2 || sblock.fs_old_flags & FS_FLAGS_UPDATED) {
404 1.34 dsl if (sblock.fs_sblockloc != sblock_try[i])
405 1.34 dsl continue;
406 1.34 dsl } else {
407 1.34 dsl if (sblock_try[i] == SBLOCK_UFS2)
408 1.34 dsl continue;
409 1.34 dsl }
410 1.36 christos #endif
411 1.34 dsl break;
412 1.19 ross }
413 1.34 dsl
414 1.1 cgd dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
415 1.1 cgd maxino = sblock.fs_ncg * sblock.fs_ipg;
416 1.1 cgd for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
417 1.25 fvdl setinodebuf(cg * sblock.fs_ipg);
418 1.36 christos #ifdef HAVE_UFSv2
419 1.25 fvdl if (sblock.fs_magic == FS_UFS2_MAGIC) {
420 1.25 fvdl bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)cgp,
421 1.25 fvdl sblock.fs_cgsize);
422 1.25 fvdl if (needswap)
423 1.25 fvdl ffs_cg_swap(cgp, cgp, &sblock);
424 1.25 fvdl inosused = cgp->cg_initediblk;
425 1.25 fvdl } else
426 1.36 christos #endif
427 1.25 fvdl inosused = sblock.fs_ipg;
428 1.25 fvdl for (i = 0; i < inosused; i++, ino++) {
429 1.23 bouyer if (got_siginfo) {
430 1.23 bouyer fprintf(stderr,
431 1.23 bouyer "%s: cyl group %d of %d (%d%%)\n",
432 1.23 bouyer fsname, cg, sblock.fs_ncg,
433 1.23 bouyer cg * 100 / sblock.fs_ncg);
434 1.23 bouyer got_siginfo = 0;
435 1.23 bouyer }
436 1.1 cgd if (ino < ROOTINO)
437 1.1 cgd continue;
438 1.1 cgd if ((dp = getnextinode(ino)) == NULL)
439 1.1 cgd continue;
440 1.25 fvdl if ((mode = DIP(dp, mode) & IFMT) == 0)
441 1.1 cgd continue;
442 1.1 cgd if (qnp->flags & HASGRP) {
443 1.36 christos fup = addid(DIP(dp, gid), GRPQUOTA,
444 1.1 cgd (char *)0);
445 1.1 cgd fup->fu_curinodes++;
446 1.1 cgd if (mode == IFREG || mode == IFDIR ||
447 1.1 cgd mode == IFLNK)
448 1.25 fvdl fup->fu_curblocks += DIP(dp, blocks);
449 1.1 cgd }
450 1.1 cgd if (qnp->flags & HASUSR) {
451 1.36 christos fup = addid(DIP(dp, uid), USRQUOTA,
452 1.1 cgd (char *)0);
453 1.1 cgd fup->fu_curinodes++;
454 1.1 cgd if (mode == IFREG || mode == IFDIR ||
455 1.1 cgd mode == IFLNK)
456 1.25 fvdl fup->fu_curblocks += DIP(dp, blocks);
457 1.1 cgd }
458 1.1 cgd }
459 1.1 cgd }
460 1.1 cgd freeinodebuf();
461 1.25 fvdl free(cgp);
462 1.1 cgd if (qnp->flags & HASUSR)
463 1.1 cgd errs += update(mntpt, qnp->usrqfname, USRQUOTA);
464 1.1 cgd if (qnp->flags & HASGRP)
465 1.1 cgd errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
466 1.1 cgd close(fi);
467 1.29 christos if (pid != NULL)
468 1.29 christos exit(errs);
469 1.25 fvdl return errs;
470 1.1 cgd }
471 1.1 cgd
472 1.1 cgd /*
473 1.1 cgd * Update a specified quota file.
474 1.1 cgd */
475 1.13 christos static int
476 1.1 cgd update(fsname, quotafile, type)
477 1.13 christos const char *fsname, *quotafile;
478 1.13 christos int type;
479 1.1 cgd {
480 1.13 christos struct fileusage *fup;
481 1.13 christos FILE *qfi, *qfo;
482 1.36 christos u_int32_t id, lastid, nextid;
483 1.29 christos int need_seek;
484 1.1 cgd struct dqblk dqbuf;
485 1.1 cgd static int warned = 0;
486 1.1 cgd static struct dqblk zerodqbuf;
487 1.1 cgd static struct fileusage zerofileusage;
488 1.1 cgd
489 1.1 cgd if ((qfo = fopen(quotafile, "r+")) == NULL) {
490 1.1 cgd if (errno == ENOENT)
491 1.1 cgd qfo = fopen(quotafile, "w+");
492 1.1 cgd if (qfo) {
493 1.1 cgd (void) fprintf(stderr,
494 1.1 cgd "quotacheck: creating quota file %s\n", quotafile);
495 1.1 cgd #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
496 1.1 cgd (void) fchown(fileno(qfo), getuid(), getquotagid());
497 1.1 cgd (void) fchmod(fileno(qfo), MODE);
498 1.1 cgd } else {
499 1.1 cgd (void) fprintf(stderr,
500 1.1 cgd "quotacheck: %s: %s\n", quotafile, strerror(errno));
501 1.1 cgd return (1);
502 1.1 cgd }
503 1.1 cgd }
504 1.1 cgd if ((qfi = fopen(quotafile, "r")) == NULL) {
505 1.1 cgd (void) fprintf(stderr,
506 1.1 cgd "quotacheck: %s: %s\n", quotafile, strerror(errno));
507 1.1 cgd (void) fclose(qfo);
508 1.1 cgd return (1);
509 1.1 cgd }
510 1.36 christos if (quotactl(fsname, QCMD(Q_SYNC, type), 0, (void *) NULL) < 0 &&
511 1.1 cgd errno == EOPNOTSUPP && !warned && vflag) {
512 1.1 cgd warned++;
513 1.5 mycroft (void)printf("*** Warning: %s\n",
514 1.1 cgd "Quotas are not compiled into this kernel");
515 1.1 cgd }
516 1.29 christos need_seek = 1;
517 1.29 christos for (lastid = highid[type], id = 0; id <= lastid; id = nextid) {
518 1.1 cgd if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
519 1.1 cgd dqbuf = zerodqbuf;
520 1.1 cgd if ((fup = lookup(id, type)) == 0)
521 1.1 cgd fup = &zerofileusage;
522 1.29 christos
523 1.29 christos nextid = subsequent(id, type);
524 1.36 christos if (nextid > 0 && nextid != id + 1) /* watch out for id == UINT32_MAX */
525 1.29 christos nextid = skipforward(id, nextid, qfi);
526 1.29 christos
527 1.29 christos if (got_siginfo) {
528 1.36 christos /* XXX this could try to show percentage through the ID list */
529 1.29 christos fprintf(stderr,
530 1.36 christos "%s: updating %s quotas for id=%" PRIu32 " (%s)\n", fsname,
531 1.29 christos qfextension[type < MAXQUOTAS ? type : MAXQUOTAS],
532 1.29 christos id, fup->fu_name);
533 1.29 christos got_siginfo = 0;
534 1.29 christos }
535 1.1 cgd if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
536 1.1 cgd dqbuf.dqb_curblocks == fup->fu_curblocks) {
537 1.29 christos fup->fu_curinodes = 0; /* reset usage */
538 1.29 christos fup->fu_curblocks = 0; /* for next filesystem */
539 1.29 christos
540 1.29 christos need_seek = 1;
541 1.36 christos if (id == UINT32_MAX || nextid == 0) { /* infinite loop avoidance (OR do as "nextid < id"?) */
542 1.29 christos break;
543 1.36 christos }
544 1.1 cgd continue;
545 1.1 cgd }
546 1.1 cgd if (vflag) {
547 1.1 cgd if (aflag)
548 1.1 cgd printf("%s: ", fsname);
549 1.1 cgd printf("%-8s fixed:", fup->fu_name);
550 1.1 cgd if (dqbuf.dqb_curinodes != fup->fu_curinodes)
551 1.13 christos (void)printf("\tinodes %d -> %ld",
552 1.1 cgd dqbuf.dqb_curinodes, fup->fu_curinodes);
553 1.1 cgd if (dqbuf.dqb_curblocks != fup->fu_curblocks)
554 1.13 christos (void)printf("\tblocks %d -> %ld",
555 1.1 cgd dqbuf.dqb_curblocks, fup->fu_curblocks);
556 1.5 mycroft (void)printf("\n");
557 1.1 cgd }
558 1.1 cgd /*
559 1.1 cgd * Reset time limit if have a soft limit and were
560 1.1 cgd * previously under it, but are now over it.
561 1.1 cgd */
562 1.1 cgd if (dqbuf.dqb_bsoftlimit &&
563 1.1 cgd dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
564 1.1 cgd fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
565 1.1 cgd dqbuf.dqb_btime = 0;
566 1.1 cgd if (dqbuf.dqb_isoftlimit &&
567 1.1 cgd dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
568 1.1 cgd fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
569 1.1 cgd dqbuf.dqb_itime = 0;
570 1.1 cgd dqbuf.dqb_curinodes = fup->fu_curinodes;
571 1.1 cgd dqbuf.dqb_curblocks = fup->fu_curblocks;
572 1.29 christos
573 1.29 christos if (need_seek) {
574 1.29 christos (void) fseeko(qfo, (off_t)id * sizeof(struct dqblk),
575 1.29 christos SEEK_SET);
576 1.29 christos need_seek = nextid != id + 1;
577 1.29 christos }
578 1.13 christos (void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
579 1.29 christos
580 1.29 christos if (!warned)
581 1.29 christos (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
582 1.29 christos (caddr_t)&dqbuf);
583 1.29 christos
584 1.1 cgd fup->fu_curinodes = 0;
585 1.1 cgd fup->fu_curblocks = 0;
586 1.36 christos if (id == UINT32_MAX || nextid == 0) { /* infinite loop avoidance (OR do as "nextid < id"?) */
587 1.29 christos break;
588 1.36 christos }
589 1.1 cgd }
590 1.13 christos (void) fclose(qfi);
591 1.13 christos (void) fflush(qfo);
592 1.36 christos if (highid[type] != UINT32_MAX)
593 1.29 christos (void) ftruncate(fileno(qfo),
594 1.29 christos (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
595 1.13 christos (void) fclose(qfo);
596 1.1 cgd return (0);
597 1.1 cgd }
598 1.1 cgd
599 1.36 christos u_int32_t
600 1.29 christos skipforward(cur, to, qfi)
601 1.36 christos u_int32_t cur, to;
602 1.29 christos FILE *qfi;
603 1.29 christos {
604 1.29 christos struct dqblk dqbuf;
605 1.29 christos
606 1.29 christos if (qflag) {
607 1.29 christos (void) fseeko(qfi, (off_t)to * sizeof(struct dqblk), SEEK_SET);
608 1.29 christos return (to);
609 1.29 christos }
610 1.29 christos
611 1.29 christos while (++cur < to) {
612 1.29 christos /*
613 1.29 christos * if EOF occurs, nothing left to read, we're done
614 1.29 christos */
615 1.29 christos if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
616 1.29 christos return (to);
617 1.29 christos
618 1.29 christos /*
619 1.29 christos * If we find an entry that shows usage, before the next
620 1.29 christos * id that has actual usage, we have to stop here, so the
621 1.29 christos * incorrect entry can be corrected in the file
622 1.29 christos */
623 1.29 christos if (dqbuf.dqb_curinodes != 0 || dqbuf.dqb_curblocks != 0) {
624 1.29 christos (void)fseek(qfi, -(long)sizeof(struct dqblk), SEEK_CUR);
625 1.29 christos return (cur);
626 1.29 christos }
627 1.29 christos }
628 1.29 christos return (to);
629 1.29 christos }
630 1.29 christos
631 1.1 cgd /*
632 1.1 cgd * Check to see if target appears in list of size cnt.
633 1.1 cgd */
634 1.13 christos static int
635 1.1 cgd oneof(target, list, cnt)
636 1.18 mycroft const char *target;
637 1.18 mycroft char *list[];
638 1.1 cgd int cnt;
639 1.1 cgd {
640 1.13 christos int i;
641 1.1 cgd
642 1.1 cgd for (i = 0; i < cnt; i++)
643 1.1 cgd if (strcmp(target, list[i]) == 0)
644 1.1 cgd return (i);
645 1.1 cgd return (-1);
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd /*
649 1.1 cgd * Determine the group identifier for quota files.
650 1.1 cgd */
651 1.13 christos static int
652 1.1 cgd getquotagid()
653 1.1 cgd {
654 1.1 cgd struct group *gr;
655 1.1 cgd
656 1.13 christos if ((gr = getgrnam(quotagroup)) != NULL)
657 1.1 cgd return (gr->gr_gid);
658 1.1 cgd return (-1);
659 1.1 cgd }
660 1.1 cgd
661 1.1 cgd /*
662 1.1 cgd * Check to see if a particular quota is to be enabled.
663 1.1 cgd */
664 1.13 christos static int
665 1.1 cgd hasquota(fs, type, qfnamep)
666 1.13 christos struct fstab *fs;
667 1.1 cgd int type;
668 1.1 cgd char **qfnamep;
669 1.1 cgd {
670 1.13 christos char *opt;
671 1.16 christos char *cp = NULL;
672 1.1 cgd static char initname, usrname[100], grpname[100];
673 1.1 cgd static char buf[BUFSIZ];
674 1.1 cgd
675 1.1 cgd if (!initname) {
676 1.5 mycroft (void)snprintf(usrname, sizeof(usrname),
677 1.5 mycroft "%s%s", qfextension[USRQUOTA], qfname);
678 1.5 mycroft (void)snprintf(grpname, sizeof(grpname),
679 1.5 mycroft "%s%s", qfextension[GRPQUOTA], qfname);
680 1.1 cgd initname = 1;
681 1.1 cgd }
682 1.27 itojun (void) strlcpy(buf, fs->fs_mntops, sizeof(buf));
683 1.1 cgd for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
684 1.13 christos if ((cp = strchr(opt, '=')) != NULL)
685 1.1 cgd *cp++ = '\0';
686 1.1 cgd if (type == USRQUOTA && strcmp(opt, usrname) == 0)
687 1.1 cgd break;
688 1.1 cgd if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
689 1.1 cgd break;
690 1.1 cgd }
691 1.1 cgd if (!opt)
692 1.1 cgd return (0);
693 1.5 mycroft if (cp)
694 1.1 cgd *qfnamep = cp;
695 1.5 mycroft else {
696 1.5 mycroft (void)snprintf(buf, sizeof(buf),
697 1.5 mycroft "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
698 1.5 mycroft *qfnamep = buf;
699 1.1 cgd }
700 1.1 cgd return (1);
701 1.1 cgd }
702 1.1 cgd
703 1.1 cgd /*
704 1.1 cgd * Routines to manage the file usage table.
705 1.1 cgd *
706 1.1 cgd * Lookup an id of a specific type.
707 1.1 cgd */
708 1.13 christos static struct fileusage *
709 1.1 cgd lookup(id, type)
710 1.36 christos u_int32_t id;
711 1.1 cgd int type;
712 1.1 cgd {
713 1.13 christos struct fileusage *fup;
714 1.1 cgd
715 1.1 cgd for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
716 1.1 cgd if (fup->fu_id == id)
717 1.1 cgd return (fup);
718 1.5 mycroft return (NULL);
719 1.1 cgd }
720 1.1 cgd
721 1.1 cgd /*
722 1.1 cgd * Add a new file usage id if it does not already exist.
723 1.1 cgd */
724 1.13 christos static struct fileusage *
725 1.1 cgd addid(id, type, name)
726 1.36 christos u_int32_t id;
727 1.1 cgd int type;
728 1.18 mycroft const char *name;
729 1.1 cgd {
730 1.1 cgd struct fileusage *fup, **fhp;
731 1.1 cgd int len;
732 1.1 cgd
733 1.13 christos if ((fup = lookup(id, type)) != NULL)
734 1.1 cgd return (fup);
735 1.1 cgd if (name)
736 1.1 cgd len = strlen(name);
737 1.1 cgd else
738 1.1 cgd len = 10;
739 1.5 mycroft if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
740 1.6 mycroft err(1, "%s", strerror(errno));
741 1.1 cgd fhp = &fuhead[type][id & (FUHASH - 1)];
742 1.1 cgd fup->fu_next = *fhp;
743 1.1 cgd *fhp = fup;
744 1.1 cgd fup->fu_id = id;
745 1.1 cgd if (id > highid[type])
746 1.1 cgd highid[type] = id;
747 1.5 mycroft if (name)
748 1.15 lukem memmove(fup->fu_name, name, len + 1);
749 1.5 mycroft else
750 1.36 christos (void) sprintf(fup->fu_name, "%" PRIu32, id);
751 1.1 cgd return (fup);
752 1.1 cgd }
753 1.1 cgd
754 1.36 christos static u_int32_t
755 1.29 christos subsequent(id, type)
756 1.36 christos u_int32_t id;
757 1.29 christos int type;
758 1.29 christos {
759 1.29 christos struct fileusage *fup, **iup, **cup;
760 1.36 christos u_int32_t next, offset;
761 1.29 christos
762 1.29 christos next = highid[type] + 1;
763 1.29 christos offset = 0;
764 1.29 christos cup = iup = &fuhead[type][id & (FUHASH-1)];
765 1.29 christos do {
766 1.29 christos ++offset;
767 1.29 christos if (++cup >= &fuhead[type][FUHASH])
768 1.29 christos cup = &fuhead[type][0];
769 1.29 christos for (fup = *cup; fup != 0; fup = fup->fu_next) {
770 1.29 christos if (fup->fu_id > id && fup->fu_id <= id + offset)
771 1.29 christos return (fup->fu_id);
772 1.29 christos if (fup->fu_id > id && fup->fu_id < next)
773 1.29 christos next = fup->fu_id;
774 1.29 christos }
775 1.29 christos } while (cup != iup);
776 1.29 christos
777 1.29 christos return next;
778 1.29 christos }
779 1.29 christos
780 1.1 cgd /*
781 1.25 fvdl * Special purpose version of ginode used to optimize first pass
782 1.1 cgd * over all the inodes in numerical order.
783 1.1 cgd */
784 1.25 fvdl static ino_t nextino, lastinum, lastvalidinum;
785 1.13 christos static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
786 1.36 christos static union comb_dinode *inodebuf;
787 1.25 fvdl #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
788 1.1 cgd
789 1.36 christos union comb_dinode *
790 1.1 cgd getnextinode(inumber)
791 1.1 cgd ino_t inumber;
792 1.1 cgd {
793 1.1 cgd long size;
794 1.1 cgd daddr_t dblk;
795 1.36 christos static union comb_dinode *dp;
796 1.36 christos union comb_dinode *ret;
797 1.25 fvdl
798 1.25 fvdl if (inumber != nextino++ || inumber > lastvalidinum) {
799 1.25 fvdl errx(1, "bad inode number %d to nextinode", inumber);
800 1.25 fvdl }
801 1.1 cgd
802 1.1 cgd if (inumber >= lastinum) {
803 1.1 cgd readcnt++;
804 1.5 mycroft dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
805 1.1 cgd if (readcnt % readpercg == 0) {
806 1.1 cgd size = partialsize;
807 1.1 cgd lastinum += partialcnt;
808 1.1 cgd } else {
809 1.1 cgd size = inobufsize;
810 1.1 cgd lastinum += fullcnt;
811 1.1 cgd }
812 1.25 fvdl (void)bread(dblk, (caddr_t)inodebuf, size);
813 1.25 fvdl if (needswap) {
814 1.36 christos #ifdef HAVE_UFSv2
815 1.25 fvdl if (is_ufs2)
816 1.25 fvdl swap_dinode2(inodebuf, lastinum - inumber);
817 1.25 fvdl else
818 1.36 christos #endif
819 1.25 fvdl swap_dinode1(inodebuf, lastinum - inumber);
820 1.25 fvdl }
821 1.36 christos dp = (union comb_dinode *)inodebuf;
822 1.1 cgd }
823 1.25 fvdl ret = dp;
824 1.36 christos dp = (union comb_dinode *)
825 1.25 fvdl ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
826 1.25 fvdl return ret;
827 1.1 cgd }
828 1.1 cgd
829 1.25 fvdl void
830 1.25 fvdl setinodebuf(inum)
831 1.25 fvdl ino_t inum;
832 1.1 cgd {
833 1.1 cgd
834 1.25 fvdl if (inum % sblock.fs_ipg != 0)
835 1.25 fvdl errx(1, "bad inode number %d to setinodebuf", inum);
836 1.25 fvdl
837 1.25 fvdl lastvalidinum = inum + sblock.fs_ipg - 1;
838 1.25 fvdl nextino = inum;
839 1.25 fvdl lastinum = inum;
840 1.1 cgd readcnt = 0;
841 1.25 fvdl if (inodebuf != NULL)
842 1.25 fvdl return;
843 1.1 cgd inobufsize = blkroundup(&sblock, INOBUFSIZE);
844 1.25 fvdl fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
845 1.1 cgd readpercg = sblock.fs_ipg / fullcnt;
846 1.1 cgd partialcnt = sblock.fs_ipg % fullcnt;
847 1.25 fvdl partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
848 1.1 cgd if (partialcnt != 0) {
849 1.1 cgd readpercg++;
850 1.1 cgd } else {
851 1.1 cgd partialcnt = fullcnt;
852 1.1 cgd partialsize = inobufsize;
853 1.1 cgd }
854 1.1 cgd if (inodebuf == NULL &&
855 1.25 fvdl (inodebuf = malloc((unsigned)inobufsize)) == NULL)
856 1.25 fvdl errx(1, "Cannot allocate space for inode buffer");
857 1.26 fvdl while (nextino < ROOTINO)
858 1.26 fvdl getnextinode(nextino);
859 1.1 cgd }
860 1.1 cgd
861 1.25 fvdl void
862 1.1 cgd freeinodebuf()
863 1.1 cgd {
864 1.1 cgd
865 1.1 cgd if (inodebuf != NULL)
866 1.25 fvdl free((char *)inodebuf);
867 1.1 cgd inodebuf = NULL;
868 1.25 fvdl }
869 1.25 fvdl
870 1.25 fvdl
871 1.36 christos #ifdef HAVE_UFSv2
872 1.25 fvdl static void
873 1.36 christos swap_dinode1(union comb_dinode *dp, int n)
874 1.25 fvdl {
875 1.25 fvdl int i;
876 1.25 fvdl struct ufs1_dinode *dp1;
877 1.25 fvdl
878 1.25 fvdl dp1 = (struct ufs1_dinode *)&dp->dp1;
879 1.25 fvdl for (i = 0; i < n; i++, dp1++)
880 1.25 fvdl ffs_dinode1_swap(dp1, dp1);
881 1.25 fvdl }
882 1.25 fvdl
883 1.25 fvdl static void
884 1.36 christos swap_dinode2(union comb_dinode *dp, int n)
885 1.25 fvdl {
886 1.25 fvdl int i;
887 1.25 fvdl struct ufs2_dinode *dp2;
888 1.25 fvdl
889 1.25 fvdl dp2 = (struct ufs2_dinode *)&dp->dp2;
890 1.25 fvdl for (i = 0; i < n; i++, dp2++)
891 1.25 fvdl ffs_dinode2_swap(dp2, dp2);
892 1.1 cgd }
893 1.1 cgd
894 1.36 christos #else
895 1.36 christos
896 1.36 christos static void
897 1.36 christos swap_dinode1(union comb_dinode *dp, int n)
898 1.36 christos {
899 1.36 christos int i;
900 1.36 christos struct dinode *dp1;
901 1.36 christos
902 1.36 christos dp1 = (struct dinode *) &dp->dp1;
903 1.36 christos for (i = 0; i < n; i++, dp1++)
904 1.36 christos ffs_dinode_swap(dp1, dp1);
905 1.36 christos }
906 1.36 christos
907 1.36 christos #endif
908 1.36 christos
909 1.1 cgd /*
910 1.1 cgd * Read specified disk blocks.
911 1.1 cgd */
912 1.13 christos static void
913 1.1 cgd bread(bno, buf, cnt)
914 1.1 cgd daddr_t bno;
915 1.1 cgd char *buf;
916 1.1 cgd long cnt;
917 1.1 cgd {
918 1.1 cgd
919 1.5 mycroft if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
920 1.5 mycroft read(fi, buf, cnt) != cnt)
921 1.24 fvdl err(1, "block %lld", (long long)bno);
922 1.1 cgd }
923 1.23 bouyer
924 1.23 bouyer void
925 1.23 bouyer infohandler(int sig)
926 1.23 bouyer {
927 1.23 bouyer got_siginfo = 1;
928 1.23 bouyer }
929