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