subr_disk_4bsd.c revision 1.3 1 1.3 thorpej /* $NetBSD: subr_disk_4bsd.c,v 1.3 2026/08/07 13:49:24 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1982, 1986, 1988, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej * (c) UNIX System Laboratories, Inc.
7 1.1 thorpej * All or some portions of this file are derived from material licensed
8 1.1 thorpej * to the University of California by American Telephone and Telegraph
9 1.1 thorpej * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.1 thorpej * the permission of UNIX System Laboratories, Inc.
11 1.1 thorpej *
12 1.1 thorpej * Redistribution and use in source and binary forms, with or without
13 1.1 thorpej * modification, are permitted provided that the following conditions
14 1.1 thorpej * are met:
15 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer.
17 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
19 1.1 thorpej * documentation and/or other materials provided with the distribution.
20 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
21 1.1 thorpej * may be used to endorse or promote products derived from this software
22 1.1 thorpej * without specific prior written permission.
23 1.1 thorpej *
24 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 thorpej * SUCH DAMAGE.
35 1.1 thorpej *
36 1.1 thorpej * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej #include <sys/cdefs.h>
40 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: subr_disk_4bsd.c,v 1.3 2026/08/07 13:49:24 thorpej Exp $");
41 1.1 thorpej
42 1.1 thorpej #include <sys/param.h>
43 1.1 thorpej #include <sys/systm.h>
44 1.1 thorpej #include <sys/buf.h>
45 1.1 thorpej #include <sys/disk.h>
46 1.1 thorpej #include <sys/disklabel.h>
47 1.1 thorpej
48 1.1 thorpej /*
49 1.1 thorpej * Implementation of readdisklabel() and writedisklabel() for the simple
50 1.1 thorpej * "basic scheme from 4BSD".
51 1.1 thorpej */
52 1.1 thorpej
53 1.1 thorpej /*
54 1.3 thorpej * We prefer the platform's "native" label sector / offset. Look for
55 1.3 thorpej * it there first. Otherwise, scan the first 3 disk sectors for the
56 1.3 thorpej * label. When updating a label, we will write it to where we found
57 1.3 thorpej * it. If writing a new label from scratch, we will write it to the
58 1.3 thorpej * "native" location.
59 1.3 thorpej */
60 1.3 thorpej static int label_sectors[] = { LABELSECTOR, 0, 1, 2 };
61 1.3 thorpej static int label_offsets[] = { LABELOFFSET, -1, -1, -1 };
62 1.3 thorpej
63 1.3 thorpej #define DISKLABEL_SIZE(x) \
64 1.3 thorpej (offsetof(struct disklabel, d_partitions) + \
65 1.3 thorpej (sizeof(struct partition) * (x)))
66 1.3 thorpej
67 1.3 thorpej /* Smallest 4.4BSD disklabel that was ever in use. */
68 1.3 thorpej #define DISKLABEL_MINSIZE DISKLABEL_SIZE(8)
69 1.3 thorpej
70 1.3 thorpej static bool
71 1.3 thorpej taste_label(const struct disklabel *lp, void *buf, size_t secsize)
72 1.3 thorpej {
73 1.3 thorpej uint16_t npartitions;
74 1.3 thorpej
75 1.3 thorpej if (lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC) {
76 1.3 thorpej return false;
77 1.3 thorpej }
78 1.3 thorpej
79 1.3 thorpej npartitions = lp->d_npartitions;
80 1.3 thorpej
81 1.3 thorpej if ((uintptr_t)lp + DISKLABEL_SIZE(npartitions) >
82 1.3 thorpej (uintptr_t)buf + secsize) {
83 1.3 thorpej /* doesn't fit in the sector. */
84 1.3 thorpej return false;
85 1.3 thorpej }
86 1.3 thorpej
87 1.3 thorpej if (npartitions > MAXPARTITIONS || dkcksum(lp)) {
88 1.3 thorpej /* disklabel is corrupted. */
89 1.3 thorpej return false;
90 1.3 thorpej }
91 1.3 thorpej
92 1.3 thorpej return true;
93 1.3 thorpej }
94 1.3 thorpej
95 1.3 thorpej static struct disklabel *
96 1.3 thorpej find_label_in_sector(void *buf, size_t secsize, int offs)
97 1.3 thorpej {
98 1.3 thorpej struct disklabel *lp = buf;
99 1.3 thorpej uintptr_t lp_lim = ((uintptr_t)buf + secsize - DISKLABEL_MINSIZE);
100 1.3 thorpej
101 1.3 thorpej if (offs != -1) {
102 1.3 thorpej lp = (void *)((uintptr_t)lp + offs);
103 1.3 thorpej return taste_label(lp, buf, secsize) ? lp : NULL;
104 1.3 thorpej }
105 1.3 thorpej
106 1.3 thorpej for (;; lp = (void *)((uintptr_t)lp + sizeof(uint32_t))) {
107 1.3 thorpej if ((uintptr_t)lp > lp_lim) {
108 1.3 thorpej /* Not found in this sector. */
109 1.3 thorpej return NULL;
110 1.3 thorpej }
111 1.3 thorpej if (taste_label(lp, buf, secsize)) {
112 1.3 thorpej return lp;
113 1.3 thorpej }
114 1.3 thorpej }
115 1.3 thorpej }
116 1.3 thorpej
117 1.3 thorpej /*
118 1.1 thorpej * Attempt to read a disk label from a device using the indicated strategy
119 1.1 thorpej * routine. The label must be partly set up before this: secpercyl and
120 1.1 thorpej * anything required in the strategy routine (e.g., sector size) must be
121 1.1 thorpej * filled in before calling us. Returns null on success and an error
122 1.1 thorpej * string on failure.
123 1.1 thorpej */
124 1.1 thorpej const char *
125 1.1 thorpej readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
126 1.1 thorpej struct cpu_disklabel *osdep)
127 1.1 thorpej {
128 1.1 thorpej struct buf *bp;
129 1.1 thorpej struct disklabel *dlp;
130 1.1 thorpej const char *msg = NULL;
131 1.1 thorpej int i;
132 1.1 thorpej
133 1.1 thorpej if (lp->d_secsize == 0)
134 1.1 thorpej lp->d_secsize = DEV_BSIZE;
135 1.1 thorpej if (lp->d_secperunit == 0)
136 1.1 thorpej lp->d_secperunit = 0x1fffffff;
137 1.1 thorpej if (lp->d_npartitions < RAW_PART + 1)
138 1.1 thorpej lp->d_npartitions = RAW_PART + 1;
139 1.1 thorpej for (i = 0; i < RAW_PART; i++) {
140 1.1 thorpej lp->d_partitions[i].p_size = 0;
141 1.1 thorpej lp->d_partitions[i].p_offset = 0;
142 1.1 thorpej }
143 1.1 thorpej if (lp->d_partitions[RAW_PART].p_size == 0)
144 1.1 thorpej lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
145 1.1 thorpej lp->d_partitions[RAW_PART].p_offset = 0;
146 1.1 thorpej
147 1.1 thorpej lp->d_partitions[0].p_size = lp->d_partitions[RAW_PART].p_size;
148 1.1 thorpej lp->d_partitions[0].p_fstype = FS_BSDFFS;
149 1.1 thorpej
150 1.1 thorpej bp = geteblk((int)lp->d_secsize);
151 1.1 thorpej bp->b_dev = dev;
152 1.3 thorpej
153 1.3 thorpej for (i = 0; i < __arraycount(label_sectors); i++) {
154 1.3 thorpej if (disk_read_sectors(strat, lp, bp, label_sectors[i], 1)) {
155 1.3 thorpej msg = "I/O error";
156 1.3 thorpej goto done;
157 1.3 thorpej }
158 1.3 thorpej
159 1.3 thorpej dlp = find_label_in_sector(bp->b_data, DEV_BSIZE,
160 1.3 thorpej label_offsets[i]);
161 1.3 thorpej if (dlp != NULL) {
162 1.1 thorpej *lp = *dlp;
163 1.1 thorpej msg = NULL;
164 1.1 thorpej break;
165 1.1 thorpej }
166 1.1 thorpej }
167 1.3 thorpej done:
168 1.1 thorpej brelse(bp, 0);
169 1.1 thorpej return msg;
170 1.1 thorpej }
171 1.1 thorpej
172 1.1 thorpej /*
173 1.1 thorpej * Write disk label back to device after modification.
174 1.1 thorpej */
175 1.1 thorpej int
176 1.1 thorpej writedisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp,
177 1.1 thorpej struct cpu_disklabel *osdep)
178 1.1 thorpej {
179 1.1 thorpej struct buf *bp;
180 1.1 thorpej struct disklabel *dlp;
181 1.1 thorpej int labelpart;
182 1.3 thorpej int i, error = 0;
183 1.1 thorpej
184 1.1 thorpej labelpart = DISKPART(dev);
185 1.1 thorpej if (lp->d_partitions[labelpart].p_offset != 0) {
186 1.1 thorpej if (lp->d_partitions[0].p_offset != 0)
187 1.1 thorpej return EXDEV; /* not quite right */
188 1.1 thorpej labelpart = 0;
189 1.1 thorpej }
190 1.1 thorpej bp = geteblk((int)lp->d_secsize);
191 1.1 thorpej bp->b_dev = MAKEDISKDEV(major(dev), DISKUNIT(dev), labelpart);
192 1.3 thorpej
193 1.3 thorpej for (i = 0; i < __arraycount(label_sectors); i++) {
194 1.3 thorpej if ((error = disk_read_sectors(strat, lp, bp,
195 1.3 thorpej label_sectors[i], 1)) != 0) {
196 1.3 thorpej goto done;
197 1.3 thorpej }
198 1.3 thorpej
199 1.3 thorpej dlp = find_label_in_sector(bp->b_data, DEV_BSIZE,
200 1.3 thorpej label_offsets[i]);
201 1.3 thorpej if (dlp != NULL) {
202 1.1 thorpej *dlp = *lp;
203 1.1 thorpej bp->b_oflags &= ~(BO_DONE);
204 1.1 thorpej bp->b_flags &= ~(B_READ);
205 1.1 thorpej bp->b_flags |= B_WRITE;
206 1.1 thorpej (*strat)(bp);
207 1.1 thorpej error = biowait(bp);
208 1.1 thorpej goto done;
209 1.1 thorpej }
210 1.1 thorpej }
211 1.1 thorpej error = ESRCH;
212 1.1 thorpej done:
213 1.1 thorpej brelse(bp, 0);
214 1.1 thorpej return error;
215 1.1 thorpej }
216