1 /* $OpenBSD: cd9660_vfsops.c,v 1.98 2023/07/17 09:41:20 semarie Exp $ */
2 /* $NetBSD: cd9660_vfsops.c,v 1.26 1997/06/13 15:38:58 pk Exp $ */
3
4 /*-
5 * Copyright (c) 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley
9 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
10 * Support code is derived from software contributed to Berkeley
11 * by Atsushi Murai (amurai@spec.co.jp).
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)cd9660_vfsops.c 8.9 (Berkeley) 12/5/94
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/lock.h>
47 #include <sys/specdev.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/fcntl.h>
51 #include <sys/disklabel.h>
52 #include <sys/ioctl.h>
53 #include <sys/cdio.h>
54 #include <sys/errno.h>
55 #include <sys/malloc.h>
56 #include <sys/stat.h>
57
58 #include <isofs/cd9660/iso.h>
59 #include <isofs/cd9660/cd9660_extern.h>
60 #include <isofs/cd9660/iso_rrip.h>
61 #include <isofs/cd9660/cd9660_node.h>
62
63 const struct vfsops cd9660_vfsops = {
64 .vfs_mount = cd9660_mount,
65 .vfs_start = cd9660_start,
66 .vfs_unmount = cd9660_unmount,
67 .vfs_root = cd9660_root,
68 .vfs_quotactl = cd9660_quotactl,
69 .vfs_statfs = cd9660_statfs,
70 .vfs_sync = cd9660_sync,
71 .vfs_vget = cd9660_vget,
72 .vfs_fhtovp = cd9660_fhtovp,
73 .vfs_vptofh = cd9660_vptofh,
74 .vfs_init = cd9660_init,
75 .vfs_sysctl = (void *)eopnotsupp,
76 .vfs_checkexp = cd9660_check_export,
77 };
78
79 /*
80 * Called by vfs_mountroot when iso is going to be mounted as root.
81 */
82
83 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
84 struct proc *p, struct iso_args *argp);
85 int iso_disklabelspoof(dev_t dev, void (*strat)(struct buf *),
86 struct disklabel *lp);
87
88 int
cd9660_mountroot(void)89 cd9660_mountroot(void)
90 {
91 struct mount *mp;
92 extern struct vnode *rootvp;
93 struct proc *p = curproc; /* XXX */
94 int error;
95 struct iso_args args;
96
97 /*
98 * Get vnodes for swapdev and rootdev.
99 */
100 if ((error = bdevvp(swapdev, &swapdev_vp)) ||
101 (error = bdevvp(rootdev, &rootvp))) {
102 printf("cd9660_mountroot: can't setup bdevvp's");
103 return (error);
104 }
105
106 if ((error = vfs_rootmountalloc("cd9660", "root_device", &mp)) != 0)
107 return (error);
108 args.flags = ISOFSMNT_ROOT;
109 if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0) {
110 vfs_unbusy(mp);
111 vfs_mount_free(mp);
112 return (error);
113 }
114
115 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
116 (void)cd9660_statfs(mp, &mp->mnt_stat, p);
117 vfs_unbusy(mp);
118 inittodr(0);
119
120 return (0);
121 }
122
123 /*
124 * VFS Operations.
125 *
126 * mount system call
127 */
128 int
cd9660_mount(struct mount * mp,const char * path,void * data,struct nameidata * ndp,struct proc * p)129 cd9660_mount(struct mount *mp, const char *path, void *data,
130 struct nameidata *ndp, struct proc *p)
131 {
132 struct iso_mnt *imp = NULL;
133 struct iso_args *args = data;
134 struct vnode *devvp;
135 char fspec[MNAMELEN];
136 int error;
137
138 if ((mp->mnt_flag & MNT_RDONLY) == 0)
139 return (EROFS);
140
141 /*
142 * If updating, check whether changing from read-only to
143 * read/write; if there is no device name, that's all we do.
144 */
145 if (mp->mnt_flag & MNT_UPDATE) {
146 imp = VFSTOISOFS(mp);
147 if (args && args->fspec == NULL)
148 return (vfs_export(mp, &imp->im_export,
149 &args->export_info));
150 return (0);
151 }
152
153 /*
154 * Not an update, or updating the name: look up the name
155 * and verify that it refers to a sensible block device.
156 */
157 error = copyinstr(args->fspec, fspec, sizeof(fspec), NULL);
158 if (error)
159 return (error);
160 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, p);
161 if ((error = namei(ndp)) != 0)
162 return (error);
163 devvp = ndp->ni_vp;
164
165 if (devvp->v_type != VBLK) {
166 vrele(devvp);
167 return (ENOTBLK);
168 }
169 if (major(devvp->v_rdev) >= nblkdev) {
170 vrele(devvp);
171 return (ENXIO);
172 }
173
174 if ((mp->mnt_flag & MNT_UPDATE) == 0)
175 error = iso_mountfs(devvp, mp, p, args);
176 else {
177 if (devvp != imp->im_devvp)
178 error = EINVAL; /* needs translation */
179 else
180 vrele(devvp);
181 }
182 if (error) {
183 vrele(devvp);
184 return (error);
185 }
186
187 bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
188 strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
189 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
190 strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
191 bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN);
192 strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
193 bcopy(args, &mp->mnt_stat.mount_info.iso_args, sizeof(*args));
194
195 cd9660_statfs(mp, &mp->mnt_stat, p);
196
197 return (0);
198 }
199
200 /*
201 * Common code for mount and mountroot
202 */
203 static int
iso_mountfs(struct vnode * devvp,struct mount * mp,struct proc * p,struct iso_args * argp)204 iso_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p,
205 struct iso_args *argp)
206 {
207 struct iso_mnt *isomp = NULL;
208 struct buf *bp = NULL;
209 struct buf *pribp = NULL, *supbp = NULL;
210 dev_t dev = devvp->v_rdev;
211 int error = EINVAL;
212 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
213 extern struct vnode *rootvp;
214 int iso_bsize;
215 int iso_blknum;
216 int joliet_level;
217 struct iso_volume_descriptor *vdp;
218 struct iso_primary_descriptor *pri = NULL;
219 struct iso_supplementary_descriptor *sup = NULL;
220 struct iso_directory_record *rootp;
221 int logical_block_size;
222 int sess;
223
224 if (!ronly)
225 return (EROFS);
226
227 /*
228 * Disallow multiple mounts of the same device.
229 * Disallow mounting of a device that is currently in use
230 * (except for root, which might share swap device for miniroot).
231 * Flush out any old buffers remaining from a previous use.
232 */
233 if ((error = vfs_mountedon(devvp)) != 0)
234 return (error);
235 if (vcount(devvp) > 1 && devvp != rootvp)
236 return (EBUSY);
237 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
238 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, INFSLP);
239 VOP_UNLOCK(devvp);
240 if (error)
241 return (error);
242
243 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
244 if (error)
245 return (error);
246
247 /*
248 * This is the "logical sector size". The standard says this
249 * should be 2048 or the physical sector size on the device,
250 * whichever is greater. For now, we'll just use a constant.
251 */
252 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
253
254 if (argp->flags & ISOFSMNT_SESS) {
255 sess = argp->sess;
256 if (sess < 0)
257 sess = 0;
258 } else {
259 sess = 0;
260 error = VOP_IOCTL(devvp, CDIOREADMSADDR, (caddr_t)&sess, 0,
261 FSCRED, p);
262 if (error)
263 sess = 0;
264 }
265
266 joliet_level = 0;
267 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
268 if ((error = bread(devvp,
269 (iso_blknum + sess) * btodb(iso_bsize),
270 iso_bsize, &bp)) != 0)
271 goto out;
272
273 vdp = (struct iso_volume_descriptor *)bp->b_data;
274 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
275 error = EINVAL;
276 goto out;
277 }
278
279 switch (isonum_711 (vdp->type)){
280 case ISO_VD_PRIMARY:
281 if (pribp == NULL) {
282 pribp = bp;
283 bp = NULL;
284 pri = (struct iso_primary_descriptor *)vdp;
285 }
286 break;
287 case ISO_VD_SUPPLEMENTARY:
288 if (supbp == NULL) {
289 supbp = bp;
290 bp = NULL;
291 sup = (struct iso_supplementary_descriptor *)vdp;
292
293 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
294 if (bcmp(sup->escape, "%/@", 3) == 0)
295 joliet_level = 1;
296 if (bcmp(sup->escape, "%/C", 3) == 0)
297 joliet_level = 2;
298 if (bcmp(sup->escape, "%/E", 3) == 0)
299 joliet_level = 3;
300
301 if (isonum_711 (sup->flags) & 1)
302 joliet_level = 0;
303 }
304 }
305 break;
306
307 case ISO_VD_END:
308 goto vd_end;
309
310 default:
311 break;
312 }
313 if (bp) {
314 brelse(bp);
315 bp = NULL;
316 }
317 }
318 vd_end:
319 if (bp) {
320 brelse(bp);
321 bp = NULL;
322 }
323
324 if (pri == NULL) {
325 error = EINVAL;
326 goto out;
327 }
328
329 logical_block_size = isonum_723 (pri->logical_block_size);
330
331 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
332 || (logical_block_size & (logical_block_size - 1)) != 0) {
333 error = EINVAL;
334 goto out;
335 }
336
337 rootp = (struct iso_directory_record *)pri->root_directory_record;
338
339 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
340 bzero((caddr_t)isomp, sizeof *isomp);
341 isomp->logical_block_size = logical_block_size;
342 isomp->volume_space_size = isonum_733 (pri->volume_space_size);
343 bcopy (rootp, isomp->root, sizeof isomp->root);
344 isomp->root_extent = isonum_733 (rootp->extent);
345 isomp->root_size = isonum_733 (rootp->size);
346 isomp->joliet_level = 0;
347 /*
348 * Since an ISO9660 multi-session CD can also access previous sessions,
349 * we have to include them into the space considerations.
350 */
351 isomp->volume_space_size += sess;
352 isomp->im_bmask = logical_block_size - 1;
353 isomp->im_bshift = ffs(logical_block_size) - 1;
354
355 brelse(pribp);
356 pribp = NULL;
357
358 mp->mnt_data = isomp;
359 mp->mnt_stat.f_fsid.val[0] = (long)dev;
360 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
361 mp->mnt_stat.f_namemax = NAME_MAX;
362 mp->mnt_flag |= MNT_LOCAL;
363 isomp->im_mountp = mp;
364 isomp->im_dev = dev;
365 isomp->im_devvp = devvp;
366
367 /* Check the Rock Ridge Extension support */
368 if (!(argp->flags & ISOFSMNT_NORRIP)) {
369 if ((error = bread(isomp->im_devvp, (isomp->root_extent +
370 isonum_711(rootp->ext_attr_length)) <<
371 (isomp->im_bshift - DEV_BSHIFT),
372 isomp->logical_block_size, &bp)) != 0)
373 goto out;
374
375 rootp = (struct iso_directory_record *)bp->b_data;
376
377 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
378 argp->flags |= ISOFSMNT_NORRIP;
379 } else {
380 argp->flags &= ~ISOFSMNT_GENS;
381 }
382
383 /*
384 * The contents are valid,
385 * but they will get reread as part of another vnode, so...
386 */
387 brelse(bp);
388 bp = NULL;
389 }
390 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
391 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
392 switch (isomp->im_flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS)) {
393 default:
394 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
395 break;
396 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
397 isomp->iso_ftype = ISO_FTYPE_9660;
398 break;
399 case 0:
400 isomp->iso_ftype = ISO_FTYPE_RRIP;
401 break;
402 }
403
404 /* Decide whether to use the Joliet descriptor */
405
406 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
407 rootp = (struct iso_directory_record *)
408 sup->root_directory_record;
409 bcopy(rootp, isomp->root, sizeof isomp->root);
410 isomp->root_extent = isonum_733(rootp->extent);
411 isomp->root_size = isonum_733(rootp->size);
412 isomp->joliet_level = joliet_level;
413 }
414
415 if (supbp) {
416 brelse(supbp);
417 supbp = NULL;
418 }
419
420 devvp->v_specmountpoint = mp;
421
422 return (0);
423 out:
424 if (devvp->v_specinfo)
425 devvp->v_specmountpoint = NULL;
426 if (bp)
427 brelse(bp);
428 if (supbp)
429 brelse(supbp);
430
431 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
432 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
433 VOP_UNLOCK(devvp);
434
435 if (isomp) {
436 free((caddr_t)isomp, M_ISOFSMNT, 0);
437 mp->mnt_data = NULL;
438 }
439 return (error);
440 }
441
442 /*
443 * Test to see if the device is an ISOFS filesystem.
444 */
445 int
iso_disklabelspoof(dev_t dev,void (* strat)(struct buf *),struct disklabel * lp)446 iso_disklabelspoof(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp)
447 {
448 struct buf *bp = NULL;
449 struct iso_volume_descriptor *vdp;
450 struct iso_primary_descriptor *pri;
451 int logical_block_size;
452 int error = EINVAL;
453 int iso_blknum;
454 int i;
455
456 bp = geteblk(ISO_DEFAULT_BLOCK_SIZE);
457 bp->b_dev = dev;
458
459 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
460 bp->b_blkno = iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE);
461 bp->b_bcount = ISO_DEFAULT_BLOCK_SIZE;
462 CLR(bp->b_flags, B_READ | B_WRITE | B_DONE);
463 SET(bp->b_flags, B_BUSY | B_READ | B_RAW);
464
465 /*printf("d_secsize %d iso_blknum %d b_blkno %d bcount %d\n",
466 lp->d_secsize, iso_blknum, bp->b_blkno, bp->b_bcount);*/
467
468 (*strat)(bp);
469
470 if (biowait(bp))
471 goto out;
472
473 vdp = (struct iso_volume_descriptor *)bp->b_data;
474 /*printf("%2x%2x%2x type %2x\n", vdp->id[0], vdp->id[1],
475 vdp->id[2], isonum_711(vdp->type));*/
476 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0 ||
477 isonum_711 (vdp->type) == ISO_VD_END)
478 goto out;
479
480 if (isonum_711 (vdp->type) == ISO_VD_PRIMARY)
481 break;
482 }
483
484 if (isonum_711 (vdp->type) != ISO_VD_PRIMARY)
485 goto out;
486
487 pri = (struct iso_primary_descriptor *)vdp;
488 logical_block_size = isonum_723 (pri->logical_block_size);
489 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE ||
490 (logical_block_size & (logical_block_size - 1)) != 0)
491 goto out;
492
493 /*
494 * build a disklabel for the CD
495 */
496 strncpy(lp->d_typename, pri->volume_id, sizeof lp->d_typename);
497 strncpy(lp->d_packname, pri->volume_id+16, sizeof lp->d_packname);
498 for (i = 0; i < MAXPARTITIONS; i++) {
499 DL_SETPSIZE(&lp->d_partitions[i], 0);
500 DL_SETPOFFSET(&lp->d_partitions[i], 0);
501 }
502 DL_SETPOFFSET(&lp->d_partitions[0], 0);
503 DL_SETPSIZE(&lp->d_partitions[0], DL_GETDSIZE(lp));
504 lp->d_partitions[0].p_fstype = FS_ISO9660;
505 DL_SETPOFFSET(&lp->d_partitions[RAW_PART], 0);
506 DL_SETPSIZE(&lp->d_partitions[RAW_PART], DL_GETDSIZE(lp));
507 lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
508 lp->d_npartitions = MAXPARTITIONS;
509 lp->d_version = 1;
510
511 lp->d_magic = DISKMAGIC;
512 lp->d_magic2 = DISKMAGIC;
513 lp->d_checksum = dkcksum(lp);
514 error = 0;
515 out:
516 bp->b_flags |= B_INVAL;
517 brelse(bp);
518 return (error);
519 }
520
521 /*
522 * Make a filesystem operational.
523 * Nothing to do at the moment.
524 */
525 int
cd9660_start(struct mount * mp,int flags,struct proc * p)526 cd9660_start(struct mount *mp, int flags, struct proc *p)
527 {
528 return (0);
529 }
530
531 /*
532 * unmount system call
533 */
534 int
cd9660_unmount(struct mount * mp,int mntflags,struct proc * p)535 cd9660_unmount(struct mount *mp, int mntflags, struct proc *p)
536 {
537 struct iso_mnt *isomp;
538 int error, flags = 0;
539
540 if (mntflags & MNT_FORCE)
541 flags |= FORCECLOSE;
542 #if 0
543 mntflushbuf(mp, 0);
544 if (mntinvalbuf(mp))
545 return (EBUSY);
546 #endif
547 if ((error = vflush(mp, NULLVP, flags)) != 0)
548 return (error);
549
550 isomp = VFSTOISOFS(mp);
551
552 isomp->im_devvp->v_specmountpoint = NULL;
553 vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
554 (void)VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
555 vput(isomp->im_devvp);
556 free((caddr_t)isomp, M_ISOFSMNT, 0);
557 mp->mnt_data = NULL;
558 mp->mnt_flag &= ~MNT_LOCAL;
559 return (0);
560 }
561
562 /*
563 * Return root of a filesystem
564 */
565 int
cd9660_root(struct mount * mp,struct vnode ** vpp)566 cd9660_root(struct mount *mp, struct vnode **vpp)
567 {
568 struct iso_mnt *imp = VFSTOISOFS(mp);
569 struct iso_directory_record *dp =
570 (struct iso_directory_record *)imp->root;
571 cdino_t ino = isodirino(dp, imp);
572
573 /*
574 * With RRIP we must use the `.' entry of the root directory.
575 * Simply tell vget, that it's a relocated directory.
576 */
577 return (cd9660_vget_internal(mp, ino, vpp,
578 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
579 }
580
581 /*
582 * Do operations associated with quotas, not supported
583 */
584 int
cd9660_quotactl(struct mount * mp,int cmd,uid_t uid,caddr_t arg,struct proc * p)585 cd9660_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg,
586 struct proc *p)
587 {
588
589 return (EOPNOTSUPP);
590 }
591
592 /*
593 * Get file system statistics.
594 */
595 int
cd9660_statfs(struct mount * mp,struct statfs * sbp,struct proc * p)596 cd9660_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
597 {
598 struct iso_mnt *isomp;
599
600 isomp = VFSTOISOFS(mp);
601
602 sbp->f_bsize = isomp->logical_block_size;
603 sbp->f_iosize = sbp->f_bsize; /* XXX */
604 sbp->f_blocks = isomp->volume_space_size;
605 sbp->f_bfree = 0; /* total free blocks */
606 sbp->f_bavail = 0; /* blocks free for non superuser */
607 sbp->f_files = 0; /* total files */
608 sbp->f_ffree = 0; /* free file nodes */
609 sbp->f_favail = 0; /* file nodes free for non superuser */
610 copy_statfs_info(sbp, mp);
611
612 return (0);
613 }
614
615 int
cd9660_sync(struct mount * mp,int waitfor,int stall,struct ucred * cred,struct proc * p)616 cd9660_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred,
617 struct proc *p)
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 int
cd9660_fhtovp(struct mount * mp,struct fid * fhp,struct vnode ** vpp)640 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
641 {
642 struct ifid *ifhp = (struct ifid *)fhp;
643 struct iso_node *ip;
644 struct vnode *nvp;
645 int error;
646
647 #ifdef ISOFS_DBG
648 printf("fhtovp: ino %d, start %ld\n", ifhp->ifid_ino,
649 ifhp->ifid_start);
650 #endif
651
652 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
653 *vpp = NULLVP;
654 return (error);
655 }
656 ip = VTOI(nvp);
657 if (ip->inode.iso_mode == 0) {
658 vput(nvp);
659 *vpp = NULLVP;
660 return (ESTALE);
661 }
662 *vpp = nvp;
663 return (0);
664 }
665
666 int
cd9660_vget(struct mount * mp,ino_t ino,struct vnode ** vpp)667 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
668 {
669
670 if (ino > (cdino_t)-1)
671 panic("cd9660_vget: alien ino_t %llu",
672 (unsigned long long)ino);
673
674 /*
675 * XXXX
676 * It would be nice if we didn't always set the `relocated' flag
677 * and force the extra read, but I don't want to think about fixing
678 * that right now.
679 */
680 return (cd9660_vget_internal(mp, ino, vpp,
681 #if 0
682 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
683 #else
684 0,
685 #endif
686 NULL));
687 }
688
689 int
cd9660_vget_internal(struct mount * mp,cdino_t ino,struct vnode ** vpp,int relocated,struct iso_directory_record * isodir)690 cd9660_vget_internal(struct mount *mp, cdino_t ino, struct vnode **vpp,
691 int relocated, struct iso_directory_record *isodir)
692 {
693 struct iso_mnt *imp;
694 struct iso_node *ip;
695 struct buf *bp;
696 struct vnode *vp, *nvp;
697 dev_t dev;
698 int error;
699
700 retry:
701 imp = VFSTOISOFS(mp);
702 dev = imp->im_dev;
703 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
704 return (0);
705
706 /* Allocate a new vnode/iso_node. */
707 if ((error = getnewvnode(VT_ISOFS, mp, &cd9660_vops, &vp)) != 0) {
708 *vpp = NULLVP;
709 return (error);
710 }
711 ip = malloc(sizeof(*ip), M_ISOFSNODE, M_WAITOK | M_ZERO);
712 rrw_init_flags(&ip->i_lock, "isoinode", RWL_DUPOK | RWL_IS_VNODE);
713 vp->v_data = ip;
714 ip->i_vnode = vp;
715 ip->i_dev = dev;
716 ip->i_number = ino;
717
718 /*
719 * Put it onto its hash chain and lock it so that other requests for
720 * this inode will block if they arrive while we are sleeping waiting
721 * for old data structures to be purged or for the contents of the
722 * disk portion of this inode to be read.
723 */
724 error = cd9660_ihashins(ip);
725
726 if (error) {
727 vrele(vp);
728
729 if (error == EEXIST)
730 goto retry;
731
732 return (error);
733 }
734
735 if (isodir == 0) {
736 int lbn, off;
737
738 lbn = lblkno(imp, ino);
739 if (lbn >= imp->volume_space_size) {
740 vput(vp);
741 printf("fhtovp: lbn exceed volume space %d\n", lbn);
742 return (ESTALE);
743 }
744
745 off = blkoff(imp, ino);
746 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size)
747 {
748 vput(vp);
749 printf("fhtovp: crosses block boundary %d\n",
750 off + ISO_DIRECTORY_RECORD_SIZE);
751 return (ESTALE);
752 }
753
754 error = bread(imp->im_devvp,
755 lbn << (imp->im_bshift - DEV_BSHIFT),
756 imp->logical_block_size, &bp);
757 if (error) {
758 vput(vp);
759 brelse(bp);
760 printf("fhtovp: bread error %d\n",error);
761 return (error);
762 }
763 isodir = (struct iso_directory_record *)(bp->b_data + off);
764
765 if (off + isonum_711(isodir->length) >
766 imp->logical_block_size) {
767 vput(vp);
768 if (bp != 0)
769 brelse(bp);
770 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
771 off +isonum_711(isodir->length), off,
772 isonum_711(isodir->length));
773 return (ESTALE);
774 }
775
776 #if 0
777 if (isonum_733(isodir->extent) +
778 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
779 if (bp != 0)
780 brelse(bp);
781 printf("fhtovp: file start miss %d vs %d\n",
782 isonum_733(isodir->extent) +
783 isonum_711(isodir->ext_attr_length),
784 ifhp->ifid_start);
785 return (ESTALE);
786 }
787 #endif
788 } else
789 bp = 0;
790
791 ip->i_mnt = imp;
792 ip->i_devvp = imp->im_devvp;
793 vref(ip->i_devvp);
794
795 if (relocated) {
796 /*
797 * On relocated directories we must
798 * read the `.' entry out of a dir.
799 */
800 ip->iso_start = ino >> imp->im_bshift;
801 if (bp != 0)
802 brelse(bp);
803 if ((error = cd9660_bufatoff(ip, (off_t)0, NULL, &bp)) != 0) {
804 vput(vp);
805 return (error);
806 }
807 isodir = (struct iso_directory_record *)bp->b_data;
808 }
809
810 ip->iso_extent = isonum_733(isodir->extent);
811 ip->i_size = (u_int32_t) isonum_733(isodir->size);
812 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
813
814 /*
815 * Setup time stamp, attribute
816 */
817 vp->v_type = VNON;
818 switch (imp->iso_ftype) {
819 default: /* ISO_FTYPE_9660 */
820 {
821 struct buf *bp2;
822 int off;
823 if ((imp->im_flags & ISOFSMNT_EXTATT) &&
824 (off = isonum_711(isodir->ext_attr_length)))
825 cd9660_bufatoff(ip, (off_t)-(off << imp->im_bshift),
826 NULL, &bp2);
827 else
828 bp2 = NULL;
829 cd9660_defattr(isodir, ip, bp2);
830 cd9660_deftstamp(isodir, ip, bp2);
831 if (bp2)
832 brelse(bp2);
833 break;
834 }
835 case ISO_FTYPE_RRIP:
836 cd9660_rrip_analyze(isodir, ip, imp);
837 break;
838 }
839
840 if (bp != 0)
841 brelse(bp);
842
843 /*
844 * Initialize the associated vnode
845 */
846 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
847 case VFIFO:
848 #ifdef FIFO
849 vp->v_op = &cd9660_fifovops;
850 break;
851 #else
852 vput(vp);
853 return (EOPNOTSUPP);
854 #endif /* FIFO */
855 case VCHR:
856 case VBLK:
857 /*
858 * if device, look at device number table for translation
859 */
860 vp->v_op = &cd9660_specvops;
861 if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
862 /*
863 * Discard unneeded vnode, but save its iso_node.
864 * Note that the lock is carried over in the iso_node
865 */
866 nvp->v_data = vp->v_data;
867 vp->v_data = NULL;
868 vp->v_op = &spec_vops;
869 vrele(vp);
870 vgone(vp);
871 /*
872 * Reinitialize aliased inode.
873 */
874 vp = nvp;
875 ip->i_vnode = vp;
876 }
877 break;
878 case VLNK:
879 case VNON:
880 case VSOCK:
881 case VDIR:
882 case VBAD:
883 break;
884 case VREG:
885 uvm_vnp_setsize(vp, ip->i_size);
886 break;
887 }
888
889 if (ip->iso_extent == imp->root_extent)
890 vp->v_flag |= VROOT;
891
892 /*
893 * XXX need generation number?
894 */
895
896 *vpp = vp;
897 return (0);
898 }
899
900 /*
901 * Vnode pointer to File handle
902 */
903 int
cd9660_vptofh(struct vnode * vp,struct fid * fhp)904 cd9660_vptofh(struct vnode *vp, struct fid *fhp)
905 {
906 struct iso_node *ip = VTOI(vp);
907 struct ifid *ifhp;
908
909 ifhp = (struct ifid *)fhp;
910 ifhp->ifid_len = sizeof(struct ifid);
911
912 ifhp->ifid_ino = ip->i_number;
913 ifhp->ifid_start = ip->iso_start;
914
915 #ifdef ISOFS_DBG
916 printf("vptofh: ino %d, start %ld\n",
917 ifhp->ifid_ino,ifhp->ifid_start);
918 #endif
919 return (0);
920 }
921
922 /*
923 * Verify a remote client has export rights and return these rights via
924 * exflagsp and credanonp.
925 */
926 int
cd9660_check_export(struct mount * mp,struct mbuf * nam,int * exflagsp,struct ucred ** credanonp)927 cd9660_check_export(struct mount *mp, struct mbuf *nam, int *exflagsp,
928 struct ucred **credanonp)
929 {
930 struct netcred *np;
931 struct iso_mnt *imp = VFSTOISOFS(mp);
932
933 /*
934 * Get the export permission structure for this <mp, client> tuple.
935 */
936 np = vfs_export_lookup(mp, &imp->im_export, nam);
937 if (np == NULL)
938 return (EACCES);
939
940 *exflagsp = np->netc_exflags;
941 *credanonp = &np->netc_anon;
942 return (0);
943 }
944