cd9660_vfsops.c revision 1.36 1 /* $NetBSD: cd9660_vfsops.c,v 1.36 2006/10/12 01:32:10 christos 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.36 2006/10/12 01:32:10 christos 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_DEFINE(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 cd9660_mount,
91 cd9660_start,
92 cd9660_unmount,
93 cd9660_root,
94 cd9660_quotactl,
95 cd9660_statvfs,
96 cd9660_sync,
97 cd9660_vget,
98 cd9660_fhtovp,
99 cd9660_vptofh,
100 cd9660_init,
101 cd9660_reinit,
102 cd9660_done,
103 cd9660_mountroot,
104 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
105 vfs_stdextattrctl,
106 cd9660_vnodeopv_descs,
107 0, /* refcount */
108 { NULL, NULL } /* list */
109 };
110 VFS_ATTACH(cd9660_vfsops);
111
112 static const struct genfs_ops cd9660_genfsops = {
113 .gop_size = genfs_size,
114 };
115
116 /*
117 * Called by vfs_mountroot when iso is going to be mounted as root.
118 *
119 * Name is updated by mount(8) after booting.
120 */
121 #define ROOTNAME "root_device"
122
123 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len);
124 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
125 struct lwp *l, struct iso_args *argp);
126
127 int
128 cd9660_mountroot()
129 {
130 struct mount *mp;
131 struct lwp *l = curlwp; /* XXX */
132 int error;
133 struct iso_args args;
134
135 if (device_class(root_device) != DV_DISK)
136 return (ENODEV);
137
138 if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
139 != 0) {
140 vrele(rootvp);
141 return (error);
142 }
143
144 args.flags = ISOFSMNT_ROOT;
145 if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) {
146 mp->mnt_op->vfs_refcount--;
147 vfs_unbusy(mp);
148 free(mp, M_MOUNT);
149 return (error);
150 }
151 simple_lock(&mountlist_slock);
152 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
153 simple_unlock(&mountlist_slock);
154 (void)cd9660_statvfs(mp, &mp->mnt_stat, l);
155 vfs_unbusy(mp);
156 return (0);
157 }
158
159 /*
160 * VFS Operations.
161 *
162 * mount system call
163 */
164 int
165 cd9660_mount(mp, path, data, ndp, l)
166 struct mount *mp;
167 const char *path;
168 void *data;
169 struct nameidata *ndp;
170 struct lwp *l;
171 {
172 struct vnode *devvp;
173 struct iso_args args;
174 int error;
175 struct iso_mnt *imp = VFSTOISOFS(mp);
176
177 if (mp->mnt_flag & MNT_GETARGS) {
178 if (imp == NULL)
179 return EIO;
180 args.fspec = NULL;
181 args.flags = imp->im_flags;
182 return copyout(&args, data, sizeof(args));
183 }
184 error = copyin(data, &args, sizeof (struct iso_args));
185 if (error)
186 return (error);
187
188 if ((mp->mnt_flag & MNT_RDONLY) == 0)
189 return (EROFS);
190
191 if ((mp->mnt_flag & MNT_UPDATE) && args.fspec == NULL)
192 return EINVAL;
193
194 /*
195 * Not an update, or updating the name: look up the name
196 * and verify that it refers to a sensible block device.
197 */
198 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
199 if ((error = namei(ndp)) != 0)
200 return (error);
201 devvp = ndp->ni_vp;
202
203 if (devvp->v_type != VBLK) {
204 vrele(devvp);
205 return ENOTBLK;
206 }
207 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
208 vrele(devvp);
209 return ENXIO;
210 }
211 /*
212 * If mount by non-root, then verify that user has necessary
213 * permissions on the device.
214 */
215 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
216 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
217 error = VOP_ACCESS(devvp, VREAD, l->l_cred, l);
218 VOP_UNLOCK(devvp, 0);
219 if (error) {
220 vrele(devvp);
221 return (error);
222 }
223 }
224 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
225 /*
226 * Disallow multiple mounts of the same device.
227 * Disallow mounting of a device that is currently in use
228 * (except for root, which might share swap device for
229 * miniroot).
230 */
231 error = vfs_mountedon(devvp);
232 if (error)
233 goto fail;
234 if (vcount(devvp) > 1 && devvp != rootvp) {
235 error = EBUSY;
236 goto fail;
237 }
238 error = VOP_OPEN(devvp, FREAD, FSCRED, l);
239 if (error)
240 goto fail;
241 error = iso_mountfs(devvp, mp, l, &args);
242 if (error) {
243 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
244 (void)VOP_CLOSE(devvp, FREAD, NOCRED, l);
245 VOP_UNLOCK(devvp, 0);
246 goto fail;
247 }
248 } else {
249 vrele(devvp);
250 if (devvp != imp->im_devvp)
251 return (EINVAL); /* needs translation */
252 }
253 return set_statvfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
254 mp, l);
255
256 fail:
257 vrele(devvp);
258 return (error);
259 }
260
261 /*
262 * Make a mount point from a volume descriptor
263 */
264 static int
265 iso_makemp(isomp, bp, ea_len)
266 struct iso_mnt *isomp;
267 struct buf *bp;
268 int *ea_len;
269 {
270 struct iso_primary_descriptor *pri;
271 int logical_block_size;
272 struct iso_directory_record *rootp;
273
274 pri = (struct iso_primary_descriptor *)bp->b_data;
275
276 logical_block_size = isonum_723 (pri->logical_block_size);
277
278 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
279 || (logical_block_size & (logical_block_size - 1)) != 0)
280 return -1;
281
282 rootp = (struct iso_directory_record *)pri->root_directory_record;
283
284 isomp->logical_block_size = logical_block_size;
285 isomp->volume_space_size = isonum_733 (pri->volume_space_size);
286 memcpy(isomp->root, rootp, sizeof(isomp->root));
287 isomp->root_extent = isonum_733 (rootp->extent);
288 isomp->root_size = isonum_733 (rootp->size);
289 isomp->im_joliet_level = 0;
290
291 isomp->im_bmask = logical_block_size - 1;
292 isomp->im_bshift = 0;
293 while ((1 << isomp->im_bshift) < isomp->logical_block_size)
294 isomp->im_bshift++;
295
296 if (ea_len != NULL)
297 *ea_len = isonum_711(rootp->ext_attr_length);
298
299 return 0;
300 }
301
302 /*
303 * Common code for mount and mountroot
304 */
305 static int
306 iso_mountfs(devvp, mp, l, argp)
307 struct vnode *devvp;
308 struct mount *mp;
309 struct lwp *l;
310 struct iso_args *argp;
311 {
312 struct iso_mnt *isomp = (struct iso_mnt *)0;
313 struct buf *bp = NULL, *pribp = NULL, *supbp = NULL;
314 dev_t dev = devvp->v_rdev;
315 int error = EINVAL;
316 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
317 int iso_bsize;
318 int iso_blknum;
319 int joliet_level;
320 struct iso_volume_descriptor *vdp;
321 struct iso_supplementary_descriptor *sup;
322 int sess = 0;
323 int ext_attr_length;
324 struct disklabel label;
325
326 if (!ronly)
327 return EROFS;
328
329 /* Flush out any old buffers remaining from a previous use. */
330 if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
331 return (error);
332
333 /* This is the "logical sector size". The standard says this
334 * should be 2048 or the physical sector size on the device,
335 * whichever is greater. For now, we'll just use a constant.
336 */
337 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
338
339 error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED, l);
340 if (!error &&
341 label.d_partitions[DISKPART(dev)].p_fstype == FS_ISO9660) {
342 /* XXX more sanity checks? */
343 sess = label.d_partitions[DISKPART(dev)].p_cdsession;
344 } else {
345 /* fallback to old method */
346 error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED, l);
347 if (error)
348 sess = 0; /* never mind */
349 }
350 #ifdef ISO_DEBUG
351 printf("isofs: session offset (part %d) %d\n", DISKPART(dev), sess);
352 #endif
353
354 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
355 if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize),
356 iso_bsize, NOCRED, &bp)) != 0)
357 goto out;
358
359 vdp = (struct iso_volume_descriptor *)bp->b_data;
360 if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) {
361 error = EINVAL;
362 goto out;
363 }
364
365 switch (isonum_711(vdp->type)) {
366 case ISO_VD_PRIMARY:
367 if (pribp == NULL) {
368 pribp = bp;
369 bp = NULL;
370 }
371 break;
372
373 case ISO_VD_SUPPLEMENTARY:
374 if (supbp == NULL) {
375 supbp = bp;
376 bp = NULL;
377 }
378 break;
379
380 default:
381 break;
382 }
383
384 if (isonum_711 (vdp->type) == ISO_VD_END) {
385 brelse(bp);
386 bp = NULL;
387 break;
388 }
389
390 if (bp != NULL) {
391 brelse(bp);
392 bp = NULL;
393 }
394 }
395
396 if (pribp == NULL) {
397 error = EINVAL;
398 goto out;
399 }
400
401 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
402 memset(isomp, 0, sizeof *isomp);
403 if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) {
404 error = EINVAL;
405 goto out;
406 }
407
408 isomp->volume_space_size += sess;
409
410 pribp->b_flags |= B_AGE;
411 brelse(pribp);
412 pribp = NULL;
413
414 mp->mnt_data = isomp;
415 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
416 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
417 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
418 mp->mnt_stat.f_namemax = MAXNAMLEN;
419 mp->mnt_flag |= MNT_LOCAL;
420 mp->mnt_dev_bshift = iso_bsize;
421 mp->mnt_fs_bshift = isomp->im_bshift;
422 isomp->im_mountp = mp;
423 isomp->im_dev = dev;
424 isomp->im_devvp = devvp;
425
426 devvp->v_specmountpoint = mp;
427
428 /* Check the Rock Ridge Extension support */
429 if (!(argp->flags & ISOFSMNT_NORRIP)) {
430 struct iso_directory_record *rootp;
431
432 if ((error = bread(isomp->im_devvp,
433 (isomp->root_extent + ext_attr_length) <<
434 (isomp->im_bshift - DEV_BSHIFT),
435 isomp->logical_block_size, NOCRED,
436 &bp)) != 0)
437 goto out;
438
439 rootp = (struct iso_directory_record *)bp->b_data;
440
441 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
442 argp->flags |= ISOFSMNT_NORRIP;
443 } else {
444 argp->flags &= ~ISOFSMNT_GENS;
445 }
446
447 /*
448 * The contents are valid,
449 * but they will get reread as part of another vnode, so...
450 */
451 bp->b_flags |= B_AGE;
452 brelse(bp);
453 bp = NULL;
454 }
455 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
456 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
457
458 if (isomp->im_flags & ISOFSMNT_GENS)
459 isomp->iso_ftype = ISO_FTYPE_9660;
460 else if (isomp->im_flags & ISOFSMNT_NORRIP) {
461 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
462 if (argp->flags & ISOFSMNT_NOCASETRANS)
463 isomp->im_flags |= ISOFSMNT_NOCASETRANS;
464 } else
465 isomp->iso_ftype = ISO_FTYPE_RRIP;
466
467 /* Check the Joliet Extension support */
468 if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
469 (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
470 supbp != NULL) {
471 joliet_level = 0;
472 sup = (struct iso_supplementary_descriptor *)supbp->b_data;
473
474 if ((isonum_711(sup->flags) & 1) == 0) {
475 if (memcmp(sup->escape, "%/@", 3) == 0)
476 joliet_level = 1;
477 if (memcmp(sup->escape, "%/C", 3) == 0)
478 joliet_level = 2;
479 if (memcmp(sup->escape, "%/E", 3) == 0)
480 joliet_level = 3;
481 }
482 if (joliet_level != 0) {
483 if (iso_makemp(isomp, supbp, NULL) == -1) {
484 error = EINVAL;
485 goto out;
486 }
487 isomp->im_joliet_level = joliet_level;
488 }
489 }
490
491 if (supbp != NULL) {
492 brelse(supbp);
493 supbp = NULL;
494 }
495
496 return 0;
497 out:
498 if (bp)
499 brelse(bp);
500 if (pribp)
501 brelse(pribp);
502 if (supbp)
503 brelse(supbp);
504 if (isomp) {
505 free(isomp, M_ISOFSMNT);
506 mp->mnt_data = NULL;
507 }
508 return error;
509 }
510
511 /*
512 * Make a filesystem operational.
513 * Nothing to do at the moment.
514 */
515 /* ARGSUSED */
516 int
517 cd9660_start(struct mount *mp __unused, int flags __unused,
518 struct lwp *l __unused)
519 {
520 return 0;
521 }
522
523 /*
524 * unmount system call
525 */
526 int
527 cd9660_unmount(mp, mntflags, l)
528 struct mount *mp;
529 int mntflags;
530 struct lwp *l;
531 {
532 struct iso_mnt *isomp;
533 int error, flags = 0;
534
535 if (mntflags & MNT_FORCE)
536 flags |= FORCECLOSE;
537 #if 0
538 mntflushbuf(mp, 0);
539 if (mntinvalbuf(mp))
540 return EBUSY;
541 #endif
542 if ((error = vflush(mp, NULLVP, flags)) != 0)
543 return (error);
544
545 isomp = VFSTOISOFS(mp);
546
547 #ifdef ISODEVMAP
548 if (isomp->iso_ftype == ISO_FTYPE_RRIP)
549 iso_dunmap(isomp->im_dev);
550 #endif
551
552 if (isomp->im_devvp->v_type != VBAD)
553 isomp->im_devvp->v_specmountpoint = NULL;
554
555 vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
556 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, l);
557 vput(isomp->im_devvp);
558 free(isomp, M_ISOFSMNT);
559 mp->mnt_data = NULL;
560 mp->mnt_flag &= ~MNT_LOCAL;
561 return (error);
562 }
563
564 /*
565 * Return root of a filesystem
566 */
567 int
568 cd9660_root(mp, vpp)
569 struct mount *mp;
570 struct vnode **vpp;
571 {
572 struct iso_mnt *imp = VFSTOISOFS(mp);
573 struct iso_directory_record *dp =
574 (struct iso_directory_record *)imp->root;
575 ino_t ino = isodirino(dp, imp);
576
577 /*
578 * With RRIP we must use the `.' entry of the root directory.
579 * Simply tell vget, that it's a relocated directory.
580 */
581 return (cd9660_vget_internal(mp, ino, vpp,
582 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
583 }
584
585 /*
586 * Do operations associated with quotas, not supported
587 */
588 /* ARGSUSED */
589 int
590 cd9660_quotactl(
591 struct mount *mp __unused,
592 int cmd __unused,
593 uid_t uid __unused,
594 void *arg __unused,
595 struct lwp *l __unused)
596 {
597
598 return (EOPNOTSUPP);
599 }
600
601 /*
602 * Get file system statistics.
603 */
604 int
605 cd9660_statvfs(
606 struct mount *mp,
607 struct statvfs *sbp,
608 struct lwp *l __unused)
609 {
610 struct iso_mnt *isomp;
611
612 isomp = VFSTOISOFS(mp);
613
614 sbp->f_bsize = isomp->logical_block_size;
615 sbp->f_frsize = sbp->f_bsize;
616 sbp->f_iosize = sbp->f_bsize; /* XXX */
617 sbp->f_blocks = isomp->volume_space_size;
618 sbp->f_bfree = 0; /* total free blocks */
619 sbp->f_bavail = 0; /* blocks free for non superuser */
620 sbp->f_bresvd = 0; /* total reserved blocks */
621 sbp->f_files = 0; /* total files */
622 sbp->f_ffree = 0; /* free file nodes */
623 sbp->f_favail = 0; /* free file nodes for non superuser */
624 sbp->f_fresvd = 0; /* reserved file nodes */
625 copy_statvfs_info(sbp, mp);
626 /* Use the first spare for flags: */
627 sbp->f_spare[0] = isomp->im_flags;
628 return 0;
629 }
630
631 /* ARGSUSED */
632 int
633 cd9660_sync(
634 struct mount *mp __unused,
635 int waitfor __unused,
636 kauth_cred_t cred __unused,
637 struct lwp *l __unused)
638 {
639 return (0);
640 }
641
642 /*
643 * File handle to vnode
644 *
645 * Have to be really careful about stale file handles:
646 * - check that the inode number is in range
647 * - call iget() to get the locked inode
648 * - check for an unallocated inode (i_mode == 0)
649 * - check that the generation number matches
650 */
651
652 struct ifid {
653 ushort ifid_len;
654 ushort ifid_pad;
655 int ifid_ino;
656 long ifid_start;
657 };
658
659 /* ARGSUSED */
660 int
661 cd9660_fhtovp(mp, fhp, vpp)
662 struct mount *mp;
663 struct fid *fhp;
664 struct vnode **vpp;
665 {
666 struct ifid ifh;
667 struct iso_node *ip;
668 struct vnode *nvp;
669 int error;
670
671 if (fhp->fid_len != sizeof(ifh))
672 return EINVAL;
673
674 memcpy(&ifh, fhp, sizeof(ifh));
675 #ifdef ISOFS_DBG
676 printf("fhtovp: ino %d, start %ld\n",
677 ifh.ifid_ino, ifh.ifid_start);
678 #endif
679
680 if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
681 *vpp = NULLVP;
682 return (error);
683 }
684 ip = VTOI(nvp);
685 if (ip->inode.iso_mode == 0) {
686 vput(nvp);
687 *vpp = NULLVP;
688 return (ESTALE);
689 }
690 *vpp = nvp;
691 return (0);
692 }
693
694 int
695 cd9660_vget(mp, ino, vpp)
696 struct mount *mp;
697 ino_t ino;
698 struct vnode **vpp;
699 {
700
701 /*
702 * XXXX
703 * It would be nice if we didn't always set the `relocated' flag
704 * and force the extra read, but I don't want to think about fixing
705 * that right now.
706 */
707 return (cd9660_vget_internal(mp, ino, vpp,
708 #if 0
709 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
710 #else
711 0,
712 #endif
713 NULL));
714 }
715
716 int
717 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
718 struct mount *mp;
719 ino_t ino;
720 struct vnode **vpp;
721 int relocated;
722 struct iso_directory_record *isodir;
723 {
724 struct iso_mnt *imp;
725 struct iso_node *ip;
726 #ifdef ISODEVMAP
727 struct iso_dnode *dp;
728 #endif
729 struct buf *bp;
730 struct vnode *vp, *nvp;
731 dev_t dev;
732 int error;
733
734 imp = VFSTOISOFS(mp);
735 dev = imp->im_dev;
736 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
737 return (0);
738
739 /* Allocate a new vnode/iso_node. */
740 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
741 *vpp = NULLVP;
742 return (error);
743 }
744 ip = pool_get(&cd9660_node_pool, PR_WAITOK);
745 memset(ip, 0, sizeof(struct iso_node));
746 vp->v_data = ip;
747 ip->i_vnode = vp;
748 ip->i_dev = dev;
749 ip->i_number = ino;
750
751 /*
752 * Put it onto its hash chain and lock it so that other requests for
753 * this inode will block if they arrive while we are sleeping waiting
754 * for old data structures to be purged or for the contents of the
755 * disk portion of this inode to be read.
756 */
757 cd9660_ihashins(ip);
758
759 if (isodir == 0) {
760 int lbn, off;
761
762 lbn = lblkno(imp, ino);
763 if (lbn >= imp->volume_space_size) {
764 vput(vp);
765 printf("fhtovp: lbn exceed volume space %d\n", lbn);
766 return (ESTALE);
767 }
768
769 off = blkoff(imp, ino);
770 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
771 vput(vp);
772 printf("fhtovp: crosses block boundary %d\n",
773 off + ISO_DIRECTORY_RECORD_SIZE);
774 return (ESTALE);
775 }
776
777 error = bread(imp->im_devvp,
778 lbn << (imp->im_bshift - DEV_BSHIFT),
779 imp->logical_block_size, NOCRED, &bp);
780 if (error) {
781 vput(vp);
782 brelse(bp);
783 printf("fhtovp: bread error %d\n",error);
784 return (error);
785 }
786 isodir = (struct iso_directory_record *)(bp->b_data + off);
787
788 if (off + isonum_711(isodir->length) >
789 imp->logical_block_size) {
790 vput(vp);
791 if (bp != 0)
792 brelse(bp);
793 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
794 off +isonum_711(isodir->length), off,
795 isonum_711(isodir->length));
796 return (ESTALE);
797 }
798
799 #if 0
800 if (isonum_733(isodir->extent) +
801 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
802 if (bp != 0)
803 brelse(bp);
804 printf("fhtovp: file start miss %d vs %d\n",
805 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
806 ifhp->ifid_start);
807 return (ESTALE);
808 }
809 #endif
810 } else
811 bp = 0;
812
813 ip->i_mnt = imp;
814 ip->i_devvp = imp->im_devvp;
815 VREF(ip->i_devvp);
816
817 if (relocated) {
818 /*
819 * On relocated directories we must
820 * read the `.' entry out of a dir.
821 */
822 ip->iso_start = ino >> imp->im_bshift;
823 if (bp != 0)
824 brelse(bp);
825 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
826 vput(vp);
827 return (error);
828 }
829 isodir = (struct iso_directory_record *)bp->b_data;
830 }
831
832 ip->iso_extent = isonum_733(isodir->extent);
833 ip->i_size = isonum_733(isodir->size);
834 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
835
836 /*
837 * Setup time stamp, attribute
838 */
839 vp->v_type = VNON;
840 switch (imp->iso_ftype) {
841 default: /* ISO_FTYPE_9660 */
842 {
843 struct buf *bp2;
844 int off;
845 if ((imp->im_flags & ISOFSMNT_EXTATT)
846 && (off = isonum_711(isodir->ext_attr_length)))
847 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
848 NULL, &bp2);
849 else
850 bp2 = NULL;
851 cd9660_defattr(isodir, ip, bp2);
852 cd9660_deftstamp(isodir, ip, bp2);
853 if (bp2)
854 brelse(bp2);
855 break;
856 }
857 case ISO_FTYPE_RRIP:
858 cd9660_rrip_analyze(isodir, ip, imp);
859 break;
860 }
861
862 if (bp != 0)
863 brelse(bp);
864
865 /*
866 * Initialize the associated vnode
867 */
868 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
869 case VFIFO:
870 vp->v_op = cd9660_fifoop_p;
871 break;
872 case VCHR:
873 case VBLK:
874 /*
875 * if device, look at device number table for translation
876 */
877 #ifdef ISODEVMAP
878 if ((dp = iso_dmap(dev, ino, 0)) != NULL)
879 ip->inode.iso_rdev = dp->d_dev;
880 #endif
881 vp->v_op = cd9660_specop_p;
882 if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
883 /*
884 * Discard unneeded vnode, but save its iso_node.
885 * Note that the lock is carried over in the iso_node
886 * to the replacement vnode.
887 */
888 nvp->v_data = vp->v_data;
889 vp->v_data = NULL;
890 VOP_UNLOCK(vp, 0);
891 vp->v_op = spec_vnodeop_p;
892 vrele(vp);
893 vgone(vp);
894 lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
895 /*
896 * Reinitialize aliased inode.
897 */
898 vp = nvp;
899 ip->i_vnode = vp;
900 }
901 break;
902 case VLNK:
903 case VNON:
904 case VSOCK:
905 case VDIR:
906 case VBAD:
907 break;
908 case VREG:
909 uvm_vnp_setsize(vp, ip->i_size);
910 break;
911 }
912
913 if (ip->iso_extent == imp->root_extent)
914 vp->v_flag |= VROOT;
915
916 /*
917 * XXX need generation number?
918 */
919
920 genfs_node_init(vp, &cd9660_genfsops);
921 *vpp = vp;
922 return (0);
923 }
924
925 /*
926 * Vnode pointer to File handle
927 */
928 /* ARGSUSED */
929 int
930 cd9660_vptofh(vp, fhp, fh_size)
931 struct vnode *vp;
932 struct fid *fhp;
933 size_t *fh_size;
934 {
935 struct iso_node *ip = VTOI(vp);
936 struct ifid ifh;
937
938 if (*fh_size < sizeof(struct ifid)) {
939 *fh_size = sizeof(struct ifid);
940 return E2BIG;
941 }
942 *fh_size = sizeof(struct ifid);
943
944 memset(&ifh, 0, sizeof(ifh));
945 ifh.ifid_len = sizeof(struct ifid);
946 ifh.ifid_ino = ip->i_number;
947 ifh.ifid_start = ip->iso_start;
948 memcpy(fhp, &ifh, sizeof(ifh));
949
950 #ifdef ISOFS_DBG
951 printf("vptofh: ino %d, start %ld\n",
952 ifh.ifid_ino,ifh.ifid_start);
953 #endif
954 return 0;
955 }
956
957 SYSCTL_SETUP(sysctl_vfs_cd9660_setup, "sysctl vfs.cd9660 subtree setup")
958 {
959
960 sysctl_createv(clog, 0, NULL, NULL,
961 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
962 NULL, 0, NULL, 0,
963 CTL_VFS, CTL_EOL);
964 sysctl_createv(clog, 0, NULL, NULL,
965 CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
966 SYSCTL_DESCR("ISO-9660 file system"),
967 NULL, 0, NULL, 0,
968 CTL_VFS, 14, CTL_EOL);
969
970 sysctl_createv(clog, 0, NULL, NULL,
971 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
972 CTLTYPE_INT, "utf8_joliet",
973 SYSCTL_DESCR("Encode Joliet file names to UTF-8"),
974 NULL, 0, &cd9660_utf8_joliet, 0,
975 CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
976
977 }
978