newfs.c (204615) | newfs.c (204654) |
---|---|
1/* 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by Marshall 6 * Kirk McKusick and Network Associates Laboratories, the Security 7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS --- 34 unchanged lines hidden (view full) --- 43 The Regents of the University of California. All rights reserved.\n"; 44#endif /* not lint */ 45 46#ifndef lint 47static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95"; 48#endif /* not lint */ 49#endif 50#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by Marshall 6 * Kirk McKusick and Network Associates Laboratories, the Security 7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS --- 34 unchanged lines hidden (view full) --- 43 The Regents of the University of California. All rights reserved.\n"; 44#endif /* not lint */ 45 46#ifndef lint 47static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95"; 48#endif /* not lint */ 49#endif 50#include <sys/cdefs.h> |
51__FBSDID("$FreeBSD: head/sbin/newfs/newfs.c 204615 2010-03-03 02:05:09Z sobomax $"); | 51__FBSDID("$FreeBSD: head/sbin/newfs/newfs.c 204654 2010-03-03 19:25:28Z sobomax $"); |
52 53/* 54 * newfs: friendly front end to mkfs 55 */ 56#include <sys/param.h> 57#include <sys/stat.h> 58#include <sys/disk.h> 59#include <sys/disklabel.h> --- 12 unchanged lines hidden (view full) --- 72#include <paths.h> 73#include <stdarg.h> 74#include <stdio.h> 75#include <stdlib.h> 76#include <string.h> 77#include <syslog.h> 78#include <unistd.h> 79 | 52 53/* 54 * newfs: friendly front end to mkfs 55 */ 56#include <sys/param.h> 57#include <sys/stat.h> 58#include <sys/disk.h> 59#include <sys/disklabel.h> --- 12 unchanged lines hidden (view full) --- 72#include <paths.h> 73#include <stdarg.h> 74#include <stdio.h> 75#include <stdlib.h> 76#include <string.h> 77#include <syslog.h> 78#include <unistd.h> 79 |
80#include <libutil.h> 81 |
|
80#include "newfs.h" 81 82int Eflag; /* Erase previous disk contents */ 83int Lflag; /* add a volume label */ 84int Nflag; /* run without writing file system */ 85int Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 86int Rflag; /* regression test */ 87int Uflag; /* enable soft updates for file system */ 88int Xflag = 0; /* exit in middle of newfs for testing */ 89int Jflag; /* enable gjournal for file system */ 90int lflag; /* enable multilabel for file system */ 91int nflag; /* do not create .snap directory */ 92intmax_t fssize; /* file system size */ | 82#include "newfs.h" 83 84int Eflag; /* Erase previous disk contents */ 85int Lflag; /* add a volume label */ 86int Nflag; /* run without writing file system */ 87int Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 88int Rflag; /* regression test */ 89int Uflag; /* enable soft updates for file system */ 90int Xflag = 0; /* exit in middle of newfs for testing */ 91int Jflag; /* enable gjournal for file system */ 92int lflag; /* enable multilabel for file system */ 93int nflag; /* do not create .snap directory */ 94intmax_t fssize; /* file system size */ |
93int sectorsize; /* bytes/sector */ | 95int64_t sectorsize; /* bytes/sector */ |
94int realsectorsize; /* bytes/sector in hardware */ | 96int realsectorsize; /* bytes/sector in hardware */ |
95int fsize = 0; /* fragment size */ 96int bsize = 0; /* block size */ 97int maxbsize = 0; /* maximum clustering */ 98int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ | 97int64_t fsize = 0; /* fragment size */ 98int64_t bsize = 0; /* block size */ 99int64_t maxbsize = 0; /* maximum clustering */ 100int64_t maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ |
99int minfree = MINFREE; /* free space threshold */ 100int opt = DEFAULTOPT; /* optimization preference (space or time) */ | 101int minfree = MINFREE; /* free space threshold */ 102int opt = DEFAULTOPT; /* optimization preference (space or time) */ |
101int density; /* number of bytes per inode */ 102int maxcontig = 0; /* max contiguous blocks to allocate */ 103int maxbpg; /* maximum blocks per file in a cyl group */ 104int avgfilesize = AVFILESIZ;/* expected average file size */ 105int avgfilesperdir = AFPDIR;/* expected number of files per directory */ | 103int64_t density; /* number of bytes per inode */ 104int64_t maxcontig = 0; /* max contiguous blocks to allocate */ 105int64_t maxbpg; /* maximum blocks per file in a cyl group */ 106int64_t avgfilesize = AVFILESIZ;/* expected average file size */ 107int64_t avgfilesperdir = AFPDIR;/* expected number of files per directory */ |
106u_char *volumelabel = NULL; /* volume label for filesystem */ 107struct uufsd disk; /* libufs disk structure */ 108 109static char device[MAXPATHLEN]; 110static u_char bootarea[BBSIZE]; 111static int is_file; /* work on a file, not a device */ 112static char *dkname; 113static char *disktype; 114static int unlabeled; 115 116static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t); 117static struct disklabel *getdisklabel(char *s); 118static void rewritelabel(char *s, struct disklabel *lp); 119static void usage(void); | 108u_char *volumelabel = NULL; /* volume label for filesystem */ 109struct uufsd disk; /* libufs disk structure */ 110 111static char device[MAXPATHLEN]; 112static u_char bootarea[BBSIZE]; 113static int is_file; /* work on a file, not a device */ 114static char *dkname; 115static char *disktype; 116static int unlabeled; 117 118static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t); 119static struct disklabel *getdisklabel(char *s); 120static void rewritelabel(char *s, struct disklabel *lp); 121static void usage(void); |
120static int parselength(const char *ls, int *sz); | |
121 122ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */ 123 124int 125main(int argc, char *argv[]) 126{ 127 struct partition *pp; 128 struct disklabel *lp; --- 36 unchanged lines hidden (view full) --- 165 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 166 errx(1, "%s: bad file system format value", 167 optarg); 168 break; 169 case 'R': 170 Rflag = 1; 171 break; 172 case 'S': | 122 123ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */ 124 125int 126main(int argc, char *argv[]) 127{ 128 struct partition *pp; 129 struct disklabel *lp; --- 36 unchanged lines hidden (view full) --- 166 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 167 errx(1, "%s: bad file system format value", 168 optarg); 169 break; 170 case 'R': 171 Rflag = 1; 172 break; 173 case 'S': |
173 rval = parselength(optarg, §orsize); | 174 rval = expand_number(optarg, §orsize); |
174 if (rval < 0 || sectorsize <= 0) 175 errx(1, "%s: bad sector size", optarg); 176 break; 177 case 'T': 178 disktype = optarg; 179 break; 180 case 'U': 181 Uflag = 1; 182 break; 183 case 'X': 184 Xflag++; 185 break; 186 case 'a': | 175 if (rval < 0 || sectorsize <= 0) 176 errx(1, "%s: bad sector size", optarg); 177 break; 178 case 'T': 179 disktype = optarg; 180 break; 181 case 'U': 182 Uflag = 1; 183 break; 184 case 'X': 185 Xflag++; 186 break; 187 case 'a': |
187 rval = parselength(optarg, &maxcontig); | 188 rval = expand_number(optarg, &maxcontig); |
188 if (rval < 0 || maxcontig <= 0) 189 errx(1, "%s: bad maximum contiguous blocks", 190 optarg); 191 break; 192 case 'b': | 189 if (rval < 0 || maxcontig <= 0) 190 errx(1, "%s: bad maximum contiguous blocks", 191 optarg); 192 break; 193 case 'b': |
193 rval = parselength(optarg, &bsize); | 194 rval = expand_number(optarg, &bsize); |
194 if (rval < 0) 195 errx(1, "%s: bad block size", 196 optarg); 197 if (bsize < MINBSIZE) 198 errx(1, "%s: block size too small, min is %d", 199 optarg, MINBSIZE); 200 if (bsize > MAXBSIZE) 201 errx(1, "%s: block size too large, max is %d", 202 optarg, MAXBSIZE); 203 break; 204 case 'c': | 195 if (rval < 0) 196 errx(1, "%s: bad block size", 197 optarg); 198 if (bsize < MINBSIZE) 199 errx(1, "%s: block size too small, min is %d", 200 optarg, MINBSIZE); 201 if (bsize > MAXBSIZE) 202 errx(1, "%s: block size too large, max is %d", 203 optarg, MAXBSIZE); 204 break; 205 case 'c': |
205 rval = parselength(optarg, &maxblkspercg); | 206 rval = expand_number(optarg, &maxblkspercg); |
206 if (rval < 0 || maxblkspercg <= 0) 207 errx(1, "%s: bad blocks per cylinder group", 208 optarg); 209 break; 210 case 'd': | 207 if (rval < 0 || maxblkspercg <= 0) 208 errx(1, "%s: bad blocks per cylinder group", 209 optarg); 210 break; 211 case 'd': |
211 rval = parselength(optarg, &maxbsize); | 212 rval = expand_number(optarg, &maxbsize); |
212 if (rval < 0 || maxbsize < MINBSIZE) 213 errx(1, "%s: bad extent block size", optarg); 214 break; 215 case 'e': | 213 if (rval < 0 || maxbsize < MINBSIZE) 214 errx(1, "%s: bad extent block size", optarg); 215 break; 216 case 'e': |
216 rval = parselength(optarg, &maxbpg); | 217 rval = expand_number(optarg, &maxbpg); |
217 if (rval < 0 || maxbpg <= 0) 218 errx(1, "%s: bad blocks per file in a cylinder group", 219 optarg); 220 break; 221 case 'f': | 218 if (rval < 0 || maxbpg <= 0) 219 errx(1, "%s: bad blocks per file in a cylinder group", 220 optarg); 221 break; 222 case 'f': |
222 rval = parselength(optarg, &fsize); | 223 rval = expand_number(optarg, &fsize); |
223 if (rval < 0 || fsize <= 0) 224 errx(1, "%s: bad fragment size", optarg); 225 break; 226 case 'g': | 224 if (rval < 0 || fsize <= 0) 225 errx(1, "%s: bad fragment size", optarg); 226 break; 227 case 'g': |
227 rval = parselength(optarg, &avgfilesize); | 228 rval = expand_number(optarg, &avgfilesize); |
228 if (rval < 0 || avgfilesize <= 0) 229 errx(1, "%s: bad average file size", optarg); 230 break; 231 case 'h': | 229 if (rval < 0 || avgfilesize <= 0) 230 errx(1, "%s: bad average file size", optarg); 231 break; 232 case 'h': |
232 rval = parselength(optarg, &avgfilesperdir); | 233 rval = expand_number(optarg, &avgfilesperdir); |
233 if (rval < 0 || avgfilesperdir <= 0) 234 errx(1, "%s: bad average files per dir", optarg); 235 break; 236 case 'i': | 234 if (rval < 0 || avgfilesperdir <= 0) 235 errx(1, "%s: bad average files per dir", optarg); 236 break; 237 case 'i': |
237 rval = parselength(optarg, &density); | 238 rval = expand_number(optarg, &density); |
238 if (rval < 0 || density <= 0) 239 errx(1, "%s: bad bytes per inode", optarg); 240 break; 241 case 'l': 242 lflag = 1; 243 break; 244 case 'm': 245 if ((minfree = atoi(optarg)) < 0 || minfree > 99) --- 244 unchanged lines hidden (view full) --- 490 fprintf(stderr, "\t-n do not create .snap directory\n"); 491 fprintf(stderr, "\t-m minimum free space %%\n"); 492 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 493 fprintf(stderr, "\t-p partition name (a..h)\n"); 494 fprintf(stderr, "\t-r reserved sectors at the end of device\n"); 495 fprintf(stderr, "\t-s file system size (sectors)\n"); 496 exit(1); 497} | 239 if (rval < 0 || density <= 0) 240 errx(1, "%s: bad bytes per inode", optarg); 241 break; 242 case 'l': 243 lflag = 1; 244 break; 245 case 'm': 246 if ((minfree = atoi(optarg)) < 0 || minfree > 99) --- 244 unchanged lines hidden (view full) --- 491 fprintf(stderr, "\t-n do not create .snap directory\n"); 492 fprintf(stderr, "\t-m minimum free space %%\n"); 493 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 494 fprintf(stderr, "\t-p partition name (a..h)\n"); 495 fprintf(stderr, "\t-r reserved sectors at the end of device\n"); 496 fprintf(stderr, "\t-s file system size (sectors)\n"); 497 exit(1); 498} |
498 499/* 500 * Return the numeric value of a string given in the form [+-][0-9]+[GMKT] 501 * or -1 on format error or overflow. 502 */ 503static int 504parselength(const char *ls, int *sz) 505{ 506 off_t length, oflow; 507 int lsign; 508 509 length = 0; 510 lsign = 1; 511 512 switch (*ls) { 513 case '-': 514 lsign = -1; 515 case '+': 516 ls++; 517 } 518 519#define ASSIGN_CHK_OFLOW(x, y) if (x < y) return -1; y = x 520 /* 521 * Calculate the value of the decimal digit string, failing 522 * on overflow. 523 */ 524 while (isdigit(*ls)) { 525 oflow = length * 10 + *ls++ - '0'; 526 ASSIGN_CHK_OFLOW(oflow, length); 527 } 528 529 switch (*ls) { 530 case 'T': 531 case 't': 532 oflow = length * 1024; 533 ASSIGN_CHK_OFLOW(oflow, length); 534 case 'G': 535 case 'g': 536 oflow = length * 1024; 537 ASSIGN_CHK_OFLOW(oflow, length); 538 case 'M': 539 case 'm': 540 oflow = length * 1024; 541 ASSIGN_CHK_OFLOW(oflow, length); 542 case 'K': 543 case 'k': 544 if (ls[1] != '\0') 545 return -1; 546 oflow = length * 1024; 547 ASSIGN_CHK_OFLOW(oflow, length); 548 case '\0': 549 break; 550 default: 551 return -1; 552 } 553 554 *sz = length * lsign; 555 return 0; 556} | |