1/* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint
| 1/* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint
|
35static const char sccsid[] = "@(#)setup.c 8.2 (Berkeley) 2/21/94";
| 35static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
|
36#endif /* not lint */ 37 38#define DKTYPENAMES 39#include <sys/param.h> 40#include <sys/time.h>
| 36#endif /* not lint */ 37 38#define DKTYPENAMES 39#include <sys/param.h> 40#include <sys/time.h>
|
41#include <ufs/ufs/dinode.h> 42#include <ufs/ffs/fs.h>
| |
43#include <sys/stat.h> 44#include <sys/ioctl.h> 45#include <sys/disklabel.h> 46#include <sys/file.h>
| 41#include <sys/stat.h> 42#include <sys/ioctl.h> 43#include <sys/disklabel.h> 44#include <sys/file.h>
|
| 45 46#include <ufs/ufs/dinode.h> 47#include <ufs/ffs/fs.h> 48 49#include <ctype.h> 50#include <err.h>
|
47#include <errno.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h>
| 51#include <errno.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h>
|
51#include <ctype.h>
| 55
|
52#include "fsck.h" 53 54struct bufarea asblk; 55#define altsblock (*asblk.b_un.b_fs) 56#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 57
| 56#include "fsck.h" 57 58struct bufarea asblk; 59#define altsblock (*asblk.b_un.b_fs) 60#define POWEROF2(num) (((num) & ((num) - 1)) == 0) 61
|
58static int readsb __P((int listerr)); 59static void badsb __P((int listerr, char *s)); 60static int calcsb __P((char *dev, int devfd, struct fs *fs)); 61static struct disklabel * getdisklabel __P((char *s, int fd));
| 62static void badsb __P((int listerr, char *s)); 63static int calcsb __P((char *dev, int devfd, struct fs *fs)); 64static struct disklabel *getdisklabel __P((char *s, int fd)); 65static int readsb __P((int listerr));
|
62
| 66
|
63
| 67/* 68 * Read in a superblock finding an alternate if necessary. 69 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem 70 * is already clean (preen mode only). 71 */
|
64int 65setup(dev) 66 char *dev; 67{ 68 long cg, size, asked, i, j;
| 72int 73setup(dev) 74 char *dev; 75{ 76 long cg, size, asked, i, j;
|
69 long bmapsize;
| 77 long skipclean, bmapsize;
|
70 struct disklabel *lp; 71 off_t sizepb; 72 struct stat statb; 73 struct fs proto; 74 75 havesb = 0; 76 fswritefd = -1;
| 78 struct disklabel *lp; 79 off_t sizepb; 80 struct stat statb; 81 struct fs proto; 82 83 havesb = 0; 84 fswritefd = -1;
|
| 85 skipclean = preen;
|
77 if (stat(dev, &statb) < 0) { 78 printf("Can't stat %s: %s\n", dev, strerror(errno)); 79 return (0); 80 } 81 if ((statb.st_mode & S_IFMT) != S_IFCHR) { 82 pfatal("%s is not a character device", dev); 83 if (reply("CONTINUE") == 0) 84 return (0); 85 } 86 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 87 printf("Can't open %s: %s\n", dev, strerror(errno)); 88 return (0); 89 } 90 if (preen == 0) 91 printf("** %s", dev); 92 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 93 fswritefd = -1; 94 if (preen) 95 pfatal("NO WRITE ACCESS"); 96 printf(" (NO WRITE)"); 97 } 98 if (preen == 0) 99 printf("\n"); 100 fsmodified = 0; 101 lfdir = 0; 102 initbarea(&sblk); 103 initbarea(&asblk); 104 sblk.b_un.b_buf = malloc(SBSIZE); 105 asblk.b_un.b_buf = malloc(SBSIZE); 106 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
| 86 if (stat(dev, &statb) < 0) { 87 printf("Can't stat %s: %s\n", dev, strerror(errno)); 88 return (0); 89 } 90 if ((statb.st_mode & S_IFMT) != S_IFCHR) { 91 pfatal("%s is not a character device", dev); 92 if (reply("CONTINUE") == 0) 93 return (0); 94 } 95 if ((fsreadfd = open(dev, O_RDONLY)) < 0) { 96 printf("Can't open %s: %s\n", dev, strerror(errno)); 97 return (0); 98 } 99 if (preen == 0) 100 printf("** %s", dev); 101 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { 102 fswritefd = -1; 103 if (preen) 104 pfatal("NO WRITE ACCESS"); 105 printf(" (NO WRITE)"); 106 } 107 if (preen == 0) 108 printf("\n"); 109 fsmodified = 0; 110 lfdir = 0; 111 initbarea(&sblk); 112 initbarea(&asblk); 113 sblk.b_un.b_buf = malloc(SBSIZE); 114 asblk.b_un.b_buf = malloc(SBSIZE); 115 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
|
107 errexit("cannot allocate space for superblock\n");
| 116 errx(EEXIT, "cannot allocate space for superblock");
|
108 lp = getdisklabel((char *)NULL, fsreadfd); 109 if (lp) 110 dev_bsize = secsize = lp->d_secsize; 111 else 112 dev_bsize = secsize = DEV_BSIZE; 113 /* 114 * Read in the superblock, looking for alternates if necessary 115 */ 116 if (readsb(1) == 0) {
| 117 lp = getdisklabel((char *)NULL, fsreadfd); 118 if (lp) 119 dev_bsize = secsize = lp->d_secsize; 120 else 121 dev_bsize = secsize = DEV_BSIZE; 122 /* 123 * Read in the superblock, looking for alternates if necessary 124 */ 125 if (readsb(1) == 0) {
|
| 126 skipclean = 0;
|
117 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 118 return(0); 119 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 120 return (0); 121 for (cg = 0; cg < proto.fs_ncg; cg++) { 122 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 123 if (readsb(0) != 0) 124 break; 125 } 126 if (cg >= proto.fs_ncg) { 127 printf("%s %s\n%s %s\n%s %s\n", 128 "SEARCH FOR ALTERNATE SUPER-BLOCK", 129 "FAILED. YOU MUST USE THE", 130 "-b OPTION TO FSCK TO SPECIFY THE", 131 "LOCATION OF AN ALTERNATE", 132 "SUPER-BLOCK TO SUPPLY NEEDED", 133 "INFORMATION; SEE fsck(8)."); 134 bflag = 0; 135 return(0); 136 } 137 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 138 bflag = 0; 139 }
| 127 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) 128 return(0); 129 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) 130 return (0); 131 for (cg = 0; cg < proto.fs_ncg; cg++) { 132 bflag = fsbtodb(&proto, cgsblock(&proto, cg)); 133 if (readsb(0) != 0) 134 break; 135 } 136 if (cg >= proto.fs_ncg) { 137 printf("%s %s\n%s %s\n%s %s\n", 138 "SEARCH FOR ALTERNATE SUPER-BLOCK", 139 "FAILED. YOU MUST USE THE", 140 "-b OPTION TO FSCK TO SPECIFY THE", 141 "LOCATION OF AN ALTERNATE", 142 "SUPER-BLOCK TO SUPPLY NEEDED", 143 "INFORMATION; SEE fsck(8)."); 144 bflag = 0; 145 return(0); 146 } 147 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); 148 bflag = 0; 149 }
|
| 150 if (skipclean && sblock.fs_clean) { 151 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); 152 return (-1); 153 }
|
140 maxfsblock = sblock.fs_size; 141 maxino = sblock.fs_ncg * sblock.fs_ipg; 142 /* 143 * Check and potentially fix certain fields in the super block. 144 */ 145 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 146 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 147 if (reply("SET TO DEFAULT") == 1) { 148 sblock.fs_optim = FS_OPTTIME; 149 sbdirty(); 150 } 151 } 152 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 153 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 154 sblock.fs_minfree); 155 if (reply("SET TO DEFAULT") == 1) { 156 sblock.fs_minfree = 10; 157 sbdirty(); 158 } 159 } 160 if (sblock.fs_interleave < 1 || 161 sblock.fs_interleave > sblock.fs_nsect) { 162 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 163 sblock.fs_interleave); 164 sblock.fs_interleave = 1; 165 if (preen) 166 printf(" (FIXED)\n"); 167 if (preen || reply("SET TO DEFAULT") == 1) { 168 sbdirty(); 169 dirty(&asblk); 170 } 171 } 172 if (sblock.fs_npsect < sblock.fs_nsect || 173 sblock.fs_npsect > sblock.fs_nsect*2) { 174 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 175 sblock.fs_npsect); 176 sblock.fs_npsect = sblock.fs_nsect; 177 if (preen) 178 printf(" (FIXED)\n"); 179 if (preen || reply("SET TO DEFAULT") == 1) { 180 sbdirty(); 181 dirty(&asblk); 182 } 183 } 184 if (sblock.fs_inodefmt >= FS_44INODEFMT) { 185 newinofmt = 1; 186 } else { 187 sblock.fs_qbmask = ~sblock.fs_bmask; 188 sblock.fs_qfmask = ~sblock.fs_fmask; 189 newinofmt = 0; 190 } 191 /* 192 * Convert to new inode format. 193 */ 194 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 195 if (preen) 196 pwarn("CONVERTING TO NEW INODE FORMAT\n"); 197 else if (!reply("CONVERT TO NEW INODE FORMAT")) 198 return(0); 199 doinglevel2++; 200 sblock.fs_inodefmt = FS_44INODEFMT; 201 sizepb = sblock.fs_bsize; 202 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 203 for (i = 0; i < NIADDR; i++) { 204 sizepb *= NINDIR(&sblock); 205 sblock.fs_maxfilesize += sizepb; 206 } 207 sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 208 sblock.fs_qbmask = ~sblock.fs_bmask; 209 sblock.fs_qfmask = ~sblock.fs_fmask; 210 sbdirty(); 211 dirty(&asblk); 212 } 213 /* 214 * Convert to new cylinder group format. 215 */ 216 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 217 if (preen) 218 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 219 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 220 return(0); 221 doinglevel1++; 222 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 223 sblock.fs_nrpos = 8; 224 sblock.fs_postbloff = 225 (char *)(&sblock.fs_opostbl[0][0]) -
| 154 maxfsblock = sblock.fs_size; 155 maxino = sblock.fs_ncg * sblock.fs_ipg; 156 /* 157 * Check and potentially fix certain fields in the super block. 158 */ 159 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { 160 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); 161 if (reply("SET TO DEFAULT") == 1) { 162 sblock.fs_optim = FS_OPTTIME; 163 sbdirty(); 164 } 165 } 166 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { 167 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 168 sblock.fs_minfree); 169 if (reply("SET TO DEFAULT") == 1) { 170 sblock.fs_minfree = 10; 171 sbdirty(); 172 } 173 } 174 if (sblock.fs_interleave < 1 || 175 sblock.fs_interleave > sblock.fs_nsect) { 176 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", 177 sblock.fs_interleave); 178 sblock.fs_interleave = 1; 179 if (preen) 180 printf(" (FIXED)\n"); 181 if (preen || reply("SET TO DEFAULT") == 1) { 182 sbdirty(); 183 dirty(&asblk); 184 } 185 } 186 if (sblock.fs_npsect < sblock.fs_nsect || 187 sblock.fs_npsect > sblock.fs_nsect*2) { 188 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", 189 sblock.fs_npsect); 190 sblock.fs_npsect = sblock.fs_nsect; 191 if (preen) 192 printf(" (FIXED)\n"); 193 if (preen || reply("SET TO DEFAULT") == 1) { 194 sbdirty(); 195 dirty(&asblk); 196 } 197 } 198 if (sblock.fs_inodefmt >= FS_44INODEFMT) { 199 newinofmt = 1; 200 } else { 201 sblock.fs_qbmask = ~sblock.fs_bmask; 202 sblock.fs_qfmask = ~sblock.fs_fmask; 203 newinofmt = 0; 204 } 205 /* 206 * Convert to new inode format. 207 */ 208 if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { 209 if (preen) 210 pwarn("CONVERTING TO NEW INODE FORMAT\n"); 211 else if (!reply("CONVERT TO NEW INODE FORMAT")) 212 return(0); 213 doinglevel2++; 214 sblock.fs_inodefmt = FS_44INODEFMT; 215 sizepb = sblock.fs_bsize; 216 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 217 for (i = 0; i < NIADDR; i++) { 218 sizepb *= NINDIR(&sblock); 219 sblock.fs_maxfilesize += sizepb; 220 } 221 sblock.fs_maxsymlinklen = MAXSYMLINKLEN; 222 sblock.fs_qbmask = ~sblock.fs_bmask; 223 sblock.fs_qfmask = ~sblock.fs_fmask; 224 sbdirty(); 225 dirty(&asblk); 226 } 227 /* 228 * Convert to new cylinder group format. 229 */ 230 if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { 231 if (preen) 232 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); 233 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) 234 return(0); 235 doinglevel1++; 236 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 237 sblock.fs_nrpos = 8; 238 sblock.fs_postbloff = 239 (char *)(&sblock.fs_opostbl[0][0]) -
|
226 (char *)(&sblock.fs_link);
| 240 (char *)(&sblock.fs_firstfield);
|
227 sblock.fs_rotbloff = &sblock.fs_space[0] -
| 241 sblock.fs_rotbloff = &sblock.fs_space[0] -
|
228 (u_char *)(&sblock.fs_link);
| 242 (u_char *)(&sblock.fs_firstfield);
|
229 sblock.fs_cgsize = 230 fragroundup(&sblock, CGSIZE(&sblock)); 231 sbdirty(); 232 dirty(&asblk); 233 }
| 243 sblock.fs_cgsize = 244 fragroundup(&sblock, CGSIZE(&sblock)); 245 sbdirty(); 246 dirty(&asblk); 247 }
|
234 if (asblk.b_dirty) { 235 bcopy((char *)&sblock, (char *)&altsblock, 236 (size_t)sblock.fs_sbsize);
| 248 if (asblk.b_dirty && !bflag) { 249 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
|
237 flush(fswritefd, &asblk); 238 } 239 /* 240 * read in the summary info. 241 */ 242 asked = 0; 243 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 244 size = sblock.fs_cssize - i < sblock.fs_bsize ? 245 sblock.fs_cssize - i : sblock.fs_bsize; 246 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 247 if (bread(fsreadfd, (char *)sblock.fs_csp[j], 248 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 249 size) != 0 && !asked) { 250 pfatal("BAD SUMMARY INFORMATION"); 251 if (reply("CONTINUE") == 0)
| 250 flush(fswritefd, &asblk); 251 } 252 /* 253 * read in the summary info. 254 */ 255 asked = 0; 256 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 257 size = sblock.fs_cssize - i < sblock.fs_bsize ? 258 sblock.fs_cssize - i : sblock.fs_bsize; 259 sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size); 260 if (bread(fsreadfd, (char *)sblock.fs_csp[j], 261 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 262 size) != 0 && !asked) { 263 pfatal("BAD SUMMARY INFORMATION"); 264 if (reply("CONTINUE") == 0)
|
252 errexit("");
| 265 exit(EEXIT);
|
253 asked++; 254 } 255 } 256 /* 257 * allocate and initialize the necessary maps 258 */ 259 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 260 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 261 if (blockmap == NULL) { 262 printf("cannot alloc %u bytes for blockmap\n", 263 (unsigned)bmapsize); 264 goto badsb; 265 } 266 statemap = calloc((unsigned)(maxino + 1), sizeof(char)); 267 if (statemap == NULL) { 268 printf("cannot alloc %u bytes for statemap\n", 269 (unsigned)(maxino + 1)); 270 goto badsb; 271 } 272 typemap = calloc((unsigned)(maxino + 1), sizeof(char)); 273 if (typemap == NULL) { 274 printf("cannot alloc %u bytes for typemap\n", 275 (unsigned)(maxino + 1)); 276 goto badsb; 277 } 278 lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short)); 279 if (lncntp == NULL) { 280 printf("cannot alloc %u bytes for lncntp\n", 281 (unsigned)(maxino + 1) * sizeof(short)); 282 goto badsb; 283 } 284 numdirs = sblock.fs_cstotal.cs_ndir; 285 inplast = 0; 286 listmax = numdirs + 10; 287 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 288 sizeof(struct inoinfo *)); 289 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 290 sizeof(struct inoinfo *)); 291 if (inpsort == NULL || inphead == NULL) { 292 printf("cannot alloc %u bytes for inphead\n", 293 (unsigned)numdirs * sizeof(struct inoinfo *)); 294 goto badsb; 295 } 296 bufinit(); 297 return (1); 298 299badsb:
| 266 asked++; 267 } 268 } 269 /* 270 * allocate and initialize the necessary maps 271 */ 272 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); 273 blockmap = calloc((unsigned)bmapsize, sizeof (char)); 274 if (blockmap == NULL) { 275 printf("cannot alloc %u bytes for blockmap\n", 276 (unsigned)bmapsize); 277 goto badsb; 278 } 279 statemap = calloc((unsigned)(maxino + 1), sizeof(char)); 280 if (statemap == NULL) { 281 printf("cannot alloc %u bytes for statemap\n", 282 (unsigned)(maxino + 1)); 283 goto badsb; 284 } 285 typemap = calloc((unsigned)(maxino + 1), sizeof(char)); 286 if (typemap == NULL) { 287 printf("cannot alloc %u bytes for typemap\n", 288 (unsigned)(maxino + 1)); 289 goto badsb; 290 } 291 lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short)); 292 if (lncntp == NULL) { 293 printf("cannot alloc %u bytes for lncntp\n", 294 (unsigned)(maxino + 1) * sizeof(short)); 295 goto badsb; 296 } 297 numdirs = sblock.fs_cstotal.cs_ndir; 298 inplast = 0; 299 listmax = numdirs + 10; 300 inpsort = (struct inoinfo **)calloc((unsigned)listmax, 301 sizeof(struct inoinfo *)); 302 inphead = (struct inoinfo **)calloc((unsigned)numdirs, 303 sizeof(struct inoinfo *)); 304 if (inpsort == NULL || inphead == NULL) { 305 printf("cannot alloc %u bytes for inphead\n", 306 (unsigned)numdirs * sizeof(struct inoinfo *)); 307 goto badsb; 308 } 309 bufinit(); 310 return (1); 311 312badsb:
|
300 ckfini();
| 313 ckfini(0);
|
301 return (0); 302} 303 304/* 305 * Read in the super block and its summary info. 306 */ 307static int 308readsb(listerr) 309 int listerr; 310{
| 314 return (0); 315} 316 317/* 318 * Read in the super block and its summary info. 319 */ 320static int 321readsb(listerr) 322 int listerr; 323{
|
311 daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
| 324 ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
|
312 313 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 314 return (0); 315 sblk.b_bno = super; 316 sblk.b_size = SBSIZE; 317 /* 318 * run a few consistency checks of the super block 319 */ 320 if (sblock.fs_magic != FS_MAGIC) 321 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 322 if (sblock.fs_ncg < 1) 323 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 324 if (sblock.fs_cpg < 1) 325 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 326 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 327 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 328 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 329 if (sblock.fs_sbsize > SBSIZE) 330 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 331 /* 332 * Compute block size that the filesystem is based on, 333 * according to fsbtodb, and adjust superblock block number 334 * so we can tell if this is an alternate later. 335 */ 336 super *= dev_bsize; 337 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 338 sblk.b_bno = super / dev_bsize; 339 if (bflag) { 340 havesb = 1; 341 return (1); 342 } 343 /* 344 * Set all possible fields that could differ, then do check 345 * of whole super block against an alternate super block. 346 * When an alternate super-block is specified this check is skipped. 347 */ 348 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 349 if (asblk.b_errs) 350 return (0);
| 325 326 if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0) 327 return (0); 328 sblk.b_bno = super; 329 sblk.b_size = SBSIZE; 330 /* 331 * run a few consistency checks of the super block 332 */ 333 if (sblock.fs_magic != FS_MAGIC) 334 { badsb(listerr, "MAGIC NUMBER WRONG"); return (0); } 335 if (sblock.fs_ncg < 1) 336 { badsb(listerr, "NCG OUT OF RANGE"); return (0); } 337 if (sblock.fs_cpg < 1) 338 { badsb(listerr, "CPG OUT OF RANGE"); return (0); } 339 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 340 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 341 { badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); } 342 if (sblock.fs_sbsize > SBSIZE) 343 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); } 344 /* 345 * Compute block size that the filesystem is based on, 346 * according to fsbtodb, and adjust superblock block number 347 * so we can tell if this is an alternate later. 348 */ 349 super *= dev_bsize; 350 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 351 sblk.b_bno = super / dev_bsize; 352 if (bflag) { 353 havesb = 1; 354 return (1); 355 } 356 /* 357 * Set all possible fields that could differ, then do check 358 * of whole super block against an alternate super block. 359 * When an alternate super-block is specified this check is skipped. 360 */ 361 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize); 362 if (asblk.b_errs) 363 return (0);
|
351 altsblock.fs_link = sblock.fs_link; 352 altsblock.fs_rlink = sblock.fs_rlink;
| 364 altsblock.fs_firstfield = sblock.fs_firstfield; 365 altsblock.fs_unused_1 = sblock.fs_unused_1;
|
353 altsblock.fs_time = sblock.fs_time; 354 altsblock.fs_cstotal = sblock.fs_cstotal; 355 altsblock.fs_cgrotor = sblock.fs_cgrotor; 356 altsblock.fs_fmod = sblock.fs_fmod; 357 altsblock.fs_clean = sblock.fs_clean; 358 altsblock.fs_ronly = sblock.fs_ronly; 359 altsblock.fs_flags = sblock.fs_flags; 360 altsblock.fs_maxcontig = sblock.fs_maxcontig; 361 altsblock.fs_minfree = sblock.fs_minfree; 362 altsblock.fs_optim = sblock.fs_optim; 363 altsblock.fs_rotdelay = sblock.fs_rotdelay; 364 altsblock.fs_maxbpg = sblock.fs_maxbpg;
| 366 altsblock.fs_time = sblock.fs_time; 367 altsblock.fs_cstotal = sblock.fs_cstotal; 368 altsblock.fs_cgrotor = sblock.fs_cgrotor; 369 altsblock.fs_fmod = sblock.fs_fmod; 370 altsblock.fs_clean = sblock.fs_clean; 371 altsblock.fs_ronly = sblock.fs_ronly; 372 altsblock.fs_flags = sblock.fs_flags; 373 altsblock.fs_maxcontig = sblock.fs_maxcontig; 374 altsblock.fs_minfree = sblock.fs_minfree; 375 altsblock.fs_optim = sblock.fs_optim; 376 altsblock.fs_rotdelay = sblock.fs_rotdelay; 377 altsblock.fs_maxbpg = sblock.fs_maxbpg;
|
365 bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp, 366 sizeof sblock.fs_csp); 367 bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt, 368 sizeof sblock.fs_fsmnt); 369 bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon, 370 sizeof sblock.fs_sparecon);
| 378 memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp); 379 altsblock.fs_maxcluster = sblock.fs_maxcluster; 380 memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt); 381 memmove(altsblock.fs_sparecon, 382 sblock.fs_sparecon, sizeof sblock.fs_sparecon);
|
371 /* 372 * The following should not have to be copied. 373 */ 374 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 375 altsblock.fs_interleave = sblock.fs_interleave; 376 altsblock.fs_npsect = sblock.fs_npsect; 377 altsblock.fs_nrpos = sblock.fs_nrpos;
| 383 /* 384 * The following should not have to be copied. 385 */ 386 altsblock.fs_fsbtodb = sblock.fs_fsbtodb; 387 altsblock.fs_interleave = sblock.fs_interleave; 388 altsblock.fs_npsect = sblock.fs_npsect; 389 altsblock.fs_nrpos = sblock.fs_nrpos;
|
| 390 altsblock.fs_state = sblock.fs_state;
|
378 altsblock.fs_qbmask = sblock.fs_qbmask; 379 altsblock.fs_qfmask = sblock.fs_qfmask; 380 altsblock.fs_state = sblock.fs_state; 381 altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
| 391 altsblock.fs_qbmask = sblock.fs_qbmask; 392 altsblock.fs_qfmask = sblock.fs_qfmask; 393 altsblock.fs_state = sblock.fs_state; 394 altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
|
382 if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
| 395 if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) { 396 if (debug) { 397 long *nlp, *olp, *endlp; 398 399 printf("superblock mismatches\n"); 400 nlp = (long *)&altsblock; 401 olp = (long *)&sblock; 402 endlp = olp + (sblock.fs_sbsize / sizeof *olp); 403 for ( ; olp < endlp; olp++, nlp++) { 404 if (*olp == *nlp) 405 continue; 406 printf("offset %d, original %d, alternate %d\n", 407 olp - (long *)&sblock, *olp, *nlp); 408 } 409 }
|
383 badsb(listerr, 384 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 385 return (0); 386 } 387 havesb = 1; 388 return (1); 389} 390 391static void 392badsb(listerr, s) 393 int listerr; 394 char *s; 395{ 396 397 if (!listerr) 398 return; 399 if (preen) 400 printf("%s: ", cdevname); 401 pfatal("BAD SUPER BLOCK: %s\n", s); 402} 403 404/* 405 * Calculate a prototype superblock based on information in the disk label. 406 * When done the cgsblock macro can be calculated and the fs_ncg field 407 * can be used. Do NOT attempt to use other macros without verifying that 408 * their needed information is available! 409 */
| 410 badsb(listerr, 411 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE"); 412 return (0); 413 } 414 havesb = 1; 415 return (1); 416} 417 418static void 419badsb(listerr, s) 420 int listerr; 421 char *s; 422{ 423 424 if (!listerr) 425 return; 426 if (preen) 427 printf("%s: ", cdevname); 428 pfatal("BAD SUPER BLOCK: %s\n", s); 429} 430 431/* 432 * Calculate a prototype superblock based on information in the disk label. 433 * When done the cgsblock macro can be calculated and the fs_ncg field 434 * can be used. Do NOT attempt to use other macros without verifying that 435 * their needed information is available! 436 */
|
410int
| 437static int
|
411calcsb(dev, devfd, fs) 412 char *dev; 413 int devfd; 414 register struct fs *fs; 415{ 416 register struct disklabel *lp; 417 register struct partition *pp; 418 register char *cp; 419 int i; 420
| 438calcsb(dev, devfd, fs) 439 char *dev; 440 int devfd; 441 register struct fs *fs; 442{ 443 register struct disklabel *lp; 444 register struct partition *pp; 445 register char *cp; 446 int i; 447
|
421 cp = index(dev, '\0') - 1;
| 448 cp = strchr(dev, '\0') - 1;
|
422 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 423 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 424 return (0); 425 } 426 lp = getdisklabel(dev, devfd); 427 if (isdigit(*cp)) 428 pp = &lp->d_partitions[0]; 429 else 430 pp = &lp->d_partitions[*cp - 'a']; 431 if (pp->p_fstype != FS_BSDFFS) { 432 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 433 dev, pp->p_fstype < FSMAXTYPES ? 434 fstypenames[pp->p_fstype] : "unknown"); 435 return (0); 436 }
| 449 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) { 450 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev); 451 return (0); 452 } 453 lp = getdisklabel(dev, devfd); 454 if (isdigit(*cp)) 455 pp = &lp->d_partitions[0]; 456 else 457 pp = &lp->d_partitions[*cp - 'a']; 458 if (pp->p_fstype != FS_BSDFFS) { 459 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n", 460 dev, pp->p_fstype < FSMAXTYPES ? 461 fstypenames[pp->p_fstype] : "unknown"); 462 return (0); 463 }
|
437 bzero((char *)fs, sizeof(struct fs));
| 464 memset(fs, 0, sizeof(struct fs));
|
438 fs->fs_fsize = pp->p_fsize; 439 fs->fs_frag = pp->p_frag; 440 fs->fs_cpg = pp->p_cpg; 441 fs->fs_size = pp->p_size; 442 fs->fs_ntrak = lp->d_ntracks; 443 fs->fs_nsect = lp->d_nsectors; 444 fs->fs_spc = lp->d_secpercyl; 445 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 446 fs->fs_sblkno = roundup( 447 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 448 fs->fs_frag); 449 fs->fs_cgmask = 0xffffffff; 450 for (i = fs->fs_ntrak; i > 1; i >>= 1) 451 fs->fs_cgmask <<= 1; 452 if (!POWEROF2(fs->fs_ntrak)) 453 fs->fs_cgmask <<= 1; 454 fs->fs_cgoffset = roundup( 455 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 456 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 457 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 458 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 459 fs->fs_fsbtodb++; 460 dev_bsize = lp->d_secsize; 461 return (1); 462} 463
| 465 fs->fs_fsize = pp->p_fsize; 466 fs->fs_frag = pp->p_frag; 467 fs->fs_cpg = pp->p_cpg; 468 fs->fs_size = pp->p_size; 469 fs->fs_ntrak = lp->d_ntracks; 470 fs->fs_nsect = lp->d_nsectors; 471 fs->fs_spc = lp->d_secpercyl; 472 fs->fs_nspf = fs->fs_fsize / lp->d_secsize; 473 fs->fs_sblkno = roundup( 474 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize), 475 fs->fs_frag); 476 fs->fs_cgmask = 0xffffffff; 477 for (i = fs->fs_ntrak; i > 1; i >>= 1) 478 fs->fs_cgmask <<= 1; 479 if (!POWEROF2(fs->fs_ntrak)) 480 fs->fs_cgmask <<= 1; 481 fs->fs_cgoffset = roundup( 482 howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag); 483 fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs); 484 fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg); 485 for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1) 486 fs->fs_fsbtodb++; 487 dev_bsize = lp->d_secsize; 488 return (1); 489} 490
|
464struct disklabel *
| 491static struct disklabel *
|
465getdisklabel(s, fd) 466 char *s; 467 int fd; 468{ 469 static struct disklabel lab; 470 471 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 472 if (s == NULL) 473 return ((struct disklabel *)NULL); 474 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
| 492getdisklabel(s, fd) 493 char *s; 494 int fd; 495{ 496 static struct disklabel lab; 497 498 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 499 if (s == NULL) 500 return ((struct disklabel *)NULL); 501 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
|
475 errexit("%s: can't read disk label\n", s);
| 502 errx(EEXIT, "%s: can't read disk label", s);
|
476 } 477 return (&lab); 478}
| 503 } 504 return (&lab); 505}
|