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