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