VFS messaging/interfacing work stage 8/99: Major reworking of the vnode
[dragonfly.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  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vfsops.c,v 1.22 2004/10/12 19:20:58 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/buf.h>
51 #include <sys/cdio.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
55 #include <sys/stat.h>
56 #include <sys/syslog.h>
57
58 #include <vm/vm_zone.h>
59
60 #include "iso.h"
61 #include "iso_rrip.h"
62 #include "cd9660_node.h"
63 #include "cd9660_mount.h"
64
65 extern struct vnodeopv_entry_desc cd9660_vnodeop_entries[];
66 extern struct vnodeopv_entry_desc cd9660_specop_entries[];
67 extern struct vnodeopv_entry_desc cd9660_fifoop_entries[];
68
69 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
70 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
71
72 static int cd9660_mount (struct mount *, char *, caddr_t, struct thread *);
73 static int cd9660_unmount (struct mount *, int, struct thread *);
74 static int cd9660_root (struct mount *, struct vnode **);
75 static int cd9660_statfs (struct mount *, struct statfs *, struct thread *);
76 static int cd9660_vget (struct mount *, ino_t, struct vnode **);
77 static int cd9660_fhtovp (struct mount *, struct fid *, struct vnode **);
78 static int cd9660_checkexp (struct mount *, struct sockaddr *,
79             int *, struct ucred **);
80 static int cd9660_vptofh (struct vnode *, struct fid *);
81
82 static struct vfsops cd9660_vfsops = {
83         cd9660_mount,
84         vfs_stdstart,
85         cd9660_unmount,
86         cd9660_root,
87         vfs_stdquotactl,
88         cd9660_statfs,
89         vfs_stdsync,
90         cd9660_vget,
91         cd9660_fhtovp,
92         cd9660_checkexp,
93         cd9660_vptofh,
94         cd9660_init,
95         cd9660_uninit,
96         vfs_stdextattrctl,
97 };
98 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
99 MODULE_VERSION(cd9660, 1);
100
101
102 /*
103  * Called by vfs_mountroot when iso is going to be mounted as root.
104  */
105
106 static int iso_get_ssector (dev_t dev, struct thread *td);
107 static int iso_mountfs (struct vnode *devvp, struct mount *mp,
108                             struct thread *td, struct iso_args *argp);
109
110 /*
111  * Try to find the start of the last data track on this CD-ROM.  This
112  * is used to mount the last session of a multi-session CD.  Bail out
113  * and return 0 if we fail, this is always a safe bet.
114  */
115 static int
116 iso_get_ssector(dev_t dev, struct thread *td)
117 {
118         struct ioc_toc_header h;
119         struct ioc_read_toc_single_entry t;
120         int i;
121
122         if (dev_dioctl(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
123                 return 0;
124
125         for (i = h.ending_track; i >= 0; i--) {
126                 t.address_format = CD_LBA_FORMAT;
127                 t.track = i;
128                 if (dev_dioctl(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0) {
129                         return 0;
130                 }
131                 if ((t.entry.control & 4) != 0)
132                         /* found a data track */
133                         break;
134         }
135
136         if (i < 0)
137                 return 0;
138
139         return ntohl(t.entry.addr.lba);
140 }
141
142 static int iso_mountroot (struct mount *mp, struct thread *td);
143
144 static int
145 iso_mountroot(struct mount *mp, struct thread *td)
146 {
147         struct iso_args args;
148         struct vnode *rootvp;
149         int error;
150
151         if ((error = bdevvp(rootdev, &rootvp))) {
152                 printf("iso_mountroot: can't find rootvp\n");
153                 return (error);
154         }
155         args.flags = ISOFSMNT_ROOT;
156
157         vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, td);
158         error = VOP_OPEN(rootvp, FREAD, FSCRED, td);
159         VOP_UNLOCK(rootvp, 0, td);
160         if (error)
161                 return (error);
162
163         args.ssector = iso_get_ssector(rootdev, td);
164
165         (void)VOP_CLOSE(rootvp, FREAD, td);
166
167         if (bootverbose)
168                 printf("iso_mountroot(): using session at block %d\n",
169                        args.ssector);
170         if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
171                 return (error);
172
173         (void)cd9660_statfs(mp, &mp->mnt_stat, td);
174         return (0);
175 }
176
177 /*
178  * VFS Operations.
179  *
180  * mount system call
181  */
182 static int
183 cd9660_mount(struct mount *mp, char *path, caddr_t data, struct thread *td)
184 {
185         struct vnode *devvp;
186         struct iso_args args;
187         size_t size;
188         int error;
189         mode_t accessmode;
190         struct iso_mnt *imp = 0;
191         struct nameidata nd;
192
193         if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
194                 return (iso_mountroot(mp, td));
195         }
196         if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
197                 return (error);
198
199         if ((mp->mnt_flag & MNT_RDONLY) == 0)
200                 return (EROFS);
201
202         KKASSERT(td->td_proc);
203
204         /*
205          * If updating, check whether changing from read-only to
206          * read/write; if there is no device name, that's all we do.
207          */
208         if (mp->mnt_flag & MNT_UPDATE) {
209                 imp = VFSTOISOFS(mp);
210                 if (args.fspec == 0)
211                         return (vfs_export(mp, &imp->im_export, &args.export));
212         }
213         /*
214          * Not an update, or updating the name: look up the name
215          * and verify that it refers to a sensible block device.
216          */
217         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
218         if ((error = namei(&nd)))
219                 return (error);
220         NDFREE(&nd, NDF_ONLY_PNBUF);
221         devvp = nd.ni_vp;
222
223         if (!vn_isdisk(devvp, &error)) {
224                 vrele(devvp);
225                 return (error);
226         }
227
228         /*       
229          * Verify that user has necessary permissions on the device,
230          * or has superuser abilities
231          */
232         accessmode = VREAD;
233         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
234         error = VOP_ACCESS(devvp, accessmode, td->td_proc->p_ucred, td);
235         if (error) 
236                 error = suser(td);
237         if (error) {
238                 vput(devvp);
239                 return (error);
240         }
241         VOP_UNLOCK(devvp, 0, td);
242
243         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
244                 error = iso_mountfs(devvp, mp, td, &args);
245         } else {
246                 if (devvp != imp->im_devvp)
247                         error = EINVAL; /* needs translation */
248                 else
249                         vrele(devvp);
250         }
251         if (error) {
252                 vrele(devvp);
253                 return error;
254         }
255         imp = VFSTOISOFS(mp);
256         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
257         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
258         (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
259             &size);
260         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
261         (void) cd9660_statfs(mp, &mp->mnt_stat, td);
262         return 0;
263 }
264
265 /*
266  * Common code for mount and mountroot
267  */
268 static int
269 iso_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
270             struct iso_args *argp)
271 {
272         struct iso_mnt *isomp = NULL;
273         struct buf *bp = NULL;
274         struct buf *pribp = NULL, *supbp = NULL;
275         dev_t dev;
276         int error = EINVAL;
277         int needclose = 0;
278         int high_sierra = 0;
279         int iso_bsize;
280         int iso_blknum;
281         int joliet_level;
282         struct iso_volume_descriptor *vdp = 0;
283         struct iso_primary_descriptor *pri = NULL;
284         struct iso_sierra_primary_descriptor *pri_sierra = NULL;
285         struct iso_supplementary_descriptor *sup = NULL;
286         struct iso_directory_record *rootp;
287         int logical_block_size;
288
289         if (!(mp->mnt_flag & MNT_RDONLY))
290                 return EROFS;
291
292         /*
293          * Disallow multiple mounts of the same device.
294          * Disallow mounting of a device that is currently in use
295          * Flush out any old buffers remaining from a previous use.
296          */
297         if ((error = vfs_mountedon(devvp)))
298                 return error;
299         if (count_udev(devvp->v_udev) > 0)
300                 return EBUSY;
301         if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
302                 return (error);
303
304         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
305         error = VOP_OPEN(devvp, FREAD, FSCRED, td);
306         VOP_UNLOCK(devvp, 0, td);
307         if (error)
308                 return error;
309         dev = devvp->v_rdev;
310         if (dev->si_iosize_max != 0)
311                 mp->mnt_iosize_max = dev->si_iosize_max;
312         if (mp->mnt_iosize_max > MAXPHYS)
313                 mp->mnt_iosize_max = MAXPHYS;
314
315         needclose = 1;
316
317         /* This is the "logical sector size".  The standard says this
318          * should be 2048 or the physical sector size on the device,
319          * whichever is greater.  For now, we'll just use a constant.
320          */
321         iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
322
323         joliet_level = 0;
324         for (iso_blknum = 16 + argp->ssector;
325              iso_blknum < 100 + argp->ssector;
326              iso_blknum++) {
327                 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
328                                   iso_bsize, &bp)) != 0)
329                         goto out;
330                 
331                 vdp = (struct iso_volume_descriptor *)bp->b_data;
332                 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
333                         if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
334                                   sizeof vdp->id) != 0) {
335                                 error = EINVAL;
336                                 goto out;
337                         } else
338                                 high_sierra = 1;
339                 }
340                 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
341                 case ISO_VD_PRIMARY:
342                         if (pribp == NULL) {
343                                 pribp = bp;
344                                 bp = NULL;
345                                 pri = (struct iso_primary_descriptor *)vdp;
346                                 pri_sierra =
347                                   (struct iso_sierra_primary_descriptor *)vdp;
348                         }
349                         break;
350
351                 case ISO_VD_SUPPLEMENTARY:
352                         if (supbp == NULL) {
353                                 supbp = bp;
354                                 bp = NULL;
355                                 sup = (struct iso_supplementary_descriptor *)vdp;
356
357                                 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
358                                         if (bcmp(sup->escape, "%/@", 3) == 0)
359                                                 joliet_level = 1;
360                                         if (bcmp(sup->escape, "%/C", 3) == 0)
361                                                 joliet_level = 2;
362                                         if (bcmp(sup->escape, "%/E", 3) == 0)
363                                                 joliet_level = 3;
364
365                                         if ((isonum_711 (sup->flags) & 1) &&
366                                             (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
367                                                 joliet_level = 0;
368                                 }
369                         }
370                         break;
371
372                 case ISO_VD_END:
373                         goto vd_end;
374
375                 default:
376                         break;
377                 }
378                 if (bp) {
379                         brelse(bp);
380                         bp = NULL;
381                 }
382         }
383  vd_end:
384         if (bp) {
385                 brelse(bp);
386                 bp = NULL;
387         }
388
389         if (pri == NULL) {
390                 error = EINVAL;
391                 goto out;
392         }
393
394         logical_block_size =
395                 isonum_723 (high_sierra?
396                             pri_sierra->logical_block_size:
397                             pri->logical_block_size);
398
399         if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
400             || (logical_block_size & (logical_block_size - 1)) != 0) {
401                 error = EINVAL;
402                 goto out;
403         }
404
405         rootp = (struct iso_directory_record *)
406                 (high_sierra?
407                  pri_sierra->root_directory_record:
408                  pri->root_directory_record);
409
410         isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
411         isomp->logical_block_size = logical_block_size;
412         isomp->volume_space_size =
413                 isonum_733 (high_sierra?
414                             pri_sierra->volume_space_size:
415                             pri->volume_space_size);
416         isomp->joliet_level = 0;
417         /*
418          * Since an ISO9660 multi-session CD can also access previous
419          * sessions, we have to include them into the space consider-
420          * ations.  This doesn't yield a very accurate number since
421          * parts of the old sessions might be inaccessible now, but we
422          * can't do much better.  This is also important for the NFS
423          * filehandle validation.
424          */
425         isomp->volume_space_size += argp->ssector;
426         bcopy (rootp, isomp->root, sizeof isomp->root);
427         isomp->root_extent = isonum_733 (rootp->extent);
428         isomp->root_size = isonum_733 (rootp->size);
429
430         isomp->im_bmask = logical_block_size - 1;
431         isomp->im_bshift = ffs(logical_block_size) - 1;
432
433         pribp->b_flags |= B_AGE;
434         brelse(pribp);
435         pribp = NULL;
436
437         mp->mnt_data = (qaddr_t)isomp;
438         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
439         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
440         mp->mnt_maxsymlinklen = 0;
441         mp->mnt_flag |= MNT_LOCAL;
442         isomp->im_mountp = mp;
443         isomp->im_dev = dev;
444         isomp->im_devvp = devvp;
445
446         dev->si_mountpoint = mp;
447
448         /* Check the Rock Ridge Extention support */
449         if (!(argp->flags & ISOFSMNT_NORRIP)) {
450                 if ((error = bread(isomp->im_devvp,
451                                   (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
452                                   (isomp->im_bshift - DEV_BSHIFT),
453                                   isomp->logical_block_size, &bp)) != 0)
454                     goto out;
455                 
456                 rootp = (struct iso_directory_record *)bp->b_data;
457                 
458                 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
459                     argp->flags  |= ISOFSMNT_NORRIP;
460                 } else {
461                     argp->flags  &= ~ISOFSMNT_GENS;
462                 }
463
464                 /*
465                  * The contents are valid,
466                  * but they will get reread as part of another vnode, so...
467                  */
468                 bp->b_flags |= B_AGE;
469                 brelse(bp);
470                 bp = NULL;
471         }
472         isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
473                                          ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
474
475         if (high_sierra) {
476                 /* this effectively ignores all the mount flags */
477                 log(LOG_INFO, "cd9660: High Sierra Format\n");
478                 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
479         } else {
480                 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
481                   default:
482                           isomp->iso_ftype = ISO_FTYPE_DEFAULT;
483                           break;
484                   case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
485                           isomp->iso_ftype = ISO_FTYPE_9660;
486                           break;
487                   case 0:
488                           log(LOG_INFO, "cd9660: RockRidge Extension\n");
489                           isomp->iso_ftype = ISO_FTYPE_RRIP;
490                           break;
491                 }
492         }
493
494         /* Decide whether to use the Joliet descriptor */
495
496         if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
497                 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
498                 rootp = (struct iso_directory_record *)
499                         sup->root_directory_record;
500                 bcopy (rootp, isomp->root, sizeof isomp->root);
501                 isomp->root_extent = isonum_733 (rootp->extent);
502                 isomp->root_size = isonum_733 (rootp->size);
503                 isomp->joliet_level = joliet_level;
504                 supbp->b_flags |= B_AGE;
505         }
506
507         if (supbp) {
508                 brelse(supbp);
509                 supbp = NULL;
510         }
511
512         vfs_add_vnodeops(&mp->mnt_vn_ops, cd9660_vnodeop_entries);
513         vfs_add_vnodeops(&mp->mnt_vn_spec_ops, cd9660_specop_entries);
514         vfs_add_vnodeops(&mp->mnt_vn_fifo_ops, cd9660_fifoop_entries);
515
516         return 0;
517 out:
518         dev->si_mountpoint = NULL;
519         if (bp)
520                 brelse(bp);
521         if (pribp)
522                 brelse(pribp);
523         if (supbp)
524                 brelse(supbp);
525         if (needclose)
526                 (void)VOP_CLOSE(devvp, FREAD, td);
527         if (isomp) {
528                 free((caddr_t)isomp, M_ISOFSMNT);
529                 mp->mnt_data = (qaddr_t)0;
530         }
531         return error;
532 }
533
534 /*
535  * unmount system call
536  */
537 static int
538 cd9660_unmount(struct mount *mp, int mntflags, struct thread *td)
539 {
540         struct iso_mnt *isomp;
541         int error, flags = 0;
542
543         if (mntflags & MNT_FORCE)
544                 flags |= FORCECLOSE;
545 #if 0
546         mntflushbuf(mp, 0);
547         if (mntinvalbuf(mp))
548                 return EBUSY;
549 #endif
550         if ((error = vflush(mp, 0, flags)))
551                 return (error);
552
553         isomp = VFSTOISOFS(mp);
554
555         isomp->im_devvp->v_rdev->si_mountpoint = NULL;
556         error = VOP_CLOSE(isomp->im_devvp, FREAD, td);
557         vrele(isomp->im_devvp);
558         free((caddr_t)isomp, M_ISOFSMNT);
559         mp->mnt_data = (qaddr_t)0;
560         mp->mnt_flag &= ~MNT_LOCAL;
561         return (error);
562 }
563
564 /*
565  * Return root of a filesystem
566  */
567 static int
568 cd9660_root(struct mount *mp, struct vnode **vpp)
569 {
570         struct iso_mnt *imp = VFSTOISOFS(mp);
571         struct iso_directory_record *dp =
572             (struct iso_directory_record *)imp->root;
573         ino_t ino = isodirino(dp, imp);
574         
575         /*
576          * With RRIP we must use the `.' entry of the root directory.
577          * Simply tell vget, that it's a relocated directory.
578          */
579         return (cd9660_vget_internal(mp, ino, vpp,
580             imp->iso_ftype == ISO_FTYPE_RRIP, dp));
581 }
582
583 /*
584  * Get file system statistics.
585  */
586 int
587 cd9660_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
588 {
589         struct iso_mnt *isomp;
590
591         isomp = VFSTOISOFS(mp);
592
593         sbp->f_bsize = isomp->logical_block_size;
594         sbp->f_iosize = sbp->f_bsize;   /* XXX */
595         sbp->f_blocks = isomp->volume_space_size;
596         sbp->f_bfree = 0; /* total free blocks */
597         sbp->f_bavail = 0; /* blocks free for non superuser */
598         sbp->f_files =  0; /* total files */
599         sbp->f_ffree = 0; /* free file nodes */
600         if (sbp != &mp->mnt_stat) {
601                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
602                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
603                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
604         }
605         return 0;
606 }
607
608 /*
609  * File handle to vnode
610  *
611  * Have to be really careful about stale file handles:
612  * - check that the inode number is in range
613  * - call iget() to get the locked inode
614  * - check for an unallocated inode (i_mode == 0)
615  * - check that the generation number matches
616  */
617
618 struct ifid {
619         ushort  ifid_len;
620         ushort  ifid_pad;
621         int     ifid_ino;
622         long    ifid_start;
623 };
624
625 /* ARGSUSED */
626 int
627 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
628 {
629         struct ifid *ifhp = (struct ifid *)fhp;
630         struct iso_node *ip;
631         struct vnode *nvp;
632         int error;
633         
634 #ifdef  ISOFS_DBG
635         printf("fhtovp: ino %d, start %ld\n",
636                ifhp->ifid_ino, ifhp->ifid_start);
637 #endif
638         
639         if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
640                 *vpp = NULLVP;
641                 return (error);
642         }
643         ip = VTOI(nvp);
644         if (ip->inode.iso_mode == 0) {
645                 vput(nvp);
646                 *vpp = NULLVP;
647                 return (ESTALE);
648         }
649         *vpp = nvp;
650         return (0);
651 }
652
653 int
654 cd9660_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
655                 struct ucred **credanonp)
656 {
657         struct netcred *np;
658         struct iso_mnt *imp;
659
660         imp = VFSTOISOFS(mp);   
661
662         /*
663          * Get the export permission structure for this <mp, client> tuple.
664          */
665         np = vfs_export_lookup(mp, &imp->im_export, nam);
666         if (np == NULL)
667                 return (EACCES);
668
669         *exflagsp = np->netc_exflags;
670         *credanonp = &np->netc_anon;
671         return (0);
672 }
673
674 int
675 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
676 {
677
678         /*
679          * XXXX
680          * It would be nice if we didn't always set the `relocated' flag
681          * and force the extra read, but I don't want to think about fixing
682          * that right now.
683          */
684         return (cd9660_vget_internal(mp, ino, vpp,
685 #if 0
686             VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
687 #else
688             0,
689 #endif
690             (struct iso_directory_record *)0));
691 }
692
693 int
694 cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
695                      int relocated, struct iso_directory_record *isodir)
696 {
697         /*struct thread *td = curthread;*/
698         struct iso_mnt *imp;
699         struct iso_node *ip;
700         struct buf *bp;
701         struct vnode *vp;
702         dev_t dev;
703         int error;
704
705         imp = VFSTOISOFS(mp);
706         dev = imp->im_dev;
707 again:
708         if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
709                 return (0);
710
711         /* Allocate a new vnode/iso_node. */
712         error = getnewvnode(VT_ISOFS, mp, mp->mnt_vn_ops, &vp, 0, 0);
713         if (error) {
714                 *vpp = NULLVP;
715                 return (error);
716         }
717         MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
718             M_WAITOK);
719         bzero((caddr_t)ip, sizeof(struct iso_node));
720         ip->i_vnode = vp;
721         ip->i_dev = dev;
722         ip->i_number = ino;
723
724         /*
725          * Insert it into the inode hash table and check for a collision.
726          * If a collision occurs, throw away the vnode and try again.
727          */
728         if (cd9660_ihashins(ip) != 0) {
729                 printf("debug: cd9660 ihashins collision, retrying\n");
730                 vx_put(vp);
731                 free(ip, M_ISOFSNODE);
732                 goto again;
733         }
734         vp->v_data = ip;
735
736         if (isodir == 0) {
737                 int lbn, off;
738
739                 lbn = lblkno(imp, ino);
740                 if (lbn >= imp->volume_space_size) {
741                         vx_put(vp);
742                         printf("fhtovp: lbn exceed volume space %d\n", lbn);
743                         return (ESTALE);
744                 }
745         
746                 off = blkoff(imp, ino);
747                 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
748                         vx_put(vp);
749                         printf("fhtovp: crosses block boundary %d\n",
750                                off + ISO_DIRECTORY_RECORD_SIZE);
751                         return (ESTALE);
752                 }
753         
754                 error = bread(imp->im_devvp,
755                               lbn << (imp->im_bshift - DEV_BSHIFT),
756                               imp->logical_block_size, &bp);
757                 if (error) {
758                         vx_put(vp);
759                         brelse(bp);
760                         printf("fhtovp: bread error %d\n",error);
761                         return (error);
762                 }
763                 isodir = (struct iso_directory_record *)(bp->b_data + off);
764
765                 if (off + isonum_711(isodir->length) >
766                     imp->logical_block_size) {
767                         vx_put(vp);
768                         if (bp != 0)
769                                 brelse(bp);
770                         printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
771                                off +isonum_711(isodir->length), off,
772                                isonum_711(isodir->length));
773                         return (ESTALE);
774                 }
775         
776 #if 0
777                 if (isonum_733(isodir->extent) +
778                     isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
779                         if (bp != 0)
780                                 brelse(bp);
781                         printf("fhtovp: file start miss %d vs %d\n",
782                                isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
783                                ifhp->ifid_start);
784                         return (ESTALE);
785                 }
786 #endif
787         } else
788                 bp = 0;
789
790         ip->i_mnt = imp;
791         ip->i_devvp = imp->im_devvp;
792         vref(ip->i_devvp);
793
794         if (relocated) {
795                 /*
796                  * On relocated directories we must
797                  * read the `.' entry out of a dir.
798                  */
799                 ip->iso_start = ino >> imp->im_bshift;
800                 if (bp != 0)
801                         brelse(bp);
802                 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
803                         vx_put(vp);
804                         return (error);
805                 }
806                 isodir = (struct iso_directory_record *)bp->b_data;
807         }
808
809         ip->iso_extent = isonum_733(isodir->extent);
810         ip->i_size = isonum_733(isodir->size);
811         ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
812         
813         /*
814          * Setup time stamp, attribute
815          */
816         vp->v_type = VNON;
817         switch (imp->iso_ftype) {
818         default:        /* ISO_FTYPE_9660 */
819             {
820                 struct buf *bp2;
821                 int off;
822                 if ((imp->im_flags & ISOFSMNT_EXTATT)
823                     && (off = isonum_711(isodir->ext_attr_length)))
824                         cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
825                                      &bp2);
826                 else
827                         bp2 = NULL;
828                 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
829                 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
830                 if (bp2)
831                         brelse(bp2);
832                 break;
833             }
834         case ISO_FTYPE_RRIP:
835                 cd9660_rrip_analyze(isodir, ip, imp);
836                 break;
837         }
838
839         if (bp != 0)
840                 brelse(bp);
841
842         /*
843          * Initialize the associated vnode
844          */
845         switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
846         case VFIFO:
847                 vp->v_ops = mp->mnt_vn_fifo_ops;
848                 break;
849         case VCHR:
850         case VBLK:
851                 vp->v_ops = mp->mnt_vn_spec_ops;
852                 addaliasu(vp, ip->inode.iso_rdev);
853                 break;
854         default:
855                 break;
856         }
857         
858         if (ip->iso_extent == imp->root_extent)
859                 vp->v_flag |= VROOT;
860
861         /*
862          * Return the locked and refd vp
863          */
864         *vpp = vp;
865         return (0);
866 }
867
868 /*
869  * Vnode pointer to File handle
870  */
871 /* ARGSUSED */
872 int
873 cd9660_vptofh(struct vnode *vp, struct fid *fhp)
874 {
875         struct iso_node *ip = VTOI(vp);
876         struct ifid *ifhp;
877
878         ifhp = (struct ifid *)fhp;
879         ifhp->ifid_len = sizeof(struct ifid);
880
881         ifhp->ifid_ino = ip->i_number;
882         ifhp->ifid_start = ip->iso_start;
883
884 #ifdef  ISOFS_DBG
885         printf("vptofh: ino %d, start %ld\n",
886                ifhp->ifid_ino,ifhp->ifid_start);
887 #endif
888         return 0;
889 }