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