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