2005-08-19 06:07:18 +04:00
|
|
|
/* $NetBSD: quotacheck.c,v 1.37 2005/08/19 02:09:50 christos Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
/*
|
1994-06-08 23:27:32 +04:00
|
|
|
* Copyright (c) 1980, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 23:25:07 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Robert Elz at The University of Melbourne.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:25:11 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-09 23:25:07 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-16 17:07:59 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-09 23:25:07 +04:00
|
|
|
#ifndef lint
|
1997-09-16 17:07:59 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-04-09 23:25:07 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-18 17:54:19 +03:00
|
|
|
#if 0
|
1997-09-16 17:07:59 +04:00
|
|
|
static char sccsid[] = "@(#)quotacheck.c 8.6 (Berkeley) 4/28/95";
|
1995-03-18 17:54:19 +03:00
|
|
|
#else
|
2005-08-19 06:07:18 +04:00
|
|
|
__RCSID("$NetBSD: quotacheck.c,v 1.37 2005/08/19 02:09:50 christos Exp $");
|
1995-03-18 17:54:19 +03:00
|
|
|
#endif
|
1993-04-09 23:25:07 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fix up / report on disk quotas & usage
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
1997-09-16 17:07:59 +04:00
|
|
|
#include <sys/queue.h>
|
1994-06-08 23:27:32 +04:00
|
|
|
|
|
|
|
#include <ufs/ufs/dinode.h>
|
|
|
|
#include <ufs/ufs/quota.h>
|
1998-03-18 20:22:38 +03:00
|
|
|
#include <ufs/ufs/ufs_bswap.h>
|
1994-06-08 23:27:32 +04:00
|
|
|
#include <ufs/ffs/fs.h>
|
1998-03-18 20:22:38 +03:00
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
1994-06-08 23:27:32 +04:00
|
|
|
|
1997-09-16 17:07:59 +04:00
|
|
|
#include <err.h>
|
1993-04-09 23:25:07 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fstab.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
#include "fsutil.h"
|
1993-04-09 23:25:07 +04:00
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifndef FS_UFS1_MAGIC
|
|
|
|
# define FS_UFS1_MAGIC FS_MAGIC /* 0x011954 */
|
|
|
|
# define FS_UFS1_MAGIC_SWAPPED 0x54190100 /* bswap32(0x011954) */
|
|
|
|
# define DINODE1_SIZE sizeof(struct dinode)
|
|
|
|
# define DINODE2_SIZE 0
|
|
|
|
#else
|
|
|
|
# define HAVE_UFSv2 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SBLOCKSIZE
|
|
|
|
# define SBLOCKSIZE SBSIZE
|
|
|
|
#endif
|
|
|
|
#ifndef SBLOCKSEARCH
|
|
|
|
# define SBLOCKSEARCH { SBSIZE, -1 }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *qfname = QUOTAFILENAME;
|
|
|
|
static const char *qfextension[] = INITQFNAMES;
|
|
|
|
static const char *quotagroup = QUOTAGROUP;
|
1996-09-28 03:25:34 +04:00
|
|
|
|
|
|
|
static union {
|
1993-04-09 23:25:07 +04:00
|
|
|
struct fs sblk;
|
|
|
|
char dummy[MAXBSIZE];
|
|
|
|
} un;
|
|
|
|
#define sblock un.sblk
|
1996-09-28 03:25:34 +04:00
|
|
|
static long dev_bsize;
|
|
|
|
static long maxino;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
struct quotaname {
|
|
|
|
long flags;
|
|
|
|
char grpqfname[MAXPATHLEN + 1];
|
|
|
|
char usrqfname[MAXPATHLEN + 1];
|
|
|
|
};
|
|
|
|
#define HASUSR 1
|
|
|
|
#define HASGRP 2
|
|
|
|
|
|
|
|
struct fileusage {
|
|
|
|
struct fileusage *fu_next;
|
|
|
|
u_long fu_curinodes;
|
|
|
|
u_long fu_curblocks;
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t fu_id; /* uid_t, gid_t */
|
1993-04-09 23:25:07 +04:00
|
|
|
char fu_name[1];
|
|
|
|
/* actually bigger */
|
|
|
|
};
|
|
|
|
#define FUHASH 1024 /* must be power of two */
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
union comb_dinode {
|
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
struct ufs1_dinode dp1;
|
|
|
|
struct ufs2_dinode dp2;
|
2004-12-12 08:57:03 +03:00
|
|
|
#else
|
|
|
|
struct dinode dp1;
|
|
|
|
#endif
|
2003-04-02 14:39:19 +04:00
|
|
|
};
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
#define DIP(dp, field) \
|
|
|
|
(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
|
2004-12-12 08:57:03 +03:00
|
|
|
#else
|
|
|
|
#define DIP(dp, field) (dp)->dp1.di_##field
|
|
|
|
#endif
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static int aflag; /* all file systems */
|
|
|
|
static int gflag; /* check group quotas */
|
|
|
|
static int uflag; /* check user quotas */
|
|
|
|
static int vflag; /* verbose */
|
2004-01-05 03:17:07 +03:00
|
|
|
static int qflag; /* quick but untidy mode */
|
1996-09-28 03:25:34 +04:00
|
|
|
static int fi; /* open disk file descriptor */
|
2004-12-12 08:57:03 +03:00
|
|
|
static u_int32_t highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
|
1998-03-18 20:22:38 +03:00
|
|
|
static int needswap; /* FS is in swapped order */
|
2002-12-11 01:42:00 +03:00
|
|
|
static int got_siginfo = 0; /* got a siginfo signal */
|
2003-04-02 14:39:19 +04:00
|
|
|
static int is_ufs2;
|
1996-09-28 03:25:34 +04:00
|
|
|
|
|
|
|
|
|
|
|
int main __P((int, char *[]));
|
|
|
|
static void usage __P((void));
|
|
|
|
static void *needchk __P((struct fstab *));
|
1996-09-28 23:20:44 +04:00
|
|
|
static int chkquota __P((const char *, const char *, const char *, void *,
|
|
|
|
pid_t *));
|
1996-09-28 03:25:34 +04:00
|
|
|
static int update __P((const char *, const char *, int));
|
2004-12-12 08:57:03 +03:00
|
|
|
static u_int32_t skipforward __P((u_int32_t, u_int32_t, FILE *));
|
1998-07-27 04:52:00 +04:00
|
|
|
static int oneof __P((const char *, char *[], int));
|
1996-09-28 03:25:34 +04:00
|
|
|
static int getquotagid __P((void));
|
|
|
|
static int hasquota __P((struct fstab *, int, char **));
|
2004-12-12 08:57:03 +03:00
|
|
|
static struct fileusage *lookup __P((u_int32_t, int));
|
|
|
|
static struct fileusage *addid __P((u_int32_t, int, const char *));
|
|
|
|
static u_int32_t subsequent __P((u_int32_t, int));
|
|
|
|
static union comb_dinode *getnextinode __P((ino_t));
|
2003-04-02 14:39:19 +04:00
|
|
|
static void setinodebuf __P((ino_t));
|
1996-09-28 03:25:34 +04:00
|
|
|
static void freeinodebuf __P((void));
|
|
|
|
static void bread __P((daddr_t, char *, long));
|
2002-12-11 01:42:00 +03:00
|
|
|
static void infohandler __P((int sig));
|
2004-12-12 08:57:03 +03:00
|
|
|
static void swap_dinode1(union comb_dinode *, int);
|
|
|
|
#ifdef HAVE_UFSv2
|
|
|
|
static void swap_dinode2(union comb_dinode *, int);
|
|
|
|
#endif
|
1994-06-08 23:27:32 +04:00
|
|
|
|
|
|
|
int
|
1993-04-09 23:25:07 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-06-08 23:27:32 +04:00
|
|
|
char *argv[];
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fstab *fs;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
1994-06-08 23:27:32 +04:00
|
|
|
struct quotaname *auxdata;
|
|
|
|
int i, argnum, maxrun, errs;
|
|
|
|
long done = 0;
|
1996-09-28 23:20:44 +04:00
|
|
|
int flags = CHECK_PREEN;
|
1998-07-27 04:52:00 +04:00
|
|
|
const char *name;
|
1996-03-31 01:34:25 +03:00
|
|
|
int ch;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
errs = maxrun = 0;
|
2004-01-05 03:17:07 +03:00
|
|
|
while ((ch = getopt(argc, argv, "aguvqdl:")) != -1) {
|
1993-04-09 23:25:07 +04:00
|
|
|
switch(ch) {
|
|
|
|
case 'a':
|
|
|
|
aflag++;
|
|
|
|
break;
|
1996-09-28 23:20:44 +04:00
|
|
|
case 'd':
|
|
|
|
flags |= CHECK_DEBUG;
|
|
|
|
break;
|
1993-04-09 23:25:07 +04:00
|
|
|
case 'g':
|
|
|
|
gflag++;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag++;
|
|
|
|
break;
|
2004-01-05 03:17:07 +03:00
|
|
|
case 'q':
|
|
|
|
qflag++;
|
|
|
|
break;
|
1993-04-09 23:25:07 +04:00
|
|
|
case 'v':
|
|
|
|
vflag++;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
maxrun = atoi(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2004-01-05 20:43:25 +03:00
|
|
|
if ((argc == 0 && !aflag) || (argc > 0 && aflag) || (!aflag && maxrun))
|
1993-04-09 23:25:07 +04:00
|
|
|
usage();
|
|
|
|
if (!gflag && !uflag) {
|
|
|
|
gflag++;
|
|
|
|
uflag++;
|
|
|
|
}
|
1999-03-31 03:56:26 +04:00
|
|
|
|
|
|
|
/* If -a, we do not want to pay the cost of processing every
|
|
|
|
* group and password entry if there are no filesystems with quotas
|
|
|
|
*/
|
|
|
|
if (aflag) {
|
|
|
|
i = 0;
|
|
|
|
while ((fs = getfsent()) != NULL) {
|
|
|
|
if (needchk(fs))
|
|
|
|
i=1;
|
|
|
|
}
|
|
|
|
endfsent();
|
|
|
|
if (!i) /* No filesystems with quotas */
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
if (gflag) {
|
|
|
|
setgrent();
|
|
|
|
while ((gr = getgrent()) != 0)
|
2004-12-12 08:57:03 +03:00
|
|
|
(void) addid((u_int32_t)gr->gr_gid, GRPQUOTA, gr->gr_name);
|
1993-04-09 23:25:07 +04:00
|
|
|
endgrent();
|
|
|
|
}
|
|
|
|
if (uflag) {
|
|
|
|
setpwent();
|
|
|
|
while ((pw = getpwent()) != 0)
|
2004-12-12 08:57:03 +03:00
|
|
|
(void) addid((u_int32_t)pw->pw_uid, USRQUOTA, pw->pw_name);
|
1993-04-09 23:25:07 +04:00
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
if (aflag)
|
1996-09-28 23:20:44 +04:00
|
|
|
exit(checkfstab(flags, maxrun, needchk, chkquota));
|
1994-06-08 23:27:32 +04:00
|
|
|
if (setfsent() == 0)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s: can't open", FSTAB);
|
1993-04-09 23:25:07 +04:00
|
|
|
while ((fs = getfsent()) != NULL) {
|
|
|
|
if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
|
|
|
|
(argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
|
|
|
|
(auxdata = needchk(fs)) &&
|
|
|
|
(name = blockcheck(fs->fs_spec))) {
|
|
|
|
done |= 1 << argnum;
|
1996-09-28 03:25:34 +04:00
|
|
|
errs += chkquota(fs->fs_type, name, fs->fs_file,
|
1996-09-28 23:20:44 +04:00
|
|
|
auxdata, NULL);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
endfsent();
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
if ((done & (1 << i)) == 0)
|
|
|
|
fprintf(stderr, "%s not found in %s\n",
|
|
|
|
argv[i], FSTAB);
|
|
|
|
exit(errs);
|
|
|
|
}
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static void
|
1993-04-09 23:25:07 +04:00
|
|
|
usage()
|
|
|
|
{
|
2001-02-20 02:22:40 +03:00
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
(void)fprintf(stderr,
|
2004-01-06 02:23:32 +03:00
|
|
|
"usage:\t%s -a [-gquv] [-l maxparallel]\n\t%s [-gquv] filesys ...\n", getprogname(),
|
2001-02-20 02:22:40 +03:00
|
|
|
getprogname());
|
1993-04-09 23:25:07 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
static void *
|
1993-04-09 23:25:07 +04:00
|
|
|
needchk(fs)
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fstab *fs;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct quotaname *qnp;
|
1993-04-09 23:25:07 +04:00
|
|
|
char *qfnp;
|
|
|
|
|
1995-11-28 08:25:28 +03:00
|
|
|
if (strcmp(fs->fs_vfstype, "ffs") ||
|
1993-04-09 23:25:07 +04:00
|
|
|
strcmp(fs->fs_type, FSTAB_RW))
|
1994-06-08 23:27:32 +04:00
|
|
|
return (NULL);
|
|
|
|
if ((qnp = malloc(sizeof(*qnp))) == NULL)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s", strerror(errno));
|
1993-04-09 23:25:07 +04:00
|
|
|
qnp->flags = 0;
|
|
|
|
if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
|
2003-07-13 16:18:55 +04:00
|
|
|
strlcpy(qnp->grpqfname, qfnp, sizeof(qnp->grpqfname));
|
1993-04-09 23:25:07 +04:00
|
|
|
qnp->flags |= HASGRP;
|
|
|
|
}
|
|
|
|
if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
|
2003-07-13 16:18:55 +04:00
|
|
|
strlcpy(qnp->usrqfname, qfnp, sizeof(qnp->usrqfname));
|
1993-04-09 23:25:07 +04:00
|
|
|
qnp->flags |= HASUSR;
|
|
|
|
}
|
|
|
|
if (qnp->flags)
|
1994-06-08 23:27:32 +04:00
|
|
|
return (qnp);
|
|
|
|
free(qnp);
|
|
|
|
return (NULL);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
off_t sblock_try[] = SBLOCKSEARCH;
|
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
/*
|
|
|
|
* Scan the specified filesystem to check quota(s) present on it.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1996-09-28 23:20:44 +04:00
|
|
|
chkquota(type, fsname, mntpt, v, pid)
|
1996-09-28 03:25:34 +04:00
|
|
|
const char *type, *fsname, *mntpt;
|
|
|
|
void *v;
|
1996-09-28 23:20:44 +04:00
|
|
|
pid_t *pid;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct quotaname *qnp = v;
|
|
|
|
struct fileusage *fup;
|
2004-12-12 08:57:03 +03:00
|
|
|
union comb_dinode *dp;
|
2003-04-02 14:39:19 +04:00
|
|
|
int cg, i, mode, errs = 0, inosused;
|
1993-04-09 23:25:07 +04:00
|
|
|
ino_t ino;
|
2003-04-02 14:39:19 +04:00
|
|
|
struct cg *cgp;
|
2004-01-05 03:17:07 +03:00
|
|
|
char msgbuf[4096];
|
1993-04-09 23:25:07 +04:00
|
|
|
|
1996-09-28 23:20:44 +04:00
|
|
|
if (pid != NULL) {
|
2004-01-05 03:17:07 +03:00
|
|
|
fflush(stdout);
|
1996-09-28 23:20:44 +04:00
|
|
|
switch ((*pid = fork())) {
|
|
|
|
default:
|
|
|
|
return 0;
|
2004-01-05 03:17:07 +03:00
|
|
|
case 0:
|
|
|
|
break;
|
1996-09-28 23:20:44 +04:00
|
|
|
case -1:
|
|
|
|
err(1, "Cannot fork");
|
|
|
|
}
|
2004-01-05 03:17:07 +03:00
|
|
|
setvbuf(stdout, msgbuf, _IOFBF, sizeof msgbuf);
|
1996-09-28 23:20:44 +04:00
|
|
|
}
|
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
|
1996-09-28 23:20:44 +04:00
|
|
|
warn("Cannot open %s", fsname);
|
2004-01-05 03:17:07 +03:00
|
|
|
if (pid != NULL)
|
|
|
|
exit(1);
|
2003-04-02 14:39:19 +04:00
|
|
|
return 1;
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
if (vflag) {
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("*** Checking ");
|
1993-04-09 23:25:07 +04:00
|
|
|
if (qnp->flags & HASUSR)
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("%s%s", qfextension[USRQUOTA],
|
1993-04-09 23:25:07 +04:00
|
|
|
(qnp->flags & HASGRP) ? " and " : "");
|
|
|
|
if (qnp->flags & HASGRP)
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("%s", qfextension[GRPQUOTA]);
|
|
|
|
(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
|
2004-01-05 03:17:07 +03:00
|
|
|
fflush(stdout);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
2002-12-11 01:42:00 +03:00
|
|
|
signal(SIGINFO, infohandler);
|
1993-04-09 23:25:07 +04:00
|
|
|
sync();
|
1994-08-16 23:22:02 +04:00
|
|
|
dev_bsize = 1;
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
cgp = malloc(sblock.fs_cgsize);
|
|
|
|
if (cgp == NULL) {
|
|
|
|
warn("%s: can't allocate %d bytes of cg space", fsname,
|
|
|
|
sblock.fs_cgsize);
|
2004-01-05 03:17:07 +03:00
|
|
|
if (pid != NULL)
|
|
|
|
exit(1);
|
2003-04-02 14:39:19 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-22 01:07:22 +03:00
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (sblock_try[i] == -1) {
|
|
|
|
warnx("%s: superblock not found", fsname);
|
|
|
|
free(cgp);
|
|
|
|
if (pid != NULL)
|
|
|
|
exit(1);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
bread(sblock_try[i], (char *)&sblock, SBLOCKSIZE);
|
|
|
|
switch (sblock.fs_magic) {
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
case FS_UFS2_MAGIC:
|
|
|
|
is_ufs2 = 1;
|
|
|
|
/*FALLTHROUGH*/
|
2004-12-12 08:57:03 +03:00
|
|
|
#endif
|
2003-04-02 14:39:19 +04:00
|
|
|
case FS_UFS1_MAGIC:
|
2004-03-22 01:07:22 +03:00
|
|
|
break;
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
case FS_UFS2_MAGIC_SWAPPED:
|
|
|
|
is_ufs2 = 1;
|
|
|
|
/*FALLTHROUGH*/
|
2004-12-12 08:57:03 +03:00
|
|
|
#endif
|
2003-04-02 14:39:19 +04:00
|
|
|
case FS_UFS1_MAGIC_SWAPPED:
|
1998-03-18 20:22:38 +03:00
|
|
|
needswap = 1;
|
2004-03-22 01:07:22 +03:00
|
|
|
ffs_sb_swap(&sblock, &sblock);
|
|
|
|
break;
|
2003-04-02 14:39:19 +04:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
2004-03-22 01:07:22 +03:00
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2004-03-27 16:11:47 +03:00
|
|
|
if (is_ufs2 || sblock.fs_old_flags & FS_FLAGS_UPDATED) {
|
2004-03-22 01:07:22 +03:00
|
|
|
if (sblock.fs_sblockloc != sblock_try[i])
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (sblock_try[i] == SBLOCK_UFS2)
|
|
|
|
continue;
|
|
|
|
}
|
2004-12-12 08:57:03 +03:00
|
|
|
#endif
|
2004-03-22 01:07:22 +03:00
|
|
|
break;
|
1998-08-28 00:31:00 +04:00
|
|
|
}
|
2004-03-22 01:07:22 +03:00
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
|
|
|
|
maxino = sblock.fs_ncg * sblock.fs_ipg;
|
|
|
|
for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
|
2003-04-02 14:39:19 +04:00
|
|
|
setinodebuf(cg * sblock.fs_ipg);
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
if (sblock.fs_magic == FS_UFS2_MAGIC) {
|
|
|
|
bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)cgp,
|
|
|
|
sblock.fs_cgsize);
|
|
|
|
if (needswap)
|
|
|
|
ffs_cg_swap(cgp, cgp, &sblock);
|
|
|
|
inosused = cgp->cg_initediblk;
|
|
|
|
} else
|
2004-12-12 08:57:03 +03:00
|
|
|
#endif
|
2003-04-02 14:39:19 +04:00
|
|
|
inosused = sblock.fs_ipg;
|
|
|
|
for (i = 0; i < inosused; i++, ino++) {
|
2002-12-11 01:42:00 +03:00
|
|
|
if (got_siginfo) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: cyl group %d of %d (%d%%)\n",
|
|
|
|
fsname, cg, sblock.fs_ncg,
|
|
|
|
cg * 100 / sblock.fs_ncg);
|
|
|
|
got_siginfo = 0;
|
|
|
|
}
|
1993-04-09 23:25:07 +04:00
|
|
|
if (ino < ROOTINO)
|
|
|
|
continue;
|
|
|
|
if ((dp = getnextinode(ino)) == NULL)
|
|
|
|
continue;
|
2003-04-02 14:39:19 +04:00
|
|
|
if ((mode = DIP(dp, mode) & IFMT) == 0)
|
1993-04-09 23:25:07 +04:00
|
|
|
continue;
|
|
|
|
if (qnp->flags & HASGRP) {
|
2004-12-12 08:57:03 +03:00
|
|
|
fup = addid(DIP(dp, gid), GRPQUOTA,
|
1993-04-09 23:25:07 +04:00
|
|
|
(char *)0);
|
|
|
|
fup->fu_curinodes++;
|
|
|
|
if (mode == IFREG || mode == IFDIR ||
|
|
|
|
mode == IFLNK)
|
2003-04-02 14:39:19 +04:00
|
|
|
fup->fu_curblocks += DIP(dp, blocks);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
if (qnp->flags & HASUSR) {
|
2004-12-12 08:57:03 +03:00
|
|
|
fup = addid(DIP(dp, uid), USRQUOTA,
|
1993-04-09 23:25:07 +04:00
|
|
|
(char *)0);
|
|
|
|
fup->fu_curinodes++;
|
|
|
|
if (mode == IFREG || mode == IFDIR ||
|
|
|
|
mode == IFLNK)
|
2003-04-02 14:39:19 +04:00
|
|
|
fup->fu_curblocks += DIP(dp, blocks);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freeinodebuf();
|
2003-04-02 14:39:19 +04:00
|
|
|
free(cgp);
|
1993-04-09 23:25:07 +04:00
|
|
|
if (qnp->flags & HASUSR)
|
|
|
|
errs += update(mntpt, qnp->usrqfname, USRQUOTA);
|
|
|
|
if (qnp->flags & HASGRP)
|
|
|
|
errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
|
|
|
|
close(fi);
|
2004-01-05 03:17:07 +03:00
|
|
|
if (pid != NULL)
|
|
|
|
exit(errs);
|
2003-04-02 14:39:19 +04:00
|
|
|
return errs;
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update a specified quota file.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
update(fsname, quotafile, type)
|
1996-09-28 03:25:34 +04:00
|
|
|
const char *fsname, *quotafile;
|
|
|
|
int type;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fileusage *fup;
|
|
|
|
FILE *qfi, *qfo;
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t id, lastid, nextid;
|
2004-01-05 03:17:07 +03:00
|
|
|
int need_seek;
|
1993-04-09 23:25:07 +04:00
|
|
|
struct dqblk dqbuf;
|
|
|
|
static int warned = 0;
|
|
|
|
static struct dqblk zerodqbuf;
|
|
|
|
static struct fileusage zerofileusage;
|
|
|
|
|
|
|
|
if ((qfo = fopen(quotafile, "r+")) == NULL) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
qfo = fopen(quotafile, "w+");
|
|
|
|
if (qfo) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"quotacheck: creating quota file %s\n", quotafile);
|
|
|
|
#define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
|
|
|
|
(void) fchown(fileno(qfo), getuid(), getquotagid());
|
|
|
|
(void) fchmod(fileno(qfo), MODE);
|
|
|
|
} else {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"quotacheck: %s: %s\n", quotafile, strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((qfi = fopen(quotafile, "r")) == NULL) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"quotacheck: %s: %s\n", quotafile, strerror(errno));
|
|
|
|
(void) fclose(qfo);
|
|
|
|
return (1);
|
|
|
|
}
|
2004-12-12 08:57:03 +03:00
|
|
|
if (quotactl(fsname, QCMD(Q_SYNC, type), 0, (void *) NULL) < 0 &&
|
1993-04-09 23:25:07 +04:00
|
|
|
errno == EOPNOTSUPP && !warned && vflag) {
|
|
|
|
warned++;
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("*** Warning: %s\n",
|
1993-04-09 23:25:07 +04:00
|
|
|
"Quotas are not compiled into this kernel");
|
|
|
|
}
|
2004-01-05 03:17:07 +03:00
|
|
|
need_seek = 1;
|
|
|
|
for (lastid = highid[type], id = 0; id <= lastid; id = nextid) {
|
1993-04-09 23:25:07 +04:00
|
|
|
if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
|
|
|
|
dqbuf = zerodqbuf;
|
|
|
|
if ((fup = lookup(id, type)) == 0)
|
|
|
|
fup = &zerofileusage;
|
2004-01-05 03:17:07 +03:00
|
|
|
|
|
|
|
nextid = subsequent(id, type);
|
2004-12-12 08:57:03 +03:00
|
|
|
if (nextid > 0 && nextid != id + 1) /* watch out for id == UINT32_MAX */
|
2004-01-05 03:17:07 +03:00
|
|
|
nextid = skipforward(id, nextid, qfi);
|
|
|
|
|
|
|
|
if (got_siginfo) {
|
2004-12-12 08:57:03 +03:00
|
|
|
/* XXX this could try to show percentage through the ID list */
|
2004-01-05 03:17:07 +03:00
|
|
|
fprintf(stderr,
|
2004-12-12 08:57:03 +03:00
|
|
|
"%s: updating %s quotas for id=%" PRIu32 " (%s)\n", fsname,
|
2004-01-05 03:17:07 +03:00
|
|
|
qfextension[type < MAXQUOTAS ? type : MAXQUOTAS],
|
|
|
|
id, fup->fu_name);
|
|
|
|
got_siginfo = 0;
|
|
|
|
}
|
1993-04-09 23:25:07 +04:00
|
|
|
if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
|
|
|
|
dqbuf.dqb_curblocks == fup->fu_curblocks) {
|
2004-01-05 03:17:07 +03:00
|
|
|
fup->fu_curinodes = 0; /* reset usage */
|
|
|
|
fup->fu_curblocks = 0; /* for next filesystem */
|
|
|
|
|
|
|
|
need_seek = 1;
|
2004-12-12 08:57:03 +03:00
|
|
|
if (id == UINT32_MAX || nextid == 0) { /* infinite loop avoidance (OR do as "nextid < id"?) */
|
2004-01-05 03:17:07 +03:00
|
|
|
break;
|
2004-12-12 08:57:03 +03:00
|
|
|
}
|
1993-04-09 23:25:07 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (vflag) {
|
|
|
|
if (aflag)
|
|
|
|
printf("%s: ", fsname);
|
|
|
|
printf("%-8s fixed:", fup->fu_name);
|
|
|
|
if (dqbuf.dqb_curinodes != fup->fu_curinodes)
|
1996-09-28 03:25:34 +04:00
|
|
|
(void)printf("\tinodes %d -> %ld",
|
1993-04-09 23:25:07 +04:00
|
|
|
dqbuf.dqb_curinodes, fup->fu_curinodes);
|
|
|
|
if (dqbuf.dqb_curblocks != fup->fu_curblocks)
|
1996-09-28 03:25:34 +04:00
|
|
|
(void)printf("\tblocks %d -> %ld",
|
1993-04-09 23:25:07 +04:00
|
|
|
dqbuf.dqb_curblocks, fup->fu_curblocks);
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)printf("\n");
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Reset time limit if have a soft limit and were
|
|
|
|
* previously under it, but are now over it.
|
|
|
|
*/
|
|
|
|
if (dqbuf.dqb_bsoftlimit &&
|
|
|
|
dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
|
|
|
|
fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
|
|
|
|
dqbuf.dqb_btime = 0;
|
|
|
|
if (dqbuf.dqb_isoftlimit &&
|
|
|
|
dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
|
|
|
|
fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
|
|
|
|
dqbuf.dqb_itime = 0;
|
|
|
|
dqbuf.dqb_curinodes = fup->fu_curinodes;
|
|
|
|
dqbuf.dqb_curblocks = fup->fu_curblocks;
|
2004-01-05 03:17:07 +03:00
|
|
|
|
|
|
|
if (need_seek) {
|
|
|
|
(void) fseeko(qfo, (off_t)id * sizeof(struct dqblk),
|
|
|
|
SEEK_SET);
|
|
|
|
need_seek = nextid != id + 1;
|
|
|
|
}
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
|
2004-01-05 03:17:07 +03:00
|
|
|
|
|
|
|
if (!warned)
|
|
|
|
(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
|
|
|
|
(caddr_t)&dqbuf);
|
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
fup->fu_curinodes = 0;
|
|
|
|
fup->fu_curblocks = 0;
|
2004-12-12 08:57:03 +03:00
|
|
|
if (id == UINT32_MAX || nextid == 0) { /* infinite loop avoidance (OR do as "nextid < id"?) */
|
2004-01-05 03:17:07 +03:00
|
|
|
break;
|
2004-12-12 08:57:03 +03:00
|
|
|
}
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fclose(qfi);
|
|
|
|
(void) fflush(qfo);
|
2004-12-12 08:57:03 +03:00
|
|
|
if (highid[type] != UINT32_MAX)
|
2004-01-05 03:17:07 +03:00
|
|
|
(void) ftruncate(fileno(qfo),
|
|
|
|
(off_t)((highid[type] + 1) * sizeof(struct dqblk)));
|
1996-09-28 03:25:34 +04:00
|
|
|
(void) fclose(qfo);
|
1993-04-09 23:25:07 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t
|
2004-01-05 03:17:07 +03:00
|
|
|
skipforward(cur, to, qfi)
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t cur, to;
|
2004-01-05 03:17:07 +03:00
|
|
|
FILE *qfi;
|
|
|
|
{
|
|
|
|
struct dqblk dqbuf;
|
|
|
|
|
|
|
|
if (qflag) {
|
|
|
|
(void) fseeko(qfi, (off_t)to * sizeof(struct dqblk), SEEK_SET);
|
|
|
|
return (to);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (++cur < to) {
|
|
|
|
/*
|
|
|
|
* if EOF occurs, nothing left to read, we're done
|
|
|
|
*/
|
|
|
|
if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
|
|
|
|
return (to);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we find an entry that shows usage, before the next
|
|
|
|
* id that has actual usage, we have to stop here, so the
|
|
|
|
* incorrect entry can be corrected in the file
|
|
|
|
*/
|
|
|
|
if (dqbuf.dqb_curinodes != 0 || dqbuf.dqb_curblocks != 0) {
|
|
|
|
(void)fseek(qfi, -(long)sizeof(struct dqblk), SEEK_CUR);
|
|
|
|
return (cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (to);
|
|
|
|
}
|
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
/*
|
|
|
|
* Check to see if target appears in list of size cnt.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
oneof(target, list, cnt)
|
1998-07-27 04:52:00 +04:00
|
|
|
const char *target;
|
|
|
|
char *list[];
|
1993-04-09 23:25:07 +04:00
|
|
|
int cnt;
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
int i;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
if (strcmp(target, list[i]) == 0)
|
|
|
|
return (i);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the group identifier for quota files.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
getquotagid()
|
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
if ((gr = getgrnam(quotagroup)) != NULL)
|
1993-04-09 23:25:07 +04:00
|
|
|
return (gr->gr_gid);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if a particular quota is to be enabled.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static int
|
1993-04-09 23:25:07 +04:00
|
|
|
hasquota(fs, type, qfnamep)
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fstab *fs;
|
1993-04-09 23:25:07 +04:00
|
|
|
int type;
|
|
|
|
char **qfnamep;
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
char *opt;
|
1997-09-20 01:22:38 +04:00
|
|
|
char *cp = NULL;
|
1993-04-09 23:25:07 +04:00
|
|
|
static char initname, usrname[100], grpname[100];
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
|
|
|
|
if (!initname) {
|
1994-06-08 23:27:32 +04:00
|
|
|
(void)snprintf(usrname, sizeof(usrname),
|
|
|
|
"%s%s", qfextension[USRQUOTA], qfname);
|
|
|
|
(void)snprintf(grpname, sizeof(grpname),
|
|
|
|
"%s%s", qfextension[GRPQUOTA], qfname);
|
1993-04-09 23:25:07 +04:00
|
|
|
initname = 1;
|
|
|
|
}
|
2003-07-13 16:18:55 +04:00
|
|
|
(void) strlcpy(buf, fs->fs_mntops, sizeof(buf));
|
1993-04-09 23:25:07 +04:00
|
|
|
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
|
1996-09-28 03:25:34 +04:00
|
|
|
if ((cp = strchr(opt, '=')) != NULL)
|
1993-04-09 23:25:07 +04:00
|
|
|
*cp++ = '\0';
|
|
|
|
if (type == USRQUOTA && strcmp(opt, usrname) == 0)
|
|
|
|
break;
|
|
|
|
if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!opt)
|
|
|
|
return (0);
|
1994-06-08 23:27:32 +04:00
|
|
|
if (cp)
|
1993-04-09 23:25:07 +04:00
|
|
|
*qfnamep = cp;
|
1994-06-08 23:27:32 +04:00
|
|
|
else {
|
|
|
|
(void)snprintf(buf, sizeof(buf),
|
|
|
|
"%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
|
|
|
|
*qfnamep = buf;
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to manage the file usage table.
|
|
|
|
*
|
|
|
|
* Lookup an id of a specific type.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct fileusage *
|
1993-04-09 23:25:07 +04:00
|
|
|
lookup(id, type)
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t id;
|
1993-04-09 23:25:07 +04:00
|
|
|
int type;
|
|
|
|
{
|
1996-09-28 03:25:34 +04:00
|
|
|
struct fileusage *fup;
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
|
|
|
|
if (fup->fu_id == id)
|
|
|
|
return (fup);
|
1994-06-08 23:27:32 +04:00
|
|
|
return (NULL);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new file usage id if it does not already exist.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static struct fileusage *
|
1993-04-09 23:25:07 +04:00
|
|
|
addid(id, type, name)
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t id;
|
1993-04-09 23:25:07 +04:00
|
|
|
int type;
|
1998-07-27 04:52:00 +04:00
|
|
|
const char *name;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
|
|
|
struct fileusage *fup, **fhp;
|
|
|
|
int len;
|
|
|
|
|
1996-09-28 03:25:34 +04:00
|
|
|
if ((fup = lookup(id, type)) != NULL)
|
1993-04-09 23:25:07 +04:00
|
|
|
return (fup);
|
|
|
|
if (name)
|
|
|
|
len = strlen(name);
|
|
|
|
else
|
|
|
|
len = 10;
|
1994-06-08 23:27:32 +04:00
|
|
|
if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
|
1994-08-16 23:22:02 +04:00
|
|
|
err(1, "%s", strerror(errno));
|
1993-04-09 23:25:07 +04:00
|
|
|
fhp = &fuhead[type][id & (FUHASH - 1)];
|
|
|
|
fup->fu_next = *fhp;
|
|
|
|
*fhp = fup;
|
|
|
|
fup->fu_id = id;
|
|
|
|
if (id > highid[type])
|
|
|
|
highid[type] = id;
|
1994-06-08 23:27:32 +04:00
|
|
|
if (name)
|
1997-09-16 17:07:59 +04:00
|
|
|
memmove(fup->fu_name, name, len + 1);
|
1994-06-08 23:27:32 +04:00
|
|
|
else
|
2004-12-12 08:57:03 +03:00
|
|
|
(void) sprintf(fup->fu_name, "%" PRIu32, id);
|
1993-04-09 23:25:07 +04:00
|
|
|
return (fup);
|
|
|
|
}
|
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
static u_int32_t
|
2004-01-05 03:17:07 +03:00
|
|
|
subsequent(id, type)
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t id;
|
2004-01-05 03:17:07 +03:00
|
|
|
int type;
|
|
|
|
{
|
|
|
|
struct fileusage *fup, **iup, **cup;
|
2004-12-12 08:57:03 +03:00
|
|
|
u_int32_t next, offset;
|
2004-01-05 03:17:07 +03:00
|
|
|
|
|
|
|
next = highid[type] + 1;
|
|
|
|
offset = 0;
|
|
|
|
cup = iup = &fuhead[type][id & (FUHASH-1)];
|
|
|
|
do {
|
|
|
|
++offset;
|
|
|
|
if (++cup >= &fuhead[type][FUHASH])
|
|
|
|
cup = &fuhead[type][0];
|
|
|
|
for (fup = *cup; fup != 0; fup = fup->fu_next) {
|
|
|
|
if (fup->fu_id > id && fup->fu_id <= id + offset)
|
|
|
|
return (fup->fu_id);
|
|
|
|
if (fup->fu_id > id && fup->fu_id < next)
|
|
|
|
next = fup->fu_id;
|
|
|
|
}
|
|
|
|
} while (cup != iup);
|
|
|
|
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
/*
|
2003-04-02 14:39:19 +04:00
|
|
|
* Special purpose version of ginode used to optimize first pass
|
1993-04-09 23:25:07 +04:00
|
|
|
* over all the inodes in numerical order.
|
|
|
|
*/
|
2003-04-02 14:39:19 +04:00
|
|
|
static ino_t nextino, lastinum, lastvalidinum;
|
1996-09-28 03:25:34 +04:00
|
|
|
static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
|
2004-12-12 08:57:03 +03:00
|
|
|
static union comb_dinode *inodebuf;
|
2003-04-02 14:39:19 +04:00
|
|
|
#define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
|
1993-04-09 23:25:07 +04:00
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
union comb_dinode *
|
1993-04-09 23:25:07 +04:00
|
|
|
getnextinode(inumber)
|
|
|
|
ino_t inumber;
|
|
|
|
{
|
|
|
|
long size;
|
|
|
|
daddr_t dblk;
|
2004-12-12 08:57:03 +03:00
|
|
|
static union comb_dinode *dp;
|
|
|
|
union comb_dinode *ret;
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
if (inumber != nextino++ || inumber > lastvalidinum) {
|
2005-08-19 06:07:18 +04:00
|
|
|
errx(1, "bad inode number %llu to nextinode",
|
|
|
|
(unsigned long long)inumber);
|
2003-04-02 14:39:19 +04:00
|
|
|
}
|
1993-04-09 23:25:07 +04:00
|
|
|
|
|
|
|
if (inumber >= lastinum) {
|
|
|
|
readcnt++;
|
1994-06-08 23:27:32 +04:00
|
|
|
dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
|
1993-04-09 23:25:07 +04:00
|
|
|
if (readcnt % readpercg == 0) {
|
|
|
|
size = partialsize;
|
|
|
|
lastinum += partialcnt;
|
|
|
|
} else {
|
|
|
|
size = inobufsize;
|
|
|
|
lastinum += fullcnt;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
(void)bread(dblk, (caddr_t)inodebuf, size);
|
|
|
|
if (needswap) {
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
swap_dinode2(inodebuf, lastinum - inumber);
|
|
|
|
else
|
2004-12-12 08:57:03 +03:00
|
|
|
#endif
|
2003-04-02 14:39:19 +04:00
|
|
|
swap_dinode1(inodebuf, lastinum - inumber);
|
|
|
|
}
|
2004-12-12 08:57:03 +03:00
|
|
|
dp = (union comb_dinode *)inodebuf;
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
ret = dp;
|
2004-12-12 08:57:03 +03:00
|
|
|
dp = (union comb_dinode *)
|
2003-04-02 14:39:19 +04:00
|
|
|
((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
|
|
|
|
return ret;
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
void
|
|
|
|
setinodebuf(inum)
|
|
|
|
ino_t inum;
|
1993-04-09 23:25:07 +04:00
|
|
|
{
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
if (inum % sblock.fs_ipg != 0)
|
2005-08-19 06:07:18 +04:00
|
|
|
errx(1, "bad inode number %llu to setinodebuf",
|
|
|
|
(unsigned long long)inum);
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
lastvalidinum = inum + sblock.fs_ipg - 1;
|
|
|
|
nextino = inum;
|
|
|
|
lastinum = inum;
|
1993-04-09 23:25:07 +04:00
|
|
|
readcnt = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
if (inodebuf != NULL)
|
|
|
|
return;
|
1993-04-09 23:25:07 +04:00
|
|
|
inobufsize = blkroundup(&sblock, INOBUFSIZE);
|
2003-04-02 14:39:19 +04:00
|
|
|
fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
|
1993-04-09 23:25:07 +04:00
|
|
|
readpercg = sblock.fs_ipg / fullcnt;
|
|
|
|
partialcnt = sblock.fs_ipg % fullcnt;
|
2003-04-02 14:39:19 +04:00
|
|
|
partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
|
1993-04-09 23:25:07 +04:00
|
|
|
if (partialcnt != 0) {
|
|
|
|
readpercg++;
|
|
|
|
} else {
|
|
|
|
partialcnt = fullcnt;
|
|
|
|
partialsize = inobufsize;
|
|
|
|
}
|
|
|
|
if (inodebuf == NULL &&
|
2003-04-02 14:39:19 +04:00
|
|
|
(inodebuf = malloc((unsigned)inobufsize)) == NULL)
|
|
|
|
errx(1, "Cannot allocate space for inode buffer");
|
2003-04-17 13:21:01 +04:00
|
|
|
while (nextino < ROOTINO)
|
|
|
|
getnextinode(nextino);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
void
|
1993-04-09 23:25:07 +04:00
|
|
|
freeinodebuf()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (inodebuf != NULL)
|
2003-04-02 14:39:19 +04:00
|
|
|
free((char *)inodebuf);
|
1993-04-09 23:25:07 +04:00
|
|
|
inodebuf = NULL;
|
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
#ifdef HAVE_UFSv2
|
2003-04-02 14:39:19 +04:00
|
|
|
static void
|
2004-12-12 08:57:03 +03:00
|
|
|
swap_dinode1(union comb_dinode *dp, int n)
|
2003-04-02 14:39:19 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct ufs1_dinode *dp1;
|
|
|
|
|
|
|
|
dp1 = (struct ufs1_dinode *)&dp->dp1;
|
|
|
|
for (i = 0; i < n; i++, dp1++)
|
|
|
|
ffs_dinode1_swap(dp1, dp1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-12-12 08:57:03 +03:00
|
|
|
swap_dinode2(union comb_dinode *dp, int n)
|
2003-04-02 14:39:19 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct ufs2_dinode *dp2;
|
|
|
|
|
|
|
|
dp2 = (struct ufs2_dinode *)&dp->dp2;
|
|
|
|
for (i = 0; i < n; i++, dp2++)
|
|
|
|
ffs_dinode2_swap(dp2, dp2);
|
|
|
|
}
|
|
|
|
|
2004-12-12 08:57:03 +03:00
|
|
|
#else
|
|
|
|
|
|
|
|
static void
|
|
|
|
swap_dinode1(union comb_dinode *dp, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dinode *dp1;
|
|
|
|
|
|
|
|
dp1 = (struct dinode *) &dp->dp1;
|
|
|
|
for (i = 0; i < n; i++, dp1++)
|
|
|
|
ffs_dinode_swap(dp1, dp1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1993-04-09 23:25:07 +04:00
|
|
|
/*
|
|
|
|
* Read specified disk blocks.
|
|
|
|
*/
|
1996-09-28 03:25:34 +04:00
|
|
|
static void
|
1993-04-09 23:25:07 +04:00
|
|
|
bread(bno, buf, cnt)
|
|
|
|
daddr_t bno;
|
|
|
|
char *buf;
|
|
|
|
long cnt;
|
|
|
|
{
|
|
|
|
|
1994-06-08 23:27:32 +04:00
|
|
|
if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
|
|
|
|
read(fi, buf, cnt) != cnt)
|
2003-01-25 00:55:02 +03:00
|
|
|
err(1, "block %lld", (long long)bno);
|
1993-04-09 23:25:07 +04:00
|
|
|
}
|
2002-12-11 01:42:00 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
infohandler(int sig)
|
|
|
|
{
|
|
|
|
got_siginfo = 1;
|
|
|
|
}
|