]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/cd9660/cd9660_vfsops.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / fs / 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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)cd9660_vfsops.c     8.18 (Berkeley) 5/22/95
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/cdio.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/stat.h>
55 #include <sys/syslog.h>
56 #include <sys/iconv.h>
57
58 #include <fs/cd9660/iso.h>
59 #include <fs/cd9660/iso_rrip.h>
60 #include <fs/cd9660/cd9660_node.h>
61 #include <fs/cd9660/cd9660_mount.h>
62
63 #include <geom/geom.h>
64 #include <geom/geom_vfs.h>
65
66 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure");
67 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part");
68
69 struct iconv_functions *cd9660_iconv = NULL;
70
71 static vfs_mount_t      cd9660_mount;
72 static vfs_cmount_t     cd9660_cmount;
73 static vfs_unmount_t    cd9660_unmount;
74 static vfs_root_t       cd9660_root;
75 static vfs_statfs_t     cd9660_statfs;
76 static vfs_vget_t       cd9660_vget;
77 static vfs_fhtovp_t     cd9660_fhtovp;
78
79 static struct vfsops cd9660_vfsops = {
80         .vfs_fhtovp =           cd9660_fhtovp,
81         .vfs_mount =            cd9660_mount,
82         .vfs_cmount =           cd9660_cmount,
83         .vfs_root =             cd9660_root,
84         .vfs_statfs =           cd9660_statfs,
85         .vfs_unmount =          cd9660_unmount,
86         .vfs_vget =             cd9660_vget,
87 };
88 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
89 MODULE_VERSION(cd9660, 1);
90
91 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
92                        struct thread *td);
93
94 /*
95  * VFS Operations.
96  */
97
98 static int
99 cd9660_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
100 {
101         struct iso_args args;
102         int error;
103
104         error = copyin(data, &args, sizeof args);
105         if (error)
106                 return (error);
107
108         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
109         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
110         ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
111         ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
112         ma = mount_argf(ma, "ssector", "%u", args.ssector);
113         ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
114         ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
115         ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
116         ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
117         ma = mount_argb(ma,
118             args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
119         ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
120
121         error = kernel_mount(ma, flags);
122
123         return (error);
124 }
125
126 static int
127 cd9660_mount(struct mount *mp, struct thread *td)
128 {
129         struct vnode *devvp;
130         char *fspec;
131         int error;
132         mode_t accessmode;
133         struct nameidata ndp;
134         struct iso_mnt *imp = 0;
135
136         /*
137          * Unconditionally mount as read-only.
138          */
139         MNT_ILOCK(mp);
140         mp->mnt_flag |= MNT_RDONLY;
141         MNT_IUNLOCK(mp);
142
143         fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
144         if (error)
145                 return (error);
146
147         imp = VFSTOISOFS(mp);
148
149         if (mp->mnt_flag & MNT_UPDATE) {
150                 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
151                         return (0);
152         }
153         /*
154          * Not an update, or updating the name: look up the name
155          * and verify that it refers to a sensible block device.
156          */
157         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
158         if ((error = namei(&ndp)))
159                 return (error);
160         NDFREE(&ndp, NDF_ONLY_PNBUF);
161         devvp = ndp.ni_vp;
162
163         if (!vn_isdisk(devvp, &error)) {
164                 vput(devvp);
165                 return (error);
166         }
167
168         /*
169          * Verify that user has necessary permissions on the device,
170          * or has superuser abilities
171          */
172         accessmode = VREAD;
173         error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
174         if (error)
175                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
176         if (error) {
177                 vput(devvp);
178                 return (error);
179         }
180
181         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
182                 error = iso_mountfs(devvp, mp, td);
183                 if (error)
184                         vrele(devvp);
185         } else {
186                 if (devvp != imp->im_devvp)
187                         error = EINVAL; /* needs translation */
188                 vput(devvp);
189         }
190         if (error)
191                 return (error);
192         vfs_mountedfrom(mp, fspec);
193         return (0);
194 }
195
196 /*
197  * Common code for mount and mountroot
198  */
199 static int
200 iso_mountfs(devvp, mp, td)
201         struct vnode *devvp;
202         struct mount *mp;
203         struct thread *td;
204 {
205         struct iso_mnt *isomp = (struct iso_mnt *)0;
206         struct buf *bp = NULL;
207         struct buf *pribp = NULL, *supbp = NULL;
208         struct cdev *dev;
209         int error = EINVAL;
210         int high_sierra = 0;
211         int iso_bsize;
212         int iso_blknum;
213         int joliet_level;
214         struct iso_volume_descriptor *vdp = 0;
215         struct iso_primary_descriptor *pri = NULL;
216         struct iso_sierra_primary_descriptor *pri_sierra = NULL;
217         struct iso_supplementary_descriptor *sup = NULL;
218         struct iso_directory_record *rootp;
219         int logical_block_size, ssector;
220         struct g_consumer *cp;
221         struct bufobj *bo;
222         char *cs_local, *cs_disk;
223
224         dev = devvp->v_rdev;
225         dev_ref(dev);
226         DROP_GIANT();
227         g_topology_lock();
228         error = g_vfs_open(devvp, &cp, "cd9660", 0);
229         g_topology_unlock();
230         PICKUP_GIANT();
231         VOP_UNLOCK(devvp, 0, td);
232         if (error)
233                 goto out;
234         if (devvp->v_rdev->si_iosize_max != 0)
235                 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
236         if (mp->mnt_iosize_max > MAXPHYS)
237                 mp->mnt_iosize_max = MAXPHYS;
238
239         bo = &devvp->v_bufobj;
240
241         /* This is the "logical sector size".  The standard says this
242          * should be 2048 or the physical sector size on the device,
243          * whichever is greater.
244          */
245         if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
246                 error = EINVAL;
247                 goto out;
248         }
249
250         iso_bsize = cp->provider->sectorsize;
251
252         joliet_level = 0;
253         if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
254                 ssector = 0;
255         for (iso_blknum = 16 + ssector;
256              iso_blknum < 100 + ssector;
257              iso_blknum++) {
258                 if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
259                                   iso_bsize, NOCRED, &bp)) != 0)
260                         goto out;
261
262                 vdp = (struct iso_volume_descriptor *)bp->b_data;
263                 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
264                         if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
265                                   sizeof vdp->id) != 0) {
266                                 error = EINVAL;
267                                 goto out;
268                         } else
269                                 high_sierra = 1;
270                 }
271                 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
272                 case ISO_VD_PRIMARY:
273                         if (pribp == NULL) {
274                                 pribp = bp;
275                                 bp = NULL;
276                                 pri = (struct iso_primary_descriptor *)vdp;
277                                 pri_sierra =
278                                   (struct iso_sierra_primary_descriptor *)vdp;
279                         }
280                         break;
281
282                 case ISO_VD_SUPPLEMENTARY:
283                         if (supbp == NULL) {
284                                 supbp = bp;
285                                 bp = NULL;
286                                 sup = (struct iso_supplementary_descriptor *)vdp;
287
288                                 if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
289                                         if (bcmp(sup->escape, "%/@", 3) == 0)
290                                                 joliet_level = 1;
291                                         if (bcmp(sup->escape, "%/C", 3) == 0)
292                                                 joliet_level = 2;
293                                         if (bcmp(sup->escape, "%/E", 3) == 0)
294                                                 joliet_level = 3;
295
296                                         if ((isonum_711 (sup->flags) & 1) &&
297                                             !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
298                                                 joliet_level = 0;
299                                 }
300                         }
301                         break;
302
303                 case ISO_VD_END:
304                         goto vd_end;
305
306                 default:
307                         break;
308                 }
309                 if (bp) {
310                         brelse(bp);
311                         bp = NULL;
312                 }
313         }
314  vd_end:
315         if (bp) {
316                 brelse(bp);
317                 bp = NULL;
318         }
319
320         if (pri == NULL) {
321                 error = EINVAL;
322                 goto out;
323         }
324
325         logical_block_size =
326                 isonum_723 (high_sierra?
327                             pri_sierra->logical_block_size:
328                             pri->logical_block_size);
329
330         if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
331             || (logical_block_size & (logical_block_size - 1)) != 0) {
332                 error = EINVAL;
333                 goto out;
334         }
335
336         rootp = (struct iso_directory_record *)
337                 (high_sierra?
338                  pri_sierra->root_directory_record:
339                  pri->root_directory_record);
340
341         isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
342         isomp->im_cp = cp;
343         isomp->im_bo = bo;
344         isomp->logical_block_size = logical_block_size;
345         isomp->volume_space_size =
346                 isonum_733 (high_sierra?
347                             pri_sierra->volume_space_size:
348                             pri->volume_space_size);
349         isomp->joliet_level = 0;
350         /*
351          * Since an ISO9660 multi-session CD can also access previous
352          * sessions, we have to include them into the space consider-
353          * ations.  This doesn't yield a very accurate number since
354          * parts of the old sessions might be inaccessible now, but we
355          * can't do much better.  This is also important for the NFS
356          * filehandle validation.
357          */
358         isomp->volume_space_size += ssector;
359         bcopy (rootp, isomp->root, sizeof isomp->root);
360         isomp->root_extent = isonum_733 (rootp->extent);
361         isomp->root_size = isonum_733 (rootp->size);
362
363         isomp->im_bmask = logical_block_size - 1;
364         isomp->im_bshift = ffs(logical_block_size) - 1;
365
366         pribp->b_flags |= B_AGE;
367         brelse(pribp);
368         pribp = NULL;
369
370         mp->mnt_data = (qaddr_t)isomp;
371         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
372         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
373         mp->mnt_maxsymlinklen = 0;
374         MNT_ILOCK(mp);
375         mp->mnt_flag |= MNT_LOCAL;
376         mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED;
377         MNT_IUNLOCK(mp);
378         isomp->im_mountp = mp;
379         isomp->im_dev = dev;
380         isomp->im_devvp = devvp;
381
382         vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
383         vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
384         vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
385         vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
386         vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
387
388         /* Check the Rock Ridge Extension support */
389         if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
390                 if ((error = bread(isomp->im_devvp,
391                                   (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
392                                   (isomp->im_bshift - DEV_BSHIFT),
393                                   isomp->logical_block_size, NOCRED, &bp)) != 0)
394                     goto out;
395
396                 rootp = (struct iso_directory_record *)bp->b_data;
397
398                 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
399                     isomp->im_flags |= ISOFSMNT_NORRIP;
400                 } else {
401                     isomp->im_flags &= ~ISOFSMNT_GENS;
402                 }
403
404                 /*
405                  * The contents are valid,
406                  * but they will get reread as part of another vnode, so...
407                  */
408                 bp->b_flags |= B_AGE;
409                 brelse(bp);
410                 bp = NULL;
411         }
412
413         if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
414                 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
415                 if (error)
416                         goto out;
417                 cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
418                 if (error)
419                         goto out;
420                 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
421                 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
422         } else {
423                 isomp->im_d2l = NULL;
424                 isomp->im_l2d = NULL;
425         }
426
427         if (high_sierra) {
428                 /* this effectively ignores all the mount flags */
429                 if (bootverbose)
430                         log(LOG_INFO, "cd9660: High Sierra Format\n");
431                 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
432         } else
433                 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
434                   default:
435                           isomp->iso_ftype = ISO_FTYPE_DEFAULT;
436                           break;
437                   case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
438                           isomp->iso_ftype = ISO_FTYPE_9660;
439                           break;
440                   case 0:
441                           if (bootverbose)
442                                   log(LOG_INFO, "cd9660: RockRidge Extension\n");
443                           isomp->iso_ftype = ISO_FTYPE_RRIP;
444                           break;
445                 }
446
447         /* Decide whether to use the Joliet descriptor */
448
449         if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
450                 if (bootverbose)
451                         log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
452                             joliet_level);
453                 rootp = (struct iso_directory_record *)
454                         sup->root_directory_record;
455                 bcopy (rootp, isomp->root, sizeof isomp->root);
456                 isomp->root_extent = isonum_733 (rootp->extent);
457                 isomp->root_size = isonum_733 (rootp->size);
458                 isomp->joliet_level = joliet_level;
459                 supbp->b_flags |= B_AGE;
460         }
461
462         if (supbp) {
463                 brelse(supbp);
464                 supbp = NULL;
465         }
466
467         return 0;
468 out:
469         if (bp)
470                 brelse(bp);
471         if (pribp)
472                 brelse(pribp);
473         if (supbp)
474                 brelse(supbp);
475         if (cp != NULL) {
476                 DROP_GIANT();
477                 g_topology_lock();
478                 g_vfs_close(cp, td);
479                 g_topology_unlock();
480                 PICKUP_GIANT();
481         }
482         if (isomp) {
483                 free((caddr_t)isomp, M_ISOFSMNT);
484                 mp->mnt_data = (qaddr_t)0;
485         }
486         dev_rel(dev);
487         return error;
488 }
489
490 /*
491  * unmount system call
492  */
493 static int
494 cd9660_unmount(mp, mntflags, td)
495         struct mount *mp;
496         int mntflags;
497         struct thread *td;
498 {
499         struct iso_mnt *isomp;
500         int error, flags = 0;
501
502         if (mntflags & MNT_FORCE)
503                 flags |= FORCECLOSE;
504 #if 0
505         mntflushbuf(mp, 0);
506         if (mntinvalbuf(mp))
507                 return EBUSY;
508 #endif
509         if ((error = vflush(mp, 0, flags, td)))
510                 return (error);
511
512         isomp = VFSTOISOFS(mp);
513
514         if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
515                 if (isomp->im_d2l)
516                         cd9660_iconv->close(isomp->im_d2l);
517                 if (isomp->im_l2d)
518                         cd9660_iconv->close(isomp->im_l2d);
519         }
520         DROP_GIANT();
521         g_topology_lock();
522         g_vfs_close(isomp->im_cp, td);
523         g_topology_unlock();
524         PICKUP_GIANT();
525         vrele(isomp->im_devvp);
526         dev_rel(isomp->im_dev);
527         free((caddr_t)isomp, M_ISOFSMNT);
528         mp->mnt_data = (qaddr_t)0;
529         MNT_ILOCK(mp);
530         mp->mnt_flag &= ~MNT_LOCAL;
531         MNT_IUNLOCK(mp);
532         return (error);
533 }
534
535 /*
536  * Return root of a filesystem
537  */
538 static int
539 cd9660_root(mp, flags, vpp, td)
540         struct mount *mp;
541         int flags;
542         struct vnode **vpp;
543         struct thread *td;
544 {
545         struct iso_mnt *imp = VFSTOISOFS(mp);
546         struct iso_directory_record *dp =
547             (struct iso_directory_record *)imp->root;
548         ino_t ino = isodirino(dp, imp);
549
550         /*
551          * With RRIP we must use the `.' entry of the root directory.
552          * Simply tell vget, that it's a relocated directory.
553          */
554         return (cd9660_vget_internal(mp, ino, flags, vpp,
555             imp->iso_ftype == ISO_FTYPE_RRIP, dp));
556 }
557
558 /*
559  * Get filesystem statistics.
560  */
561 static int
562 cd9660_statfs(mp, sbp, td)
563         struct mount *mp;
564         struct statfs *sbp;
565         struct thread *td;
566 {
567         struct iso_mnt *isomp;
568
569         isomp = VFSTOISOFS(mp);
570
571         sbp->f_bsize = isomp->logical_block_size;
572         sbp->f_iosize = sbp->f_bsize;   /* XXX */
573         sbp->f_blocks = isomp->volume_space_size;
574         sbp->f_bfree = 0; /* total free blocks */
575         sbp->f_bavail = 0; /* blocks free for non superuser */
576         sbp->f_files =  0; /* total files */
577         sbp->f_ffree = 0; /* free file nodes */
578         return 0;
579 }
580
581 /*
582  * File handle to vnode
583  *
584  * Have to be really careful about stale file handles:
585  * - check that the inode number is in range
586  * - call iget() to get the locked inode
587  * - check for an unallocated inode (i_mode == 0)
588  * - check that the generation number matches
589  */
590
591 /* ARGSUSED */
592 static int
593 cd9660_fhtovp(mp, fhp, vpp)
594         struct mount *mp;
595         struct fid *fhp;
596         struct vnode **vpp;
597 {
598         struct ifid *ifhp = (struct ifid *)fhp;
599         struct iso_node *ip;
600         struct vnode *nvp;
601         int error;
602
603 #ifdef  ISOFS_DBG
604         printf("fhtovp: ino %d, start %ld\n",
605                ifhp->ifid_ino, ifhp->ifid_start);
606 #endif
607
608         if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
609                 *vpp = NULLVP;
610                 return (error);
611         }
612         ip = VTOI(nvp);
613         if (ip->inode.iso_mode == 0) {
614                 vput(nvp);
615                 *vpp = NULLVP;
616                 return (ESTALE);
617         }
618         *vpp = nvp;
619         vnode_create_vobject(*vpp, ip->i_size, curthread);
620         return (0);
621 }
622
623 static int
624 cd9660_vget(mp, ino, flags, vpp)
625         struct mount *mp;
626         ino_t ino;
627         int flags;
628         struct vnode **vpp;
629 {
630
631         /*
632          * XXXX
633          * It would be nice if we didn't always set the `relocated' flag
634          * and force the extra read, but I don't want to think about fixing
635          * that right now.
636          */
637         return (cd9660_vget_internal(mp, ino, flags, vpp,
638 #if 0
639             VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
640 #else
641             0,
642 #endif
643             (struct iso_directory_record *)0));
644 }
645
646 int
647 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
648         struct mount *mp;
649         ino_t ino;
650         int flags;
651         struct vnode **vpp;
652         int relocated;
653         struct iso_directory_record *isodir;
654 {
655         struct iso_mnt *imp;
656         struct iso_node *ip;
657         struct buf *bp;
658         struct vnode *vp;
659         struct cdev *dev;
660         int error;
661         struct thread *td;
662
663         td = curthread;
664         error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
665         if (error || *vpp != NULL)
666                 return (error);
667
668         /*
669          * We must promote to an exclusive lock for vnode creation.  This
670          * can happen if lookup is passed LOCKSHARED.
671          */
672         if ((flags & LK_TYPE_MASK) == LK_SHARED) {
673                 flags &= ~LK_TYPE_MASK;
674                 flags |= LK_EXCLUSIVE;
675         }
676
677         /*
678          * We do not lock vnode creation as it is believed to be too
679          * expensive for such rare case as simultaneous creation of vnode
680          * for same ino by different processes. We just allow them to race
681          * and check later to decide who wins. Let the race begin!
682          */
683
684         imp = VFSTOISOFS(mp);
685         dev = imp->im_dev;
686
687         /* Allocate a new vnode/iso_node. */
688         if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
689                 *vpp = NULLVP;
690                 return (error);
691         }
692         MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
693             M_WAITOK | M_ZERO);
694         vp->v_data = ip;
695         ip->i_vnode = vp;
696         ip->i_number = ino;
697
698         lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td);
699         error = insmntque(vp, mp);
700         if (error != 0) {
701                 free(ip, M_ISOFSNODE);
702                 *vpp = NULLVP;
703                 return (error);
704         }
705         error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
706         if (error || *vpp != NULL)
707                 return (error);
708
709         if (isodir == 0) {
710                 int lbn, off;
711
712                 lbn = lblkno(imp, ino);
713                 if (lbn >= imp->volume_space_size) {
714                         vput(vp);
715                         printf("fhtovp: lbn exceed volume space %d\n", lbn);
716                         return (ESTALE);
717                 }
718
719                 off = blkoff(imp, ino);
720                 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
721                         vput(vp);
722                         printf("fhtovp: crosses block boundary %d\n",
723                                off + ISO_DIRECTORY_RECORD_SIZE);
724                         return (ESTALE);
725                 }
726
727                 error = bread(imp->im_devvp,
728                               lbn << (imp->im_bshift - DEV_BSHIFT),
729                               imp->logical_block_size, NOCRED, &bp);
730                 if (error) {
731                         vput(vp);
732                         brelse(bp);
733                         printf("fhtovp: bread error %d\n",error);
734                         return (error);
735                 }
736                 isodir = (struct iso_directory_record *)(bp->b_data + off);
737
738                 if (off + isonum_711(isodir->length) >
739                     imp->logical_block_size) {
740                         vput(vp);
741                         if (bp != 0)
742                                 brelse(bp);
743                         printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
744                                off +isonum_711(isodir->length), off,
745                                isonum_711(isodir->length));
746                         return (ESTALE);
747                 }
748
749 #if 0
750                 if (isonum_733(isodir->extent) +
751                     isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
752                         if (bp != 0)
753                                 brelse(bp);
754                         printf("fhtovp: file start miss %d vs %d\n",
755                                isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
756                                ifhp->ifid_start);
757                         return (ESTALE);
758                 }
759 #endif
760         } else
761                 bp = 0;
762
763         ip->i_mnt = imp;
764
765         if (relocated) {
766                 /*
767                  * On relocated directories we must
768                  * read the `.' entry out of a dir.
769                  */
770                 ip->iso_start = ino >> imp->im_bshift;
771                 if (bp != 0)
772                         brelse(bp);
773                 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
774                         vput(vp);
775                         return (error);
776                 }
777                 isodir = (struct iso_directory_record *)bp->b_data;
778         }
779
780         ip->iso_extent = isonum_733(isodir->extent);
781         ip->i_size = isonum_733(isodir->size);
782         ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
783
784         /*
785          * Setup time stamp, attribute
786          */
787         vp->v_type = VNON;
788         switch (imp->iso_ftype) {
789         default:        /* ISO_FTYPE_9660 */
790             {
791                 struct buf *bp2;
792                 int off;
793                 if ((imp->im_flags & ISOFSMNT_EXTATT)
794                     && (off = isonum_711(isodir->ext_attr_length)))
795                         cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
796                                      &bp2);
797                 else
798                         bp2 = NULL;
799                 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
800                 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
801                 if (bp2)
802                         brelse(bp2);
803                 break;
804             }
805         case ISO_FTYPE_RRIP:
806                 cd9660_rrip_analyze(isodir, ip, imp);
807                 break;
808         }
809
810         if (bp != 0)
811                 brelse(bp);
812
813         /*
814          * Initialize the associated vnode
815          */
816         switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
817         case VFIFO:
818                 vp->v_op = &cd9660_fifoops;
819                 break;
820         default:
821                 vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
822                 break;
823         }
824
825         if (ip->iso_extent == imp->root_extent)
826                 vp->v_vflag |= VV_ROOT;
827
828         /*
829          * XXX need generation number?
830          */
831
832         *vpp = vp;
833         return (0);
834 }