1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
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 demonstrates 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/limits.h>
85 #include <sys/malloc.h>
86 #include <sys/mount.h>
87 #include <sys/namei.h>
88 #include <sys/priv.h>
89 #include <sys/proc.h>
90 #include <sys/queue.h>
91 #include <sys/vnode.h>
92 #include <sys/endian.h>
93
94 #include <geom/geom.h>
95 #include <geom/geom_vfs.h>
96
97 #include <vm/uma.h>
98
99 #include <fs/udf/ecma167-udf.h>
100 #include <fs/udf/osta.h>
101 #include <fs/udf/udf.h>
102 #include <fs/udf/udf_mount.h>
103
104 static MALLOC_DEFINE(M_UDFMOUNT, "udf_mount", "UDF mount structure");
105 MALLOC_DEFINE(M_UDFFENTRY, "udf_fentry", "UDF file entry structure");
106
107 struct iconv_functions *udf_iconv = NULL;
108
109 /* Zones */
110 uma_zone_t udf_zone_trans = NULL;
111 uma_zone_t udf_zone_node = NULL;
112 uma_zone_t udf_zone_ds = NULL;
113
114 static vfs_init_t udf_init;
115 static vfs_uninit_t udf_uninit;
116 static vfs_mount_t udf_mount;
117 static vfs_root_t udf_root;
118 static vfs_statfs_t udf_statfs;
119 static vfs_unmount_t udf_unmount;
120 static vfs_fhtovp_t udf_fhtovp;
121
122 static int udf_find_partmaps(struct udf_mnt *, struct logvol_desc *);
123
124 static struct vfsops udf_vfsops = {
125 .vfs_fhtovp = udf_fhtovp,
126 .vfs_init = udf_init,
127 .vfs_mount = udf_mount,
128 .vfs_root = udf_root,
129 .vfs_statfs = udf_statfs,
130 .vfs_uninit = udf_uninit,
131 .vfs_unmount = udf_unmount,
132 .vfs_vget = udf_vget,
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 *);
139
140 static int
udf_init(struct vfsconf * foo)141 udf_init(struct vfsconf *foo)
142 {
143
144 udf_zone_trans = uma_zcreate("UDF translation buffer, zone", MAXNAMLEN *
145 sizeof(unicode_t), NULL, NULL, NULL, NULL, 0, 0);
146
147 udf_zone_node = uma_zcreate("UDF Node zone", sizeof(struct udf_node),
148 NULL, NULL, NULL, NULL, 0, 0);
149
150 udf_zone_ds = uma_zcreate("UDF Dirstream zone",
151 sizeof(struct udf_dirstream), NULL, NULL, NULL, NULL, 0, 0);
152
153 return 0;
154 }
155
156 static int
udf_uninit(struct vfsconf * foo)157 udf_uninit(struct vfsconf *foo)
158 {
159
160 if (udf_zone_trans != NULL) {
161 uma_zdestroy(udf_zone_trans);
162 udf_zone_trans = NULL;
163 }
164
165 if (udf_zone_node != NULL) {
166 uma_zdestroy(udf_zone_node);
167 udf_zone_node = NULL;
168 }
169
170 if (udf_zone_ds != NULL) {
171 uma_zdestroy(udf_zone_ds);
172 udf_zone_ds = NULL;
173 }
174
175 return (0);
176 }
177
178 static int
udf_mount(struct mount * mp)179 udf_mount(struct mount *mp)
180 {
181 struct vnode *devvp; /* vnode of the mount device */
182 struct thread *td;
183 struct udf_mnt *imp = NULL;
184 struct vfsoptlist *opts;
185 char *fspec, *cs_disk, *cs_local;
186 int error, len, *udf_flags;
187 struct nameidata nd, *ndp = &nd;
188
189 td = curthread;
190 opts = mp->mnt_optnew;
191
192 /*
193 * Unconditionally mount as read-only.
194 */
195 MNT_ILOCK(mp);
196 mp->mnt_flag |= MNT_RDONLY;
197 MNT_IUNLOCK(mp);
198
199 /*
200 * No root filesystem support. Probably not a big deal, since the
201 * bootloader doesn't understand UDF.
202 */
203 if (mp->mnt_flag & MNT_ROOTFS)
204 return (ENOTSUP);
205
206 fspec = NULL;
207 error = vfs_getopt(opts, "from", (void **)&fspec, &len);
208 if (!error && fspec[len - 1] != '\0')
209 return (EINVAL);
210
211 if (mp->mnt_flag & MNT_UPDATE) {
212 return (0);
213 }
214
215 /* Check that the mount device exists */
216 if (fspec == NULL)
217 return (EINVAL);
218 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec);
219 if ((error = namei(ndp)))
220 return (error);
221 NDFREE_PNBUF(ndp);
222 devvp = ndp->ni_vp;
223
224 if (!vn_isdisk_error(devvp, &error)) {
225 vput(devvp);
226 return (error);
227 }
228
229 /* Check the access rights on the mount device */
230 error = VOP_ACCESS(devvp, VREAD, td->td_ucred, td);
231 if (error)
232 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
233 if (error) {
234 vput(devvp);
235 return (error);
236 }
237
238 if ((error = udf_mountfs(devvp, mp))) {
239 vrele(devvp);
240 return (error);
241 }
242
243 imp = VFSTOUDFFS(mp);
244
245 udf_flags = NULL;
246 error = vfs_getopt(opts, "flags", (void **)&udf_flags, &len);
247 if (error || len != sizeof(int))
248 return (EINVAL);
249 imp->im_flags = *udf_flags;
250
251 if (imp->im_flags & UDFMNT_KICONV && udf_iconv) {
252 cs_disk = NULL;
253 error = vfs_getopt(opts, "cs_disk", (void **)&cs_disk, &len);
254 if (!error && cs_disk[len - 1] != '\0')
255 return (EINVAL);
256 cs_local = NULL;
257 error = vfs_getopt(opts, "cs_local", (void **)&cs_local, &len);
258 if (!error && cs_local[len - 1] != '\0')
259 return (EINVAL);
260 udf_iconv->open(cs_local, cs_disk, &imp->im_d2l);
261 #if 0
262 udf_iconv->open(cs_disk, cs_local, &imp->im_l2d);
263 #endif
264 }
265
266 vfs_mountedfrom(mp, fspec);
267 return 0;
268 };
269
270 /*
271 * Check the descriptor tag for both the correct id and correct checksum.
272 * Return zero if all is good, EINVAL if not.
273 */
274 int
udf_checktag(struct desc_tag * tag,uint16_t id)275 udf_checktag(struct desc_tag *tag, uint16_t id)
276 {
277 uint8_t *itag;
278 uint8_t i, cksum = 0;
279
280 itag = (uint8_t *)tag;
281
282 if (le16toh(tag->id) != id)
283 return (EINVAL);
284
285 for (i = 0; i < 16; i++)
286 cksum = cksum + itag[i];
287 cksum = cksum - itag[4];
288
289 if (cksum == tag->cksum)
290 return (0);
291
292 return (EINVAL);
293 }
294
295 static int
udf_mountfs(struct vnode * devvp,struct mount * mp)296 udf_mountfs(struct vnode *devvp, struct mount *mp)
297 {
298 struct buf *bp = NULL;
299 struct cdev *dev;
300 struct anchor_vdp avdp;
301 struct udf_mnt *udfmp = NULL;
302 struct part_desc *pd;
303 struct logvol_desc *lvd;
304 struct fileset_desc *fsd;
305 struct file_entry *root_fentry;
306 uint32_t sector, size, mvds_start, mvds_end;
307 uint32_t logical_secsize;
308 uint32_t fsd_offset = 0;
309 uint16_t part_num = 0, fsd_part = 0;
310 int error = EINVAL;
311 int logvol_found = 0, part_found = 0, fsd_found = 0;
312 int bsize;
313 struct g_consumer *cp;
314 struct bufobj *bo;
315
316 dev = devvp->v_rdev;
317 dev_ref(dev);
318 g_topology_lock();
319 error = g_vfs_open(devvp, &cp, "udf", 0);
320 g_topology_unlock();
321 VOP_UNLOCK(devvp);
322 if (error)
323 goto bail;
324
325 bo = &devvp->v_bufobj;
326
327 if (devvp->v_rdev->si_iosize_max != 0)
328 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
329 if (mp->mnt_iosize_max > maxphys)
330 mp->mnt_iosize_max = maxphys;
331
332 /* XXX: should be M_WAITOK */
333 udfmp = malloc(sizeof(struct udf_mnt), M_UDFMOUNT,
334 M_NOWAIT | M_ZERO);
335 if (udfmp == NULL) {
336 printf("Cannot allocate UDF mount struct\n");
337 error = ENOMEM;
338 goto bail;
339 }
340
341 mp->mnt_data = udfmp;
342 mp->mnt_stat.f_fsid.val[0] = dev2udev(devvp->v_rdev);
343 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
344 MNT_ILOCK(mp);
345 mp->mnt_flag |= MNT_LOCAL;
346 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
347 MNT_IUNLOCK(mp);
348 udfmp->im_mountp = mp;
349 udfmp->im_dev = dev;
350 udfmp->im_devvp = devvp;
351 udfmp->im_d2l = NULL;
352 udfmp->im_cp = cp;
353 udfmp->im_bo = bo;
354
355 #if 0
356 udfmp->im_l2d = NULL;
357 #endif
358 /*
359 * The UDF specification defines a logical sectorsize of 2048
360 * for DVD media.
361 */
362 logical_secsize = 2048;
363
364 if (((logical_secsize % cp->provider->sectorsize) != 0) ||
365 (logical_secsize < cp->provider->sectorsize)) {
366 error = EINVAL;
367 goto bail;
368 }
369
370 bsize = cp->provider->sectorsize;
371
372 /*
373 * Get the Anchor Volume Descriptor Pointer from sector 256.
374 * XXX Should also check sector n - 256, n, and 512.
375 */
376 sector = 256;
377 if ((error = bread(devvp, sector * btodb(logical_secsize), bsize,
378 NOCRED, &bp)) != 0)
379 goto bail;
380 if ((error = udf_checktag((struct desc_tag *)bp->b_data, TAGID_ANCHOR)))
381 goto bail;
382
383 bcopy(bp->b_data, &avdp, sizeof(struct anchor_vdp));
384 brelse(bp);
385 bp = NULL;
386
387 /*
388 * Extract the Partition Descriptor and Logical Volume Descriptor
389 * from the Volume Descriptor Sequence.
390 * XXX Should we care about the partition type right now?
391 * XXX What about multiple partitions?
392 */
393 mvds_start = le32toh(avdp.main_vds_ex.loc);
394 mvds_end = mvds_start + (le32toh(avdp.main_vds_ex.len) - 1) / bsize;
395 for (sector = mvds_start; sector < mvds_end; sector++) {
396 if ((error = bread(devvp, sector * btodb(logical_secsize),
397 bsize, NOCRED, &bp)) != 0) {
398 printf("Can't read sector %d of VDS\n", sector);
399 goto bail;
400 }
401 lvd = (struct logvol_desc *)bp->b_data;
402 if (!udf_checktag(&lvd->tag, TAGID_LOGVOL)) {
403 udfmp->bsize = le32toh(lvd->lb_size);
404 if (udfmp->bsize < 0 || udfmp->bsize > maxbcachebuf) {
405 printf("lvd block size %d\n", udfmp->bsize);
406 error = EINVAL;
407 goto bail;
408 }
409 udfmp->bmask = udfmp->bsize - 1;
410 udfmp->bshift = ffs(udfmp->bsize) - 1;
411 fsd_part = le16toh(lvd->_lvd_use.fsd_loc.loc.part_num);
412 fsd_offset = le32toh(lvd->_lvd_use.fsd_loc.loc.lb_num);
413 if (udf_find_partmaps(udfmp, lvd))
414 break;
415 logvol_found = 1;
416 }
417 pd = (struct part_desc *)bp->b_data;
418 if (!udf_checktag(&pd->tag, TAGID_PARTITION)) {
419 part_found = 1;
420 part_num = le16toh(pd->part_num);
421 udfmp->part_len = le32toh(pd->part_len);
422 udfmp->part_start = le32toh(pd->start_loc);
423 }
424
425 brelse(bp);
426 bp = NULL;
427 if ((part_found) && (logvol_found))
428 break;
429 }
430
431 if (!part_found || !logvol_found) {
432 error = EINVAL;
433 goto bail;
434 }
435
436 if (fsd_part != part_num) {
437 printf("FSD does not lie within the partition!\n");
438 error = EINVAL;
439 goto bail;
440 }
441
442 /*
443 * Grab the Fileset Descriptor
444 * Thanks to Chuck McCrobie <mccrobie@cablespeed.com> for pointing
445 * me in the right direction here.
446 */
447 sector = udfmp->part_start + fsd_offset;
448 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
449 printf("Cannot read sector %d of FSD\n", sector);
450 goto bail;
451 }
452 fsd = (struct fileset_desc *)bp->b_data;
453 if (!udf_checktag(&fsd->tag, TAGID_FSD)) {
454 fsd_found = 1;
455 bcopy(&fsd->rootdir_icb, &udfmp->root_icb,
456 sizeof(struct long_ad));
457 }
458
459 brelse(bp);
460 bp = NULL;
461
462 if (!fsd_found) {
463 printf("Couldn't find the fsd\n");
464 error = EINVAL;
465 goto bail;
466 }
467
468 /*
469 * Find the file entry for the root directory.
470 */
471 sector = le32toh(udfmp->root_icb.loc.lb_num) + udfmp->part_start;
472 size = le32toh(udfmp->root_icb.len);
473 if (size < UDF_FENTRY_SIZE) {
474 printf("Invalid root directory file entry length %u\n",
475 size);
476 goto bail;
477 }
478 if ((error = udf_readdevblks(udfmp, sector, size, &bp)) != 0) {
479 printf("Cannot read sector %d\n", sector);
480 goto bail;
481 }
482
483 root_fentry = (struct file_entry *)bp->b_data;
484 if ((error = udf_checktag(&root_fentry->tag, TAGID_FENTRY))) {
485 printf("Invalid root file entry!\n");
486 goto bail;
487 }
488
489 brelse(bp);
490 bp = NULL;
491
492 return 0;
493
494 bail:
495 if (udfmp != NULL)
496 free(udfmp, M_UDFMOUNT);
497 if (bp != NULL)
498 brelse(bp);
499 if (cp != NULL) {
500 g_topology_lock();
501 g_vfs_close(cp);
502 g_topology_unlock();
503 }
504 dev_rel(dev);
505 return error;
506 };
507
508 static int
udf_unmount(struct mount * mp,int mntflags)509 udf_unmount(struct mount *mp, int mntflags)
510 {
511 struct udf_mnt *udfmp;
512 int error, flags = 0;
513
514 udfmp = VFSTOUDFFS(mp);
515
516 if (mntflags & MNT_FORCE)
517 flags |= FORCECLOSE;
518
519 if ((error = vflush(mp, 0, flags, curthread)))
520 return (error);
521
522 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
523 if (udfmp->im_d2l)
524 udf_iconv->close(udfmp->im_d2l);
525 #if 0
526 if (udfmp->im_l2d)
527 udf_iconv->close(udfmp->im_l2d);
528 #endif
529 }
530
531 g_topology_lock();
532 g_vfs_close(udfmp->im_cp);
533 g_topology_unlock();
534 vrele(udfmp->im_devvp);
535 dev_rel(udfmp->im_dev);
536
537 if (udfmp->s_table != NULL)
538 free(udfmp->s_table, M_UDFMOUNT);
539
540 free(udfmp, M_UDFMOUNT);
541
542 mp->mnt_data = NULL;
543 return (0);
544 }
545
546 static int
udf_root(struct mount * mp,int flags,struct vnode ** vpp)547 udf_root(struct mount *mp, int flags, struct vnode **vpp)
548 {
549 struct udf_mnt *udfmp;
550 ino_t id;
551
552 udfmp = VFSTOUDFFS(mp);
553
554 id = udf_getid(&udfmp->root_icb);
555
556 return (udf_vget(mp, id, flags, vpp));
557 }
558
559 static int
udf_statfs(struct mount * mp,struct statfs * sbp)560 udf_statfs(struct mount *mp, struct statfs *sbp)
561 {
562 struct udf_mnt *udfmp;
563
564 udfmp = VFSTOUDFFS(mp);
565
566 sbp->f_bsize = udfmp->bsize;
567 sbp->f_iosize = udfmp->bsize;
568 sbp->f_blocks = udfmp->part_len;
569 sbp->f_bfree = 0;
570 sbp->f_bavail = 0;
571 sbp->f_files = 0;
572 sbp->f_ffree = 0;
573 return 0;
574 }
575
576 int
udf_vget(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp)577 udf_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
578 {
579 struct buf *bp;
580 struct vnode *devvp;
581 struct udf_mnt *udfmp;
582 struct thread *td;
583 struct vnode *vp;
584 struct udf_node *unode;
585 struct file_entry *fe;
586 uint32_t lea, lad;
587 int error, sector, size;
588
589 error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
590 if (error || *vpp != NULL)
591 return (error);
592
593 /*
594 * We must promote to an exclusive lock for vnode creation. This
595 * can happen if lookup is passed LOCKSHARED.
596 */
597 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
598 flags &= ~LK_TYPE_MASK;
599 flags |= LK_EXCLUSIVE;
600 }
601
602 /*
603 * We do not lock vnode creation as it is believed to be too
604 * expensive for such rare case as simultaneous creation of vnode
605 * for same ino by different processes. We just allow them to race
606 * and check later to decide who wins. Let the race begin!
607 */
608
609 td = curthread;
610 udfmp = VFSTOUDFFS(mp);
611
612 unode = uma_zalloc(udf_zone_node, M_WAITOK | M_ZERO);
613
614 if ((error = udf_allocv(mp, &vp, td))) {
615 printf("Error from udf_allocv\n");
616 uma_zfree(udf_zone_node, unode);
617 return (error);
618 }
619
620 unode->i_vnode = vp;
621 unode->hash_id = ino;
622 unode->udfmp = udfmp;
623 vp->v_data = unode;
624
625 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
626 error = insmntque(vp, mp);
627 if (error != 0) {
628 uma_zfree(udf_zone_node, unode);
629 return (error);
630 }
631 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
632 if (error || *vpp != NULL)
633 return (error);
634
635 /*
636 * Copy in the file entry. Per the spec, the size can only be 1 block.
637 */
638 sector = ino + udfmp->part_start;
639 devvp = udfmp->im_devvp;
640 if ((error = RDSECTOR(devvp, sector, udfmp->bsize, &bp)) != 0) {
641 printf("Cannot read sector %d\n", sector);
642 goto error;
643 }
644
645 /*
646 * File entry length validation.
647 */
648 fe = (struct file_entry *)bp->b_data;
649 if (udf_checktag(&fe->tag, TAGID_FENTRY)) {
650 printf("Invalid file entry!\n");
651 error = ENOMEM;
652 goto error;
653 }
654 lea = le32toh(fe->l_ea);
655 lad = le32toh(fe->l_ad);
656 if (lea > udfmp->bsize || lad > udfmp->bsize) {
657 printf("Invalid EA and AD lengths %u, %u\n", lea, lad);
658 error = EIO;
659 goto error;
660 }
661 size = UDF_FENTRY_SIZE + lea + lad;
662 if (size > udfmp->bsize) {
663 printf("Invalid file entry size %u\n", size);
664 error = EIO;
665 goto error;
666 }
667
668 unode->fentry = malloc(size, M_UDFFENTRY, M_NOWAIT | M_ZERO);
669 if (unode->fentry == NULL) {
670 printf("Cannot allocate file entry block\n");
671 error = ENOMEM;
672 goto error;
673 }
674
675 bcopy(bp->b_data, unode->fentry, size);
676
677 brelse(bp);
678 bp = NULL;
679
680 switch (unode->fentry->icbtag.file_type) {
681 default:
682 vp->v_type = VBAD;
683 break;
684 case 4:
685 vp->v_type = VDIR;
686 break;
687 case 5:
688 vp->v_type = VREG;
689 break;
690 case 6:
691 vp->v_type = VBLK;
692 break;
693 case 7:
694 vp->v_type = VCHR;
695 break;
696 case 9:
697 vp->v_type = VFIFO;
698 vp->v_op = &udf_fifoops;
699 break;
700 case 10:
701 vp->v_type = VSOCK;
702 break;
703 case 12:
704 vp->v_type = VLNK;
705 break;
706 }
707
708 if (vp->v_type != VFIFO)
709 VN_LOCK_ASHARE(vp);
710
711 if (ino == udf_getid(&udfmp->root_icb))
712 vp->v_vflag |= VV_ROOT;
713
714 vn_set_state(vp, VSTATE_CONSTRUCTED);
715 *vpp = vp;
716
717 return (0);
718
719 error:
720 vgone(vp);
721 vput(vp);
722 brelse(bp);
723 *vpp = NULL;
724 return (error);
725 }
726
727 static int
udf_fhtovp(struct mount * mp,struct fid * fhp,int flags,struct vnode ** vpp)728 udf_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
729 {
730 struct ifid *ifhp;
731 struct vnode *nvp;
732 struct udf_node *np;
733 uint64_t fsize;
734 int error;
735
736 ifhp = (struct ifid *)fhp;
737
738 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
739 *vpp = NULLVP;
740 return (error);
741 }
742
743 np = VTON(nvp);
744 fsize = le64toh(np->fentry->inf_len);
745 if (fsize > OFF_MAX) {
746 *vpp = NULLVP;
747 return (EIO);
748 }
749
750 *vpp = nvp;
751 vnode_create_vobject(*vpp, fsize, curthread);
752 return (0);
753 }
754
755 static int
udf_find_partmaps(struct udf_mnt * udfmp,struct logvol_desc * lvd)756 udf_find_partmaps(struct udf_mnt *udfmp, struct logvol_desc *lvd)
757 {
758 struct part_map_spare *pms;
759 struct regid *pmap_id;
760 struct buf *bp;
761 unsigned char regid_id[UDF_REGID_ID_SIZE + 1];
762 int i, k, ptype, psize, error;
763 uint8_t *pmap = (uint8_t *) &lvd->maps[0];
764
765 for (i = 0; i < le32toh(lvd->n_pm); i++) {
766 ptype = pmap[0];
767 psize = pmap[1];
768 if (((ptype != 1) && (ptype != 2)) ||
769 ((psize != UDF_PMAP_TYPE1_SIZE) &&
770 (psize != UDF_PMAP_TYPE2_SIZE))) {
771 printf("Invalid partition map found\n");
772 return (1);
773 }
774
775 if (ptype == 1) {
776 /* Type 1 map. We don't care */
777 pmap += UDF_PMAP_TYPE1_SIZE;
778 continue;
779 }
780
781 /* Type 2 map. Gotta find out the details */
782 pmap_id = (struct regid *)&pmap[4];
783 bzero(®id_id[0], UDF_REGID_ID_SIZE);
784 bcopy(&pmap_id->id[0], ®id_id[0], UDF_REGID_ID_SIZE);
785
786 if (bcmp(®id_id[0], "*UDF Sparable Partition",
787 UDF_REGID_ID_SIZE)) {
788 printf("Unsupported partition map: %s\n", ®id_id[0]);
789 return (1);
790 }
791
792 pms = (struct part_map_spare *)pmap;
793 pmap += UDF_PMAP_TYPE2_SIZE;
794 udfmp->s_table = malloc(le32toh(pms->st_size),
795 M_UDFMOUNT, M_NOWAIT | M_ZERO);
796 if (udfmp->s_table == NULL)
797 return (ENOMEM);
798
799 /* Calculate the number of sectors per packet. */
800 /* XXX Logical or physical? */
801 udfmp->p_sectors = le16toh(pms->packet_len) / udfmp->bsize;
802
803 /*
804 * XXX If reading the first Sparing Table fails, should look
805 * for another table.
806 */
807 if ((error = udf_readdevblks(udfmp, le32toh(pms->st_loc[0]),
808 le32toh(pms->st_size), &bp)) != 0) {
809 if (bp != NULL)
810 brelse(bp);
811 printf("Failed to read Sparing Table at sector %d\n",
812 le32toh(pms->st_loc[0]));
813 free(udfmp->s_table, M_UDFMOUNT);
814 return (error);
815 }
816 bcopy(bp->b_data, udfmp->s_table, le32toh(pms->st_size));
817 brelse(bp);
818
819 if (udf_checktag(&udfmp->s_table->tag, 0)) {
820 printf("Invalid sparing table found\n");
821 free(udfmp->s_table, M_UDFMOUNT);
822 return (EINVAL);
823 }
824
825 /* See how many valid entries there are here. The list is
826 * supposed to be sorted. 0xfffffff0 and higher are not valid
827 */
828 for (k = 0; k < le16toh(udfmp->s_table->rt_l); k++) {
829 udfmp->s_table_entries = k;
830 if (le32toh(udfmp->s_table->entries[k].org) >=
831 0xfffffff0)
832 break;
833 }
834 }
835
836 return (0);
837 }
838