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