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