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