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