newfs.c revision 1.3.4.2 1 /* $NetBSD: newfs.c,v 1.3.4.2 2000/10/16 22:28:37 tv Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 5/24/95";
45 #else
46 __RCSID("$NetBSD: newfs.c,v 1.3.4.2 2000/10/16 22:28:37 tv Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * newfs: friendly front end to mkfs
52 */
53 #include <sys/param.h>
54 #include <sys/ucred.h>
55 #include <sys/stat.h>
56 #include <sys/ioctl.h>
57 #include <sys/disklabel.h>
58 #include <sys/file.h>
59 #include <sys/mount.h>
60 #include <sys/sysctl.h>
61
62 #include <ufs/ufs/dir.h>
63 #include <ufs/ufs/dinode.h>
64
65 #include <disktab.h>
66 #include <errno.h>
67 #include <unistd.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <ctype.h>
71 #include <string.h>
72 #include <paths.h>
73 #include <util.h>
74 #include "config.h"
75 #include "extern.h"
76
77 #define COMPAT /* allow non-labeled disks */
78
79 int Nflag; /* run without writing file system */
80 int fssize; /* file system size */
81 int sectorsize; /* bytes/sector */
82 int fsize = 0; /* fragment size */
83 int bsize = 0; /* block size */
84 int minfree = MINFREE; /* free space threshold */
85 int minfreeseg = 0; /* free segments reserved for the cleaner */
86 int bbsize = BBSIZE; /* boot block size */
87 int sbsize = SBSIZE; /* superblock size */
88 u_long memleft; /* virtual memory available */
89 caddr_t membase; /* start address of memory based filesystem */
90 #ifdef COMPAT
91 char *disktype;
92 int unlabeled;
93 #endif
94
95 char device[MAXPATHLEN];
96 char *progname, *special;
97
98 int main __P((int, char **));
99 static struct disklabel *getdisklabel __P((char *, int));
100 static struct disklabel *debug_readlabel __P((int));
101 #ifdef notdef
102 static void rewritelabel __P((char *, int, struct disklabel *));
103 #endif
104 static void usage __P((void));
105
106 int
107 main(argc, argv)
108 int argc;
109 char *argv[];
110 {
111 int ch;
112 struct partition *pp;
113 struct disklabel *lp;
114 struct stat st;
115 int debug, force, lfs, fsi, fso, segsize, maxpartitions;
116 char *cp, *opstring;
117
118 if ((progname = strrchr(*argv, '/')) != NULL)
119 ++progname;
120 else
121 progname = *argv;
122
123 maxpartitions = getmaxpartitions();
124 if (maxpartitions > 26)
125 fatal("insane maxpartitions value %d", maxpartitions);
126
127 opstring = "B:DFLNb:f:M:m:s:";
128
129 debug = force = lfs = segsize = 0;
130 while ((ch = getopt(argc, argv, opstring)) != -1)
131 switch(ch) {
132 case 'B': /* LFS segment size */
133 if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
134 fatal("%s: bad segment size", optarg);
135 break;
136 case 'D':
137 debug = 1;
138 break;
139 case 'F':
140 force = 1;
141 break;
142 case 'L': /* Create lfs */
143 lfs = 1;
144 break;
145 case 'M':
146 minfreeseg = atoi(optarg);
147 break;
148 case 'N':
149 Nflag++;
150 break;
151 #ifdef COMPAT
152 case 'T':
153 disktype = optarg;
154 break;
155 #endif
156 case 'b': /* used for LFS */
157 if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
158 fatal("%s: bad block size", optarg);
159 break;
160 case 'f':
161 if ((fsize = atoi(optarg)) <= 0)
162 fatal("%s: bad frag size", optarg);
163 break;
164 case 'm':
165 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
166 fatal("%s: bad free space %%\n", optarg);
167 break;
168 case 's':
169 if ((fssize = atoi(optarg)) <= 0)
170 fatal("%s: bad file system size", optarg);
171 break;
172 case '?':
173 default:
174 usage();
175 }
176 argc -= optind;
177 argv += optind;
178
179 if (argc != 2 && argc != 1)
180 usage();
181
182 /*
183 * If the -N flag isn't specified, open the output file. If no path
184 * prefix, try /dev/r%s and then /dev/%s.
185 */
186 special = argv[0];
187 if (strchr(special, '/') == NULL) {
188 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
189 special);
190 if (stat(device, &st) == -1)
191 (void)snprintf(device, sizeof(device), "%s%s",
192 _PATH_DEV, special);
193 special = device;
194 }
195 if (!Nflag) {
196 fso = open(special,
197 (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
198 if (fso < 0)
199 fatal("%s: %s", special, strerror(errno));
200 } else
201 fso = -1;
202
203 /* Open the input file. */
204 fsi = open(special, O_RDONLY);
205 if (fsi < 0)
206 fatal("%s: %s", special, strerror(errno));
207 if (fstat(fsi, &st) < 0)
208 fatal("%s: %s", special, strerror(errno));
209
210 if (!debug && !S_ISCHR(st.st_mode))
211 (void)printf("%s: %s: not a character-special device\n",
212 progname, special);
213 cp = strchr(argv[0], '\0') - 1;
214 if (!debug
215 && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
216 && !isdigit(*cp))))
217 fatal("%s: can't figure out file system partition", argv[0]);
218
219 #ifdef COMPAT
220 if (disktype == NULL)
221 disktype = argv[1];
222 #endif
223 if (debug)
224 lp = debug_readlabel(fsi);
225 else
226 lp = getdisklabel(special, fsi);
227
228 if (isdigit(*cp))
229 pp = &lp->d_partitions[0];
230 else
231 pp = &lp->d_partitions[*cp - 'a'];
232 if (pp->p_size == 0)
233 fatal("%s: `%c' partition is unavailable", argv[0], *cp);
234
235 /* If force, make the partition look like an LFS */
236 if(force) {
237 pp->p_fstype = FS_BSDLFS;
238 /* 0 means to use defaults */
239 pp->p_fsize = 0;
240 pp->p_frag = 0;
241 pp->p_sgs = 0;
242 }
243
244 /* If we're making a LFS, we break out here */
245 exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize,
246 minfreeseg));
247 }
248
249 #ifdef COMPAT
250 char lmsg[] = "%s: can't read disk label; disk type must be specified";
251 #else
252 char lmsg[] = "%s: can't read disk label";
253 #endif
254
255 static struct disklabel *
256 getdisklabel(s, fd)
257 char *s;
258 int fd;
259 {
260 static struct disklabel lab;
261
262 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
263 #ifdef COMPAT
264 if (disktype) {
265 struct disklabel *lp;
266
267 unlabeled++;
268 lp = getdiskbyname(disktype);
269 if (lp == NULL)
270 fatal("%s: unknown disk type", disktype);
271 return (lp);
272 }
273 #endif
274 (void)fprintf(stderr,
275 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
276 fatal(lmsg, s);
277 }
278 return (&lab);
279 }
280
281
282 static struct disklabel *
283 debug_readlabel(fd)
284 int fd;
285 {
286 static struct disklabel lab;
287 int n;
288
289 if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
290 fatal("unable to read disk label: %s", strerror(errno));
291 else if (n < sizeof(struct disklabel))
292 fatal("short read of disklabel: %d of %ld bytes", n,
293 (u_long) sizeof(struct disklabel));
294 return(&lab);
295 }
296
297 #ifdef notdef
298 static void
299 rewritelabel(s, fd, lp)
300 char *s;
301 int fd;
302 struct disklabel *lp;
303 {
304 #ifdef COMPAT
305 if (unlabeled)
306 return;
307 #endif
308 lp->d_checksum = 0;
309 lp->d_checksum = dkcksum(lp);
310 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
311 (void)fprintf(stderr,
312 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
313 fatal("%s: can't rewrite disk label", s);
314 }
315 #if __vax__
316 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
317 int i;
318 int cfd;
319 daddr_t alt;
320 char specname[64];
321 char blk[1024];
322 char *cp;
323
324 /*
325 * Make name for 'c' partition.
326 */
327 strcpy(specname, s);
328 cp = specname + strlen(specname) - 1;
329 if (!isdigit(*cp))
330 *cp = 'c';
331 cfd = open(specname, O_WRONLY);
332 if (cfd < 0)
333 fatal("%s: %s", specname, strerror(errno));
334 memset(blk, 0, sizeof(blk));
335 *(struct disklabel *)(blk + LABELOFFSET) = *lp;
336 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
337 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
338 if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
339 SEEK_SET) == -1)
340 fatal("lseek to badsector area: %s",
341 strerror(errno));
342 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
343 fprintf(stderr,
344 "%s: alternate label %d write: %s\n",
345 progname, i/2, strerror(errno));
346 }
347 close(cfd);
348 }
349 #endif /* vax */
350 }
351 #endif /* notdef */
352
353 void
354 usage()
355 {
356 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
357 fprintf(stderr, "where fsoptions are:\n");
358 fprintf(stderr, "\t-B LFS segment size\n");
359 fprintf(stderr, "\t-D debug\n");
360 /* fprintf(stderr, "\t-L create LFS file system\n"); */
361 fprintf(stderr,
362 "\t-N do not create file system, just print out parameters\n");
363 fprintf(stderr, "\t-b block size\n");
364 fprintf(stderr, "\t-f frag size\n");
365 fprintf(stderr, "\t-m minimum free space %%\n");
366 fprintf(stderr, "\t-s file system size (sectors)\n");
367 exit(1);
368 }
369