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