/*-1* Copyright 2021 Toomas Soome <[email protected]>2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425#include <stand.h>26#include <sys/queue.h>2728/*29* While setting "currdev" environment variable, alse "mount" the30* new root file system. This is done to hold disk device open31* in between file accesses, and thus preserve block cache for32* this device. Additionally, this allows us to optimize filesystem33* access by sharing filesystem metadata (like superblock).34*/3536typedef STAILQ_HEAD(mnt_info_list, mnt_info) mnt_info_list_t;3738typedef struct mnt_info {39STAILQ_ENTRY(mnt_info) mnt_link; /* link in mount list */40const struct fs_ops *mnt_fs;41char *mnt_dev;42char *mnt_path;43unsigned mnt_refcount;44void *mnt_data; /* Private state */45} mnt_info_t;4647/* list of mounted filesystems. */48static mnt_info_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);4950static void51free_mnt(mnt_info_t *mnt)52{53free(mnt->mnt_dev);54free(mnt->mnt_path);55free(mnt);56}5758static int59add_mnt_info(struct fs_ops *fs, const char *dev, const char *path, void *data)60{61mnt_info_t *mnt;6263mnt = malloc(sizeof(*mnt));64if (mnt == NULL)65return (ENOMEM);6667mnt->mnt_fs = fs;68mnt->mnt_dev = strdup(dev);69mnt->mnt_path = strdup(path);70mnt->mnt_data = data;71mnt->mnt_refcount = 1;7273if (mnt->mnt_dev == NULL || mnt->mnt_path == NULL) {74free_mnt(mnt);75return (ENOMEM);76}77STAILQ_INSERT_TAIL(&mnt_list, mnt, mnt_link);78return (0);79}8081static void82delete_mnt_info(mnt_info_t *mnt)83{84STAILQ_REMOVE(&mnt_list, mnt, mnt_info, mnt_link);85free_mnt(mnt);86}8788int89mount(const char *dev, const char *path, int flags __unused, void *data)90{91mnt_info_t *mnt;92int rc = -1;9394/* Is it already mounted? */95STAILQ_FOREACH(mnt, &mnt_list, mnt_link) {96if (strcmp(dev, mnt->mnt_dev) == 0 &&97strcmp(path, mnt->mnt_path) == 0) {98mnt->mnt_refcount++;99return (0);100}101}102103for (int i = 0; file_system[i] != NULL; i++) {104struct fs_ops *fs;105106fs = file_system[i];107if (fs->fo_mount == NULL)108continue;109DEBUG_PRINTF(1,("%s: fs=%s path=%s\n",110__func__, fs->fs_name, path));111if (is_tftp())112break;113if (fs->fo_mount(dev, path, &data) != 0)114continue;115116rc = add_mnt_info(fs, dev, path, data);117if (rc != 0 && mnt->mnt_fs->fo_unmount != NULL) {118printf("failed to mount %s: %s\n", dev,119strerror(rc));120(void)mnt->mnt_fs->fo_unmount(dev, data);121}122break;123}124125126/*127* if rc is -1, it means we have no file system with fo_mount()128* callback, or all fo_mount() calls failed. As long as we129* have missing fo_mount() callbacks, we allow mount() to return 0.130*/131if (rc == -1)132rc = 0;133134return (rc);135}136137int138unmount(const char *dev, int flags __unused)139{140mnt_info_t *mnt;141int rv;142143rv = 0;144STAILQ_FOREACH(mnt, &mnt_list, mnt_link) {145if (strcmp(dev, mnt->mnt_dev) == 0) {146if (mnt->mnt_refcount > 1) {147mnt->mnt_refcount--;148break;149}150151if (mnt->mnt_fs->fo_unmount != NULL)152rv = mnt->mnt_fs->fo_unmount(dev,153mnt->mnt_data);154delete_mnt_info(mnt);155break;156}157}158159if (rv != 0)160printf("failed to unmount %s: %d\n", dev, rv);161return (0);162}163164165