Add prototypes. Check malloc() return value. Use err(). Remove unused #includes

Do not \n nor dot terminate syslog()/err() messages. -Wall.
This commit is contained in:
Philippe Charnier 1998-07-15 06:28:05 +00:00
parent 7485340223
commit 27750b35a5
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=37664
3 changed files with 141 additions and 94 deletions

View File

@ -32,12 +32,21 @@
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <err.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <ufs/ufs/dinode.h>
@ -49,9 +58,15 @@ static char sccsid[] = "@(#)mkfs.c 8.11 (Berkeley) 5/3/95";
#include <sys/ioctl.h>
#ifndef STANDALONE
#include <a.out.h>
#include <stdio.h>
#include <stdlib.h>
#else
extern int atoi __P((char *));
extern char * getenv __P((char *));
#endif
#ifdef FSIRAND
extern long random __P((void));
extern void srandomdev __P((void));
#endif
/*
@ -102,9 +117,6 @@ extern int bbsize; /* boot block size */
extern int sbsize; /* superblock size */
extern u_long memleft; /* virtual memory available */
extern caddr_t membase; /* start address of memory based filesystem */
#ifdef STANDALONE
extern caddr_t malloc(), calloc();
#endif
extern char * filename;
union {
@ -129,7 +141,27 @@ int randinit;
daddr_t alloc();
long calcipg();
static int charsperline();
void clrblock __P((struct fs *, unsigned char *, int));
void fsinit __P((time_t));
void initcg __P((int, time_t));
int isblock __P((struct fs *, unsigned char *, int));
void iput __P((struct dinode *, ino_t));
int makedir __P((struct direct *, int));
void rdfs __P((daddr_t, int, char *));
void setblock __P((struct fs *, unsigned char *, int));
void wtfs __P((daddr_t, int, char *));
#ifndef STANDALONE
void get_memleft __P((void));
void raise_data_limit __P((void));
#else
void free __P((char *));
char * calloc __P((u_long, u_long));
caddr_t malloc __P((u_long));
caddr_t realloc __P((char *, u_long));
#endif
void
mkfs(pp, fsys, fi, fo)
struct partition *pp;
char *fsys;
@ -141,7 +173,7 @@ mkfs(pp, fsys, fi, fo)
off_t usedb;
long mapcramped, inodecramped;
long postblsize, rotblsize, totalsbsize;
int ppid, status, fd;
int ppid = 0, status, fd;
time_t utime;
quad_t sizepb;
void started();
@ -160,11 +192,9 @@ mkfs(pp, fsys, fi, fo)
if (mfs) {
ppid = getpid();
(void) signal(SIGUSR1, started);
if (i = fork()) {
if (i == -1) {
perror("mfs");
exit(10);
}
if ((i = fork())) {
if (i == -1)
err(10, "mfs");
if (waitpid(i, &status, 0) != -1 && WIFEXITED(status))
exit(WEXITSTATUS(status));
exit(11);
@ -179,18 +209,14 @@ mkfs(pp, fsys, fi, fo)
unsigned char buf[BUFSIZ];
unsigned long l,l1;
fd = open(filename,O_RDWR|O_TRUNC|O_CREAT,0644);
if(fd < 0) {
perror(filename);
exit(12);
}
if(fd < 0)
err(12, "%s", filename);
for(l=0;l< fssize * sectorsize;l += l1) {
l1 = fssize * sectorsize;
if (BUFSIZ < l1)
l1 = BUFSIZ;
if (l1 != write(fd,buf,l1)) {
perror(filename);
exit(12);
}
if (l1 != write(fd,buf,l1))
err(12, "%s", filename);
}
membase = mmap(
0,
@ -199,10 +225,8 @@ mkfs(pp, fsys, fi, fo)
MAP_SHARED,
fd,
0);
if(membase == MAP_FAILED) {
perror("mmap");
exit(12);
}
if(membase == MAP_FAILED)
err(12, "mmap");
close(fd);
} else {
#ifndef STANDALONE
@ -210,10 +234,8 @@ mkfs(pp, fsys, fi, fo)
#endif
if (fssize * sectorsize > (memleft - 131072))
fssize = (memleft - 131072) / sectorsize;
if ((membase = malloc(fssize * sectorsize)) == NULL) {
perror("malloc");
exit(13);
}
if ((membase = malloc(fssize * sectorsize)) == NULL)
errx(13, "malloc failed");
}
}
fsi = fi;
@ -610,10 +632,8 @@ mkfs(pp, fsys, fi, fo)
for (sblock.fs_csshift = 0; i > 1; i >>= 1)
sblock.fs_csshift++;
fscs = (struct csum *)calloc(1, sblock.fs_cssize);
if (fscs == NULL) {
perror("calloc");
exit(31);
}
if (fscs == NULL)
errx(31, "calloc failed");
sblock.fs_magic = FS_MAGIC;
sblock.fs_rotdelay = rotdelay;
sblock.fs_minfree = minfree;
@ -718,13 +738,17 @@ mkfs(pp, fsys, fi, fo)
/*
* Initialize a cylinder group.
*/
void
initcg(cylno, utime)
int cylno;
time_t utime;
{
daddr_t cbase, d, dlower, dupper, dmax, blkno;
long i, j, s;
long i;
register struct csum *cs;
#ifdef FSIRAND
long j;
#endif
/*
* Determine block bounds for cylinder group.
@ -807,7 +831,7 @@ initcg(cylno, utime)
sblock.fs_dsize += dlower;
}
sblock.fs_dsize += acg.cg_ndblk - dupper;
if (i = dupper % sblock.fs_frag) {
if ((i = dupper % sblock.fs_frag)) {
acg.cg_frsum[sblock.fs_frag - i]++;
for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
setbit(cg_blksfree(&acg), dupper);
@ -914,10 +938,13 @@ struct odirect olost_found_dir[] = {
#endif
char buf[MAXBSIZE];
void
fsinit(utime)
time_t utime;
{
#ifdef LOSTDIR
int i;
#endif
/*
* initialize the node
@ -970,6 +997,7 @@ fsinit(utime)
* construct a set of directory entries in "buf".
* return size of directory.
*/
int
makedir(protodir, entries)
register struct direct *protodir;
int entries;
@ -1084,6 +1112,7 @@ calcipg(cpg, bpcg, usedbp)
/*
* Allocate an inode on the disk
*/
void
iput(ip, ino)
register struct dinode *ip;
register ino_t ino;
@ -1113,9 +1142,9 @@ iput(ip, ino)
exit(32);
}
d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
rdfs(d, sblock.fs_bsize, buf);
rdfs(d, sblock.fs_bsize, (char *)buf);
buf[ino_to_fsbo(&sblock, ino)] = *ip;
wtfs(d, sblock.fs_bsize, buf);
wtfs(d, sblock.fs_bsize, (char *)buf);
}
/*
@ -1146,10 +1175,10 @@ malloc(size)
i = (char *)((u_long)(base + pgsz) &~ pgsz);
base = sbrk(i - base);
if (getrlimit(RLIMIT_DATA, &rlp) < 0)
perror("getrlimit");
warn("getrlimit");
rlp.rlim_cur = rlp.rlim_max;
if (setrlimit(RLIMIT_DATA, &rlp) < 0)
perror("setrlimit");
warn("setrlimit");
memleft = rlp.rlim_max - (u_long)base;
}
size = (size + pgsz) &~ pgsz;
@ -1188,7 +1217,8 @@ calloc(size, numelm)
caddr_t base;
size *= numelm;
base = malloc(size);
if ((base = malloc(size)) == NULL);
return (NULL);
memset(base, 0, size);
return (base);
}
@ -1196,6 +1226,7 @@ calloc(size, numelm)
/*
* Replace libc function with one suited to our needs.
*/
void
free(ptr)
char *ptr;
{
@ -1205,15 +1236,16 @@ free(ptr)
#else /* !STANDALONE */
void
raise_data_limit()
{
struct rlimit rlp;
if (getrlimit(RLIMIT_DATA, &rlp) < 0)
perror("getrlimit");
warn("getrlimit");
rlp.rlim_cur = rlp.rlim_max;
if (setrlimit(RLIMIT_DATA, &rlp) < 0)
perror("setrlimit");
warn("setrlimit");
}
#ifdef __ELF__
@ -1223,6 +1255,7 @@ extern char *_etext;
extern char *etext;
#endif
void
get_memleft()
{
static u_long pgsz;
@ -1235,7 +1268,7 @@ get_memleft()
dstart = ((u_long)&etext) &~ pgsz;
freestart = ((u_long)(sbrk(0) + pgsz) &~ pgsz);
if (getrlimit(RLIMIT_DATA, &rlp) < 0)
perror("getrlimit");
warn("getrlimit");
memused = freestart - dstart;
memleft = rlp.rlim_cur - memused;
}
@ -1244,6 +1277,7 @@ get_memleft()
/*
* read a block from the file system
*/
void
rdfs(bno, size, bf)
daddr_t bno;
int size;
@ -1257,20 +1291,19 @@ rdfs(bno, size, bf)
}
if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) {
printf("seek error: %ld\n", (long)bno);
perror("rdfs");
exit(33);
err(33, "rdfs");
}
n = read(fsi, bf, size);
if (n != size) {
printf("read error: %ld\n", (long)bno);
perror("rdfs");
exit(34);
err(34, "rdfs");
}
}
/*
* write a block to the file system
*/
void
wtfs(bno, size, bf)
daddr_t bno;
int size;
@ -1286,20 +1319,19 @@ wtfs(bno, size, bf)
return;
if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) {
printf("seek error: %ld\n", (long)bno);
perror("wtfs");
exit(35);
err(35, "wtfs");
}
n = write(fso, bf, size);
if (n != size) {
printf("write error: %ld\n", (long)bno);
perror("wtfs");
exit(36);
err(36, "wtfs");
}
}
/*
* check if a block is available
*/
int
isblock(fs, cp, h)
struct fs *fs;
unsigned char *cp;
@ -1332,6 +1364,7 @@ isblock(fs, cp, h)
/*
* take a block out of the map
*/
void
clrblock(fs, cp, h)
struct fs *fs;
unsigned char *cp;
@ -1363,6 +1396,7 @@ clrblock(fs, cp, h)
/*
* put a block into the map
*/
void
setblock(fs, cp, h)
struct fs *fs;
unsigned char *cp;
@ -1402,7 +1436,6 @@ charsperline()
int columns;
char *cp;
struct winsize ws;
extern char *getenv();
columns = 0;
if (ioctl(0, TIOCGWINSZ, &ws) != -1)

View File

@ -30,6 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)newfs.8 8.6 (Berkeley) 5/3/95
.\" $Id$
.\"
.Dd May 3, 1995
.Dt NEWFS 8
@ -84,7 +85,7 @@ replaces the more obtuse
.Xr mkfs 8
program.
Before running
.Nm newfs
.Nm
or
.Nm mount_mfs ,
the disk must be labeled using
@ -92,7 +93,7 @@ the disk must be labeled using
.Nm Newfs
builds a file system on the specified special device.
Typically the defaults are reasonable, however
.Nm newfs
.Nm
has numerous options to allow the defaults to be selectively overridden.
.Pp
.Nm Mount_mfs
@ -110,7 +111,7 @@ corresponding file system.
The parameters to
.Nm mount_mfs
are the same as those to
.Nm newfs .
.Nm Ns .
If the
.Fl T
flag is specified (see below), the special file is unused.
@ -121,33 +122,35 @@ since that is where the file system will be backed up when
free memory gets low and the memory supporting
the file system has to be paged.
.Pp
The following options define the general layout policies.
.Bl -tag -width Fl
The following options define the general layout policies:
.Bl -tag -width indent
.It Fl T Ar disktype
For backward compatibility and for
.Nm mount_mfs .
.It Fl F Ar file
.Nm mount_mfs
.Nm Mount_mfs
will use this file for the image of the filesystem. When
.Nm mount_mfs
exits, this file will be left behind.
.It Fl N
Causes the file system parameters to be printed out
Cause the file system parameters to be printed out
without really creating the file system.
.It Fl O
Creates a 4.3BSD format filesystem.
Create a
.Bx 4.3
format filesystem.
This options is primarily used to build root filesystems
that can be understood by older boot ROMs.
.It Fl T
Uses information for the specified disk from
Use information for the specified disk from
.Pa /etc/disktab
instead of trying to get the information from a disklabel.
.It Fl a Ar maxcontig
This specifies the maximum number of contiguous blocks that will be
Specify the maximum number of contiguous blocks that will be
laid out before forcing a rotational delay (see the
.Fl d
option).
The default value is one.
The default value is 1.
See
.Xr tunefs 8
for more details on how to set this option.
@ -157,7 +160,7 @@ The block size of the file system, in bytes.
The number of cylinders per cylinder group in a file system.
The default value is 16.
.It Fl d Ar rotdelay
This specifies the expected time (in milliseconds) to service a transfer
Specify the expected time (in milliseconds) to service a transfer
completion interrupt and initiate a new transfer on the same disk.
The default is 0 milliseconds.
See
@ -165,7 +168,7 @@ See
for more details on how to set this option.
.ne 1i
.It Fl e Ar maxbpg
This indicates the maximum number of blocks any single file can
Indicate the maximum number of blocks any single file can
allocate out of a cylinder group before it is forced to begin
allocating blocks from another cylinder group.
The default is about one quarter of the total blocks in a cylinder group.
@ -176,7 +179,7 @@ for more details on how to set this option.
The fragment size of the file system in bytes.
The default is 1024 bytes.
.It Fl i Ar number of bytes per inode
This specifies the density of inodes in the file system.
Specify the density of inodes in the file system.
The default is to create an inode for every (4 * frag-size) bytes of data space.
If fewer inodes are desired, a larger number should be used;
to create more inodes a smaller number should be given.
@ -193,8 +196,9 @@ See
.Xr tunefs 8
for more details on how to set this option.
.It Fl n Ar number of distinguished rotational positions
Determines how many rotational time slots there are in one revolution of
the disk. Defaults to 1, which essentially disables the rotational position table.
Determine how many rotational time slots there are in one revolution of
the disk. Defaults to 1, which essentially disables the rotational position
table.
.It Fl o Ar optimization\ preference
.Pq ``space'' or ``time''
The file system can either be instructed to try to minimize the time spent
@ -213,15 +217,15 @@ The size of the file system in sectors.
The following options override the standard sizes for the disk geometry.
Their default values are taken from the disk label.
Changing these defaults is useful only when using
.Nm newfs
.Nm
to build a file system whose raw image will eventually be used on a
different type of disk than the one on which it is initially created
(for example on a write-once disk).
Note that changing any of these values from their defaults will make
it impossible for
.Xr fsck
.Xr fsck 8
to find the alternate superblocks if the standard superblock is lost.
.Bl -tag -width Fl
.Bl -tag -width indent
.It Fl S Ar sector-size
The size of a sector in bytes (almost never anything but 512).
.It Fl k Ar sector \&0 skew , per track
@ -259,7 +263,7 @@ If zero is specified, the value from the disklabel will be used.
This does not include sectors reserved at the end of each track for bad
block replacement (see the
.Fl p
option.)
option).
.It Fl x Ar spare sectors per cylinder
Spare sectors (bad sector replacements) are physical sectors that occupy
space at the end of the last track in the cylinder.
@ -272,7 +276,7 @@ system for data allocation.
The options to the
.Nm mount_mfs
command are as described for the
.Nm newfs
.Nm
command, except for the
.Fl o
option.
@ -295,7 +299,9 @@ Mount a 64 MB large memory file system on /tmp, with
.Xr mount 8
options nosuid and nodev.
.Sh BUGS
The boot code of FreeBSD assumes that the file system that carries the
The boot code of
.Bx Free
assumes that the file system that carries the
kernel has blocks of 8 kilobytes and fragments of 1 kilobyte. You will
not be able to boot from a file system that uses another size.
.Sh SEE ALSO

View File

@ -32,13 +32,17 @@
*/
#ifndef lint
static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
static const char copyright[] =
"@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#if 0
static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
#endif
static const char rcsid[] =
"$Id$";
#endif /* not lint */
/*
@ -46,17 +50,15 @@ static char copyright[] =
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/disklabel.h>
#include <sys/file.h>
#include <sys/mount.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#include <ufs/ufs/ufsmount.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <paths.h>
#include <stdio.h>
@ -65,10 +67,13 @@ static char copyright[] =
#include <syslog.h>
#include <unistd.h>
#ifdef MFS
#include <sys/types.h>
#include <sys/mman.h>
#endif
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include "mntopts.h"
@ -201,13 +206,14 @@ int unlabeled;
char device[MAXPATHLEN];
char *progname;
extern void mkfs __P((struct partition *, char *, int, int));
static void usage __P((void));
int
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
register int ch;
register struct partition *pp;
register struct disklabel *lp;
@ -217,13 +223,14 @@ main(argc, argv)
struct stat st;
struct statfs *mp;
int fsi, fso, len, n;
char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
char *cp, *s1, *s2, *special, *opstring;
#ifdef MFS
struct vfsconf vfc;
int error;
char buf[BUFSIZ];
#endif
if (progname = strrchr(*argv, '/'))
if ((progname = strrchr(*argv, '/')))
++progname;
else
progname = *argv;
@ -258,7 +265,7 @@ main(argc, argv)
break;
case 'a':
if ((maxcontig = atoi(optarg)) <= 0)
fatal("%s: bad maximum contiguous blocks\n",
fatal("%s: bad maximum contiguous blocks",
optarg);
break;
case 'b':
@ -272,11 +279,11 @@ main(argc, argv)
break;
case 'd':
if ((rotdelay = atoi(optarg)) < 0)
fatal("%s: bad rotational delay\n", optarg);
fatal("%s: bad rotational delay", optarg);
break;
case 'e':
if ((maxbpg = atoi(optarg)) <= 0)
fatal("%s: bad blocks per file in a cylinder group\n",
fatal("%s: bad blocks per file in a cylinder group",
optarg);
break;
case 'f':
@ -285,7 +292,7 @@ main(argc, argv)
break;
case 'i':
if ((density = atoi(optarg)) <= 0)
fatal("%s: bad bytes per inode\n", optarg);
fatal("%s: bad bytes per inode", optarg);
break;
case 'k':
if ((trackskew = atoi(optarg)) < 0)
@ -297,11 +304,11 @@ main(argc, argv)
break;
case 'm':
if ((minfree = atoi(optarg)) < 0 || minfree > 99)
fatal("%s: bad free space %%\n", optarg);
fatal("%s: bad free space %%", optarg);
break;
case 'n':
if ((nrpos = atoi(optarg)) < 0)
fatal("%s: bad rotational layout count\n",
fatal("%s: bad rotational layout count",
optarg);
if (nrpos == 0)
nrpos = 1;
@ -315,7 +322,7 @@ main(argc, argv)
else if (strcmp(optarg, "time") == 0)
opt = FS_OPTTIME;
else
fatal("%s: unknown optimization preference: use `space' or `time'.");
fatal("%s: unknown optimization preference: use `space' or `time'");
}
break;
case 'p':
@ -325,7 +332,7 @@ main(argc, argv)
break;
case 'r':
if ((rpm = atoi(optarg)) <= 0)
fatal("%s: bad revolutions/minute\n", optarg);
fatal("%s: bad revolutions/minute", optarg);
break;
case 's':
if ((fssize = atoi(optarg)) <= 0)
@ -441,7 +448,7 @@ main(argc, argv)
progname, special);
cp = strchr(argv[0], '\0') - 1;
if (cp == (char *)-1 ||
(*cp < 'a' || *cp > 'h') && !isdigit(*cp))
((*cp < 'a' || *cp > 'h') && !isdigit(*cp)))
fatal("%s: can't figure out file system partition",
argv[0]);
#ifdef COMPAT
@ -681,6 +688,7 @@ fatal(fmt, va_alist)
/*NOTREACHED*/
}
static void
usage()
{
if (mfs) {