GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: newfs.c,v 1.109 2016/10/11 07:02:46 natano Exp $ */ |
||
2 |
/* $NetBSD: newfs.c,v 1.20 1996/05/16 07:13:03 thorpej Exp $ */ |
||
3 |
|||
4 |
/* |
||
5 |
* Copyright (c) 2002 Networks Associates Technology, Inc. |
||
6 |
* All rights reserved. |
||
7 |
* |
||
8 |
* This software was developed for the FreeBSD Project by Marshall |
||
9 |
* Kirk McKusick and Network Associates Laboratories, the Security |
||
10 |
* Research Division of Network Associates, Inc. under DARPA/SPAWAR |
||
11 |
* contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS |
||
12 |
* research program. |
||
13 |
* |
||
14 |
* Copyright (c) 1983, 1989, 1993, 1994 |
||
15 |
* The Regents of the University of California. All rights reserved. |
||
16 |
* |
||
17 |
* Redistribution and use in source and binary forms, with or without |
||
18 |
* modification, are permitted provided that the following conditions |
||
19 |
* are met: |
||
20 |
* 1. Redistributions of source code must retain the above copyright |
||
21 |
* notice, this list of conditions and the following disclaimer. |
||
22 |
* 2. Redistributions in binary form must reproduce the above copyright |
||
23 |
* notice, this list of conditions and the following disclaimer in the |
||
24 |
* documentation and/or other materials provided with the distribution. |
||
25 |
* 3. Neither the name of the University nor the names of its contributors |
||
26 |
* may be used to endorse or promote products derived from this software |
||
27 |
* without specific prior written permission. |
||
28 |
* |
||
29 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
||
30 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
31 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
32 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
||
33 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
34 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
35 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
36 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
37 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
38 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
39 |
* SUCH DAMAGE. |
||
40 |
*/ |
||
41 |
|||
42 |
#include <sys/param.h> /* DEV_BSIZE MAXBSIZE */ |
||
43 |
#include <sys/types.h> |
||
44 |
#include <sys/stat.h> |
||
45 |
#include <sys/ioctl.h> |
||
46 |
#include <sys/dkio.h> |
||
47 |
#include <sys/disklabel.h> |
||
48 |
#include <sys/mount.h> |
||
49 |
#include <sys/resource.h> |
||
50 |
#include <sys/sysctl.h> |
||
51 |
#include <sys/wait.h> |
||
52 |
|||
53 |
#include <ufs/ufs/dinode.h> |
||
54 |
#include <ufs/ufs/dir.h> |
||
55 |
#include <ufs/ffs/fs.h> |
||
56 |
|||
57 |
#include <ctype.h> |
||
58 |
#include <err.h> |
||
59 |
#include <errno.h> |
||
60 |
#include <fcntl.h> |
||
61 |
#include <paths.h> |
||
62 |
#include <stdarg.h> |
||
63 |
#include <stdio.h> |
||
64 |
#include <stdlib.h> |
||
65 |
#include <string.h> |
||
66 |
#include <syslog.h> |
||
67 |
#include <unistd.h> |
||
68 |
#include <limits.h> |
||
69 |
#include <signal.h> |
||
70 |
#include <util.h> |
||
71 |
|||
72 |
#include "mntopts.h" |
||
73 |
#include "pathnames.h" |
||
74 |
|||
75 |
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) |
||
76 |
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) |
||
77 |
|||
78 |
struct mntopt mopts[] = { |
||
79 |
MOPT_STDOPTS, |
||
80 |
MOPT_WXALLOWED, |
||
81 |
MOPT_NOPERM, |
||
82 |
MOPT_ASYNC, |
||
83 |
MOPT_UPDATE, |
||
84 |
MOPT_FORCE, |
||
85 |
{ NULL }, |
||
86 |
}; |
||
87 |
|||
88 |
void fatal(const char *fmt, ...) |
||
89 |
__attribute__((__format__ (printf, 1, 2))) |
||
90 |
__attribute__((__nonnull__ (1))); |
||
91 |
__dead void usage(void); |
||
92 |
void mkfs(struct partition *, char *, int, int, mode_t, uid_t, gid_t); |
||
93 |
void getphysmem(void); |
||
94 |
void rewritelabel(char *, int, struct disklabel *); |
||
95 |
u_short dkcksum(struct disklabel *); |
||
96 |
|||
97 |
/* |
||
98 |
* The following two constants set the default block and fragment sizes. |
||
99 |
* Both constants must be a power of 2 and meet the following constraints: |
||
100 |
* MINBSIZE <= DESBLKSIZE <= MAXBSIZE |
||
101 |
* sectorsize <= DESFRAGSIZE <= DESBLKSIZE |
||
102 |
* DESBLKSIZE / DESFRAGSIZE <= 8 |
||
103 |
*/ |
||
104 |
#define DFL_FRAGSIZE 2048 |
||
105 |
#define DFL_BLKSIZE 16384 |
||
106 |
|||
107 |
/* |
||
108 |
* MAXBLKPG determines the maximum number of data blocks which are |
||
109 |
* placed in a single cylinder group. The default is one indirect |
||
110 |
* block worth of data blocks. |
||
111 |
*/ |
||
112 |
#define MAXBLKPG_FFS1(bsize) ((bsize) / sizeof(int32_t)) |
||
113 |
#define MAXBLKPG_FFS2(bsize) ((bsize) / sizeof(int64_t)) |
||
114 |
|||
115 |
/* |
||
116 |
* Each file system has a number of inodes statically allocated. |
||
117 |
* We allocate one inode slot per NFPI fragments, expecting this |
||
118 |
* to be far more than we will ever need. |
||
119 |
*/ |
||
120 |
#define NFPI 4 |
||
121 |
|||
122 |
int mfs; /* run as the memory based filesystem */ |
||
123 |
int Nflag; /* run without writing file system */ |
||
124 |
int Oflag = 1; /* 0 = 4.3BSD ffs, 1 = 4.4BSD ffs, 2 = ffs2 */ |
||
125 |
daddr_t fssize; /* file system size in 512-byte blocks */ |
||
126 |
long long sectorsize; /* bytes/sector */ |
||
127 |
int fsize = 0; /* fragment size */ |
||
128 |
int bsize = 0; /* block size */ |
||
129 |
int maxfrgspercg = INT_MAX; /* maximum fragments per cylinder group */ |
||
130 |
int minfree = MINFREE; /* free space threshold */ |
||
131 |
int opt = DEFAULTOPT; /* optimization preference (space or time) */ |
||
132 |
int reqopt = -1; /* opt preference has not been specified */ |
||
133 |
int density; /* number of bytes per inode */ |
||
134 |
int maxbpg; /* maximum blocks per file in a cyl group */ |
||
135 |
int avgfilesize = AVFILESIZ;/* expected average file size */ |
||
136 |
int avgfilesperdir = AFPDIR;/* expected number of files per directory */ |
||
137 |
int mntflags = MNT_ASYNC; /* flags to be passed to mount */ |
||
138 |
int quiet = 0; /* quiet flag */ |
||
139 |
caddr_t membase; /* start address of memory based filesystem */ |
||
140 |
char *disktype; |
||
141 |
int unlabeled; |
||
142 |
|||
143 |
extern char *__progname; |
||
144 |
struct disklabel *getdisklabel(char *, int); |
||
145 |
|||
146 |
#ifdef MFS |
||
147 |
static int do_exec(const char *, const char *, char *const[]); |
||
148 |
static int isdir(const char *); |
||
149 |
static void copy(char *, char *, struct mfs_args *); |
||
150 |
static int gettmpmnt(char *, size_t); |
||
151 |
#endif |
||
152 |
|||
153 |
int64_t physmem; |
||
154 |
|||
155 |
void |
||
156 |
getphysmem(void) |
||
157 |
{ |
||
158 |
32 |
int mib[] = { CTL_HW, HW_PHYSMEM64 }; |
|
159 |
16 |
size_t len = sizeof(physmem); |
|
160 |
|||
161 |
✗✓ | 16 |
if (sysctl(mib, 2, &physmem, &len, NULL, 0) != 0) |
162 |
err(1, "can't get physmem"); |
||
163 |
16 |
} |
|
164 |
|||
165 |
int |
||
166 |
main(int argc, char *argv[]) |
||
167 |
{ |
||
168 |
int ch; |
||
169 |
struct partition *pp; |
||
170 |
struct disklabel *lp; |
||
171 |
32 |
struct disklabel mfsfakelabel; |
|
172 |
16 |
struct partition oldpartition; |
|
173 |
16 |
struct stat st; |
|
174 |
16 |
struct statfs *mp; |
|
175 |
16 |
struct rlimit rl; |
|
176 |
int fsi = -1, oflagset = 0, fso, len, n, maxpartitions; |
||
177 |
16 |
char *cp = NULL, *s1, *s2, *special, *opstring, *realdev; |
|
178 |
#ifdef MFS |
||
179 |
16 |
char mountfromname[BUFSIZ]; |
|
180 |
16 |
char *pop = NULL, node[PATH_MAX]; |
|
181 |
pid_t pid, res; |
||
182 |
16 |
struct statfs sf; |
|
183 |
16 |
struct stat mountpoint; |
|
184 |
16 |
int status; |
|
185 |
#endif |
||
186 |
uid_t mfsuid = 0; |
||
187 |
gid_t mfsgid = 0; |
||
188 |
mode_t mfsmode = 0; |
||
189 |
char *fstype = NULL; |
||
190 |
char **saveargv = argv; |
||
191 |
int ffsflag = 1; |
||
192 |
16 |
const char *errstr; |
|
193 |
16 |
long long fssize_input = 0; |
|
194 |
int fssize_usebytes = 0; |
||
195 |
u_int64_t nsecs; |
||
196 |
|||
197 |
✗✓ | 16 |
if (strstr(__progname, "mfs")) |
198 |
mfs = Nflag = quiet = 1; |
||
199 |
|||
200 |
16 |
getphysmem(); |
|
201 |
16 |
maxpartitions = getmaxpartitions(); |
|
202 |
✗✓ | 16 |
if (maxpartitions > 26) |
203 |
fatal("insane maxpartitions value %d", maxpartitions); |
||
204 |
|||
205 |
16 |
opstring = mfs ? |
|
206 |
"P:T:b:c:e:f:i:m:o:s:" : |
||
207 |
"NO:S:T:b:c:e:f:g:h:i:m:o:qs:t:"; |
||
208 |
✓✓ | 92 |
while ((ch = getopt(argc, argv, opstring)) != -1) { |
209 |
✗✗✗✗ ✗✓✗✗ ✗✗✓✓ ✓✗✗✗ ✗✗ |
60 |
switch (ch) { |
210 |
case 'N': |
||
211 |
Nflag = 1; |
||
212 |
break; |
||
213 |
case 'O': |
||
214 |
Oflag = strtonum(optarg, 0, 2, &errstr); |
||
215 |
if (errstr) |
||
216 |
fatal("%s: invalid ffs version", optarg); |
||
217 |
oflagset = 1; |
||
218 |
break; |
||
219 |
case 'S': |
||
220 |
if (scan_scaled(optarg, §orsize) == -1 || |
||
221 |
sectorsize <= 0 || (sectorsize % DEV_BSIZE)) |
||
222 |
fatal("sector size invalid: %s", optarg); |
||
223 |
break; |
||
224 |
case 'T': |
||
225 |
disktype = optarg; |
||
226 |
break; |
||
227 |
case 'b': |
||
228 |
bsize = strtonum(optarg, MINBSIZE, MAXBSIZE, &errstr); |
||
229 |
if (errstr) |
||
230 |
fatal("block size is %s: %s", errstr, optarg); |
||
231 |
break; |
||
232 |
case 'c': |
||
233 |
12 |
maxfrgspercg = strtonum(optarg, 1, INT_MAX, &errstr); |
|
234 |
✗✓ | 12 |
if (errstr) |
235 |
fatal("fragments per cylinder group is %s: %s", |
||
236 |
errstr, optarg); |
||
237 |
break; |
||
238 |
case 'e': |
||
239 |
maxbpg = strtonum(optarg, 1, INT_MAX, &errstr); |
||
240 |
if (errstr) |
||
241 |
fatal("blocks per file in a cylinder group is" |
||
242 |
" %s: %s", errstr, optarg); |
||
243 |
break; |
||
244 |
case 'f': |
||
245 |
fsize = strtonum(optarg, MINBSIZE / MAXFRAG, MAXBSIZE, |
||
246 |
&errstr); |
||
247 |
if (errstr) |
||
248 |
fatal("fragment size is %s: %s", |
||
249 |
errstr, optarg); |
||
250 |
break; |
||
251 |
case 'g': |
||
252 |
avgfilesize = strtonum(optarg, 1, INT_MAX, &errstr); |
||
253 |
if (errstr) |
||
254 |
fatal("average file size is %s: %s", |
||
255 |
errstr, optarg); |
||
256 |
break; |
||
257 |
case 'h': |
||
258 |
avgfilesperdir = strtonum(optarg, 1, INT_MAX, &errstr); |
||
259 |
if (errstr) |
||
260 |
fatal("average files per dir is %s: %s", |
||
261 |
errstr, optarg); |
||
262 |
break; |
||
263 |
case 'i': |
||
264 |
16 |
density = strtonum(optarg, 1, INT_MAX, &errstr); |
|
265 |
✗✓ | 16 |
if (errstr) |
266 |
fatal("bytes per inode is %s: %s", |
||
267 |
errstr, optarg); |
||
268 |
break; |
||
269 |
case 'm': |
||
270 |
16 |
minfree = strtonum(optarg, 0, 99, &errstr); |
|
271 |
✗✓ | 16 |
if (errstr) |
272 |
fatal("free space %% is %s: %s", |
||
273 |
errstr, optarg); |
||
274 |
break; |
||
275 |
case 'o': |
||
276 |
✗✓ | 16 |
if (mfs) |
277 |
getmntopts(optarg, mopts, &mntflags); |
||
278 |
else { |
||
279 |
✓✗ | 16 |
if (strcmp(optarg, "space") == 0) |
280 |
16 |
reqopt = opt = FS_OPTSPACE; |
|
281 |
else if (strcmp(optarg, "time") == 0) |
||
282 |
reqopt = opt = FS_OPTTIME; |
||
283 |
else |
||
284 |
fatal("%s: unknown optimization " |
||
285 |
"preference: use `space' or `time'.", |
||
286 |
optarg); |
||
287 |
} |
||
288 |
break; |
||
289 |
case 'q': |
||
290 |
quiet = 1; |
||
291 |
break; |
||
292 |
case 's': |
||
293 |
if (scan_scaled(optarg, &fssize_input) == -1 || |
||
294 |
fssize_input <= 0) |
||
295 |
fatal("file system size invalid: %s", optarg); |
||
296 |
fssize_usebytes = 0; /* in case of multiple -s */ |
||
297 |
for (s1 = optarg; *s1 != '\0'; s1++) |
||
298 |
if (isalpha((unsigned char)*s1)) { |
||
299 |
fssize_usebytes = 1; |
||
300 |
break; |
||
301 |
} |
||
302 |
break; |
||
303 |
case 't': |
||
304 |
fstype = optarg; |
||
305 |
if (strcmp(fstype, "ffs")) |
||
306 |
ffsflag = 0; |
||
307 |
break; |
||
308 |
#ifdef MFS |
||
309 |
case 'P': |
||
310 |
pop = optarg; |
||
311 |
break; |
||
312 |
#endif |
||
313 |
case '?': |
||
314 |
default: |
||
315 |
usage(); |
||
316 |
} |
||
317 |
✓✗ | 60 |
if (!ffsflag) |
318 |
break; |
||
319 |
} |
||
320 |
16 |
argc -= optind; |
|
321 |
16 |
argv += optind; |
|
322 |
|||
323 |
✓✗✗✓ |
32 |
if (ffsflag && argc - mfs != 1) |
324 |
usage(); |
||
325 |
|||
326 |
✗✓ | 16 |
if (mfs) { |
327 |
/* Increase our data size to the max */ |
||
328 |
if (getrlimit(RLIMIT_DATA, &rl) == 0) { |
||
329 |
rl.rlim_cur = rl.rlim_max; |
||
330 |
(void)setrlimit(RLIMIT_DATA, &rl); |
||
331 |
} |
||
332 |
} |
||
333 |
|||
334 |
16 |
special = argv[0]; |
|
335 |
|||
336 |
✓✗ | 16 |
if (!mfs) { |
337 |
16 |
char execname[PATH_MAX], name[PATH_MAX]; |
|
338 |
|||
339 |
✓✗ | 16 |
if (fstype == NULL) |
340 |
16 |
fstype = readlabelfs(special, 0); |
|
341 |
✓✗✗✓ |
32 |
if (fstype != NULL && strcmp(fstype, "ffs")) { |
342 |
snprintf(name, sizeof name, "newfs_%s", fstype); |
||
343 |
saveargv[0] = name; |
||
344 |
snprintf(execname, sizeof execname, "%s/newfs_%s", |
||
345 |
_PATH_SBIN, fstype); |
||
346 |
(void)execv(execname, saveargv); |
||
347 |
snprintf(execname, sizeof execname, "%s/newfs_%s", |
||
348 |
_PATH_USRSBIN, fstype); |
||
349 |
(void)execv(execname, saveargv); |
||
350 |
err(1, "%s not found", name); |
||
351 |
} |
||
352 |
16 |
} |
|
353 |
|||
354 |
✗✓✗✗ |
16 |
if (mfs && !strcmp(special, "swap")) { |
355 |
/* |
||
356 |
* it's an MFS, mounted on "swap." fake up a label. |
||
357 |
* XXX XXX XXX |
||
358 |
*/ |
||
359 |
fso = -1; /* XXX; normally done below. */ |
||
360 |
|||
361 |
memset(&mfsfakelabel, 0, sizeof(mfsfakelabel)); |
||
362 |
mfsfakelabel.d_secsize = 512; |
||
363 |
mfsfakelabel.d_nsectors = 64; |
||
364 |
mfsfakelabel.d_ntracks = 16; |
||
365 |
mfsfakelabel.d_ncylinders = 16; |
||
366 |
mfsfakelabel.d_secpercyl = 1024; |
||
367 |
DL_SETDSIZE(&mfsfakelabel, 16384); |
||
368 |
mfsfakelabel.d_npartitions = 1; |
||
369 |
mfsfakelabel.d_version = 1; |
||
370 |
DL_SETPSIZE(&mfsfakelabel.d_partitions[0], 16384); |
||
371 |
mfsfakelabel.d_partitions[0].p_fragblock = |
||
372 |
DISKLABELV1_FFS_FRAGBLOCK(1024, 8); |
||
373 |
mfsfakelabel.d_partitions[0].p_cpg = 16; |
||
374 |
|||
375 |
lp = &mfsfakelabel; |
||
376 |
pp = &mfsfakelabel.d_partitions[0]; |
||
377 |
|||
378 |
goto havelabel; |
||
379 |
} |
||
380 |
✗✓ | 16 |
if (Nflag) { |
381 |
fso = -1; |
||
382 |
} else { |
||
383 |
16 |
fso = opendev(special, O_WRONLY, 0, &realdev); |
|
384 |
✗✓ | 16 |
if (fso < 0) |
385 |
fatal("%s: %s", special, strerror(errno)); |
||
386 |
16 |
special = realdev; |
|
387 |
|||
388 |
/* Bail if target special is mounted */ |
||
389 |
16 |
n = getmntinfo(&mp, MNT_NOWAIT); |
|
390 |
✗✓ | 16 |
if (n == 0) |
391 |
fatal("%s: getmntinfo: %s", special, strerror(errno)); |
||
392 |
|||
393 |
len = sizeof(_PATH_DEV) - 1; |
||
394 |
s1 = special; |
||
395 |
✓✗ | 16 |
if (strncmp(_PATH_DEV, s1, len) == 0) |
396 |
16 |
s1 += len; |
|
397 |
|||
398 |
✓✓ | 304 |
while (--n >= 0) { |
399 |
144 |
s2 = mp->f_mntfromname; |
|
400 |
✓✗ | 144 |
if (strncmp(_PATH_DEV, s2, len) == 0) { |
401 |
144 |
s2 += len - 1; |
|
402 |
144 |
*s2 = 'r'; |
|
403 |
144 |
} |
|
404 |
✓✗✗✓ |
288 |
if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) |
405 |
fatal("%s is mounted on %s", |
||
406 |
special, mp->f_mntonname); |
||
407 |
144 |
++mp; |
|
408 |
} |
||
409 |
} |
||
410 |
✗✓ | 16 |
if (mfs && disktype != NULL) { |
411 |
lp = (struct disklabel *)getdiskbyname(disktype); |
||
412 |
if (lp == NULL) |
||
413 |
fatal("%s: unknown disk type", disktype); |
||
414 |
pp = &lp->d_partitions[1]; |
||
415 |
} else { |
||
416 |
16 |
fsi = opendev(special, O_RDONLY, 0, NULL); |
|
417 |
✗✓ | 16 |
if (fsi < 0) |
418 |
fatal("%s: %s", special, strerror(errno)); |
||
419 |
✗✓ | 16 |
if (fstat(fsi, &st) < 0) |
420 |
fatal("%s: %s", special, strerror(errno)); |
||
421 |
✓✗ | 16 |
if (!mfs) { |
422 |
✗✓ | 16 |
if (S_ISBLK(st.st_mode)) |
423 |
fatal("%s: block device", special); |
||
424 |
✗✓ | 16 |
if (!S_ISCHR(st.st_mode)) |
425 |
warnx("%s: not a character-special device", |
||
426 |
special); |
||
427 |
} |
||
428 |
✗✓ | 16 |
if (*argv[0] == '\0') |
429 |
fatal("empty partition name supplied"); |
||
430 |
16 |
cp = argv[0] + strlen(argv[0]) - 1; |
|
431 |
✓✗✗✗ |
32 |
if ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) |
432 |
✗✓ | 16 |
&& !isdigit((unsigned char)*cp)) |
433 |
fatal("%s: can't figure out file system partition", |
||
434 |
argv[0]); |
||
435 |
16 |
lp = getdisklabel(special, fsi); |
|
436 |
✓✗ | 16 |
if (!mfs) { |
437 |
✗✓ | 16 |
if (pledge("stdio disklabel tty flock rpath cpath wpath", NULL) == -1) |
438 |
err(1, "pledge"); |
||
439 |
} |
||
440 |
✗✓ | 16 |
if (isdigit((unsigned char)*cp)) |
441 |
pp = &lp->d_partitions[0]; |
||
442 |
else |
||
443 |
16 |
pp = &lp->d_partitions[*cp - 'a']; |
|
444 |
✗✓ | 16 |
if (DL_GETPSIZE(pp) == 0) |
445 |
fatal("%s: `%c' partition is unavailable", |
||
446 |
argv[0], *cp); |
||
447 |
✗✓ | 16 |
if (pp->p_fstype == FS_BOOT) |
448 |
fatal("%s: `%c' partition overlaps boot program", |
||
449 |
argv[0], *cp); |
||
450 |
} |
||
451 |
havelabel: |
||
452 |
✓✗ | 16 |
if (sectorsize == 0) { |
453 |
16 |
sectorsize = lp->d_secsize; |
|
454 |
✗✓ | 16 |
if (sectorsize <= 0) |
455 |
fatal("%s: no default sector size", argv[0]); |
||
456 |
} |
||
457 |
|||
458 |
✗✓ | 16 |
if (fssize_usebytes) { |
459 |
nsecs = fssize_input / sectorsize; |
||
460 |
if (fssize_input % sectorsize != 0) |
||
461 |
nsecs++; |
||
462 |
✓✗ | 16 |
} else if (fssize_input == 0) |
463 |
16 |
nsecs = DL_GETPSIZE(pp); |
|
464 |
else |
||
465 |
nsecs = fssize_input; |
||
466 |
|||
467 |
✗✓ | 16 |
if (nsecs > DL_GETPSIZE(pp) && !mfs) |
468 |
fatal("%s: maximum file system size on the `%c' partition is " |
||
469 |
"%llu sectors", argv[0], *cp, DL_GETPSIZE(pp)); |
||
470 |
|||
471 |
/* Can't use DL_SECTOBLK() because sectorsize may not be from label! */ |
||
472 |
16 |
fssize = nsecs * (sectorsize / DEV_BSIZE); |
|
473 |
✗✓ | 16 |
if (oflagset == 0 && fssize >= INT_MAX) |
474 |
Oflag = 2; /* FFS2 */ |
||
475 |
✓✗ | 16 |
if (fsize == 0) { |
476 |
✓✗✓✗ ✓✗✓✗ |
144 |
fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); |
477 |
✗✓ | 16 |
if (fsize <= 0) |
478 |
fsize = MAXIMUM(DFL_FRAGSIZE, lp->d_secsize); |
||
479 |
} |
||
480 |
✓✗ | 16 |
if (bsize == 0) { |
481 |
✓✗ | 48 |
bsize = DISKLABELV1_FFS_BSIZE(pp->p_fragblock); |
482 |
✗✓ | 16 |
if (bsize <= 0) |
483 |
bsize = MINIMUM(DFL_BLKSIZE, 8 * fsize); |
||
484 |
} |
||
485 |
✗✓ | 16 |
if (density == 0) |
486 |
density = NFPI * fsize; |
||
487 |
✗✓ | 16 |
if (minfree < MINFREE && opt != FS_OPTSPACE && reqopt == -1) { |
488 |
warnx("warning: changing optimization to space " |
||
489 |
"because minfree is less than %d%%\n", MINFREE); |
||
490 |
opt = FS_OPTSPACE; |
||
491 |
} |
||
492 |
✓✗ | 16 |
if (maxbpg == 0) { |
493 |
16 |
if (Oflag <= 1) |
|
494 |
maxbpg = MAXBLKPG_FFS1(bsize); |
||
495 |
else |
||
496 |
maxbpg = MAXBLKPG_FFS2(bsize); |
||
497 |
16 |
} |
|
498 |
16 |
oldpartition = *pp; |
|
499 |
#ifdef MFS |
||
500 |
✗✓ | 16 |
if (mfs) { |
501 |
if (realpath(argv[1], node) == NULL) |
||
502 |
err(1, "realpath %s", argv[1]); |
||
503 |
if (stat(node, &mountpoint) < 0) |
||
504 |
err(ECANCELED, "stat %s", node); |
||
505 |
mfsuid = mountpoint.st_uid; |
||
506 |
mfsgid = mountpoint.st_gid; |
||
507 |
mfsmode = mountpoint.st_mode & ALLPERMS; |
||
508 |
} |
||
509 |
#endif |
||
510 |
|||
511 |
16 |
mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid); |
|
512 |
✓✗✓✗ |
32 |
if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition))) |
513 |
16 |
rewritelabel(special, fso, lp); |
|
514 |
✓✗ | 16 |
if (!Nflag) |
515 |
16 |
close(fso); |
|
516 |
16 |
close(fsi); |
|
517 |
#ifdef MFS |
||
518 |
✗✓ | 16 |
if (mfs) { |
519 |
struct mfs_args args; |
||
520 |
memset(&args, 0, sizeof(args)); |
||
521 |
args.base = membase; |
||
522 |
args.size = fssize * DEV_BSIZE; |
||
523 |
args.export_info.ex_root = -2; |
||
524 |
if (mntflags & MNT_RDONLY) |
||
525 |
args.export_info.ex_flags = MNT_EXRDONLY; |
||
526 |
if (mntflags & MNT_NOPERM) |
||
527 |
mntflags |= MNT_NODEV | MNT_NOEXEC; |
||
528 |
|||
529 |
switch (pid = fork()) { |
||
530 |
case -1: |
||
531 |
err(10, "mfs"); |
||
532 |
case 0: |
||
533 |
snprintf(mountfromname, sizeof(mountfromname), |
||
534 |
"mfs:%d", getpid()); |
||
535 |
break; |
||
536 |
default: |
||
537 |
snprintf(mountfromname, sizeof(mountfromname), |
||
538 |
"mfs:%d", pid); |
||
539 |
for (;;) { |
||
540 |
/* |
||
541 |
* spin until the mount succeeds |
||
542 |
* or the child exits |
||
543 |
*/ |
||
544 |
usleep(1); |
||
545 |
|||
546 |
/* |
||
547 |
* XXX Here is a race condition: another process |
||
548 |
* can mount a filesystem which hides our |
||
549 |
* ramdisk before we see the success. |
||
550 |
*/ |
||
551 |
if (statfs(node, &sf) < 0) |
||
552 |
err(ECANCELED, "statfs %s", node); |
||
553 |
if (!strcmp(sf.f_mntfromname, mountfromname) && |
||
554 |
!strncmp(sf.f_mntonname, node, |
||
555 |
MNAMELEN) && |
||
556 |
!strcmp(sf.f_fstypename, "mfs")) { |
||
557 |
if (pop != NULL) |
||
558 |
copy(pop, node, &args); |
||
559 |
exit(0); |
||
560 |
} |
||
561 |
res = waitpid(pid, &status, WNOHANG); |
||
562 |
if (res == -1) |
||
563 |
err(EDEADLK, "waitpid"); |
||
564 |
if (res != pid) |
||
565 |
continue; |
||
566 |
if (WIFEXITED(status)) { |
||
567 |
if (WEXITSTATUS(status) == 0) |
||
568 |
exit(0); |
||
569 |
errx(1, "%s: mount: %s", node, |
||
570 |
strerror(WEXITSTATUS(status))); |
||
571 |
} else |
||
572 |
errx(EDEADLK, "abnormal termination"); |
||
573 |
} |
||
574 |
/* NOTREACHED */ |
||
575 |
} |
||
576 |
|||
577 |
(void) setsid(); |
||
578 |
(void) close(0); |
||
579 |
(void) close(1); |
||
580 |
(void) close(2); |
||
581 |
(void) chdir("/"); |
||
582 |
|||
583 |
args.fspec = mountfromname; |
||
584 |
if (mntflags & MNT_RDONLY && pop != NULL) |
||
585 |
mntflags &= ~MNT_RDONLY; |
||
586 |
if (mount(MOUNT_MFS, node, mntflags, &args) < 0) |
||
587 |
exit(errno); /* parent prints message */ |
||
588 |
} |
||
589 |
#endif |
||
590 |
exit(0); |
||
591 |
} |
||
592 |
|||
593 |
struct disklabel * |
||
594 |
getdisklabel(char *s, int fd) |
||
595 |
{ |
||
596 |
static struct disklabel lab; |
||
597 |
|||
598 |
✗✓ | 32 |
if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { |
599 |
if (disktype != NULL) { |
||
600 |
struct disklabel *lp; |
||
601 |
|||
602 |
unlabeled++; |
||
603 |
lp = getdiskbyname(disktype); |
||
604 |
if (lp == NULL) |
||
605 |
fatal("%s: unknown disk type", disktype); |
||
606 |
return (lp); |
||
607 |
} |
||
608 |
warn("ioctl (GDINFO)"); |
||
609 |
fatal("%s: can't read disk label; disk type must be specified", |
||
610 |
s); |
||
611 |
} |
||
612 |
16 |
return (&lab); |
|
613 |
16 |
} |
|
614 |
|||
615 |
void |
||
616 |
rewritelabel(char *s, int fd, struct disklabel *lp) |
||
617 |
{ |
||
618 |
✓✗ | 32 |
if (unlabeled) |
619 |
return; |
||
620 |
|||
621 |
16 |
lp->d_checksum = 0; |
|
622 |
16 |
lp->d_checksum = dkcksum(lp); |
|
623 |
✗✓ | 16 |
if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) { |
624 |
warn("ioctl (WDINFO)"); |
||
625 |
fatal("%s: can't rewrite disk label", s); |
||
626 |
} |
||
627 |
16 |
} |
|
628 |
|||
629 |
void |
||
630 |
fatal(const char *fmt, ...) |
||
631 |
{ |
||
632 |
va_list ap; |
||
633 |
|||
634 |
va_start(ap, fmt); |
||
635 |
if (fcntl(STDERR_FILENO, F_GETFL) < 0) { |
||
636 |
openlog(__progname, LOG_CONS, LOG_DAEMON); |
||
637 |
vsyslog(LOG_ERR, fmt, ap); |
||
638 |
closelog(); |
||
639 |
} else { |
||
640 |
vwarnx(fmt, ap); |
||
641 |
} |
||
642 |
va_end(ap); |
||
643 |
exit(1); |
||
644 |
/*NOTREACHED*/ |
||
645 |
} |
||
646 |
|||
647 |
__dead void |
||
648 |
usage(void) |
||
649 |
{ |
||
650 |
extern char *__progname; |
||
651 |
|||
652 |
if (mfs) { |
||
653 |
fprintf(stderr, |
||
654 |
"usage: %s [-b block-size] [-c fragments-per-cylinder-group] " |
||
655 |
"[-e maxbpg]\n" |
||
656 |
"\t[-f frag-size] [-i bytes] [-m free-space] [-o options] " |
||
657 |
"[-P file]\n" |
||
658 |
"\t[-s size] special node\n", |
||
659 |
__progname); |
||
660 |
} else { |
||
661 |
fprintf(stderr, |
||
662 |
"usage: %s [-Nq] [-b block-size] " |
||
663 |
"[-c fragments-per-cylinder-group] [-e maxbpg]\n" |
||
664 |
"\t[-f frag-size] [-g avgfilesize] [-h avgfpdir] [-i bytes]\n" |
||
665 |
"\t[-m free-space] [-O filesystem-format] [-o optimization]\n" |
||
666 |
"\t[-S sector-size] [-s size] [-T disktype] [-t fstype] " |
||
667 |
"special\n", |
||
668 |
__progname); |
||
669 |
} |
||
670 |
|||
671 |
exit(1); |
||
672 |
} |
||
673 |
|||
674 |
#ifdef MFS |
||
675 |
|||
676 |
static int |
||
677 |
do_exec(const char *dir, const char *cmd, char *const argv[]) |
||
678 |
{ |
||
679 |
pid_t pid; |
||
680 |
int ret, status; |
||
681 |
sig_t intsave, quitsave; |
||
682 |
|||
683 |
switch (pid = fork()) { |
||
684 |
case -1: |
||
685 |
err(1, "fork"); |
||
686 |
case 0: |
||
687 |
if (dir != NULL && chdir(dir) != 0) |
||
688 |
err(1, "chdir"); |
||
689 |
if (execv(cmd, argv) != 0) |
||
690 |
err(1, "%s", cmd); |
||
691 |
break; |
||
692 |
default: |
||
693 |
intsave = signal(SIGINT, SIG_IGN); |
||
694 |
quitsave = signal(SIGQUIT, SIG_IGN); |
||
695 |
for (;;) { |
||
696 |
ret = waitpid(pid, &status, 0); |
||
697 |
if (ret == -1) |
||
698 |
err(11, "waitpid"); |
||
699 |
if (WIFEXITED(status)) { |
||
700 |
status = WEXITSTATUS(status); |
||
701 |
if (status != 0) |
||
702 |
warnx("%s: exited", cmd); |
||
703 |
break; |
||
704 |
} else if (WIFSIGNALED(status)) { |
||
705 |
warnx("%s: %s", cmd, |
||
706 |
strsignal(WTERMSIG(status))); |
||
707 |
status = 1; |
||
708 |
break; |
||
709 |
} |
||
710 |
} |
||
711 |
signal(SIGINT, intsave); |
||
712 |
signal(SIGQUIT, quitsave); |
||
713 |
return (status); |
||
714 |
} |
||
715 |
/* NOTREACHED */ |
||
716 |
return (-1); |
||
717 |
} |
||
718 |
|||
719 |
static int |
||
720 |
isdir(const char *path) |
||
721 |
{ |
||
722 |
struct stat st; |
||
723 |
|||
724 |
if (stat(path, &st) != 0) |
||
725 |
err(1, "cannot stat %s", path); |
||
726 |
if (!S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode)) |
||
727 |
errx(1, "%s: not a dir or a block device", path); |
||
728 |
return (S_ISDIR(st.st_mode)); |
||
729 |
} |
||
730 |
|||
731 |
static void |
||
732 |
copy(char *src, char *dst, struct mfs_args *args) |
||
733 |
{ |
||
734 |
int ret, dir, created = 0; |
||
735 |
struct ufs_args mount_args; |
||
736 |
char mountpoint[MNAMELEN]; |
||
737 |
char *const argv[] = { "pax", "-rw", "-pe", ".", dst, NULL } ; |
||
738 |
|||
739 |
dir = isdir(src); |
||
740 |
if (dir) |
||
741 |
strlcpy(mountpoint, src, sizeof(mountpoint)); |
||
742 |
else { |
||
743 |
created = gettmpmnt(mountpoint, sizeof(mountpoint)); |
||
744 |
memset(&mount_args, 0, sizeof(mount_args)); |
||
745 |
mount_args.fspec = src; |
||
746 |
ret = mount(MOUNT_FFS, mountpoint, MNT_RDONLY, &mount_args); |
||
747 |
if (ret != 0) { |
||
748 |
int saved_errno = errno; |
||
749 |
if (created && rmdir(mountpoint) != 0) |
||
750 |
warn("rmdir %s", mountpoint); |
||
751 |
if (unmount(dst, 0) != 0) |
||
752 |
warn("unmount %s", dst); |
||
753 |
errc(1, saved_errno, "mount %s %s", src, mountpoint); |
||
754 |
} |
||
755 |
} |
||
756 |
ret = do_exec(mountpoint, "/bin/pax", argv); |
||
757 |
if (!dir && unmount(mountpoint, 0) != 0) |
||
758 |
warn("unmount %s", mountpoint); |
||
759 |
if (created && rmdir(mountpoint) != 0) |
||
760 |
warn("rmdir %s", mountpoint); |
||
761 |
if (ret != 0) { |
||
762 |
if (unmount(dst, 0) != 0) |
||
763 |
warn("unmount %s", dst); |
||
764 |
errx(1, "copy %s to %s failed", mountpoint, dst); |
||
765 |
} |
||
766 |
|||
767 |
if (mntflags & MNT_RDONLY) { |
||
768 |
mntflags |= MNT_UPDATE; |
||
769 |
if (mount(MOUNT_MFS, dst, mntflags, args) < 0) { |
||
770 |
warn("%s: mount (update, rdonly)", dst); |
||
771 |
if (unmount(dst, 0) != 0) |
||
772 |
warn("unmount %s", dst); |
||
773 |
exit(1); |
||
774 |
} |
||
775 |
} |
||
776 |
} |
||
777 |
|||
778 |
static int |
||
779 |
gettmpmnt(char *mountpoint, size_t len) |
||
780 |
{ |
||
781 |
const char *tmp = _PATH_TMP; |
||
782 |
const char *mnt = _PATH_MNT; |
||
783 |
struct statfs fs; |
||
784 |
size_t n; |
||
785 |
|||
786 |
if (statfs(tmp, &fs) != 0) |
||
787 |
err(1, "statfs %s", tmp); |
||
788 |
if (fs.f_flags & MNT_RDONLY) { |
||
789 |
if (statfs(mnt, &fs) != 0) |
||
790 |
err(1, "statfs %s", mnt); |
||
791 |
if (strcmp(fs.f_mntonname, "/") != 0) |
||
792 |
errx(1, "tmp mountpoint %s busy", mnt); |
||
793 |
if (strlcpy(mountpoint, mnt, len) >= len) |
||
794 |
errx(1, "tmp mountpoint %s too long", mnt); |
||
795 |
return (0); |
||
796 |
} |
||
797 |
n = strlcpy(mountpoint, tmp, len); |
||
798 |
if (n >= len) |
||
799 |
errx(1, "tmp mount point too long"); |
||
800 |
if (mountpoint[n - 1] != '/') |
||
801 |
strlcat(mountpoint, "/", len); |
||
802 |
n = strlcat(mountpoint, "mntXXXXXXXXXX", len); |
||
803 |
if (n >= len) |
||
804 |
errx(1, "tmp mount point too long"); |
||
805 |
if (mkdtemp(mountpoint) == NULL) |
||
806 |
err(1, "mkdtemp %s", mountpoint); |
||
807 |
return (1); |
||
808 |
} |
||
809 |
|||
810 |
#endif /* MFS */ |
Generated by: GCOVR (Version 3.3) |