newfs.c revision 140603
11558Srgrimes/* 298542Smckusick * Copyright (c) 2002 Networks Associates Technology, Inc. 398542Smckusick * All rights reserved. 498542Smckusick * 598542Smckusick * This software was developed for the FreeBSD Project by Marshall 698542Smckusick * Kirk McKusick and Network Associates Laboratories, the Security 798542Smckusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR 898542Smckusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 9110884Smckusick * research program. 1098542Smckusick * 111558Srgrimes * Copyright (c) 1983, 1989, 1993, 1994 121558Srgrimes * The Regents of the University of California. All rights reserved. 131558Srgrimes * 141558Srgrimes * Redistribution and use in source and binary forms, with or without 151558Srgrimes * modification, are permitted provided that the following conditions 161558Srgrimes * are met: 171558Srgrimes * 1. Redistributions of source code must retain the above copyright 181558Srgrimes * notice, this list of conditions and the following disclaimer. 191558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 201558Srgrimes * notice, this list of conditions and the following disclaimer in the 211558Srgrimes * documentation and/or other materials provided with the distribution. 221558Srgrimes * 4. Neither the name of the University nor the names of its contributors 231558Srgrimes * may be used to endorse or promote products derived from this software 241558Srgrimes * without specific prior written permission. 251558Srgrimes * 261558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361558Srgrimes * SUCH DAMAGE. 371558Srgrimes */ 381558Srgrimes 39114589Sobrien#if 0 401558Srgrimes#ifndef lint 4137664Scharnierstatic const char copyright[] = 4237664Scharnier"@(#) Copyright (c) 1983, 1989, 1993, 1994\n\ 4337664Scharnier The Regents of the University of California. All rights reserved.\n"; 441558Srgrimes#endif /* not lint */ 451558Srgrimes 461558Srgrimes#ifndef lint 4737664Scharnierstatic char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95"; 48114589Sobrien#endif /* not lint */ 4937664Scharnier#endif 50114589Sobrien#include <sys/cdefs.h> 51114589Sobrien__FBSDID("$FreeBSD: head/sbin/newfs/newfs.c 140603 2005-01-21 22:20:25Z wes $"); 521558Srgrimes 531558Srgrimes/* 541558Srgrimes * newfs: friendly front end to mkfs 551558Srgrimes */ 561558Srgrimes#include <sys/param.h> 571558Srgrimes#include <sys/stat.h> 5895357Sphk#include <sys/disk.h> 591558Srgrimes#include <sys/disklabel.h> 601558Srgrimes#include <sys/file.h> 611558Srgrimes#include <sys/mount.h> 621558Srgrimes 6337707Scharnier#include <ufs/ufs/dir.h> 6437707Scharnier#include <ufs/ufs/dinode.h> 651558Srgrimes#include <ufs/ffs/fs.h> 6623682Speter#include <ufs/ufs/ufsmount.h> 671558Srgrimes 681558Srgrimes#include <ctype.h> 6937664Scharnier#include <err.h> 701558Srgrimes#include <errno.h> 71135460Spjd#include <inttypes.h> 721558Srgrimes#include <paths.h> 7393777Sbde#include <stdarg.h> 741558Srgrimes#include <stdio.h> 751558Srgrimes#include <stdlib.h> 761558Srgrimes#include <string.h> 771558Srgrimes#include <syslog.h> 781558Srgrimes#include <unistd.h> 791558Srgrimes 8092717Sphk#include "newfs.h" 811558Srgrimes 821558Srgrimes/* 831558Srgrimes * The following two constants set the default block and fragment sizes. 841558Srgrimes * Both constants must be a power of 2 and meet the following constraints: 851558Srgrimes * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 861558Srgrimes * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 871558Srgrimes * DESBLKSIZE / DESFRAGSIZE <= 8 881558Srgrimes */ 8987661Ssheldonh#define DFL_FRAGSIZE 2048 9087661Ssheldonh#define DFL_BLKSIZE 16384 911558Srgrimes 921558Srgrimes/* 9398542Smckusick * Cylinder groups may have up to MAXBLKSPERCG blocks. The actual 941558Srgrimes * number used depends upon how much information can be stored 95102231Strhodes * in a cylinder group map which must fit in a single file system 9698542Smckusick * block. The default is to use as many as possible blocks per group. 971558Srgrimes */ 9898542Smckusick#define MAXBLKSPERCG 0x7fffffff /* desired fs_fpg ("infinity") */ 991558Srgrimes 1001558Srgrimes/* 1011558Srgrimes * MAXBLKPG determines the maximum number of data blocks which are 1021558Srgrimes * placed in a single cylinder group. The default is one indirect 1031558Srgrimes * block worth of data blocks. 1041558Srgrimes */ 10598542Smckusick#define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t)) 1061558Srgrimes 1071558Srgrimes/* 108102231Strhodes * Each file system has a number of inodes statically allocated. 1091558Srgrimes * We allocate one inode slot per NFPI fragments, expecting this 1101558Srgrimes * to be far more than we will ever need. 1111558Srgrimes */ 1121558Srgrimes#define NFPI 4 1131558Srgrimes 114110174Sgordonint Lflag; /* add a volume label */ 115102231Strhodesint Nflag; /* run without writing file system */ 116113751Srwatsonint Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 11792722Sphkint Rflag; /* regression test */ 118102231Strhodesint Uflag; /* enable soft updates for file system */ 119122785Swesint Eflag = 0; /* exit in middle of newfs for testing */ 120126254Srwatsonint lflag; /* enable multilabel for file system */ 121140603Swesint nflag; /* do not create .snap directory */ 12298542Smckusickquad_t fssize; /* file system size */ 12398542Smckusickint sectorsize; /* bytes/sector */ 1241558Srgrimesint realsectorsize; /* bytes/sector in hardware */ 1251558Srgrimesint fsize = 0; /* fragment size */ 1261558Srgrimesint bsize = 0; /* block size */ 12798542Smckusickint maxbsize = 0; /* maximum clustering */ 12898542Smckusickint maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ 1291558Srgrimesint minfree = MINFREE; /* free space threshold */ 1301558Srgrimesint opt = DEFAULTOPT; /* optimization preference (space or time) */ 1311558Srgrimesint density; /* number of bytes per inode */ 1321558Srgrimesint maxcontig = 0; /* max contiguous blocks to allocate */ 1331558Srgrimesint maxbpg; /* maximum blocks per file in a cyl group */ 13475377Smckusickint avgfilesize = AVFILESIZ;/* expected average file size */ 13575377Smckusickint avgfilesperdir = AFPDIR;/* expected number of files per directory */ 136110174Sgordonu_char *volumelabel = NULL; /* volume label for filesystem */ 137110671Sjmallettstruct uufsd disk; /* libufs disk structure */ 1381558Srgrimes 13992763Sphkstatic char device[MAXPATHLEN]; 14093777Sbdestatic char *disktype; 14193777Sbdestatic int unlabeled; 1421558Srgrimes 14395357Sphkstatic struct disklabel *getdisklabel(char *s); 14495357Sphkstatic void rewritelabel(char *s, struct disklabel *lp); 14593777Sbdestatic void usage(void); 14637664Scharnier 1471558Srgrimesint 14892711Siedowsemain(int argc, char *argv[]) 1491558Srgrimes{ 15092483Sphk struct partition *pp; 15192483Sphk struct disklabel *lp; 1521558Srgrimes struct partition oldpartition; 1531558Srgrimes struct stat st; 15495357Sphk char *cp, *special; 155110174Sgordon int ch, i; 15695357Sphk off_t mediasize; 1571558Srgrimes 15892717Sphk while ((ch = getopt(argc, argv, 159140603Swes "EL:NO:RS:T:Ua:b:c:d:e:f:g:h:i:lm:no:s:")) != -1) 1601558Srgrimes switch (ch) { 161122785Swes case 'E': 162122785Swes Eflag++; 163122785Swes break; 164110174Sgordon case 'L': 165110174Sgordon volumelabel = optarg; 166110174Sgordon i = -1; 167110174Sgordon while (isalnum(volumelabel[++i])); 168110174Sgordon if (volumelabel[i] != '\0') { 169110174Sgordon errx(1, "bad volume label. Valid characters are alphanumerics."); 170110174Sgordon } 171110174Sgordon if (strlen(volumelabel) >= MAXVOLLEN) { 172110174Sgordon errx(1, "bad volume label. Length is longer than %d.", 173110174Sgordon MAXVOLLEN); 174110174Sgordon } 175110174Sgordon Lflag = 1; 176110174Sgordon break; 1771558Srgrimes case 'N': 1781558Srgrimes Nflag = 1; 1791558Srgrimes break; 18098542Smckusick case 'O': 18198542Smckusick if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 182102231Strhodes errx(1, "%s: bad file system format value", 18398542Smckusick optarg); 18498542Smckusick break; 18592722Sphk case 'R': 18692722Sphk Rflag = 1; 18792722Sphk break; 1881558Srgrimes case 'S': 1891558Srgrimes if ((sectorsize = atoi(optarg)) <= 0) 19095357Sphk errx(1, "%s: bad sector size", optarg); 1911558Srgrimes break; 1921558Srgrimes case 'T': 1931558Srgrimes disktype = optarg; 1941558Srgrimes break; 19575078Sobrien case 'U': 19675078Sobrien Uflag = 1; 19775078Sobrien break; 1981558Srgrimes case 'a': 1991558Srgrimes if ((maxcontig = atoi(optarg)) <= 0) 20095357Sphk errx(1, "%s: bad maximum contiguous blocks", 2011558Srgrimes optarg); 2021558Srgrimes break; 2031558Srgrimes case 'b': 2041558Srgrimes if ((bsize = atoi(optarg)) < MINBSIZE) 205107412Smckusick errx(1, "%s: block size too small, min is %d", 206107412Smckusick optarg, MINBSIZE); 207107412Smckusick if (bsize > MAXBSIZE) 208107412Smckusick errx(1, "%s: block size too large, max is %d", 209107412Smckusick optarg, MAXBSIZE); 2101558Srgrimes break; 2111558Srgrimes case 'c': 21298542Smckusick if ((maxblkspercg = atoi(optarg)) <= 0) 21398542Smckusick errx(1, "%s: bad blocks per cylinder group", 21498542Smckusick optarg); 2151558Srgrimes break; 21698542Smckusick case 'd': 21798542Smckusick if ((maxbsize = atoi(optarg)) < MINBSIZE) 21898542Smckusick errx(1, "%s: bad extent block size", optarg); 21998542Smckusick break; 2201558Srgrimes case 'e': 2211558Srgrimes if ((maxbpg = atoi(optarg)) <= 0) 22298542Smckusick errx(1, "%s: bad blocks per file in a cylinder group", 2231558Srgrimes optarg); 2241558Srgrimes break; 2251558Srgrimes case 'f': 2261558Srgrimes if ((fsize = atoi(optarg)) <= 0) 22795357Sphk errx(1, "%s: bad fragment size", optarg); 2281558Srgrimes break; 22975377Smckusick case 'g': 23075377Smckusick if ((avgfilesize = atoi(optarg)) <= 0) 23195357Sphk errx(1, "%s: bad average file size", optarg); 23275377Smckusick break; 23375377Smckusick case 'h': 23475377Smckusick if ((avgfilesperdir = atoi(optarg)) <= 0) 23598542Smckusick errx(1, "%s: bad average files per dir", optarg); 23675377Smckusick break; 2371558Srgrimes case 'i': 2381558Srgrimes if ((density = atoi(optarg)) <= 0) 23995357Sphk errx(1, "%s: bad bytes per inode", optarg); 2401558Srgrimes break; 241126254Srwatson case 'l': 242126254Srwatson lflag = 1; 243126254Srwatson break; 2441558Srgrimes case 'm': 2451558Srgrimes if ((minfree = atoi(optarg)) < 0 || minfree > 99) 24695357Sphk errx(1, "%s: bad free space %%", optarg); 2471558Srgrimes break; 248140603Swes case 'n': 249140603Swes nflag = 1; 250140603Swes break; 2511558Srgrimes case 'o': 25277420Sphk if (strcmp(optarg, "space") == 0) 25377420Sphk opt = FS_OPTSPACE; 25477420Sphk else if (strcmp(optarg, "time") == 0) 25577420Sphk opt = FS_OPTTIME; 25677420Sphk else 25795357Sphk errx(1, 25892533Sbde "%s: unknown optimization preference: use `space' or `time'", 25992533Sbde optarg); 2601558Srgrimes break; 2611558Srgrimes case 's': 262135460Spjd errno = 0; 263135460Spjd fssize = strtoimax(optarg, NULL, 0); 264135460Spjd if (errno != 0) 265135460Spjd err(1, "%s: bad file system size", optarg); 2661558Srgrimes break; 2671558Srgrimes case '?': 2681558Srgrimes default: 2691558Srgrimes usage(); 2701558Srgrimes } 2711558Srgrimes argc -= optind; 2721558Srgrimes argv += optind; 2731558Srgrimes 27495357Sphk if (argc != 1) 2751558Srgrimes usage(); 2761558Srgrimes 2771558Srgrimes special = argv[0]; 27823682Speter cp = strrchr(special, '/'); 2791558Srgrimes if (cp == 0) { 2801558Srgrimes /* 28192533Sbde * No path prefix; try prefixing _PATH_DEV. 2821558Srgrimes */ 28392533Sbde snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); 2841558Srgrimes special = device; 2851558Srgrimes } 2861558Srgrimes 287110671Sjmallett if (ufs_disk_fillout_blank(&disk, special) == -1 || 288110671Sjmallett (!Nflag && ufs_disk_write(&disk) == -1)) { 289110671Sjmallett if (disk.d_error != NULL) 290110671Sjmallett errx(1, "%s: %s", special, disk.d_error); 291110671Sjmallett else 292110671Sjmallett err(1, "%s", special); 293110671Sjmallett } 294110671Sjmallett if (fstat(disk.d_fd, &st) < 0) 29595357Sphk err(1, "%s", special); 29695357Sphk if ((st.st_mode & S_IFMT) != S_IFCHR) 29795357Sphk errx(1, "%s: not a character-special device", special); 2981558Srgrimes 299110671Sjmallett if (sectorsize == 0) 300110671Sjmallett ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize); 301110671Sjmallett if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) { 30295357Sphk if (fssize == 0) 30395357Sphk fssize = mediasize / sectorsize; 30495357Sphk else if (fssize > mediasize / sectorsize) 305135460Spjd errx(1, "%s: maximum file system size is %jd", 306139909Spjd special, (intmax_t)(mediasize / sectorsize)); 3071558Srgrimes } 30895357Sphk pp = NULL; 30995357Sphk lp = getdisklabel(special); 31095357Sphk if (lp != NULL) { 31195357Sphk cp = strchr(special, '\0'); 31295357Sphk cp--; 31395360Sphk if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 314102231Strhodes errx(1, "%s: can't figure out file system partition", 31595357Sphk special); 31695360Sphk if (isdigit(*cp)) 31795360Sphk pp = &lp->d_partitions[RAW_PART]; 31895357Sphk else 31995357Sphk pp = &lp->d_partitions[*cp - 'a']; 32095357Sphk oldpartition = *pp; 32195357Sphk if (pp->p_size == 0) 32295357Sphk errx(1, "%s: `%c' partition is unavailable", 32395357Sphk special, *cp); 32495357Sphk if (pp->p_fstype == FS_BOOT) 32595357Sphk errx(1, "%s: `%c' partition overlaps boot program", 32695357Sphk special, *cp); 32795357Sphk if (fssize == 0) 32895357Sphk fssize = pp->p_size; 32995357Sphk if (fssize > pp->p_size) 33095357Sphk errx(1, 331102231Strhodes "%s: maximum file system size %d", special, pp->p_size); 33295357Sphk if (sectorsize == 0) 33395357Sphk sectorsize = lp->d_secsize; 33495357Sphk if (fsize == 0) 33595357Sphk fsize = pp->p_fsize; 33695357Sphk if (bsize == 0) 33795357Sphk bsize = pp->p_frag * pp->p_fsize; 3381558Srgrimes } 33995357Sphk if (sectorsize <= 0) 34095357Sphk errx(1, "%s: no default sector size", special); 34195357Sphk if (fsize <= 0) 34295357Sphk fsize = MAX(DFL_FRAGSIZE, sectorsize); 34395357Sphk if (bsize <= 0) 34495357Sphk bsize = MIN(DFL_BLKSIZE, 8 * fsize); 34598542Smckusick if (maxbsize == 0) 34698542Smckusick maxbsize = bsize; 3471558Srgrimes /* 3481558Srgrimes * Maxcontig sets the default for the maximum number of blocks 349102231Strhodes * that may be allocated sequentially. With file system clustering 3501558Srgrimes * it is possible to allocate contiguous blocks up to the maximum 3511558Srgrimes * transfer size permitted by the controller or buffering. 3521558Srgrimes */ 3531558Srgrimes if (maxcontig == 0) 35498542Smckusick maxcontig = MAX(1, MAXPHYS / bsize); 3551558Srgrimes if (density == 0) 3561558Srgrimes density = NFPI * fsize; 3571558Srgrimes if (minfree < MINFREE && opt != FS_OPTSPACE) { 3581558Srgrimes fprintf(stderr, "Warning: changing optimization to space "); 3591558Srgrimes fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 3601558Srgrimes opt = FS_OPTSPACE; 3611558Srgrimes } 3621558Srgrimes if (maxbpg == 0) 3631558Srgrimes maxbpg = MAXBLKPG(bsize); 3641558Srgrimes realsectorsize = sectorsize; 3651558Srgrimes if (sectorsize != DEV_BSIZE) { /* XXX */ 36620061Ssos int secperblk = sectorsize / DEV_BSIZE; 36720061Ssos 36820061Ssos sectorsize = DEV_BSIZE; 36920061Ssos fssize *= secperblk; 370104308Sphk if (pp != NULL) 37195357Sphk pp->p_size *= secperblk; 37220061Ssos } 37395357Sphk mkfs(pp, special); 37495357Sphk if (!unlabeled) { 37595357Sphk if (realsectorsize != DEV_BSIZE) 37698542Smckusick pp->p_size /= realsectorsize / DEV_BSIZE; 37795357Sphk if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 37895357Sphk rewritelabel(special, lp); 37995357Sphk } 380110671Sjmallett ufs_disk_close(&disk); 3811558Srgrimes exit(0); 3821558Srgrimes} 3831558Srgrimes 3841558Srgrimesstruct disklabel * 38595357Sphkgetdisklabel(char *s) 3861558Srgrimes{ 3871558Srgrimes static struct disklabel lab; 38895357Sphk struct disklabel *lp; 3891558Srgrimes 390110671Sjmallett if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab)) 39195357Sphk return (&lab); 39295357Sphk unlabeled++; 39395357Sphk if (disktype) { 39495357Sphk lp = getdiskbyname(disktype); 39595357Sphk if (lp != NULL) 3961558Srgrimes return (lp); 3971558Srgrimes } 39895357Sphk return (NULL); 3991558Srgrimes} 4001558Srgrimes 40177420Sphkvoid 40295357Sphkrewritelabel(char *s, struct disklabel *lp) 40337775Sbde{ 40437775Sbde if (unlabeled) 40537775Sbde return; 40637775Sbde lp->d_checksum = 0; 40737775Sbde lp->d_checksum = dkcksum(lp); 408110671Sjmallett if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0) 409103797Sphk warn("ioctl (WDINFO): %s: can't rewrite disk label", s); 41037775Sbde} 41137775Sbde 41237664Scharnierstatic void 413110065Sjmallettusage() 4141558Srgrimes{ 41577420Sphk fprintf(stderr, 41677420Sphk "usage: %s [ -fsoptions ] special-device%s\n", 41795357Sphk getprogname(), 41877420Sphk " [device-type]"); 4191558Srgrimes fprintf(stderr, "where fsoptions are:\n"); 420110174Sgordon fprintf(stderr, "\t-L volume label to add to superblock\n"); 4211558Srgrimes fprintf(stderr, 422102231Strhodes "\t-N do not create file system, just print out parameters\n"); 423102231Strhodes fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n"); 42492722Sphk fprintf(stderr, "\t-R regression test, supress random factors\n"); 4251558Srgrimes fprintf(stderr, "\t-S sector size\n"); 4261558Srgrimes fprintf(stderr, "\t-T disktype\n"); 42775078Sobrien fprintf(stderr, "\t-U enable soft updates\n"); 4281558Srgrimes fprintf(stderr, "\t-a maximum contiguous blocks\n"); 4291558Srgrimes fprintf(stderr, "\t-b block size\n"); 43098542Smckusick fprintf(stderr, "\t-c blocks per cylinders group\n"); 43198542Smckusick fprintf(stderr, "\t-d maximum extent size\n"); 4321558Srgrimes fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 4331558Srgrimes fprintf(stderr, "\t-f frag size\n"); 43475377Smckusick fprintf(stderr, "\t-g average file size\n"); 43575377Smckusick fprintf(stderr, "\t-h average files per directory\n"); 4361558Srgrimes fprintf(stderr, "\t-i number of bytes per inode\n"); 4371558Srgrimes fprintf(stderr, "\t-m minimum free space %%\n"); 4381558Srgrimes fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 43998542Smckusick fprintf(stderr, "\t-s file systemsize (sectors)\n"); 4401558Srgrimes exit(1); 4411558Srgrimes} 442