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