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: releng/6.4/sys/fs/udf/udf_vfsops.c 176341 2008-02-16 16:21:26Z markus $
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 #include <sys/types.h>
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/uio.h>
77 #include <sys/bio.h>
78 #include <sys/buf.h>
79 #include <sys/conf.h>
80 #include <sys/dirent.h>
81 #include <sys/fcntl.h>
82 #include <sys/iconv.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mount.h>
86 #include <sys/namei.h>
87 #include <sys/proc.h>
88 #include <sys/queue.h>
89 #include <sys/vnode.h>
90 #include <sys/endian.h>
91
92 #include <geom/geom.h>
93 #include <geom/geom_vfs.h>
94
95 #include <vm/uma.h>
96
97 #include <fs/udf/ecma167-udf.h>
98 #include <fs/udf/osta.h>
99 #include <fs/udf/udf.h>
100 #include <fs/udf/udf_mount.h>
101
102 static MALLOC_DEFINE(M_UDFMOUNT, "UDF mount", "UDF mount structure");
103 MALLOC_DEFINE(M_UDFFENTRY, "UDF fentry", "UDF file entry structure");
104
105 struct iconv_functions *udf_iconv = NULL;
106
107 /* Zones */
108 uma_zone_t udf_zone_trans = NULL;
109 uma_zone_t udf_zone_node = NULL;
110 uma_zone_t udf_zone_ds = NULL;
111
112 static vfs_init_t udf_init;
113 static vfs_uninit_t udf_uninit;
114 static vfs_mount_t udf_mount;
115 static vfs_root_t udf_root;
116 static vfs_statfs_t udf_statfs;
117 static vfs_unmount_t udf_unmount;
118 static vfs_fhtovp_t udf_fhtovp;
119 static vfs_vptofh_t udf_vptofh;
120
121 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
122
123 static struct vfsops udf_vfsops = {
124 .vfs_fhtovp = udf_fhtovp,
125 .vfs_init = udf_init,
126 .vfs_mount = udf_mount,
127 .vfs_root = udf_root,
128 .vfs_statfs = udf_statfs,
129 .vfs_uninit = udf_uninit,
130 .vfs_unmount = udf_unmount,
131 .vfs_vget = udf_vget,
132 .vfs_vptofh = udf_vptofh,
133 };
134 VFS_SET(udf_vfsops, udf, VFCF_READONLY);
135
136 MODULE_VERSION(udf, 1);
137
138 static int udf_mountfs(struct vnode *, struct mount *, struct thread *);
139
140 static int
141 udf_init(struct vfsconf *foo)
142 {
143
144 /*
145 * This code used to pre-allocate a certain number of pages for each
146 * pool, reducing the need to grow the zones later on. UMA doesn't
147 * advertise any such functionality, unfortunately =-<
148 */
149 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
150 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
151
152 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
153 NULL, NULL, NULL, NULL, 0, 0);
154
155 udf_zone_ds = uma_zcreate("UDF Dirstream zone",
156 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
157
158 if ((udf_zone_node == NULL) || (udf_zone_trans == NULL) ||
159 (udf_zone_ds == NULL)) {
160 printf("Cannot create allocation zones.\n");
161 return (ENOMEM);
162 }
163
164 return 0;
165 }
166
167 static int
168 udf_uninit(struct vfsconf *foo)
169 {
170
171 if (udf_zone_trans != NULL) {
172 uma_zdestroy(udf_zone_trans);
173 udf_zone_trans = NULL;
174 }
175
176 if (udf_zone_node != NULL) {
177 uma_zdestroy(udf_zone_node);
178 udf_zone_node = NULL;
179 }
180
181 if (udf_zone_ds != NULL) {
182 uma_zdestroy(udf_zone_ds);
183 udf_zone_ds = NULL;
184 }
185
186 return (0);
187 }
188
189 static int
190 udf_mount(struct mount *mp, struct thread *td)
191 {
192 struct vnode *devvp; /* vnode of the mount device */
193 struct udf_mnt *imp = 0;
194 struct vfsoptlist *opts;
195 char *fspec, *cs_disk, *cs_local;
196 int error, len, *udf_flags;
197 struct nameidata nd, *ndp = &nd;
198
199 opts = mp->mnt_optnew;
200
201 if ((mp->mnt_flag & MNT_RDONLY) == 0)
202 return (EROFS);
203
204 /*
205 * No root filesystem support. Probably not a big deal, since the
206 * bootloader doesn't understand UDF.
207 */
208 if (mp->mnt_flag & MNT_ROOTFS)
209 return (ENOTSUP);
210
211 fspec = NULL;
212 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
213 if (!error && fspec[len - 1] != '\0')
214 return (EINVAL);
215
216 if (mp->mnt_flag & MNT_UPDATE) {
217 return (0);
218 }
219
220 /* Check that the mount device exists */
221 if (fspec == NULL)
222 return (EINVAL);
223 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
224 if ((error = namei(ndp)))
225 return (error);
226 NDFREE(ndp, NDF_ONLY_PNBUF);
227 devvp = ndp->ni_vp;
228
229 if (vn_isdisk(devvp, &error) == 0) {
230 vput(devvp);
231 return (error);
232 }
233
234 /* Check the access rights on the mount device */
235 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
236 if (error)
237 error = suser(td);
238 if (error) {
239 vput(devvp);
240 return (error);
241 }
242
243 if ((error = udf_mountfs(devvp, mp, td))) {
244 vrele(devvp);
245 return (error);
246 }
247
248 imp = VFSTOUDFFS(mp);
249
250 udf_flags = NULL;
251 error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
252 if (error || len != sizeof(int))
253 return (EINVAL);
254 imp->im_flags = *udf_flags;
255
256 if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
257 cs_disk = NULL;
258 error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
259 if (!error && cs_disk[len - 1] != '\0')
260 return (EINVAL);
261 cs_local = NULL;
262 error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
263 if (!error && cs_local[len - 1] != '\0')
264 return (EINVAL);
265 udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
266 #if 0
267 udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
268 #endif
269 }
270
271 vfs_mountedfrom(mp, fspec);
272 return 0;
273 };
274
275 /*
276 * Check the descriptor tag for both the correct id and correct checksum.
277 * Return zero if all is good, EINVAL if not.
278 */
279 int
280 udf_checktag(struct desc_tag *tag, uint16_t id)
281 {
282 uint8_t *itag;
283 uint8_t i, cksum = 0;
284
285 itag = (uint8_t *)tag;
286
287 if (tag->id != id)
288 return (EINVAL);
289
290 for (i = 0; i < 16; i++)
291 cksum = cksum + itag[i];
292 cksum = cksum - itag[4];
293
294 if (cksum == tag->cksum)
295 return (0);
296
297 return (EINVAL);
298 }
299
300 static int
301 udf_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td) {
302 struct buf *bp = NULL;
303 struct anchor_vdp avdp;
304 struct udf_mnt *udfmp = NULL;
305 struct part_desc *pd;
306 struct logvol_desc *lvd;
307 struct fileset_desc *fsd;
308 struct file_entry *root_fentry;
309 uint32_t sector, size, mvds_start, mvds_end;
310 uint32_t fsd_offset = 0;
311 uint16_t part_num = 0, fsd_part = 0;
312 int error = EINVAL;
313 int logvol_found = 0, part_found = 0, fsd_found = 0;
314 int bsize;
315 struct g_consumer *cp;
316 struct bufobj *bo;
317
318 DROP_GIANT();
319 g_topology_lock();
320 error = g_vfs_open(devvp, &cp, "udf", 0);
321 g_topology_unlock();
322 PICKUP_GIANT();
323 VOP_UNLOCK(devvp, 0, td);
324 if (error)
325 return error;
326
327 bo = &devvp->v_bufobj;
328
329 /* XXX: should be M_WAITOK */
330 MALLOC(udfmp, struct udf_mnt *, sizeof(struct udf_mnt), M_UDFMOUNT,
331 M_NOWAIT | M_ZERO);
332 if (udfmp == NULL) {
333 printf("Cannot allocate UDF mount struct\n");
334 error = ENOMEM;
335 goto bail;
336 }
337
338 mp->mnt_data = (qaddr_t)udfmp;
339 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
340 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
341 MNT_ILOCK(mp);
342 mp->mnt_flag |= MNT_LOCAL;
343 MNT_IUNLOCK(mp);
344 udfmp->im_mountp = mp;
345 udfmp->im_dev = devvp->v_rdev;
346 udfmp->im_devvp = devvp;
347 udfmp->im_d2l = NULL;
348 udfmp->im_cp = cp;
349 udfmp->im_bo = bo;
350
351 #if 0
352 udfmp->im_l2d = NULL;
353 #endif
354
355 bsize = 2048; /* XXX Should probe the media for it's size */
356
357 /*
358 * Get the Anchor Volume Descriptor Pointer from sector 256.
359 * XXX Should also check sector n - 256, n, and 512.
360 */
361 sector = 256;
362 if ((error = bread(devvp, sector * btodb(bsize), bsize, NOCRED,
363 &bp)) != 0)
364 goto bail;
365 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
366 goto bail;
367
368 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
369 brelse(bp);
370 bp = NULL;
371
372 /*
373 * Extract the Partition Descriptor and Logical Volume Descriptor
374 * from the Volume Descriptor Sequence.
375 * XXX Should we care about the partition type right now?
376 * XXX What about multiple partitions?
377 */
378 mvds_start = le32toh(avdp.main_vds_ex.loc);
379 mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
380 for (sector = mvds_start; sector < mvds_end; sector++) {
381 if ((error = bread(devvp, sector * btodb(bsize), bsize,
382 NOCRED, &bp)) != 0) {
383 printf("Can't read sector %d of VDS\n", sector);
384 goto bail;
385 }
386 lvd = (struct logvol_desc *)bp->b_data;
387 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
388 udfmp->bsize = le32toh(lvd->lb_size);
389 udfmp->bmask = udfmp->bsize - 1;
390 udfmp->bshift = ffs(udfmp->bsize) - 1;
391 fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
392 fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
393 if (udf_find_partmaps(udfmp, lvd))
394 break;
395 logvol_found = 1;
396 }
397 pd = (struct part_desc *)bp->b_data;
398 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
399 part_found = 1;
400 part_num = le16toh(pd->part_num);
401 udfmp->part_len = le32toh(pd->part_len);
402 udfmp->part_start = le32toh(pd->start_loc);
403 }
404
405 brelse(bp);
406 bp = NULL;
407 if ((part_found) && (logvol_found))
408 break;
409 }
410
411 if (!part_found || !logvol_found) {
412 error = EINVAL;
413 goto bail;
414 }
415
416 if (fsd_part != part_num) {
417 printf("FSD does not lie within the partition!\n");
418 error = EINVAL;
419 goto bail;
420 }
421
422
423 /*
424 * Grab the Fileset Descriptor
425 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
426 * me in the right direction here.
427 */
428 sector = udfmp->part_start + fsd_offset;
429 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
430 printf("Cannot read sector %d of FSD\n", sector);
431 goto bail;
432 }
433 fsd = (struct fileset_desc *)bp->b_data;
434 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
435 fsd_found = 1;
436 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
437 sizeof(struct long_ad));
438 }
439
440 brelse(bp);
441 bp = NULL;
442
443 if (!fsd_found) {
444 printf("Couldn't find the fsd\n");
445 error = EINVAL;
446 goto bail;
447 }
448
449 /*
450 * Find the file entry for the root directory.
451 */
452 sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
453 size = le32toh(udfmp->root_icb.len);
454 if ((error = udf_readlblks(udfmp, sector, size, &bp)) != 0) {
455 printf("Cannot read sector %d\n", sector);
456 goto bail;
457 }
458
459 root_fentry = (struct file_entry *)bp->b_data;
460 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
461 printf("Invalid root file entry!\n");
462 goto bail;
463 }
464
465 brelse(bp);
466 bp = NULL;
467
468 return 0;
469
470 bail:
471 if (udfmp != NULL)
472 FREE(udfmp, M_UDFMOUNT);
473 if (bp != NULL)
474 brelse(bp);
475 DROP_GIANT();
476 g_topology_lock();
477 g_vfs_close(cp, td);
478 g_topology_unlock();
479 PICKUP_GIANT();
480 return error;
481 };
482
483 static int
484 udf_unmount(struct mount *mp, int mntflags, struct thread *td)
485 {
486 struct udf_mnt *udfmp;
487 int error, flags = 0;
488
489 udfmp = VFSTOUDFFS(mp);
490
491 if (mntflags & MNT_FORCE)
492 flags |= FORCECLOSE;
493
494 if ((error = vflush(mp, 0, flags, td)))
495 return (error);
496
497 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
498 if (udfmp->im_d2l)
499 udf_iconv->close(udfmp->im_d2l);
500 #if 0
501 if (udfmp->im_l2d)
502 udf_iconv->close(udfmp->im_l2d);
503 #endif
504 }
505
506 DROP_GIANT();
507 g_topology_lock();
508 g_vfs_close(udfmp->im_cp, td);
509 g_topology_unlock();
510 PICKUP_GIANT();
511 vrele(udfmp->im_devvp);
512
513 if (udfmp->s_table != NULL)
514 FREE(udfmp->s_table, M_UDFMOUNT);
515
516 FREE(udfmp, M_UDFMOUNT);
517
518 mp->mnt_data = (qaddr_t)0;
519 MNT_ILOCK(mp);
520 mp->mnt_flag &= ~MNT_LOCAL;
521 MNT_IUNLOCK(mp);
522
523 return (0);
524 }
525
526 static int
527 udf_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
528 {
529 struct udf_mnt *udfmp;
530 struct vnode *vp;
531 ino_t id;
532 int error;
533
534 udfmp = VFSTOUDFFS(mp);
535
536 id = udf_getid(&udfmp->root_icb);
537
538 error = udf_vget(mp, id, LK_EXCLUSIVE, vpp);
539 if (error)
540 return error;
541
542 vp = *vpp;
543 vp->v_vflag |= VV_ROOT;
544
545 return (0);
546 }
547
548 static int
549 udf_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
550 {
551 struct udf_mnt *udfmp;
552
553 udfmp = VFSTOUDFFS(mp);
554
555 sbp->f_bsize = udfmp->bsize;
556 sbp->f_iosize = udfmp->bsize;
557 sbp->f_blocks = udfmp->part_len;
558 sbp->f_bfree = 0;
559 sbp->f_bavail = 0;
560 sbp->f_files = 0;
561 sbp->f_ffree = 0;
562 return 0;
563 }
564
565 int
566 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
567 {
568 struct buf *bp;
569 struct vnode *devvp;
570 struct udf_mnt *udfmp;
571 struct thread *td;
572 struct vnode *vp;
573 struct udf_node *unode;
574 struct file_entry *fe;
575 int error, sector, size;
576
577 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
578 if (error || *vpp != NULL)
579 return (error);
580
581 td = curthread;
582 udfmp = VFSTOUDFFS(mp);
583
584 unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
585
586 if ((error = udf_allocv(mp, &vp, td))) {
587 printf("Error from udf_allocv\n");
588 uma_zfree(udf_zone_node, unode);
589 return (error);
590 }
591
592 unode->i_vnode = vp;
593 unode->hash_id = ino;
594 unode->udfmp = udfmp;
595 vp->v_data = unode;
596
597 error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
598 if (error || *vpp != NULL)
599 return (error);
600
601 /*
602 * Copy in the file entry. Per the spec, the size can only be 1 block.
603 */
604 sector = ino + udfmp->part_start;
605 devvp = udfmp->im_devvp;
606 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
607 printf("Cannot read sector %d\n", sector);
608 vput(vp);
609 brelse(bp);
610 *vpp = NULL;
611 return (error);
612 }
613
614 fe = (struct file_entry *)bp->b_data;
615 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
616 printf("Invalid file entry!\n");
617 vput(vp);
618 brelse(bp);
619 *vpp = NULL;
620 return (ENOMEM);
621 }
622 size = UDF_FENTRY_SIZE + le32toh(fe->l_ea) + le32toh(fe->l_ad);
623 MALLOC(unode->fentry, struct file_entry *, size, M_UDFFENTRY,
624 M_NOWAIT | M_ZERO);
625 if (unode->fentry == NULL) {
626 printf("Cannot allocate file entry block\n");
627 vput(vp);
628 brelse(bp);
629 *vpp = NULL;
630 return (ENOMEM);
631 }
632
633 bcopy(bp->b_data, unode->fentry, size);
634
635 brelse(bp);
636 bp = NULL;
637
638 switch (unode->fentry->icbtag.file_type) {
639 default:
640 vp->v_type = VBAD;
641 break;
642 case 4:
643 vp->v_type = VDIR;
644 break;
645 case 5:
646 vp->v_type = VREG;
647 break;
648 case 6:
649 vp->v_type = VBLK;
650 break;
651 case 7:
652 vp->v_type = VCHR;
653 break;
654 case 9:
655 vp->v_type = VFIFO;
656 break;
657 case 10:
658 vp->v_type = VSOCK;
659 break;
660 case 12:
661 vp->v_type = VLNK;
662 break;
663 }
664 *vpp = vp;
665
666 return (0);
667 }
668
669 struct ifid {
670 u_short ifid_len;
671 u_short ifid_pad;
672 int ifid_ino;
673 long ifid_start;
674 };
675
676 static int
677 udf_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
678 {
679 struct ifid *ifhp;
680 struct vnode *nvp;
681 struct udf_node *np;
682 off_t fsize;
683 int error;
684
685 ifhp = (struct ifid *)fhp;
686
687 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
688 *vpp = NULLVP;
689 return (error);
690 }
691
692 np = VTON(nvp);
693 fsize = le64toh(np->fentry->inf_len);
694
695 *vpp = nvp;
696 vnode_create_vobject_off(*vpp, fsize, curthread);
697 return (0);
698 }
699
700 static int
701 udf_vptofh (struct vnode *vp, struct fid *fhp)
702 {
703 struct udf_node *node;
704 struct ifid *ifhp;
705
706 node = VTON(vp);
707 ifhp = (struct ifid *)fhp;
708 ifhp->ifid_len = sizeof(struct ifid);
709 ifhp->ifid_ino = node->hash_id;
710
711 return (0);
712 }
713
714 static int
715 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
716 {
717 union udf_pmap *pmap;
718 struct part_map_spare *pms;
719 struct regid *pmap_id;
720 struct buf *bp;
721 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
722 int i, ptype, psize, error;
723
724 for (i = 0; i < le32toh(lvd->n_pm); i++) {
725 pmap = (union udf_pmap *)&lvd->maps[i * UDF_PMAP_SIZE];
726 ptype = pmap->data[0];
727 psize = pmap->data[1];
728 if (((ptype != 1) && (ptype != 2)) ||
729 ((psize != UDF_PMAP_SIZE) && (psize != 6))) {
730 printf("Invalid partition map found\n");
731 return (1);
732 }
733
734 if (ptype == 1) {
735 /* Type 1 map. We don't care */
736 continue;
737 }
738
739 /* Type 2 map. Gotta find out the details */
740 pmap_id = (struct regid *)&pmap->data[4];
741 bzero(®id_id[0], UDF_REGID_ID_SIZE);
742 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE);
743
744 if (bcmp(®id_id[0], "*UDF Sparable Partition",
745 UDF_REGID_ID_SIZE)) {
746 printf("Unsupported partition map: %s\n", ®id_id[0]);
747 return (1);
748 }
749
750 pms = &pmap->pms;
751 MALLOC(udfmp->s_table, struct udf_sparing_table *,
752 le32toh(pms->st_size), M_UDFMOUNT, M_NOWAIT | M_ZERO);
753 if (udfmp->s_table == NULL)
754 return (ENOMEM);
755
756 /* Calculate the number of sectors per packet. */
757 /* XXX Logical or physical? */
758 udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
759
760 /*
761 * XXX If reading the first Sparing Table fails, should look
762 * for another table.
763 */
764 if ((error = udf_readlblks(udfmp, le32toh(pms->st_loc[0]),
765 le32toh(pms->st_size), &bp)) != 0) {
766 if (bp != NULL)
767 brelse(bp);
768 printf("Failed to read Sparing Table at sector %d\n",
769 le32toh(pms->st_loc[0]));
770 return (error);
771 }
772 bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
773 brelse(bp);
774
775 if (udf_checktag(&udfmp->s_table->tag, 0)) {
776 printf("Invalid sparing table found\n");
777 return (EINVAL);
778 }
779
780 /* See how many valid entries there are here. The list is
781 * supposed to be sorted. 0xfffffff0 and higher are not valid
782 */
783 for (i = 0; i < le16toh(udfmp->s_table->rt_l); i++) {
784 udfmp->s_table_entries = i;
785 if (le32toh(udfmp->s_table->entries[i].org) >=
786 0xfffffff0)
787 break;
788 }
789 }
790
791 return (0);
792 }
Cache object: 77733bbdb8fbd0fe0323c948c4341d1c
|