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