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