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