This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / sys / ufs / ufs_disksubr.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3 * 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 *
78ed81a3 33 * from: @(#)ufs_disksubr.c 7.16 (Berkeley) 5/4/91
34 * $Id$
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "buf.h"
40#include "dkbad.h"
41#include "disklabel.h"
42#include "syslog.h"
43
44/*
45 * Seek sort for disks. We depend on the driver
46 * which calls us using b_resid as the current cylinder number.
47 *
48 * The argument dp structure holds a b_actf activity chain pointer
49 * on which we keep two queues, sorted in ascending cylinder order.
50 * The first queue holds those requests which are positioned after
51 * the current cylinder (in the first request); the second holds
52 * requests which came in after their cylinder number was passed.
53 * Thus we implement a one way scan, retracting after reaching the
54 * end of the drive to the first request on the second queue,
55 * at which time it becomes the first queue.
56 *
57 * A one-way scan is natural because of the way UNIX read-ahead
58 * blocks are allocated.
59 */
60
61#define b_cylin b_resid
62
63void
64disksort(dp, bp)
65 register struct buf *dp, *bp;
66{
67 register struct buf *ap;
68
69 /*
70 * If nothing on the activity queue, then
71 * we become the only thing.
72 */
73 ap = dp->b_actf;
74 if(ap == NULL) {
75 dp->b_actf = bp;
76 dp->b_actl = bp;
77 bp->av_forw = NULL;
78 return;
79 }
80 /*
81 * If we lie after the first (currently active)
82 * request, then we must locate the second request list
83 * and add ourselves to it.
84 */
85 if (bp->b_cylin < ap->b_cylin) {
86 while (ap->av_forw) {
87 /*
88 * Check for an ``inversion'' in the
89 * normally ascending cylinder numbers,
90 * indicating the start of the second request list.
91 */
92 if (ap->av_forw->b_cylin < ap->b_cylin) {
93 /*
94 * Search the second request list
95 * for the first request at a larger
96 * cylinder number. We go before that;
97 * if there is no such request, we go at end.
98 */
99 do {
100 if (bp->b_cylin < ap->av_forw->b_cylin)
101 goto insert;
102 if (bp->b_cylin == ap->av_forw->b_cylin &&
103 bp->b_blkno < ap->av_forw->b_blkno)
104 goto insert;
105 ap = ap->av_forw;
106 } while (ap->av_forw);
107 goto insert; /* after last */
108 }
109 ap = ap->av_forw;
110 }
111 /*
112 * No inversions... we will go after the last, and
113 * be the first request in the second request list.
114 */
115 goto insert;
116 }
117 /*
118 * Request is at/after the current request...
119 * sort in the first request list.
120 */
121 while (ap->av_forw) {
122 /*
123 * We want to go after the current request
124 * if there is an inversion after it (i.e. it is
125 * the end of the first request list), or if
126 * the next request is a larger cylinder than our request.
127 */
128 if (ap->av_forw->b_cylin < ap->b_cylin ||
129 bp->b_cylin < ap->av_forw->b_cylin ||
130 (bp->b_cylin == ap->av_forw->b_cylin &&
131 bp->b_blkno < ap->av_forw->b_blkno))
132 goto insert;
133 ap = ap->av_forw;
134 }
135 /*
136 * Neither a second list nor a larger
137 * request... we go at the end of the first list,
138 * which is the same as the end of the whole schebang.
139 */
140insert:
141 bp->av_forw = ap->av_forw;
142 ap->av_forw = bp;
143 if (ap == dp->b_actl)
144 dp->b_actl = bp;
145}
146
147/* encoding of disk minor numbers, should be elsewhere... */
148#define dkunit(dev) (minor(dev) >> 3)
149#define dkpart(dev) (minor(dev) & 7)
150#define dkminor(unit, part) (((unit) << 3) | (part))
151
152/*
153 * Attempt to read a disk label from a device
154 * using the indicated stategy routine.
155 * The label must be partly set up before this:
156 * secpercyl, secsize and anything required for a block i/o read
157 * operation in the driver's strategy/start routines
158 * must be filled in before calling us.
159 *
160 * If dos partition table requested, attempt to load it and
161 * find disklabel inside a DOS partition. Also, if bad block
162 * table needed, attempt to extract it as well. Return buffer
163 * for use in signalling errors if requested.
164 *
165 * Returns null on success and an error string on failure.
166 */
167char *
168readdisklabel(dev, strat, lp, dp, bdp, bpp)
169 dev_t dev;
170 int (*strat)();
171 register struct disklabel *lp;
172 struct dos_partition *dp;
173 struct dkbad *bdp;
174 struct buf **bpp;
175{
176 register struct buf *bp;
177 struct disklabel *dlp;
178 char *msg = NULL;
179 int cyl, dospartoff, i;
180
181 /* minimal requirements for archtypal disk label */
182 if (lp->d_secperunit == 0)
183 lp->d_secperunit = 0x1fffffff;
184 lp->d_npartitions = 1;
185 if (lp->d_partitions[0].p_size == 0)
186 lp->d_partitions[0].p_size = 0x1fffffff;
187 lp->d_partitions[0].p_offset = 0;
188
189 /* obtain buffer to probe drive with */
190 bp = geteblk((int)lp->d_secsize);
191
192 /* request no partition relocation by driver on I/O operations */
193 bp->b_dev = makedev(major(dev), dkminor((dkunit(dev)), 3));
194
195 /* do dos partitions in the process of getting disklabel? */
196 dospartoff = 0;
197 cyl = LABELSECTOR / lp->d_secpercyl;
198 if (dp) {
199 struct dos_partition *ap;
200
201 /* read master boot record */
202 bp->b_blkno = DOSBBSECTOR;
203 bp->b_bcount = lp->d_secsize;
204 bp->b_flags = B_BUSY | B_READ;
205 bp->b_cylin = DOSBBSECTOR / lp->d_secpercyl;
206 (*strat)(bp);
207
208 /* if successful, wander through dos partition table */
209 if (biowait(bp)) {
210 msg = "dos partition I/O error";
211 goto done;
212 } else {
213 /* XXX how do we check veracity/bounds of this? */
214 bcopy(bp->b_un.b_addr + DOSPARTOFF, dp,
215 NDOSPART * sizeof(*dp));
216 for (i = 0; i < NDOSPART; i++, dp++)
217 /* is this ours? */
218 if (dp->dp_size &&
219 dp->dp_typ == DOSPTYP_386BSD
220 && dospartoff == 0) {
221
222 /* need sector address for SCSI/IDE,
223 cylinder for ESDI/ST506/RLL */
224 dospartoff = dp->dp_start;
225 cyl = DPCYL(dp->dp_scyl, dp->dp_ssect);
226
227 /* update disklabel with details */
228 lp->d_partitions[0].p_size =
229 dp->dp_size;
230 lp->d_partitions[0].p_offset =
231 dp->dp_start;
232 lp->d_ntracks = dp->dp_ehd + 1;
233 lp->d_nsectors = DPSECT(dp->dp_esect);
234 lp->d_subtype |= (lp->d_subtype & 3)
235 + i | DSTYPE_INDOSPART;
236 lp->d_secpercyl = lp->d_ntracks *
237 lp->d_nsectors;
238 }
239 }
240
241 }
242
243 /* next, dig out disk label */
244 bp->b_blkno = dospartoff + LABELSECTOR;
245 bp->b_cylin = cyl;
246 bp->b_bcount = lp->d_secsize;
247 bp->b_flags = B_BUSY | B_READ;
248 (*strat)(bp);
249
250 /* if successful, locate disk label within block and validate */
251 if (biowait(bp)) {
252 msg = "disk label I/O error";
253 goto done;
254 } else for (dlp = (struct disklabel *)bp->b_un.b_addr;
255 dlp <= (struct disklabel *)(bp->b_un.b_addr+DEV_BSIZE-sizeof(*dlp));
256 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
257 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) {
258 if (msg == NULL)
259 msg = "no disk label";
260 } else if (dlp->d_npartitions > MAXPARTITIONS ||
261 dkcksum(dlp) != 0)
262 msg = "disk label corrupted";
263 else {
264 *lp = *dlp;
265 msg = NULL;
266 break;
267 }
268 }
269
270 if (msg)
271 goto done;
272
273 /* obtain bad sector table if requested and present */
274 if (bdp && (lp->d_flags & D_BADSECT)) {
275 struct dkbad *db;
276
277 i = 0;
278 do {
279 /* read a bad sector table */
280 bp->b_flags = B_BUSY | B_READ;
281 bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
282 if (lp->d_secsize > DEV_BSIZE)
283 bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
284 else
285 bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
286 bp->b_bcount = lp->d_secsize;
287 bp->b_cylin = lp->d_ncylinders - 1;
288 (*strat)(bp);
289
290 /* if successful, validate, otherwise try another */
291 if (biowait(bp)) {
292 msg = "bad sector table I/O error";
293 } else {
294 db = (struct dkbad *)(bp->b_un.b_addr);
295#define DKBAD_MAGIC 0x4321
296 if (db->bt_mbz == 0
297 && db->bt_flag == DKBAD_MAGIC) {
298 msg = NULL;
299 *bdp = *db;
300 break;
301 } else
302 msg = "bad sector table corrupted";
303 }
304 } while ((bp->b_flags & B_ERROR) && (i += 2) < 10 &&
305 i < lp->d_nsectors);
306 }
307
308done:
309 bp->b_flags = B_INVAL | B_AGE | B_READ;
310#ifndef old
311 /* if desired, pass back allocated block so caller can use */
312 if (bpp)
313 *bpp = bp;
314 else
315#endif
316 brelse(bp);
317 return (msg);
318}
319
320/*
321 * Check new disk label for sensibility
322 * before setting it.
323 */
324setdisklabel(olp, nlp, openmask, dp)
325 register struct disklabel *olp, *nlp;
326 u_long openmask;
327 struct dos_partition *dp;
328{
329 register i;
330 register struct partition *opp, *npp;
331
332 /* sanity clause */
333 if (nlp->d_secpercyl == 0 || nlp->d_secsize == 0
334 || (nlp->d_secsize % DEV_BSIZE) != 0)
335 return(EINVAL);
336
337 /* special case to allow disklabel to be invalidated */
338 if (nlp->d_magic == 0xffffffff) {
339 *olp = *nlp;
340 return (0);
341 }
342
343 if (nlp->d_magic != DISKMAGIC || nlp->d_magic2 != DISKMAGIC ||
344 dkcksum(nlp) != 0)
345 return (EINVAL);
346
347 /* XXX missing check if other dos partitions will be overwritten */
348
349 while ((i = ffs((long)openmask)) != 0) {
350 i--;
351 openmask &= ~(1 << i);
352 if (nlp->d_npartitions <= i)
353 return (EBUSY);
354 opp = &olp->d_partitions[i];
355 npp = &nlp->d_partitions[i];
356 if (npp->p_offset != opp->p_offset || npp->p_size < opp->p_size)
357 return (EBUSY);
358 /*
359 * Copy internally-set partition information
360 * if new label doesn't include it. XXX
361 */
362 if (npp->p_fstype == FS_UNUSED && opp->p_fstype != FS_UNUSED) {
363 npp->p_fstype = opp->p_fstype;
364 npp->p_fsize = opp->p_fsize;
365 npp->p_frag = opp->p_frag;
366 npp->p_cpg = opp->p_cpg;
367 }
368 }
369 nlp->d_checksum = 0;
370 nlp->d_checksum = dkcksum(nlp);
371 *olp = *nlp;
372 return (0);
373}
374
375
376/*
377 * Write disk label back to device after modification.
378 */
379writedisklabel(dev, strat, lp, dp)
380 dev_t dev;
381 int (*strat)();
382 register struct disklabel *lp;
383 struct dos_partition *dp;
384{
385 struct buf *bp;
386 struct disklabel *dlp;
387 int labelpart, error = 0, dospartoff, cyl, i;
388
389 labelpart = dkpart(dev);
390#ifdef nope
391 if (lp->d_partitions[labelpart].p_offset != 0) {
392 if (lp->d_partitions[0].p_offset != 0)
393 return (EXDEV); /* not quite right */
394 labelpart = 0;
395 }
396#else
397 labelpart = 3;
398#endif
399
400 bp = geteblk((int)lp->d_secsize);
401 /* request no partition relocation by driver on I/O operations */
402 bp->b_dev = makedev(major(dev), dkminor((dkunit(dev)), 3));
403
404 /* do dos partitions in the process of getting disklabel? */
405 dospartoff = 0;
406 cyl = LABELSECTOR / lp->d_secpercyl;
407 if (dp) {
408 bp->b_blkno = DOSBBSECTOR;
409 bp->b_bcount = lp->d_secsize;
410 bp->b_flags = B_BUSY | B_READ;
411 bp->b_cylin = DOSBBSECTOR / lp->d_secpercyl;
412 (*strat)(bp);
413 if ((error = biowait(bp)) == 0) {
414 bcopy(bp->b_un.b_addr + DOSPARTOFF, dp,
415 NDOSPART * sizeof(*dp));
416 for (i = 0; i < NDOSPART; i++, dp++)
417 if(dp->dp_size && dp->dp_typ == DOSPTYP_386BSD
418 && dospartoff == 0) {
419 /* need sector address for SCSI/IDE,
420 cylinder for ESDI/ST506/RLL */
421 dospartoff = dp->dp_start;
422 cyl = dp->dp_scyl |
423 ((dp->dp_ssect & 0xc0) << 2);
424 }
425 }
426
427 }
428
429#ifdef maybe
430 /* disklabel in appropriate location? */
431 if (lp->d_partitions[0].p_offset != 0
432 && lp->d_partitions[0].p_offset != dospartoff) {
433 error = EXDEV;
434 goto done;
435 }
436#endif
437
438 bp->b_blkno = dospartoff + LABELSECTOR;
439 bp->b_cylin = cyl;
440 bp->b_bcount = lp->d_secsize;
441 bp->b_flags = B_READ;
442 (*strat)(bp);
443 if (error = biowait(bp))
444 goto done;
445 for (dlp = (struct disklabel *)bp->b_un.b_addr;
446 dlp <= (struct disklabel *)
447 (bp->b_un.b_addr + lp->d_secsize - sizeof(*dlp));
448 dlp = (struct disklabel *)((char *)dlp + sizeof(long))) {
449 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC &&
450 dkcksum(dlp) == 0) {
451 *dlp = *lp;
452 bp->b_flags = B_WRITE;
453 (*strat)(bp);
454 error = biowait(bp);
455 goto done;
456 }
457 }
458 error = ESRCH;
459done:
460 brelse(bp);
461 return (error);
462}
463
464/*
465 * Compute checksum for disk label.
466 */
467dkcksum(lp)
468 register struct disklabel *lp;
469{
470 register u_short *start, *end;
471 register u_short sum = 0;
472
473 start = (u_short *)lp;
474 end = (u_short *)&lp->d_partitions[lp->d_npartitions];
475 while (start < end)
476 sum ^= *start++;
477 return (sum);
478}
479
480/*
481 * Determine the size of the transfer, and make sure it is
482 * within the boundaries of the partition. Adjust transfer
483 * if needed, and signal errors or early completion.
484 */
485int
486bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
487{
488 struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
489 int labelsect = lp->d_partitions[0].p_offset;
490 int maxsz = p->p_size,
491 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
492
493 /* overwriting disk label ? */
494 /* XXX should also protect bootstrap in first 8K */
495 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
496#if LABELSECTOR != 0
497 bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
498#endif
499 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
500 bp->b_error = EROFS;
501 goto bad;
502 }
503
504#if defined(DOSBBSECTOR) && defined(notyet)
505 /* overwriting master boot record? */
506 if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
507 (bp->b_flags & B_READ) == 0 && wlabel == 0) {
508 bp->b_error = EROFS;
509 goto bad;
510 }
511#endif
512
513 /* beyond partition? */
514 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
515 /* if exactly at end of disk, return an EOF */
516 if (bp->b_blkno == maxsz) {
517 bp->b_resid = bp->b_bcount;
518 return(0);
519 }
520 /* or truncate if part of it fits */
521 sz = maxsz - bp->b_blkno;
522 if (sz <= 0) {
523 bp->b_error = EINVAL;
524 goto bad;
525 }
526 bp->b_bcount = sz << DEV_BSHIFT;
527 }
528
529 /* calculate cylinder for disksort to order transfers with */
530 bp->b_cylin = (bp->b_blkno + p->p_offset) / lp->d_secpercyl;
531 return(1);
532
533bad:
534 bp->b_flags |= B_ERROR;
535 return(-1);
536}
537
538/*
539 * Disk error is the preface to plaintive error messages
540 * about failing disk transfers. It prints messages of the form
541
542hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
543
544 * if the offset of the error in the transfer and a disk label
545 * are both available. blkdone should be -1 if the position of the error
546 * is unknown; the disklabel pointer may be null from drivers that have not
547 * been converted to use them. The message is printed with printf
548 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
549 * The message should be completed (with at least a newline) with printf
550 * or addlog, respectively. There is no trailing space.
551 */
552void
553diskerr(bp, dname, what, pri, blkdone, lp)
554 register struct buf *bp;
555 char *dname, *what;
556 int pri, blkdone;
557 register struct disklabel *lp;
558{
559 int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
78ed81a3 560 register int (*pr) __P((const char *, ...));
15637ed4
RG
561 char partname = 'a' + part;
562 int sn;
563
564 if (pri != LOG_PRINTF) {
565 log(pri, "");
566 pr = addlog;
567 } else
568 pr = printf;
569 (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
570 bp->b_flags & B_READ ? "read" : "writ");
571 sn = bp->b_blkno;
572 if (bp->b_bcount <= DEV_BSIZE)
573 (*pr)("%d", sn);
574 else {
575 if (blkdone >= 0) {
576 sn += blkdone;
577 (*pr)("%d of ", sn);
578 }
579 (*pr)("%d-%d", bp->b_blkno,
580 bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
581 }
582 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
583#ifdef tahoe
584 sn *= DEV_BSIZE / lp->d_secsize; /* XXX */
585#endif
586 sn += lp->d_partitions[part].p_offset;
587 (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
588 sn / lp->d_secpercyl);
589 sn %= lp->d_secpercyl;
590 (*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
591 }
78ed81a3 592 (*pr)("\n");
15637ed4 593}