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