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