Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / vfs / udf / udf_vfsops.c
CommitLineData
03998195
JS
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 $
086c1d7e 27 * $DragonFly: src/sys/vfs/udf/udf_vfsops.c,v 1.25 2006/12/23 00:41:30 swildner Exp $
03998195
JS
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>
03998195
JS
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>
fad57d0e 86#include <sys/nlookup.h>
03998195
JS
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
66a1ddf5 96extern struct vop_ops udf_vnode_vops;
0961aa92 97
03998195
JS
98MALLOC_DEFINE(M_UDFNODE, "UDF node", "UDF node structure");
99MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
100MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
101
acde96db
MD
102static int udf_mount(struct mount *, char *, caddr_t, struct ucred *);
103static int udf_unmount(struct mount *, int);
03998195 104static int udf_root(struct mount *, struct vnode **);
acde96db 105static int udf_statfs(struct mount *, struct statfs *, struct ucred *);
03998195
JS
106static int udf_fhtovp(struct mount *, struct fid *, struct vnode **);
107static int udf_vptofh(struct vnode *, struct fid *);
108
109static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
110
111static struct vfsops udf_vfsops = {
43c45e8f
HP
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
03998195
JS
120};
121VFS_SET(udf_vfsops, udf, VFCF_READONLY);
122
123MODULE_VERSION(udf, 1);
124
acde96db 125static int udf_mountfs(struct vnode *, struct mount *);
03998195
JS
126
127static int
acde96db 128udf_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
03998195
JS
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;
fad57d0e 135 struct nlookupdata nd;
03998195
JS
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 */
fad57d0e
MD
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)
28623bf9 162 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
fad57d0e
MD
163 nlookup_done(&nd);
164 if (error)
165 return (error);
03998195
JS
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 */
ca466bae 173 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
acde96db 174 error = VOP_ACCESS(devvp, VREAD, cred);
03998195 175 if (error)
acde96db 176 error = suser_cred(cred, 0);
03998195
JS
177 if (error) {
178 vput(devvp);
179 return(error);
180 }
a11aaa81 181 vn_unlock(devvp);
03998195 182
acde96db 183 if ((error = udf_mountfs(devvp, mp))) {
03998195
JS
184 vrele(devvp);
185 return(error);
186 }
187
188 imp = VFSTOUDFFS(mp);
189
190 imp->im_flags = args.flags;
191
03998195
JS
192 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
193 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
acde96db 194 udf_statfs(mp, &mp->mnt_stat, cred);
03998195 195 return(0);
4a77c3ab 196}
03998195
JS
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 */
202int
203udf_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
223static int
acde96db 224udf_mountfs(struct vnode *devvp, struct mount *mp)
e4c9c0c8 225{
03998195
JS
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;
b13267a5 233 cdev_t dev;
03998195
JS
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);
715f92b6 247 if (count_udev(devvp->v_udev) > 0)
03998195 248 return(EBUSY);
87de5057 249 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)))
03998195
JS
250 return(error);
251
ca466bae 252 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 253 error = VOP_OPEN(devvp, FREAD, FSCRED, NULL);
a11aaa81 254 vn_unlock(devvp);
03998195
JS
255 if (error)
256 return(error);
257 needclose = 1;
e4c9c0c8 258 dev = devvp->v_rdev;
03998195 259
efda3bd0 260 udfmp = kmalloc(sizeof(*udfmp), M_UDFMOUNT, M_WAITOK | M_ZERO);
03998195
JS
261
262 mp->mnt_data = (qaddr_t)udfmp;
e4c9c0c8 263 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
03998195
JS
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;
e4c9c0c8 268 udfmp->im_dev = dev;
03998195
JS
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;
54078292 278 if ((error = bread(devvp, (off_t)sector * bsize, bsize, &bp)) != 0)
03998195
JS
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++) {
54078292 296 if ((error = bread(devvp, (off_t)sector * bsize, bsize,
03998195 297 &bp)) != 0) {
086c1d7e 298 kprintf("Can't read sector %d of VDS\n", sector);
03998195
JS
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) {
086c1d7e 332 kprintf("FSD does not lie within the partition!\n");
03998195
JS
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) {
086c1d7e 345 kprintf("Cannot read sector %d of FSD\n", sector);
03998195
JS
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) {
086c1d7e 359 kprintf("Couldn't find the fsd\n");
03998195
JS
360 error = EINVAL;
361 goto bail;
0961aa92
MD
362 }
363
66a1ddf5 364 vfs_add_vnodeops(mp, &udf_vnode_vops, &mp->mnt_vn_norm_ops);
03998195
JS
365
366 /*
367 * Find the file entry for the root directory.
368 */
369 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
370 size = udfmp->root_icb.len;
371 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
086c1d7e 372 kprintf("Cannot read sector %d\n", sector);
03998195
JS
373 goto bail;
374 }
375
376 root_fentry = (struct file_entry *)bp->b_data;
377 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
086c1d7e 378 kprintf("Invalid root file entry!\n");
03998195
JS
379 goto bail;
380 }
381
382 brelse(bp);
383 bp = NULL;
384
385 lwkt_token_init(&udfmp->hash_token);
386 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
387
388 return(0);
389
390bail:
391 if (udfmp != NULL)
efda3bd0 392 kfree(udfmp, M_UDFMOUNT);
03998195
JS
393 if (bp != NULL)
394 brelse(bp);
395 if (needclose)
87de5057 396 VOP_CLOSE(devvp, FREAD);
03998195 397 return(error);
4a77c3ab 398}
03998195
JS
399
400static int
acde96db 401udf_unmount(struct mount *mp, int mntflags)
03998195
JS
402{
403 struct udf_mnt *udfmp;
404 int error, flags = 0;
405
406 udfmp = VFSTOUDFFS(mp);
407
408 if (mntflags & MNT_FORCE)
409 flags |= FORCECLOSE;
410
411 if ((error = vflush(mp, 0, flags)))
412 return (error);
413
e4c9c0c8 414 udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
87de5057 415 error = VOP_CLOSE(udfmp->im_devvp, FREAD);
03998195
JS
416 vrele(udfmp->im_devvp);
417
418 if (udfmp->s_table)
efda3bd0 419 kfree(udfmp->s_table, M_UDFMOUNT);
03998195 420 if (udfmp->hashtbl)
efda3bd0
MD
421 kfree(udfmp->hashtbl, M_UDFMOUNT);
422 kfree(udfmp, M_UDFMOUNT);
03998195
JS
423
424 mp->mnt_data = (qaddr_t)0;
425 mp->mnt_flag &= ~MNT_LOCAL;
426
427 return (error);
428}
429
430static int
431udf_root(struct mount *mp, struct vnode **vpp)
432{
433 struct udf_mnt *udfmp;
434 struct vnode *vp;
435 ino_t id;
436 int error;
437
438 udfmp = VFSTOUDFFS(mp);
439
440 id = udf_getid(&udfmp->root_icb);
441
442 error = udf_vget(mp, id, vpp);
443 if (error)
444 return(error);
445
446 vp = *vpp;
447 vp->v_flag |= VROOT;
448 udfmp->root_vp = vp;
449
450 return(0);
451}
452
453static int
acde96db 454udf_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
03998195
JS
455{
456 struct udf_mnt *udfmp;
457
458 udfmp = VFSTOUDFFS(mp);
459
460 sbp->f_bsize = udfmp->bsize;
461 sbp->f_iosize = udfmp->bsize;
462 sbp->f_blocks = udfmp->part_len;
463 sbp->f_bfree = 0;
464 sbp->f_bavail = 0;
465 sbp->f_files = 0;
466 sbp->f_ffree = 0;
467 if (sbp != &mp->mnt_stat) {
468 sbp->f_type = mp->mnt_vfc->vfc_typenum;
03998195
JS
469 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
470 }
471
472 return(0);
473}
474
475int
476udf_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 */
efda3bd0 501 unode = kmalloc(sizeof(*unode), M_UDFNODE, M_WAITOK | M_ZERO);
03998195
JS
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) {
086c1d7e 509 kprintf("Cannot read sector %d\n", sector);
efda3bd0 510 kfree(unode, M_UDFNODE);
03998195
JS
511 return(error);
512 }
513
514 fe = (struct file_entry *)bp->b_data;
515 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
086c1d7e 516 kprintf("Invalid file entry!\n");
efda3bd0 517 kfree(unode, M_UDFNODE);
03998195
JS
518 brelse(bp);
519 return(ENOMEM);
520 }
521 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
efda3bd0 522 unode->fentry = kmalloc(size, M_UDFFENTRY, M_WAITOK | M_ZERO);
03998195
JS
523
524 bcopy(bp->b_data, unode->fentry, size);
525
526 brelse(bp);
527 bp = NULL;
528
529 if ((error = udf_allocv(mp, &vp))) {
086c1d7e 530 kprintf("Error from udf_allocv\n");
efda3bd0 531 kfree(unode, M_UDFNODE);
03998195
JS
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;
597aea93 541 vref(udfmp->im_devvp);
03998195
JS
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 }
5fd012e0
MD
570 /*
571 * Locked and refd vnode returned
572 */
03998195
JS
573 *vpp = vp;
574
575 return(0);
576}
577
578struct ifid {
579 u_short ifid_len;
580 u_short ifid_pad;
581 int ifid_ino;
582 long ifid_start;
583};
584
585static int
586udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
587{
588 struct ifid *ifhp;
589 struct vnode *nvp;
590 int error;
591
592 ifhp = (struct ifid *)fhp;
593
594 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
595 *vpp = NULLVP;
596 return(error);
597 }
598
599 *vpp = nvp;
600 return(0);
601}
602
603static int
604udf_vptofh (struct vnode *vp, struct fid *fhp)
605{
606 struct udf_node *node;
607 struct ifid *ifhp;
608
609 node = VTON(vp);
610 ifhp = (struct ifid *)fhp;
611 ifhp->ifid_len = sizeof(struct ifid);
612 ifhp->ifid_ino = node->hash_id;
613
614 return(0);
615}
616
617static int
618udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
619{
620 union udf_pmap *pmap;
621 struct part_map_spare *pms;
622 struct regid *pmap_id;
623 struct buf *bp;
624 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
625 int ptype, psize, error;
626 unsigned int i;
627
628 for (i = 0; i < lvd->n_pm; i++) {
629 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
630 ptype = pmap->data[0];
631 psize = pmap->data[1];
632 if (((ptype != 1) && (ptype != 2)) ||
633 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
086c1d7e 634 kprintf("Invalid partition map found\n");
03998195
JS
635 return(1);
636 }
637
638 if (ptype == 1) {
639 /* Type 1 map. We don't care */
640 continue;
641 }
642
643 /* Type 2 map. Gotta find out the details */
644 pmap_id = (struct regid *)&pmap->data[4];
645 bzero(&regid_id[0], UDF_REGID_ID_SIZE);
646 bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
647
648 if (bcmp(&regid_id[0], "*UDF Sparable Partition",
649 UDF_REGID_ID_SIZE)) {
086c1d7e 650 kprintf("Unsupported partition map: %s\n", &regid_id[0]);
03998195
JS
651 return(1);
652 }
653
654 pms = &pmap->pms;
efda3bd0 655 udfmp->s_table = kmalloc(pms->st_size, M_UDFMOUNT,
03998195
JS
656 M_WAITOK | M_ZERO);
657 if (udfmp->s_table == NULL)
658 return(ENOMEM);
659
660 /* Calculate the number of sectors per packet. */
661 /* XXX Logical or physical? */
662 udfmp->p_sectors = pms->packet_len / udfmp->bsize;
663
664 /*
665 * XXX If reading the first Sparing Table fails, should look
666 * for another table.
667 */
668 if ((error = udf_readlblks(udfmp, pms->st_loc[0], pms->st_size,
669 &bp)) != 0) {
ffaa7d78
MD
670 if (bp)
671 brelse(bp);
086c1d7e 672 kprintf("Failed to read Sparing Table at sector %d\n",
03998195
JS
673 pms->st_loc[0]);
674 return(error);
675 }
676 bcopy(bp->b_data, udfmp->s_table, pms->st_size);
677 brelse(bp);
678
679 if (udf_checktag(&udfmp->s_table->tag, 0)) {
086c1d7e 680 kprintf("Invalid sparing table found\n");
03998195
JS
681 return(EINVAL);
682 }
683
684 /* See how many valid entries there are here. The list is
685 * supposed to be sorted. 0xfffffff0 and higher are not valid
686 */
687 for (i = 0; i < udfmp->s_table->rt_l; i++) {
688 udfmp->s_table_entries = i;
689 if (udfmp->s_table->entries[i].org >= 0xfffffff0)
690 break;
691 }
692 }
693
694 return(0);
695}