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