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