xref: /freebsd/stand/libsa/cd9660.c (revision 3e15b01d)
1ca987d46SWarner Losh /*	$NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $	*/
2ca987d46SWarner Losh 
3ca987d46SWarner Losh /*
4ca987d46SWarner Losh  * Copyright (C) 1996 Wolfgang Solfrank.
5ca987d46SWarner Losh  * Copyright (C) 1996 TooLs GmbH.
6ca987d46SWarner Losh  * All rights reserved.
7ca987d46SWarner Losh  *
8ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
9ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
10ca987d46SWarner Losh  * are met:
11ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
12ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
13ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
14ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
15ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
16ca987d46SWarner Losh  * 3. All advertising materials mentioning features or use of this software
17ca987d46SWarner Losh  *    must display the following acknowledgement:
18ca987d46SWarner Losh  *	This product includes software developed by TooLs GmbH.
19ca987d46SWarner Losh  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20ca987d46SWarner Losh  *    derived from this software without specific prior written permission.
21ca987d46SWarner Losh  *
22ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23ca987d46SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24ca987d46SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25ca987d46SWarner Losh  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26ca987d46SWarner Losh  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27ca987d46SWarner Losh  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28ca987d46SWarner Losh  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29ca987d46SWarner Losh  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30ca987d46SWarner Losh  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31ca987d46SWarner Losh  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32ca987d46SWarner Losh  */
33ca987d46SWarner Losh 
34ca987d46SWarner Losh /*
35ca987d46SWarner Losh  * Stand-alone ISO9660 file reading package.
36ca987d46SWarner Losh  *
37ca987d46SWarner Losh  * Note: This doesn't support Rock Ridge extensions, extended attributes,
38ca987d46SWarner Losh  * blocksizes other than 2048 bytes, multi-extent files, etc.
39ca987d46SWarner Losh  */
40ca987d46SWarner Losh #include <sys/param.h>
41ca987d46SWarner Losh #include <string.h>
426cea60aeSToomas Soome #include <stdbool.h>
43ca987d46SWarner Losh #include <sys/dirent.h>
44b3e16b02SWarner Losh #include <fs/cd9660/iso.h>
45b3e16b02SWarner Losh #include <fs/cd9660/cd9660_rrip.h>
46ca987d46SWarner Losh 
47ca987d46SWarner Losh #include "stand.h"
48ca987d46SWarner Losh 
49ca987d46SWarner Losh #define	SUSP_CONTINUATION	"CE"
50ca987d46SWarner Losh #define	SUSP_PRESENT		"SP"
51ca987d46SWarner Losh #define	SUSP_STOP		"ST"
52ca987d46SWarner Losh #define	SUSP_EXTREF		"ER"
53ca987d46SWarner Losh #define	RRIP_NAME		"NM"
54ca987d46SWarner Losh 
55ca987d46SWarner Losh typedef struct {
56ca987d46SWarner Losh 	ISO_SUSP_HEADER		h;
57ca987d46SWarner Losh 	u_char signature	[ISODCL (  5,    6)];
58ca987d46SWarner Losh 	u_char len_skp		[ISODCL (  7,    7)]; /* 711 */
59ca987d46SWarner Losh } ISO_SUSP_PRESENT;
60ca987d46SWarner Losh 
61ca987d46SWarner Losh static int	buf_read_file(struct open_file *f, char **buf_p,
62ca987d46SWarner Losh 		    size_t *size_p);
63ca987d46SWarner Losh static int	cd9660_open(const char *path, struct open_file *f);
64ca987d46SWarner Losh static int	cd9660_close(struct open_file *f);
65ca987d46SWarner Losh static int	cd9660_read(struct open_file *f, void *buf, size_t size,
66ca987d46SWarner Losh 		    size_t *resid);
67ca987d46SWarner Losh static off_t	cd9660_seek(struct open_file *f, off_t offset, int where);
68ca987d46SWarner Losh static int	cd9660_stat(struct open_file *f, struct stat *sb);
69ca987d46SWarner Losh static int	cd9660_readdir(struct open_file *f, struct dirent *d);
70b4cb3fe0SToomas Soome static int	cd9660_mount(const char *, const char *, void **);
71b4cb3fe0SToomas Soome static int	cd9660_unmount(const char *, void *);
72ca987d46SWarner Losh static int	dirmatch(struct open_file *f, const char *path,
73ca987d46SWarner Losh 		    struct iso_directory_record *dp, int use_rrip, int lenskip);
74ca987d46SWarner Losh static int	rrip_check(struct open_file *f, struct iso_directory_record *dp,
75ca987d46SWarner Losh 		    int *lenskip);
76ca987d46SWarner Losh static char	*rrip_lookup_name(struct open_file *f,
77ca987d46SWarner Losh 		    struct iso_directory_record *dp, int lenskip, size_t *len);
78ca987d46SWarner Losh static ISO_SUSP_HEADER *susp_lookup_record(struct open_file *f,
79ca987d46SWarner Losh 		    const char *identifier, struct iso_directory_record *dp,
80ca987d46SWarner Losh 		    int lenskip);
81ca987d46SWarner Losh 
82ca987d46SWarner Losh struct fs_ops cd9660_fsops = {
83b4cb3fe0SToomas Soome 	.fs_name = "cd9660",
84b4cb3fe0SToomas Soome 	.fo_open = cd9660_open,
85b4cb3fe0SToomas Soome 	.fo_close = cd9660_close,
86b4cb3fe0SToomas Soome 	.fo_read = cd9660_read,
87b4cb3fe0SToomas Soome 	.fo_write = null_write,
88b4cb3fe0SToomas Soome 	.fo_seek = cd9660_seek,
89b4cb3fe0SToomas Soome 	.fo_stat = cd9660_stat,
90b4cb3fe0SToomas Soome 	.fo_readdir = cd9660_readdir,
91b4cb3fe0SToomas Soome 	.fo_mount = cd9660_mount,
92b4cb3fe0SToomas Soome 	.fo_unmount = cd9660_unmount
93ca987d46SWarner Losh };
94ca987d46SWarner Losh 
95b4cb3fe0SToomas Soome typedef struct cd9660_mnt {
96b4cb3fe0SToomas Soome 	struct devdesc			*cd_dev;
97b4cb3fe0SToomas Soome 	int				cd_fd;
98b4cb3fe0SToomas Soome 	struct iso_directory_record	cd_rec;
99b4cb3fe0SToomas Soome 	STAILQ_ENTRY(cd9660_mnt)	cd_link;
100b4cb3fe0SToomas Soome } cd9660_mnt_t;
101b4cb3fe0SToomas Soome 
102b4cb3fe0SToomas Soome typedef STAILQ_HEAD(cd9660_mnt_list, cd9660_mnt) cd9660_mnt_list_t;
103b4cb3fe0SToomas Soome static cd9660_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
104b4cb3fe0SToomas Soome 
105ca987d46SWarner Losh #define	F_ISDIR		0x0001		/* Directory */
106ca987d46SWarner Losh #define	F_ROOTDIR	0x0002		/* Root directory */
107ca987d46SWarner Losh #define	F_RR		0x0004		/* Rock Ridge on this volume */
108ca987d46SWarner Losh 
109ca987d46SWarner Losh struct file {
110ca987d46SWarner Losh 	int 		f_flags;	/* file flags */
111ca987d46SWarner Losh 	off_t 		f_off;		/* Current offset within file */
112ca987d46SWarner Losh 	daddr_t 	f_bno;		/* Starting block number */
113ca987d46SWarner Losh 	off_t 		f_size;		/* Size of file */
114ca987d46SWarner Losh 	daddr_t		f_buf_blkno;	/* block number of data block */
115ca987d46SWarner Losh 	char		*f_buf;		/* buffer for data block */
116ca987d46SWarner Losh 	int		f_susp_skip;	/* len_skip for SUSP records */
117ca987d46SWarner Losh };
118ca987d46SWarner Losh 
119ca987d46SWarner Losh struct ptable_ent {
120ca987d46SWarner Losh 	char namlen	[ISODCL( 1, 1)];	/* 711 */
121ca987d46SWarner Losh 	char extlen	[ISODCL( 2, 2)];	/* 711 */
122ca987d46SWarner Losh 	char block	[ISODCL( 3, 6)];	/* 732 */
123ca987d46SWarner Losh 	char parent	[ISODCL( 7, 8)];	/* 722 */
124ca987d46SWarner Losh 	char name	[1];
125ca987d46SWarner Losh };
126ca987d46SWarner Losh #define	PTFIXSZ		8
127ca987d46SWarner Losh #define	PTSIZE(pp)	roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
128ca987d46SWarner Losh 
129ca987d46SWarner Losh #define	cdb2devb(bno)	((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
130ca987d46SWarner Losh 
131ca987d46SWarner Losh static ISO_SUSP_HEADER *
susp_lookup_record(struct open_file * f,const char * identifier,struct iso_directory_record * dp,int lenskip)132ca987d46SWarner Losh susp_lookup_record(struct open_file *f, const char *identifier,
133ca987d46SWarner Losh     struct iso_directory_record *dp, int lenskip)
134ca987d46SWarner Losh {
135ca987d46SWarner Losh 	static char susp_buffer[ISO_DEFAULT_BLOCK_SIZE];
136ca987d46SWarner Losh 	ISO_SUSP_HEADER *sh;
137ca987d46SWarner Losh 	ISO_RRIP_CONT *shc;
138ca987d46SWarner Losh 	char *p, *end;
139ca987d46SWarner Losh 	int error;
140ca987d46SWarner Losh 	size_t read;
141ca987d46SWarner Losh 
142ca987d46SWarner Losh 	p = dp->name + isonum_711(dp->name_len) + lenskip;
143ca987d46SWarner Losh 	/* Names of even length have a padding byte after the name. */
144ca987d46SWarner Losh 	if ((isonum_711(dp->name_len) & 1) == 0)
145ca987d46SWarner Losh 		p++;
146ca987d46SWarner Losh 	end = (char *)dp + isonum_711(dp->length);
147ca987d46SWarner Losh 	while (p + 3 < end) {
148ca987d46SWarner Losh 		sh = (ISO_SUSP_HEADER *)p;
149ca987d46SWarner Losh 		if (bcmp(sh->type, identifier, 2) == 0)
150ca987d46SWarner Losh 			return (sh);
151ca987d46SWarner Losh 		if (bcmp(sh->type, SUSP_STOP, 2) == 0)
152ca987d46SWarner Losh 			return (NULL);
153ca987d46SWarner Losh 		if (bcmp(sh->type, SUSP_CONTINUATION, 2) == 0) {
154ca987d46SWarner Losh 			shc = (ISO_RRIP_CONT *)sh;
155ca987d46SWarner Losh 			error = f->f_dev->dv_strategy(f->f_devdata, F_READ,
156ca987d46SWarner Losh 			    cdb2devb(isonum_733(shc->location)),
157ca987d46SWarner Losh 			    ISO_DEFAULT_BLOCK_SIZE, susp_buffer, &read);
158ca987d46SWarner Losh 
159ca987d46SWarner Losh 			/* Bail if it fails. */
160ca987d46SWarner Losh 			if (error != 0 || read != ISO_DEFAULT_BLOCK_SIZE)
161ca987d46SWarner Losh 				return (NULL);
162ca987d46SWarner Losh 			p = susp_buffer + isonum_733(shc->offset);
163ca987d46SWarner Losh 			end = p + isonum_733(shc->length);
164ca987d46SWarner Losh 		} else {
165ca987d46SWarner Losh 			/* Ignore this record and skip to the next. */
166ca987d46SWarner Losh 			p += isonum_711(sh->length);
167ca987d46SWarner Losh 
168ca987d46SWarner Losh 			/* Avoid infinite loops with corrupted file systems */
169ca987d46SWarner Losh 			if (isonum_711(sh->length) == 0)
170ca987d46SWarner Losh 				return (NULL);
171ca987d46SWarner Losh 		}
172ca987d46SWarner Losh 	}
173ca987d46SWarner Losh 	return (NULL);
174ca987d46SWarner Losh }
175ca987d46SWarner Losh 
176ca987d46SWarner Losh static char *
rrip_lookup_name(struct open_file * f,struct iso_directory_record * dp,int lenskip,size_t * len)177ca987d46SWarner Losh rrip_lookup_name(struct open_file *f, struct iso_directory_record *dp,
178ca987d46SWarner Losh     int lenskip, size_t *len)
179ca987d46SWarner Losh {
180ca987d46SWarner Losh 	ISO_RRIP_ALTNAME *p;
181ca987d46SWarner Losh 
182ca987d46SWarner Losh 	if (len == NULL)
183ca987d46SWarner Losh 		return (NULL);
184ca987d46SWarner Losh 
185ca987d46SWarner Losh 	p = (ISO_RRIP_ALTNAME *)susp_lookup_record(f, RRIP_NAME, dp, lenskip);
186ca987d46SWarner Losh 	if (p == NULL)
187ca987d46SWarner Losh 		return (NULL);
188ca987d46SWarner Losh 	switch (*p->flags) {
189ca987d46SWarner Losh 	case ISO_SUSP_CFLAG_CURRENT:
190ca987d46SWarner Losh 		*len = 1;
191ca987d46SWarner Losh 		return (".");
192ca987d46SWarner Losh 	case ISO_SUSP_CFLAG_PARENT:
193ca987d46SWarner Losh 		*len = 2;
194ca987d46SWarner Losh 		return ("..");
195ca987d46SWarner Losh 	case 0:
196ca987d46SWarner Losh 		*len = isonum_711(p->h.length) - 5;
197ca987d46SWarner Losh 		return ((char *)p + 5);
198ca987d46SWarner Losh 	default:
199ca987d46SWarner Losh 		/*
200ca987d46SWarner Losh 		 * We don't handle hostnames or continued names as they are
201ca987d46SWarner Losh 		 * too hard, so just bail and use the default name.
202ca987d46SWarner Losh 		 */
203ca987d46SWarner Losh 		return (NULL);
204ca987d46SWarner Losh 	}
205ca987d46SWarner Losh }
206ca987d46SWarner Losh 
207ca987d46SWarner Losh static int
rrip_check(struct open_file * f,struct iso_directory_record * dp,int * lenskip)208ca987d46SWarner Losh rrip_check(struct open_file *f, struct iso_directory_record *dp, int *lenskip)
209ca987d46SWarner Losh {
210ca987d46SWarner Losh 	ISO_SUSP_PRESENT *sp;
211ca987d46SWarner Losh 	ISO_RRIP_EXTREF *er;
212ca987d46SWarner Losh 	char *p;
213ca987d46SWarner Losh 
214ca987d46SWarner Losh 	/* First, see if we can find a SP field. */
215ca987d46SWarner Losh 	p = dp->name + isonum_711(dp->name_len);
216ca987d46SWarner Losh 	if (p > (char *)dp + isonum_711(dp->length))
217ca987d46SWarner Losh 		return (0);
218ca987d46SWarner Losh 	sp = (ISO_SUSP_PRESENT *)p;
219ca987d46SWarner Losh 	if (bcmp(sp->h.type, SUSP_PRESENT, 2) != 0)
220ca987d46SWarner Losh 		return (0);
221ca987d46SWarner Losh 	if (isonum_711(sp->h.length) != sizeof(ISO_SUSP_PRESENT))
222ca987d46SWarner Losh 		return (0);
223ca987d46SWarner Losh 	if (sp->signature[0] != 0xbe || sp->signature[1] != 0xef)
224ca987d46SWarner Losh 		return (0);
225ca987d46SWarner Losh 	*lenskip = isonum_711(sp->len_skp);
226ca987d46SWarner Losh 
227ca987d46SWarner Losh 	/*
228ca987d46SWarner Losh 	 * Now look for an ER field.  If RRIP is present, then there must
229ca987d46SWarner Losh 	 * be at least one of these.  It would be more pedantic to walk
230ca987d46SWarner Losh 	 * through the list of fields looking for a Rock Ridge ER field.
231ca987d46SWarner Losh 	 */
232ca987d46SWarner Losh 	er = (ISO_RRIP_EXTREF *)susp_lookup_record(f, SUSP_EXTREF, dp, 0);
233ca987d46SWarner Losh 	if (er == NULL)
234ca987d46SWarner Losh 		return (0);
235ca987d46SWarner Losh 	return (1);
236ca987d46SWarner Losh }
237ca987d46SWarner Losh 
238ca987d46SWarner Losh static int
dirmatch(struct open_file * f,const char * path,struct iso_directory_record * dp,int use_rrip,int lenskip)239ca987d46SWarner Losh dirmatch(struct open_file *f, const char *path, struct iso_directory_record *dp,
240ca987d46SWarner Losh     int use_rrip, int lenskip)
241ca987d46SWarner Losh {
2426cea60aeSToomas Soome 	size_t len, plen;
2436cea60aeSToomas Soome 	char *cp, *sep;
244ca987d46SWarner Losh 	int i, icase;
245ca987d46SWarner Losh 
246ca987d46SWarner Losh 	if (use_rrip)
247ca987d46SWarner Losh 		cp = rrip_lookup_name(f, dp, lenskip, &len);
248ca987d46SWarner Losh 	else
249ca987d46SWarner Losh 		cp = NULL;
250ca987d46SWarner Losh 	if (cp == NULL) {
251ca987d46SWarner Losh 		len = isonum_711(dp->name_len);
252ca987d46SWarner Losh 		cp = dp->name;
253ca987d46SWarner Losh 		icase = 1;
254ca987d46SWarner Losh 	} else
255ca987d46SWarner Losh 		icase = 0;
25661250f78SToomas Soome 
2576cea60aeSToomas Soome 	sep = strchr(path, '/');
2586cea60aeSToomas Soome 	if (sep != NULL) {
2596cea60aeSToomas Soome 		plen = sep - path;
2606cea60aeSToomas Soome 	} else {
2616cea60aeSToomas Soome 		plen = strlen(path);
2626cea60aeSToomas Soome 	}
2636cea60aeSToomas Soome 
2646cea60aeSToomas Soome 	if (plen != len)
26561250f78SToomas Soome 		return (0);
26661250f78SToomas Soome 
267ca987d46SWarner Losh 	for (i = len; --i >= 0; path++, cp++) {
268ca987d46SWarner Losh 		if (!*path || *path == '/')
269ca987d46SWarner Losh 			break;
270ca987d46SWarner Losh 		if (*path == *cp)
271ca987d46SWarner Losh 			continue;
272ca987d46SWarner Losh 		if (!icase && toupper(*path) == *cp)
273ca987d46SWarner Losh 			continue;
274ca987d46SWarner Losh 		return 0;
275ca987d46SWarner Losh 	}
276ca987d46SWarner Losh 	if (*path && *path != '/')
277ca987d46SWarner Losh 		return 0;
278ca987d46SWarner Losh 	/*
279ca987d46SWarner Losh 	 * Allow stripping of trailing dots and the version number.
280ca987d46SWarner Losh 	 * Note that this will find the first instead of the last version
281ca987d46SWarner Losh 	 * of a file.
282ca987d46SWarner Losh 	 */
283ca987d46SWarner Losh 	if (i >= 0 && (*cp == ';' || *cp == '.')) {
284ca987d46SWarner Losh 		/* This is to prevent matching of numeric extensions */
285ca987d46SWarner Losh 		if (*cp == '.' && cp[1] != ';')
286ca987d46SWarner Losh 			return 0;
287ca987d46SWarner Losh 		while (--i >= 0)
288ca987d46SWarner Losh 			if (*++cp != ';' && (*cp < '0' || *cp > '9'))
289ca987d46SWarner Losh 				return 0;
290ca987d46SWarner Losh 	}
291ca987d46SWarner Losh 	return 1;
292ca987d46SWarner Losh }
293ca987d46SWarner Losh 
294ca987d46SWarner Losh static int
cd9660_read_dr(struct open_file * f,struct iso_directory_record * rec)295b4cb3fe0SToomas Soome cd9660_read_dr(struct open_file *f, struct iso_directory_record *rec)
296ca987d46SWarner Losh {
297ca987d46SWarner Losh 	struct iso_primary_descriptor *vd;
298b4cb3fe0SToomas Soome 	size_t read;
299b4cb3fe0SToomas Soome 	daddr_t bno;
300b4cb3fe0SToomas Soome 	int rc;
301ca987d46SWarner Losh 
302b4cb3fe0SToomas Soome 	errno = 0;
303b4cb3fe0SToomas Soome 	vd = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
3048ac66965SToomas Soome 	    sizeof(struct iso_primary_descriptor)));
305b4cb3fe0SToomas Soome 	if (vd == NULL)
306b4cb3fe0SToomas Soome 		return (errno);
307b4cb3fe0SToomas Soome 
308ca987d46SWarner Losh 	for (bno = 16;; bno++) {
309ca987d46SWarner Losh 		twiddle(1);
310ca987d46SWarner Losh 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
311b4cb3fe0SToomas Soome 		    ISO_DEFAULT_BLOCK_SIZE, (char *)vd, &read);
312ca987d46SWarner Losh 		if (rc)
313ca987d46SWarner Losh 			goto out;
314ca987d46SWarner Losh 		if (read != ISO_DEFAULT_BLOCK_SIZE) {
315ca987d46SWarner Losh 			rc = EIO;
316ca987d46SWarner Losh 			goto out;
317ca987d46SWarner Losh 		}
318ca987d46SWarner Losh 		rc = EINVAL;
319b4cb3fe0SToomas Soome 		if (bcmp(vd->id, ISO_STANDARD_ID, sizeof(vd->id)) != 0)
320ca987d46SWarner Losh 			goto out;
321ca987d46SWarner Losh 		if (isonum_711(vd->type) == ISO_VD_END)
322ca987d46SWarner Losh 			goto out;
323ca987d46SWarner Losh 		if (isonum_711(vd->type) == ISO_VD_PRIMARY)
324ca987d46SWarner Losh 			break;
325ca987d46SWarner Losh 	}
326b4cb3fe0SToomas Soome 	if (isonum_723(vd->logical_block_size) == ISO_DEFAULT_BLOCK_SIZE) {
327b4cb3fe0SToomas Soome 		bcopy(vd->root_directory_record, rec, sizeof(*rec));
328b4cb3fe0SToomas Soome 		rc = 0;
329b4cb3fe0SToomas Soome 	}
330b4cb3fe0SToomas Soome out:
331b4cb3fe0SToomas Soome 	free(vd);
332b4cb3fe0SToomas Soome 	return (rc);
333b4cb3fe0SToomas Soome }
334b4cb3fe0SToomas Soome 
335b4cb3fe0SToomas Soome static int
cd9660_open(const char * path,struct open_file * f)336b4cb3fe0SToomas Soome cd9660_open(const char *path, struct open_file *f)
337b4cb3fe0SToomas Soome {
338b4cb3fe0SToomas Soome 	struct file *fp = NULL;
339b4cb3fe0SToomas Soome 	void *buf;
340b4cb3fe0SToomas Soome 	size_t read, dsize, off;
341b4cb3fe0SToomas Soome 	daddr_t bno, boff;
342b4cb3fe0SToomas Soome 	struct iso_directory_record rec;
343b4cb3fe0SToomas Soome 	struct iso_directory_record *dp = NULL;
344b4cb3fe0SToomas Soome 	int rc, first, use_rrip, lenskip;
345b4cb3fe0SToomas Soome 	bool isdir = false;
346b4cb3fe0SToomas Soome 	struct devdesc *dev;
347b4cb3fe0SToomas Soome 	cd9660_mnt_t *mnt;
348b4cb3fe0SToomas Soome 
349b4cb3fe0SToomas Soome 	/* First find the volume descriptor */
350b4cb3fe0SToomas Soome 	errno = 0;
351b4cb3fe0SToomas Soome 	buf = malloc(MAX(ISO_DEFAULT_BLOCK_SIZE,
352b4cb3fe0SToomas Soome 	    sizeof(struct iso_primary_descriptor)));
353b4cb3fe0SToomas Soome 	if (buf == NULL)
354b4cb3fe0SToomas Soome 		return (errno);
355b4cb3fe0SToomas Soome 
356b4cb3fe0SToomas Soome 	dev = f->f_devdata;
357b4cb3fe0SToomas Soome 	STAILQ_FOREACH(mnt, &mnt_list, cd_link) {
358b4cb3fe0SToomas Soome 		if (dev->d_dev->dv_type == mnt->cd_dev->d_dev->dv_type &&
359b4cb3fe0SToomas Soome 		    dev->d_unit == mnt->cd_dev->d_unit)
360b4cb3fe0SToomas Soome 			break;
361b4cb3fe0SToomas Soome 	}
362b4cb3fe0SToomas Soome 
363b4cb3fe0SToomas Soome 	rc = 0;
364b4cb3fe0SToomas Soome 	if (mnt == NULL)
365b4cb3fe0SToomas Soome 		rc = cd9660_read_dr(f, &rec);
366b4cb3fe0SToomas Soome 	else
367b4cb3fe0SToomas Soome 		rec = mnt->cd_rec;
368b4cb3fe0SToomas Soome 
369b4cb3fe0SToomas Soome 	if (rc != 0)
370ca987d46SWarner Losh 		goto out;
371ca987d46SWarner Losh 
372b4cb3fe0SToomas Soome 	if (*path == '/')
373b4cb3fe0SToomas Soome 		path++; /* eat leading '/' */
374ca987d46SWarner Losh 
375ca987d46SWarner Losh 	first = 1;
376ca987d46SWarner Losh 	use_rrip = 0;
377213f235fSToomas Soome 	lenskip = 0;
378ca987d46SWarner Losh 	while (*path) {
379ca987d46SWarner Losh 		bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
380ca987d46SWarner Losh 		dsize = isonum_733(rec.size);
381ca987d46SWarner Losh 		off = 0;
382ca987d46SWarner Losh 		boff = 0;
383ca987d46SWarner Losh 
384ca987d46SWarner Losh 		while (off < dsize) {
385ca987d46SWarner Losh 			if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
386ca987d46SWarner Losh 				twiddle(1);
387ca987d46SWarner Losh 				rc = f->f_dev->dv_strategy
388ca987d46SWarner Losh 					(f->f_devdata, F_READ,
389ca987d46SWarner Losh 					 cdb2devb(bno + boff),
390ca987d46SWarner Losh 					 ISO_DEFAULT_BLOCK_SIZE,
391ca987d46SWarner Losh 					 buf, &read);
392ca987d46SWarner Losh 				if (rc)
393ca987d46SWarner Losh 					goto out;
394ca987d46SWarner Losh 				if (read != ISO_DEFAULT_BLOCK_SIZE) {
395ca987d46SWarner Losh 					rc = EIO;
396ca987d46SWarner Losh 					goto out;
397ca987d46SWarner Losh 				}
398ca987d46SWarner Losh 				boff++;
399ca987d46SWarner Losh 				dp = (struct iso_directory_record *) buf;
400ca987d46SWarner Losh 			}
401ca987d46SWarner Losh 			if (isonum_711(dp->length) == 0) {
402ca987d46SWarner Losh 			    /* skip to next block, if any */
403ca987d46SWarner Losh 			    off = boff * ISO_DEFAULT_BLOCK_SIZE;
404ca987d46SWarner Losh 			    continue;
405ca987d46SWarner Losh 			}
406ca987d46SWarner Losh 
407ca987d46SWarner Losh 			/* See if RRIP is in use. */
408ca987d46SWarner Losh 			if (first)
409ca987d46SWarner Losh 				use_rrip = rrip_check(f, dp, &lenskip);
410ca987d46SWarner Losh 
411ca987d46SWarner Losh 			if (dirmatch(f, path, dp, use_rrip,
412ca987d46SWarner Losh 			    first ? 0 : lenskip)) {
413ca987d46SWarner Losh 				first = 0;
414ca987d46SWarner Losh 				break;
415ca987d46SWarner Losh 			} else
416ca987d46SWarner Losh 				first = 0;
417ca987d46SWarner Losh 
418ca987d46SWarner Losh 			dp = (struct iso_directory_record *)
419ca987d46SWarner Losh 				((char *) dp + isonum_711(dp->length));
420ca987d46SWarner Losh 			/* If the new block has zero length, it is padding. */
421ca987d46SWarner Losh 			if (isonum_711(dp->length) == 0) {
422ca987d46SWarner Losh 				/* Skip to next block, if any. */
423ca987d46SWarner Losh 				off = boff * ISO_DEFAULT_BLOCK_SIZE;
424ca987d46SWarner Losh 				continue;
425ca987d46SWarner Losh 			}
426ca987d46SWarner Losh 			off += isonum_711(dp->length);
427ca987d46SWarner Losh 		}
428ca987d46SWarner Losh 		if (off >= dsize) {
429ca987d46SWarner Losh 			rc = ENOENT;
430ca987d46SWarner Losh 			goto out;
431ca987d46SWarner Losh 		}
432ca987d46SWarner Losh 
433ca987d46SWarner Losh 		rec = *dp;
434ca987d46SWarner Losh 		while (*path && *path != '/') /* look for next component */
435ca987d46SWarner Losh 			path++;
4366cea60aeSToomas Soome 
4376cea60aeSToomas Soome 		if (*path)	/* this component was directory */
4386cea60aeSToomas Soome 			isdir = true;
4396cea60aeSToomas Soome 
4406cea60aeSToomas Soome 		while (*path == '/')
4416cea60aeSToomas Soome 			path++;	/* skip '/' */
4426cea60aeSToomas Soome 
4436cea60aeSToomas Soome 		if (*path)	/* We do have next component. */
4446cea60aeSToomas Soome 			isdir = false;
4456cea60aeSToomas Soome 	}
4466cea60aeSToomas Soome 
4476cea60aeSToomas Soome 	/*
4486cea60aeSToomas Soome 	 * if the path had trailing / but the path does point to file,
4496cea60aeSToomas Soome 	 * report the error ENOTDIR.
4506cea60aeSToomas Soome 	 */
4516cea60aeSToomas Soome 	if (isdir == true && (isonum_711(rec.flags) & 2) == 0) {
4526cea60aeSToomas Soome 		rc = ENOTDIR;
4536cea60aeSToomas Soome 		goto out;
454ca987d46SWarner Losh 	}
455ca987d46SWarner Losh 
456ca987d46SWarner Losh 	/* allocate file system specific data structure */
457ca987d46SWarner Losh 	fp = malloc(sizeof(struct file));
458ca987d46SWarner Losh 	bzero(fp, sizeof(struct file));
459ca987d46SWarner Losh 	f->f_fsdata = (void *)fp;
460ca987d46SWarner Losh 
461ca987d46SWarner Losh 	if ((isonum_711(rec.flags) & 2) != 0) {
462ca987d46SWarner Losh 		fp->f_flags = F_ISDIR;
463ca987d46SWarner Losh 	}
464ca987d46SWarner Losh 	if (first) {
465ca987d46SWarner Losh 		fp->f_flags |= F_ROOTDIR;
466ca987d46SWarner Losh 
467ca987d46SWarner Losh 		/* Check for Rock Ridge since we didn't in the loop above. */
468ca987d46SWarner Losh 		bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
469ca987d46SWarner Losh 		twiddle(1);
470ca987d46SWarner Losh 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
471ca987d46SWarner Losh 		    ISO_DEFAULT_BLOCK_SIZE, buf, &read);
472ca987d46SWarner Losh 		if (rc)
473ca987d46SWarner Losh 			goto out;
474ca987d46SWarner Losh 		if (read != ISO_DEFAULT_BLOCK_SIZE) {
475ca987d46SWarner Losh 			rc = EIO;
476ca987d46SWarner Losh 			goto out;
477ca987d46SWarner Losh 		}
478ca987d46SWarner Losh 		dp = (struct iso_directory_record *)buf;
479ca987d46SWarner Losh 		use_rrip = rrip_check(f, dp, &lenskip);
480ca987d46SWarner Losh 	}
481ca987d46SWarner Losh 	if (use_rrip) {
482ca987d46SWarner Losh 		fp->f_flags |= F_RR;
483ca987d46SWarner Losh 		fp->f_susp_skip = lenskip;
484ca987d46SWarner Losh 	}
485ca987d46SWarner Losh 	fp->f_off = 0;
486ca987d46SWarner Losh 	fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
487ca987d46SWarner Losh 	fp->f_size = isonum_733(rec.size);
488ca987d46SWarner Losh 	free(buf);
489ca987d46SWarner Losh 
490ca987d46SWarner Losh 	return 0;
491ca987d46SWarner Losh 
492ca987d46SWarner Losh out:
493ca987d46SWarner Losh 	free(fp);
494ca987d46SWarner Losh 	free(buf);
495ca987d46SWarner Losh 
496ca987d46SWarner Losh 	return rc;
497ca987d46SWarner Losh }
498ca987d46SWarner Losh 
499ca987d46SWarner Losh static int
cd9660_close(struct open_file * f)500ca987d46SWarner Losh cd9660_close(struct open_file *f)
501ca987d46SWarner Losh {
502ca987d46SWarner Losh 	struct file *fp = (struct file *)f->f_fsdata;
503ca987d46SWarner Losh 
504ca987d46SWarner Losh 	f->f_fsdata = NULL;
505ca987d46SWarner Losh 	free(fp);
506ca987d46SWarner Losh 
507ca987d46SWarner Losh 	return 0;
508ca987d46SWarner Losh }
509ca987d46SWarner Losh 
510ca987d46SWarner Losh static int
buf_read_file(struct open_file * f,char ** buf_p,size_t * size_p)511ca987d46SWarner Losh buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
512ca987d46SWarner Losh {
513ca987d46SWarner Losh 	struct file *fp = (struct file *)f->f_fsdata;
514ca987d46SWarner Losh 	daddr_t blkno, blkoff;
515ca987d46SWarner Losh 	int rc = 0;
516ca987d46SWarner Losh 	size_t read;
517ca987d46SWarner Losh 
518ca987d46SWarner Losh 	blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
519ca987d46SWarner Losh 	blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
520ca987d46SWarner Losh 
521ca987d46SWarner Losh 	if (blkno != fp->f_buf_blkno) {
522ca987d46SWarner Losh 		if (fp->f_buf == (char *)0)
523ca987d46SWarner Losh 			fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
524ca987d46SWarner Losh 
525ca987d46SWarner Losh 		twiddle(16);
526ca987d46SWarner Losh 		rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
527ca987d46SWarner Losh 		    cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE,
528ca987d46SWarner Losh 		    fp->f_buf, &read);
529ca987d46SWarner Losh 		if (rc)
530ca987d46SWarner Losh 			return (rc);
531ca987d46SWarner Losh 		if (read != ISO_DEFAULT_BLOCK_SIZE)
532ca987d46SWarner Losh 			return (EIO);
533ca987d46SWarner Losh 
534ca987d46SWarner Losh 		fp->f_buf_blkno = blkno;
535ca987d46SWarner Losh 	}
536ca987d46SWarner Losh 
537ca987d46SWarner Losh 	*buf_p = fp->f_buf + blkoff;
538ca987d46SWarner Losh 	*size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
539ca987d46SWarner Losh 
540ca987d46SWarner Losh 	if (*size_p > fp->f_size - fp->f_off)
541ca987d46SWarner Losh 		*size_p = fp->f_size - fp->f_off;
542ca987d46SWarner Losh 	return (rc);
543ca987d46SWarner Losh }
544ca987d46SWarner Losh 
545ca987d46SWarner Losh static int
cd9660_read(struct open_file * f,void * start,size_t size,size_t * resid)546ca987d46SWarner Losh cd9660_read(struct open_file *f, void *start, size_t size, size_t *resid)
547ca987d46SWarner Losh {
548ca987d46SWarner Losh 	struct file *fp = (struct file *)f->f_fsdata;
549ca987d46SWarner Losh 	char *buf, *addr;
550ca987d46SWarner Losh 	size_t buf_size, csize;
551ca987d46SWarner Losh 	int rc = 0;
552ca987d46SWarner Losh 
553ca987d46SWarner Losh 	addr = start;
554ca987d46SWarner Losh 	while (size) {
555ca987d46SWarner Losh 		if (fp->f_off < 0 || fp->f_off >= fp->f_size)
556ca987d46SWarner Losh 			break;
557ca987d46SWarner Losh 
558ca987d46SWarner Losh 		rc = buf_read_file(f, &buf, &buf_size);
559ca987d46SWarner Losh 		if (rc)
560ca987d46SWarner Losh 			break;
561ca987d46SWarner Losh 
562ca987d46SWarner Losh 		csize = size > buf_size ? buf_size : size;
563ca987d46SWarner Losh 		bcopy(buf, addr, csize);
564ca987d46SWarner Losh 
565ca987d46SWarner Losh 		fp->f_off += csize;
566ca987d46SWarner Losh 		addr += csize;
567ca987d46SWarner Losh 		size -= csize;
568ca987d46SWarner Losh 	}
569ca987d46SWarner Losh 	if (resid)
570ca987d46SWarner Losh 		*resid = size;
571ca987d46SWarner Losh 	return (rc);
572ca987d46SWarner Losh }
573ca987d46SWarner Losh 
574ca987d46SWarner Losh static int
cd9660_readdir(struct open_file * f,struct dirent * d)575ca987d46SWarner Losh cd9660_readdir(struct open_file *f, struct dirent *d)
576ca987d46SWarner Losh {
577ca987d46SWarner Losh 	struct file *fp = (struct file *)f->f_fsdata;
578ca987d46SWarner Losh 	struct iso_directory_record *ep;
579ca987d46SWarner Losh 	size_t buf_size, reclen, namelen;
580ca987d46SWarner Losh 	int error = 0;
581ca987d46SWarner Losh 	int lenskip;
582ca987d46SWarner Losh 	char *buf, *name;
583ca987d46SWarner Losh 
584ca987d46SWarner Losh again:
585ca987d46SWarner Losh 	if (fp->f_off >= fp->f_size)
586ca987d46SWarner Losh 		return (ENOENT);
587ca987d46SWarner Losh 	error = buf_read_file(f, &buf, &buf_size);
588ca987d46SWarner Losh 	if (error)
589ca987d46SWarner Losh 		return (error);
590ca987d46SWarner Losh 	ep = (struct iso_directory_record *)buf;
591ca987d46SWarner Losh 
592ca987d46SWarner Losh 	if (isonum_711(ep->length) == 0) {
593ca987d46SWarner Losh 		daddr_t blkno;
594ca987d46SWarner Losh 
595ca987d46SWarner Losh 		/* skip to next block, if any */
596ca987d46SWarner Losh 		blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
597ca987d46SWarner Losh 		fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
598ca987d46SWarner Losh 		goto again;
599ca987d46SWarner Losh 	}
600ca987d46SWarner Losh 
601ca987d46SWarner Losh 	if (fp->f_flags & F_RR) {
602ca987d46SWarner Losh 		if (fp->f_flags & F_ROOTDIR && fp->f_off == 0)
603ca987d46SWarner Losh 			lenskip = 0;
604ca987d46SWarner Losh 		else
605ca987d46SWarner Losh 			lenskip = fp->f_susp_skip;
606ca987d46SWarner Losh 		name = rrip_lookup_name(f, ep, lenskip, &namelen);
607ca987d46SWarner Losh 	} else
608ca987d46SWarner Losh 		name = NULL;
609ca987d46SWarner Losh 	if (name == NULL) {
610ca987d46SWarner Losh 		namelen = isonum_711(ep->name_len);
611ca987d46SWarner Losh 		name = ep->name;
612ca987d46SWarner Losh 		if (namelen == 1) {
613ca987d46SWarner Losh 			if (ep->name[0] == 0)
614ca987d46SWarner Losh 				name = ".";
615ca987d46SWarner Losh 			else if (ep->name[0] == 1) {
616ca987d46SWarner Losh 				namelen = 2;
617ca987d46SWarner Losh 				name = "..";
618ca987d46SWarner Losh 			}
619ca987d46SWarner Losh 		}
620ca987d46SWarner Losh 	}
621ca987d46SWarner Losh 	reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
622ca987d46SWarner Losh 	reclen = (reclen + 3) & ~3;
623ca987d46SWarner Losh 
624ca987d46SWarner Losh 	d->d_fileno = isonum_733(ep->extent);
625ca987d46SWarner Losh 	d->d_reclen = reclen;
626ca987d46SWarner Losh 	if (isonum_711(ep->flags) & 2)
627ca987d46SWarner Losh 		d->d_type = DT_DIR;
628ca987d46SWarner Losh 	else
629ca987d46SWarner Losh 		d->d_type = DT_REG;
630ca987d46SWarner Losh 	d->d_namlen = namelen;
631ca987d46SWarner Losh 
632ca987d46SWarner Losh 	bcopy(name, d->d_name, d->d_namlen);
633ca987d46SWarner Losh 	d->d_name[d->d_namlen] = 0;
634ca987d46SWarner Losh 
635ca987d46SWarner Losh 	fp->f_off += isonum_711(ep->length);
636ca987d46SWarner Losh 	return (0);
637ca987d46SWarner Losh }
638ca987d46SWarner Losh 
639ca987d46SWarner Losh static off_t
cd9660_seek(struct open_file * f,off_t offset,int where)640ca987d46SWarner Losh cd9660_seek(struct open_file *f, off_t offset, int where)
641ca987d46SWarner Losh {
642ca987d46SWarner Losh 	struct file *fp = (struct file *)f->f_fsdata;
643ca987d46SWarner Losh 
644ca987d46SWarner Losh 	switch (where) {
645ca987d46SWarner Losh 	case SEEK_SET:
646ca987d46SWarner Losh 		fp->f_off = offset;
647ca987d46SWarner Losh 		break;
648ca987d46SWarner Losh 	case SEEK_CUR:
649ca987d46SWarner Losh 		fp->f_off += offset;
650ca987d46SWarner Losh 		break;
651ca987d46SWarner Losh 	case SEEK_END:
652ca987d46SWarner Losh 		fp->f_off = fp->f_size - offset;
653ca987d46SWarner Losh 		break;
654ca987d46SWarner Losh 	default:
655ca987d46SWarner Losh 		return -1;
656ca987d46SWarner Losh 	}
657ca987d46SWarner Losh 	return fp->f_off;
658ca987d46SWarner Losh }
659ca987d46SWarner Losh 
660ca987d46SWarner Losh static int
cd9660_stat(struct open_file * f,struct stat * sb)661ca987d46SWarner Losh cd9660_stat(struct open_file *f, struct stat *sb)
662ca987d46SWarner Losh {
663ca987d46SWarner Losh 	struct file *fp = (struct file *)f->f_fsdata;
664ca987d46SWarner Losh 
665ca987d46SWarner Losh 	/* only important stuff */
666ca987d46SWarner Losh 	sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
667ca987d46SWarner Losh 	if (fp->f_flags & F_ISDIR)
668ca987d46SWarner Losh 		sb->st_mode |= S_IFDIR;
669ca987d46SWarner Losh 	else
670ca987d46SWarner Losh 		sb->st_mode |= S_IFREG;
671ca987d46SWarner Losh 	sb->st_uid = sb->st_gid = 0;
672ca987d46SWarner Losh 	sb->st_size = fp->f_size;
673ca987d46SWarner Losh 	return 0;
674ca987d46SWarner Losh }
675b4cb3fe0SToomas Soome 
676b4cb3fe0SToomas Soome static int
cd9660_mount(const char * dev,const char * path,void ** data)677b4cb3fe0SToomas Soome cd9660_mount(const char *dev, const char *path, void **data)
678b4cb3fe0SToomas Soome {
679b4cb3fe0SToomas Soome 	cd9660_mnt_t *mnt;
680b4cb3fe0SToomas Soome 	struct open_file *f;
681b4cb3fe0SToomas Soome 	char *fs;
682b4cb3fe0SToomas Soome 
683b4cb3fe0SToomas Soome 	errno = 0;
684b4cb3fe0SToomas Soome 	mnt = calloc(1, sizeof(*mnt));
685b4cb3fe0SToomas Soome 	if (mnt == NULL)
686b4cb3fe0SToomas Soome 		return (errno);
687b4cb3fe0SToomas Soome 	mnt->cd_fd = -1;
688b4cb3fe0SToomas Soome 
689b4cb3fe0SToomas Soome 	if (asprintf(&fs, "%s%s", dev, path) < 0)
690b4cb3fe0SToomas Soome 		goto done;
691b4cb3fe0SToomas Soome 
692b4cb3fe0SToomas Soome 	mnt->cd_fd = open(fs, O_RDONLY);
693b4cb3fe0SToomas Soome 	free(fs);
694b4cb3fe0SToomas Soome 	if (mnt->cd_fd == -1)
695b4cb3fe0SToomas Soome 		goto done;
696b4cb3fe0SToomas Soome 
697b4cb3fe0SToomas Soome 	f = fd2open_file(mnt->cd_fd);
698b4cb3fe0SToomas Soome 	/* Is it cd9660 file system? */
699b4cb3fe0SToomas Soome 	if (strcmp(f->f_ops->fs_name, "cd9660") == 0) {
700b4cb3fe0SToomas Soome 		mnt->cd_dev = f->f_devdata;
701b4cb3fe0SToomas Soome 		errno = cd9660_read_dr(f, &mnt->cd_rec);
702b4cb3fe0SToomas Soome 		STAILQ_INSERT_TAIL(&mnt_list, mnt, cd_link);
703b4cb3fe0SToomas Soome 	} else {
704b4cb3fe0SToomas Soome 		errno = ENXIO;
705b4cb3fe0SToomas Soome 	}
706b4cb3fe0SToomas Soome 
707b4cb3fe0SToomas Soome done:
708b4cb3fe0SToomas Soome 	if (errno != 0) {
709b4cb3fe0SToomas Soome 		free(mnt->cd_dev);
710b4cb3fe0SToomas Soome 		if (mnt->cd_fd >= 0)
711b4cb3fe0SToomas Soome 			close(mnt->cd_fd);
712b4cb3fe0SToomas Soome 		free(mnt);
713b4cb3fe0SToomas Soome 	} else {
714b4cb3fe0SToomas Soome 		*data = mnt;
715b4cb3fe0SToomas Soome 	}
716b4cb3fe0SToomas Soome 	return (errno);
717b4cb3fe0SToomas Soome }
718b4cb3fe0SToomas Soome 
719b4cb3fe0SToomas Soome static int
cd9660_unmount(const char * dev __unused,void * data)720b4cb3fe0SToomas Soome cd9660_unmount(const char *dev __unused, void *data)
721b4cb3fe0SToomas Soome {
722b4cb3fe0SToomas Soome 	cd9660_mnt_t *mnt = data;
723b4cb3fe0SToomas Soome 
724b4cb3fe0SToomas Soome 	STAILQ_REMOVE(&mnt_list, mnt, cd9660_mnt, cd_link);
725b4cb3fe0SToomas Soome 	close(mnt->cd_fd);
726b4cb3fe0SToomas Soome 	free(mnt);
727b4cb3fe0SToomas Soome 	return (0);
728b4cb3fe0SToomas Soome }
729