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