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