setup.c revision 1.55 1 1.55 dbj /* $NetBSD: setup.c,v 1.55 2002/11/05 05:18:50 dbj Exp $ */
2 1.19 cgd
3 1.1 cgd /*
4 1.10 mycroft * Copyright (c) 1980, 1986, 1993
5 1.10 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.28 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.19 cgd #if 0
39 1.30 lukem static char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
40 1.19 cgd #else
41 1.55 dbj __RCSID("$NetBSD: setup.c,v 1.55 2002/11/05 05:18:50 dbj Exp $");
42 1.19 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.8 cgd #include <sys/time.h>
47 1.1 cgd #include <sys/stat.h>
48 1.1 cgd #include <sys/ioctl.h>
49 1.35 christos #define FSTYPENAMES
50 1.1 cgd #include <sys/disklabel.h>
51 1.1 cgd #include <sys/file.h>
52 1.15 cgd
53 1.30 lukem #include <ufs/ufs/dinode.h>
54 1.54 dbj #include <ufs/ufs/dir.h>
55 1.33 bouyer #include <ufs/ufs/ufs_bswap.h>
56 1.30 lukem #include <ufs/ffs/fs.h>
57 1.33 bouyer #include <ufs/ffs/ffs_extern.h>
58 1.30 lukem
59 1.30 lukem #include <ctype.h>
60 1.30 lukem #include <err.h>
61 1.1 cgd #include <errno.h>
62 1.31 lukem #include <stdio.h>
63 1.31 lukem #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.26 christos
66 1.1 cgd #include "fsck.h"
67 1.15 cgd #include "extern.h"
68 1.27 christos #include "fsutil.h"
69 1.1 cgd
70 1.1 cgd #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
71 1.1 cgd
72 1.30 lukem static void badsb __P((int, char *));
73 1.34 mycroft static int calcsb __P((const char *, int, struct fs *));
74 1.34 mycroft static struct disklabel *getdisklabel __P((const char *, int));
75 1.54 dbj static struct partition *getdisklabelpart __P((const char *, struct disklabel *));
76 1.26 christos static int readsb __P((int));
77 1.54 dbj static int readappleufs __P((void));
78 1.1 cgd
79 1.30 lukem /*
80 1.30 lukem * Read in a superblock finding an alternate if necessary.
81 1.30 lukem * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
82 1.30 lukem * is already clean (preen mode only).
83 1.30 lukem */
84 1.15 cgd int
85 1.1 cgd setup(dev)
86 1.34 mycroft const char *dev;
87 1.1 cgd {
88 1.1 cgd long cg, size, asked, i, j;
89 1.1 cgd long bmapsize;
90 1.1 cgd struct disklabel *lp;
91 1.10 mycroft off_t sizepb;
92 1.10 mycroft struct stat statb;
93 1.1 cgd struct fs proto;
94 1.21 mycroft int doskipclean;
95 1.24 mycroft u_int64_t maxfilesize;
96 1.46 lukem struct csum *ccsp;
97 1.1 cgd
98 1.1 cgd havesb = 0;
99 1.10 mycroft fswritefd = -1;
100 1.21 mycroft doskipclean = skipclean;
101 1.1 cgd if (stat(dev, &statb) < 0) {
102 1.1 cgd printf("Can't stat %s: %s\n", dev, strerror(errno));
103 1.1 cgd return (0);
104 1.1 cgd }
105 1.51 lukem if (!forceimage && !S_ISCHR(statb.st_mode)) {
106 1.1 cgd pfatal("%s is not a character device", dev);
107 1.1 cgd if (reply("CONTINUE") == 0)
108 1.1 cgd return (0);
109 1.1 cgd }
110 1.1 cgd if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
111 1.1 cgd printf("Can't open %s: %s\n", dev, strerror(errno));
112 1.1 cgd return (0);
113 1.1 cgd }
114 1.1 cgd if (preen == 0)
115 1.1 cgd printf("** %s", dev);
116 1.1 cgd if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
117 1.1 cgd fswritefd = -1;
118 1.1 cgd if (preen)
119 1.1 cgd pfatal("NO WRITE ACCESS");
120 1.1 cgd printf(" (NO WRITE)");
121 1.1 cgd }
122 1.1 cgd if (preen == 0)
123 1.1 cgd printf("\n");
124 1.1 cgd fsmodified = 0;
125 1.1 cgd lfdir = 0;
126 1.1 cgd initbarea(&sblk);
127 1.1 cgd initbarea(&asblk);
128 1.1 cgd sblk.b_un.b_buf = malloc(SBSIZE);
129 1.33 bouyer sblock = malloc(SBSIZE);
130 1.1 cgd asblk.b_un.b_buf = malloc(SBSIZE);
131 1.33 bouyer altsblock = malloc(SBSIZE);
132 1.33 bouyer if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
133 1.33 bouyer sblock == NULL || altsblock == NULL)
134 1.30 lukem errx(EEXIT, "cannot allocate space for superblock");
135 1.51 lukem if (!forceimage && (lp = getdisklabel(NULL, fsreadfd)) != NULL)
136 1.1 cgd dev_bsize = secsize = lp->d_secsize;
137 1.1 cgd else
138 1.1 cgd dev_bsize = secsize = DEV_BSIZE;
139 1.1 cgd /*
140 1.1 cgd * Read in the superblock, looking for alternates if necessary
141 1.1 cgd */
142 1.1 cgd if (readsb(1) == 0) {
143 1.51 lukem if (bflag || preen || forceimage ||
144 1.51 lukem calcsb(dev, fsreadfd, &proto) == 0)
145 1.1 cgd return(0);
146 1.1 cgd if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
147 1.1 cgd return (0);
148 1.1 cgd for (cg = 0; cg < proto.fs_ncg; cg++) {
149 1.1 cgd bflag = fsbtodb(&proto, cgsblock(&proto, cg));
150 1.1 cgd if (readsb(0) != 0)
151 1.1 cgd break;
152 1.1 cgd }
153 1.1 cgd if (cg >= proto.fs_ncg) {
154 1.1 cgd printf("%s %s\n%s %s\n%s %s\n",
155 1.1 cgd "SEARCH FOR ALTERNATE SUPER-BLOCK",
156 1.1 cgd "FAILED. YOU MUST USE THE",
157 1.43 hubertf "-b OPTION TO fsck_ffs TO SPECIFY THE",
158 1.1 cgd "LOCATION OF AN ALTERNATE",
159 1.1 cgd "SUPER-BLOCK TO SUPPLY NEEDED",
160 1.23 cgd "INFORMATION; SEE fsck_ffs(8).");
161 1.1 cgd return(0);
162 1.1 cgd }
163 1.21 mycroft doskipclean = 0;
164 1.1 cgd pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
165 1.1 cgd }
166 1.21 mycroft if (debug)
167 1.33 bouyer printf("clean = %d\n", sblock->fs_clean);
168 1.33 bouyer if (doswap)
169 1.33 bouyer doskipclean = 0;
170 1.33 bouyer if (sblock->fs_clean & FS_ISCLEAN) {
171 1.22 cgd if (doskipclean) {
172 1.22 cgd pwarn("%sile system is clean; not checking\n",
173 1.22 cgd preen ? "f" : "** F");
174 1.21 mycroft return (-1);
175 1.21 mycroft }
176 1.33 bouyer if (!preen && !doswap)
177 1.21 mycroft pwarn("** File system is already clean\n");
178 1.21 mycroft }
179 1.33 bouyer maxfsblock = sblock->fs_size;
180 1.33 bouyer maxino = sblock->fs_ncg * sblock->fs_ipg;
181 1.33 bouyer sizepb = sblock->fs_bsize;
182 1.33 bouyer maxfilesize = sblock->fs_bsize * NDADDR - 1;
183 1.24 mycroft for (i = 0; i < NIADDR; i++) {
184 1.33 bouyer sizepb *= NINDIR(sblock);
185 1.24 mycroft maxfilesize += sizepb;
186 1.24 mycroft }
187 1.1 cgd /*
188 1.1 cgd * Check and potentially fix certain fields in the super block.
189 1.1 cgd */
190 1.33 bouyer if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
191 1.1 cgd pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
192 1.1 cgd if (reply("SET TO DEFAULT") == 1) {
193 1.33 bouyer sblock->fs_optim = FS_OPTTIME;
194 1.1 cgd sbdirty();
195 1.1 cgd }
196 1.1 cgd }
197 1.33 bouyer if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
198 1.1 cgd pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
199 1.33 bouyer sblock->fs_minfree);
200 1.1 cgd if (reply("SET TO DEFAULT") == 1) {
201 1.33 bouyer sblock->fs_minfree = 10;
202 1.1 cgd sbdirty();
203 1.1 cgd }
204 1.1 cgd }
205 1.53 dbj if (sblock->fs_postblformat != FS_42POSTBLFMT &&
206 1.53 dbj (sblock->fs_interleave < 1 ||
207 1.53 dbj sblock->fs_interleave > sblock->fs_nsect)) {
208 1.1 cgd pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
209 1.33 bouyer sblock->fs_interleave);
210 1.33 bouyer sblock->fs_interleave = 1;
211 1.1 cgd if (preen)
212 1.1 cgd printf(" (FIXED)\n");
213 1.1 cgd if (preen || reply("SET TO DEFAULT") == 1) {
214 1.1 cgd sbdirty();
215 1.1 cgd dirty(&asblk);
216 1.1 cgd }
217 1.1 cgd }
218 1.53 dbj if (sblock->fs_postblformat != FS_42POSTBLFMT &&
219 1.53 dbj (sblock->fs_npsect < sblock->fs_nsect ||
220 1.53 dbj sblock->fs_npsect > sblock->fs_nsect*2)) {
221 1.1 cgd pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
222 1.33 bouyer sblock->fs_npsect);
223 1.33 bouyer sblock->fs_npsect = sblock->fs_nsect;
224 1.1 cgd if (preen)
225 1.1 cgd printf(" (FIXED)\n");
226 1.1 cgd if (preen || reply("SET TO DEFAULT") == 1) {
227 1.1 cgd sbdirty();
228 1.1 cgd dirty(&asblk);
229 1.1 cgd }
230 1.1 cgd }
231 1.33 bouyer if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
232 1.28 lukem pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
233 1.33 bouyer sblock->fs_bmask);
234 1.33 bouyer sblock->fs_bmask = ~(sblock->fs_bsize - 1);
235 1.24 mycroft if (preen)
236 1.24 mycroft printf(" (FIXED)\n");
237 1.24 mycroft if (preen || reply("FIX") == 1) {
238 1.24 mycroft sbdirty();
239 1.24 mycroft dirty(&asblk);
240 1.24 mycroft }
241 1.24 mycroft }
242 1.33 bouyer if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
243 1.28 lukem pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
244 1.33 bouyer sblock->fs_fmask);
245 1.33 bouyer sblock->fs_fmask = ~(sblock->fs_fsize - 1);
246 1.24 mycroft if (preen)
247 1.24 mycroft printf(" (FIXED)\n");
248 1.24 mycroft if (preen || reply("FIX") == 1) {
249 1.24 mycroft sbdirty();
250 1.24 mycroft dirty(&asblk);
251 1.24 mycroft }
252 1.24 mycroft }
253 1.33 bouyer if (sblock->fs_inodefmt >= FS_44INODEFMT) {
254 1.33 bouyer if (sblock->fs_maxfilesize != maxfilesize) {
255 1.38 lukem pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
256 1.33 bouyer (unsigned long long)sblock->fs_maxfilesize);
257 1.33 bouyer sblock->fs_maxfilesize = maxfilesize;
258 1.24 mycroft if (preen)
259 1.24 mycroft printf(" (FIXED)\n");
260 1.24 mycroft if (preen || reply("FIX") == 1) {
261 1.24 mycroft sbdirty();
262 1.24 mycroft dirty(&asblk);
263 1.24 mycroft }
264 1.24 mycroft }
265 1.33 bouyer if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
266 1.24 mycroft pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
267 1.33 bouyer sblock->fs_maxsymlinklen);
268 1.33 bouyer sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
269 1.24 mycroft if (preen)
270 1.24 mycroft printf(" (FIXED)\n");
271 1.24 mycroft if (preen || reply("FIX") == 1) {
272 1.24 mycroft sbdirty();
273 1.24 mycroft dirty(&asblk);
274 1.24 mycroft }
275 1.24 mycroft }
276 1.33 bouyer if (sblock->fs_qbmask != ~sblock->fs_bmask) {
277 1.38 lukem pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
278 1.33 bouyer (unsigned long long)sblock->fs_qbmask);
279 1.33 bouyer sblock->fs_qbmask = ~sblock->fs_bmask;
280 1.24 mycroft if (preen)
281 1.24 mycroft printf(" (FIXED)\n");
282 1.24 mycroft if (preen || reply("FIX") == 1) {
283 1.24 mycroft sbdirty();
284 1.24 mycroft dirty(&asblk);
285 1.24 mycroft }
286 1.24 mycroft }
287 1.33 bouyer if (sblock->fs_qfmask != ~sblock->fs_fmask) {
288 1.38 lukem pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
289 1.33 bouyer (unsigned long long)sblock->fs_qfmask);
290 1.33 bouyer sblock->fs_qfmask = ~sblock->fs_fmask;
291 1.24 mycroft if (preen)
292 1.24 mycroft printf(" (FIXED)\n");
293 1.24 mycroft if (preen || reply("FIX") == 1) {
294 1.24 mycroft sbdirty();
295 1.24 mycroft dirty(&asblk);
296 1.24 mycroft }
297 1.24 mycroft }
298 1.10 mycroft newinofmt = 1;
299 1.10 mycroft } else {
300 1.33 bouyer sblock->fs_qbmask = ~sblock->fs_bmask;
301 1.33 bouyer sblock->fs_qfmask = ~sblock->fs_fmask;
302 1.10 mycroft newinofmt = 0;
303 1.10 mycroft }
304 1.10 mycroft /*
305 1.10 mycroft * Convert to new inode format.
306 1.10 mycroft */
307 1.33 bouyer if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
308 1.10 mycroft if (preen)
309 1.10 mycroft pwarn("CONVERTING TO NEW INODE FORMAT\n");
310 1.10 mycroft else if (!reply("CONVERT TO NEW INODE FORMAT"))
311 1.10 mycroft return(0);
312 1.10 mycroft doinglevel2++;
313 1.33 bouyer sblock->fs_inodefmt = FS_44INODEFMT;
314 1.33 bouyer sblock->fs_maxfilesize = maxfilesize;
315 1.33 bouyer sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
316 1.33 bouyer sblock->fs_qbmask = ~sblock->fs_bmask;
317 1.33 bouyer sblock->fs_qfmask = ~sblock->fs_fmask;
318 1.10 mycroft sbdirty();
319 1.10 mycroft dirty(&asblk);
320 1.10 mycroft }
321 1.10 mycroft /*
322 1.10 mycroft * Convert to new cylinder group format.
323 1.10 mycroft */
324 1.33 bouyer if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
325 1.10 mycroft if (preen)
326 1.10 mycroft pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
327 1.10 mycroft else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
328 1.10 mycroft return(0);
329 1.10 mycroft doinglevel1++;
330 1.33 bouyer sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
331 1.33 bouyer sblock->fs_nrpos = 8;
332 1.53 dbj if (sblock->fs_npsect < sblock->fs_nsect)
333 1.53 dbj sblock->fs_npsect = sblock->fs_nsect;
334 1.53 dbj if (sblock->fs_interleave < 1)
335 1.53 dbj sblock->fs_interleave = 1;
336 1.33 bouyer sblock->fs_postbloff =
337 1.33 bouyer (char *)(&sblock->fs_opostbl[0][0]) -
338 1.33 bouyer (char *)(&sblock->fs_firstfield);
339 1.33 bouyer sblock->fs_rotbloff = &sblock->fs_space[0] -
340 1.33 bouyer (u_char *)(&sblock->fs_firstfield);
341 1.33 bouyer sblock->fs_cgsize =
342 1.33 bouyer fragroundup(sblock, CGSIZE(sblock));
343 1.10 mycroft sbdirty();
344 1.10 mycroft dirty(&asblk);
345 1.1 cgd }
346 1.11 ws if (asblk.b_dirty && !bflag) {
347 1.33 bouyer memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
348 1.33 bouyer if (needswap)
349 1.45 lukem ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs);
350 1.33 bouyer memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
351 1.1 cgd flush(fswritefd, &asblk);
352 1.1 cgd }
353 1.1 cgd /*
354 1.1 cgd * read in the summary info.
355 1.1 cgd */
356 1.1 cgd asked = 0;
357 1.46 lukem sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
358 1.33 bouyer for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
359 1.33 bouyer size = sblock->fs_cssize - i < sblock->fs_bsize ?
360 1.33 bouyer sblock->fs_cssize - i : sblock->fs_bsize;
361 1.46 lukem ccsp = (struct csum *)((char *)sblock->fs_csp + i);
362 1.46 lukem if (bread(fsreadfd, (char *)ccsp,
363 1.33 bouyer fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
364 1.1 cgd size) != 0 && !asked) {
365 1.1 cgd pfatal("BAD SUMMARY INFORMATION");
366 1.37 fvdl if (reply("CONTINUE") == 0) {
367 1.37 fvdl markclean = 0;
368 1.30 lukem exit(EEXIT);
369 1.37 fvdl }
370 1.1 cgd asked++;
371 1.1 cgd }
372 1.33 bouyer if (doswap) {
373 1.46 lukem ffs_csum_swap(ccsp, ccsp, size);
374 1.46 lukem bwrite(fswritefd, (char *)ccsp,
375 1.37 fvdl fsbtodb(sblock,
376 1.37 fvdl sblock->fs_csaddr + j * sblock->fs_frag),
377 1.37 fvdl size);
378 1.33 bouyer }
379 1.46 lukem if (needswap)
380 1.46 lukem ffs_csum_swap(ccsp, ccsp, size);
381 1.1 cgd }
382 1.1 cgd /*
383 1.1 cgd * allocate and initialize the necessary maps
384 1.1 cgd */
385 1.20 cgd bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
386 1.1 cgd blockmap = calloc((unsigned)bmapsize, sizeof (char));
387 1.1 cgd if (blockmap == NULL) {
388 1.1 cgd printf("cannot alloc %u bytes for blockmap\n",
389 1.1 cgd (unsigned)bmapsize);
390 1.15 cgd goto badsblabel;
391 1.1 cgd }
392 1.1 cgd statemap = calloc((unsigned)(maxino + 1), sizeof(char));
393 1.1 cgd if (statemap == NULL) {
394 1.1 cgd printf("cannot alloc %u bytes for statemap\n",
395 1.1 cgd (unsigned)(maxino + 1));
396 1.15 cgd goto badsblabel;
397 1.1 cgd }
398 1.10 mycroft typemap = calloc((unsigned)(maxino + 1), sizeof(char));
399 1.10 mycroft if (typemap == NULL) {
400 1.10 mycroft printf("cannot alloc %u bytes for typemap\n",
401 1.10 mycroft (unsigned)(maxino + 1));
402 1.15 cgd goto badsblabel;
403 1.10 mycroft }
404 1.20 cgd lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
405 1.1 cgd if (lncntp == NULL) {
406 1.1 cgd printf("cannot alloc %u bytes for lncntp\n",
407 1.29 mrg (unsigned)((maxino + 1) * sizeof(int16_t)));
408 1.15 cgd goto badsblabel;
409 1.1 cgd }
410 1.39 mycroft /*
411 1.39 mycroft * cs_ndir may be inaccurate, particularly if we're using the -b
412 1.39 mycroft * option, so set a minimum to prevent bogus subdirectory reconnects
413 1.39 mycroft * and really inefficient directory scans.
414 1.39 mycroft * Also set a maximum in case the value is too large.
415 1.39 mycroft */
416 1.33 bouyer numdirs = sblock->fs_cstotal.cs_ndir;
417 1.39 mycroft if (numdirs < 1024)
418 1.39 mycroft numdirs = 1024;
419 1.39 mycroft if (numdirs > maxino + 1)
420 1.39 mycroft numdirs = maxino + 1;
421 1.1 cgd inplast = 0;
422 1.1 cgd listmax = numdirs + 10;
423 1.1 cgd inpsort = (struct inoinfo **)calloc((unsigned)listmax,
424 1.1 cgd sizeof(struct inoinfo *));
425 1.1 cgd inphead = (struct inoinfo **)calloc((unsigned)numdirs,
426 1.1 cgd sizeof(struct inoinfo *));
427 1.1 cgd if (inpsort == NULL || inphead == NULL) {
428 1.1 cgd printf("cannot alloc %u bytes for inphead\n",
429 1.29 mrg (unsigned)(numdirs * sizeof(struct inoinfo *)));
430 1.15 cgd goto badsblabel;
431 1.1 cgd }
432 1.37 fvdl cgrp = malloc(sblock->fs_cgsize);
433 1.37 fvdl if (cgrp == NULL) {
434 1.37 fvdl printf("cannot alloc %u bytes for cylinder group\n",
435 1.37 fvdl sblock->fs_cgsize);
436 1.37 fvdl goto badsblabel;
437 1.37 fvdl }
438 1.1 cgd bufinit();
439 1.37 fvdl if (sblock->fs_flags & FS_DOSOFTDEP)
440 1.37 fvdl usedsoftdep = 1;
441 1.37 fvdl else
442 1.37 fvdl usedsoftdep = 0;
443 1.54 dbj
444 1.54 dbj {
445 1.54 dbj struct partition *pp = 0;
446 1.55 dbj if (!forceimage && lp)
447 1.54 dbj pp = getdisklabelpart(dev,lp);
448 1.54 dbj if (pp && (pp->p_fstype == FS_APPLEUFS)) {
449 1.54 dbj isappleufs = 1;
450 1.54 dbj }
451 1.54 dbj }
452 1.54 dbj if (readappleufs()) {
453 1.54 dbj isappleufs = 1;
454 1.54 dbj }
455 1.54 dbj
456 1.54 dbj dirblksiz = DIRBLKSIZ;
457 1.54 dbj if (isappleufs)
458 1.54 dbj dirblksiz = APPLEUFS_DIRBLKSIZ;
459 1.54 dbj
460 1.54 dbj if (debug)
461 1.54 dbj printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
462 1.54 dbj
463 1.1 cgd return (1);
464 1.1 cgd
465 1.15 cgd badsblabel:
466 1.33 bouyer markclean=0;
467 1.33 bouyer ckfini();
468 1.1 cgd return (0);
469 1.1 cgd }
470 1.1 cgd
471 1.54 dbj static int
472 1.54 dbj readappleufs()
473 1.54 dbj {
474 1.54 dbj ufs_daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
475 1.54 dbj struct appleufslabel *appleufs;
476 1.54 dbj int i;
477 1.54 dbj
478 1.54 dbj /* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
479 1.54 dbj * being block aligned (CD's?)
480 1.54 dbj */
481 1.54 dbj if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label, (long)APPLEUFS_LABEL_SIZE) != 0)
482 1.54 dbj return 0;
483 1.54 dbj appleufsblk.b_bno = label;
484 1.54 dbj appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
485 1.54 dbj
486 1.54 dbj appleufs = appleufsblk.b_un.b_appleufs;
487 1.54 dbj
488 1.54 dbj if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
489 1.54 dbj if (!isappleufs) {
490 1.54 dbj return 0;
491 1.54 dbj } else {
492 1.54 dbj pfatal("MISSING APPLEUFS VOLUME LABEL\n");
493 1.54 dbj if (reply("FIX") == 0) {
494 1.54 dbj return 1;
495 1.54 dbj }
496 1.54 dbj ffs_appleufs_set(appleufs,NULL,-1);
497 1.54 dbj appleufsdirty();
498 1.54 dbj }
499 1.54 dbj }
500 1.54 dbj
501 1.54 dbj if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
502 1.54 dbj pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
503 1.54 dbj ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
504 1.54 dbj if (preen) {
505 1.54 dbj printf(" (CORRECTED)\n");
506 1.54 dbj }
507 1.54 dbj if (preen || reply("CORRECT")) {
508 1.54 dbj appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
509 1.54 dbj appleufsdirty();
510 1.54 dbj }
511 1.54 dbj }
512 1.54 dbj
513 1.54 dbj if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
514 1.54 dbj pwarn("APPLE UFS LABEL NAME TOO LONG");
515 1.54 dbj if (preen) {
516 1.54 dbj printf(" (TRUNCATED)\n");
517 1.54 dbj }
518 1.54 dbj if (preen || reply("TRUNCATE")) {
519 1.54 dbj appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
520 1.54 dbj appleufsdirty();
521 1.54 dbj }
522 1.54 dbj }
523 1.54 dbj
524 1.54 dbj if (ntohs(appleufs->ul_namelen) == 0) {
525 1.54 dbj pwarn("MISSING APPLE UFS LABEL NAME");
526 1.54 dbj if (preen) {
527 1.54 dbj printf(" (FIXED)\n");
528 1.54 dbj }
529 1.54 dbj if (preen || reply("FIX")) {
530 1.54 dbj ffs_appleufs_set(appleufs,NULL,-1);
531 1.54 dbj appleufsdirty();
532 1.54 dbj }
533 1.54 dbj }
534 1.54 dbj
535 1.54 dbj /* Scan name for first illegal character */
536 1.54 dbj for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
537 1.54 dbj if ((appleufs->ul_name[i] == '\0') ||
538 1.54 dbj (appleufs->ul_name[i] == ':') ||
539 1.54 dbj (appleufs->ul_name[i] == '/')) {
540 1.54 dbj pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
541 1.54 dbj if (preen) {
542 1.54 dbj printf(" (TRUNCATED)\n");
543 1.54 dbj }
544 1.54 dbj if (preen || reply("TRUNCATE")) {
545 1.54 dbj appleufs->ul_namelen = i+1;
546 1.54 dbj appleufsdirty();
547 1.54 dbj }
548 1.54 dbj break;
549 1.54 dbj }
550 1.54 dbj }
551 1.54 dbj
552 1.54 dbj /* Check the checksum last, because if anything else was wrong,
553 1.54 dbj * then the checksum gets reset anyway.
554 1.54 dbj */
555 1.54 dbj appleufs->ul_checksum = 0;
556 1.54 dbj appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
557 1.54 dbj if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
558 1.54 dbj pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
559 1.54 dbj appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
560 1.54 dbj if (preen) {
561 1.54 dbj printf(" (CORRECTED)\n");
562 1.54 dbj }
563 1.54 dbj if (preen || reply("CORRECT")) {
564 1.54 dbj appleufsdirty();
565 1.54 dbj } else {
566 1.54 dbj /* put the incorrect checksum back in place */
567 1.54 dbj appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
568 1.54 dbj }
569 1.54 dbj }
570 1.54 dbj return 1;
571 1.54 dbj }
572 1.54 dbj
573 1.1 cgd /*
574 1.1 cgd * Read in the super block and its summary info.
575 1.1 cgd */
576 1.26 christos static int
577 1.1 cgd readsb(listerr)
578 1.1 cgd int listerr;
579 1.1 cgd {
580 1.30 lukem ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
581 1.33 bouyer struct fs *fs;
582 1.1 cgd
583 1.33 bouyer if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
584 1.1 cgd return (0);
585 1.1 cgd sblk.b_bno = super;
586 1.1 cgd sblk.b_size = SBSIZE;
587 1.33 bouyer
588 1.33 bouyer fs = sblk.b_un.b_fs;
589 1.33 bouyer /* auto detect byte order */
590 1.33 bouyer if( fs->fs_magic == FS_MAGIC) {
591 1.33 bouyer if (endian == 0 || BYTE_ORDER == endian) {
592 1.33 bouyer needswap = 0;
593 1.33 bouyer doswap = do_blkswap = do_dirswap = 0;
594 1.33 bouyer } else {
595 1.33 bouyer needswap = 1;
596 1.33 bouyer doswap = do_blkswap = do_dirswap = 1;
597 1.33 bouyer }
598 1.33 bouyer } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
599 1.33 bouyer if (endian == 0 || BYTE_ORDER != endian) {
600 1.33 bouyer needswap = 1;
601 1.33 bouyer doswap = do_blkswap = do_dirswap = 0;
602 1.33 bouyer } else {
603 1.33 bouyer needswap = 0;
604 1.33 bouyer doswap = do_blkswap = do_dirswap = 1;
605 1.33 bouyer }
606 1.33 bouyer } else {
607 1.33 bouyer badsb(listerr, "MAGIC NUMBER WRONG");
608 1.33 bouyer return (0);
609 1.33 bouyer }
610 1.33 bouyer if (doswap) {
611 1.33 bouyer if (preen)
612 1.33 bouyer errx(EEXIT, "incompatible options -B and -p");
613 1.33 bouyer if (nflag)
614 1.33 bouyer errx(EEXIT, "incompatible options -B and -n");
615 1.33 bouyer if (endian == LITTLE_ENDIAN) {
616 1.36 is if (!reply("CONVERT TO LITTLE ENDIAN"))
617 1.33 bouyer return 0;
618 1.33 bouyer } else if (endian == BIG_ENDIAN) {
619 1.36 is if (!reply("CONVERT TO BIG ENDIAN"))
620 1.33 bouyer return 0;
621 1.33 bouyer } else
622 1.33 bouyer pfatal("INTERNAL ERROR: unknown endian");
623 1.33 bouyer }
624 1.33 bouyer if (needswap)
625 1.33 bouyer printf("** Swapped byte order\n");
626 1.33 bouyer /* swap SB byte order if asked */
627 1.33 bouyer if (doswap)
628 1.45 lukem ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
629 1.33 bouyer
630 1.33 bouyer memmove(sblock, sblk.b_un.b_fs, SBSIZE);
631 1.33 bouyer if (needswap)
632 1.45 lukem ffs_sb_swap(sblk.b_un.b_fs, sblock);
633 1.33 bouyer
634 1.1 cgd /*
635 1.1 cgd * run a few consistency checks of the super block
636 1.1 cgd */
637 1.33 bouyer if (sblock->fs_ncg < 1)
638 1.1 cgd { badsb(listerr, "NCG OUT OF RANGE"); return (0); }
639 1.33 bouyer if (sblock->fs_cpg < 1)
640 1.1 cgd { badsb(listerr, "CPG OUT OF RANGE"); return (0); }
641 1.33 bouyer if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
642 1.33 bouyer (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
643 1.1 cgd { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
644 1.33 bouyer if (sblock->fs_sbsize > SBSIZE)
645 1.1 cgd { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
646 1.1 cgd /*
647 1.1 cgd * Compute block size that the filesystem is based on,
648 1.1 cgd * according to fsbtodb, and adjust superblock block number
649 1.1 cgd * so we can tell if this is an alternate later.
650 1.1 cgd */
651 1.1 cgd super *= dev_bsize;
652 1.33 bouyer dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
653 1.1 cgd sblk.b_bno = super / dev_bsize;
654 1.33 bouyer
655 1.10 mycroft if (bflag) {
656 1.10 mycroft havesb = 1;
657 1.10 mycroft return (1);
658 1.10 mycroft }
659 1.1 cgd /*
660 1.1 cgd * Set all possible fields that could differ, then do check
661 1.1 cgd * of whole super block against an alternate super block.
662 1.1 cgd * When an alternate super-block is specified this check is skipped.
663 1.1 cgd */
664 1.33 bouyer getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
665 1.1 cgd if (asblk.b_errs)
666 1.1 cgd return (0);
667 1.33 bouyer /* swap SB byte order if asked */
668 1.33 bouyer if (doswap)
669 1.45 lukem ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
670 1.33 bouyer
671 1.33 bouyer memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
672 1.33 bouyer if (needswap)
673 1.45 lukem ffs_sb_swap(asblk.b_un.b_fs, altsblock);
674 1.41 thorpej if (cmpsblks(sblock, altsblock)) {
675 1.18 mycroft if (debug) {
676 1.18 mycroft long *nlp, *olp, *endlp;
677 1.18 mycroft
678 1.18 mycroft printf("superblock mismatches\n");
679 1.33 bouyer nlp = (long *)altsblock;
680 1.33 bouyer olp = (long *)sblock;
681 1.33 bouyer endlp = olp + (sblock->fs_sbsize / sizeof *olp);
682 1.18 mycroft for ( ; olp < endlp; olp++, nlp++) {
683 1.18 mycroft if (*olp == *nlp)
684 1.18 mycroft continue;
685 1.33 bouyer printf("offset %ld, original %lx, alternate %lx\n",
686 1.33 bouyer (long)(olp - (long *)sblock), *olp, *nlp);
687 1.18 mycroft }
688 1.18 mycroft }
689 1.1 cgd badsb(listerr,
690 1.1 cgd "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
691 1.1 cgd return (0);
692 1.1 cgd }
693 1.33 bouyer /* Now we know the SB is valid, we can write it back if needed */
694 1.33 bouyer if (doswap) {
695 1.33 bouyer sbdirty();
696 1.33 bouyer dirty(&asblk);
697 1.33 bouyer }
698 1.1 cgd havesb = 1;
699 1.1 cgd return (1);
700 1.41 thorpej }
701 1.41 thorpej
702 1.41 thorpej int
703 1.41 thorpej cmpsblks(const struct fs *sb, struct fs *asb)
704 1.41 thorpej {
705 1.41 thorpej
706 1.50 lukem /*
707 1.50 lukem * Copy fields which we don't care if they're different in the
708 1.50 lukem * alternate superblocks, as they're either likely to be
709 1.50 lukem * different because they're per-cylinder-group specific, or
710 1.50 lukem * because they're transient details which are only maintained
711 1.50 lukem * in the primary superblock.
712 1.50 lukem */
713 1.41 thorpej asb->fs_firstfield = sb->fs_firstfield;
714 1.41 thorpej asb->fs_unused_1 = sb->fs_unused_1;
715 1.41 thorpej asb->fs_time = sb->fs_time;
716 1.41 thorpej asb->fs_cstotal = sb->fs_cstotal;
717 1.41 thorpej asb->fs_cgrotor = sb->fs_cgrotor;
718 1.41 thorpej asb->fs_fmod = sb->fs_fmod;
719 1.41 thorpej asb->fs_clean = sb->fs_clean;
720 1.41 thorpej asb->fs_ronly = sb->fs_ronly;
721 1.41 thorpej asb->fs_flags = sb->fs_flags;
722 1.41 thorpej asb->fs_maxcontig = sb->fs_maxcontig;
723 1.41 thorpej asb->fs_minfree = sb->fs_minfree;
724 1.41 thorpej asb->fs_optim = sb->fs_optim;
725 1.41 thorpej asb->fs_rotdelay = sb->fs_rotdelay;
726 1.41 thorpej asb->fs_maxbpg = sb->fs_maxbpg;
727 1.46 lukem memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
728 1.49 lukem asb->fs_contigdirs = sb->fs_contigdirs;
729 1.46 lukem asb->fs_csp = sb->fs_csp;
730 1.41 thorpej asb->fs_maxcluster = sb->fs_maxcluster;
731 1.41 thorpej memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
732 1.49 lukem memmove(asb->fs_snapinum,
733 1.49 lukem sb->fs_snapinum, sizeof sb->fs_snapinum);
734 1.49 lukem asb->fs_avgfilesize = sb->fs_avgfilesize;
735 1.49 lukem asb->fs_avgfpdir = sb->fs_avgfpdir;
736 1.52 fvdl asb->fs_pendingblocks = sb->fs_pendingblocks;
737 1.52 fvdl asb->fs_pendinginodes = sb->fs_pendinginodes;
738 1.41 thorpej memmove(asb->fs_sparecon,
739 1.41 thorpej sb->fs_sparecon, sizeof sb->fs_sparecon);
740 1.41 thorpej /*
741 1.50 lukem * The following should not have to be copied, but need to be.
742 1.41 thorpej */
743 1.41 thorpej asb->fs_fsbtodb = sb->fs_fsbtodb;
744 1.41 thorpej asb->fs_interleave = sb->fs_interleave;
745 1.41 thorpej asb->fs_npsect = sb->fs_npsect;
746 1.41 thorpej asb->fs_nrpos = sb->fs_nrpos;
747 1.41 thorpej asb->fs_qbmask = sb->fs_qbmask;
748 1.41 thorpej asb->fs_qfmask = sb->fs_qfmask;
749 1.41 thorpej asb->fs_state = sb->fs_state;
750 1.41 thorpej asb->fs_maxfilesize = sb->fs_maxfilesize;
751 1.41 thorpej
752 1.50 lukem /*
753 1.50 lukem * Compare the superblocks, effectively checking every other
754 1.50 lukem * field to see if they differ.
755 1.50 lukem */
756 1.41 thorpej return (memcmp(sb, asb, (int)sb->fs_sbsize));
757 1.1 cgd }
758 1.1 cgd
759 1.30 lukem static void
760 1.1 cgd badsb(listerr, s)
761 1.1 cgd int listerr;
762 1.1 cgd char *s;
763 1.1 cgd {
764 1.1 cgd
765 1.1 cgd if (!listerr)
766 1.1 cgd return;
767 1.1 cgd if (preen)
768 1.26 christos printf("%s: ", cdevname());
769 1.1 cgd pfatal("BAD SUPER BLOCK: %s\n", s);
770 1.1 cgd }
771 1.1 cgd
772 1.1 cgd /*
773 1.1 cgd * Calculate a prototype superblock based on information in the disk label.
774 1.1 cgd * When done the cgsblock macro can be calculated and the fs_ncg field
775 1.1 cgd * can be used. Do NOT attempt to use other macros without verifying that
776 1.1 cgd * their needed information is available!
777 1.1 cgd */
778 1.30 lukem static int
779 1.1 cgd calcsb(dev, devfd, fs)
780 1.34 mycroft const char *dev;
781 1.1 cgd int devfd;
782 1.28 lukem struct fs *fs;
783 1.1 cgd {
784 1.28 lukem struct disklabel *lp;
785 1.28 lukem struct partition *pp;
786 1.1 cgd int i;
787 1.1 cgd
788 1.54 dbj lp = getdisklabel(dev, devfd);
789 1.54 dbj pp = getdisklabelpart(dev,lp);
790 1.54 dbj if (pp == 0) {
791 1.1 cgd pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
792 1.1 cgd return (0);
793 1.1 cgd }
794 1.54 dbj if ((pp->p_fstype != FS_BSDFFS) && (pp->p_fstype != FS_APPLEUFS)) {
795 1.1 cgd pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
796 1.1 cgd dev, pp->p_fstype < FSMAXTYPES ?
797 1.1 cgd fstypenames[pp->p_fstype] : "unknown");
798 1.1 cgd return (0);
799 1.1 cgd }
800 1.33 bouyer /* avoid divide by 0 */
801 1.33 bouyer if (pp->p_fsize == 0 || pp->p_frag == 0)
802 1.33 bouyer return (0);
803 1.12 mycroft memset(fs, 0, sizeof(struct fs));
804 1.1 cgd fs->fs_fsize = pp->p_fsize;
805 1.1 cgd fs->fs_frag = pp->p_frag;
806 1.1 cgd fs->fs_cpg = pp->p_cpg;
807 1.1 cgd fs->fs_size = pp->p_size;
808 1.1 cgd fs->fs_ntrak = lp->d_ntracks;
809 1.1 cgd fs->fs_nsect = lp->d_nsectors;
810 1.1 cgd fs->fs_spc = lp->d_secpercyl;
811 1.1 cgd fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
812 1.1 cgd fs->fs_sblkno = roundup(
813 1.1 cgd howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
814 1.1 cgd fs->fs_frag);
815 1.1 cgd fs->fs_cgmask = 0xffffffff;
816 1.1 cgd for (i = fs->fs_ntrak; i > 1; i >>= 1)
817 1.1 cgd fs->fs_cgmask <<= 1;
818 1.1 cgd if (!POWEROF2(fs->fs_ntrak))
819 1.1 cgd fs->fs_cgmask <<= 1;
820 1.1 cgd fs->fs_cgoffset = roundup(
821 1.1 cgd howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
822 1.1 cgd fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
823 1.1 cgd fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
824 1.1 cgd for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
825 1.1 cgd fs->fs_fsbtodb++;
826 1.1 cgd dev_bsize = lp->d_secsize;
827 1.1 cgd return (1);
828 1.1 cgd }
829 1.1 cgd
830 1.26 christos static struct disklabel *
831 1.1 cgd getdisklabel(s, fd)
832 1.34 mycroft const char *s;
833 1.1 cgd int fd;
834 1.1 cgd {
835 1.1 cgd static struct disklabel lab;
836 1.1 cgd
837 1.1 cgd if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
838 1.1 cgd if (s == NULL)
839 1.1 cgd return ((struct disklabel *)NULL);
840 1.1 cgd pwarn("ioctl (GCINFO): %s\n", strerror(errno));
841 1.30 lukem errx(EEXIT, "%s: can't read disk label", s);
842 1.1 cgd }
843 1.1 cgd return (&lab);
844 1.1 cgd }
845 1.54 dbj
846 1.54 dbj static struct partition *
847 1.54 dbj getdisklabelpart(dev, lp)
848 1.54 dbj const char *dev;
849 1.54 dbj struct disklabel *lp;
850 1.54 dbj {
851 1.54 dbj char *cp;
852 1.54 dbj
853 1.54 dbj cp = strchr(dev, '\0') - 1;
854 1.54 dbj if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'p')) && !isdigit(*cp)) {
855 1.54 dbj return 0;
856 1.54 dbj }
857 1.54 dbj if (isdigit(*cp))
858 1.54 dbj return &lp->d_partitions[0];
859 1.54 dbj else
860 1.54 dbj return &lp->d_partitions[*cp - 'a'];
861 1.54 dbj }
862 1.54 dbj
863