1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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@blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai@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 __FBSDID("$FreeBSD$");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/bio.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 <fs/cd9660/iso.h>
61 #include <fs/cd9660/iso_rrip.h>
62 #include <fs/cd9660/cd9660_node.h>
63 #include <fs/cd9660/cd9660_mount.h>
64
65 #include <geom/geom.h>
66 #include <geom/geom_vfs.h>
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 vfs_mount_t cd9660_mount;
74 static vfs_cmount_t cd9660_cmount;
75 static vfs_unmount_t cd9660_unmount;
76 static vfs_root_t cd9660_root;
77 static vfs_statfs_t cd9660_statfs;
78 static vfs_vget_t cd9660_vget;
79 static vfs_fhtovp_t cd9660_fhtovp;
80
81 static struct vfsops cd9660_vfsops = {
82 .vfs_fhtovp = cd9660_fhtovp,
83 .vfs_mount = cd9660_mount,
84 .vfs_cmount = cd9660_cmount,
85 .vfs_root = cd9660_root,
86 .vfs_statfs = cd9660_statfs,
87 .vfs_unmount = cd9660_unmount,
88 .vfs_vget = cd9660_vget,
89 };
90 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
91 MODULE_VERSION(cd9660, 1);
92
93 static int cd9660_vfs_hash_cmp(struct vnode *vp, void *pino);
94 static int iso_mountfs(struct vnode *devvp, struct mount *mp);
95
96 /*
97 * VFS Operations.
98 */
99
100 static int
101 cd9660_cmount(struct mntarg *ma, void *data, uint64_t flags)
102 {
103 struct iso_args args;
104 int error;
105
106 error = copyin(data, &args, sizeof args);
107 if (error)
108 return (error);
109
110 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
111 ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
112 ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
113 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
114 ma = mount_argf(ma, "ssector", "%u", args.ssector);
115 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
116 ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
117 ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
118 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
119 ma = mount_argb(ma,
120 args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
121 ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
122
123 error = kernel_mount(ma, flags);
124
125 return (error);
126 }
127
128 static int
129 cd9660_mount(struct mount *mp)
130 {
131 struct vnode *devvp;
132 struct thread *td;
133 char *fspec;
134 int error;
135 accmode_t accmode;
136 struct nameidata ndp;
137 struct iso_mnt *imp = NULL;
138
139 td = curthread;
140
141 /*
142 * Unconditionally mount as read-only.
143 */
144 MNT_ILOCK(mp);
145 mp->mnt_flag |= MNT_RDONLY;
146 MNT_IUNLOCK(mp);
147
148 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
149 if (error)
150 return (error);
151
152 imp = VFSTOISOFS(mp);
153
154 if (mp->mnt_flag & MNT_UPDATE) {
155 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
156 return (0);
157 }
158 /*
159 * Not an update, or updating the name: look up the name
160 * and verify that it refers to a sensible block device.
161 */
162 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
163 if ((error = namei(&ndp)))
164 return (error);
165 NDFREE(&ndp, NDF_ONLY_PNBUF);
166 devvp = ndp.ni_vp;
167
168 if (!vn_isdisk_error(devvp, &error)) {
169 vput(devvp);
170 return (error);
171 }
172
173 /*
174 * Verify that user has necessary permissions on the device,
175 * or has superuser abilities
176 */
177 accmode = VREAD;
178 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
179 if (error)
180 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
181 if (error) {
182 vput(devvp);
183 return (error);
184 }
185
186 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
187 error = iso_mountfs(devvp, mp);
188 if (error)
189 vrele(devvp);
190 } else {
191 if (devvp != imp->im_devvp)
192 error = EINVAL; /* needs translation */
193 vput(devvp);
194 }
195 if (error)
196 return (error);
197 vfs_mountedfrom(mp, fspec);
198 return (0);
199 }
200
201 /*
202 * Common code for mount and mountroot
203 */
204 static int
205 iso_mountfs(devvp, mp)
206 struct vnode *devvp;
207 struct mount *mp;
208 {
209 struct iso_mnt *isomp = NULL;
210 struct buf *bp = NULL;
211 struct buf *pribp = NULL, *supbp = NULL;
212 struct cdev *dev;
213 int error = EINVAL;
214 int high_sierra = 0;
215 int iso_bsize;
216 int iso_blknum;
217 int joliet_level;
218 int isverified = 0;
219 struct iso_volume_descriptor *vdp = NULL;
220 struct iso_primary_descriptor *pri = NULL;
221 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
222 struct iso_supplementary_descriptor *sup = NULL;
223 struct iso_directory_record *rootp;
224 int logical_block_size, ssector;
225 struct g_consumer *cp;
226 struct bufobj *bo;
227 char *cs_local, *cs_disk;
228
229 dev = devvp->v_rdev;
230 dev_ref(dev);
231 g_topology_lock();
232 error = g_vfs_open(devvp, &cp, "cd9660", 0);
233 if (error == 0)
234 g_getattr("MNT::verified", cp, &isverified);
235 g_topology_unlock();
236 VOP_UNLOCK(devvp);
237 if (error)
238 goto out;
239 if (devvp->v_rdev->si_iosize_max != 0)
240 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
241 if (mp->mnt_iosize_max > maxphys)
242 mp->mnt_iosize_max = maxphys;
243
244 bo = &devvp->v_bufobj;
245
246 /* This is the "logical sector size". The standard says this
247 * should be 2048 or the physical sector size on the device,
248 * whichever is greater.
249 */
250 if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
251 error = EINVAL;
252 goto out;
253 }
254
255 iso_bsize = cp->provider->sectorsize;
256
257 joliet_level = 0;
258 if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
259 ssector = 0;
260 for (iso_blknum = 16 + ssector;
261 iso_blknum < 100 + ssector;
262 iso_blknum++) {
263 if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
264 iso_bsize, NOCRED, &bp)) != 0)
265 goto out;
266
267 vdp = (struct iso_volume_descriptor *)bp->b_data;
268 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
269 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
270 sizeof vdp->id_sierra) != 0) {
271 error = EINVAL;
272 goto out;
273 } else
274 high_sierra = 1;
275 }
276 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
277 case ISO_VD_PRIMARY:
278 if (pribp == NULL) {
279 pribp = bp;
280 bp = NULL;
281 pri = (struct iso_primary_descriptor *)vdp;
282 pri_sierra =
283 (struct iso_sierra_primary_descriptor *)vdp;
284 }
285 break;
286
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 (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
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 !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
303 joliet_level = 0;
304 }
305 }
306 break;
307
308 case ISO_VD_END:
309 goto vd_end;
310
311 default:
312 break;
313 }
314 if (bp != NULL) {
315 brelse(bp);
316 bp = NULL;
317 }
318 }
319 vd_end:
320 if (bp != NULL) {
321 brelse(bp);
322 bp = NULL;
323 }
324
325 if (pri == NULL) {
326 error = EINVAL;
327 goto out;
328 }
329
330 logical_block_size =
331 isonum_723 (high_sierra?
332 pri_sierra->logical_block_size:
333 pri->logical_block_size);
334
335 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
336 || (logical_block_size & (logical_block_size - 1)) != 0) {
337 error = EINVAL;
338 goto out;
339 }
340
341 rootp = (struct iso_directory_record *)
342 (high_sierra?
343 pri_sierra->root_directory_record:
344 pri->root_directory_record);
345
346 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
347 isomp->im_cp = cp;
348 isomp->im_bo = bo;
349 isomp->logical_block_size = logical_block_size;
350 isomp->volume_space_size =
351 isonum_733 (high_sierra?
352 pri_sierra->volume_space_size:
353 pri->volume_space_size);
354 isomp->joliet_level = 0;
355 /*
356 * Since an ISO9660 multi-session CD can also access previous
357 * sessions, we have to include them into the space consider-
358 * ations. This doesn't yield a very accurate number since
359 * parts of the old sessions might be inaccessible now, but we
360 * can't do much better. This is also important for the NFS
361 * filehandle validation.
362 */
363 isomp->volume_space_size += ssector;
364 memcpy(isomp->root, rootp, sizeof isomp->root);
365 isomp->root_extent = isonum_733 (rootp->extent);
366 isomp->root_size = isonum_733 (rootp->size);
367
368 isomp->im_bmask = logical_block_size - 1;
369 isomp->im_bshift = ffs(logical_block_size) - 1;
370
371 pribp->b_flags |= B_AGE;
372 brelse(pribp);
373 pribp = NULL;
374 rootp = NULL;
375 pri = NULL;
376 pri_sierra = NULL;
377
378 mp->mnt_data = isomp;
379 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
380 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
381 MNT_ILOCK(mp);
382 if (isverified)
383 mp->mnt_flag |= MNT_VERIFIED;
384 mp->mnt_flag |= MNT_LOCAL;
385 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
386 MNT_IUNLOCK(mp);
387 isomp->im_mountp = mp;
388 isomp->im_dev = dev;
389 isomp->im_devvp = devvp;
390
391 vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
392 vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
393 vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
394 vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
395 vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
396
397 /* Check the Rock Ridge Extension support */
398 if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
399 if ((error = bread(isomp->im_devvp, (isomp->root_extent +
400 isonum_711(((struct iso_directory_record *)isomp->root)->
401 ext_attr_length)) << (isomp->im_bshift - DEV_BSHIFT),
402 isomp->logical_block_size, NOCRED, &bp)) != 0)
403 goto out;
404
405 rootp = (struct iso_directory_record *)bp->b_data;
406
407 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
408 isomp->im_flags |= ISOFSMNT_NORRIP;
409 } else {
410 isomp->im_flags &= ~ISOFSMNT_GENS;
411 }
412
413 /*
414 * The contents are valid,
415 * but they will get reread as part of another vnode, so...
416 */
417 bp->b_flags |= B_AGE;
418 brelse(bp);
419 bp = NULL;
420 rootp = NULL;
421 }
422
423 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
424 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
425 if (error)
426 goto out;
427 cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
428 if (error)
429 goto out;
430 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
431 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
432 } else {
433 isomp->im_d2l = NULL;
434 isomp->im_l2d = NULL;
435 }
436
437 if (high_sierra) {
438 /* this effectively ignores all the mount flags */
439 if (bootverbose)
440 log(LOG_INFO, "cd9660: High Sierra Format\n");
441 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
442 } else
443 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
444 default:
445 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
446 break;
447 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
448 isomp->iso_ftype = ISO_FTYPE_9660;
449 break;
450 case 0:
451 if (bootverbose)
452 log(LOG_INFO, "cd9660: RockRidge Extension\n");
453 isomp->iso_ftype = ISO_FTYPE_RRIP;
454 break;
455 }
456
457 /* Decide whether to use the Joliet descriptor */
458
459 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
460 if (bootverbose)
461 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
462 joliet_level);
463 rootp = (struct iso_directory_record *)
464 sup->root_directory_record;
465 memcpy(isomp->root, rootp, sizeof isomp->root);
466 isomp->root_extent = isonum_733 (rootp->extent);
467 isomp->root_size = isonum_733 (rootp->size);
468 isomp->joliet_level = joliet_level;
469 supbp->b_flags |= B_AGE;
470 }
471
472 if (supbp) {
473 brelse(supbp);
474 supbp = NULL;
475 sup = NULL;
476 }
477
478 return 0;
479 out:
480 if (bp != NULL)
481 brelse(bp);
482 if (pribp != NULL)
483 brelse(pribp);
484 if (supbp != NULL)
485 brelse(supbp);
486 if (cp != NULL) {
487 g_topology_lock();
488 g_vfs_close(cp);
489 g_topology_unlock();
490 }
491 if (isomp) {
492 free(isomp, M_ISOFSMNT);
493 mp->mnt_data = NULL;
494 }
495 dev_rel(dev);
496 return error;
497 }
498
499 /*
500 * unmount system call
501 */
502 static int
503 cd9660_unmount(mp, mntflags)
504 struct mount *mp;
505 int mntflags;
506 {
507 struct iso_mnt *isomp;
508 int error, flags = 0;
509
510 if (mntflags & MNT_FORCE)
511 flags |= FORCECLOSE;
512 if ((error = vflush(mp, 0, flags, curthread)))
513 return (error);
514
515 isomp = VFSTOISOFS(mp);
516
517 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
518 if (isomp->im_d2l)
519 cd9660_iconv->close(isomp->im_d2l);
520 if (isomp->im_l2d)
521 cd9660_iconv->close(isomp->im_l2d);
522 }
523 g_topology_lock();
524 g_vfs_close(isomp->im_cp);
525 g_topology_unlock();
526 vrele(isomp->im_devvp);
527 dev_rel(isomp->im_dev);
528 free(isomp, M_ISOFSMNT);
529 mp->mnt_data = NULL;
530 return (error);
531 }
532
533 /*
534 * Return root of a filesystem
535 */
536 static int
537 cd9660_root(mp, flags, vpp)
538 struct mount *mp;
539 int flags;
540 struct vnode **vpp;
541 {
542 struct iso_mnt *imp = VFSTOISOFS(mp);
543 struct iso_directory_record *dp =
544 (struct iso_directory_record *)imp->root;
545 cd_ino_t ino = isodirino(dp, imp);
546
547 /*
548 * With RRIP we must use the `.' entry of the root directory.
549 * Simply tell vget, that it's a relocated directory.
550 */
551 return (cd9660_vget_internal(mp, ino, flags, vpp,
552 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
553 }
554
555 /*
556 * Get filesystem statistics.
557 */
558 static int
559 cd9660_statfs(mp, sbp)
560 struct mount *mp;
561 struct statfs *sbp;
562 {
563 struct iso_mnt *isomp;
564
565 isomp = VFSTOISOFS(mp);
566
567 sbp->f_bsize = isomp->logical_block_size;
568 sbp->f_iosize = sbp->f_bsize; /* XXX */
569 sbp->f_blocks = isomp->volume_space_size;
570 sbp->f_bfree = 0; /* total free blocks */
571 sbp->f_bavail = 0; /* blocks free for non superuser */
572 sbp->f_files = 0; /* total files */
573 sbp->f_ffree = 0; /* free file nodes */
574 return 0;
575 }
576
577 /*
578 * File handle to vnode
579 *
580 * Have to be really careful about stale file handles:
581 * - check that the inode number is in range
582 * - call iget() to get the locked inode
583 * - check for an unallocated inode (i_mode == 0)
584 * - check that the generation number matches
585 */
586
587 /* ARGSUSED */
588 static int
589 cd9660_fhtovp(mp, fhp, flags, vpp)
590 struct mount *mp;
591 struct fid *fhp;
592 int flags;
593 struct vnode **vpp;
594 {
595 struct ifid ifh;
596 struct iso_node *ip;
597 struct vnode *nvp;
598 int error;
599
600 memcpy(&ifh, fhp, sizeof(ifh));
601
602 #ifdef ISOFS_DBG
603 printf("fhtovp: ino %d, start %ld\n",
604 ifh.ifid_ino, ifh.ifid_start);
605 #endif
606
607 if ((error = VFS_VGET(mp, ifh.ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
608 *vpp = NULLVP;
609 return (error);
610 }
611 ip = VTOI(nvp);
612 if (ip->inode.iso_mode == 0) {
613 vput(nvp);
614 *vpp = NULLVP;
615 return (ESTALE);
616 }
617 *vpp = nvp;
618 vnode_create_vobject(*vpp, ip->i_size, curthread);
619 return (0);
620 }
621
622 /*
623 * Conform to standard VFS interface; can't vget arbitrary inodes beyond 4GB
624 * into media with current inode scheme and 32-bit ino_t. This shouldn't be
625 * needed for anything other than nfsd, and who exports a mounted DVD over NFS?
626 */
627 static int
628 cd9660_vget(mp, ino, flags, vpp)
629 struct mount *mp;
630 ino_t ino;
631 int flags;
632 struct vnode **vpp;
633 {
634
635 /*
636 * XXXX
637 * It would be nice if we didn't always set the `relocated' flag
638 * and force the extra read, but I don't want to think about fixing
639 * that right now.
640 */
641 return (cd9660_vget_internal(mp, ino, flags, vpp,
642 #if 0
643 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
644 #else
645 0,
646 #endif
647 (struct iso_directory_record *)0));
648 }
649
650 /* Use special comparator for full 64-bit ino comparison. */
651 static int
652 cd9660_vfs_hash_cmp(vp, pino)
653 struct vnode *vp;
654 void *pino;
655 {
656 struct iso_node *ip;
657 cd_ino_t ino;
658
659 ip = VTOI(vp);
660 ino = *(cd_ino_t *)pino;
661 return (ip->i_number != ino);
662 }
663
664 int
665 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
666 struct mount *mp;
667 cd_ino_t ino;
668 int flags;
669 struct vnode **vpp;
670 int relocated;
671 struct iso_directory_record *isodir;
672 {
673 struct iso_mnt *imp;
674 struct iso_node *ip;
675 struct buf *bp;
676 struct vnode *vp;
677 int error;
678 struct thread *td;
679
680 td = curthread;
681 error = vfs_hash_get(mp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
682 &ino);
683 if (error || *vpp != NULL)
684 return (error);
685
686 /*
687 * We must promote to an exclusive lock for vnode creation. This
688 * can happen if lookup is passed LOCKSHARED.
689 */
690 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
691 flags &= ~LK_TYPE_MASK;
692 flags |= LK_EXCLUSIVE;
693 }
694
695 /*
696 * We do not lock vnode creation as it is believed to be too
697 * expensive for such rare case as simultaneous creation of vnode
698 * for same ino by different processes. We just allow them to race
699 * and check later to decide who wins. Let the race begin!
700 */
701
702 imp = VFSTOISOFS(mp);
703
704 /* Allocate a new vnode/iso_node. */
705 if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
706 *vpp = NULLVP;
707 return (error);
708 }
709 ip = malloc(sizeof(struct iso_node), M_ISOFSNODE,
710 M_WAITOK | M_ZERO);
711 vp->v_data = ip;
712 ip->i_vnode = vp;
713 ip->i_number = ino;
714
715 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
716 error = insmntque(vp, mp);
717 if (error != 0) {
718 free(ip, M_ISOFSNODE);
719 *vpp = NULLVP;
720 return (error);
721 }
722 error = vfs_hash_insert(vp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
723 &ino);
724 if (error || *vpp != NULL)
725 return (error);
726
727 if (isodir == NULL) {
728 int lbn, off;
729
730 lbn = lblkno(imp, ino);
731 if (lbn >= imp->volume_space_size) {
732 vput(vp);
733 printf("fhtovp: lbn exceed volume space %d\n", lbn);
734 return (ESTALE);
735 }
736
737 off = blkoff(imp, ino);
738 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
739 vput(vp);
740 printf("fhtovp: crosses block boundary %d\n",
741 off + ISO_DIRECTORY_RECORD_SIZE);
742 return (ESTALE);
743 }
744
745 error = bread(imp->im_devvp,
746 lbn << (imp->im_bshift - DEV_BSHIFT),
747 imp->logical_block_size, NOCRED, &bp);
748 if (error) {
749 vput(vp);
750 printf("fhtovp: bread error %d\n",error);
751 return (error);
752 }
753 isodir = (struct iso_directory_record *)(bp->b_data + off);
754
755 if (off + isonum_711(isodir->length) >
756 imp->logical_block_size) {
757 vput(vp);
758 brelse(bp);
759 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
760 off +isonum_711(isodir->length), off,
761 isonum_711(isodir->length));
762 return (ESTALE);
763 }
764
765 #if 0
766 if (isonum_733(isodir->extent) +
767 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
768 brelse(bp);
769 printf("fhtovp: file start miss %d vs %d\n",
770 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
771 ifhp->ifid_start);
772 return (ESTALE);
773 }
774 #endif
775 } else
776 bp = NULL;
777
778 ip->i_mnt = imp;
779
780 if (relocated) {
781 /*
782 * On relocated directories we must
783 * read the `.' entry out of a dir.
784 */
785 ip->iso_start = ino >> imp->im_bshift;
786 if (bp != NULL)
787 brelse(bp);
788 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
789 vput(vp);
790 return (error);
791 }
792 isodir = (struct iso_directory_record *)bp->b_data;
793 }
794
795 ip->iso_extent = isonum_733(isodir->extent);
796 ip->i_size = isonum_733(isodir->size);
797 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
798
799 /*
800 * Setup time stamp, attribute
801 */
802 vp->v_type = VNON;
803 switch (imp->iso_ftype) {
804 default: /* ISO_FTYPE_9660 */
805 {
806 struct buf *bp2;
807 int off;
808 if ((imp->im_flags & ISOFSMNT_EXTATT)
809 && (off = isonum_711(isodir->ext_attr_length)))
810 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
811 &bp2);
812 else
813 bp2 = NULL;
814 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
815 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
816 if (bp2)
817 brelse(bp2);
818 break;
819 }
820 case ISO_FTYPE_RRIP:
821 cd9660_rrip_analyze(isodir, ip, imp);
822 break;
823 }
824
825 brelse(bp);
826
827 /*
828 * Initialize the associated vnode
829 */
830 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
831 case VFIFO:
832 vp->v_op = &cd9660_fifoops;
833 break;
834 default:
835 VN_LOCK_ASHARE(vp);
836 break;
837 }
838
839 if (ip->iso_extent == imp->root_extent)
840 vp->v_vflag |= VV_ROOT;
841
842 /*
843 * XXX need generation number?
844 */
845
846 *vpp = vp;
847 return (0);
848 }
Cache object: 12fb7e4973a964c3cac32678c30a663c
|