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: 10 * 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. 20 * 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. 33 * 34 * $DragonFly: src/sys/kern/subr_disklabel64.c,v 1.5 2007/07/20 17:21:51 dillon Exp $ 35 */ 36 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> 47 48 /* 49 * Retrieve the partition start and extent, in blocks. Return 0 on success, 50 * EINVAL on error. 51 */ 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) 55 { 56 struct partition64 *pp; 57 58 if (part >= lp.lab64->d_npartitions) 59 return (EINVAL); 60 61 pp = &lp.lab64->d_partitions[part]; 62 63 if ((pp->p_boffset & (ssp->dss_secsize - 1)) || 64 (pp->p_bsize & (ssp->dss_secsize - 1))) { 65 return (EINVAL); 66 } 67 *start = pp->p_boffset / ssp->dss_secsize; 68 *blocks = pp->p_bsize / ssp->dss_secsize; 69 return(0); 70 } 71 72 /* 73 * Get the filesystem type XXX - diskslices code needs to use uuids 74 */ 75 static void 76 l64_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart) 77 { 78 struct partition64 *pp; 79 const size_t uuid_size = sizeof(struct uuid); 80 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; 90 } 91 } 92 93 /* 94 * Get the number of partitions 95 */ 96 static u_int32_t 97 l64_getnumparts(disklabel_t lp) 98 { 99 return(lp.lab64->d_npartitions); 100 } 101 102 /* 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. 106 * 107 * Returns NULL on sucess, and an error string on failure. 108 */ 109 static const char * 110 l64_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp, 111 struct disk_info *info) 112 { 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; 120 121 /* 122 * XXX I/O size is subject to device DMA limitations 123 */ 124 secsize = info->d_media_blksize; 125 bpsize = (sizeof(*dlp) + secsize - 1) & ~(secsize - 1); 126 127 bp = geteblk(bpsize); 128 bp->b_bio1.bio_offset = 0; 129 bp->b_bio1.bio_done = biodone_sync; 130 bp->b_bio1.bio_flags |= BIO_SYNC; 131 bp->b_bcount = bpsize; 132 bp->b_flags &= ~B_INVAL; 133 bp->b_cmd = BUF_CMD_READ; 134 dev_dstrategy(dev, &bp->b_bio1); 135 136 if (biowait(&bp->b_bio1, "labrd")) { 137 msg = "I/O error"; 138 } else { 139 dlp = (struct disklabel64 *)bp->b_data; 140 dlpcrcsize = offsetof(struct disklabel64, 141 d_partitions[dlp->d_npartitions]) - 142 offsetof(struct disklabel64, d_magic); 143 savecrc = dlp->d_crc; 144 dlp->d_crc = 0; 145 if (dlp->d_magic != DISKMAGIC64) { 146 msg = "no disk label"; 147 } else if (dlp->d_npartitions > MAXPARTITIONS64) { 148 msg = "disklabel64 corrupted, too many partitions"; 149 } else if (savecrc != crc32(&dlp->d_magic, dlpcrcsize)) { 150 msg = "disklabel64 corrupted, bad CRC"; 151 } else { 152 dlp->d_crc = savecrc; 153 (*lpp).lab64 = kmalloc(sizeof(*dlp), 154 M_DEVBUF, M_WAITOK|M_ZERO); 155 *(*lpp).lab64 = *dlp; 156 msg = NULL; 157 } 158 } 159 bp->b_flags |= B_INVAL | B_AGE; 160 brelse(bp); 161 return (msg); 162 } 163 164 /* 165 * If everything is good, copy olpx to nlpx. Check to see if any 166 * open partitions would change. 167 */ 168 static int 169 l64_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp, 170 struct diskslice *sp, u_int32_t *openmask) 171 { 172 struct disklabel64 *olp, *nlp; 173 struct partition64 *opp, *npp; 174 uint32_t savecrc; 175 uint64_t slicebsize; 176 size_t nlpcrcsize; 177 int part; 178 int i; 179 180 olp = olpx.lab64; 181 nlp = nlpx.lab64; 182 183 slicebsize = (uint64_t)sp->ds_size * ssp->dss_secsize; 184 185 if (nlp->d_magic != DISKMAGIC64) 186 return (EINVAL); 187 if (nlp->d_npartitions > MAXPARTITIONS64) 188 return (EINVAL); 189 savecrc = nlp->d_crc; 190 nlp->d_crc = 0; 191 nlpcrcsize = offsetof(struct disklabel64, 192 d_partitions[nlp->d_npartitions]) - 193 offsetof(struct disklabel64, d_magic); 194 if (crc32(&nlp->d_magic, nlpcrcsize) != savecrc) { 195 nlp->d_crc = savecrc; 196 return (EINVAL); 197 } 198 nlp->d_crc = savecrc; 199 200 /* 201 * Check if open partitions have changed 202 */ 203 i = 0; 204 while (i < 128) { 205 if (openmask[i >> 5] == 0) { 206 i += 32; 207 continue; 208 } 209 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) { 210 ++i; 211 continue; 212 } 213 if (nlp->d_npartitions <= i) 214 return (EBUSY); 215 opp = &olp->d_partitions[i]; 216 npp = &nlp->d_partitions[i]; 217 if (npp->p_boffset != opp->p_boffset || 218 npp->p_bsize < opp->p_bsize) { 219 return (EBUSY); 220 } 221 222 /* 223 * Do not allow p_type_uuid or p_stor_uuid to change if 224 * the partition is currently open. 225 */ 226 if (bcmp(&npp->p_type_uuid, &opp->p_type_uuid, 227 sizeof(npp->p_type_uuid)) != 0) { 228 return (EBUSY); 229 } 230 if (bcmp(&npp->p_stor_uuid, &opp->p_stor_uuid, 231 sizeof(npp->p_stor_uuid)) != 0) { 232 return (EBUSY); 233 } 234 ++i; 235 } 236 237 /* 238 * Make sure the label and partition offsets and sizes are sane. 239 */ 240 if (nlp->d_total_size > slicebsize) 241 return (ENOSPC); 242 if (nlp->d_total_size & (ssp->dss_secsize - 1)) 243 return (EINVAL); 244 if (nlp->d_bbase & (ssp->dss_secsize - 1)) 245 return (EINVAL); 246 if (nlp->d_pbase & (ssp->dss_secsize - 1)) 247 return (EINVAL); 248 if (nlp->d_pstop & (ssp->dss_secsize - 1)) 249 return (EINVAL); 250 if (nlp->d_abase & (ssp->dss_secsize - 1)) 251 return (EINVAL); 252 253 for (part = 0; part < nlp->d_npartitions; ++part) { 254 npp = &nlp->d_partitions[i]; 255 if (npp->p_bsize == 0) { 256 if (npp->p_boffset != 0) 257 return (EINVAL); 258 continue; 259 } 260 if (npp->p_boffset & (ssp->dss_secsize - 1)) 261 return (EINVAL); 262 if (npp->p_bsize & (ssp->dss_secsize - 1)) 263 return (EINVAL); 264 if (npp->p_boffset < nlp->d_pbase) 265 return (ENOSPC); 266 if (npp->p_boffset + npp->p_bsize > nlp->d_total_size) 267 return (ENOSPC); 268 } 269 270 /* 271 * Structurally we may add code to make modifications above in the 272 * future, so regenerate the crc anyway. 273 */ 274 nlp->d_crc = 0; 275 nlp->d_crc = crc32(&nlp->d_magic, nlpcrcsize); 276 *olp = *nlp; 277 278 return (0); 279 } 280 281 /* 282 * Write disk label back to device after modification. 283 */ 284 static int 285 l64_writedisklabel(cdev_t dev, struct diskslices *ssp, 286 struct diskslice *sp, disklabel_t lpx) 287 { 288 struct disklabel64 *lp; 289 struct disklabel64 *dlp; 290 struct buf *bp; 291 int error = 0; 292 size_t bpsize; 293 int secsize; 294 295 lp = lpx.lab64; 296 297 kprintf("this is l64_writedisklabel: part: %d, slice: %d\n", dkpart(dev), dkslice(dev)); 298 kprintf("Avoiding disaster and returning now\n"); 299 return 0; 300 301 /* 302 * XXX I/O size is subject to device DMA limitations 303 */ 304 secsize = ssp->dss_secsize; 305 bpsize = (sizeof(*lp) + secsize - 1) & ~(secsize - 1); 306 307 bp = geteblk(bpsize); 308 bp->b_bio1.bio_offset = 0; 309 bp->b_bio1.bio_done = biodone_sync; 310 bp->b_bio1.bio_flags |= BIO_SYNC; 311 bp->b_bcount = bpsize; 312 313 /* 314 * Because our I/O is larger then the label, and because we do not 315 * write the d_reserved0[] area, do a read-modify-write. 316 */ 317 bp->b_flags &= ~B_INVAL; 318 bp->b_cmd = BUF_CMD_READ; 319 dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1); 320 error = biowait(&bp->b_bio1, "labrd"); 321 if (error) 322 goto done; 323 324 dlp = (void *)bp->b_data; 325 bcopy(&lp->d_magic, &dlp->d_magic, 326 sizeof(*lp) - offsetof(struct disklabel64, d_magic)); 327 bp->b_cmd = BUF_CMD_WRITE; 328 bp->b_bio1.bio_done = biodone_sync; 329 bp->b_bio1.bio_flags |= BIO_SYNC; 330 dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1); 331 error = biowait(&bp->b_bio1, "labwr"); 332 done: 333 bp->b_flags |= B_INVAL | B_AGE; 334 brelse(bp); 335 return (error); 336 } 337 338 /* 339 * Create a disklabel based on a disk_info structure for the purposes of 340 * DSO_COMPATLABEL - cases where no real label exists on the storage medium. 341 * 342 * If a diskslice is passed, the label is truncated to the slice. 343 * 344 * NOTE! This is not a legal label because d_bbase and d_pbase are both 345 * set to 0. 346 */ 347 static disklabel_t 348 l64_clone_label(struct disk_info *info, struct diskslice *sp) 349 { 350 struct disklabel64 *lp; 351 disklabel_t res; 352 uint32_t blksize = info->d_media_blksize; 353 size_t lpcrcsize; 354 355 lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO); 356 357 if (sp) 358 lp->d_total_size = (uint64_t)sp->ds_size * blksize; 359 else 360 lp->d_total_size = info->d_media_blocks * blksize; 361 362 lp->d_magic = DISKMAGIC64; 363 lp->d_align = blksize; 364 lp->d_npartitions = MAXPARTITIONS64; 365 lp->d_pstop = lp->d_total_size; 366 367 /* 368 * Create a dummy 'c' part and a dummy 'a' part (if requested). 369 * Note that the 'c' part is really a hack. 64 bit disklabels 370 * do not use 'c' to mean the raw partition. 371 */ 372 373 lp->d_partitions[2].p_boffset = 0; 374 lp->d_partitions[2].p_bsize = lp->d_total_size; 375 /* XXX SET FS TYPE */ 376 377 if (info->d_dsflags & DSO_COMPATPARTA) { 378 lp->d_partitions[0].p_boffset = 0; 379 lp->d_partitions[0].p_bsize = lp->d_total_size; 380 /* XXX SET FS TYPE */ 381 } 382 383 lpcrcsize = offsetof(struct disklabel64, 384 d_partitions[lp->d_npartitions]) - 385 offsetof(struct disklabel64, d_magic); 386 387 lp->d_crc = crc32(&lp->d_magic, lpcrcsize); 388 res.lab64 = lp; 389 return (res); 390 } 391 392 /* 393 * Create a virgin disklabel64 suitable for writing to the media. 394 * 395 * disklabel64 always reserves 32KB for a boot area and leaves room 396 * for up to RESPARTITIONS64 partitions. 397 */ 398 static void 399 l64_makevirginlabel(disklabel_t lpx, struct diskslices *ssp, 400 struct diskslice *sp, struct disk_info *info) 401 { 402 struct disklabel64 *lp = lpx.lab64; 403 struct partition64 *pp; 404 uint32_t blksize; 405 uint32_t ressize; 406 uint64_t blkmask; /* 64 bits so we can ~ */ 407 size_t lpcrcsize; 408 409 /* 410 * Setup the initial label. Use of a block size of at least 4KB 411 * for calculating the initial reserved areas to allow some degree 412 * of portability between media with different sector sizes. 413 * 414 * Note that the modified blksize is stored in d_align as a hint 415 * to the disklabeling program. 416 */ 417 bzero(lp, sizeof(*lp)); 418 if ((blksize = info->d_media_blksize) < 4096) 419 blksize = 4096; 420 blkmask = blksize - 1; 421 422 if (sp) 423 lp->d_total_size = (uint64_t)sp->ds_size * ssp->dss_secsize; 424 else 425 lp->d_total_size = info->d_media_blocks * info->d_media_blksize; 426 427 lp->d_magic = DISKMAGIC64; 428 lp->d_align = blksize; 429 lp->d_npartitions = MAXPARTITIONS64; 430 kern_uuidgen(&lp->d_stor_uuid, 1); 431 432 ressize = offsetof(struct disklabel64, d_partitions[RESPARTITIONS64]); 433 ressize = (ressize + (uint32_t)blkmask) & ~blkmask; 434 435 lp->d_bbase = ressize; 436 lp->d_pbase = lp->d_bbase + ((32768 + blkmask) & ~blkmask); 437 lp->d_pstop = (lp->d_total_size - lp->d_bbase) & ~blkmask; 438 lp->d_abase = lp->d_pstop; 439 440 /* 441 * All partitions are left empty unless DSO_COMPATPARTA is set 442 */ 443 444 if (info->d_dsflags & DSO_COMPATPARTA) { 445 pp = &lp->d_partitions[0]; 446 pp->p_boffset = lp->d_pbase; 447 pp->p_bsize = lp->d_pstop - lp->d_pbase; 448 /* XXX SET FS TYPE */ 449 } 450 451 lpcrcsize = offsetof(struct disklabel64, 452 d_partitions[lp->d_npartitions]) - 453 offsetof(struct disklabel64, d_magic); 454 lp->d_crc = crc32(&lp->d_magic, lpcrcsize); 455 } 456 457 /* 458 * Set the number of blocks at the beginning of the slice which have 459 * been reserved for label operations. This area will be write-protected 460 * when accessed via the slice. 461 * 462 * For now just protect the label area proper. Do not protect the 463 * boot area. Note partitions in 64 bit disklabels do not overlap 464 * the disklabel or boot area. 465 */ 466 static void 467 l64_adjust_label_reserved(struct diskslices *ssp, int slice, 468 struct diskslice *sp) 469 { 470 struct disklabel64 *lp = sp->ds_label.lab64; 471 472 sp->ds_reserved = lp->d_bbase / ssp->dss_secsize; 473 } 474 475 struct disklabel_ops disklabel64_ops = { 476 .labelsize = sizeof(struct disklabel64), 477 .op_readdisklabel = l64_readdisklabel, 478 .op_setdisklabel = l64_setdisklabel, 479 .op_writedisklabel = l64_writedisklabel, 480 .op_clone_label = l64_clone_label, 481 .op_adjust_label_reserved = l64_adjust_label_reserved, 482 .op_getpartbounds = l64_getpartbounds, 483 .op_loadpartinfo = l64_loadpartinfo, 484 .op_getnumparts = l64_getnumparts, 485 .op_makevirginlabel = l64_makevirginlabel 486 }; 487 488