i4b(4): Initialize callout handles before first accessing them.
[dragonfly.git] / sys / kern / subr_disklabel64.c
blobae85b27be775887f5af6ab198735495eb95810a7
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/kern/subr_disklabel64.c,v 1.5 2007/07/20 17:21:51 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/conf.h>
41 #include <sys/disklabel.h>
42 #include <sys/disklabel64.h>
43 #include <sys/diskslice.h>
44 #include <sys/disk.h>
45 #include <sys/kern_syscall.h>
46 #include <sys/buf2.h>
49 * Retrieve the partition start and extent, in blocks. Return 0 on success,
50 * EINVAL on error.
52 static int
53 l64_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
54 u_int64_t *start, u_int64_t *blocks)
56 struct partition64 *pp;
58 if (part >= lp.lab64->d_npartitions)
59 return (EINVAL);
61 pp = &lp.lab64->d_partitions[part];
63 if ((pp->p_boffset & (ssp->dss_secsize - 1)) ||
64 (pp->p_bsize & (ssp->dss_secsize - 1))) {
65 return (EINVAL);
67 *start = pp->p_boffset / ssp->dss_secsize;
68 *blocks = pp->p_bsize / ssp->dss_secsize;
69 return(0);
73 * Get the filesystem type XXX - diskslices code needs to use uuids
75 static void
76 l64_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
78 struct partition64 *pp;
79 const size_t uuid_size = sizeof(struct uuid);
81 if (part < lp.lab64->d_npartitions) {
82 pp = &lp.lab64->d_partitions[part];
83 dpart->fstype_uuid = pp->p_type_uuid;
84 dpart->storage_uuid = pp->p_stor_uuid;
85 dpart->fstype = pp->p_fstype;
86 } else {
87 bzero(&dpart->fstype_uuid, uuid_size);
88 bzero(&dpart->storage_uuid, uuid_size);
89 dpart->fstype = 0;
94 * Get the number of partitions
96 static u_int32_t
97 l64_getnumparts(disklabel_t lp)
99 return(lp.lab64->d_npartitions);
103 * Attempt to read a disk label from a device. 64 bit disklabels are
104 * sector-agnostic and begin at offset 0 on the device. 64 bit disklabels
105 * may only be used with GPT partitioning schemes.
107 * Returns NULL on sucess, and an error string on failure.
109 static const char *
110 l64_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
111 struct disk_info *info)
113 struct buf *bp;
114 struct disklabel64 *dlp;
115 const char *msg;
116 uint32_t savecrc;
117 size_t dlpcrcsize;
118 size_t bpsize;
119 int secsize;
122 * XXX I/O size is subject to device DMA limitations
124 secsize = info->d_media_blksize;
125 bpsize = (sizeof(*dlp) + secsize - 1) & ~(secsize - 1);
127 bp = geteblk(bpsize);
128 bp->b_bio1.bio_offset = 0;
129 bp->b_bcount = bpsize;
130 bp->b_flags &= ~B_INVAL;
131 bp->b_cmd = BUF_CMD_READ;
132 dev_dstrategy(dev, &bp->b_bio1);
134 if (biowait(bp)) {
135 msg = "I/O error";
136 } else {
137 dlp = (struct disklabel64 *)bp->b_data;
138 dlpcrcsize = offsetof(struct disklabel64,
139 d_partitions[dlp->d_npartitions]) -
140 offsetof(struct disklabel64, d_magic);
141 savecrc = dlp->d_crc;
142 dlp->d_crc = 0;
143 if (dlp->d_magic != DISKMAGIC64) {
144 msg = "no disk label";
145 } else if (dlp->d_npartitions > MAXPARTITIONS64) {
146 msg = "disklabel64 corrupted, too many partitions";
147 } else if (savecrc != crc32(&dlp->d_magic, dlpcrcsize)) {
148 msg = "disklabel64 corrupted, bad CRC";
149 } else {
150 dlp->d_crc = savecrc;
151 (*lpp).lab64 = kmalloc(sizeof(*dlp),
152 M_DEVBUF, M_WAITOK|M_ZERO);
153 *(*lpp).lab64 = *dlp;
154 msg = NULL;
157 bp->b_flags |= B_INVAL | B_AGE;
158 brelse(bp);
159 return (msg);
163 * If everything is good, copy olpx to nlpx. Check to see if any
164 * open partitions would change.
166 static int
167 l64_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
168 struct diskslice *sp, u_int32_t *openmask)
170 struct disklabel64 *olp, *nlp;
171 struct partition64 *opp, *npp;
172 uint32_t savecrc;
173 uint64_t slicebsize;
174 size_t nlpcrcsize;
175 int part;
176 int i;
178 olp = olpx.lab64;
179 nlp = nlpx.lab64;
181 slicebsize = (uint64_t)sp->ds_size * ssp->dss_secsize;
183 if (nlp->d_magic != DISKMAGIC64)
184 return (EINVAL);
185 if (nlp->d_npartitions > MAXPARTITIONS64)
186 return (EINVAL);
187 savecrc = nlp->d_crc;
188 nlp->d_crc = 0;
189 nlpcrcsize = offsetof(struct disklabel64,
190 d_partitions[nlp->d_npartitions]) -
191 offsetof(struct disklabel64, d_magic);
192 if (crc32(&nlp->d_magic, nlpcrcsize) != savecrc) {
193 nlp->d_crc = savecrc;
194 return (EINVAL);
196 nlp->d_crc = savecrc;
199 * Check if open partitions have changed
201 i = 0;
202 while (i < 128) {
203 if (openmask[i >> 5] == 0) {
204 i += 32;
205 continue;
207 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
208 ++i;
209 continue;
211 if (nlp->d_npartitions <= i)
212 return (EBUSY);
213 opp = &olp->d_partitions[i];
214 npp = &nlp->d_partitions[i];
215 if (npp->p_boffset != opp->p_boffset ||
216 npp->p_bsize < opp->p_bsize) {
217 return (EBUSY);
221 * Do not allow p_type_uuid or p_stor_uuid to change if
222 * the partition is currently open.
224 if (bcmp(&npp->p_type_uuid, &opp->p_type_uuid,
225 sizeof(npp->p_type_uuid)) != 0) {
226 return (EBUSY);
228 if (bcmp(&npp->p_stor_uuid, &opp->p_stor_uuid,
229 sizeof(npp->p_stor_uuid)) != 0) {
230 return (EBUSY);
232 ++i;
236 * Make sure the label and partition offsets and sizes are sane.
238 if (nlp->d_total_size > slicebsize)
239 return (ENOSPC);
240 if (nlp->d_total_size & (ssp->dss_secsize - 1))
241 return (EINVAL);
242 if (nlp->d_bbase & (ssp->dss_secsize - 1))
243 return (EINVAL);
244 if (nlp->d_pbase & (ssp->dss_secsize - 1))
245 return (EINVAL);
246 if (nlp->d_pstop & (ssp->dss_secsize - 1))
247 return (EINVAL);
248 if (nlp->d_abase & (ssp->dss_secsize - 1))
249 return (EINVAL);
251 for (part = 0; part < nlp->d_npartitions; ++part) {
252 npp = &nlp->d_partitions[i];
253 if (npp->p_bsize == 0) {
254 if (npp->p_boffset != 0)
255 return (EINVAL);
256 continue;
258 if (npp->p_boffset & (ssp->dss_secsize - 1))
259 return (EINVAL);
260 if (npp->p_bsize & (ssp->dss_secsize - 1))
261 return (EINVAL);
262 if (npp->p_boffset < nlp->d_pbase)
263 return (ENOSPC);
264 if (npp->p_boffset + npp->p_bsize > nlp->d_total_size)
265 return (ENOSPC);
269 * Structurally we may add code to make modifications above in the
270 * future, so regenerate the crc anyway.
272 nlp->d_crc = 0;
273 nlp->d_crc = crc32(&nlp->d_magic, nlpcrcsize);
274 *olp = *nlp;
276 return (0);
280 * Write disk label back to device after modification.
282 static int
283 l64_writedisklabel(cdev_t dev, struct diskslices *ssp,
284 struct diskslice *sp, disklabel_t lpx)
286 struct disklabel64 *lp;
287 struct disklabel64 *dlp;
288 struct buf *bp;
289 int error = 0;
290 size_t bpsize;
291 int secsize;
293 lp = lpx.lab64;
296 * XXX I/O size is subject to device DMA limitations
298 secsize = ssp->dss_secsize;
299 bpsize = (sizeof(*lp) + secsize - 1) & ~(secsize - 1);
301 bp = geteblk(bpsize);
302 bp->b_bio1.bio_offset = 0;
303 bp->b_bcount = bpsize;
306 * Because our I/O is larger then the label, and because we do not
307 * write the d_reserved0[] area, do a read-modify-write.
309 bp->b_flags &= ~B_INVAL;
310 bp->b_cmd = BUF_CMD_READ;
311 dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1);
312 error = biowait(bp);
313 if (error)
314 goto done;
316 dlp = (void *)bp->b_data;
317 bcopy(&lp->d_magic, &dlp->d_magic,
318 sizeof(*lp) - offsetof(struct disklabel64, d_magic));
319 bp->b_cmd = BUF_CMD_WRITE;
320 dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1);
321 error = biowait(bp);
322 done:
323 bp->b_flags |= B_INVAL | B_AGE;
324 brelse(bp);
325 return (error);
329 * Create a disklabel based on a disk_info structure for the purposes of
330 * DSO_COMPATLABEL - cases where no real label exists on the storage medium.
332 * If a diskslice is passed, the label is truncated to the slice.
334 * NOTE! This is not a legal label because d_bbase and d_pbase are both
335 * set to 0.
337 static disklabel_t
338 l64_clone_label(struct disk_info *info, struct diskslice *sp)
340 struct disklabel64 *lp;
341 disklabel_t res;
342 uint32_t blksize = info->d_media_blksize;
343 size_t lpcrcsize;
345 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
347 if (sp)
348 lp->d_total_size = (uint64_t)sp->ds_size * blksize;
349 else
350 lp->d_total_size = info->d_media_blocks * blksize;
352 lp->d_magic = DISKMAGIC64;
353 lp->d_align = blksize;
354 lp->d_npartitions = MAXPARTITIONS64;
355 lp->d_pstop = lp->d_total_size;
358 * Create a dummy 'c' part and a dummy 'a' part (if requested).
359 * Note that the 'c' part is really a hack. 64 bit disklabels
360 * do not use 'c' to mean the raw partition.
363 lp->d_partitions[2].p_boffset = 0;
364 lp->d_partitions[2].p_bsize = lp->d_total_size;
365 /* XXX SET FS TYPE */
367 if (info->d_dsflags & DSO_COMPATPARTA) {
368 lp->d_partitions[0].p_boffset = 0;
369 lp->d_partitions[0].p_bsize = lp->d_total_size;
370 /* XXX SET FS TYPE */
373 lpcrcsize = offsetof(struct disklabel64,
374 d_partitions[lp->d_npartitions]) -
375 offsetof(struct disklabel64, d_magic);
377 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
378 res.lab64 = lp;
379 return (res);
383 * Create a virgin disklabel64 suitable for writing to the media.
385 * disklabel64 always reserves 32KB for a boot area and leaves room
386 * for up to RESPARTITIONS64 partitions.
388 static void
389 l64_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
390 struct diskslice *sp, struct disk_info *info)
392 struct disklabel64 *lp = lpx.lab64;
393 struct partition64 *pp;
394 uint32_t blksize;
395 uint32_t ressize;
396 uint64_t blkmask; /* 64 bits so we can ~ */
397 size_t lpcrcsize;
400 * Setup the initial label. Use of a block size of at least 4KB
401 * for calculating the initial reserved areas to allow some degree
402 * of portability between media with different sector sizes.
404 * Note that the modified blksize is stored in d_align as a hint
405 * to the disklabeling program.
407 bzero(lp, sizeof(*lp));
408 if ((blksize = info->d_media_blksize) < 4096)
409 blksize = 4096;
410 blkmask = blksize - 1;
412 if (sp)
413 lp->d_total_size = (uint64_t)sp->ds_size * ssp->dss_secsize;
414 else
415 lp->d_total_size = info->d_media_blocks * info->d_media_blksize;
417 lp->d_magic = DISKMAGIC64;
418 lp->d_align = blksize;
419 lp->d_npartitions = MAXPARTITIONS64;
420 kern_uuidgen(&lp->d_stor_uuid, 1);
422 ressize = offsetof(struct disklabel64, d_partitions[RESPARTITIONS64]);
423 ressize = (ressize + (uint32_t)blkmask) & ~blkmask;
425 lp->d_bbase = ressize;
426 lp->d_pbase = lp->d_bbase + ((32768 + blkmask) & ~blkmask);
427 lp->d_pstop = (lp->d_total_size - lp->d_bbase) & ~blkmask;
428 lp->d_abase = lp->d_pstop;
431 * All partitions are left empty unless DSO_COMPATPARTA is set
434 if (info->d_dsflags & DSO_COMPATPARTA) {
435 pp = &lp->d_partitions[0];
436 pp->p_boffset = lp->d_pbase;
437 pp->p_bsize = lp->d_pstop - lp->d_pbase;
438 /* XXX SET FS TYPE */
441 lpcrcsize = offsetof(struct disklabel64,
442 d_partitions[lp->d_npartitions]) -
443 offsetof(struct disklabel64, d_magic);
444 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
448 * Set the number of blocks at the beginning of the slice which have
449 * been reserved for label operations. This area will be write-protected
450 * when accessed via the slice.
452 * For now just protect the label area proper. Do not protect the
453 * boot area. Note partitions in 64 bit disklabels do not overlap
454 * the disklabel or boot area.
456 static void
457 l64_adjust_label_reserved(struct diskslices *ssp, int slice,
458 struct diskslice *sp)
460 struct disklabel64 *lp = sp->ds_label.lab64;
462 sp->ds_reserved = lp->d_bbase / ssp->dss_secsize;
465 struct disklabel_ops disklabel64_ops = {
466 .labelsize = sizeof(struct disklabel64),
467 .op_readdisklabel = l64_readdisklabel,
468 .op_setdisklabel = l64_setdisklabel,
469 .op_writedisklabel = l64_writedisklabel,
470 .op_clone_label = l64_clone_label,
471 .op_adjust_label_reserved = l64_adjust_label_reserved,
472 .op_getpartbounds = l64_getpartbounds,
473 .op_loadpartinfo = l64_loadpartinfo,
474 .op_getnumparts = l64_getnumparts,
475 .op_makevirginlabel = l64_makevirginlabel