Changeset View
Standalone View
stand/libsa/mount.c
- This file was added.
/*- | |||||
* Copyright 2021 Toomas Soome <tsoome@me.com> | |||||
* | |||||
* 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. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <stand.h> | |||||
#include <sys/queue.h> | |||||
/* | |||||
* While setting "currdev" environment variable, alse "mount" the | |||||
* new root file system. This is done to hold disk device open | |||||
* in between file accesses, and thus preserve block cache for | |||||
* this device. Additionally, this allows us to optimize filesystem | |||||
* access by sharing filesystem metadata (like superblock). | |||||
*/ | |||||
typedef STAILQ_HEAD(mnt_info_list, mnt_info) mnt_info_list_t; | |||||
typedef struct mnt_info { | |||||
STAILQ_ENTRY(mnt_info) mnt_link; /* link in mount list */ | |||||
const struct fs_ops *mnt_fs; | |||||
char *mnt_dev; | |||||
char *mnt_path; | |||||
unsigned mnt_refcount; | |||||
void *mnt_data; /* Private state */ | |||||
} mnt_info_t; | |||||
/* list of mounted filesystems. */ | |||||
static mnt_info_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list); | |||||
static void | |||||
free_mnt(mnt_info_t *mnt) | |||||
{ | |||||
free(mnt->mnt_dev); | |||||
free(mnt->mnt_path); | |||||
free(mnt); | |||||
} | |||||
static int | |||||
add_mnt_info(struct fs_ops *fs, const char *dev, const char *path, void *data) | |||||
{ | |||||
mnt_info_t *mnt; | |||||
mnt = malloc(sizeof(*mnt)); | |||||
if (mnt == NULL) | |||||
return (ENOMEM); | |||||
mnt->mnt_fs = fs; | |||||
mnt->mnt_dev = strdup(dev); | |||||
mnt->mnt_path = strdup(path); | |||||
mnt->mnt_data = data; | |||||
mnt->mnt_refcount = 1; | |||||
if (mnt->mnt_dev == NULL || mnt->mnt_path == NULL) { | |||||
free_mnt(mnt); | |||||
return (ENOMEM); | |||||
} | |||||
STAILQ_INSERT_TAIL(&mnt_list, mnt, mnt_link); | |||||
return (0); | |||||
} | |||||
static void | |||||
delete_mnt_info(mnt_info_t *mnt) | |||||
{ | |||||
STAILQ_REMOVE(&mnt_list, mnt, mnt_info, mnt_link); | |||||
free_mnt(mnt); | |||||
} | |||||
int | |||||
mount(const char *dev, const char *path, int flags __unused, void *data) | |||||
{ | |||||
mnt_info_t *mnt; | |||||
int rc = -1; | |||||
/* Is it already mounted? */ | |||||
STAILQ_FOREACH(mnt, &mnt_list, mnt_link) { | |||||
if (strcmp(dev, mnt->mnt_dev) == 0 && | |||||
strcmp(path, mnt->mnt_path) == 0) { | |||||
imp: I would think we'd want to keep a mount count and only free things up in umount if that count… | |||||
mnt->mnt_refcount++; | |||||
return (0); | |||||
} | |||||
} | |||||
for (int i = 0; file_system[i] != NULL; i++) { | |||||
struct fs_ops *fs; | |||||
Done Inline ActionsThe idiomatic way is to say if (fo->fo_mount == NULL) continue so that we can move the indentation back a level. imp: The idiomatic way is to say if (fo->fo_mount == NULL) continue so that we can move the… | |||||
fs = file_system[i]; | |||||
if (fs->fo_mount == NULL) | |||||
continue; | |||||
if (fs->fo_mount(dev, path, &data) != 0) | |||||
Done Inline Actionsdo we need to call fo_umount() here if add_mnt_info fails? imp: do we need to call fo_umount() here if add_mnt_info fails?
| |||||
continue; | |||||
rc = add_mnt_info(fs, dev, path, data); | |||||
if (rc != 0 && mnt->mnt_fs->fo_unmount != NULL) { | |||||
printf("failed to mount %s: %s\n", dev, | |||||
strerror(rc)); | |||||
(void)mnt->mnt_fs->fo_unmount(dev, data); | |||||
Done Inline ActionsWhy do we always succeed, even when we've failed to mount it? imp: Why do we always succeed, even when we've failed to mount it?
| |||||
Done Inline ActionsIt is temporary code there (I'll mark it with comment). In this version, the fo_mount/fo_unmount callbacks are only added for ufs, we definitely want them for most/all disk based file systems, but *maybe* not for some virtual file systems (pkgfs?). Therefore it just needs some work.... tsoome: It is temporary code there (I'll mark it with comment). In this version, the… | |||||
} | |||||
break; | |||||
} | |||||
/* | |||||
* if rc is -1, it means we have no file system with fo_mount() | |||||
* callback, or all fo_mount() calls failed. As long as we | |||||
* have missing fo_mount() callbacks, we allow mount() to return 0. | |||||
*/ | |||||
if (rc == -1) | |||||
rc = 0; | |||||
Done Inline ActionsWe're not checking the return value here. ufs_umount doesn't return anything but 0, but others might in the future. imp: We're not checking the return value here. ufs_umount doesn't return anything but 0, but others… | |||||
return (rc); | |||||
} | |||||
int | |||||
unmount(const char *dev, int flags __unused) | |||||
{ | |||||
mnt_info_t *mnt; | |||||
int rv; | |||||
rv = 0; | |||||
STAILQ_FOREACH(mnt, &mnt_list, mnt_link) { | |||||
if (strcmp(dev, mnt->mnt_dev) == 0) { | |||||
if (mnt->mnt_refcount > 1) { | |||||
mnt->mnt_refcount--; | |||||
Done Inline Actionsclassically, I'd expect the ref count to start at 1, not 0, so this is confusing to me. The code is technically correct, but I worry it's more fragile since it uses an unfamiliar pattern. imp: classically, I'd expect the ref count to start at 1, not 0, so this is confusing to me. The… | |||||
break; | |||||
} | |||||
if (mnt->mnt_fs->fo_unmount != NULL) | |||||
rv = mnt->mnt_fs->fo_unmount(dev, | |||||
mnt->mnt_data); | |||||
delete_mnt_info(mnt); | |||||
break; | |||||
} | |||||
} | |||||
if (rv != 0) | |||||
printf("failed to unmount %s: %d\n", dev, rv); | |||||
return (0); | |||||
} | |||||
Done Inline ActionsI think this should live in common/main.c rather than here because it's /boot/loader specific and libsa isn't only for boot loader. imp: I think this should live in common/main.c rather than here because it's /boot/loader specific… |
I would think we'd want to keep a mount count and only free things up in umount if that count, after decrementing, falls to 0. That would avoid cases where we umount twice because we mounted twice.
Alternatively, maybe we should make mounting a mounted filesystem something to warn about. It just feels weird to succeed here there's multiple mounts, but then at least whine in the unmount path.