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