xref: /illumos-gate/usr/src/boot/libsa/open.c (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1*22028508SToomas Soome /*	$NetBSD: open.c,v 1.16 1997/01/28 09:41:03 pk Exp $	*/
2*22028508SToomas Soome 
3*22028508SToomas Soome /*
4*22028508SToomas Soome  * Copyright (c) 1993
5*22028508SToomas Soome  *	The Regents of the University of California.  All rights reserved.
6*22028508SToomas Soome  *
7*22028508SToomas Soome  * This code is derived from software contributed to Berkeley by
8*22028508SToomas Soome  * The Mach Operating System project at Carnegie-Mellon University.
9*22028508SToomas Soome  *
10*22028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
11*22028508SToomas Soome  * modification, are permitted provided that the following conditions
12*22028508SToomas Soome  * are met:
13*22028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15*22028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17*22028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
18*22028508SToomas Soome  * 3. Neither the name of the University nor the names of its contributors
19*22028508SToomas Soome  *    may be used to endorse or promote products derived from this software
20*22028508SToomas Soome  *    without specific prior written permission.
21*22028508SToomas Soome  *
22*22028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*22028508SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*22028508SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*22028508SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*22028508SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*22028508SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*22028508SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*22028508SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*22028508SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*22028508SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*22028508SToomas Soome  * SUCH DAMAGE.
33*22028508SToomas Soome  *
34*22028508SToomas Soome  *	@(#)open.c	8.1 (Berkeley) 6/11/93
35*22028508SToomas Soome  *
36*22028508SToomas Soome  *
37*22028508SToomas Soome  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38*22028508SToomas Soome  * All Rights Reserved.
39*22028508SToomas Soome  *
40*22028508SToomas Soome  * Author: Alessandro Forin
41*22028508SToomas Soome  *
42*22028508SToomas Soome  * Permission to use, copy, modify and distribute this software and its
43*22028508SToomas Soome  * documentation is hereby granted, provided that both the copyright
44*22028508SToomas Soome  * notice and this permission notice appear in all copies of the
45*22028508SToomas Soome  * software, derivative works or modified versions, and any portions
46*22028508SToomas Soome  * thereof, and that both notices appear in supporting documentation.
47*22028508SToomas Soome  *
48*22028508SToomas Soome  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49*22028508SToomas Soome  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50*22028508SToomas Soome  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51*22028508SToomas Soome  *
52*22028508SToomas Soome  * Carnegie Mellon requests users of this software to return to
53*22028508SToomas Soome  *
54*22028508SToomas Soome  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55*22028508SToomas Soome  *  School of Computer Science
56*22028508SToomas Soome  *  Carnegie Mellon University
57*22028508SToomas Soome  *  Pittsburgh PA 15213-3890
58*22028508SToomas Soome  *
59*22028508SToomas Soome  * any improvements or extensions that they make and grant Carnegie the
60*22028508SToomas Soome  * rights to redistribute these changes.
61*22028508SToomas Soome  */
62*22028508SToomas Soome 
63*22028508SToomas Soome #include <sys/cdefs.h>
64*22028508SToomas Soome 
65*22028508SToomas Soome #include "stand.h"
66*22028508SToomas Soome 
67*22028508SToomas Soome struct fs_ops *exclusive_file_system;
68*22028508SToomas Soome 
69*22028508SToomas Soome /*
70*22028508SToomas Soome  * Open file list. The current implementation and assumption is,
71*22028508SToomas Soome  * we only remove entries from tail and we only add new entries to tail.
72*22028508SToomas Soome  * This decision is to keep file id management simple - we get list
73*22028508SToomas Soome  * entries ordered by continiously growing f_id field.
74*22028508SToomas Soome  * If we do have multiple files open and we do close file not from tail,
75*22028508SToomas Soome  * this entry will be marked unused. open() will reuse unused entry, or
76*22028508SToomas Soome  * close will free all unused tail entries.
77*22028508SToomas Soome  *
78*22028508SToomas Soome  * Only case we expect open file list to grow long, is with zfs pools with
79*22028508SToomas Soome  * many disks.
80*22028508SToomas Soome  */
81*22028508SToomas Soome file_list_t files = TAILQ_HEAD_INITIALIZER(files);
82*22028508SToomas Soome 
83*22028508SToomas Soome /*
84*22028508SToomas Soome  * Walk file list and return pointer to open_file structure.
85*22028508SToomas Soome  * if fd is < 0, return first unused open_file.
86*22028508SToomas Soome  */
87*22028508SToomas Soome struct open_file *
fd2open_file(int fd)88*22028508SToomas Soome fd2open_file(int fd)
89*22028508SToomas Soome {
90*22028508SToomas Soome 	struct open_file *f;
91*22028508SToomas Soome 
92*22028508SToomas Soome 	TAILQ_FOREACH(f, &files, f_link) {
93*22028508SToomas Soome 		if (fd >= 0) {
94*22028508SToomas Soome 			if (f->f_id == fd)
95*22028508SToomas Soome 				break;
96*22028508SToomas Soome 			continue;
97*22028508SToomas Soome 		}
98*22028508SToomas Soome 		if (f->f_flags == 0)
99*22028508SToomas Soome 			break;
100*22028508SToomas Soome 	}
101*22028508SToomas Soome 	return (f);
102*22028508SToomas Soome }
103*22028508SToomas Soome 
104*22028508SToomas Soome static int
o_gethandle(struct open_file ** ptr)105*22028508SToomas Soome o_gethandle(struct open_file **ptr)
106*22028508SToomas Soome {
107*22028508SToomas Soome 	struct open_file *f, *last;
108*22028508SToomas Soome 
109*22028508SToomas Soome 	/* Pick up unused entry */
110*22028508SToomas Soome 	f = fd2open_file(-1);
111*22028508SToomas Soome 	if (f != NULL) {
112*22028508SToomas Soome 		*ptr = f;
113*22028508SToomas Soome 		return (f->f_id);
114*22028508SToomas Soome 	}
115*22028508SToomas Soome 
116*22028508SToomas Soome 	/* Add new entry */
117*22028508SToomas Soome 	f = calloc(1, sizeof (*f));
118*22028508SToomas Soome 	if (f == NULL)
119*22028508SToomas Soome 		return (-1);
120*22028508SToomas Soome 
121*22028508SToomas Soome 	last = TAILQ_LAST(&files, file_list);
122*22028508SToomas Soome 	if (last != NULL)
123*22028508SToomas Soome 		f->f_id = last->f_id + 1;
124*22028508SToomas Soome 	TAILQ_INSERT_TAIL(&files, f, f_link);
125*22028508SToomas Soome 
126*22028508SToomas Soome 	*ptr = f;
127*22028508SToomas Soome 	return (f->f_id);
128*22028508SToomas Soome }
129*22028508SToomas Soome 
130*22028508SToomas Soome static void
o_rainit(struct open_file * f)131*22028508SToomas Soome o_rainit(struct open_file *f)
132*22028508SToomas Soome {
133*22028508SToomas Soome 	f->f_rabuf = malloc(SOPEN_RASIZE);
134*22028508SToomas Soome 	f->f_ralen = 0;
135*22028508SToomas Soome 	f->f_raoffset = 0;
136*22028508SToomas Soome }
137*22028508SToomas Soome 
138*22028508SToomas Soome int
open(const char * fname,int mode)139*22028508SToomas Soome open(const char *fname, int mode)
140*22028508SToomas Soome {
141*22028508SToomas Soome 	struct fs_ops *fs;
142*22028508SToomas Soome 	struct open_file *f;
143*22028508SToomas Soome 	int fd, i, error, besterror;
144*22028508SToomas Soome 	const char *file;
145*22028508SToomas Soome 
146*22028508SToomas Soome 	if ((fd = o_gethandle(&f)) == -1) {
147*22028508SToomas Soome 		errno = EMFILE;
148*22028508SToomas Soome 		return (-1);
149*22028508SToomas Soome 	}
150*22028508SToomas Soome 
151*22028508SToomas Soome 	f->f_flags = mode + 1;
152*22028508SToomas Soome 	f->f_dev = NULL;
153*22028508SToomas Soome 	f->f_ops = NULL;
154*22028508SToomas Soome 	f->f_offset = 0;
155*22028508SToomas Soome 	f->f_devdata = NULL;
156*22028508SToomas Soome 	file = NULL;
157*22028508SToomas Soome 
158*22028508SToomas Soome 	if (exclusive_file_system != NULL) {
159*22028508SToomas Soome 		fs = exclusive_file_system;
160*22028508SToomas Soome 		error = (fs->fo_open)(fname, f);
161*22028508SToomas Soome 		if (error == 0)
162*22028508SToomas Soome 			goto ok;
163*22028508SToomas Soome 		goto err;
164*22028508SToomas Soome 	}
165*22028508SToomas Soome 
166*22028508SToomas Soome 	error = devopen(f, fname, &file);
167*22028508SToomas Soome 	if (error ||
168*22028508SToomas Soome 	    (((f->f_flags & F_NODEV) == 0) && f->f_dev == NULL))
169*22028508SToomas Soome 		goto err;
170*22028508SToomas Soome 
171*22028508SToomas Soome 	/* see if we opened a raw device; otherwise, 'file' is the file name. */
172*22028508SToomas Soome 	if (file == NULL || *file == '\0') {
173*22028508SToomas Soome 		f->f_flags |= F_RAW;
174*22028508SToomas Soome 		f->f_rabuf = NULL;
175*22028508SToomas Soome 		return (fd);
176*22028508SToomas Soome 	}
177*22028508SToomas Soome 
178*22028508SToomas Soome 	/* pass file name to the different filesystem open routines */
179*22028508SToomas Soome 	besterror = ENOENT;
180*22028508SToomas Soome 	for (i = 0; file_system[i] != NULL; i++) {
181*22028508SToomas Soome 		fs = file_system[i];
182*22028508SToomas Soome 		error = (fs->fo_open)(file, f);
183*22028508SToomas Soome 		if (error == 0)
184*22028508SToomas Soome 			goto ok;
185*22028508SToomas Soome 		if (error != EINVAL)
186*22028508SToomas Soome 			besterror = error;
187*22028508SToomas Soome 	}
188*22028508SToomas Soome 	error = besterror;
189*22028508SToomas Soome 
190*22028508SToomas Soome 	if ((f->f_flags & F_NODEV) == 0 && f->f_dev != NULL)
191*22028508SToomas Soome 		f->f_dev->dv_close(f);
192*22028508SToomas Soome 	if (error)
193*22028508SToomas Soome 		devclose(f);
194*22028508SToomas Soome 
195*22028508SToomas Soome err:
196*22028508SToomas Soome 	f->f_flags = 0;
197*22028508SToomas Soome 	errno = error;
198*22028508SToomas Soome 	return (-1);
199*22028508SToomas Soome 
200*22028508SToomas Soome ok:
201*22028508SToomas Soome 	f->f_ops = fs;
202*22028508SToomas Soome 	o_rainit(f);
203*22028508SToomas Soome 	return (fd);
204*22028508SToomas Soome }
205