Initial import from FreeBSD RELENG_4:
[games.git] / sys / vfs / isofs / cd9660 / cd9660_vfsops.c
1 /*-
2  * Copyright (c) 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)cd9660_vfsops.c     8.18 (Berkeley) 5/22/95
39  * $FreeBSD: src/sys/isofs/cd9660/cd9660_vfsops.c,v 1.74.2.7 2002/04/08 09:39:29 bde Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/cdio.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/stat.h>
55 #include <sys/syslog.h>
56
57 #include <vm/vm_zone.h>
58
59 #include <isofs/cd9660/iso.h>
60 #include <isofs/cd9660/iso_rrip.h>
61 #include <isofs/cd9660/cd9660_node.h>
62 #include <isofs/cd9660/cd9660_mount.h>
63
64 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
65 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
66
67 static int cd9660_mount __P((struct mount *,
68             char *, caddr_t, struct nameidata *, struct proc *));
69 static int cd9660_unmount __P((struct mount *, int, struct proc *));
70 static int cd9660_root __P((struct mount *, struct vnode **));
71 static int cd9660_statfs __P((struct mount *, struct statfs *, struct proc *));
72 static int cd9660_vget __P((struct mount *, ino_t, struct vnode **));
73 static int cd9660_fhtovp __P((struct mount *, struct fid *, struct vnode **));
74 static int cd9660_checkexp __P((struct mount *, struct sockaddr *,
75             int *, struct ucred **));
76 static int cd9660_vptofh __P((struct vnode *, struct fid *));
77
78 static struct vfsops cd9660_vfsops = {
79         cd9660_mount,
80         vfs_stdstart,
81         cd9660_unmount,
82         cd9660_root,
83         vfs_stdquotactl,
84         cd9660_statfs,
85         vfs_stdsync,
86         cd9660_vget,
87         cd9660_fhtovp,
88         cd9660_checkexp,
89         cd9660_vptofh,
90         cd9660_init,
91         cd9660_uninit,
92         vfs_stdextattrctl,
93 };
94 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
95 MODULE_VERSION(cd9660, 1);
96
97
98 /*
99  * Called by vfs_mountroot when iso is going to be mounted as root.
100  */
101
102 static int iso_get_ssector __P((dev_t dev, struct proc *p));
103 static int iso_mountfs __P((struct vnode *devvp, struct mount *mp,
104                             struct proc *p, struct iso_args *argp));
105
106 /*
107  * Try to find the start of the last data track on this CD-ROM.  This
108  * is used to mount the last session of a multi-session CD.  Bail out
109  * and return 0 if we fail, this is always a safe bet.
110  */
111 static int
112 iso_get_ssector(dev, p)
113         dev_t dev;
114         struct proc *p;
115 {
116         struct ioc_toc_header h;
117         struct ioc_read_toc_single_entry t;
118         int i;
119         struct cdevsw *bd;
120         d_ioctl_t *ioctlp;
121
122         bd = devsw(dev);
123         ioctlp = bd->d_ioctl;
124         if (ioctlp == NULL)
125                 return 0;
126
127         if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, p) != 0)
128                 return 0;
129
130         for (i = h.ending_track; i >= 0; i--) {
131                 t.address_format = CD_LBA_FORMAT;
132                 t.track = i;
133                 if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, p) != 0)
134                         return 0;
135                 if ((t.entry.control & 4) != 0)
136                         /* found a data track */
137                         break;
138         }
139
140         if (i < 0)
141                 return 0;
142
143         return ntohl(t.entry.addr.lba);
144 }
145
146 static int iso_mountroot __P((struct mount *mp, struct proc *p));
147
148 static int
149 iso_mountroot(mp, p)
150         struct mount *mp;
151         struct proc *p;
152 {
153         struct iso_args args;
154         int error;
155
156         if ((error = bdevvp(rootdev, &rootvp))) {
157                 printf("iso_mountroot: can't find rootvp\n");
158                 return (error);
159         }
160         args.flags = ISOFSMNT_ROOT;
161
162         vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, p);
163         error = VOP_OPEN(rootvp, FREAD, FSCRED, p);
164         VOP_UNLOCK(rootvp, 0, p);
165         if (error)
166                 return (error);
167
168         args.ssector = iso_get_ssector(rootdev, p);
169
170         (void)VOP_CLOSE(rootvp, FREAD, NOCRED, p);
171
172         if (bootverbose)
173                 printf("iso_mountroot(): using session at block %d\n",
174                        args.ssector);
175         if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0)
176                 return (error);
177
178         (void)cd9660_statfs(mp, &mp->mnt_stat, p);
179         return (0);
180 }
181
182 /*
183  * VFS Operations.
184  *
185  * mount system call
186  */
187 static int
188 cd9660_mount(mp, path, data, ndp, p)
189         register struct mount *mp;
190         char *path;
191         caddr_t data;
192         struct nameidata *ndp;
193         struct proc *p;
194 {
195         struct vnode *devvp;
196         struct iso_args args;
197         size_t size;
198         int error;
199         mode_t accessmode;
200         struct iso_mnt *imp = 0;
201
202         if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
203                 return (iso_mountroot(mp, p));
204         }
205         if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
206                 return (error);
207
208         if ((mp->mnt_flag & MNT_RDONLY) == 0)
209                 return (EROFS);
210
211         /*
212          * If updating, check whether changing from read-only to
213          * read/write; if there is no device name, that's all we do.
214          */
215         if (mp->mnt_flag & MNT_UPDATE) {
216                 imp = VFSTOISOFS(mp);
217                 if (args.fspec == 0)
218                         return (vfs_export(mp, &imp->im_export, &args.export));
219         }
220         /*
221          * Not an update, or updating the name: look up the name
222          * and verify that it refers to a sensible block device.
223          */
224         NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
225         if ((error = namei(ndp)))
226                 return (error);
227         NDFREE(ndp, NDF_ONLY_PNBUF);
228         devvp = ndp->ni_vp;
229
230         if (!vn_isdisk(devvp, &error)) {
231                 vrele(devvp);
232                 return (error);
233         }
234
235         /*       
236          * Verify that user has necessary permissions on the device,
237          * or has superuser abilities
238          */
239         accessmode = VREAD;
240         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
241         error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
242         if (error) 
243                 error = suser(p);
244         if (error) {
245                 vput(devvp);
246                 return (error);
247         }
248         VOP_UNLOCK(devvp, 0, p);
249
250         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
251                 error = iso_mountfs(devvp, mp, p, &args);
252         } else {
253                 if (devvp != imp->im_devvp)
254                         error = EINVAL; /* needs translation */
255                 else
256                         vrele(devvp);
257         }
258         if (error) {
259                 vrele(devvp);
260                 return error;
261         }
262         imp = VFSTOISOFS(mp);
263         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
264         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
265         (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
266             &size);
267         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
268         (void) cd9660_statfs(mp, &mp->mnt_stat, p);
269         return 0;
270 }
271
272 /*
273  * Common code for mount and mountroot
274  */
275 static int
276 iso_mountfs(devvp, mp, p, argp)
277         register struct vnode *devvp;
278         struct mount *mp;
279         struct proc *p;
280         struct iso_args *argp;
281 {
282         register struct iso_mnt *isomp = (struct iso_mnt *)0;
283         struct buf *bp = NULL;
284         struct buf *pribp = NULL, *supbp = NULL;
285         dev_t dev = devvp->v_rdev;
286         int error = EINVAL;
287         int needclose = 0;
288         int high_sierra = 0;
289         int iso_bsize;
290         int iso_blknum;
291         int joliet_level;
292         struct iso_volume_descriptor *vdp = 0;
293         struct iso_primary_descriptor *pri = NULL;
294         struct iso_sierra_primary_descriptor *pri_sierra = NULL;
295         struct iso_supplementary_descriptor *sup = NULL;
296         struct iso_directory_record *rootp;
297         int logical_block_size;
298
299         if (!(mp->mnt_flag & MNT_RDONLY))
300                 return EROFS;
301
302         /*
303          * Disallow multiple mounts of the same device.
304          * Disallow mounting of a device that is currently in use
305          * (except for root, which might share swap device for miniroot).
306          * Flush out any old buffers remaining from a previous use.
307          */
308         if ((error = vfs_mountedon(devvp)))
309                 return error;
310         if (vcount(devvp) > 1 && devvp != rootvp)
311                 return EBUSY;
312         if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)))
313                 return (error);
314
315         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
316         error = VOP_OPEN(devvp, FREAD, FSCRED, p);
317         VOP_UNLOCK(devvp, 0, p);
318         if (error)
319                 return error;
320         if (devvp->v_rdev->si_iosize_max != 0)
321                 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
322         if (mp->mnt_iosize_max > MAXPHYS)
323                 mp->mnt_iosize_max = MAXPHYS;
324
325         needclose = 1;
326
327         /* This is the "logical sector size".  The standard says this
328          * should be 2048 or the physical sector size on the device,
329          * whichever is greater.  For now, we'll just use a constant.
330          */
331         iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
332
333         joliet_level = 0;
334         for (iso_blknum = 16 + argp->ssector;
335              iso_blknum < 100 + argp->ssector;
336              iso_blknum++) {
337                 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
338                                   iso_bsize, NOCRED, &bp)) != 0)
339                         goto out;
340                 
341                 vdp = (struct iso_volume_descriptor *)bp->b_data;
342                 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
343                         if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
344                                   sizeof vdp->id) != 0) {
345                                 error = EINVAL;
346                                 goto out;
347                         } else
348                                 high_sierra = 1;
349                 }
350                 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
351                 case ISO_VD_PRIMARY:
352                         if (pribp == NULL) {
353                                 pribp = bp;
354                                 bp = NULL;
355                                 pri = (struct iso_primary_descriptor *)vdp;
356                                 pri_sierra =
357                                   (struct iso_sierra_primary_descriptor *)vdp;
358                         }
359                         break;
360
361                 case ISO_VD_SUPPLEMENTARY:
362                         if (supbp == NULL) {
363                                 supbp = bp;
364                                 bp = NULL;
365                                 sup = (struct iso_supplementary_descriptor *)vdp;
366
367                                 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
368                                         if (bcmp(sup->escape, "%/@", 3) == 0)
369                                                 joliet_level = 1;
370                                         if (bcmp(sup->escape, "%/C", 3) == 0)
371                                                 joliet_level = 2;
372                                         if (bcmp(sup->escape, "%/E", 3) == 0)
373                                                 joliet_level = 3;
374
375                                         if ((isonum_711 (sup->flags) & 1) &&
376                                             (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
377                                                 joliet_level = 0;
378                                 }
379                         }
380                         break;
381
382                 case ISO_VD_END:
383                         goto vd_end;
384
385                 default:
386                         break;
387                 }
388                 if (bp) {
389                         brelse(bp);
390                         bp = NULL;
391                 }
392         }
393  vd_end:
394         if (bp) {
395                 brelse(bp);
396                 bp = NULL;
397         }
398
399         if (pri == NULL) {
400                 error = EINVAL;
401                 goto out;
402         }
403
404         logical_block_size =
405                 isonum_723 (high_sierra?
406                             pri_sierra->logical_block_size:
407                             pri->logical_block_size);
408
409         if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
410             || (logical_block_size & (logical_block_size - 1)) != 0) {
411                 error = EINVAL;
412                 goto out;
413         }
414
415         rootp = (struct iso_directory_record *)
416                 (high_sierra?
417                  pri_sierra->root_directory_record:
418                  pri->root_directory_record);
419
420         isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
421         bzero((caddr_t)isomp, sizeof *isomp);
422         isomp->logical_block_size = logical_block_size;
423         isomp->volume_space_size =
424                 isonum_733 (high_sierra?
425                             pri_sierra->volume_space_size:
426                             pri->volume_space_size);
427         isomp->joliet_level = 0;
428         /*
429          * Since an ISO9660 multi-session CD can also access previous
430          * sessions, we have to include them into the space consider-
431          * ations.  This doesn't yield a very accurate number since
432          * parts of the old sessions might be inaccessible now, but we
433          * can't do much better.  This is also important for the NFS
434          * filehandle validation.
435          */
436         isomp->volume_space_size += argp->ssector;
437         bcopy (rootp, isomp->root, sizeof isomp->root);
438         isomp->root_extent = isonum_733 (rootp->extent);
439         isomp->root_size = isonum_733 (rootp->size);
440
441         isomp->im_bmask = logical_block_size - 1;
442         isomp->im_bshift = ffs(logical_block_size) - 1;
443
444         pribp->b_flags |= B_AGE;
445         brelse(pribp);
446         pribp = NULL;
447
448         mp->mnt_data = (qaddr_t)isomp;
449         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
450         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
451         mp->mnt_maxsymlinklen = 0;
452         mp->mnt_flag |= MNT_LOCAL;
453         isomp->im_mountp = mp;
454         isomp->im_dev = dev;
455         isomp->im_devvp = devvp;
456
457         devvp->v_specmountpoint = mp;
458
459         /* Check the Rock Ridge Extention support */
460         if (!(argp->flags & ISOFSMNT_NORRIP)) {
461                 if ((error = bread(isomp->im_devvp,
462                                   (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
463                                   (isomp->im_bshift - DEV_BSHIFT),
464                                   isomp->logical_block_size, NOCRED, &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                 bp->b_flags |= B_AGE;
480                 brelse(bp);
481                 bp = NULL;
482         }
483         isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
484                                          ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
485
486         if (high_sierra) {
487                 /* this effectively ignores all the mount flags */
488                 log(LOG_INFO, "cd9660: High Sierra Format\n");
489                 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
490         } else
491                 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
492                   default:
493                           isomp->iso_ftype = ISO_FTYPE_DEFAULT;
494                           break;
495                   case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
496                           isomp->iso_ftype = ISO_FTYPE_9660;
497                           break;
498                   case 0:
499                           log(LOG_INFO, "cd9660: RockRidge Extension\n");
500                           isomp->iso_ftype = ISO_FTYPE_RRIP;
501                           break;
502                 }
503
504         /* Decide whether to use the Joliet descriptor */
505
506         if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
507                 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
508                 rootp = (struct iso_directory_record *)
509                         sup->root_directory_record;
510                 bcopy (rootp, isomp->root, sizeof isomp->root);
511                 isomp->root_extent = isonum_733 (rootp->extent);
512                 isomp->root_size = isonum_733 (rootp->size);
513                 isomp->joliet_level = joliet_level;
514                 supbp->b_flags |= B_AGE;
515         }
516
517         if (supbp) {
518                 brelse(supbp);
519                 supbp = NULL;
520         }
521
522         return 0;
523 out:
524         devvp->v_specmountpoint = NULL;
525         if (bp)
526                 brelse(bp);
527         if (pribp)
528                 brelse(pribp);
529         if (supbp)
530                 brelse(supbp);
531         if (needclose)
532                 (void)VOP_CLOSE(devvp, FREAD, NOCRED, p);
533         if (isomp) {
534                 free((caddr_t)isomp, M_ISOFSMNT);
535                 mp->mnt_data = (qaddr_t)0;
536         }
537         return error;
538 }
539
540 /*
541  * unmount system call
542  */
543 static int
544 cd9660_unmount(mp, mntflags, p)
545         struct mount *mp;
546         int mntflags;
547         struct proc *p;
548 {
549         register struct iso_mnt *isomp;
550         int error, flags = 0;
551
552         if (mntflags & MNT_FORCE)
553                 flags |= FORCECLOSE;
554 #if 0
555         mntflushbuf(mp, 0);
556         if (mntinvalbuf(mp))
557                 return EBUSY;
558 #endif
559         if ((error = vflush(mp, 0, flags)))
560                 return (error);
561
562         isomp = VFSTOISOFS(mp);
563
564         isomp->im_devvp->v_specmountpoint = NULL;
565         error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
566         vrele(isomp->im_devvp);
567         free((caddr_t)isomp, M_ISOFSMNT);
568         mp->mnt_data = (qaddr_t)0;
569         mp->mnt_flag &= ~MNT_LOCAL;
570         return (error);
571 }
572
573 /*
574  * Return root of a filesystem
575  */
576 static int
577 cd9660_root(mp, vpp)
578         struct mount *mp;
579         struct vnode **vpp;
580 {
581         struct iso_mnt *imp = VFSTOISOFS(mp);
582         struct iso_directory_record *dp =
583             (struct iso_directory_record *)imp->root;
584         ino_t ino = isodirino(dp, imp);
585         
586         /*
587          * With RRIP we must use the `.' entry of the root directory.
588          * Simply tell vget, that it's a relocated directory.
589          */
590         return (cd9660_vget_internal(mp, ino, vpp,
591             imp->iso_ftype == ISO_FTYPE_RRIP, dp));
592 }
593
594 /*
595  * Get file system statistics.
596  */
597 int
598 cd9660_statfs(mp, sbp, p)
599         struct mount *mp;
600         register struct statfs *sbp;
601         struct proc *p;
602 {
603         register struct iso_mnt *isomp;
604
605         isomp = VFSTOISOFS(mp);
606
607         sbp->f_bsize = isomp->logical_block_size;
608         sbp->f_iosize = sbp->f_bsize;   /* XXX */
609         sbp->f_blocks = isomp->volume_space_size;
610         sbp->f_bfree = 0; /* total free blocks */
611         sbp->f_bavail = 0; /* blocks free for non superuser */
612         sbp->f_files =  0; /* total files */
613         sbp->f_ffree = 0; /* free file nodes */
614         if (sbp != &mp->mnt_stat) {
615                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
616                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
617                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
618         }
619         return 0;
620 }
621
622 /*
623  * File handle to vnode
624  *
625  * Have to be really careful about stale file handles:
626  * - check that the inode number is in range
627  * - call iget() to get the locked inode
628  * - check for an unallocated inode (i_mode == 0)
629  * - check that the generation number matches
630  */
631
632 struct ifid {
633         ushort  ifid_len;
634         ushort  ifid_pad;
635         int     ifid_ino;
636         long    ifid_start;
637 };
638
639 /* ARGSUSED */
640 int
641 cd9660_fhtovp(mp, fhp, vpp)
642         register struct mount *mp;
643         struct fid *fhp;
644         struct vnode **vpp;
645 {
646         struct ifid *ifhp = (struct ifid *)fhp;
647         register struct iso_node *ip;
648         struct vnode *nvp;
649         int error;
650         
651 #ifdef  ISOFS_DBG
652         printf("fhtovp: ino %d, start %ld\n",
653                ifhp->ifid_ino, ifhp->ifid_start);
654 #endif
655         
656         if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
657                 *vpp = NULLVP;
658                 return (error);
659         }
660         ip = VTOI(nvp);
661         if (ip->inode.iso_mode == 0) {
662                 vput(nvp);
663                 *vpp = NULLVP;
664                 return (ESTALE);
665         }
666         *vpp = nvp;
667         return (0);
668 }
669
670 int
671 cd9660_checkexp(mp, nam, exflagsp, credanonp)
672         struct mount *mp;
673         struct sockaddr *nam;
674         int *exflagsp;
675         struct ucred **credanonp;
676 {
677         register struct netcred *np;
678         register struct iso_mnt *imp;
679
680         imp = VFSTOISOFS(mp);   
681
682         /*
683          * Get the export permission structure for this <mp, client> tuple.
684          */
685         np = vfs_export_lookup(mp, &imp->im_export, nam);
686         if (np == NULL)
687                 return (EACCES);
688
689         *exflagsp = np->netc_exflags;
690         *credanonp = &np->netc_anon;
691         return (0);
692 }
693
694 int
695 cd9660_vget(mp, ino, vpp)
696         struct mount *mp;
697         ino_t ino;
698         struct vnode **vpp;
699 {
700
701         /*
702          * XXXX
703          * It would be nice if we didn't always set the `relocated' flag
704          * and force the extra read, but I don't want to think about fixing
705          * that right now.
706          */
707         return (cd9660_vget_internal(mp, ino, vpp,
708 #if 0
709             VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
710 #else
711             0,
712 #endif
713             (struct iso_directory_record *)0));
714 }
715
716 int
717 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
718         struct mount *mp;
719         ino_t ino;
720         struct vnode **vpp;
721         int relocated;
722         struct iso_directory_record *isodir;
723 {
724         struct iso_mnt *imp;
725         struct iso_node *ip;
726         struct buf *bp;
727         struct vnode *vp;
728         dev_t dev;
729         int error;
730
731         imp = VFSTOISOFS(mp);
732         dev = imp->im_dev;
733         if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
734                 return (0);
735
736         /* Allocate a new vnode/iso_node. */
737         if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
738                 *vpp = NULLVP;
739                 return (error);
740         }
741         MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
742             M_WAITOK);
743         bzero((caddr_t)ip, sizeof(struct iso_node));
744         lockinit(&ip->i_lock, PINOD, "isonode", 0, 0);
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 *)(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), NULL,
847                                      &bp2);
848                 else
849                         bp2 = NULL;
850                 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
851                 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
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                 vp->v_op = cd9660_specop_p;
874                 addaliasu(vp, ip->inode.iso_rdev);
875                 break;
876         default:
877                 break;
878         }
879         
880         if (ip->iso_extent == imp->root_extent)
881                 vp->v_flag |= VROOT;
882
883         /*
884          * XXX need generation number?
885          */
886         
887         *vpp = vp;
888         return (0);
889 }
890
891 /*
892  * Vnode pointer to File handle
893  */
894 /* ARGSUSED */
895 int
896 cd9660_vptofh(vp, fhp)
897         struct vnode *vp;
898         struct fid *fhp;
899 {
900         register struct iso_node *ip = VTOI(vp);
901         register struct ifid *ifhp;
902
903         ifhp = (struct ifid *)fhp;
904         ifhp->ifid_len = sizeof(struct ifid);
905
906         ifhp->ifid_ino = ip->i_number;
907         ifhp->ifid_start = ip->iso_start;
908
909 #ifdef  ISOFS_DBG
910         printf("vptofh: ino %d, start %ld\n",
911                ifhp->ifid_ino,ifhp->ifid_start);
912 #endif
913         return 0;
914 }