Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / vfs / udf / udf_vfsops.c
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: src/sys/fs/udf/udf_vfsops.c,v 1.16 2003/11/05 06:56:08 scottl Exp $
27  * $DragonFly: src/sys/vfs/udf/udf_vfsops.c,v 1.7 2004/08/17 18:57:35 dillon Exp $
28  */
29
30 /* udf_vfsops.c */
31 /* Implement the VFS side of things */
32
33 /*
34  * Ok, here's how it goes.  The UDF specs are pretty clear on how each data
35  * structure is made up, but not very clear on how they relate to each other.
36  * Here is the skinny... This demostrates a filesystem with one file in the
37  * root directory.  Subdirectories are treated just as normal files, but they
38  * have File Id Descriptors of their children as their file data.  As for the
39  * Anchor Volume Descriptor Pointer, it can exist in two of the following three
40  * places: sector 256, sector n (the max sector of the disk), or sector
41  * n - 256.  It's a pretty good bet that one will exist at sector 256 though.
42  * One caveat is unclosed CD media.  For that, sector 256 cannot be written,
43  * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
44  * media is closed.
45  *
46  *  Sector:
47  *     256:
48  *       n: Anchor Volume Descriptor Pointer
49  * n - 256:     |
50  *              |
51  *              |-->Main Volume Descriptor Sequence
52  *                      |       |
53  *                      |       |
54  *                      |       |-->Logical Volume Descriptor
55  *                      |                         |
56  *                      |-->Partition Descriptor  |
57  *                              |                 |
58  *                              |                 |
59  *                              |-->Fileset Descriptor
60  *                                      |
61  *                                      |
62  *                                      |-->Root Dir File Entry
63  *                                              |
64  *                                              |
65  *                                              |-->File data:
66  *                                                  File Id Descriptor
67  *                                                      |
68  *                                                      |
69  *                                                      |-->File Entry
70  *                                                              |
71  *                                                              |
72  *                                                              |-->File data
73  */
74
75 #include <sys/types.h>
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/uio.h>
79 #include <sys/buf.h>
80 #include <sys/conf.h>
81 #include <sys/dirent.h>
82 #include <sys/fcntl.h>
83 #include <sys/module.h>
84 #include <sys/kernel.h>
85 #include <sys/malloc.h>
86 #include <sys/mount.h>
87 #include <sys/namei.h>
88 #include <sys/proc.h>
89 #include <sys/queue.h>
90 #include <sys/vnode.h>
91
92 #include <vfs/udf/ecma167-udf.h>
93 #include <vfs/udf/osta.h>
94 #include <vfs/udf/udf.h>
95 #include <vfs/udf/udf_mount.h>
96
97 extern struct vnodeopv_entry_desc udf_vnodeop_entries[];
98
99 MALLOC_DEFINE(M_UDFNODE, "UDF node", "UDF node structure");
100 MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
101 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
102
103 static int udf_mount(struct mount *, char *, caddr_t, struct nameidata *,
104                      struct thread *);
105 static int udf_unmount(struct mount *, int, struct thread *);
106 static int udf_root(struct mount *, struct vnode **);
107 static int udf_statfs(struct mount *, struct statfs *, struct thread *);
108 static int udf_fhtovp(struct mount *, struct fid *, struct vnode **);
109 static int udf_vptofh(struct vnode *, struct fid *);
110
111 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
112
113 static struct vfsops udf_vfsops = {
114         udf_mount,
115         vfs_stdstart,
116         udf_unmount,
117         udf_root,
118         vfs_stdquotactl,
119         udf_statfs,
120         vfs_stdsync,
121         udf_vget,
122         udf_fhtovp,
123         vfs_stdcheckexp,
124         udf_vptofh,
125         vfs_stdinit,
126         vfs_stduninit,
127         vfs_stdextattrctl,
128 };
129 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
130
131 MODULE_VERSION(udf, 1);
132
133 static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
134
135 static int
136 udf_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
137           struct thread *td)
138 {
139         struct vnode *devvp;    /* vnode of the mount device */
140         struct udf_args args;
141         struct udf_mnt *imp = 0;
142         size_t size;
143         int error;
144
145         if ((mp->mnt_flag & MNT_RDONLY) == 0)
146                 return (EROFS);
147
148         /*
149          * No root filesystem support.  Probably not a big deal, since the
150          * bootloader doesn't understand UDF.
151          */
152         if (mp->mnt_flag & MNT_ROOTFS)
153                 return (ENOTSUP);
154
155         if ((error = copyin(data, (caddr_t)&args, sizeof(struct udf_args))))
156                 return(error);
157
158         if (mp->mnt_flag & MNT_UPDATE) {
159                 imp = VFSTOUDFFS(mp);
160                 if (args.fspec == NULL)
161                         return(vfs_export(mp, &imp->im_export, &args.export));
162         }
163
164         /* Check that the mount device exists */
165         NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
166         if ((error = namei(ndp)))
167                 return(error);
168         NDFREE(ndp, NDF_ONLY_PNBUF);
169         devvp = ndp->ni_vp;
170
171         if (vn_isdisk(devvp, &error) == 0) {
172                 vrele(devvp);
173                 return(error);
174         }
175
176         /* Check the access rights on the mount device */
177         vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
178         error = VOP_ACCESS(devvp, VREAD, td->td_proc->p_ucred, td);
179         if (error)
180                 error = suser(td);
181         if (error) {
182                 vput(devvp);
183                 return(error);
184         }
185         VOP_UNLOCK(devvp, NULL, 0, td);
186
187         if ((error = udf_mountfs(devvp, mp, td))) {
188                 vrele(devvp);
189                 return(error);
190         }
191
192         imp = VFSTOUDFFS(mp);
193
194         imp->im_flags = args.flags;
195
196         copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
197         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
198         copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
199         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
200         udf_statfs(mp, &mp->mnt_stat, td);
201         return(0);
202 }
203
204 /*
205  * Check the descriptor tag for both the correct id and correct checksum.
206  * Return zero if all is good, EINVAL if not.
207  */
208 int
209 udf_checktag(struct desc_tag *tag, uint16_t id)
210 {
211         uint8_t *itag;
212         uint8_t i, cksum = 0;
213
214         itag = (uint8_t *)tag;
215
216         if (tag->id != id)
217                 return(EINVAL);
218
219         for (i = 0; i < 15; i++)
220                 cksum = cksum + itag[i];
221         cksum = cksum - itag[4];
222
223         if (cksum == tag->cksum)
224                 return(0);
225
226         return(EINVAL);
227 }
228
229 static int
230 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) 
231 {
232         struct buf *bp = NULL;
233         struct anchor_vdp avdp;
234         struct udf_mnt *udfmp = NULL;
235         struct part_desc *pd;
236         struct logvol_desc *lvd;
237         struct fileset_desc *fsd;
238         struct file_entry *root_fentry;
239         dev_t dev;
240         uint32_t sector, size, mvds_start, mvds_end;
241         uint32_t fsd_offset = 0;
242         uint16_t part_num = 0, fsd_part = 0;
243         int error = EINVAL, needclose = 0;
244         int logvol_found = 0, part_found = 0, fsd_found = 0;
245         int bsize;
246
247         /*
248          * Disallow multiple mounts of the same device. Flush the buffer
249          * cache for the device.
250          */
251         if ((error = vfs_mountedon(devvp)))
252                 return(error);
253         if (count_udev(devvp->v_udev) > 0)
254                 return(EBUSY);
255         if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
256                 return(error);
257
258         vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
259         error = VOP_OPEN(devvp, FREAD, FSCRED, td);
260         VOP_UNLOCK(devvp, NULL, 0, td);
261         if (error)
262                 return(error);
263         needclose = 1;
264         dev = devvp->v_rdev;
265
266         udfmp = malloc(sizeof(*udfmp), M_UDFMOUNT, M_WAITOK | M_ZERO);
267
268         mp->mnt_data = (qaddr_t)udfmp;
269         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
270         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
271         mp->mnt_maxsymlinklen = 0;
272         mp->mnt_flag |= MNT_LOCAL;
273         udfmp->im_mountp = mp;
274         udfmp->im_dev = dev;
275         udfmp->im_devvp = devvp;
276
277         bsize = 2048;   /* XXX Should probe the media for it's size */
278
279         /* 
280          * Get the Anchor Volume Descriptor Pointer from sector 256.
281          * XXX Should also check sector n - 256, n, and 512.
282          */
283         sector = 256;
284         if ((error = bread(devvp, sector * btodb(bsize), bsize, &bp)) != 0)
285                 goto bail;
286         if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
287                 goto bail;
288
289         bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
290         brelse(bp);
291         bp = NULL;
292
293         /*
294          * Extract the Partition Descriptor and Logical Volume Descriptor
295          * from the Volume Descriptor Sequence.
296          * XXX Should we care about the partition type right now?
297          * XXX What about multiple partitions?
298          */
299         mvds_start = avdp.main_vds_ex.loc;
300         mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
301         for (sector = mvds_start; sector < mvds_end; sector++) {
302                 if ((error = bread(devvp, sector * btodb(bsize), bsize,
303                                    &bp)) != 0) {
304                         printf("Can't read sector %d of VDS\n", sector);
305                         goto bail;
306                 }
307                 lvd = (struct logvol_desc *)bp->b_data;
308                 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
309                         udfmp->bsize = lvd->lb_size;
310                         udfmp->bmask = udfmp->bsize - 1;
311                         udfmp->bshift = ffs(udfmp->bsize) - 1;
312                         fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
313                         fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
314                         if (udf_find_partmaps(udfmp, lvd))
315                                 break;
316                         logvol_found = 1;
317                 }
318                 pd = (struct part_desc *)bp->b_data;
319                 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
320                         part_found = 1;
321                         part_num = pd->part_num;
322                         udfmp->part_len = pd->part_len;
323                         udfmp->part_start = pd->start_loc;
324                 }
325
326                 brelse(bp); 
327                 bp = NULL;
328                 if ((part_found) && (logvol_found))
329                         break;
330         }
331
332         if (!part_found || !logvol_found) {
333                 error = EINVAL;
334                 goto bail;
335         }
336
337         if (fsd_part != part_num) {
338                 printf("FSD does not lie within the partition!\n");
339                 error = EINVAL;
340                 goto bail;
341         }
342
343
344         /*
345          * Grab the Fileset Descriptor
346          * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
347          * me in the right direction here.
348          */
349         sector = udfmp->part_start + fsd_offset;
350         if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
351                 printf("Cannot read sector %d of FSD\n", sector);
352                 goto bail;
353         }
354         fsd = (struct fileset_desc *)bp->b_data;
355         if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
356                 fsd_found = 1;
357                 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
358                       sizeof(struct long_ad));
359         }
360
361         brelse(bp);
362         bp = NULL;
363
364         if (!fsd_found) {
365                 printf("Couldn't find the fsd\n");
366                 error = EINVAL;
367                 goto bail;
368         } 
369
370         vfs_add_vnodeops(&mp->mnt_vn_ops, udf_vnodeop_entries);
371
372         /*
373          * Find the file entry for the root directory.
374          */
375         sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
376         size = udfmp->root_icb.len;
377         if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
378                 printf("Cannot read sector %d\n", sector);
379                 goto bail;
380         }
381
382         root_fentry = (struct file_entry *)bp->b_data;
383         if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
384                 printf("Invalid root file entry!\n");
385                 goto bail;
386         }
387
388         brelse(bp);
389         bp = NULL;
390
391         lwkt_token_init(&udfmp->hash_token);
392         udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
393
394         return(0);
395
396 bail:
397         if (udfmp != NULL)
398                 free(udfmp, M_UDFMOUNT);
399         if (bp != NULL)
400                 brelse(bp);
401         if (needclose)
402                 VOP_CLOSE(devvp, FREAD, td);
403         return(error);
404 }
405
406 static int
407 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
408 {
409         struct udf_mnt *udfmp;
410         int error, flags = 0;
411
412         udfmp = VFSTOUDFFS(mp);
413
414         if (mntflags & MNT_FORCE)
415                 flags |= FORCECLOSE;
416
417         if ((error = vflush(mp, 0, flags)))
418                 return (error);
419
420         udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
421         error = VOP_CLOSE(udfmp->im_devvp, FREAD, td);
422         vrele(udfmp->im_devvp);
423
424         if (udfmp->s_table)
425                 free(udfmp->s_table, M_UDFMOUNT);
426         if (udfmp->hashtbl)
427                 free(udfmp->hashtbl, M_UDFMOUNT);
428         free(udfmp, M_UDFMOUNT);
429
430         mp->mnt_data = (qaddr_t)0;
431         mp->mnt_flag &= ~MNT_LOCAL;
432
433         return (error);
434 }
435
436 static int
437 udf_root(struct mount *mp, struct vnode **vpp)
438 {
439         struct udf_mnt *udfmp;
440         struct vnode *vp;
441         ino_t id;
442         int error;
443
444         udfmp = VFSTOUDFFS(mp);
445
446         id = udf_getid(&udfmp->root_icb);
447
448         error = udf_vget(mp, id, vpp);
449         if (error)
450                 return(error);
451
452         vp = *vpp;
453         vp->v_flag |= VROOT;
454         udfmp->root_vp = vp;
455
456         return(0);
457 }
458
459 static int
460 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
461 {
462         struct udf_mnt *udfmp;
463
464         udfmp = VFSTOUDFFS(mp);
465
466         sbp->f_bsize = udfmp->bsize;
467         sbp->f_iosize = udfmp->bsize;
468         sbp->f_blocks = udfmp->part_len;
469         sbp->f_bfree = 0;
470         sbp->f_bavail = 0;
471         sbp->f_files = 0;
472         sbp->f_ffree = 0;
473         if (sbp != &mp->mnt_stat) {
474                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
475                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
476                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
477         }
478
479         return(0);
480 }
481
482 int
483 udf_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
484 {
485         struct buf *bp;
486         struct vnode *devvp;
487         struct udf_mnt *udfmp;
488         struct thread *td;
489         struct vnode *vp;
490         struct udf_node *unode;
491         struct file_entry *fe;
492         int error, sector, size;
493
494         td = curthread;
495         udfmp = VFSTOUDFFS(mp);
496
497         /* See if we already have this in the cache */
498         if ((error = udf_hashlookup(udfmp, ino, vpp)) != 0)
499                 return(error);
500         if (*vpp != NULL) {
501                 return(0);
502         }
503
504         /*
505          * Allocate memory and check the tag id's before grabbing a new
506          * vnode, since it's hard to roll back if there is a problem.
507          */
508         unode = malloc(sizeof(*unode), M_UDFNODE, M_WAITOK | M_ZERO);
509
510         /*
511          * Copy in the file entry.  Per the spec, the size can only be 1 block.
512          */
513         sector = ino + udfmp->part_start;
514         devvp = udfmp->im_devvp;
515         if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
516                 printf("Cannot read sector %d\n", sector);
517                 free(unode, M_UDFNODE);
518                 return(error);
519         }
520
521         fe = (struct file_entry *)bp->b_data;
522         if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
523                 printf("Invalid file entry!\n");
524                 free(unode, M_UDFNODE);
525                 brelse(bp);
526                 return(ENOMEM);
527         }
528         size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
529         unode->fentry = malloc(size, M_UDFFENTRY, M_WAITOK | M_ZERO);
530
531         bcopy(bp->b_data, unode->fentry, size);
532         
533         brelse(bp);
534         bp = NULL;
535
536         if ((error = udf_allocv(mp, &vp))) {
537                 printf("Error from udf_allocv\n");
538                 free(unode, M_UDFNODE);
539                 return(error);
540         }
541
542         unode->i_vnode = vp;
543         unode->hash_id = ino;
544         unode->i_devvp = udfmp->im_devvp;
545         unode->i_dev = udfmp->im_dev;
546         unode->udfmp = udfmp;
547         vp->v_data = unode;
548         vref(udfmp->im_devvp);
549         udf_hashins(unode);
550
551         switch (unode->fentry->icbtag.file_type) {
552         default:
553                 vp->v_type = VBAD;
554                 break;
555         case 4:
556                 vp->v_type = VDIR;
557                 break;
558         case 5:
559                 vp->v_type = VREG;
560                 break;
561         case 6:
562                 vp->v_type = VBLK;
563                 break;
564         case 7:
565                 vp->v_type = VCHR;
566                 break;
567         case 9:
568                 vp->v_type = VFIFO;
569                 break;
570         case 10:
571                 vp->v_type = VSOCK;
572                 break;
573         case 12:
574                 vp->v_type = VLNK;
575                 break;
576         }
577         *vpp = vp;
578
579         return(0);
580 }
581
582 struct ifid {
583         u_short ifid_len;
584         u_short ifid_pad;
585         int     ifid_ino;
586         long    ifid_start;
587 };
588
589 static int
590 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
591 {
592         struct ifid *ifhp;
593         struct vnode *nvp;
594         int error;
595
596         ifhp = (struct ifid *)fhp;
597
598         if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
599                 *vpp = NULLVP;
600                 return(error);
601         }
602
603         *vpp = nvp;
604         return(0);
605 }
606
607 static int
608 udf_vptofh (struct vnode *vp, struct fid *fhp)
609 {
610         struct udf_node *node;
611         struct ifid *ifhp;
612
613         node = VTON(vp);
614         ifhp = (struct ifid *)fhp;
615         ifhp->ifid_len = sizeof(struct ifid);
616         ifhp->ifid_ino = node->hash_id;
617
618         return(0);
619 }
620
621 static int
622 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
623 {
624         union udf_pmap *pmap;
625         struct part_map_spare *pms;
626         struct regid *pmap_id;
627         struct buf *bp;
628         unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
629         int ptype, psize, error;
630         unsigned int i;
631
632         for (i = 0; i < lvd->n_pm; i++) {
633                 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
634                 ptype = pmap->data[0];
635                 psize = pmap->data[1];
636                 if (((ptype != 1) && (ptype != 2)) ||
637                     ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
638                         printf("Invalid partition map found\n");
639                         return(1);
640                 }
641
642                 if (ptype == 1) {
643                         /* Type 1 map.  We don't care */
644                         continue;
645                 }
646
647                 /* Type 2 map.  Gotta find out the details */
648                 pmap_id = (struct regid *)&pmap->data[4];
649                 bzero(&regid_id[0], UDF_REGID_ID_SIZE);
650                 bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
651
652                 if (bcmp(&regid_id[0], "*UDF Sparable Partition",
653                     UDF_REGID_ID_SIZE)) {
654                         printf("Unsupported partition map: %s\n", &regid_id[0]);
655                         return(1);
656                 }
657
658                 pms = &pmap->pms;
659                 udfmp->s_table = malloc(pms->st_size, M_UDFMOUNT,
660                                         M_WAITOK | M_ZERO);
661                 if (udfmp->s_table == NULL)
662                         return(ENOMEM);
663
664                 /* Calculate the number of sectors per packet. */
665                 /* XXX Logical or physical? */
666                 udfmp->p_sectors = pms->packet_len / udfmp->bsize;
667
668                 /*
669                  * XXX If reading the first Sparing Table fails, should look
670                  * for another table.
671                  */
672                 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
673                     &bp)) != 0) {
674                         if (bp)
675                                 brelse(bp);
676                         printf("Failed to read Sparing Table at sector %d\n",
677                             pms->st_loc[0]);
678                         return(error);
679                 }
680                 bcopy(bp->b_data, udfmp->s_table, pms->st_size);
681                 brelse(bp);
682
683                 if (udf_checktag(&udfmp->s_table->tag, 0)) {
684                         printf("Invalid sparing table found\n");
685                         return(EINVAL);
686                 }
687
688                 /* See how many valid entries there are here.  The list is
689                  * supposed to be sorted. 0xfffffff0 and higher are not valid
690                  */
691                 for (i = 0; i < udfmp->s_table->rt_l; i++) {
692                         udfmp->s_table_entries = i;
693                         if (udfmp->s_table->entries[i].org >= 0xfffffff0)
694                                 break;
695                 }
696         }
697
698         return(0);
699 }