2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <libintl.h>
2N/A#include <errno.h>
2N/A#include <strings.h>
2N/A#include <unistd.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/param.h>
2N/A#include <sys/fstyp.h>
2N/A#include <fcntl.h>
2N/A#include <errno.h>
2N/A#include <dirent.h>
2N/A#include <dlfcn.h>
2N/A#include <link.h>
2N/A#include <libnvpair.h>
2N/A#include <libfstyp.h>
2N/A#include <libfstyp_module.h>
2N/A
2N/A/* default module directory */
2N/Aconst char *default_libfs_dir = "/usr/lib/fs";
2N/A
2N/A#define FSTYP_VERSION 1
2N/A
2N/A#ifndef TEXT_DOMAIN
2N/A#define TEXT_DOMAIN "SYS_TEST"
2N/A#endif
2N/A
2N/Atypedef struct fstyp_ops {
2N/A int (*fstyp_init)(int fd, off64_t offset,
2N/A fstyp_mod_handle_t *handle);
2N/A void (*fstyp_fini)(fstyp_mod_handle_t handle);
2N/A int (*fstyp_ident)(fstyp_mod_handle_t handle);
2N/A int (*fstyp_get_attr)(fstyp_mod_handle_t handle,
2N/A nvlist_t **attr);
2N/A int (*fstyp_dump)(fstyp_mod_handle_t handle,
2N/A FILE *fout, FILE *ferr);
2N/A} fstyp_ops_t;
2N/A
2N/Atypedef struct fstyp_module {
2N/A struct fstyp_module *next;
2N/A char fsname[FSTYPSZ + 1];
2N/A char *pathname; /* absolute module pathname */
2N/A void *dl_handle; /* can be NULL if not loaded */
2N/A fstyp_ops_t ops;
2N/A fstyp_mod_handle_t mod_handle;
2N/A} fstyp_module_t;
2N/A
2N/Astruct fstyp_handle {
2N/A char *libfs_dir; /* directory to look for modules */
2N/A char *module_dir; /* specific module directory */
2N/A fstyp_module_t *modules; /* list of modules */
2N/A fstyp_module_t *modules_tail; /* last module in the list */
2N/A fstyp_module_t *ident; /* identified module */
2N/A int fd;
2N/A off64_t offset;
2N/A long name_max;
2N/A};
2N/A
2N/A#define NELEM(a) sizeof (a) / sizeof (*(a))
2N/A
2N/A/* local functions */
2N/Astatic int fstyp_ident_all(struct fstyp_handle *h, const char **ident);
2N/Astatic int fstyp_ident_one(struct fstyp_handle *h, const char *fsname,
2N/A const char **ident);
2N/Astatic fstyp_module_t *fstyp_find_module_by_name(struct fstyp_handle *h,
2N/A const char *fsname);
2N/Astatic int fstyp_init_module(struct fstyp_handle *h,
2N/A char *mdir, char *fsname, fstyp_module_t **mpp);
2N/Astatic void fstyp_fini_module(struct fstyp_handle *h,
2N/A fstyp_module_t *mp);
2N/Astatic int fstyp_init_all_modules(struct fstyp_handle *h);
2N/Astatic void fstyp_fini_all_modules(struct fstyp_handle *h);
2N/Astatic int fstyp_load_module(struct fstyp_handle *h,
2N/A fstyp_module_t *mp);
2N/Astatic void fstyp_unload_module(struct fstyp_handle *h,
2N/A fstyp_module_t *);
2N/A
2N/A/*
2N/A * Locate and initialize all modules.
2N/A * If 'module_dir' is specified, only initialize module from this dir.
2N/A */
2N/Aint
2N/Afstyp_init(int fd, off64_t offset, char *module_dir, fstyp_handle_t *handle)
2N/A{
2N/A struct fstyp_handle *h;
2N/A int error;
2N/A
2N/A if ((h = calloc(1, sizeof (struct fstyp_handle))) == NULL) {
2N/A return (FSTYP_ERR_NOMEM);
2N/A }
2N/A if ((module_dir != NULL) &&
2N/A ((h->module_dir = strdup(module_dir)) == NULL)) {
2N/A free(h);
2N/A return (FSTYP_ERR_NOMEM);
2N/A }
2N/A
2N/A h->fd = fd;
2N/A h->offset = offset;
2N/A h->libfs_dir = (char *)default_libfs_dir;
2N/A
2N/A if ((h->name_max = pathconf(h->libfs_dir, _PC_NAME_MAX)) < 0) {
2N/A h->name_max = MAXNAMELEN;
2N/A }
2N/A h->name_max++;
2N/A
2N/A if (h->module_dir == NULL) {
2N/A error = fstyp_init_all_modules(h);
2N/A } else {
2N/A error = fstyp_init_module(h, h->module_dir, "", NULL);
2N/A }
2N/A if (error != 0) {
2N/A fstyp_fini(h);
2N/A return (error);
2N/A }
2N/A
2N/A *handle = h;
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Afstyp_fini(struct fstyp_handle *h)
2N/A{
2N/A if (h != NULL) {
2N/A fstyp_fini_all_modules(h);
2N/A if (h->module_dir != NULL) {
2N/A free(h->module_dir);
2N/A }
2N/A free(h);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Identify the filesystem, return result in 'ident'.
2N/A * If 'fsname' is specified, only attempt that filesystem.
2N/A */
2N/Aint
2N/Afstyp_ident(struct fstyp_handle *h, const char *fsname, const char **ident)
2N/A{
2N/A if (fsname == NULL) {
2N/A return (fstyp_ident_all(h, ident));
2N/A } else {
2N/A return (fstyp_ident_one(h, fsname, ident));
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * use all modules for identification
2N/A */
2N/Astatic int
2N/Afstyp_ident_all(struct fstyp_handle *h, const char **ident)
2N/A{
2N/A fstyp_module_t *mp;
2N/A
2N/A if (h->ident != NULL) {
2N/A *ident = &h->ident->fsname[0];
2N/A return (0);
2N/A }
2N/A
2N/A for (mp = h->modules; mp != NULL; mp = mp->next) {
2N/A if ((fstyp_load_module(h, mp) == 0) &&
2N/A (mp->ops.fstyp_ident(mp->mod_handle) == 0)) {
2N/A if (h->ident != NULL) {
2N/A h->ident = NULL;
2N/A *ident = NULL;
2N/A return (FSTYP_ERR_MULT_MATCH);
2N/A } else {
2N/A h->ident = mp;
2N/A *ident = &mp->fsname[0];
2N/A return (0);
2N/A }
2N/A }
2N/A }
2N/A return (FSTYP_ERR_NO_MATCH);
2N/A}
2N/A
2N/A/*
2N/A * use only the specified module for identification
2N/A */
2N/Astatic int
2N/Afstyp_ident_one(struct fstyp_handle *h, const char *fsname, const char **ident)
2N/A{
2N/A fstyp_module_t *mp;
2N/A int error = FSTYP_ERR_NO_MATCH;
2N/A
2N/A if (h->ident != NULL) {
2N/A if (strcmp(h->ident->fsname, fsname) == 0) {
2N/A *ident = (char *)fsname;
2N/A return (0);
2N/A } else {
2N/A return (FSTYP_ERR_NO_MATCH);
2N/A }
2N/A }
2N/A
2N/A if (strlen(fsname) > FSTYPSZ) {
2N/A return (FSTYP_ERR_NAME_TOO_LONG);
2N/A }
2N/A if (h->module_dir == NULL) {
2N/A mp = fstyp_find_module_by_name(h, fsname);
2N/A } else {
2N/A mp = h->modules;
2N/A }
2N/A if (mp == NULL) {
2N/A return (FSTYP_ERR_MOD_NOT_FOUND);
2N/A }
2N/A
2N/A if (((error = fstyp_load_module(h, mp)) == 0) &&
2N/A ((error = mp->ops.fstyp_ident(mp->mod_handle)) == 0)) {
2N/A h->ident = mp;
2N/A *ident = (char *)fsname;
2N/A return (0);
2N/A }
2N/A return (error);
2N/A}
2N/A
2N/A/*
2N/A * Get the list of fs attributes.
2N/A */
2N/Aint
2N/Afstyp_get_attr(struct fstyp_handle *h, nvlist_t **attr)
2N/A{
2N/A fstyp_module_t *mp = h->ident;
2N/A
2N/A if (mp == NULL) {
2N/A return (FSTYP_ERR_NO_MATCH);
2N/A }
2N/A
2N/A return (mp->ops.fstyp_get_attr(mp->mod_handle, attr));
2N/A}
2N/A
2N/A/*
2N/A * Dump free-form filesystem information.
2N/A */
2N/Aint
2N/Afstyp_dump(struct fstyp_handle *h, FILE *fout, FILE *ferr)
2N/A{
2N/A fstyp_module_t *mp = h->ident;
2N/A
2N/A if (mp == NULL) {
2N/A return (FSTYP_ERR_NO_MATCH);
2N/A }
2N/A
2N/A if (mp->ops.fstyp_dump == NULL) {
2N/A return (FSTYP_ERR_NOP);
2N/A }
2N/A
2N/A return (mp->ops.fstyp_dump(mp->mod_handle, fout, ferr));
2N/A}
2N/A
2N/A/* ARGSUSED */
2N/Aconst char *
2N/Afstyp_strerror(struct fstyp_handle *h, int error)
2N/A{
2N/A char *str;
2N/A
2N/A switch (error) {
2N/A case FSTYP_ERR_OK:
2N/A str = dgettext(TEXT_DOMAIN, "success");
2N/A break;
2N/A case FSTYP_ERR_NO_MATCH:
2N/A str = dgettext(TEXT_DOMAIN, "no matches");
2N/A break;
2N/A case FSTYP_ERR_MULT_MATCH:
2N/A str = dgettext(TEXT_DOMAIN, "multiple matches");
2N/A break;
2N/A case FSTYP_ERR_HANDLE:
2N/A str = dgettext(TEXT_DOMAIN, "invalid handle");
2N/A break;
2N/A case FSTYP_ERR_OFFSET:
2N/A str = dgettext(TEXT_DOMAIN, "invalid or unsupported offset");
2N/A break;
2N/A case FSTYP_ERR_NO_PARTITION:
2N/A str = dgettext(TEXT_DOMAIN, "partition not found");
2N/A break;
2N/A case FSTYP_ERR_NOP:
2N/A str = dgettext(TEXT_DOMAIN, "no such operation");
2N/A break;
2N/A case FSTYP_ERR_DEV_OPEN:
2N/A str = dgettext(TEXT_DOMAIN, "cannot open device");
2N/A break;
2N/A case FSTYP_ERR_IO:
2N/A str = dgettext(TEXT_DOMAIN, "i/o error");
2N/A break;
2N/A case FSTYP_ERR_NOMEM:
2N/A str = dgettext(TEXT_DOMAIN, "out of memory");
2N/A break;
2N/A case FSTYP_ERR_MOD_NOT_FOUND:
2N/A str = dgettext(TEXT_DOMAIN, "module not found");
2N/A break;
2N/A case FSTYP_ERR_MOD_DIR_OPEN:
2N/A str = dgettext(TEXT_DOMAIN, "cannot open module directory");
2N/A break;
2N/A case FSTYP_ERR_MOD_OPEN:
2N/A str = dgettext(TEXT_DOMAIN, "cannot open module");
2N/A break;
2N/A case FSTYP_ERR_MOD_VERSION:
2N/A str = dgettext(TEXT_DOMAIN, "invalid module version");
2N/A break;
2N/A case FSTYP_ERR_MOD_INVALID:
2N/A str = dgettext(TEXT_DOMAIN, "invalid module");
2N/A break;
2N/A case FSTYP_ERR_NAME_TOO_LONG:
2N/A str = dgettext(TEXT_DOMAIN, "filesystem name too long");
2N/A break;
2N/A default:
2N/A str = dgettext(TEXT_DOMAIN, "undefined error");
2N/A break;
2N/A }
2N/A
2N/A return (str);
2N/A}
2N/A
2N/A
2N/Astatic fstyp_module_t *
2N/Afstyp_find_module_by_name(struct fstyp_handle *h, const char *fsname)
2N/A{
2N/A fstyp_module_t *mp;
2N/A
2N/A for (mp = h->modules; mp != NULL; mp = mp->next) {
2N/A if (strcmp(mp->fsname, fsname) == 0) {
2N/A return (mp);
2N/A }
2N/A }
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Allocate and initialize module structure. Do not load just yet.
2N/A * A pointer to the existing module is returned, if such is found.
2N/A */
2N/Astatic int
2N/Afstyp_init_module(struct fstyp_handle *h, char *mdir, char *fsname,
2N/A fstyp_module_t **mpp)
2N/A{
2N/A char *pathname;
2N/A struct stat sb;
2N/A fstyp_module_t *mp;
2N/A
2N/A /* if it's already inited, just return the pointer */
2N/A if ((mp = fstyp_find_module_by_name(h, fsname)) != NULL) {
2N/A if (mpp != NULL) {
2N/A *mpp = mp;
2N/A }
2N/A return (0);
2N/A }
2N/A
2N/A /* allocate pathname buffer */
2N/A if ((pathname = calloc(1, h->name_max)) == NULL) {
2N/A return (FSTYP_ERR_NOMEM);
2N/A }
2N/A
2N/A /* locate module */
2N/A (void) snprintf(pathname, h->name_max, "%s/fstyp.so.%d", mdir,
2N/A FSTYP_VERSION);
2N/A if (stat(pathname, &sb) < 0) {
2N/A return (FSTYP_ERR_MOD_NOT_FOUND);
2N/A }
2N/A
2N/A if ((mp = calloc(1, sizeof (fstyp_module_t))) == NULL) {
2N/A free(pathname);
2N/A return (FSTYP_ERR_NOMEM);
2N/A }
2N/A
2N/A mp->pathname = pathname;
2N/A (void) strlcpy(mp->fsname, fsname, sizeof (mp->fsname));
2N/A
2N/A /* append to list */
2N/A if (h->modules_tail == NULL) {
2N/A h->modules = h->modules_tail = mp;
2N/A } else {
2N/A h->modules_tail->next = mp;
2N/A h->modules_tail = mp;
2N/A }
2N/A
2N/A if (mpp != NULL) {
2N/A *mpp = mp;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Free module resources. NOTE: this does not update the module list.
2N/A */
2N/Astatic void
2N/Afstyp_fini_module(struct fstyp_handle *h, fstyp_module_t *mp)
2N/A{
2N/A if (h->ident == mp) {
2N/A h->ident = NULL;
2N/A }
2N/A fstyp_unload_module(h, mp);
2N/A if (mp->pathname != NULL) {
2N/A free(mp->pathname);
2N/A }
2N/A free(mp);
2N/A}
2N/A
2N/A/*
2N/A * Look for .so's and save them in the list.
2N/A */
2N/Astatic int
2N/Afstyp_init_all_modules(struct fstyp_handle *h)
2N/A{
2N/A char *mdir;
2N/A DIR *dirp;
2N/A struct dirent *dp_mem, *dp;
2N/A
2N/A if ((mdir = calloc(1, h->name_max)) == NULL) {
2N/A return (FSTYP_ERR_NOMEM);
2N/A }
2N/A dp = dp_mem = calloc(1, sizeof (struct dirent) + h->name_max + 1);
2N/A if (dp == NULL) {
2N/A return (FSTYP_ERR_NOMEM);
2N/A }
2N/A if ((dirp = opendir(h->libfs_dir)) == NULL) {
2N/A free(mdir);
2N/A free(dp_mem);
2N/A return (FSTYP_ERR_MOD_DIR_OPEN);
2N/A }
2N/A
2N/A while ((readdir_r(dirp, dp, &dp) == 0) && (dp != NULL)) {
2N/A if (dp->d_name[0] == '.') {
2N/A continue;
2N/A }
2N/A (void) snprintf(mdir, h->name_max,
2N/A "%s/%s", h->libfs_dir, dp->d_name);
2N/A (void) fstyp_init_module(h, mdir, dp->d_name, NULL);
2N/A }
2N/A
2N/A free(mdir);
2N/A free(dp_mem);
2N/A (void) closedir(dirp);
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/Afstyp_fini_all_modules(struct fstyp_handle *h)
2N/A{
2N/A fstyp_module_t *mp, *mp_next;
2N/A
2N/A for (mp = h->modules; mp != NULL; mp = mp_next) {
2N/A mp_next = mp->next;
2N/A fstyp_fini_module(h, mp);
2N/A }
2N/A h->modules = h->modules_tail = h->ident = NULL;
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Load the .so module.
2N/A */
2N/Astatic int
2N/Afstyp_load_module(struct fstyp_handle *h, fstyp_module_t *mp)
2N/A{
2N/A int error;
2N/A
2N/A if (mp->dl_handle != NULL) {
2N/A return (0);
2N/A }
2N/A
2N/A if ((mp->dl_handle = dlopen(mp->pathname, RTLD_LAZY)) == NULL) {
2N/A return (FSTYP_ERR_MOD_OPEN);
2N/A }
2N/A
2N/A mp->ops.fstyp_init = (int (*)(int, off64_t, fstyp_mod_handle_t *))
2N/A dlsym(mp->dl_handle, "fstyp_mod_init");
2N/A mp->ops.fstyp_fini = (void (*)(fstyp_mod_handle_t))
2N/A dlsym(mp->dl_handle, "fstyp_mod_fini");
2N/A mp->ops.fstyp_ident = (int (*)(fstyp_mod_handle_t))
2N/A dlsym(mp->dl_handle, "fstyp_mod_ident");
2N/A mp->ops.fstyp_get_attr = (int (*)(fstyp_mod_handle_t, nvlist_t **))
2N/A dlsym(mp->dl_handle, "fstyp_mod_get_attr");
2N/A mp->ops.fstyp_dump = (int (*)(fstyp_mod_handle_t, FILE *, FILE *))
2N/A dlsym(mp->dl_handle, "fstyp_mod_dump");
2N/A
2N/A if (((mp->ops.fstyp_init) == NULL) ||
2N/A ((mp->ops.fstyp_fini) == NULL) ||
2N/A ((mp->ops.fstyp_ident) == NULL) ||
2N/A ((mp->ops.fstyp_get_attr) == NULL)) {
2N/A fstyp_unload_module(h, mp);
2N/A return (FSTYP_ERR_MOD_INVALID);
2N/A }
2N/A
2N/A error = mp->ops.fstyp_init(h->fd, h->offset, &mp->mod_handle);
2N/A if (error != 0) {
2N/A fstyp_unload_module(h, mp);
2N/A return (error);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Afstyp_unload_module(struct fstyp_handle *h, fstyp_module_t *mp)
2N/A{
2N/A if (mp->mod_handle != NULL) {
2N/A mp->ops.fstyp_fini(mp->mod_handle);
2N/A mp->mod_handle = NULL;
2N/A }
2N/A if (mp->dl_handle != NULL) {
2N/A (void) dlclose(mp->dl_handle);
2N/A mp->dl_handle = NULL;
2N/A }
2N/A}