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