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