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