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  * %sccs.include.redist.c%
11  *
12  *	@(#)cd9660_vfsops.c	8.3 (Berkeley) 01/31/94
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/namei.h>
18 #include <sys/proc.h>
19 #include <sys/kernel.h>
20 #include <sys/vnode.h>
21 #include <miscfs/specfs/specdev.h>
22 #include <sys/mount.h>
23 #include <sys/buf.h>
24 #include <sys/file.h>
25 #include <sys/dkbad.h>
26 #include <sys/disklabel.h>
27 #include <sys/ioctl.h>
28 #include <sys/errno.h>
29 #include <sys/malloc.h>
30 
31 #include <isofs/cd9660/iso.h>
32 #include <isofs/cd9660/cd9660_node.h>
33 
34 extern int enodev ();
35 
36 struct vfsops cd9660_vfsops = {
37 	cd9660_mount,
38 	cd9660_start,
39 	cd9660_unmount,
40 	cd9660_root,
41 	cd9660_quotactl,
42 	cd9660_statfs,
43 	cd9660_sync,
44 	cd9660_vget,
45 	cd9660_fhtovp,
46 	cd9660_vptofh,
47 	cd9660_init,
48 };
49 
50 /*
51  * Called by vfs_mountroot when iso is going to be mounted as root.
52  *
53  * Name is updated by mount(8) after booting.
54  */
55 #define ROOTNAME	"root_device"
56 
57 static iso_mountfs();
58 
59 cd9660_mountroot()
60 {
61 	register struct mount *mp;
62 	extern struct vnode *rootvp;
63 	struct proc *p = curproc;	/* XXX */
64 	struct iso_mnt *imp;
65 	register struct fs *fs;
66 	u_int size;
67 	int error;
68 	struct iso_args args;
69 
70 	/*
71 	 * Get vnodes for swapdev and rootdev.
72 	 */
73 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
74 		panic("cd9660_mountroot: can't setup bdevvp's");
75 
76 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
77 	bzero((char *)mp, (u_long)sizeof(struct mount));
78 	mp->mnt_op = &cd9660_vfsops;
79 	mp->mnt_flag = MNT_RDONLY;
80 	args.flags = ISOFSMNT_ROOT;
81 	if (error = iso_mountfs(rootvp, mp, p, &args)) {
82 		free(mp, M_MOUNT);
83 		return (error);
84 	}
85 	if (error = vfs_lock(mp)) {
86 		(void)cd9660_unmount(mp, 0, p);
87 		free(mp, M_MOUNT);
88 		return (error);
89 	}
90 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
91 	mp->mnt_flag |= MNT_ROOTFS;
92 	mp->mnt_vnodecovered = NULLVP;
93 	imp = VFSTOISOFS(mp);
94 	bzero(imp->im_fsmnt, sizeof(imp->im_fsmnt));
95 	imp->im_fsmnt[0] = '/';
96 	bcopy((caddr_t)imp->im_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
97 	    MNAMELEN);
98 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
99 	    &size);
100 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
101 	(void) cd9660_statfs(mp, &mp->mnt_stat, p);
102 	vfs_unlock(mp);
103 	return (0);
104 }
105 
106 /*
107  * Flag to allow forcible unmounting.
108  */
109 int iso_doforce = 1;
110 
111 /*
112  * VFS Operations.
113  *
114  * mount system call
115  */
116 cd9660_mount(mp, path, data, ndp, p)
117 	register struct mount *mp;
118 	char *path;
119 	caddr_t data;
120 	struct nameidata *ndp;
121 	struct proc *p;
122 {
123 	struct vnode *devvp;
124 	struct iso_args args;
125 	u_int size;
126 	int error;
127 	struct iso_mnt *imp;
128 
129 	if (error = copyin(data, (caddr_t)&args, sizeof (struct iso_args)))
130 		return (error);
131 
132 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
133 		return (EROFS);
134 
135 	/*
136 	 * If updating, check whether changing from read-only to
137 	 * read/write; if there is no device name, that's all we do.
138 	 */
139 	if (mp->mnt_flag & MNT_UPDATE) {
140 		imp = VFSTOISOFS(mp);
141 		if (args.fspec == 0)
142 			return (vfs_export(mp, &imp->im_export, &args.export));
143 	}
144 	/*
145 	 * Not an update, or updating the name: look up the name
146 	 * and verify that it refers to a sensible block device.
147 	 */
148 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
149 	if (error = namei(ndp))
150 		return (error);
151 	devvp = ndp->ni_vp;
152 
153 	if (devvp->v_type != VBLK) {
154 		vrele(devvp);
155 		return ENOTBLK;
156 	}
157 	if (major(devvp->v_rdev) >= nblkdev) {
158 		vrele(devvp);
159 		return ENXIO;
160 	}
161 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
162 		error = iso_mountfs(devvp, mp, p, &args);
163 	else {
164 		if (devvp != imp->im_devvp)
165 			error = EINVAL;	/* needs translation */
166 		else
167 			vrele(devvp);
168 	}
169 	if (error) {
170 		vrele(devvp);
171 		return error;
172 	}
173 	imp = VFSTOISOFS(mp);
174 	(void) copyinstr(path, imp->im_fsmnt, sizeof(imp->im_fsmnt)-1, &size);
175 	bzero(imp->im_fsmnt + size, sizeof(imp->im_fsmnt) - size);
176 	bcopy((caddr_t)imp->im_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
177 	    MNAMELEN);
178 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
179 	    &size);
180 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
181 	(void) cd9660_statfs(mp, &mp->mnt_stat, p);
182 	return 0;
183 }
184 
185 /*
186  * Common code for mount and mountroot
187  */
188 static iso_mountfs(devvp, mp, p, argp)
189 	register struct vnode *devvp;
190 	struct mount *mp;
191 	struct proc *p;
192 	struct iso_args *argp;
193 {
194 	register struct iso_mnt *isomp = (struct iso_mnt *)0;
195 	struct buf *bp = NULL;
196 	dev_t dev = devvp->v_rdev;
197 	caddr_t base, space;
198 	int havepart = 0, blks;
199 	int error = EINVAL, i, size;
200 	int needclose = 0;
201 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
202 	extern struct vnode *rootvp;
203 	int j;
204 	int iso_bsize;
205 	int iso_blknum;
206 	struct iso_volume_descriptor *vdp;
207 	struct iso_primary_descriptor *pri;
208 	struct iso_directory_record *rootp;
209 	int logical_block_size;
210 
211 	if (!ronly)
212 		return EROFS;
213 
214 	/*
215 	 * Disallow multiple mounts of the same device.
216 	 * Disallow mounting of a device that is currently in use
217 	 * (except for root, which might share swap device for miniroot).
218 	 * Flush out any old buffers remaining from a previous use.
219 	 */
220 	if (error = vfs_mountedon(devvp))
221 		return error;
222 	if (vcount(devvp) > 1 && devvp != rootvp)
223 		return EBUSY;
224 	if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
225 		return (error);
226 
227 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
228 		return error;
229 	needclose = 1;
230 
231 	/* This is the "logical sector size".  The standard says this
232 	 * should be 2048 or the physical sector size on the device,
233 	 * whichever is greater.  For now, we'll just use a constant.
234 	 */
235 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
236 
237 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
238 		if (error = bread (devvp, btodb(iso_blknum * iso_bsize),
239 				   iso_bsize, NOCRED, &bp))
240 			goto out;
241 
242 		vdp = (struct iso_volume_descriptor *)bp->b_un.b_addr;
243 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
244 			error = EINVAL;
245 			goto out;
246 		}
247 
248 		if (isonum_711 (vdp->type) == ISO_VD_END) {
249 			error = EINVAL;
250 			goto out;
251 		}
252 
253 		if (isonum_711 (vdp->type) == ISO_VD_PRIMARY)
254 			break;
255 		brelse(bp);
256 	}
257 
258 	if (isonum_711 (vdp->type) != ISO_VD_PRIMARY) {
259 		error = EINVAL;
260 		goto out;
261 	}
262 
263 	pri = (struct iso_primary_descriptor *)vdp;
264 
265 	logical_block_size = isonum_723 (pri->logical_block_size);
266 
267 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
268 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
269 		error = EINVAL;
270 		goto out;
271 	}
272 
273 	rootp = (struct iso_directory_record *)pri->root_directory_record;
274 
275 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
276 	bzero((caddr_t)isomp, sizeof *isomp);
277 	isomp->logical_block_size = logical_block_size;
278 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
279 	bcopy (rootp, isomp->root, sizeof isomp->root);
280 	isomp->root_extent = isonum_733 (rootp->extent);
281 	isomp->root_size = isonum_733 (rootp->size);
282 
283 	isomp->im_bmask = logical_block_size - 1;
284 	isomp->im_bshift = 0;
285 	while ((1 << isomp->im_bshift) < isomp->logical_block_size)
286 		isomp->im_bshift++;
287 
288 	bp->b_flags |= B_AGE;
289 	brelse(bp);
290 	bp = NULL;
291 
292 	mp->mnt_data = (qaddr_t)isomp;
293 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
294 	mp->mnt_stat.f_fsid.val[1] = MOUNT_CD9660;
295 	mp->mnt_maxsymlinklen = 0;
296 	mp->mnt_flag |= MNT_LOCAL;
297 	isomp->im_mountp = mp;
298 	isomp->im_dev = dev;
299 	isomp->im_devvp = devvp;
300 
301 	devvp->v_specflags |= SI_MOUNTEDON;
302 
303 	/* Check the Rock Ridge Extention support */
304 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
305 		if (error = bread (isomp->im_devvp,
306 				   (isomp->root_extent + isonum_711(rootp->ext_attr_length))
307 				   * isomp->logical_block_size / DEV_BSIZE,
308 				   isomp->logical_block_size,NOCRED,&bp))
309 		    goto out;
310 
311 		rootp = (struct iso_directory_record *)bp->b_un.b_addr;
312 
313 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
314 		    argp->flags  |= ISOFSMNT_NORRIP;
315 		} else {
316 		    argp->flags  &= ~ISOFSMNT_GENS;
317 		}
318 
319 		/*
320 		 * The contents are valid,
321 		 * but they will get reread as part of another vnode, so...
322 		 */
323 		bp->b_flags |= B_AGE;
324 		brelse(bp);
325 		bp = NULL;
326 	}
327 	isomp->im_flags = argp->flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS|ISOFSMNT_EXTATT);
328 	switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
329 	default:
330 	    isomp->iso_ftype = ISO_FTYPE_DEFAULT;
331 	    break;
332 	case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
333 	    isomp->iso_ftype = ISO_FTYPE_9660;
334 	    break;
335 	case 0:
336 	    isomp->iso_ftype = ISO_FTYPE_RRIP;
337 	    break;
338 	}
339 
340 	return 0;
341 out:
342 	if (bp)
343 		brelse(bp);
344 	if (needclose)
345 		(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
346 	if (isomp) {
347 		free((caddr_t)isomp, M_ISOFSMNT);
348 		mp->mnt_data = (qaddr_t)0;
349 	}
350 	return error;
351 }
352 
353 /*
354  * Make a filesystem operational.
355  * Nothing to do at the moment.
356  */
357 /* ARGSUSED */
358 cd9660_start(mp, flags, p)
359 	struct mount *mp;
360 	int flags;
361 	struct proc *p;
362 {
363 	return 0;
364 }
365 
366 /*
367  * unmount system call
368  */
369 int
370 cd9660_unmount(mp, mntflags, p)
371 	struct mount *mp;
372 	int mntflags;
373 	struct proc *p;
374 {
375 	register struct iso_mnt *isomp;
376 	int i, error, ronly, flags = 0;
377 
378 	if (mntflags & MNT_FORCE) {
379 		if (!iso_doforce || (mp->mnt_flag & MNT_ROOTFS))
380 			return (EINVAL);
381 		flags |= FORCECLOSE;
382 	}
383 #if 0
384 	mntflushbuf(mp, 0);
385 	if (mntinvalbuf(mp))
386 		return EBUSY;
387 #endif
388 	if (error = vflush(mp, NULLVP, flags))
389 		return (error);
390 
391 	isomp = VFSTOISOFS(mp);
392 
393 #ifdef	ISODEVMAP
394 	if (isomp->iso_ftype == ISO_FTYPE_RRIP)
395 		iso_dunmap(isomp->im_dev);
396 #endif
397 
398 	isomp->im_devvp->v_specflags &= ~SI_MOUNTEDON;
399 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
400 	vrele(isomp->im_devvp);
401 	free((caddr_t)isomp, M_ISOFSMNT);
402 	mp->mnt_data = (qaddr_t)0;
403 	mp->mnt_flag &= ~MNT_LOCAL;
404 	return (error);
405 }
406 
407 /*
408  * Return root of a filesystem
409  */
410 cd9660_root(mp, vpp)
411 	struct mount *mp;
412 	struct vnode **vpp;
413 {
414 	register struct iso_node *ip;
415 	struct iso_node tip, *nip;
416 	struct vnode tvp;
417 	int error;
418 	struct iso_mnt *imp = VFSTOISOFS (mp);
419 	struct iso_directory_record *dp;
420 
421 	tvp.v_mount = mp;
422 	tvp.v_data = &tip;
423 	ip = VTOI(&tvp);
424 	ip->i_vnode = &tvp;
425 	ip->i_dev = imp->im_dev;
426 	ip->i_diroff = 0;
427 	dp = (struct iso_directory_record *)imp->root;
428 	isodirino(&ip->i_number,dp,imp);
429 
430 	/*
431 	 * With RRIP we must use the `.' entry of the root directory.
432 	 * Simply tell iget, that it's a relocated directory.
433 	 */
434 	error = iso_iget(ip,ip->i_number,
435 			 imp->iso_ftype == ISO_FTYPE_RRIP,
436 			 &nip,dp);
437 	if (error)
438 		return error;
439 	*vpp = ITOV(nip);
440 	return 0;
441 }
442 
443 /*
444  * Do operations associated with quotas, not supported
445  */
446 /* ARGSUSED */
447 int
448 cd9660_quotactl(mp, cmd, uid, arg, p)
449 	struct mount *mp;
450 	int cmd;
451 	uid_t uid;
452 	caddr_t arg;
453 	struct proc *p;
454 {
455 
456 	return (EOPNOTSUPP);
457 }
458 
459 /*
460  * Get file system statistics.
461  */
462 cd9660_statfs(mp, sbp, p)
463 	struct mount *mp;
464 	register struct statfs *sbp;
465 	struct proc *p;
466 {
467 	register struct iso_mnt *isomp;
468 	register struct fs *fs;
469 
470 	isomp = VFSTOISOFS(mp);
471 
472 	sbp->f_type = MOUNT_CD9660;
473 	sbp->f_bsize = isomp->logical_block_size;
474 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
475 	sbp->f_blocks = isomp->volume_space_size;
476 	sbp->f_bfree = 0; /* total free blocks */
477 	sbp->f_bavail = 0; /* blocks free for non superuser */
478 	sbp->f_files =  0; /* total files */
479 	sbp->f_ffree = 0; /* free file nodes */
480 	if (sbp != &mp->mnt_stat) {
481 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
482 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
483 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
484 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
485 	}
486 	/* Use the first spare for flags: */
487 	sbp->f_spare[0] = isomp->im_flags;
488 	return 0;
489 }
490 
491 /* ARGSUSED */
492 int
493 cd9660_sync(mp, waitfor, cred, p)
494 	struct mount *mp;
495 	int waitfor;
496 	struct ucred *cred;
497 	struct proc *p;
498 {
499 	return (0);
500 }
501 
502 /*
503  * Flat namespace lookup.
504  * Currently unsupported.
505  */
506 /* ARGSUSED */
507 int
508 cd9660_vget(mp, ino, vpp)
509 	struct mount *mp;
510 	ino_t ino;
511 	struct vnode **vpp;
512 {
513 
514 	return (EOPNOTSUPP);
515 }
516 
517 /*
518  * File handle to vnode
519  *
520  * Have to be really careful about stale file handles:
521  * - check that the inode number is in range
522  * - call iget() to get the locked inode
523  * - check for an unallocated inode (i_mode == 0)
524  * - check that the generation number matches
525  */
526 
527 struct ifid {
528 	ushort	ifid_len;
529 	ushort	ifid_pad;
530 	int	ifid_ino;
531 	long	ifid_start;
532 };
533 
534 /* ARGSUSED */
535 int
536 cd9660_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
537 	register struct mount *mp;
538 	struct fid *fhp;
539 	struct mbuf *nam;
540 	struct vnode **vpp;
541 	int *exflagsp;
542 	struct ucred **credanonp;
543 {
544 	struct vnode			tvp;
545 	int				error;
546 	int				lbn, off;
547 	struct ifid			*ifhp;
548 	struct iso_mnt			*imp;
549 	struct buf			*bp;
550 	struct iso_directory_record	*dirp;
551 	struct iso_node 		tip, *ip, *nip;
552 	struct netcred			*np;
553 
554 	imp = VFSTOISOFS (mp);
555 	ifhp = (struct ifid *)fhp;
556 
557 #ifdef	ISOFS_DBG
558 	printf("fhtovp: ino %d, start %ld\n",
559 	       ifhp->ifid_ino, ifhp->ifid_start);
560 #endif
561 
562 	np = vfs_export_lookup(mp, &imp->im_export, nam);
563 	if (np == NULL)
564 		return (EACCES);
565 
566 	lbn = iso_lblkno(imp, ifhp->ifid_ino);
567 	if (lbn >= imp->volume_space_size) {
568 		printf("fhtovp: lbn exceed volume space %d\n", lbn);
569 		return (ESTALE);
570 	}
571 
572 	off = iso_blkoff(imp, ifhp->ifid_ino);
573 	if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
574 		printf("fhtovp: crosses block boundary %d\n",
575 		       off + ISO_DIRECTORY_RECORD_SIZE);
576 		return (ESTALE);
577 	}
578 
579 	error = bread(imp->im_devvp, btodb(lbn * imp->logical_block_size),
580 		      imp->logical_block_size, NOCRED, &bp);
581 	if (error) {
582 		printf("fhtovp: bread error %d\n",error);
583 		brelse(bp);
584 		return (error);
585 	}
586 
587 	dirp = (struct iso_directory_record *)(bp->b_un.b_addr + off);
588 	if (off + isonum_711(dirp->length) > imp->logical_block_size) {
589 		brelse(bp);
590 		printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
591 		       off+isonum_711(dirp->length), off,
592 		       isonum_711(dirp->length));
593 		return (ESTALE);
594 	}
595 
596 	if (isonum_733(dirp->extent) + isonum_711(dirp->ext_attr_length) !=
597 	    ifhp->ifid_start) {
598 		brelse(bp);
599 		printf("fhtovp: file start miss %d vs %d\n",
600 		       isonum_733(dirp->extent)+isonum_711(dirp->ext_attr_length),
601 		       ifhp->ifid_start);
602 		return (ESTALE);
603 	}
604 	brelse(bp);
605 
606 	ip = &tip;
607 	tvp.v_mount = mp;
608 	tvp.v_data = ip;
609 	ip->i_vnode = &tvp;
610 	ip->i_dev = imp->im_dev;
611 	if (error = iso_iget(ip, ifhp->ifid_ino, 0, &nip, dirp)) {
612 		*vpp = NULLVP;
613 		printf("fhtovp: failed to get inode\n");
614 		return (error);
615 	}
616 	ip = nip;
617 	/*
618 	 * XXX need generation number?
619 	 */
620 	if (ip->inode.iso_mode == 0) {
621 		iso_iput(ip);
622 		*vpp = NULLVP;
623 		printf("fhtovp: inode mode == 0\n");
624 		return (ESTALE);
625 	}
626 	*vpp = ITOV(ip);
627 	*exflagsp = np->netc_exflags;
628 	*credanonp = &np->netc_anon;
629 	return 0;
630 }
631 
632 /*
633  * Vnode pointer to File handle
634  */
635 /* ARGSUSED */
636 cd9660_vptofh(vp, fhp)
637 	struct vnode *vp;
638 	struct fid *fhp;
639 {
640 	register struct iso_node *ip = VTOI(vp);
641 	register struct ifid *ifhp;
642 	register struct iso_mnt *mp = ip->i_mnt;
643 
644 	ifhp = (struct ifid *)fhp;
645 	ifhp->ifid_len = sizeof(struct ifid);
646 
647 	ifhp->ifid_ino = ip->i_number;
648 	ifhp->ifid_start = ip->iso_start;
649 
650 #ifdef	ISOFS_DBG
651 	printf("vptofh: ino %d, start %ld\n",
652 	       ifhp->ifid_ino,ifhp->ifid_start);
653 #endif
654 	return 0;
655 }
656