newfs.c revision 1.64 1 1.64 fvdl /* $NetBSD: newfs.c,v 1.64 2003/04/02 10:39:30 fvdl Exp $ */
2 1.18 cgd
3 1.1 cgd /*
4 1.64 fvdl * Copyright (c) 2002 Networks Associates Technology, Inc.
5 1.64 fvdl * All rights reserved.
6 1.64 fvdl *
7 1.64 fvdl * This software was developed for the FreeBSD Project by Marshall
8 1.64 fvdl * Kirk McKusick and Network Associates Laboratories, the Security
9 1.64 fvdl * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 1.64 fvdl * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 1.64 fvdl * research program
12 1.64 fvdl *
13 1.10 mycroft * Copyright (c) 1983, 1989, 1993, 1994
14 1.10 mycroft * The Regents of the University of California. All rights reserved.
15 1.1 cgd *
16 1.1 cgd * Redistribution and use in source and binary forms, with or without
17 1.1 cgd * modification, are permitted provided that the following conditions
18 1.1 cgd * are met:
19 1.1 cgd * 1. Redistributions of source code must retain the above copyright
20 1.1 cgd * notice, this list of conditions and the following disclaimer.
21 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 cgd * notice, this list of conditions and the following disclaimer in the
23 1.1 cgd * documentation and/or other materials provided with the distribution.
24 1.1 cgd * 3. All advertising materials mentioning features or use of this software
25 1.1 cgd * must display the following acknowledgement:
26 1.1 cgd * This product includes software developed by the University of
27 1.1 cgd * California, Berkeley and its contributors.
28 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
29 1.1 cgd * may be used to endorse or promote products derived from this software
30 1.1 cgd * without specific prior written permission.
31 1.1 cgd *
32 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 cgd * SUCH DAMAGE.
43 1.1 cgd */
44 1.1 cgd
45 1.25 christos #include <sys/cdefs.h>
46 1.1 cgd #ifndef lint
47 1.25 christos __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
48 1.25 christos The Regents of the University of California. All rights reserved.\n");
49 1.1 cgd #endif /* not lint */
50 1.1 cgd
51 1.1 cgd #ifndef lint
52 1.18 cgd #if 0
53 1.27 lukem static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
54 1.18 cgd #else
55 1.64 fvdl __RCSID("$NetBSD: newfs.c,v 1.64 2003/04/02 10:39:30 fvdl Exp $");
56 1.18 cgd #endif
57 1.1 cgd #endif /* not lint */
58 1.1 cgd
59 1.1 cgd /*
60 1.1 cgd * newfs: friendly front end to mkfs
61 1.1 cgd */
62 1.1 cgd #include <sys/param.h>
63 1.1 cgd #include <sys/ioctl.h>
64 1.1 cgd #include <sys/disklabel.h>
65 1.1 cgd #include <sys/file.h>
66 1.1 cgd #include <sys/mount.h>
67 1.19 thorpej #include <sys/sysctl.h>
68 1.30 drochner #include <sys/wait.h>
69 1.1 cgd
70 1.10 mycroft #include <ufs/ufs/dir.h>
71 1.27 lukem #include <ufs/ufs/dinode.h>
72 1.32 fvdl #include <ufs/ufs/ufsmount.h>
73 1.10 mycroft #include <ufs/ffs/fs.h>
74 1.10 mycroft
75 1.10 mycroft #include <ctype.h>
76 1.37 tron #include <disktab.h>
77 1.54 simonb #include <err.h>
78 1.1 cgd #include <errno.h>
79 1.54 simonb #include <grp.h>
80 1.10 mycroft #include <paths.h>
81 1.54 simonb #include <pwd.h>
82 1.54 simonb #include <signal.h>
83 1.1 cgd #include <stdio.h>
84 1.10 mycroft #include <stdlib.h>
85 1.1 cgd #include <string.h>
86 1.10 mycroft #include <syslog.h>
87 1.10 mycroft #include <unistd.h>
88 1.20 thorpej #include <util.h>
89 1.10 mycroft
90 1.10 mycroft #include "mntopts.h"
91 1.25 christos #include "dkcksum.h"
92 1.25 christos #include "extern.h"
93 1.10 mycroft
94 1.10 mycroft struct mntopt mopts[] = {
95 1.10 mycroft MOPT_STDOPTS,
96 1.10 mycroft MOPT_ASYNC,
97 1.22 cgd MOPT_UPDATE,
98 1.60 christos MOPT_GETARGS,
99 1.23 tls MOPT_NOATIME,
100 1.10 mycroft { NULL },
101 1.10 mycroft };
102 1.10 mycroft
103 1.40 simonb static struct disklabel *getdisklabel(char *, int);
104 1.40 simonb static void rewritelabel(char *, int, struct disklabel *);
105 1.54 simonb static gid_t mfs_group(const char *);
106 1.54 simonb static uid_t mfs_user(const char *);
107 1.43 lukem static int strsuftoi(const char *, const char *, int, int);
108 1.40 simonb static void usage(void);
109 1.40 simonb int main(int, char *[]);
110 1.1 cgd
111 1.1 cgd #define COMPAT /* allow non-labeled disks */
112 1.1 cgd
113 1.1 cgd /*
114 1.1 cgd * The following two constants set the default block and fragment sizes.
115 1.1 cgd * Both constants must be a power of 2 and meet the following constraints:
116 1.1 cgd * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
117 1.1 cgd * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
118 1.1 cgd * DESBLKSIZE / DESFRAGSIZE <= 8
119 1.1 cgd */
120 1.53 augustss /*
121 1.53 augustss * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults,
122 1.53 augustss * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, otherwise use
123 1.53 augustss * L_DFL_*.
124 1.53 augustss */
125 1.54 simonb #define SMALL_FSSIZE (20*1024*2)
126 1.53 augustss #define S_DFL_FRAGSIZE 512
127 1.53 augustss #define S_DFL_BLKSIZE 4096
128 1.54 simonb #define MEDIUM_FSSIZE (1000*1024*2)
129 1.53 augustss #define M_DFL_FRAGSIZE 1024
130 1.53 augustss #define M_DFL_BLKSIZE 8192
131 1.53 augustss #define L_DFL_FRAGSIZE 2048
132 1.53 augustss #define L_DFL_BLKSIZE 16384
133 1.1 cgd
134 1.1 cgd /*
135 1.43 lukem * Default sector size.
136 1.43 lukem */
137 1.54 simonb #define DFL_SECSIZE 512
138 1.43 lukem
139 1.43 lukem /*
140 1.1 cgd * Cylinder groups may have up to many cylinders. The actual
141 1.1 cgd * number used depends upon how much information can be stored
142 1.1 cgd * on a single cylinder. The default is to use 16 cylinders
143 1.1 cgd * per group.
144 1.1 cgd */
145 1.64 fvdl #define MAXBLKSPERCG 0x7fffffff /* desired fs_fpg ("infinity") */
146 1.1 cgd
147 1.1 cgd /*
148 1.1 cgd * MAXBLKPG determines the maximum number of data blocks which are
149 1.1 cgd * placed in a single cylinder group. The default is one indirect
150 1.1 cgd * block worth of data blocks.
151 1.1 cgd */
152 1.64 fvdl #define MAXBLKPG_UFS1(bsize) ((bsize) / sizeof(int32_t))
153 1.64 fvdl #define MAXBLKPG_UFS2(bsize) ((bsize) / sizeof(int64_t))
154 1.1 cgd
155 1.1 cgd /*
156 1.1 cgd * Each file system has a number of inodes statically allocated.
157 1.1 cgd * We allocate one inode slot per NFPI fragments, expecting this
158 1.1 cgd * to be far more than we will ever need.
159 1.1 cgd */
160 1.1 cgd #define NFPI 4
161 1.1 cgd
162 1.1 cgd
163 1.1 cgd int mfs; /* run as the memory based filesystem */
164 1.1 cgd int Nflag; /* run without writing file system */
165 1.64 fvdl int Oflag = 1; /* format as an 4.3BSD file system */
166 1.64 fvdl int64_t fssize; /* file system size */
167 1.1 cgd int sectorsize; /* bytes/sector */
168 1.1 cgd int fsize = 0; /* fragment size */
169 1.1 cgd int bsize = 0; /* block size */
170 1.64 fvdl int maxbsize = 0; /* maximum clustering */
171 1.64 fvdl int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
172 1.1 cgd int minfree = MINFREE; /* free space threshold */
173 1.1 cgd int opt = DEFAULTOPT; /* optimization preference (space or time) */
174 1.1 cgd int density; /* number of bytes per inode */
175 1.36 mycroft int maxcontig = 0; /* max contiguous blocks to allocate */
176 1.1 cgd int maxbpg; /* maximum blocks per file in a cyl group */
177 1.47 lukem int avgfilesize = AVFILESIZ;/* expected average file size */
178 1.47 lukem int avgfpdir = AFPDIR; /* expected number of files per directory */
179 1.1 cgd int bbsize = BBSIZE; /* boot block size */
180 1.64 fvdl int sbsize = SBLOCKSIZE; /* superblock size */
181 1.10 mycroft int mntflags = MNT_ASYNC; /* flags to be passed to mount */
182 1.1 cgd u_long memleft; /* virtual memory available */
183 1.1 cgd caddr_t membase; /* start address of memory based filesystem */
184 1.43 lukem int needswap; /* Filesystem not in native byte order */
185 1.1 cgd #ifdef COMPAT
186 1.1 cgd char *disktype;
187 1.1 cgd int unlabeled;
188 1.1 cgd #endif
189 1.61 dbj char *appleufs_volname = 0; /* Apple UFS volume name */
190 1.61 dbj int isappleufs = 0;
191 1.1 cgd
192 1.1 cgd char device[MAXPATHLEN];
193 1.1 cgd
194 1.10 mycroft int
195 1.40 simonb main(int argc, char *argv[])
196 1.1 cgd {
197 1.25 christos struct partition *pp;
198 1.25 christos struct disklabel *lp;
199 1.14 cgd struct disklabel mfsfakelabel;
200 1.1 cgd struct partition oldpartition;
201 1.10 mycroft struct statfs *mp;
202 1.58 lukem int ch, fsi, fso, len, maxpartitions, n, Fflag, Iflag, Zflag;
203 1.43 lukem char *cp, *endp, *s1, *s2, *special;
204 1.43 lukem const char *opstring;
205 1.43 lukem long long llsize;
206 1.53 augustss int dfl_fragsize, dfl_blksize;
207 1.30 drochner #ifdef MFS
208 1.30 drochner char mountfromname[100];
209 1.30 drochner pid_t pid, res;
210 1.30 drochner struct statfs sf;
211 1.56 lukem int status;
212 1.56 lukem #endif
213 1.54 simonb mode_t mfsmode;
214 1.54 simonb uid_t mfsuid;
215 1.54 simonb gid_t mfsgid;
216 1.1 cgd
217 1.43 lukem cp = NULL;
218 1.43 lukem fsi = fso = -1;
219 1.58 lukem Fflag = Iflag = Zflag = 0;
220 1.42 cgd if (strstr(getprogname(), "mfs")) {
221 1.1 cgd mfs = 1;
222 1.54 simonb mfsmode = 01777; /* default mode for a /tmp-type directory */
223 1.54 simonb mfsuid = 0; /* user root */
224 1.54 simonb mfsgid = 0; /* group wheel */
225 1.1 cgd Nflag++;
226 1.1 cgd }
227 1.1 cgd
228 1.19 thorpej maxpartitions = getmaxpartitions();
229 1.19 thorpej if (maxpartitions > 26)
230 1.25 christos errx(1, "insane maxpartitions value %d", maxpartitions);
231 1.19 thorpej
232 1.10 mycroft opstring = mfs ?
233 1.54 simonb "NT:a:b:c:d:e:f:g:h:i:m:o:p:s:u:" :
234 1.64 fvdl "B:FINO:S:T:Za:b:c:d:e:f:g:h:i:l:m:o:p:r:s:u:v:";
235 1.26 lukem while ((ch = getopt(argc, argv, opstring)) != -1)
236 1.10 mycroft switch (ch) {
237 1.33 bouyer case 'B':
238 1.33 bouyer if (strcmp(optarg, "be") == 0) {
239 1.33 bouyer #if BYTE_ORDER == LITTLE_ENDIAN
240 1.33 bouyer needswap = 1;
241 1.33 bouyer #endif
242 1.33 bouyer } else if (strcmp(optarg, "le") == 0) {
243 1.33 bouyer #if BYTE_ORDER == BIG_ENDIAN
244 1.33 bouyer needswap = 1;
245 1.33 bouyer #endif
246 1.33 bouyer } else
247 1.33 bouyer usage();
248 1.33 bouyer break;
249 1.43 lukem case 'F':
250 1.43 lukem Fflag = 1;
251 1.43 lukem break;
252 1.58 lukem case 'I':
253 1.58 lukem Iflag = 1;
254 1.58 lukem break;
255 1.10 mycroft case 'N':
256 1.10 mycroft Nflag = 1;
257 1.1 cgd break;
258 1.10 mycroft case 'O':
259 1.64 fvdl Oflag = strsuftoi("format", optarg, 0, 2);
260 1.1 cgd break;
261 1.1 cgd case 'S':
262 1.43 lukem sectorsize = strsuftoi("sector size",
263 1.43 lukem optarg, 1, INT_MAX);
264 1.1 cgd break;
265 1.1 cgd #ifdef COMPAT
266 1.1 cgd case 'T':
267 1.1 cgd disktype = optarg;
268 1.1 cgd break;
269 1.1 cgd #endif
270 1.43 lukem case 'Z':
271 1.43 lukem Zflag = 1;
272 1.43 lukem break;
273 1.1 cgd case 'a':
274 1.43 lukem maxcontig = strsuftoi("maximum contiguous blocks",
275 1.43 lukem optarg, 1, INT_MAX);
276 1.1 cgd break;
277 1.1 cgd case 'b':
278 1.43 lukem bsize = strsuftoi("block size",
279 1.50 lukem optarg, MINBSIZE, MAXBSIZE);
280 1.1 cgd break;
281 1.1 cgd case 'c':
282 1.64 fvdl maxblkspercg = strsuftoi("max. blocks per group",
283 1.43 lukem optarg, 1, INT_MAX);
284 1.1 cgd break;
285 1.1 cgd case 'd':
286 1.64 fvdl maxbsize = strsuftoi("maximum extent size",
287 1.43 lukem optarg, 0, INT_MAX);
288 1.1 cgd break;
289 1.1 cgd case 'e':
290 1.43 lukem maxbpg = strsuftoi(
291 1.43 lukem "blocks per file in a cylinder group",
292 1.43 lukem optarg, 1, INT_MAX);
293 1.1 cgd break;
294 1.1 cgd case 'f':
295 1.43 lukem fsize = strsuftoi("fragment size",
296 1.50 lukem optarg, 1, MAXBSIZE);
297 1.1 cgd break;
298 1.47 lukem case 'g':
299 1.54 simonb if (mfs)
300 1.54 simonb mfsgid = mfs_group(optarg);
301 1.54 simonb else {
302 1.54 simonb avgfilesize = strsuftoi("average file size",
303 1.54 simonb optarg, 1, INT_MAX);
304 1.54 simonb }
305 1.47 lukem break;
306 1.47 lukem case 'h':
307 1.47 lukem avgfpdir = strsuftoi("expected files per directory",
308 1.47 lukem optarg, 1, INT_MAX);
309 1.47 lukem break;
310 1.1 cgd case 'i':
311 1.43 lukem density = strsuftoi("bytes per inode",
312 1.43 lukem optarg, 1, INT_MAX);
313 1.1 cgd break;
314 1.1 cgd case 'm':
315 1.43 lukem minfree = strsuftoi("free space %",
316 1.43 lukem optarg, 0, 99);
317 1.1 cgd break;
318 1.1 cgd case 'o':
319 1.10 mycroft if (mfs)
320 1.27 lukem getmntopts(optarg, mopts, &mntflags, 0);
321 1.10 mycroft else {
322 1.10 mycroft if (strcmp(optarg, "space") == 0)
323 1.10 mycroft opt = FS_OPTSPACE;
324 1.10 mycroft else if (strcmp(optarg, "time") == 0)
325 1.10 mycroft opt = FS_OPTTIME;
326 1.10 mycroft else
327 1.25 christos errx(1, "%s %s",
328 1.25 christos "unknown optimization preference: ",
329 1.25 christos "use `space' or `time'.");
330 1.10 mycroft }
331 1.1 cgd break;
332 1.1 cgd case 'p':
333 1.54 simonb if (mfs) {
334 1.54 simonb if ((mfsmode = strtol(optarg, NULL, 8)) <= 0)
335 1.54 simonb errx(1, "bad mode `%s'", optarg);
336 1.64 fvdl } else
337 1.64 fvdl errx(1, "unknown option 'p'");
338 1.1 cgd break;
339 1.1 cgd case 's':
340 1.43 lukem llsize = strtoll(optarg, &endp, 10);
341 1.43 lukem if (endp[0] != '\0' && endp[1] != '\0')
342 1.43 lukem llsize = -1;
343 1.43 lukem else {
344 1.43 lukem int ssiz;
345 1.43 lukem
346 1.43 lukem ssiz = (sectorsize ? sectorsize : DFL_SECSIZE);
347 1.43 lukem switch (tolower((unsigned char)endp[0])) {
348 1.43 lukem case 'b':
349 1.43 lukem llsize /= ssiz;
350 1.43 lukem break;
351 1.43 lukem case 'k':
352 1.43 lukem llsize *= 1024 / ssiz;
353 1.43 lukem break;
354 1.43 lukem case 'm':
355 1.43 lukem llsize *= 1024 * 1024 / ssiz;
356 1.43 lukem break;
357 1.43 lukem case 'g':
358 1.43 lukem llsize *= 1024 * 1024 * 1024 / ssiz;
359 1.43 lukem break;
360 1.43 lukem case '\0':
361 1.43 lukem case 's':
362 1.43 lukem break;
363 1.43 lukem default:
364 1.43 lukem llsize = -1;
365 1.41 simonb }
366 1.41 simonb }
367 1.64 fvdl if (llsize > LLONG_MAX)
368 1.43 lukem errx(1, "file system size `%s' is too large.",
369 1.43 lukem optarg);
370 1.43 lukem if (llsize <= 0)
371 1.43 lukem errx(1,
372 1.43 lukem "`%s' is not a valid number for file system size.",
373 1.43 lukem optarg);
374 1.64 fvdl fssize = llsize;
375 1.1 cgd break;
376 1.1 cgd case 'u':
377 1.54 simonb if (mfs)
378 1.54 simonb mfsuid = mfs_user(optarg);
379 1.64 fvdl else
380 1.64 fvdl errx(1, "deprecated option 'u'");
381 1.1 cgd break;
382 1.61 dbj case 'v':
383 1.61 dbj appleufs_volname = optarg;
384 1.61 dbj if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/'))
385 1.61 dbj errx(1,"Apple UFS volume name cannot contain ':' or '/'");
386 1.61 dbj if (appleufs_volname[0] == '\0')
387 1.61 dbj errx(1,"Apple UFS volume name cannot be zero length");
388 1.61 dbj isappleufs = 1;
389 1.61 dbj break;
390 1.1 cgd case '?':
391 1.1 cgd default:
392 1.1 cgd usage();
393 1.1 cgd }
394 1.1 cgd argc -= optind;
395 1.1 cgd argv += optind;
396 1.60 christos if (mntflags & MNT_GETARGS)
397 1.60 christos goto doit;
398 1.1 cgd
399 1.1 cgd if (argc != 2 && (mfs || argc != 1))
400 1.1 cgd usage();
401 1.1 cgd
402 1.1 cgd special = argv[0];
403 1.49 lukem if (Fflag || mfs) {
404 1.14 cgd /*
405 1.49 lukem * it's a file system image or an MFS, so fake up a label.
406 1.49 lukem * XXX
407 1.14 cgd */
408 1.43 lukem if (!sectorsize)
409 1.43 lukem sectorsize = DFL_SECSIZE;
410 1.43 lukem
411 1.43 lukem if (Fflag && !Nflag) { /* creating image in a regular file */
412 1.44 lukem if (fssize == 0)
413 1.44 lukem errx(1, "need to specify size when using -F");
414 1.43 lukem fso = open(special, O_RDWR | O_CREAT | O_TRUNC, 0777);
415 1.43 lukem if (fso == -1)
416 1.43 lukem err(1, "can't open file %s", special);
417 1.44 lukem if ((fsi = dup(fso)) == -1)
418 1.44 lukem err(1, "can't dup(2) image fd");
419 1.48 lukem /* XXXLUKEM: only ftruncate() regular files ? */
420 1.43 lukem if (ftruncate(fso, (off_t)fssize * sectorsize) == -1)
421 1.64 fvdl err(1, "can't resize %s to %lld",
422 1.64 fvdl special, (long long)fssize);
423 1.43 lukem
424 1.43 lukem if (Zflag) { /* pre-zero the file */
425 1.43 lukem char *buf;
426 1.45 lukem int bufsize, i;
427 1.45 lukem off_t bufrem;
428 1.45 lukem struct statfs sfs;
429 1.45 lukem
430 1.45 lukem if (fstatfs(fso, &sfs) == -1) {
431 1.45 lukem warn("can't fstatfs `%s'", special);
432 1.45 lukem bufsize = 8192;
433 1.45 lukem } else
434 1.45 lukem bufsize = sfs.f_iosize;
435 1.43 lukem
436 1.45 lukem if ((buf = calloc(1, bufsize)) == NULL)
437 1.43 lukem err(1, "can't malloc buffer of %d",
438 1.45 lukem bufsize);
439 1.45 lukem bufrem = fssize * sectorsize;
440 1.43 lukem printf(
441 1.45 lukem "Creating file system image in `%s', size %lld bytes, in %d byte chunks.\n",
442 1.45 lukem special, (long long)bufrem, bufsize);
443 1.45 lukem while (bufrem > 0) {
444 1.45 lukem i = write(fso, buf,
445 1.45 lukem MIN(bufsize, bufrem));
446 1.45 lukem if (i == -1)
447 1.45 lukem err(1, "writing image");
448 1.45 lukem bufrem -= i;
449 1.43 lukem }
450 1.43 lukem }
451 1.43 lukem
452 1.43 lukem }
453 1.14 cgd
454 1.14 cgd memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
455 1.43 lukem mfsfakelabel.d_secsize = sectorsize;
456 1.43 lukem mfsfakelabel.d_nsectors = 64; /* these 3 add up to 16MB */
457 1.14 cgd mfsfakelabel.d_ntracks = 16;
458 1.14 cgd mfsfakelabel.d_ncylinders = 16;
459 1.43 lukem mfsfakelabel.d_secpercyl =
460 1.43 lukem mfsfakelabel.d_nsectors * mfsfakelabel.d_ntracks;
461 1.43 lukem mfsfakelabel.d_secperunit =
462 1.43 lukem mfsfakelabel.d_ncylinders * mfsfakelabel.d_secpercyl;
463 1.49 lukem mfsfakelabel.d_rpm = 10000;
464 1.14 cgd mfsfakelabel.d_interleave = 1;
465 1.14 cgd mfsfakelabel.d_npartitions = 1;
466 1.43 lukem mfsfakelabel.d_partitions[0].p_size = mfsfakelabel.d_secperunit;
467 1.14 cgd mfsfakelabel.d_partitions[0].p_fsize = 1024;
468 1.14 cgd mfsfakelabel.d_partitions[0].p_frag = 8;
469 1.14 cgd mfsfakelabel.d_partitions[0].p_cpg = 16;
470 1.14 cgd
471 1.14 cgd lp = &mfsfakelabel;
472 1.14 cgd pp = &mfsfakelabel.d_partitions[0];
473 1.49 lukem } else { /* !Fflag && !mfs */
474 1.49 lukem fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0);
475 1.49 lukem special = device;
476 1.49 lukem if (fsi < 0)
477 1.49 lukem err(1, "%s: open for read", special);
478 1.49 lukem
479 1.49 lukem if (Nflag) {
480 1.49 lukem fso = -1;
481 1.49 lukem } else {
482 1.49 lukem fso = open(special, O_WRONLY);
483 1.49 lukem if (fso < 0)
484 1.49 lukem err(1, "%s: open for write", special);
485 1.49 lukem
486 1.49 lukem /* Bail if target special is mounted */
487 1.49 lukem n = getmntinfo(&mp, MNT_NOWAIT);
488 1.49 lukem if (n == 0)
489 1.49 lukem err(1, "%s: getmntinfo", special);
490 1.49 lukem
491 1.49 lukem len = sizeof(_PATH_DEV) - 1;
492 1.49 lukem s1 = special;
493 1.49 lukem if (strncmp(_PATH_DEV, s1, len) == 0)
494 1.49 lukem s1 += len;
495 1.49 lukem
496 1.49 lukem while (--n >= 0) {
497 1.49 lukem s2 = mp->f_mntfromname;
498 1.49 lukem if (strncmp(_PATH_DEV, s2, len) == 0) {
499 1.49 lukem s2 += len - 1;
500 1.49 lukem *s2 = 'r';
501 1.49 lukem }
502 1.49 lukem if (strcmp(s1, s2) == 0 ||
503 1.49 lukem strcmp(s1, &s2[1]) == 0)
504 1.49 lukem errx(1, "%s is mounted on %s",
505 1.49 lukem special, mp->f_mntonname);
506 1.49 lukem ++mp;
507 1.10 mycroft }
508 1.10 mycroft }
509 1.11 mycroft cp = strchr(argv[0], '\0') - 1;
510 1.25 christos if (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
511 1.25 christos && !isdigit(*cp)))
512 1.25 christos errx(1, "can't figure out file system partition");
513 1.1 cgd #ifdef COMPAT
514 1.49 lukem if (disktype == NULL)
515 1.10 mycroft disktype = argv[1];
516 1.1 cgd #endif
517 1.10 mycroft lp = getdisklabel(special, fsi);
518 1.10 mycroft if (isdigit(*cp))
519 1.10 mycroft pp = &lp->d_partitions[0];
520 1.10 mycroft else
521 1.10 mycroft pp = &lp->d_partitions[*cp - 'a'];
522 1.10 mycroft if (pp->p_size == 0)
523 1.25 christos errx(1, "`%c' partition is unavailable", *cp);
524 1.61 dbj if (pp->p_fstype == FS_APPLEUFS)
525 1.61 dbj isappleufs = 1;
526 1.61 dbj if (isappleufs) {
527 1.61 dbj if (!Iflag && (pp->p_fstype != FS_APPLEUFS))
528 1.61 dbj errx(1, "`%c' partition type is not `Apple UFS'", *cp);
529 1.61 dbj } else {
530 1.61 dbj if (!Iflag && (pp->p_fstype != FS_BSDFFS))
531 1.61 dbj errx(1, "`%c' partition type is not `4.2BSD'", *cp);
532 1.61 dbj }
533 1.49 lukem } /* !Fflag && !mfs */
534 1.43 lukem
535 1.1 cgd if (fssize == 0)
536 1.1 cgd fssize = pp->p_size;
537 1.43 lukem if (fssize > pp->p_size && !mfs && !Fflag)
538 1.25 christos errx(1, "maximum file system size on the `%c' partition is %d",
539 1.25 christos *cp, pp->p_size);
540 1.1 cgd if (sectorsize == 0) {
541 1.1 cgd sectorsize = lp->d_secsize;
542 1.1 cgd if (sectorsize <= 0)
543 1.25 christos errx(1, "no default sector size");
544 1.1 cgd }
545 1.53 augustss
546 1.53 augustss if (fssize < SMALL_FSSIZE) {
547 1.53 augustss dfl_fragsize = S_DFL_FRAGSIZE;
548 1.53 augustss dfl_blksize = S_DFL_BLKSIZE;
549 1.53 augustss } else if (fssize < MEDIUM_FSSIZE) {
550 1.53 augustss dfl_fragsize = M_DFL_FRAGSIZE;
551 1.53 augustss dfl_blksize = M_DFL_BLKSIZE;
552 1.53 augustss } else {
553 1.53 augustss dfl_fragsize = L_DFL_FRAGSIZE;
554 1.53 augustss dfl_blksize = L_DFL_BLKSIZE;
555 1.53 augustss }
556 1.53 augustss
557 1.1 cgd if (fsize == 0) {
558 1.1 cgd fsize = pp->p_fsize;
559 1.1 cgd if (fsize <= 0)
560 1.53 augustss fsize = MAX(dfl_fragsize, lp->d_secsize);
561 1.1 cgd }
562 1.1 cgd if (bsize == 0) {
563 1.1 cgd bsize = pp->p_frag * pp->p_fsize;
564 1.1 cgd if (bsize <= 0)
565 1.53 augustss bsize = MIN(dfl_blksize, 8 * fsize);
566 1.1 cgd }
567 1.10 mycroft /*
568 1.10 mycroft * Maxcontig sets the default for the maximum number of blocks
569 1.10 mycroft * that may be allocated sequentially. With filesystem clustering
570 1.10 mycroft * it is possible to allocate contiguous blocks up to the maximum
571 1.10 mycroft * transfer size permitted by the controller or buffering.
572 1.10 mycroft */
573 1.10 mycroft if (maxcontig == 0)
574 1.27 lukem maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
575 1.1 cgd if (density == 0)
576 1.1 cgd density = NFPI * fsize;
577 1.8 cgd if (minfree < MINFREE && opt != FS_OPTSPACE) {
578 1.25 christos warnx("%s %s %d%%", "Warning: changing optimization to space",
579 1.25 christos "because minfree is less than", MINFREE);
580 1.1 cgd opt = FS_OPTSPACE;
581 1.1 cgd }
582 1.64 fvdl if (maxbpg == 0) {
583 1.64 fvdl if (Oflag <= 1)
584 1.64 fvdl maxbpg = MAXBLKPG_UFS1(bsize);
585 1.64 fvdl else
586 1.64 fvdl maxbpg = MAXBLKPG_UFS2(bsize);
587 1.64 fvdl }
588 1.1 cgd #ifdef notdef /* label may be 0 if faked up by kernel */
589 1.1 cgd bbsize = lp->d_bbsize;
590 1.1 cgd sbsize = lp->d_sbsize;
591 1.1 cgd #endif
592 1.1 cgd oldpartition = *pp;
593 1.54 simonb mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid);
594 1.43 lukem if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)) && !Fflag)
595 1.1 cgd rewritelabel(special, fso, lp);
596 1.1 cgd if (!Nflag)
597 1.1 cgd close(fso);
598 1.1 cgd close(fsi);
599 1.1 cgd #ifdef MFS
600 1.1 cgd if (mfs) {
601 1.1 cgd struct mfs_args args;
602 1.1 cgd
603 1.30 drochner switch (pid = fork()) {
604 1.30 drochner case -1:
605 1.30 drochner perror("mfs");
606 1.30 drochner exit(10);
607 1.30 drochner case 0:
608 1.34 mycroft (void)snprintf(mountfromname, sizeof(mountfromname),
609 1.34 mycroft "mfs:%d", getpid());
610 1.30 drochner break;
611 1.30 drochner default:
612 1.34 mycroft (void)snprintf(mountfromname, sizeof(mountfromname),
613 1.34 mycroft "mfs:%d", pid);
614 1.30 drochner for (;;) {
615 1.30 drochner /*
616 1.30 drochner * spin until the mount succeeds
617 1.30 drochner * or the child exits
618 1.30 drochner */
619 1.30 drochner usleep(1);
620 1.30 drochner
621 1.30 drochner /*
622 1.30 drochner * XXX Here is a race condition: another process
623 1.30 drochner * can mount a filesystem which hides our
624 1.30 drochner * ramdisk before we see the success.
625 1.30 drochner */
626 1.30 drochner if (statfs(argv[1], &sf) < 0)
627 1.30 drochner err(88, "statfs %s", argv[1]);
628 1.30 drochner if (!strcmp(sf.f_mntfromname, mountfromname) &&
629 1.30 drochner !strncmp(sf.f_mntonname, argv[1],
630 1.30 drochner MNAMELEN) &&
631 1.30 drochner !strcmp(sf.f_fstypename, "mfs"))
632 1.30 drochner exit(0);
633 1.30 drochner
634 1.30 drochner res = waitpid(pid, &status, WNOHANG);
635 1.30 drochner if (res == -1)
636 1.30 drochner err(11, "waitpid");
637 1.30 drochner if (res != pid)
638 1.30 drochner continue;
639 1.31 drochner if (WIFEXITED(status)) {
640 1.31 drochner if (WEXITSTATUS(status) == 0)
641 1.31 drochner exit(0);
642 1.30 drochner errx(1, "%s: mount: %s", argv[1],
643 1.30 drochner strerror(WEXITSTATUS(status)));
644 1.31 drochner } else
645 1.30 drochner errx(11, "abnormal termination");
646 1.30 drochner }
647 1.30 drochner /* NOTREACHED */
648 1.30 drochner }
649 1.30 drochner
650 1.30 drochner (void) setsid();
651 1.30 drochner (void) close(0);
652 1.30 drochner (void) close(1);
653 1.30 drochner (void) close(2);
654 1.30 drochner (void) chdir("/");
655 1.30 drochner
656 1.30 drochner args.fspec = mountfromname;
657 1.10 mycroft args.export.ex_root = -2;
658 1.10 mycroft if (mntflags & MNT_RDONLY)
659 1.10 mycroft args.export.ex_flags = MNT_EXRDONLY;
660 1.10 mycroft else
661 1.10 mycroft args.export.ex_flags = 0;
662 1.1 cgd args.base = membase;
663 1.1 cgd args.size = fssize * sectorsize;
664 1.60 christos doit:
665 1.60 christos if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0) {
666 1.60 christos if (mntflags & MNT_GETARGS)
667 1.60 christos err(1, "mount `%s' failed", argv[1]);
668 1.30 drochner exit(errno); /* parent prints message */
669 1.60 christos }
670 1.60 christos if (mntflags & MNT_GETARGS)
671 1.60 christos printf("base=%p, size=%ld\n", args.base, args.size);
672 1.1 cgd }
673 1.1 cgd #endif
674 1.1 cgd exit(0);
675 1.1 cgd }
676 1.1 cgd
677 1.1 cgd #ifdef COMPAT
678 1.39 is const char lmsg[] = "%s: can't read disk label; disk type must be specified";
679 1.1 cgd #else
680 1.39 is const char lmsg[] = "%s: can't read disk label";
681 1.1 cgd #endif
682 1.1 cgd
683 1.25 christos static struct disklabel *
684 1.40 simonb getdisklabel(char *s, volatile int fd)
685 1.40 simonb /* XXX why is fs volatile?! */
686 1.1 cgd {
687 1.1 cgd static struct disklabel lab;
688 1.1 cgd
689 1.24 tls if (ioctl(fd, DIOCGDINFO, &lab) < 0) {
690 1.1 cgd #ifdef COMPAT
691 1.1 cgd if (disktype) {
692 1.25 christos struct disklabel *lp;
693 1.1 cgd
694 1.1 cgd unlabeled++;
695 1.1 cgd lp = getdiskbyname(disktype);
696 1.1 cgd if (lp == NULL)
697 1.25 christos errx(1, "%s: unknown disk type", disktype);
698 1.1 cgd return (lp);
699 1.1 cgd }
700 1.1 cgd #endif
701 1.10 mycroft warn("ioctl (GDINFO)");
702 1.25 christos errx(1, lmsg, s);
703 1.1 cgd }
704 1.1 cgd return (&lab);
705 1.1 cgd }
706 1.1 cgd
707 1.25 christos static void
708 1.40 simonb rewritelabel(char *s, volatile int fd, struct disklabel *lp)
709 1.40 simonb /* XXX why is fd volatile?! */
710 1.1 cgd {
711 1.1 cgd #ifdef COMPAT
712 1.1 cgd if (unlabeled)
713 1.1 cgd return;
714 1.1 cgd #endif
715 1.1 cgd lp->d_checksum = 0;
716 1.1 cgd lp->d_checksum = dkcksum(lp);
717 1.1 cgd if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
718 1.57 thorpej if (errno == ESRCH)
719 1.57 thorpej return;
720 1.10 mycroft warn("ioctl (WDINFO)");
721 1.25 christos errx(1, "%s: can't rewrite disk label", s);
722 1.1 cgd }
723 1.35 matt #if __vax__
724 1.1 cgd if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
725 1.25 christos int i;
726 1.1 cgd int cfd;
727 1.1 cgd daddr_t alt;
728 1.62 scw off_t loff;
729 1.1 cgd char specname[64];
730 1.1 cgd char blk[1024];
731 1.1 cgd char *cp;
732 1.1 cgd
733 1.1 cgd /*
734 1.1 cgd * Make name for 'c' partition.
735 1.1 cgd */
736 1.1 cgd strcpy(specname, s);
737 1.1 cgd cp = specname + strlen(specname) - 1;
738 1.1 cgd if (!isdigit(*cp))
739 1.1 cgd *cp = 'c';
740 1.1 cgd cfd = open(specname, O_WRONLY);
741 1.1 cgd if (cfd < 0)
742 1.28 enami err(1, "%s: open", specname);
743 1.62 scw if ((loff = getlabeloffset()) < 0)
744 1.62 scw err(1, "getlabeloffset()");
745 1.11 mycroft memset(blk, 0, sizeof(blk));
746 1.62 scw *(struct disklabel *)(blk + loff) = *lp;
747 1.1 cgd alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
748 1.1 cgd for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
749 1.17 cgd off_t offset;
750 1.17 cgd
751 1.17 cgd offset = alt + i;
752 1.17 cgd offset *= lp->d_secsize;
753 1.17 cgd if (lseek(cfd, offset, SEEK_SET) == -1)
754 1.25 christos err(1, "lseek to badsector area: ");
755 1.1 cgd if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
756 1.10 mycroft warn("alternate label %d write", i/2);
757 1.1 cgd }
758 1.1 cgd close(cfd);
759 1.1 cgd }
760 1.1 cgd #endif
761 1.1 cgd }
762 1.1 cgd
763 1.54 simonb static gid_t
764 1.54 simonb mfs_group(const char *gname)
765 1.54 simonb {
766 1.54 simonb struct group *gp;
767 1.54 simonb
768 1.54 simonb if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname))
769 1.54 simonb errx(1, "unknown gname %s", gname);
770 1.54 simonb return gp ? gp->gr_gid : atoi(gname);
771 1.54 simonb }
772 1.54 simonb
773 1.54 simonb static uid_t
774 1.54 simonb mfs_user(const char *uname)
775 1.54 simonb {
776 1.54 simonb struct passwd *pp;
777 1.54 simonb
778 1.54 simonb if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname))
779 1.54 simonb errx(1, "unknown user %s", uname);
780 1.54 simonb return pp ? pp->pw_uid : atoi(uname);
781 1.54 simonb }
782 1.54 simonb
783 1.43 lukem static int
784 1.43 lukem strsuftoi(const char *desc, const char *arg, int min, int max)
785 1.43 lukem {
786 1.43 lukem long long result;
787 1.43 lukem char *ep;
788 1.43 lukem
789 1.43 lukem errno = 0;
790 1.43 lukem result = strtoll(arg, &ep, 10);
791 1.43 lukem if (ep[0] != '\0' && ep[1] != '\0')
792 1.43 lukem errx(1, "%s `%s' is not a valid number.", desc, arg);
793 1.43 lukem switch (tolower((unsigned char)ep[0])) {
794 1.43 lukem case '\0':
795 1.43 lukem case 'b':
796 1.43 lukem break;
797 1.43 lukem case 'k':
798 1.43 lukem result <<= 10;
799 1.43 lukem break;
800 1.43 lukem case 'm':
801 1.43 lukem result <<= 20;
802 1.43 lukem break;
803 1.43 lukem case 'g':
804 1.43 lukem result <<= 30;
805 1.43 lukem break;
806 1.43 lukem default:
807 1.43 lukem errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
808 1.43 lukem }
809 1.43 lukem if (result < min)
810 1.43 lukem errx(1, "%s `%s' (%lld) is less than minimum (%d).",
811 1.43 lukem desc, arg, result, min);
812 1.43 lukem if (result > max)
813 1.43 lukem errx(1, "%s `%s' (%lld) is greater than maximum (%d).",
814 1.43 lukem desc, arg, result, max);
815 1.43 lukem return ((int)result);
816 1.43 lukem }
817 1.43 lukem
818 1.54 simonb #define NEWFS 1
819 1.54 simonb #define MFS_MOUNT 2
820 1.54 simonb #define BOTH NEWFS | MFS_MOUNT
821 1.54 simonb
822 1.54 simonb struct help_strings {
823 1.54 simonb int flags;
824 1.54 simonb const char *str;
825 1.54 simonb } const help_strings[] = {
826 1.54 simonb { NEWFS, "-B byteorder\tbyte order (`be' or `le')" },
827 1.54 simonb { NEWFS, "-F \t\tcreate file system image in regular file" },
828 1.58 lukem { NEWFS, "-I \t\tdo not check that the file system type is '4.2BSD'" },
829 1.54 simonb { BOTH, "-N \t\tdo not create file system, just print out "
830 1.54 simonb "parameters" },
831 1.64 fvdl { NEWFS, "-O N\t\tfilesystem format: 0 ==> 4.3BSD, 1 ==> FFS, 2==> UFS2" },
832 1.54 simonb { NEWFS, "-S secsize\tsector size" },
833 1.54 simonb #ifdef COMPAT
834 1.54 simonb { NEWFS, "-T disktype\tdisk type" },
835 1.54 simonb #endif
836 1.54 simonb { NEWFS, "-Z \t\tpre-zero the image file (with -F)" },
837 1.54 simonb { BOTH, "-a maxcontig\tmaximum contiguous blocks" },
838 1.54 simonb { BOTH, "-b bsize\tblock size" },
839 1.64 fvdl { BOTH, "-c blocks\tblocks per cylinder group" },
840 1.64 fvdl { BOTH, "-d maxbsize\t maximum extent size" },
841 1.54 simonb { BOTH, "-e maxbpg\tmaximum blocks per file in a cylinder group"
842 1.54 simonb },
843 1.54 simonb { BOTH, "-f fsize\tfrag size" },
844 1.54 simonb { NEWFS, "-g avgfilesize\taverage file size" },
845 1.54 simonb { MFS_MOUNT, "-g groupname\tgroup name of mount point" },
846 1.54 simonb { BOTH, "-h avgfpdir\taverage files per directory" },
847 1.54 simonb { BOTH, "-i density\tnumber of bytes per inode" },
848 1.54 simonb { BOTH, "-m minfree\tminimum free space %%" },
849 1.54 simonb { BOTH, "-o optim\toptimization preference (`space' or `time')"
850 1.54 simonb },
851 1.54 simonb { MFS_MOUNT, "-p perm\t\tpermissions (in octal)" },
852 1.54 simonb { BOTH, "-s fssize\tfile system size (sectors)" },
853 1.54 simonb { MFS_MOUNT, "-u username\tuser name of mount point" },
854 1.61 dbj { NEWFS, "-v volname\tApple UFS volume name" },
855 1.54 simonb { 0, NULL }
856 1.54 simonb };
857 1.54 simonb
858 1.25 christos static void
859 1.40 simonb usage(void)
860 1.1 cgd {
861 1.54 simonb int match;
862 1.54 simonb const struct help_strings *hs;
863 1.43 lukem
864 1.1 cgd if (mfs) {
865 1.1 cgd fprintf(stderr,
866 1.43 lukem "usage: %s [ fsoptions ] special-device mount-point\n",
867 1.42 cgd getprogname());
868 1.1 cgd } else
869 1.1 cgd fprintf(stderr,
870 1.43 lukem "usage: %s [ fsoptions ] special-device%s\n",
871 1.42 cgd getprogname(),
872 1.1 cgd #ifdef COMPAT
873 1.1 cgd " [device-type]");
874 1.1 cgd #else
875 1.1 cgd "");
876 1.1 cgd #endif
877 1.1 cgd fprintf(stderr, "where fsoptions are:\n");
878 1.54 simonb
879 1.54 simonb match = mfs ? MFS_MOUNT : NEWFS;
880 1.54 simonb for (hs = help_strings; hs->flags != 0; hs++)
881 1.54 simonb if (hs->flags & match)
882 1.54 simonb fprintf(stderr, "\t%s\n", hs->str);
883 1.1 cgd exit(1);
884 1.1 cgd }
885