kernel: Remove some unused variables.
[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 $
03998195
JS
27 */
28
29/* udf_vfsops.c */
30/* Implement the VFS side of things */
31
32/*
33 * Ok, here's how it goes. The UDF specs are pretty clear on how each data
34 * structure is made up, but not very clear on how they relate to each other.
35 * Here is the skinny... This demostrates a filesystem with one file in the
36 * root directory. Subdirectories are treated just as normal files, but they
37 * have File Id Descriptors of their children as their file data. As for the
38 * Anchor Volume Descriptor Pointer, it can exist in two of the following three
39 * places: sector 256, sector n (the max sector of the disk), or sector
40 * n - 256. It's a pretty good bet that one will exist at sector 256 though.
41 * One caveat is unclosed CD media. For that, sector 256 cannot be written,
42 * so the Anchor Volume Descriptor Pointer can exist at sector 512 until the
43 * media is closed.
44 *
45 * Sector:
46 * 256:
47 * n: Anchor Volume Descriptor Pointer
48 * n - 256: |
49 * |
50 * |-->Main Volume Descriptor Sequence
51 * | |
52 * | |
53 * | |-->Logical Volume Descriptor
54 * | |
55 * |-->Partition Descriptor |
56 * | |
57 * | |
58 * |-->Fileset Descriptor
59 * |
60 * |
61 * |-->Root Dir File Entry
62 * |
63 * |
64 * |-->File data:
65 * File Id Descriptor
66 * |
67 * |
68 * |-->File Entry
69 * |
70 * |
71 * |-->File data
72 */
73
74#include <sys/types.h>
75#include <sys/param.h>
76#include <sys/systm.h>
77#include <sys/uio.h>
78#include <sys/buf.h>
79#include <sys/conf.h>
03998195
JS
80#include <sys/fcntl.h>
81#include <sys/module.h>
82#include <sys/kernel.h>
83#include <sys/malloc.h>
84#include <sys/mount.h>
fad57d0e 85#include <sys/nlookup.h>
03998195 86#include <sys/proc.h>
895c1f85 87#include <sys/priv.h>
03998195
JS
88#include <sys/queue.h>
89#include <sys/vnode.h>
90
54341a3b
MD
91#include <sys/buf2.h>
92
03998195
JS
93#include <vfs/udf/ecma167-udf.h>
94#include <vfs/udf/osta.h>
95#include <vfs/udf/udf.h>
96#include <vfs/udf/udf_mount.h>
97
66a1ddf5 98extern struct vop_ops udf_vnode_vops;
0961aa92 99
03998195
JS
100MALLOC_DEFINE(M_UDFNODE, "UDF node", "UDF node structure");
101MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
102MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
103
acde96db
MD
104static int udf_mount(struct mount *, char *, caddr_t, struct ucred *);
105static int udf_unmount(struct mount *, int);
03998195 106static int udf_root(struct mount *, struct vnode **);
acde96db 107static int udf_statfs(struct mount *, struct statfs *, struct ucred *);
67863d04
MD
108static int udf_fhtovp(struct mount *, struct vnode *,
109 struct fid *, struct vnode **);
03998195
JS
110static int udf_vptofh(struct vnode *, struct fid *);
111
112static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
113
114static struct vfsops udf_vfsops = {
43c45e8f
HP
115 .vfs_mount = udf_mount,
116 .vfs_unmount = udf_unmount,
117 .vfs_root = udf_root,
118 .vfs_statfs = udf_statfs,
119 .vfs_sync = vfs_stdsync,
120 .vfs_vget = udf_vget,
121 .vfs_fhtovp = udf_fhtovp,
122 .vfs_vptofh = udf_vptofh
03998195
JS
123};
124VFS_SET(udf_vfsops, udf, VFCF_READONLY);
125
126MODULE_VERSION(udf, 1);
127
acde96db 128static int udf_mountfs(struct vnode *, struct mount *);
03998195
JS
129
130static int
acde96db 131udf_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
03998195
JS
132{
133 struct vnode *devvp; /* vnode of the mount device */
134 struct udf_args args;
4090d6ff 135 struct udf_mnt *imp = NULL;
03998195
JS
136 size_t size;
137 int error;
fad57d0e 138 struct nlookupdata nd;
03998195
JS
139
140 if ((mp->mnt_flag & MNT_RDONLY) == 0)
141 return (EROFS);
142
143 /*
144 * No root filesystem support. Probably not a big deal, since the
145 * bootloader doesn't understand UDF.
146 */
147 if (mp->mnt_flag & MNT_ROOTFS)
148 return (ENOTSUP);
149
150 if ((error = copyin(data, (caddr_t)&args, sizeof(struct udf_args))))
151 return(error);
152
153 if (mp->mnt_flag & MNT_UPDATE) {
154 imp = VFSTOUDFFS(mp);
155 if (args.fspec == NULL)
156 return(vfs_export(mp, &imp->im_export, &args.export));
157 }
158
159 /* Check that the mount device exists */
fad57d0e
MD
160 devvp = NULL;
161 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
162 if (error == 0)
163 error = nlookup(&nd);
164 if (error == 0)
28623bf9 165 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
fad57d0e
MD
166 nlookup_done(&nd);
167 if (error)
168 return (error);
03998195
JS
169
170 if (vn_isdisk(devvp, &error) == 0) {
171 vrele(devvp);
172 return(error);
173 }
174
175 /* Check the access rights on the mount device */
ca466bae 176 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
cb66845a 177 error = VOP_EACCESS(devvp, VREAD, cred);
03998195 178 if (error)
895c1f85 179 error = priv_check_cred(cred, PRIV_ROOT, 0);
03998195
JS
180 if (error) {
181 vput(devvp);
182 return(error);
183 }
a11aaa81 184 vn_unlock(devvp);
03998195 185
acde96db 186 if ((error = udf_mountfs(devvp, mp))) {
03998195
JS
187 vrele(devvp);
188 return(error);
189 }
190
191 imp = VFSTOUDFFS(mp);
192
193 imp->im_flags = args.flags;
194
03998195
JS
195 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
196 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
acde96db 197 udf_statfs(mp, &mp->mnt_stat, cred);
03998195 198 return(0);
4a77c3ab 199}
03998195
JS
200
201/*
202 * Check the descriptor tag for both the correct id and correct checksum.
203 * Return zero if all is good, EINVAL if not.
204 */
205int
206udf_checktag(struct desc_tag *tag, uint16_t id)
207{
208 uint8_t *itag;
209 uint8_t i, cksum = 0;
210
211 itag = (uint8_t *)tag;
212
213 if (tag->id != id)
214 return(EINVAL);
215
216 for (i = 0; i < 15; i++)
217 cksum = cksum + itag[i];
218 cksum = cksum - itag[4];
219
220 if (cksum == tag->cksum)
221 return(0);
222
223 return(EINVAL);
224}
225
226static int
acde96db 227udf_mountfs(struct vnode *devvp, struct mount *mp)
e4c9c0c8 228{
03998195
JS
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;
b13267a5 236 cdev_t dev;
03998195
JS
237 uint32_t sector, size, mvds_start, mvds_end;
238 uint32_t fsd_offset = 0;
239 uint16_t part_num = 0, fsd_part = 0;
240 int error = EINVAL, needclose = 0;
241 int logvol_found = 0, part_found = 0, fsd_found = 0;
242 int bsize;
243
244 /*
245 * Disallow multiple mounts of the same device. Flush the buffer
246 * cache for the device.
247 */
248 if ((error = vfs_mountedon(devvp)))
249 return(error);
8be7edad 250 if (vcount(devvp) > 0)
03998195 251 return(EBUSY);
87de5057 252 if ((error = vinvalbuf(devvp, V_SAVE, 0, 0)))
03998195
JS
253 return(error);
254
ca466bae 255 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 256 error = VOP_OPEN(devvp, FREAD, FSCRED, NULL);
a11aaa81 257 vn_unlock(devvp);
03998195
JS
258 if (error)
259 return(error);
260 needclose = 1;
e4c9c0c8 261 dev = devvp->v_rdev;
03998195 262
efda3bd0 263 udfmp = kmalloc(sizeof(*udfmp), M_UDFMOUNT, M_WAITOK | M_ZERO);
03998195
JS
264
265 mp->mnt_data = (qaddr_t)udfmp;
e4c9c0c8 266 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
03998195
JS
267 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
268 mp->mnt_maxsymlinklen = 0;
269 mp->mnt_flag |= MNT_LOCAL;
270 udfmp->im_mountp = mp;
e4c9c0c8 271 udfmp->im_dev = dev;
03998195
JS
272 udfmp->im_devvp = devvp;
273
274 bsize = 2048; /* XXX Should probe the media for it's size */
275
276 /*
277 * Get the Anchor Volume Descriptor Pointer from sector 256.
278 * XXX Should also check sector n - 256, n, and 512.
279 */
280 sector = 256;
54078292 281 if ((error = bread(devvp, (off_t)sector * bsize, bsize, &bp)) != 0)
03998195
JS
282 goto bail;
283 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
284 goto bail;
285
286 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
287 brelse(bp);
288 bp = NULL;
289
290 /*
291 * Extract the Partition Descriptor and Logical Volume Descriptor
292 * from the Volume Descriptor Sequence.
293 * XXX Should we care about the partition type right now?
294 * XXX What about multiple partitions?
295 */
296 mvds_start = avdp.main_vds_ex.loc;
297 mvds_end = mvds_start + (avdp.main_vds_ex.len - 1) / bsize;
298 for (sector = mvds_start; sector < mvds_end; sector++) {
54078292 299 if ((error = bread(devvp, (off_t)sector * bsize, bsize,
03998195 300 &bp)) != 0) {
086c1d7e 301 kprintf("Can't read sector %d of VDS\n", sector);
03998195
JS
302 goto bail;
303 }
304 lvd = (struct logvol_desc *)bp->b_data;
305 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
306 udfmp->bsize = lvd->lb_size;
307 udfmp->bmask = udfmp->bsize - 1;
308 udfmp->bshift = ffs(udfmp->bsize) - 1;
309 fsd_part = lvd->_lvd_use.fsd_loc.loc.part_num;
310 fsd_offset = lvd->_lvd_use.fsd_loc.loc.lb_num;
311 if (udf_find_partmaps(udfmp, lvd))
312 break;
313 logvol_found = 1;
314 }
315 pd = (struct part_desc *)bp->b_data;
316 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
317 part_found = 1;
318 part_num = pd->part_num;
319 udfmp->part_len = pd->part_len;
320 udfmp->part_start = pd->start_loc;
321 }
322
323 brelse(bp);
324 bp = NULL;
325 if ((part_found) && (logvol_found))
326 break;
327 }
328
329 if (!part_found || !logvol_found) {
330 error = EINVAL;
331 goto bail;
332 }
333
334 if (fsd_part != part_num) {
086c1d7e 335 kprintf("FSD does not lie within the partition!\n");
03998195
JS
336 error = EINVAL;
337 goto bail;
338 }
339
340
341 /*
342 * Grab the Fileset Descriptor
343 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
344 * me in the right direction here.
345 */
346 sector = udfmp->part_start + fsd_offset;
347 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
086c1d7e 348 kprintf("Cannot read sector %d of FSD\n", sector);
03998195
JS
349 goto bail;
350 }
351 fsd = (struct fileset_desc *)bp->b_data;
352 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
353 fsd_found = 1;
354 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
355 sizeof(struct long_ad));
356 }
357
358 brelse(bp);
359 bp = NULL;
360
361 if (!fsd_found) {
086c1d7e 362 kprintf("Couldn't find the fsd\n");
03998195
JS
363 error = EINVAL;
364 goto bail;
0961aa92
MD
365 }
366
66a1ddf5 367 vfs_add_vnodeops(mp, &udf_vnode_vops, &mp->mnt_vn_norm_ops);
03998195
JS
368
369 /*
370 * Find the file entry for the root directory.
371 */
372 sector = udfmp->root_icb.loc.lb_num + udfmp->part_start;
373 size = udfmp->root_icb.len;
374 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
086c1d7e 375 kprintf("Cannot read sector %d\n", sector);
03998195
JS
376 goto bail;
377 }
378
379 root_fentry = (struct file_entry *)bp->b_data;
380 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
086c1d7e 381 kprintf("Invalid root file entry!\n");
03998195
JS
382 goto bail;
383 }
384
385 brelse(bp);
386 bp = NULL;
387
a3c18566 388 lwkt_token_init(&udfmp->hash_token, "udfihash");
03998195
JS
389 udfmp->hashtbl = phashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, &udfmp->hashsz);
390
391 return(0);
392
393bail:
394 if (udfmp != NULL)
efda3bd0 395 kfree(udfmp, M_UDFMOUNT);
03998195
JS
396 if (bp != NULL)
397 brelse(bp);
398 if (needclose)
87de5057 399 VOP_CLOSE(devvp, FREAD);
03998195 400 return(error);
4a77c3ab 401}
03998195
JS
402
403static int
acde96db 404udf_unmount(struct mount *mp, int mntflags)
03998195
JS
405{
406 struct udf_mnt *udfmp;
407 int error, flags = 0;
408
409 udfmp = VFSTOUDFFS(mp);
410
411 if (mntflags & MNT_FORCE)
412 flags |= FORCECLOSE;
413
414 if ((error = vflush(mp, 0, flags)))
415 return (error);
416
e4c9c0c8 417 udfmp->im_devvp->v_rdev->si_mountpoint = NULL;
87de5057 418 error = VOP_CLOSE(udfmp->im_devvp, FREAD);
03998195
JS
419 vrele(udfmp->im_devvp);
420
421 if (udfmp->s_table)
efda3bd0 422 kfree(udfmp->s_table, M_UDFMOUNT);
03998195 423 if (udfmp->hashtbl)
efda3bd0
MD
424 kfree(udfmp->hashtbl, M_UDFMOUNT);
425 kfree(udfmp, M_UDFMOUNT);
03998195
JS
426
427 mp->mnt_data = (qaddr_t)0;
428 mp->mnt_flag &= ~MNT_LOCAL;
429
430 return (error);
431}
432
433static int
434udf_root(struct mount *mp, struct vnode **vpp)
435{
436 struct udf_mnt *udfmp;
437 struct vnode *vp;
438 ino_t id;
439 int error;
440
441 udfmp = VFSTOUDFFS(mp);
442
443 id = udf_getid(&udfmp->root_icb);
444
b9b0a6d0 445 error = udf_vget(mp, NULL, id, vpp);
03998195
JS
446 if (error)
447 return(error);
448
449 vp = *vpp;
2247fe02 450 vsetflags(vp, VROOT);
03998195
JS
451 udfmp->root_vp = vp;
452
453 return(0);
454}
455
456static int
acde96db 457udf_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
03998195
JS
458{
459 struct udf_mnt *udfmp;
460
461 udfmp = VFSTOUDFFS(mp);
462
463 sbp->f_bsize = udfmp->bsize;
464 sbp->f_iosize = udfmp->bsize;
465 sbp->f_blocks = udfmp->part_len;
466 sbp->f_bfree = 0;
467 sbp->f_bavail = 0;
468 sbp->f_files = 0;
469 sbp->f_ffree = 0;
470 if (sbp != &mp->mnt_stat) {
471 sbp->f_type = mp->mnt_vfc->vfc_typenum;
03998195
JS
472 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
473 }
474
475 return(0);
476}
477
478int
b9b0a6d0 479udf_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
03998195
JS
480{
481 struct buf *bp;
482 struct vnode *devvp;
483 struct udf_mnt *udfmp;
03998195
JS
484 struct vnode *vp;
485 struct udf_node *unode;
486 struct file_entry *fe;
487 int error, sector, size;
488
03998195
JS
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 */
efda3bd0 502 unode = kmalloc(sizeof(*unode), M_UDFNODE, M_WAITOK | M_ZERO);
03998195
JS
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) {
086c1d7e 510 kprintf("Cannot read sector %d\n", sector);
efda3bd0 511 kfree(unode, M_UDFNODE);
03998195
JS
512 return(error);
513 }
514
515 fe = (struct file_entry *)bp->b_data;
516 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
086c1d7e 517 kprintf("Invalid file entry!\n");
efda3bd0 518 kfree(unode, M_UDFNODE);
03998195
JS
519 brelse(bp);
520 return(ENOMEM);
521 }
522 size = UDF_FENTRY_SIZE + fe->l_ea + fe->l_ad;
efda3bd0 523 unode->fentry = kmalloc(size, M_UDFFENTRY, M_WAITOK | M_ZERO);
03998195
JS
524
525 bcopy(bp->b_data, unode->fentry, size);
526
527 brelse(bp);
528 bp = NULL;
529
530 if ((error = udf_allocv(mp, &vp))) {
086c1d7e 531 kprintf("Error from udf_allocv\n");
efda3bd0 532 kfree(unode, M_UDFNODE);
03998195
JS
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;
597aea93 542 vref(udfmp->im_devvp);
03998195
JS
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 }
5fd012e0
MD
571 /*
572 * Locked and refd vnode returned
573 */
03998195
JS
574 *vpp = vp;
575
576 return(0);
577}
578
579struct ifid {
580 u_short ifid_len;
581 u_short ifid_pad;
582 int ifid_ino;
583 long ifid_start;
584};
585
586static int
67863d04
MD
587udf_fhtovp(struct mount *mp, struct vnode *rootvp,
588 struct fid *fhp, struct vnode **vpp)
03998195
JS
589{
590 struct ifid *ifhp;
591 struct vnode *nvp;
592 int error;
593
594 ifhp = (struct ifid *)fhp;
595
b9b0a6d0 596 if ((error = VFS_VGET(mp, NULL, ifhp->ifid_ino, &nvp)) != 0) {
03998195
JS
597 *vpp = NULLVP;
598 return(error);
599 }
600
601 *vpp = nvp;
602 return(0);
603}
604
605static int
606udf_vptofh (struct vnode *vp, struct fid *fhp)
607{
608 struct udf_node *node;
609 struct ifid *ifhp;
610
611 node = VTON(vp);
612 ifhp = (struct ifid *)fhp;
613 ifhp->ifid_len = sizeof(struct ifid);
614 ifhp->ifid_ino = node->hash_id;
615
616 return(0);
617}
618
619static int
620udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
621{
622 union udf_pmap *pmap;
623 struct part_map_spare *pms;
624 struct regid *pmap_id;
625 struct buf *bp;
626 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
627 int ptype, psize, error;
628 unsigned int i;
629
630 for (i = 0; i < lvd->n_pm; i++) {
631 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
632 ptype = pmap->data[0];
633 psize = pmap->data[1];
634 if (((ptype != 1) && (ptype != 2)) ||
635 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
086c1d7e 636 kprintf("Invalid partition map found\n");
03998195
JS
637 return(1);
638 }
639
640 if (ptype == 1) {
641 /* Type 1 map. We don't care */
642 continue;
643 }
644
645 /* Type 2 map. Gotta find out the details */
646 pmap_id = (struct regid *)&pmap->data[4];
647 bzero(&regid_id[0], UDF_REGID_ID_SIZE);
648 bcopy(&pmap_id->id[0], &regid_id[0], UDF_REGID_ID_SIZE);
649
650 if (bcmp(&regid_id[0], "*UDF Sparable Partition",
651 UDF_REGID_ID_SIZE)) {
086c1d7e 652 kprintf("Unsupported partition map: %s\n", &regid_id[0]);
03998195
JS
653 return(1);
654 }
655
656 pms = &pmap->pms;
efda3bd0 657 udfmp->s_table = kmalloc(pms->st_size, M_UDFMOUNT,
03998195 658 M_WAITOK | M_ZERO);
03998195
JS
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}