2022-11-17 09:40:38 +03:00
|
|
|
/* $NetBSD: quot.c,v 1.35 2022/11/17 06:40:41 chs Exp $ */
|
1996-05-24 16:51:23 +04:00
|
|
|
|
1994-02-10 20:58:38 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1991, 1994 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1991, 1994 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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.
|
|
|
|
*/
|
1994-02-11 03:23:34 +03:00
|
|
|
|
1997-10-17 16:36:30 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-02-11 03:23:34 +03:00
|
|
|
#ifndef lint
|
2022-11-17 09:40:38 +03:00
|
|
|
__RCSID("$NetBSD: quot.c,v 1.35 2022/11/17 06:40:41 chs Exp $");
|
1994-02-11 03:23:34 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-02-10 20:58:38 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
1994-04-25 22:17:04 +04:00
|
|
|
#include <sys/time.h>
|
2003-04-02 14:39:19 +04:00
|
|
|
#include <ufs/ufs/dinode.h>
|
1998-03-01 05:20:01 +03:00
|
|
|
#include <ufs/ffs/fs.h>
|
1994-02-10 20:58:38 +03:00
|
|
|
|
1997-10-18 15:11:18 +04:00
|
|
|
#include <err.h>
|
1997-10-17 16:36:30 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
1994-04-25 22:17:04 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-05-24 12:52:41 +04:00
|
|
|
#include <unistd.h>
|
1994-04-25 22:17:04 +04:00
|
|
|
|
1994-02-10 20:58:38 +03:00
|
|
|
/* some flags of what to do: */
|
|
|
|
static char estimate;
|
|
|
|
static char count;
|
|
|
|
static char unused;
|
2011-03-07 02:41:47 +03:00
|
|
|
static void (*func)(int, struct fs *, const char *);
|
1994-02-10 20:58:38 +03:00
|
|
|
static long blocksize;
|
|
|
|
static char *header;
|
|
|
|
|
1994-02-11 03:23:34 +03:00
|
|
|
/*
|
|
|
|
* Original BSD quot doesn't round to number of frags/blocks,
|
|
|
|
* doesn't account for indirection blocks and gets it totally
|
|
|
|
* wrong if the size is a multiple of the blocksize.
|
1996-05-24 16:51:23 +04:00
|
|
|
* The new code always counts the number of DEV_BSIZE byte blocks
|
1994-02-11 03:23:34 +03:00
|
|
|
* instead of the number of kilobytes and converts them to
|
|
|
|
* kByte when done (on request).
|
|
|
|
*/
|
1994-02-10 20:58:38 +03:00
|
|
|
#ifdef COMPAT
|
1999-10-06 11:20:20 +04:00
|
|
|
#define SIZE(n) ((long long)(n))
|
1994-02-10 20:58:38 +03:00
|
|
|
#else
|
1999-10-06 11:20:20 +04:00
|
|
|
#define SIZE(n) howmany((long long)(n) * DEV_BSIZE, (long long)blocksize)
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define INOCNT(fs) ((fs)->fs_ipg)
|
2003-04-02 14:39:19 +04:00
|
|
|
#define INOSZ(fs) \
|
|
|
|
(((fs)->fs_magic == FS_UFS1_MAGIC ? sizeof(struct ufs1_dinode) : \
|
|
|
|
sizeof(struct ufs2_dinode)) * INOCNT(fs))
|
|
|
|
|
|
|
|
union dinode {
|
|
|
|
struct ufs1_dinode dp1;
|
|
|
|
struct ufs2_dinode dp2;
|
|
|
|
};
|
|
|
|
#define DIP(fs, dp, field) \
|
|
|
|
(((fs)->fs_magic == FS_UFS1_MAGIC) ? \
|
|
|
|
(dp)->dp1.di_##field : (dp)->dp2.di_##field)
|
|
|
|
|
1994-02-10 20:58:38 +03:00
|
|
|
|
2011-03-07 02:41:47 +03:00
|
|
|
static int cmpusers(const void *, const void *);
|
|
|
|
static void dofsizes(int, struct fs *, const char *);
|
|
|
|
static void donames(int, struct fs *, const char *);
|
|
|
|
static void douser(int, struct fs *, const char *);
|
|
|
|
static union dinode *get_inode(int, struct fs *, ino_t);
|
|
|
|
static void ffs_oldfscompat(struct fs *);
|
|
|
|
static void initfsizes(void);
|
|
|
|
static void inituser(void);
|
|
|
|
static int isfree(struct fs *, union dinode *);
|
|
|
|
static void quot(const char *, const char *);
|
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
|
|
|
static struct user *user(uid_t);
|
|
|
|
static void uses(uid_t, daddr_t, time_t);
|
|
|
|
static void usrrehash(void);
|
|
|
|
static int virtualblocks(struct fs *, union dinode *);
|
1997-10-17 16:36:30 +04:00
|
|
|
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
static union dinode *
|
2011-03-07 02:41:47 +03:00
|
|
|
get_inode(int fd, struct fs *super, ino_t ino)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
static char *ipbuf;
|
1994-02-10 20:58:38 +03:00
|
|
|
static ino_t last;
|
|
|
|
|
|
|
|
if (fd < 0) { /* flush cache */
|
2003-04-02 14:39:19 +04:00
|
|
|
if (ipbuf) {
|
|
|
|
free(ipbuf);
|
|
|
|
ipbuf = NULL;
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
if (!ipbuf || ino < last || ino >= last + INOCNT(super)) {
|
|
|
|
if (!ipbuf
|
|
|
|
&& !(ipbuf = malloc(INOSZ(super))))
|
1997-10-18 15:11:18 +04:00
|
|
|
errx(1, "allocate inodes");
|
1994-02-10 20:58:38 +03:00
|
|
|
last = (ino / INOCNT(super)) * INOCNT(super);
|
1996-05-24 16:51:23 +04:00
|
|
|
if (lseek(fd,
|
1996-12-12 03:43:28 +03:00
|
|
|
(off_t)ino_to_fsba(super, last) << super->fs_fshift,
|
|
|
|
0) < 0 ||
|
2011-03-07 02:41:47 +03:00
|
|
|
read(fd, ipbuf, INOSZ(super)) != (ssize_t)INOSZ(super))
|
1997-10-18 15:11:18 +04:00
|
|
|
errx(1, "read inodes");
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
|
|
|
if (super->fs_magic == FS_UFS1_MAGIC)
|
|
|
|
return ((union dinode *)
|
|
|
|
&((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]);
|
|
|
|
return ((union dinode *)
|
|
|
|
&((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)]);
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COMPAT
|
2011-03-07 02:41:47 +03:00
|
|
|
#define actualblocks(fs, dp) (int)(DIP(fs, dp, blocks) / 2)
|
1994-02-10 20:58:38 +03:00
|
|
|
#else
|
2011-03-07 02:41:47 +03:00
|
|
|
#define actualblocks(fs, dp) (int)(DIP(fs, dp, blocks))
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static int
|
2011-03-07 02:41:47 +03:00
|
|
|
virtualblocks(struct fs *super, union dinode *dp)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
1997-10-18 15:11:18 +04:00
|
|
|
off_t nblk, sz;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
sz = DIP(super, dp, size);
|
1994-02-10 20:58:38 +03:00
|
|
|
#ifdef COMPAT
|
2013-06-23 11:28:36 +04:00
|
|
|
if (ffs_lblkno(super, sz) >= UFS_NDADDR) {
|
|
|
|
nblk = ffs_blkroundup(super, sz);
|
1994-02-10 20:58:38 +03:00
|
|
|
if (sz == nblk)
|
|
|
|
nblk += super->fs_bsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sz / 1024;
|
|
|
|
#else /* COMPAT */
|
|
|
|
|
2013-06-23 11:28:36 +04:00
|
|
|
if (ffs_lblkno(super, sz) >= UFS_NDADDR) {
|
|
|
|
nblk = ffs_blkroundup(super, sz);
|
|
|
|
sz = ffs_lblkno(super, nblk);
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
sz = howmany(sz - UFS_NDADDR, FFS_NINDIR(super));
|
1994-02-10 20:58:38 +03:00
|
|
|
while (sz > 0) {
|
|
|
|
nblk += sz * super->fs_bsize;
|
1996-05-24 16:51:23 +04:00
|
|
|
/* One block on this level is in the inode itself */
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
sz = howmany(sz - 1, FFS_NINDIR(super));
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
} else
|
2013-06-23 11:28:36 +04:00
|
|
|
nblk = ffs_fragroundup(super, sz);
|
1994-02-10 20:58:38 +03:00
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
return nblk / DEV_BSIZE;
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif /* COMPAT */
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static int
|
2003-04-02 14:39:19 +04:00
|
|
|
isfree(fs, dp)
|
|
|
|
struct fs *fs;
|
|
|
|
union dinode *dp;
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
|
|
|
#ifdef COMPAT
|
2003-04-02 14:39:19 +04:00
|
|
|
return (DIP(fs, dp, mode) & IFMT) == 0;
|
1994-02-10 20:58:38 +03:00
|
|
|
#else /* COMPAT */
|
2003-04-02 14:39:19 +04:00
|
|
|
switch (DIP(fs, dp, mode) & IFMT) {
|
1994-02-10 20:58:38 +03:00
|
|
|
case IFIFO:
|
|
|
|
case IFLNK: /* should check FASTSYMLINK? */
|
|
|
|
case IFDIR:
|
|
|
|
case IFREG:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct user {
|
|
|
|
uid_t uid;
|
|
|
|
char *name;
|
|
|
|
daddr_t space;
|
|
|
|
long count;
|
|
|
|
daddr_t spc30;
|
|
|
|
daddr_t spc60;
|
|
|
|
daddr_t spc90;
|
|
|
|
} *users;
|
|
|
|
static int nusers;
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
inituser(void)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
1997-10-18 15:11:18 +04:00
|
|
|
int i;
|
|
|
|
struct user *usr;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
|
|
|
if (!nusers) {
|
|
|
|
nusers = 8;
|
2011-03-07 02:41:47 +03:00
|
|
|
if (!(users = calloc(nusers, sizeof(*users))))
|
|
|
|
err(1, "allocate users");
|
1994-02-10 20:58:38 +03:00
|
|
|
} else {
|
|
|
|
for (usr = users, i = nusers; --i >= 0; usr++) {
|
|
|
|
usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
|
|
|
|
usr->count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
usrrehash(void)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
1997-10-18 15:11:18 +04:00
|
|
|
int i;
|
|
|
|
struct user *usr, *usrn;
|
1994-02-10 20:58:38 +03:00
|
|
|
struct user *svusr;
|
|
|
|
|
|
|
|
svusr = users;
|
|
|
|
nusers <<= 1;
|
2011-03-07 02:41:47 +03:00
|
|
|
if (!(users = calloc(nusers, sizeof(*users))))
|
|
|
|
err(1, "allocate users");
|
1994-02-10 20:58:38 +03:00
|
|
|
for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
|
1996-05-24 16:51:23 +04:00
|
|
|
for (usrn = users + (usr->uid&(nusers - 1));
|
|
|
|
usrn->name;
|
|
|
|
usrn--) {
|
1994-02-10 20:58:38 +03:00
|
|
|
if (usrn <= users)
|
|
|
|
usrn = users + nusers;
|
|
|
|
}
|
|
|
|
*usrn = *usr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static struct user *
|
2011-03-07 02:41:47 +03:00
|
|
|
user(uid_t uid)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
1997-10-18 15:11:18 +04:00
|
|
|
struct user *usr;
|
|
|
|
int i;
|
1994-02-10 20:58:38 +03:00
|
|
|
struct passwd *pwd;
|
|
|
|
|
2011-03-07 02:41:47 +03:00
|
|
|
for (;;) {
|
|
|
|
for (usr = users + (uid & (nusers - 1)), i = nusers;
|
1996-05-24 16:51:23 +04:00
|
|
|
--i >= 0;
|
|
|
|
usr--) {
|
1994-02-10 20:58:38 +03:00
|
|
|
if (!usr->name) {
|
|
|
|
usr->uid = uid;
|
|
|
|
|
2011-03-07 02:41:47 +03:00
|
|
|
if (!(pwd = getpwuid(uid)))
|
|
|
|
asprintf(&usr->name, "#%u", uid);
|
|
|
|
else
|
|
|
|
asprintf(&usr->name, "%s",
|
|
|
|
pwd->pw_name);
|
1997-10-18 15:11:18 +04:00
|
|
|
if (!usr->name)
|
|
|
|
errx(1, "allocate users");
|
1994-02-10 20:58:38 +03:00
|
|
|
return usr;
|
|
|
|
} else if (usr->uid == uid)
|
|
|
|
return usr;
|
|
|
|
|
|
|
|
if (usr <= users)
|
|
|
|
usr = users + nusers;
|
|
|
|
}
|
|
|
|
usrrehash();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static int
|
|
|
|
cmpusers(u1, u2)
|
1997-10-17 16:36:30 +04:00
|
|
|
const void *u1, *u2;
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
2009-04-18 12:17:23 +04:00
|
|
|
return ((const struct user *)u2)->space - ((const struct user *)u1)->space;
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
1994-02-11 03:23:34 +03:00
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
#define sortusers(users) (qsort((users), nusers, sizeof(struct user), \
|
|
|
|
cmpusers))
|
1994-02-10 20:58:38 +03:00
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
|
|
|
uses(uid, blks, act)
|
1994-02-10 20:58:38 +03:00
|
|
|
uid_t uid;
|
|
|
|
daddr_t blks;
|
|
|
|
time_t act;
|
|
|
|
{
|
|
|
|
static time_t today;
|
1997-10-18 15:11:18 +04:00
|
|
|
struct user *usr;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
|
|
|
if (!today)
|
|
|
|
time(&today);
|
|
|
|
|
|
|
|
usr = user(uid);
|
|
|
|
usr->count++;
|
|
|
|
usr->space += blks;
|
|
|
|
|
|
|
|
if (today - act > 90L * 24L * 60L * 60L)
|
|
|
|
usr->spc90 += blks;
|
|
|
|
if (today - act > 60L * 24L * 60L * 60L)
|
|
|
|
usr->spc60 += blks;
|
|
|
|
if (today - act > 30L * 24L * 60L * 60L)
|
|
|
|
usr->spc30 += blks;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COMPAT
|
|
|
|
#define FSZCNT 500
|
|
|
|
#else
|
|
|
|
#define FSZCNT 512
|
|
|
|
#endif
|
|
|
|
struct fsizes {
|
|
|
|
struct fsizes *fsz_next;
|
|
|
|
daddr_t fsz_first, fsz_last;
|
|
|
|
ino_t fsz_count[FSZCNT];
|
|
|
|
daddr_t fsz_sz[FSZCNT];
|
|
|
|
} *fsizes;
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
|
|
|
initfsizes()
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
1997-10-18 15:11:18 +04:00
|
|
|
struct fsizes *fp;
|
|
|
|
int i;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
|
|
|
for (fp = fsizes; fp; fp = fp->fsz_next) {
|
|
|
|
for (i = FSZCNT; --i >= 0;) {
|
|
|
|
fp->fsz_count[i] = 0;
|
|
|
|
fp->fsz_sz[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
dofsizes(int fd, struct fs *super, const char *name)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
|
|
|
ino_t inode, maxino;
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
1994-02-10 20:58:38 +03:00
|
|
|
daddr_t sz, ksz;
|
|
|
|
struct fsizes *fp, **fsp;
|
1997-10-18 15:11:18 +04:00
|
|
|
int i;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
|
|
|
maxino = super->fs_ncg * super->fs_ipg - 1;
|
|
|
|
#ifdef COMPAT
|
2011-03-07 02:41:47 +03:00
|
|
|
if (!(fsizes = malloc(sizeof(*fsizes))))
|
|
|
|
err(1, "alloc fsize structure");
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif /* COMPAT */
|
|
|
|
for (inode = 0; inode < maxino; inode++) {
|
|
|
|
errno = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
if ((dp = get_inode(fd, super, inode))
|
1994-02-10 20:58:38 +03:00
|
|
|
#ifdef COMPAT
|
2003-04-02 14:39:19 +04:00
|
|
|
&& ((DIP(super, dp, mode) & IFMT) == IFREG
|
|
|
|
|| (DIP(dp, mode) & IFMT) == IFDIR)
|
1994-02-10 20:58:38 +03:00
|
|
|
#else /* COMPAT */
|
2003-04-02 14:39:19 +04:00
|
|
|
&& !isfree(super, dp)
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif /* COMPAT */
|
|
|
|
) {
|
2003-04-02 14:39:19 +04:00
|
|
|
sz = estimate ? virtualblocks(super, dp) :
|
|
|
|
actualblocks(super, dp);
|
1994-02-10 20:58:38 +03:00
|
|
|
#ifdef COMPAT
|
|
|
|
if (sz >= FSZCNT) {
|
|
|
|
fsizes->fsz_count[FSZCNT-1]++;
|
|
|
|
fsizes->fsz_sz[FSZCNT-1] += sz;
|
|
|
|
} else {
|
|
|
|
fsizes->fsz_count[sz]++;
|
|
|
|
fsizes->fsz_sz[sz] += sz;
|
|
|
|
}
|
|
|
|
#else /* COMPAT */
|
|
|
|
ksz = SIZE(sz);
|
1997-10-17 16:36:30 +04:00
|
|
|
for (fsp = &fsizes; (fp = *fsp) != NULL;
|
|
|
|
fsp = &fp->fsz_next) {
|
1994-02-10 20:58:38 +03:00
|
|
|
if (ksz < fp->fsz_last)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!fp || ksz < fp->fsz_first) {
|
2011-03-07 02:41:47 +03:00
|
|
|
if (!(fp = malloc(sizeof(*fp))))
|
|
|
|
err(1, "alloc fsize structure");
|
1994-02-10 20:58:38 +03:00
|
|
|
fp->fsz_next = *fsp;
|
|
|
|
*fsp = fp;
|
|
|
|
fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
|
|
|
|
fp->fsz_last = fp->fsz_first + FSZCNT;
|
|
|
|
for (i = FSZCNT; --i >= 0;) {
|
|
|
|
fp->fsz_count[i] = 0;
|
|
|
|
fp->fsz_sz[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fp->fsz_count[ksz % FSZCNT]++;
|
|
|
|
fp->fsz_sz[ksz % FSZCNT] += sz;
|
|
|
|
#endif /* COMPAT */
|
1997-10-18 15:11:18 +04:00
|
|
|
} else if (errno)
|
|
|
|
errx(1, "%s", name);
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
sz = 0;
|
|
|
|
for (fp = fsizes; fp; fp = fp->fsz_next) {
|
|
|
|
for (i = 0; i < FSZCNT; i++) {
|
|
|
|
if (fp->fsz_count[i])
|
1999-10-06 11:20:20 +04:00
|
|
|
printf("%ld\t%ld\t%lld\n",
|
1997-10-17 16:36:30 +04:00
|
|
|
(long)(fp->fsz_first + i),
|
|
|
|
(long)fp->fsz_count[i],
|
1999-10-05 07:14:28 +04:00
|
|
|
SIZE(sz += fp->fsz_sz[i]));
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
douser(int fd, struct fs *super, const char *name)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
|
|
|
ino_t inode, maxino;
|
|
|
|
struct user *usr, *usrs;
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
1997-10-18 15:11:18 +04:00
|
|
|
int n;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
|
|
|
maxino = super->fs_ncg * super->fs_ipg - 1;
|
|
|
|
for (inode = 0; inode < maxino; inode++) {
|
|
|
|
errno = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
if ((dp = get_inode(fd, super, inode))
|
|
|
|
&& !isfree(super, dp))
|
|
|
|
uses(DIP(super, dp, uid),
|
|
|
|
estimate ? virtualblocks(super, dp) :
|
|
|
|
actualblocks(super, dp), DIP(super, dp, atime));
|
1997-10-18 15:11:18 +04:00
|
|
|
else if (errno)
|
|
|
|
errx(1, "%s", name);
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
2011-03-07 02:41:47 +03:00
|
|
|
if (!(usrs = calloc(nusers, sizeof(*usrs))))
|
1997-10-18 15:11:18 +04:00
|
|
|
errx(1, "allocate users");
|
2011-03-07 02:41:47 +03:00
|
|
|
memmove(usrs, users, nusers * sizeof(*usrs));
|
1994-02-10 20:58:38 +03:00
|
|
|
sortusers(usrs);
|
|
|
|
for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
|
1999-10-06 11:20:20 +04:00
|
|
|
printf("%5lld", SIZE(usr->space));
|
1994-02-10 20:58:38 +03:00
|
|
|
if (count)
|
1999-10-05 07:14:28 +04:00
|
|
|
printf("\t%5ld", usr->count);
|
1996-05-24 16:51:23 +04:00
|
|
|
printf("\t%-8s", usr->name);
|
1994-02-10 20:58:38 +03:00
|
|
|
if (unused)
|
1999-10-06 11:20:20 +04:00
|
|
|
printf("\t%5lld\t%5lld\t%5lld",
|
1996-12-12 03:43:28 +03:00
|
|
|
SIZE(usr->spc30), SIZE(usr->spc60),
|
|
|
|
SIZE(usr->spc90));
|
1994-02-10 20:58:38 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
free(usrs);
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
donames(int fd, struct fs *super, const char *name)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
|
|
|
int c;
|
2013-10-19 21:16:37 +04:00
|
|
|
ino_t inode;
|
|
|
|
#ifdef COMPAT
|
|
|
|
ino_t inode1 = -1;
|
|
|
|
#endif
|
1994-02-10 20:58:38 +03:00
|
|
|
ino_t maxino;
|
2003-04-02 14:39:19 +04:00
|
|
|
union dinode *dp;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
|
|
|
maxino = super->fs_ncg * super->fs_ipg - 1;
|
|
|
|
/* first skip the name of the filesystem */
|
|
|
|
while ((c = getchar()) != EOF && (c < '0' || c > '9'))
|
|
|
|
while ((c = getchar()) != EOF && c != '\n');
|
1996-05-24 16:51:23 +04:00
|
|
|
ungetc(c, stdin);
|
2005-08-20 19:00:27 +04:00
|
|
|
while (scanf("%" SCNu64, &inode) == 1) {
|
2009-04-18 12:17:23 +04:00
|
|
|
if (inode > maxino) {
|
1996-05-24 16:51:23 +04:00
|
|
|
#ifndef COMPAT
|
2005-08-20 19:00:27 +04:00
|
|
|
warnx("invalid inode %" PRIu64, inode);
|
1996-05-24 16:51:23 +04:00
|
|
|
#endif
|
1994-02-10 20:58:38 +03:00
|
|
|
return;
|
|
|
|
}
|
1996-05-24 16:51:23 +04:00
|
|
|
#ifdef COMPAT
|
|
|
|
if (inode < inode1)
|
|
|
|
continue;
|
|
|
|
#endif
|
1994-02-10 20:58:38 +03:00
|
|
|
errno = 0;
|
2003-04-02 14:39:19 +04:00
|
|
|
if ((dp = get_inode(fd, super, inode))
|
|
|
|
&& !isfree(super, dp)) {
|
|
|
|
printf("%s\t", user(DIP(super, dp, uid))->name);
|
1994-02-10 20:58:38 +03:00
|
|
|
/* now skip whitespace */
|
|
|
|
while ((c = getchar()) == ' ' || c == '\t');
|
|
|
|
/* and print out the remainder of the input line */
|
|
|
|
while (c != EOF && c != '\n') {
|
|
|
|
putchar(c);
|
|
|
|
c = getchar();
|
|
|
|
}
|
|
|
|
putchar('\n');
|
2013-10-19 21:16:37 +04:00
|
|
|
#ifdef COMPAT
|
1994-02-10 20:58:38 +03:00
|
|
|
inode1 = inode;
|
2013-10-19 21:16:37 +04:00
|
|
|
#endif
|
1994-02-10 20:58:38 +03:00
|
|
|
} else {
|
1997-10-18 15:11:18 +04:00
|
|
|
if (errno)
|
|
|
|
errx(1, "%s", name);
|
1994-02-10 20:58:38 +03:00
|
|
|
/* skip this line */
|
2011-03-07 02:41:47 +03:00
|
|
|
while ((c = getchar()) != EOF && c != '\n')
|
|
|
|
continue;
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
usage(void)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
2011-03-07 02:41:47 +03:00
|
|
|
const char *p = getprogname();
|
1994-02-10 20:58:38 +03:00
|
|
|
#ifdef COMPAT
|
2011-03-07 02:41:47 +03:00
|
|
|
fprintf(stderr, "Usage: %s [-nfcvha] [<filesystem> ...]\n", p);
|
1994-02-10 20:58:38 +03:00
|
|
|
#else /* COMPAT */
|
2011-03-07 02:41:47 +03:00
|
|
|
fprintf(stderr, "Usage: %s [ -acfhknv ] [<filesystem> ... ]\n", p);
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif /* COMPAT */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
/*
|
|
|
|
* Sanity checks for old file systems.
|
|
|
|
* Stolen from <sys/lib/libsa/ufs.c>
|
|
|
|
*/
|
|
|
|
static void
|
2011-03-07 02:41:47 +03:00
|
|
|
ffs_oldfscompat(struct fs *fs)
|
1996-05-24 16:51:23 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-07-28 11:24:58 +03:00
|
|
|
if (fs->fs_magic == FS_UFS1_MAGIC &&
|
|
|
|
fs->fs_old_inodefmt < FS_44INODEFMT) {
|
2003-04-02 14:39:19 +04:00
|
|
|
quad_t sizepb = fs->fs_bsize;
|
|
|
|
|
2013-01-22 13:39:11 +04:00
|
|
|
fs->fs_maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
|
|
|
|
for (i = 0; i < UFS_NIADDR; i++) {
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
sizepb *= FFS_NINDIR(fs);
|
2003-04-02 14:39:19 +04:00
|
|
|
fs->fs_maxfilesize += sizepb;
|
|
|
|
}
|
|
|
|
fs->fs_qbmask = ~fs->fs_bmask;
|
|
|
|
fs->fs_qfmask = ~fs->fs_fmask;
|
|
|
|
}
|
1996-05-24 16:51:23 +04:00
|
|
|
}
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
/*
|
|
|
|
* Possible superblock locations ordered from most to least likely.
|
|
|
|
*/
|
|
|
|
static int sblock_try[] = SBLOCKSEARCH;
|
|
|
|
static char superblock[SBLOCKSIZE];
|
|
|
|
|
|
|
|
|
2011-03-07 02:41:47 +03:00
|
|
|
static void
|
|
|
|
quot(const char *name, const char *mp)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
int fd, i;
|
|
|
|
struct fs *fs;
|
2004-03-22 01:07:22 +03:00
|
|
|
int sbloc;
|
1994-02-10 20:58:38 +03:00
|
|
|
|
1997-10-17 16:36:30 +04:00
|
|
|
get_inode(-1, 0, 0); /* flush cache */
|
1994-02-10 20:58:38 +03:00
|
|
|
inituser();
|
|
|
|
initfsizes();
|
2003-04-02 14:39:19 +04:00
|
|
|
if ((fd = open(name, 0)) < 0) {
|
1997-11-06 17:36:45 +03:00
|
|
|
warn("%s", name);
|
1994-02-10 20:58:38 +03:00
|
|
|
return;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
2004-03-22 01:07:22 +03:00
|
|
|
for (i = 0; ; i++) {
|
|
|
|
sbloc = sblock_try[i];
|
|
|
|
if (sbloc == -1) {
|
|
|
|
warnx("%s: not a BSD filesystem", name);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pread(fd, superblock, SBLOCKSIZE, sbloc) != SBLOCKSIZE)
|
2003-04-02 14:39:19 +04:00
|
|
|
continue;
|
|
|
|
fs = (struct fs *)superblock;
|
|
|
|
|
2004-03-22 01:07:22 +03:00
|
|
|
if (fs->fs_magic != FS_UFS1_MAGIC &&
|
2022-11-17 09:40:38 +03:00
|
|
|
fs->fs_magic != FS_UFS2_MAGIC &&
|
|
|
|
fs->fs_magic != FS_UFS2EA_MAGIC)
|
2004-03-22 01:07:22 +03:00
|
|
|
continue;
|
|
|
|
|
2004-03-27 16:08:50 +03:00
|
|
|
if (fs->fs_magic == FS_UFS2_MAGIC
|
2022-11-17 09:40:38 +03:00
|
|
|
|| fs->fs_magic == FS_UFS2EA_MAGIC
|
2004-03-27 16:08:50 +03:00
|
|
|
|| fs->fs_old_flags & FS_FLAGS_UPDATED) {
|
2004-03-22 01:07:22 +03:00
|
|
|
/* Not the main superblock */
|
|
|
|
if (fs->fs_sblockloc != sbloc)
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* might be a first alt. id blocksize 64k */
|
|
|
|
if (sbloc == SBLOCK_UFS2)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs->fs_bsize > MAXBSIZE ||
|
2011-03-07 02:41:47 +03:00
|
|
|
(size_t)fs->fs_bsize < sizeof(struct fs))
|
2004-03-22 01:07:22 +03:00
|
|
|
continue;
|
|
|
|
break;
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
2004-03-22 01:07:22 +03:00
|
|
|
|
1997-10-17 16:36:30 +04:00
|
|
|
ffs_oldfscompat((struct fs *)superblock);
|
1996-05-24 16:51:23 +04:00
|
|
|
printf("%s:", name);
|
1994-02-10 20:58:38 +03:00
|
|
|
if (mp)
|
1996-05-24 16:51:23 +04:00
|
|
|
printf(" (%s)", mp);
|
1994-02-10 20:58:38 +03:00
|
|
|
putchar('\n');
|
2003-04-02 14:39:19 +04:00
|
|
|
(*func)(fd, fs, name);
|
1994-02-10 20:58:38 +03:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
1996-05-24 16:51:23 +04:00
|
|
|
int
|
2011-03-07 02:41:47 +03:00
|
|
|
main(int argc, char **argv)
|
1994-02-10 20:58:38 +03:00
|
|
|
{
|
|
|
|
char all = 0;
|
2004-04-21 05:05:31 +04:00
|
|
|
struct statvfs *mp;
|
1994-02-10 20:58:38 +03:00
|
|
|
char dev[MNAMELEN + 1];
|
|
|
|
char *nm;
|
|
|
|
int cnt;
|
|
|
|
|
|
|
|
func = douser;
|
|
|
|
#ifndef COMPAT
|
2003-05-30 04:17:25 +04:00
|
|
|
header = getbsize(NULL, &blocksize);
|
1994-02-10 20:58:38 +03:00
|
|
|
#endif
|
|
|
|
while (--argc > 0 && **++argv == '-') {
|
|
|
|
while (*++*argv) {
|
|
|
|
switch (**argv) {
|
|
|
|
case 'n':
|
|
|
|
func = donames;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
func = dofsizes;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
all = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
count = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
estimate = 1;
|
|
|
|
break;
|
|
|
|
#ifndef COMPAT
|
|
|
|
case 'k':
|
|
|
|
blocksize = 1024;
|
|
|
|
break;
|
|
|
|
#endif /* COMPAT */
|
|
|
|
case 'v':
|
|
|
|
unused = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (all) {
|
1996-05-24 16:51:23 +04:00
|
|
|
cnt = getmntinfo(&mp, MNT_NOWAIT);
|
1994-02-10 20:58:38 +03:00
|
|
|
for (; --cnt >= 0; mp++) {
|
2007-07-18 02:00:46 +04:00
|
|
|
if (!strncmp(mp->f_fstypename, MOUNT_FFS,
|
|
|
|
sizeof(mp->f_fstypename))) {
|
1997-10-17 16:36:30 +04:00
|
|
|
if ((nm =
|
|
|
|
strrchr(mp->f_mntfromname, '/')) != NULL) {
|
2011-03-07 02:41:47 +03:00
|
|
|
snprintf(dev, sizeof(dev), "/dev/r%s",
|
|
|
|
nm + 1);
|
1994-02-10 20:58:38 +03:00
|
|
|
nm = dev;
|
|
|
|
} else
|
|
|
|
nm = mp->f_mntfromname;
|
1996-05-24 16:51:23 +04:00
|
|
|
quot(nm, mp->f_mntonname);
|
1994-02-10 20:58:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--argc >= 0)
|
1996-05-24 16:51:23 +04:00
|
|
|
quot(*argv++, 0);
|
1994-02-10 20:58:38 +03:00
|
|
|
return 0;
|
|
|
|
}
|