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/namei.h> 76 #include <sys/vnode.h> 77 #include <sys/mount.h> 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/fcntl.h> 81 #include <sys/bio.h> 82 #include <sys/buf.h> 83 #include <sys/malloc.h> 84 #include <sys/kernel.h> 85 #include <sys/mount.h> 86 #include <sys/conf.h> 87 #include <sys/queue.h> 88 #include <sys/dirent.h> 89 90 #include <vm/uma.h> 91 92 #include <fs/udf/ecma167-udf.h> 93 #include <fs/udf/udf.h> 94 #include <fs/udf/udf_mount.h> 95 #include <fs/udf/osta.h> 96 97 MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure"); 98 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure"); 99 MALLOC_DEFINE(M_UDFSTABLE, "UDF s_table", "UDF sparing table"); 100 101 /* Zones */ 102 uma_zone_t udf_zone_trans = NULL; 103 uma_zone_t udf_zone_node = NULL; 104 105 static int udf_init(struct vfsconf *); 106 static int udf_uninit(struct vfsconf *); 107 static int udf_mount(struct mount *, char *, caddr_t, struct nameidata *, 108 struct thread *); 109 static int udf_unmount(struct mount *, int, struct thread *); 110 static int udf_root(struct mount *, struct vnode **); 111 static int udf_statfs(struct mount *, struct statfs *, struct thread *); 112 static int udf_fhtovp(struct mount *, struct fid *, struct vnode **); 113 static int udf_vptofh(struct vnode *, struct fid *); 114 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *); 115 116 static struct vfsops udf_vfsops = { 117 udf_mount, 118 vfs_stdstart, 119 udf_unmount, 120 udf_root, 121 vfs_stdquotactl, 122 udf_statfs, 123 vfs_stdsync, 124 udf_vget, 125 udf_fhtovp, 126 vfs_stdcheckexp, 127 udf_vptofh, 128 udf_init, 129 udf_uninit, 130 vfs_stdextattrctl, 131 }; 132 VFS_SET(udf_vfsops, udf, VFCF_READONLY); 133 134 static int udf_mountfs(struct vnode *, struct mount *, struct thread *, struct udf_args *); 135 136 static int 137 udf_init(struct vfsconf *foo) 138 { 139 140 /* 141 * This code used to pre-allocate a certain number of pages for each 142 * pool, reducing the need to grow the zones later on. UMA doesn't 143 * advertise any such functionality, unfortunately =-< 144 */ 145 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN * 146 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0); 147 148 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node), 149 NULL, NULL, NULL, NULL, 0, 0); 150 151 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL)) { 152 printf("Cannot create allocation zones.\n"); 153 return (ENOMEM); 154 } 155 156 return 0; 157 } 158 159 static int 160 udf_uninit(struct vfsconf *foo) 161 { 162 163 if (udf_zone_trans != NULL) { 164 uma_zdestroy(udf_zone_trans); 165 udf_zone_trans = NULL; 166 } 167 168 if (udf_zone_node != NULL) { 169 uma_zdestroy(udf_zone_node); 170 udf_zone_node = NULL; 171 } 172 173 return (0); 174 } 175 176 static int 177 udf_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp, struct thread *td) 178 { 179 struct vnode *devvp; /* vnode of the mount device */ 180 struct udf_args args; 181 struct udf_mnt *imp = 0; 182 size_t size; 183 int error; 184 185 if ((mp->mnt_flag & MNT_RDONLY) == 0) 186 return (EROFS); 187 188 /* 189 * No root filesystem support. Probably not a big deal, since the 190 * bootloader doesn't understand UDF. 191 */ 192 if (mp->mnt_flag & MNT_ROOTFS) 193 return (ENOTSUP); 194 195 if ((error = copyin(data, (caddr_t)&args, sizeof(struct udf_args)))) 196 return (error); 197 198 if (mp->mnt_flag & MNT_UPDATE) { 199 imp = VFSTOUDFFS(mp); 200 if (args.fspec == 0) 201 return (vfs_export(mp, &args.export)); 202 } 203 204 /* Check that the mount device exists */ 205 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td); 206 if ((error = namei(ndp))) 207 return (error); 208 NDFREE(ndp, NDF_ONLY_PNBUF); 209 devvp = ndp->ni_vp; 210 211 if (vn_isdisk(devvp, &error) == 0) { 212 vrele(devvp); 213 return (error); 214 } 215 216 /* Check the access rights on the mount device */ 217 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 218 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td); 219 if (error) 220 error = suser(td); 221 if (error) { 222 vput(devvp); 223 return (error); 224 } 225 VOP_UNLOCK(devvp, 0, td); 226 227 if ((error = udf_mountfs(devvp, mp, td, &args))) { 228 vrele(devvp); 229 return (error); 230 } 231 232 imp = VFSTOUDFFS(mp); 233 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 234 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 235 udf_statfs(mp, &mp->mnt_stat, td); 236 return 0; 237 }; 238 239 /* 240 * Check the descriptor tag for both the correct id and correct checksum. 241 * Return zero if all is good, EINVAL if not. 242 */ 243 int 244 udf_checktag(struct desc_tag *tag, uint16_t id) 245 { 246 uint8_t *itag; 247 uint8_t i, cksum = 0; 248 249 itag = (uint8_t *)tag; 250 251 if (tag->id != id) 252 return (EINVAL); 253 254 for (i = 0; i < 15; i++) 255 cksum = cksum + itag[i]; 256 cksum = cksum - itag[4]; 257 258 if (cksum == tag->cksum) 259 return (0); 260 261 return (EINVAL); 262 } 263 264 static int 265 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td, struct udf_args *argp) { 266 struct buf *bp = NULL; 267 struct anchor_vdp avdp; 268 struct udf_mnt *udfmp = NULL; 269 struct part_desc *pd; 270 struct logvol_desc *lvd; 271 struct fileset_desc *fsd; 272 struct file_entry *root_fentry; 273 uint32_t sector, size, mvds_start, mvds_end; 274 uint32_t fsd_offset = 0; 275 uint16_t part_num = 0, fsd_part = 0; 276 int error = EINVAL, needclose = 0; 277 int logvol_found = 0, part_found = 0, fsd_found = 0; 278 int bsize; 279 280 /* 281 * Disallow multiple mounts of the same device. Flush the buffer 282 * cache for the device. 283 */ 284 if ((error = vfs_mountedon(devvp))) 285 return (error); 286 if (vcount(devvp) > 1) 287 return (EBUSY); 288 if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0))) 289 return (error); 290 291 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 292 error = VOP_OPEN(devvp, FREAD, FSCRED, td); 293 VOP_UNLOCK(devvp, 0, td); 294 if (error) 295 return error; 296 needclose = 1; 297 298 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT, 299 M_NOWAIT | M_ZERO); 300 if (udfmp == NULL) { 301 printf("Cannot allocate UDF mount struct\n"); 302 error = ENOMEM; 303 goto bail; 304 } 305 306 mp->mnt_data = (qaddr_t)udfmp; 307 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev); 308 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 309 mp->mnt_flag |= MNT_LOCAL; 310 udfmp->im_mountp = mp; 311 udfmp->im_dev = devvp->v_rdev; 312 udfmp->im_devvp = devvp; 313 314 bsize = 2048; /* XXX Should probe the media for it's size */ 315 316 /* 317 * Get the Anchor Volume Descriptor Pointer from sector 256. 318 * XXX Should also check sector n - 256, n, and 512. 319 */ 320 sector = 256; 321 if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED, 322 &bp)) != 0) 323 goto bail; 324 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR))) 325 goto bail; 326 327 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp)); 328 brelse(bp); 329 bp = NULL; 330 331 /* 332 * Extract the Partition Descriptor and Logical Volume Descriptor 333 * from the Volume Descriptor Sequence. 334 * XXX Should we care about the partition type right now? 335 * XXX What about multiple partitions? 336 */ 337 mvds_start = avdp.main_vds_ex.loc; 338 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize; 339 for (sector = mvds_start; sector < mvds_end; sector++) { 340 if ((error = bread(devvp, sector * btodb(bsize), bsize, 341 NOCRED, &bp)) != 0) { 342 printf("Can't read sector %d of VDS\n", sector); 343 goto bail; 344 } 345 lvd = (struct logvol_desc *)bp->b_data; 346 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) { 347 udfmp->bsize = lvd->lb_size; 348 udfmp->bmask = udfmp->bsize - 1; 349 udfmp->bshift = ffs(udfmp->bsize) - 1; 350 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num; 351 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num; 352 if (udf_find_partmaps(udfmp, lvd)) 353 break; 354 logvol_found = 1; 355 } 356 pd = (struct part_desc *)bp->b_data; 357 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) { 358 part_found = 1; 359 part_num = pd->part_num; 360 udfmp->part_len = pd->part_len; 361 udfmp->part_start = pd->start_loc; 362 } 363 364 brelse(bp); 365 bp = NULL; 366 if ((part_found) && (logvol_found)) 367 break; 368 } 369 370 if (!part_found || !logvol_found) { 371 error = EINVAL; 372 goto bail; 373 } 374 375 if (fsd_part != part_num) { 376 printf("FSD does not lie within the partition!\n"); 377 error = EINVAL; 378 goto bail; 379 } 380 381 382 /* 383 * Grab the Fileset Descriptor 384 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing 385 * me in the right direction here. 386 */ 387 sector = udfmp->part_start + fsd_offset; 388 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) { 389 printf("Cannot read sector %d of FSD\n", sector); 390 goto bail; 391 } 392 fsd = (struct fileset_desc *)bp->b_data; 393 if (!udf_checktag(&fsd->tag, TAGID_FSD)) { 394 fsd_found = 1; 395 bcopy(&fsd->rootdir_icb, &udfmp->root_icb, 396 sizeof(struct long_ad)); 397 } 398 399 brelse(bp); 400 bp = NULL; 401 402 if (!fsd_found) { 403 printf("Couldn't find the fsd\n"); 404 error = EINVAL; 405 goto bail; 406 } 407 408 /* 409 * Find the file entry for the root directory. 410 */ 411 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start; 412 size = udfmp->root_icb.len; 413 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) { 414 printf("Cannot read sector %d\n", sector); 415 goto bail; 416 } 417 418 root_fentry = (struct file_entry *)bp->b_data; 419 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) { 420 printf("Invalid root file entry!\n"); 421 goto bail; 422 } 423 424 brelse(bp); 425 bp = NULL; 426 427 TAILQ_INIT(&udfmp->udf_tqh); 428 devvp->v_rdev->si_mountpoint = mp; 429 430 mtx_init(&udfmp->hash_mtx, "udf_hash", NULL, MTX_DEF); 431 return 0; 432 433 bail: 434 if (udfmp != NULL) 435 FREE(udfmp, M_UDFMOUNT); 436 if (bp != NULL) 437 brelse(bp); 438 if (needclose) 439 VOP_CLOSE(devvp, FREAD, NOCRED, td); 440 return error; 441 }; 442 443 static int 444 udf_unmount(struct mount *mp, int mntflags, struct thread *td) 445 { 446 struct udf_mnt *udfmp; 447 int error, flags = 0; 448 449 udfmp = VFSTOUDFFS(mp); 450 451 if (mntflags & MNT_FORCE) 452 flags |= FORCECLOSE; 453 454 if ((error = vflush(mp, 0, flags))) 455 return (error); 456 457 udfmp->im_devvp->v_rdev->si_mountpoint = NULL; 458 error = VOP_CLOSE(udfmp->im_devvp, FREAD, NOCRED, td); 459 vrele(udfmp->im_devvp); 460 461 if (udfmp->s_table != NULL) 462 FREE(udfmp->s_table, M_UDFSTABLE); 463 FREE(udfmp, M_UDFMOUNT); 464 465 mp->mnt_data = (qaddr_t)0; 466 mp->mnt_flag &= ~MNT_LOCAL; 467 468 return (0); 469 } 470 471 static int 472 udf_root(struct mount *mp, struct vnode **vpp) 473 { 474 struct udf_mnt *udfmp; 475 struct vnode *vp; 476 ino_t id; 477 int error; 478 479 udfmp = VFSTOUDFFS(mp); 480 481 id = udf_getid(&udfmp->root_icb); 482 483 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp); 484 if (error) 485 return error; 486 487 vp = *vpp; 488 vp->v_flag |= VROOT; 489 udfmp->root_vp = vp; 490 491 return (0); 492 } 493 494 static int 495 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 496 { 497 struct udf_mnt *udfmp; 498 499 udfmp = VFSTOUDFFS(mp); 500 501 sbp->f_bsize = udfmp->bsize; 502 sbp->f_iosize = udfmp->bsize; 503 sbp->f_blocks = udfmp->part_len; 504 sbp->f_bfree = 0; 505 sbp->f_bavail = 0; 506 sbp->f_files = 0; 507 sbp->f_ffree = 0; 508 if (sbp != &mp->mnt_stat) { 509 sbp->f_type = mp->mnt_vfc->vfc_typenum; 510 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 511 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 512 } 513 514 return 0; 515 } 516 517 int 518 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 519 { 520 struct buf *bp; 521 struct vnode *devvp; 522 struct udf_mnt *udfmp; 523 struct thread *td; 524 struct vnode *vp; 525 struct udf_node *unode; 526 struct file_entry *fe; 527 int error, sector, size; 528 529 td = curthread; 530 udfmp = VFSTOUDFFS(mp); 531 532 /* See if we already have this in the cache */ 533 if ((error = udf_hashlookup(udfmp, ino, flags, vpp)) != 0) 534 return (error); 535 if (*vpp != NULL) { 536 return (0); 537 } 538 539 /* 540 * Allocate memory and check the tag id's before grabbing a new 541 * vnode, since it's hard to roll back if there is a problem. 542 */ 543 unode = uma_zalloc(udf_zone_node, M_WAITOK); 544 if (unode == NULL) { 545 printf("Cannot allocate udf node\n"); 546 return (ENOMEM); 547 } 548 549 /* 550 * Copy in the file entry. Per the spec, the size can only be 1 block. 551 */ 552 sector = ino + udfmp->part_start; 553 devvp = udfmp->im_devvp; 554 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) { 555 printf("Cannot read sector %d\n", sector); 556 uma_zfree(udf_zone_node, unode); 557 return (error); 558 } 559 560 fe = (struct file_entry *)bp->b_data; 561 if (udf_checktag(&fe->tag, TAGID_FENTRY)) { 562 printf("Invalid file entry!\n"); 563 uma_zfree(udf_zone_node, unode); 564 brelse(bp); 565 return (ENOMEM); 566 } 567 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad; 568 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY, 569 M_NOWAIT | M_ZERO); 570 if (unode->fentry == NULL) { 571 printf("Cannot allocate file entry block\n"); 572 uma_zfree(udf_zone_node, unode); 573 brelse(bp); 574 return (ENOMEM); 575 } 576 577 bcopy(bp->b_data, unode->fentry, size); 578 579 brelse(bp); 580 bp = NULL; 581 582 if ((error = udf_allocv(mp, &vp, td))) { 583 printf("Error from udf_allocv\n"); 584 uma_zfree(udf_zone_node, unode); 585 return (error); 586 } 587 588 unode->i_vnode = vp; 589 unode->hash_id = ino; 590 unode->i_devvp = udfmp->im_devvp; 591 unode->i_dev = udfmp->im_dev; 592 unode->udfmp = udfmp; 593 vp->v_data = unode; 594 lockinit(&vp->v_lock, PINOD, "udfnode", 0, 0); 595 vp->v_vnlock = &vp->v_lock; 596 VREF(udfmp->im_devvp); 597 udf_hashins(unode); 598 599 switch (unode->fentry->icbtag.file_type) { 600 default: 601 vp->v_type = VBAD; 602 break; 603 case 4: 604 vp->v_type = VDIR; 605 break; 606 case 5: 607 vp->v_type = VREG; 608 break; 609 case 6: 610 vp->v_type = VBLK; 611 break; 612 case 7: 613 vp->v_type = VCHR; 614 break; 615 case 9: 616 vp->v_type = VFIFO; 617 break; 618 case 10: 619 vp->v_type = VSOCK; 620 break; 621 case 12: 622 vp->v_type = VLNK; 623 break; 624 } 625 *vpp = vp; 626 627 return (0); 628 } 629 630 struct ifid { 631 ushort ifid_len; 632 ushort ifid_pad; 633 int ifid_ino; 634 long ifid_start; 635 }; 636 637 static int 638 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 639 { 640 struct ifid *ifhp; 641 struct vnode *nvp; 642 int error; 643 644 ifhp = (struct ifid *)fhp; 645 646 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) { 647 *vpp = NULLVP; 648 return (error); 649 } 650 651 *vpp = nvp; 652 return (0); 653 } 654 655 static int 656 udf_vptofh (struct vnode *vp, struct fid *fhp) 657 { 658 struct udf_node *node; 659 struct ifid *ifhp; 660 661 node = VTON(vp); 662 ifhp = (struct ifid *)fhp; 663 ifhp->ifid_len = sizeof(struct ifid); 664 ifhp->ifid_ino = node->hash_id; 665 666 return (0); 667 } 668 669 static int 670 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd) 671 { 672 union udf_pmap *pmap; 673 struct part_map_spare *pms; 674 struct regid *pmap_id; 675 struct buf *bp; 676 unsigned char regid_id[UDF_REGID_ID_SIZE + 1]; 677 int i, ptype, psize, error; 678 679 for (i = 0; i < lvd->n_pm; i++) { 680 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE]; 681 ptype = pmap->data[0]; 682 psize = pmap->data[1]; 683 if (((ptype != 1) && (ptype != 2)) || 684 ((psize != UDF_PMAP_SIZE) && (psize != 6))) { 685 printf("Invalid partition map found\n"); 686 return (1); 687 } 688 689 if (ptype == 1) { 690 /* Type 1 map. We don't care */ 691 continue; 692 } 693 694 /* Type 2 map. Gotta find out the details */ 695 pmap_id = (struct regid *)&pmap->data[4]; 696 bzero(®id_id[0], UDF_REGID_ID_SIZE); 697 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE); 698 699 if (bcmp(®id_id[0], "*UDF Sparable Partition", 700 UDF_REGID_ID_SIZE)) { 701 printf("Unsupported partition map: %s\n", ®id_id[0]); 702 return (1); 703 } 704 705 pms = &pmap->pms; 706 MALLOC(udfmp->s_table, struct udf_sparing_table *, pms->st_size, 707 M_UDFSTABLE, M_NOWAIT | M_ZERO); 708 if (udfmp->s_table == NULL) 709 return (ENOMEM); 710 711 /* Calculate the number of sectors per packet. */ 712 /* XXX Logical or physical? */ 713 udfmp->p_sectors = pms->packet_len / udfmp->bsize; 714 715 /* 716 * XXX If reading the first Sparing Table fails, should look 717 * for another table. 718 */ 719 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size, 720 &bp)) != 0) { 721 printf("Failed to read Sparing Table at sector %d\n", 722 pms->st_loc[0]); 723 return (error); 724 } 725 bcopy(bp->b_data, udfmp->s_table, pms->st_size); 726 brelse(bp); 727 728 if (udf_checktag(&udfmp->s_table->tag, 0)) { 729 printf("Invalid sparing table found\n"); 730 return (EINVAL); 731 } 732 733 /* See how many valid entries there are here. The list is 734 * supposed to be sorted. 0xfffffff0 and higher are not valid 735 */ 736 for (i = 0; i < udfmp->s_table->rt_l; i++) { 737 udfmp->s_table_entries = i; 738 if (udfmp->s_table->entries[i].org >= 0xfffffff0) 739 break; 740 } 741 } 742 743 return (0); 744 } 745