cd9660_vfsops.c revision 1.54 1 /* $NetBSD: cd9660_vfsops.c,v 1.54 2008/01/24 17:32:52 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace (at) blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai (at) spec.co.jp).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_vfsops.c,v 1.54 2008/01/24 17:32:52 ad Exp $");
41
42 #if defined(_KERNEL_OPT)
43 #include "opt_compat_netbsd.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <miscfs/specfs/specdev.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/file.h>
57 #include <sys/disklabel.h>
58 #include <sys/device.h>
59 #include <sys/ioctl.h>
60 #include <sys/cdio.h>
61 #include <sys/errno.h>
62 #include <sys/malloc.h>
63 #include <sys/pool.h>
64 #include <sys/stat.h>
65 #include <sys/conf.h>
66 #include <sys/dirent.h>
67 #include <sys/kauth.h>
68
69 #include <fs/cd9660/iso.h>
70 #include <fs/cd9660/cd9660_extern.h>
71 #include <fs/cd9660/iso_rrip.h>
72 #include <fs/cd9660/cd9660_node.h>
73 #include <fs/cd9660/cd9660_mount.h>
74
75 MALLOC_JUSTDEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
76
77 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc;
78 extern const struct vnodeopv_desc cd9660_specop_opv_desc;
79 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc;
80
81 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = {
82 &cd9660_vnodeop_opv_desc,
83 &cd9660_specop_opv_desc,
84 &cd9660_fifoop_opv_desc,
85 NULL,
86 };
87
88 struct vfsops cd9660_vfsops = {
89 MOUNT_CD9660,
90 sizeof (struct iso_args),
91 cd9660_mount,
92 cd9660_start,
93 cd9660_unmount,
94 cd9660_root,
95 (void *)eopnotsupp,
96 cd9660_statvfs,
97 cd9660_sync,
98 cd9660_vget,
99 cd9660_fhtovp,
100 cd9660_vptofh,
101 cd9660_init,
102 cd9660_reinit,
103 cd9660_done,
104 cd9660_mountroot,
105 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
106 vfs_stdextattrctl,
107 (void *)eopnotsupp, /* vfs_suspendctl */
108 cd9660_vnodeopv_descs,
109 0, /* refcount */
110 { NULL, NULL } /* list */
111 };
112 VFS_ATTACH(cd9660_vfsops);
113
114 static const struct genfs_ops cd9660_genfsops = {
115 .gop_size = genfs_size,
116 };
117
118 /*
119 * Called by vfs_mountroot when iso is going to be mounted as root.
120 *
121 * Name is updated by mount(8) after booting.
122 */
123 #define ROOTNAME "root_device"
124
125 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len);
126 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
127 struct lwp *l, struct iso_args *argp);
128
129 int
130 cd9660_mountroot()
131 {
132 struct mount *mp;
133 struct lwp *l = curlwp;
134 int error;
135 struct iso_args args;
136
137 if (device_class(root_device) != DV_DISK)
138 return (ENODEV);
139
140 if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
141 != 0) {
142 vrele(rootvp);
143 return (error);
144 }
145
146 args.flags = ISOFSMNT_ROOT;
147 if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) {
148 mp->mnt_op->vfs_refcount--;
149 vfs_unbusy(mp);
150 vfs_destroy(mp);
151 return (error);
152 }
153 mutex_enter(&mountlist_lock);
154 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
155 mutex_exit(&mountlist_lock);
156 (void)cd9660_statvfs(mp, &mp->mnt_stat);
157 vfs_unbusy(mp);
158 return (0);
159 }
160
161 /*
162 * VFS Operations.
163 *
164 * mount system call
165 */
166 int
167 cd9660_mount(mp, path, data, data_len)
168 struct mount *mp;
169 const char *path;
170 void *data;
171 size_t *data_len;
172 {
173 struct lwp *l = curlwp;
174 struct nameidata nd;
175 struct vnode *devvp;
176 struct iso_args *args = data;
177 int error;
178 struct iso_mnt *imp = VFSTOISOFS(mp);
179
180 if (*data_len < sizeof *args)
181 return EINVAL;
182
183 if (mp->mnt_flag & MNT_GETARGS) {
184 if (imp == NULL)
185 return EIO;
186 args->fspec = NULL;
187 args->flags = imp->im_flags;
188 *data_len = sizeof (*args);
189 return 0;
190 }
191
192 if ((mp->mnt_flag & MNT_RDONLY) == 0)
193 return (EROFS);
194
195 if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
196 return EINVAL;
197
198 /*
199 * Not an update, or updating the name: look up the name
200 * and verify that it refers to a sensible block device.
201 */
202 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
203 if ((error = namei(&nd)) != 0)
204 return (error);
205 devvp = nd.ni_vp;
206
207 if (devvp->v_type != VBLK) {
208 vrele(devvp);
209 return ENOTBLK;
210 }
211 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
212 vrele(devvp);
213 return ENXIO;
214 }
215 /*
216 * If mount by non-root, then verify that user has necessary
217 * permissions on the device.
218 */
219 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
220 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
221 error = VOP_ACCESS(devvp, VREAD, l->l_cred);
222 VOP_UNLOCK(devvp, 0);
223 if (error) {
224 vrele(devvp);
225 return (error);
226 }
227 }
228 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
229 error = VOP_OPEN(devvp, FREAD, FSCRED);
230 if (error)
231 goto fail;
232 error = iso_mountfs(devvp, mp, l, args);
233 if (error) {
234 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
235 (void)VOP_CLOSE(devvp, FREAD, NOCRED);
236 VOP_UNLOCK(devvp, 0);
237 goto fail;
238 }
239 } else {
240 vrele(devvp);
241 if (devvp != imp->im_devvp)
242 return (EINVAL); /* needs translation */
243 }
244 return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
245 mp->mnt_op->vfs_name, mp, l);
246
247 fail:
248 vrele(devvp);
249 return (error);
250 }
251
252 /*
253 * Make a mount point from a volume descriptor
254 */
255 static int
256 iso_makemp(isomp, bp, ea_len)
257 struct iso_mnt *isomp;
258 struct buf *bp;
259 int *ea_len;
260 {
261 struct iso_primary_descriptor *pri;
262 int logical_block_size;
263 struct iso_directory_record *rootp;
264
265 pri = (struct iso_primary_descriptor *)bp->b_data;
266
267 logical_block_size = isonum_723 (pri->logical_block_size);
268
269 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
270 || (logical_block_size & (logical_block_size - 1)) != 0)
271 return -1;
272
273 rootp = (struct iso_directory_record *)pri->root_directory_record;
274
275 isomp->logical_block_size = logical_block_size;
276 isomp->volume_space_size = isonum_733 (pri->volume_space_size);
277 memcpy(isomp->root, rootp, sizeof(isomp->root));
278 isomp->root_extent = isonum_733 (rootp->extent);
279 isomp->root_size = isonum_733 (rootp->size);
280 isomp->im_joliet_level = 0;
281
282 isomp->im_bmask = logical_block_size - 1;
283 isomp->im_bshift = 0;
284 while ((1 << isomp->im_bshift) < isomp->logical_block_size)
285 isomp->im_bshift++;
286
287 if (ea_len != NULL)
288 *ea_len = isonum_711(rootp->ext_attr_length);
289
290 return 0;
291 }
292
293 /*
294 * Common code for mount and mountroot
295 */
296 static int
297 iso_mountfs(devvp, mp, l, argp)
298 struct vnode *devvp;
299 struct mount *mp;
300 struct lwp *l;
301 struct iso_args *argp;
302 {
303 struct iso_mnt *isomp = (struct iso_mnt *)0;
304 struct buf *bp = NULL, *pribp = NULL, *supbp = NULL;
305 dev_t dev = devvp->v_rdev;
306 int error = EINVAL;
307 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
308 int iso_bsize;
309 int iso_blknum;
310 int joliet_level;
311 struct iso_volume_descriptor *vdp;
312 struct iso_supplementary_descriptor *sup;
313 int sess = 0;
314 int ext_attr_length;
315 struct disklabel label;
316
317 if (!ronly)
318 return EROFS;
319
320 /* Flush out any old buffers remaining from a previous use. */
321 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
322 return (error);
323
324 /* This is the "logical sector size". The standard says this
325 * should be 2048 or the physical sector size on the device,
326 * whichever is greater. For now, we'll just use a constant.
327 */
328 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
329
330 error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED);
331 if (!error &&
332 label.d_partitions[DISKPART(dev)].p_fstype == FS_ISO9660) {
333 /* XXX more sanity checks? */
334 sess = label.d_partitions[DISKPART(dev)].p_cdsession;
335 } else {
336 /* fallback to old method */
337 error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED);
338 if (error)
339 sess = 0; /* never mind */
340 }
341 #ifdef ISO_DEBUG
342 printf("isofs: session offset (part %d) %d\n", DISKPART(dev), sess);
343 #endif
344
345 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
346 if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize),
347 iso_bsize, NOCRED, &bp)) != 0)
348 goto out;
349
350 vdp = (struct iso_volume_descriptor *)bp->b_data;
351 if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) {
352 error = EINVAL;
353 goto out;
354 }
355
356 switch (isonum_711(vdp->type)) {
357 case ISO_VD_PRIMARY:
358 if (pribp == NULL) {
359 pribp = bp;
360 bp = NULL;
361 }
362 break;
363
364 case ISO_VD_SUPPLEMENTARY:
365 if (supbp == NULL) {
366 supbp = bp;
367 bp = NULL;
368 }
369 break;
370
371 default:
372 break;
373 }
374
375 if (isonum_711 (vdp->type) == ISO_VD_END) {
376 brelse(bp, 0);
377 bp = NULL;
378 break;
379 }
380
381 if (bp != NULL) {
382 brelse(bp, 0);
383 bp = NULL;
384 }
385 }
386
387 if (pribp == NULL) {
388 error = EINVAL;
389 goto out;
390 }
391
392 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
393 memset(isomp, 0, sizeof *isomp);
394 if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) {
395 error = EINVAL;
396 goto out;
397 }
398
399 isomp->volume_space_size += sess;
400
401 brelse(pribp, BC_AGE);
402 pribp = NULL;
403
404 mp->mnt_data = isomp;
405 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
406 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
407 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
408 mp->mnt_stat.f_namemax = MAXNAMLEN;
409 mp->mnt_flag |= MNT_LOCAL;
410 mp->mnt_iflag |= IMNT_MPSAFE;
411 mp->mnt_dev_bshift = iso_bsize;
412 mp->mnt_fs_bshift = isomp->im_bshift;
413 isomp->im_mountp = mp;
414 isomp->im_dev = dev;
415 isomp->im_devvp = devvp;
416
417 /* Check the Rock Ridge Extension support */
418 if (!(argp->flags & ISOFSMNT_NORRIP)) {
419 struct iso_directory_record *rootp;
420
421 if ((error = bread(isomp->im_devvp,
422 (isomp->root_extent + ext_attr_length) <<
423 (isomp->im_bshift - DEV_BSHIFT),
424 isomp->logical_block_size, NOCRED,
425 &bp)) != 0)
426 goto out;
427
428 rootp = (struct iso_directory_record *)bp->b_data;
429
430 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
431 argp->flags |= ISOFSMNT_NORRIP;
432 } else {
433 argp->flags &= ~ISOFSMNT_GENS;
434 }
435
436 /*
437 * The contents are valid,
438 * but they will get reread as part of another vnode, so...
439 */
440 brelse(bp, BC_AGE);
441 bp = NULL;
442 }
443 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
444 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
445
446 if (isomp->im_flags & ISOFSMNT_GENS)
447 isomp->iso_ftype = ISO_FTYPE_9660;
448 else if (isomp->im_flags & ISOFSMNT_NORRIP) {
449 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
450 if (argp->flags & ISOFSMNT_NOCASETRANS)
451 isomp->im_flags |= ISOFSMNT_NOCASETRANS;
452 } else
453 isomp->iso_ftype = ISO_FTYPE_RRIP;
454
455 /* Check the Joliet Extension support */
456 if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
457 (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
458 supbp != NULL) {
459 joliet_level = 0;
460 sup = (struct iso_supplementary_descriptor *)supbp->b_data;
461
462 if ((isonum_711(sup->flags) & 1) == 0) {
463 if (memcmp(sup->escape, "%/@", 3) == 0)
464 joliet_level = 1;
465 if (memcmp(sup->escape, "%/C", 3) == 0)
466 joliet_level = 2;
467 if (memcmp(sup->escape, "%/E", 3) == 0)
468 joliet_level = 3;
469 }
470 if (joliet_level != 0) {
471 if (iso_makemp(isomp, supbp, NULL) == -1) {
472 error = EINVAL;
473 goto out;
474 }
475 isomp->im_joliet_level = joliet_level;
476 }
477 }
478
479 if (supbp != NULL) {
480 brelse(supbp, 0);
481 supbp = NULL;
482 }
483
484 devvp->v_specmountpoint = mp;
485
486 return 0;
487 out:
488 if (bp)
489 brelse(bp, 0);
490 if (pribp)
491 brelse(pribp, 0);
492 if (supbp)
493 brelse(supbp, 0);
494 if (isomp) {
495 free(isomp, M_ISOFSMNT);
496 mp->mnt_data = NULL;
497 }
498 return error;
499 }
500
501 /*
502 * Make a filesystem operational.
503 * Nothing to do at the moment.
504 */
505 /* ARGSUSED */
506 int
507 cd9660_start(struct mount *mp, int flags)
508 {
509 return 0;
510 }
511
512 /*
513 * unmount system call
514 */
515 int
516 cd9660_unmount(mp, mntflags)
517 struct mount *mp;
518 int mntflags;
519 {
520 struct iso_mnt *isomp;
521 int error, flags = 0;
522
523 if (mntflags & MNT_FORCE)
524 flags |= FORCECLOSE;
525 if ((error = vflush(mp, NULLVP, flags)) != 0)
526 return (error);
527
528 isomp = VFSTOISOFS(mp);
529
530 if (isomp->im_devvp->v_type != VBAD)
531 isomp->im_devvp->v_specmountpoint = NULL;
532
533 vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
534 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED);
535 vput(isomp->im_devvp);
536 free(isomp, M_ISOFSMNT);
537 mp->mnt_data = NULL;
538 mp->mnt_flag &= ~MNT_LOCAL;
539 return (error);
540 }
541
542 /*
543 * Return root of a filesystem
544 */
545 int
546 cd9660_root(mp, vpp)
547 struct mount *mp;
548 struct vnode **vpp;
549 {
550 struct iso_mnt *imp = VFSTOISOFS(mp);
551 struct iso_directory_record *dp =
552 (struct iso_directory_record *)imp->root;
553 ino_t ino = isodirino(dp, imp);
554
555 /*
556 * With RRIP we must use the `.' entry of the root directory.
557 * Simply tell vget, that it's a relocated directory.
558 */
559 return (cd9660_vget_internal(mp, ino, vpp,
560 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
561 }
562
563 /*
564 * Get file system statistics.
565 */
566 int
567 cd9660_statvfs(
568 struct mount *mp,
569 struct statvfs *sbp)
570 {
571 struct iso_mnt *isomp;
572
573 isomp = VFSTOISOFS(mp);
574
575 sbp->f_bsize = isomp->logical_block_size;
576 sbp->f_frsize = sbp->f_bsize;
577 sbp->f_iosize = sbp->f_bsize; /* XXX */
578 sbp->f_blocks = isomp->volume_space_size;
579 sbp->f_bfree = 0; /* total free blocks */
580 sbp->f_bavail = 0; /* blocks free for non superuser */
581 sbp->f_bresvd = 0; /* total reserved blocks */
582 sbp->f_files = 0; /* total files */
583 sbp->f_ffree = 0; /* free file nodes */
584 sbp->f_favail = 0; /* free file nodes for non superuser */
585 sbp->f_fresvd = 0; /* reserved file nodes */
586 copy_statvfs_info(sbp, mp);
587 /* Use the first spare for flags: */
588 sbp->f_spare[0] = isomp->im_flags;
589 return 0;
590 }
591
592 /* ARGSUSED */
593 int
594 cd9660_sync(
595 struct mount *mp,
596 int waitfor,
597 kauth_cred_t cred)
598 {
599 return (0);
600 }
601
602 /*
603 * File handle to vnode
604 *
605 * Have to be really careful about stale file handles:
606 * - check that the inode number is in range
607 * - call iget() to get the locked inode
608 * - check for an unallocated inode (i_mode == 0)
609 * - check that the generation number matches
610 */
611
612 struct ifid {
613 ushort ifid_len;
614 ushort ifid_pad;
615 int ifid_ino;
616 long ifid_start;
617 };
618
619 /* ARGSUSED */
620 int
621 cd9660_fhtovp(mp, fhp, vpp)
622 struct mount *mp;
623 struct fid *fhp;
624 struct vnode **vpp;
625 {
626 struct ifid ifh;
627 struct iso_node *ip;
628 struct vnode *nvp;
629 int error;
630
631 if (fhp->fid_len != sizeof(ifh))
632 return EINVAL;
633
634 memcpy(&ifh, fhp, sizeof(ifh));
635 #ifdef ISOFS_DBG
636 printf("fhtovp: ino %d, start %ld\n",
637 ifh.ifid_ino, ifh.ifid_start);
638 #endif
639
640 if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
641 *vpp = NULLVP;
642 return (error);
643 }
644 ip = VTOI(nvp);
645 if (ip->inode.iso_mode == 0) {
646 vput(nvp);
647 *vpp = NULLVP;
648 return (ESTALE);
649 }
650 *vpp = nvp;
651 return (0);
652 }
653
654 int
655 cd9660_vget(mp, ino, vpp)
656 struct mount *mp;
657 ino_t ino;
658 struct vnode **vpp;
659 {
660
661 /*
662 * XXXX
663 * It would be nice if we didn't always set the `relocated' flag
664 * and force the extra read, but I don't want to think about fixing
665 * that right now.
666 */
667 return (cd9660_vget_internal(mp, ino, vpp,
668 #if 0
669 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
670 #else
671 0,
672 #endif
673 NULL));
674 }
675
676 int
677 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
678 struct mount *mp;
679 ino_t ino;
680 struct vnode **vpp;
681 int relocated;
682 struct iso_directory_record *isodir;
683 {
684 struct iso_mnt *imp;
685 struct iso_node *ip;
686 struct buf *bp;
687 struct vnode *vp;
688 dev_t dev;
689 int error;
690
691 imp = VFSTOISOFS(mp);
692 dev = imp->im_dev;
693
694 retry:
695 if ((*vpp = cd9660_ihashget(dev, ino, LK_EXCLUSIVE)) != NULLVP)
696 return (0);
697
698 /* Allocate a new vnode/iso_node. */
699 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
700 *vpp = NULLVP;
701 return (error);
702 }
703 ip = pool_get(&cd9660_node_pool, PR_WAITOK);
704
705 /*
706 * If someone beat us to it, put back the freshly allocated
707 * vnode/inode pair and retry.
708 */
709 mutex_enter(&cd9660_hashlock);
710 if (cd9660_ihashget(dev, ino, 0) != NULL) {
711 mutex_exit(&cd9660_hashlock);
712 ungetnewvnode(vp);
713 pool_put(&cd9660_node_pool, ip);
714 goto retry;
715 }
716
717 memset(ip, 0, sizeof(struct iso_node));
718 vp->v_data = ip;
719 ip->i_vnode = vp;
720 ip->i_dev = dev;
721 ip->i_number = ino;
722 ip->i_mnt = imp;
723 ip->i_devvp = imp->im_devvp;
724 genfs_node_init(vp, &cd9660_genfsops);
725
726 /*
727 * Put it onto its hash chain and lock it so that other requests for
728 * this inode will block if they arrive while we are sleeping waiting
729 * for old data structures to be purged or for the contents of the
730 * disk portion of this inode to be read.
731 */
732 cd9660_ihashins(ip);
733 mutex_exit(&cd9660_hashlock);
734
735 if (isodir == 0) {
736 int lbn, off;
737
738 lbn = lblkno(imp, ino);
739 if (lbn >= imp->volume_space_size) {
740 vput(vp);
741 printf("fhtovp: lbn exceed volume space %d\n", lbn);
742 return (ESTALE);
743 }
744
745 off = blkoff(imp, ino);
746 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
747 vput(vp);
748 printf("fhtovp: crosses block boundary %d\n",
749 off + ISO_DIRECTORY_RECORD_SIZE);
750 return (ESTALE);
751 }
752
753 error = bread(imp->im_devvp,
754 lbn << (imp->im_bshift - DEV_BSHIFT),
755 imp->logical_block_size, NOCRED, &bp);
756 if (error) {
757 vput(vp);
758 brelse(bp, 0);
759 printf("fhtovp: bread error %d\n",error);
760 return (error);
761 }
762 isodir = (struct iso_directory_record *)((char *)bp->b_data + off);
763
764 if (off + isonum_711(isodir->length) >
765 imp->logical_block_size) {
766 vput(vp);
767 if (bp != 0)
768 brelse(bp, 0);
769 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
770 off +isonum_711(isodir->length), off,
771 isonum_711(isodir->length));
772 return (ESTALE);
773 }
774
775 #if 0
776 if (isonum_733(isodir->extent) +
777 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
778 if (bp != 0)
779 brelse(bp, 0);
780 printf("fhtovp: file start miss %d vs %d\n",
781 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
782 ifhp->ifid_start);
783 return (ESTALE);
784 }
785 #endif
786 } else
787 bp = 0;
788
789 VREF(ip->i_devvp);
790
791 if (relocated) {
792 /*
793 * On relocated directories we must
794 * read the `.' entry out of a dir.
795 */
796 ip->iso_start = ino >> imp->im_bshift;
797 if (bp != 0)
798 brelse(bp, 0);
799 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
800 vput(vp);
801 return (error);
802 }
803 isodir = (struct iso_directory_record *)bp->b_data;
804 }
805
806 ip->iso_extent = isonum_733(isodir->extent);
807 ip->i_size = isonum_733(isodir->size);
808 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
809
810 /*
811 * Setup time stamp, attribute
812 */
813 vp->v_type = VNON;
814 switch (imp->iso_ftype) {
815 default: /* ISO_FTYPE_9660 */
816 {
817 struct buf *bp2;
818 int off;
819 if ((imp->im_flags & ISOFSMNT_EXTATT)
820 && (off = isonum_711(isodir->ext_attr_length)))
821 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
822 NULL, &bp2);
823 else
824 bp2 = NULL;
825 cd9660_defattr(isodir, ip, bp2);
826 cd9660_deftstamp(isodir, ip, bp2);
827 if (bp2)
828 brelse(bp2, 0);
829 break;
830 }
831 case ISO_FTYPE_RRIP:
832 cd9660_rrip_analyze(isodir, ip, imp);
833 break;
834 }
835
836 if (bp != 0)
837 brelse(bp, 0);
838
839 /*
840 * Initialize the associated vnode
841 */
842 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
843 case VFIFO:
844 vp->v_op = cd9660_fifoop_p;
845 break;
846 case VCHR:
847 case VBLK:
848 /*
849 * if device, look at device number table for translation
850 */
851 vp->v_op = cd9660_specop_p;
852 spec_node_init(vp, ip->inode.iso_rdev);
853 break;
854 case VLNK:
855 case VNON:
856 case VSOCK:
857 case VDIR:
858 case VBAD:
859 break;
860 case VREG:
861 uvm_vnp_setsize(vp, ip->i_size);
862 break;
863 }
864
865 if (vp->v_type != VREG)
866 uvm_vnp_setsize(vp, 0);
867
868 if (ip->iso_extent == imp->root_extent)
869 vp->v_vflag |= VV_ROOT;
870
871 /*
872 * XXX need generation number?
873 */
874
875 *vpp = vp;
876 return (0);
877 }
878
879 /*
880 * Vnode pointer to File handle
881 */
882 /* ARGSUSED */
883 int
884 cd9660_vptofh(vp, fhp, fh_size)
885 struct vnode *vp;
886 struct fid *fhp;
887 size_t *fh_size;
888 {
889 struct iso_node *ip = VTOI(vp);
890 struct ifid ifh;
891
892 if (*fh_size < sizeof(struct ifid)) {
893 *fh_size = sizeof(struct ifid);
894 return E2BIG;
895 }
896 *fh_size = sizeof(struct ifid);
897
898 memset(&ifh, 0, sizeof(ifh));
899 ifh.ifid_len = sizeof(struct ifid);
900 ifh.ifid_ino = ip->i_number;
901 ifh.ifid_start = ip->iso_start;
902 memcpy(fhp, &ifh, sizeof(ifh));
903
904 #ifdef ISOFS_DBG
905 printf("vptofh: ino %d, start %ld\n",
906 ifh.ifid_ino,ifh.ifid_start);
907 #endif
908 return 0;
909 }
910
911 SYSCTL_SETUP(sysctl_vfs_cd9660_setup, "sysctl vfs.cd9660 subtree setup")
912 {
913
914 sysctl_createv(clog, 0, NULL, NULL,
915 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
916 NULL, 0, NULL, 0,
917 CTL_VFS, CTL_EOL);
918 sysctl_createv(clog, 0, NULL, NULL,
919 CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
920 SYSCTL_DESCR("ISO-9660 file system"),
921 NULL, 0, NULL, 0,
922 CTL_VFS, 14, CTL_EOL);
923
924 sysctl_createv(clog, 0, NULL, NULL,
925 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
926 CTLTYPE_INT, "utf8_joliet",
927 SYSCTL_DESCR("Encode Joliet file names to UTF-8"),
928 NULL, 0, &cd9660_utf8_joliet, 0,
929 CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
930
931 }
932