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