1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/*
* Sys V version
*
* Reads the i-node structures from a raw disk
* device and then sums up the disk usage for
* each user. Prints out the number of blocks
* each user is using.
*/
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/filsys.h>
#include <sys/inode.h>
#include <sys/ino.h>
#include <fcntl.h>
#include <stdio.h>
#include <pwd.h>
/*
* Maximum user id.
*/
#ifndef MAXUID
#define MAXUID 32768
#endif
#define SBSIZE BSIZE /* size of super-block */
#define sblock sb_un.u_sblock
/*
* The super-block. We allow enough room for
* a complete disk block.
*/
union {
char dummy[SBSIZE];
struct filsys u_sblock;
} sb_un;
int nfiles; /* no. of files in filsys */
char *pname; /* program name (argv[0]) */
char *device; /* name of disk device */
char *filsys; /* name of file system */
size_t blocks[MAXUID]; /* count of blocks used */
struct dinode *dinode; /* will hold the i-nodes */
main(argc, argv)
int argc;
char **argv;
{
int i, fd;
register ino_t ino;
register struct dinode *di;
/*
* Save the program name and check our arguments.
*/
pname = *argv;
if (argc != 2) {
fprintf(stderr, "Usage: %s raw-disk-device\n", pname);
exit(1);
}
/*
* Open the device for reading.
*/
device = *++argv;
if ((fd = open(device, O_RDONLY)) < 0) {
perror(device);
exit(1);
}
/*
* Get the super-block from the device.
*/
getsblock(fd);
/*
* Get the i-node structures from the device.
*/
getinodes(fd);
close(fd);
/*
* Zero the block counts.
*/
for (i = 0; i < MAXUID; i++)
blocks[i] = 0;
/*
* Add up the number of blocks being used by each
* user id.
*/
for (ino = 0; ino < nfiles; ino++) {
/*
* ROOTINO is the first i-node; skip any
* before it.
*/
if (ino < ROOTINO)
continue;
di = &dinode[ino];
/*
* If this is zero, the i-node is free (not
* in use).
*/
if ((di->di_mode & IFMT) == 0)
continue;
/*
* Count the number of blocks being used by
* this file. We round the number of bytes to
* the next highest multiple of 512.
*/
blocks[di->di_uid] += (di->di_size + 511) / 512;
}
/*
* Print out what we added up.
*/
printusage();
exit(0);
}
/*
* getsblock--get the super-block from the device referred
* to by fd.
*/
getsblock(fd)
int fd;
{
/*
* Make sure the disk information is current. This
* causes all disk writes to be scheduled.
*/
sync();
/*
* Read in the super-block. It is stored at file
* system address SUPERBOFF.
*/
lseek(fd, (long) SUPERBOFF, 0);
read(fd, &sblock, SBSIZE);
/*
* The number of files (i-nodes) is calculated by
* multiplying the number of blocks used to hold
* i-nodes by the number of i-nodes in a block.
*/
nfiles = sblock.s_isize * INOPB;
/*
* Save the name of the file system.
*/
filsys = sblock.s_fname;
}
/*
* getinodes--read in the i-node structures from the device
* referred to by fd.
*/
getinodes(fd)
int fd;
{
register ino_t ino;
register daddr_t iblk;
struct dinode *malloc();
/*
* Allocate space for them all.
*/
dinode = malloc(nfiles * sizeof(struct dinode));
if (dinode == NULL) {
fprintf(stderr, "%s: out of memory.\n", pname);
exit(1);
}
/*
* We read in i-nodes a disk block-full at a time.
* The INOPB constant is the number of i-nodes in
* a block.
*/
for (ino = 0; ino < nfiles; ino += INOPB) {
/*
* The i-node's disk block number is given by
* the itod macro.
*/
iblk = itod(ino);
/*
* Read in this block of i-nodes.
*/
bread(fd, iblk, &dinode[ino], BSIZE);
}
}
/*
* bread--read cnt bytes from fd into buf, starting at
* address bno.
*/
bread(fd, bno, buf, cnt)
daddr_t bno;
char *buf;
int cnt;
{
int n;
/*
* Seek to the proper block. The shifting by BSHIFT
* converts the block number to a byte address.
*/
lseek(fd, (long) (bno << BSHIFT), 0);
/*
* Read in the data.
*/
if ((n = read(fd, buf, cnt)) != cnt) {
perror(filsys);
exit(1);
}
}
/*
* printusage--print out the disk usage in blocks.
*/
printusage()
{
register int i;
struct passwd *pwd;
struct passwd *getpwuid();
printf("%s (%s):\n", device, filsys);
printf(" Blocks \t User\n");
for (i = 0; i < MAXUID; i++) {
if (blocks[i] == 0)
continue;
/*
* Look up the login name, and use it if
* we find it.
*/
if ((pwd = getpwuid(i)) != NULL)
printf("%8d\t%s\n", blocks[i], pwd->pw_name);
else
printf("%8d\t#%d\n", blocks[i], i);
}
}
|