cd9660_vfsops.c revision 1.40.2.10 1 /* $NetBSD: cd9660_vfsops.c,v 1.40.2.10 2007/10/24 16:23:21 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace (at) blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai (at) spec.co.jp).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_vfsops.c,v 1.40.2.10 2007/10/24 16:23:21 ad Exp $");
41
42 #if defined(_KERNEL_OPT)
43 #include "opt_compat_netbsd.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <miscfs/specfs/specdev.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/file.h>
57 #include <sys/disklabel.h>
58 #include <sys/device.h>
59 #include <sys/ioctl.h>
60 #include <sys/cdio.h>
61 #include <sys/errno.h>
62 #include <sys/malloc.h>
63 #include <sys/pool.h>
64 #include <sys/stat.h>
65 #include <sys/conf.h>
66 #include <sys/dirent.h>
67 #include <sys/kauth.h>
68
69 #include <fs/cd9660/iso.h>
70 #include <fs/cd9660/cd9660_extern.h>
71 #include <fs/cd9660/iso_rrip.h>
72 #include <fs/cd9660/cd9660_node.h>
73 #include <fs/cd9660/cd9660_mount.h>
74
75 MALLOC_JUSTDEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
76
77 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc;
78 extern const struct vnodeopv_desc cd9660_specop_opv_desc;
79 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc;
80
81 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = {
82 &cd9660_vnodeop_opv_desc,
83 &cd9660_specop_opv_desc,
84 &cd9660_fifoop_opv_desc,
85 NULL,
86 };
87
88 struct vfsops cd9660_vfsops = {
89 MOUNT_CD9660,
90 sizeof (struct iso_args),
91 cd9660_mount,
92 cd9660_start,
93 cd9660_unmount,
94 cd9660_root,
95 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 vfs_destroy(mp);
151 return (error);
152 }
153 mutex_enter(&mountlist_lock);
154 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
155 mutex_exit(&mountlist_lock);
156 (void)cd9660_statvfs(mp, &mp->mnt_stat, 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, l)
168 struct mount *mp;
169 const char *path;
170 void *data;
171 size_t *data_len;
172 struct lwp *l;
173 {
174 struct nameidata nd;
175 struct vnode *devvp;
176 struct iso_args *args = data;
177 int error;
178 struct iso_mnt *imp = VFSTOISOFS(mp);
179
180 if (*data_len < sizeof *args)
181 return EINVAL;
182
183 if (mp->mnt_flag & MNT_GETARGS) {
184 if (imp == NULL)
185 return EIO;
186 args->fspec = NULL;
187 args->flags = imp->im_flags;
188 *data_len = sizeof (*args);
189 return 0;
190 }
191
192 if ((mp->mnt_flag & MNT_RDONLY) == 0)
193 return (EROFS);
194
195 if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
196 return EINVAL;
197
198 /*
199 * Not an update, or updating the name: look up the name
200 * and verify that it refers to a sensible block device.
201 */
202 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec, l);
203 if ((error = namei(&nd)) != 0)
204 return (error);
205 devvp = nd.ni_vp;
206
207 if (devvp->v_type != VBLK) {
208 vrele(devvp);
209 return ENOTBLK;
210 }
211 if (bdevsw_lookup(devvp->v_rdev) == NULL) {
212 vrele(devvp);
213 return ENXIO;
214 }
215 /*
216 * If mount by non-root, then verify that user has necessary
217 * permissions on the device.
218 */
219 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
220 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
221 error = VOP_ACCESS(devvp, VREAD, l->l_cred, 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, 0);
390 bp = NULL;
391 break;
392 }
393
394 if (bp != NULL) {
395 brelse(bp, 0);
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 brelse(pribp, BC_AGE);
415 pribp = NULL;
416
417 mp->mnt_data = isomp;
418 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
419 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
420 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
421 mp->mnt_stat.f_namemax = MAXNAMLEN;
422 mp->mnt_flag |= MNT_LOCAL;
423 mp->mnt_iflag |= IMNT_MPSAFE;
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 brelse(bp, BC_AGE);
454 bp = NULL;
455 }
456 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
457 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
458
459 if (isomp->im_flags & ISOFSMNT_GENS)
460 isomp->iso_ftype = ISO_FTYPE_9660;
461 else if (isomp->im_flags & ISOFSMNT_NORRIP) {
462 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
463 if (argp->flags & ISOFSMNT_NOCASETRANS)
464 isomp->im_flags |= ISOFSMNT_NOCASETRANS;
465 } else
466 isomp->iso_ftype = ISO_FTYPE_RRIP;
467
468 /* Check the Joliet Extension support */
469 if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
470 (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
471 supbp != NULL) {
472 joliet_level = 0;
473 sup = (struct iso_supplementary_descriptor *)supbp->b_data;
474
475 if ((isonum_711(sup->flags) & 1) == 0) {
476 if (memcmp(sup->escape, "%/@", 3) == 0)
477 joliet_level = 1;
478 if (memcmp(sup->escape, "%/C", 3) == 0)
479 joliet_level = 2;
480 if (memcmp(sup->escape, "%/E", 3) == 0)
481 joliet_level = 3;
482 }
483 if (joliet_level != 0) {
484 if (iso_makemp(isomp, supbp, NULL) == -1) {
485 error = EINVAL;
486 goto out;
487 }
488 isomp->im_joliet_level = joliet_level;
489 }
490 }
491
492 if (supbp != NULL) {
493 brelse(supbp, 0);
494 supbp = NULL;
495 }
496
497 devvp->v_specmountpoint = mp;
498
499 return 0;
500 out:
501 if (bp)
502 brelse(bp, 0);
503 if (pribp)
504 brelse(pribp, 0);
505 if (supbp)
506 brelse(supbp, 0);
507 if (isomp) {
508 free(isomp, M_ISOFSMNT);
509 mp->mnt_data = NULL;
510 }
511 return error;
512 }
513
514 /*
515 * Make a filesystem operational.
516 * Nothing to do at the moment.
517 */
518 /* ARGSUSED */
519 int
520 cd9660_start(struct mount *mp, int flags,
521 struct lwp *l)
522 {
523 return 0;
524 }
525
526 /*
527 * unmount system call
528 */
529 int
530 cd9660_unmount(mp, mntflags, l)
531 struct mount *mp;
532 int mntflags;
533 struct lwp *l;
534 {
535 struct iso_mnt *isomp;
536 int error, flags = 0;
537
538 if (mntflags & MNT_FORCE)
539 flags |= FORCECLOSE;
540 if ((error = vflush(mp, NULLVP, flags)) != 0)
541 return (error);
542
543 isomp = VFSTOISOFS(mp);
544
545 if (isomp->im_devvp->v_type != VBAD)
546 isomp->im_devvp->v_specmountpoint = NULL;
547
548 vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
549 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, l);
550 vput(isomp->im_devvp);
551 free(isomp, M_ISOFSMNT);
552 mp->mnt_data = NULL;
553 mp->mnt_flag &= ~MNT_LOCAL;
554 return (error);
555 }
556
557 /*
558 * Return root of a filesystem
559 */
560 int
561 cd9660_root(mp, vpp)
562 struct mount *mp;
563 struct vnode **vpp;
564 {
565 struct iso_mnt *imp = VFSTOISOFS(mp);
566 struct iso_directory_record *dp =
567 (struct iso_directory_record *)imp->root;
568 ino_t ino = isodirino(dp, imp);
569
570 /*
571 * With RRIP we must use the `.' entry of the root directory.
572 * Simply tell vget, that it's a relocated directory.
573 */
574 return (cd9660_vget_internal(mp, ino, vpp,
575 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
576 }
577
578 /*
579 * Do operations associated with quotas, not supported
580 */
581 /* ARGSUSED */
582 int
583 cd9660_quotactl(
584 struct mount *mp,
585 int cmd,
586 uid_t uid,
587 void *arg,
588 struct lwp *l)
589 {
590
591 return (EOPNOTSUPP);
592 }
593
594 /*
595 * Get file system statistics.
596 */
597 int
598 cd9660_statvfs(
599 struct mount *mp,
600 struct statvfs *sbp,
601 struct lwp *l)
602 {
603 struct iso_mnt *isomp;
604
605 isomp = VFSTOISOFS(mp);
606
607 sbp->f_bsize = isomp->logical_block_size;
608 sbp->f_frsize = sbp->f_bsize;
609 sbp->f_iosize = sbp->f_bsize; /* XXX */
610 sbp->f_blocks = isomp->volume_space_size;
611 sbp->f_bfree = 0; /* total free blocks */
612 sbp->f_bavail = 0; /* blocks free for non superuser */
613 sbp->f_bresvd = 0; /* total reserved blocks */
614 sbp->f_files = 0; /* total files */
615 sbp->f_ffree = 0; /* free file nodes */
616 sbp->f_favail = 0; /* free file nodes for non superuser */
617 sbp->f_fresvd = 0; /* reserved file nodes */
618 copy_statvfs_info(sbp, mp);
619 /* Use the first spare for flags: */
620 sbp->f_spare[0] = isomp->im_flags;
621 return 0;
622 }
623
624 /* ARGSUSED */
625 int
626 cd9660_sync(
627 struct mount *mp,
628 int waitfor,
629 kauth_cred_t cred,
630 struct lwp *l)
631 {
632 return (0);
633 }
634
635 /*
636 * File handle to vnode
637 *
638 * Have to be really careful about stale file handles:
639 * - check that the inode number is in range
640 * - call iget() to get the locked inode
641 * - check for an unallocated inode (i_mode == 0)
642 * - check that the generation number matches
643 */
644
645 struct ifid {
646 ushort ifid_len;
647 ushort ifid_pad;
648 int ifid_ino;
649 long ifid_start;
650 };
651
652 /* ARGSUSED */
653 int
654 cd9660_fhtovp(mp, fhp, vpp)
655 struct mount *mp;
656 struct fid *fhp;
657 struct vnode **vpp;
658 {
659 struct ifid ifh;
660 struct iso_node *ip;
661 struct vnode *nvp;
662 int error;
663
664 if (fhp->fid_len != sizeof(ifh))
665 return EINVAL;
666
667 memcpy(&ifh, fhp, sizeof(ifh));
668 #ifdef ISOFS_DBG
669 printf("fhtovp: ino %d, start %ld\n",
670 ifh.ifid_ino, ifh.ifid_start);
671 #endif
672
673 if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
674 *vpp = NULLVP;
675 return (error);
676 }
677 ip = VTOI(nvp);
678 if (ip->inode.iso_mode == 0) {
679 vput(nvp);
680 *vpp = NULLVP;
681 return (ESTALE);
682 }
683 *vpp = nvp;
684 return (0);
685 }
686
687 int
688 cd9660_vget(mp, ino, vpp)
689 struct mount *mp;
690 ino_t ino;
691 struct vnode **vpp;
692 {
693
694 /*
695 * XXXX
696 * It would be nice if we didn't always set the `relocated' flag
697 * and force the extra read, but I don't want to think about fixing
698 * that right now.
699 */
700 return (cd9660_vget_internal(mp, ino, vpp,
701 #if 0
702 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
703 #else
704 0,
705 #endif
706 NULL));
707 }
708
709 int
710 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
711 struct mount *mp;
712 ino_t ino;
713 struct vnode **vpp;
714 int relocated;
715 struct iso_directory_record *isodir;
716 {
717 struct iso_mnt *imp;
718 struct iso_node *ip;
719 struct buf *bp;
720 struct vnode *vp, *nvp;
721 dev_t dev;
722 int error;
723
724 imp = VFSTOISOFS(mp);
725 dev = imp->im_dev;
726
727 retry:
728 if ((*vpp = cd9660_ihashget(dev, ino, LK_EXCLUSIVE)) != NULLVP)
729 return (0);
730
731 /* Allocate a new vnode/iso_node. */
732 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
733 *vpp = NULLVP;
734 return (error);
735 }
736 ip = pool_get(&cd9660_node_pool, PR_WAITOK);
737
738 /*
739 * If someone beat us to it, put back the freshly allocated
740 * vnode/inode pair and retry.
741 */
742 mutex_enter(&cd9660_hashlock);
743 if (cd9660_ihashget(dev, ino, 0) != NULL) {
744 mutex_exit(&cd9660_hashlock);
745 ungetnewvnode(vp);
746 pool_put(&cd9660_node_pool, ip);
747 goto retry;
748 }
749
750 memset(ip, 0, sizeof(struct iso_node));
751 vp->v_data = ip;
752 ip->i_vnode = vp;
753 ip->i_dev = dev;
754 ip->i_number = ino;
755 ip->i_mnt = imp;
756 ip->i_devvp = imp->im_devvp;
757 genfs_node_init(vp, &cd9660_genfsops);
758
759 /*
760 * Put it onto its hash chain and lock it so that other requests for
761 * this inode will block if they arrive while we are sleeping waiting
762 * for old data structures to be purged or for the contents of the
763 * disk portion of this inode to be read.
764 */
765 cd9660_ihashins(ip);
766 mutex_exit(&cd9660_hashlock);
767
768 if (isodir == 0) {
769 int lbn, off;
770
771 lbn = lblkno(imp, ino);
772 if (lbn >= imp->volume_space_size) {
773 vput(vp);
774 printf("fhtovp: lbn exceed volume space %d\n", lbn);
775 return (ESTALE);
776 }
777
778 off = blkoff(imp, ino);
779 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
780 vput(vp);
781 printf("fhtovp: crosses block boundary %d\n",
782 off + ISO_DIRECTORY_RECORD_SIZE);
783 return (ESTALE);
784 }
785
786 error = bread(imp->im_devvp,
787 lbn << (imp->im_bshift - DEV_BSHIFT),
788 imp->logical_block_size, NOCRED, &bp);
789 if (error) {
790 vput(vp);
791 brelse(bp, 0);
792 printf("fhtovp: bread error %d\n",error);
793 return (error);
794 }
795 isodir = (struct iso_directory_record *)((char *)bp->b_data + off);
796
797 if (off + isonum_711(isodir->length) >
798 imp->logical_block_size) {
799 vput(vp);
800 if (bp != 0)
801 brelse(bp, 0);
802 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
803 off +isonum_711(isodir->length), off,
804 isonum_711(isodir->length));
805 return (ESTALE);
806 }
807
808 #if 0
809 if (isonum_733(isodir->extent) +
810 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
811 if (bp != 0)
812 brelse(bp, 0);
813 printf("fhtovp: file start miss %d vs %d\n",
814 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
815 ifhp->ifid_start);
816 return (ESTALE);
817 }
818 #endif
819 } else
820 bp = 0;
821
822 VREF(ip->i_devvp);
823
824 if (relocated) {
825 /*
826 * On relocated directories we must
827 * read the `.' entry out of a dir.
828 */
829 ip->iso_start = ino >> imp->im_bshift;
830 if (bp != 0)
831 brelse(bp, 0);
832 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
833 vput(vp);
834 return (error);
835 }
836 isodir = (struct iso_directory_record *)bp->b_data;
837 }
838
839 ip->iso_extent = isonum_733(isodir->extent);
840 ip->i_size = isonum_733(isodir->size);
841 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
842
843 /*
844 * Setup time stamp, attribute
845 */
846 vp->v_type = VNON;
847 switch (imp->iso_ftype) {
848 default: /* ISO_FTYPE_9660 */
849 {
850 struct buf *bp2;
851 int off;
852 if ((imp->im_flags & ISOFSMNT_EXTATT)
853 && (off = isonum_711(isodir->ext_attr_length)))
854 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
855 NULL, &bp2);
856 else
857 bp2 = NULL;
858 cd9660_defattr(isodir, ip, bp2);
859 cd9660_deftstamp(isodir, ip, bp2);
860 if (bp2)
861 brelse(bp2, 0);
862 break;
863 }
864 case ISO_FTYPE_RRIP:
865 cd9660_rrip_analyze(isodir, ip, imp);
866 break;
867 }
868
869 if (bp != 0)
870 brelse(bp, 0);
871
872 /*
873 * Initialize the associated vnode
874 */
875 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
876 case VFIFO:
877 vp->v_op = cd9660_fifoop_p;
878 break;
879 case VCHR:
880 case VBLK:
881 /*
882 * if device, look at device number table for translation
883 */
884 vp->v_op = cd9660_specop_p;
885 if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
886 /*
887 * Discard unneeded vnode, but save its iso_node.
888 * Note that the lock is carried over in the iso_node
889 * to the replacement vnode.
890 */
891 nvp->v_data = vp->v_data;
892 vp->v_data = NULL;
893 VOP_UNLOCK(vp, 0);
894 vp->v_op = spec_vnodeop_p;
895 vgone(vp);
896 lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
897 /*
898 * Reinitialize aliased inode.
899 */
900 vp = nvp;
901 ip->i_vnode = vp;
902 }
903 break;
904 case VLNK:
905 case VNON:
906 case VSOCK:
907 case VDIR:
908 case VBAD:
909 break;
910 case VREG:
911 uvm_vnp_setsize(vp, ip->i_size);
912 break;
913 }
914
915 if (vp->v_type != VREG)
916 uvm_vnp_setsize(vp, 0);
917
918 if (ip->iso_extent == imp->root_extent)
919 vp->v_vflag |= VV_ROOT;
920
921 /*
922 * XXX need generation number?
923 */
924
925 *vpp = vp;
926 return (0);
927 }
928
929 /*
930 * Vnode pointer to File handle
931 */
932 /* ARGSUSED */
933 int
934 cd9660_vptofh(vp, fhp, fh_size)
935 struct vnode *vp;
936 struct fid *fhp;
937 size_t *fh_size;
938 {
939 struct iso_node *ip = VTOI(vp);
940 struct ifid ifh;
941
942 if (*fh_size < sizeof(struct ifid)) {
943 *fh_size = sizeof(struct ifid);
944 return E2BIG;
945 }
946 *fh_size = sizeof(struct ifid);
947
948 memset(&ifh, 0, sizeof(ifh));
949 ifh.ifid_len = sizeof(struct ifid);
950 ifh.ifid_ino = ip->i_number;
951 ifh.ifid_start = ip->iso_start;
952 memcpy(fhp, &ifh, sizeof(ifh));
953
954 #ifdef ISOFS_DBG
955 printf("vptofh: ino %d, start %ld\n",
956 ifh.ifid_ino,ifh.ifid_start);
957 #endif
958 return 0;
959 }
960
961 SYSCTL_SETUP(sysctl_vfs_cd9660_setup, "sysctl vfs.cd9660 subtree setup")
962 {
963
964 sysctl_createv(clog, 0, NULL, NULL,
965 CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
966 NULL, 0, NULL, 0,
967 CTL_VFS, CTL_EOL);
968 sysctl_createv(clog, 0, NULL, NULL,
969 CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
970 SYSCTL_DESCR("ISO-9660 file system"),
971 NULL, 0, NULL, 0,
972 CTL_VFS, 14, CTL_EOL);
973
974 sysctl_createv(clog, 0, NULL, NULL,
975 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
976 CTLTYPE_INT, "utf8_joliet",
977 SYSCTL_DESCR("Encode Joliet file names to UTF-8"),
978 NULL, 0, &cd9660_utf8_joliet, 0,
979 CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
980
981 }
982