1998-08-20 08:19:55 +00:00
|
|
|
/* $NetBSD: open.c,v 1.16 1997/01/28 09:41:03 pk Exp $ */
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* The Mach Operating System project at Carnegie-Mellon University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1998-08-20 08:19:55 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Author: Alessandro Forin
|
2018-06-14 08:58:10 +00:00
|
|
|
*
|
1998-08-20 08:19:55 +00:00
|
|
|
* Permission to use, copy, modify and distribute this software and its
|
|
|
|
* documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
2018-06-14 08:58:10 +00:00
|
|
|
*
|
1998-08-20 08:19:55 +00:00
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
|
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
|
|
|
|
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
2018-06-14 08:58:10 +00:00
|
|
|
*
|
1998-08-20 08:19:55 +00:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
2018-06-14 08:58:10 +00:00
|
|
|
*
|
1998-08-20 08:19:55 +00:00
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
2018-06-14 08:58:10 +00:00
|
|
|
*
|
1998-08-20 08:19:55 +00:00
|
|
|
* any improvements or extensions that they make and grant Carnegie the
|
|
|
|
* rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
|
Provide a means for loaders to control which file system to use. This
to counteract the default behaviour of always trying each and every
file system until one succeeds, or the open fails. The problem with the
loader is that we've implemented features based on this behavior. The
handling of compressed files is a good example of this. However, it is
in general highly undesirable to not have a one-time probe (or taste
in the geom lingo), followed by something similar to a mount whenever
we (first) read from a device. Everytime we go to the same device, we
can reasonably assume it (still) has the same file system. For file
systems that need to do far more that a trivial read of a super block,
not having something similar to a mount operation is disastrous from
a performance (and thus usability) perspective.
But, again, since we've implemented features based on this stateless
approach, things can get complicated quickly if and when we want to
change this. And yet, we sometimes do need stateful behaviour.
For this reason, this change simply introduces exclusive_file_system.
When set to the fsops of the file system to use, the open call will
only try this file system. Setting it to NULL restores the default
behaviour. It's a low-cost (low-brow?) approach to provide enough
control without re-implementing the guts of the loader.
A good example of when this is useful is when we're trying to load
files out of a container (say, a software packaga) that itself lives
on a file system or is fetched over the network. While opening the
container can be done in the normal stateless manner, once it is
opened, subsequent opens should only consider the container.
Obtained from: Juniper Networks, Inc.
2014-07-30 16:08:16 +00:00
|
|
|
struct fs_ops *exclusive_file_system;
|
|
|
|
|
2021-07-31 08:09:48 +00:00
|
|
|
/*
|
|
|
|
* Open file list. The current implementation and assumption is,
|
|
|
|
* we only remove entries from tail and we only add new entries to tail.
|
|
|
|
* This decision is to keep file id management simple - we get list
|
|
|
|
* entries ordered by continiously growing f_id field.
|
|
|
|
* If we do have multiple files open and we do close file not from tail,
|
|
|
|
* this entry will be marked unused. open() will reuse unused entry, or
|
|
|
|
* close will free all unused tail entries.
|
|
|
|
*
|
|
|
|
* Only case we expect open file list to grow long, is with zfs pools with
|
|
|
|
* many disks.
|
|
|
|
*/
|
|
|
|
file_list_t files = TAILQ_HEAD_INITIALIZER(files);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk file list and return pointer to open_file structure.
|
|
|
|
* if fd is < 0, return first unused open_file.
|
|
|
|
*/
|
|
|
|
struct open_file *
|
|
|
|
fd2open_file(int fd)
|
|
|
|
{
|
|
|
|
struct open_file *f;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(f, &files, f_link) {
|
|
|
|
if (fd >= 0) {
|
|
|
|
if (f->f_id == fd)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f->f_flags == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (f);
|
|
|
|
}
|
1998-08-20 08:19:55 +00:00
|
|
|
|
|
|
|
static int
|
2021-07-31 08:09:48 +00:00
|
|
|
o_gethandle(struct open_file **ptr)
|
1998-08-20 08:19:55 +00:00
|
|
|
{
|
2021-07-31 08:09:48 +00:00
|
|
|
struct open_file *f, *last;
|
|
|
|
|
|
|
|
/* Pick up unused entry */
|
|
|
|
f = fd2open_file(-1);
|
|
|
|
if (f != NULL) {
|
|
|
|
*ptr = f;
|
|
|
|
return (f->f_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new entry */
|
|
|
|
f = calloc(1, sizeof (*f));
|
|
|
|
if (f == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
last = TAILQ_LAST(&files, file_list);
|
|
|
|
if (last != NULL)
|
|
|
|
f->f_id = last->f_id + 1;
|
|
|
|
TAILQ_INSERT_TAIL(&files, f, f_link);
|
2018-06-14 08:58:10 +00:00
|
|
|
|
2021-07-31 08:09:48 +00:00
|
|
|
*ptr = f;
|
|
|
|
return (f->f_id);
|
1998-08-20 08:19:55 +00:00
|
|
|
}
|
|
|
|
|
2000-09-05 09:52:50 +00:00
|
|
|
static void
|
|
|
|
o_rainit(struct open_file *f)
|
|
|
|
{
|
2018-06-14 08:58:10 +00:00
|
|
|
f->f_rabuf = malloc(SOPEN_RASIZE);
|
|
|
|
f->f_ralen = 0;
|
|
|
|
f->f_raoffset = 0;
|
2000-09-05 09:52:50 +00:00
|
|
|
}
|
1998-08-20 08:19:55 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
open(const char *fname, int mode)
|
|
|
|
{
|
2018-06-14 08:58:10 +00:00
|
|
|
struct fs_ops *fs;
|
|
|
|
struct open_file *f;
|
|
|
|
int fd, i, error, besterror;
|
|
|
|
const char *file;
|
|
|
|
|
2021-05-31 00:01:25 +00:00
|
|
|
TSENTER();
|
|
|
|
|
2021-07-31 08:09:48 +00:00
|
|
|
if ((fd = o_gethandle(&f)) == -1) {
|
2018-06-14 08:58:10 +00:00
|
|
|
errno = EMFILE;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
f->f_flags = mode + 1;
|
2018-06-14 16:11:50 +00:00
|
|
|
f->f_dev = NULL;
|
|
|
|
f->f_ops = NULL;
|
2018-06-14 08:58:10 +00:00
|
|
|
f->f_offset = 0;
|
|
|
|
f->f_devdata = NULL;
|
2018-06-14 16:11:50 +00:00
|
|
|
file = NULL;
|
2018-06-14 08:58:10 +00:00
|
|
|
|
|
|
|
if (exclusive_file_system != NULL) {
|
|
|
|
fs = exclusive_file_system;
|
|
|
|
error = (fs->fo_open)(fname, f);
|
|
|
|
if (error == 0)
|
|
|
|
goto ok;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = devopen(f, fname, &file);
|
|
|
|
if (error ||
|
2018-06-14 16:11:50 +00:00
|
|
|
(((f->f_flags & F_NODEV) == 0) && f->f_dev == NULL))
|
2018-06-14 08:58:10 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* see if we opened a raw device; otherwise, 'file' is the file name. */
|
2018-06-14 16:11:50 +00:00
|
|
|
if (file == NULL || *file == '\0') {
|
2018-06-14 08:58:10 +00:00
|
|
|
f->f_flags |= F_RAW;
|
|
|
|
f->f_rabuf = NULL;
|
2021-05-31 00:01:25 +00:00
|
|
|
TSEXIT();
|
2018-06-14 08:58:10 +00:00
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass file name to the different filesystem open routines */
|
|
|
|
besterror = ENOENT;
|
|
|
|
for (i = 0; file_system[i] != NULL; i++) {
|
|
|
|
fs = file_system[i];
|
|
|
|
error = (fs->fo_open)(file, f);
|
|
|
|
if (error == 0)
|
|
|
|
goto ok;
|
|
|
|
if (error != EINVAL)
|
|
|
|
besterror = error;
|
|
|
|
}
|
|
|
|
error = besterror;
|
|
|
|
|
|
|
|
if ((f->f_flags & F_NODEV) == 0 && f->f_dev != NULL)
|
|
|
|
f->f_dev->dv_close(f);
|
|
|
|
if (error)
|
|
|
|
devclose(f);
|
|
|
|
|
|
|
|
err:
|
|
|
|
f->f_flags = 0;
|
|
|
|
errno = error;
|
2021-05-31 00:01:25 +00:00
|
|
|
TSEXIT();
|
2018-06-14 08:58:10 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
ok:
|
|
|
|
f->f_ops = fs;
|
|
|
|
o_rainit(f);
|
2021-05-31 00:01:25 +00:00
|
|
|
TSEXIT();
|
1998-08-20 08:19:55 +00:00
|
|
|
return (fd);
|
|
|
|
}
|