1 /*- 2 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> 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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* udf_vfsops.c */ 30 /* Implement the VFS side of things */ 31 32 /* 33 * Ok, here's how it goes. The UDF specs are pretty clear on how each data 34 * structure is made up, but not very clear on how they relate to each other. 35 * Here is the skinny... This demostrates a filesystem with one file in the 36 * root directory. Subdirectories are treated just as normal files, but they 37 * have File Id Descriptors of their children as their file data. As for the 38 * Anchor Volume Descriptor Pointer, it can exist in two of the following three 39 * places: sector 256, sector n (the max sector of the disk), or sector 40 * n - 256. It's a pretty good bet that one will exist at sector 256 though. 41 * One caveat is unclosed CD media. For that, sector 256 cannot be written, 42 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the 43 * media is closed. 44 * 45 * Sector: 46 * 256: 47 * n: Anchor Volume Descriptor Pointer 48 * n - 256: | 49 * | 50 * |-->Main Volume Descriptor Sequence 51 * | | 52 * | | 53 * | |-->Logical Volume Descriptor 54 * | | 55 * |-->Partition Descriptor | 56 * | | 57 * | | 58 * |-->Fileset Descriptor 59 * | 60 * | 61 * |-->Root Dir File Entry 62 * | 63 * | 64 * |-->File data: 65 * File Id Descriptor 66 * | 67 * | 68 * |-->File Entry 69 * | 70 * | 71 * |-->File data 72 */ 73 #include <sys/types.h> 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/uio.h> 77 #include <sys/bio.h> 78 #include <sys/buf.h> 79 #include <sys/conf.h> 80 #include <sys/dirent.h> 81 #include <sys/fcntl.h> 82 #include <sys/iconv.h> 83 #include <sys/kernel.h> 84 #include <sys/malloc.h> 85 #include <sys/mount.h> 86 #include <sys/namei.h> 87 #include <sys/proc.h> 88 #include <sys/queue.h> 89 #include <sys/vnode.h> 90 #include <sys/endian.h> 91 92 #include <geom/geom.h> 93 #include <geom/geom_vfs.h> 94 95 #include <vm/uma.h> 96 97 #include <fs/udf/ecma167-udf.h> 98 #include <fs/udf/osta.h> 99 #include <fs/udf/udf.h> 100 #include <fs/udf/udf_mount.h> 101 102 static MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure"); 103 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure"); 104 105 struct iconv_functions *udf_iconv = NULL; 106 107 /* Zones */ 108 uma_zone_t udf_zone_trans = NULL; 109 uma_zone_t udf_zone_node = NULL; 110 uma_zone_t udf_zone_ds = NULL; 111 112 static vfs_init_t udf_init; 113 static vfs_uninit_t udf_uninit; 114 static vfs_mount_t udf_mount; 115 static vfs_root_t udf_root; 116 static vfs_statfs_t udf_statfs; 117 static vfs_unmount_t udf_unmount; 118 static vfs_fhtovp_t udf_fhtovp; 119 static vfs_vptofh_t udf_vptofh; 120 121 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *); 122 123 static struct vfsops udf_vfsops = { 124 .vfs_fhtovp = udf_fhtovp, 125 .vfs_init = udf_init, 126 .vfs_mount = udf_mount, 127 .vfs_root = udf_root, 128 .vfs_statfs = udf_statfs, 129 .vfs_uninit = udf_uninit, 130 .vfs_unmount = udf_unmount, 131 .vfs_vget = udf_vget, 132 .vfs_vptofh = udf_vptofh, 133 }; 134 VFS_SET(udf_vfsops, udf, VFCF_READONLY); 135 136 MODULE_VERSION(udf, 1); 137 138 static int udf_mountfs(struct vnode *, struct mount *, struct thread *); 139 140 static int 141 udf_init(struct vfsconf *foo) 142 { 143 144 /* 145 * This code used to pre-allocate a certain number of pages for each 146 * pool, reducing the need to grow the zones later on. UMA doesn't 147 * advertise any such functionality, unfortunately =-< 148 */ 149 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN * 150 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0); 151 152 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node), 153 NULL, NULL, NULL, NULL, 0, 0); 154 155 udf_zone_ds = uma_zcreate("UDF Dirstream zone", 156 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0); 157 158 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) || 159 (udf_zone_ds == NULL)) { 160 printf("Cannot create allocation zones.\n"); 161 return (ENOMEM); 162 } 163 164 return 0; 165 } 166 167 static int 168 udf_uninit(struct vfsconf *foo) 169 { 170 171 if (udf_zone_trans != NULL) { 172 uma_zdestroy(udf_zone_trans); 173 udf_zone_trans = NULL; 174 } 175 176 if (udf_zone_node != NULL) { 177 uma_zdestroy(udf_zone_node); 178 udf_zone_node = NULL; 179 } 180 181 if (udf_zone_ds != NULL) { 182 uma_zdestroy(udf_zone_ds); 183 udf_zone_ds = NULL; 184 } 185 186 return (0); 187 } 188 189 static int 190 udf_mount(struct mount *mp, struct thread *td) 191 { 192 struct vnode *devvp; /* vnode of the mount device */ 193 struct udf_mnt *imp = 0; 194 struct export_args *export; 195 struct vfsoptlist *opts; 196 char *fspec, *cs_disk, *cs_local; 197 int error, len, *udf_flags; 198 struct nameidata nd, *ndp = &nd; 199 200 opts = mp->mnt_optnew; 201 202 if ((mp->mnt_flag & MNT_RDONLY) == 0) 203 return (EROFS); 204 205 /* 206 * No root filesystem support. Probably not a big deal, since the 207 * bootloader doesn't understand UDF. 208 */ 209 if (mp->mnt_flag & MNT_ROOTFS) 210 return (ENOTSUP); 211 212 fspec = NULL; 213 error = vfs_getopt(opts, "from", (void **)&fspec, &len); 214 if (!error && fspec[len - 1] != '\0') 215 return (EINVAL); 216 217 if (mp->mnt_flag & MNT_UPDATE) { 218 imp = VFSTOUDFFS(mp); 219 if (fspec == NULL) { 220 error = vfs_getopt(opts, "export", (void **)&export, 221 &len); 222 if (error || len != sizeof(struct export_args)) 223 return (EINVAL); 224 return (vfs_export(mp, export)); 225 } 226 } 227 228 /* Check that the mount device exists */ 229 if (fspec == NULL) 230 return (EINVAL); 231 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td); 232 if ((error = namei(ndp))) 233 return (error); 234 NDFREE(ndp, NDF_ONLY_PNBUF); 235 devvp = ndp->ni_vp; 236 237 if (vn_isdisk(devvp, &error) == 0) { 238 vput(devvp); 239 return (error); 240 } 241 242 /* Check the access rights on the mount device */ 243 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td); 244 if (error) 245 error = suser(td); 246 if (error) { 247 vput(devvp); 248 return (error); 249 } 250 251 if ((error = udf_mountfs(devvp, mp, td))) { 252 vrele(devvp); 253 return (error); 254 } 255 256 imp = VFSTOUDFFS(mp); 257 258 udf_flags = NULL; 259 error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len); 260 if (error || len != sizeof(int)) 261 return (EINVAL); 262 imp->im_flags = *udf_flags; 263 264 if (imp->im_flags & UDFMNT_KICONV && udf_iconv) { 265 cs_disk = NULL; 266 error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len); 267 if (!error && cs_disk[len - 1] != '\0') 268 return (EINVAL); 269 cs_local = NULL; 270 error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len); 271 if (!error && cs_local[len - 1] != '\0') 272 return (EINVAL); 273 udf_iconv->open(cs_local, cs_disk, &imp->im_d2l); 274 #if 0 275 udf_iconv->open(cs_disk, cs_local, &imp->im_l2d); 276 #endif 277 } 278 279 vfs_mountedfrom(mp, fspec); 280 return 0; 281 }; 282 283 /* 284 * Check the descriptor tag for both the correct id and correct checksum. 285 * Return zero if all is good, EINVAL if not. 286 */ 287 int 288 udf_checktag(struct desc_tag *tag, uint16_t id) 289 { 290 uint8_t *itag; 291 uint8_t i, cksum = 0; 292 293 itag = (uint8_t *)tag; 294 295 if (tag->id != id) 296 return (EINVAL); 297 298 for (i = 0; i < 15; i++) 299 cksum = cksum + itag[i]; 300 cksum = cksum - itag[4]; 301 302 if (cksum == tag->cksum) 303 return (0); 304 305 return (EINVAL); 306 } 307 308 static int 309 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) { 310 struct buf *bp = NULL; 311 struct anchor_vdp avdp; 312 struct udf_mnt *udfmp = NULL; 313 struct part_desc *pd; 314 struct logvol_desc *lvd; 315 struct fileset_desc *fsd; 316 struct file_entry *root_fentry; 317 uint32_t sector, size, mvds_start, mvds_end; 318 uint32_t fsd_offset = 0; 319 uint16_t part_num = 0, fsd_part = 0; 320 int error = EINVAL; 321 int logvol_found = 0, part_found = 0, fsd_found = 0; 322 int bsize; 323 struct g_consumer *cp; 324 struct bufobj *bo; 325 326 DROP_GIANT(); 327 g_topology_lock(); 328 error = g_vfs_open(devvp, &cp, "udf", 0); 329 g_topology_unlock(); 330 PICKUP_GIANT(); 331 VOP_UNLOCK(devvp, 0, td); 332 if (error) 333 return error; 334 335 bo = &devvp->v_bufobj; 336 337 /* XXX: should be M_WAITOK */ 338 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT, 339 M_NOWAIT | M_ZERO); 340 if (udfmp == NULL) { 341 printf("Cannot allocate UDF mount struct\n"); 342 error = ENOMEM; 343 goto bail; 344 } 345 346 mp->mnt_data = (qaddr_t)udfmp; 347 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev); 348 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 349 mp->mnt_flag |= MNT_LOCAL; 350 udfmp->im_mountp = mp; 351 udfmp->im_dev = devvp->v_rdev; 352 udfmp->im_devvp = devvp; 353 udfmp->im_d2l = NULL; 354 udfmp->im_cp = cp; 355 udfmp->im_bo = bo; 356 357 #if 0 358 udfmp->im_l2d = NULL; 359 #endif 360 361 bsize = 2048; /* XXX Should probe the media for it's size */ 362 363 /* 364 * Get the Anchor Volume Descriptor Pointer from sector 256. 365 * XXX Should also check sector n - 256, n, and 512. 366 */ 367 sector = 256; 368 if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED, 369 &bp)) != 0) 370 goto bail; 371 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR))) 372 goto bail; 373 374 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp)); 375 brelse(bp); 376 bp = NULL; 377 378 /* 379 * Extract the Partition Descriptor and Logical Volume Descriptor 380 * from the Volume Descriptor Sequence. 381 * XXX Should we care about the partition type right now? 382 * XXX What about multiple partitions? 383 */ 384 mvds_start = le32toh(avdp.main_vds_ex.loc); 385 mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize; 386 for (sector = mvds_start; sector < mvds_end; sector++) { 387 if ((error = bread(devvp, sector * btodb(bsize), bsize, 388 NOCRED, &bp)) != 0) { 389 printf("Can't read sector %d of VDS\n", sector); 390 goto bail; 391 } 392 lvd = (struct logvol_desc *)bp->b_data; 393 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) { 394 udfmp->bsize = le32toh(lvd->lb_size); 395 udfmp->bmask = udfmp->bsize - 1; 396 udfmp->bshift = ffs(udfmp->bsize) - 1; 397 fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num); 398 fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num); 399 if (udf_find_partmaps(udfmp, lvd)) 400 break; 401 logvol_found = 1; 402 } 403 pd = (struct part_desc *)bp->b_data; 404 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) { 405 part_found = 1; 406 part_num = le16toh(pd->part_num); 407 udfmp->part_len = le32toh(pd->part_len); 408 udfmp->part_start = le32toh(pd->start_loc); 409 } 410 411 brelse(bp); 412 bp = NULL; 413 if ((part_found) && (logvol_found)) 414 break; 415 } 416 417 if (!part_found || !logvol_found) { 418 error = EINVAL; 419 goto bail; 420 } 421 422 if (fsd_part != part_num) { 423 printf("FSD does not lie within the partition!\n"); 424 error = EINVAL; 425 goto bail; 426 } 427 428 429 /* 430 * Grab the Fileset Descriptor 431 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing 432 * me in the right direction here. 433 */ 434 sector = udfmp->part_start + fsd_offset; 435 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) { 436 printf("Cannot read sector %d of FSD\n", sector); 437 goto bail; 438 } 439 fsd = (struct fileset_desc *)bp->b_data; 440 if (!udf_checktag(&fsd->tag, TAGID_FSD)) { 441 fsd_found = 1; 442 bcopy(&fsd->rootdir_icb, &udfmp->root_icb, 443 sizeof(struct long_ad)); 444 } 445 446 brelse(bp); 447 bp = NULL; 448 449 if (!fsd_found) { 450 printf("Couldn't find the fsd\n"); 451 error = EINVAL; 452 goto bail; 453 } 454 455 /* 456 * Find the file entry for the root directory. 457 */ 458 sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start; 459 size = le32toh(udfmp->root_icb.len); 460 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) { 461 printf("Cannot read sector %d\n", sector); 462 goto bail; 463 } 464 465 root_fentry = (struct file_entry *)bp->b_data; 466 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) { 467 printf("Invalid root file entry!\n"); 468 goto bail; 469 } 470 471 brelse(bp); 472 bp = NULL; 473 474 return 0; 475 476 bail: 477 if (udfmp != NULL) 478 FREE(udfmp, M_UDFMOUNT); 479 if (bp != NULL) 480 brelse(bp); 481 DROP_GIANT(); 482 g_topology_lock(); 483 g_vfs_close(cp, td); 484 g_topology_unlock(); 485 PICKUP_GIANT(); 486 return error; 487 }; 488 489 static int 490 udf_unmount(struct mount *mp, int mntflags, struct thread *td) 491 { 492 struct udf_mnt *udfmp; 493 int error, flags = 0; 494 495 udfmp = VFSTOUDFFS(mp); 496 497 if (mntflags & MNT_FORCE) 498 flags |= FORCECLOSE; 499 500 if ((error = vflush(mp, 0, flags, td))) 501 return (error); 502 503 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) { 504 if (udfmp->im_d2l) 505 udf_iconv->close(udfmp->im_d2l); 506 #if 0 507 if (udfmp->im_l2d) 508 udf_iconv->close(udfmp->im_l2d); 509 #endif 510 } 511 512 DROP_GIANT(); 513 g_topology_lock(); 514 g_vfs_close(udfmp->im_cp, td); 515 g_topology_unlock(); 516 PICKUP_GIANT(); 517 vrele(udfmp->im_devvp); 518 519 if (udfmp->s_table != NULL) 520 FREE(udfmp->s_table, M_UDFMOUNT); 521 522 FREE(udfmp, M_UDFMOUNT); 523 524 mp->mnt_data = (qaddr_t)0; 525 mp->mnt_flag &= ~MNT_LOCAL; 526 527 return (0); 528 } 529 530 static int 531 udf_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 532 { 533 struct udf_mnt *udfmp; 534 struct vnode *vp; 535 ino_t id; 536 int error; 537 538 udfmp = VFSTOUDFFS(mp); 539 540 id = udf_getid(&udfmp->root_icb); 541 542 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp); 543 if (error) 544 return error; 545 546 vp = *vpp; 547 vp->v_vflag |= VV_ROOT; 548 549 return (0); 550 } 551 552 static int 553 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 554 { 555 struct udf_mnt *udfmp; 556 557 udfmp = VFSTOUDFFS(mp); 558 559 sbp->f_bsize = udfmp->bsize; 560 sbp->f_iosize = udfmp->bsize; 561 sbp->f_blocks = udfmp->part_len; 562 sbp->f_bfree = 0; 563 sbp->f_bavail = 0; 564 sbp->f_files = 0; 565 sbp->f_ffree = 0; 566 return 0; 567 } 568 569 int 570 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 571 { 572 struct buf *bp; 573 struct vnode *devvp; 574 struct udf_mnt *udfmp; 575 struct thread *td; 576 struct vnode *vp; 577 struct udf_node *unode; 578 struct file_entry *fe; 579 int error, sector, size; 580 581 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL); 582 if (error || *vpp != NULL) 583 return (error); 584 585 td = curthread; 586 udfmp = VFSTOUDFFS(mp); 587 588 unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO); 589 590 if ((error = udf_allocv(mp, &vp, td))) { 591 printf("Error from udf_allocv\n"); 592 uma_zfree(udf_zone_node, unode); 593 return (error); 594 } 595 596 unode->i_vnode = vp; 597 unode->hash_id = ino; 598 unode->udfmp = udfmp; 599 vp->v_data = unode; 600 601 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL); 602 if (error || *vpp != NULL) 603 return (error); 604 605 /* 606 * Copy in the file entry. Per the spec, the size can only be 1 block. 607 */ 608 sector = ino + udfmp->part_start; 609 devvp = udfmp->im_devvp; 610 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) { 611 printf("Cannot read sector %d\n", sector); 612 vput(vp); 613 brelse(bp); 614 *vpp = NULL; 615 return (error); 616 } 617 618 fe = (struct file_entry *)bp->b_data; 619 if (udf_checktag(&fe->tag, TAGID_FENTRY)) { 620 printf("Invalid file entry!\n"); 621 vput(vp); 622 brelse(bp); 623 *vpp = NULL; 624 return (ENOMEM); 625 } 626 size = UDF_FENTRY_SIZE + le32toh(fe->l_ea) + le32toh(fe->l_ad); 627 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY, 628 M_NOWAIT | M_ZERO); 629 if (unode->fentry == NULL) { 630 printf("Cannot allocate file entry block\n"); 631 vput(vp); 632 brelse(bp); 633 *vpp = NULL; 634 return (ENOMEM); 635 } 636 637 bcopy(bp->b_data, unode->fentry, size); 638 639 brelse(bp); 640 bp = NULL; 641 642 switch (unode->fentry->icbtag.file_type) { 643 default: 644 vp->v_type = VBAD; 645 break; 646 case 4: 647 vp->v_type = VDIR; 648 break; 649 case 5: 650 vp->v_type = VREG; 651 break; 652 case 6: 653 vp->v_type = VBLK; 654 break; 655 case 7: 656 vp->v_type = VCHR; 657 break; 658 case 9: 659 vp->v_type = VFIFO; 660 break; 661 case 10: 662 vp->v_type = VSOCK; 663 break; 664 case 12: 665 vp->v_type = VLNK; 666 break; 667 } 668 *vpp = vp; 669 670 return (0); 671 } 672 673 struct ifid { 674 u_short ifid_len; 675 u_short ifid_pad; 676 int ifid_ino; 677 long ifid_start; 678 }; 679 680 static int 681 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 682 { 683 struct ifid *ifhp; 684 struct vnode *nvp; 685 int error; 686 687 ifhp = (struct ifid *)fhp; 688 689 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) { 690 *vpp = NULLVP; 691 return (error); 692 } 693 694 *vpp = nvp; 695 vnode_create_vobject(*vpp, 0, curthread); 696 return (0); 697 } 698 699 static int 700 udf_vptofh (struct vnode *vp, struct fid *fhp) 701 { 702 struct udf_node *node; 703 struct ifid *ifhp; 704 705 node = VTON(vp); 706 ifhp = (struct ifid *)fhp; 707 ifhp->ifid_len = sizeof(struct ifid); 708 ifhp->ifid_ino = node->hash_id; 709 710 return (0); 711 } 712 713 static int 714 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd) 715 { 716 union udf_pmap *pmap; 717 struct part_map_spare *pms; 718 struct regid *pmap_id; 719 struct buf *bp; 720 unsigned char regid_id[UDF_REGID_ID_SIZE + 1]; 721 int i, ptype, psize, error; 722 723 for (i = 0; i < le32toh(lvd->n_pm); i++) { 724 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE]; 725 ptype = pmap->data[0]; 726 psize = pmap->data[1]; 727 if (((ptype != 1) && (ptype != 2)) || 728 ((psize != UDF_PMAP_SIZE) && (psize != 6))) { 729 printf("Invalid partition map found\n"); 730 return (1); 731 } 732 733 if (ptype == 1) { 734 /* Type 1 map. We don't care */ 735 continue; 736 } 737 738 /* Type 2 map. Gotta find out the details */ 739 pmap_id = (struct regid *)&pmap->data[4]; 740 bzero(®id_id[0], UDF_REGID_ID_SIZE); 741 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE); 742 743 if (bcmp(®id_id[0], "*UDF Sparable Partition", 744 UDF_REGID_ID_SIZE)) { 745 printf("Unsupported partition map: %s\n", ®id_id[0]); 746 return (1); 747 } 748 749 pms = &pmap->pms; 750 MALLOC(udfmp->s_table, struct udf_sparing_table *, 751 le32toh(pms->st_size), M_UDFMOUNT, M_NOWAIT | M_ZERO); 752 if (udfmp->s_table == NULL) 753 return (ENOMEM); 754 755 /* Calculate the number of sectors per packet. */ 756 /* XXX Logical or physical? */ 757 udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize; 758 759 /* 760 * XXX If reading the first Sparing Table fails, should look 761 * for another table. 762 */ 763 if ((error = udf_readlblks(udfmp, le32toh(pms->st_loc[0]), 764 le32toh(pms->st_size), &bp)) != 0) { 765 if (bp != NULL) 766 brelse(bp); 767 printf("Failed to read Sparing Table at sector %d\n", 768 le32toh(pms->st_loc[0])); 769 return (error); 770 } 771 bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size)); 772 brelse(bp); 773 774 if (udf_checktag(&udfmp->s_table->tag, 0)) { 775 printf("Invalid sparing table found\n"); 776 return (EINVAL); 777 } 778 779 /* See how many valid entries there are here. The list is 780 * supposed to be sorted. 0xfffffff0 and higher are not valid 781 */ 782 for (i = 0; i < le16toh(udfmp->s_table->rt_l); i++) { 783 udfmp->s_table_entries = i; 784 if (le32toh(udfmp->s_table->entries[i].org) >= 785 0xfffffff0) 786 break; 787 } 788 } 789 790 return (0); 791 } 792